xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 8211)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * 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 /*
235756Seschrock  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens /*
28789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
29789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
30789Sahrens  * pool.
31789Sahrens  */
32789Sahrens 
33789Sahrens #include <sys/zfs_context.h>
341544Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens #include <sys/spa_impl.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/zio_checksum.h>
38789Sahrens #include <sys/zio_compress.h>
39789Sahrens #include <sys/dmu.h>
40789Sahrens #include <sys/dmu_tx.h>
41789Sahrens #include <sys/zap.h>
42789Sahrens #include <sys/zil.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
45789Sahrens #include <sys/uberblock_impl.h>
46789Sahrens #include <sys/txg.h>
47789Sahrens #include <sys/avl.h>
48789Sahrens #include <sys/dmu_traverse.h>
493912Slling #include <sys/dmu_objset.h>
50789Sahrens #include <sys/unique.h>
51789Sahrens #include <sys/dsl_pool.h>
523912Slling #include <sys/dsl_dataset.h>
53789Sahrens #include <sys/dsl_dir.h>
54789Sahrens #include <sys/dsl_prop.h>
553912Slling #include <sys/dsl_synctask.h>
56789Sahrens #include <sys/fs/zfs.h>
575450Sbrendan #include <sys/arc.h>
58789Sahrens #include <sys/callb.h>
593975Sek110237 #include <sys/systeminfo.h>
603975Sek110237 #include <sys/sunddi.h>
616423Sgw25295 #include <sys/spa_boot.h>
62789Sahrens 
635094Slling #include "zfs_prop.h"
645913Sperrin #include "zfs_comutil.h"
655094Slling 
667754SJeff.Bonwick@Sun.COM int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
677754SJeff.Bonwick@Sun.COM 	/*	ISSUE	INTR					*/
687754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_NULL	*/
697754SJeff.Bonwick@Sun.COM 	{	1,	8	},	/* ZIO_TYPE_READ	*/
707754SJeff.Bonwick@Sun.COM 	{	8,	1	},	/* ZIO_TYPE_WRITE	*/
717754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_FREE	*/
727754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_CLAIM	*/
737754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_IOCTL	*/
747754SJeff.Bonwick@Sun.COM };
752986Sek110237 
765094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
777214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
785094Slling 
795094Slling /*
805094Slling  * ==========================================================================
815094Slling  * SPA properties routines
825094Slling  * ==========================================================================
835094Slling  */
845094Slling 
855094Slling /*
865094Slling  * Add a (source=src, propname=propval) list to an nvlist.
875094Slling  */
885949Slling static void
895094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
905094Slling     uint64_t intval, zprop_source_t src)
915094Slling {
925094Slling 	const char *propname = zpool_prop_to_name(prop);
935094Slling 	nvlist_t *propval;
945949Slling 
955949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
965949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
975949Slling 
985949Slling 	if (strval != NULL)
995949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1005949Slling 	else
1015949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1025949Slling 
1035949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1045094Slling 	nvlist_free(propval);
1055094Slling }
1065094Slling 
1075094Slling /*
1085094Slling  * Get property values from the spa configuration.
1095094Slling  */
1105949Slling static void
1115094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1125094Slling {
1135094Slling 	uint64_t size = spa_get_space(spa);
1145094Slling 	uint64_t used = spa_get_alloc(spa);
1155094Slling 	uint64_t cap, version;
1165094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1176643Seschrock 	spa_config_dirent_t *dp;
1185094Slling 
1197754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1207754SJeff.Bonwick@Sun.COM 
1215094Slling 	/*
1225094Slling 	 * readonly properties
1235094Slling 	 */
1247754SJeff.Bonwick@Sun.COM 	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1255949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1265949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1275949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
1285094Slling 
1295094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
1305949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1315949Slling 
1325949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1335949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1345949Slling 	    spa->spa_root_vdev->vdev_state, src);
1355094Slling 
1365094Slling 	/*
1375094Slling 	 * settable properties that are not stored in the pool property object.
1385094Slling 	 */
1395094Slling 	version = spa_version(spa);
1405094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1415094Slling 		src = ZPROP_SRC_DEFAULT;
1425094Slling 	else
1435094Slling 		src = ZPROP_SRC_LOCAL;
1445949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1455949Slling 
1465949Slling 	if (spa->spa_root != NULL)
1475949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1485949Slling 		    0, ZPROP_SRC_LOCAL);
1495094Slling 
1506643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1516643Seschrock 		if (dp->scd_path == NULL) {
1525949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1536643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1546643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1555949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1566643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1575363Seschrock 		}
1585363Seschrock 	}
1595094Slling }
1605094Slling 
1615094Slling /*
1625094Slling  * Get zpool property values.
1635094Slling  */
1645094Slling int
1655094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1665094Slling {
1675094Slling 	zap_cursor_t zc;
1685094Slling 	zap_attribute_t za;
1695094Slling 	objset_t *mos = spa->spa_meta_objset;
1705094Slling 	int err;
1715094Slling 
1725949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1735094Slling 
1747754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
1757754SJeff.Bonwick@Sun.COM 
1765094Slling 	/*
1775094Slling 	 * Get properties from the spa config.
1785094Slling 	 */
1795949Slling 	spa_prop_get_config(spa, nvp);
1805094Slling 
1815094Slling 	/* If no pool property object, no more prop to get. */
1825094Slling 	if (spa->spa_pool_props_object == 0) {
1835094Slling 		mutex_exit(&spa->spa_props_lock);
1845094Slling 		return (0);
1855094Slling 	}
1865094Slling 
1875094Slling 	/*
1885094Slling 	 * Get properties from the MOS pool property object.
1895094Slling 	 */
1905094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1915094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1925094Slling 	    zap_cursor_advance(&zc)) {
1935094Slling 		uint64_t intval = 0;
1945094Slling 		char *strval = NULL;
1955094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
1965094Slling 		zpool_prop_t prop;
1975094Slling 
1985094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
1995094Slling 			continue;
2005094Slling 
2015094Slling 		switch (za.za_integer_length) {
2025094Slling 		case 8:
2035094Slling 			/* integer property */
2045094Slling 			if (za.za_first_integer !=
2055094Slling 			    zpool_prop_default_numeric(prop))
2065094Slling 				src = ZPROP_SRC_LOCAL;
2075094Slling 
2085094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2095094Slling 				dsl_pool_t *dp;
2105094Slling 				dsl_dataset_t *ds = NULL;
2115094Slling 
2125094Slling 				dp = spa_get_dsl(spa);
2135094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2146689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2156689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2165094Slling 					rw_exit(&dp->dp_config_rwlock);
2175094Slling 					break;
2185094Slling 				}
2195094Slling 
2205094Slling 				strval = kmem_alloc(
2215094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2225094Slling 				    KM_SLEEP);
2235094Slling 				dsl_dataset_name(ds, strval);
2246689Smaybee 				dsl_dataset_rele(ds, FTAG);
2255094Slling 				rw_exit(&dp->dp_config_rwlock);
2265094Slling 			} else {
2275094Slling 				strval = NULL;
2285094Slling 				intval = za.za_first_integer;
2295094Slling 			}
2305094Slling 
2315949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2325094Slling 
2335094Slling 			if (strval != NULL)
2345094Slling 				kmem_free(strval,
2355094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2365094Slling 
2375094Slling 			break;
2385094Slling 
2395094Slling 		case 1:
2405094Slling 			/* string property */
2415094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2425094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2435094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2445094Slling 			if (err) {
2455094Slling 				kmem_free(strval, za.za_num_integers);
2465094Slling 				break;
2475094Slling 			}
2485949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2495094Slling 			kmem_free(strval, za.za_num_integers);
2505094Slling 			break;
2515094Slling 
2525094Slling 		default:
2535094Slling 			break;
2545094Slling 		}
2555094Slling 	}
2565094Slling 	zap_cursor_fini(&zc);
2575094Slling 	mutex_exit(&spa->spa_props_lock);
2585094Slling out:
2595094Slling 	if (err && err != ENOENT) {
2605094Slling 		nvlist_free(*nvp);
2615949Slling 		*nvp = NULL;
2625094Slling 		return (err);
2635094Slling 	}
2645094Slling 
2655094Slling 	return (0);
2665094Slling }
2675094Slling 
2685094Slling /*
2695094Slling  * Validate the given pool properties nvlist and modify the list
2705094Slling  * for the property values to be set.
2715094Slling  */
2725094Slling static int
2735094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2745094Slling {
2755094Slling 	nvpair_t *elem;
2765094Slling 	int error = 0, reset_bootfs = 0;
2775094Slling 	uint64_t objnum;
2785094Slling 
2795094Slling 	elem = NULL;
2805094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2815094Slling 		zpool_prop_t prop;
2825094Slling 		char *propname, *strval;
2835094Slling 		uint64_t intval;
2845094Slling 		objset_t *os;
2855363Seschrock 		char *slash;
2865094Slling 
2875094Slling 		propname = nvpair_name(elem);
2885094Slling 
2895094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2905094Slling 			return (EINVAL);
2915094Slling 
2925094Slling 		switch (prop) {
2935094Slling 		case ZPOOL_PROP_VERSION:
2945094Slling 			error = nvpair_value_uint64(elem, &intval);
2955094Slling 			if (!error &&
2965094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
2975094Slling 				error = EINVAL;
2985094Slling 			break;
2995094Slling 
3005094Slling 		case ZPOOL_PROP_DELEGATION:
3015094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3027538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3035094Slling 			error = nvpair_value_uint64(elem, &intval);
3045094Slling 			if (!error && intval > 1)
3055094Slling 				error = EINVAL;
3065094Slling 			break;
3075094Slling 
3085094Slling 		case ZPOOL_PROP_BOOTFS:
3095094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3105094Slling 				error = ENOTSUP;
3115094Slling 				break;
3125094Slling 			}
3135094Slling 
3145094Slling 			/*
3157042Sgw25295 			 * Make sure the vdev config is bootable
3165094Slling 			 */
3177042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3185094Slling 				error = ENOTSUP;
3195094Slling 				break;
3205094Slling 			}
3215094Slling 
3225094Slling 			reset_bootfs = 1;
3235094Slling 
3245094Slling 			error = nvpair_value_string(elem, &strval);
3255094Slling 
3265094Slling 			if (!error) {
3277042Sgw25295 				uint64_t compress;
3287042Sgw25295 
3295094Slling 				if (strval == NULL || strval[0] == '\0') {
3305094Slling 					objnum = zpool_prop_default_numeric(
3315094Slling 					    ZPOOL_PROP_BOOTFS);
3325094Slling 					break;
3335094Slling 				}
3345094Slling 
3355094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
3366689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3375094Slling 					break;
3387042Sgw25295 
3397042Sgw25295 				/* We don't support gzip bootable datasets */
3407042Sgw25295 				if ((error = dsl_prop_get_integer(strval,
3417042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3427042Sgw25295 				    &compress, NULL)) == 0 &&
3437042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3447042Sgw25295 					error = ENOTSUP;
3457042Sgw25295 				} else {
3467042Sgw25295 					objnum = dmu_objset_id(os);
3477042Sgw25295 				}
3485094Slling 				dmu_objset_close(os);
3495094Slling 			}
3505094Slling 			break;
3517754SJeff.Bonwick@Sun.COM 
3525329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3535329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3545329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3555329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3565329Sgw25295 				error = EINVAL;
3575329Sgw25295 
3585329Sgw25295 			/*
3595329Sgw25295 			 * This is a special case which only occurs when
3605329Sgw25295 			 * the pool has completely failed. This allows
3615329Sgw25295 			 * the user to change the in-core failmode property
3625329Sgw25295 			 * without syncing it out to disk (I/Os might
3635329Sgw25295 			 * currently be blocked). We do this by returning
3645329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3655329Sgw25295 			 * into thinking we encountered a property validation
3665329Sgw25295 			 * error.
3675329Sgw25295 			 */
3687754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
3695329Sgw25295 				spa->spa_failmode = intval;
3705329Sgw25295 				error = EIO;
3715329Sgw25295 			}
3725329Sgw25295 			break;
3735363Seschrock 
3745363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3755363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3765363Seschrock 				break;
3775363Seschrock 
3785363Seschrock 			if (strval[0] == '\0')
3795363Seschrock 				break;
3805363Seschrock 
3815363Seschrock 			if (strcmp(strval, "none") == 0)
3825363Seschrock 				break;
3835363Seschrock 
3845363Seschrock 			if (strval[0] != '/') {
3855363Seschrock 				error = EINVAL;
3865363Seschrock 				break;
3875363Seschrock 			}
3885363Seschrock 
3895363Seschrock 			slash = strrchr(strval, '/');
3905363Seschrock 			ASSERT(slash != NULL);
3915363Seschrock 
3925363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3935363Seschrock 			    strcmp(slash, "/..") == 0)
3945363Seschrock 				error = EINVAL;
3955363Seschrock 			break;
3965094Slling 		}
3975094Slling 
3985094Slling 		if (error)
3995094Slling 			break;
4005094Slling 	}
4015094Slling 
4025094Slling 	if (!error && reset_bootfs) {
4035094Slling 		error = nvlist_remove(props,
4045094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4055094Slling 
4065094Slling 		if (!error) {
4075094Slling 			error = nvlist_add_uint64(props,
4085094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4095094Slling 		}
4105094Slling 	}
4115094Slling 
4125094Slling 	return (error);
4135094Slling }
4145094Slling 
4155094Slling int
4165094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4175094Slling {
4185094Slling 	int error;
4195094Slling 
4205094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4215094Slling 		return (error);
4225094Slling 
4235094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4245094Slling 	    spa, nvp, 3));
4255094Slling }
4265094Slling 
4275094Slling /*
4285094Slling  * If the bootfs property value is dsobj, clear it.
4295094Slling  */
4305094Slling void
4315094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4325094Slling {
4335094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4345094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4355094Slling 		    spa->spa_pool_props_object,
4365094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4375094Slling 		spa->spa_bootfs = 0;
4385094Slling 	}
4395094Slling }
4405094Slling 
441789Sahrens /*
442789Sahrens  * ==========================================================================
443789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
444789Sahrens  * ==========================================================================
445789Sahrens  */
446789Sahrens 
4471544Seschrock static int
4481544Seschrock spa_error_entry_compare(const void *a, const void *b)
4491544Seschrock {
4501544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4511544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4521544Seschrock 	int ret;
4531544Seschrock 
4541544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4551544Seschrock 	    sizeof (zbookmark_t));
4561544Seschrock 
4571544Seschrock 	if (ret < 0)
4581544Seschrock 		return (-1);
4591544Seschrock 	else if (ret > 0)
4601544Seschrock 		return (1);
4611544Seschrock 	else
4621544Seschrock 		return (0);
4631544Seschrock }
4641544Seschrock 
4651544Seschrock /*
4661544Seschrock  * Utility function which retrieves copies of the current logs and
4671544Seschrock  * re-initializes them in the process.
4681544Seschrock  */
4691544Seschrock void
4701544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4711544Seschrock {
4721544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4731544Seschrock 
4741544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4751544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4761544Seschrock 
4771544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4781544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4791544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4801544Seschrock 	avl_create(&spa->spa_errlist_last,
4811544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4821544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4831544Seschrock }
4841544Seschrock 
485789Sahrens /*
486789Sahrens  * Activate an uninitialized pool.
487789Sahrens  */
488789Sahrens static void
489789Sahrens spa_activate(spa_t *spa)
490789Sahrens {
491789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
492789Sahrens 
493789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
494789Sahrens 
495789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4964527Sperrin 	spa->spa_log_class = metaslab_class_create();
497789Sahrens 
4987754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
4997754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5007754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = taskq_create("spa_zio",
5017754SJeff.Bonwick@Sun.COM 			    zio_taskq_threads[t][q], maxclsyspri, 50,
5027754SJeff.Bonwick@Sun.COM 			    INT_MAX, TASKQ_PREPOPULATE);
5037754SJeff.Bonwick@Sun.COM 		}
504789Sahrens 	}
505789Sahrens 
5067754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
5077754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
5087754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
5097754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
510789Sahrens 
511789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
512789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
5131544Seschrock 
5141544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5151544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5161544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5171544Seschrock 	avl_create(&spa->spa_errlist_last,
5181544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5191544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
520789Sahrens }
521789Sahrens 
522789Sahrens /*
523789Sahrens  * Opposite of spa_activate().
524789Sahrens  */
525789Sahrens static void
526789Sahrens spa_deactivate(spa_t *spa)
527789Sahrens {
528789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
529789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
530789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
531789Sahrens 
532789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
533789Sahrens 
534789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
535789Sahrens 
5367754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
5377754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
5387754SJeff.Bonwick@Sun.COM 
5397754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
5407754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5417754SJeff.Bonwick@Sun.COM 			taskq_destroy(spa->spa_zio_taskq[t][q]);
5427754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
5437754SJeff.Bonwick@Sun.COM 		}
544789Sahrens 	}
545789Sahrens 
546789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
547789Sahrens 	spa->spa_normal_class = NULL;
548789Sahrens 
5494527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5504527Sperrin 	spa->spa_log_class = NULL;
5514527Sperrin 
5521544Seschrock 	/*
5531544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5541544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5551544Seschrock 	 */
5561544Seschrock 	spa_errlog_drain(spa);
5571544Seschrock 
5581544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5591544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5601544Seschrock 
561789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
562789Sahrens }
563789Sahrens 
564789Sahrens /*
565789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
566789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
567789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
568789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
569789Sahrens  */
5702082Seschrock static int
5712082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5722082Seschrock     uint_t id, int atype)
573789Sahrens {
574789Sahrens 	nvlist_t **child;
575789Sahrens 	uint_t c, children;
5762082Seschrock 	int error;
5772082Seschrock 
5782082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5792082Seschrock 		return (error);
5802082Seschrock 
5812082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5822082Seschrock 		return (0);
583789Sahrens 
5847754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
5857754SJeff.Bonwick@Sun.COM 	    &child, &children);
5867754SJeff.Bonwick@Sun.COM 
5877754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
5887754SJeff.Bonwick@Sun.COM 		return (0);
5897754SJeff.Bonwick@Sun.COM 
5907754SJeff.Bonwick@Sun.COM 	if (error) {
5912082Seschrock 		vdev_free(*vdp);
5922082Seschrock 		*vdp = NULL;
5932082Seschrock 		return (EINVAL);
594789Sahrens 	}
595789Sahrens 
596789Sahrens 	for (c = 0; c < children; c++) {
5972082Seschrock 		vdev_t *vd;
5982082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5992082Seschrock 		    atype)) != 0) {
6002082Seschrock 			vdev_free(*vdp);
6012082Seschrock 			*vdp = NULL;
6022082Seschrock 			return (error);
603789Sahrens 		}
604789Sahrens 	}
605789Sahrens 
6062082Seschrock 	ASSERT(*vdp != NULL);
6072082Seschrock 
6082082Seschrock 	return (0);
609789Sahrens }
610789Sahrens 
611789Sahrens /*
612789Sahrens  * Opposite of spa_load().
613789Sahrens  */
614789Sahrens static void
615789Sahrens spa_unload(spa_t *spa)
616789Sahrens {
6172082Seschrock 	int i;
6182082Seschrock 
6197754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
6207754SJeff.Bonwick@Sun.COM 
621789Sahrens 	/*
6221544Seschrock 	 * Stop async tasks.
6231544Seschrock 	 */
6241544Seschrock 	spa_async_suspend(spa);
6251544Seschrock 
6261544Seschrock 	/*
627789Sahrens 	 * Stop syncing.
628789Sahrens 	 */
629789Sahrens 	if (spa->spa_sync_on) {
630789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
631789Sahrens 		spa->spa_sync_on = B_FALSE;
632789Sahrens 	}
633789Sahrens 
634789Sahrens 	/*
6357754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
636789Sahrens 	 */
6377754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_async_root_lock);
6387754SJeff.Bonwick@Sun.COM 	while (spa->spa_async_root_count != 0)
6397754SJeff.Bonwick@Sun.COM 		cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock);
6407754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_async_root_lock);
641789Sahrens 
642789Sahrens 	/*
6435450Sbrendan 	 * Drop and purge level 2 cache
6445450Sbrendan 	 */
6455450Sbrendan 	spa_l2cache_drop(spa);
6465450Sbrendan 
6475450Sbrendan 	/*
648789Sahrens 	 * Close the dsl pool.
649789Sahrens 	 */
650789Sahrens 	if (spa->spa_dsl_pool) {
651789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
652789Sahrens 		spa->spa_dsl_pool = NULL;
653789Sahrens 	}
654789Sahrens 
655789Sahrens 	/*
656789Sahrens 	 * Close all vdevs.
657789Sahrens 	 */
6581585Sbonwick 	if (spa->spa_root_vdev)
659789Sahrens 		vdev_free(spa->spa_root_vdev);
6601585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6611544Seschrock 
6625450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
6635450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
6645450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
6655450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
6665450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
6675450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
6685450Sbrendan 	}
6695450Sbrendan 	if (spa->spa_spares.sav_config) {
6705450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
6715450Sbrendan 		spa->spa_spares.sav_config = NULL;
6722082Seschrock 	}
6737377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
6745450Sbrendan 
6755450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
6765450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
6775450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
6785450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
6795450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
6805450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
6815450Sbrendan 	}
6825450Sbrendan 	if (spa->spa_l2cache.sav_config) {
6835450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
6845450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
6852082Seschrock 	}
6867377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
6872082Seschrock 
6881544Seschrock 	spa->spa_async_suspended = 0;
689789Sahrens }
690789Sahrens 
691789Sahrens /*
6922082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6932082Seschrock  * this pool.  When this is called, we have some form of basic information in
6945450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
6955450Sbrendan  * then re-generate a more complete list including status information.
6962082Seschrock  */
6972082Seschrock static void
6982082Seschrock spa_load_spares(spa_t *spa)
6992082Seschrock {
7002082Seschrock 	nvlist_t **spares;
7012082Seschrock 	uint_t nspares;
7022082Seschrock 	int i;
7033377Seschrock 	vdev_t *vd, *tvd;
7042082Seschrock 
7057754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
7067754SJeff.Bonwick@Sun.COM 
7072082Seschrock 	/*
7082082Seschrock 	 * First, close and free any existing spare vdevs.
7092082Seschrock 	 */
7105450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7115450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
7123377Seschrock 
7133377Seschrock 		/* Undo the call to spa_activate() below */
7146643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7156643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
7163377Seschrock 			spa_spare_remove(tvd);
7173377Seschrock 		vdev_close(vd);
7183377Seschrock 		vdev_free(vd);
7192082Seschrock 	}
7203377Seschrock 
7215450Sbrendan 	if (spa->spa_spares.sav_vdevs)
7225450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7235450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7245450Sbrendan 
7255450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7262082Seschrock 		nspares = 0;
7272082Seschrock 	else
7285450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7292082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7302082Seschrock 
7315450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7325450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7332082Seschrock 
7342082Seschrock 	if (nspares == 0)
7352082Seschrock 		return;
7362082Seschrock 
7372082Seschrock 	/*
7382082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7393377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7403377Seschrock 	 * structures associated with it: one in the list of spares (used only
7413377Seschrock 	 * for basic validation purposes) and one in the active vdev
7423377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7433377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7443377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7452082Seschrock 	 */
7465450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
7475450Sbrendan 	    KM_SLEEP);
7485450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7492082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
7502082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
7512082Seschrock 		ASSERT(vd != NULL);
7522082Seschrock 
7535450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
7542082Seschrock 
7556643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7566643Seschrock 		    B_FALSE)) != NULL) {
7573377Seschrock 			if (!tvd->vdev_isspare)
7583377Seschrock 				spa_spare_add(tvd);
7593377Seschrock 
7603377Seschrock 			/*
7613377Seschrock 			 * We only mark the spare active if we were successfully
7623377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7633377Seschrock 			 * with a bad active spare would result in strange
7643377Seschrock 			 * behavior, because multiple pool would think the spare
7653377Seschrock 			 * is actively in use.
7663377Seschrock 			 *
7673377Seschrock 			 * There is a vulnerability here to an equally bizarre
7683377Seschrock 			 * circumstance, where a dead active spare is later
7693377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7703377Seschrock 			 * the rarity of this scenario, and the extra complexity
7713377Seschrock 			 * it adds, we ignore the possibility.
7723377Seschrock 			 */
7733377Seschrock 			if (!vdev_is_dead(tvd))
7743377Seschrock 				spa_spare_activate(tvd);
7753377Seschrock 		}
7763377Seschrock 
7777754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
7787754SJeff.Bonwick@Sun.COM 
7792082Seschrock 		if (vdev_open(vd) != 0)
7802082Seschrock 			continue;
7812082Seschrock 
7825450Sbrendan 		if (vdev_validate_aux(vd) == 0)
7835450Sbrendan 			spa_spare_add(vd);
7842082Seschrock 	}
7852082Seschrock 
7862082Seschrock 	/*
7872082Seschrock 	 * Recompute the stashed list of spares, with status information
7882082Seschrock 	 * this time.
7892082Seschrock 	 */
7905450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
7912082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7922082Seschrock 
7935450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
7945450Sbrendan 	    KM_SLEEP);
7955450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7965450Sbrendan 		spares[i] = vdev_config_generate(spa,
7975450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
7985450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
7995450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
8005450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
8012082Seschrock 		nvlist_free(spares[i]);
8025450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
8035450Sbrendan }
8045450Sbrendan 
8055450Sbrendan /*
8065450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
8075450Sbrendan  * this pool.  When this is called, we have some form of basic information in
8085450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
8095450Sbrendan  * then re-generate a more complete list including status information.
8105450Sbrendan  * Devices which are already active have their details maintained, and are
8115450Sbrendan  * not re-opened.
8125450Sbrendan  */
8135450Sbrendan static void
8145450Sbrendan spa_load_l2cache(spa_t *spa)
8155450Sbrendan {
8165450Sbrendan 	nvlist_t **l2cache;
8175450Sbrendan 	uint_t nl2cache;
8185450Sbrendan 	int i, j, oldnvdevs;
8196643Seschrock 	uint64_t guid, size;
8205450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
8215450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
8225450Sbrendan 
8237754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
8247754SJeff.Bonwick@Sun.COM 
8255450Sbrendan 	if (sav->sav_config != NULL) {
8265450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8275450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8285450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8295450Sbrendan 	} else {
8305450Sbrendan 		nl2cache = 0;
8315450Sbrendan 	}
8325450Sbrendan 
8335450Sbrendan 	oldvdevs = sav->sav_vdevs;
8345450Sbrendan 	oldnvdevs = sav->sav_count;
8355450Sbrendan 	sav->sav_vdevs = NULL;
8365450Sbrendan 	sav->sav_count = 0;
8375450Sbrendan 
8385450Sbrendan 	/*
8395450Sbrendan 	 * Process new nvlist of vdevs.
8405450Sbrendan 	 */
8415450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8425450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8435450Sbrendan 		    &guid) == 0);
8445450Sbrendan 
8455450Sbrendan 		newvdevs[i] = NULL;
8465450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
8475450Sbrendan 			vd = oldvdevs[j];
8485450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
8495450Sbrendan 				/*
8505450Sbrendan 				 * Retain previous vdev for add/remove ops.
8515450Sbrendan 				 */
8525450Sbrendan 				newvdevs[i] = vd;
8535450Sbrendan 				oldvdevs[j] = NULL;
8545450Sbrendan 				break;
8555450Sbrendan 			}
8565450Sbrendan 		}
8575450Sbrendan 
8585450Sbrendan 		if (newvdevs[i] == NULL) {
8595450Sbrendan 			/*
8605450Sbrendan 			 * Create new vdev
8615450Sbrendan 			 */
8625450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
8635450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
8645450Sbrendan 			ASSERT(vd != NULL);
8655450Sbrendan 			newvdevs[i] = vd;
8665450Sbrendan 
8675450Sbrendan 			/*
8685450Sbrendan 			 * Commit this vdev as an l2cache device,
8695450Sbrendan 			 * even if it fails to open.
8705450Sbrendan 			 */
8715450Sbrendan 			spa_l2cache_add(vd);
8725450Sbrendan 
8736643Seschrock 			vd->vdev_top = vd;
8746643Seschrock 			vd->vdev_aux = sav;
8756643Seschrock 
8766643Seschrock 			spa_l2cache_activate(vd);
8776643Seschrock 
8785450Sbrendan 			if (vdev_open(vd) != 0)
8795450Sbrendan 				continue;
8805450Sbrendan 
8815450Sbrendan 			(void) vdev_validate_aux(vd);
8825450Sbrendan 
8835450Sbrendan 			if (!vdev_is_dead(vd)) {
8845450Sbrendan 				size = vdev_get_rsize(vd);
8856643Seschrock 				l2arc_add_vdev(spa, vd,
8866643Seschrock 				    VDEV_LABEL_START_SIZE,
8876643Seschrock 				    size - VDEV_LABEL_START_SIZE);
8885450Sbrendan 			}
8895450Sbrendan 		}
8905450Sbrendan 	}
8915450Sbrendan 
8925450Sbrendan 	/*
8935450Sbrendan 	 * Purge vdevs that were dropped
8945450Sbrendan 	 */
8955450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
8965450Sbrendan 		uint64_t pool;
8975450Sbrendan 
8985450Sbrendan 		vd = oldvdevs[i];
8995450Sbrendan 		if (vd != NULL) {
9007754SJeff.Bonwick@Sun.COM 			if ((spa_mode & FWRITE) &&
9015450Sbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
9026643Seschrock 			    pool != 0ULL &&
9036643Seschrock 			    l2arc_vdev_present(vd)) {
9045450Sbrendan 				l2arc_remove_vdev(vd);
9055450Sbrendan 			}
9065450Sbrendan 			(void) vdev_close(vd);
9075450Sbrendan 			spa_l2cache_remove(vd);
9085450Sbrendan 		}
9095450Sbrendan 	}
9105450Sbrendan 
9115450Sbrendan 	if (oldvdevs)
9125450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
9135450Sbrendan 
9145450Sbrendan 	if (sav->sav_config == NULL)
9155450Sbrendan 		goto out;
9165450Sbrendan 
9175450Sbrendan 	sav->sav_vdevs = newvdevs;
9185450Sbrendan 	sav->sav_count = (int)nl2cache;
9195450Sbrendan 
9205450Sbrendan 	/*
9215450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
9225450Sbrendan 	 * information this time.
9235450Sbrendan 	 */
9245450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
9255450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9265450Sbrendan 
9275450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9285450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9295450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9305450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9315450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9325450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9335450Sbrendan out:
9345450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9355450Sbrendan 		nvlist_free(l2cache[i]);
9365450Sbrendan 	if (sav->sav_count)
9375450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9382082Seschrock }
9392082Seschrock 
9402082Seschrock static int
9412082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9422082Seschrock {
9432082Seschrock 	dmu_buf_t *db;
9442082Seschrock 	char *packed = NULL;
9452082Seschrock 	size_t nvsize = 0;
9462082Seschrock 	int error;
9472082Seschrock 	*value = NULL;
9482082Seschrock 
9492082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9502082Seschrock 	nvsize = *(uint64_t *)db->db_data;
9512082Seschrock 	dmu_buf_rele(db, FTAG);
9522082Seschrock 
9532082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
9542082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
9552082Seschrock 	if (error == 0)
9562082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
9572082Seschrock 	kmem_free(packed, nvsize);
9582082Seschrock 
9592082Seschrock 	return (error);
9602082Seschrock }
9612082Seschrock 
9622082Seschrock /*
9634451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9644451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9654451Seschrock  */
9664451Seschrock static void
9674451Seschrock spa_check_removed(vdev_t *vd)
9684451Seschrock {
9694451Seschrock 	int c;
9704451Seschrock 
9714451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9724451Seschrock 		spa_check_removed(vd->vdev_child[c]);
9734451Seschrock 
9744451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9754451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9764451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9774451Seschrock 	}
9784451Seschrock }
9794451Seschrock 
9804451Seschrock /*
9817294Sperrin  * Check for missing log devices
9827294Sperrin  */
9837294Sperrin int
9847294Sperrin spa_check_logs(spa_t *spa)
9857294Sperrin {
9867294Sperrin 	switch (spa->spa_log_state) {
9877294Sperrin 	case SPA_LOG_MISSING:
9887294Sperrin 		/* need to recheck in case slog has been restored */
9897294Sperrin 	case SPA_LOG_UNKNOWN:
9907294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
9917294Sperrin 		    DS_FIND_CHILDREN)) {
9927294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
9937294Sperrin 			return (1);
9947294Sperrin 		}
9957294Sperrin 		break;
9967294Sperrin 
9977294Sperrin 	case SPA_LOG_CLEAR:
9987294Sperrin 		(void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
9997294Sperrin 		    DS_FIND_CHILDREN);
10007294Sperrin 		break;
10017294Sperrin 	}
10027294Sperrin 	spa->spa_log_state = SPA_LOG_GOOD;
10037294Sperrin 	return (0);
10047294Sperrin }
10057294Sperrin 
10067294Sperrin /*
1007789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
10081544Seschrock  * source of configuration information.
1009789Sahrens  */
1010789Sahrens static int
10111544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1012789Sahrens {
1013789Sahrens 	int error = 0;
1014789Sahrens 	nvlist_t *nvroot = NULL;
1015789Sahrens 	vdev_t *rvd;
1016789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
10171635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1018789Sahrens 	uint64_t pool_guid;
10192082Seschrock 	uint64_t version;
10204451Seschrock 	uint64_t autoreplace = 0;
10217294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
1022789Sahrens 
10237754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
10247754SJeff.Bonwick@Sun.COM 
10251544Seschrock 	spa->spa_load_state = state;
10261635Sbonwick 
1027789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
10281733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
10291544Seschrock 		error = EINVAL;
10301544Seschrock 		goto out;
10311544Seschrock 	}
1032789Sahrens 
10332082Seschrock 	/*
10342082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
10352082Seschrock 	 * it's not present treat it as the initial version.
10362082Seschrock 	 */
10372082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
10384577Sahrens 		version = SPA_VERSION_INITIAL;
10392082Seschrock 
10401733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
10411733Sbonwick 	    &spa->spa_config_txg);
10421733Sbonwick 
10431635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
10441544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
10451544Seschrock 		error = EEXIST;
10461544Seschrock 		goto out;
10471544Seschrock 	}
1048789Sahrens 
10492174Seschrock 	spa->spa_load_guid = pool_guid;
10502174Seschrock 
1051789Sahrens 	/*
10522082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
10532082Seschrock 	 * value that will be returned by spa_version() since parsing the
10542082Seschrock 	 * configuration requires knowing the version number.
1055789Sahrens 	 */
10567754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
10572082Seschrock 	spa->spa_ubsync.ub_version = version;
10582082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
10597754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1060789Sahrens 
10612082Seschrock 	if (error != 0)
10621544Seschrock 		goto out;
1063789Sahrens 
10641585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1065789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1066789Sahrens 
1067789Sahrens 	/*
1068789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1069789Sahrens 	 */
10707754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
10714070Smc142369 	error = vdev_open(rvd);
10727754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
10734070Smc142369 	if (error != 0)
10741544Seschrock 		goto out;
1075789Sahrens 
1076789Sahrens 	/*
10771986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
10787754SJeff.Bonwick@Sun.COM 	 * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER.
10791986Seschrock 	 */
10807754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
10811986Seschrock 	error = vdev_validate(rvd);
10827754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
10831986Seschrock 
10844070Smc142369 	if (error != 0)
10851986Seschrock 		goto out;
10861986Seschrock 
10871986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
10881986Seschrock 		error = ENXIO;
10891986Seschrock 		goto out;
10901986Seschrock 	}
10911986Seschrock 
10921986Seschrock 	/*
1093789Sahrens 	 * Find the best uberblock.
1094789Sahrens 	 */
10957754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1096789Sahrens 
1097789Sahrens 	/*
1098789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1099789Sahrens 	 */
1100789Sahrens 	if (ub->ub_txg == 0) {
11011760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11021760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11031544Seschrock 		error = ENXIO;
11041544Seschrock 		goto out;
11051544Seschrock 	}
11061544Seschrock 
11071544Seschrock 	/*
11081544Seschrock 	 * If the pool is newer than the code, we can't open it.
11091544Seschrock 	 */
11104577Sahrens 	if (ub->ub_version > SPA_VERSION) {
11111760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11121760Seschrock 		    VDEV_AUX_VERSION_NEWER);
11131544Seschrock 		error = ENOTSUP;
11141544Seschrock 		goto out;
1115789Sahrens 	}
1116789Sahrens 
1117789Sahrens 	/*
1118789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1119789Sahrens 	 * incomplete configuration.
1120789Sahrens 	 */
11211732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
11221544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11231544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
11241544Seschrock 		error = ENXIO;
11251544Seschrock 		goto out;
1126789Sahrens 	}
1127789Sahrens 
1128789Sahrens 	/*
1129789Sahrens 	 * Initialize internal SPA structures.
1130789Sahrens 	 */
1131789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1132789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1133789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
11341544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
11351544Seschrock 	if (error) {
11361544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11371544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11381544Seschrock 		goto out;
11391544Seschrock 	}
1140789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1141789Sahrens 
11421544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1143789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
11441544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
11451544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11461544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11471544Seschrock 		error = EIO;
11481544Seschrock 		goto out;
11491544Seschrock 	}
1150789Sahrens 
1151789Sahrens 	if (!mosconfig) {
11522082Seschrock 		nvlist_t *newconfig;
11533975Sek110237 		uint64_t hostid;
11542082Seschrock 
11552082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
11561544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11571544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11581544Seschrock 			error = EIO;
11591544Seschrock 			goto out;
11601544Seschrock 		}
1161789Sahrens 
11627706SLin.Ling@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig,
11637706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
11643975Sek110237 			char *hostname;
11653975Sek110237 			unsigned long myhostid = 0;
11663975Sek110237 
11673975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
11683975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
11693975Sek110237 
11703975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
11714178Slling 			if (hostid != 0 && myhostid != 0 &&
11724178Slling 			    (unsigned long)hostid != myhostid) {
11733975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
11743975Sek110237 				    "loaded as it was last accessed by "
11757706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
11763975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
11777754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
11783975Sek110237 				    (unsigned long)hostid);
11793975Sek110237 				error = EBADF;
11803975Sek110237 				goto out;
11813975Sek110237 			}
11823975Sek110237 		}
11833975Sek110237 
1184789Sahrens 		spa_config_set(spa, newconfig);
1185789Sahrens 		spa_unload(spa);
1186789Sahrens 		spa_deactivate(spa);
1187789Sahrens 		spa_activate(spa);
1188789Sahrens 
11891544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
11901544Seschrock 	}
11911544Seschrock 
11921544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
11931544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
11941544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
11951544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11961544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11971544Seschrock 		error = EIO;
11981544Seschrock 		goto out;
1199789Sahrens 	}
1200789Sahrens 
12011544Seschrock 	/*
12022082Seschrock 	 * Load the bit that tells us to use the new accounting function
12032082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
12042082Seschrock 	 * be present.
12052082Seschrock 	 */
12062082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12072082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
12082082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
12092082Seschrock 	if (error != 0 && error != ENOENT) {
12102082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12112082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12122082Seschrock 		error = EIO;
12132082Seschrock 		goto out;
12142082Seschrock 	}
12152082Seschrock 
12162082Seschrock 	/*
12171544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
12181544Seschrock 	 * not be present.
12191544Seschrock 	 */
12201544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12211544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
12221544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
12231807Sbonwick 	if (error != 0 && error != ENOENT) {
12241544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12251544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12261544Seschrock 		error = EIO;
12271544Seschrock 		goto out;
12281544Seschrock 	}
12291544Seschrock 
12301544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12311544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
12321544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
12331544Seschrock 	if (error != 0 && error != ENOENT) {
12341544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12351544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12361544Seschrock 		error = EIO;
12371544Seschrock 		goto out;
12381544Seschrock 	}
1239789Sahrens 
1240789Sahrens 	/*
12412926Sek110237 	 * Load the history object.  If we have an older pool, this
12422926Sek110237 	 * will not be present.
12432926Sek110237 	 */
12442926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
12452926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
12462926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
12472926Sek110237 	if (error != 0 && error != ENOENT) {
12482926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12492926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
12502926Sek110237 		error = EIO;
12512926Sek110237 		goto out;
12522926Sek110237 	}
12532926Sek110237 
12542926Sek110237 	/*
12552082Seschrock 	 * Load any hot spares for this pool.
12562082Seschrock 	 */
12572082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12585450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
12592082Seschrock 	if (error != 0 && error != ENOENT) {
12602082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12612082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12622082Seschrock 		error = EIO;
12632082Seschrock 		goto out;
12642082Seschrock 	}
12652082Seschrock 	if (error == 0) {
12664577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
12675450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
12685450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
12692082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12702082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12712082Seschrock 			error = EIO;
12722082Seschrock 			goto out;
12732082Seschrock 		}
12742082Seschrock 
12757754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12762082Seschrock 		spa_load_spares(spa);
12777754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
12782082Seschrock 	}
12792082Seschrock 
12805450Sbrendan 	/*
12815450Sbrendan 	 * Load any level 2 ARC devices for this pool.
12825450Sbrendan 	 */
12835450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12845450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
12855450Sbrendan 	    &spa->spa_l2cache.sav_object);
12865450Sbrendan 	if (error != 0 && error != ENOENT) {
12875450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12885450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
12895450Sbrendan 		error = EIO;
12905450Sbrendan 		goto out;
12915450Sbrendan 	}
12925450Sbrendan 	if (error == 0) {
12935450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
12945450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
12955450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
12965450Sbrendan 			vdev_set_state(rvd, B_TRUE,
12975450Sbrendan 			    VDEV_STATE_CANT_OPEN,
12985450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
12995450Sbrendan 			error = EIO;
13005450Sbrendan 			goto out;
13015450Sbrendan 		}
13025450Sbrendan 
13037754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13045450Sbrendan 		spa_load_l2cache(spa);
13057754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
13065450Sbrendan 	}
13075450Sbrendan 
13087294Sperrin 	if (spa_check_logs(spa)) {
13097294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13107294Sperrin 		    VDEV_AUX_BAD_LOG);
13117294Sperrin 		error = ENXIO;
13127294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
13137294Sperrin 		goto out;
13147294Sperrin 	}
13157294Sperrin 
13167294Sperrin 
13175094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
13184543Smarks 
13193912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13203912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
13213912Slling 
13223912Slling 	if (error && error != ENOENT) {
13233912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13243912Slling 		    VDEV_AUX_CORRUPT_DATA);
13253912Slling 		error = EIO;
13263912Slling 		goto out;
13273912Slling 	}
13283912Slling 
13293912Slling 	if (error == 0) {
13303912Slling 		(void) zap_lookup(spa->spa_meta_objset,
13313912Slling 		    spa->spa_pool_props_object,
13324451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
13333912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
13344451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
13354451Seschrock 		    spa->spa_pool_props_object,
13364451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
13374451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
13384543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
13394543Smarks 		    spa->spa_pool_props_object,
13404543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
13414543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
13425329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
13435329Sgw25295 		    spa->spa_pool_props_object,
13445329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
13455329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
13463912Slling 	}
13473912Slling 
13482082Seschrock 	/*
13494451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
13504451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
13514451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
13524451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
13534451Seschrock 	 * over.
13544451Seschrock 	 */
13555756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
13564451Seschrock 		spa_check_removed(spa->spa_root_vdev);
13574451Seschrock 
13584451Seschrock 	/*
13591986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1360789Sahrens 	 */
13611986Seschrock 	vdev_load(rvd);
1362789Sahrens 
1363789Sahrens 	/*
1364789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1365789Sahrens 	 */
13667754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1367789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
13687754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1369789Sahrens 
1370789Sahrens 	/*
1371789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1372789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1373789Sahrens 	 */
13741544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13751544Seschrock 		error = ENXIO;
13761544Seschrock 		goto out;
13771544Seschrock 	}
1378789Sahrens 
13791544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13801635Sbonwick 		dmu_tx_t *tx;
13811635Sbonwick 		int need_update = B_FALSE;
13821585Sbonwick 		int c;
13831601Sbonwick 
13841635Sbonwick 		/*
13851635Sbonwick 		 * Claim log blocks that haven't been committed yet.
13861635Sbonwick 		 * This must all happen in a single txg.
13871635Sbonwick 		 */
13881601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1389789Sahrens 		    spa_first_txg(spa));
13907754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
13912417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1392789Sahrens 		dmu_tx_commit(tx);
1393789Sahrens 
1394789Sahrens 		spa->spa_sync_on = B_TRUE;
1395789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1396789Sahrens 
1397789Sahrens 		/*
1398789Sahrens 		 * Wait for all claims to sync.
1399789Sahrens 		 */
1400789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
14011585Sbonwick 
14021585Sbonwick 		/*
14031635Sbonwick 		 * If the config cache is stale, or we have uninitialized
14041635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
14051585Sbonwick 		 */
14061635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
14071635Sbonwick 		    state == SPA_LOAD_IMPORT)
14081635Sbonwick 			need_update = B_TRUE;
14091635Sbonwick 
14101635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
14111635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
14121635Sbonwick 				need_update = B_TRUE;
14131585Sbonwick 
14141585Sbonwick 		/*
14151635Sbonwick 		 * Update the config cache asychronously in case we're the
14161635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
14171585Sbonwick 		 */
14181635Sbonwick 		if (need_update)
14191635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1420789Sahrens 	}
1421789Sahrens 
14221544Seschrock 	error = 0;
14231544Seschrock out:
14247046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
14252082Seschrock 	if (error && error != EBADF)
14267294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
14271544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
14281544Seschrock 	spa->spa_ena = 0;
14291544Seschrock 
14301544Seschrock 	return (error);
1431789Sahrens }
1432789Sahrens 
1433789Sahrens /*
1434789Sahrens  * Pool Open/Import
1435789Sahrens  *
1436789Sahrens  * The import case is identical to an open except that the configuration is sent
1437789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1438789Sahrens  * case of an open, the pool configuration will exist in the
14394451Seschrock  * POOL_STATE_UNINITIALIZED state.
1440789Sahrens  *
1441789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1442789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1443789Sahrens  * ambiguous state.
1444789Sahrens  */
1445789Sahrens static int
1446789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1447789Sahrens {
1448789Sahrens 	spa_t *spa;
1449789Sahrens 	int error;
1450789Sahrens 	int locked = B_FALSE;
1451789Sahrens 
1452789Sahrens 	*spapp = NULL;
1453789Sahrens 
1454789Sahrens 	/*
1455789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1456789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1457789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1458789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1459789Sahrens 	 */
1460789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1461789Sahrens 		mutex_enter(&spa_namespace_lock);
1462789Sahrens 		locked = B_TRUE;
1463789Sahrens 	}
1464789Sahrens 
1465789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1466789Sahrens 		if (locked)
1467789Sahrens 			mutex_exit(&spa_namespace_lock);
1468789Sahrens 		return (ENOENT);
1469789Sahrens 	}
1470789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1471789Sahrens 
1472789Sahrens 		spa_activate(spa);
1473789Sahrens 
14741635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1475789Sahrens 
1476789Sahrens 		if (error == EBADF) {
1477789Sahrens 			/*
14781986Seschrock 			 * If vdev_validate() returns failure (indicated by
14791986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
14801986Seschrock 			 * that the pool has been exported or destroyed.  If
14811986Seschrock 			 * this is the case, the config cache is out of sync and
14821986Seschrock 			 * we should remove the pool from the namespace.
1483789Sahrens 			 */
1484789Sahrens 			spa_unload(spa);
1485789Sahrens 			spa_deactivate(spa);
14866643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1487789Sahrens 			spa_remove(spa);
1488789Sahrens 			if (locked)
1489789Sahrens 				mutex_exit(&spa_namespace_lock);
1490789Sahrens 			return (ENOENT);
14911544Seschrock 		}
14921544Seschrock 
14931544Seschrock 		if (error) {
1494789Sahrens 			/*
1495789Sahrens 			 * We can't open the pool, but we still have useful
1496789Sahrens 			 * information: the state of each vdev after the
1497789Sahrens 			 * attempted vdev_open().  Return this to the user.
1498789Sahrens 			 */
14997754SJeff.Bonwick@Sun.COM 			if (config != NULL && spa->spa_root_vdev != NULL)
1500789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1501789Sahrens 				    B_TRUE);
1502789Sahrens 			spa_unload(spa);
1503789Sahrens 			spa_deactivate(spa);
15041544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1505789Sahrens 			if (locked)
1506789Sahrens 				mutex_exit(&spa_namespace_lock);
1507789Sahrens 			*spapp = NULL;
1508789Sahrens 			return (error);
15091544Seschrock 		} else {
15101544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1511789Sahrens 		}
1512789Sahrens 	}
1513789Sahrens 
1514789Sahrens 	spa_open_ref(spa, tag);
15154451Seschrock 
1516789Sahrens 	if (locked)
1517789Sahrens 		mutex_exit(&spa_namespace_lock);
1518789Sahrens 
1519789Sahrens 	*spapp = spa;
1520789Sahrens 
15217754SJeff.Bonwick@Sun.COM 	if (config != NULL)
1522789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1523789Sahrens 
1524789Sahrens 	return (0);
1525789Sahrens }
1526789Sahrens 
1527789Sahrens int
1528789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1529789Sahrens {
1530789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1531789Sahrens }
1532789Sahrens 
15331544Seschrock /*
15341544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
15351544Seschrock  * preventing it from being exported or destroyed.
15361544Seschrock  */
15371544Seschrock spa_t *
15381544Seschrock spa_inject_addref(char *name)
15391544Seschrock {
15401544Seschrock 	spa_t *spa;
15411544Seschrock 
15421544Seschrock 	mutex_enter(&spa_namespace_lock);
15431544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
15441544Seschrock 		mutex_exit(&spa_namespace_lock);
15451544Seschrock 		return (NULL);
15461544Seschrock 	}
15471544Seschrock 	spa->spa_inject_ref++;
15481544Seschrock 	mutex_exit(&spa_namespace_lock);
15491544Seschrock 
15501544Seschrock 	return (spa);
15511544Seschrock }
15521544Seschrock 
15531544Seschrock void
15541544Seschrock spa_inject_delref(spa_t *spa)
15551544Seschrock {
15561544Seschrock 	mutex_enter(&spa_namespace_lock);
15571544Seschrock 	spa->spa_inject_ref--;
15581544Seschrock 	mutex_exit(&spa_namespace_lock);
15591544Seschrock }
15601544Seschrock 
15615450Sbrendan /*
15625450Sbrendan  * Add spares device information to the nvlist.
15635450Sbrendan  */
15642082Seschrock static void
15652082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
15662082Seschrock {
15672082Seschrock 	nvlist_t **spares;
15682082Seschrock 	uint_t i, nspares;
15692082Seschrock 	nvlist_t *nvroot;
15702082Seschrock 	uint64_t guid;
15712082Seschrock 	vdev_stat_t *vs;
15722082Seschrock 	uint_t vsc;
15733377Seschrock 	uint64_t pool;
15742082Seschrock 
15755450Sbrendan 	if (spa->spa_spares.sav_count == 0)
15762082Seschrock 		return;
15772082Seschrock 
15782082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
15792082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15805450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
15812082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15822082Seschrock 	if (nspares != 0) {
15832082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
15842082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15852082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15862082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15872082Seschrock 
15882082Seschrock 		/*
15892082Seschrock 		 * Go through and find any spares which have since been
15902082Seschrock 		 * repurposed as an active spare.  If this is the case, update
15912082Seschrock 		 * their status appropriately.
15922082Seschrock 		 */
15932082Seschrock 		for (i = 0; i < nspares; i++) {
15942082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
15952082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15967214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
15977214Slling 			    pool != 0ULL) {
15982082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
15992082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
16002082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
16012082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
16022082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
16032082Seschrock 			}
16042082Seschrock 		}
16052082Seschrock 	}
16062082Seschrock }
16072082Seschrock 
16085450Sbrendan /*
16095450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
16105450Sbrendan  */
16115450Sbrendan static void
16125450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
16135450Sbrendan {
16145450Sbrendan 	nvlist_t **l2cache;
16155450Sbrendan 	uint_t i, j, nl2cache;
16165450Sbrendan 	nvlist_t *nvroot;
16175450Sbrendan 	uint64_t guid;
16185450Sbrendan 	vdev_t *vd;
16195450Sbrendan 	vdev_stat_t *vs;
16205450Sbrendan 	uint_t vsc;
16215450Sbrendan 
16225450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
16235450Sbrendan 		return;
16245450Sbrendan 
16257754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
16265450Sbrendan 
16275450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
16285450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16295450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
16305450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
16315450Sbrendan 	if (nl2cache != 0) {
16325450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
16335450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
16345450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
16355450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
16365450Sbrendan 
16375450Sbrendan 		/*
16385450Sbrendan 		 * Update level 2 cache device stats.
16395450Sbrendan 		 */
16405450Sbrendan 
16415450Sbrendan 		for (i = 0; i < nl2cache; i++) {
16425450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
16435450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
16445450Sbrendan 
16455450Sbrendan 			vd = NULL;
16465450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
16475450Sbrendan 				if (guid ==
16485450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
16495450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
16505450Sbrendan 					break;
16515450Sbrendan 				}
16525450Sbrendan 			}
16535450Sbrendan 			ASSERT(vd != NULL);
16545450Sbrendan 
16555450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
16565450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
16575450Sbrendan 			vdev_get_stats(vd, vs);
16585450Sbrendan 		}
16595450Sbrendan 	}
16605450Sbrendan 
16617754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
16625450Sbrendan }
16635450Sbrendan 
1664789Sahrens int
16651544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1666789Sahrens {
1667789Sahrens 	int error;
1668789Sahrens 	spa_t *spa;
1669789Sahrens 
1670789Sahrens 	*config = NULL;
1671789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1672789Sahrens 
16732082Seschrock 	if (spa && *config != NULL) {
16741544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
16751544Seschrock 		    spa_get_errlog_size(spa)) == 0);
16761544Seschrock 
16777754SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
16787754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
16797754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0);
16807754SJeff.Bonwick@Sun.COM 
16812082Seschrock 		spa_add_spares(spa, *config);
16825450Sbrendan 		spa_add_l2cache(spa, *config);
16832082Seschrock 	}
16842082Seschrock 
16851544Seschrock 	/*
16861544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
16871544Seschrock 	 * and call spa_lookup() directly.
16881544Seschrock 	 */
16891544Seschrock 	if (altroot) {
16901544Seschrock 		if (spa == NULL) {
16911544Seschrock 			mutex_enter(&spa_namespace_lock);
16921544Seschrock 			spa = spa_lookup(name);
16931544Seschrock 			if (spa)
16941544Seschrock 				spa_altroot(spa, altroot, buflen);
16951544Seschrock 			else
16961544Seschrock 				altroot[0] = '\0';
16971544Seschrock 			spa = NULL;
16981544Seschrock 			mutex_exit(&spa_namespace_lock);
16991544Seschrock 		} else {
17001544Seschrock 			spa_altroot(spa, altroot, buflen);
17011544Seschrock 		}
17021544Seschrock 	}
17031544Seschrock 
1704789Sahrens 	if (spa != NULL)
1705789Sahrens 		spa_close(spa, FTAG);
1706789Sahrens 
1707789Sahrens 	return (error);
1708789Sahrens }
1709789Sahrens 
1710789Sahrens /*
17115450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
17125450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
17135450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
17145450Sbrendan  * specified, as long as they are well-formed.
17152082Seschrock  */
17162082Seschrock static int
17175450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
17185450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
17195450Sbrendan     vdev_labeltype_t label)
17202082Seschrock {
17215450Sbrendan 	nvlist_t **dev;
17225450Sbrendan 	uint_t i, ndev;
17232082Seschrock 	vdev_t *vd;
17242082Seschrock 	int error;
17252082Seschrock 
17267754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
17277754SJeff.Bonwick@Sun.COM 
17282082Seschrock 	/*
17295450Sbrendan 	 * It's acceptable to have no devs specified.
17302082Seschrock 	 */
17315450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
17322082Seschrock 		return (0);
17332082Seschrock 
17345450Sbrendan 	if (ndev == 0)
17352082Seschrock 		return (EINVAL);
17362082Seschrock 
17372082Seschrock 	/*
17385450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
17395450Sbrendan 	 * device type.
17402082Seschrock 	 */
17415450Sbrendan 	if (spa_version(spa) < version)
17422082Seschrock 		return (ENOTSUP);
17432082Seschrock 
17443377Seschrock 	/*
17455450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
17463377Seschrock 	 * checking.
17473377Seschrock 	 */
17485450Sbrendan 	sav->sav_pending = dev;
17495450Sbrendan 	sav->sav_npending = ndev;
17505450Sbrendan 
17515450Sbrendan 	for (i = 0; i < ndev; i++) {
17525450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
17532082Seschrock 		    mode)) != 0)
17543377Seschrock 			goto out;
17552082Seschrock 
17562082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
17572082Seschrock 			vdev_free(vd);
17583377Seschrock 			error = EINVAL;
17593377Seschrock 			goto out;
17602082Seschrock 		}
17612082Seschrock 
17625450Sbrendan 		/*
17637754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
17647754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
17655450Sbrendan 		 */
17667754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
17675450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
17685450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
17695450Sbrendan 			error = ENOTBLK;
17705450Sbrendan 			goto out;
17715450Sbrendan 		}
17727754SJeff.Bonwick@Sun.COM #endif
17732082Seschrock 		vd->vdev_top = vd;
17743377Seschrock 
17753377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
17765450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
17775450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
17783377Seschrock 			    vd->vdev_guid) == 0);
17792082Seschrock 		}
17802082Seschrock 
17812082Seschrock 		vdev_free(vd);
17823377Seschrock 
17835450Sbrendan 		if (error &&
17845450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
17853377Seschrock 			goto out;
17863377Seschrock 		else
17873377Seschrock 			error = 0;
17882082Seschrock 	}
17892082Seschrock 
17903377Seschrock out:
17915450Sbrendan 	sav->sav_pending = NULL;
17925450Sbrendan 	sav->sav_npending = 0;
17933377Seschrock 	return (error);
17942082Seschrock }
17952082Seschrock 
17965450Sbrendan static int
17975450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
17985450Sbrendan {
17995450Sbrendan 	int error;
18005450Sbrendan 
18017754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18027754SJeff.Bonwick@Sun.COM 
18035450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
18045450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
18055450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
18065450Sbrendan 		return (error);
18075450Sbrendan 	}
18085450Sbrendan 
18095450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
18105450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
18115450Sbrendan 	    VDEV_LABEL_L2CACHE));
18125450Sbrendan }
18135450Sbrendan 
18145450Sbrendan static void
18155450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
18165450Sbrendan     const char *config)
18175450Sbrendan {
18185450Sbrendan 	int i;
18195450Sbrendan 
18205450Sbrendan 	if (sav->sav_config != NULL) {
18215450Sbrendan 		nvlist_t **olddevs;
18225450Sbrendan 		uint_t oldndevs;
18235450Sbrendan 		nvlist_t **newdevs;
18245450Sbrendan 
18255450Sbrendan 		/*
18265450Sbrendan 		 * Generate new dev list by concatentating with the
18275450Sbrendan 		 * current dev list.
18285450Sbrendan 		 */
18295450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
18305450Sbrendan 		    &olddevs, &oldndevs) == 0);
18315450Sbrendan 
18325450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
18335450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
18345450Sbrendan 		for (i = 0; i < oldndevs; i++)
18355450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
18365450Sbrendan 			    KM_SLEEP) == 0);
18375450Sbrendan 		for (i = 0; i < ndevs; i++)
18385450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
18395450Sbrendan 			    KM_SLEEP) == 0);
18405450Sbrendan 
18415450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
18425450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
18435450Sbrendan 
18445450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
18455450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
18465450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
18475450Sbrendan 			nvlist_free(newdevs[i]);
18485450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
18495450Sbrendan 	} else {
18505450Sbrendan 		/*
18515450Sbrendan 		 * Generate a new dev list.
18525450Sbrendan 		 */
18535450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
18545450Sbrendan 		    KM_SLEEP) == 0);
18555450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
18565450Sbrendan 		    devs, ndevs) == 0);
18575450Sbrendan 	}
18585450Sbrendan }
18595450Sbrendan 
18605450Sbrendan /*
18615450Sbrendan  * Stop and drop level 2 ARC devices
18625450Sbrendan  */
18635450Sbrendan void
18645450Sbrendan spa_l2cache_drop(spa_t *spa)
18655450Sbrendan {
18665450Sbrendan 	vdev_t *vd;
18675450Sbrendan 	int i;
18685450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
18695450Sbrendan 
18705450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
18715450Sbrendan 		uint64_t pool;
18725450Sbrendan 
18735450Sbrendan 		vd = sav->sav_vdevs[i];
18745450Sbrendan 		ASSERT(vd != NULL);
18755450Sbrendan 
18767754SJeff.Bonwick@Sun.COM 		if ((spa_mode & FWRITE) &&
18776643Seschrock 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL &&
18786643Seschrock 		    l2arc_vdev_present(vd)) {
18795450Sbrendan 			l2arc_remove_vdev(vd);
18805450Sbrendan 		}
18815450Sbrendan 		if (vd->vdev_isl2cache)
18825450Sbrendan 			spa_l2cache_remove(vd);
18835450Sbrendan 		vdev_clear_stats(vd);
18845450Sbrendan 		(void) vdev_close(vd);
18855450Sbrendan 	}
18865450Sbrendan }
18875450Sbrendan 
18882082Seschrock /*
1889789Sahrens  * Pool Creation
1890789Sahrens  */
1891789Sahrens int
18925094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
18937184Stimh     const char *history_str, nvlist_t *zplprops)
1894789Sahrens {
1895789Sahrens 	spa_t *spa;
18965094Slling 	char *altroot = NULL;
18971635Sbonwick 	vdev_t *rvd;
1898789Sahrens 	dsl_pool_t *dp;
1899789Sahrens 	dmu_tx_t *tx;
19002082Seschrock 	int c, error = 0;
1901789Sahrens 	uint64_t txg = TXG_INITIAL;
19025450Sbrendan 	nvlist_t **spares, **l2cache;
19035450Sbrendan 	uint_t nspares, nl2cache;
19045094Slling 	uint64_t version;
1905789Sahrens 
1906789Sahrens 	/*
1907789Sahrens 	 * If this pool already exists, return failure.
1908789Sahrens 	 */
1909789Sahrens 	mutex_enter(&spa_namespace_lock);
1910789Sahrens 	if (spa_lookup(pool) != NULL) {
1911789Sahrens 		mutex_exit(&spa_namespace_lock);
1912789Sahrens 		return (EEXIST);
1913789Sahrens 	}
1914789Sahrens 
1915789Sahrens 	/*
1916789Sahrens 	 * Allocate a new spa_t structure.
1917789Sahrens 	 */
19185094Slling 	(void) nvlist_lookup_string(props,
19195094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
19201635Sbonwick 	spa = spa_add(pool, altroot);
1921789Sahrens 	spa_activate(spa);
1922789Sahrens 
1923789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
19245094Slling 
19255094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
19265094Slling 		spa_unload(spa);
19275094Slling 		spa_deactivate(spa);
19285094Slling 		spa_remove(spa);
19296643Seschrock 		mutex_exit(&spa_namespace_lock);
19305094Slling 		return (error);
19315094Slling 	}
19325094Slling 
19335094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
19345094Slling 	    &version) != 0)
19355094Slling 		version = SPA_VERSION;
19365094Slling 	ASSERT(version <= SPA_VERSION);
19375094Slling 	spa->spa_uberblock.ub_version = version;
1938789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1939789Sahrens 
19401635Sbonwick 	/*
19411635Sbonwick 	 * Create the root vdev.
19421635Sbonwick 	 */
19437754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19441635Sbonwick 
19452082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
19462082Seschrock 
19472082Seschrock 	ASSERT(error != 0 || rvd != NULL);
19482082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
19492082Seschrock 
19505913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
19511635Sbonwick 		error = EINVAL;
19522082Seschrock 
19532082Seschrock 	if (error == 0 &&
19542082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
19555450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
19562082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
19572082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
19582082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
19592082Seschrock 		vdev_config_dirty(rvd);
19601635Sbonwick 	}
19611635Sbonwick 
19627754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1963789Sahrens 
19642082Seschrock 	if (error != 0) {
1965789Sahrens 		spa_unload(spa);
1966789Sahrens 		spa_deactivate(spa);
1967789Sahrens 		spa_remove(spa);
1968789Sahrens 		mutex_exit(&spa_namespace_lock);
1969789Sahrens 		return (error);
1970789Sahrens 	}
1971789Sahrens 
19722082Seschrock 	/*
19732082Seschrock 	 * Get the list of spares, if specified.
19742082Seschrock 	 */
19752082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19762082Seschrock 	    &spares, &nspares) == 0) {
19775450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
19782082Seschrock 		    KM_SLEEP) == 0);
19795450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
19802082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
19817754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19822082Seschrock 		spa_load_spares(spa);
19837754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
19845450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
19855450Sbrendan 	}
19865450Sbrendan 
19875450Sbrendan 	/*
19885450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
19895450Sbrendan 	 */
19905450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
19915450Sbrendan 	    &l2cache, &nl2cache) == 0) {
19925450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
19935450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
19945450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
19955450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
19967754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19975450Sbrendan 		spa_load_l2cache(spa);
19987754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
19995450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
20002082Seschrock 	}
20012082Seschrock 
20027184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2003789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2004789Sahrens 
2005789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2006789Sahrens 
2007789Sahrens 	/*
2008789Sahrens 	 * Create the pool config object.
2009789Sahrens 	 */
2010789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
20117497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2012789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2013789Sahrens 
20141544Seschrock 	if (zap_add(spa->spa_meta_objset,
2015789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
20161544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
20171544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
20181544Seschrock 	}
2019789Sahrens 
20205094Slling 	/* Newly created pools with the right version are always deflated. */
20215094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
20225094Slling 		spa->spa_deflate = TRUE;
20235094Slling 		if (zap_add(spa->spa_meta_objset,
20245094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
20255094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
20265094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
20275094Slling 		}
20282082Seschrock 	}
20292082Seschrock 
2030789Sahrens 	/*
2031789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2032789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2033789Sahrens 	 * keeps changing.
2034789Sahrens 	 */
2035789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2036789Sahrens 	    1 << 14, tx);
2037789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2038789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2039789Sahrens 
20401544Seschrock 	if (zap_add(spa->spa_meta_objset,
2041789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
20421544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
20431544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
20441544Seschrock 	}
2045789Sahrens 
20462926Sek110237 	/*
20472926Sek110237 	 * Create the pool's history object.
20482926Sek110237 	 */
20495094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
20505094Slling 		spa_history_create_obj(spa, tx);
20515094Slling 
20525094Slling 	/*
20535094Slling 	 * Set pool properties.
20545094Slling 	 */
20555094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
20565094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
20575329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
20585094Slling 	if (props)
20595094Slling 		spa_sync_props(spa, props, CRED(), tx);
20602926Sek110237 
2061789Sahrens 	dmu_tx_commit(tx);
2062789Sahrens 
2063789Sahrens 	spa->spa_sync_on = B_TRUE;
2064789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2065789Sahrens 
2066789Sahrens 	/*
2067789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2068789Sahrens 	 * bean counters are appropriately updated.
2069789Sahrens 	 */
2070789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2071789Sahrens 
20726643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2073789Sahrens 
20745094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
20754715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
20764715Sek110237 
2077789Sahrens 	mutex_exit(&spa_namespace_lock);
2078789Sahrens 
20797046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
20807046Sahrens 
2081789Sahrens 	return (0);
2082789Sahrens }
2083789Sahrens 
2084789Sahrens /*
2085789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2086789Sahrens  * then call spa_load() to do the dirty work.
2087789Sahrens  */
20886423Sgw25295 static int
20896423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
20906643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2091789Sahrens {
2092789Sahrens 	spa_t *spa;
20935094Slling 	char *altroot = NULL;
20946643Seschrock 	int error, loaderr;
20952082Seschrock 	nvlist_t *nvroot;
20965450Sbrendan 	nvlist_t **spares, **l2cache;
20975450Sbrendan 	uint_t nspares, nl2cache;
2098789Sahrens 
2099789Sahrens 	/*
2100789Sahrens 	 * If a pool with this name exists, return failure.
2101789Sahrens 	 */
2102789Sahrens 	mutex_enter(&spa_namespace_lock);
21037897SLin.Ling@Sun.COM 	if ((spa = spa_lookup(pool)) != NULL) {
21047897SLin.Ling@Sun.COM 		if (isroot) {
21057897SLin.Ling@Sun.COM 			/*
21067897SLin.Ling@Sun.COM 			 * Remove the existing root pool from the
21077897SLin.Ling@Sun.COM 			 * namespace so that we can replace it with
21087897SLin.Ling@Sun.COM 			 * the correct config we just read in.
21097897SLin.Ling@Sun.COM 			 */
21107897SLin.Ling@Sun.COM 			ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
21117897SLin.Ling@Sun.COM 			spa_remove(spa);
21127897SLin.Ling@Sun.COM 		} else {
21137897SLin.Ling@Sun.COM 			mutex_exit(&spa_namespace_lock);
21147897SLin.Ling@Sun.COM 			return (EEXIST);
21157897SLin.Ling@Sun.COM 		}
2116789Sahrens 	}
2117789Sahrens 
2118789Sahrens 	/*
21191635Sbonwick 	 * Create and initialize the spa structure.
2120789Sahrens 	 */
21215094Slling 	(void) nvlist_lookup_string(props,
21225094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
21231635Sbonwick 	spa = spa_add(pool, altroot);
2124789Sahrens 	spa_activate(spa);
2125789Sahrens 
21266643Seschrock 	if (allowfaulted)
21276643Seschrock 		spa->spa_import_faulted = B_TRUE;
21286673Seschrock 	spa->spa_is_root = isroot;
21296643Seschrock 
2130789Sahrens 	/*
21311635Sbonwick 	 * Pass off the heavy lifting to spa_load().
21327046Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
21337046Sahrens 	 * the user-supplied config is actually the one to trust when
21347046Sahrens 	 * doing an import.
21351601Sbonwick 	 */
21367046Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2137789Sahrens 
21387754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
21392082Seschrock 	/*
21402082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
21412082Seschrock 	 * and conflicts with spa_has_spare().
21422082Seschrock 	 */
21436423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
21445450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
21455450Sbrendan 		spa->spa_spares.sav_config = NULL;
21462082Seschrock 		spa_load_spares(spa);
21472082Seschrock 	}
21486423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
21495450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
21505450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
21515450Sbrendan 		spa_load_l2cache(spa);
21525450Sbrendan 	}
21532082Seschrock 
21542082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
21552082Seschrock 	    &nvroot) == 0);
21565450Sbrendan 	if (error == 0)
21575450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
21585450Sbrendan 	if (error == 0)
21595450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
21605450Sbrendan 		    VDEV_ALLOC_L2CACHE);
21617754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
21622082Seschrock 
21635094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
21646643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
21656643Seschrock 			/*
21666643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
21676643Seschrock 			 * set, then manually set the config as if the config
21686643Seschrock 			 * passed in was specified in the cache file.
21696643Seschrock 			 */
21706643Seschrock 			error = 0;
21716643Seschrock 			spa->spa_import_faulted = B_FALSE;
21727754SJeff.Bonwick@Sun.COM 			if (spa->spa_config == NULL)
21736643Seschrock 				spa->spa_config = spa_config_generate(spa,
21746643Seschrock 				    NULL, -1ULL, B_TRUE);
21756643Seschrock 			spa_unload(spa);
21766643Seschrock 			spa_deactivate(spa);
21776643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
21786643Seschrock 		} else {
21796643Seschrock 			spa_unload(spa);
21806643Seschrock 			spa_deactivate(spa);
21816643Seschrock 			spa_remove(spa);
21826643Seschrock 		}
2183789Sahrens 		mutex_exit(&spa_namespace_lock);
2184789Sahrens 		return (error);
2185789Sahrens 	}
2186789Sahrens 
21871635Sbonwick 	/*
21885450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
21895450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
21902082Seschrock 	 */
21912082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
21922082Seschrock 	    &spares, &nspares) == 0) {
21935450Sbrendan 		if (spa->spa_spares.sav_config)
21945450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
21952082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
21962082Seschrock 		else
21975450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
21982082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21995450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
22002082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
22017754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22022082Seschrock 		spa_load_spares(spa);
22037754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22045450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
22055450Sbrendan 	}
22065450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
22075450Sbrendan 	    &l2cache, &nl2cache) == 0) {
22085450Sbrendan 		if (spa->spa_l2cache.sav_config)
22095450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
22105450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
22115450Sbrendan 		else
22125450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
22135450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
22145450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
22155450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
22167754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22175450Sbrendan 		spa_load_l2cache(spa);
22187754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22195450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
22202082Seschrock 	}
22212082Seschrock 
22226643Seschrock 	if (spa_mode & FWRITE) {
22236643Seschrock 		/*
22246643Seschrock 		 * Update the config cache to include the newly-imported pool.
22256643Seschrock 		 */
22266423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
22276643Seschrock 	}
22286643Seschrock 
22296643Seschrock 	spa->spa_import_faulted = B_FALSE;
22304451Seschrock 	mutex_exit(&spa_namespace_lock);
22314451Seschrock 
2232789Sahrens 	return (0);
2233789Sahrens }
2234789Sahrens 
22356423Sgw25295 #ifdef _KERNEL
22366423Sgw25295 /*
22376423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
22386423Sgw25295  * device label.
22396423Sgw25295  */
22406423Sgw25295 static void
22416423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
22426423Sgw25295 {
22436423Sgw25295 	nvlist_t *nvtop, *nvroot;
22446423Sgw25295 	uint64_t pgid;
22456423Sgw25295 
22466423Sgw25295 	/*
22476423Sgw25295 	 * Add this top-level vdev to the child array.
22486423Sgw25295 	 */
22496423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
22506423Sgw25295 	    == 0);
22516423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
22526423Sgw25295 	    == 0);
22536423Sgw25295 
22546423Sgw25295 	/*
22556423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
22566423Sgw25295 	 */
22576423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
22586423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
22596423Sgw25295 	    == 0);
22606423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
22616423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
22626423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
22636423Sgw25295 	    &nvtop, 1) == 0);
22646423Sgw25295 
22656423Sgw25295 	/*
22666423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
22676423Sgw25295 	 * this pool's configuration (remove the old, add the new).
22686423Sgw25295 	 */
22696423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
22706423Sgw25295 	nvlist_free(nvroot);
22716423Sgw25295 }
22726423Sgw25295 
22736423Sgw25295 /*
22746423Sgw25295  * Get the root pool information from the root disk, then import the root pool
22756423Sgw25295  * during the system boot up time.
22766423Sgw25295  */
22777539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
22787147Staylor 
22797147Staylor int
22807147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
22816423Sgw25295     uint64_t *besttxg)
22826423Sgw25295 {
22836423Sgw25295 	nvlist_t *config;
22846423Sgw25295 	uint64_t txg;
22857539SLin.Ling@Sun.COM 	int error;
22867539SLin.Ling@Sun.COM 
22877539SLin.Ling@Sun.COM 	if (error = vdev_disk_read_rootlabel(devpath, devid, &config))
22887539SLin.Ling@Sun.COM 		return (error);
22896423Sgw25295 
22906423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
22916423Sgw25295 
22927147Staylor 	if (bestconf != NULL)
22936423Sgw25295 		*bestconf = config;
22947539SLin.Ling@Sun.COM 	else
22957539SLin.Ling@Sun.COM 		nvlist_free(config);
22967147Staylor 	*besttxg = txg;
22977147Staylor 	return (0);
22986423Sgw25295 }
22996423Sgw25295 
23006423Sgw25295 boolean_t
23016423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
23026423Sgw25295 {
23036423Sgw25295 	uint64_t ival;
23046423Sgw25295 
23056423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
23066423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
23076423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
23086423Sgw25295 		return (B_FALSE);
23096423Sgw25295 
23106423Sgw25295 	return (B_TRUE);
23116423Sgw25295 }
23126423Sgw25295 
23137147Staylor 
23147147Staylor /*
23157147Staylor  * Given the boot device's physical path or devid, check if the device
23167147Staylor  * is in a valid state.  If so, return the configuration from the vdev
23177147Staylor  * label.
23187147Staylor  */
23197147Staylor int
23207147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
23217147Staylor {
23227147Staylor 	nvlist_t *conf = NULL;
23237147Staylor 	uint64_t txg = 0;
23247147Staylor 	nvlist_t *nvtop, **child;
23257147Staylor 	char *type;
23267147Staylor 	char *bootpath = NULL;
23277147Staylor 	uint_t children, c;
23287147Staylor 	char *tmp;
23297539SLin.Ling@Sun.COM 	int error;
23307147Staylor 
23317147Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
23327147Staylor 		*tmp = '\0';
23337539SLin.Ling@Sun.COM 	if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) {
23347147Staylor 		cmn_err(CE_NOTE, "error reading device label");
23357539SLin.Ling@Sun.COM 		return (error);
23367147Staylor 	}
23377147Staylor 	if (txg == 0) {
23387147Staylor 		cmn_err(CE_NOTE, "this device is detached");
23397147Staylor 		nvlist_free(conf);
23407147Staylor 		return (EINVAL);
23417147Staylor 	}
23427147Staylor 
23437147Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
23447147Staylor 	    &nvtop) == 0);
23457147Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
23467147Staylor 
23477147Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
23487147Staylor 		if (spa_rootdev_validate(nvtop)) {
23497147Staylor 			goto out;
23507147Staylor 		} else {
23517147Staylor 			nvlist_free(conf);
23527147Staylor 			return (EINVAL);
23537147Staylor 		}
23547147Staylor 	}
23557147Staylor 
23567147Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
23577147Staylor 
23587147Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
23597147Staylor 	    &child, &children) == 0);
23607147Staylor 
23617147Staylor 	/*
23627147Staylor 	 * Go thru vdevs in the mirror to see if the given device
23637147Staylor 	 * has the most recent txg. Only the device with the most
23647147Staylor 	 * recent txg has valid information and should be booted.
23657147Staylor 	 */
23667147Staylor 	for (c = 0; c < children; c++) {
23677147Staylor 		char *cdevid, *cpath;
23687147Staylor 		uint64_t tmptxg;
23697147Staylor 
23707147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
23717147Staylor 		    &cpath) != 0)
23727147Staylor 			return (EINVAL);
23737147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID,
23747147Staylor 		    &cdevid) != 0)
23757147Staylor 			return (EINVAL);
23767687SLin.Ling@Sun.COM 		if ((spa_check_rootconf(cpath, cdevid, NULL,
23777687SLin.Ling@Sun.COM 		    &tmptxg) == 0) && (tmptxg > txg)) {
23787147Staylor 			txg = tmptxg;
23797147Staylor 			VERIFY(nvlist_lookup_string(child[c],
23807147Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
23817147Staylor 		}
23827147Staylor 	}
23837147Staylor 
23847147Staylor 	/* Does the best device match the one we've booted from? */
23857147Staylor 	if (bootpath) {
23867147Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
23877147Staylor 		return (EINVAL);
23887147Staylor 	}
23897147Staylor out:
23907147Staylor 	*bestconf = conf;
23917147Staylor 	return (0);
23927147Staylor }
23937147Staylor 
23946423Sgw25295 /*
23956423Sgw25295  * Import a root pool.
23966423Sgw25295  *
23977147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
23987147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
23997147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
24006423Sgw25295  *
24016423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
24026423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
24036423Sgw25295  * e.g.
24046423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
24056423Sgw25295  */
24066423Sgw25295 int
24077147Staylor spa_import_rootpool(char *devpath, char *devid)
24086423Sgw25295 {
24096423Sgw25295 	nvlist_t *conf = NULL;
24106423Sgw25295 	char *pname;
24116423Sgw25295 	int error;
24126423Sgw25295 
24136423Sgw25295 	/*
24146423Sgw25295 	 * Get the vdev pathname and configuation from the most
24156423Sgw25295 	 * recently updated vdev (highest txg).
24166423Sgw25295 	 */
24177147Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
24186423Sgw25295 		goto msg_out;
24196423Sgw25295 
24206423Sgw25295 	/*
24216423Sgw25295 	 * Add type "root" vdev to the config.
24226423Sgw25295 	 */
24236423Sgw25295 	spa_build_rootpool_config(conf);
24246423Sgw25295 
24256423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
24266423Sgw25295 
24276673Seschrock 	/*
24286673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
24296673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
24306673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
24316673Seschrock 	 * pool open, not import.
24326673Seschrock 	 */
24336673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
24347897SLin.Ling@Sun.COM 	ASSERT(error != EEXIST);
24356423Sgw25295 
24366423Sgw25295 	nvlist_free(conf);
24376423Sgw25295 	return (error);
24386423Sgw25295 
24396423Sgw25295 msg_out:
24407147Staylor 	cmn_err(CE_NOTE, "\n"
24416423Sgw25295 	    "  ***************************************************  \n"
24426423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
24436423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
24446423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
24457147Staylor 	    "  ***************************************************  ");
24466423Sgw25295 
24476423Sgw25295 	return (error);
24486423Sgw25295 }
24496423Sgw25295 #endif
24506423Sgw25295 
24516423Sgw25295 /*
24526423Sgw25295  * Import a non-root pool into the system.
24536423Sgw25295  */
24546423Sgw25295 int
24556423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
24566423Sgw25295 {
24576643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
24586423Sgw25295 }
24596423Sgw25295 
24606643Seschrock int
24616643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
24626643Seschrock {
24636643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
24646643Seschrock }
24656643Seschrock 
24666643Seschrock 
2467789Sahrens /*
2468789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2469789Sahrens  * to get the vdev stats associated with the imported devices.
2470789Sahrens  */
2471789Sahrens #define	TRYIMPORT_NAME	"$import"
2472789Sahrens 
2473789Sahrens nvlist_t *
2474789Sahrens spa_tryimport(nvlist_t *tryconfig)
2475789Sahrens {
2476789Sahrens 	nvlist_t *config = NULL;
2477789Sahrens 	char *poolname;
2478789Sahrens 	spa_t *spa;
2479789Sahrens 	uint64_t state;
2480789Sahrens 
2481789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2482789Sahrens 		return (NULL);
2483789Sahrens 
2484789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2485789Sahrens 		return (NULL);
2486789Sahrens 
24871635Sbonwick 	/*
24881635Sbonwick 	 * Create and initialize the spa structure.
24891635Sbonwick 	 */
2490789Sahrens 	mutex_enter(&spa_namespace_lock);
24911635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2492789Sahrens 	spa_activate(spa);
2493789Sahrens 
2494789Sahrens 	/*
24951635Sbonwick 	 * Pass off the heavy lifting to spa_load().
24961732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
24971732Sbonwick 	 * is actually the one to trust when doing an import.
2498789Sahrens 	 */
24991732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2500789Sahrens 
2501789Sahrens 	/*
2502789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2503789Sahrens 	 */
2504789Sahrens 	if (spa->spa_root_vdev != NULL) {
2505789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2506789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2507789Sahrens 		    poolname) == 0);
2508789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2509789Sahrens 		    state) == 0);
25103975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
25113975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
25122082Seschrock 
25132082Seschrock 		/*
25146423Sgw25295 		 * If the bootfs property exists on this pool then we
25156423Sgw25295 		 * copy it out so that external consumers can tell which
25166423Sgw25295 		 * pools are bootable.
25176423Sgw25295 		 */
25186423Sgw25295 		if (spa->spa_bootfs) {
25196423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
25206423Sgw25295 
25216423Sgw25295 			/*
25226423Sgw25295 			 * We have to play games with the name since the
25236423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
25246423Sgw25295 			 */
25257754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
25266423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
25276423Sgw25295 				char *cp;
25286423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
25296423Sgw25295 
25306423Sgw25295 				cp = strchr(tmpname, '/');
25316423Sgw25295 				if (cp == NULL) {
25326423Sgw25295 					(void) strlcpy(dsname, tmpname,
25336423Sgw25295 					    MAXPATHLEN);
25346423Sgw25295 				} else {
25356423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
25366423Sgw25295 					    "%s/%s", poolname, ++cp);
25376423Sgw25295 				}
25386423Sgw25295 				VERIFY(nvlist_add_string(config,
25396423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
25406423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
25416423Sgw25295 			}
25426423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
25436423Sgw25295 		}
25446423Sgw25295 
25456423Sgw25295 		/*
25465450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
25472082Seschrock 		 */
25482082Seschrock 		spa_add_spares(spa, config);
25495450Sbrendan 		spa_add_l2cache(spa, config);
2550789Sahrens 	}
2551789Sahrens 
2552789Sahrens 	spa_unload(spa);
2553789Sahrens 	spa_deactivate(spa);
2554789Sahrens 	spa_remove(spa);
2555789Sahrens 	mutex_exit(&spa_namespace_lock);
2556789Sahrens 
2557789Sahrens 	return (config);
2558789Sahrens }
2559789Sahrens 
2560789Sahrens /*
2561789Sahrens  * Pool export/destroy
2562789Sahrens  *
2563789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2564789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2565789Sahrens  * update the pool state and sync all the labels to disk, removing the
2566*8211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
2567*8211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
2568789Sahrens  */
2569789Sahrens static int
25707214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
2571*8211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
2572789Sahrens {
2573789Sahrens 	spa_t *spa;
2574789Sahrens 
25751775Sbillm 	if (oldconfig)
25761775Sbillm 		*oldconfig = NULL;
25771775Sbillm 
2578789Sahrens 	if (!(spa_mode & FWRITE))
2579789Sahrens 		return (EROFS);
2580789Sahrens 
2581789Sahrens 	mutex_enter(&spa_namespace_lock);
2582789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2583789Sahrens 		mutex_exit(&spa_namespace_lock);
2584789Sahrens 		return (ENOENT);
2585789Sahrens 	}
2586789Sahrens 
2587789Sahrens 	/*
25881544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
25891544Seschrock 	 * reacquire the namespace lock, and see if we can export.
25901544Seschrock 	 */
25911544Seschrock 	spa_open_ref(spa, FTAG);
25921544Seschrock 	mutex_exit(&spa_namespace_lock);
25931544Seschrock 	spa_async_suspend(spa);
25941544Seschrock 	mutex_enter(&spa_namespace_lock);
25951544Seschrock 	spa_close(spa, FTAG);
25961544Seschrock 
25971544Seschrock 	/*
2598789Sahrens 	 * The pool will be in core if it's openable,
2599789Sahrens 	 * in which case we can modify its state.
2600789Sahrens 	 */
2601789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2602789Sahrens 		/*
2603789Sahrens 		 * Objsets may be open only because they're dirty, so we
2604789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2605789Sahrens 		 */
2606789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2607789Sahrens 
26081544Seschrock 		/*
26091544Seschrock 		 * A pool cannot be exported or destroyed if there are active
26101544Seschrock 		 * references.  If we are resetting a pool, allow references by
26111544Seschrock 		 * fault injection handlers.
26121544Seschrock 		 */
26131544Seschrock 		if (!spa_refcount_zero(spa) ||
26141544Seschrock 		    (spa->spa_inject_ref != 0 &&
26151544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
26161544Seschrock 			spa_async_resume(spa);
2617789Sahrens 			mutex_exit(&spa_namespace_lock);
2618789Sahrens 			return (EBUSY);
2619789Sahrens 		}
2620789Sahrens 
2621789Sahrens 		/*
26227214Slling 		 * A pool cannot be exported if it has an active shared spare.
26237214Slling 		 * This is to prevent other pools stealing the active spare
26247214Slling 		 * from an exported pool. At user's own will, such pool can
26257214Slling 		 * be forcedly exported.
26267214Slling 		 */
26277214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
26287214Slling 		    spa_has_active_shared_spare(spa)) {
26297214Slling 			spa_async_resume(spa);
26307214Slling 			mutex_exit(&spa_namespace_lock);
26317214Slling 			return (EXDEV);
26327214Slling 		}
26337214Slling 
26347214Slling 		/*
2635789Sahrens 		 * We want this to be reflected on every label,
2636789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2637789Sahrens 		 * final sync that pushes these changes out.
2638789Sahrens 		 */
2639*8211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
26407754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
26411544Seschrock 			spa->spa_state = new_state;
26421635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
26431544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
26447754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
26451544Seschrock 		}
2646789Sahrens 	}
2647789Sahrens 
26484451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
26494451Seschrock 
2650789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2651789Sahrens 		spa_unload(spa);
2652789Sahrens 		spa_deactivate(spa);
2653789Sahrens 	}
2654789Sahrens 
26551775Sbillm 	if (oldconfig && spa->spa_config)
26561775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
26571775Sbillm 
26581544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
2659*8211SGeorge.Wilson@Sun.COM 		if (!hardforce)
2660*8211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
26611544Seschrock 		spa_remove(spa);
26621544Seschrock 	}
2663789Sahrens 	mutex_exit(&spa_namespace_lock);
2664789Sahrens 
2665789Sahrens 	return (0);
2666789Sahrens }
2667789Sahrens 
2668789Sahrens /*
2669789Sahrens  * Destroy a storage pool.
2670789Sahrens  */
2671789Sahrens int
2672789Sahrens spa_destroy(char *pool)
2673789Sahrens {
2674*8211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
2675*8211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
2676789Sahrens }
2677789Sahrens 
2678789Sahrens /*
2679789Sahrens  * Export a storage pool.
2680789Sahrens  */
2681789Sahrens int
2682*8211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
2683*8211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
2684789Sahrens {
2685*8211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
2686*8211SGeorge.Wilson@Sun.COM 	    force, hardforce));
2687789Sahrens }
2688789Sahrens 
2689789Sahrens /*
26901544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
26911544Seschrock  * from the namespace in any way.
26921544Seschrock  */
26931544Seschrock int
26941544Seschrock spa_reset(char *pool)
26951544Seschrock {
26967214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
2697*8211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
26981544Seschrock }
26991544Seschrock 
27001544Seschrock /*
2701789Sahrens  * ==========================================================================
2702789Sahrens  * Device manipulation
2703789Sahrens  * ==========================================================================
2704789Sahrens  */
2705789Sahrens 
2706789Sahrens /*
27074527Sperrin  * Add a device to a storage pool.
2708789Sahrens  */
2709789Sahrens int
2710789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2711789Sahrens {
2712789Sahrens 	uint64_t txg;
27131635Sbonwick 	int c, error;
2714789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
27151585Sbonwick 	vdev_t *vd, *tvd;
27165450Sbrendan 	nvlist_t **spares, **l2cache;
27175450Sbrendan 	uint_t nspares, nl2cache;
2718789Sahrens 
2719789Sahrens 	txg = spa_vdev_enter(spa);
2720789Sahrens 
27212082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
27222082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
27232082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
27242082Seschrock 
27257754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
2726789Sahrens 
27275450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
27285450Sbrendan 	    &nspares) != 0)
27292082Seschrock 		nspares = 0;
27302082Seschrock 
27315450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
27325450Sbrendan 	    &nl2cache) != 0)
27335450Sbrendan 		nl2cache = 0;
27345450Sbrendan 
27357754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
27362082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
27377754SJeff.Bonwick@Sun.COM 
27387754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
27397754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
27407754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
27412082Seschrock 
27423377Seschrock 	/*
27435450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
27445450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
27453377Seschrock 	 */
27467754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
27473377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
27483377Seschrock 
27493377Seschrock 	/*
27503377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
27513377Seschrock 	 */
27523377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
27533377Seschrock 		tvd = vd->vdev_child[c];
27543377Seschrock 		vdev_remove_child(vd, tvd);
27553377Seschrock 		tvd->vdev_id = rvd->vdev_children;
27563377Seschrock 		vdev_add_child(rvd, tvd);
27573377Seschrock 		vdev_config_dirty(tvd);
27583377Seschrock 	}
27593377Seschrock 
27602082Seschrock 	if (nspares != 0) {
27615450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
27625450Sbrendan 		    ZPOOL_CONFIG_SPARES);
27632082Seschrock 		spa_load_spares(spa);
27645450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27655450Sbrendan 	}
27665450Sbrendan 
27675450Sbrendan 	if (nl2cache != 0) {
27685450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
27695450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
27705450Sbrendan 		spa_load_l2cache(spa);
27715450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2772789Sahrens 	}
2773789Sahrens 
2774789Sahrens 	/*
27751585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
27761585Sbonwick 	 * If other threads start allocating from these vdevs before we
27771585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
27781585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
27791585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
27801585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
27811635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
27821585Sbonwick 	 *
27831585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
27841585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
27851585Sbonwick 	 * steps will be completed the next time we load the pool.
2786789Sahrens 	 */
27871635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
27881585Sbonwick 
27891635Sbonwick 	mutex_enter(&spa_namespace_lock);
27901635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
27911635Sbonwick 	mutex_exit(&spa_namespace_lock);
2792789Sahrens 
27931635Sbonwick 	return (0);
2794789Sahrens }
2795789Sahrens 
2796789Sahrens /*
2797789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2798789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2799789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2800789Sahrens  *
2801789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2802789Sahrens  * existing device; in this case the two devices are made into their own
28034451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2804789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2805789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2806789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2807789Sahrens  * is automatically detached.
2808789Sahrens  */
2809789Sahrens int
28101544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2811789Sahrens {
2812789Sahrens 	uint64_t txg, open_txg;
2813789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2814789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
28152082Seschrock 	vdev_ops_t *pvops;
28167313SEric.Kustarz@Sun.COM 	dmu_tx_t *tx;
28177313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
28187313SEric.Kustarz@Sun.COM 	int newvd_isspare;
28197313SEric.Kustarz@Sun.COM 	int error;
2820789Sahrens 
2821789Sahrens 	txg = spa_vdev_enter(spa);
2822789Sahrens 
28236643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2824789Sahrens 
2825789Sahrens 	if (oldvd == NULL)
2826789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2827789Sahrens 
28281585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
28291585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28301585Sbonwick 
2831789Sahrens 	pvd = oldvd->vdev_parent;
2832789Sahrens 
28332082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
28344451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
28354451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
28364451Seschrock 
28374451Seschrock 	if (newrootvd->vdev_children != 1)
2838789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2839789Sahrens 
2840789Sahrens 	newvd = newrootvd->vdev_child[0];
2841789Sahrens 
2842789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2843789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2844789Sahrens 
28452082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2846789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2847789Sahrens 
28484527Sperrin 	/*
28494527Sperrin 	 * Spares can't replace logs
28504527Sperrin 	 */
28517326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
28524527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28534527Sperrin 
28542082Seschrock 	if (!replacing) {
28552082Seschrock 		/*
28562082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
28572082Seschrock 		 * vdev.
28582082Seschrock 		 */
28592082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
28602082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
28612082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28622082Seschrock 
28632082Seschrock 		pvops = &vdev_mirror_ops;
28642082Seschrock 	} else {
28652082Seschrock 		/*
28662082Seschrock 		 * Active hot spares can only be replaced by inactive hot
28672082Seschrock 		 * spares.
28682082Seschrock 		 */
28692082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
28702082Seschrock 		    pvd->vdev_child[1] == oldvd &&
28712082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
28722082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28732082Seschrock 
28742082Seschrock 		/*
28752082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
28762082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
28773377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
28783377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
28793377Seschrock 		 * the same (spare replaces spare, non-spare replaces
28803377Seschrock 		 * non-spare).
28812082Seschrock 		 */
28822082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
28832082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28843377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
28853377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
28863377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28872082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
28882082Seschrock 		    newvd->vdev_isspare)
28892082Seschrock 			pvops = &vdev_spare_ops;
28902082Seschrock 		else
28912082Seschrock 			pvops = &vdev_replacing_ops;
28922082Seschrock 	}
28932082Seschrock 
28941175Slling 	/*
28951175Slling 	 * Compare the new device size with the replaceable/attachable
28961175Slling 	 * device size.
28971175Slling 	 */
28981175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2899789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2900789Sahrens 
29011732Sbonwick 	/*
29021732Sbonwick 	 * The new device cannot have a higher alignment requirement
29031732Sbonwick 	 * than the top-level vdev.
29041732Sbonwick 	 */
29051732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2906789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2907789Sahrens 
2908789Sahrens 	/*
2909789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2910789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2911789Sahrens 	 */
2912789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2913789Sahrens 		spa_strfree(oldvd->vdev_path);
2914789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2915789Sahrens 		    KM_SLEEP);
2916789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2917789Sahrens 		    newvd->vdev_path, "old");
2918789Sahrens 		if (oldvd->vdev_devid != NULL) {
2919789Sahrens 			spa_strfree(oldvd->vdev_devid);
2920789Sahrens 			oldvd->vdev_devid = NULL;
2921789Sahrens 		}
2922789Sahrens 	}
2923789Sahrens 
2924789Sahrens 	/*
29252082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
29262082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2927789Sahrens 	 */
2928789Sahrens 	if (pvd->vdev_ops != pvops)
2929789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2930789Sahrens 
2931789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2932789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2933789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2934789Sahrens 
2935789Sahrens 	/*
2936789Sahrens 	 * Extract the new device from its root and add it to pvd.
2937789Sahrens 	 */
2938789Sahrens 	vdev_remove_child(newrootvd, newvd);
2939789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2940789Sahrens 	vdev_add_child(pvd, newvd);
2941789Sahrens 
29421544Seschrock 	/*
29431544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
29441544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
29451544Seschrock 	 */
29461544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
29471544Seschrock 
2948789Sahrens 	tvd = newvd->vdev_top;
2949789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2950789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2951789Sahrens 
2952789Sahrens 	vdev_config_dirty(tvd);
2953789Sahrens 
2954789Sahrens 	/*
2955789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2956789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2957789Sahrens 	 */
2958789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2959789Sahrens 
2960789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2961789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2962789Sahrens 	    open_txg - TXG_INITIAL + 1);
2963789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2964789Sahrens 
29653377Seschrock 	if (newvd->vdev_isspare)
29663377Seschrock 		spa_spare_activate(newvd);
29677754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
29687754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
29697313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
29701544Seschrock 
2971789Sahrens 	/*
2972789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2973789Sahrens 	 */
29741732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2975789Sahrens 
2976789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2977789Sahrens 
29787313SEric.Kustarz@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
29797313SEric.Kustarz@Sun.COM 	if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
29807313SEric.Kustarz@Sun.COM 		spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
29817313SEric.Kustarz@Sun.COM 		    CRED(),  "%s vdev=%s %s vdev=%s",
29827313SEric.Kustarz@Sun.COM 		    replacing && newvd_isspare ? "spare in" :
29837313SEric.Kustarz@Sun.COM 		    replacing ? "replace" : "attach", newvdpath,
29847313SEric.Kustarz@Sun.COM 		    replacing ? "for" : "to", oldvdpath);
29857313SEric.Kustarz@Sun.COM 		dmu_tx_commit(tx);
29867313SEric.Kustarz@Sun.COM 	} else {
29877313SEric.Kustarz@Sun.COM 		dmu_tx_abort(tx);
29887313SEric.Kustarz@Sun.COM 	}
29897313SEric.Kustarz@Sun.COM 
29907313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
29917313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
29927313SEric.Kustarz@Sun.COM 
2993789Sahrens 	/*
29947046Sahrens 	 * Kick off a resilver to update newvd.
2995789Sahrens 	 */
29967046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
2997789Sahrens 
2998789Sahrens 	return (0);
2999789Sahrens }
3000789Sahrens 
3001789Sahrens /*
3002789Sahrens  * Detach a device from a mirror or replacing vdev.
3003789Sahrens  * If 'replace_done' is specified, only detach if the parent
3004789Sahrens  * is a replacing vdev.
3005789Sahrens  */
3006789Sahrens int
30071544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
3008789Sahrens {
3009789Sahrens 	uint64_t txg;
3010789Sahrens 	int c, t, error;
3011789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3012789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
30132082Seschrock 	boolean_t unspare = B_FALSE;
30142082Seschrock 	uint64_t unspare_guid;
30156673Seschrock 	size_t len;
3016789Sahrens 
3017789Sahrens 	txg = spa_vdev_enter(spa);
3018789Sahrens 
30196643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3020789Sahrens 
3021789Sahrens 	if (vd == NULL)
3022789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3023789Sahrens 
30241585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
30251585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30261585Sbonwick 
3027789Sahrens 	pvd = vd->vdev_parent;
3028789Sahrens 
3029789Sahrens 	/*
3030789Sahrens 	 * If replace_done is specified, only remove this device if it's
30312082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
30322082Seschrock 	 * disk can be removed.
3033789Sahrens 	 */
30342082Seschrock 	if (replace_done) {
30352082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
30362082Seschrock 			if (vd->vdev_id != 0)
30372082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30382082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
30392082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30402082Seschrock 		}
30412082Seschrock 	}
30422082Seschrock 
30432082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
30444577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3045789Sahrens 
3046789Sahrens 	/*
30472082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3048789Sahrens 	 */
3049789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
30502082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
30512082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3052789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3053789Sahrens 
3054789Sahrens 	/*
3055789Sahrens 	 * If there's only one replica, you can't detach it.
3056789Sahrens 	 */
3057789Sahrens 	if (pvd->vdev_children <= 1)
3058789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3059789Sahrens 
3060789Sahrens 	/*
3061789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
3062789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
3063789Sahrens 	 *
3064789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
3065789Sahrens 	 * precise DTL check.
3066789Sahrens 	 */
3067789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
3068789Sahrens 		uint64_t dirty;
3069789Sahrens 
3070789Sahrens 		cvd = pvd->vdev_child[c];
3071789Sahrens 		if (cvd == vd)
3072789Sahrens 			continue;
3073789Sahrens 		if (vdev_is_dead(cvd))
3074789Sahrens 			continue;
3075789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
3076789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
3077789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
3078789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
3079789Sahrens 		if (!dirty)
3080789Sahrens 			break;
3081789Sahrens 	}
30822082Seschrock 
30837754SJeff.Bonwick@Sun.COM 	if (c == pvd->vdev_children)
3084789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3085789Sahrens 
3086789Sahrens 	/*
30876673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
30886673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
30896673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
30906673Seschrock 	 */
30916673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
30926673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
30936673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
30946673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
30956673Seschrock 		cvd = pvd->vdev_child[0];
30966673Seschrock 		len = strlen(vd->vdev_path);
30976673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
30986673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
30996673Seschrock 			spa_strfree(cvd->vdev_path);
31006673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
31016673Seschrock 		}
31026673Seschrock 	}
31036673Seschrock 
31046673Seschrock 	/*
31052082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
31062082Seschrock 	 * that the spare should become a real disk, and be removed from the
31072082Seschrock 	 * active spare list for the pool.
31082082Seschrock 	 */
31092082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
31102082Seschrock 	    vd->vdev_id == 0)
31112082Seschrock 		unspare = B_TRUE;
31122082Seschrock 
31132082Seschrock 	/*
3114789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3115789Sahrens 	 * This must be done after all other error cases are handled,
3116789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3117789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3118789Sahrens 	 * it may be that the unwritability of the disk is the reason
3119789Sahrens 	 * it's being detached!
3120789Sahrens 	 */
31213377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3122789Sahrens 
3123789Sahrens 	/*
3124789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3125789Sahrens 	 */
3126789Sahrens 	vdev_remove_child(pvd, vd);
3127789Sahrens 	vdev_compact_children(pvd);
3128789Sahrens 
3129789Sahrens 	/*
3130789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3131789Sahrens 	 */
3132789Sahrens 	cvd = pvd->vdev_child[0];
3133789Sahrens 
3134789Sahrens 	/*
31352082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
31362082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
31372082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
31382082Seschrock 	 * GUID if it creates a new toplevel GUID.
31392082Seschrock 	 */
31402082Seschrock 	if (unspare) {
31412082Seschrock 		ASSERT(cvd->vdev_isspare);
31423377Seschrock 		spa_spare_remove(cvd);
31432082Seschrock 		unspare_guid = cvd->vdev_guid;
31442082Seschrock 	}
31452082Seschrock 
31462082Seschrock 	/*
3147789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3148789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3149789Sahrens 	 */
3150789Sahrens 	if (pvd->vdev_children == 1)
3151789Sahrens 		vdev_remove_parent(cvd);
3152789Sahrens 
3153789Sahrens 	/*
3154789Sahrens 	 * We don't set tvd until now because the parent we just removed
3155789Sahrens 	 * may have been the previous top-level vdev.
3156789Sahrens 	 */
3157789Sahrens 	tvd = cvd->vdev_top;
3158789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3159789Sahrens 
3160789Sahrens 	/*
31613377Seschrock 	 * Reevaluate the parent vdev state.
3162789Sahrens 	 */
31634451Seschrock 	vdev_propagate_state(cvd);
3164789Sahrens 
3165789Sahrens 	/*
31663377Seschrock 	 * If the device we just detached was smaller than the others, it may be
31673377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
31683377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
31693377Seschrock 	 * there's nothing to read from disk.
3170789Sahrens 	 */
31711732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3172789Sahrens 
3173789Sahrens 	vdev_config_dirty(tvd);
3174789Sahrens 
3175789Sahrens 	/*
31763377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
31773377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
31783377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
31793377Seschrock 	 * prevent vd from being accessed after it's freed.
3180789Sahrens 	 */
3181789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
3182789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
31831732Sbonwick 	vd->vdev_detached = B_TRUE;
31841732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3185789Sahrens 
31864451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
31874451Seschrock 
31882082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
31892082Seschrock 
31902082Seschrock 	/*
31913377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
31923377Seschrock 	 * then we want to go through and remove the device from the hot spare
31933377Seschrock 	 * list of every other pool.
31942082Seschrock 	 */
31952082Seschrock 	if (unspare) {
31962082Seschrock 		spa = NULL;
31972082Seschrock 		mutex_enter(&spa_namespace_lock);
31982082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
31992082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
32002082Seschrock 				continue;
32017793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
32027793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
32032082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
32047793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
32057793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
32062082Seschrock 		}
32072082Seschrock 		mutex_exit(&spa_namespace_lock);
32082082Seschrock 	}
32092082Seschrock 
32102082Seschrock 	return (error);
32112082Seschrock }
32122082Seschrock 
32137754SJeff.Bonwick@Sun.COM static nvlist_t *
32147754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
32152082Seschrock {
32167754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
32177754SJeff.Bonwick@Sun.COM 		uint64_t guid;
32187754SJeff.Bonwick@Sun.COM 
32197754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
32207754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
32217754SJeff.Bonwick@Sun.COM 
32227754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
32237754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
32242082Seschrock 	}
32252082Seschrock 
32267754SJeff.Bonwick@Sun.COM 	return (NULL);
32275450Sbrendan }
32285450Sbrendan 
32297754SJeff.Bonwick@Sun.COM static void
32307754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
32317754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
32325450Sbrendan {
32337754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
32347754SJeff.Bonwick@Sun.COM 
32357754SJeff.Bonwick@Sun.COM 	if (count > 1)
32367754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
32377754SJeff.Bonwick@Sun.COM 
32387754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
32397754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
32407754SJeff.Bonwick@Sun.COM 			continue;
32417754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
32425450Sbrendan 	}
32435450Sbrendan 
32447754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
32457754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
32467754SJeff.Bonwick@Sun.COM 
32477754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
32487754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
32497754SJeff.Bonwick@Sun.COM 
32507754SJeff.Bonwick@Sun.COM 	if (count > 1)
32517754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
32525450Sbrendan }
32535450Sbrendan 
32545450Sbrendan /*
32555450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
32565450Sbrendan  * spares and level 2 ARC devices.
32575450Sbrendan  */
32585450Sbrendan int
32595450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
32605450Sbrendan {
32615450Sbrendan 	vdev_t *vd;
32627754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
32635450Sbrendan 	uint_t nspares, nl2cache;
32647754SJeff.Bonwick@Sun.COM 	uint64_t txg;
32655450Sbrendan 	int error = 0;
32665450Sbrendan 
32677754SJeff.Bonwick@Sun.COM 	txg = spa_vdev_enter(spa);
32685450Sbrendan 
32696643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
32705450Sbrendan 
32715450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
32725450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
32737754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
32747754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
32757754SJeff.Bonwick@Sun.COM 		/*
32767754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
32777754SJeff.Bonwick@Sun.COM 		 * in this pool.
32787754SJeff.Bonwick@Sun.COM 		 */
32797754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
32807754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
32817754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
32827754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
32837754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
32847754SJeff.Bonwick@Sun.COM 		} else {
32857754SJeff.Bonwick@Sun.COM 			error = EBUSY;
32867754SJeff.Bonwick@Sun.COM 		}
32877754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
32885450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
32897754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
32907754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
32917754SJeff.Bonwick@Sun.COM 		/*
32927754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
32937754SJeff.Bonwick@Sun.COM 		 */
32947754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
32957754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
32965450Sbrendan 		spa_load_l2cache(spa);
32975450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
32987754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
32997754SJeff.Bonwick@Sun.COM 		/*
33007754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
33017754SJeff.Bonwick@Sun.COM 		 */
33027754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
33037754SJeff.Bonwick@Sun.COM 	} else {
33047754SJeff.Bonwick@Sun.COM 		/*
33057754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
33067754SJeff.Bonwick@Sun.COM 		 */
33077754SJeff.Bonwick@Sun.COM 		error = ENOENT;
33085450Sbrendan 	}
33092082Seschrock 
33107754SJeff.Bonwick@Sun.COM 	return (spa_vdev_exit(spa, NULL, txg, error));
3311789Sahrens }
3312789Sahrens 
3313789Sahrens /*
33144451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
33154451Seschrock  * current spared, so we can detach it.
3316789Sahrens  */
33171544Seschrock static vdev_t *
33184451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3319789Sahrens {
33201544Seschrock 	vdev_t *newvd, *oldvd;
3321789Sahrens 	int c;
3322789Sahrens 
33231544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
33244451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
33251544Seschrock 		if (oldvd != NULL)
33261544Seschrock 			return (oldvd);
33271544Seschrock 	}
3328789Sahrens 
33294451Seschrock 	/*
33304451Seschrock 	 * Check for a completed replacement.
33314451Seschrock 	 */
3332789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
33331544Seschrock 		oldvd = vd->vdev_child[0];
33341544Seschrock 		newvd = vd->vdev_child[1];
3335789Sahrens 
33361544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33371544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
33381544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33391544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33401544Seschrock 			return (oldvd);
33411544Seschrock 		}
33421544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33431544Seschrock 	}
3344789Sahrens 
33454451Seschrock 	/*
33464451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
33474451Seschrock 	 */
33484451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
33494451Seschrock 		newvd = vd->vdev_child[0];
33504451Seschrock 		oldvd = vd->vdev_child[1];
33514451Seschrock 
33524451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33534451Seschrock 		if (newvd->vdev_unspare &&
33544451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
33554451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33564451Seschrock 			newvd->vdev_unspare = 0;
33574451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33584451Seschrock 			return (oldvd);
33594451Seschrock 		}
33604451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33614451Seschrock 	}
33624451Seschrock 
33631544Seschrock 	return (NULL);
3364789Sahrens }
3365789Sahrens 
33661544Seschrock static void
33674451Seschrock spa_vdev_resilver_done(spa_t *spa)
3368789Sahrens {
33691544Seschrock 	vdev_t *vd;
33702082Seschrock 	vdev_t *pvd;
33711544Seschrock 	uint64_t guid;
33722082Seschrock 	uint64_t pguid = 0;
3373789Sahrens 
33747754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3375789Sahrens 
33764451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
33771544Seschrock 		guid = vd->vdev_guid;
33782082Seschrock 		/*
33792082Seschrock 		 * If we have just finished replacing a hot spared device, then
33802082Seschrock 		 * we need to detach the parent's first child (the original hot
33812082Seschrock 		 * spare) as well.
33822082Seschrock 		 */
33832082Seschrock 		pvd = vd->vdev_parent;
33842082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
33852082Seschrock 		    pvd->vdev_id == 0) {
33862082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
33872082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
33882082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
33892082Seschrock 		}
33907754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
33911544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
33921544Seschrock 			return;
33932082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
33942082Seschrock 			return;
33957754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3396789Sahrens 	}
3397789Sahrens 
33987754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3399789Sahrens }
3400789Sahrens 
3401789Sahrens /*
34021354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
34031354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
34041354Seschrock  */
34051354Seschrock int
34061354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
34071354Seschrock {
34086643Seschrock 	vdev_t *vd;
34091354Seschrock 	uint64_t txg;
34101354Seschrock 
34111354Seschrock 	txg = spa_vdev_enter(spa);
34121354Seschrock 
34136643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
34142082Seschrock 		/*
34156643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
34166643Seschrock 		 * it is, update the path manually as there is no associated
34176643Seschrock 		 * vdev_t that can be synced to disk.
34182082Seschrock 		 */
34196643Seschrock 		nvlist_t **spares;
34206643Seschrock 		uint_t i, nspares;
34215450Sbrendan 
34225450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
34235450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
34245450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
34255450Sbrendan 			    &spares, &nspares) == 0);
34262082Seschrock 			for (i = 0; i < nspares; i++) {
34272082Seschrock 				uint64_t theguid;
34282082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
34292082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
34305450Sbrendan 				if (theguid == guid) {
34315450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
34325450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
34335450Sbrendan 					spa_load_spares(spa);
34345450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
34355450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
34365450Sbrendan 					    0));
34375450Sbrendan 				}
34382082Seschrock 			}
34392082Seschrock 		}
34405450Sbrendan 
34415450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
34422082Seschrock 	}
34431354Seschrock 
34441585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
34451585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34461585Sbonwick 
34471354Seschrock 	spa_strfree(vd->vdev_path);
34481354Seschrock 	vd->vdev_path = spa_strdup(newpath);
34491354Seschrock 
34501354Seschrock 	vdev_config_dirty(vd->vdev_top);
34511354Seschrock 
34521354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
34531354Seschrock }
34541354Seschrock 
34551354Seschrock /*
3456789Sahrens  * ==========================================================================
3457789Sahrens  * SPA Scrubbing
3458789Sahrens  * ==========================================================================
3459789Sahrens  */
3460789Sahrens 
34617046Sahrens int
34627046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3463789Sahrens {
34647754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
34654808Sek110237 
3466789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3467789Sahrens 		return (ENOTSUP);
3468789Sahrens 
3469789Sahrens 	/*
34707046Sahrens 	 * If a resilver was requested, but there is no DTL on a
34717046Sahrens 	 * writeable leaf device, we have nothing to do.
3472789Sahrens 	 */
34737046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
34747046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
34757046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
34761544Seschrock 		return (0);
34771544Seschrock 	}
3478789Sahrens 
34797046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
34807046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
34817046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
34827046Sahrens 		return (EBUSY);
34837046Sahrens 
34847046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
34857046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
34867046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
34877046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
34881544Seschrock 	} else {
34897046Sahrens 		return (EINVAL);
34901544Seschrock 	}
3491789Sahrens }
3492789Sahrens 
34931544Seschrock /*
34941544Seschrock  * ==========================================================================
34951544Seschrock  * SPA async task processing
34961544Seschrock  * ==========================================================================
34971544Seschrock  */
34981544Seschrock 
34991544Seschrock static void
35004451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3501789Sahrens {
35027361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
35037361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
35047361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
35057754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd);
35067754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
35071544Seschrock 	}
35087361SBrendan.Gregg@Sun.COM 
35097754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
35107361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
35111544Seschrock }
35121544Seschrock 
35131544Seschrock static void
35147754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
35157754SJeff.Bonwick@Sun.COM {
35167754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
35177754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
35187754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
35197754SJeff.Bonwick@Sun.COM 	}
35207754SJeff.Bonwick@Sun.COM 
35217754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
35227754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
35237754SJeff.Bonwick@Sun.COM }
35247754SJeff.Bonwick@Sun.COM 
35257754SJeff.Bonwick@Sun.COM static void
35261544Seschrock spa_async_thread(spa_t *spa)
35271544Seschrock {
35287754SJeff.Bonwick@Sun.COM 	int tasks;
35291544Seschrock 
35301544Seschrock 	ASSERT(spa->spa_sync_on);
3531789Sahrens 
35321544Seschrock 	mutex_enter(&spa->spa_async_lock);
35331544Seschrock 	tasks = spa->spa_async_tasks;
35341544Seschrock 	spa->spa_async_tasks = 0;
35351544Seschrock 	mutex_exit(&spa->spa_async_lock);
35361544Seschrock 
35371544Seschrock 	/*
35381635Sbonwick 	 * See if the config needs to be updated.
35391635Sbonwick 	 */
35401635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
35411635Sbonwick 		mutex_enter(&spa_namespace_lock);
35421635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
35431635Sbonwick 		mutex_exit(&spa_namespace_lock);
35441635Sbonwick 	}
35451635Sbonwick 
35461635Sbonwick 	/*
35474451Seschrock 	 * See if any devices need to be marked REMOVED.
35481544Seschrock 	 */
35497754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
35507754SJeff.Bonwick@Sun.COM 		spa_vdev_state_enter(spa);
35514451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
35527754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
35537361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
35547754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
35557361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
35567754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
35577754SJeff.Bonwick@Sun.COM 	}
35587754SJeff.Bonwick@Sun.COM 
35597754SJeff.Bonwick@Sun.COM 	/*
35607754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
35617754SJeff.Bonwick@Sun.COM 	 */
35627754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
35637754SJeff.Bonwick@Sun.COM 		spa_vdev_state_enter(spa);
35647754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
35657754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
35664451Seschrock 	}
35671544Seschrock 
35681544Seschrock 	/*
35691544Seschrock 	 * If any devices are done replacing, detach them.
35701544Seschrock 	 */
35714451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
35724451Seschrock 		spa_vdev_resilver_done(spa);
3573789Sahrens 
35741544Seschrock 	/*
35751544Seschrock 	 * Kick off a resilver.
35761544Seschrock 	 */
35777046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
35787046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
35791544Seschrock 
35801544Seschrock 	/*
35811544Seschrock 	 * Let the world know that we're done.
35821544Seschrock 	 */
35831544Seschrock 	mutex_enter(&spa->spa_async_lock);
35841544Seschrock 	spa->spa_async_thread = NULL;
35851544Seschrock 	cv_broadcast(&spa->spa_async_cv);
35861544Seschrock 	mutex_exit(&spa->spa_async_lock);
35871544Seschrock 	thread_exit();
35881544Seschrock }
35891544Seschrock 
35901544Seschrock void
35911544Seschrock spa_async_suspend(spa_t *spa)
35921544Seschrock {
35931544Seschrock 	mutex_enter(&spa->spa_async_lock);
35941544Seschrock 	spa->spa_async_suspended++;
35951544Seschrock 	while (spa->spa_async_thread != NULL)
35961544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
35971544Seschrock 	mutex_exit(&spa->spa_async_lock);
35981544Seschrock }
35991544Seschrock 
36001544Seschrock void
36011544Seschrock spa_async_resume(spa_t *spa)
36021544Seschrock {
36031544Seschrock 	mutex_enter(&spa->spa_async_lock);
36041544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
36051544Seschrock 	spa->spa_async_suspended--;
36061544Seschrock 	mutex_exit(&spa->spa_async_lock);
36071544Seschrock }
36081544Seschrock 
36091544Seschrock static void
36101544Seschrock spa_async_dispatch(spa_t *spa)
36111544Seschrock {
36121544Seschrock 	mutex_enter(&spa->spa_async_lock);
36131544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
36141635Sbonwick 	    spa->spa_async_thread == NULL &&
36151635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
36161544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
36171544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
36181544Seschrock 	mutex_exit(&spa->spa_async_lock);
36191544Seschrock }
36201544Seschrock 
36211544Seschrock void
36221544Seschrock spa_async_request(spa_t *spa, int task)
36231544Seschrock {
36241544Seschrock 	mutex_enter(&spa->spa_async_lock);
36251544Seschrock 	spa->spa_async_tasks |= task;
36261544Seschrock 	mutex_exit(&spa->spa_async_lock);
3627789Sahrens }
3628789Sahrens 
3629789Sahrens /*
3630789Sahrens  * ==========================================================================
3631789Sahrens  * SPA syncing routines
3632789Sahrens  * ==========================================================================
3633789Sahrens  */
3634789Sahrens 
3635789Sahrens static void
3636789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3637789Sahrens {
3638789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3639789Sahrens 	dmu_tx_t *tx;
3640789Sahrens 	blkptr_t blk;
3641789Sahrens 	uint64_t itor = 0;
3642789Sahrens 	zio_t *zio;
3643789Sahrens 	int error;
3644789Sahrens 	uint8_t c = 1;
3645789Sahrens 
36467754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
36477754SJeff.Bonwick@Sun.COM 
36487754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
36497754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
36507754SJeff.Bonwick@Sun.COM 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
36517754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
36527754SJeff.Bonwick@Sun.COM 	}
3653789Sahrens 
3654789Sahrens 	error = zio_wait(zio);
3655789Sahrens 	ASSERT3U(error, ==, 0);
3656789Sahrens 
3657789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3658789Sahrens 	bplist_vacate(bpl, tx);
3659789Sahrens 
3660789Sahrens 	/*
3661789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3662789Sahrens 	 * (Usually only the first block is needed.)
3663789Sahrens 	 */
3664789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3665789Sahrens 	dmu_tx_commit(tx);
3666789Sahrens }
3667789Sahrens 
3668789Sahrens static void
36692082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
36702082Seschrock {
36712082Seschrock 	char *packed = NULL;
36727497STim.Haley@Sun.COM 	size_t bufsize;
36732082Seschrock 	size_t nvsize = 0;
36742082Seschrock 	dmu_buf_t *db;
36752082Seschrock 
36762082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
36772082Seschrock 
36787497STim.Haley@Sun.COM 	/*
36797497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
36807497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
36817497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
36827497STim.Haley@Sun.COM 	 */
36837497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
36847497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
36852082Seschrock 
36862082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
36872082Seschrock 	    KM_SLEEP) == 0);
36887497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
36897497STim.Haley@Sun.COM 
36907497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
36917497STim.Haley@Sun.COM 
36927497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
36932082Seschrock 
36942082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
36952082Seschrock 	dmu_buf_will_dirty(db, tx);
36962082Seschrock 	*(uint64_t *)db->db_data = nvsize;
36972082Seschrock 	dmu_buf_rele(db, FTAG);
36982082Seschrock }
36992082Seschrock 
37002082Seschrock static void
37015450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
37025450Sbrendan     const char *config, const char *entry)
37032082Seschrock {
37042082Seschrock 	nvlist_t *nvroot;
37055450Sbrendan 	nvlist_t **list;
37062082Seschrock 	int i;
37072082Seschrock 
37085450Sbrendan 	if (!sav->sav_sync)
37092082Seschrock 		return;
37102082Seschrock 
37112082Seschrock 	/*
37125450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
37135450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
37144451Seschrock 	 * valid and the vdevs are labeled appropriately.
37152082Seschrock 	 */
37165450Sbrendan 	if (sav->sav_object == 0) {
37175450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
37185450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
37195450Sbrendan 		    sizeof (uint64_t), tx);
37202082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
37215450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
37225450Sbrendan 		    &sav->sav_object, tx) == 0);
37232082Seschrock 	}
37242082Seschrock 
37252082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
37265450Sbrendan 	if (sav->sav_count == 0) {
37275450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
37282082Seschrock 	} else {
37295450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
37305450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
37315450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
37325450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
37335450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
37345450Sbrendan 		    sav->sav_count) == 0);
37355450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
37365450Sbrendan 			nvlist_free(list[i]);
37375450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
37382082Seschrock 	}
37392082Seschrock 
37405450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
37412926Sek110237 	nvlist_free(nvroot);
37422082Seschrock 
37435450Sbrendan 	sav->sav_sync = B_FALSE;
37442082Seschrock }
37452082Seschrock 
37462082Seschrock static void
3747789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3748789Sahrens {
3749789Sahrens 	nvlist_t *config;
3750789Sahrens 
37517754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
3752789Sahrens 		return;
3753789Sahrens 
37547754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
37557754SJeff.Bonwick@Sun.COM 
37567754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
37577754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
37587754SJeff.Bonwick@Sun.COM 
37597754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
3760789Sahrens 
37611635Sbonwick 	if (spa->spa_config_syncing)
37621635Sbonwick 		nvlist_free(spa->spa_config_syncing);
37631635Sbonwick 	spa->spa_config_syncing = config;
3764789Sahrens 
37652082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3766789Sahrens }
3767789Sahrens 
37685094Slling /*
37695094Slling  * Set zpool properties.
37705094Slling  */
37713912Slling static void
37724543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
37733912Slling {
37743912Slling 	spa_t *spa = arg1;
37755094Slling 	objset_t *mos = spa->spa_meta_objset;
37763912Slling 	nvlist_t *nvp = arg2;
37775094Slling 	nvpair_t *elem;
37784451Seschrock 	uint64_t intval;
37796643Seschrock 	char *strval;
37805094Slling 	zpool_prop_t prop;
37815094Slling 	const char *propname;
37825094Slling 	zprop_type_t proptype;
37836643Seschrock 	spa_config_dirent_t *dp;
37845094Slling 
37857754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
37867754SJeff.Bonwick@Sun.COM 
37875094Slling 	elem = NULL;
37885094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
37895094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
37905094Slling 		case ZPOOL_PROP_VERSION:
37915094Slling 			/*
37925094Slling 			 * Only set version for non-zpool-creation cases
37935094Slling 			 * (set/import). spa_create() needs special care
37945094Slling 			 * for version setting.
37955094Slling 			 */
37965094Slling 			if (tx->tx_txg != TXG_INITIAL) {
37975094Slling 				VERIFY(nvpair_value_uint64(elem,
37985094Slling 				    &intval) == 0);
37995094Slling 				ASSERT(intval <= SPA_VERSION);
38005094Slling 				ASSERT(intval >= spa_version(spa));
38015094Slling 				spa->spa_uberblock.ub_version = intval;
38025094Slling 				vdev_config_dirty(spa->spa_root_vdev);
38035094Slling 			}
38045094Slling 			break;
38055094Slling 
38065094Slling 		case ZPOOL_PROP_ALTROOT:
38075094Slling 			/*
38085094Slling 			 * 'altroot' is a non-persistent property. It should
38095094Slling 			 * have been set temporarily at creation or import time.
38105094Slling 			 */
38115094Slling 			ASSERT(spa->spa_root != NULL);
38125094Slling 			break;
38135094Slling 
38145363Seschrock 		case ZPOOL_PROP_CACHEFILE:
38155094Slling 			/*
38165363Seschrock 			 * 'cachefile' is a non-persistent property, but note
38175363Seschrock 			 * an async request that the config cache needs to be
38185363Seschrock 			 * udpated.
38195094Slling 			 */
38205363Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
38216643Seschrock 
38227754SJeff.Bonwick@Sun.COM 			dp = kmem_alloc(sizeof (spa_config_dirent_t), KM_SLEEP);
38236643Seschrock 
38246643Seschrock 			if (strval[0] == '\0')
38256643Seschrock 				dp->scd_path = spa_strdup(spa_config_path);
38266643Seschrock 			else if (strcmp(strval, "none") == 0)
38276643Seschrock 				dp->scd_path = NULL;
38286643Seschrock 			else
38296643Seschrock 				dp->scd_path = spa_strdup(strval);
38306643Seschrock 
38316643Seschrock 			list_insert_head(&spa->spa_config_list, dp);
38325363Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
38334543Smarks 			break;
38345094Slling 		default:
38355094Slling 			/*
38365094Slling 			 * Set pool property values in the poolprops mos object.
38375094Slling 			 */
38385094Slling 			if (spa->spa_pool_props_object == 0) {
38395094Slling 				objset_t *mos = spa->spa_meta_objset;
38405094Slling 
38415094Slling 				VERIFY((spa->spa_pool_props_object =
38425094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
38435094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
38445094Slling 
38455094Slling 				VERIFY(zap_update(mos,
38465094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
38475094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
38485094Slling 				    == 0);
38495094Slling 			}
38505094Slling 
38515094Slling 			/* normalize the property name */
38525094Slling 			propname = zpool_prop_to_name(prop);
38535094Slling 			proptype = zpool_prop_get_type(prop);
38545094Slling 
38555094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
38565094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
38575094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
38585094Slling 				VERIFY(zap_update(mos,
38595094Slling 				    spa->spa_pool_props_object, propname,
38605094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
38615094Slling 
38625094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
38635094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
38645094Slling 
38655094Slling 				if (proptype == PROP_TYPE_INDEX) {
38665094Slling 					const char *unused;
38675094Slling 					VERIFY(zpool_prop_index_to_string(
38685094Slling 					    prop, intval, &unused) == 0);
38695094Slling 				}
38705094Slling 				VERIFY(zap_update(mos,
38715094Slling 				    spa->spa_pool_props_object, propname,
38725094Slling 				    8, 1, &intval, tx) == 0);
38735094Slling 			} else {
38745094Slling 				ASSERT(0); /* not allowed */
38755094Slling 			}
38765094Slling 
38775329Sgw25295 			switch (prop) {
38785329Sgw25295 			case ZPOOL_PROP_DELEGATION:
38795094Slling 				spa->spa_delegation = intval;
38805329Sgw25295 				break;
38815329Sgw25295 			case ZPOOL_PROP_BOOTFS:
38825094Slling 				spa->spa_bootfs = intval;
38835329Sgw25295 				break;
38845329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
38855329Sgw25295 				spa->spa_failmode = intval;
38865329Sgw25295 				break;
38875329Sgw25295 			default:
38885329Sgw25295 				break;
38895329Sgw25295 			}
38903912Slling 		}
38915094Slling 
38925094Slling 		/* log internal history if this is not a zpool create */
38935094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
38945094Slling 		    tx->tx_txg != TXG_INITIAL) {
38955094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
38965094Slling 			    spa, tx, cr, "%s %lld %s",
38977754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
38985094Slling 		}
38993912Slling 	}
39007754SJeff.Bonwick@Sun.COM 
39017754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
39023912Slling }
39033912Slling 
3904789Sahrens /*
3905789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3906789Sahrens  * part of the process, so we iterate until it converges.
3907789Sahrens  */
3908789Sahrens void
3909789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3910789Sahrens {
3911789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3912789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3913789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
39141635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3915789Sahrens 	vdev_t *vd;
3916789Sahrens 	dmu_tx_t *tx;
3917789Sahrens 	int dirty_vdevs;
39187754SJeff.Bonwick@Sun.COM 	int error;
3919789Sahrens 
3920789Sahrens 	/*
3921789Sahrens 	 * Lock out configuration changes.
3922789Sahrens 	 */
39237754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3924789Sahrens 
3925789Sahrens 	spa->spa_syncing_txg = txg;
3926789Sahrens 	spa->spa_sync_pass = 0;
3927789Sahrens 
39287754SJeff.Bonwick@Sun.COM 	/*
39297754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
39307754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
39317754SJeff.Bonwick@Sun.COM 	 */
39327754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
39337754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
39347754SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
39357754SJeff.Bonwick@Sun.COM 		vdev_config_dirty(vd);
39367754SJeff.Bonwick@Sun.COM 	}
39377754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
39387754SJeff.Bonwick@Sun.COM 
39391544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3940789Sahrens 
39412082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
39422082Seschrock 
39432082Seschrock 	/*
39444577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
39452082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
39462082Seschrock 	 */
39474577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
39484577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
39492082Seschrock 		int i;
39502082Seschrock 
39512082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
39522082Seschrock 			vd = rvd->vdev_child[i];
39532082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
39542082Seschrock 				break;
39552082Seschrock 		}
39562082Seschrock 		if (i == rvd->vdev_children) {
39572082Seschrock 			spa->spa_deflate = TRUE;
39582082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
39592082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
39602082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
39612082Seschrock 		}
39622082Seschrock 	}
39632082Seschrock 
39647046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
39657046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
39667046Sahrens 		dsl_pool_create_origin(dp, tx);
39677046Sahrens 
39687046Sahrens 		/* Keeping the origin open increases spa_minref */
39697046Sahrens 		spa->spa_minref += 3;
39707046Sahrens 	}
39717046Sahrens 
39727046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
39737046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
39747046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
39757046Sahrens 	}
39767046Sahrens 
3977789Sahrens 	/*
3978789Sahrens 	 * If anything has changed in this txg, push the deferred frees
3979789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
3980789Sahrens 	 * don't generate work on an otherwise idle system.
3981789Sahrens 	 */
3982789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
39832329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
39842329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
3985789Sahrens 		spa_sync_deferred_frees(spa, txg);
3986789Sahrens 
3987789Sahrens 	/*
3988789Sahrens 	 * Iterate to convergence.
3989789Sahrens 	 */
3990789Sahrens 	do {
3991789Sahrens 		spa->spa_sync_pass++;
3992789Sahrens 
3993789Sahrens 		spa_sync_config_object(spa, tx);
39945450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
39955450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
39965450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
39975450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
39981544Seschrock 		spa_errlog_sync(spa, txg);
3999789Sahrens 		dsl_pool_sync(dp, txg);
4000789Sahrens 
4001789Sahrens 		dirty_vdevs = 0;
4002789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4003789Sahrens 			vdev_sync(vd, txg);
4004789Sahrens 			dirty_vdevs++;
4005789Sahrens 		}
4006789Sahrens 
4007789Sahrens 		bplist_sync(bpl, tx);
4008789Sahrens 	} while (dirty_vdevs);
4009789Sahrens 
4010789Sahrens 	bplist_close(bpl);
4011789Sahrens 
4012789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4013789Sahrens 
4014789Sahrens 	/*
4015789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4016789Sahrens 	 * to commit the transaction group.
40171635Sbonwick 	 *
40185688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
40195688Sbonwick 	 * random top-level vdevs that are known to be visible in the
40207754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
40217754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4022789Sahrens 	 */
40237754SJeff.Bonwick@Sun.COM 	for (;;) {
40247754SJeff.Bonwick@Sun.COM 		/*
40257754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
40267754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
40277754SJeff.Bonwick@Sun.COM 		 */
40287754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
40297754SJeff.Bonwick@Sun.COM 
40307754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
40317754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
40327754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
40337754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
40347754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
40357754SJeff.Bonwick@Sun.COM 			int c;
40367754SJeff.Bonwick@Sun.COM 
40377754SJeff.Bonwick@Sun.COM 			for (c = 0; c < children; c++) {
40387754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
40397754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
40407754SJeff.Bonwick@Sun.COM 					continue;
40417754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
40427754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
40437754SJeff.Bonwick@Sun.COM 					break;
40447754SJeff.Bonwick@Sun.COM 			}
40457754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg);
40467754SJeff.Bonwick@Sun.COM 		} else {
40477754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
40487754SJeff.Bonwick@Sun.COM 			    rvd->vdev_children, txg);
40491635Sbonwick 		}
40507754SJeff.Bonwick@Sun.COM 
40517754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
40527754SJeff.Bonwick@Sun.COM 
40537754SJeff.Bonwick@Sun.COM 		if (error == 0)
40547754SJeff.Bonwick@Sun.COM 			break;
40557754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
40567754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
40571635Sbonwick 	}
40582082Seschrock 	dmu_tx_commit(tx);
40592082Seschrock 
40601635Sbonwick 	/*
40611635Sbonwick 	 * Clear the dirty config list.
40621635Sbonwick 	 */
40637754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
40641635Sbonwick 		vdev_config_clean(vd);
40651635Sbonwick 
40661635Sbonwick 	/*
40671635Sbonwick 	 * Now that the new config has synced transactionally,
40681635Sbonwick 	 * let it become visible to the config cache.
40691635Sbonwick 	 */
40701635Sbonwick 	if (spa->spa_config_syncing != NULL) {
40711635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
40721635Sbonwick 		spa->spa_config_txg = txg;
40731635Sbonwick 		spa->spa_config_syncing = NULL;
40741635Sbonwick 	}
4075789Sahrens 
4076789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4077789Sahrens 
4078789Sahrens 	/*
4079789Sahrens 	 * Clean up the ZIL records for the synced txg.
4080789Sahrens 	 */
4081789Sahrens 	dsl_pool_zil_clean(dp);
4082789Sahrens 
4083789Sahrens 	/*
4084789Sahrens 	 * Update usable space statistics.
4085789Sahrens 	 */
4086789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4087789Sahrens 		vdev_sync_done(vd, txg);
4088789Sahrens 
4089789Sahrens 	/*
4090789Sahrens 	 * It had better be the case that we didn't dirty anything
40912082Seschrock 	 * since vdev_config_sync().
4092789Sahrens 	 */
4093789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4094789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4095789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4096789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4097789Sahrens 
40987754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
40991544Seschrock 
41001544Seschrock 	/*
41011544Seschrock 	 * If any async tasks have been requested, kick them off.
41021544Seschrock 	 */
41031544Seschrock 	spa_async_dispatch(spa);
4104789Sahrens }
4105789Sahrens 
4106789Sahrens /*
4107789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4108789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4109789Sahrens  * sync.
4110789Sahrens  */
4111789Sahrens void
4112789Sahrens spa_sync_allpools(void)
4113789Sahrens {
4114789Sahrens 	spa_t *spa = NULL;
4115789Sahrens 	mutex_enter(&spa_namespace_lock);
4116789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
41177754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4118789Sahrens 			continue;
4119789Sahrens 		spa_open_ref(spa, FTAG);
4120789Sahrens 		mutex_exit(&spa_namespace_lock);
4121789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4122789Sahrens 		mutex_enter(&spa_namespace_lock);
4123789Sahrens 		spa_close(spa, FTAG);
4124789Sahrens 	}
4125789Sahrens 	mutex_exit(&spa_namespace_lock);
4126789Sahrens }
4127789Sahrens 
4128789Sahrens /*
4129789Sahrens  * ==========================================================================
4130789Sahrens  * Miscellaneous routines
4131789Sahrens  * ==========================================================================
4132789Sahrens  */
4133789Sahrens 
4134789Sahrens /*
4135789Sahrens  * Remove all pools in the system.
4136789Sahrens  */
4137789Sahrens void
4138789Sahrens spa_evict_all(void)
4139789Sahrens {
4140789Sahrens 	spa_t *spa;
4141789Sahrens 
4142789Sahrens 	/*
4143789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4144789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4145789Sahrens 	 */
4146789Sahrens 	mutex_enter(&spa_namespace_lock);
4147789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4148789Sahrens 		/*
41491544Seschrock 		 * Stop async tasks.  The async thread may need to detach
41501544Seschrock 		 * a device that's been replaced, which requires grabbing
41511544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4152789Sahrens 		 */
4153789Sahrens 		spa_open_ref(spa, FTAG);
4154789Sahrens 		mutex_exit(&spa_namespace_lock);
41551544Seschrock 		spa_async_suspend(spa);
41564808Sek110237 		mutex_enter(&spa_namespace_lock);
4157789Sahrens 		spa_close(spa, FTAG);
4158789Sahrens 
4159789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4160789Sahrens 			spa_unload(spa);
4161789Sahrens 			spa_deactivate(spa);
4162789Sahrens 		}
4163789Sahrens 		spa_remove(spa);
4164789Sahrens 	}
4165789Sahrens 	mutex_exit(&spa_namespace_lock);
4166789Sahrens }
41671544Seschrock 
41681544Seschrock vdev_t *
41696643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
41701544Seschrock {
41716643Seschrock 	vdev_t *vd;
41726643Seschrock 	int i;
41736643Seschrock 
41746643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
41756643Seschrock 		return (vd);
41766643Seschrock 
41776643Seschrock 	if (l2cache) {
41786643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
41796643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
41806643Seschrock 			if (vd->vdev_guid == guid)
41816643Seschrock 				return (vd);
41826643Seschrock 		}
41836643Seschrock 	}
41846643Seschrock 
41856643Seschrock 	return (NULL);
41861544Seschrock }
41871760Seschrock 
41881760Seschrock void
41895094Slling spa_upgrade(spa_t *spa, uint64_t version)
41901760Seschrock {
41917754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
41921760Seschrock 
41931760Seschrock 	/*
41941760Seschrock 	 * This should only be called for a non-faulted pool, and since a
41951760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
41961760Seschrock 	 * possible.
41971760Seschrock 	 */
41984577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
41995094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
42005094Slling 
42015094Slling 	spa->spa_uberblock.ub_version = version;
42021760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
42031760Seschrock 
42047754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
42052082Seschrock 
42062082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
42071760Seschrock }
42082082Seschrock 
42092082Seschrock boolean_t
42102082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
42112082Seschrock {
42122082Seschrock 	int i;
42133377Seschrock 	uint64_t spareguid;
42145450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
42155450Sbrendan 
42165450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
42175450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
42182082Seschrock 			return (B_TRUE);
42192082Seschrock 
42205450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
42215450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
42225450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
42233377Seschrock 			return (B_TRUE);
42243377Seschrock 	}
42253377Seschrock 
42262082Seschrock 	return (B_FALSE);
42272082Seschrock }
42283912Slling 
42294451Seschrock /*
42307214Slling  * Check if a pool has an active shared spare device.
42317214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
42327214Slling  */
42337214Slling static boolean_t
42347214Slling spa_has_active_shared_spare(spa_t *spa)
42357214Slling {
42367214Slling 	int i, refcnt;
42377214Slling 	uint64_t pool;
42387214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
42397214Slling 
42407214Slling 	for (i = 0; i < sav->sav_count; i++) {
42417214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
42427214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
42437214Slling 		    refcnt > 2)
42447214Slling 			return (B_TRUE);
42457214Slling 	}
42467214Slling 
42477214Slling 	return (B_FALSE);
42487214Slling }
42497214Slling 
42507214Slling /*
42514451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
42524451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
42534451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
42544451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
42554451Seschrock  * or zdb as real changes.
42564451Seschrock  */
42574451Seschrock void
42584451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
42594451Seschrock {
42604451Seschrock #ifdef _KERNEL
42614451Seschrock 	sysevent_t		*ev;
42624451Seschrock 	sysevent_attr_list_t	*attr = NULL;
42634451Seschrock 	sysevent_value_t	value;
42644451Seschrock 	sysevent_id_t		eid;
42654451Seschrock 
42664451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
42674451Seschrock 	    SE_SLEEP);
42684451Seschrock 
42694451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
42704451Seschrock 	value.value.sv_string = spa_name(spa);
42714451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
42724451Seschrock 		goto done;
42734451Seschrock 
42744451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
42754451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
42764451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
42774451Seschrock 		goto done;
42784451Seschrock 
42794451Seschrock 	if (vd) {
42804451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
42814451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
42824451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
42834451Seschrock 		    SE_SLEEP) != 0)
42844451Seschrock 			goto done;
42854451Seschrock 
42864451Seschrock 		if (vd->vdev_path) {
42874451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
42884451Seschrock 			value.value.sv_string = vd->vdev_path;
42894451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
42904451Seschrock 			    &value, SE_SLEEP) != 0)
42914451Seschrock 				goto done;
42924451Seschrock 		}
42934451Seschrock 	}
42944451Seschrock 
42955756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
42965756Seschrock 		goto done;
42975756Seschrock 	attr = NULL;
42985756Seschrock 
42994451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
43004451Seschrock 
43014451Seschrock done:
43024451Seschrock 	if (attr)
43034451Seschrock 		sysevent_free_attr(attr);
43044451Seschrock 	sysevent_free(ev);
43054451Seschrock #endif
43064451Seschrock }
4307