xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 10922)
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 /*
238525SEric.Schrock@Sun.COM  * Copyright 2009 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/dmu.h>
39789Sahrens #include <sys/dmu_tx.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
42*10922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
4510594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
46789Sahrens #include <sys/uberblock_impl.h>
47789Sahrens #include <sys/txg.h>
48789Sahrens #include <sys/avl.h>
49789Sahrens #include <sys/dmu_traverse.h>
503912Slling #include <sys/dmu_objset.h>
51789Sahrens #include <sys/unique.h>
52789Sahrens #include <sys/dsl_pool.h>
533912Slling #include <sys/dsl_dataset.h>
54789Sahrens #include <sys/dsl_dir.h>
55789Sahrens #include <sys/dsl_prop.h>
563912Slling #include <sys/dsl_synctask.h>
57789Sahrens #include <sys/fs/zfs.h>
585450Sbrendan #include <sys/arc.h>
59789Sahrens #include <sys/callb.h>
603975Sek110237 #include <sys/systeminfo.h>
613975Sek110237 #include <sys/sunddi.h>
626423Sgw25295 #include <sys/spa_boot.h>
639816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h>
64789Sahrens 
658662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
668662SJordan.Vaughan@Sun.com #include <sys/zone.h>
6710822SJack.Meng@Sun.COM #include <sys/bootprops.h>
688662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
698662SJordan.Vaughan@Sun.com 
705094Slling #include "zfs_prop.h"
715913Sperrin #include "zfs_comutil.h"
725094Slling 
739515SJonathan.Adams@Sun.COM enum zti_modes {
749515SJonathan.Adams@Sun.COM 	zti_mode_fixed,			/* value is # of threads (min 1) */
759515SJonathan.Adams@Sun.COM 	zti_mode_online_percent,	/* value is % of online CPUs */
769515SJonathan.Adams@Sun.COM 	zti_mode_tune,			/* fill from zio_taskq_tune_* */
779515SJonathan.Adams@Sun.COM 	zti_nmodes
787754SJeff.Bonwick@Sun.COM };
792986Sek110237 
809515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_FIX(n)	{ zti_mode_fixed, (n) }
819515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_PCT(n)	{ zti_mode_online_percent, (n) }
829515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_TUNE		{ zti_mode_tune, 0 }
839515SJonathan.Adams@Sun.COM 
849515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_ONE		ZTI_THREAD_FIX(1)
859515SJonathan.Adams@Sun.COM 
869515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info {
879515SJonathan.Adams@Sun.COM 	const char *zti_name;
889515SJonathan.Adams@Sun.COM 	struct {
899515SJonathan.Adams@Sun.COM 		enum zti_modes zti_mode;
909515SJonathan.Adams@Sun.COM 		uint_t zti_value;
919515SJonathan.Adams@Sun.COM 	} zti_nthreads[ZIO_TASKQ_TYPES];
929515SJonathan.Adams@Sun.COM } zio_taskq_info_t;
939515SJonathan.Adams@Sun.COM 
949515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
959515SJonathan.Adams@Sun.COM 				"issue",		"intr"
969515SJonathan.Adams@Sun.COM };
979515SJonathan.Adams@Sun.COM 
989515SJonathan.Adams@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = {
999515SJonathan.Adams@Sun.COM 	/*			ISSUE			INTR		*/
1009515SJonathan.Adams@Sun.COM 	{ "spa_zio_null",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1019515SJonathan.Adams@Sun.COM 	{ "spa_zio_read",	{ ZTI_THREAD_FIX(8),	ZTI_THREAD_TUNE } },
1029515SJonathan.Adams@Sun.COM 	{ "spa_zio_write",	{ ZTI_THREAD_TUNE,	ZTI_THREAD_FIX(8) } },
1039515SJonathan.Adams@Sun.COM 	{ "spa_zio_free",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1049515SJonathan.Adams@Sun.COM 	{ "spa_zio_claim",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1059515SJonathan.Adams@Sun.COM 	{ "spa_zio_ioctl",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1069515SJonathan.Adams@Sun.COM };
1079515SJonathan.Adams@Sun.COM 
1089515SJonathan.Adams@Sun.COM enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent;
1099515SJonathan.Adams@Sun.COM uint_t zio_taskq_tune_value = 80;	/* #threads = 80% of # online CPUs */
1109515SJonathan.Adams@Sun.COM 
1115094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
1127214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
1135094Slling 
1145094Slling /*
1155094Slling  * ==========================================================================
1165094Slling  * SPA properties routines
1175094Slling  * ==========================================================================
1185094Slling  */
1195094Slling 
1205094Slling /*
1215094Slling  * Add a (source=src, propname=propval) list to an nvlist.
1225094Slling  */
1235949Slling static void
1245094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
1255094Slling     uint64_t intval, zprop_source_t src)
1265094Slling {
1275094Slling 	const char *propname = zpool_prop_to_name(prop);
1285094Slling 	nvlist_t *propval;
1295949Slling 
1305949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1315949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1325949Slling 
1335949Slling 	if (strval != NULL)
1345949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1355949Slling 	else
1365949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1375949Slling 
1385949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1395094Slling 	nvlist_free(propval);
1405094Slling }
1415094Slling 
1425094Slling /*
1435094Slling  * Get property values from the spa configuration.
1445094Slling  */
1455949Slling static void
1465094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1475094Slling {
1488525SEric.Schrock@Sun.COM 	uint64_t size;
1498525SEric.Schrock@Sun.COM 	uint64_t used;
1505094Slling 	uint64_t cap, version;
1515094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1526643Seschrock 	spa_config_dirent_t *dp;
1535094Slling 
1547754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1557754SJeff.Bonwick@Sun.COM 
1568525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
157*10922SJeff.Bonwick@Sun.COM 		used = metaslab_class_get_alloc(spa_normal_class(spa));
158*10922SJeff.Bonwick@Sun.COM 		size = metaslab_class_get_space(spa_normal_class(spa));
1598525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1608525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1618525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1628525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
1638525SEric.Schrock@Sun.COM 		    size - used, src);
1648525SEric.Schrock@Sun.COM 
1658525SEric.Schrock@Sun.COM 		cap = (size == 0) ? 0 : (used * 100 / size);
1668525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1678525SEric.Schrock@Sun.COM 
168*10922SJeff.Bonwick@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
169*10922SJeff.Bonwick@Sun.COM 		    ddt_get_pool_dedup_ratio(spa), src);
170*10922SJeff.Bonwick@Sun.COM 
1718525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1728525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1738525SEric.Schrock@Sun.COM 
1748525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1758525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1768525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1778525SEric.Schrock@Sun.COM 		else
1788525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1798525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1808525SEric.Schrock@Sun.COM 	}
1815949Slling 
1825949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1835949Slling 
1845949Slling 	if (spa->spa_root != NULL)
1855949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1865949Slling 		    0, ZPROP_SRC_LOCAL);
1875094Slling 
1886643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1896643Seschrock 		if (dp->scd_path == NULL) {
1905949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1916643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1926643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1935949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1946643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1955363Seschrock 		}
1965363Seschrock 	}
1975094Slling }
1985094Slling 
1995094Slling /*
2005094Slling  * Get zpool property values.
2015094Slling  */
2025094Slling int
2035094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2045094Slling {
205*10922SJeff.Bonwick@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
2065094Slling 	zap_cursor_t zc;
2075094Slling 	zap_attribute_t za;
2085094Slling 	int err;
2095094Slling 
2105949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2115094Slling 
2127754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2137754SJeff.Bonwick@Sun.COM 
2145094Slling 	/*
2155094Slling 	 * Get properties from the spa config.
2165094Slling 	 */
2175949Slling 	spa_prop_get_config(spa, nvp);
2185094Slling 
2195094Slling 	/* If no pool property object, no more prop to get. */
2205094Slling 	if (spa->spa_pool_props_object == 0) {
2215094Slling 		mutex_exit(&spa->spa_props_lock);
2225094Slling 		return (0);
2235094Slling 	}
2245094Slling 
2255094Slling 	/*
2265094Slling 	 * Get properties from the MOS pool property object.
2275094Slling 	 */
2285094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
2295094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
2305094Slling 	    zap_cursor_advance(&zc)) {
2315094Slling 		uint64_t intval = 0;
2325094Slling 		char *strval = NULL;
2335094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
2345094Slling 		zpool_prop_t prop;
2355094Slling 
2365094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
2375094Slling 			continue;
2385094Slling 
2395094Slling 		switch (za.za_integer_length) {
2405094Slling 		case 8:
2415094Slling 			/* integer property */
2425094Slling 			if (za.za_first_integer !=
2435094Slling 			    zpool_prop_default_numeric(prop))
2445094Slling 				src = ZPROP_SRC_LOCAL;
2455094Slling 
2465094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2475094Slling 				dsl_pool_t *dp;
2485094Slling 				dsl_dataset_t *ds = NULL;
2495094Slling 
2505094Slling 				dp = spa_get_dsl(spa);
2515094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2526689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2536689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2545094Slling 					rw_exit(&dp->dp_config_rwlock);
2555094Slling 					break;
2565094Slling 				}
2575094Slling 
2585094Slling 				strval = kmem_alloc(
2595094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2605094Slling 				    KM_SLEEP);
2615094Slling 				dsl_dataset_name(ds, strval);
2626689Smaybee 				dsl_dataset_rele(ds, FTAG);
2635094Slling 				rw_exit(&dp->dp_config_rwlock);
2645094Slling 			} else {
2655094Slling 				strval = NULL;
2665094Slling 				intval = za.za_first_integer;
2675094Slling 			}
2685094Slling 
2695949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2705094Slling 
2715094Slling 			if (strval != NULL)
2725094Slling 				kmem_free(strval,
2735094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2745094Slling 
2755094Slling 			break;
2765094Slling 
2775094Slling 		case 1:
2785094Slling 			/* string property */
2795094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2805094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2815094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2825094Slling 			if (err) {
2835094Slling 				kmem_free(strval, za.za_num_integers);
2845094Slling 				break;
2855094Slling 			}
2865949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2875094Slling 			kmem_free(strval, za.za_num_integers);
2885094Slling 			break;
2895094Slling 
2905094Slling 		default:
2915094Slling 			break;
2925094Slling 		}
2935094Slling 	}
2945094Slling 	zap_cursor_fini(&zc);
2955094Slling 	mutex_exit(&spa->spa_props_lock);
2965094Slling out:
2975094Slling 	if (err && err != ENOENT) {
2985094Slling 		nvlist_free(*nvp);
2995949Slling 		*nvp = NULL;
3005094Slling 		return (err);
3015094Slling 	}
3025094Slling 
3035094Slling 	return (0);
3045094Slling }
3055094Slling 
3065094Slling /*
3075094Slling  * Validate the given pool properties nvlist and modify the list
3085094Slling  * for the property values to be set.
3095094Slling  */
3105094Slling static int
3115094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
3125094Slling {
3135094Slling 	nvpair_t *elem;
3145094Slling 	int error = 0, reset_bootfs = 0;
3155094Slling 	uint64_t objnum;
3165094Slling 
3175094Slling 	elem = NULL;
3185094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3195094Slling 		zpool_prop_t prop;
3205094Slling 		char *propname, *strval;
3215094Slling 		uint64_t intval;
3225094Slling 		objset_t *os;
3235363Seschrock 		char *slash;
3245094Slling 
3255094Slling 		propname = nvpair_name(elem);
3265094Slling 
3275094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
3285094Slling 			return (EINVAL);
3295094Slling 
3305094Slling 		switch (prop) {
3315094Slling 		case ZPOOL_PROP_VERSION:
3325094Slling 			error = nvpair_value_uint64(elem, &intval);
3335094Slling 			if (!error &&
3345094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
3355094Slling 				error = EINVAL;
3365094Slling 			break;
3375094Slling 
3385094Slling 		case ZPOOL_PROP_DELEGATION:
3395094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3407538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3419816SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_AUTOEXPAND:
3425094Slling 			error = nvpair_value_uint64(elem, &intval);
3435094Slling 			if (!error && intval > 1)
3445094Slling 				error = EINVAL;
3455094Slling 			break;
3465094Slling 
3475094Slling 		case ZPOOL_PROP_BOOTFS:
3489630SJeff.Bonwick@Sun.COM 			/*
3499630SJeff.Bonwick@Sun.COM 			 * If the pool version is less than SPA_VERSION_BOOTFS,
3509630SJeff.Bonwick@Sun.COM 			 * or the pool is still being created (version == 0),
3519630SJeff.Bonwick@Sun.COM 			 * the bootfs property cannot be set.
3529630SJeff.Bonwick@Sun.COM 			 */
3535094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3545094Slling 				error = ENOTSUP;
3555094Slling 				break;
3565094Slling 			}
3575094Slling 
3585094Slling 			/*
3597042Sgw25295 			 * Make sure the vdev config is bootable
3605094Slling 			 */
3617042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3625094Slling 				error = ENOTSUP;
3635094Slling 				break;
3645094Slling 			}
3655094Slling 
3665094Slling 			reset_bootfs = 1;
3675094Slling 
3685094Slling 			error = nvpair_value_string(elem, &strval);
3695094Slling 
3705094Slling 			if (!error) {
3717042Sgw25295 				uint64_t compress;
3727042Sgw25295 
3735094Slling 				if (strval == NULL || strval[0] == '\0') {
3745094Slling 					objnum = zpool_prop_default_numeric(
3755094Slling 					    ZPOOL_PROP_BOOTFS);
3765094Slling 					break;
3775094Slling 				}
3785094Slling 
37910298SMatthew.Ahrens@Sun.COM 				if (error = dmu_objset_hold(strval, FTAG, &os))
3805094Slling 					break;
3817042Sgw25295 
38210298SMatthew.Ahrens@Sun.COM 				/* Must be ZPL and not gzip compressed. */
38310298SMatthew.Ahrens@Sun.COM 
38410298SMatthew.Ahrens@Sun.COM 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
38510298SMatthew.Ahrens@Sun.COM 					error = ENOTSUP;
38610298SMatthew.Ahrens@Sun.COM 				} else if ((error = dsl_prop_get_integer(strval,
3877042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3887042Sgw25295 				    &compress, NULL)) == 0 &&
3897042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3907042Sgw25295 					error = ENOTSUP;
3917042Sgw25295 				} else {
3927042Sgw25295 					objnum = dmu_objset_id(os);
3937042Sgw25295 				}
39410298SMatthew.Ahrens@Sun.COM 				dmu_objset_rele(os, FTAG);
3955094Slling 			}
3965094Slling 			break;
3977754SJeff.Bonwick@Sun.COM 
3985329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3995329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
4005329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
4015329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
4025329Sgw25295 				error = EINVAL;
4035329Sgw25295 
4045329Sgw25295 			/*
4055329Sgw25295 			 * This is a special case which only occurs when
4065329Sgw25295 			 * the pool has completely failed. This allows
4075329Sgw25295 			 * the user to change the in-core failmode property
4085329Sgw25295 			 * without syncing it out to disk (I/Os might
4095329Sgw25295 			 * currently be blocked). We do this by returning
4105329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
4115329Sgw25295 			 * into thinking we encountered a property validation
4125329Sgw25295 			 * error.
4135329Sgw25295 			 */
4147754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
4155329Sgw25295 				spa->spa_failmode = intval;
4165329Sgw25295 				error = EIO;
4175329Sgw25295 			}
4185329Sgw25295 			break;
4195363Seschrock 
4205363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4215363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
4225363Seschrock 				break;
4235363Seschrock 
4245363Seschrock 			if (strval[0] == '\0')
4255363Seschrock 				break;
4265363Seschrock 
4275363Seschrock 			if (strcmp(strval, "none") == 0)
4285363Seschrock 				break;
4295363Seschrock 
4305363Seschrock 			if (strval[0] != '/') {
4315363Seschrock 				error = EINVAL;
4325363Seschrock 				break;
4335363Seschrock 			}
4345363Seschrock 
4355363Seschrock 			slash = strrchr(strval, '/');
4365363Seschrock 			ASSERT(slash != NULL);
4375363Seschrock 
4385363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4395363Seschrock 			    strcmp(slash, "/..") == 0)
4405363Seschrock 				error = EINVAL;
4415363Seschrock 			break;
442*10922SJeff.Bonwick@Sun.COM 
443*10922SJeff.Bonwick@Sun.COM 		case ZPOOL_PROP_DEDUPDITTO:
444*10922SJeff.Bonwick@Sun.COM 			if (spa_version(spa) < SPA_VERSION_DEDUP)
445*10922SJeff.Bonwick@Sun.COM 				error = ENOTSUP;
446*10922SJeff.Bonwick@Sun.COM 			else
447*10922SJeff.Bonwick@Sun.COM 				error = nvpair_value_uint64(elem, &intval);
448*10922SJeff.Bonwick@Sun.COM 			if (error == 0 &&
449*10922SJeff.Bonwick@Sun.COM 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
450*10922SJeff.Bonwick@Sun.COM 				error = EINVAL;
451*10922SJeff.Bonwick@Sun.COM 			break;
4525094Slling 		}
4535094Slling 
4545094Slling 		if (error)
4555094Slling 			break;
4565094Slling 	}
4575094Slling 
4585094Slling 	if (!error && reset_bootfs) {
4595094Slling 		error = nvlist_remove(props,
4605094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4615094Slling 
4625094Slling 		if (!error) {
4635094Slling 			error = nvlist_add_uint64(props,
4645094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4655094Slling 		}
4665094Slling 	}
4675094Slling 
4685094Slling 	return (error);
4695094Slling }
4705094Slling 
4718525SEric.Schrock@Sun.COM void
4728525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
4738525SEric.Schrock@Sun.COM {
4748525SEric.Schrock@Sun.COM 	char *cachefile;
4758525SEric.Schrock@Sun.COM 	spa_config_dirent_t *dp;
4768525SEric.Schrock@Sun.COM 
4778525SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
4788525SEric.Schrock@Sun.COM 	    &cachefile) != 0)
4798525SEric.Schrock@Sun.COM 		return;
4808525SEric.Schrock@Sun.COM 
4818525SEric.Schrock@Sun.COM 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
4828525SEric.Schrock@Sun.COM 	    KM_SLEEP);
4838525SEric.Schrock@Sun.COM 
4848525SEric.Schrock@Sun.COM 	if (cachefile[0] == '\0')
4858525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(spa_config_path);
4868525SEric.Schrock@Sun.COM 	else if (strcmp(cachefile, "none") == 0)
4878525SEric.Schrock@Sun.COM 		dp->scd_path = NULL;
4888525SEric.Schrock@Sun.COM 	else
4898525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(cachefile);
4908525SEric.Schrock@Sun.COM 
4918525SEric.Schrock@Sun.COM 	list_insert_head(&spa->spa_config_list, dp);
4928525SEric.Schrock@Sun.COM 	if (need_sync)
4938525SEric.Schrock@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4948525SEric.Schrock@Sun.COM }
4958525SEric.Schrock@Sun.COM 
4965094Slling int
4975094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4985094Slling {
4995094Slling 	int error;
5008525SEric.Schrock@Sun.COM 	nvpair_t *elem;
5018525SEric.Schrock@Sun.COM 	boolean_t need_sync = B_FALSE;
5028525SEric.Schrock@Sun.COM 	zpool_prop_t prop;
5035094Slling 
5045094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
5055094Slling 		return (error);
5065094Slling 
5078525SEric.Schrock@Sun.COM 	elem = NULL;
5088525SEric.Schrock@Sun.COM 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
5098525SEric.Schrock@Sun.COM 		if ((prop = zpool_name_to_prop(
5108525SEric.Schrock@Sun.COM 		    nvpair_name(elem))) == ZPROP_INVAL)
5118525SEric.Schrock@Sun.COM 			return (EINVAL);
5128525SEric.Schrock@Sun.COM 
5138525SEric.Schrock@Sun.COM 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
5148525SEric.Schrock@Sun.COM 			continue;
5158525SEric.Schrock@Sun.COM 
5168525SEric.Schrock@Sun.COM 		need_sync = B_TRUE;
5178525SEric.Schrock@Sun.COM 		break;
5188525SEric.Schrock@Sun.COM 	}
5198525SEric.Schrock@Sun.COM 
5208525SEric.Schrock@Sun.COM 	if (need_sync)
5218525SEric.Schrock@Sun.COM 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
5228525SEric.Schrock@Sun.COM 		    spa, nvp, 3));
5238525SEric.Schrock@Sun.COM 	else
5248525SEric.Schrock@Sun.COM 		return (0);
5255094Slling }
5265094Slling 
5275094Slling /*
5285094Slling  * If the bootfs property value is dsobj, clear it.
5295094Slling  */
5305094Slling void
5315094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
5325094Slling {
5335094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
5345094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
5355094Slling 		    spa->spa_pool_props_object,
5365094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
5375094Slling 		spa->spa_bootfs = 0;
5385094Slling 	}
5395094Slling }
5405094Slling 
541789Sahrens /*
542789Sahrens  * ==========================================================================
543789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
544789Sahrens  * ==========================================================================
545789Sahrens  */
546789Sahrens 
5471544Seschrock static int
5481544Seschrock spa_error_entry_compare(const void *a, const void *b)
5491544Seschrock {
5501544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
5511544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
5521544Seschrock 	int ret;
5531544Seschrock 
5541544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
5551544Seschrock 	    sizeof (zbookmark_t));
5561544Seschrock 
5571544Seschrock 	if (ret < 0)
5581544Seschrock 		return (-1);
5591544Seschrock 	else if (ret > 0)
5601544Seschrock 		return (1);
5611544Seschrock 	else
5621544Seschrock 		return (0);
5631544Seschrock }
5641544Seschrock 
5651544Seschrock /*
5661544Seschrock  * Utility function which retrieves copies of the current logs and
5671544Seschrock  * re-initializes them in the process.
5681544Seschrock  */
5691544Seschrock void
5701544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
5711544Seschrock {
5721544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
5731544Seschrock 
5741544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
5751544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
5761544Seschrock 
5771544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5781544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5791544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5801544Seschrock 	avl_create(&spa->spa_errlist_last,
5811544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5821544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5831544Seschrock }
5841544Seschrock 
585789Sahrens /*
586789Sahrens  * Activate an uninitialized pool.
587789Sahrens  */
588789Sahrens static void
5898241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode)
590789Sahrens {
591789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
592789Sahrens 
593789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
5948241SJeff.Bonwick@Sun.COM 	spa->spa_mode = mode;
595789Sahrens 
59610594SGeorge.Wilson@Sun.COM 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
59710594SGeorge.Wilson@Sun.COM 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
598789Sahrens 
5997754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
6009515SJonathan.Adams@Sun.COM 		const zio_taskq_info_t *ztip = &zio_taskqs[t];
6017754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
6029515SJonathan.Adams@Sun.COM 			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
6039515SJonathan.Adams@Sun.COM 			uint_t value = ztip->zti_nthreads[q].zti_value;
6049515SJonathan.Adams@Sun.COM 			char name[32];
6059515SJonathan.Adams@Sun.COM 
6069515SJonathan.Adams@Sun.COM 			(void) snprintf(name, sizeof (name),
6079515SJonathan.Adams@Sun.COM 			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
6089515SJonathan.Adams@Sun.COM 
6099515SJonathan.Adams@Sun.COM 			if (mode == zti_mode_tune) {
6109515SJonathan.Adams@Sun.COM 				mode = zio_taskq_tune_mode;
6119515SJonathan.Adams@Sun.COM 				value = zio_taskq_tune_value;
6129515SJonathan.Adams@Sun.COM 				if (mode == zti_mode_tune)
6139515SJonathan.Adams@Sun.COM 					mode = zti_mode_online_percent;
6149515SJonathan.Adams@Sun.COM 			}
6159515SJonathan.Adams@Sun.COM 
6169515SJonathan.Adams@Sun.COM 			switch (mode) {
6179515SJonathan.Adams@Sun.COM 			case zti_mode_fixed:
6189515SJonathan.Adams@Sun.COM 				ASSERT3U(value, >=, 1);
6199515SJonathan.Adams@Sun.COM 				value = MAX(value, 1);
6209515SJonathan.Adams@Sun.COM 
6219515SJonathan.Adams@Sun.COM 				spa->spa_zio_taskq[t][q] = taskq_create(name,
6229515SJonathan.Adams@Sun.COM 				    value, maxclsyspri, 50, INT_MAX,
6239515SJonathan.Adams@Sun.COM 				    TASKQ_PREPOPULATE);
6249515SJonathan.Adams@Sun.COM 				break;
6259515SJonathan.Adams@Sun.COM 
6269515SJonathan.Adams@Sun.COM 			case zti_mode_online_percent:
6279515SJonathan.Adams@Sun.COM 				spa->spa_zio_taskq[t][q] = taskq_create(name,
6289515SJonathan.Adams@Sun.COM 				    value, maxclsyspri, 50, INT_MAX,
6299515SJonathan.Adams@Sun.COM 				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
6309515SJonathan.Adams@Sun.COM 				break;
6319515SJonathan.Adams@Sun.COM 
6329515SJonathan.Adams@Sun.COM 			case zti_mode_tune:
6339515SJonathan.Adams@Sun.COM 			default:
6349515SJonathan.Adams@Sun.COM 				panic("unrecognized mode for "
6359515SJonathan.Adams@Sun.COM 				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
6369515SJonathan.Adams@Sun.COM 				    "in spa_activate()",
6379515SJonathan.Adams@Sun.COM 				    t, q, mode, value);
6389515SJonathan.Adams@Sun.COM 				break;
6399515SJonathan.Adams@Sun.COM 			}
6407754SJeff.Bonwick@Sun.COM 		}
641789Sahrens 	}
642789Sahrens 
6437754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
6447754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
6457754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
6467754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
647789Sahrens 
648789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
649789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
6501544Seschrock 
6511544Seschrock 	avl_create(&spa->spa_errlist_scrub,
6521544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6531544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
6541544Seschrock 	avl_create(&spa->spa_errlist_last,
6551544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6561544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
657789Sahrens }
658789Sahrens 
659789Sahrens /*
660789Sahrens  * Opposite of spa_activate().
661789Sahrens  */
662789Sahrens static void
663789Sahrens spa_deactivate(spa_t *spa)
664789Sahrens {
665789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
666789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
667789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
6689630SJeff.Bonwick@Sun.COM 	ASSERT(spa->spa_async_zio_root == NULL);
669789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
670789Sahrens 
671789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
672789Sahrens 
6737754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
6747754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
6757754SJeff.Bonwick@Sun.COM 
6767754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
6777754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
6787754SJeff.Bonwick@Sun.COM 			taskq_destroy(spa->spa_zio_taskq[t][q]);
6797754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
6807754SJeff.Bonwick@Sun.COM 		}
681789Sahrens 	}
682789Sahrens 
683789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
684789Sahrens 	spa->spa_normal_class = NULL;
685789Sahrens 
6864527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
6874527Sperrin 	spa->spa_log_class = NULL;
6884527Sperrin 
6891544Seschrock 	/*
6901544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
6911544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
6921544Seschrock 	 */
6931544Seschrock 	spa_errlog_drain(spa);
6941544Seschrock 
6951544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
6961544Seschrock 	avl_destroy(&spa->spa_errlist_last);
6971544Seschrock 
698789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
699789Sahrens }
700789Sahrens 
701789Sahrens /*
702789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
703789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
704789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
705789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
706789Sahrens  */
7072082Seschrock static int
7082082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
7092082Seschrock     uint_t id, int atype)
710789Sahrens {
711789Sahrens 	nvlist_t **child;
7129816SGeorge.Wilson@Sun.COM 	uint_t children;
7132082Seschrock 	int error;
7142082Seschrock 
7152082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
7162082Seschrock 		return (error);
7172082Seschrock 
7182082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
7192082Seschrock 		return (0);
720789Sahrens 
7217754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
7227754SJeff.Bonwick@Sun.COM 	    &child, &children);
7237754SJeff.Bonwick@Sun.COM 
7247754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
7257754SJeff.Bonwick@Sun.COM 		return (0);
7267754SJeff.Bonwick@Sun.COM 
7277754SJeff.Bonwick@Sun.COM 	if (error) {
7282082Seschrock 		vdev_free(*vdp);
7292082Seschrock 		*vdp = NULL;
7302082Seschrock 		return (EINVAL);
731789Sahrens 	}
732789Sahrens 
7339816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < children; c++) {
7342082Seschrock 		vdev_t *vd;
7352082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
7362082Seschrock 		    atype)) != 0) {
7372082Seschrock 			vdev_free(*vdp);
7382082Seschrock 			*vdp = NULL;
7392082Seschrock 			return (error);
740789Sahrens 		}
741789Sahrens 	}
742789Sahrens 
7432082Seschrock 	ASSERT(*vdp != NULL);
7442082Seschrock 
7452082Seschrock 	return (0);
746789Sahrens }
747789Sahrens 
748789Sahrens /*
749789Sahrens  * Opposite of spa_load().
750789Sahrens  */
751789Sahrens static void
752789Sahrens spa_unload(spa_t *spa)
753789Sahrens {
7542082Seschrock 	int i;
7552082Seschrock 
7567754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
7577754SJeff.Bonwick@Sun.COM 
758789Sahrens 	/*
7591544Seschrock 	 * Stop async tasks.
7601544Seschrock 	 */
7611544Seschrock 	spa_async_suspend(spa);
7621544Seschrock 
7631544Seschrock 	/*
764789Sahrens 	 * Stop syncing.
765789Sahrens 	 */
766789Sahrens 	if (spa->spa_sync_on) {
767789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
768789Sahrens 		spa->spa_sync_on = B_FALSE;
769789Sahrens 	}
770789Sahrens 
771789Sahrens 	/*
7727754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
773789Sahrens 	 */
7749234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root != NULL) {
7759234SGeorge.Wilson@Sun.COM 		(void) zio_wait(spa->spa_async_zio_root);
7769234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = NULL;
7779234SGeorge.Wilson@Sun.COM 	}
778789Sahrens 
779789Sahrens 	/*
780789Sahrens 	 * Close the dsl pool.
781789Sahrens 	 */
782789Sahrens 	if (spa->spa_dsl_pool) {
783789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
784789Sahrens 		spa->spa_dsl_pool = NULL;
785789Sahrens 	}
786789Sahrens 
787*10922SJeff.Bonwick@Sun.COM 	ddt_unload(spa);
788*10922SJeff.Bonwick@Sun.COM 
7898241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7908241SJeff.Bonwick@Sun.COM 
7918241SJeff.Bonwick@Sun.COM 	/*
7928241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
7938241SJeff.Bonwick@Sun.COM 	 */
7948241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
7958241SJeff.Bonwick@Sun.COM 
796789Sahrens 	/*
797789Sahrens 	 * Close all vdevs.
798789Sahrens 	 */
7991585Sbonwick 	if (spa->spa_root_vdev)
800789Sahrens 		vdev_free(spa->spa_root_vdev);
8011585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
8021544Seschrock 
8035450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
8045450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
8055450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
8065450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
8075450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
8085450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
8095450Sbrendan 	}
8105450Sbrendan 	if (spa->spa_spares.sav_config) {
8115450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
8125450Sbrendan 		spa->spa_spares.sav_config = NULL;
8132082Seschrock 	}
8147377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
8155450Sbrendan 
8165450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
8175450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
8185450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
8195450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
8205450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
8215450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
8225450Sbrendan 	}
8235450Sbrendan 	if (spa->spa_l2cache.sav_config) {
8245450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
8255450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
8262082Seschrock 	}
8277377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
8282082Seschrock 
8291544Seschrock 	spa->spa_async_suspended = 0;
8308241SJeff.Bonwick@Sun.COM 
8318241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
832789Sahrens }
833789Sahrens 
834789Sahrens /*
8352082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
8362082Seschrock  * this pool.  When this is called, we have some form of basic information in
8375450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
8385450Sbrendan  * then re-generate a more complete list including status information.
8392082Seschrock  */
8402082Seschrock static void
8412082Seschrock spa_load_spares(spa_t *spa)
8422082Seschrock {
8432082Seschrock 	nvlist_t **spares;
8442082Seschrock 	uint_t nspares;
8452082Seschrock 	int i;
8463377Seschrock 	vdev_t *vd, *tvd;
8472082Seschrock 
8487754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
8497754SJeff.Bonwick@Sun.COM 
8502082Seschrock 	/*
8512082Seschrock 	 * First, close and free any existing spare vdevs.
8522082Seschrock 	 */
8535450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8545450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
8553377Seschrock 
8563377Seschrock 		/* Undo the call to spa_activate() below */
8576643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8586643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
8593377Seschrock 			spa_spare_remove(tvd);
8603377Seschrock 		vdev_close(vd);
8613377Seschrock 		vdev_free(vd);
8622082Seschrock 	}
8633377Seschrock 
8645450Sbrendan 	if (spa->spa_spares.sav_vdevs)
8655450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
8665450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
8675450Sbrendan 
8685450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
8692082Seschrock 		nspares = 0;
8702082Seschrock 	else
8715450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
8722082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
8732082Seschrock 
8745450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
8755450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
8762082Seschrock 
8772082Seschrock 	if (nspares == 0)
8782082Seschrock 		return;
8792082Seschrock 
8802082Seschrock 	/*
8812082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
8823377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
8833377Seschrock 	 * structures associated with it: one in the list of spares (used only
8843377Seschrock 	 * for basic validation purposes) and one in the active vdev
8853377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
8863377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
8873377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
8882082Seschrock 	 */
8895450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
8905450Sbrendan 	    KM_SLEEP);
8915450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8922082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
8932082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
8942082Seschrock 		ASSERT(vd != NULL);
8952082Seschrock 
8965450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
8972082Seschrock 
8986643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8996643Seschrock 		    B_FALSE)) != NULL) {
9003377Seschrock 			if (!tvd->vdev_isspare)
9013377Seschrock 				spa_spare_add(tvd);
9023377Seschrock 
9033377Seschrock 			/*
9043377Seschrock 			 * We only mark the spare active if we were successfully
9053377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
9063377Seschrock 			 * with a bad active spare would result in strange
9073377Seschrock 			 * behavior, because multiple pool would think the spare
9083377Seschrock 			 * is actively in use.
9093377Seschrock 			 *
9103377Seschrock 			 * There is a vulnerability here to an equally bizarre
9113377Seschrock 			 * circumstance, where a dead active spare is later
9123377Seschrock 			 * brought back to life (onlined or otherwise).  Given
9133377Seschrock 			 * the rarity of this scenario, and the extra complexity
9143377Seschrock 			 * it adds, we ignore the possibility.
9153377Seschrock 			 */
9163377Seschrock 			if (!vdev_is_dead(tvd))
9173377Seschrock 				spa_spare_activate(tvd);
9183377Seschrock 		}
9193377Seschrock 
9207754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
9219425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
9227754SJeff.Bonwick@Sun.COM 
9232082Seschrock 		if (vdev_open(vd) != 0)
9242082Seschrock 			continue;
9252082Seschrock 
9265450Sbrendan 		if (vdev_validate_aux(vd) == 0)
9275450Sbrendan 			spa_spare_add(vd);
9282082Seschrock 	}
9292082Seschrock 
9302082Seschrock 	/*
9312082Seschrock 	 * Recompute the stashed list of spares, with status information
9322082Seschrock 	 * this time.
9332082Seschrock 	 */
9345450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
9352082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9362082Seschrock 
9375450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
9385450Sbrendan 	    KM_SLEEP);
9395450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9405450Sbrendan 		spares[i] = vdev_config_generate(spa,
9415450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
9425450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
9435450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
9445450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9452082Seschrock 		nvlist_free(spares[i]);
9465450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
9475450Sbrendan }
9485450Sbrendan 
9495450Sbrendan /*
9505450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
9515450Sbrendan  * this pool.  When this is called, we have some form of basic information in
9525450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
9535450Sbrendan  * then re-generate a more complete list including status information.
9545450Sbrendan  * Devices which are already active have their details maintained, and are
9555450Sbrendan  * not re-opened.
9565450Sbrendan  */
9575450Sbrendan static void
9585450Sbrendan spa_load_l2cache(spa_t *spa)
9595450Sbrendan {
9605450Sbrendan 	nvlist_t **l2cache;
9615450Sbrendan 	uint_t nl2cache;
9625450Sbrendan 	int i, j, oldnvdevs;
9639816SGeorge.Wilson@Sun.COM 	uint64_t guid;
9645450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
9655450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
9665450Sbrendan 
9677754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
9687754SJeff.Bonwick@Sun.COM 
9695450Sbrendan 	if (sav->sav_config != NULL) {
9705450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
9715450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
9725450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
9735450Sbrendan 	} else {
9745450Sbrendan 		nl2cache = 0;
9755450Sbrendan 	}
9765450Sbrendan 
9775450Sbrendan 	oldvdevs = sav->sav_vdevs;
9785450Sbrendan 	oldnvdevs = sav->sav_count;
9795450Sbrendan 	sav->sav_vdevs = NULL;
9805450Sbrendan 	sav->sav_count = 0;
9815450Sbrendan 
9825450Sbrendan 	/*
9835450Sbrendan 	 * Process new nvlist of vdevs.
9845450Sbrendan 	 */
9855450Sbrendan 	for (i = 0; i < nl2cache; i++) {
9865450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
9875450Sbrendan 		    &guid) == 0);
9885450Sbrendan 
9895450Sbrendan 		newvdevs[i] = NULL;
9905450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
9915450Sbrendan 			vd = oldvdevs[j];
9925450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
9935450Sbrendan 				/*
9945450Sbrendan 				 * Retain previous vdev for add/remove ops.
9955450Sbrendan 				 */
9965450Sbrendan 				newvdevs[i] = vd;
9975450Sbrendan 				oldvdevs[j] = NULL;
9985450Sbrendan 				break;
9995450Sbrendan 			}
10005450Sbrendan 		}
10015450Sbrendan 
10025450Sbrendan 		if (newvdevs[i] == NULL) {
10035450Sbrendan 			/*
10045450Sbrendan 			 * Create new vdev
10055450Sbrendan 			 */
10065450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
10075450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
10085450Sbrendan 			ASSERT(vd != NULL);
10095450Sbrendan 			newvdevs[i] = vd;
10105450Sbrendan 
10115450Sbrendan 			/*
10125450Sbrendan 			 * Commit this vdev as an l2cache device,
10135450Sbrendan 			 * even if it fails to open.
10145450Sbrendan 			 */
10155450Sbrendan 			spa_l2cache_add(vd);
10165450Sbrendan 
10176643Seschrock 			vd->vdev_top = vd;
10186643Seschrock 			vd->vdev_aux = sav;
10196643Seschrock 
10206643Seschrock 			spa_l2cache_activate(vd);
10216643Seschrock 
10225450Sbrendan 			if (vdev_open(vd) != 0)
10235450Sbrendan 				continue;
10245450Sbrendan 
10255450Sbrendan 			(void) vdev_validate_aux(vd);
10265450Sbrendan 
10279816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
10289816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
10295450Sbrendan 		}
10305450Sbrendan 	}
10315450Sbrendan 
10325450Sbrendan 	/*
10335450Sbrendan 	 * Purge vdevs that were dropped
10345450Sbrendan 	 */
10355450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
10365450Sbrendan 		uint64_t pool;
10375450Sbrendan 
10385450Sbrendan 		vd = oldvdevs[i];
10395450Sbrendan 		if (vd != NULL) {
10408241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
10418241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
10425450Sbrendan 				l2arc_remove_vdev(vd);
10435450Sbrendan 			(void) vdev_close(vd);
10445450Sbrendan 			spa_l2cache_remove(vd);
10455450Sbrendan 		}
10465450Sbrendan 	}
10475450Sbrendan 
10485450Sbrendan 	if (oldvdevs)
10495450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
10505450Sbrendan 
10515450Sbrendan 	if (sav->sav_config == NULL)
10525450Sbrendan 		goto out;
10535450Sbrendan 
10545450Sbrendan 	sav->sav_vdevs = newvdevs;
10555450Sbrendan 	sav->sav_count = (int)nl2cache;
10565450Sbrendan 
10575450Sbrendan 	/*
10585450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
10595450Sbrendan 	 * information this time.
10605450Sbrendan 	 */
10615450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
10625450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
10635450Sbrendan 
10645450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
10655450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
10665450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
10675450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
10685450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
10695450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
10705450Sbrendan out:
10715450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
10725450Sbrendan 		nvlist_free(l2cache[i]);
10735450Sbrendan 	if (sav->sav_count)
10745450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
10752082Seschrock }
10762082Seschrock 
10772082Seschrock static int
10782082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
10792082Seschrock {
10802082Seschrock 	dmu_buf_t *db;
10812082Seschrock 	char *packed = NULL;
10822082Seschrock 	size_t nvsize = 0;
10832082Seschrock 	int error;
10842082Seschrock 	*value = NULL;
10852082Seschrock 
10862082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
10872082Seschrock 	nvsize = *(uint64_t *)db->db_data;
10882082Seschrock 	dmu_buf_rele(db, FTAG);
10892082Seschrock 
10902082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
10919512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
10929512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
10932082Seschrock 	if (error == 0)
10942082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
10952082Seschrock 	kmem_free(packed, nvsize);
10962082Seschrock 
10972082Seschrock 	return (error);
10982082Seschrock }
10992082Seschrock 
11002082Seschrock /*
11014451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
11024451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
11034451Seschrock  */
11044451Seschrock static void
11054451Seschrock spa_check_removed(vdev_t *vd)
11064451Seschrock {
11079816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
11084451Seschrock 		spa_check_removed(vd->vdev_child[c]);
11094451Seschrock 
11104451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
11114451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
11124451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
11134451Seschrock 	}
11144451Seschrock }
11154451Seschrock 
11164451Seschrock /*
11179701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
11189701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
11199701SGeorge.Wilson@Sun.COM  */
11209701SGeorge.Wilson@Sun.COM void
112110594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
11229701SGeorge.Wilson@Sun.COM {
112310594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
112410594SGeorge.Wilson@Sun.COM 
112510594SGeorge.Wilson@Sun.COM 	/*
112610594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
112710594SGeorge.Wilson@Sun.COM 	 */
112810594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
112910594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
113010594SGeorge.Wilson@Sun.COM 
113110594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
113210594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
113310594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
113410594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
11359701SGeorge.Wilson@Sun.COM 	}
113610594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
113710594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
11389701SGeorge.Wilson@Sun.COM }
11399701SGeorge.Wilson@Sun.COM 
11409701SGeorge.Wilson@Sun.COM /*
11417294Sperrin  * Check for missing log devices
11427294Sperrin  */
11437294Sperrin int
11447294Sperrin spa_check_logs(spa_t *spa)
11457294Sperrin {
11467294Sperrin 	switch (spa->spa_log_state) {
11477294Sperrin 	case SPA_LOG_MISSING:
11487294Sperrin 		/* need to recheck in case slog has been restored */
11497294Sperrin 	case SPA_LOG_UNKNOWN:
11507294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
11517294Sperrin 		    DS_FIND_CHILDREN)) {
11527294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
11537294Sperrin 			return (1);
11547294Sperrin 		}
11557294Sperrin 		break;
11567294Sperrin 	}
11577294Sperrin 	return (0);
11587294Sperrin }
11597294Sperrin 
116010672SEric.Schrock@Sun.COM static void
116110672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
116210672SEric.Schrock@Sun.COM {
1163*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < sav->sav_count; i++)
116410672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
116510672SEric.Schrock@Sun.COM }
116610672SEric.Schrock@Sun.COM 
1167*10922SJeff.Bonwick@Sun.COM void
1168*10922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio)
1169*10922SJeff.Bonwick@Sun.COM {
1170*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
1171*10922SJeff.Bonwick@Sun.COM 
1172*10922SJeff.Bonwick@Sun.COM 	if (zio->io_error)
1173*10922SJeff.Bonwick@Sun.COM 		return;
1174*10922SJeff.Bonwick@Sun.COM 
1175*10922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
1176*10922SJeff.Bonwick@Sun.COM 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1177*10922SJeff.Bonwick@Sun.COM 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1178*10922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
1179*10922SJeff.Bonwick@Sun.COM }
1180*10922SJeff.Bonwick@Sun.COM 
118110921STim.Haley@Sun.COM typedef struct spa_load_error {
118210921STim.Haley@Sun.COM 	uint64_t	sle_metadata_count;
118310921STim.Haley@Sun.COM 	uint64_t	sle_data_count;
118410921STim.Haley@Sun.COM } spa_load_error_t;
118510921STim.Haley@Sun.COM 
118610921STim.Haley@Sun.COM static void
118710921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio)
118810921STim.Haley@Sun.COM {
118910921STim.Haley@Sun.COM 	blkptr_t *bp = zio->io_bp;
119010921STim.Haley@Sun.COM 	spa_load_error_t *sle = zio->io_private;
119110921STim.Haley@Sun.COM 	dmu_object_type_t type = BP_GET_TYPE(bp);
119210921STim.Haley@Sun.COM 	int error = zio->io_error;
119310921STim.Haley@Sun.COM 
119410921STim.Haley@Sun.COM 	if (error) {
119510921STim.Haley@Sun.COM 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
119610921STim.Haley@Sun.COM 		    type != DMU_OT_INTENT_LOG)
119710921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_metadata_count, 1);
119810921STim.Haley@Sun.COM 		else
119910921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_data_count, 1);
120010921STim.Haley@Sun.COM 	}
120110921STim.Haley@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
120210921STim.Haley@Sun.COM }
120310921STim.Haley@Sun.COM 
120410921STim.Haley@Sun.COM /*ARGSUSED*/
120510921STim.Haley@Sun.COM static int
1206*10922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1207*10922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
120810921STim.Haley@Sun.COM {
120910921STim.Haley@Sun.COM 	if (bp != NULL) {
121010921STim.Haley@Sun.COM 		zio_t *rio = arg;
121110921STim.Haley@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
121210921STim.Haley@Sun.COM 		void *data = zio_data_buf_alloc(size);
121310921STim.Haley@Sun.COM 
121410921STim.Haley@Sun.COM 		zio_nowait(zio_read(rio, spa, bp, data, size,
121510921STim.Haley@Sun.COM 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
121610921STim.Haley@Sun.COM 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
121710921STim.Haley@Sun.COM 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
121810921STim.Haley@Sun.COM 	}
121910921STim.Haley@Sun.COM 	return (0);
122010921STim.Haley@Sun.COM }
122110921STim.Haley@Sun.COM 
122210921STim.Haley@Sun.COM static int
122310921STim.Haley@Sun.COM spa_load_verify(spa_t *spa)
122410921STim.Haley@Sun.COM {
122510921STim.Haley@Sun.COM 	zio_t *rio;
122610921STim.Haley@Sun.COM 	spa_load_error_t sle = { 0 };
122710921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
122810921STim.Haley@Sun.COM 	boolean_t verify_ok = B_FALSE;
122910921STim.Haley@Sun.COM 	int error;
123010921STim.Haley@Sun.COM 
123110921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
123210921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
123310921STim.Haley@Sun.COM 
123410921STim.Haley@Sun.COM 	error = traverse_pool(spa, spa_load_verify_cb, rio,
123510921STim.Haley@Sun.COM 	    spa->spa_verify_min_txg);
123610921STim.Haley@Sun.COM 
123710921STim.Haley@Sun.COM 	(void) zio_wait(rio);
123810921STim.Haley@Sun.COM 
123910921STim.Haley@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
124010921STim.Haley@Sun.COM 
124110921STim.Haley@Sun.COM 	spa->spa_load_meta_errors = sle.sle_metadata_count;
124210921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
124310921STim.Haley@Sun.COM 
124410921STim.Haley@Sun.COM 	if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta &&
124510921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
124610921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
124710921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
124810921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
124910921STim.Haley@Sun.COM 	}
125010921STim.Haley@Sun.COM 
125110921STim.Haley@Sun.COM 	if (error) {
125210921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
125310921STim.Haley@Sun.COM 			error = EIO;
125410921STim.Haley@Sun.COM 		return (error);
125510921STim.Haley@Sun.COM 	}
125610921STim.Haley@Sun.COM 
125710921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
125810921STim.Haley@Sun.COM }
125910921STim.Haley@Sun.COM 
12607294Sperrin /*
1261789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
12621544Seschrock  * source of configuration information.
1263789Sahrens  */
1264789Sahrens static int
126510921STim.Haley@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, int mosconfig)
1266789Sahrens {
1267789Sahrens 	int error = 0;
126810594SGeorge.Wilson@Sun.COM 	nvlist_t *nvconfig, *nvroot = NULL;
1269789Sahrens 	vdev_t *rvd;
1270789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
12711635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1272789Sahrens 	uint64_t pool_guid;
12732082Seschrock 	uint64_t version;
12744451Seschrock 	uint64_t autoreplace = 0;
12758241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
12767294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
127710921STim.Haley@Sun.COM 	nvlist_t *config = spa->spa_config;
1278789Sahrens 
12798241SJeff.Bonwick@Sun.COM 	/*
12808241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
12818241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
12828241SJeff.Bonwick@Sun.COM 	 */
12838241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
12848241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
12858241SJeff.Bonwick@Sun.COM 
12867754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
12877754SJeff.Bonwick@Sun.COM 
12881544Seschrock 	spa->spa_load_state = state;
12891635Sbonwick 
1290789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
12911733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
12921544Seschrock 		error = EINVAL;
12931544Seschrock 		goto out;
12941544Seschrock 	}
1295789Sahrens 
12962082Seschrock 	/*
12972082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
12982082Seschrock 	 * it's not present treat it as the initial version.
12992082Seschrock 	 */
13002082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
13014577Sahrens 		version = SPA_VERSION_INITIAL;
13022082Seschrock 
13031733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
13041733Sbonwick 	    &spa->spa_config_txg);
13051733Sbonwick 
13061635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
13071544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
13081544Seschrock 		error = EEXIST;
13091544Seschrock 		goto out;
13101544Seschrock 	}
1311789Sahrens 
13122174Seschrock 	spa->spa_load_guid = pool_guid;
13132174Seschrock 
1314789Sahrens 	/*
13159234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
13169234SGeorge.Wilson@Sun.COM 	 */
13179630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
13189630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
13199234SGeorge.Wilson@Sun.COM 
13209234SGeorge.Wilson@Sun.COM 	/*
13212082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
13222082Seschrock 	 * value that will be returned by spa_version() since parsing the
13232082Seschrock 	 * configuration requires knowing the version number.
1324789Sahrens 	 */
13257754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13262082Seschrock 	spa->spa_ubsync.ub_version = version;
13272082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
13287754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1329789Sahrens 
13302082Seschrock 	if (error != 0)
13311544Seschrock 		goto out;
1332789Sahrens 
13331585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1334789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1335789Sahrens 
1336789Sahrens 	/*
1337789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1338789Sahrens 	 */
13397754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13404070Smc142369 	error = vdev_open(rvd);
13417754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13424070Smc142369 	if (error != 0)
13431544Seschrock 		goto out;
1344789Sahrens 
1345789Sahrens 	/*
13469276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
13479276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
13489276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
13499276SMark.Musante@Sun.COM 	 * that config. Otherwise, we're validating against the cached config
13509276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
13519276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
13529276SMark.Musante@Sun.COM 	 * the vdev config.
13531986Seschrock 	 */
13549276SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13559276SMark.Musante@Sun.COM 	error = vdev_validate(rvd);
13569276SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13579276SMark.Musante@Sun.COM 	if (error != 0)
13589276SMark.Musante@Sun.COM 		goto out;
13591986Seschrock 
13601986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13611986Seschrock 		error = ENXIO;
13621986Seschrock 		goto out;
13631986Seschrock 	}
13641986Seschrock 
13651986Seschrock 	/*
1366789Sahrens 	 * Find the best uberblock.
1367789Sahrens 	 */
13687754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1369789Sahrens 
1370789Sahrens 	/*
1371789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1372789Sahrens 	 */
1373789Sahrens 	if (ub->ub_txg == 0) {
13741760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13751760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13761544Seschrock 		error = ENXIO;
13771544Seschrock 		goto out;
13781544Seschrock 	}
13791544Seschrock 
13801544Seschrock 	/*
13811544Seschrock 	 * If the pool is newer than the code, we can't open it.
13821544Seschrock 	 */
13834577Sahrens 	if (ub->ub_version > SPA_VERSION) {
13841760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13851760Seschrock 		    VDEV_AUX_VERSION_NEWER);
13861544Seschrock 		error = ENOTSUP;
13871544Seschrock 		goto out;
1388789Sahrens 	}
1389789Sahrens 
1390789Sahrens 	/*
1391789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1392789Sahrens 	 * incomplete configuration.
1393789Sahrens 	 */
13941732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
13951544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13961544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
13971544Seschrock 		error = ENXIO;
13981544Seschrock 		goto out;
1399789Sahrens 	}
1400789Sahrens 
1401789Sahrens 	/*
1402789Sahrens 	 * Initialize internal SPA structures.
1403789Sahrens 	 */
1404789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1405789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
140610921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
140710921STim.Haley@Sun.COM 	    TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE;
140810921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
140910921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
1410*10922SJeff.Bonwick@Sun.COM 	spa->spa_claim_max_txg = spa->spa_first_txg;
1411*10922SJeff.Bonwick@Sun.COM 
14121544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
14131544Seschrock 	if (error) {
14141544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14151544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
141610921STim.Haley@Sun.COM 		error = EIO;
14171544Seschrock 		goto out;
14181544Seschrock 	}
1419789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1420789Sahrens 
14211544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1422789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
14231544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
14241544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14251544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14261544Seschrock 		error = EIO;
14271544Seschrock 		goto out;
14281544Seschrock 	}
1429789Sahrens 
143010594SGeorge.Wilson@Sun.COM 	if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) {
143110594SGeorge.Wilson@Sun.COM 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
143210594SGeorge.Wilson@Sun.COM 		    VDEV_AUX_CORRUPT_DATA);
143310594SGeorge.Wilson@Sun.COM 		error = EIO;
143410594SGeorge.Wilson@Sun.COM 		goto out;
143510594SGeorge.Wilson@Sun.COM 	}
143610594SGeorge.Wilson@Sun.COM 
1437789Sahrens 	if (!mosconfig) {
14383975Sek110237 		uint64_t hostid;
14392082Seschrock 
144010594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
14417706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
14423975Sek110237 			char *hostname;
14433975Sek110237 			unsigned long myhostid = 0;
14443975Sek110237 
144510594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
14463975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
14473975Sek110237 
14488662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
14498662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
14508662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
14518662SJordan.Vaughan@Sun.com 			/*
14528662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
14538662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
14548662SJordan.Vaughan@Sun.com 			 */
14553975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
14568662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
14574178Slling 			if (hostid != 0 && myhostid != 0 &&
14588662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
14593975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
14603975Sek110237 				    "loaded as it was last accessed by "
14617706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
14623975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
14637754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
14643975Sek110237 				    (unsigned long)hostid);
14653975Sek110237 				error = EBADF;
14663975Sek110237 				goto out;
14673975Sek110237 			}
14683975Sek110237 		}
14693975Sek110237 
147010594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1471789Sahrens 		spa_unload(spa);
1472789Sahrens 		spa_deactivate(spa);
14738241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1474789Sahrens 
147510921STim.Haley@Sun.COM 		return (spa_load(spa, state, B_TRUE));
14761544Seschrock 	}
14771544Seschrock 
14781544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
14791544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1480*10922SJeff.Bonwick@Sun.COM 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj) != 0) {
14811544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14821544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14831544Seschrock 		error = EIO;
14841544Seschrock 		goto out;
1485789Sahrens 	}
1486789Sahrens 
14871544Seschrock 	/*
14882082Seschrock 	 * Load the bit that tells us to use the new accounting function
14892082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
14902082Seschrock 	 * be present.
14912082Seschrock 	 */
14922082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
14932082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
14942082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
14952082Seschrock 	if (error != 0 && error != ENOENT) {
14962082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14972082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14982082Seschrock 		error = EIO;
14992082Seschrock 		goto out;
15002082Seschrock 	}
15012082Seschrock 
15022082Seschrock 	/*
15031544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
15041544Seschrock 	 * not be present.
15051544Seschrock 	 */
15061544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
15071544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
15081544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
15091807Sbonwick 	if (error != 0 && error != ENOENT) {
15101544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15111544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
15121544Seschrock 		error = EIO;
15131544Seschrock 		goto out;
15141544Seschrock 	}
15151544Seschrock 
15161544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
15171544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
15181544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
15191544Seschrock 	if (error != 0 && error != ENOENT) {
15201544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15211544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
15221544Seschrock 		error = EIO;
15231544Seschrock 		goto out;
15241544Seschrock 	}
1525789Sahrens 
1526789Sahrens 	/*
15272926Sek110237 	 * Load the history object.  If we have an older pool, this
15282926Sek110237 	 * will not be present.
15292926Sek110237 	 */
15302926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
15312926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
15322926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
15332926Sek110237 	if (error != 0 && error != ENOENT) {
15342926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15352926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
15362926Sek110237 		error = EIO;
15372926Sek110237 		goto out;
15382926Sek110237 	}
15392926Sek110237 
15402926Sek110237 	/*
15412082Seschrock 	 * Load any hot spares for this pool.
15422082Seschrock 	 */
15432082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15445450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
15452082Seschrock 	if (error != 0 && error != ENOENT) {
15462082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15472082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
15482082Seschrock 		error = EIO;
15492082Seschrock 		goto out;
15502082Seschrock 	}
15512082Seschrock 	if (error == 0) {
15524577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
15535450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
15545450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
15552082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15562082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
15572082Seschrock 			error = EIO;
15582082Seschrock 			goto out;
15592082Seschrock 		}
15602082Seschrock 
15617754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
15622082Seschrock 		spa_load_spares(spa);
15637754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
15642082Seschrock 	}
15652082Seschrock 
15665450Sbrendan 	/*
15675450Sbrendan 	 * Load any level 2 ARC devices for this pool.
15685450Sbrendan 	 */
15695450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15705450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
15715450Sbrendan 	    &spa->spa_l2cache.sav_object);
15725450Sbrendan 	if (error != 0 && error != ENOENT) {
15735450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15745450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
15755450Sbrendan 		error = EIO;
15765450Sbrendan 		goto out;
15775450Sbrendan 	}
15785450Sbrendan 	if (error == 0) {
15795450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
15805450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
15815450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
15825450Sbrendan 			vdev_set_state(rvd, B_TRUE,
15835450Sbrendan 			    VDEV_STATE_CANT_OPEN,
15845450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
15855450Sbrendan 			error = EIO;
15865450Sbrendan 			goto out;
15875450Sbrendan 		}
15885450Sbrendan 
15897754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
15905450Sbrendan 		spa_load_l2cache(spa);
15917754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
15925450Sbrendan 	}
15935450Sbrendan 
15945094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
15954543Smarks 
15963912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15973912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
15983912Slling 
15993912Slling 	if (error && error != ENOENT) {
16003912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
16013912Slling 		    VDEV_AUX_CORRUPT_DATA);
16023912Slling 		error = EIO;
16033912Slling 		goto out;
16043912Slling 	}
16053912Slling 
16063912Slling 	if (error == 0) {
16073912Slling 		(void) zap_lookup(spa->spa_meta_objset,
16083912Slling 		    spa->spa_pool_props_object,
16094451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
16103912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
16114451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
16124451Seschrock 		    spa->spa_pool_props_object,
16134451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
16144451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
161510672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
16164543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
16174543Smarks 		    spa->spa_pool_props_object,
16184543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
16194543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
16205329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
16215329Sgw25295 		    spa->spa_pool_props_object,
16225329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
16235329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
16249816SGeorge.Wilson@Sun.COM 		(void) zap_lookup(spa->spa_meta_objset,
16259816SGeorge.Wilson@Sun.COM 		    spa->spa_pool_props_object,
16269816SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND),
16279816SGeorge.Wilson@Sun.COM 		    sizeof (uint64_t), 1, &spa->spa_autoexpand);
1628*10922SJeff.Bonwick@Sun.COM 		(void) zap_lookup(spa->spa_meta_objset,
1629*10922SJeff.Bonwick@Sun.COM 		    spa->spa_pool_props_object,
1630*10922SJeff.Bonwick@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO),
1631*10922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &spa->spa_dedup_ditto);
16323912Slling 	}
16333912Slling 
16342082Seschrock 	/*
16354451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
16364451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
16374451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
16384451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
16394451Seschrock 	 * over.
16404451Seschrock 	 */
164110672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
16424451Seschrock 		spa_check_removed(spa->spa_root_vdev);
164310672SEric.Schrock@Sun.COM 		/*
164410672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
164510672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
164610672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
164710672SEric.Schrock@Sun.COM 		 */
164810672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
164910672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
165010672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
165110672SEric.Schrock@Sun.COM 		}
165210672SEric.Schrock@Sun.COM 	}
16534451Seschrock 
16544451Seschrock 	/*
16551986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1656789Sahrens 	 */
16571986Seschrock 	vdev_load(rvd);
1658789Sahrens 
1659789Sahrens 	/*
1660789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1661789Sahrens 	 */
16627754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1663789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
16647754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1665789Sahrens 
1666789Sahrens 	/*
1667789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1668789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1669789Sahrens 	 */
16701544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
16711544Seschrock 		error = ENXIO;
16721544Seschrock 		goto out;
16731544Seschrock 	}
1674789Sahrens 
1675*10922SJeff.Bonwick@Sun.COM 	/*
1676*10922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
1677*10922SJeff.Bonwick@Sun.COM 	 */
1678*10922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
1679*10922SJeff.Bonwick@Sun.COM 	if (error != 0) {
1680*10922SJeff.Bonwick@Sun.COM 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1681*10922SJeff.Bonwick@Sun.COM 		    VDEV_AUX_CORRUPT_DATA);
1682*10922SJeff.Bonwick@Sun.COM 		error = EIO;
1683*10922SJeff.Bonwick@Sun.COM 		goto out;
1684*10922SJeff.Bonwick@Sun.COM 	}
1685*10922SJeff.Bonwick@Sun.COM 
168610921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
168710921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
168810921STim.Haley@Sun.COM 		if (error) {
168910921STim.Haley@Sun.COM 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
169010921STim.Haley@Sun.COM 			    VDEV_AUX_CORRUPT_DATA);
169110921STim.Haley@Sun.COM 			goto out;
169210921STim.Haley@Sun.COM 		}
169310921STim.Haley@Sun.COM 	}
169410921STim.Haley@Sun.COM 
1695*10922SJeff.Bonwick@Sun.COM 	/*
1696*10922SJeff.Bonwick@Sun.COM 	 * Load the intent log state and check log integrity.
1697*10922SJeff.Bonwick@Sun.COM 	 */
1698*10922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
1699*10922SJeff.Bonwick@Sun.COM 	    &nvroot) == 0);
1700*10922SJeff.Bonwick@Sun.COM 	spa_load_log_state(spa, nvroot);
1701*10922SJeff.Bonwick@Sun.COM 	nvlist_free(nvconfig);
1702*10922SJeff.Bonwick@Sun.COM 
1703*10922SJeff.Bonwick@Sun.COM 	if (spa_check_logs(spa)) {
1704*10922SJeff.Bonwick@Sun.COM 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1705*10922SJeff.Bonwick@Sun.COM 		    VDEV_AUX_BAD_LOG);
1706*10922SJeff.Bonwick@Sun.COM 		error = ENXIO;
1707*10922SJeff.Bonwick@Sun.COM 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
1708*10922SJeff.Bonwick@Sun.COM 		goto out;
1709*10922SJeff.Bonwick@Sun.COM 	}
1710*10922SJeff.Bonwick@Sun.COM 
171110921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
171210921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
17131635Sbonwick 		dmu_tx_t *tx;
17141635Sbonwick 		int need_update = B_FALSE;
17158241SJeff.Bonwick@Sun.COM 
17168241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
17171601Sbonwick 
17181635Sbonwick 		/*
17191635Sbonwick 		 * Claim log blocks that haven't been committed yet.
17201635Sbonwick 		 * This must all happen in a single txg.
1721*10922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
1722*10922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
172310921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
17241635Sbonwick 		 */
1725*10922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
1726*10922SJeff.Bonwick@Sun.COM 
17271601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1728789Sahrens 		    spa_first_txg(spa));
17297754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
17302417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1731789Sahrens 		dmu_tx_commit(tx);
1732789Sahrens 
1733*10922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
1734*10922SJeff.Bonwick@Sun.COM 
17359701SGeorge.Wilson@Sun.COM 		spa->spa_log_state = SPA_LOG_GOOD;
1736789Sahrens 		spa->spa_sync_on = B_TRUE;
1737789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1738789Sahrens 
1739789Sahrens 		/*
1740*10922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
1741*10922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
1742*10922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
1743*10922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
1744*10922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
1745789Sahrens 		 */
1746*10922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
17471585Sbonwick 
17481585Sbonwick 		/*
17491635Sbonwick 		 * If the config cache is stale, or we have uninitialized
17501635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
175110100SLin.Ling@Sun.COM 		 *
175210100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
175310100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
17541585Sbonwick 		 */
17551635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
175610921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
175710921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
17581635Sbonwick 			need_update = B_TRUE;
17591635Sbonwick 
17608241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
17611635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
17621635Sbonwick 				need_update = B_TRUE;
17631585Sbonwick 
17641585Sbonwick 		/*
17651635Sbonwick 		 * Update the config cache asychronously in case we're the
17661635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
17671585Sbonwick 		 */
17681635Sbonwick 		if (need_update)
17691635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
17708241SJeff.Bonwick@Sun.COM 
17718241SJeff.Bonwick@Sun.COM 		/*
17728241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
17738241SJeff.Bonwick@Sun.COM 		 */
17748241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
17758241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
177610298SMatthew.Ahrens@Sun.COM 
177710298SMatthew.Ahrens@Sun.COM 		/*
177810298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
177910298SMatthew.Ahrens@Sun.COM 		 */
178010298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
178110298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
178210342Schris.kirby@sun.com 
178310342Schris.kirby@sun.com 		/*
178410342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
178510342Schris.kirby@sun.com 		 */
178610342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
1787789Sahrens 	}
1788789Sahrens 
17891544Seschrock 	error = 0;
17901544Seschrock out:
179110921STim.Haley@Sun.COM 
17927046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
17932082Seschrock 	if (error && error != EBADF)
17947294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
17951544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
17961544Seschrock 	spa->spa_ena = 0;
17971544Seschrock 
17981544Seschrock 	return (error);
1799789Sahrens }
1800789Sahrens 
180110921STim.Haley@Sun.COM static int
180210921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
180310921STim.Haley@Sun.COM {
180410921STim.Haley@Sun.COM 	spa_unload(spa);
180510921STim.Haley@Sun.COM 	spa_deactivate(spa);
180610921STim.Haley@Sun.COM 
180710921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
180810921STim.Haley@Sun.COM 
180910921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
181010921STim.Haley@Sun.COM 	spa_async_suspend(spa);
181110921STim.Haley@Sun.COM 
181210921STim.Haley@Sun.COM 	return (spa_load(spa, state, mosconfig));
181310921STim.Haley@Sun.COM }
181410921STim.Haley@Sun.COM 
181510921STim.Haley@Sun.COM static int
181610921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
181710921STim.Haley@Sun.COM     uint64_t max_request, boolean_t extreme)
181810921STim.Haley@Sun.COM {
181910921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
182010921STim.Haley@Sun.COM 	int load_error, rewind_error;
182110921STim.Haley@Sun.COM 	uint64_t safe_rollback_txg;
182210921STim.Haley@Sun.COM 	uint64_t min_txg;
182310921STim.Haley@Sun.COM 
182410921STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER)
182510921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
182610921STim.Haley@Sun.COM 	else
182710921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
182810921STim.Haley@Sun.COM 
182910921STim.Haley@Sun.COM 	load_error = rewind_error = spa_load(spa, state, mosconfig);
183010921STim.Haley@Sun.COM 	if (load_error == 0)
183110921STim.Haley@Sun.COM 		return (0);
183210921STim.Haley@Sun.COM 
183310921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
183410921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
183510921STim.Haley@Sun.COM 
183610921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
183710921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
183810921STim.Haley@Sun.COM 
183910921STim.Haley@Sun.COM 	/* specific txg requested */
184010921STim.Haley@Sun.COM 	if (spa->spa_load_max_txg != UINT64_MAX && !extreme) {
184110921STim.Haley@Sun.COM 		nvlist_free(config);
184210921STim.Haley@Sun.COM 		return (load_error);
184310921STim.Haley@Sun.COM 	}
184410921STim.Haley@Sun.COM 
184510921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
184610921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
184710921STim.Haley@Sun.COM 		spa->spa_log_state = SPA_LOG_CLEAR;
184810921STim.Haley@Sun.COM 
184910921STim.Haley@Sun.COM 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
185010921STim.Haley@Sun.COM 	safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE;
185110921STim.Haley@Sun.COM 
185210921STim.Haley@Sun.COM 	min_txg = extreme ? TXG_INITIAL : safe_rollback_txg;
185310921STim.Haley@Sun.COM 	while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) {
185410921STim.Haley@Sun.COM 		if (spa->spa_load_max_txg < safe_rollback_txg)
185510921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
185610921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
185710921STim.Haley@Sun.COM 	}
185810921STim.Haley@Sun.COM 
185910921STim.Haley@Sun.COM 	if (config)
186010921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
186110921STim.Haley@Sun.COM 
186210921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
186310921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
186410921STim.Haley@Sun.COM 
186510921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
186610921STim.Haley@Sun.COM 		spa_config_set(spa, config);
186710921STim.Haley@Sun.COM 
186810921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
186910921STim.Haley@Sun.COM }
187010921STim.Haley@Sun.COM 
1871789Sahrens /*
1872789Sahrens  * Pool Open/Import
1873789Sahrens  *
1874789Sahrens  * The import case is identical to an open except that the configuration is sent
1875789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1876789Sahrens  * case of an open, the pool configuration will exist in the
18774451Seschrock  * POOL_STATE_UNINITIALIZED state.
1878789Sahrens  *
1879789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1880789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1881789Sahrens  * ambiguous state.
1882789Sahrens  */
1883789Sahrens static int
188410921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
188510921STim.Haley@Sun.COM     nvlist_t **config)
1886789Sahrens {
1887789Sahrens 	spa_t *spa;
188810921STim.Haley@Sun.COM 	boolean_t norewind;
188910921STim.Haley@Sun.COM 	boolean_t extreme;
189010921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
189110921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_OPEN;
1892789Sahrens 	int error;
1893789Sahrens 	int locked = B_FALSE;
1894789Sahrens 
1895789Sahrens 	*spapp = NULL;
1896789Sahrens 
189710921STim.Haley@Sun.COM 	zpool_get_rewind_policy(nvpolicy, &policy);
189810921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
189910921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
190010921STim.Haley@Sun.COM 	norewind = (policy.zrp_request == ZPOOL_NO_REWIND);
190110921STim.Haley@Sun.COM 	extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0);
190210921STim.Haley@Sun.COM 
1903789Sahrens 	/*
1904789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1905789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1906789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1907789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1908789Sahrens 	 */
1909789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1910789Sahrens 		mutex_enter(&spa_namespace_lock);
1911789Sahrens 		locked = B_TRUE;
1912789Sahrens 	}
1913789Sahrens 
1914789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1915789Sahrens 		if (locked)
1916789Sahrens 			mutex_exit(&spa_namespace_lock);
1917789Sahrens 		return (ENOENT);
1918789Sahrens 	}
191910921STim.Haley@Sun.COM 
1920789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1921789Sahrens 
19228241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
1923789Sahrens 
192410921STim.Haley@Sun.COM 		if (spa->spa_last_open_failed && norewind) {
192510921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
192610921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config,
192710921STim.Haley@Sun.COM 				    config, KM_SLEEP) == 0);
192810921STim.Haley@Sun.COM 			spa_deactivate(spa);
192910921STim.Haley@Sun.COM 			if (locked)
193010921STim.Haley@Sun.COM 				mutex_exit(&spa_namespace_lock);
193110921STim.Haley@Sun.COM 			return (spa->spa_last_open_failed);
193210921STim.Haley@Sun.COM 		}
193310921STim.Haley@Sun.COM 
193410921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
193510921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
193610921STim.Haley@Sun.COM 
193710921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
193810921STim.Haley@Sun.COM 		    extreme);
1939789Sahrens 
1940789Sahrens 		if (error == EBADF) {
1941789Sahrens 			/*
19421986Seschrock 			 * If vdev_validate() returns failure (indicated by
19431986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
19441986Seschrock 			 * that the pool has been exported or destroyed.  If
19451986Seschrock 			 * this is the case, the config cache is out of sync and
19461986Seschrock 			 * we should remove the pool from the namespace.
1947789Sahrens 			 */
1948789Sahrens 			spa_unload(spa);
1949789Sahrens 			spa_deactivate(spa);
19506643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1951789Sahrens 			spa_remove(spa);
1952789Sahrens 			if (locked)
1953789Sahrens 				mutex_exit(&spa_namespace_lock);
1954789Sahrens 			return (ENOENT);
19551544Seschrock 		}
19561544Seschrock 
19571544Seschrock 		if (error) {
1958789Sahrens 			/*
1959789Sahrens 			 * We can't open the pool, but we still have useful
1960789Sahrens 			 * information: the state of each vdev after the
1961789Sahrens 			 * attempted vdev_open().  Return this to the user.
1962789Sahrens 			 */
196310921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
196410921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
196510921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
1966789Sahrens 			spa_unload(spa);
1967789Sahrens 			spa_deactivate(spa);
196810921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
1969789Sahrens 			if (locked)
1970789Sahrens 				mutex_exit(&spa_namespace_lock);
1971789Sahrens 			*spapp = NULL;
1972789Sahrens 			return (error);
1973789Sahrens 		}
197410921STim.Haley@Sun.COM 
1975789Sahrens 	}
1976789Sahrens 
1977789Sahrens 	spa_open_ref(spa, tag);
19784451Seschrock 
197910921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
198010921STim.Haley@Sun.COM 
198110921STim.Haley@Sun.COM 	if (config != NULL)
198210921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
198310921STim.Haley@Sun.COM 
198410921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = 0;
198510921STim.Haley@Sun.COM 	spa->spa_load_txg = 0;
198610921STim.Haley@Sun.COM 
1987789Sahrens 	if (locked)
1988789Sahrens 		mutex_exit(&spa_namespace_lock);
1989789Sahrens 
1990789Sahrens 	*spapp = spa;
1991789Sahrens 
1992789Sahrens 	return (0);
1993789Sahrens }
1994789Sahrens 
1995789Sahrens int
199610921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
199710921STim.Haley@Sun.COM     nvlist_t **config)
199810921STim.Haley@Sun.COM {
199910921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
200010921STim.Haley@Sun.COM }
200110921STim.Haley@Sun.COM 
200210921STim.Haley@Sun.COM int
2003789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2004789Sahrens {
200510921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2006789Sahrens }
2007789Sahrens 
20081544Seschrock /*
20091544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
20101544Seschrock  * preventing it from being exported or destroyed.
20111544Seschrock  */
20121544Seschrock spa_t *
20131544Seschrock spa_inject_addref(char *name)
20141544Seschrock {
20151544Seschrock 	spa_t *spa;
20161544Seschrock 
20171544Seschrock 	mutex_enter(&spa_namespace_lock);
20181544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
20191544Seschrock 		mutex_exit(&spa_namespace_lock);
20201544Seschrock 		return (NULL);
20211544Seschrock 	}
20221544Seschrock 	spa->spa_inject_ref++;
20231544Seschrock 	mutex_exit(&spa_namespace_lock);
20241544Seschrock 
20251544Seschrock 	return (spa);
20261544Seschrock }
20271544Seschrock 
20281544Seschrock void
20291544Seschrock spa_inject_delref(spa_t *spa)
20301544Seschrock {
20311544Seschrock 	mutex_enter(&spa_namespace_lock);
20321544Seschrock 	spa->spa_inject_ref--;
20331544Seschrock 	mutex_exit(&spa_namespace_lock);
20341544Seschrock }
20351544Seschrock 
20365450Sbrendan /*
20375450Sbrendan  * Add spares device information to the nvlist.
20385450Sbrendan  */
20392082Seschrock static void
20402082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
20412082Seschrock {
20422082Seschrock 	nvlist_t **spares;
20432082Seschrock 	uint_t i, nspares;
20442082Seschrock 	nvlist_t *nvroot;
20452082Seschrock 	uint64_t guid;
20462082Seschrock 	vdev_stat_t *vs;
20472082Seschrock 	uint_t vsc;
20483377Seschrock 	uint64_t pool;
20492082Seschrock 
20509425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
20519425SEric.Schrock@Sun.COM 
20525450Sbrendan 	if (spa->spa_spares.sav_count == 0)
20532082Seschrock 		return;
20542082Seschrock 
20552082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
20562082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
20575450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
20582082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
20592082Seschrock 	if (nspares != 0) {
20602082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
20612082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
20622082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
20632082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
20642082Seschrock 
20652082Seschrock 		/*
20662082Seschrock 		 * Go through and find any spares which have since been
20672082Seschrock 		 * repurposed as an active spare.  If this is the case, update
20682082Seschrock 		 * their status appropriately.
20692082Seschrock 		 */
20702082Seschrock 		for (i = 0; i < nspares; i++) {
20712082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
20722082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
20737214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
20747214Slling 			    pool != 0ULL) {
20752082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
20762082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
20772082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
20782082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
20792082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
20802082Seschrock 			}
20812082Seschrock 		}
20822082Seschrock 	}
20832082Seschrock }
20842082Seschrock 
20855450Sbrendan /*
20865450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
20875450Sbrendan  */
20885450Sbrendan static void
20895450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
20905450Sbrendan {
20915450Sbrendan 	nvlist_t **l2cache;
20925450Sbrendan 	uint_t i, j, nl2cache;
20935450Sbrendan 	nvlist_t *nvroot;
20945450Sbrendan 	uint64_t guid;
20955450Sbrendan 	vdev_t *vd;
20965450Sbrendan 	vdev_stat_t *vs;
20975450Sbrendan 	uint_t vsc;
20985450Sbrendan 
20999425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
21009425SEric.Schrock@Sun.COM 
21015450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
21025450Sbrendan 		return;
21035450Sbrendan 
21045450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
21055450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
21065450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
21075450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
21085450Sbrendan 	if (nl2cache != 0) {
21095450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
21105450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
21115450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
21125450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
21135450Sbrendan 
21145450Sbrendan 		/*
21155450Sbrendan 		 * Update level 2 cache device stats.
21165450Sbrendan 		 */
21175450Sbrendan 
21185450Sbrendan 		for (i = 0; i < nl2cache; i++) {
21195450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
21205450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
21215450Sbrendan 
21225450Sbrendan 			vd = NULL;
21235450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
21245450Sbrendan 				if (guid ==
21255450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
21265450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
21275450Sbrendan 					break;
21285450Sbrendan 				}
21295450Sbrendan 			}
21305450Sbrendan 			ASSERT(vd != NULL);
21315450Sbrendan 
21325450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
21335450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
21345450Sbrendan 			vdev_get_stats(vd, vs);
21355450Sbrendan 		}
21365450Sbrendan 	}
21375450Sbrendan }
21385450Sbrendan 
2139789Sahrens int
21401544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2141789Sahrens {
2142789Sahrens 	int error;
2143789Sahrens 	spa_t *spa;
2144789Sahrens 
2145789Sahrens 	*config = NULL;
214610921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2147789Sahrens 
21489425SEric.Schrock@Sun.COM 	if (spa != NULL) {
21499425SEric.Schrock@Sun.COM 		/*
21509425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
21519425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
21529425SEric.Schrock@Sun.COM 		 * self-inconsistent.
21539425SEric.Schrock@Sun.COM 		 */
21549425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
21559425SEric.Schrock@Sun.COM 
21569425SEric.Schrock@Sun.COM 		if (*config != NULL) {
21577754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
21589425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
21599425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
21609425SEric.Schrock@Sun.COM 
21619425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
21629425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
21639425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
21649425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
21659425SEric.Schrock@Sun.COM 
21669425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
21679425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
21689425SEric.Schrock@Sun.COM 		}
21692082Seschrock 	}
21702082Seschrock 
21711544Seschrock 	/*
21721544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
21731544Seschrock 	 * and call spa_lookup() directly.
21741544Seschrock 	 */
21751544Seschrock 	if (altroot) {
21761544Seschrock 		if (spa == NULL) {
21771544Seschrock 			mutex_enter(&spa_namespace_lock);
21781544Seschrock 			spa = spa_lookup(name);
21791544Seschrock 			if (spa)
21801544Seschrock 				spa_altroot(spa, altroot, buflen);
21811544Seschrock 			else
21821544Seschrock 				altroot[0] = '\0';
21831544Seschrock 			spa = NULL;
21841544Seschrock 			mutex_exit(&spa_namespace_lock);
21851544Seschrock 		} else {
21861544Seschrock 			spa_altroot(spa, altroot, buflen);
21871544Seschrock 		}
21881544Seschrock 	}
21891544Seschrock 
21909425SEric.Schrock@Sun.COM 	if (spa != NULL) {
21919425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2192789Sahrens 		spa_close(spa, FTAG);
21939425SEric.Schrock@Sun.COM 	}
2194789Sahrens 
2195789Sahrens 	return (error);
2196789Sahrens }
2197789Sahrens 
2198789Sahrens /*
21995450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
22005450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
22015450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
22025450Sbrendan  * specified, as long as they are well-formed.
22032082Seschrock  */
22042082Seschrock static int
22055450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
22065450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
22075450Sbrendan     vdev_labeltype_t label)
22082082Seschrock {
22095450Sbrendan 	nvlist_t **dev;
22105450Sbrendan 	uint_t i, ndev;
22112082Seschrock 	vdev_t *vd;
22122082Seschrock 	int error;
22132082Seschrock 
22147754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
22157754SJeff.Bonwick@Sun.COM 
22162082Seschrock 	/*
22175450Sbrendan 	 * It's acceptable to have no devs specified.
22182082Seschrock 	 */
22195450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
22202082Seschrock 		return (0);
22212082Seschrock 
22225450Sbrendan 	if (ndev == 0)
22232082Seschrock 		return (EINVAL);
22242082Seschrock 
22252082Seschrock 	/*
22265450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
22275450Sbrendan 	 * device type.
22282082Seschrock 	 */
22295450Sbrendan 	if (spa_version(spa) < version)
22302082Seschrock 		return (ENOTSUP);
22312082Seschrock 
22323377Seschrock 	/*
22335450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
22343377Seschrock 	 * checking.
22353377Seschrock 	 */
22365450Sbrendan 	sav->sav_pending = dev;
22375450Sbrendan 	sav->sav_npending = ndev;
22385450Sbrendan 
22395450Sbrendan 	for (i = 0; i < ndev; i++) {
22405450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
22412082Seschrock 		    mode)) != 0)
22423377Seschrock 			goto out;
22432082Seschrock 
22442082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
22452082Seschrock 			vdev_free(vd);
22463377Seschrock 			error = EINVAL;
22473377Seschrock 			goto out;
22482082Seschrock 		}
22492082Seschrock 
22505450Sbrendan 		/*
22517754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
22527754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
22535450Sbrendan 		 */
22547754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
22555450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
22565450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
22575450Sbrendan 			error = ENOTBLK;
22585450Sbrendan 			goto out;
22595450Sbrendan 		}
22607754SJeff.Bonwick@Sun.COM #endif
22612082Seschrock 		vd->vdev_top = vd;
22623377Seschrock 
22633377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
22645450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
22655450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
22663377Seschrock 			    vd->vdev_guid) == 0);
22672082Seschrock 		}
22682082Seschrock 
22692082Seschrock 		vdev_free(vd);
22703377Seschrock 
22715450Sbrendan 		if (error &&
22725450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
22733377Seschrock 			goto out;
22743377Seschrock 		else
22753377Seschrock 			error = 0;
22762082Seschrock 	}
22772082Seschrock 
22783377Seschrock out:
22795450Sbrendan 	sav->sav_pending = NULL;
22805450Sbrendan 	sav->sav_npending = 0;
22813377Seschrock 	return (error);
22822082Seschrock }
22832082Seschrock 
22845450Sbrendan static int
22855450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
22865450Sbrendan {
22875450Sbrendan 	int error;
22885450Sbrendan 
22897754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
22907754SJeff.Bonwick@Sun.COM 
22915450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
22925450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
22935450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
22945450Sbrendan 		return (error);
22955450Sbrendan 	}
22965450Sbrendan 
22975450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
22985450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
22995450Sbrendan 	    VDEV_LABEL_L2CACHE));
23005450Sbrendan }
23015450Sbrendan 
23025450Sbrendan static void
23035450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
23045450Sbrendan     const char *config)
23055450Sbrendan {
23065450Sbrendan 	int i;
23075450Sbrendan 
23085450Sbrendan 	if (sav->sav_config != NULL) {
23095450Sbrendan 		nvlist_t **olddevs;
23105450Sbrendan 		uint_t oldndevs;
23115450Sbrendan 		nvlist_t **newdevs;
23125450Sbrendan 
23135450Sbrendan 		/*
23145450Sbrendan 		 * Generate new dev list by concatentating with the
23155450Sbrendan 		 * current dev list.
23165450Sbrendan 		 */
23175450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
23185450Sbrendan 		    &olddevs, &oldndevs) == 0);
23195450Sbrendan 
23205450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
23215450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
23225450Sbrendan 		for (i = 0; i < oldndevs; i++)
23235450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
23245450Sbrendan 			    KM_SLEEP) == 0);
23255450Sbrendan 		for (i = 0; i < ndevs; i++)
23265450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
23275450Sbrendan 			    KM_SLEEP) == 0);
23285450Sbrendan 
23295450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
23305450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
23315450Sbrendan 
23325450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
23335450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
23345450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
23355450Sbrendan 			nvlist_free(newdevs[i]);
23365450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
23375450Sbrendan 	} else {
23385450Sbrendan 		/*
23395450Sbrendan 		 * Generate a new dev list.
23405450Sbrendan 		 */
23415450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
23425450Sbrendan 		    KM_SLEEP) == 0);
23435450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
23445450Sbrendan 		    devs, ndevs) == 0);
23455450Sbrendan 	}
23465450Sbrendan }
23475450Sbrendan 
23485450Sbrendan /*
23495450Sbrendan  * Stop and drop level 2 ARC devices
23505450Sbrendan  */
23515450Sbrendan void
23525450Sbrendan spa_l2cache_drop(spa_t *spa)
23535450Sbrendan {
23545450Sbrendan 	vdev_t *vd;
23555450Sbrendan 	int i;
23565450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
23575450Sbrendan 
23585450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
23595450Sbrendan 		uint64_t pool;
23605450Sbrendan 
23615450Sbrendan 		vd = sav->sav_vdevs[i];
23625450Sbrendan 		ASSERT(vd != NULL);
23635450Sbrendan 
23648241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
23658241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
23665450Sbrendan 			l2arc_remove_vdev(vd);
23675450Sbrendan 		if (vd->vdev_isl2cache)
23685450Sbrendan 			spa_l2cache_remove(vd);
23695450Sbrendan 		vdev_clear_stats(vd);
23705450Sbrendan 		(void) vdev_close(vd);
23715450Sbrendan 	}
23725450Sbrendan }
23735450Sbrendan 
23742082Seschrock /*
2375789Sahrens  * Pool Creation
2376789Sahrens  */
2377789Sahrens int
23785094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
23797184Stimh     const char *history_str, nvlist_t *zplprops)
2380789Sahrens {
2381789Sahrens 	spa_t *spa;
23825094Slling 	char *altroot = NULL;
23831635Sbonwick 	vdev_t *rvd;
2384789Sahrens 	dsl_pool_t *dp;
2385789Sahrens 	dmu_tx_t *tx;
23869816SGeorge.Wilson@Sun.COM 	int error = 0;
2387789Sahrens 	uint64_t txg = TXG_INITIAL;
23885450Sbrendan 	nvlist_t **spares, **l2cache;
23895450Sbrendan 	uint_t nspares, nl2cache;
23905094Slling 	uint64_t version;
2391789Sahrens 
2392789Sahrens 	/*
2393789Sahrens 	 * If this pool already exists, return failure.
2394789Sahrens 	 */
2395789Sahrens 	mutex_enter(&spa_namespace_lock);
2396789Sahrens 	if (spa_lookup(pool) != NULL) {
2397789Sahrens 		mutex_exit(&spa_namespace_lock);
2398789Sahrens 		return (EEXIST);
2399789Sahrens 	}
2400789Sahrens 
2401789Sahrens 	/*
2402789Sahrens 	 * Allocate a new spa_t structure.
2403789Sahrens 	 */
24045094Slling 	(void) nvlist_lookup_string(props,
24055094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
240610921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
24078241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2408789Sahrens 
24095094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
24105094Slling 		spa_deactivate(spa);
24115094Slling 		spa_remove(spa);
24126643Seschrock 		mutex_exit(&spa_namespace_lock);
24135094Slling 		return (error);
24145094Slling 	}
24155094Slling 
24165094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
24175094Slling 	    &version) != 0)
24185094Slling 		version = SPA_VERSION;
24195094Slling 	ASSERT(version <= SPA_VERSION);
2420*10922SJeff.Bonwick@Sun.COM 
2421*10922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
2422*10922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
24235094Slling 	spa->spa_uberblock.ub_version = version;
2424789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2425789Sahrens 
24261635Sbonwick 	/*
24279234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
24289234SGeorge.Wilson@Sun.COM 	 */
24299630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
24309630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
24319234SGeorge.Wilson@Sun.COM 
24329234SGeorge.Wilson@Sun.COM 	/*
24331635Sbonwick 	 * Create the root vdev.
24341635Sbonwick 	 */
24357754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
24361635Sbonwick 
24372082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
24382082Seschrock 
24392082Seschrock 	ASSERT(error != 0 || rvd != NULL);
24402082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
24412082Seschrock 
24425913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
24431635Sbonwick 		error = EINVAL;
24442082Seschrock 
24452082Seschrock 	if (error == 0 &&
24462082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
24475450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
24482082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
24499816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
24509816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
24519816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
24529816SGeorge.Wilson@Sun.COM 		}
24531635Sbonwick 	}
24541635Sbonwick 
24557754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2456789Sahrens 
24572082Seschrock 	if (error != 0) {
2458789Sahrens 		spa_unload(spa);
2459789Sahrens 		spa_deactivate(spa);
2460789Sahrens 		spa_remove(spa);
2461789Sahrens 		mutex_exit(&spa_namespace_lock);
2462789Sahrens 		return (error);
2463789Sahrens 	}
2464789Sahrens 
24652082Seschrock 	/*
24662082Seschrock 	 * Get the list of spares, if specified.
24672082Seschrock 	 */
24682082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
24692082Seschrock 	    &spares, &nspares) == 0) {
24705450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
24712082Seschrock 		    KM_SLEEP) == 0);
24725450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
24732082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
24747754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
24752082Seschrock 		spa_load_spares(spa);
24767754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
24775450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
24785450Sbrendan 	}
24795450Sbrendan 
24805450Sbrendan 	/*
24815450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
24825450Sbrendan 	 */
24835450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
24845450Sbrendan 	    &l2cache, &nl2cache) == 0) {
24855450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
24865450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24875450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
24885450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
24897754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
24905450Sbrendan 		spa_load_l2cache(spa);
24917754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
24925450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
24932082Seschrock 	}
24942082Seschrock 
24957184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2496789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2497789Sahrens 
2498789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2499789Sahrens 
2500789Sahrens 	/*
2501789Sahrens 	 * Create the pool config object.
2502789Sahrens 	 */
2503789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
25047497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2505789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2506789Sahrens 
25071544Seschrock 	if (zap_add(spa->spa_meta_objset,
2508789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
25091544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
25101544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
25111544Seschrock 	}
2512789Sahrens 
25135094Slling 	/* Newly created pools with the right version are always deflated. */
25145094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
25155094Slling 		spa->spa_deflate = TRUE;
25165094Slling 		if (zap_add(spa->spa_meta_objset,
25175094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
25185094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
25195094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
25205094Slling 		}
25212082Seschrock 	}
25222082Seschrock 
2523789Sahrens 	/*
2524789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2525789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2526789Sahrens 	 * keeps changing.
2527789Sahrens 	 */
2528*10922SJeff.Bonwick@Sun.COM 	spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset,
2529789Sahrens 	    1 << 14, tx);
2530*10922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset,
2531*10922SJeff.Bonwick@Sun.COM 	    spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx);
2532789Sahrens 
25331544Seschrock 	if (zap_add(spa->spa_meta_objset,
2534789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
2535*10922SJeff.Bonwick@Sun.COM 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) {
25361544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
25371544Seschrock 	}
2538789Sahrens 
25392926Sek110237 	/*
25402926Sek110237 	 * Create the pool's history object.
25412926Sek110237 	 */
25425094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
25435094Slling 		spa_history_create_obj(spa, tx);
25445094Slling 
25455094Slling 	/*
25465094Slling 	 * Set pool properties.
25475094Slling 	 */
25485094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
25495094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
25505329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
25519816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
2552*10922SJeff.Bonwick@Sun.COM 
25538525SEric.Schrock@Sun.COM 	if (props != NULL) {
25548525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
25555094Slling 		spa_sync_props(spa, props, CRED(), tx);
25568525SEric.Schrock@Sun.COM 	}
25572926Sek110237 
2558*10922SJeff.Bonwick@Sun.COM 	/*
2559*10922SJeff.Bonwick@Sun.COM 	 * Create DDTs (dedup tables).
2560*10922SJeff.Bonwick@Sun.COM 	 */
2561*10922SJeff.Bonwick@Sun.COM 	ddt_create(spa);
2562*10922SJeff.Bonwick@Sun.COM 
2563789Sahrens 	dmu_tx_commit(tx);
2564789Sahrens 
2565789Sahrens 	spa->spa_sync_on = B_TRUE;
2566789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2567789Sahrens 
2568789Sahrens 	/*
2569789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2570789Sahrens 	 * bean counters are appropriately updated.
2571789Sahrens 	 */
2572789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2573789Sahrens 
25746643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2575789Sahrens 
25765094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
25774715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
25789946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
25794715Sek110237 
25808667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
25818667SGeorge.Wilson@Sun.COM 
2582789Sahrens 	mutex_exit(&spa_namespace_lock);
2583789Sahrens 
2584789Sahrens 	return (0);
2585789Sahrens }
2586789Sahrens 
25876423Sgw25295 #ifdef _KERNEL
25886423Sgw25295 /*
25899790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
25909790SLin.Ling@Sun.COM  * during the system boot up time.
25916423Sgw25295  */
25929790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
25939790SLin.Ling@Sun.COM 
25949790SLin.Ling@Sun.COM static nvlist_t *
25959790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
25966423Sgw25295 {
25979790SLin.Ling@Sun.COM 	nvlist_t *config;
25986423Sgw25295 	nvlist_t *nvtop, *nvroot;
25996423Sgw25295 	uint64_t pgid;
26006423Sgw25295 
26019790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
26029790SLin.Ling@Sun.COM 		return (NULL);
26039790SLin.Ling@Sun.COM 
26046423Sgw25295 	/*
26056423Sgw25295 	 * Add this top-level vdev to the child array.
26066423Sgw25295 	 */
26079790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
26089790SLin.Ling@Sun.COM 	    &nvtop) == 0);
26099790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
26109790SLin.Ling@Sun.COM 	    &pgid) == 0);
26119790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
26126423Sgw25295 
26136423Sgw25295 	/*
26146423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
26156423Sgw25295 	 */
26166423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
26179790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
26189790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
26196423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
26206423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
26216423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
26226423Sgw25295 	    &nvtop, 1) == 0);
26236423Sgw25295 
26246423Sgw25295 	/*
26256423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
26266423Sgw25295 	 * this pool's configuration (remove the old, add the new).
26276423Sgw25295 	 */
26286423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
26296423Sgw25295 	nvlist_free(nvroot);
26309790SLin.Ling@Sun.COM 	return (config);
26316423Sgw25295 }
26326423Sgw25295 
26336423Sgw25295 /*
26349790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
26359790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
26369790SLin.Ling@Sun.COM  * device has a more recent txg.
26376423Sgw25295  */
26389790SLin.Ling@Sun.COM static void
26399790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
26407147Staylor {
26419816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
26429790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
26439790SLin.Ling@Sun.COM 
26449790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
26459790SLin.Ling@Sun.COM 		nvlist_t *label;
26469790SLin.Ling@Sun.COM 		uint64_t label_txg;
26479790SLin.Ling@Sun.COM 
26489790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
26499790SLin.Ling@Sun.COM 		    &label) != 0)
26509790SLin.Ling@Sun.COM 			return;
26519790SLin.Ling@Sun.COM 
26529790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
26539790SLin.Ling@Sun.COM 		    &label_txg) == 0);
26549790SLin.Ling@Sun.COM 
26559790SLin.Ling@Sun.COM 		/*
26569790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
26579790SLin.Ling@Sun.COM 		 */
26589790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
26599790SLin.Ling@Sun.COM 			*txg = label_txg;
26609790SLin.Ling@Sun.COM 			*avd = vd;
26617147Staylor 		}
26629790SLin.Ling@Sun.COM 		nvlist_free(label);
26637147Staylor 	}
26647147Staylor }
26657147Staylor 
26666423Sgw25295 /*
26676423Sgw25295  * Import a root pool.
26686423Sgw25295  *
26697147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
26707147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
26717147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
26726423Sgw25295  *
26736423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
26746423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
26756423Sgw25295  * e.g.
26766423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
26776423Sgw25295  */
26786423Sgw25295 int
26797147Staylor spa_import_rootpool(char *devpath, char *devid)
26806423Sgw25295 {
26819790SLin.Ling@Sun.COM 	spa_t *spa;
26829790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
26839790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
26849790SLin.Ling@Sun.COM 	uint64_t guid, txg;
26856423Sgw25295 	char *pname;
26866423Sgw25295 	int error;
26876423Sgw25295 
26886423Sgw25295 	/*
26899790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
26906423Sgw25295 	 */
269110822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
269210822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
269310822SJack.Meng@Sun.COM 	if (config == NULL) {
269410822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
269510822SJack.Meng@Sun.COM 			/* iscsi boot */
269610822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
269710822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
269810822SJack.Meng@Sun.COM 		}
269910822SJack.Meng@Sun.COM 	}
270010822SJack.Meng@Sun.COM #endif
270110822SJack.Meng@Sun.COM 	if (config == NULL) {
27029790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
27039790SLin.Ling@Sun.COM 		    devpath);
27049790SLin.Ling@Sun.COM 		return (EIO);
27059790SLin.Ling@Sun.COM 	}
27069790SLin.Ling@Sun.COM 
27079790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
27089790SLin.Ling@Sun.COM 	    &pname) == 0);
27099790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
27106423Sgw25295 
27119425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
27129425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
27139425SEric.Schrock@Sun.COM 		/*
27149425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
27159425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
27169425SEric.Schrock@Sun.COM 		 */
27179425SEric.Schrock@Sun.COM 		spa_remove(spa);
27189425SEric.Schrock@Sun.COM 	}
27199425SEric.Schrock@Sun.COM 
272010921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
27219425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
272210100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
27239790SLin.Ling@Sun.COM 
27249790SLin.Ling@Sun.COM 	/*
27259790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
27269790SLin.Ling@Sun.COM 	 */
27279790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
27289790SLin.Ling@Sun.COM 	    &nvtop) == 0);
27299790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27309790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
27319790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
27329790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
27339790SLin.Ling@Sun.COM 	if (error) {
27349790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
27359790SLin.Ling@Sun.COM 		nvlist_free(config);
27369790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
27379790SLin.Ling@Sun.COM 		    pname);
27389790SLin.Ling@Sun.COM 		return (error);
27399790SLin.Ling@Sun.COM 	}
27409790SLin.Ling@Sun.COM 
27419790SLin.Ling@Sun.COM 	/*
27429790SLin.Ling@Sun.COM 	 * Get the boot vdev.
27439790SLin.Ling@Sun.COM 	 */
27449790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
27459790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
27469790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
27479790SLin.Ling@Sun.COM 		error = ENOENT;
27489790SLin.Ling@Sun.COM 		goto out;
27499790SLin.Ling@Sun.COM 	}
27509790SLin.Ling@Sun.COM 
27519790SLin.Ling@Sun.COM 	/*
27529790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
27539790SLin.Ling@Sun.COM 	 */
27549790SLin.Ling@Sun.COM 	avd = bvd;
27559790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
27569790SLin.Ling@Sun.COM 	if (avd != bvd) {
27579790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
27589790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
27599790SLin.Ling@Sun.COM 		error = EINVAL;
27609790SLin.Ling@Sun.COM 		goto out;
27619790SLin.Ling@Sun.COM 	}
27629790SLin.Ling@Sun.COM 
27639790SLin.Ling@Sun.COM 	/*
27649790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
27659790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
27669790SLin.Ling@Sun.COM 	 */
27679790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
27689790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
27699790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
27709790SLin.Ling@Sun.COM 		    "try booting from '%s'",
27719790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
27729790SLin.Ling@Sun.COM 		error = EINVAL;
27739790SLin.Ling@Sun.COM 		goto out;
27749790SLin.Ling@Sun.COM 	}
27759790SLin.Ling@Sun.COM 
27769790SLin.Ling@Sun.COM 	VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
27779790SLin.Ling@Sun.COM 	error = 0;
27789946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
27799790SLin.Ling@Sun.COM out:
27809790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27819790SLin.Ling@Sun.COM 	vdev_free(rvd);
27829790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
27839425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
27846423Sgw25295 
27859790SLin.Ling@Sun.COM 	nvlist_free(config);
27866423Sgw25295 	return (error);
27876423Sgw25295 }
27889790SLin.Ling@Sun.COM 
27896423Sgw25295 #endif
27906423Sgw25295 
27916423Sgw25295 /*
27929425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
27939425SEric.Schrock@Sun.COM  * boot.
27949425SEric.Schrock@Sun.COM  */
27959425SEric.Schrock@Sun.COM int
27969425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
27979425SEric.Schrock@Sun.COM {
27989425SEric.Schrock@Sun.COM 	spa_t *spa;
279910921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
28009425SEric.Schrock@Sun.COM 	char *altroot = NULL;
28019425SEric.Schrock@Sun.COM 
28029425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
28039425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
28049425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
28059425SEric.Schrock@Sun.COM 		return (EEXIST);
28069425SEric.Schrock@Sun.COM 	}
28079425SEric.Schrock@Sun.COM 
28089425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
28099425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
281010921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
281110921STim.Haley@Sun.COM 
281210921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
281310921STim.Haley@Sun.COM 	spa->spa_load_max_txg = policy.zrp_txg;
28149425SEric.Schrock@Sun.COM 
281510100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
281610000SVictor.Latushkin@Sun.COM 
28179425SEric.Schrock@Sun.COM 	if (props != NULL)
28189425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
28199425SEric.Schrock@Sun.COM 
28209425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
28219425SEric.Schrock@Sun.COM 
28229425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
28239946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
28249425SEric.Schrock@Sun.COM 
28259425SEric.Schrock@Sun.COM 	return (0);
28269425SEric.Schrock@Sun.COM }
28279425SEric.Schrock@Sun.COM 
28289425SEric.Schrock@Sun.COM /*
28296423Sgw25295  * Import a non-root pool into the system.
28306423Sgw25295  */
28316423Sgw25295 int
28326423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
28336423Sgw25295 {
28349425SEric.Schrock@Sun.COM 	spa_t *spa;
28359425SEric.Schrock@Sun.COM 	char *altroot = NULL;
283610921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
283710921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
28389425SEric.Schrock@Sun.COM 	int error;
28399425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
28409425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
28419425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
28429425SEric.Schrock@Sun.COM 
28439425SEric.Schrock@Sun.COM 	/*
28449425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
28459425SEric.Schrock@Sun.COM 	 */
28469425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
28479425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pool)) != NULL) {
28489425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
28499425SEric.Schrock@Sun.COM 		return (EEXIST);
28509425SEric.Schrock@Sun.COM 	}
28519425SEric.Schrock@Sun.COM 
285210921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
285310921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
285410921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
285510921STim.Haley@Sun.COM 
28569425SEric.Schrock@Sun.COM 	/*
28579425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
28589425SEric.Schrock@Sun.COM 	 */
28599425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
28609425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
286110921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
28629425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
28639425SEric.Schrock@Sun.COM 
28649425SEric.Schrock@Sun.COM 	/*
28659630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
28669630SJeff.Bonwick@Sun.COM 	 */
28679630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
28689630SJeff.Bonwick@Sun.COM 
28699630SJeff.Bonwick@Sun.COM 	/*
28709425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
28719425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
28729425SEric.Schrock@Sun.COM 	 * doing an import.
28739425SEric.Schrock@Sun.COM 	 */
287410921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
287510921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
287610921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
287710921STim.Haley@Sun.COM 	    ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0));
287810921STim.Haley@Sun.COM 
287910921STim.Haley@Sun.COM 	/*
288010921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
288110921STim.Haley@Sun.COM 	 * back to caller
288210921STim.Haley@Sun.COM 	 */
288310921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
28849425SEric.Schrock@Sun.COM 
28859425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28869425SEric.Schrock@Sun.COM 	/*
28879425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
28889425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
28899425SEric.Schrock@Sun.COM 	 */
28909425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
28919425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
28929425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
28939425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
28949425SEric.Schrock@Sun.COM 	}
28959425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
28969425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
28979425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
28989425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
28999425SEric.Schrock@Sun.COM 	}
29009425SEric.Schrock@Sun.COM 
29019425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29029425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
29039425SEric.Schrock@Sun.COM 	if (error == 0)
29049425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
29059425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
29069425SEric.Schrock@Sun.COM 	if (error == 0)
29079425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
29089425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
29099425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
29109425SEric.Schrock@Sun.COM 
29119425SEric.Schrock@Sun.COM 	if (props != NULL)
29129425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
29139425SEric.Schrock@Sun.COM 
29149425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
29159425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
29169425SEric.Schrock@Sun.COM 		spa_unload(spa);
29179425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
29189425SEric.Schrock@Sun.COM 		spa_remove(spa);
29199425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
29209425SEric.Schrock@Sun.COM 		return (error);
29219425SEric.Schrock@Sun.COM 	}
29229425SEric.Schrock@Sun.COM 
29239630SJeff.Bonwick@Sun.COM 	spa_async_resume(spa);
29249630SJeff.Bonwick@Sun.COM 
29259425SEric.Schrock@Sun.COM 	/*
29269425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
29279425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
29289425SEric.Schrock@Sun.COM 	 */
29299425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
29309425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
29319425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
29329425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
29339425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
29349425SEric.Schrock@Sun.COM 		else
29359425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
29369425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
29379425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
29389425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
29399425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
29409425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
29419425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
29429425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
29439425SEric.Schrock@Sun.COM 	}
29449425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
29459425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
29469425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
29479425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
29489425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
29499425SEric.Schrock@Sun.COM 		else
29509425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
29519425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
29529425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
29539425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
29549425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
29559425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
29569425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
29579425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
29589425SEric.Schrock@Sun.COM 	}
29599425SEric.Schrock@Sun.COM 
296010672SEric.Schrock@Sun.COM 	/*
296110672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
296210672SEric.Schrock@Sun.COM 	 */
296310672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
296410672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
296510672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
296610672SEric.Schrock@Sun.COM 	}
296710672SEric.Schrock@Sun.COM 
29689425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
29699425SEric.Schrock@Sun.COM 		/*
29709425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
29719425SEric.Schrock@Sun.COM 		 */
297210100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
29739425SEric.Schrock@Sun.COM 	}
29749425SEric.Schrock@Sun.COM 
29759816SGeorge.Wilson@Sun.COM 	/*
29769816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
29779816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
29789816SGeorge.Wilson@Sun.COM 	 */
29799816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
29809816SGeorge.Wilson@Sun.COM 
29819425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
29829946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
29839425SEric.Schrock@Sun.COM 
29849425SEric.Schrock@Sun.COM 	return (0);
29856643Seschrock }
29866643Seschrock 
29876643Seschrock 
2988789Sahrens /*
2989789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2990789Sahrens  * to get the vdev stats associated with the imported devices.
2991789Sahrens  */
2992789Sahrens #define	TRYIMPORT_NAME	"$import"
2993789Sahrens 
2994789Sahrens nvlist_t *
2995789Sahrens spa_tryimport(nvlist_t *tryconfig)
2996789Sahrens {
2997789Sahrens 	nvlist_t *config = NULL;
2998789Sahrens 	char *poolname;
2999789Sahrens 	spa_t *spa;
3000789Sahrens 	uint64_t state;
30018680SLin.Ling@Sun.COM 	int error;
3002789Sahrens 
3003789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3004789Sahrens 		return (NULL);
3005789Sahrens 
3006789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3007789Sahrens 		return (NULL);
3008789Sahrens 
30091635Sbonwick 	/*
30101635Sbonwick 	 * Create and initialize the spa structure.
30111635Sbonwick 	 */
3012789Sahrens 	mutex_enter(&spa_namespace_lock);
301310921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
30148241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3015789Sahrens 
3016789Sahrens 	/*
30171635Sbonwick 	 * Pass off the heavy lifting to spa_load().
30181732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
30191732Sbonwick 	 * is actually the one to trust when doing an import.
3020789Sahrens 	 */
302110921STim.Haley@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE);
3022789Sahrens 
3023789Sahrens 	/*
3024789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3025789Sahrens 	 */
3026789Sahrens 	if (spa->spa_root_vdev != NULL) {
3027789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3028789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3029789Sahrens 		    poolname) == 0);
3030789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3031789Sahrens 		    state) == 0);
30323975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
30333975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
30342082Seschrock 
30352082Seschrock 		/*
30366423Sgw25295 		 * If the bootfs property exists on this pool then we
30376423Sgw25295 		 * copy it out so that external consumers can tell which
30386423Sgw25295 		 * pools are bootable.
30396423Sgw25295 		 */
30408680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
30416423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
30426423Sgw25295 
30436423Sgw25295 			/*
30446423Sgw25295 			 * We have to play games with the name since the
30456423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
30466423Sgw25295 			 */
30477754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
30486423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
30496423Sgw25295 				char *cp;
30506423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
30516423Sgw25295 
30526423Sgw25295 				cp = strchr(tmpname, '/');
30536423Sgw25295 				if (cp == NULL) {
30546423Sgw25295 					(void) strlcpy(dsname, tmpname,
30556423Sgw25295 					    MAXPATHLEN);
30566423Sgw25295 				} else {
30576423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
30586423Sgw25295 					    "%s/%s", poolname, ++cp);
30596423Sgw25295 				}
30606423Sgw25295 				VERIFY(nvlist_add_string(config,
30616423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
30626423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
30636423Sgw25295 			}
30646423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
30656423Sgw25295 		}
30666423Sgw25295 
30676423Sgw25295 		/*
30685450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
30692082Seschrock 		 */
30709425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
30712082Seschrock 		spa_add_spares(spa, config);
30725450Sbrendan 		spa_add_l2cache(spa, config);
30739425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3074789Sahrens 	}
3075789Sahrens 
3076789Sahrens 	spa_unload(spa);
3077789Sahrens 	spa_deactivate(spa);
3078789Sahrens 	spa_remove(spa);
3079789Sahrens 	mutex_exit(&spa_namespace_lock);
3080789Sahrens 
3081789Sahrens 	return (config);
3082789Sahrens }
3083789Sahrens 
3084789Sahrens /*
3085789Sahrens  * Pool export/destroy
3086789Sahrens  *
3087789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3088789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3089789Sahrens  * update the pool state and sync all the labels to disk, removing the
30908211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
30918211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3092789Sahrens  */
3093789Sahrens static int
30947214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
30958211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3096789Sahrens {
3097789Sahrens 	spa_t *spa;
3098789Sahrens 
30991775Sbillm 	if (oldconfig)
31001775Sbillm 		*oldconfig = NULL;
31011775Sbillm 
31028241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3103789Sahrens 		return (EROFS);
3104789Sahrens 
3105789Sahrens 	mutex_enter(&spa_namespace_lock);
3106789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3107789Sahrens 		mutex_exit(&spa_namespace_lock);
3108789Sahrens 		return (ENOENT);
3109789Sahrens 	}
3110789Sahrens 
3111789Sahrens 	/*
31121544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
31131544Seschrock 	 * reacquire the namespace lock, and see if we can export.
31141544Seschrock 	 */
31151544Seschrock 	spa_open_ref(spa, FTAG);
31161544Seschrock 	mutex_exit(&spa_namespace_lock);
31171544Seschrock 	spa_async_suspend(spa);
31181544Seschrock 	mutex_enter(&spa_namespace_lock);
31191544Seschrock 	spa_close(spa, FTAG);
31201544Seschrock 
31211544Seschrock 	/*
3122789Sahrens 	 * The pool will be in core if it's openable,
3123789Sahrens 	 * in which case we can modify its state.
3124789Sahrens 	 */
3125789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3126789Sahrens 		/*
3127789Sahrens 		 * Objsets may be open only because they're dirty, so we
3128789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3129789Sahrens 		 */
3130789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3131789Sahrens 
31321544Seschrock 		/*
31331544Seschrock 		 * A pool cannot be exported or destroyed if there are active
31341544Seschrock 		 * references.  If we are resetting a pool, allow references by
31351544Seschrock 		 * fault injection handlers.
31361544Seschrock 		 */
31371544Seschrock 		if (!spa_refcount_zero(spa) ||
31381544Seschrock 		    (spa->spa_inject_ref != 0 &&
31391544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
31401544Seschrock 			spa_async_resume(spa);
3141789Sahrens 			mutex_exit(&spa_namespace_lock);
3142789Sahrens 			return (EBUSY);
3143789Sahrens 		}
3144789Sahrens 
3145789Sahrens 		/*
31467214Slling 		 * A pool cannot be exported if it has an active shared spare.
31477214Slling 		 * This is to prevent other pools stealing the active spare
31487214Slling 		 * from an exported pool. At user's own will, such pool can
31497214Slling 		 * be forcedly exported.
31507214Slling 		 */
31517214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
31527214Slling 		    spa_has_active_shared_spare(spa)) {
31537214Slling 			spa_async_resume(spa);
31547214Slling 			mutex_exit(&spa_namespace_lock);
31557214Slling 			return (EXDEV);
31567214Slling 		}
31577214Slling 
31587214Slling 		/*
3159789Sahrens 		 * We want this to be reflected on every label,
3160789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3161789Sahrens 		 * final sync that pushes these changes out.
3162789Sahrens 		 */
31638211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
31647754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31651544Seschrock 			spa->spa_state = new_state;
31661635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
31671544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
31687754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
31691544Seschrock 		}
3170789Sahrens 	}
3171789Sahrens 
31724451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
31734451Seschrock 
3174789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3175789Sahrens 		spa_unload(spa);
3176789Sahrens 		spa_deactivate(spa);
3177789Sahrens 	}
3178789Sahrens 
31791775Sbillm 	if (oldconfig && spa->spa_config)
31801775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
31811775Sbillm 
31821544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
31838211SGeorge.Wilson@Sun.COM 		if (!hardforce)
31848211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
31851544Seschrock 		spa_remove(spa);
31861544Seschrock 	}
3187789Sahrens 	mutex_exit(&spa_namespace_lock);
3188789Sahrens 
3189789Sahrens 	return (0);
3190789Sahrens }
3191789Sahrens 
3192789Sahrens /*
3193789Sahrens  * Destroy a storage pool.
3194789Sahrens  */
3195789Sahrens int
3196789Sahrens spa_destroy(char *pool)
3197789Sahrens {
31988211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
31998211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3200789Sahrens }
3201789Sahrens 
3202789Sahrens /*
3203789Sahrens  * Export a storage pool.
3204789Sahrens  */
3205789Sahrens int
32068211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
32078211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3208789Sahrens {
32098211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
32108211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3211789Sahrens }
3212789Sahrens 
3213789Sahrens /*
32141544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
32151544Seschrock  * from the namespace in any way.
32161544Seschrock  */
32171544Seschrock int
32181544Seschrock spa_reset(char *pool)
32191544Seschrock {
32207214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
32218211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
32221544Seschrock }
32231544Seschrock 
32241544Seschrock /*
3225789Sahrens  * ==========================================================================
3226789Sahrens  * Device manipulation
3227789Sahrens  * ==========================================================================
3228789Sahrens  */
3229789Sahrens 
3230789Sahrens /*
32314527Sperrin  * Add a device to a storage pool.
3232789Sahrens  */
3233789Sahrens int
3234789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3235789Sahrens {
323610594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
32378241SJeff.Bonwick@Sun.COM 	int error;
3238789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
32391585Sbonwick 	vdev_t *vd, *tvd;
32405450Sbrendan 	nvlist_t **spares, **l2cache;
32415450Sbrendan 	uint_t nspares, nl2cache;
3242789Sahrens 
3243789Sahrens 	txg = spa_vdev_enter(spa);
3244789Sahrens 
32452082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
32462082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
32472082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
32482082Seschrock 
32497754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3250789Sahrens 
32515450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
32525450Sbrendan 	    &nspares) != 0)
32532082Seschrock 		nspares = 0;
32542082Seschrock 
32555450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
32565450Sbrendan 	    &nl2cache) != 0)
32575450Sbrendan 		nl2cache = 0;
32585450Sbrendan 
32597754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
32602082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
32617754SJeff.Bonwick@Sun.COM 
32627754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
32637754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
32647754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
32652082Seschrock 
32663377Seschrock 	/*
32675450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
32685450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
32693377Seschrock 	 */
32707754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
32713377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
32723377Seschrock 
32733377Seschrock 	/*
32743377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
32753377Seschrock 	 */
32768241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
327710594SGeorge.Wilson@Sun.COM 
327810594SGeorge.Wilson@Sun.COM 		/*
327910594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
328010594SGeorge.Wilson@Sun.COM 		 */
328110594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
328210594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
328310594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
328410594SGeorge.Wilson@Sun.COM 				break;
328510594SGeorge.Wilson@Sun.COM 			}
328610594SGeorge.Wilson@Sun.COM 		}
32873377Seschrock 		tvd = vd->vdev_child[c];
32883377Seschrock 		vdev_remove_child(vd, tvd);
328910594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
32903377Seschrock 		vdev_add_child(rvd, tvd);
32913377Seschrock 		vdev_config_dirty(tvd);
32923377Seschrock 	}
32933377Seschrock 
32942082Seschrock 	if (nspares != 0) {
32955450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
32965450Sbrendan 		    ZPOOL_CONFIG_SPARES);
32972082Seschrock 		spa_load_spares(spa);
32985450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
32995450Sbrendan 	}
33005450Sbrendan 
33015450Sbrendan 	if (nl2cache != 0) {
33025450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
33035450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
33045450Sbrendan 		spa_load_l2cache(spa);
33055450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3306789Sahrens 	}
3307789Sahrens 
3308789Sahrens 	/*
33091585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
33101585Sbonwick 	 * If other threads start allocating from these vdevs before we
33111585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
33121585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
33131585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
33141585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
33151635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
33161585Sbonwick 	 *
33171585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
33181585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
33191585Sbonwick 	 * steps will be completed the next time we load the pool.
3320789Sahrens 	 */
33211635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
33221585Sbonwick 
33231635Sbonwick 	mutex_enter(&spa_namespace_lock);
33241635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
33251635Sbonwick 	mutex_exit(&spa_namespace_lock);
3326789Sahrens 
33271635Sbonwick 	return (0);
3328789Sahrens }
3329789Sahrens 
3330789Sahrens /*
3331789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3332789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3333789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3334789Sahrens  *
3335789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3336789Sahrens  * existing device; in this case the two devices are made into their own
33374451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3338789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3339789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3340789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3341789Sahrens  * is automatically detached.
3342789Sahrens  */
3343789Sahrens int
33441544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3345789Sahrens {
3346789Sahrens 	uint64_t txg, open_txg;
3347789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3348789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
33492082Seschrock 	vdev_ops_t *pvops;
33507313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
33517313SEric.Kustarz@Sun.COM 	int newvd_isspare;
33527313SEric.Kustarz@Sun.COM 	int error;
3353789Sahrens 
3354789Sahrens 	txg = spa_vdev_enter(spa);
3355789Sahrens 
33566643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3357789Sahrens 
3358789Sahrens 	if (oldvd == NULL)
3359789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3360789Sahrens 
33611585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
33621585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
33631585Sbonwick 
3364789Sahrens 	pvd = oldvd->vdev_parent;
3365789Sahrens 
33662082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
33674451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
33684451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
33694451Seschrock 
33704451Seschrock 	if (newrootvd->vdev_children != 1)
3371789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3372789Sahrens 
3373789Sahrens 	newvd = newrootvd->vdev_child[0];
3374789Sahrens 
3375789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3376789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3377789Sahrens 
33782082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3379789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3380789Sahrens 
33814527Sperrin 	/*
33824527Sperrin 	 * Spares can't replace logs
33834527Sperrin 	 */
33847326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
33854527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33864527Sperrin 
33872082Seschrock 	if (!replacing) {
33882082Seschrock 		/*
33892082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
33902082Seschrock 		 * vdev.
33912082Seschrock 		 */
33922082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
33932082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
33942082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33952082Seschrock 
33962082Seschrock 		pvops = &vdev_mirror_ops;
33972082Seschrock 	} else {
33982082Seschrock 		/*
33992082Seschrock 		 * Active hot spares can only be replaced by inactive hot
34002082Seschrock 		 * spares.
34012082Seschrock 		 */
34022082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
34032082Seschrock 		    pvd->vdev_child[1] == oldvd &&
34042082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
34052082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
34062082Seschrock 
34072082Seschrock 		/*
34082082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
34092082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
34103377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
34113377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
34123377Seschrock 		 * the same (spare replaces spare, non-spare replaces
34133377Seschrock 		 * non-spare).
34142082Seschrock 		 */
34152082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
34162082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
34173377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
34183377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
34193377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
34202082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
34212082Seschrock 		    newvd->vdev_isspare)
34222082Seschrock 			pvops = &vdev_spare_ops;
34232082Seschrock 		else
34242082Seschrock 			pvops = &vdev_replacing_ops;
34252082Seschrock 	}
34262082Seschrock 
34271175Slling 	/*
34289816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
34291175Slling 	 */
34309816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3431789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3432789Sahrens 
34331732Sbonwick 	/*
34341732Sbonwick 	 * The new device cannot have a higher alignment requirement
34351732Sbonwick 	 * than the top-level vdev.
34361732Sbonwick 	 */
34371732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3438789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3439789Sahrens 
3440789Sahrens 	/*
3441789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3442789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3443789Sahrens 	 */
3444789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3445789Sahrens 		spa_strfree(oldvd->vdev_path);
3446789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3447789Sahrens 		    KM_SLEEP);
3448789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3449789Sahrens 		    newvd->vdev_path, "old");
3450789Sahrens 		if (oldvd->vdev_devid != NULL) {
3451789Sahrens 			spa_strfree(oldvd->vdev_devid);
3452789Sahrens 			oldvd->vdev_devid = NULL;
3453789Sahrens 		}
3454789Sahrens 	}
3455789Sahrens 
3456789Sahrens 	/*
34572082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
34582082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3459789Sahrens 	 */
3460789Sahrens 	if (pvd->vdev_ops != pvops)
3461789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3462789Sahrens 
3463789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3464789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3465789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3466789Sahrens 
3467789Sahrens 	/*
3468789Sahrens 	 * Extract the new device from its root and add it to pvd.
3469789Sahrens 	 */
3470789Sahrens 	vdev_remove_child(newrootvd, newvd);
3471789Sahrens 	newvd->vdev_id = pvd->vdev_children;
347210594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3473789Sahrens 	vdev_add_child(pvd, newvd);
3474789Sahrens 
3475789Sahrens 	tvd = newvd->vdev_top;
3476789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3477789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3478789Sahrens 
3479789Sahrens 	vdev_config_dirty(tvd);
3480789Sahrens 
3481789Sahrens 	/*
3482789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3483789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3484789Sahrens 	 */
3485789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3486789Sahrens 
34878241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
34888241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3489789Sahrens 
34909425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
34913377Seschrock 		spa_spare_activate(newvd);
34929425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
34939425SEric.Schrock@Sun.COM 	}
34949425SEric.Schrock@Sun.COM 
34957754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
34967754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
34977313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
34981544Seschrock 
3499789Sahrens 	/*
3500789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3501789Sahrens 	 */
35021732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3503789Sahrens 
3504789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3505789Sahrens 
35069946SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
35079946SMark.Musante@Sun.COM 	    CRED(),  "%s vdev=%s %s vdev=%s",
35089946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
35099946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
35109946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
35117313SEric.Kustarz@Sun.COM 
35127313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
35137313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
35147313SEric.Kustarz@Sun.COM 
3515789Sahrens 	/*
35167046Sahrens 	 * Kick off a resilver to update newvd.
3517789Sahrens 	 */
35187046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3519789Sahrens 
3520789Sahrens 	return (0);
3521789Sahrens }
3522789Sahrens 
3523789Sahrens /*
3524789Sahrens  * Detach a device from a mirror or replacing vdev.
3525789Sahrens  * If 'replace_done' is specified, only detach if the parent
3526789Sahrens  * is a replacing vdev.
3527789Sahrens  */
3528789Sahrens int
35298241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3530789Sahrens {
3531789Sahrens 	uint64_t txg;
35328241SJeff.Bonwick@Sun.COM 	int error;
3533789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3534789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
35352082Seschrock 	boolean_t unspare = B_FALSE;
35362082Seschrock 	uint64_t unspare_guid;
35376673Seschrock 	size_t len;
3538789Sahrens 
3539789Sahrens 	txg = spa_vdev_enter(spa);
3540789Sahrens 
35416643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3542789Sahrens 
3543789Sahrens 	if (vd == NULL)
3544789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3545789Sahrens 
35461585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
35471585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35481585Sbonwick 
3549789Sahrens 	pvd = vd->vdev_parent;
3550789Sahrens 
3551789Sahrens 	/*
35528241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
35538241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
35548241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
35558241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
35568241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
35578241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
35588241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
35598241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
35608241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
35618241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
35628241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
35638241SJeff.Bonwick@Sun.COM 	 */
35648241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
35658241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
35668241SJeff.Bonwick@Sun.COM 
35678241SJeff.Bonwick@Sun.COM 	/*
3568789Sahrens 	 * If replace_done is specified, only remove this device if it's
35692082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
35702082Seschrock 	 * disk can be removed.
3571789Sahrens 	 */
35722082Seschrock 	if (replace_done) {
35732082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
35742082Seschrock 			if (vd->vdev_id != 0)
35752082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35762082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
35772082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35782082Seschrock 		}
35792082Seschrock 	}
35802082Seschrock 
35812082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
35824577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3583789Sahrens 
3584789Sahrens 	/*
35852082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3586789Sahrens 	 */
3587789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
35882082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
35892082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3590789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3591789Sahrens 
3592789Sahrens 	/*
35938241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
35948241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3595789Sahrens 	 */
35968241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3597789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3598789Sahrens 
35998241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
36008241SJeff.Bonwick@Sun.COM 
3601789Sahrens 	/*
36026673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
36036673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
36046673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
36056673Seschrock 	 */
36066673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
36076673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
36086673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
36096673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
36106673Seschrock 		cvd = pvd->vdev_child[0];
36116673Seschrock 		len = strlen(vd->vdev_path);
36126673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
36136673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
36146673Seschrock 			spa_strfree(cvd->vdev_path);
36156673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
36166673Seschrock 		}
36176673Seschrock 	}
36186673Seschrock 
36196673Seschrock 	/*
36202082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
36212082Seschrock 	 * that the spare should become a real disk, and be removed from the
36222082Seschrock 	 * active spare list for the pool.
36232082Seschrock 	 */
36242082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
36258241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
36262082Seschrock 		unspare = B_TRUE;
36272082Seschrock 
36282082Seschrock 	/*
3629789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3630789Sahrens 	 * This must be done after all other error cases are handled,
3631789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3632789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3633789Sahrens 	 * it may be that the unwritability of the disk is the reason
3634789Sahrens 	 * it's being detached!
3635789Sahrens 	 */
36363377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3637789Sahrens 
3638789Sahrens 	/*
3639789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3640789Sahrens 	 */
3641789Sahrens 	vdev_remove_child(pvd, vd);
3642789Sahrens 	vdev_compact_children(pvd);
3643789Sahrens 
3644789Sahrens 	/*
3645789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3646789Sahrens 	 */
3647789Sahrens 	cvd = pvd->vdev_child[0];
3648789Sahrens 
3649789Sahrens 	/*
36502082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
36518241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
36528241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
36538241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
36548241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
36558241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
36568241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
36572082Seschrock 	 */
36582082Seschrock 	if (unspare) {
36592082Seschrock 		ASSERT(cvd->vdev_isspare);
36603377Seschrock 		spa_spare_remove(cvd);
36612082Seschrock 		unspare_guid = cvd->vdev_guid;
36628241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
36632082Seschrock 	}
36642082Seschrock 
36652082Seschrock 	/*
3666789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3667789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3668789Sahrens 	 */
3669789Sahrens 	if (pvd->vdev_children == 1)
3670789Sahrens 		vdev_remove_parent(cvd);
3671789Sahrens 
3672789Sahrens 	/*
3673789Sahrens 	 * We don't set tvd until now because the parent we just removed
3674789Sahrens 	 * may have been the previous top-level vdev.
3675789Sahrens 	 */
3676789Sahrens 	tvd = cvd->vdev_top;
3677789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3678789Sahrens 
3679789Sahrens 	/*
36803377Seschrock 	 * Reevaluate the parent vdev state.
3681789Sahrens 	 */
36824451Seschrock 	vdev_propagate_state(cvd);
3683789Sahrens 
3684789Sahrens 	/*
36859816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
36869816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
36879816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
36889816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
36899816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3690789Sahrens 	 */
36919816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
36929816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
36939816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
36949816SGeorge.Wilson@Sun.COM 	}
3695789Sahrens 
3696789Sahrens 	vdev_config_dirty(tvd);
3697789Sahrens 
3698789Sahrens 	/*
36993377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
37003377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
37013377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
37023377Seschrock 	 * prevent vd from being accessed after it's freed.
3703789Sahrens 	 */
37048241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3705789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
37061732Sbonwick 	vd->vdev_detached = B_TRUE;
37071732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3708789Sahrens 
37094451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
37104451Seschrock 
37112082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
37122082Seschrock 
37132082Seschrock 	/*
37143377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
37153377Seschrock 	 * then we want to go through and remove the device from the hot spare
37163377Seschrock 	 * list of every other pool.
37172082Seschrock 	 */
37182082Seschrock 	if (unspare) {
37198241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
37202082Seschrock 		spa = NULL;
37212082Seschrock 		mutex_enter(&spa_namespace_lock);
37222082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
37232082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
37242082Seschrock 				continue;
37258241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
37268241SJeff.Bonwick@Sun.COM 				continue;
37277793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
37287793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
37292082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
37307793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
37317793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
37322082Seschrock 		}
37332082Seschrock 		mutex_exit(&spa_namespace_lock);
37342082Seschrock 	}
37352082Seschrock 
37362082Seschrock 	return (error);
37372082Seschrock }
37382082Seschrock 
37397754SJeff.Bonwick@Sun.COM static nvlist_t *
37407754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
37412082Seschrock {
37427754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
37437754SJeff.Bonwick@Sun.COM 		uint64_t guid;
37447754SJeff.Bonwick@Sun.COM 
37457754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
37467754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
37477754SJeff.Bonwick@Sun.COM 
37487754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
37497754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
37502082Seschrock 	}
37512082Seschrock 
37527754SJeff.Bonwick@Sun.COM 	return (NULL);
37535450Sbrendan }
37545450Sbrendan 
37557754SJeff.Bonwick@Sun.COM static void
37567754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
37577754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
37585450Sbrendan {
37597754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
37607754SJeff.Bonwick@Sun.COM 
37617754SJeff.Bonwick@Sun.COM 	if (count > 1)
37627754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
37637754SJeff.Bonwick@Sun.COM 
37647754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
37657754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
37667754SJeff.Bonwick@Sun.COM 			continue;
37677754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
37685450Sbrendan 	}
37695450Sbrendan 
37707754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
37717754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
37727754SJeff.Bonwick@Sun.COM 
37737754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
37747754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
37757754SJeff.Bonwick@Sun.COM 
37767754SJeff.Bonwick@Sun.COM 	if (count > 1)
37777754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
37785450Sbrendan }
37795450Sbrendan 
37805450Sbrendan /*
378110594SGeorge.Wilson@Sun.COM  * Removing a device from the vdev namespace requires several steps
378210594SGeorge.Wilson@Sun.COM  * and can take a significant amount of time.  As a result we use
378310594SGeorge.Wilson@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
378410594SGeorge.Wilson@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
378510594SGeorge.Wilson@Sun.COM  * lock.  During each step the configuration is synced out.
378610594SGeorge.Wilson@Sun.COM  */
378710594SGeorge.Wilson@Sun.COM 
378810594SGeorge.Wilson@Sun.COM /*
378910594SGeorge.Wilson@Sun.COM  * Initial phase of device removal - stop future allocations from this device.
379010594SGeorge.Wilson@Sun.COM  */
379110594SGeorge.Wilson@Sun.COM void
379210594SGeorge.Wilson@Sun.COM spa_vdev_remove_start(spa_t *spa, vdev_t *vd)
379310594SGeorge.Wilson@Sun.COM {
379410594SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
379510594SGeorge.Wilson@Sun.COM 
379610594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
379710594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3798*10922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
379910594SGeorge.Wilson@Sun.COM 
380010594SGeorge.Wilson@Sun.COM 	/*
380110594SGeorge.Wilson@Sun.COM 	 * Remove our vdev from the allocatable vdevs
380210594SGeorge.Wilson@Sun.COM 	 */
380310594SGeorge.Wilson@Sun.COM 	if (mg)
380410594SGeorge.Wilson@Sun.COM 		metaslab_class_remove(mg->mg_class, mg);
380510594SGeorge.Wilson@Sun.COM }
380610594SGeorge.Wilson@Sun.COM 
380710594SGeorge.Wilson@Sun.COM /*
380810594SGeorge.Wilson@Sun.COM  * Evacuate the device.
380910594SGeorge.Wilson@Sun.COM  */
381010594SGeorge.Wilson@Sun.COM int
381110594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
381210594SGeorge.Wilson@Sun.COM {
381310594SGeorge.Wilson@Sun.COM 	uint64_t txg;
381410594SGeorge.Wilson@Sun.COM 	int error;
381510594SGeorge.Wilson@Sun.COM 
381610594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
381710594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
3818*10922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
381910594SGeorge.Wilson@Sun.COM 
382010594SGeorge.Wilson@Sun.COM 	/*
382110594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
382210594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
382310594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
382410594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
382510594SGeorge.Wilson@Sun.COM 	 */
382610594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
382710594SGeorge.Wilson@Sun.COM 		/*
382810594SGeorge.Wilson@Sun.COM 		 * Evacuate the device.
382910594SGeorge.Wilson@Sun.COM 		 */
383010594SGeorge.Wilson@Sun.COM 		if (error = dmu_objset_find(spa_name(spa),
383110594SGeorge.Wilson@Sun.COM 		    zil_vdev_offline, NULL, DS_FIND_CHILDREN)) {
383210594SGeorge.Wilson@Sun.COM 			uint64_t txg;
383310594SGeorge.Wilson@Sun.COM 
383410594SGeorge.Wilson@Sun.COM 			txg = spa_vdev_config_enter(spa);
383510594SGeorge.Wilson@Sun.COM 			metaslab_class_add(spa->spa_log_class,
383610594SGeorge.Wilson@Sun.COM 			    vd->vdev_mg);
383710594SGeorge.Wilson@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
383810594SGeorge.Wilson@Sun.COM 		}
383910594SGeorge.Wilson@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
384010594SGeorge.Wilson@Sun.COM 	}
384110594SGeorge.Wilson@Sun.COM 
384210594SGeorge.Wilson@Sun.COM 	/*
384310594SGeorge.Wilson@Sun.COM 	 * Remove any remaining MOS metadata associated with the device.
384410594SGeorge.Wilson@Sun.COM 	 */
384510594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
384610594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
384710594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
384810594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
384910594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
385010594SGeorge.Wilson@Sun.COM 
385110594SGeorge.Wilson@Sun.COM 	return (0);
385210594SGeorge.Wilson@Sun.COM }
385310594SGeorge.Wilson@Sun.COM 
385410594SGeorge.Wilson@Sun.COM /*
385510594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
385610594SGeorge.Wilson@Sun.COM  */
385710594SGeorge.Wilson@Sun.COM void
385810594SGeorge.Wilson@Sun.COM spa_vdev_remove_done(spa_t *spa, vdev_t *vd)
385910594SGeorge.Wilson@Sun.COM {
386010594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
386110594SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
386210594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
386310594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
386410594SGeorge.Wilson@Sun.COM 
386510594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
386610594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3867*10922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
386810594SGeorge.Wilson@Sun.COM 
386910594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3870*10922SJeff.Bonwick@Sun.COM 
3871*10922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
3872*10922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
3873*10922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
3874*10922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
3875*10922SJeff.Bonwick@Sun.COM 
387610594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
387710594SGeorge.Wilson@Sun.COM 
387810594SGeorge.Wilson@Sun.COM 	/*
387910594SGeorge.Wilson@Sun.COM 	 * It's possible that another thread is trying todo a spa_vdev_add()
388010594SGeorge.Wilson@Sun.COM 	 * at the same time we're trying remove it. As a result the
388110594SGeorge.Wilson@Sun.COM 	 * added vdev may not have initialized its metaslabs yet.
388210594SGeorge.Wilson@Sun.COM 	 */
388310594SGeorge.Wilson@Sun.COM 	if (mg != NULL)
388410594SGeorge.Wilson@Sun.COM 		metaslab_group_destroy(mg);
388510594SGeorge.Wilson@Sun.COM 
388610594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
388710594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
388810594SGeorge.Wilson@Sun.COM 	} else {
388910594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
389010594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
389110594SGeorge.Wilson@Sun.COM 	}
389210594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(rvd);
389310594SGeorge.Wilson@Sun.COM 
389410594SGeorge.Wilson@Sun.COM 	/*
389510594SGeorge.Wilson@Sun.COM 	 * Reassess the health of our root vdev.
389610594SGeorge.Wilson@Sun.COM 	 */
389710594SGeorge.Wilson@Sun.COM 	vdev_reopen(rvd);
389810594SGeorge.Wilson@Sun.COM }
389910594SGeorge.Wilson@Sun.COM 
390010594SGeorge.Wilson@Sun.COM /*
39015450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
390210594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
39035450Sbrendan  */
39045450Sbrendan int
39055450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
39065450Sbrendan {
39075450Sbrendan 	vdev_t *vd;
39087754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
390910594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
39105450Sbrendan 	uint_t nspares, nl2cache;
39115450Sbrendan 	int error = 0;
39128241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
39138241SJeff.Bonwick@Sun.COM 
39148241SJeff.Bonwick@Sun.COM 	if (!locked)
39158241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
39165450Sbrendan 
39176643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
39185450Sbrendan 
39195450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
39205450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
39217754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
39227754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
39237754SJeff.Bonwick@Sun.COM 		/*
39247754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
39257754SJeff.Bonwick@Sun.COM 		 * in this pool.
39267754SJeff.Bonwick@Sun.COM 		 */
39277754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
39287754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
39297754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
39307754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
39317754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
39327754SJeff.Bonwick@Sun.COM 		} else {
39337754SJeff.Bonwick@Sun.COM 			error = EBUSY;
39347754SJeff.Bonwick@Sun.COM 		}
39357754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
39365450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
39377754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
39387754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
39397754SJeff.Bonwick@Sun.COM 		/*
39407754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
39417754SJeff.Bonwick@Sun.COM 		 */
39427754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
39437754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
39445450Sbrendan 		spa_load_l2cache(spa);
39455450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
394610594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
394710594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
3948*10922SJeff.Bonwick@Sun.COM 		ASSERT(vd == vd->vdev_top);
394910594SGeorge.Wilson@Sun.COM 
395010594SGeorge.Wilson@Sun.COM 		/*
395110594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
395210594SGeorge.Wilson@Sun.COM 		 * become the common case.
395310594SGeorge.Wilson@Sun.COM 		 */
395410594SGeorge.Wilson@Sun.COM 
395510594SGeorge.Wilson@Sun.COM 		/*
395610594SGeorge.Wilson@Sun.COM 		 * 1. Stop allocations
395710594SGeorge.Wilson@Sun.COM 		 * 2. Evacuate the device (i.e. kill off stubby and
395810594SGeorge.Wilson@Sun.COM 		 *    metadata) and wait for it to complete (i.e. sync).
395910594SGeorge.Wilson@Sun.COM 		 * 3. Cleanup the vdev namespace.
396010594SGeorge.Wilson@Sun.COM 		 */
396110594SGeorge.Wilson@Sun.COM 		spa_vdev_remove_start(spa, vd);
396210594SGeorge.Wilson@Sun.COM 
3963*10922SJeff.Bonwick@Sun.COM 		/*
3964*10922SJeff.Bonwick@Sun.COM 		 * Wait for the youngest allocations and frees to sync,
3965*10922SJeff.Bonwick@Sun.COM 		 * and then wait for the deferral of those frees to finish.
3966*10922SJeff.Bonwick@Sun.COM 		 */
3967*10922SJeff.Bonwick@Sun.COM 		spa_vdev_config_exit(spa, NULL,
3968*10922SJeff.Bonwick@Sun.COM 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
3969*10922SJeff.Bonwick@Sun.COM 
397010594SGeorge.Wilson@Sun.COM 		if ((error = spa_vdev_remove_evacuate(spa, vd)) != 0)
397110594SGeorge.Wilson@Sun.COM 			return (error);
397210594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
397310594SGeorge.Wilson@Sun.COM 
397410594SGeorge.Wilson@Sun.COM 		spa_vdev_remove_done(spa, vd);
397510594SGeorge.Wilson@Sun.COM 
39767754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
39777754SJeff.Bonwick@Sun.COM 		/*
39787754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
39797754SJeff.Bonwick@Sun.COM 		 */
39807754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
39817754SJeff.Bonwick@Sun.COM 	} else {
39827754SJeff.Bonwick@Sun.COM 		/*
39837754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
39847754SJeff.Bonwick@Sun.COM 		 */
39857754SJeff.Bonwick@Sun.COM 		error = ENOENT;
39865450Sbrendan 	}
39872082Seschrock 
39888241SJeff.Bonwick@Sun.COM 	if (!locked)
39898241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
39908241SJeff.Bonwick@Sun.COM 
39918241SJeff.Bonwick@Sun.COM 	return (error);
3992789Sahrens }
3993789Sahrens 
3994789Sahrens /*
39954451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
39964451Seschrock  * current spared, so we can detach it.
3997789Sahrens  */
39981544Seschrock static vdev_t *
39994451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
4000789Sahrens {
40011544Seschrock 	vdev_t *newvd, *oldvd;
40029816SGeorge.Wilson@Sun.COM 
40039816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
40044451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
40051544Seschrock 		if (oldvd != NULL)
40061544Seschrock 			return (oldvd);
40071544Seschrock 	}
4008789Sahrens 
40094451Seschrock 	/*
40104451Seschrock 	 * Check for a completed replacement.
40114451Seschrock 	 */
4012789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
40131544Seschrock 		oldvd = vd->vdev_child[0];
40141544Seschrock 		newvd = vd->vdev_child[1];
4015789Sahrens 
40168241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
40178241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
40181544Seschrock 			return (oldvd);
40191544Seschrock 	}
4020789Sahrens 
40214451Seschrock 	/*
40224451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
40234451Seschrock 	 */
40244451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
40254451Seschrock 		newvd = vd->vdev_child[0];
40264451Seschrock 		oldvd = vd->vdev_child[1];
40274451Seschrock 
40284451Seschrock 		if (newvd->vdev_unspare &&
40298241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
40308241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
40314451Seschrock 			newvd->vdev_unspare = 0;
40324451Seschrock 			return (oldvd);
40334451Seschrock 		}
40344451Seschrock 	}
40354451Seschrock 
40361544Seschrock 	return (NULL);
4037789Sahrens }
4038789Sahrens 
40391544Seschrock static void
40404451Seschrock spa_vdev_resilver_done(spa_t *spa)
4041789Sahrens {
40428241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
40438241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
40448241SJeff.Bonwick@Sun.COM 
40458241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4046789Sahrens 
40474451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
40488241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
40498241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
40501544Seschrock 		guid = vd->vdev_guid;
40518241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
40528241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
40538241SJeff.Bonwick@Sun.COM 		sguid = 0;
40542082Seschrock 		/*
40552082Seschrock 		 * If we have just finished replacing a hot spared device, then
40562082Seschrock 		 * we need to detach the parent's first child (the original hot
40572082Seschrock 		 * spare) as well.
40582082Seschrock 		 */
40598241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
40602082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
40618241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
40628241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
40632082Seschrock 		}
40648241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
40658241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
40661544Seschrock 			return;
40678241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
40682082Seschrock 			return;
40698241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4070789Sahrens 	}
4071789Sahrens 
40728241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4073789Sahrens }
4074789Sahrens 
4075789Sahrens /*
40769425SEric.Schrock@Sun.COM  * Update the stored path or FRU for this vdev.  Dirty the vdev configuration,
40779425SEric.Schrock@Sun.COM  * relying on spa_vdev_enter/exit() to synchronize the labels and cache.
40781354Seschrock  */
40791354Seschrock int
40809425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
40819425SEric.Schrock@Sun.COM     boolean_t ispath)
40821354Seschrock {
40836643Seschrock 	vdev_t *vd;
40841354Seschrock 	uint64_t txg;
40851354Seschrock 
40861354Seschrock 	txg = spa_vdev_enter(spa);
40871354Seschrock 
40889425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
40895450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
40901354Seschrock 
40911585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
40921585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
40931585Sbonwick 
40949425SEric.Schrock@Sun.COM 	if (ispath) {
40959425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_path);
40969425SEric.Schrock@Sun.COM 		vd->vdev_path = spa_strdup(value);
40979425SEric.Schrock@Sun.COM 	} else {
40989425SEric.Schrock@Sun.COM 		if (vd->vdev_fru != NULL)
40999425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
41009425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(value);
41019425SEric.Schrock@Sun.COM 	}
41021354Seschrock 
41031354Seschrock 	vdev_config_dirty(vd->vdev_top);
41041354Seschrock 
41051354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
41061354Seschrock }
41071354Seschrock 
41089425SEric.Schrock@Sun.COM int
41099425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
41109425SEric.Schrock@Sun.COM {
41119425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
41129425SEric.Schrock@Sun.COM }
41139425SEric.Schrock@Sun.COM 
41149425SEric.Schrock@Sun.COM int
41159425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
41169425SEric.Schrock@Sun.COM {
41179425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
41189425SEric.Schrock@Sun.COM }
41199425SEric.Schrock@Sun.COM 
41201354Seschrock /*
4121789Sahrens  * ==========================================================================
4122789Sahrens  * SPA Scrubbing
4123789Sahrens  * ==========================================================================
4124789Sahrens  */
4125789Sahrens 
41267046Sahrens int
41277046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
4128789Sahrens {
41297754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
41304808Sek110237 
4131789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
4132789Sahrens 		return (ENOTSUP);
4133789Sahrens 
4134789Sahrens 	/*
41357046Sahrens 	 * If a resilver was requested, but there is no DTL on a
41367046Sahrens 	 * writeable leaf device, we have nothing to do.
4137789Sahrens 	 */
41387046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
41397046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
41407046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
41411544Seschrock 		return (0);
41421544Seschrock 	}
4143789Sahrens 
41447046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
41457046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
41467046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
41477046Sahrens 		return (EBUSY);
41487046Sahrens 
41497046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
41507046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
41517046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
41527046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
41531544Seschrock 	} else {
41547046Sahrens 		return (EINVAL);
41551544Seschrock 	}
4156789Sahrens }
4157789Sahrens 
41581544Seschrock /*
41591544Seschrock  * ==========================================================================
41601544Seschrock  * SPA async task processing
41611544Seschrock  * ==========================================================================
41621544Seschrock  */
41631544Seschrock 
41641544Seschrock static void
41654451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4166789Sahrens {
41677361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
41687361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
41697361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
417010575SEric.Schrock@Sun.COM 
417110575SEric.Schrock@Sun.COM 		/*
417210575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
417310575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
417410575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
417510575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
417610575SEric.Schrock@Sun.COM 		 */
417710575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
417810575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
417910575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
418010575SEric.Schrock@Sun.COM 
41817754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
41821544Seschrock 	}
41837361SBrendan.Gregg@Sun.COM 
41847754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
41857361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
41861544Seschrock }
41871544Seschrock 
41881544Seschrock static void
41897754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
41907754SJeff.Bonwick@Sun.COM {
41917754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
41927754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
41937754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
41947754SJeff.Bonwick@Sun.COM 	}
41957754SJeff.Bonwick@Sun.COM 
41967754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
41977754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
41987754SJeff.Bonwick@Sun.COM }
41997754SJeff.Bonwick@Sun.COM 
42007754SJeff.Bonwick@Sun.COM static void
42019816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
42029816SGeorge.Wilson@Sun.COM {
42039816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
42049816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
42059816SGeorge.Wilson@Sun.COM 	char *physpath;
42069816SGeorge.Wilson@Sun.COM 
42079816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
42089816SGeorge.Wilson@Sun.COM 		return;
42099816SGeorge.Wilson@Sun.COM 
42109816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
42119816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
42129816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
42139816SGeorge.Wilson@Sun.COM 	}
42149816SGeorge.Wilson@Sun.COM 
42159816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
42169816SGeorge.Wilson@Sun.COM 		return;
42179816SGeorge.Wilson@Sun.COM 
42189816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
42199816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
42209816SGeorge.Wilson@Sun.COM 
42219816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
42229816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
42239816SGeorge.Wilson@Sun.COM 
42249816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
42259816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
42269816SGeorge.Wilson@Sun.COM 
42279816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
42289816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
42299816SGeorge.Wilson@Sun.COM }
42309816SGeorge.Wilson@Sun.COM 
42319816SGeorge.Wilson@Sun.COM static void
42321544Seschrock spa_async_thread(spa_t *spa)
42331544Seschrock {
42347754SJeff.Bonwick@Sun.COM 	int tasks;
42351544Seschrock 
42361544Seschrock 	ASSERT(spa->spa_sync_on);
4237789Sahrens 
42381544Seschrock 	mutex_enter(&spa->spa_async_lock);
42391544Seschrock 	tasks = spa->spa_async_tasks;
42401544Seschrock 	spa->spa_async_tasks = 0;
42411544Seschrock 	mutex_exit(&spa->spa_async_lock);
42421544Seschrock 
42431544Seschrock 	/*
42441635Sbonwick 	 * See if the config needs to be updated.
42451635Sbonwick 	 */
42461635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
4247*10922SJeff.Bonwick@Sun.COM 		uint64_t old_space, new_space;
42489816SGeorge.Wilson@Sun.COM 
42491635Sbonwick 		mutex_enter(&spa_namespace_lock);
4250*10922SJeff.Bonwick@Sun.COM 		old_space = metaslab_class_get_space(spa_normal_class(spa));
42511635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4252*10922SJeff.Bonwick@Sun.COM 		new_space = metaslab_class_get_space(spa_normal_class(spa));
42531635Sbonwick 		mutex_exit(&spa_namespace_lock);
42549816SGeorge.Wilson@Sun.COM 
42559816SGeorge.Wilson@Sun.COM 		/*
42569816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
42579816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
42589816SGeorge.Wilson@Sun.COM 		 */
4259*10922SJeff.Bonwick@Sun.COM 		if (new_space != old_space) {
42609946SMark.Musante@Sun.COM 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
42619946SMark.Musante@Sun.COM 			    spa, NULL, CRED(),
42629946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
4263*10922SJeff.Bonwick@Sun.COM 			    spa_name(spa), new_space, new_space - old_space);
42649816SGeorge.Wilson@Sun.COM 		}
42651635Sbonwick 	}
42661635Sbonwick 
42671635Sbonwick 	/*
42684451Seschrock 	 * See if any devices need to be marked REMOVED.
42691544Seschrock 	 */
42707754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
427110685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
42724451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
42737754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
42747361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
42757754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
42767361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
42777754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
42787754SJeff.Bonwick@Sun.COM 	}
42797754SJeff.Bonwick@Sun.COM 
42809816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
42819816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
42829816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
42839816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
42849816SGeorge.Wilson@Sun.COM 	}
42859816SGeorge.Wilson@Sun.COM 
42867754SJeff.Bonwick@Sun.COM 	/*
42877754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
42887754SJeff.Bonwick@Sun.COM 	 */
42897754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
429010685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
42917754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
42927754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
42934451Seschrock 	}
42941544Seschrock 
42951544Seschrock 	/*
42961544Seschrock 	 * If any devices are done replacing, detach them.
42971544Seschrock 	 */
42984451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
42994451Seschrock 		spa_vdev_resilver_done(spa);
4300789Sahrens 
43011544Seschrock 	/*
43021544Seschrock 	 * Kick off a resilver.
43031544Seschrock 	 */
43047046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
43057046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
43061544Seschrock 
43071544Seschrock 	/*
43081544Seschrock 	 * Let the world know that we're done.
43091544Seschrock 	 */
43101544Seschrock 	mutex_enter(&spa->spa_async_lock);
43111544Seschrock 	spa->spa_async_thread = NULL;
43121544Seschrock 	cv_broadcast(&spa->spa_async_cv);
43131544Seschrock 	mutex_exit(&spa->spa_async_lock);
43141544Seschrock 	thread_exit();
43151544Seschrock }
43161544Seschrock 
43171544Seschrock void
43181544Seschrock spa_async_suspend(spa_t *spa)
43191544Seschrock {
43201544Seschrock 	mutex_enter(&spa->spa_async_lock);
43211544Seschrock 	spa->spa_async_suspended++;
43221544Seschrock 	while (spa->spa_async_thread != NULL)
43231544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
43241544Seschrock 	mutex_exit(&spa->spa_async_lock);
43251544Seschrock }
43261544Seschrock 
43271544Seschrock void
43281544Seschrock spa_async_resume(spa_t *spa)
43291544Seschrock {
43301544Seschrock 	mutex_enter(&spa->spa_async_lock);
43311544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
43321544Seschrock 	spa->spa_async_suspended--;
43331544Seschrock 	mutex_exit(&spa->spa_async_lock);
43341544Seschrock }
43351544Seschrock 
43361544Seschrock static void
43371544Seschrock spa_async_dispatch(spa_t *spa)
43381544Seschrock {
43391544Seschrock 	mutex_enter(&spa->spa_async_lock);
43401544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
43411635Sbonwick 	    spa->spa_async_thread == NULL &&
43421635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
43431544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
43441544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
43451544Seschrock 	mutex_exit(&spa->spa_async_lock);
43461544Seschrock }
43471544Seschrock 
43481544Seschrock void
43491544Seschrock spa_async_request(spa_t *spa, int task)
43501544Seschrock {
43511544Seschrock 	mutex_enter(&spa->spa_async_lock);
43521544Seschrock 	spa->spa_async_tasks |= task;
43531544Seschrock 	mutex_exit(&spa->spa_async_lock);
4354789Sahrens }
4355789Sahrens 
4356789Sahrens /*
4357789Sahrens  * ==========================================================================
4358789Sahrens  * SPA syncing routines
4359789Sahrens  * ==========================================================================
4360789Sahrens  */
4361789Sahrens static void
4362*10922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg)
4363789Sahrens {
4364789Sahrens 	blkptr_t blk;
4365789Sahrens 	uint64_t itor = 0;
4366789Sahrens 	uint8_t c = 1;
4367789Sahrens 
43687754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
43697754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
4370*10922SJeff.Bonwick@Sun.COM 		zio_free(spa, txg, &blk);
43717754SJeff.Bonwick@Sun.COM 	}
4372789Sahrens 
4373789Sahrens 	bplist_vacate(bpl, tx);
4374789Sahrens 
4375789Sahrens 	/*
4376789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4377789Sahrens 	 * (Usually only the first block is needed.)
4378789Sahrens 	 */
4379*10922SJeff.Bonwick@Sun.COM 	dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx);
4380*10922SJeff.Bonwick@Sun.COM }
4381*10922SJeff.Bonwick@Sun.COM 
4382*10922SJeff.Bonwick@Sun.COM static void
4383*10922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
4384*10922SJeff.Bonwick@Sun.COM {
4385*10922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
4386*10922SJeff.Bonwick@Sun.COM 
4387*10922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
4388*10922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
4389789Sahrens }
4390789Sahrens 
4391789Sahrens static void
43922082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
43932082Seschrock {
43942082Seschrock 	char *packed = NULL;
43957497STim.Haley@Sun.COM 	size_t bufsize;
43962082Seschrock 	size_t nvsize = 0;
43972082Seschrock 	dmu_buf_t *db;
43982082Seschrock 
43992082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
44002082Seschrock 
44017497STim.Haley@Sun.COM 	/*
44027497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
44037497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
44047497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
44057497STim.Haley@Sun.COM 	 */
44067497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
44077497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
44082082Seschrock 
44092082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
44102082Seschrock 	    KM_SLEEP) == 0);
44117497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
44127497STim.Haley@Sun.COM 
44137497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
44147497STim.Haley@Sun.COM 
44157497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
44162082Seschrock 
44172082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
44182082Seschrock 	dmu_buf_will_dirty(db, tx);
44192082Seschrock 	*(uint64_t *)db->db_data = nvsize;
44202082Seschrock 	dmu_buf_rele(db, FTAG);
44212082Seschrock }
44222082Seschrock 
44232082Seschrock static void
44245450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
44255450Sbrendan     const char *config, const char *entry)
44262082Seschrock {
44272082Seschrock 	nvlist_t *nvroot;
44285450Sbrendan 	nvlist_t **list;
44292082Seschrock 	int i;
44302082Seschrock 
44315450Sbrendan 	if (!sav->sav_sync)
44322082Seschrock 		return;
44332082Seschrock 
44342082Seschrock 	/*
44355450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
44365450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
44374451Seschrock 	 * valid and the vdevs are labeled appropriately.
44382082Seschrock 	 */
44395450Sbrendan 	if (sav->sav_object == 0) {
44405450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
44415450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
44425450Sbrendan 		    sizeof (uint64_t), tx);
44432082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
44445450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
44455450Sbrendan 		    &sav->sav_object, tx) == 0);
44462082Seschrock 	}
44472082Seschrock 
44482082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
44495450Sbrendan 	if (sav->sav_count == 0) {
44505450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
44512082Seschrock 	} else {
44525450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
44535450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
44545450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
44555450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
44565450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
44575450Sbrendan 		    sav->sav_count) == 0);
44585450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
44595450Sbrendan 			nvlist_free(list[i]);
44605450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
44612082Seschrock 	}
44622082Seschrock 
44635450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
44642926Sek110237 	nvlist_free(nvroot);
44652082Seschrock 
44665450Sbrendan 	sav->sav_sync = B_FALSE;
44672082Seschrock }
44682082Seschrock 
44692082Seschrock static void
4470789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
4471789Sahrens {
4472789Sahrens 	nvlist_t *config;
4473789Sahrens 
44747754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
4475789Sahrens 		return;
4476789Sahrens 
44777754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44787754SJeff.Bonwick@Sun.COM 
44797754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
44807754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
44817754SJeff.Bonwick@Sun.COM 
44827754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
4483789Sahrens 
44841635Sbonwick 	if (spa->spa_config_syncing)
44851635Sbonwick 		nvlist_free(spa->spa_config_syncing);
44861635Sbonwick 	spa->spa_config_syncing = config;
4487789Sahrens 
44882082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
4489789Sahrens }
4490789Sahrens 
44915094Slling /*
44925094Slling  * Set zpool properties.
44935094Slling  */
44943912Slling static void
44954543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
44963912Slling {
44973912Slling 	spa_t *spa = arg1;
44985094Slling 	objset_t *mos = spa->spa_meta_objset;
44993912Slling 	nvlist_t *nvp = arg2;
45005094Slling 	nvpair_t *elem;
45014451Seschrock 	uint64_t intval;
45026643Seschrock 	char *strval;
45035094Slling 	zpool_prop_t prop;
45045094Slling 	const char *propname;
45055094Slling 	zprop_type_t proptype;
45065094Slling 
45077754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
45087754SJeff.Bonwick@Sun.COM 
45095094Slling 	elem = NULL;
45105094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
45115094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
45125094Slling 		case ZPOOL_PROP_VERSION:
45135094Slling 			/*
45145094Slling 			 * Only set version for non-zpool-creation cases
45155094Slling 			 * (set/import). spa_create() needs special care
45165094Slling 			 * for version setting.
45175094Slling 			 */
45185094Slling 			if (tx->tx_txg != TXG_INITIAL) {
45195094Slling 				VERIFY(nvpair_value_uint64(elem,
45205094Slling 				    &intval) == 0);
45215094Slling 				ASSERT(intval <= SPA_VERSION);
45225094Slling 				ASSERT(intval >= spa_version(spa));
45235094Slling 				spa->spa_uberblock.ub_version = intval;
45245094Slling 				vdev_config_dirty(spa->spa_root_vdev);
45255094Slling 			}
45265094Slling 			break;
45275094Slling 
45285094Slling 		case ZPOOL_PROP_ALTROOT:
45295094Slling 			/*
45305094Slling 			 * 'altroot' is a non-persistent property. It should
45315094Slling 			 * have been set temporarily at creation or import time.
45325094Slling 			 */
45335094Slling 			ASSERT(spa->spa_root != NULL);
45345094Slling 			break;
45355094Slling 
45365363Seschrock 		case ZPOOL_PROP_CACHEFILE:
45375094Slling 			/*
45388525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
45395094Slling 			 */
45404543Smarks 			break;
45415094Slling 		default:
45425094Slling 			/*
45435094Slling 			 * Set pool property values in the poolprops mos object.
45445094Slling 			 */
45455094Slling 			if (spa->spa_pool_props_object == 0) {
45465094Slling 				VERIFY((spa->spa_pool_props_object =
45475094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
45485094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
45495094Slling 
45505094Slling 				VERIFY(zap_update(mos,
45515094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
45525094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
45535094Slling 				    == 0);
45545094Slling 			}
45555094Slling 
45565094Slling 			/* normalize the property name */
45575094Slling 			propname = zpool_prop_to_name(prop);
45585094Slling 			proptype = zpool_prop_get_type(prop);
45595094Slling 
45605094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
45615094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
45625094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
45635094Slling 				VERIFY(zap_update(mos,
45645094Slling 				    spa->spa_pool_props_object, propname,
45655094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
45665094Slling 
45675094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
45685094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
45695094Slling 
45705094Slling 				if (proptype == PROP_TYPE_INDEX) {
45715094Slling 					const char *unused;
45725094Slling 					VERIFY(zpool_prop_index_to_string(
45735094Slling 					    prop, intval, &unused) == 0);
45745094Slling 				}
45755094Slling 				VERIFY(zap_update(mos,
45765094Slling 				    spa->spa_pool_props_object, propname,
45775094Slling 				    8, 1, &intval, tx) == 0);
45785094Slling 			} else {
45795094Slling 				ASSERT(0); /* not allowed */
45805094Slling 			}
45815094Slling 
45825329Sgw25295 			switch (prop) {
45835329Sgw25295 			case ZPOOL_PROP_DELEGATION:
45845094Slling 				spa->spa_delegation = intval;
45855329Sgw25295 				break;
45865329Sgw25295 			case ZPOOL_PROP_BOOTFS:
45875094Slling 				spa->spa_bootfs = intval;
45885329Sgw25295 				break;
45895329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
45905329Sgw25295 				spa->spa_failmode = intval;
45915329Sgw25295 				break;
45929816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
45939816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
45949816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
45959816SGeorge.Wilson@Sun.COM 				break;
4596*10922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
4597*10922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
4598*10922SJeff.Bonwick@Sun.COM 				break;
45995329Sgw25295 			default:
46005329Sgw25295 				break;
46015329Sgw25295 			}
46023912Slling 		}
46035094Slling 
46045094Slling 		/* log internal history if this is not a zpool create */
46055094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
46065094Slling 		    tx->tx_txg != TXG_INITIAL) {
46075094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
46085094Slling 			    spa, tx, cr, "%s %lld %s",
46097754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
46105094Slling 		}
46113912Slling 	}
46127754SJeff.Bonwick@Sun.COM 
46137754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
46143912Slling }
46153912Slling 
4616789Sahrens /*
4617789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
4618789Sahrens  * part of the process, so we iterate until it converges.
4619789Sahrens  */
4620789Sahrens void
4621789Sahrens spa_sync(spa_t *spa, uint64_t txg)
4622789Sahrens {
4623789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
4624789Sahrens 	objset_t *mos = spa->spa_meta_objset;
4625*10922SJeff.Bonwick@Sun.COM 	bplist_t *defer_bpl = &spa->spa_deferred_bplist;
4626*10922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
46271635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4628789Sahrens 	vdev_t *vd;
4629789Sahrens 	dmu_tx_t *tx;
46307754SJeff.Bonwick@Sun.COM 	int error;
4631789Sahrens 
4632789Sahrens 	/*
4633789Sahrens 	 * Lock out configuration changes.
4634789Sahrens 	 */
46357754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4636789Sahrens 
4637789Sahrens 	spa->spa_syncing_txg = txg;
4638789Sahrens 	spa->spa_sync_pass = 0;
4639789Sahrens 
46407754SJeff.Bonwick@Sun.COM 	/*
46417754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
46427754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
46437754SJeff.Bonwick@Sun.COM 	 */
46447754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
46458241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
46468241SJeff.Bonwick@Sun.COM 		/*
46478241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
46488241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
46498241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
46508241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
46518241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
46528241SJeff.Bonwick@Sun.COM 		 */
46538241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
46548241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
46558241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
46568241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
46578241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
46588241SJeff.Bonwick@Sun.COM 		}
46598241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
46608241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
46617754SJeff.Bonwick@Sun.COM 	}
46627754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
46637754SJeff.Bonwick@Sun.COM 
4664*10922SJeff.Bonwick@Sun.COM 	VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj));
4665789Sahrens 
46662082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
46672082Seschrock 
46682082Seschrock 	/*
46694577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
46702082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
46712082Seschrock 	 */
46724577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
46734577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
46742082Seschrock 		int i;
46752082Seschrock 
46762082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
46772082Seschrock 			vd = rvd->vdev_child[i];
46782082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
46792082Seschrock 				break;
46802082Seschrock 		}
46812082Seschrock 		if (i == rvd->vdev_children) {
46822082Seschrock 			spa->spa_deflate = TRUE;
46832082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
46842082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
46852082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
46862082Seschrock 		}
46872082Seschrock 	}
46882082Seschrock 
46897046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
46907046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
46917046Sahrens 		dsl_pool_create_origin(dp, tx);
46927046Sahrens 
46937046Sahrens 		/* Keeping the origin open increases spa_minref */
46947046Sahrens 		spa->spa_minref += 3;
46957046Sahrens 	}
46967046Sahrens 
46977046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
46987046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
46997046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
47007046Sahrens 	}
47017046Sahrens 
4702789Sahrens 	/*
4703789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4704789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4705789Sahrens 	 * don't generate work on an otherwise idle system.
4706789Sahrens 	 */
4707789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
47082329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
47092329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4710*10922SJeff.Bonwick@Sun.COM 		spa_sync_deferred_bplist(spa, defer_bpl, tx, txg);
4711789Sahrens 
4712789Sahrens 	/*
4713789Sahrens 	 * Iterate to convergence.
4714789Sahrens 	 */
4715789Sahrens 	do {
4716*10922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
4717789Sahrens 
4718789Sahrens 		spa_sync_config_object(spa, tx);
47195450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
47205450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
47215450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
47225450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
47231544Seschrock 		spa_errlog_sync(spa, txg);
4724789Sahrens 		dsl_pool_sync(dp, txg);
4725789Sahrens 
4726*10922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
4727*10922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
4728*10922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, spa_sync_free, zio, tx);
4729*10922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
4730*10922SJeff.Bonwick@Sun.COM 		} else {
4731*10922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx);
4732789Sahrens 		}
4733789Sahrens 
4734*10922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
4735*10922SJeff.Bonwick@Sun.COM 
4736*10922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
4737*10922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
4738*10922SJeff.Bonwick@Sun.COM 
4739*10922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
4740*10922SJeff.Bonwick@Sun.COM 
4741*10922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
4742*10922SJeff.Bonwick@Sun.COM 
4743*10922SJeff.Bonwick@Sun.COM 	bplist_close(defer_bpl);
4744789Sahrens 
4745789Sahrens 	/*
4746789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4747789Sahrens 	 * to commit the transaction group.
47481635Sbonwick 	 *
47495688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
47505688Sbonwick 	 * random top-level vdevs that are known to be visible in the
47517754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
47527754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4753789Sahrens 	 */
47547754SJeff.Bonwick@Sun.COM 	for (;;) {
47557754SJeff.Bonwick@Sun.COM 		/*
47567754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
47577754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
47587754SJeff.Bonwick@Sun.COM 		 */
47597754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
47607754SJeff.Bonwick@Sun.COM 
47617754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
47627754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
47637754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
47647754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
47657754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
47669816SGeorge.Wilson@Sun.COM 
47679816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
47687754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
47697754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
47707754SJeff.Bonwick@Sun.COM 					continue;
47717754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
47727754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
47737754SJeff.Bonwick@Sun.COM 					break;
47747754SJeff.Bonwick@Sun.COM 			}
47759725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
47769725SEric.Schrock@Sun.COM 			if (error != 0)
47779725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
47789725SEric.Schrock@Sun.COM 				    B_TRUE);
47797754SJeff.Bonwick@Sun.COM 		} else {
47807754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
47819725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
47829725SEric.Schrock@Sun.COM 			if (error != 0)
47839725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
47849725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
47851635Sbonwick 		}
47867754SJeff.Bonwick@Sun.COM 
47877754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
47887754SJeff.Bonwick@Sun.COM 
47897754SJeff.Bonwick@Sun.COM 		if (error == 0)
47907754SJeff.Bonwick@Sun.COM 			break;
47917754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
47927754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
47931635Sbonwick 	}
47942082Seschrock 	dmu_tx_commit(tx);
47952082Seschrock 
47961635Sbonwick 	/*
47971635Sbonwick 	 * Clear the dirty config list.
47981635Sbonwick 	 */
47997754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
48001635Sbonwick 		vdev_config_clean(vd);
48011635Sbonwick 
48021635Sbonwick 	/*
48031635Sbonwick 	 * Now that the new config has synced transactionally,
48041635Sbonwick 	 * let it become visible to the config cache.
48051635Sbonwick 	 */
48061635Sbonwick 	if (spa->spa_config_syncing != NULL) {
48071635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
48081635Sbonwick 		spa->spa_config_txg = txg;
48091635Sbonwick 		spa->spa_config_syncing = NULL;
48101635Sbonwick 	}
4811789Sahrens 
4812789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4813789Sahrens 
4814*10922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
4815789Sahrens 
4816789Sahrens 	/*
4817789Sahrens 	 * Update usable space statistics.
4818789Sahrens 	 */
4819789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4820789Sahrens 		vdev_sync_done(vd, txg);
4821789Sahrens 
4822789Sahrens 	/*
4823789Sahrens 	 * It had better be the case that we didn't dirty anything
48242082Seschrock 	 * since vdev_config_sync().
4825789Sahrens 	 */
4826789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4827789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4828789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4829*10922SJeff.Bonwick@Sun.COM 	ASSERT(defer_bpl->bpl_queue == NULL);
4830*10922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
4831*10922SJeff.Bonwick@Sun.COM 
4832*10922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
4833789Sahrens 
48347754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
48351544Seschrock 
483610921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
483710921STim.Haley@Sun.COM 
48381544Seschrock 	/*
48391544Seschrock 	 * If any async tasks have been requested, kick them off.
48401544Seschrock 	 */
48411544Seschrock 	spa_async_dispatch(spa);
4842789Sahrens }
4843789Sahrens 
4844789Sahrens /*
4845789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4846789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4847789Sahrens  * sync.
4848789Sahrens  */
4849789Sahrens void
4850789Sahrens spa_sync_allpools(void)
4851789Sahrens {
4852789Sahrens 	spa_t *spa = NULL;
4853789Sahrens 	mutex_enter(&spa_namespace_lock);
4854789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
48557754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4856789Sahrens 			continue;
4857789Sahrens 		spa_open_ref(spa, FTAG);
4858789Sahrens 		mutex_exit(&spa_namespace_lock);
4859789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4860789Sahrens 		mutex_enter(&spa_namespace_lock);
4861789Sahrens 		spa_close(spa, FTAG);
4862789Sahrens 	}
4863789Sahrens 	mutex_exit(&spa_namespace_lock);
4864789Sahrens }
4865789Sahrens 
4866789Sahrens /*
4867789Sahrens  * ==========================================================================
4868789Sahrens  * Miscellaneous routines
4869789Sahrens  * ==========================================================================
4870789Sahrens  */
4871789Sahrens 
4872789Sahrens /*
4873789Sahrens  * Remove all pools in the system.
4874789Sahrens  */
4875789Sahrens void
4876789Sahrens spa_evict_all(void)
4877789Sahrens {
4878789Sahrens 	spa_t *spa;
4879789Sahrens 
4880789Sahrens 	/*
4881789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4882789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4883789Sahrens 	 */
4884789Sahrens 	mutex_enter(&spa_namespace_lock);
4885789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4886789Sahrens 		/*
48871544Seschrock 		 * Stop async tasks.  The async thread may need to detach
48881544Seschrock 		 * a device that's been replaced, which requires grabbing
48891544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4890789Sahrens 		 */
4891789Sahrens 		spa_open_ref(spa, FTAG);
4892789Sahrens 		mutex_exit(&spa_namespace_lock);
48931544Seschrock 		spa_async_suspend(spa);
48944808Sek110237 		mutex_enter(&spa_namespace_lock);
4895789Sahrens 		spa_close(spa, FTAG);
4896789Sahrens 
4897789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4898789Sahrens 			spa_unload(spa);
4899789Sahrens 			spa_deactivate(spa);
4900789Sahrens 		}
4901789Sahrens 		spa_remove(spa);
4902789Sahrens 	}
4903789Sahrens 	mutex_exit(&spa_namespace_lock);
4904789Sahrens }
49051544Seschrock 
49061544Seschrock vdev_t *
49079425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
49081544Seschrock {
49096643Seschrock 	vdev_t *vd;
49106643Seschrock 	int i;
49116643Seschrock 
49126643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
49136643Seschrock 		return (vd);
49146643Seschrock 
49159425SEric.Schrock@Sun.COM 	if (aux) {
49166643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
49176643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
49186643Seschrock 			if (vd->vdev_guid == guid)
49196643Seschrock 				return (vd);
49206643Seschrock 		}
49219425SEric.Schrock@Sun.COM 
49229425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
49239425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
49249425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
49259425SEric.Schrock@Sun.COM 				return (vd);
49269425SEric.Schrock@Sun.COM 		}
49276643Seschrock 	}
49286643Seschrock 
49296643Seschrock 	return (NULL);
49301544Seschrock }
49311760Seschrock 
49321760Seschrock void
49335094Slling spa_upgrade(spa_t *spa, uint64_t version)
49341760Seschrock {
49357754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
49361760Seschrock 
49371760Seschrock 	/*
49381760Seschrock 	 * This should only be called for a non-faulted pool, and since a
49391760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
49401760Seschrock 	 * possible.
49411760Seschrock 	 */
49424577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
49435094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
49445094Slling 
49455094Slling 	spa->spa_uberblock.ub_version = version;
49461760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
49471760Seschrock 
49487754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
49492082Seschrock 
49502082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
49511760Seschrock }
49522082Seschrock 
49532082Seschrock boolean_t
49542082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
49552082Seschrock {
49562082Seschrock 	int i;
49573377Seschrock 	uint64_t spareguid;
49585450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
49595450Sbrendan 
49605450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
49615450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
49622082Seschrock 			return (B_TRUE);
49632082Seschrock 
49645450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
49655450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
49665450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
49673377Seschrock 			return (B_TRUE);
49683377Seschrock 	}
49693377Seschrock 
49702082Seschrock 	return (B_FALSE);
49712082Seschrock }
49723912Slling 
49734451Seschrock /*
49747214Slling  * Check if a pool has an active shared spare device.
49757214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
49767214Slling  */
49777214Slling static boolean_t
49787214Slling spa_has_active_shared_spare(spa_t *spa)
49797214Slling {
49807214Slling 	int i, refcnt;
49817214Slling 	uint64_t pool;
49827214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
49837214Slling 
49847214Slling 	for (i = 0; i < sav->sav_count; i++) {
49857214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
49867214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
49877214Slling 		    refcnt > 2)
49887214Slling 			return (B_TRUE);
49897214Slling 	}
49907214Slling 
49917214Slling 	return (B_FALSE);
49927214Slling }
49937214Slling 
49947214Slling /*
49954451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
49964451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
49974451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
49984451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
49994451Seschrock  * or zdb as real changes.
50004451Seschrock  */
50014451Seschrock void
50024451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
50034451Seschrock {
50044451Seschrock #ifdef _KERNEL
50054451Seschrock 	sysevent_t		*ev;
50064451Seschrock 	sysevent_attr_list_t	*attr = NULL;
50074451Seschrock 	sysevent_value_t	value;
50084451Seschrock 	sysevent_id_t		eid;
50094451Seschrock 
50104451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
50114451Seschrock 	    SE_SLEEP);
50124451Seschrock 
50134451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
50144451Seschrock 	value.value.sv_string = spa_name(spa);
50154451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
50164451Seschrock 		goto done;
50174451Seschrock 
50184451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
50194451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
50204451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
50214451Seschrock 		goto done;
50224451Seschrock 
50234451Seschrock 	if (vd) {
50244451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
50254451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
50264451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
50274451Seschrock 		    SE_SLEEP) != 0)
50284451Seschrock 			goto done;
50294451Seschrock 
50304451Seschrock 		if (vd->vdev_path) {
50314451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
50324451Seschrock 			value.value.sv_string = vd->vdev_path;
50334451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
50344451Seschrock 			    &value, SE_SLEEP) != 0)
50354451Seschrock 				goto done;
50364451Seschrock 		}
50374451Seschrock 	}
50384451Seschrock 
50395756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
50405756Seschrock 		goto done;
50415756Seschrock 	attr = NULL;
50425756Seschrock 
50434451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
50444451Seschrock 
50454451Seschrock done:
50464451Seschrock 	if (attr)
50474451Seschrock 		sysevent_free_attr(attr);
50484451Seschrock 	sysevent_free(ev);
50494451Seschrock #endif
50504451Seschrock }
5051