xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 10921)
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/zio_compress.h>
39789Sahrens #include <sys/dmu.h>
40789Sahrens #include <sys/dmu_tx.h>
41789Sahrens #include <sys/zap.h>
42789Sahrens #include <sys/zil.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
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) {
1578525SEric.Schrock@Sun.COM 		size = spa_get_space(spa);
1588525SEric.Schrock@Sun.COM 		used = spa_get_alloc(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 
1688525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1698525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1708525SEric.Schrock@Sun.COM 
1718525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1728525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1738525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1748525SEric.Schrock@Sun.COM 		else
1758525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1768525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1778525SEric.Schrock@Sun.COM 	}
1785949Slling 
1795949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1805949Slling 
1815949Slling 	if (spa->spa_root != NULL)
1825949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1835949Slling 		    0, ZPROP_SRC_LOCAL);
1845094Slling 
1856643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1866643Seschrock 		if (dp->scd_path == NULL) {
1875949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1886643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1896643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1905949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1916643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1925363Seschrock 		}
1935363Seschrock 	}
1945094Slling }
1955094Slling 
1965094Slling /*
1975094Slling  * Get zpool property values.
1985094Slling  */
1995094Slling int
2005094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2015094Slling {
2025094Slling 	zap_cursor_t zc;
2035094Slling 	zap_attribute_t za;
2045094Slling 	objset_t *mos = spa->spa_meta_objset;
2055094Slling 	int err;
2065094Slling 
2075949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2085094Slling 
2097754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2107754SJeff.Bonwick@Sun.COM 
2115094Slling 	/*
2125094Slling 	 * Get properties from the spa config.
2135094Slling 	 */
2145949Slling 	spa_prop_get_config(spa, nvp);
2155094Slling 
2165094Slling 	/* If no pool property object, no more prop to get. */
2175094Slling 	if (spa->spa_pool_props_object == 0) {
2185094Slling 		mutex_exit(&spa->spa_props_lock);
2195094Slling 		return (0);
2205094Slling 	}
2215094Slling 
2225094Slling 	/*
2235094Slling 	 * Get properties from the MOS pool property object.
2245094Slling 	 */
2255094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
2265094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
2275094Slling 	    zap_cursor_advance(&zc)) {
2285094Slling 		uint64_t intval = 0;
2295094Slling 		char *strval = NULL;
2305094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
2315094Slling 		zpool_prop_t prop;
2325094Slling 
2335094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
2345094Slling 			continue;
2355094Slling 
2365094Slling 		switch (za.za_integer_length) {
2375094Slling 		case 8:
2385094Slling 			/* integer property */
2395094Slling 			if (za.za_first_integer !=
2405094Slling 			    zpool_prop_default_numeric(prop))
2415094Slling 				src = ZPROP_SRC_LOCAL;
2425094Slling 
2435094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2445094Slling 				dsl_pool_t *dp;
2455094Slling 				dsl_dataset_t *ds = NULL;
2465094Slling 
2475094Slling 				dp = spa_get_dsl(spa);
2485094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2496689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2506689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2515094Slling 					rw_exit(&dp->dp_config_rwlock);
2525094Slling 					break;
2535094Slling 				}
2545094Slling 
2555094Slling 				strval = kmem_alloc(
2565094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2575094Slling 				    KM_SLEEP);
2585094Slling 				dsl_dataset_name(ds, strval);
2596689Smaybee 				dsl_dataset_rele(ds, FTAG);
2605094Slling 				rw_exit(&dp->dp_config_rwlock);
2615094Slling 			} else {
2625094Slling 				strval = NULL;
2635094Slling 				intval = za.za_first_integer;
2645094Slling 			}
2655094Slling 
2665949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2675094Slling 
2685094Slling 			if (strval != NULL)
2695094Slling 				kmem_free(strval,
2705094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2715094Slling 
2725094Slling 			break;
2735094Slling 
2745094Slling 		case 1:
2755094Slling 			/* string property */
2765094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2775094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2785094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2795094Slling 			if (err) {
2805094Slling 				kmem_free(strval, za.za_num_integers);
2815094Slling 				break;
2825094Slling 			}
2835949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2845094Slling 			kmem_free(strval, za.za_num_integers);
2855094Slling 			break;
2865094Slling 
2875094Slling 		default:
2885094Slling 			break;
2895094Slling 		}
2905094Slling 	}
2915094Slling 	zap_cursor_fini(&zc);
2925094Slling 	mutex_exit(&spa->spa_props_lock);
2935094Slling out:
2945094Slling 	if (err && err != ENOENT) {
2955094Slling 		nvlist_free(*nvp);
2965949Slling 		*nvp = NULL;
2975094Slling 		return (err);
2985094Slling 	}
2995094Slling 
3005094Slling 	return (0);
3015094Slling }
3025094Slling 
3035094Slling /*
3045094Slling  * Validate the given pool properties nvlist and modify the list
3055094Slling  * for the property values to be set.
3065094Slling  */
3075094Slling static int
3085094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
3095094Slling {
3105094Slling 	nvpair_t *elem;
3115094Slling 	int error = 0, reset_bootfs = 0;
3125094Slling 	uint64_t objnum;
3135094Slling 
3145094Slling 	elem = NULL;
3155094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3165094Slling 		zpool_prop_t prop;
3175094Slling 		char *propname, *strval;
3185094Slling 		uint64_t intval;
3195094Slling 		objset_t *os;
3205363Seschrock 		char *slash;
3215094Slling 
3225094Slling 		propname = nvpair_name(elem);
3235094Slling 
3245094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
3255094Slling 			return (EINVAL);
3265094Slling 
3275094Slling 		switch (prop) {
3285094Slling 		case ZPOOL_PROP_VERSION:
3295094Slling 			error = nvpair_value_uint64(elem, &intval);
3305094Slling 			if (!error &&
3315094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
3325094Slling 				error = EINVAL;
3335094Slling 			break;
3345094Slling 
3355094Slling 		case ZPOOL_PROP_DELEGATION:
3365094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3377538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3389816SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_AUTOEXPAND:
3395094Slling 			error = nvpair_value_uint64(elem, &intval);
3405094Slling 			if (!error && intval > 1)
3415094Slling 				error = EINVAL;
3425094Slling 			break;
3435094Slling 
3445094Slling 		case ZPOOL_PROP_BOOTFS:
3459630SJeff.Bonwick@Sun.COM 			/*
3469630SJeff.Bonwick@Sun.COM 			 * If the pool version is less than SPA_VERSION_BOOTFS,
3479630SJeff.Bonwick@Sun.COM 			 * or the pool is still being created (version == 0),
3489630SJeff.Bonwick@Sun.COM 			 * the bootfs property cannot be set.
3499630SJeff.Bonwick@Sun.COM 			 */
3505094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3515094Slling 				error = ENOTSUP;
3525094Slling 				break;
3535094Slling 			}
3545094Slling 
3555094Slling 			/*
3567042Sgw25295 			 * Make sure the vdev config is bootable
3575094Slling 			 */
3587042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3595094Slling 				error = ENOTSUP;
3605094Slling 				break;
3615094Slling 			}
3625094Slling 
3635094Slling 			reset_bootfs = 1;
3645094Slling 
3655094Slling 			error = nvpair_value_string(elem, &strval);
3665094Slling 
3675094Slling 			if (!error) {
3687042Sgw25295 				uint64_t compress;
3697042Sgw25295 
3705094Slling 				if (strval == NULL || strval[0] == '\0') {
3715094Slling 					objnum = zpool_prop_default_numeric(
3725094Slling 					    ZPOOL_PROP_BOOTFS);
3735094Slling 					break;
3745094Slling 				}
3755094Slling 
37610298SMatthew.Ahrens@Sun.COM 				if (error = dmu_objset_hold(strval, FTAG, &os))
3775094Slling 					break;
3787042Sgw25295 
37910298SMatthew.Ahrens@Sun.COM 				/* Must be ZPL and not gzip compressed. */
38010298SMatthew.Ahrens@Sun.COM 
38110298SMatthew.Ahrens@Sun.COM 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
38210298SMatthew.Ahrens@Sun.COM 					error = ENOTSUP;
38310298SMatthew.Ahrens@Sun.COM 				} else if ((error = dsl_prop_get_integer(strval,
3847042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3857042Sgw25295 				    &compress, NULL)) == 0 &&
3867042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3877042Sgw25295 					error = ENOTSUP;
3887042Sgw25295 				} else {
3897042Sgw25295 					objnum = dmu_objset_id(os);
3907042Sgw25295 				}
39110298SMatthew.Ahrens@Sun.COM 				dmu_objset_rele(os, FTAG);
3925094Slling 			}
3935094Slling 			break;
3947754SJeff.Bonwick@Sun.COM 
3955329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3965329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3975329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3985329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3995329Sgw25295 				error = EINVAL;
4005329Sgw25295 
4015329Sgw25295 			/*
4025329Sgw25295 			 * This is a special case which only occurs when
4035329Sgw25295 			 * the pool has completely failed. This allows
4045329Sgw25295 			 * the user to change the in-core failmode property
4055329Sgw25295 			 * without syncing it out to disk (I/Os might
4065329Sgw25295 			 * currently be blocked). We do this by returning
4075329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
4085329Sgw25295 			 * into thinking we encountered a property validation
4095329Sgw25295 			 * error.
4105329Sgw25295 			 */
4117754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
4125329Sgw25295 				spa->spa_failmode = intval;
4135329Sgw25295 				error = EIO;
4145329Sgw25295 			}
4155329Sgw25295 			break;
4165363Seschrock 
4175363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4185363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
4195363Seschrock 				break;
4205363Seschrock 
4215363Seschrock 			if (strval[0] == '\0')
4225363Seschrock 				break;
4235363Seschrock 
4245363Seschrock 			if (strcmp(strval, "none") == 0)
4255363Seschrock 				break;
4265363Seschrock 
4275363Seschrock 			if (strval[0] != '/') {
4285363Seschrock 				error = EINVAL;
4295363Seschrock 				break;
4305363Seschrock 			}
4315363Seschrock 
4325363Seschrock 			slash = strrchr(strval, '/');
4335363Seschrock 			ASSERT(slash != NULL);
4345363Seschrock 
4355363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4365363Seschrock 			    strcmp(slash, "/..") == 0)
4375363Seschrock 				error = EINVAL;
4385363Seschrock 			break;
4395094Slling 		}
4405094Slling 
4415094Slling 		if (error)
4425094Slling 			break;
4435094Slling 	}
4445094Slling 
4455094Slling 	if (!error && reset_bootfs) {
4465094Slling 		error = nvlist_remove(props,
4475094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4485094Slling 
4495094Slling 		if (!error) {
4505094Slling 			error = nvlist_add_uint64(props,
4515094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4525094Slling 		}
4535094Slling 	}
4545094Slling 
4555094Slling 	return (error);
4565094Slling }
4575094Slling 
4588525SEric.Schrock@Sun.COM void
4598525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
4608525SEric.Schrock@Sun.COM {
4618525SEric.Schrock@Sun.COM 	char *cachefile;
4628525SEric.Schrock@Sun.COM 	spa_config_dirent_t *dp;
4638525SEric.Schrock@Sun.COM 
4648525SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
4658525SEric.Schrock@Sun.COM 	    &cachefile) != 0)
4668525SEric.Schrock@Sun.COM 		return;
4678525SEric.Schrock@Sun.COM 
4688525SEric.Schrock@Sun.COM 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
4698525SEric.Schrock@Sun.COM 	    KM_SLEEP);
4708525SEric.Schrock@Sun.COM 
4718525SEric.Schrock@Sun.COM 	if (cachefile[0] == '\0')
4728525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(spa_config_path);
4738525SEric.Schrock@Sun.COM 	else if (strcmp(cachefile, "none") == 0)
4748525SEric.Schrock@Sun.COM 		dp->scd_path = NULL;
4758525SEric.Schrock@Sun.COM 	else
4768525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(cachefile);
4778525SEric.Schrock@Sun.COM 
4788525SEric.Schrock@Sun.COM 	list_insert_head(&spa->spa_config_list, dp);
4798525SEric.Schrock@Sun.COM 	if (need_sync)
4808525SEric.Schrock@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4818525SEric.Schrock@Sun.COM }
4828525SEric.Schrock@Sun.COM 
4835094Slling int
4845094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4855094Slling {
4865094Slling 	int error;
4878525SEric.Schrock@Sun.COM 	nvpair_t *elem;
4888525SEric.Schrock@Sun.COM 	boolean_t need_sync = B_FALSE;
4898525SEric.Schrock@Sun.COM 	zpool_prop_t prop;
4905094Slling 
4915094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4925094Slling 		return (error);
4935094Slling 
4948525SEric.Schrock@Sun.COM 	elem = NULL;
4958525SEric.Schrock@Sun.COM 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
4968525SEric.Schrock@Sun.COM 		if ((prop = zpool_name_to_prop(
4978525SEric.Schrock@Sun.COM 		    nvpair_name(elem))) == ZPROP_INVAL)
4988525SEric.Schrock@Sun.COM 			return (EINVAL);
4998525SEric.Schrock@Sun.COM 
5008525SEric.Schrock@Sun.COM 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
5018525SEric.Schrock@Sun.COM 			continue;
5028525SEric.Schrock@Sun.COM 
5038525SEric.Schrock@Sun.COM 		need_sync = B_TRUE;
5048525SEric.Schrock@Sun.COM 		break;
5058525SEric.Schrock@Sun.COM 	}
5068525SEric.Schrock@Sun.COM 
5078525SEric.Schrock@Sun.COM 	if (need_sync)
5088525SEric.Schrock@Sun.COM 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
5098525SEric.Schrock@Sun.COM 		    spa, nvp, 3));
5108525SEric.Schrock@Sun.COM 	else
5118525SEric.Schrock@Sun.COM 		return (0);
5125094Slling }
5135094Slling 
5145094Slling /*
5155094Slling  * If the bootfs property value is dsobj, clear it.
5165094Slling  */
5175094Slling void
5185094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
5195094Slling {
5205094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
5215094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
5225094Slling 		    spa->spa_pool_props_object,
5235094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
5245094Slling 		spa->spa_bootfs = 0;
5255094Slling 	}
5265094Slling }
5275094Slling 
528789Sahrens /*
529789Sahrens  * ==========================================================================
530789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
531789Sahrens  * ==========================================================================
532789Sahrens  */
533789Sahrens 
5341544Seschrock static int
5351544Seschrock spa_error_entry_compare(const void *a, const void *b)
5361544Seschrock {
5371544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
5381544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
5391544Seschrock 	int ret;
5401544Seschrock 
5411544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
5421544Seschrock 	    sizeof (zbookmark_t));
5431544Seschrock 
5441544Seschrock 	if (ret < 0)
5451544Seschrock 		return (-1);
5461544Seschrock 	else if (ret > 0)
5471544Seschrock 		return (1);
5481544Seschrock 	else
5491544Seschrock 		return (0);
5501544Seschrock }
5511544Seschrock 
5521544Seschrock /*
5531544Seschrock  * Utility function which retrieves copies of the current logs and
5541544Seschrock  * re-initializes them in the process.
5551544Seschrock  */
5561544Seschrock void
5571544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
5581544Seschrock {
5591544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
5601544Seschrock 
5611544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
5621544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
5631544Seschrock 
5641544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5651544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5661544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5671544Seschrock 	avl_create(&spa->spa_errlist_last,
5681544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5691544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5701544Seschrock }
5711544Seschrock 
572789Sahrens /*
573789Sahrens  * Activate an uninitialized pool.
574789Sahrens  */
575789Sahrens static void
5768241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode)
577789Sahrens {
578789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
579789Sahrens 
580789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
5818241SJeff.Bonwick@Sun.COM 	spa->spa_mode = mode;
582789Sahrens 
58310594SGeorge.Wilson@Sun.COM 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
58410594SGeorge.Wilson@Sun.COM 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
585789Sahrens 
5867754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
5879515SJonathan.Adams@Sun.COM 		const zio_taskq_info_t *ztip = &zio_taskqs[t];
5887754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5899515SJonathan.Adams@Sun.COM 			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
5909515SJonathan.Adams@Sun.COM 			uint_t value = ztip->zti_nthreads[q].zti_value;
5919515SJonathan.Adams@Sun.COM 			char name[32];
5929515SJonathan.Adams@Sun.COM 
5939515SJonathan.Adams@Sun.COM 			(void) snprintf(name, sizeof (name),
5949515SJonathan.Adams@Sun.COM 			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
5959515SJonathan.Adams@Sun.COM 
5969515SJonathan.Adams@Sun.COM 			if (mode == zti_mode_tune) {
5979515SJonathan.Adams@Sun.COM 				mode = zio_taskq_tune_mode;
5989515SJonathan.Adams@Sun.COM 				value = zio_taskq_tune_value;
5999515SJonathan.Adams@Sun.COM 				if (mode == zti_mode_tune)
6009515SJonathan.Adams@Sun.COM 					mode = zti_mode_online_percent;
6019515SJonathan.Adams@Sun.COM 			}
6029515SJonathan.Adams@Sun.COM 
6039515SJonathan.Adams@Sun.COM 			switch (mode) {
6049515SJonathan.Adams@Sun.COM 			case zti_mode_fixed:
6059515SJonathan.Adams@Sun.COM 				ASSERT3U(value, >=, 1);
6069515SJonathan.Adams@Sun.COM 				value = MAX(value, 1);
6079515SJonathan.Adams@Sun.COM 
6089515SJonathan.Adams@Sun.COM 				spa->spa_zio_taskq[t][q] = taskq_create(name,
6099515SJonathan.Adams@Sun.COM 				    value, maxclsyspri, 50, INT_MAX,
6109515SJonathan.Adams@Sun.COM 				    TASKQ_PREPOPULATE);
6119515SJonathan.Adams@Sun.COM 				break;
6129515SJonathan.Adams@Sun.COM 
6139515SJonathan.Adams@Sun.COM 			case zti_mode_online_percent:
6149515SJonathan.Adams@Sun.COM 				spa->spa_zio_taskq[t][q] = taskq_create(name,
6159515SJonathan.Adams@Sun.COM 				    value, maxclsyspri, 50, INT_MAX,
6169515SJonathan.Adams@Sun.COM 				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
6179515SJonathan.Adams@Sun.COM 				break;
6189515SJonathan.Adams@Sun.COM 
6199515SJonathan.Adams@Sun.COM 			case zti_mode_tune:
6209515SJonathan.Adams@Sun.COM 			default:
6219515SJonathan.Adams@Sun.COM 				panic("unrecognized mode for "
6229515SJonathan.Adams@Sun.COM 				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
6239515SJonathan.Adams@Sun.COM 				    "in spa_activate()",
6249515SJonathan.Adams@Sun.COM 				    t, q, mode, value);
6259515SJonathan.Adams@Sun.COM 				break;
6269515SJonathan.Adams@Sun.COM 			}
6277754SJeff.Bonwick@Sun.COM 		}
628789Sahrens 	}
629789Sahrens 
6307754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
6317754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
6327754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
6337754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
634789Sahrens 
635789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
636789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
6371544Seschrock 
6381544Seschrock 	avl_create(&spa->spa_errlist_scrub,
6391544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6401544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
6411544Seschrock 	avl_create(&spa->spa_errlist_last,
6421544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6431544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
644789Sahrens }
645789Sahrens 
646789Sahrens /*
647789Sahrens  * Opposite of spa_activate().
648789Sahrens  */
649789Sahrens static void
650789Sahrens spa_deactivate(spa_t *spa)
651789Sahrens {
652789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
653789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
654789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
6559630SJeff.Bonwick@Sun.COM 	ASSERT(spa->spa_async_zio_root == NULL);
656789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
657789Sahrens 
658789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
659789Sahrens 
6607754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
6617754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
6627754SJeff.Bonwick@Sun.COM 
6637754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
6647754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
6657754SJeff.Bonwick@Sun.COM 			taskq_destroy(spa->spa_zio_taskq[t][q]);
6667754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
6677754SJeff.Bonwick@Sun.COM 		}
668789Sahrens 	}
669789Sahrens 
670789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
671789Sahrens 	spa->spa_normal_class = NULL;
672789Sahrens 
6734527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
6744527Sperrin 	spa->spa_log_class = NULL;
6754527Sperrin 
6761544Seschrock 	/*
6771544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
6781544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
6791544Seschrock 	 */
6801544Seschrock 	spa_errlog_drain(spa);
6811544Seschrock 
6821544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
6831544Seschrock 	avl_destroy(&spa->spa_errlist_last);
6841544Seschrock 
685789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
686789Sahrens }
687789Sahrens 
688789Sahrens /*
689789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
690789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
691789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
692789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
693789Sahrens  */
6942082Seschrock static int
6952082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
6962082Seschrock     uint_t id, int atype)
697789Sahrens {
698789Sahrens 	nvlist_t **child;
6999816SGeorge.Wilson@Sun.COM 	uint_t children;
7002082Seschrock 	int error;
7012082Seschrock 
7022082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
7032082Seschrock 		return (error);
7042082Seschrock 
7052082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
7062082Seschrock 		return (0);
707789Sahrens 
7087754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
7097754SJeff.Bonwick@Sun.COM 	    &child, &children);
7107754SJeff.Bonwick@Sun.COM 
7117754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
7127754SJeff.Bonwick@Sun.COM 		return (0);
7137754SJeff.Bonwick@Sun.COM 
7147754SJeff.Bonwick@Sun.COM 	if (error) {
7152082Seschrock 		vdev_free(*vdp);
7162082Seschrock 		*vdp = NULL;
7172082Seschrock 		return (EINVAL);
718789Sahrens 	}
719789Sahrens 
7209816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < children; c++) {
7212082Seschrock 		vdev_t *vd;
7222082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
7232082Seschrock 		    atype)) != 0) {
7242082Seschrock 			vdev_free(*vdp);
7252082Seschrock 			*vdp = NULL;
7262082Seschrock 			return (error);
727789Sahrens 		}
728789Sahrens 	}
729789Sahrens 
7302082Seschrock 	ASSERT(*vdp != NULL);
7312082Seschrock 
7322082Seschrock 	return (0);
733789Sahrens }
734789Sahrens 
735789Sahrens /*
736789Sahrens  * Opposite of spa_load().
737789Sahrens  */
738789Sahrens static void
739789Sahrens spa_unload(spa_t *spa)
740789Sahrens {
7412082Seschrock 	int i;
7422082Seschrock 
7437754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
7447754SJeff.Bonwick@Sun.COM 
745789Sahrens 	/*
7461544Seschrock 	 * Stop async tasks.
7471544Seschrock 	 */
7481544Seschrock 	spa_async_suspend(spa);
7491544Seschrock 
7501544Seschrock 	/*
751789Sahrens 	 * Stop syncing.
752789Sahrens 	 */
753789Sahrens 	if (spa->spa_sync_on) {
754789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
755789Sahrens 		spa->spa_sync_on = B_FALSE;
756789Sahrens 	}
757789Sahrens 
758789Sahrens 	/*
7597754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
760789Sahrens 	 */
7619234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root != NULL) {
7629234SGeorge.Wilson@Sun.COM 		(void) zio_wait(spa->spa_async_zio_root);
7639234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = NULL;
7649234SGeorge.Wilson@Sun.COM 	}
765789Sahrens 
766789Sahrens 	/*
767789Sahrens 	 * Close the dsl pool.
768789Sahrens 	 */
769789Sahrens 	if (spa->spa_dsl_pool) {
770789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
771789Sahrens 		spa->spa_dsl_pool = NULL;
772789Sahrens 	}
773789Sahrens 
7748241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7758241SJeff.Bonwick@Sun.COM 
7768241SJeff.Bonwick@Sun.COM 	/*
7778241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
7788241SJeff.Bonwick@Sun.COM 	 */
7798241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
7808241SJeff.Bonwick@Sun.COM 
781789Sahrens 	/*
782789Sahrens 	 * Close all vdevs.
783789Sahrens 	 */
7841585Sbonwick 	if (spa->spa_root_vdev)
785789Sahrens 		vdev_free(spa->spa_root_vdev);
7861585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
7871544Seschrock 
7885450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7895450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
7905450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
7915450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7925450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7935450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
7945450Sbrendan 	}
7955450Sbrendan 	if (spa->spa_spares.sav_config) {
7965450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
7975450Sbrendan 		spa->spa_spares.sav_config = NULL;
7982082Seschrock 	}
7997377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
8005450Sbrendan 
8015450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
8025450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
8035450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
8045450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
8055450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
8065450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
8075450Sbrendan 	}
8085450Sbrendan 	if (spa->spa_l2cache.sav_config) {
8095450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
8105450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
8112082Seschrock 	}
8127377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
8132082Seschrock 
8141544Seschrock 	spa->spa_async_suspended = 0;
8158241SJeff.Bonwick@Sun.COM 
8168241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
817789Sahrens }
818789Sahrens 
819789Sahrens /*
8202082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
8212082Seschrock  * this pool.  When this is called, we have some form of basic information in
8225450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
8235450Sbrendan  * then re-generate a more complete list including status information.
8242082Seschrock  */
8252082Seschrock static void
8262082Seschrock spa_load_spares(spa_t *spa)
8272082Seschrock {
8282082Seschrock 	nvlist_t **spares;
8292082Seschrock 	uint_t nspares;
8302082Seschrock 	int i;
8313377Seschrock 	vdev_t *vd, *tvd;
8322082Seschrock 
8337754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
8347754SJeff.Bonwick@Sun.COM 
8352082Seschrock 	/*
8362082Seschrock 	 * First, close and free any existing spare vdevs.
8372082Seschrock 	 */
8385450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8395450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
8403377Seschrock 
8413377Seschrock 		/* Undo the call to spa_activate() below */
8426643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8436643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
8443377Seschrock 			spa_spare_remove(tvd);
8453377Seschrock 		vdev_close(vd);
8463377Seschrock 		vdev_free(vd);
8472082Seschrock 	}
8483377Seschrock 
8495450Sbrendan 	if (spa->spa_spares.sav_vdevs)
8505450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
8515450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
8525450Sbrendan 
8535450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
8542082Seschrock 		nspares = 0;
8552082Seschrock 	else
8565450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
8572082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
8582082Seschrock 
8595450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
8605450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
8612082Seschrock 
8622082Seschrock 	if (nspares == 0)
8632082Seschrock 		return;
8642082Seschrock 
8652082Seschrock 	/*
8662082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
8673377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
8683377Seschrock 	 * structures associated with it: one in the list of spares (used only
8693377Seschrock 	 * for basic validation purposes) and one in the active vdev
8703377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
8713377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
8723377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
8732082Seschrock 	 */
8745450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
8755450Sbrendan 	    KM_SLEEP);
8765450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8772082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
8782082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
8792082Seschrock 		ASSERT(vd != NULL);
8802082Seschrock 
8815450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
8822082Seschrock 
8836643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8846643Seschrock 		    B_FALSE)) != NULL) {
8853377Seschrock 			if (!tvd->vdev_isspare)
8863377Seschrock 				spa_spare_add(tvd);
8873377Seschrock 
8883377Seschrock 			/*
8893377Seschrock 			 * We only mark the spare active if we were successfully
8903377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
8913377Seschrock 			 * with a bad active spare would result in strange
8923377Seschrock 			 * behavior, because multiple pool would think the spare
8933377Seschrock 			 * is actively in use.
8943377Seschrock 			 *
8953377Seschrock 			 * There is a vulnerability here to an equally bizarre
8963377Seschrock 			 * circumstance, where a dead active spare is later
8973377Seschrock 			 * brought back to life (onlined or otherwise).  Given
8983377Seschrock 			 * the rarity of this scenario, and the extra complexity
8993377Seschrock 			 * it adds, we ignore the possibility.
9003377Seschrock 			 */
9013377Seschrock 			if (!vdev_is_dead(tvd))
9023377Seschrock 				spa_spare_activate(tvd);
9033377Seschrock 		}
9043377Seschrock 
9057754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
9069425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
9077754SJeff.Bonwick@Sun.COM 
9082082Seschrock 		if (vdev_open(vd) != 0)
9092082Seschrock 			continue;
9102082Seschrock 
9115450Sbrendan 		if (vdev_validate_aux(vd) == 0)
9125450Sbrendan 			spa_spare_add(vd);
9132082Seschrock 	}
9142082Seschrock 
9152082Seschrock 	/*
9162082Seschrock 	 * Recompute the stashed list of spares, with status information
9172082Seschrock 	 * this time.
9182082Seschrock 	 */
9195450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
9202082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9212082Seschrock 
9225450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
9235450Sbrendan 	    KM_SLEEP);
9245450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9255450Sbrendan 		spares[i] = vdev_config_generate(spa,
9265450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
9275450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
9285450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
9295450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9302082Seschrock 		nvlist_free(spares[i]);
9315450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
9325450Sbrendan }
9335450Sbrendan 
9345450Sbrendan /*
9355450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
9365450Sbrendan  * this pool.  When this is called, we have some form of basic information in
9375450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
9385450Sbrendan  * then re-generate a more complete list including status information.
9395450Sbrendan  * Devices which are already active have their details maintained, and are
9405450Sbrendan  * not re-opened.
9415450Sbrendan  */
9425450Sbrendan static void
9435450Sbrendan spa_load_l2cache(spa_t *spa)
9445450Sbrendan {
9455450Sbrendan 	nvlist_t **l2cache;
9465450Sbrendan 	uint_t nl2cache;
9475450Sbrendan 	int i, j, oldnvdevs;
9489816SGeorge.Wilson@Sun.COM 	uint64_t guid;
9495450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
9505450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
9515450Sbrendan 
9527754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
9537754SJeff.Bonwick@Sun.COM 
9545450Sbrendan 	if (sav->sav_config != NULL) {
9555450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
9565450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
9575450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
9585450Sbrendan 	} else {
9595450Sbrendan 		nl2cache = 0;
9605450Sbrendan 	}
9615450Sbrendan 
9625450Sbrendan 	oldvdevs = sav->sav_vdevs;
9635450Sbrendan 	oldnvdevs = sav->sav_count;
9645450Sbrendan 	sav->sav_vdevs = NULL;
9655450Sbrendan 	sav->sav_count = 0;
9665450Sbrendan 
9675450Sbrendan 	/*
9685450Sbrendan 	 * Process new nvlist of vdevs.
9695450Sbrendan 	 */
9705450Sbrendan 	for (i = 0; i < nl2cache; i++) {
9715450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
9725450Sbrendan 		    &guid) == 0);
9735450Sbrendan 
9745450Sbrendan 		newvdevs[i] = NULL;
9755450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
9765450Sbrendan 			vd = oldvdevs[j];
9775450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
9785450Sbrendan 				/*
9795450Sbrendan 				 * Retain previous vdev for add/remove ops.
9805450Sbrendan 				 */
9815450Sbrendan 				newvdevs[i] = vd;
9825450Sbrendan 				oldvdevs[j] = NULL;
9835450Sbrendan 				break;
9845450Sbrendan 			}
9855450Sbrendan 		}
9865450Sbrendan 
9875450Sbrendan 		if (newvdevs[i] == NULL) {
9885450Sbrendan 			/*
9895450Sbrendan 			 * Create new vdev
9905450Sbrendan 			 */
9915450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
9925450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
9935450Sbrendan 			ASSERT(vd != NULL);
9945450Sbrendan 			newvdevs[i] = vd;
9955450Sbrendan 
9965450Sbrendan 			/*
9975450Sbrendan 			 * Commit this vdev as an l2cache device,
9985450Sbrendan 			 * even if it fails to open.
9995450Sbrendan 			 */
10005450Sbrendan 			spa_l2cache_add(vd);
10015450Sbrendan 
10026643Seschrock 			vd->vdev_top = vd;
10036643Seschrock 			vd->vdev_aux = sav;
10046643Seschrock 
10056643Seschrock 			spa_l2cache_activate(vd);
10066643Seschrock 
10075450Sbrendan 			if (vdev_open(vd) != 0)
10085450Sbrendan 				continue;
10095450Sbrendan 
10105450Sbrendan 			(void) vdev_validate_aux(vd);
10115450Sbrendan 
10129816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
10139816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
10145450Sbrendan 		}
10155450Sbrendan 	}
10165450Sbrendan 
10175450Sbrendan 	/*
10185450Sbrendan 	 * Purge vdevs that were dropped
10195450Sbrendan 	 */
10205450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
10215450Sbrendan 		uint64_t pool;
10225450Sbrendan 
10235450Sbrendan 		vd = oldvdevs[i];
10245450Sbrendan 		if (vd != NULL) {
10258241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
10268241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
10275450Sbrendan 				l2arc_remove_vdev(vd);
10285450Sbrendan 			(void) vdev_close(vd);
10295450Sbrendan 			spa_l2cache_remove(vd);
10305450Sbrendan 		}
10315450Sbrendan 	}
10325450Sbrendan 
10335450Sbrendan 	if (oldvdevs)
10345450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
10355450Sbrendan 
10365450Sbrendan 	if (sav->sav_config == NULL)
10375450Sbrendan 		goto out;
10385450Sbrendan 
10395450Sbrendan 	sav->sav_vdevs = newvdevs;
10405450Sbrendan 	sav->sav_count = (int)nl2cache;
10415450Sbrendan 
10425450Sbrendan 	/*
10435450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
10445450Sbrendan 	 * information this time.
10455450Sbrendan 	 */
10465450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
10475450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
10485450Sbrendan 
10495450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
10505450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
10515450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
10525450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
10535450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
10545450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
10555450Sbrendan out:
10565450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
10575450Sbrendan 		nvlist_free(l2cache[i]);
10585450Sbrendan 	if (sav->sav_count)
10595450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
10602082Seschrock }
10612082Seschrock 
10622082Seschrock static int
10632082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
10642082Seschrock {
10652082Seschrock 	dmu_buf_t *db;
10662082Seschrock 	char *packed = NULL;
10672082Seschrock 	size_t nvsize = 0;
10682082Seschrock 	int error;
10692082Seschrock 	*value = NULL;
10702082Seschrock 
10712082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
10722082Seschrock 	nvsize = *(uint64_t *)db->db_data;
10732082Seschrock 	dmu_buf_rele(db, FTAG);
10742082Seschrock 
10752082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
10769512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
10779512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
10782082Seschrock 	if (error == 0)
10792082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
10802082Seschrock 	kmem_free(packed, nvsize);
10812082Seschrock 
10822082Seschrock 	return (error);
10832082Seschrock }
10842082Seschrock 
10852082Seschrock /*
10864451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
10874451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
10884451Seschrock  */
10894451Seschrock static void
10904451Seschrock spa_check_removed(vdev_t *vd)
10914451Seschrock {
10929816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
10934451Seschrock 		spa_check_removed(vd->vdev_child[c]);
10944451Seschrock 
10954451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
10964451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
10974451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
10984451Seschrock 	}
10994451Seschrock }
11004451Seschrock 
11014451Seschrock /*
11029701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
11039701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
11049701SGeorge.Wilson@Sun.COM  */
11059701SGeorge.Wilson@Sun.COM void
110610594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
11079701SGeorge.Wilson@Sun.COM {
110810594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
110910594SGeorge.Wilson@Sun.COM 
111010594SGeorge.Wilson@Sun.COM 	/*
111110594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
111210594SGeorge.Wilson@Sun.COM 	 */
111310594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
111410594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
111510594SGeorge.Wilson@Sun.COM 
111610594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
111710594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
111810594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
111910594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
11209701SGeorge.Wilson@Sun.COM 	}
112110594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
112210594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
11239701SGeorge.Wilson@Sun.COM }
11249701SGeorge.Wilson@Sun.COM 
11259701SGeorge.Wilson@Sun.COM /*
11267294Sperrin  * Check for missing log devices
11277294Sperrin  */
11287294Sperrin int
11297294Sperrin spa_check_logs(spa_t *spa)
11307294Sperrin {
11317294Sperrin 	switch (spa->spa_log_state) {
11327294Sperrin 	case SPA_LOG_MISSING:
11337294Sperrin 		/* need to recheck in case slog has been restored */
11347294Sperrin 	case SPA_LOG_UNKNOWN:
11357294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
11367294Sperrin 		    DS_FIND_CHILDREN)) {
11377294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
11387294Sperrin 			return (1);
11397294Sperrin 		}
11407294Sperrin 		break;
11417294Sperrin 	}
11427294Sperrin 	return (0);
11437294Sperrin }
11447294Sperrin 
114510672SEric.Schrock@Sun.COM static void
114610672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
114710672SEric.Schrock@Sun.COM {
114810672SEric.Schrock@Sun.COM 	int i;
114910672SEric.Schrock@Sun.COM 
115010672SEric.Schrock@Sun.COM 	for (i = 0; i < sav->sav_count; i++)
115110672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
115210672SEric.Schrock@Sun.COM }
115310672SEric.Schrock@Sun.COM 
1154*10921STim.Haley@Sun.COM typedef struct spa_load_error {
1155*10921STim.Haley@Sun.COM 	uint64_t	sle_metadata_count;
1156*10921STim.Haley@Sun.COM 	uint64_t	sle_data_count;
1157*10921STim.Haley@Sun.COM } spa_load_error_t;
1158*10921STim.Haley@Sun.COM 
1159*10921STim.Haley@Sun.COM static void
1160*10921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio)
1161*10921STim.Haley@Sun.COM {
1162*10921STim.Haley@Sun.COM 	blkptr_t *bp = zio->io_bp;
1163*10921STim.Haley@Sun.COM 	spa_load_error_t *sle = zio->io_private;
1164*10921STim.Haley@Sun.COM 	dmu_object_type_t type = BP_GET_TYPE(bp);
1165*10921STim.Haley@Sun.COM 	int error = zio->io_error;
1166*10921STim.Haley@Sun.COM 
1167*10921STim.Haley@Sun.COM 	if (error) {
1168*10921STim.Haley@Sun.COM 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1169*10921STim.Haley@Sun.COM 		    type != DMU_OT_INTENT_LOG)
1170*10921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_metadata_count, 1);
1171*10921STim.Haley@Sun.COM 		else
1172*10921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_data_count, 1);
1173*10921STim.Haley@Sun.COM 	}
1174*10921STim.Haley@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
1175*10921STim.Haley@Sun.COM }
1176*10921STim.Haley@Sun.COM 
1177*10921STim.Haley@Sun.COM /*ARGSUSED*/
1178*10921STim.Haley@Sun.COM static int
1179*10921STim.Haley@Sun.COM spa_load_verify_cb(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
1180*10921STim.Haley@Sun.COM     const dnode_phys_t *dnp, void *arg)
1181*10921STim.Haley@Sun.COM {
1182*10921STim.Haley@Sun.COM 	if (bp != NULL) {
1183*10921STim.Haley@Sun.COM 		zio_t *rio = arg;
1184*10921STim.Haley@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
1185*10921STim.Haley@Sun.COM 		void *data = zio_data_buf_alloc(size);
1186*10921STim.Haley@Sun.COM 
1187*10921STim.Haley@Sun.COM 		zio_nowait(zio_read(rio, spa, bp, data, size,
1188*10921STim.Haley@Sun.COM 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1189*10921STim.Haley@Sun.COM 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1190*10921STim.Haley@Sun.COM 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1191*10921STim.Haley@Sun.COM 	}
1192*10921STim.Haley@Sun.COM 	return (0);
1193*10921STim.Haley@Sun.COM }
1194*10921STim.Haley@Sun.COM 
1195*10921STim.Haley@Sun.COM static int
1196*10921STim.Haley@Sun.COM spa_load_verify(spa_t *spa)
1197*10921STim.Haley@Sun.COM {
1198*10921STim.Haley@Sun.COM 	zio_t *rio;
1199*10921STim.Haley@Sun.COM 	spa_load_error_t sle = { 0 };
1200*10921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
1201*10921STim.Haley@Sun.COM 	boolean_t verify_ok = B_FALSE;
1202*10921STim.Haley@Sun.COM 	int error;
1203*10921STim.Haley@Sun.COM 
1204*10921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
1205*10921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1206*10921STim.Haley@Sun.COM 
1207*10921STim.Haley@Sun.COM 	error = traverse_pool(spa, spa_load_verify_cb, rio,
1208*10921STim.Haley@Sun.COM 	    spa->spa_verify_min_txg);
1209*10921STim.Haley@Sun.COM 
1210*10921STim.Haley@Sun.COM 	(void) zio_wait(rio);
1211*10921STim.Haley@Sun.COM 
1212*10921STim.Haley@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
1213*10921STim.Haley@Sun.COM 
1214*10921STim.Haley@Sun.COM 	spa->spa_load_meta_errors = sle.sle_metadata_count;
1215*10921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
1216*10921STim.Haley@Sun.COM 
1217*10921STim.Haley@Sun.COM 	if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta &&
1218*10921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
1219*10921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
1220*10921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1221*10921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1222*10921STim.Haley@Sun.COM 	}
1223*10921STim.Haley@Sun.COM 
1224*10921STim.Haley@Sun.COM 	if (error) {
1225*10921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
1226*10921STim.Haley@Sun.COM 			error = EIO;
1227*10921STim.Haley@Sun.COM 		return (error);
1228*10921STim.Haley@Sun.COM 	}
1229*10921STim.Haley@Sun.COM 
1230*10921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
1231*10921STim.Haley@Sun.COM }
1232*10921STim.Haley@Sun.COM 
12337294Sperrin /*
1234789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
12351544Seschrock  * source of configuration information.
1236789Sahrens  */
1237789Sahrens static int
1238*10921STim.Haley@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, int mosconfig)
1239789Sahrens {
1240789Sahrens 	int error = 0;
124110594SGeorge.Wilson@Sun.COM 	nvlist_t *nvconfig, *nvroot = NULL;
1242789Sahrens 	vdev_t *rvd;
1243789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
12441635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1245789Sahrens 	uint64_t pool_guid;
12462082Seschrock 	uint64_t version;
12474451Seschrock 	uint64_t autoreplace = 0;
12488241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
12497294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
1250*10921STim.Haley@Sun.COM 	nvlist_t *config = spa->spa_config;
1251789Sahrens 
12528241SJeff.Bonwick@Sun.COM 	/*
12538241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
12548241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
12558241SJeff.Bonwick@Sun.COM 	 */
12568241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
12578241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
12588241SJeff.Bonwick@Sun.COM 
12597754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
12607754SJeff.Bonwick@Sun.COM 
12611544Seschrock 	spa->spa_load_state = state;
12621635Sbonwick 
1263789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
12641733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
12651544Seschrock 		error = EINVAL;
12661544Seschrock 		goto out;
12671544Seschrock 	}
1268789Sahrens 
12692082Seschrock 	/*
12702082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
12712082Seschrock 	 * it's not present treat it as the initial version.
12722082Seschrock 	 */
12732082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
12744577Sahrens 		version = SPA_VERSION_INITIAL;
12752082Seschrock 
12761733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
12771733Sbonwick 	    &spa->spa_config_txg);
12781733Sbonwick 
12791635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
12801544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
12811544Seschrock 		error = EEXIST;
12821544Seschrock 		goto out;
12831544Seschrock 	}
1284789Sahrens 
12852174Seschrock 	spa->spa_load_guid = pool_guid;
12862174Seschrock 
1287789Sahrens 	/*
12889234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
12899234SGeorge.Wilson@Sun.COM 	 */
12909630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
12919630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
12929234SGeorge.Wilson@Sun.COM 
12939234SGeorge.Wilson@Sun.COM 	/*
12942082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
12952082Seschrock 	 * value that will be returned by spa_version() since parsing the
12962082Seschrock 	 * configuration requires knowing the version number.
1297789Sahrens 	 */
12987754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12992082Seschrock 	spa->spa_ubsync.ub_version = version;
13002082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
13017754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1302789Sahrens 
13032082Seschrock 	if (error != 0)
13041544Seschrock 		goto out;
1305789Sahrens 
13061585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1307789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1308789Sahrens 
1309789Sahrens 	/*
1310789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1311789Sahrens 	 */
13127754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13134070Smc142369 	error = vdev_open(rvd);
13147754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13154070Smc142369 	if (error != 0)
13161544Seschrock 		goto out;
1317789Sahrens 
1318789Sahrens 	/*
13199276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
13209276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
13219276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
13229276SMark.Musante@Sun.COM 	 * that config. Otherwise, we're validating against the cached config
13239276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
13249276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
13259276SMark.Musante@Sun.COM 	 * the vdev config.
13261986Seschrock 	 */
13279276SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13289276SMark.Musante@Sun.COM 	error = vdev_validate(rvd);
13299276SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13309276SMark.Musante@Sun.COM 	if (error != 0)
13319276SMark.Musante@Sun.COM 		goto out;
13321986Seschrock 
13331986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13341986Seschrock 		error = ENXIO;
13351986Seschrock 		goto out;
13361986Seschrock 	}
13371986Seschrock 
13381986Seschrock 	/*
1339789Sahrens 	 * Find the best uberblock.
1340789Sahrens 	 */
13417754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1342789Sahrens 
1343789Sahrens 	/*
1344789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1345789Sahrens 	 */
1346789Sahrens 	if (ub->ub_txg == 0) {
13471760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13481760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13491544Seschrock 		error = ENXIO;
13501544Seschrock 		goto out;
13511544Seschrock 	}
13521544Seschrock 
13531544Seschrock 	/*
13541544Seschrock 	 * If the pool is newer than the code, we can't open it.
13551544Seschrock 	 */
13564577Sahrens 	if (ub->ub_version > SPA_VERSION) {
13571760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13581760Seschrock 		    VDEV_AUX_VERSION_NEWER);
13591544Seschrock 		error = ENOTSUP;
13601544Seschrock 		goto out;
1361789Sahrens 	}
1362789Sahrens 
1363789Sahrens 	/*
1364789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1365789Sahrens 	 * incomplete configuration.
1366789Sahrens 	 */
13671732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
13681544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13691544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
13701544Seschrock 		error = ENXIO;
13711544Seschrock 		goto out;
1372789Sahrens 	}
1373789Sahrens 
1374789Sahrens 	/*
1375789Sahrens 	 * Initialize internal SPA structures.
1376789Sahrens 	 */
1377789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1378789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1379*10921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
1380*10921STim.Haley@Sun.COM 	    TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE;
1381*10921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
1382*10921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
13831544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
13841544Seschrock 	if (error) {
13851544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13861544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1387*10921STim.Haley@Sun.COM 		error = EIO;
13881544Seschrock 		goto out;
13891544Seschrock 	}
1390789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1391789Sahrens 
13921544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1393789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
13941544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
13951544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13961544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13971544Seschrock 		error = EIO;
13981544Seschrock 		goto out;
13991544Seschrock 	}
1400789Sahrens 
140110594SGeorge.Wilson@Sun.COM 	if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) {
140210594SGeorge.Wilson@Sun.COM 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
140310594SGeorge.Wilson@Sun.COM 		    VDEV_AUX_CORRUPT_DATA);
140410594SGeorge.Wilson@Sun.COM 		error = EIO;
140510594SGeorge.Wilson@Sun.COM 		goto out;
140610594SGeorge.Wilson@Sun.COM 	}
140710594SGeorge.Wilson@Sun.COM 
1408789Sahrens 	if (!mosconfig) {
14093975Sek110237 		uint64_t hostid;
14102082Seschrock 
141110594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
14127706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
14133975Sek110237 			char *hostname;
14143975Sek110237 			unsigned long myhostid = 0;
14153975Sek110237 
141610594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
14173975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
14183975Sek110237 
14198662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
14208662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
14218662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
14228662SJordan.Vaughan@Sun.com 			/*
14238662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
14248662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
14258662SJordan.Vaughan@Sun.com 			 */
14263975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
14278662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
14284178Slling 			if (hostid != 0 && myhostid != 0 &&
14298662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
14303975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
14313975Sek110237 				    "loaded as it was last accessed by "
14327706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
14333975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
14347754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
14353975Sek110237 				    (unsigned long)hostid);
14363975Sek110237 				error = EBADF;
14373975Sek110237 				goto out;
14383975Sek110237 			}
14393975Sek110237 		}
14403975Sek110237 
144110594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1442789Sahrens 		spa_unload(spa);
1443789Sahrens 		spa_deactivate(spa);
14448241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1445789Sahrens 
1446*10921STim.Haley@Sun.COM 		return (spa_load(spa, state, B_TRUE));
14471544Seschrock 	}
14481544Seschrock 
14491544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
14501544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
14511544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
14521544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14531544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14541544Seschrock 		error = EIO;
14551544Seschrock 		goto out;
1456789Sahrens 	}
1457789Sahrens 
14581544Seschrock 	/*
14592082Seschrock 	 * Load the bit that tells us to use the new accounting function
14602082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
14612082Seschrock 	 * be present.
14622082Seschrock 	 */
14632082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
14642082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
14652082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
14662082Seschrock 	if (error != 0 && error != ENOENT) {
14672082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14682082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14692082Seschrock 		error = EIO;
14702082Seschrock 		goto out;
14712082Seschrock 	}
14722082Seschrock 
14732082Seschrock 	/*
14741544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
14751544Seschrock 	 * not be present.
14761544Seschrock 	 */
14771544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
14781544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
14791544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
14801807Sbonwick 	if (error != 0 && error != ENOENT) {
14811544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14821544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14831544Seschrock 		error = EIO;
14841544Seschrock 		goto out;
14851544Seschrock 	}
14861544Seschrock 
14871544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
14881544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
14891544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
14901544Seschrock 	if (error != 0 && error != ENOENT) {
14911544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14921544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14931544Seschrock 		error = EIO;
14941544Seschrock 		goto out;
14951544Seschrock 	}
1496789Sahrens 
1497789Sahrens 	/*
14982926Sek110237 	 * Load the history object.  If we have an older pool, this
14992926Sek110237 	 * will not be present.
15002926Sek110237 	 */
15012926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
15022926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
15032926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
15042926Sek110237 	if (error != 0 && error != ENOENT) {
15052926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15062926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
15072926Sek110237 		error = EIO;
15082926Sek110237 		goto out;
15092926Sek110237 	}
15102926Sek110237 
15112926Sek110237 	/*
15122082Seschrock 	 * Load any hot spares for this pool.
15132082Seschrock 	 */
15142082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15155450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
15162082Seschrock 	if (error != 0 && error != ENOENT) {
15172082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15182082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
15192082Seschrock 		error = EIO;
15202082Seschrock 		goto out;
15212082Seschrock 	}
15222082Seschrock 	if (error == 0) {
15234577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
15245450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
15255450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
15262082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15272082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
15282082Seschrock 			error = EIO;
15292082Seschrock 			goto out;
15302082Seschrock 		}
15312082Seschrock 
15327754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
15332082Seschrock 		spa_load_spares(spa);
15347754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
15352082Seschrock 	}
15362082Seschrock 
15375450Sbrendan 	/*
15385450Sbrendan 	 * Load any level 2 ARC devices for this pool.
15395450Sbrendan 	 */
15405450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15415450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
15425450Sbrendan 	    &spa->spa_l2cache.sav_object);
15435450Sbrendan 	if (error != 0 && error != ENOENT) {
15445450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15455450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
15465450Sbrendan 		error = EIO;
15475450Sbrendan 		goto out;
15485450Sbrendan 	}
15495450Sbrendan 	if (error == 0) {
15505450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
15515450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
15525450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
15535450Sbrendan 			vdev_set_state(rvd, B_TRUE,
15545450Sbrendan 			    VDEV_STATE_CANT_OPEN,
15555450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
15565450Sbrendan 			error = EIO;
15575450Sbrendan 			goto out;
15585450Sbrendan 		}
15595450Sbrendan 
15607754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
15615450Sbrendan 		spa_load_l2cache(spa);
15627754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
15635450Sbrendan 	}
15645450Sbrendan 
156510594SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
156610594SGeorge.Wilson@Sun.COM 	    &nvroot) == 0);
156710594SGeorge.Wilson@Sun.COM 	spa_load_log_state(spa, nvroot);
156810594SGeorge.Wilson@Sun.COM 	nvlist_free(nvconfig);
15699701SGeorge.Wilson@Sun.COM 
15707294Sperrin 	if (spa_check_logs(spa)) {
15717294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15727294Sperrin 		    VDEV_AUX_BAD_LOG);
15737294Sperrin 		error = ENXIO;
15747294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
15757294Sperrin 		goto out;
15767294Sperrin 	}
15777294Sperrin 
15787294Sperrin 
15795094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
15804543Smarks 
15813912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
15823912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
15833912Slling 
15843912Slling 	if (error && error != ENOENT) {
15853912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15863912Slling 		    VDEV_AUX_CORRUPT_DATA);
15873912Slling 		error = EIO;
15883912Slling 		goto out;
15893912Slling 	}
15903912Slling 
15913912Slling 	if (error == 0) {
15923912Slling 		(void) zap_lookup(spa->spa_meta_objset,
15933912Slling 		    spa->spa_pool_props_object,
15944451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
15953912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
15964451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
15974451Seschrock 		    spa->spa_pool_props_object,
15984451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
15994451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
160010672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
16014543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
16024543Smarks 		    spa->spa_pool_props_object,
16034543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
16044543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
16055329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
16065329Sgw25295 		    spa->spa_pool_props_object,
16075329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
16085329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
16099816SGeorge.Wilson@Sun.COM 		(void) zap_lookup(spa->spa_meta_objset,
16109816SGeorge.Wilson@Sun.COM 		    spa->spa_pool_props_object,
16119816SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND),
16129816SGeorge.Wilson@Sun.COM 		    sizeof (uint64_t), 1, &spa->spa_autoexpand);
16133912Slling 	}
16143912Slling 
16152082Seschrock 	/*
16164451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
16174451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
16184451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
16194451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
16204451Seschrock 	 * over.
16214451Seschrock 	 */
162210672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
16234451Seschrock 		spa_check_removed(spa->spa_root_vdev);
162410672SEric.Schrock@Sun.COM 		/*
162510672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
162610672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
162710672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
162810672SEric.Schrock@Sun.COM 		 */
162910672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
163010672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
163110672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
163210672SEric.Schrock@Sun.COM 		}
163310672SEric.Schrock@Sun.COM 	}
16344451Seschrock 
16354451Seschrock 	/*
16361986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1637789Sahrens 	 */
16381986Seschrock 	vdev_load(rvd);
1639789Sahrens 
1640789Sahrens 	/*
1641789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1642789Sahrens 	 */
16437754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1644789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
16457754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1646789Sahrens 
1647789Sahrens 	/*
1648789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1649789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1650789Sahrens 	 */
16511544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
16521544Seschrock 		error = ENXIO;
16531544Seschrock 		goto out;
16541544Seschrock 	}
1655789Sahrens 
1656*10921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
1657*10921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
1658*10921STim.Haley@Sun.COM 		if (error) {
1659*10921STim.Haley@Sun.COM 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1660*10921STim.Haley@Sun.COM 			    VDEV_AUX_CORRUPT_DATA);
1661*10921STim.Haley@Sun.COM 			goto out;
1662*10921STim.Haley@Sun.COM 		}
1663*10921STim.Haley@Sun.COM 	}
1664*10921STim.Haley@Sun.COM 
1665*10921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
1666*10921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
16671635Sbonwick 		dmu_tx_t *tx;
16681635Sbonwick 		int need_update = B_FALSE;
16698241SJeff.Bonwick@Sun.COM 
16708241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
16711601Sbonwick 
16721635Sbonwick 		/*
16731635Sbonwick 		 * Claim log blocks that haven't been committed yet.
16741635Sbonwick 		 * This must all happen in a single txg.
1675*10921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
16761635Sbonwick 		 */
16771601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1678789Sahrens 		    spa_first_txg(spa));
16797754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
16802417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1681789Sahrens 		dmu_tx_commit(tx);
1682789Sahrens 
16839701SGeorge.Wilson@Sun.COM 		spa->spa_log_state = SPA_LOG_GOOD;
1684789Sahrens 		spa->spa_sync_on = B_TRUE;
1685789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1686789Sahrens 
1687789Sahrens 		/*
1688789Sahrens 		 * Wait for all claims to sync.
1689789Sahrens 		 */
1690789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
16911585Sbonwick 
16921585Sbonwick 		/*
16931635Sbonwick 		 * If the config cache is stale, or we have uninitialized
16941635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
169510100SLin.Ling@Sun.COM 		 *
169610100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
169710100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
16981585Sbonwick 		 */
16991635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
1700*10921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
1701*10921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
17021635Sbonwick 			need_update = B_TRUE;
17031635Sbonwick 
17048241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
17051635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
17061635Sbonwick 				need_update = B_TRUE;
17071585Sbonwick 
17081585Sbonwick 		/*
17091635Sbonwick 		 * Update the config cache asychronously in case we're the
17101635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
17111585Sbonwick 		 */
17121635Sbonwick 		if (need_update)
17131635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
17148241SJeff.Bonwick@Sun.COM 
17158241SJeff.Bonwick@Sun.COM 		/*
17168241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
17178241SJeff.Bonwick@Sun.COM 		 */
17188241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
17198241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
172010298SMatthew.Ahrens@Sun.COM 
172110298SMatthew.Ahrens@Sun.COM 		/*
172210298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
172310298SMatthew.Ahrens@Sun.COM 		 */
172410298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
172510298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
172610342Schris.kirby@sun.com 
172710342Schris.kirby@sun.com 		/*
172810342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
172910342Schris.kirby@sun.com 		 */
173010342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
1731789Sahrens 	}
1732789Sahrens 
17331544Seschrock 	error = 0;
17341544Seschrock out:
1735*10921STim.Haley@Sun.COM 
17367046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
17372082Seschrock 	if (error && error != EBADF)
17387294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
17391544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
17401544Seschrock 	spa->spa_ena = 0;
17411544Seschrock 
17421544Seschrock 	return (error);
1743789Sahrens }
1744789Sahrens 
1745*10921STim.Haley@Sun.COM static int
1746*10921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
1747*10921STim.Haley@Sun.COM {
1748*10921STim.Haley@Sun.COM 	spa_unload(spa);
1749*10921STim.Haley@Sun.COM 	spa_deactivate(spa);
1750*10921STim.Haley@Sun.COM 
1751*10921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
1752*10921STim.Haley@Sun.COM 
1753*10921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
1754*10921STim.Haley@Sun.COM 	spa_async_suspend(spa);
1755*10921STim.Haley@Sun.COM 
1756*10921STim.Haley@Sun.COM 	return (spa_load(spa, state, mosconfig));
1757*10921STim.Haley@Sun.COM }
1758*10921STim.Haley@Sun.COM 
1759*10921STim.Haley@Sun.COM static int
1760*10921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
1761*10921STim.Haley@Sun.COM     uint64_t max_request, boolean_t extreme)
1762*10921STim.Haley@Sun.COM {
1763*10921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
1764*10921STim.Haley@Sun.COM 	int load_error, rewind_error;
1765*10921STim.Haley@Sun.COM 	uint64_t safe_rollback_txg;
1766*10921STim.Haley@Sun.COM 	uint64_t min_txg;
1767*10921STim.Haley@Sun.COM 
1768*10921STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER)
1769*10921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
1770*10921STim.Haley@Sun.COM 	else
1771*10921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
1772*10921STim.Haley@Sun.COM 
1773*10921STim.Haley@Sun.COM 	load_error = rewind_error = spa_load(spa, state, mosconfig);
1774*10921STim.Haley@Sun.COM 	if (load_error == 0)
1775*10921STim.Haley@Sun.COM 		return (0);
1776*10921STim.Haley@Sun.COM 
1777*10921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
1778*10921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1779*10921STim.Haley@Sun.COM 
1780*10921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
1781*10921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
1782*10921STim.Haley@Sun.COM 
1783*10921STim.Haley@Sun.COM 	/* specific txg requested */
1784*10921STim.Haley@Sun.COM 	if (spa->spa_load_max_txg != UINT64_MAX && !extreme) {
1785*10921STim.Haley@Sun.COM 		nvlist_free(config);
1786*10921STim.Haley@Sun.COM 		return (load_error);
1787*10921STim.Haley@Sun.COM 	}
1788*10921STim.Haley@Sun.COM 
1789*10921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
1790*10921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
1791*10921STim.Haley@Sun.COM 		spa->spa_log_state = SPA_LOG_CLEAR;
1792*10921STim.Haley@Sun.COM 
1793*10921STim.Haley@Sun.COM 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1794*10921STim.Haley@Sun.COM 	safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE;
1795*10921STim.Haley@Sun.COM 
1796*10921STim.Haley@Sun.COM 	min_txg = extreme ? TXG_INITIAL : safe_rollback_txg;
1797*10921STim.Haley@Sun.COM 	while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) {
1798*10921STim.Haley@Sun.COM 		if (spa->spa_load_max_txg < safe_rollback_txg)
1799*10921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
1800*10921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
1801*10921STim.Haley@Sun.COM 	}
1802*10921STim.Haley@Sun.COM 
1803*10921STim.Haley@Sun.COM 	if (config)
1804*10921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
1805*10921STim.Haley@Sun.COM 
1806*10921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
1807*10921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
1808*10921STim.Haley@Sun.COM 
1809*10921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
1810*10921STim.Haley@Sun.COM 		spa_config_set(spa, config);
1811*10921STim.Haley@Sun.COM 
1812*10921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
1813*10921STim.Haley@Sun.COM }
1814*10921STim.Haley@Sun.COM 
1815789Sahrens /*
1816789Sahrens  * Pool Open/Import
1817789Sahrens  *
1818789Sahrens  * The import case is identical to an open except that the configuration is sent
1819789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1820789Sahrens  * case of an open, the pool configuration will exist in the
18214451Seschrock  * POOL_STATE_UNINITIALIZED state.
1822789Sahrens  *
1823789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1824789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1825789Sahrens  * ambiguous state.
1826789Sahrens  */
1827789Sahrens static int
1828*10921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
1829*10921STim.Haley@Sun.COM     nvlist_t **config)
1830789Sahrens {
1831789Sahrens 	spa_t *spa;
1832*10921STim.Haley@Sun.COM 	boolean_t norewind;
1833*10921STim.Haley@Sun.COM 	boolean_t extreme;
1834*10921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
1835*10921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_OPEN;
1836789Sahrens 	int error;
1837789Sahrens 	int locked = B_FALSE;
1838789Sahrens 
1839789Sahrens 	*spapp = NULL;
1840789Sahrens 
1841*10921STim.Haley@Sun.COM 	zpool_get_rewind_policy(nvpolicy, &policy);
1842*10921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
1843*10921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
1844*10921STim.Haley@Sun.COM 	norewind = (policy.zrp_request == ZPOOL_NO_REWIND);
1845*10921STim.Haley@Sun.COM 	extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0);
1846*10921STim.Haley@Sun.COM 
1847789Sahrens 	/*
1848789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1849789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1850789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1851789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1852789Sahrens 	 */
1853789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1854789Sahrens 		mutex_enter(&spa_namespace_lock);
1855789Sahrens 		locked = B_TRUE;
1856789Sahrens 	}
1857789Sahrens 
1858789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1859789Sahrens 		if (locked)
1860789Sahrens 			mutex_exit(&spa_namespace_lock);
1861789Sahrens 		return (ENOENT);
1862789Sahrens 	}
1863*10921STim.Haley@Sun.COM 
1864789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1865789Sahrens 
18668241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
1867789Sahrens 
1868*10921STim.Haley@Sun.COM 		if (spa->spa_last_open_failed && norewind) {
1869*10921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
1870*10921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config,
1871*10921STim.Haley@Sun.COM 				    config, KM_SLEEP) == 0);
1872*10921STim.Haley@Sun.COM 			spa_deactivate(spa);
1873*10921STim.Haley@Sun.COM 			if (locked)
1874*10921STim.Haley@Sun.COM 				mutex_exit(&spa_namespace_lock);
1875*10921STim.Haley@Sun.COM 			return (spa->spa_last_open_failed);
1876*10921STim.Haley@Sun.COM 		}
1877*10921STim.Haley@Sun.COM 
1878*10921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
1879*10921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
1880*10921STim.Haley@Sun.COM 
1881*10921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
1882*10921STim.Haley@Sun.COM 		    extreme);
1883789Sahrens 
1884789Sahrens 		if (error == EBADF) {
1885789Sahrens 			/*
18861986Seschrock 			 * If vdev_validate() returns failure (indicated by
18871986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
18881986Seschrock 			 * that the pool has been exported or destroyed.  If
18891986Seschrock 			 * this is the case, the config cache is out of sync and
18901986Seschrock 			 * we should remove the pool from the namespace.
1891789Sahrens 			 */
1892789Sahrens 			spa_unload(spa);
1893789Sahrens 			spa_deactivate(spa);
18946643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1895789Sahrens 			spa_remove(spa);
1896789Sahrens 			if (locked)
1897789Sahrens 				mutex_exit(&spa_namespace_lock);
1898789Sahrens 			return (ENOENT);
18991544Seschrock 		}
19001544Seschrock 
19011544Seschrock 		if (error) {
1902789Sahrens 			/*
1903789Sahrens 			 * We can't open the pool, but we still have useful
1904789Sahrens 			 * information: the state of each vdev after the
1905789Sahrens 			 * attempted vdev_open().  Return this to the user.
1906789Sahrens 			 */
1907*10921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
1908*10921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
1909*10921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
1910789Sahrens 			spa_unload(spa);
1911789Sahrens 			spa_deactivate(spa);
1912*10921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
1913789Sahrens 			if (locked)
1914789Sahrens 				mutex_exit(&spa_namespace_lock);
1915789Sahrens 			*spapp = NULL;
1916789Sahrens 			return (error);
1917789Sahrens 		}
1918*10921STim.Haley@Sun.COM 
1919789Sahrens 	}
1920789Sahrens 
1921789Sahrens 	spa_open_ref(spa, tag);
19224451Seschrock 
1923*10921STim.Haley@Sun.COM 	spa->spa_last_open_failed = 0;
1924*10921STim.Haley@Sun.COM 
1925*10921STim.Haley@Sun.COM 	if (config != NULL)
1926*10921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1927*10921STim.Haley@Sun.COM 
1928*10921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = 0;
1929*10921STim.Haley@Sun.COM 	spa->spa_load_txg = 0;
1930*10921STim.Haley@Sun.COM 
1931789Sahrens 	if (locked)
1932789Sahrens 		mutex_exit(&spa_namespace_lock);
1933789Sahrens 
1934789Sahrens 	*spapp = spa;
1935789Sahrens 
1936789Sahrens 	return (0);
1937789Sahrens }
1938789Sahrens 
1939789Sahrens int
1940*10921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
1941*10921STim.Haley@Sun.COM     nvlist_t **config)
1942*10921STim.Haley@Sun.COM {
1943*10921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
1944*10921STim.Haley@Sun.COM }
1945*10921STim.Haley@Sun.COM 
1946*10921STim.Haley@Sun.COM int
1947789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1948789Sahrens {
1949*10921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
1950789Sahrens }
1951789Sahrens 
19521544Seschrock /*
19531544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
19541544Seschrock  * preventing it from being exported or destroyed.
19551544Seschrock  */
19561544Seschrock spa_t *
19571544Seschrock spa_inject_addref(char *name)
19581544Seschrock {
19591544Seschrock 	spa_t *spa;
19601544Seschrock 
19611544Seschrock 	mutex_enter(&spa_namespace_lock);
19621544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
19631544Seschrock 		mutex_exit(&spa_namespace_lock);
19641544Seschrock 		return (NULL);
19651544Seschrock 	}
19661544Seschrock 	spa->spa_inject_ref++;
19671544Seschrock 	mutex_exit(&spa_namespace_lock);
19681544Seschrock 
19691544Seschrock 	return (spa);
19701544Seschrock }
19711544Seschrock 
19721544Seschrock void
19731544Seschrock spa_inject_delref(spa_t *spa)
19741544Seschrock {
19751544Seschrock 	mutex_enter(&spa_namespace_lock);
19761544Seschrock 	spa->spa_inject_ref--;
19771544Seschrock 	mutex_exit(&spa_namespace_lock);
19781544Seschrock }
19791544Seschrock 
19805450Sbrendan /*
19815450Sbrendan  * Add spares device information to the nvlist.
19825450Sbrendan  */
19832082Seschrock static void
19842082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
19852082Seschrock {
19862082Seschrock 	nvlist_t **spares;
19872082Seschrock 	uint_t i, nspares;
19882082Seschrock 	nvlist_t *nvroot;
19892082Seschrock 	uint64_t guid;
19902082Seschrock 	vdev_stat_t *vs;
19912082Seschrock 	uint_t vsc;
19923377Seschrock 	uint64_t pool;
19932082Seschrock 
19949425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
19959425SEric.Schrock@Sun.COM 
19965450Sbrendan 	if (spa->spa_spares.sav_count == 0)
19972082Seschrock 		return;
19982082Seschrock 
19992082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
20002082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
20015450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
20022082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
20032082Seschrock 	if (nspares != 0) {
20042082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
20052082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
20062082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
20072082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
20082082Seschrock 
20092082Seschrock 		/*
20102082Seschrock 		 * Go through and find any spares which have since been
20112082Seschrock 		 * repurposed as an active spare.  If this is the case, update
20122082Seschrock 		 * their status appropriately.
20132082Seschrock 		 */
20142082Seschrock 		for (i = 0; i < nspares; i++) {
20152082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
20162082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
20177214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
20187214Slling 			    pool != 0ULL) {
20192082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
20202082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
20212082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
20222082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
20232082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
20242082Seschrock 			}
20252082Seschrock 		}
20262082Seschrock 	}
20272082Seschrock }
20282082Seschrock 
20295450Sbrendan /*
20305450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
20315450Sbrendan  */
20325450Sbrendan static void
20335450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
20345450Sbrendan {
20355450Sbrendan 	nvlist_t **l2cache;
20365450Sbrendan 	uint_t i, j, nl2cache;
20375450Sbrendan 	nvlist_t *nvroot;
20385450Sbrendan 	uint64_t guid;
20395450Sbrendan 	vdev_t *vd;
20405450Sbrendan 	vdev_stat_t *vs;
20415450Sbrendan 	uint_t vsc;
20425450Sbrendan 
20439425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
20449425SEric.Schrock@Sun.COM 
20455450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
20465450Sbrendan 		return;
20475450Sbrendan 
20485450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
20495450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
20505450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
20515450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
20525450Sbrendan 	if (nl2cache != 0) {
20535450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
20545450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
20555450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
20565450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
20575450Sbrendan 
20585450Sbrendan 		/*
20595450Sbrendan 		 * Update level 2 cache device stats.
20605450Sbrendan 		 */
20615450Sbrendan 
20625450Sbrendan 		for (i = 0; i < nl2cache; i++) {
20635450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
20645450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
20655450Sbrendan 
20665450Sbrendan 			vd = NULL;
20675450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
20685450Sbrendan 				if (guid ==
20695450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
20705450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
20715450Sbrendan 					break;
20725450Sbrendan 				}
20735450Sbrendan 			}
20745450Sbrendan 			ASSERT(vd != NULL);
20755450Sbrendan 
20765450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
20775450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
20785450Sbrendan 			vdev_get_stats(vd, vs);
20795450Sbrendan 		}
20805450Sbrendan 	}
20815450Sbrendan }
20825450Sbrendan 
2083789Sahrens int
20841544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2085789Sahrens {
2086789Sahrens 	int error;
2087789Sahrens 	spa_t *spa;
2088789Sahrens 
2089789Sahrens 	*config = NULL;
2090*10921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2091789Sahrens 
20929425SEric.Schrock@Sun.COM 	if (spa != NULL) {
20939425SEric.Schrock@Sun.COM 		/*
20949425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
20959425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
20969425SEric.Schrock@Sun.COM 		 * self-inconsistent.
20979425SEric.Schrock@Sun.COM 		 */
20989425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
20999425SEric.Schrock@Sun.COM 
21009425SEric.Schrock@Sun.COM 		if (*config != NULL) {
21017754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
21029425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
21039425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
21049425SEric.Schrock@Sun.COM 
21059425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
21069425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
21079425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
21089425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
21099425SEric.Schrock@Sun.COM 
21109425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
21119425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
21129425SEric.Schrock@Sun.COM 		}
21132082Seschrock 	}
21142082Seschrock 
21151544Seschrock 	/*
21161544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
21171544Seschrock 	 * and call spa_lookup() directly.
21181544Seschrock 	 */
21191544Seschrock 	if (altroot) {
21201544Seschrock 		if (spa == NULL) {
21211544Seschrock 			mutex_enter(&spa_namespace_lock);
21221544Seschrock 			spa = spa_lookup(name);
21231544Seschrock 			if (spa)
21241544Seschrock 				spa_altroot(spa, altroot, buflen);
21251544Seschrock 			else
21261544Seschrock 				altroot[0] = '\0';
21271544Seschrock 			spa = NULL;
21281544Seschrock 			mutex_exit(&spa_namespace_lock);
21291544Seschrock 		} else {
21301544Seschrock 			spa_altroot(spa, altroot, buflen);
21311544Seschrock 		}
21321544Seschrock 	}
21331544Seschrock 
21349425SEric.Schrock@Sun.COM 	if (spa != NULL) {
21359425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2136789Sahrens 		spa_close(spa, FTAG);
21379425SEric.Schrock@Sun.COM 	}
2138789Sahrens 
2139789Sahrens 	return (error);
2140789Sahrens }
2141789Sahrens 
2142789Sahrens /*
21435450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
21445450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
21455450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
21465450Sbrendan  * specified, as long as they are well-formed.
21472082Seschrock  */
21482082Seschrock static int
21495450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
21505450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
21515450Sbrendan     vdev_labeltype_t label)
21522082Seschrock {
21535450Sbrendan 	nvlist_t **dev;
21545450Sbrendan 	uint_t i, ndev;
21552082Seschrock 	vdev_t *vd;
21562082Seschrock 	int error;
21572082Seschrock 
21587754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
21597754SJeff.Bonwick@Sun.COM 
21602082Seschrock 	/*
21615450Sbrendan 	 * It's acceptable to have no devs specified.
21622082Seschrock 	 */
21635450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
21642082Seschrock 		return (0);
21652082Seschrock 
21665450Sbrendan 	if (ndev == 0)
21672082Seschrock 		return (EINVAL);
21682082Seschrock 
21692082Seschrock 	/*
21705450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
21715450Sbrendan 	 * device type.
21722082Seschrock 	 */
21735450Sbrendan 	if (spa_version(spa) < version)
21742082Seschrock 		return (ENOTSUP);
21752082Seschrock 
21763377Seschrock 	/*
21775450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
21783377Seschrock 	 * checking.
21793377Seschrock 	 */
21805450Sbrendan 	sav->sav_pending = dev;
21815450Sbrendan 	sav->sav_npending = ndev;
21825450Sbrendan 
21835450Sbrendan 	for (i = 0; i < ndev; i++) {
21845450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
21852082Seschrock 		    mode)) != 0)
21863377Seschrock 			goto out;
21872082Seschrock 
21882082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
21892082Seschrock 			vdev_free(vd);
21903377Seschrock 			error = EINVAL;
21913377Seschrock 			goto out;
21922082Seschrock 		}
21932082Seschrock 
21945450Sbrendan 		/*
21957754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
21967754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
21975450Sbrendan 		 */
21987754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
21995450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
22005450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
22015450Sbrendan 			error = ENOTBLK;
22025450Sbrendan 			goto out;
22035450Sbrendan 		}
22047754SJeff.Bonwick@Sun.COM #endif
22052082Seschrock 		vd->vdev_top = vd;
22063377Seschrock 
22073377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
22085450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
22095450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
22103377Seschrock 			    vd->vdev_guid) == 0);
22112082Seschrock 		}
22122082Seschrock 
22132082Seschrock 		vdev_free(vd);
22143377Seschrock 
22155450Sbrendan 		if (error &&
22165450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
22173377Seschrock 			goto out;
22183377Seschrock 		else
22193377Seschrock 			error = 0;
22202082Seschrock 	}
22212082Seschrock 
22223377Seschrock out:
22235450Sbrendan 	sav->sav_pending = NULL;
22245450Sbrendan 	sav->sav_npending = 0;
22253377Seschrock 	return (error);
22262082Seschrock }
22272082Seschrock 
22285450Sbrendan static int
22295450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
22305450Sbrendan {
22315450Sbrendan 	int error;
22325450Sbrendan 
22337754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
22347754SJeff.Bonwick@Sun.COM 
22355450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
22365450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
22375450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
22385450Sbrendan 		return (error);
22395450Sbrendan 	}
22405450Sbrendan 
22415450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
22425450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
22435450Sbrendan 	    VDEV_LABEL_L2CACHE));
22445450Sbrendan }
22455450Sbrendan 
22465450Sbrendan static void
22475450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
22485450Sbrendan     const char *config)
22495450Sbrendan {
22505450Sbrendan 	int i;
22515450Sbrendan 
22525450Sbrendan 	if (sav->sav_config != NULL) {
22535450Sbrendan 		nvlist_t **olddevs;
22545450Sbrendan 		uint_t oldndevs;
22555450Sbrendan 		nvlist_t **newdevs;
22565450Sbrendan 
22575450Sbrendan 		/*
22585450Sbrendan 		 * Generate new dev list by concatentating with the
22595450Sbrendan 		 * current dev list.
22605450Sbrendan 		 */
22615450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
22625450Sbrendan 		    &olddevs, &oldndevs) == 0);
22635450Sbrendan 
22645450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
22655450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
22665450Sbrendan 		for (i = 0; i < oldndevs; i++)
22675450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
22685450Sbrendan 			    KM_SLEEP) == 0);
22695450Sbrendan 		for (i = 0; i < ndevs; i++)
22705450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
22715450Sbrendan 			    KM_SLEEP) == 0);
22725450Sbrendan 
22735450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
22745450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
22755450Sbrendan 
22765450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
22775450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
22785450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
22795450Sbrendan 			nvlist_free(newdevs[i]);
22805450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
22815450Sbrendan 	} else {
22825450Sbrendan 		/*
22835450Sbrendan 		 * Generate a new dev list.
22845450Sbrendan 		 */
22855450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
22865450Sbrendan 		    KM_SLEEP) == 0);
22875450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
22885450Sbrendan 		    devs, ndevs) == 0);
22895450Sbrendan 	}
22905450Sbrendan }
22915450Sbrendan 
22925450Sbrendan /*
22935450Sbrendan  * Stop and drop level 2 ARC devices
22945450Sbrendan  */
22955450Sbrendan void
22965450Sbrendan spa_l2cache_drop(spa_t *spa)
22975450Sbrendan {
22985450Sbrendan 	vdev_t *vd;
22995450Sbrendan 	int i;
23005450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
23015450Sbrendan 
23025450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
23035450Sbrendan 		uint64_t pool;
23045450Sbrendan 
23055450Sbrendan 		vd = sav->sav_vdevs[i];
23065450Sbrendan 		ASSERT(vd != NULL);
23075450Sbrendan 
23088241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
23098241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
23105450Sbrendan 			l2arc_remove_vdev(vd);
23115450Sbrendan 		if (vd->vdev_isl2cache)
23125450Sbrendan 			spa_l2cache_remove(vd);
23135450Sbrendan 		vdev_clear_stats(vd);
23145450Sbrendan 		(void) vdev_close(vd);
23155450Sbrendan 	}
23165450Sbrendan }
23175450Sbrendan 
23182082Seschrock /*
2319789Sahrens  * Pool Creation
2320789Sahrens  */
2321789Sahrens int
23225094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
23237184Stimh     const char *history_str, nvlist_t *zplprops)
2324789Sahrens {
2325789Sahrens 	spa_t *spa;
23265094Slling 	char *altroot = NULL;
23271635Sbonwick 	vdev_t *rvd;
2328789Sahrens 	dsl_pool_t *dp;
2329789Sahrens 	dmu_tx_t *tx;
23309816SGeorge.Wilson@Sun.COM 	int error = 0;
2331789Sahrens 	uint64_t txg = TXG_INITIAL;
23325450Sbrendan 	nvlist_t **spares, **l2cache;
23335450Sbrendan 	uint_t nspares, nl2cache;
23345094Slling 	uint64_t version;
2335789Sahrens 
2336789Sahrens 	/*
2337789Sahrens 	 * If this pool already exists, return failure.
2338789Sahrens 	 */
2339789Sahrens 	mutex_enter(&spa_namespace_lock);
2340789Sahrens 	if (spa_lookup(pool) != NULL) {
2341789Sahrens 		mutex_exit(&spa_namespace_lock);
2342789Sahrens 		return (EEXIST);
2343789Sahrens 	}
2344789Sahrens 
2345789Sahrens 	/*
2346789Sahrens 	 * Allocate a new spa_t structure.
2347789Sahrens 	 */
23485094Slling 	(void) nvlist_lookup_string(props,
23495094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2350*10921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
23518241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2352789Sahrens 
2353789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
23545094Slling 
23555094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
23565094Slling 		spa_deactivate(spa);
23575094Slling 		spa_remove(spa);
23586643Seschrock 		mutex_exit(&spa_namespace_lock);
23595094Slling 		return (error);
23605094Slling 	}
23615094Slling 
23625094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
23635094Slling 	    &version) != 0)
23645094Slling 		version = SPA_VERSION;
23655094Slling 	ASSERT(version <= SPA_VERSION);
23665094Slling 	spa->spa_uberblock.ub_version = version;
2367789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2368789Sahrens 
23691635Sbonwick 	/*
23709234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
23719234SGeorge.Wilson@Sun.COM 	 */
23729630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
23739630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
23749234SGeorge.Wilson@Sun.COM 
23759234SGeorge.Wilson@Sun.COM 	/*
23761635Sbonwick 	 * Create the root vdev.
23771635Sbonwick 	 */
23787754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
23791635Sbonwick 
23802082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
23812082Seschrock 
23822082Seschrock 	ASSERT(error != 0 || rvd != NULL);
23832082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
23842082Seschrock 
23855913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
23861635Sbonwick 		error = EINVAL;
23872082Seschrock 
23882082Seschrock 	if (error == 0 &&
23892082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
23905450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
23912082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
23929816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
23939816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
23949816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
23959816SGeorge.Wilson@Sun.COM 		}
23961635Sbonwick 	}
23971635Sbonwick 
23987754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2399789Sahrens 
24002082Seschrock 	if (error != 0) {
2401789Sahrens 		spa_unload(spa);
2402789Sahrens 		spa_deactivate(spa);
2403789Sahrens 		spa_remove(spa);
2404789Sahrens 		mutex_exit(&spa_namespace_lock);
2405789Sahrens 		return (error);
2406789Sahrens 	}
2407789Sahrens 
24082082Seschrock 	/*
24092082Seschrock 	 * Get the list of spares, if specified.
24102082Seschrock 	 */
24112082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
24122082Seschrock 	    &spares, &nspares) == 0) {
24135450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
24142082Seschrock 		    KM_SLEEP) == 0);
24155450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
24162082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
24177754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
24182082Seschrock 		spa_load_spares(spa);
24197754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
24205450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
24215450Sbrendan 	}
24225450Sbrendan 
24235450Sbrendan 	/*
24245450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
24255450Sbrendan 	 */
24265450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
24275450Sbrendan 	    &l2cache, &nl2cache) == 0) {
24285450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
24295450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
24305450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
24315450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
24327754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
24335450Sbrendan 		spa_load_l2cache(spa);
24347754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
24355450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
24362082Seschrock 	}
24372082Seschrock 
24387184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2439789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2440789Sahrens 
2441789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2442789Sahrens 
2443789Sahrens 	/*
2444789Sahrens 	 * Create the pool config object.
2445789Sahrens 	 */
2446789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
24477497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2448789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2449789Sahrens 
24501544Seschrock 	if (zap_add(spa->spa_meta_objset,
2451789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
24521544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
24531544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
24541544Seschrock 	}
2455789Sahrens 
24565094Slling 	/* Newly created pools with the right version are always deflated. */
24575094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
24585094Slling 		spa->spa_deflate = TRUE;
24595094Slling 		if (zap_add(spa->spa_meta_objset,
24605094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
24615094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
24625094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
24635094Slling 		}
24642082Seschrock 	}
24652082Seschrock 
2466789Sahrens 	/*
2467789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2468789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2469789Sahrens 	 * keeps changing.
2470789Sahrens 	 */
2471789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2472789Sahrens 	    1 << 14, tx);
2473789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2474789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2475789Sahrens 
24761544Seschrock 	if (zap_add(spa->spa_meta_objset,
2477789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
24781544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
24791544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
24801544Seschrock 	}
2481789Sahrens 
24822926Sek110237 	/*
24832926Sek110237 	 * Create the pool's history object.
24842926Sek110237 	 */
24855094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
24865094Slling 		spa_history_create_obj(spa, tx);
24875094Slling 
24885094Slling 	/*
24895094Slling 	 * Set pool properties.
24905094Slling 	 */
24915094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
24925094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
24935329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
24949816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
24958525SEric.Schrock@Sun.COM 	if (props != NULL) {
24968525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
24975094Slling 		spa_sync_props(spa, props, CRED(), tx);
24988525SEric.Schrock@Sun.COM 	}
24992926Sek110237 
2500789Sahrens 	dmu_tx_commit(tx);
2501789Sahrens 
2502789Sahrens 	spa->spa_sync_on = B_TRUE;
2503789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2504789Sahrens 
2505789Sahrens 	/*
2506789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2507789Sahrens 	 * bean counters are appropriately updated.
2508789Sahrens 	 */
2509789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2510789Sahrens 
25116643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2512789Sahrens 
25135094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
25144715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
25159946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
25164715Sek110237 
25178667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
25188667SGeorge.Wilson@Sun.COM 
2519789Sahrens 	mutex_exit(&spa_namespace_lock);
2520789Sahrens 
2521789Sahrens 	return (0);
2522789Sahrens }
2523789Sahrens 
25246423Sgw25295 #ifdef _KERNEL
25256423Sgw25295 /*
25269790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
25279790SLin.Ling@Sun.COM  * during the system boot up time.
25286423Sgw25295  */
25299790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
25309790SLin.Ling@Sun.COM 
25319790SLin.Ling@Sun.COM static nvlist_t *
25329790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
25336423Sgw25295 {
25349790SLin.Ling@Sun.COM 	nvlist_t *config;
25356423Sgw25295 	nvlist_t *nvtop, *nvroot;
25366423Sgw25295 	uint64_t pgid;
25376423Sgw25295 
25389790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
25399790SLin.Ling@Sun.COM 		return (NULL);
25409790SLin.Ling@Sun.COM 
25416423Sgw25295 	/*
25426423Sgw25295 	 * Add this top-level vdev to the child array.
25436423Sgw25295 	 */
25449790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
25459790SLin.Ling@Sun.COM 	    &nvtop) == 0);
25469790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
25479790SLin.Ling@Sun.COM 	    &pgid) == 0);
25489790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
25496423Sgw25295 
25506423Sgw25295 	/*
25516423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
25526423Sgw25295 	 */
25536423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
25549790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
25559790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
25566423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
25576423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
25586423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
25596423Sgw25295 	    &nvtop, 1) == 0);
25606423Sgw25295 
25616423Sgw25295 	/*
25626423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
25636423Sgw25295 	 * this pool's configuration (remove the old, add the new).
25646423Sgw25295 	 */
25656423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
25666423Sgw25295 	nvlist_free(nvroot);
25679790SLin.Ling@Sun.COM 	return (config);
25686423Sgw25295 }
25696423Sgw25295 
25706423Sgw25295 /*
25719790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
25729790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
25739790SLin.Ling@Sun.COM  * device has a more recent txg.
25746423Sgw25295  */
25759790SLin.Ling@Sun.COM static void
25769790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
25777147Staylor {
25789816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
25799790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
25809790SLin.Ling@Sun.COM 
25819790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
25829790SLin.Ling@Sun.COM 		nvlist_t *label;
25839790SLin.Ling@Sun.COM 		uint64_t label_txg;
25849790SLin.Ling@Sun.COM 
25859790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
25869790SLin.Ling@Sun.COM 		    &label) != 0)
25879790SLin.Ling@Sun.COM 			return;
25889790SLin.Ling@Sun.COM 
25899790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
25909790SLin.Ling@Sun.COM 		    &label_txg) == 0);
25919790SLin.Ling@Sun.COM 
25929790SLin.Ling@Sun.COM 		/*
25939790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
25949790SLin.Ling@Sun.COM 		 */
25959790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
25969790SLin.Ling@Sun.COM 			*txg = label_txg;
25979790SLin.Ling@Sun.COM 			*avd = vd;
25987147Staylor 		}
25999790SLin.Ling@Sun.COM 		nvlist_free(label);
26007147Staylor 	}
26017147Staylor }
26027147Staylor 
26036423Sgw25295 /*
26046423Sgw25295  * Import a root pool.
26056423Sgw25295  *
26067147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
26077147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
26087147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
26096423Sgw25295  *
26106423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
26116423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
26126423Sgw25295  * e.g.
26136423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
26146423Sgw25295  */
26156423Sgw25295 int
26167147Staylor spa_import_rootpool(char *devpath, char *devid)
26176423Sgw25295 {
26189790SLin.Ling@Sun.COM 	spa_t *spa;
26199790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
26209790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
26219790SLin.Ling@Sun.COM 	uint64_t guid, txg;
26226423Sgw25295 	char *pname;
26236423Sgw25295 	int error;
26246423Sgw25295 
26256423Sgw25295 	/*
26269790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
26276423Sgw25295 	 */
262810822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
262910822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
263010822SJack.Meng@Sun.COM 	if (config == NULL) {
263110822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
263210822SJack.Meng@Sun.COM 			/* iscsi boot */
263310822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
263410822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
263510822SJack.Meng@Sun.COM 		}
263610822SJack.Meng@Sun.COM 	}
263710822SJack.Meng@Sun.COM #endif
263810822SJack.Meng@Sun.COM 	if (config == NULL) {
26399790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
26409790SLin.Ling@Sun.COM 		    devpath);
26419790SLin.Ling@Sun.COM 		return (EIO);
26429790SLin.Ling@Sun.COM 	}
26439790SLin.Ling@Sun.COM 
26449790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
26459790SLin.Ling@Sun.COM 	    &pname) == 0);
26469790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
26476423Sgw25295 
26489425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
26499425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
26509425SEric.Schrock@Sun.COM 		/*
26519425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
26529425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
26539425SEric.Schrock@Sun.COM 		 */
26549425SEric.Schrock@Sun.COM 		spa_remove(spa);
26559425SEric.Schrock@Sun.COM 	}
26569425SEric.Schrock@Sun.COM 
2657*10921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
26589425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
265910100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
26609790SLin.Ling@Sun.COM 
26619790SLin.Ling@Sun.COM 	/*
26629790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
26639790SLin.Ling@Sun.COM 	 */
26649790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
26659790SLin.Ling@Sun.COM 	    &nvtop) == 0);
26669790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
26679790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
26689790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
26699790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
26709790SLin.Ling@Sun.COM 	if (error) {
26719790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
26729790SLin.Ling@Sun.COM 		nvlist_free(config);
26739790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
26749790SLin.Ling@Sun.COM 		    pname);
26759790SLin.Ling@Sun.COM 		return (error);
26769790SLin.Ling@Sun.COM 	}
26779790SLin.Ling@Sun.COM 
26789790SLin.Ling@Sun.COM 	/*
26799790SLin.Ling@Sun.COM 	 * Get the boot vdev.
26809790SLin.Ling@Sun.COM 	 */
26819790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
26829790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
26839790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
26849790SLin.Ling@Sun.COM 		error = ENOENT;
26859790SLin.Ling@Sun.COM 		goto out;
26869790SLin.Ling@Sun.COM 	}
26879790SLin.Ling@Sun.COM 
26889790SLin.Ling@Sun.COM 	/*
26899790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
26909790SLin.Ling@Sun.COM 	 */
26919790SLin.Ling@Sun.COM 	avd = bvd;
26929790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
26939790SLin.Ling@Sun.COM 	if (avd != bvd) {
26949790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
26959790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
26969790SLin.Ling@Sun.COM 		error = EINVAL;
26979790SLin.Ling@Sun.COM 		goto out;
26989790SLin.Ling@Sun.COM 	}
26999790SLin.Ling@Sun.COM 
27009790SLin.Ling@Sun.COM 	/*
27019790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
27029790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
27039790SLin.Ling@Sun.COM 	 */
27049790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
27059790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
27069790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
27079790SLin.Ling@Sun.COM 		    "try booting from '%s'",
27089790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
27099790SLin.Ling@Sun.COM 		error = EINVAL;
27109790SLin.Ling@Sun.COM 		goto out;
27119790SLin.Ling@Sun.COM 	}
27129790SLin.Ling@Sun.COM 
27139790SLin.Ling@Sun.COM 	VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
27149790SLin.Ling@Sun.COM 	error = 0;
27159946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
27169790SLin.Ling@Sun.COM out:
27179790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27189790SLin.Ling@Sun.COM 	vdev_free(rvd);
27199790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
27209425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
27216423Sgw25295 
27229790SLin.Ling@Sun.COM 	nvlist_free(config);
27236423Sgw25295 	return (error);
27246423Sgw25295 }
27259790SLin.Ling@Sun.COM 
27266423Sgw25295 #endif
27276423Sgw25295 
27286423Sgw25295 /*
27299425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
27309425SEric.Schrock@Sun.COM  * boot.
27319425SEric.Schrock@Sun.COM  */
27329425SEric.Schrock@Sun.COM int
27339425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
27349425SEric.Schrock@Sun.COM {
27359425SEric.Schrock@Sun.COM 	spa_t *spa;
2736*10921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
27379425SEric.Schrock@Sun.COM 	char *altroot = NULL;
27389425SEric.Schrock@Sun.COM 
27399425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
27409425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
27419425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
27429425SEric.Schrock@Sun.COM 		return (EEXIST);
27439425SEric.Schrock@Sun.COM 	}
27449425SEric.Schrock@Sun.COM 
27459425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
27469425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2747*10921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
2748*10921STim.Haley@Sun.COM 
2749*10921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
2750*10921STim.Haley@Sun.COM 	spa->spa_load_max_txg = policy.zrp_txg;
27519425SEric.Schrock@Sun.COM 
275210100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
275310000SVictor.Latushkin@Sun.COM 
27549425SEric.Schrock@Sun.COM 	if (props != NULL)
27559425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
27569425SEric.Schrock@Sun.COM 
27579425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
27589425SEric.Schrock@Sun.COM 
27599425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
27609946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
27619425SEric.Schrock@Sun.COM 
27629425SEric.Schrock@Sun.COM 	return (0);
27639425SEric.Schrock@Sun.COM }
27649425SEric.Schrock@Sun.COM 
27659425SEric.Schrock@Sun.COM /*
27666423Sgw25295  * Import a non-root pool into the system.
27676423Sgw25295  */
27686423Sgw25295 int
27696423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
27706423Sgw25295 {
27719425SEric.Schrock@Sun.COM 	spa_t *spa;
27729425SEric.Schrock@Sun.COM 	char *altroot = NULL;
2773*10921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
2774*10921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
27759425SEric.Schrock@Sun.COM 	int error;
27769425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
27779425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
27789425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
27799425SEric.Schrock@Sun.COM 
27809425SEric.Schrock@Sun.COM 	/*
27819425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
27829425SEric.Schrock@Sun.COM 	 */
27839425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
27849425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pool)) != NULL) {
27859425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
27869425SEric.Schrock@Sun.COM 		return (EEXIST);
27879425SEric.Schrock@Sun.COM 	}
27889425SEric.Schrock@Sun.COM 
2789*10921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
2790*10921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
2791*10921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
2792*10921STim.Haley@Sun.COM 
27939425SEric.Schrock@Sun.COM 	/*
27949425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
27959425SEric.Schrock@Sun.COM 	 */
27969425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
27979425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2798*10921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
27999425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
28009425SEric.Schrock@Sun.COM 
28019425SEric.Schrock@Sun.COM 	/*
28029630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
28039630SJeff.Bonwick@Sun.COM 	 */
28049630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
28059630SJeff.Bonwick@Sun.COM 
28069630SJeff.Bonwick@Sun.COM 	/*
28079425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
28089425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
28099425SEric.Schrock@Sun.COM 	 * doing an import.
28109425SEric.Schrock@Sun.COM 	 */
2811*10921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
2812*10921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2813*10921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
2814*10921STim.Haley@Sun.COM 	    ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0));
2815*10921STim.Haley@Sun.COM 
2816*10921STim.Haley@Sun.COM 	/*
2817*10921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
2818*10921STim.Haley@Sun.COM 	 * back to caller
2819*10921STim.Haley@Sun.COM 	 */
2820*10921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
28219425SEric.Schrock@Sun.COM 
28229425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28239425SEric.Schrock@Sun.COM 	/*
28249425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
28259425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
28269425SEric.Schrock@Sun.COM 	 */
28279425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
28289425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
28299425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
28309425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
28319425SEric.Schrock@Sun.COM 	}
28329425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
28339425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
28349425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
28359425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
28369425SEric.Schrock@Sun.COM 	}
28379425SEric.Schrock@Sun.COM 
28389425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28399425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
28409425SEric.Schrock@Sun.COM 	if (error == 0)
28419425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
28429425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
28439425SEric.Schrock@Sun.COM 	if (error == 0)
28449425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
28459425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
28469425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
28479425SEric.Schrock@Sun.COM 
28489425SEric.Schrock@Sun.COM 	if (props != NULL)
28499425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
28509425SEric.Schrock@Sun.COM 
28519425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
28529425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
28539425SEric.Schrock@Sun.COM 		spa_unload(spa);
28549425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
28559425SEric.Schrock@Sun.COM 		spa_remove(spa);
28569425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
28579425SEric.Schrock@Sun.COM 		return (error);
28589425SEric.Schrock@Sun.COM 	}
28599425SEric.Schrock@Sun.COM 
28609630SJeff.Bonwick@Sun.COM 	spa_async_resume(spa);
28619630SJeff.Bonwick@Sun.COM 
28629425SEric.Schrock@Sun.COM 	/*
28639425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
28649425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
28659425SEric.Schrock@Sun.COM 	 */
28669425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
28679425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
28689425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
28699425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
28709425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
28719425SEric.Schrock@Sun.COM 		else
28729425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
28739425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
28749425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
28759425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
28769425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28779425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
28789425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
28799425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
28809425SEric.Schrock@Sun.COM 	}
28819425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
28829425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
28839425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
28849425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
28859425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
28869425SEric.Schrock@Sun.COM 		else
28879425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
28889425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
28899425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
28909425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
28919425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28929425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
28939425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
28949425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
28959425SEric.Schrock@Sun.COM 	}
28969425SEric.Schrock@Sun.COM 
289710672SEric.Schrock@Sun.COM 	/*
289810672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
289910672SEric.Schrock@Sun.COM 	 */
290010672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
290110672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
290210672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
290310672SEric.Schrock@Sun.COM 	}
290410672SEric.Schrock@Sun.COM 
29059425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
29069425SEric.Schrock@Sun.COM 		/*
29079425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
29089425SEric.Schrock@Sun.COM 		 */
290910100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
29109425SEric.Schrock@Sun.COM 	}
29119425SEric.Schrock@Sun.COM 
29129816SGeorge.Wilson@Sun.COM 	/*
29139816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
29149816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
29159816SGeorge.Wilson@Sun.COM 	 */
29169816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
29179816SGeorge.Wilson@Sun.COM 
29189425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
29199946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
29209425SEric.Schrock@Sun.COM 
29219425SEric.Schrock@Sun.COM 	return (0);
29226643Seschrock }
29236643Seschrock 
29246643Seschrock 
2925789Sahrens /*
2926789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2927789Sahrens  * to get the vdev stats associated with the imported devices.
2928789Sahrens  */
2929789Sahrens #define	TRYIMPORT_NAME	"$import"
2930789Sahrens 
2931789Sahrens nvlist_t *
2932789Sahrens spa_tryimport(nvlist_t *tryconfig)
2933789Sahrens {
2934789Sahrens 	nvlist_t *config = NULL;
2935789Sahrens 	char *poolname;
2936789Sahrens 	spa_t *spa;
2937789Sahrens 	uint64_t state;
29388680SLin.Ling@Sun.COM 	int error;
2939789Sahrens 
2940789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2941789Sahrens 		return (NULL);
2942789Sahrens 
2943789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2944789Sahrens 		return (NULL);
2945789Sahrens 
29461635Sbonwick 	/*
29471635Sbonwick 	 * Create and initialize the spa structure.
29481635Sbonwick 	 */
2949789Sahrens 	mutex_enter(&spa_namespace_lock);
2950*10921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
29518241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
2952789Sahrens 
2953789Sahrens 	/*
29541635Sbonwick 	 * Pass off the heavy lifting to spa_load().
29551732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
29561732Sbonwick 	 * is actually the one to trust when doing an import.
2957789Sahrens 	 */
2958*10921STim.Haley@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE);
2959789Sahrens 
2960789Sahrens 	/*
2961789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2962789Sahrens 	 */
2963789Sahrens 	if (spa->spa_root_vdev != NULL) {
2964789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2965789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2966789Sahrens 		    poolname) == 0);
2967789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2968789Sahrens 		    state) == 0);
29693975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
29703975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
29712082Seschrock 
29722082Seschrock 		/*
29736423Sgw25295 		 * If the bootfs property exists on this pool then we
29746423Sgw25295 		 * copy it out so that external consumers can tell which
29756423Sgw25295 		 * pools are bootable.
29766423Sgw25295 		 */
29778680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
29786423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
29796423Sgw25295 
29806423Sgw25295 			/*
29816423Sgw25295 			 * We have to play games with the name since the
29826423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
29836423Sgw25295 			 */
29847754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
29856423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
29866423Sgw25295 				char *cp;
29876423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
29886423Sgw25295 
29896423Sgw25295 				cp = strchr(tmpname, '/');
29906423Sgw25295 				if (cp == NULL) {
29916423Sgw25295 					(void) strlcpy(dsname, tmpname,
29926423Sgw25295 					    MAXPATHLEN);
29936423Sgw25295 				} else {
29946423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
29956423Sgw25295 					    "%s/%s", poolname, ++cp);
29966423Sgw25295 				}
29976423Sgw25295 				VERIFY(nvlist_add_string(config,
29986423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
29996423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
30006423Sgw25295 			}
30016423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
30026423Sgw25295 		}
30036423Sgw25295 
30046423Sgw25295 		/*
30055450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
30062082Seschrock 		 */
30079425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
30082082Seschrock 		spa_add_spares(spa, config);
30095450Sbrendan 		spa_add_l2cache(spa, config);
30109425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3011789Sahrens 	}
3012789Sahrens 
3013789Sahrens 	spa_unload(spa);
3014789Sahrens 	spa_deactivate(spa);
3015789Sahrens 	spa_remove(spa);
3016789Sahrens 	mutex_exit(&spa_namespace_lock);
3017789Sahrens 
3018789Sahrens 	return (config);
3019789Sahrens }
3020789Sahrens 
3021789Sahrens /*
3022789Sahrens  * Pool export/destroy
3023789Sahrens  *
3024789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3025789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3026789Sahrens  * update the pool state and sync all the labels to disk, removing the
30278211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
30288211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3029789Sahrens  */
3030789Sahrens static int
30317214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
30328211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3033789Sahrens {
3034789Sahrens 	spa_t *spa;
3035789Sahrens 
30361775Sbillm 	if (oldconfig)
30371775Sbillm 		*oldconfig = NULL;
30381775Sbillm 
30398241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3040789Sahrens 		return (EROFS);
3041789Sahrens 
3042789Sahrens 	mutex_enter(&spa_namespace_lock);
3043789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3044789Sahrens 		mutex_exit(&spa_namespace_lock);
3045789Sahrens 		return (ENOENT);
3046789Sahrens 	}
3047789Sahrens 
3048789Sahrens 	/*
30491544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
30501544Seschrock 	 * reacquire the namespace lock, and see if we can export.
30511544Seschrock 	 */
30521544Seschrock 	spa_open_ref(spa, FTAG);
30531544Seschrock 	mutex_exit(&spa_namespace_lock);
30541544Seschrock 	spa_async_suspend(spa);
30551544Seschrock 	mutex_enter(&spa_namespace_lock);
30561544Seschrock 	spa_close(spa, FTAG);
30571544Seschrock 
30581544Seschrock 	/*
3059789Sahrens 	 * The pool will be in core if it's openable,
3060789Sahrens 	 * in which case we can modify its state.
3061789Sahrens 	 */
3062789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3063789Sahrens 		/*
3064789Sahrens 		 * Objsets may be open only because they're dirty, so we
3065789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3066789Sahrens 		 */
3067789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3068789Sahrens 
30691544Seschrock 		/*
30701544Seschrock 		 * A pool cannot be exported or destroyed if there are active
30711544Seschrock 		 * references.  If we are resetting a pool, allow references by
30721544Seschrock 		 * fault injection handlers.
30731544Seschrock 		 */
30741544Seschrock 		if (!spa_refcount_zero(spa) ||
30751544Seschrock 		    (spa->spa_inject_ref != 0 &&
30761544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
30771544Seschrock 			spa_async_resume(spa);
3078789Sahrens 			mutex_exit(&spa_namespace_lock);
3079789Sahrens 			return (EBUSY);
3080789Sahrens 		}
3081789Sahrens 
3082789Sahrens 		/*
30837214Slling 		 * A pool cannot be exported if it has an active shared spare.
30847214Slling 		 * This is to prevent other pools stealing the active spare
30857214Slling 		 * from an exported pool. At user's own will, such pool can
30867214Slling 		 * be forcedly exported.
30877214Slling 		 */
30887214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
30897214Slling 		    spa_has_active_shared_spare(spa)) {
30907214Slling 			spa_async_resume(spa);
30917214Slling 			mutex_exit(&spa_namespace_lock);
30927214Slling 			return (EXDEV);
30937214Slling 		}
30947214Slling 
30957214Slling 		/*
3096789Sahrens 		 * We want this to be reflected on every label,
3097789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3098789Sahrens 		 * final sync that pushes these changes out.
3099789Sahrens 		 */
31008211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
31017754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31021544Seschrock 			spa->spa_state = new_state;
31031635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
31041544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
31057754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
31061544Seschrock 		}
3107789Sahrens 	}
3108789Sahrens 
31094451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
31104451Seschrock 
3111789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3112789Sahrens 		spa_unload(spa);
3113789Sahrens 		spa_deactivate(spa);
3114789Sahrens 	}
3115789Sahrens 
31161775Sbillm 	if (oldconfig && spa->spa_config)
31171775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
31181775Sbillm 
31191544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
31208211SGeorge.Wilson@Sun.COM 		if (!hardforce)
31218211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
31221544Seschrock 		spa_remove(spa);
31231544Seschrock 	}
3124789Sahrens 	mutex_exit(&spa_namespace_lock);
3125789Sahrens 
3126789Sahrens 	return (0);
3127789Sahrens }
3128789Sahrens 
3129789Sahrens /*
3130789Sahrens  * Destroy a storage pool.
3131789Sahrens  */
3132789Sahrens int
3133789Sahrens spa_destroy(char *pool)
3134789Sahrens {
31358211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
31368211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3137789Sahrens }
3138789Sahrens 
3139789Sahrens /*
3140789Sahrens  * Export a storage pool.
3141789Sahrens  */
3142789Sahrens int
31438211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
31448211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3145789Sahrens {
31468211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
31478211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3148789Sahrens }
3149789Sahrens 
3150789Sahrens /*
31511544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
31521544Seschrock  * from the namespace in any way.
31531544Seschrock  */
31541544Seschrock int
31551544Seschrock spa_reset(char *pool)
31561544Seschrock {
31577214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
31588211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
31591544Seschrock }
31601544Seschrock 
31611544Seschrock /*
3162789Sahrens  * ==========================================================================
3163789Sahrens  * Device manipulation
3164789Sahrens  * ==========================================================================
3165789Sahrens  */
3166789Sahrens 
3167789Sahrens /*
31684527Sperrin  * Add a device to a storage pool.
3169789Sahrens  */
3170789Sahrens int
3171789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3172789Sahrens {
317310594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
31748241SJeff.Bonwick@Sun.COM 	int error;
3175789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
31761585Sbonwick 	vdev_t *vd, *tvd;
31775450Sbrendan 	nvlist_t **spares, **l2cache;
31785450Sbrendan 	uint_t nspares, nl2cache;
3179789Sahrens 
3180789Sahrens 	txg = spa_vdev_enter(spa);
3181789Sahrens 
31822082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
31832082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
31842082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
31852082Seschrock 
31867754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3187789Sahrens 
31885450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
31895450Sbrendan 	    &nspares) != 0)
31902082Seschrock 		nspares = 0;
31912082Seschrock 
31925450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
31935450Sbrendan 	    &nl2cache) != 0)
31945450Sbrendan 		nl2cache = 0;
31955450Sbrendan 
31967754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
31972082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
31987754SJeff.Bonwick@Sun.COM 
31997754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
32007754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
32017754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
32022082Seschrock 
32033377Seschrock 	/*
32045450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
32055450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
32063377Seschrock 	 */
32077754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
32083377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
32093377Seschrock 
32103377Seschrock 	/*
32113377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
32123377Seschrock 	 */
32138241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
321410594SGeorge.Wilson@Sun.COM 
321510594SGeorge.Wilson@Sun.COM 		/*
321610594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
321710594SGeorge.Wilson@Sun.COM 		 */
321810594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
321910594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
322010594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
322110594SGeorge.Wilson@Sun.COM 				break;
322210594SGeorge.Wilson@Sun.COM 			}
322310594SGeorge.Wilson@Sun.COM 		}
32243377Seschrock 		tvd = vd->vdev_child[c];
32253377Seschrock 		vdev_remove_child(vd, tvd);
322610594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
32273377Seschrock 		vdev_add_child(rvd, tvd);
32283377Seschrock 		vdev_config_dirty(tvd);
32293377Seschrock 	}
32303377Seschrock 
32312082Seschrock 	if (nspares != 0) {
32325450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
32335450Sbrendan 		    ZPOOL_CONFIG_SPARES);
32342082Seschrock 		spa_load_spares(spa);
32355450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
32365450Sbrendan 	}
32375450Sbrendan 
32385450Sbrendan 	if (nl2cache != 0) {
32395450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
32405450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
32415450Sbrendan 		spa_load_l2cache(spa);
32425450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3243789Sahrens 	}
3244789Sahrens 
3245789Sahrens 	/*
32461585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
32471585Sbonwick 	 * If other threads start allocating from these vdevs before we
32481585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
32491585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
32501585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
32511585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
32521635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
32531585Sbonwick 	 *
32541585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
32551585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
32561585Sbonwick 	 * steps will be completed the next time we load the pool.
3257789Sahrens 	 */
32581635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
32591585Sbonwick 
32601635Sbonwick 	mutex_enter(&spa_namespace_lock);
32611635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32621635Sbonwick 	mutex_exit(&spa_namespace_lock);
3263789Sahrens 
32641635Sbonwick 	return (0);
3265789Sahrens }
3266789Sahrens 
3267789Sahrens /*
3268789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3269789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3270789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3271789Sahrens  *
3272789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3273789Sahrens  * existing device; in this case the two devices are made into their own
32744451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3275789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3276789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3277789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3278789Sahrens  * is automatically detached.
3279789Sahrens  */
3280789Sahrens int
32811544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3282789Sahrens {
3283789Sahrens 	uint64_t txg, open_txg;
3284789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3285789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
32862082Seschrock 	vdev_ops_t *pvops;
32877313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
32887313SEric.Kustarz@Sun.COM 	int newvd_isspare;
32897313SEric.Kustarz@Sun.COM 	int error;
3290789Sahrens 
3291789Sahrens 	txg = spa_vdev_enter(spa);
3292789Sahrens 
32936643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3294789Sahrens 
3295789Sahrens 	if (oldvd == NULL)
3296789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3297789Sahrens 
32981585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
32991585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
33001585Sbonwick 
3301789Sahrens 	pvd = oldvd->vdev_parent;
3302789Sahrens 
33032082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
33044451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
33054451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
33064451Seschrock 
33074451Seschrock 	if (newrootvd->vdev_children != 1)
3308789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3309789Sahrens 
3310789Sahrens 	newvd = newrootvd->vdev_child[0];
3311789Sahrens 
3312789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3313789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3314789Sahrens 
33152082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3316789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3317789Sahrens 
33184527Sperrin 	/*
33194527Sperrin 	 * Spares can't replace logs
33204527Sperrin 	 */
33217326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
33224527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33234527Sperrin 
33242082Seschrock 	if (!replacing) {
33252082Seschrock 		/*
33262082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
33272082Seschrock 		 * vdev.
33282082Seschrock 		 */
33292082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
33302082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
33312082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33322082Seschrock 
33332082Seschrock 		pvops = &vdev_mirror_ops;
33342082Seschrock 	} else {
33352082Seschrock 		/*
33362082Seschrock 		 * Active hot spares can only be replaced by inactive hot
33372082Seschrock 		 * spares.
33382082Seschrock 		 */
33392082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
33402082Seschrock 		    pvd->vdev_child[1] == oldvd &&
33412082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
33422082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33432082Seschrock 
33442082Seschrock 		/*
33452082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
33462082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
33473377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
33483377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
33493377Seschrock 		 * the same (spare replaces spare, non-spare replaces
33503377Seschrock 		 * non-spare).
33512082Seschrock 		 */
33522082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
33532082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33543377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
33553377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
33563377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
33572082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
33582082Seschrock 		    newvd->vdev_isspare)
33592082Seschrock 			pvops = &vdev_spare_ops;
33602082Seschrock 		else
33612082Seschrock 			pvops = &vdev_replacing_ops;
33622082Seschrock 	}
33632082Seschrock 
33641175Slling 	/*
33659816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
33661175Slling 	 */
33679816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3368789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3369789Sahrens 
33701732Sbonwick 	/*
33711732Sbonwick 	 * The new device cannot have a higher alignment requirement
33721732Sbonwick 	 * than the top-level vdev.
33731732Sbonwick 	 */
33741732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3375789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3376789Sahrens 
3377789Sahrens 	/*
3378789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3379789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3380789Sahrens 	 */
3381789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3382789Sahrens 		spa_strfree(oldvd->vdev_path);
3383789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3384789Sahrens 		    KM_SLEEP);
3385789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3386789Sahrens 		    newvd->vdev_path, "old");
3387789Sahrens 		if (oldvd->vdev_devid != NULL) {
3388789Sahrens 			spa_strfree(oldvd->vdev_devid);
3389789Sahrens 			oldvd->vdev_devid = NULL;
3390789Sahrens 		}
3391789Sahrens 	}
3392789Sahrens 
3393789Sahrens 	/*
33942082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
33952082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3396789Sahrens 	 */
3397789Sahrens 	if (pvd->vdev_ops != pvops)
3398789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3399789Sahrens 
3400789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3401789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3402789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3403789Sahrens 
3404789Sahrens 	/*
3405789Sahrens 	 * Extract the new device from its root and add it to pvd.
3406789Sahrens 	 */
3407789Sahrens 	vdev_remove_child(newrootvd, newvd);
3408789Sahrens 	newvd->vdev_id = pvd->vdev_children;
340910594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3410789Sahrens 	vdev_add_child(pvd, newvd);
3411789Sahrens 
3412789Sahrens 	tvd = newvd->vdev_top;
3413789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3414789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3415789Sahrens 
3416789Sahrens 	vdev_config_dirty(tvd);
3417789Sahrens 
3418789Sahrens 	/*
3419789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3420789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3421789Sahrens 	 */
3422789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3423789Sahrens 
34248241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
34258241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3426789Sahrens 
34279425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
34283377Seschrock 		spa_spare_activate(newvd);
34299425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
34309425SEric.Schrock@Sun.COM 	}
34319425SEric.Schrock@Sun.COM 
34327754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
34337754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
34347313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
34351544Seschrock 
3436789Sahrens 	/*
3437789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3438789Sahrens 	 */
34391732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3440789Sahrens 
3441789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3442789Sahrens 
34439946SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
34449946SMark.Musante@Sun.COM 	    CRED(),  "%s vdev=%s %s vdev=%s",
34459946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
34469946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
34479946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
34487313SEric.Kustarz@Sun.COM 
34497313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
34507313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
34517313SEric.Kustarz@Sun.COM 
3452789Sahrens 	/*
34537046Sahrens 	 * Kick off a resilver to update newvd.
3454789Sahrens 	 */
34557046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3456789Sahrens 
3457789Sahrens 	return (0);
3458789Sahrens }
3459789Sahrens 
3460789Sahrens /*
3461789Sahrens  * Detach a device from a mirror or replacing vdev.
3462789Sahrens  * If 'replace_done' is specified, only detach if the parent
3463789Sahrens  * is a replacing vdev.
3464789Sahrens  */
3465789Sahrens int
34668241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3467789Sahrens {
3468789Sahrens 	uint64_t txg;
34698241SJeff.Bonwick@Sun.COM 	int error;
3470789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3471789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
34722082Seschrock 	boolean_t unspare = B_FALSE;
34732082Seschrock 	uint64_t unspare_guid;
34746673Seschrock 	size_t len;
3475789Sahrens 
3476789Sahrens 	txg = spa_vdev_enter(spa);
3477789Sahrens 
34786643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3479789Sahrens 
3480789Sahrens 	if (vd == NULL)
3481789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3482789Sahrens 
34831585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
34841585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34851585Sbonwick 
3486789Sahrens 	pvd = vd->vdev_parent;
3487789Sahrens 
3488789Sahrens 	/*
34898241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
34908241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
34918241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
34928241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
34938241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
34948241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
34958241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
34968241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
34978241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
34988241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
34998241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
35008241SJeff.Bonwick@Sun.COM 	 */
35018241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
35028241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
35038241SJeff.Bonwick@Sun.COM 
35048241SJeff.Bonwick@Sun.COM 	/*
3505789Sahrens 	 * If replace_done is specified, only remove this device if it's
35062082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
35072082Seschrock 	 * disk can be removed.
3508789Sahrens 	 */
35092082Seschrock 	if (replace_done) {
35102082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
35112082Seschrock 			if (vd->vdev_id != 0)
35122082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35132082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
35142082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35152082Seschrock 		}
35162082Seschrock 	}
35172082Seschrock 
35182082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
35194577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3520789Sahrens 
3521789Sahrens 	/*
35222082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3523789Sahrens 	 */
3524789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
35252082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
35262082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3527789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3528789Sahrens 
3529789Sahrens 	/*
35308241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
35318241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3532789Sahrens 	 */
35338241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3534789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3535789Sahrens 
35368241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
35378241SJeff.Bonwick@Sun.COM 
3538789Sahrens 	/*
35396673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
35406673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
35416673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
35426673Seschrock 	 */
35436673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
35446673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
35456673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
35466673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
35476673Seschrock 		cvd = pvd->vdev_child[0];
35486673Seschrock 		len = strlen(vd->vdev_path);
35496673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
35506673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
35516673Seschrock 			spa_strfree(cvd->vdev_path);
35526673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
35536673Seschrock 		}
35546673Seschrock 	}
35556673Seschrock 
35566673Seschrock 	/*
35572082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
35582082Seschrock 	 * that the spare should become a real disk, and be removed from the
35592082Seschrock 	 * active spare list for the pool.
35602082Seschrock 	 */
35612082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
35628241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
35632082Seschrock 		unspare = B_TRUE;
35642082Seschrock 
35652082Seschrock 	/*
3566789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3567789Sahrens 	 * This must be done after all other error cases are handled,
3568789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3569789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3570789Sahrens 	 * it may be that the unwritability of the disk is the reason
3571789Sahrens 	 * it's being detached!
3572789Sahrens 	 */
35733377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3574789Sahrens 
3575789Sahrens 	/*
3576789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3577789Sahrens 	 */
3578789Sahrens 	vdev_remove_child(pvd, vd);
3579789Sahrens 	vdev_compact_children(pvd);
3580789Sahrens 
3581789Sahrens 	/*
3582789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3583789Sahrens 	 */
3584789Sahrens 	cvd = pvd->vdev_child[0];
3585789Sahrens 
3586789Sahrens 	/*
35872082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
35888241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
35898241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
35908241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
35918241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
35928241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
35938241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
35942082Seschrock 	 */
35952082Seschrock 	if (unspare) {
35962082Seschrock 		ASSERT(cvd->vdev_isspare);
35973377Seschrock 		spa_spare_remove(cvd);
35982082Seschrock 		unspare_guid = cvd->vdev_guid;
35998241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
36002082Seschrock 	}
36012082Seschrock 
36022082Seschrock 	/*
3603789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3604789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3605789Sahrens 	 */
3606789Sahrens 	if (pvd->vdev_children == 1)
3607789Sahrens 		vdev_remove_parent(cvd);
3608789Sahrens 
3609789Sahrens 	/*
3610789Sahrens 	 * We don't set tvd until now because the parent we just removed
3611789Sahrens 	 * may have been the previous top-level vdev.
3612789Sahrens 	 */
3613789Sahrens 	tvd = cvd->vdev_top;
3614789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3615789Sahrens 
3616789Sahrens 	/*
36173377Seschrock 	 * Reevaluate the parent vdev state.
3618789Sahrens 	 */
36194451Seschrock 	vdev_propagate_state(cvd);
3620789Sahrens 
3621789Sahrens 	/*
36229816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
36239816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
36249816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
36259816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
36269816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3627789Sahrens 	 */
36289816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
36299816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
36309816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
36319816SGeorge.Wilson@Sun.COM 	}
3632789Sahrens 
3633789Sahrens 	vdev_config_dirty(tvd);
3634789Sahrens 
3635789Sahrens 	/*
36363377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
36373377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
36383377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
36393377Seschrock 	 * prevent vd from being accessed after it's freed.
3640789Sahrens 	 */
36418241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3642789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
36431732Sbonwick 	vd->vdev_detached = B_TRUE;
36441732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3645789Sahrens 
36464451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
36474451Seschrock 
36482082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
36492082Seschrock 
36502082Seschrock 	/*
36513377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
36523377Seschrock 	 * then we want to go through and remove the device from the hot spare
36533377Seschrock 	 * list of every other pool.
36542082Seschrock 	 */
36552082Seschrock 	if (unspare) {
36568241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
36572082Seschrock 		spa = NULL;
36582082Seschrock 		mutex_enter(&spa_namespace_lock);
36592082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
36602082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
36612082Seschrock 				continue;
36628241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
36638241SJeff.Bonwick@Sun.COM 				continue;
36647793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
36657793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
36662082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
36677793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
36687793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
36692082Seschrock 		}
36702082Seschrock 		mutex_exit(&spa_namespace_lock);
36712082Seschrock 	}
36722082Seschrock 
36732082Seschrock 	return (error);
36742082Seschrock }
36752082Seschrock 
36767754SJeff.Bonwick@Sun.COM static nvlist_t *
36777754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
36782082Seschrock {
36797754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
36807754SJeff.Bonwick@Sun.COM 		uint64_t guid;
36817754SJeff.Bonwick@Sun.COM 
36827754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
36837754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
36847754SJeff.Bonwick@Sun.COM 
36857754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
36867754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
36872082Seschrock 	}
36882082Seschrock 
36897754SJeff.Bonwick@Sun.COM 	return (NULL);
36905450Sbrendan }
36915450Sbrendan 
36927754SJeff.Bonwick@Sun.COM static void
36937754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
36947754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
36955450Sbrendan {
36967754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
36977754SJeff.Bonwick@Sun.COM 
36987754SJeff.Bonwick@Sun.COM 	if (count > 1)
36997754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
37007754SJeff.Bonwick@Sun.COM 
37017754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
37027754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
37037754SJeff.Bonwick@Sun.COM 			continue;
37047754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
37055450Sbrendan 	}
37065450Sbrendan 
37077754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
37087754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
37097754SJeff.Bonwick@Sun.COM 
37107754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
37117754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
37127754SJeff.Bonwick@Sun.COM 
37137754SJeff.Bonwick@Sun.COM 	if (count > 1)
37147754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
37155450Sbrendan }
37165450Sbrendan 
37175450Sbrendan /*
371810594SGeorge.Wilson@Sun.COM  * Removing a device from the vdev namespace requires several steps
371910594SGeorge.Wilson@Sun.COM  * and can take a significant amount of time.  As a result we use
372010594SGeorge.Wilson@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
372110594SGeorge.Wilson@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
372210594SGeorge.Wilson@Sun.COM  * lock.  During each step the configuration is synced out.
372310594SGeorge.Wilson@Sun.COM  */
372410594SGeorge.Wilson@Sun.COM 
372510594SGeorge.Wilson@Sun.COM /*
372610594SGeorge.Wilson@Sun.COM  * Initial phase of device removal - stop future allocations from this device.
372710594SGeorge.Wilson@Sun.COM  */
372810594SGeorge.Wilson@Sun.COM void
372910594SGeorge.Wilson@Sun.COM spa_vdev_remove_start(spa_t *spa, vdev_t *vd)
373010594SGeorge.Wilson@Sun.COM {
373110594SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
373210594SGeorge.Wilson@Sun.COM 
373310594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
373410594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
373510594SGeorge.Wilson@Sun.COM 
373610594SGeorge.Wilson@Sun.COM 	/*
373710594SGeorge.Wilson@Sun.COM 	 * Remove our vdev from the allocatable vdevs
373810594SGeorge.Wilson@Sun.COM 	 */
373910594SGeorge.Wilson@Sun.COM 	if (mg)
374010594SGeorge.Wilson@Sun.COM 		metaslab_class_remove(mg->mg_class, mg);
374110594SGeorge.Wilson@Sun.COM }
374210594SGeorge.Wilson@Sun.COM 
374310594SGeorge.Wilson@Sun.COM /*
374410594SGeorge.Wilson@Sun.COM  * Evacuate the device.
374510594SGeorge.Wilson@Sun.COM  */
374610594SGeorge.Wilson@Sun.COM int
374710594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
374810594SGeorge.Wilson@Sun.COM {
374910594SGeorge.Wilson@Sun.COM 	uint64_t txg;
375010594SGeorge.Wilson@Sun.COM 	int error;
375110594SGeorge.Wilson@Sun.COM 
375210594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
375310594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
375410594SGeorge.Wilson@Sun.COM 
375510594SGeorge.Wilson@Sun.COM 	/*
375610594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
375710594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
375810594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
375910594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
376010594SGeorge.Wilson@Sun.COM 	 */
376110594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
376210594SGeorge.Wilson@Sun.COM 		/*
376310594SGeorge.Wilson@Sun.COM 		 * Evacuate the device.
376410594SGeorge.Wilson@Sun.COM 		 */
376510594SGeorge.Wilson@Sun.COM 		if (error = dmu_objset_find(spa_name(spa),
376610594SGeorge.Wilson@Sun.COM 		    zil_vdev_offline, NULL, DS_FIND_CHILDREN)) {
376710594SGeorge.Wilson@Sun.COM 			uint64_t txg;
376810594SGeorge.Wilson@Sun.COM 
376910594SGeorge.Wilson@Sun.COM 			txg = spa_vdev_config_enter(spa);
377010594SGeorge.Wilson@Sun.COM 			metaslab_class_add(spa->spa_log_class,
377110594SGeorge.Wilson@Sun.COM 			    vd->vdev_mg);
377210594SGeorge.Wilson@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
377310594SGeorge.Wilson@Sun.COM 		}
377410594SGeorge.Wilson@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
377510594SGeorge.Wilson@Sun.COM 	}
377610594SGeorge.Wilson@Sun.COM 
377710594SGeorge.Wilson@Sun.COM 	/*
377810594SGeorge.Wilson@Sun.COM 	 * Remove any remaining MOS metadata associated with the device.
377910594SGeorge.Wilson@Sun.COM 	 */
378010594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
378110594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
378210594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
378310594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
378410594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
378510594SGeorge.Wilson@Sun.COM 
378610594SGeorge.Wilson@Sun.COM 	return (0);
378710594SGeorge.Wilson@Sun.COM }
378810594SGeorge.Wilson@Sun.COM 
378910594SGeorge.Wilson@Sun.COM /*
379010594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
379110594SGeorge.Wilson@Sun.COM  */
379210594SGeorge.Wilson@Sun.COM void
379310594SGeorge.Wilson@Sun.COM spa_vdev_remove_done(spa_t *spa, vdev_t *vd)
379410594SGeorge.Wilson@Sun.COM {
379510594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
379610594SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
379710594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
379810594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
379910594SGeorge.Wilson@Sun.COM 
380010594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
380110594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
380210594SGeorge.Wilson@Sun.COM 
380310594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
380410594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
380510594SGeorge.Wilson@Sun.COM 
380610594SGeorge.Wilson@Sun.COM 	/*
380710594SGeorge.Wilson@Sun.COM 	 * It's possible that another thread is trying todo a spa_vdev_add()
380810594SGeorge.Wilson@Sun.COM 	 * at the same time we're trying remove it. As a result the
380910594SGeorge.Wilson@Sun.COM 	 * added vdev may not have initialized its metaslabs yet.
381010594SGeorge.Wilson@Sun.COM 	 */
381110594SGeorge.Wilson@Sun.COM 	if (mg != NULL)
381210594SGeorge.Wilson@Sun.COM 		metaslab_group_destroy(mg);
381310594SGeorge.Wilson@Sun.COM 
381410594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
381510594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
381610594SGeorge.Wilson@Sun.COM 	} else {
381710594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
381810594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
381910594SGeorge.Wilson@Sun.COM 	}
382010594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(rvd);
382110594SGeorge.Wilson@Sun.COM 
382210594SGeorge.Wilson@Sun.COM 	/*
382310594SGeorge.Wilson@Sun.COM 	 * Reassess the health of our root vdev.
382410594SGeorge.Wilson@Sun.COM 	 */
382510594SGeorge.Wilson@Sun.COM 	vdev_reopen(rvd);
382610594SGeorge.Wilson@Sun.COM }
382710594SGeorge.Wilson@Sun.COM 
382810594SGeorge.Wilson@Sun.COM /*
38295450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
383010594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
38315450Sbrendan  */
38325450Sbrendan int
38335450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
38345450Sbrendan {
38355450Sbrendan 	vdev_t *vd;
38367754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
383710594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
38385450Sbrendan 	uint_t nspares, nl2cache;
38395450Sbrendan 	int error = 0;
38408241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
38418241SJeff.Bonwick@Sun.COM 
38428241SJeff.Bonwick@Sun.COM 	if (!locked)
38438241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
38445450Sbrendan 
38456643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
38465450Sbrendan 
38475450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
38485450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
38497754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
38507754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
38517754SJeff.Bonwick@Sun.COM 		/*
38527754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
38537754SJeff.Bonwick@Sun.COM 		 * in this pool.
38547754SJeff.Bonwick@Sun.COM 		 */
38557754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
38567754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
38577754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
38587754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
38597754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
38607754SJeff.Bonwick@Sun.COM 		} else {
38617754SJeff.Bonwick@Sun.COM 			error = EBUSY;
38627754SJeff.Bonwick@Sun.COM 		}
38637754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
38645450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
38657754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
38667754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
38677754SJeff.Bonwick@Sun.COM 		/*
38687754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
38697754SJeff.Bonwick@Sun.COM 		 */
38707754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
38717754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
38725450Sbrendan 		spa_load_l2cache(spa);
38735450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
387410594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
387510594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
387610594SGeorge.Wilson@Sun.COM 
387710594SGeorge.Wilson@Sun.COM 		/*
387810594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
387910594SGeorge.Wilson@Sun.COM 		 * become the common case.
388010594SGeorge.Wilson@Sun.COM 		 */
388110594SGeorge.Wilson@Sun.COM 
388210594SGeorge.Wilson@Sun.COM 		/*
388310594SGeorge.Wilson@Sun.COM 		 * 1. Stop allocations
388410594SGeorge.Wilson@Sun.COM 		 * 2. Evacuate the device (i.e. kill off stubby and
388510594SGeorge.Wilson@Sun.COM 		 *    metadata) and wait for it to complete (i.e. sync).
388610594SGeorge.Wilson@Sun.COM 		 * 3. Cleanup the vdev namespace.
388710594SGeorge.Wilson@Sun.COM 		 */
388810594SGeorge.Wilson@Sun.COM 		spa_vdev_remove_start(spa, vd);
388910594SGeorge.Wilson@Sun.COM 
389010594SGeorge.Wilson@Sun.COM 		spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
389110594SGeorge.Wilson@Sun.COM 		if ((error = spa_vdev_remove_evacuate(spa, vd)) != 0)
389210594SGeorge.Wilson@Sun.COM 			return (error);
389310594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
389410594SGeorge.Wilson@Sun.COM 
389510594SGeorge.Wilson@Sun.COM 		spa_vdev_remove_done(spa, vd);
389610594SGeorge.Wilson@Sun.COM 
38977754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
38987754SJeff.Bonwick@Sun.COM 		/*
38997754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
39007754SJeff.Bonwick@Sun.COM 		 */
39017754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
39027754SJeff.Bonwick@Sun.COM 	} else {
39037754SJeff.Bonwick@Sun.COM 		/*
39047754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
39057754SJeff.Bonwick@Sun.COM 		 */
39067754SJeff.Bonwick@Sun.COM 		error = ENOENT;
39075450Sbrendan 	}
39082082Seschrock 
39098241SJeff.Bonwick@Sun.COM 	if (!locked)
39108241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
39118241SJeff.Bonwick@Sun.COM 
39128241SJeff.Bonwick@Sun.COM 	return (error);
3913789Sahrens }
3914789Sahrens 
3915789Sahrens /*
39164451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
39174451Seschrock  * current spared, so we can detach it.
3918789Sahrens  */
39191544Seschrock static vdev_t *
39204451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3921789Sahrens {
39221544Seschrock 	vdev_t *newvd, *oldvd;
39239816SGeorge.Wilson@Sun.COM 
39249816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
39254451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
39261544Seschrock 		if (oldvd != NULL)
39271544Seschrock 			return (oldvd);
39281544Seschrock 	}
3929789Sahrens 
39304451Seschrock 	/*
39314451Seschrock 	 * Check for a completed replacement.
39324451Seschrock 	 */
3933789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
39341544Seschrock 		oldvd = vd->vdev_child[0];
39351544Seschrock 		newvd = vd->vdev_child[1];
3936789Sahrens 
39378241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
39388241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
39391544Seschrock 			return (oldvd);
39401544Seschrock 	}
3941789Sahrens 
39424451Seschrock 	/*
39434451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
39444451Seschrock 	 */
39454451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
39464451Seschrock 		newvd = vd->vdev_child[0];
39474451Seschrock 		oldvd = vd->vdev_child[1];
39484451Seschrock 
39494451Seschrock 		if (newvd->vdev_unspare &&
39508241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
39518241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
39524451Seschrock 			newvd->vdev_unspare = 0;
39534451Seschrock 			return (oldvd);
39544451Seschrock 		}
39554451Seschrock 	}
39564451Seschrock 
39571544Seschrock 	return (NULL);
3958789Sahrens }
3959789Sahrens 
39601544Seschrock static void
39614451Seschrock spa_vdev_resilver_done(spa_t *spa)
3962789Sahrens {
39638241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
39648241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
39658241SJeff.Bonwick@Sun.COM 
39668241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3967789Sahrens 
39684451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
39698241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
39708241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
39711544Seschrock 		guid = vd->vdev_guid;
39728241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
39738241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
39748241SJeff.Bonwick@Sun.COM 		sguid = 0;
39752082Seschrock 		/*
39762082Seschrock 		 * If we have just finished replacing a hot spared device, then
39772082Seschrock 		 * we need to detach the parent's first child (the original hot
39782082Seschrock 		 * spare) as well.
39792082Seschrock 		 */
39808241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
39812082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
39828241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
39838241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
39842082Seschrock 		}
39858241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
39868241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
39871544Seschrock 			return;
39888241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
39892082Seschrock 			return;
39908241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3991789Sahrens 	}
3992789Sahrens 
39938241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
3994789Sahrens }
3995789Sahrens 
3996789Sahrens /*
39979425SEric.Schrock@Sun.COM  * Update the stored path or FRU for this vdev.  Dirty the vdev configuration,
39989425SEric.Schrock@Sun.COM  * relying on spa_vdev_enter/exit() to synchronize the labels and cache.
39991354Seschrock  */
40001354Seschrock int
40019425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
40029425SEric.Schrock@Sun.COM     boolean_t ispath)
40031354Seschrock {
40046643Seschrock 	vdev_t *vd;
40051354Seschrock 	uint64_t txg;
40061354Seschrock 
40071354Seschrock 	txg = spa_vdev_enter(spa);
40081354Seschrock 
40099425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
40105450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
40111354Seschrock 
40121585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
40131585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
40141585Sbonwick 
40159425SEric.Schrock@Sun.COM 	if (ispath) {
40169425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_path);
40179425SEric.Schrock@Sun.COM 		vd->vdev_path = spa_strdup(value);
40189425SEric.Schrock@Sun.COM 	} else {
40199425SEric.Schrock@Sun.COM 		if (vd->vdev_fru != NULL)
40209425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
40219425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(value);
40229425SEric.Schrock@Sun.COM 	}
40231354Seschrock 
40241354Seschrock 	vdev_config_dirty(vd->vdev_top);
40251354Seschrock 
40261354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
40271354Seschrock }
40281354Seschrock 
40299425SEric.Schrock@Sun.COM int
40309425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
40319425SEric.Schrock@Sun.COM {
40329425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
40339425SEric.Schrock@Sun.COM }
40349425SEric.Schrock@Sun.COM 
40359425SEric.Schrock@Sun.COM int
40369425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
40379425SEric.Schrock@Sun.COM {
40389425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
40399425SEric.Schrock@Sun.COM }
40409425SEric.Schrock@Sun.COM 
40411354Seschrock /*
4042789Sahrens  * ==========================================================================
4043789Sahrens  * SPA Scrubbing
4044789Sahrens  * ==========================================================================
4045789Sahrens  */
4046789Sahrens 
40477046Sahrens int
40487046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
4049789Sahrens {
40507754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
40514808Sek110237 
4052789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
4053789Sahrens 		return (ENOTSUP);
4054789Sahrens 
4055789Sahrens 	/*
40567046Sahrens 	 * If a resilver was requested, but there is no DTL on a
40577046Sahrens 	 * writeable leaf device, we have nothing to do.
4058789Sahrens 	 */
40597046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
40607046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
40617046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
40621544Seschrock 		return (0);
40631544Seschrock 	}
4064789Sahrens 
40657046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
40667046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
40677046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
40687046Sahrens 		return (EBUSY);
40697046Sahrens 
40707046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
40717046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
40727046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
40737046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
40741544Seschrock 	} else {
40757046Sahrens 		return (EINVAL);
40761544Seschrock 	}
4077789Sahrens }
4078789Sahrens 
40791544Seschrock /*
40801544Seschrock  * ==========================================================================
40811544Seschrock  * SPA async task processing
40821544Seschrock  * ==========================================================================
40831544Seschrock  */
40841544Seschrock 
40851544Seschrock static void
40864451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4087789Sahrens {
40887361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
40897361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
40907361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
409110575SEric.Schrock@Sun.COM 
409210575SEric.Schrock@Sun.COM 		/*
409310575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
409410575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
409510575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
409610575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
409710575SEric.Schrock@Sun.COM 		 */
409810575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
409910575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
410010575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
410110575SEric.Schrock@Sun.COM 
41027754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
41031544Seschrock 	}
41047361SBrendan.Gregg@Sun.COM 
41057754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
41067361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
41071544Seschrock }
41081544Seschrock 
41091544Seschrock static void
41107754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
41117754SJeff.Bonwick@Sun.COM {
41127754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
41137754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
41147754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
41157754SJeff.Bonwick@Sun.COM 	}
41167754SJeff.Bonwick@Sun.COM 
41177754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
41187754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
41197754SJeff.Bonwick@Sun.COM }
41207754SJeff.Bonwick@Sun.COM 
41217754SJeff.Bonwick@Sun.COM static void
41229816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
41239816SGeorge.Wilson@Sun.COM {
41249816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
41259816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
41269816SGeorge.Wilson@Sun.COM 	char *physpath;
41279816SGeorge.Wilson@Sun.COM 
41289816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
41299816SGeorge.Wilson@Sun.COM 		return;
41309816SGeorge.Wilson@Sun.COM 
41319816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
41329816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
41339816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
41349816SGeorge.Wilson@Sun.COM 	}
41359816SGeorge.Wilson@Sun.COM 
41369816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
41379816SGeorge.Wilson@Sun.COM 		return;
41389816SGeorge.Wilson@Sun.COM 
41399816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
41409816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
41419816SGeorge.Wilson@Sun.COM 
41429816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
41439816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
41449816SGeorge.Wilson@Sun.COM 
41459816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
41469816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
41479816SGeorge.Wilson@Sun.COM 
41489816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
41499816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
41509816SGeorge.Wilson@Sun.COM }
41519816SGeorge.Wilson@Sun.COM 
41529816SGeorge.Wilson@Sun.COM static void
41531544Seschrock spa_async_thread(spa_t *spa)
41541544Seschrock {
41557754SJeff.Bonwick@Sun.COM 	int tasks;
41561544Seschrock 
41571544Seschrock 	ASSERT(spa->spa_sync_on);
4158789Sahrens 
41591544Seschrock 	mutex_enter(&spa->spa_async_lock);
41601544Seschrock 	tasks = spa->spa_async_tasks;
41611544Seschrock 	spa->spa_async_tasks = 0;
41621544Seschrock 	mutex_exit(&spa->spa_async_lock);
41631544Seschrock 
41641544Seschrock 	/*
41651635Sbonwick 	 * See if the config needs to be updated.
41661635Sbonwick 	 */
41671635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
41689816SGeorge.Wilson@Sun.COM 		uint64_t oldsz, space_update;
41699816SGeorge.Wilson@Sun.COM 
41701635Sbonwick 		mutex_enter(&spa_namespace_lock);
41719816SGeorge.Wilson@Sun.COM 		oldsz = spa_get_space(spa);
41721635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
41739816SGeorge.Wilson@Sun.COM 		space_update = spa_get_space(spa) - oldsz;
41741635Sbonwick 		mutex_exit(&spa_namespace_lock);
41759816SGeorge.Wilson@Sun.COM 
41769816SGeorge.Wilson@Sun.COM 		/*
41779816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
41789816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
41799816SGeorge.Wilson@Sun.COM 		 */
41809816SGeorge.Wilson@Sun.COM 		if (space_update) {
41819946SMark.Musante@Sun.COM 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
41829946SMark.Musante@Sun.COM 			    spa, NULL, CRED(),
41839946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
41849946SMark.Musante@Sun.COM 			    spa_name(spa), spa_get_space(spa),
41859946SMark.Musante@Sun.COM 			    space_update);
41869816SGeorge.Wilson@Sun.COM 		}
41871635Sbonwick 	}
41881635Sbonwick 
41891635Sbonwick 	/*
41904451Seschrock 	 * See if any devices need to be marked REMOVED.
41911544Seschrock 	 */
41927754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
419310685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
41944451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
41957754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
41967361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
41977754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
41987361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
41997754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
42007754SJeff.Bonwick@Sun.COM 	}
42017754SJeff.Bonwick@Sun.COM 
42029816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
42039816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
42049816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
42059816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
42069816SGeorge.Wilson@Sun.COM 	}
42079816SGeorge.Wilson@Sun.COM 
42087754SJeff.Bonwick@Sun.COM 	/*
42097754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
42107754SJeff.Bonwick@Sun.COM 	 */
42117754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
421210685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
42137754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
42147754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
42154451Seschrock 	}
42161544Seschrock 
42171544Seschrock 	/*
42181544Seschrock 	 * If any devices are done replacing, detach them.
42191544Seschrock 	 */
42204451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
42214451Seschrock 		spa_vdev_resilver_done(spa);
4222789Sahrens 
42231544Seschrock 	/*
42241544Seschrock 	 * Kick off a resilver.
42251544Seschrock 	 */
42267046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
42277046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
42281544Seschrock 
42291544Seschrock 	/*
42301544Seschrock 	 * Let the world know that we're done.
42311544Seschrock 	 */
42321544Seschrock 	mutex_enter(&spa->spa_async_lock);
42331544Seschrock 	spa->spa_async_thread = NULL;
42341544Seschrock 	cv_broadcast(&spa->spa_async_cv);
42351544Seschrock 	mutex_exit(&spa->spa_async_lock);
42361544Seschrock 	thread_exit();
42371544Seschrock }
42381544Seschrock 
42391544Seschrock void
42401544Seschrock spa_async_suspend(spa_t *spa)
42411544Seschrock {
42421544Seschrock 	mutex_enter(&spa->spa_async_lock);
42431544Seschrock 	spa->spa_async_suspended++;
42441544Seschrock 	while (spa->spa_async_thread != NULL)
42451544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
42461544Seschrock 	mutex_exit(&spa->spa_async_lock);
42471544Seschrock }
42481544Seschrock 
42491544Seschrock void
42501544Seschrock spa_async_resume(spa_t *spa)
42511544Seschrock {
42521544Seschrock 	mutex_enter(&spa->spa_async_lock);
42531544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
42541544Seschrock 	spa->spa_async_suspended--;
42551544Seschrock 	mutex_exit(&spa->spa_async_lock);
42561544Seschrock }
42571544Seschrock 
42581544Seschrock static void
42591544Seschrock spa_async_dispatch(spa_t *spa)
42601544Seschrock {
42611544Seschrock 	mutex_enter(&spa->spa_async_lock);
42621544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
42631635Sbonwick 	    spa->spa_async_thread == NULL &&
42641635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
42651544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
42661544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
42671544Seschrock 	mutex_exit(&spa->spa_async_lock);
42681544Seschrock }
42691544Seschrock 
42701544Seschrock void
42711544Seschrock spa_async_request(spa_t *spa, int task)
42721544Seschrock {
42731544Seschrock 	mutex_enter(&spa->spa_async_lock);
42741544Seschrock 	spa->spa_async_tasks |= task;
42751544Seschrock 	mutex_exit(&spa->spa_async_lock);
4276789Sahrens }
4277789Sahrens 
4278789Sahrens /*
4279789Sahrens  * ==========================================================================
4280789Sahrens  * SPA syncing routines
4281789Sahrens  * ==========================================================================
4282789Sahrens  */
4283789Sahrens 
4284789Sahrens static void
4285789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
4286789Sahrens {
4287789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
4288789Sahrens 	dmu_tx_t *tx;
4289789Sahrens 	blkptr_t blk;
4290789Sahrens 	uint64_t itor = 0;
4291789Sahrens 	zio_t *zio;
4292789Sahrens 	int error;
4293789Sahrens 	uint8_t c = 1;
4294789Sahrens 
42957754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
42967754SJeff.Bonwick@Sun.COM 
42977754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
42987754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
42997754SJeff.Bonwick@Sun.COM 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
43007754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
43017754SJeff.Bonwick@Sun.COM 	}
4302789Sahrens 
4303789Sahrens 	error = zio_wait(zio);
4304789Sahrens 	ASSERT3U(error, ==, 0);
4305789Sahrens 
4306789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
4307789Sahrens 	bplist_vacate(bpl, tx);
4308789Sahrens 
4309789Sahrens 	/*
4310789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4311789Sahrens 	 * (Usually only the first block is needed.)
4312789Sahrens 	 */
4313789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
4314789Sahrens 	dmu_tx_commit(tx);
4315789Sahrens }
4316789Sahrens 
4317789Sahrens static void
43182082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
43192082Seschrock {
43202082Seschrock 	char *packed = NULL;
43217497STim.Haley@Sun.COM 	size_t bufsize;
43222082Seschrock 	size_t nvsize = 0;
43232082Seschrock 	dmu_buf_t *db;
43242082Seschrock 
43252082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
43262082Seschrock 
43277497STim.Haley@Sun.COM 	/*
43287497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
43297497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
43307497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
43317497STim.Haley@Sun.COM 	 */
43327497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
43337497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
43342082Seschrock 
43352082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
43362082Seschrock 	    KM_SLEEP) == 0);
43377497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
43387497STim.Haley@Sun.COM 
43397497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
43407497STim.Haley@Sun.COM 
43417497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
43422082Seschrock 
43432082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
43442082Seschrock 	dmu_buf_will_dirty(db, tx);
43452082Seschrock 	*(uint64_t *)db->db_data = nvsize;
43462082Seschrock 	dmu_buf_rele(db, FTAG);
43472082Seschrock }
43482082Seschrock 
43492082Seschrock static void
43505450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
43515450Sbrendan     const char *config, const char *entry)
43522082Seschrock {
43532082Seschrock 	nvlist_t *nvroot;
43545450Sbrendan 	nvlist_t **list;
43552082Seschrock 	int i;
43562082Seschrock 
43575450Sbrendan 	if (!sav->sav_sync)
43582082Seschrock 		return;
43592082Seschrock 
43602082Seschrock 	/*
43615450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
43625450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
43634451Seschrock 	 * valid and the vdevs are labeled appropriately.
43642082Seschrock 	 */
43655450Sbrendan 	if (sav->sav_object == 0) {
43665450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
43675450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
43685450Sbrendan 		    sizeof (uint64_t), tx);
43692082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
43705450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
43715450Sbrendan 		    &sav->sav_object, tx) == 0);
43722082Seschrock 	}
43732082Seschrock 
43742082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
43755450Sbrendan 	if (sav->sav_count == 0) {
43765450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
43772082Seschrock 	} else {
43785450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
43795450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
43805450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
43815450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
43825450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
43835450Sbrendan 		    sav->sav_count) == 0);
43845450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
43855450Sbrendan 			nvlist_free(list[i]);
43865450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
43872082Seschrock 	}
43882082Seschrock 
43895450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
43902926Sek110237 	nvlist_free(nvroot);
43912082Seschrock 
43925450Sbrendan 	sav->sav_sync = B_FALSE;
43932082Seschrock }
43942082Seschrock 
43952082Seschrock static void
4396789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
4397789Sahrens {
4398789Sahrens 	nvlist_t *config;
4399789Sahrens 
44007754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
4401789Sahrens 		return;
4402789Sahrens 
44037754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44047754SJeff.Bonwick@Sun.COM 
44057754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
44067754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
44077754SJeff.Bonwick@Sun.COM 
44087754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
4409789Sahrens 
44101635Sbonwick 	if (spa->spa_config_syncing)
44111635Sbonwick 		nvlist_free(spa->spa_config_syncing);
44121635Sbonwick 	spa->spa_config_syncing = config;
4413789Sahrens 
44142082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
4415789Sahrens }
4416789Sahrens 
44175094Slling /*
44185094Slling  * Set zpool properties.
44195094Slling  */
44203912Slling static void
44214543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
44223912Slling {
44233912Slling 	spa_t *spa = arg1;
44245094Slling 	objset_t *mos = spa->spa_meta_objset;
44253912Slling 	nvlist_t *nvp = arg2;
44265094Slling 	nvpair_t *elem;
44274451Seschrock 	uint64_t intval;
44286643Seschrock 	char *strval;
44295094Slling 	zpool_prop_t prop;
44305094Slling 	const char *propname;
44315094Slling 	zprop_type_t proptype;
44325094Slling 
44337754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
44347754SJeff.Bonwick@Sun.COM 
44355094Slling 	elem = NULL;
44365094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
44375094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
44385094Slling 		case ZPOOL_PROP_VERSION:
44395094Slling 			/*
44405094Slling 			 * Only set version for non-zpool-creation cases
44415094Slling 			 * (set/import). spa_create() needs special care
44425094Slling 			 * for version setting.
44435094Slling 			 */
44445094Slling 			if (tx->tx_txg != TXG_INITIAL) {
44455094Slling 				VERIFY(nvpair_value_uint64(elem,
44465094Slling 				    &intval) == 0);
44475094Slling 				ASSERT(intval <= SPA_VERSION);
44485094Slling 				ASSERT(intval >= spa_version(spa));
44495094Slling 				spa->spa_uberblock.ub_version = intval;
44505094Slling 				vdev_config_dirty(spa->spa_root_vdev);
44515094Slling 			}
44525094Slling 			break;
44535094Slling 
44545094Slling 		case ZPOOL_PROP_ALTROOT:
44555094Slling 			/*
44565094Slling 			 * 'altroot' is a non-persistent property. It should
44575094Slling 			 * have been set temporarily at creation or import time.
44585094Slling 			 */
44595094Slling 			ASSERT(spa->spa_root != NULL);
44605094Slling 			break;
44615094Slling 
44625363Seschrock 		case ZPOOL_PROP_CACHEFILE:
44635094Slling 			/*
44648525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
44655094Slling 			 */
44664543Smarks 			break;
44675094Slling 		default:
44685094Slling 			/*
44695094Slling 			 * Set pool property values in the poolprops mos object.
44705094Slling 			 */
44715094Slling 			if (spa->spa_pool_props_object == 0) {
44725094Slling 				objset_t *mos = spa->spa_meta_objset;
44735094Slling 
44745094Slling 				VERIFY((spa->spa_pool_props_object =
44755094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
44765094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
44775094Slling 
44785094Slling 				VERIFY(zap_update(mos,
44795094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
44805094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
44815094Slling 				    == 0);
44825094Slling 			}
44835094Slling 
44845094Slling 			/* normalize the property name */
44855094Slling 			propname = zpool_prop_to_name(prop);
44865094Slling 			proptype = zpool_prop_get_type(prop);
44875094Slling 
44885094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
44895094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
44905094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
44915094Slling 				VERIFY(zap_update(mos,
44925094Slling 				    spa->spa_pool_props_object, propname,
44935094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
44945094Slling 
44955094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
44965094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
44975094Slling 
44985094Slling 				if (proptype == PROP_TYPE_INDEX) {
44995094Slling 					const char *unused;
45005094Slling 					VERIFY(zpool_prop_index_to_string(
45015094Slling 					    prop, intval, &unused) == 0);
45025094Slling 				}
45035094Slling 				VERIFY(zap_update(mos,
45045094Slling 				    spa->spa_pool_props_object, propname,
45055094Slling 				    8, 1, &intval, tx) == 0);
45065094Slling 			} else {
45075094Slling 				ASSERT(0); /* not allowed */
45085094Slling 			}
45095094Slling 
45105329Sgw25295 			switch (prop) {
45115329Sgw25295 			case ZPOOL_PROP_DELEGATION:
45125094Slling 				spa->spa_delegation = intval;
45135329Sgw25295 				break;
45145329Sgw25295 			case ZPOOL_PROP_BOOTFS:
45155094Slling 				spa->spa_bootfs = intval;
45165329Sgw25295 				break;
45175329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
45185329Sgw25295 				spa->spa_failmode = intval;
45195329Sgw25295 				break;
45209816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
45219816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
45229816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
45239816SGeorge.Wilson@Sun.COM 				break;
45245329Sgw25295 			default:
45255329Sgw25295 				break;
45265329Sgw25295 			}
45273912Slling 		}
45285094Slling 
45295094Slling 		/* log internal history if this is not a zpool create */
45305094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
45315094Slling 		    tx->tx_txg != TXG_INITIAL) {
45325094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
45335094Slling 			    spa, tx, cr, "%s %lld %s",
45347754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
45355094Slling 		}
45363912Slling 	}
45377754SJeff.Bonwick@Sun.COM 
45387754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
45393912Slling }
45403912Slling 
4541789Sahrens /*
4542789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
4543789Sahrens  * part of the process, so we iterate until it converges.
4544789Sahrens  */
4545789Sahrens void
4546789Sahrens spa_sync(spa_t *spa, uint64_t txg)
4547789Sahrens {
4548789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
4549789Sahrens 	objset_t *mos = spa->spa_meta_objset;
4550789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
45511635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4552789Sahrens 	vdev_t *vd;
4553789Sahrens 	dmu_tx_t *tx;
4554789Sahrens 	int dirty_vdevs;
45557754SJeff.Bonwick@Sun.COM 	int error;
4556789Sahrens 
4557789Sahrens 	/*
4558789Sahrens 	 * Lock out configuration changes.
4559789Sahrens 	 */
45607754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4561789Sahrens 
4562789Sahrens 	spa->spa_syncing_txg = txg;
4563789Sahrens 	spa->spa_sync_pass = 0;
4564789Sahrens 
45657754SJeff.Bonwick@Sun.COM 	/*
45667754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
45677754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
45687754SJeff.Bonwick@Sun.COM 	 */
45697754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
45708241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
45718241SJeff.Bonwick@Sun.COM 		/*
45728241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
45738241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
45748241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
45758241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
45768241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
45778241SJeff.Bonwick@Sun.COM 		 */
45788241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
45798241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
45808241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
45818241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
45828241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
45838241SJeff.Bonwick@Sun.COM 		}
45848241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
45858241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
45867754SJeff.Bonwick@Sun.COM 	}
45877754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
45887754SJeff.Bonwick@Sun.COM 
45891544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4590789Sahrens 
45912082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
45922082Seschrock 
45932082Seschrock 	/*
45944577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
45952082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
45962082Seschrock 	 */
45974577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
45984577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
45992082Seschrock 		int i;
46002082Seschrock 
46012082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
46022082Seschrock 			vd = rvd->vdev_child[i];
46032082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
46042082Seschrock 				break;
46052082Seschrock 		}
46062082Seschrock 		if (i == rvd->vdev_children) {
46072082Seschrock 			spa->spa_deflate = TRUE;
46082082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
46092082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
46102082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
46112082Seschrock 		}
46122082Seschrock 	}
46132082Seschrock 
46147046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
46157046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
46167046Sahrens 		dsl_pool_create_origin(dp, tx);
46177046Sahrens 
46187046Sahrens 		/* Keeping the origin open increases spa_minref */
46197046Sahrens 		spa->spa_minref += 3;
46207046Sahrens 	}
46217046Sahrens 
46227046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
46237046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
46247046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
46257046Sahrens 	}
46267046Sahrens 
4627789Sahrens 	/*
4628789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4629789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4630789Sahrens 	 * don't generate work on an otherwise idle system.
4631789Sahrens 	 */
4632789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
46332329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
46342329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4635789Sahrens 		spa_sync_deferred_frees(spa, txg);
4636789Sahrens 
4637789Sahrens 	/*
4638789Sahrens 	 * Iterate to convergence.
4639789Sahrens 	 */
4640789Sahrens 	do {
4641789Sahrens 		spa->spa_sync_pass++;
4642789Sahrens 
4643789Sahrens 		spa_sync_config_object(spa, tx);
46445450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
46455450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
46465450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
46475450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
46481544Seschrock 		spa_errlog_sync(spa, txg);
4649789Sahrens 		dsl_pool_sync(dp, txg);
4650789Sahrens 
4651789Sahrens 		dirty_vdevs = 0;
4652789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4653789Sahrens 			vdev_sync(vd, txg);
4654789Sahrens 			dirty_vdevs++;
4655789Sahrens 		}
4656789Sahrens 
4657789Sahrens 		bplist_sync(bpl, tx);
4658789Sahrens 	} while (dirty_vdevs);
4659789Sahrens 
4660789Sahrens 	bplist_close(bpl);
4661789Sahrens 
4662789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4663789Sahrens 
4664789Sahrens 	/*
4665789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4666789Sahrens 	 * to commit the transaction group.
46671635Sbonwick 	 *
46685688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
46695688Sbonwick 	 * random top-level vdevs that are known to be visible in the
46707754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
46717754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4672789Sahrens 	 */
46737754SJeff.Bonwick@Sun.COM 	for (;;) {
46747754SJeff.Bonwick@Sun.COM 		/*
46757754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
46767754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
46777754SJeff.Bonwick@Sun.COM 		 */
46787754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
46797754SJeff.Bonwick@Sun.COM 
46807754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
46817754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
46827754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
46837754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
46847754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
46859816SGeorge.Wilson@Sun.COM 
46869816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
46877754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
46887754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
46897754SJeff.Bonwick@Sun.COM 					continue;
46907754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
46917754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
46927754SJeff.Bonwick@Sun.COM 					break;
46937754SJeff.Bonwick@Sun.COM 			}
46949725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
46959725SEric.Schrock@Sun.COM 			if (error != 0)
46969725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
46979725SEric.Schrock@Sun.COM 				    B_TRUE);
46987754SJeff.Bonwick@Sun.COM 		} else {
46997754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
47009725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
47019725SEric.Schrock@Sun.COM 			if (error != 0)
47029725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
47039725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
47041635Sbonwick 		}
47057754SJeff.Bonwick@Sun.COM 
47067754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
47077754SJeff.Bonwick@Sun.COM 
47087754SJeff.Bonwick@Sun.COM 		if (error == 0)
47097754SJeff.Bonwick@Sun.COM 			break;
47107754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
47117754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
47121635Sbonwick 	}
47132082Seschrock 	dmu_tx_commit(tx);
47142082Seschrock 
47151635Sbonwick 	/*
47161635Sbonwick 	 * Clear the dirty config list.
47171635Sbonwick 	 */
47187754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
47191635Sbonwick 		vdev_config_clean(vd);
47201635Sbonwick 
47211635Sbonwick 	/*
47221635Sbonwick 	 * Now that the new config has synced transactionally,
47231635Sbonwick 	 * let it become visible to the config cache.
47241635Sbonwick 	 */
47251635Sbonwick 	if (spa->spa_config_syncing != NULL) {
47261635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
47271635Sbonwick 		spa->spa_config_txg = txg;
47281635Sbonwick 		spa->spa_config_syncing = NULL;
47291635Sbonwick 	}
4730789Sahrens 
4731789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4732789Sahrens 
4733789Sahrens 	/*
4734789Sahrens 	 * Clean up the ZIL records for the synced txg.
4735789Sahrens 	 */
4736789Sahrens 	dsl_pool_zil_clean(dp);
4737789Sahrens 
4738789Sahrens 	/*
4739789Sahrens 	 * Update usable space statistics.
4740789Sahrens 	 */
4741789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4742789Sahrens 		vdev_sync_done(vd, txg);
4743789Sahrens 
4744789Sahrens 	/*
4745789Sahrens 	 * It had better be the case that we didn't dirty anything
47462082Seschrock 	 * since vdev_config_sync().
4747789Sahrens 	 */
4748789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4749789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4750789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4751789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4752789Sahrens 
47537754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
47541544Seschrock 
4755*10921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
4756*10921STim.Haley@Sun.COM 
47571544Seschrock 	/*
47581544Seschrock 	 * If any async tasks have been requested, kick them off.
47591544Seschrock 	 */
47601544Seschrock 	spa_async_dispatch(spa);
4761789Sahrens }
4762789Sahrens 
4763789Sahrens /*
4764789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4765789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4766789Sahrens  * sync.
4767789Sahrens  */
4768789Sahrens void
4769789Sahrens spa_sync_allpools(void)
4770789Sahrens {
4771789Sahrens 	spa_t *spa = NULL;
4772789Sahrens 	mutex_enter(&spa_namespace_lock);
4773789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
47747754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4775789Sahrens 			continue;
4776789Sahrens 		spa_open_ref(spa, FTAG);
4777789Sahrens 		mutex_exit(&spa_namespace_lock);
4778789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4779789Sahrens 		mutex_enter(&spa_namespace_lock);
4780789Sahrens 		spa_close(spa, FTAG);
4781789Sahrens 	}
4782789Sahrens 	mutex_exit(&spa_namespace_lock);
4783789Sahrens }
4784789Sahrens 
4785789Sahrens /*
4786789Sahrens  * ==========================================================================
4787789Sahrens  * Miscellaneous routines
4788789Sahrens  * ==========================================================================
4789789Sahrens  */
4790789Sahrens 
4791789Sahrens /*
4792789Sahrens  * Remove all pools in the system.
4793789Sahrens  */
4794789Sahrens void
4795789Sahrens spa_evict_all(void)
4796789Sahrens {
4797789Sahrens 	spa_t *spa;
4798789Sahrens 
4799789Sahrens 	/*
4800789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4801789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4802789Sahrens 	 */
4803789Sahrens 	mutex_enter(&spa_namespace_lock);
4804789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4805789Sahrens 		/*
48061544Seschrock 		 * Stop async tasks.  The async thread may need to detach
48071544Seschrock 		 * a device that's been replaced, which requires grabbing
48081544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4809789Sahrens 		 */
4810789Sahrens 		spa_open_ref(spa, FTAG);
4811789Sahrens 		mutex_exit(&spa_namespace_lock);
48121544Seschrock 		spa_async_suspend(spa);
48134808Sek110237 		mutex_enter(&spa_namespace_lock);
4814789Sahrens 		spa_close(spa, FTAG);
4815789Sahrens 
4816789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4817789Sahrens 			spa_unload(spa);
4818789Sahrens 			spa_deactivate(spa);
4819789Sahrens 		}
4820789Sahrens 		spa_remove(spa);
4821789Sahrens 	}
4822789Sahrens 	mutex_exit(&spa_namespace_lock);
4823789Sahrens }
48241544Seschrock 
48251544Seschrock vdev_t *
48269425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
48271544Seschrock {
48286643Seschrock 	vdev_t *vd;
48296643Seschrock 	int i;
48306643Seschrock 
48316643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
48326643Seschrock 		return (vd);
48336643Seschrock 
48349425SEric.Schrock@Sun.COM 	if (aux) {
48356643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
48366643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
48376643Seschrock 			if (vd->vdev_guid == guid)
48386643Seschrock 				return (vd);
48396643Seschrock 		}
48409425SEric.Schrock@Sun.COM 
48419425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
48429425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
48439425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
48449425SEric.Schrock@Sun.COM 				return (vd);
48459425SEric.Schrock@Sun.COM 		}
48466643Seschrock 	}
48476643Seschrock 
48486643Seschrock 	return (NULL);
48491544Seschrock }
48501760Seschrock 
48511760Seschrock void
48525094Slling spa_upgrade(spa_t *spa, uint64_t version)
48531760Seschrock {
48547754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
48551760Seschrock 
48561760Seschrock 	/*
48571760Seschrock 	 * This should only be called for a non-faulted pool, and since a
48581760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
48591760Seschrock 	 * possible.
48601760Seschrock 	 */
48614577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
48625094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
48635094Slling 
48645094Slling 	spa->spa_uberblock.ub_version = version;
48651760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
48661760Seschrock 
48677754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
48682082Seschrock 
48692082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
48701760Seschrock }
48712082Seschrock 
48722082Seschrock boolean_t
48732082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
48742082Seschrock {
48752082Seschrock 	int i;
48763377Seschrock 	uint64_t spareguid;
48775450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
48785450Sbrendan 
48795450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
48805450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
48812082Seschrock 			return (B_TRUE);
48822082Seschrock 
48835450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
48845450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
48855450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
48863377Seschrock 			return (B_TRUE);
48873377Seschrock 	}
48883377Seschrock 
48892082Seschrock 	return (B_FALSE);
48902082Seschrock }
48913912Slling 
48924451Seschrock /*
48937214Slling  * Check if a pool has an active shared spare device.
48947214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
48957214Slling  */
48967214Slling static boolean_t
48977214Slling spa_has_active_shared_spare(spa_t *spa)
48987214Slling {
48997214Slling 	int i, refcnt;
49007214Slling 	uint64_t pool;
49017214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
49027214Slling 
49037214Slling 	for (i = 0; i < sav->sav_count; i++) {
49047214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
49057214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
49067214Slling 		    refcnt > 2)
49077214Slling 			return (B_TRUE);
49087214Slling 	}
49097214Slling 
49107214Slling 	return (B_FALSE);
49117214Slling }
49127214Slling 
49137214Slling /*
49144451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
49154451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
49164451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
49174451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
49184451Seschrock  * or zdb as real changes.
49194451Seschrock  */
49204451Seschrock void
49214451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
49224451Seschrock {
49234451Seschrock #ifdef _KERNEL
49244451Seschrock 	sysevent_t		*ev;
49254451Seschrock 	sysevent_attr_list_t	*attr = NULL;
49264451Seschrock 	sysevent_value_t	value;
49274451Seschrock 	sysevent_id_t		eid;
49284451Seschrock 
49294451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
49304451Seschrock 	    SE_SLEEP);
49314451Seschrock 
49324451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
49334451Seschrock 	value.value.sv_string = spa_name(spa);
49344451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
49354451Seschrock 		goto done;
49364451Seschrock 
49374451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
49384451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
49394451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
49404451Seschrock 		goto done;
49414451Seschrock 
49424451Seschrock 	if (vd) {
49434451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
49444451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
49454451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
49464451Seschrock 		    SE_SLEEP) != 0)
49474451Seschrock 			goto done;
49484451Seschrock 
49494451Seschrock 		if (vd->vdev_path) {
49504451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
49514451Seschrock 			value.value.sv_string = vd->vdev_path;
49524451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
49534451Seschrock 			    &value, SE_SLEEP) != 0)
49544451Seschrock 				goto done;
49554451Seschrock 		}
49564451Seschrock 	}
49574451Seschrock 
49585756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
49595756Seschrock 		goto done;
49605756Seschrock 	attr = NULL;
49615756Seschrock 
49624451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
49634451Seschrock 
49644451Seschrock done:
49654451Seschrock 	if (attr)
49664451Seschrock 		sysevent_free_attr(attr);
49674451Seschrock 	sysevent_free(ev);
49684451Seschrock #endif
49694451Seschrock }
4970