xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 10685)
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>
678662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
688662SJordan.Vaughan@Sun.com 
695094Slling #include "zfs_prop.h"
705913Sperrin #include "zfs_comutil.h"
715094Slling 
729515SJonathan.Adams@Sun.COM enum zti_modes {
739515SJonathan.Adams@Sun.COM 	zti_mode_fixed,			/* value is # of threads (min 1) */
749515SJonathan.Adams@Sun.COM 	zti_mode_online_percent,	/* value is % of online CPUs */
759515SJonathan.Adams@Sun.COM 	zti_mode_tune,			/* fill from zio_taskq_tune_* */
769515SJonathan.Adams@Sun.COM 	zti_nmodes
777754SJeff.Bonwick@Sun.COM };
782986Sek110237 
799515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_FIX(n)	{ zti_mode_fixed, (n) }
809515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_PCT(n)	{ zti_mode_online_percent, (n) }
819515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_TUNE		{ zti_mode_tune, 0 }
829515SJonathan.Adams@Sun.COM 
839515SJonathan.Adams@Sun.COM #define	ZTI_THREAD_ONE		ZTI_THREAD_FIX(1)
849515SJonathan.Adams@Sun.COM 
859515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info {
869515SJonathan.Adams@Sun.COM 	const char *zti_name;
879515SJonathan.Adams@Sun.COM 	struct {
889515SJonathan.Adams@Sun.COM 		enum zti_modes zti_mode;
899515SJonathan.Adams@Sun.COM 		uint_t zti_value;
909515SJonathan.Adams@Sun.COM 	} zti_nthreads[ZIO_TASKQ_TYPES];
919515SJonathan.Adams@Sun.COM } zio_taskq_info_t;
929515SJonathan.Adams@Sun.COM 
939515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
949515SJonathan.Adams@Sun.COM 				"issue",		"intr"
959515SJonathan.Adams@Sun.COM };
969515SJonathan.Adams@Sun.COM 
979515SJonathan.Adams@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = {
989515SJonathan.Adams@Sun.COM 	/*			ISSUE			INTR		*/
999515SJonathan.Adams@Sun.COM 	{ "spa_zio_null",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1009515SJonathan.Adams@Sun.COM 	{ "spa_zio_read",	{ ZTI_THREAD_FIX(8),	ZTI_THREAD_TUNE } },
1019515SJonathan.Adams@Sun.COM 	{ "spa_zio_write",	{ ZTI_THREAD_TUNE,	ZTI_THREAD_FIX(8) } },
1029515SJonathan.Adams@Sun.COM 	{ "spa_zio_free",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1039515SJonathan.Adams@Sun.COM 	{ "spa_zio_claim",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1049515SJonathan.Adams@Sun.COM 	{ "spa_zio_ioctl",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1059515SJonathan.Adams@Sun.COM };
1069515SJonathan.Adams@Sun.COM 
1079515SJonathan.Adams@Sun.COM enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent;
1089515SJonathan.Adams@Sun.COM uint_t zio_taskq_tune_value = 80;	/* #threads = 80% of # online CPUs */
1099515SJonathan.Adams@Sun.COM 
1105094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
1117214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
1125094Slling 
1135094Slling /*
1145094Slling  * ==========================================================================
1155094Slling  * SPA properties routines
1165094Slling  * ==========================================================================
1175094Slling  */
1185094Slling 
1195094Slling /*
1205094Slling  * Add a (source=src, propname=propval) list to an nvlist.
1215094Slling  */
1225949Slling static void
1235094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
1245094Slling     uint64_t intval, zprop_source_t src)
1255094Slling {
1265094Slling 	const char *propname = zpool_prop_to_name(prop);
1275094Slling 	nvlist_t *propval;
1285949Slling 
1295949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1305949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1315949Slling 
1325949Slling 	if (strval != NULL)
1335949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1345949Slling 	else
1355949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1365949Slling 
1375949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1385094Slling 	nvlist_free(propval);
1395094Slling }
1405094Slling 
1415094Slling /*
1425094Slling  * Get property values from the spa configuration.
1435094Slling  */
1445949Slling static void
1455094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1465094Slling {
1478525SEric.Schrock@Sun.COM 	uint64_t size;
1488525SEric.Schrock@Sun.COM 	uint64_t used;
1495094Slling 	uint64_t cap, version;
1505094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1516643Seschrock 	spa_config_dirent_t *dp;
1525094Slling 
1537754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1547754SJeff.Bonwick@Sun.COM 
1558525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
1568525SEric.Schrock@Sun.COM 		size = spa_get_space(spa);
1578525SEric.Schrock@Sun.COM 		used = spa_get_alloc(spa);
1588525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1598525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1608525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1618525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
1628525SEric.Schrock@Sun.COM 		    size - used, src);
1638525SEric.Schrock@Sun.COM 
1648525SEric.Schrock@Sun.COM 		cap = (size == 0) ? 0 : (used * 100 / size);
1658525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1668525SEric.Schrock@Sun.COM 
1678525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1688525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1698525SEric.Schrock@Sun.COM 
1708525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1718525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1728525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1738525SEric.Schrock@Sun.COM 		else
1748525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1758525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1768525SEric.Schrock@Sun.COM 	}
1775949Slling 
1785949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1795949Slling 
1805949Slling 	if (spa->spa_root != NULL)
1815949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1825949Slling 		    0, ZPROP_SRC_LOCAL);
1835094Slling 
1846643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1856643Seschrock 		if (dp->scd_path == NULL) {
1865949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1876643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1886643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1895949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1906643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1915363Seschrock 		}
1925363Seschrock 	}
1935094Slling }
1945094Slling 
1955094Slling /*
1965094Slling  * Get zpool property values.
1975094Slling  */
1985094Slling int
1995094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2005094Slling {
2015094Slling 	zap_cursor_t zc;
2025094Slling 	zap_attribute_t za;
2035094Slling 	objset_t *mos = spa->spa_meta_objset;
2045094Slling 	int err;
2055094Slling 
2065949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2075094Slling 
2087754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2097754SJeff.Bonwick@Sun.COM 
2105094Slling 	/*
2115094Slling 	 * Get properties from the spa config.
2125094Slling 	 */
2135949Slling 	spa_prop_get_config(spa, nvp);
2145094Slling 
2155094Slling 	/* If no pool property object, no more prop to get. */
2165094Slling 	if (spa->spa_pool_props_object == 0) {
2175094Slling 		mutex_exit(&spa->spa_props_lock);
2185094Slling 		return (0);
2195094Slling 	}
2205094Slling 
2215094Slling 	/*
2225094Slling 	 * Get properties from the MOS pool property object.
2235094Slling 	 */
2245094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
2255094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
2265094Slling 	    zap_cursor_advance(&zc)) {
2275094Slling 		uint64_t intval = 0;
2285094Slling 		char *strval = NULL;
2295094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
2305094Slling 		zpool_prop_t prop;
2315094Slling 
2325094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
2335094Slling 			continue;
2345094Slling 
2355094Slling 		switch (za.za_integer_length) {
2365094Slling 		case 8:
2375094Slling 			/* integer property */
2385094Slling 			if (za.za_first_integer !=
2395094Slling 			    zpool_prop_default_numeric(prop))
2405094Slling 				src = ZPROP_SRC_LOCAL;
2415094Slling 
2425094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2435094Slling 				dsl_pool_t *dp;
2445094Slling 				dsl_dataset_t *ds = NULL;
2455094Slling 
2465094Slling 				dp = spa_get_dsl(spa);
2475094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2486689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2496689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2505094Slling 					rw_exit(&dp->dp_config_rwlock);
2515094Slling 					break;
2525094Slling 				}
2535094Slling 
2545094Slling 				strval = kmem_alloc(
2555094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2565094Slling 				    KM_SLEEP);
2575094Slling 				dsl_dataset_name(ds, strval);
2586689Smaybee 				dsl_dataset_rele(ds, FTAG);
2595094Slling 				rw_exit(&dp->dp_config_rwlock);
2605094Slling 			} else {
2615094Slling 				strval = NULL;
2625094Slling 				intval = za.za_first_integer;
2635094Slling 			}
2645094Slling 
2655949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2665094Slling 
2675094Slling 			if (strval != NULL)
2685094Slling 				kmem_free(strval,
2695094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2705094Slling 
2715094Slling 			break;
2725094Slling 
2735094Slling 		case 1:
2745094Slling 			/* string property */
2755094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2765094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2775094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2785094Slling 			if (err) {
2795094Slling 				kmem_free(strval, za.za_num_integers);
2805094Slling 				break;
2815094Slling 			}
2825949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2835094Slling 			kmem_free(strval, za.za_num_integers);
2845094Slling 			break;
2855094Slling 
2865094Slling 		default:
2875094Slling 			break;
2885094Slling 		}
2895094Slling 	}
2905094Slling 	zap_cursor_fini(&zc);
2915094Slling 	mutex_exit(&spa->spa_props_lock);
2925094Slling out:
2935094Slling 	if (err && err != ENOENT) {
2945094Slling 		nvlist_free(*nvp);
2955949Slling 		*nvp = NULL;
2965094Slling 		return (err);
2975094Slling 	}
2985094Slling 
2995094Slling 	return (0);
3005094Slling }
3015094Slling 
3025094Slling /*
3035094Slling  * Validate the given pool properties nvlist and modify the list
3045094Slling  * for the property values to be set.
3055094Slling  */
3065094Slling static int
3075094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
3085094Slling {
3095094Slling 	nvpair_t *elem;
3105094Slling 	int error = 0, reset_bootfs = 0;
3115094Slling 	uint64_t objnum;
3125094Slling 
3135094Slling 	elem = NULL;
3145094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3155094Slling 		zpool_prop_t prop;
3165094Slling 		char *propname, *strval;
3175094Slling 		uint64_t intval;
3185094Slling 		objset_t *os;
3195363Seschrock 		char *slash;
3205094Slling 
3215094Slling 		propname = nvpair_name(elem);
3225094Slling 
3235094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
3245094Slling 			return (EINVAL);
3255094Slling 
3265094Slling 		switch (prop) {
3275094Slling 		case ZPOOL_PROP_VERSION:
3285094Slling 			error = nvpair_value_uint64(elem, &intval);
3295094Slling 			if (!error &&
3305094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
3315094Slling 				error = EINVAL;
3325094Slling 			break;
3335094Slling 
3345094Slling 		case ZPOOL_PROP_DELEGATION:
3355094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3367538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3379816SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_AUTOEXPAND:
3385094Slling 			error = nvpair_value_uint64(elem, &intval);
3395094Slling 			if (!error && intval > 1)
3405094Slling 				error = EINVAL;
3415094Slling 			break;
3425094Slling 
3435094Slling 		case ZPOOL_PROP_BOOTFS:
3449630SJeff.Bonwick@Sun.COM 			/*
3459630SJeff.Bonwick@Sun.COM 			 * If the pool version is less than SPA_VERSION_BOOTFS,
3469630SJeff.Bonwick@Sun.COM 			 * or the pool is still being created (version == 0),
3479630SJeff.Bonwick@Sun.COM 			 * the bootfs property cannot be set.
3489630SJeff.Bonwick@Sun.COM 			 */
3495094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3505094Slling 				error = ENOTSUP;
3515094Slling 				break;
3525094Slling 			}
3535094Slling 
3545094Slling 			/*
3557042Sgw25295 			 * Make sure the vdev config is bootable
3565094Slling 			 */
3577042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3585094Slling 				error = ENOTSUP;
3595094Slling 				break;
3605094Slling 			}
3615094Slling 
3625094Slling 			reset_bootfs = 1;
3635094Slling 
3645094Slling 			error = nvpair_value_string(elem, &strval);
3655094Slling 
3665094Slling 			if (!error) {
3677042Sgw25295 				uint64_t compress;
3687042Sgw25295 
3695094Slling 				if (strval == NULL || strval[0] == '\0') {
3705094Slling 					objnum = zpool_prop_default_numeric(
3715094Slling 					    ZPOOL_PROP_BOOTFS);
3725094Slling 					break;
3735094Slling 				}
3745094Slling 
37510298SMatthew.Ahrens@Sun.COM 				if (error = dmu_objset_hold(strval, FTAG, &os))
3765094Slling 					break;
3777042Sgw25295 
37810298SMatthew.Ahrens@Sun.COM 				/* Must be ZPL and not gzip compressed. */
37910298SMatthew.Ahrens@Sun.COM 
38010298SMatthew.Ahrens@Sun.COM 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
38110298SMatthew.Ahrens@Sun.COM 					error = ENOTSUP;
38210298SMatthew.Ahrens@Sun.COM 				} else if ((error = dsl_prop_get_integer(strval,
3837042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3847042Sgw25295 				    &compress, NULL)) == 0 &&
3857042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3867042Sgw25295 					error = ENOTSUP;
3877042Sgw25295 				} else {
3887042Sgw25295 					objnum = dmu_objset_id(os);
3897042Sgw25295 				}
39010298SMatthew.Ahrens@Sun.COM 				dmu_objset_rele(os, FTAG);
3915094Slling 			}
3925094Slling 			break;
3937754SJeff.Bonwick@Sun.COM 
3945329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3955329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3965329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3975329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3985329Sgw25295 				error = EINVAL;
3995329Sgw25295 
4005329Sgw25295 			/*
4015329Sgw25295 			 * This is a special case which only occurs when
4025329Sgw25295 			 * the pool has completely failed. This allows
4035329Sgw25295 			 * the user to change the in-core failmode property
4045329Sgw25295 			 * without syncing it out to disk (I/Os might
4055329Sgw25295 			 * currently be blocked). We do this by returning
4065329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
4075329Sgw25295 			 * into thinking we encountered a property validation
4085329Sgw25295 			 * error.
4095329Sgw25295 			 */
4107754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
4115329Sgw25295 				spa->spa_failmode = intval;
4125329Sgw25295 				error = EIO;
4135329Sgw25295 			}
4145329Sgw25295 			break;
4155363Seschrock 
4165363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4175363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
4185363Seschrock 				break;
4195363Seschrock 
4205363Seschrock 			if (strval[0] == '\0')
4215363Seschrock 				break;
4225363Seschrock 
4235363Seschrock 			if (strcmp(strval, "none") == 0)
4245363Seschrock 				break;
4255363Seschrock 
4265363Seschrock 			if (strval[0] != '/') {
4275363Seschrock 				error = EINVAL;
4285363Seschrock 				break;
4295363Seschrock 			}
4305363Seschrock 
4315363Seschrock 			slash = strrchr(strval, '/');
4325363Seschrock 			ASSERT(slash != NULL);
4335363Seschrock 
4345363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4355363Seschrock 			    strcmp(slash, "/..") == 0)
4365363Seschrock 				error = EINVAL;
4375363Seschrock 			break;
4385094Slling 		}
4395094Slling 
4405094Slling 		if (error)
4415094Slling 			break;
4425094Slling 	}
4435094Slling 
4445094Slling 	if (!error && reset_bootfs) {
4455094Slling 		error = nvlist_remove(props,
4465094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4475094Slling 
4485094Slling 		if (!error) {
4495094Slling 			error = nvlist_add_uint64(props,
4505094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4515094Slling 		}
4525094Slling 	}
4535094Slling 
4545094Slling 	return (error);
4555094Slling }
4565094Slling 
4578525SEric.Schrock@Sun.COM void
4588525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
4598525SEric.Schrock@Sun.COM {
4608525SEric.Schrock@Sun.COM 	char *cachefile;
4618525SEric.Schrock@Sun.COM 	spa_config_dirent_t *dp;
4628525SEric.Schrock@Sun.COM 
4638525SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
4648525SEric.Schrock@Sun.COM 	    &cachefile) != 0)
4658525SEric.Schrock@Sun.COM 		return;
4668525SEric.Schrock@Sun.COM 
4678525SEric.Schrock@Sun.COM 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
4688525SEric.Schrock@Sun.COM 	    KM_SLEEP);
4698525SEric.Schrock@Sun.COM 
4708525SEric.Schrock@Sun.COM 	if (cachefile[0] == '\0')
4718525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(spa_config_path);
4728525SEric.Schrock@Sun.COM 	else if (strcmp(cachefile, "none") == 0)
4738525SEric.Schrock@Sun.COM 		dp->scd_path = NULL;
4748525SEric.Schrock@Sun.COM 	else
4758525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(cachefile);
4768525SEric.Schrock@Sun.COM 
4778525SEric.Schrock@Sun.COM 	list_insert_head(&spa->spa_config_list, dp);
4788525SEric.Schrock@Sun.COM 	if (need_sync)
4798525SEric.Schrock@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4808525SEric.Schrock@Sun.COM }
4818525SEric.Schrock@Sun.COM 
4825094Slling int
4835094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4845094Slling {
4855094Slling 	int error;
4868525SEric.Schrock@Sun.COM 	nvpair_t *elem;
4878525SEric.Schrock@Sun.COM 	boolean_t need_sync = B_FALSE;
4888525SEric.Schrock@Sun.COM 	zpool_prop_t prop;
4895094Slling 
4905094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4915094Slling 		return (error);
4925094Slling 
4938525SEric.Schrock@Sun.COM 	elem = NULL;
4948525SEric.Schrock@Sun.COM 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
4958525SEric.Schrock@Sun.COM 		if ((prop = zpool_name_to_prop(
4968525SEric.Schrock@Sun.COM 		    nvpair_name(elem))) == ZPROP_INVAL)
4978525SEric.Schrock@Sun.COM 			return (EINVAL);
4988525SEric.Schrock@Sun.COM 
4998525SEric.Schrock@Sun.COM 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
5008525SEric.Schrock@Sun.COM 			continue;
5018525SEric.Schrock@Sun.COM 
5028525SEric.Schrock@Sun.COM 		need_sync = B_TRUE;
5038525SEric.Schrock@Sun.COM 		break;
5048525SEric.Schrock@Sun.COM 	}
5058525SEric.Schrock@Sun.COM 
5068525SEric.Schrock@Sun.COM 	if (need_sync)
5078525SEric.Schrock@Sun.COM 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
5088525SEric.Schrock@Sun.COM 		    spa, nvp, 3));
5098525SEric.Schrock@Sun.COM 	else
5108525SEric.Schrock@Sun.COM 		return (0);
5115094Slling }
5125094Slling 
5135094Slling /*
5145094Slling  * If the bootfs property value is dsobj, clear it.
5155094Slling  */
5165094Slling void
5175094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
5185094Slling {
5195094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
5205094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
5215094Slling 		    spa->spa_pool_props_object,
5225094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
5235094Slling 		spa->spa_bootfs = 0;
5245094Slling 	}
5255094Slling }
5265094Slling 
527789Sahrens /*
528789Sahrens  * ==========================================================================
529789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
530789Sahrens  * ==========================================================================
531789Sahrens  */
532789Sahrens 
5331544Seschrock static int
5341544Seschrock spa_error_entry_compare(const void *a, const void *b)
5351544Seschrock {
5361544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
5371544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
5381544Seschrock 	int ret;
5391544Seschrock 
5401544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
5411544Seschrock 	    sizeof (zbookmark_t));
5421544Seschrock 
5431544Seschrock 	if (ret < 0)
5441544Seschrock 		return (-1);
5451544Seschrock 	else if (ret > 0)
5461544Seschrock 		return (1);
5471544Seschrock 	else
5481544Seschrock 		return (0);
5491544Seschrock }
5501544Seschrock 
5511544Seschrock /*
5521544Seschrock  * Utility function which retrieves copies of the current logs and
5531544Seschrock  * re-initializes them in the process.
5541544Seschrock  */
5551544Seschrock void
5561544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
5571544Seschrock {
5581544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
5591544Seschrock 
5601544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
5611544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
5621544Seschrock 
5631544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5641544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5651544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5661544Seschrock 	avl_create(&spa->spa_errlist_last,
5671544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5681544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5691544Seschrock }
5701544Seschrock 
571789Sahrens /*
572789Sahrens  * Activate an uninitialized pool.
573789Sahrens  */
574789Sahrens static void
5758241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode)
576789Sahrens {
577789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
578789Sahrens 
579789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
5808241SJeff.Bonwick@Sun.COM 	spa->spa_mode = mode;
581789Sahrens 
58210594SGeorge.Wilson@Sun.COM 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
58310594SGeorge.Wilson@Sun.COM 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
584789Sahrens 
5857754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
5869515SJonathan.Adams@Sun.COM 		const zio_taskq_info_t *ztip = &zio_taskqs[t];
5877754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5889515SJonathan.Adams@Sun.COM 			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
5899515SJonathan.Adams@Sun.COM 			uint_t value = ztip->zti_nthreads[q].zti_value;
5909515SJonathan.Adams@Sun.COM 			char name[32];
5919515SJonathan.Adams@Sun.COM 
5929515SJonathan.Adams@Sun.COM 			(void) snprintf(name, sizeof (name),
5939515SJonathan.Adams@Sun.COM 			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
5949515SJonathan.Adams@Sun.COM 
5959515SJonathan.Adams@Sun.COM 			if (mode == zti_mode_tune) {
5969515SJonathan.Adams@Sun.COM 				mode = zio_taskq_tune_mode;
5979515SJonathan.Adams@Sun.COM 				value = zio_taskq_tune_value;
5989515SJonathan.Adams@Sun.COM 				if (mode == zti_mode_tune)
5999515SJonathan.Adams@Sun.COM 					mode = zti_mode_online_percent;
6009515SJonathan.Adams@Sun.COM 			}
6019515SJonathan.Adams@Sun.COM 
6029515SJonathan.Adams@Sun.COM 			switch (mode) {
6039515SJonathan.Adams@Sun.COM 			case zti_mode_fixed:
6049515SJonathan.Adams@Sun.COM 				ASSERT3U(value, >=, 1);
6059515SJonathan.Adams@Sun.COM 				value = MAX(value, 1);
6069515SJonathan.Adams@Sun.COM 
6079515SJonathan.Adams@Sun.COM 				spa->spa_zio_taskq[t][q] = taskq_create(name,
6089515SJonathan.Adams@Sun.COM 				    value, maxclsyspri, 50, INT_MAX,
6099515SJonathan.Adams@Sun.COM 				    TASKQ_PREPOPULATE);
6109515SJonathan.Adams@Sun.COM 				break;
6119515SJonathan.Adams@Sun.COM 
6129515SJonathan.Adams@Sun.COM 			case zti_mode_online_percent:
6139515SJonathan.Adams@Sun.COM 				spa->spa_zio_taskq[t][q] = taskq_create(name,
6149515SJonathan.Adams@Sun.COM 				    value, maxclsyspri, 50, INT_MAX,
6159515SJonathan.Adams@Sun.COM 				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
6169515SJonathan.Adams@Sun.COM 				break;
6179515SJonathan.Adams@Sun.COM 
6189515SJonathan.Adams@Sun.COM 			case zti_mode_tune:
6199515SJonathan.Adams@Sun.COM 			default:
6209515SJonathan.Adams@Sun.COM 				panic("unrecognized mode for "
6219515SJonathan.Adams@Sun.COM 				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
6229515SJonathan.Adams@Sun.COM 				    "in spa_activate()",
6239515SJonathan.Adams@Sun.COM 				    t, q, mode, value);
6249515SJonathan.Adams@Sun.COM 				break;
6259515SJonathan.Adams@Sun.COM 			}
6267754SJeff.Bonwick@Sun.COM 		}
627789Sahrens 	}
628789Sahrens 
6297754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
6307754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
6317754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
6327754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
633789Sahrens 
634789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
635789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
6361544Seschrock 
6371544Seschrock 	avl_create(&spa->spa_errlist_scrub,
6381544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6391544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
6401544Seschrock 	avl_create(&spa->spa_errlist_last,
6411544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6421544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
643789Sahrens }
644789Sahrens 
645789Sahrens /*
646789Sahrens  * Opposite of spa_activate().
647789Sahrens  */
648789Sahrens static void
649789Sahrens spa_deactivate(spa_t *spa)
650789Sahrens {
651789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
652789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
653789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
6549630SJeff.Bonwick@Sun.COM 	ASSERT(spa->spa_async_zio_root == NULL);
655789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
656789Sahrens 
657789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
658789Sahrens 
6597754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
6607754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
6617754SJeff.Bonwick@Sun.COM 
6627754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
6637754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
6647754SJeff.Bonwick@Sun.COM 			taskq_destroy(spa->spa_zio_taskq[t][q]);
6657754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
6667754SJeff.Bonwick@Sun.COM 		}
667789Sahrens 	}
668789Sahrens 
669789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
670789Sahrens 	spa->spa_normal_class = NULL;
671789Sahrens 
6724527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
6734527Sperrin 	spa->spa_log_class = NULL;
6744527Sperrin 
6751544Seschrock 	/*
6761544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
6771544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
6781544Seschrock 	 */
6791544Seschrock 	spa_errlog_drain(spa);
6801544Seschrock 
6811544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
6821544Seschrock 	avl_destroy(&spa->spa_errlist_last);
6831544Seschrock 
684789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
685789Sahrens }
686789Sahrens 
687789Sahrens /*
688789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
689789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
690789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
691789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
692789Sahrens  */
6932082Seschrock static int
6942082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
6952082Seschrock     uint_t id, int atype)
696789Sahrens {
697789Sahrens 	nvlist_t **child;
6989816SGeorge.Wilson@Sun.COM 	uint_t children;
6992082Seschrock 	int error;
7002082Seschrock 
7012082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
7022082Seschrock 		return (error);
7032082Seschrock 
7042082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
7052082Seschrock 		return (0);
706789Sahrens 
7077754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
7087754SJeff.Bonwick@Sun.COM 	    &child, &children);
7097754SJeff.Bonwick@Sun.COM 
7107754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
7117754SJeff.Bonwick@Sun.COM 		return (0);
7127754SJeff.Bonwick@Sun.COM 
7137754SJeff.Bonwick@Sun.COM 	if (error) {
7142082Seschrock 		vdev_free(*vdp);
7152082Seschrock 		*vdp = NULL;
7162082Seschrock 		return (EINVAL);
717789Sahrens 	}
718789Sahrens 
7199816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < children; c++) {
7202082Seschrock 		vdev_t *vd;
7212082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
7222082Seschrock 		    atype)) != 0) {
7232082Seschrock 			vdev_free(*vdp);
7242082Seschrock 			*vdp = NULL;
7252082Seschrock 			return (error);
726789Sahrens 		}
727789Sahrens 	}
728789Sahrens 
7292082Seschrock 	ASSERT(*vdp != NULL);
7302082Seschrock 
7312082Seschrock 	return (0);
732789Sahrens }
733789Sahrens 
734789Sahrens /*
735789Sahrens  * Opposite of spa_load().
736789Sahrens  */
737789Sahrens static void
738789Sahrens spa_unload(spa_t *spa)
739789Sahrens {
7402082Seschrock 	int i;
7412082Seschrock 
7427754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
7437754SJeff.Bonwick@Sun.COM 
744789Sahrens 	/*
7451544Seschrock 	 * Stop async tasks.
7461544Seschrock 	 */
7471544Seschrock 	spa_async_suspend(spa);
7481544Seschrock 
7491544Seschrock 	/*
750789Sahrens 	 * Stop syncing.
751789Sahrens 	 */
752789Sahrens 	if (spa->spa_sync_on) {
753789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
754789Sahrens 		spa->spa_sync_on = B_FALSE;
755789Sahrens 	}
756789Sahrens 
757789Sahrens 	/*
7587754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
759789Sahrens 	 */
7609234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root != NULL) {
7619234SGeorge.Wilson@Sun.COM 		(void) zio_wait(spa->spa_async_zio_root);
7629234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = NULL;
7639234SGeorge.Wilson@Sun.COM 	}
764789Sahrens 
765789Sahrens 	/*
766789Sahrens 	 * Close the dsl pool.
767789Sahrens 	 */
768789Sahrens 	if (spa->spa_dsl_pool) {
769789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
770789Sahrens 		spa->spa_dsl_pool = NULL;
771789Sahrens 	}
772789Sahrens 
7738241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7748241SJeff.Bonwick@Sun.COM 
7758241SJeff.Bonwick@Sun.COM 	/*
7768241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
7778241SJeff.Bonwick@Sun.COM 	 */
7788241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
7798241SJeff.Bonwick@Sun.COM 
780789Sahrens 	/*
781789Sahrens 	 * Close all vdevs.
782789Sahrens 	 */
7831585Sbonwick 	if (spa->spa_root_vdev)
784789Sahrens 		vdev_free(spa->spa_root_vdev);
7851585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
7861544Seschrock 
7875450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7885450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
7895450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
7905450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7915450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7925450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
7935450Sbrendan 	}
7945450Sbrendan 	if (spa->spa_spares.sav_config) {
7955450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
7965450Sbrendan 		spa->spa_spares.sav_config = NULL;
7972082Seschrock 	}
7987377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
7995450Sbrendan 
8005450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
8015450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
8025450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
8035450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
8045450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
8055450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
8065450Sbrendan 	}
8075450Sbrendan 	if (spa->spa_l2cache.sav_config) {
8085450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
8095450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
8102082Seschrock 	}
8117377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
8122082Seschrock 
8131544Seschrock 	spa->spa_async_suspended = 0;
8148241SJeff.Bonwick@Sun.COM 
8158241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
816789Sahrens }
817789Sahrens 
818789Sahrens /*
8192082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
8202082Seschrock  * this pool.  When this is called, we have some form of basic information in
8215450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
8225450Sbrendan  * then re-generate a more complete list including status information.
8232082Seschrock  */
8242082Seschrock static void
8252082Seschrock spa_load_spares(spa_t *spa)
8262082Seschrock {
8272082Seschrock 	nvlist_t **spares;
8282082Seschrock 	uint_t nspares;
8292082Seschrock 	int i;
8303377Seschrock 	vdev_t *vd, *tvd;
8312082Seschrock 
8327754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
8337754SJeff.Bonwick@Sun.COM 
8342082Seschrock 	/*
8352082Seschrock 	 * First, close and free any existing spare vdevs.
8362082Seschrock 	 */
8375450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8385450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
8393377Seschrock 
8403377Seschrock 		/* Undo the call to spa_activate() below */
8416643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8426643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
8433377Seschrock 			spa_spare_remove(tvd);
8443377Seschrock 		vdev_close(vd);
8453377Seschrock 		vdev_free(vd);
8462082Seschrock 	}
8473377Seschrock 
8485450Sbrendan 	if (spa->spa_spares.sav_vdevs)
8495450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
8505450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
8515450Sbrendan 
8525450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
8532082Seschrock 		nspares = 0;
8542082Seschrock 	else
8555450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
8562082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
8572082Seschrock 
8585450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
8595450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
8602082Seschrock 
8612082Seschrock 	if (nspares == 0)
8622082Seschrock 		return;
8632082Seschrock 
8642082Seschrock 	/*
8652082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
8663377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
8673377Seschrock 	 * structures associated with it: one in the list of spares (used only
8683377Seschrock 	 * for basic validation purposes) and one in the active vdev
8693377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
8703377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
8713377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
8722082Seschrock 	 */
8735450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
8745450Sbrendan 	    KM_SLEEP);
8755450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8762082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
8772082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
8782082Seschrock 		ASSERT(vd != NULL);
8792082Seschrock 
8805450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
8812082Seschrock 
8826643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8836643Seschrock 		    B_FALSE)) != NULL) {
8843377Seschrock 			if (!tvd->vdev_isspare)
8853377Seschrock 				spa_spare_add(tvd);
8863377Seschrock 
8873377Seschrock 			/*
8883377Seschrock 			 * We only mark the spare active if we were successfully
8893377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
8903377Seschrock 			 * with a bad active spare would result in strange
8913377Seschrock 			 * behavior, because multiple pool would think the spare
8923377Seschrock 			 * is actively in use.
8933377Seschrock 			 *
8943377Seschrock 			 * There is a vulnerability here to an equally bizarre
8953377Seschrock 			 * circumstance, where a dead active spare is later
8963377Seschrock 			 * brought back to life (onlined or otherwise).  Given
8973377Seschrock 			 * the rarity of this scenario, and the extra complexity
8983377Seschrock 			 * it adds, we ignore the possibility.
8993377Seschrock 			 */
9003377Seschrock 			if (!vdev_is_dead(tvd))
9013377Seschrock 				spa_spare_activate(tvd);
9023377Seschrock 		}
9033377Seschrock 
9047754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
9059425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
9067754SJeff.Bonwick@Sun.COM 
9072082Seschrock 		if (vdev_open(vd) != 0)
9082082Seschrock 			continue;
9092082Seschrock 
9105450Sbrendan 		if (vdev_validate_aux(vd) == 0)
9115450Sbrendan 			spa_spare_add(vd);
9122082Seschrock 	}
9132082Seschrock 
9142082Seschrock 	/*
9152082Seschrock 	 * Recompute the stashed list of spares, with status information
9162082Seschrock 	 * this time.
9172082Seschrock 	 */
9185450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
9192082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9202082Seschrock 
9215450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
9225450Sbrendan 	    KM_SLEEP);
9235450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9245450Sbrendan 		spares[i] = vdev_config_generate(spa,
9255450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
9265450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
9275450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
9285450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9292082Seschrock 		nvlist_free(spares[i]);
9305450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
9315450Sbrendan }
9325450Sbrendan 
9335450Sbrendan /*
9345450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
9355450Sbrendan  * this pool.  When this is called, we have some form of basic information in
9365450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
9375450Sbrendan  * then re-generate a more complete list including status information.
9385450Sbrendan  * Devices which are already active have their details maintained, and are
9395450Sbrendan  * not re-opened.
9405450Sbrendan  */
9415450Sbrendan static void
9425450Sbrendan spa_load_l2cache(spa_t *spa)
9435450Sbrendan {
9445450Sbrendan 	nvlist_t **l2cache;
9455450Sbrendan 	uint_t nl2cache;
9465450Sbrendan 	int i, j, oldnvdevs;
9479816SGeorge.Wilson@Sun.COM 	uint64_t guid;
9485450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
9495450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
9505450Sbrendan 
9517754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
9527754SJeff.Bonwick@Sun.COM 
9535450Sbrendan 	if (sav->sav_config != NULL) {
9545450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
9555450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
9565450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
9575450Sbrendan 	} else {
9585450Sbrendan 		nl2cache = 0;
9595450Sbrendan 	}
9605450Sbrendan 
9615450Sbrendan 	oldvdevs = sav->sav_vdevs;
9625450Sbrendan 	oldnvdevs = sav->sav_count;
9635450Sbrendan 	sav->sav_vdevs = NULL;
9645450Sbrendan 	sav->sav_count = 0;
9655450Sbrendan 
9665450Sbrendan 	/*
9675450Sbrendan 	 * Process new nvlist of vdevs.
9685450Sbrendan 	 */
9695450Sbrendan 	for (i = 0; i < nl2cache; i++) {
9705450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
9715450Sbrendan 		    &guid) == 0);
9725450Sbrendan 
9735450Sbrendan 		newvdevs[i] = NULL;
9745450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
9755450Sbrendan 			vd = oldvdevs[j];
9765450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
9775450Sbrendan 				/*
9785450Sbrendan 				 * Retain previous vdev for add/remove ops.
9795450Sbrendan 				 */
9805450Sbrendan 				newvdevs[i] = vd;
9815450Sbrendan 				oldvdevs[j] = NULL;
9825450Sbrendan 				break;
9835450Sbrendan 			}
9845450Sbrendan 		}
9855450Sbrendan 
9865450Sbrendan 		if (newvdevs[i] == NULL) {
9875450Sbrendan 			/*
9885450Sbrendan 			 * Create new vdev
9895450Sbrendan 			 */
9905450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
9915450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
9925450Sbrendan 			ASSERT(vd != NULL);
9935450Sbrendan 			newvdevs[i] = vd;
9945450Sbrendan 
9955450Sbrendan 			/*
9965450Sbrendan 			 * Commit this vdev as an l2cache device,
9975450Sbrendan 			 * even if it fails to open.
9985450Sbrendan 			 */
9995450Sbrendan 			spa_l2cache_add(vd);
10005450Sbrendan 
10016643Seschrock 			vd->vdev_top = vd;
10026643Seschrock 			vd->vdev_aux = sav;
10036643Seschrock 
10046643Seschrock 			spa_l2cache_activate(vd);
10056643Seschrock 
10065450Sbrendan 			if (vdev_open(vd) != 0)
10075450Sbrendan 				continue;
10085450Sbrendan 
10095450Sbrendan 			(void) vdev_validate_aux(vd);
10105450Sbrendan 
10119816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
10129816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
10135450Sbrendan 		}
10145450Sbrendan 	}
10155450Sbrendan 
10165450Sbrendan 	/*
10175450Sbrendan 	 * Purge vdevs that were dropped
10185450Sbrendan 	 */
10195450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
10205450Sbrendan 		uint64_t pool;
10215450Sbrendan 
10225450Sbrendan 		vd = oldvdevs[i];
10235450Sbrendan 		if (vd != NULL) {
10248241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
10258241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
10265450Sbrendan 				l2arc_remove_vdev(vd);
10275450Sbrendan 			(void) vdev_close(vd);
10285450Sbrendan 			spa_l2cache_remove(vd);
10295450Sbrendan 		}
10305450Sbrendan 	}
10315450Sbrendan 
10325450Sbrendan 	if (oldvdevs)
10335450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
10345450Sbrendan 
10355450Sbrendan 	if (sav->sav_config == NULL)
10365450Sbrendan 		goto out;
10375450Sbrendan 
10385450Sbrendan 	sav->sav_vdevs = newvdevs;
10395450Sbrendan 	sav->sav_count = (int)nl2cache;
10405450Sbrendan 
10415450Sbrendan 	/*
10425450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
10435450Sbrendan 	 * information this time.
10445450Sbrendan 	 */
10455450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
10465450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
10475450Sbrendan 
10485450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
10495450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
10505450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
10515450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
10525450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
10535450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
10545450Sbrendan out:
10555450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
10565450Sbrendan 		nvlist_free(l2cache[i]);
10575450Sbrendan 	if (sav->sav_count)
10585450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
10592082Seschrock }
10602082Seschrock 
10612082Seschrock static int
10622082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
10632082Seschrock {
10642082Seschrock 	dmu_buf_t *db;
10652082Seschrock 	char *packed = NULL;
10662082Seschrock 	size_t nvsize = 0;
10672082Seschrock 	int error;
10682082Seschrock 	*value = NULL;
10692082Seschrock 
10702082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
10712082Seschrock 	nvsize = *(uint64_t *)db->db_data;
10722082Seschrock 	dmu_buf_rele(db, FTAG);
10732082Seschrock 
10742082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
10759512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
10769512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
10772082Seschrock 	if (error == 0)
10782082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
10792082Seschrock 	kmem_free(packed, nvsize);
10802082Seschrock 
10812082Seschrock 	return (error);
10822082Seschrock }
10832082Seschrock 
10842082Seschrock /*
10854451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
10864451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
10874451Seschrock  */
10884451Seschrock static void
10894451Seschrock spa_check_removed(vdev_t *vd)
10904451Seschrock {
10919816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
10924451Seschrock 		spa_check_removed(vd->vdev_child[c]);
10934451Seschrock 
10944451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
10954451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
10964451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
10974451Seschrock 	}
10984451Seschrock }
10994451Seschrock 
11004451Seschrock /*
11019701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
11029701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
11039701SGeorge.Wilson@Sun.COM  */
11049701SGeorge.Wilson@Sun.COM void
110510594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
11069701SGeorge.Wilson@Sun.COM {
110710594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
110810594SGeorge.Wilson@Sun.COM 
110910594SGeorge.Wilson@Sun.COM 	/*
111010594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
111110594SGeorge.Wilson@Sun.COM 	 */
111210594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
111310594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
111410594SGeorge.Wilson@Sun.COM 
111510594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
111610594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
111710594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
111810594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
11199701SGeorge.Wilson@Sun.COM 	}
112010594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
112110594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
11229701SGeorge.Wilson@Sun.COM }
11239701SGeorge.Wilson@Sun.COM 
11249701SGeorge.Wilson@Sun.COM /*
11257294Sperrin  * Check for missing log devices
11267294Sperrin  */
11277294Sperrin int
11287294Sperrin spa_check_logs(spa_t *spa)
11297294Sperrin {
11307294Sperrin 	switch (spa->spa_log_state) {
11317294Sperrin 	case SPA_LOG_MISSING:
11327294Sperrin 		/* need to recheck in case slog has been restored */
11337294Sperrin 	case SPA_LOG_UNKNOWN:
11347294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
11357294Sperrin 		    DS_FIND_CHILDREN)) {
11367294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
11377294Sperrin 			return (1);
11387294Sperrin 		}
11397294Sperrin 		break;
11407294Sperrin 	}
11417294Sperrin 	return (0);
11427294Sperrin }
11437294Sperrin 
114410672SEric.Schrock@Sun.COM static void
114510672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
114610672SEric.Schrock@Sun.COM {
114710672SEric.Schrock@Sun.COM 	int i;
114810672SEric.Schrock@Sun.COM 
114910672SEric.Schrock@Sun.COM 	for (i = 0; i < sav->sav_count; i++)
115010672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
115110672SEric.Schrock@Sun.COM }
115210672SEric.Schrock@Sun.COM 
11537294Sperrin /*
1154789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
11551544Seschrock  * source of configuration information.
1156789Sahrens  */
1157789Sahrens static int
11581544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1159789Sahrens {
1160789Sahrens 	int error = 0;
116110594SGeorge.Wilson@Sun.COM 	nvlist_t *nvconfig, *nvroot = NULL;
1162789Sahrens 	vdev_t *rvd;
1163789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
11641635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1165789Sahrens 	uint64_t pool_guid;
11662082Seschrock 	uint64_t version;
11674451Seschrock 	uint64_t autoreplace = 0;
11688241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
11697294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
1170789Sahrens 
11718241SJeff.Bonwick@Sun.COM 	/*
11728241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
11738241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
11748241SJeff.Bonwick@Sun.COM 	 */
11758241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
11768241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
11778241SJeff.Bonwick@Sun.COM 
11787754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
11797754SJeff.Bonwick@Sun.COM 
11801544Seschrock 	spa->spa_load_state = state;
11811635Sbonwick 
1182789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
11831733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
11841544Seschrock 		error = EINVAL;
11851544Seschrock 		goto out;
11861544Seschrock 	}
1187789Sahrens 
11882082Seschrock 	/*
11892082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
11902082Seschrock 	 * it's not present treat it as the initial version.
11912082Seschrock 	 */
11922082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
11934577Sahrens 		version = SPA_VERSION_INITIAL;
11942082Seschrock 
11951733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
11961733Sbonwick 	    &spa->spa_config_txg);
11971733Sbonwick 
11981635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
11991544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
12001544Seschrock 		error = EEXIST;
12011544Seschrock 		goto out;
12021544Seschrock 	}
1203789Sahrens 
12042174Seschrock 	spa->spa_load_guid = pool_guid;
12052174Seschrock 
1206789Sahrens 	/*
12079234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
12089234SGeorge.Wilson@Sun.COM 	 */
12099630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
12109630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
12119234SGeorge.Wilson@Sun.COM 
12129234SGeorge.Wilson@Sun.COM 	/*
12132082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
12142082Seschrock 	 * value that will be returned by spa_version() since parsing the
12152082Seschrock 	 * configuration requires knowing the version number.
1216789Sahrens 	 */
12177754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12182082Seschrock 	spa->spa_ubsync.ub_version = version;
12192082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
12207754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1221789Sahrens 
12222082Seschrock 	if (error != 0)
12231544Seschrock 		goto out;
1224789Sahrens 
12251585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1226789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1227789Sahrens 
1228789Sahrens 	/*
1229789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1230789Sahrens 	 */
12317754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12324070Smc142369 	error = vdev_open(rvd);
12337754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
12344070Smc142369 	if (error != 0)
12351544Seschrock 		goto out;
1236789Sahrens 
1237789Sahrens 	/*
12389276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
12399276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
12409276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
12419276SMark.Musante@Sun.COM 	 * that config. Otherwise, we're validating against the cached config
12429276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
12439276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
12449276SMark.Musante@Sun.COM 	 * the vdev config.
12451986Seschrock 	 */
12469276SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12479276SMark.Musante@Sun.COM 	error = vdev_validate(rvd);
12489276SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
12499276SMark.Musante@Sun.COM 	if (error != 0)
12509276SMark.Musante@Sun.COM 		goto out;
12511986Seschrock 
12521986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
12531986Seschrock 		error = ENXIO;
12541986Seschrock 		goto out;
12551986Seschrock 	}
12561986Seschrock 
12571986Seschrock 	/*
1258789Sahrens 	 * Find the best uberblock.
1259789Sahrens 	 */
12607754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1261789Sahrens 
1262789Sahrens 	/*
1263789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1264789Sahrens 	 */
1265789Sahrens 	if (ub->ub_txg == 0) {
12661760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12671760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12681544Seschrock 		error = ENXIO;
12691544Seschrock 		goto out;
12701544Seschrock 	}
12711544Seschrock 
12721544Seschrock 	/*
12731544Seschrock 	 * If the pool is newer than the code, we can't open it.
12741544Seschrock 	 */
12754577Sahrens 	if (ub->ub_version > SPA_VERSION) {
12761760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12771760Seschrock 		    VDEV_AUX_VERSION_NEWER);
12781544Seschrock 		error = ENOTSUP;
12791544Seschrock 		goto out;
1280789Sahrens 	}
1281789Sahrens 
1282789Sahrens 	/*
1283789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1284789Sahrens 	 * incomplete configuration.
1285789Sahrens 	 */
12861732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
12871544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12881544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
12891544Seschrock 		error = ENXIO;
12901544Seschrock 		goto out;
1291789Sahrens 	}
1292789Sahrens 
1293789Sahrens 	/*
1294789Sahrens 	 * Initialize internal SPA structures.
1295789Sahrens 	 */
1296789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1297789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1298789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
12991544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
13001544Seschrock 	if (error) {
13011544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13021544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13031544Seschrock 		goto out;
13041544Seschrock 	}
1305789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1306789Sahrens 
13071544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1308789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
13091544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
13101544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13111544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13121544Seschrock 		error = EIO;
13131544Seschrock 		goto out;
13141544Seschrock 	}
1315789Sahrens 
131610594SGeorge.Wilson@Sun.COM 	if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) {
131710594SGeorge.Wilson@Sun.COM 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
131810594SGeorge.Wilson@Sun.COM 		    VDEV_AUX_CORRUPT_DATA);
131910594SGeorge.Wilson@Sun.COM 		error = EIO;
132010594SGeorge.Wilson@Sun.COM 		goto out;
132110594SGeorge.Wilson@Sun.COM 	}
132210594SGeorge.Wilson@Sun.COM 
1323789Sahrens 	if (!mosconfig) {
13243975Sek110237 		uint64_t hostid;
13252082Seschrock 
132610594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
13277706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
13283975Sek110237 			char *hostname;
13293975Sek110237 			unsigned long myhostid = 0;
13303975Sek110237 
133110594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
13323975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
13333975Sek110237 
13348662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
13358662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
13368662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
13378662SJordan.Vaughan@Sun.com 			/*
13388662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
13398662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
13408662SJordan.Vaughan@Sun.com 			 */
13413975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
13428662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
13434178Slling 			if (hostid != 0 && myhostid != 0 &&
13448662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
13453975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
13463975Sek110237 				    "loaded as it was last accessed by "
13477706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
13483975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
13497754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
13503975Sek110237 				    (unsigned long)hostid);
13513975Sek110237 				error = EBADF;
13523975Sek110237 				goto out;
13533975Sek110237 			}
13543975Sek110237 		}
13553975Sek110237 
135610594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1357789Sahrens 		spa_unload(spa);
1358789Sahrens 		spa_deactivate(spa);
13598241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1360789Sahrens 
136110594SGeorge.Wilson@Sun.COM 		return (spa_load(spa, nvconfig, state, B_TRUE));
13621544Seschrock 	}
13631544Seschrock 
13641544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
13651544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
13661544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
13671544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13681544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13691544Seschrock 		error = EIO;
13701544Seschrock 		goto out;
1371789Sahrens 	}
1372789Sahrens 
13731544Seschrock 	/*
13742082Seschrock 	 * Load the bit that tells us to use the new accounting function
13752082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
13762082Seschrock 	 * be present.
13772082Seschrock 	 */
13782082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
13792082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
13802082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
13812082Seschrock 	if (error != 0 && error != ENOENT) {
13822082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13832082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13842082Seschrock 		error = EIO;
13852082Seschrock 		goto out;
13862082Seschrock 	}
13872082Seschrock 
13882082Seschrock 	/*
13891544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
13901544Seschrock 	 * not be present.
13911544Seschrock 	 */
13921544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
13931544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
13941544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
13951807Sbonwick 	if (error != 0 && error != ENOENT) {
13961544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13971544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13981544Seschrock 		error = EIO;
13991544Seschrock 		goto out;
14001544Seschrock 	}
14011544Seschrock 
14021544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
14031544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
14041544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
14051544Seschrock 	if (error != 0 && error != ENOENT) {
14061544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14071544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14081544Seschrock 		error = EIO;
14091544Seschrock 		goto out;
14101544Seschrock 	}
1411789Sahrens 
1412789Sahrens 	/*
14132926Sek110237 	 * Load the history object.  If we have an older pool, this
14142926Sek110237 	 * will not be present.
14152926Sek110237 	 */
14162926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
14172926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
14182926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
14192926Sek110237 	if (error != 0 && error != ENOENT) {
14202926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14212926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
14222926Sek110237 		error = EIO;
14232926Sek110237 		goto out;
14242926Sek110237 	}
14252926Sek110237 
14262926Sek110237 	/*
14272082Seschrock 	 * Load any hot spares for this pool.
14282082Seschrock 	 */
14292082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
14305450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
14312082Seschrock 	if (error != 0 && error != ENOENT) {
14322082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14332082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
14342082Seschrock 		error = EIO;
14352082Seschrock 		goto out;
14362082Seschrock 	}
14372082Seschrock 	if (error == 0) {
14384577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
14395450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
14405450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
14412082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14422082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
14432082Seschrock 			error = EIO;
14442082Seschrock 			goto out;
14452082Seschrock 		}
14462082Seschrock 
14477754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
14482082Seschrock 		spa_load_spares(spa);
14497754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
14502082Seschrock 	}
14512082Seschrock 
14525450Sbrendan 	/*
14535450Sbrendan 	 * Load any level 2 ARC devices for this pool.
14545450Sbrendan 	 */
14555450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
14565450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
14575450Sbrendan 	    &spa->spa_l2cache.sav_object);
14585450Sbrendan 	if (error != 0 && error != ENOENT) {
14595450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14605450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
14615450Sbrendan 		error = EIO;
14625450Sbrendan 		goto out;
14635450Sbrendan 	}
14645450Sbrendan 	if (error == 0) {
14655450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
14665450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
14675450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
14685450Sbrendan 			vdev_set_state(rvd, B_TRUE,
14695450Sbrendan 			    VDEV_STATE_CANT_OPEN,
14705450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
14715450Sbrendan 			error = EIO;
14725450Sbrendan 			goto out;
14735450Sbrendan 		}
14745450Sbrendan 
14757754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
14765450Sbrendan 		spa_load_l2cache(spa);
14777754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
14785450Sbrendan 	}
14795450Sbrendan 
148010594SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
148110594SGeorge.Wilson@Sun.COM 	    &nvroot) == 0);
148210594SGeorge.Wilson@Sun.COM 	spa_load_log_state(spa, nvroot);
148310594SGeorge.Wilson@Sun.COM 	nvlist_free(nvconfig);
14849701SGeorge.Wilson@Sun.COM 
14857294Sperrin 	if (spa_check_logs(spa)) {
14867294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
14877294Sperrin 		    VDEV_AUX_BAD_LOG);
14887294Sperrin 		error = ENXIO;
14897294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
14907294Sperrin 		goto out;
14917294Sperrin 	}
14927294Sperrin 
14937294Sperrin 
14945094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
14954543Smarks 
14963912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
14973912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
14983912Slling 
14993912Slling 	if (error && error != ENOENT) {
15003912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
15013912Slling 		    VDEV_AUX_CORRUPT_DATA);
15023912Slling 		error = EIO;
15033912Slling 		goto out;
15043912Slling 	}
15053912Slling 
15063912Slling 	if (error == 0) {
15073912Slling 		(void) zap_lookup(spa->spa_meta_objset,
15083912Slling 		    spa->spa_pool_props_object,
15094451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
15103912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
15114451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
15124451Seschrock 		    spa->spa_pool_props_object,
15134451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
15144451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
151510672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
15164543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
15174543Smarks 		    spa->spa_pool_props_object,
15184543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
15194543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
15205329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
15215329Sgw25295 		    spa->spa_pool_props_object,
15225329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
15235329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
15249816SGeorge.Wilson@Sun.COM 		(void) zap_lookup(spa->spa_meta_objset,
15259816SGeorge.Wilson@Sun.COM 		    spa->spa_pool_props_object,
15269816SGeorge.Wilson@Sun.COM 		    zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND),
15279816SGeorge.Wilson@Sun.COM 		    sizeof (uint64_t), 1, &spa->spa_autoexpand);
15283912Slling 	}
15293912Slling 
15302082Seschrock 	/*
15314451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
15324451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
15334451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
15344451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
15354451Seschrock 	 * over.
15364451Seschrock 	 */
153710672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
15384451Seschrock 		spa_check_removed(spa->spa_root_vdev);
153910672SEric.Schrock@Sun.COM 		/*
154010672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
154110672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
154210672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
154310672SEric.Schrock@Sun.COM 		 */
154410672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
154510672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
154610672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
154710672SEric.Schrock@Sun.COM 		}
154810672SEric.Schrock@Sun.COM 	}
15494451Seschrock 
15504451Seschrock 	/*
15511986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1552789Sahrens 	 */
15531986Seschrock 	vdev_load(rvd);
1554789Sahrens 
1555789Sahrens 	/*
1556789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1557789Sahrens 	 */
15587754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1559789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
15607754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1561789Sahrens 
1562789Sahrens 	/*
1563789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1564789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1565789Sahrens 	 */
15661544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
15671544Seschrock 		error = ENXIO;
15681544Seschrock 		goto out;
15691544Seschrock 	}
1570789Sahrens 
15718241SJeff.Bonwick@Sun.COM 	if (spa_writeable(spa)) {
15721635Sbonwick 		dmu_tx_t *tx;
15731635Sbonwick 		int need_update = B_FALSE;
15748241SJeff.Bonwick@Sun.COM 
15758241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
15761601Sbonwick 
15771635Sbonwick 		/*
15781635Sbonwick 		 * Claim log blocks that haven't been committed yet.
15791635Sbonwick 		 * This must all happen in a single txg.
15801635Sbonwick 		 */
15811601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1582789Sahrens 		    spa_first_txg(spa));
15837754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
15842417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1585789Sahrens 		dmu_tx_commit(tx);
1586789Sahrens 
15879701SGeorge.Wilson@Sun.COM 		spa->spa_log_state = SPA_LOG_GOOD;
1588789Sahrens 		spa->spa_sync_on = B_TRUE;
1589789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1590789Sahrens 
1591789Sahrens 		/*
1592789Sahrens 		 * Wait for all claims to sync.
1593789Sahrens 		 */
1594789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
15951585Sbonwick 
15961585Sbonwick 		/*
15971635Sbonwick 		 * If the config cache is stale, or we have uninitialized
15981635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
159910100SLin.Ling@Sun.COM 		 *
160010100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
160110100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
16021585Sbonwick 		 */
16031635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
160410100SLin.Ling@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim)
16051635Sbonwick 			need_update = B_TRUE;
16061635Sbonwick 
16078241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
16081635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
16091635Sbonwick 				need_update = B_TRUE;
16101585Sbonwick 
16111585Sbonwick 		/*
16121635Sbonwick 		 * Update the config cache asychronously in case we're the
16131635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
16141585Sbonwick 		 */
16151635Sbonwick 		if (need_update)
16161635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
16178241SJeff.Bonwick@Sun.COM 
16188241SJeff.Bonwick@Sun.COM 		/*
16198241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
16208241SJeff.Bonwick@Sun.COM 		 */
16218241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
16228241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
162310298SMatthew.Ahrens@Sun.COM 
162410298SMatthew.Ahrens@Sun.COM 		/*
162510298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
162610298SMatthew.Ahrens@Sun.COM 		 */
162710298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
162810298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
162910342Schris.kirby@sun.com 
163010342Schris.kirby@sun.com 		/*
163110342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
163210342Schris.kirby@sun.com 		 */
163310342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
1634789Sahrens 	}
1635789Sahrens 
16361544Seschrock 	error = 0;
16371544Seschrock out:
16387046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
16392082Seschrock 	if (error && error != EBADF)
16407294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
16411544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
16421544Seschrock 	spa->spa_ena = 0;
16431544Seschrock 
16441544Seschrock 	return (error);
1645789Sahrens }
1646789Sahrens 
1647789Sahrens /*
1648789Sahrens  * Pool Open/Import
1649789Sahrens  *
1650789Sahrens  * The import case is identical to an open except that the configuration is sent
1651789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1652789Sahrens  * case of an open, the pool configuration will exist in the
16534451Seschrock  * POOL_STATE_UNINITIALIZED state.
1654789Sahrens  *
1655789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1656789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1657789Sahrens  * ambiguous state.
1658789Sahrens  */
1659789Sahrens static int
1660789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1661789Sahrens {
1662789Sahrens 	spa_t *spa;
1663789Sahrens 	int error;
1664789Sahrens 	int locked = B_FALSE;
1665789Sahrens 
1666789Sahrens 	*spapp = NULL;
1667789Sahrens 
1668789Sahrens 	/*
1669789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1670789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1671789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1672789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1673789Sahrens 	 */
1674789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1675789Sahrens 		mutex_enter(&spa_namespace_lock);
1676789Sahrens 		locked = B_TRUE;
1677789Sahrens 	}
1678789Sahrens 
1679789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1680789Sahrens 		if (locked)
1681789Sahrens 			mutex_exit(&spa_namespace_lock);
1682789Sahrens 		return (ENOENT);
1683789Sahrens 	}
1684789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1685789Sahrens 
16868241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
1687789Sahrens 
16881635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1689789Sahrens 
1690789Sahrens 		if (error == EBADF) {
1691789Sahrens 			/*
16921986Seschrock 			 * If vdev_validate() returns failure (indicated by
16931986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
16941986Seschrock 			 * that the pool has been exported or destroyed.  If
16951986Seschrock 			 * this is the case, the config cache is out of sync and
16961986Seschrock 			 * we should remove the pool from the namespace.
1697789Sahrens 			 */
1698789Sahrens 			spa_unload(spa);
1699789Sahrens 			spa_deactivate(spa);
17006643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1701789Sahrens 			spa_remove(spa);
1702789Sahrens 			if (locked)
1703789Sahrens 				mutex_exit(&spa_namespace_lock);
1704789Sahrens 			return (ENOENT);
17051544Seschrock 		}
17061544Seschrock 
17071544Seschrock 		if (error) {
1708789Sahrens 			/*
1709789Sahrens 			 * We can't open the pool, but we still have useful
1710789Sahrens 			 * information: the state of each vdev after the
1711789Sahrens 			 * attempted vdev_open().  Return this to the user.
1712789Sahrens 			 */
17137754SJeff.Bonwick@Sun.COM 			if (config != NULL && spa->spa_root_vdev != NULL)
1714789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1715789Sahrens 				    B_TRUE);
1716789Sahrens 			spa_unload(spa);
1717789Sahrens 			spa_deactivate(spa);
17181544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1719789Sahrens 			if (locked)
1720789Sahrens 				mutex_exit(&spa_namespace_lock);
1721789Sahrens 			*spapp = NULL;
1722789Sahrens 			return (error);
17231544Seschrock 		} else {
17241544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1725789Sahrens 		}
1726789Sahrens 	}
1727789Sahrens 
1728789Sahrens 	spa_open_ref(spa, tag);
17294451Seschrock 
1730789Sahrens 	if (locked)
1731789Sahrens 		mutex_exit(&spa_namespace_lock);
1732789Sahrens 
1733789Sahrens 	*spapp = spa;
1734789Sahrens 
17357754SJeff.Bonwick@Sun.COM 	if (config != NULL)
1736789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1737789Sahrens 
1738789Sahrens 	return (0);
1739789Sahrens }
1740789Sahrens 
1741789Sahrens int
1742789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1743789Sahrens {
1744789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1745789Sahrens }
1746789Sahrens 
17471544Seschrock /*
17481544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
17491544Seschrock  * preventing it from being exported or destroyed.
17501544Seschrock  */
17511544Seschrock spa_t *
17521544Seschrock spa_inject_addref(char *name)
17531544Seschrock {
17541544Seschrock 	spa_t *spa;
17551544Seschrock 
17561544Seschrock 	mutex_enter(&spa_namespace_lock);
17571544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
17581544Seschrock 		mutex_exit(&spa_namespace_lock);
17591544Seschrock 		return (NULL);
17601544Seschrock 	}
17611544Seschrock 	spa->spa_inject_ref++;
17621544Seschrock 	mutex_exit(&spa_namespace_lock);
17631544Seschrock 
17641544Seschrock 	return (spa);
17651544Seschrock }
17661544Seschrock 
17671544Seschrock void
17681544Seschrock spa_inject_delref(spa_t *spa)
17691544Seschrock {
17701544Seschrock 	mutex_enter(&spa_namespace_lock);
17711544Seschrock 	spa->spa_inject_ref--;
17721544Seschrock 	mutex_exit(&spa_namespace_lock);
17731544Seschrock }
17741544Seschrock 
17755450Sbrendan /*
17765450Sbrendan  * Add spares device information to the nvlist.
17775450Sbrendan  */
17782082Seschrock static void
17792082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
17802082Seschrock {
17812082Seschrock 	nvlist_t **spares;
17822082Seschrock 	uint_t i, nspares;
17832082Seschrock 	nvlist_t *nvroot;
17842082Seschrock 	uint64_t guid;
17852082Seschrock 	vdev_stat_t *vs;
17862082Seschrock 	uint_t vsc;
17873377Seschrock 	uint64_t pool;
17882082Seschrock 
17899425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
17909425SEric.Schrock@Sun.COM 
17915450Sbrendan 	if (spa->spa_spares.sav_count == 0)
17922082Seschrock 		return;
17932082Seschrock 
17942082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
17952082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
17965450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
17972082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
17982082Seschrock 	if (nspares != 0) {
17992082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
18002082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
18012082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
18022082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
18032082Seschrock 
18042082Seschrock 		/*
18052082Seschrock 		 * Go through and find any spares which have since been
18062082Seschrock 		 * repurposed as an active spare.  If this is the case, update
18072082Seschrock 		 * their status appropriately.
18082082Seschrock 		 */
18092082Seschrock 		for (i = 0; i < nspares; i++) {
18102082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
18112082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
18127214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
18137214Slling 			    pool != 0ULL) {
18142082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
18152082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
18162082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
18172082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
18182082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
18192082Seschrock 			}
18202082Seschrock 		}
18212082Seschrock 	}
18222082Seschrock }
18232082Seschrock 
18245450Sbrendan /*
18255450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
18265450Sbrendan  */
18275450Sbrendan static void
18285450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
18295450Sbrendan {
18305450Sbrendan 	nvlist_t **l2cache;
18315450Sbrendan 	uint_t i, j, nl2cache;
18325450Sbrendan 	nvlist_t *nvroot;
18335450Sbrendan 	uint64_t guid;
18345450Sbrendan 	vdev_t *vd;
18355450Sbrendan 	vdev_stat_t *vs;
18365450Sbrendan 	uint_t vsc;
18375450Sbrendan 
18389425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
18399425SEric.Schrock@Sun.COM 
18405450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
18415450Sbrendan 		return;
18425450Sbrendan 
18435450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
18445450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
18455450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
18465450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
18475450Sbrendan 	if (nl2cache != 0) {
18485450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
18495450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
18505450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
18515450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
18525450Sbrendan 
18535450Sbrendan 		/*
18545450Sbrendan 		 * Update level 2 cache device stats.
18555450Sbrendan 		 */
18565450Sbrendan 
18575450Sbrendan 		for (i = 0; i < nl2cache; i++) {
18585450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
18595450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
18605450Sbrendan 
18615450Sbrendan 			vd = NULL;
18625450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
18635450Sbrendan 				if (guid ==
18645450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
18655450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
18665450Sbrendan 					break;
18675450Sbrendan 				}
18685450Sbrendan 			}
18695450Sbrendan 			ASSERT(vd != NULL);
18705450Sbrendan 
18715450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
18725450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
18735450Sbrendan 			vdev_get_stats(vd, vs);
18745450Sbrendan 		}
18755450Sbrendan 	}
18765450Sbrendan }
18775450Sbrendan 
1878789Sahrens int
18791544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1880789Sahrens {
1881789Sahrens 	int error;
1882789Sahrens 	spa_t *spa;
1883789Sahrens 
1884789Sahrens 	*config = NULL;
1885789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1886789Sahrens 
18879425SEric.Schrock@Sun.COM 	if (spa != NULL) {
18889425SEric.Schrock@Sun.COM 		/*
18899425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
18909425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
18919425SEric.Schrock@Sun.COM 		 * self-inconsistent.
18929425SEric.Schrock@Sun.COM 		 */
18939425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
18949425SEric.Schrock@Sun.COM 
18959425SEric.Schrock@Sun.COM 		if (*config != NULL) {
18967754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
18979425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
18989425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
18999425SEric.Schrock@Sun.COM 
19009425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
19019425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
19029425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
19039425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
19049425SEric.Schrock@Sun.COM 
19059425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
19069425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
19079425SEric.Schrock@Sun.COM 		}
19082082Seschrock 	}
19092082Seschrock 
19101544Seschrock 	/*
19111544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
19121544Seschrock 	 * and call spa_lookup() directly.
19131544Seschrock 	 */
19141544Seschrock 	if (altroot) {
19151544Seschrock 		if (spa == NULL) {
19161544Seschrock 			mutex_enter(&spa_namespace_lock);
19171544Seschrock 			spa = spa_lookup(name);
19181544Seschrock 			if (spa)
19191544Seschrock 				spa_altroot(spa, altroot, buflen);
19201544Seschrock 			else
19211544Seschrock 				altroot[0] = '\0';
19221544Seschrock 			spa = NULL;
19231544Seschrock 			mutex_exit(&spa_namespace_lock);
19241544Seschrock 		} else {
19251544Seschrock 			spa_altroot(spa, altroot, buflen);
19261544Seschrock 		}
19271544Seschrock 	}
19281544Seschrock 
19299425SEric.Schrock@Sun.COM 	if (spa != NULL) {
19309425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
1931789Sahrens 		spa_close(spa, FTAG);
19329425SEric.Schrock@Sun.COM 	}
1933789Sahrens 
1934789Sahrens 	return (error);
1935789Sahrens }
1936789Sahrens 
1937789Sahrens /*
19385450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
19395450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
19405450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
19415450Sbrendan  * specified, as long as they are well-formed.
19422082Seschrock  */
19432082Seschrock static int
19445450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
19455450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
19465450Sbrendan     vdev_labeltype_t label)
19472082Seschrock {
19485450Sbrendan 	nvlist_t **dev;
19495450Sbrendan 	uint_t i, ndev;
19502082Seschrock 	vdev_t *vd;
19512082Seschrock 	int error;
19522082Seschrock 
19537754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
19547754SJeff.Bonwick@Sun.COM 
19552082Seschrock 	/*
19565450Sbrendan 	 * It's acceptable to have no devs specified.
19572082Seschrock 	 */
19585450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
19592082Seschrock 		return (0);
19602082Seschrock 
19615450Sbrendan 	if (ndev == 0)
19622082Seschrock 		return (EINVAL);
19632082Seschrock 
19642082Seschrock 	/*
19655450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
19665450Sbrendan 	 * device type.
19672082Seschrock 	 */
19685450Sbrendan 	if (spa_version(spa) < version)
19692082Seschrock 		return (ENOTSUP);
19702082Seschrock 
19713377Seschrock 	/*
19725450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
19733377Seschrock 	 * checking.
19743377Seschrock 	 */
19755450Sbrendan 	sav->sav_pending = dev;
19765450Sbrendan 	sav->sav_npending = ndev;
19775450Sbrendan 
19785450Sbrendan 	for (i = 0; i < ndev; i++) {
19795450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
19802082Seschrock 		    mode)) != 0)
19813377Seschrock 			goto out;
19822082Seschrock 
19832082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
19842082Seschrock 			vdev_free(vd);
19853377Seschrock 			error = EINVAL;
19863377Seschrock 			goto out;
19872082Seschrock 		}
19882082Seschrock 
19895450Sbrendan 		/*
19907754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
19917754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
19925450Sbrendan 		 */
19937754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
19945450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
19955450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
19965450Sbrendan 			error = ENOTBLK;
19975450Sbrendan 			goto out;
19985450Sbrendan 		}
19997754SJeff.Bonwick@Sun.COM #endif
20002082Seschrock 		vd->vdev_top = vd;
20013377Seschrock 
20023377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
20035450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
20045450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
20053377Seschrock 			    vd->vdev_guid) == 0);
20062082Seschrock 		}
20072082Seschrock 
20082082Seschrock 		vdev_free(vd);
20093377Seschrock 
20105450Sbrendan 		if (error &&
20115450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
20123377Seschrock 			goto out;
20133377Seschrock 		else
20143377Seschrock 			error = 0;
20152082Seschrock 	}
20162082Seschrock 
20173377Seschrock out:
20185450Sbrendan 	sav->sav_pending = NULL;
20195450Sbrendan 	sav->sav_npending = 0;
20203377Seschrock 	return (error);
20212082Seschrock }
20222082Seschrock 
20235450Sbrendan static int
20245450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
20255450Sbrendan {
20265450Sbrendan 	int error;
20275450Sbrendan 
20287754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
20297754SJeff.Bonwick@Sun.COM 
20305450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
20315450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
20325450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
20335450Sbrendan 		return (error);
20345450Sbrendan 	}
20355450Sbrendan 
20365450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
20375450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
20385450Sbrendan 	    VDEV_LABEL_L2CACHE));
20395450Sbrendan }
20405450Sbrendan 
20415450Sbrendan static void
20425450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
20435450Sbrendan     const char *config)
20445450Sbrendan {
20455450Sbrendan 	int i;
20465450Sbrendan 
20475450Sbrendan 	if (sav->sav_config != NULL) {
20485450Sbrendan 		nvlist_t **olddevs;
20495450Sbrendan 		uint_t oldndevs;
20505450Sbrendan 		nvlist_t **newdevs;
20515450Sbrendan 
20525450Sbrendan 		/*
20535450Sbrendan 		 * Generate new dev list by concatentating with the
20545450Sbrendan 		 * current dev list.
20555450Sbrendan 		 */
20565450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
20575450Sbrendan 		    &olddevs, &oldndevs) == 0);
20585450Sbrendan 
20595450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
20605450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
20615450Sbrendan 		for (i = 0; i < oldndevs; i++)
20625450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
20635450Sbrendan 			    KM_SLEEP) == 0);
20645450Sbrendan 		for (i = 0; i < ndevs; i++)
20655450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
20665450Sbrendan 			    KM_SLEEP) == 0);
20675450Sbrendan 
20685450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
20695450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
20705450Sbrendan 
20715450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
20725450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
20735450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
20745450Sbrendan 			nvlist_free(newdevs[i]);
20755450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
20765450Sbrendan 	} else {
20775450Sbrendan 		/*
20785450Sbrendan 		 * Generate a new dev list.
20795450Sbrendan 		 */
20805450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
20815450Sbrendan 		    KM_SLEEP) == 0);
20825450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
20835450Sbrendan 		    devs, ndevs) == 0);
20845450Sbrendan 	}
20855450Sbrendan }
20865450Sbrendan 
20875450Sbrendan /*
20885450Sbrendan  * Stop and drop level 2 ARC devices
20895450Sbrendan  */
20905450Sbrendan void
20915450Sbrendan spa_l2cache_drop(spa_t *spa)
20925450Sbrendan {
20935450Sbrendan 	vdev_t *vd;
20945450Sbrendan 	int i;
20955450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
20965450Sbrendan 
20975450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
20985450Sbrendan 		uint64_t pool;
20995450Sbrendan 
21005450Sbrendan 		vd = sav->sav_vdevs[i];
21015450Sbrendan 		ASSERT(vd != NULL);
21025450Sbrendan 
21038241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
21048241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
21055450Sbrendan 			l2arc_remove_vdev(vd);
21065450Sbrendan 		if (vd->vdev_isl2cache)
21075450Sbrendan 			spa_l2cache_remove(vd);
21085450Sbrendan 		vdev_clear_stats(vd);
21095450Sbrendan 		(void) vdev_close(vd);
21105450Sbrendan 	}
21115450Sbrendan }
21125450Sbrendan 
21132082Seschrock /*
2114789Sahrens  * Pool Creation
2115789Sahrens  */
2116789Sahrens int
21175094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
21187184Stimh     const char *history_str, nvlist_t *zplprops)
2119789Sahrens {
2120789Sahrens 	spa_t *spa;
21215094Slling 	char *altroot = NULL;
21221635Sbonwick 	vdev_t *rvd;
2123789Sahrens 	dsl_pool_t *dp;
2124789Sahrens 	dmu_tx_t *tx;
21259816SGeorge.Wilson@Sun.COM 	int error = 0;
2126789Sahrens 	uint64_t txg = TXG_INITIAL;
21275450Sbrendan 	nvlist_t **spares, **l2cache;
21285450Sbrendan 	uint_t nspares, nl2cache;
21295094Slling 	uint64_t version;
2130789Sahrens 
2131789Sahrens 	/*
2132789Sahrens 	 * If this pool already exists, return failure.
2133789Sahrens 	 */
2134789Sahrens 	mutex_enter(&spa_namespace_lock);
2135789Sahrens 	if (spa_lookup(pool) != NULL) {
2136789Sahrens 		mutex_exit(&spa_namespace_lock);
2137789Sahrens 		return (EEXIST);
2138789Sahrens 	}
2139789Sahrens 
2140789Sahrens 	/*
2141789Sahrens 	 * Allocate a new spa_t structure.
2142789Sahrens 	 */
21435094Slling 	(void) nvlist_lookup_string(props,
21445094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
21451635Sbonwick 	spa = spa_add(pool, altroot);
21468241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2147789Sahrens 
2148789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
21495094Slling 
21505094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
21515094Slling 		spa_deactivate(spa);
21525094Slling 		spa_remove(spa);
21536643Seschrock 		mutex_exit(&spa_namespace_lock);
21545094Slling 		return (error);
21555094Slling 	}
21565094Slling 
21575094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
21585094Slling 	    &version) != 0)
21595094Slling 		version = SPA_VERSION;
21605094Slling 	ASSERT(version <= SPA_VERSION);
21615094Slling 	spa->spa_uberblock.ub_version = version;
2162789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2163789Sahrens 
21641635Sbonwick 	/*
21659234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
21669234SGeorge.Wilson@Sun.COM 	 */
21679630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
21689630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
21699234SGeorge.Wilson@Sun.COM 
21709234SGeorge.Wilson@Sun.COM 	/*
21711635Sbonwick 	 * Create the root vdev.
21721635Sbonwick 	 */
21737754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
21741635Sbonwick 
21752082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
21762082Seschrock 
21772082Seschrock 	ASSERT(error != 0 || rvd != NULL);
21782082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
21792082Seschrock 
21805913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
21811635Sbonwick 		error = EINVAL;
21822082Seschrock 
21832082Seschrock 	if (error == 0 &&
21842082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
21855450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
21862082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
21879816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
21889816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
21899816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
21909816SGeorge.Wilson@Sun.COM 		}
21911635Sbonwick 	}
21921635Sbonwick 
21937754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2194789Sahrens 
21952082Seschrock 	if (error != 0) {
2196789Sahrens 		spa_unload(spa);
2197789Sahrens 		spa_deactivate(spa);
2198789Sahrens 		spa_remove(spa);
2199789Sahrens 		mutex_exit(&spa_namespace_lock);
2200789Sahrens 		return (error);
2201789Sahrens 	}
2202789Sahrens 
22032082Seschrock 	/*
22042082Seschrock 	 * Get the list of spares, if specified.
22052082Seschrock 	 */
22062082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
22072082Seschrock 	    &spares, &nspares) == 0) {
22085450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
22092082Seschrock 		    KM_SLEEP) == 0);
22105450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
22112082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
22127754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22132082Seschrock 		spa_load_spares(spa);
22147754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22155450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
22165450Sbrendan 	}
22175450Sbrendan 
22185450Sbrendan 	/*
22195450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
22205450Sbrendan 	 */
22215450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
22225450Sbrendan 	    &l2cache, &nl2cache) == 0) {
22235450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
22245450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
22255450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
22265450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
22277754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22285450Sbrendan 		spa_load_l2cache(spa);
22297754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22305450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
22312082Seschrock 	}
22322082Seschrock 
22337184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2234789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2235789Sahrens 
2236789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2237789Sahrens 
2238789Sahrens 	/*
2239789Sahrens 	 * Create the pool config object.
2240789Sahrens 	 */
2241789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
22427497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2243789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2244789Sahrens 
22451544Seschrock 	if (zap_add(spa->spa_meta_objset,
2246789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
22471544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
22481544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
22491544Seschrock 	}
2250789Sahrens 
22515094Slling 	/* Newly created pools with the right version are always deflated. */
22525094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
22535094Slling 		spa->spa_deflate = TRUE;
22545094Slling 		if (zap_add(spa->spa_meta_objset,
22555094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
22565094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
22575094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
22585094Slling 		}
22592082Seschrock 	}
22602082Seschrock 
2261789Sahrens 	/*
2262789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2263789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2264789Sahrens 	 * keeps changing.
2265789Sahrens 	 */
2266789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2267789Sahrens 	    1 << 14, tx);
2268789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2269789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2270789Sahrens 
22711544Seschrock 	if (zap_add(spa->spa_meta_objset,
2272789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
22731544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
22741544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
22751544Seschrock 	}
2276789Sahrens 
22772926Sek110237 	/*
22782926Sek110237 	 * Create the pool's history object.
22792926Sek110237 	 */
22805094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
22815094Slling 		spa_history_create_obj(spa, tx);
22825094Slling 
22835094Slling 	/*
22845094Slling 	 * Set pool properties.
22855094Slling 	 */
22865094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
22875094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
22885329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
22899816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
22908525SEric.Schrock@Sun.COM 	if (props != NULL) {
22918525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
22925094Slling 		spa_sync_props(spa, props, CRED(), tx);
22938525SEric.Schrock@Sun.COM 	}
22942926Sek110237 
2295789Sahrens 	dmu_tx_commit(tx);
2296789Sahrens 
2297789Sahrens 	spa->spa_sync_on = B_TRUE;
2298789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2299789Sahrens 
2300789Sahrens 	/*
2301789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2302789Sahrens 	 * bean counters are appropriately updated.
2303789Sahrens 	 */
2304789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2305789Sahrens 
23066643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2307789Sahrens 
23085094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
23094715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
23109946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
23114715Sek110237 
23128667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
23138667SGeorge.Wilson@Sun.COM 
2314789Sahrens 	mutex_exit(&spa_namespace_lock);
2315789Sahrens 
2316789Sahrens 	return (0);
2317789Sahrens }
2318789Sahrens 
23196423Sgw25295 #ifdef _KERNEL
23206423Sgw25295 /*
23219790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
23229790SLin.Ling@Sun.COM  * during the system boot up time.
23236423Sgw25295  */
23249790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
23259790SLin.Ling@Sun.COM 
23269790SLin.Ling@Sun.COM static nvlist_t *
23279790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
23286423Sgw25295 {
23299790SLin.Ling@Sun.COM 	nvlist_t *config;
23306423Sgw25295 	nvlist_t *nvtop, *nvroot;
23316423Sgw25295 	uint64_t pgid;
23326423Sgw25295 
23339790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
23349790SLin.Ling@Sun.COM 		return (NULL);
23359790SLin.Ling@Sun.COM 
23366423Sgw25295 	/*
23376423Sgw25295 	 * Add this top-level vdev to the child array.
23386423Sgw25295 	 */
23399790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
23409790SLin.Ling@Sun.COM 	    &nvtop) == 0);
23419790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
23429790SLin.Ling@Sun.COM 	    &pgid) == 0);
23439790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
23446423Sgw25295 
23456423Sgw25295 	/*
23466423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
23476423Sgw25295 	 */
23486423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
23499790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
23509790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
23516423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
23526423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
23536423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
23546423Sgw25295 	    &nvtop, 1) == 0);
23556423Sgw25295 
23566423Sgw25295 	/*
23576423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
23586423Sgw25295 	 * this pool's configuration (remove the old, add the new).
23596423Sgw25295 	 */
23606423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
23616423Sgw25295 	nvlist_free(nvroot);
23629790SLin.Ling@Sun.COM 	return (config);
23636423Sgw25295 }
23646423Sgw25295 
23656423Sgw25295 /*
23669790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
23679790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
23689790SLin.Ling@Sun.COM  * device has a more recent txg.
23696423Sgw25295  */
23709790SLin.Ling@Sun.COM static void
23719790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
23727147Staylor {
23739816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
23749790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
23759790SLin.Ling@Sun.COM 
23769790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
23779790SLin.Ling@Sun.COM 		nvlist_t *label;
23789790SLin.Ling@Sun.COM 		uint64_t label_txg;
23799790SLin.Ling@Sun.COM 
23809790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
23819790SLin.Ling@Sun.COM 		    &label) != 0)
23829790SLin.Ling@Sun.COM 			return;
23839790SLin.Ling@Sun.COM 
23849790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
23859790SLin.Ling@Sun.COM 		    &label_txg) == 0);
23869790SLin.Ling@Sun.COM 
23879790SLin.Ling@Sun.COM 		/*
23889790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
23899790SLin.Ling@Sun.COM 		 */
23909790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
23919790SLin.Ling@Sun.COM 			*txg = label_txg;
23929790SLin.Ling@Sun.COM 			*avd = vd;
23937147Staylor 		}
23949790SLin.Ling@Sun.COM 		nvlist_free(label);
23957147Staylor 	}
23967147Staylor }
23977147Staylor 
23986423Sgw25295 /*
23996423Sgw25295  * Import a root pool.
24006423Sgw25295  *
24017147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
24027147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
24037147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
24046423Sgw25295  *
24056423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
24066423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
24076423Sgw25295  * e.g.
24086423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
24096423Sgw25295  */
24106423Sgw25295 int
24117147Staylor spa_import_rootpool(char *devpath, char *devid)
24126423Sgw25295 {
24139790SLin.Ling@Sun.COM 	spa_t *spa;
24149790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
24159790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
24169790SLin.Ling@Sun.COM 	uint64_t guid, txg;
24176423Sgw25295 	char *pname;
24186423Sgw25295 	int error;
24196423Sgw25295 
24206423Sgw25295 	/*
24219790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
24226423Sgw25295 	 */
24239790SLin.Ling@Sun.COM 	if ((config = spa_generate_rootconf(devpath, devid, &guid)) == NULL) {
24249790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
24259790SLin.Ling@Sun.COM 		    devpath);
24269790SLin.Ling@Sun.COM 		return (EIO);
24279790SLin.Ling@Sun.COM 	}
24289790SLin.Ling@Sun.COM 
24299790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
24309790SLin.Ling@Sun.COM 	    &pname) == 0);
24319790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
24326423Sgw25295 
24339425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
24349425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
24359425SEric.Schrock@Sun.COM 		/*
24369425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
24379425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
24389425SEric.Schrock@Sun.COM 		 */
24399425SEric.Schrock@Sun.COM 		spa_remove(spa);
24409425SEric.Schrock@Sun.COM 	}
24419425SEric.Schrock@Sun.COM 
24429425SEric.Schrock@Sun.COM 	spa = spa_add(pname, NULL);
24439425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
244410100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
24459790SLin.Ling@Sun.COM 
24469790SLin.Ling@Sun.COM 	/*
24479790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
24489790SLin.Ling@Sun.COM 	 */
24499790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
24509790SLin.Ling@Sun.COM 	    &nvtop) == 0);
24519790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
24529790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
24539790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
24549790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
24559790SLin.Ling@Sun.COM 	if (error) {
24569790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
24579790SLin.Ling@Sun.COM 		nvlist_free(config);
24589790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
24599790SLin.Ling@Sun.COM 		    pname);
24609790SLin.Ling@Sun.COM 		return (error);
24619790SLin.Ling@Sun.COM 	}
24629790SLin.Ling@Sun.COM 
24639790SLin.Ling@Sun.COM 	/*
24649790SLin.Ling@Sun.COM 	 * Get the boot vdev.
24659790SLin.Ling@Sun.COM 	 */
24669790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
24679790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
24689790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
24699790SLin.Ling@Sun.COM 		error = ENOENT;
24709790SLin.Ling@Sun.COM 		goto out;
24719790SLin.Ling@Sun.COM 	}
24729790SLin.Ling@Sun.COM 
24739790SLin.Ling@Sun.COM 	/*
24749790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
24759790SLin.Ling@Sun.COM 	 */
24769790SLin.Ling@Sun.COM 	avd = bvd;
24779790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
24789790SLin.Ling@Sun.COM 	if (avd != bvd) {
24799790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
24809790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
24819790SLin.Ling@Sun.COM 		error = EINVAL;
24829790SLin.Ling@Sun.COM 		goto out;
24839790SLin.Ling@Sun.COM 	}
24849790SLin.Ling@Sun.COM 
24859790SLin.Ling@Sun.COM 	/*
24869790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
24879790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
24889790SLin.Ling@Sun.COM 	 */
24899790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
24909790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
24919790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
24929790SLin.Ling@Sun.COM 		    "try booting from '%s'",
24939790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
24949790SLin.Ling@Sun.COM 		error = EINVAL;
24959790SLin.Ling@Sun.COM 		goto out;
24969790SLin.Ling@Sun.COM 	}
24979790SLin.Ling@Sun.COM 
24989790SLin.Ling@Sun.COM 	VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
24999790SLin.Ling@Sun.COM 	error = 0;
25009946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
25019790SLin.Ling@Sun.COM out:
25029790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
25039790SLin.Ling@Sun.COM 	vdev_free(rvd);
25049790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
25059425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
25066423Sgw25295 
25079790SLin.Ling@Sun.COM 	nvlist_free(config);
25086423Sgw25295 	return (error);
25096423Sgw25295 }
25109790SLin.Ling@Sun.COM 
25116423Sgw25295 #endif
25126423Sgw25295 
25136423Sgw25295 /*
25149425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
25159425SEric.Schrock@Sun.COM  * boot.
25169425SEric.Schrock@Sun.COM  */
25179425SEric.Schrock@Sun.COM int
25189425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
25199425SEric.Schrock@Sun.COM {
25209425SEric.Schrock@Sun.COM 	spa_t *spa;
25219425SEric.Schrock@Sun.COM 	char *altroot = NULL;
25229425SEric.Schrock@Sun.COM 
25239425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
25249425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
25259425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
25269425SEric.Schrock@Sun.COM 		return (EEXIST);
25279425SEric.Schrock@Sun.COM 	}
25289425SEric.Schrock@Sun.COM 
25299425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
25309425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
25319425SEric.Schrock@Sun.COM 	spa = spa_add(pool, altroot);
25329425SEric.Schrock@Sun.COM 
253310100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
253410000SVictor.Latushkin@Sun.COM 
25359425SEric.Schrock@Sun.COM 	VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
25369425SEric.Schrock@Sun.COM 
25379425SEric.Schrock@Sun.COM 	if (props != NULL)
25389425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
25399425SEric.Schrock@Sun.COM 
25409425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
25419425SEric.Schrock@Sun.COM 
25429425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
25439946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
25449425SEric.Schrock@Sun.COM 
25459425SEric.Schrock@Sun.COM 	return (0);
25469425SEric.Schrock@Sun.COM }
25479425SEric.Schrock@Sun.COM 
25489425SEric.Schrock@Sun.COM /*
25496423Sgw25295  * Import a non-root pool into the system.
25506423Sgw25295  */
25516423Sgw25295 int
25526423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
25536423Sgw25295 {
25549425SEric.Schrock@Sun.COM 	spa_t *spa;
25559425SEric.Schrock@Sun.COM 	char *altroot = NULL;
25569425SEric.Schrock@Sun.COM 	int error;
25579425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
25589425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
25599425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
25609425SEric.Schrock@Sun.COM 
25619425SEric.Schrock@Sun.COM 	/*
25629425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
25639425SEric.Schrock@Sun.COM 	 */
25649425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
25659425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pool)) != NULL) {
25669425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
25679425SEric.Schrock@Sun.COM 		return (EEXIST);
25689425SEric.Schrock@Sun.COM 	}
25699425SEric.Schrock@Sun.COM 
25709425SEric.Schrock@Sun.COM 	/*
25719425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
25729425SEric.Schrock@Sun.COM 	 */
25739425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
25749425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
25759425SEric.Schrock@Sun.COM 	spa = spa_add(pool, altroot);
25769425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
25779425SEric.Schrock@Sun.COM 
25789425SEric.Schrock@Sun.COM 	/*
25799630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
25809630SJeff.Bonwick@Sun.COM 	 */
25819630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
25829630SJeff.Bonwick@Sun.COM 
25839630SJeff.Bonwick@Sun.COM 	/*
25849425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
25859425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
25869425SEric.Schrock@Sun.COM 	 * doing an import.
25879425SEric.Schrock@Sun.COM 	 */
25889425SEric.Schrock@Sun.COM 	error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE);
25899425SEric.Schrock@Sun.COM 
25909425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
25919425SEric.Schrock@Sun.COM 	/*
25929425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
25939425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
25949425SEric.Schrock@Sun.COM 	 */
25959425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
25969425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
25979425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
25989425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
25999425SEric.Schrock@Sun.COM 	}
26009425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
26019425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
26029425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
26039425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
26049425SEric.Schrock@Sun.COM 	}
26059425SEric.Schrock@Sun.COM 
26069425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
26079425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
26089425SEric.Schrock@Sun.COM 	if (error == 0)
26099425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
26109425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
26119425SEric.Schrock@Sun.COM 	if (error == 0)
26129425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
26139425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
26149425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
26159425SEric.Schrock@Sun.COM 
26169425SEric.Schrock@Sun.COM 	if (props != NULL)
26179425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
26189425SEric.Schrock@Sun.COM 
26199425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
26209425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
26219425SEric.Schrock@Sun.COM 		spa_unload(spa);
26229425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
26239425SEric.Schrock@Sun.COM 		spa_remove(spa);
26249425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
26259425SEric.Schrock@Sun.COM 		return (error);
26269425SEric.Schrock@Sun.COM 	}
26279425SEric.Schrock@Sun.COM 
26289630SJeff.Bonwick@Sun.COM 	spa_async_resume(spa);
26299630SJeff.Bonwick@Sun.COM 
26309425SEric.Schrock@Sun.COM 	/*
26319425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
26329425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
26339425SEric.Schrock@Sun.COM 	 */
26349425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
26359425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
26369425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
26379425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
26389425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
26399425SEric.Schrock@Sun.COM 		else
26409425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
26419425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
26429425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
26439425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
26449425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
26459425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
26469425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
26479425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
26489425SEric.Schrock@Sun.COM 	}
26499425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
26509425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
26519425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
26529425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
26539425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
26549425SEric.Schrock@Sun.COM 		else
26559425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
26569425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
26579425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
26589425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
26599425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
26609425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
26619425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
26629425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
26639425SEric.Schrock@Sun.COM 	}
26649425SEric.Schrock@Sun.COM 
266510672SEric.Schrock@Sun.COM 	/*
266610672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
266710672SEric.Schrock@Sun.COM 	 */
266810672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
266910672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
267010672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
267110672SEric.Schrock@Sun.COM 	}
267210672SEric.Schrock@Sun.COM 
26739425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
26749425SEric.Schrock@Sun.COM 		/*
26759425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
26769425SEric.Schrock@Sun.COM 		 */
267710100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
26789425SEric.Schrock@Sun.COM 	}
26799425SEric.Schrock@Sun.COM 
26809816SGeorge.Wilson@Sun.COM 	/*
26819816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
26829816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
26839816SGeorge.Wilson@Sun.COM 	 */
26849816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
26859816SGeorge.Wilson@Sun.COM 
26869425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
26879946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
26889425SEric.Schrock@Sun.COM 
26899425SEric.Schrock@Sun.COM 	return (0);
26906643Seschrock }
26916643Seschrock 
26926643Seschrock 
2693789Sahrens /*
2694789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2695789Sahrens  * to get the vdev stats associated with the imported devices.
2696789Sahrens  */
2697789Sahrens #define	TRYIMPORT_NAME	"$import"
2698789Sahrens 
2699789Sahrens nvlist_t *
2700789Sahrens spa_tryimport(nvlist_t *tryconfig)
2701789Sahrens {
2702789Sahrens 	nvlist_t *config = NULL;
2703789Sahrens 	char *poolname;
2704789Sahrens 	spa_t *spa;
2705789Sahrens 	uint64_t state;
27068680SLin.Ling@Sun.COM 	int error;
2707789Sahrens 
2708789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2709789Sahrens 		return (NULL);
2710789Sahrens 
2711789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2712789Sahrens 		return (NULL);
2713789Sahrens 
27141635Sbonwick 	/*
27151635Sbonwick 	 * Create and initialize the spa structure.
27161635Sbonwick 	 */
2717789Sahrens 	mutex_enter(&spa_namespace_lock);
27181635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
27198241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
2720789Sahrens 
2721789Sahrens 	/*
27221635Sbonwick 	 * Pass off the heavy lifting to spa_load().
27231732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
27241732Sbonwick 	 * is actually the one to trust when doing an import.
2725789Sahrens 	 */
27268680SLin.Ling@Sun.COM 	error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2727789Sahrens 
2728789Sahrens 	/*
2729789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2730789Sahrens 	 */
2731789Sahrens 	if (spa->spa_root_vdev != NULL) {
2732789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2733789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2734789Sahrens 		    poolname) == 0);
2735789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2736789Sahrens 		    state) == 0);
27373975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
27383975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
27392082Seschrock 
27402082Seschrock 		/*
27416423Sgw25295 		 * If the bootfs property exists on this pool then we
27426423Sgw25295 		 * copy it out so that external consumers can tell which
27436423Sgw25295 		 * pools are bootable.
27446423Sgw25295 		 */
27458680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
27466423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
27476423Sgw25295 
27486423Sgw25295 			/*
27496423Sgw25295 			 * We have to play games with the name since the
27506423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
27516423Sgw25295 			 */
27527754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
27536423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
27546423Sgw25295 				char *cp;
27556423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
27566423Sgw25295 
27576423Sgw25295 				cp = strchr(tmpname, '/');
27586423Sgw25295 				if (cp == NULL) {
27596423Sgw25295 					(void) strlcpy(dsname, tmpname,
27606423Sgw25295 					    MAXPATHLEN);
27616423Sgw25295 				} else {
27626423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
27636423Sgw25295 					    "%s/%s", poolname, ++cp);
27646423Sgw25295 				}
27656423Sgw25295 				VERIFY(nvlist_add_string(config,
27666423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
27676423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
27686423Sgw25295 			}
27696423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
27706423Sgw25295 		}
27716423Sgw25295 
27726423Sgw25295 		/*
27735450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
27742082Seschrock 		 */
27759425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
27762082Seschrock 		spa_add_spares(spa, config);
27775450Sbrendan 		spa_add_l2cache(spa, config);
27789425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2779789Sahrens 	}
2780789Sahrens 
2781789Sahrens 	spa_unload(spa);
2782789Sahrens 	spa_deactivate(spa);
2783789Sahrens 	spa_remove(spa);
2784789Sahrens 	mutex_exit(&spa_namespace_lock);
2785789Sahrens 
2786789Sahrens 	return (config);
2787789Sahrens }
2788789Sahrens 
2789789Sahrens /*
2790789Sahrens  * Pool export/destroy
2791789Sahrens  *
2792789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2793789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2794789Sahrens  * update the pool state and sync all the labels to disk, removing the
27958211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
27968211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
2797789Sahrens  */
2798789Sahrens static int
27997214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
28008211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
2801789Sahrens {
2802789Sahrens 	spa_t *spa;
2803789Sahrens 
28041775Sbillm 	if (oldconfig)
28051775Sbillm 		*oldconfig = NULL;
28061775Sbillm 
28078241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
2808789Sahrens 		return (EROFS);
2809789Sahrens 
2810789Sahrens 	mutex_enter(&spa_namespace_lock);
2811789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2812789Sahrens 		mutex_exit(&spa_namespace_lock);
2813789Sahrens 		return (ENOENT);
2814789Sahrens 	}
2815789Sahrens 
2816789Sahrens 	/*
28171544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
28181544Seschrock 	 * reacquire the namespace lock, and see if we can export.
28191544Seschrock 	 */
28201544Seschrock 	spa_open_ref(spa, FTAG);
28211544Seschrock 	mutex_exit(&spa_namespace_lock);
28221544Seschrock 	spa_async_suspend(spa);
28231544Seschrock 	mutex_enter(&spa_namespace_lock);
28241544Seschrock 	spa_close(spa, FTAG);
28251544Seschrock 
28261544Seschrock 	/*
2827789Sahrens 	 * The pool will be in core if it's openable,
2828789Sahrens 	 * in which case we can modify its state.
2829789Sahrens 	 */
2830789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2831789Sahrens 		/*
2832789Sahrens 		 * Objsets may be open only because they're dirty, so we
2833789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2834789Sahrens 		 */
2835789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2836789Sahrens 
28371544Seschrock 		/*
28381544Seschrock 		 * A pool cannot be exported or destroyed if there are active
28391544Seschrock 		 * references.  If we are resetting a pool, allow references by
28401544Seschrock 		 * fault injection handlers.
28411544Seschrock 		 */
28421544Seschrock 		if (!spa_refcount_zero(spa) ||
28431544Seschrock 		    (spa->spa_inject_ref != 0 &&
28441544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
28451544Seschrock 			spa_async_resume(spa);
2846789Sahrens 			mutex_exit(&spa_namespace_lock);
2847789Sahrens 			return (EBUSY);
2848789Sahrens 		}
2849789Sahrens 
2850789Sahrens 		/*
28517214Slling 		 * A pool cannot be exported if it has an active shared spare.
28527214Slling 		 * This is to prevent other pools stealing the active spare
28537214Slling 		 * from an exported pool. At user's own will, such pool can
28547214Slling 		 * be forcedly exported.
28557214Slling 		 */
28567214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
28577214Slling 		    spa_has_active_shared_spare(spa)) {
28587214Slling 			spa_async_resume(spa);
28597214Slling 			mutex_exit(&spa_namespace_lock);
28607214Slling 			return (EXDEV);
28617214Slling 		}
28627214Slling 
28637214Slling 		/*
2864789Sahrens 		 * We want this to be reflected on every label,
2865789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2866789Sahrens 		 * final sync that pushes these changes out.
2867789Sahrens 		 */
28688211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
28697754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28701544Seschrock 			spa->spa_state = new_state;
28711635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
28721544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
28737754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
28741544Seschrock 		}
2875789Sahrens 	}
2876789Sahrens 
28774451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
28784451Seschrock 
2879789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2880789Sahrens 		spa_unload(spa);
2881789Sahrens 		spa_deactivate(spa);
2882789Sahrens 	}
2883789Sahrens 
28841775Sbillm 	if (oldconfig && spa->spa_config)
28851775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
28861775Sbillm 
28871544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
28888211SGeorge.Wilson@Sun.COM 		if (!hardforce)
28898211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
28901544Seschrock 		spa_remove(spa);
28911544Seschrock 	}
2892789Sahrens 	mutex_exit(&spa_namespace_lock);
2893789Sahrens 
2894789Sahrens 	return (0);
2895789Sahrens }
2896789Sahrens 
2897789Sahrens /*
2898789Sahrens  * Destroy a storage pool.
2899789Sahrens  */
2900789Sahrens int
2901789Sahrens spa_destroy(char *pool)
2902789Sahrens {
29038211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
29048211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
2905789Sahrens }
2906789Sahrens 
2907789Sahrens /*
2908789Sahrens  * Export a storage pool.
2909789Sahrens  */
2910789Sahrens int
29118211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
29128211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
2913789Sahrens {
29148211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
29158211SGeorge.Wilson@Sun.COM 	    force, hardforce));
2916789Sahrens }
2917789Sahrens 
2918789Sahrens /*
29191544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
29201544Seschrock  * from the namespace in any way.
29211544Seschrock  */
29221544Seschrock int
29231544Seschrock spa_reset(char *pool)
29241544Seschrock {
29257214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
29268211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
29271544Seschrock }
29281544Seschrock 
29291544Seschrock /*
2930789Sahrens  * ==========================================================================
2931789Sahrens  * Device manipulation
2932789Sahrens  * ==========================================================================
2933789Sahrens  */
2934789Sahrens 
2935789Sahrens /*
29364527Sperrin  * Add a device to a storage pool.
2937789Sahrens  */
2938789Sahrens int
2939789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2940789Sahrens {
294110594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
29428241SJeff.Bonwick@Sun.COM 	int error;
2943789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
29441585Sbonwick 	vdev_t *vd, *tvd;
29455450Sbrendan 	nvlist_t **spares, **l2cache;
29465450Sbrendan 	uint_t nspares, nl2cache;
2947789Sahrens 
2948789Sahrens 	txg = spa_vdev_enter(spa);
2949789Sahrens 
29502082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
29512082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
29522082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
29532082Seschrock 
29547754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
2955789Sahrens 
29565450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
29575450Sbrendan 	    &nspares) != 0)
29582082Seschrock 		nspares = 0;
29592082Seschrock 
29605450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
29615450Sbrendan 	    &nl2cache) != 0)
29625450Sbrendan 		nl2cache = 0;
29635450Sbrendan 
29647754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
29652082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
29667754SJeff.Bonwick@Sun.COM 
29677754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
29687754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
29697754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
29702082Seschrock 
29713377Seschrock 	/*
29725450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
29735450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
29743377Seschrock 	 */
29757754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
29763377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
29773377Seschrock 
29783377Seschrock 	/*
29793377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
29803377Seschrock 	 */
29818241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
298210594SGeorge.Wilson@Sun.COM 
298310594SGeorge.Wilson@Sun.COM 		/*
298410594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
298510594SGeorge.Wilson@Sun.COM 		 */
298610594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
298710594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
298810594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
298910594SGeorge.Wilson@Sun.COM 				break;
299010594SGeorge.Wilson@Sun.COM 			}
299110594SGeorge.Wilson@Sun.COM 		}
29923377Seschrock 		tvd = vd->vdev_child[c];
29933377Seschrock 		vdev_remove_child(vd, tvd);
299410594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
29953377Seschrock 		vdev_add_child(rvd, tvd);
29963377Seschrock 		vdev_config_dirty(tvd);
29973377Seschrock 	}
29983377Seschrock 
29992082Seschrock 	if (nspares != 0) {
30005450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
30015450Sbrendan 		    ZPOOL_CONFIG_SPARES);
30022082Seschrock 		spa_load_spares(spa);
30035450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
30045450Sbrendan 	}
30055450Sbrendan 
30065450Sbrendan 	if (nl2cache != 0) {
30075450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
30085450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
30095450Sbrendan 		spa_load_l2cache(spa);
30105450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3011789Sahrens 	}
3012789Sahrens 
3013789Sahrens 	/*
30141585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
30151585Sbonwick 	 * If other threads start allocating from these vdevs before we
30161585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
30171585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
30181585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
30191585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
30201635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
30211585Sbonwick 	 *
30221585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
30231585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
30241585Sbonwick 	 * steps will be completed the next time we load the pool.
3025789Sahrens 	 */
30261635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
30271585Sbonwick 
30281635Sbonwick 	mutex_enter(&spa_namespace_lock);
30291635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
30301635Sbonwick 	mutex_exit(&spa_namespace_lock);
3031789Sahrens 
30321635Sbonwick 	return (0);
3033789Sahrens }
3034789Sahrens 
3035789Sahrens /*
3036789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3037789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3038789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3039789Sahrens  *
3040789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3041789Sahrens  * existing device; in this case the two devices are made into their own
30424451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3043789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3044789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3045789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3046789Sahrens  * is automatically detached.
3047789Sahrens  */
3048789Sahrens int
30491544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3050789Sahrens {
3051789Sahrens 	uint64_t txg, open_txg;
3052789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3053789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
30542082Seschrock 	vdev_ops_t *pvops;
30557313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
30567313SEric.Kustarz@Sun.COM 	int newvd_isspare;
30577313SEric.Kustarz@Sun.COM 	int error;
3058789Sahrens 
3059789Sahrens 	txg = spa_vdev_enter(spa);
3060789Sahrens 
30616643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3062789Sahrens 
3063789Sahrens 	if (oldvd == NULL)
3064789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3065789Sahrens 
30661585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
30671585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30681585Sbonwick 
3069789Sahrens 	pvd = oldvd->vdev_parent;
3070789Sahrens 
30712082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
30724451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
30734451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
30744451Seschrock 
30754451Seschrock 	if (newrootvd->vdev_children != 1)
3076789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3077789Sahrens 
3078789Sahrens 	newvd = newrootvd->vdev_child[0];
3079789Sahrens 
3080789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3081789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3082789Sahrens 
30832082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3084789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3085789Sahrens 
30864527Sperrin 	/*
30874527Sperrin 	 * Spares can't replace logs
30884527Sperrin 	 */
30897326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
30904527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
30914527Sperrin 
30922082Seschrock 	if (!replacing) {
30932082Seschrock 		/*
30942082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
30952082Seschrock 		 * vdev.
30962082Seschrock 		 */
30972082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
30982082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
30992082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
31002082Seschrock 
31012082Seschrock 		pvops = &vdev_mirror_ops;
31022082Seschrock 	} else {
31032082Seschrock 		/*
31042082Seschrock 		 * Active hot spares can only be replaced by inactive hot
31052082Seschrock 		 * spares.
31062082Seschrock 		 */
31072082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
31082082Seschrock 		    pvd->vdev_child[1] == oldvd &&
31092082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
31102082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
31112082Seschrock 
31122082Seschrock 		/*
31132082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
31142082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
31153377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
31163377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
31173377Seschrock 		 * the same (spare replaces spare, non-spare replaces
31183377Seschrock 		 * non-spare).
31192082Seschrock 		 */
31202082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
31212082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
31223377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
31233377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
31243377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
31252082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
31262082Seschrock 		    newvd->vdev_isspare)
31272082Seschrock 			pvops = &vdev_spare_ops;
31282082Seschrock 		else
31292082Seschrock 			pvops = &vdev_replacing_ops;
31302082Seschrock 	}
31312082Seschrock 
31321175Slling 	/*
31339816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
31341175Slling 	 */
31359816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3136789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3137789Sahrens 
31381732Sbonwick 	/*
31391732Sbonwick 	 * The new device cannot have a higher alignment requirement
31401732Sbonwick 	 * than the top-level vdev.
31411732Sbonwick 	 */
31421732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3143789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3144789Sahrens 
3145789Sahrens 	/*
3146789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3147789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3148789Sahrens 	 */
3149789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3150789Sahrens 		spa_strfree(oldvd->vdev_path);
3151789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3152789Sahrens 		    KM_SLEEP);
3153789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3154789Sahrens 		    newvd->vdev_path, "old");
3155789Sahrens 		if (oldvd->vdev_devid != NULL) {
3156789Sahrens 			spa_strfree(oldvd->vdev_devid);
3157789Sahrens 			oldvd->vdev_devid = NULL;
3158789Sahrens 		}
3159789Sahrens 	}
3160789Sahrens 
3161789Sahrens 	/*
31622082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
31632082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3164789Sahrens 	 */
3165789Sahrens 	if (pvd->vdev_ops != pvops)
3166789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3167789Sahrens 
3168789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3169789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3170789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3171789Sahrens 
3172789Sahrens 	/*
3173789Sahrens 	 * Extract the new device from its root and add it to pvd.
3174789Sahrens 	 */
3175789Sahrens 	vdev_remove_child(newrootvd, newvd);
3176789Sahrens 	newvd->vdev_id = pvd->vdev_children;
317710594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3178789Sahrens 	vdev_add_child(pvd, newvd);
3179789Sahrens 
3180789Sahrens 	tvd = newvd->vdev_top;
3181789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3182789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3183789Sahrens 
3184789Sahrens 	vdev_config_dirty(tvd);
3185789Sahrens 
3186789Sahrens 	/*
3187789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3188789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3189789Sahrens 	 */
3190789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3191789Sahrens 
31928241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
31938241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3194789Sahrens 
31959425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
31963377Seschrock 		spa_spare_activate(newvd);
31979425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
31989425SEric.Schrock@Sun.COM 	}
31999425SEric.Schrock@Sun.COM 
32007754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
32017754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
32027313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
32031544Seschrock 
3204789Sahrens 	/*
3205789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3206789Sahrens 	 */
32071732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3208789Sahrens 
3209789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3210789Sahrens 
32119946SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
32129946SMark.Musante@Sun.COM 	    CRED(),  "%s vdev=%s %s vdev=%s",
32139946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
32149946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
32159946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
32167313SEric.Kustarz@Sun.COM 
32177313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
32187313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
32197313SEric.Kustarz@Sun.COM 
3220789Sahrens 	/*
32217046Sahrens 	 * Kick off a resilver to update newvd.
3222789Sahrens 	 */
32237046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3224789Sahrens 
3225789Sahrens 	return (0);
3226789Sahrens }
3227789Sahrens 
3228789Sahrens /*
3229789Sahrens  * Detach a device from a mirror or replacing vdev.
3230789Sahrens  * If 'replace_done' is specified, only detach if the parent
3231789Sahrens  * is a replacing vdev.
3232789Sahrens  */
3233789Sahrens int
32348241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3235789Sahrens {
3236789Sahrens 	uint64_t txg;
32378241SJeff.Bonwick@Sun.COM 	int error;
3238789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3239789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
32402082Seschrock 	boolean_t unspare = B_FALSE;
32412082Seschrock 	uint64_t unspare_guid;
32426673Seschrock 	size_t len;
3243789Sahrens 
3244789Sahrens 	txg = spa_vdev_enter(spa);
3245789Sahrens 
32466643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3247789Sahrens 
3248789Sahrens 	if (vd == NULL)
3249789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3250789Sahrens 
32511585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
32521585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
32531585Sbonwick 
3254789Sahrens 	pvd = vd->vdev_parent;
3255789Sahrens 
3256789Sahrens 	/*
32578241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
32588241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
32598241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
32608241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
32618241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
32628241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
32638241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
32648241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
32658241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
32668241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
32678241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
32688241SJeff.Bonwick@Sun.COM 	 */
32698241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
32708241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
32718241SJeff.Bonwick@Sun.COM 
32728241SJeff.Bonwick@Sun.COM 	/*
3273789Sahrens 	 * If replace_done is specified, only remove this device if it's
32742082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
32752082Seschrock 	 * disk can be removed.
3276789Sahrens 	 */
32772082Seschrock 	if (replace_done) {
32782082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
32792082Seschrock 			if (vd->vdev_id != 0)
32802082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
32812082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
32822082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
32832082Seschrock 		}
32842082Seschrock 	}
32852082Seschrock 
32862082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
32874577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3288789Sahrens 
3289789Sahrens 	/*
32902082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3291789Sahrens 	 */
3292789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
32932082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
32942082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3295789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3296789Sahrens 
3297789Sahrens 	/*
32988241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
32998241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3300789Sahrens 	 */
33018241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3302789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3303789Sahrens 
33048241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
33058241SJeff.Bonwick@Sun.COM 
3306789Sahrens 	/*
33076673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
33086673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
33096673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
33106673Seschrock 	 */
33116673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
33126673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
33136673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
33146673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
33156673Seschrock 		cvd = pvd->vdev_child[0];
33166673Seschrock 		len = strlen(vd->vdev_path);
33176673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
33186673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
33196673Seschrock 			spa_strfree(cvd->vdev_path);
33206673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
33216673Seschrock 		}
33226673Seschrock 	}
33236673Seschrock 
33246673Seschrock 	/*
33252082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
33262082Seschrock 	 * that the spare should become a real disk, and be removed from the
33272082Seschrock 	 * active spare list for the pool.
33282082Seschrock 	 */
33292082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
33308241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
33312082Seschrock 		unspare = B_TRUE;
33322082Seschrock 
33332082Seschrock 	/*
3334789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3335789Sahrens 	 * This must be done after all other error cases are handled,
3336789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3337789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3338789Sahrens 	 * it may be that the unwritability of the disk is the reason
3339789Sahrens 	 * it's being detached!
3340789Sahrens 	 */
33413377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3342789Sahrens 
3343789Sahrens 	/*
3344789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3345789Sahrens 	 */
3346789Sahrens 	vdev_remove_child(pvd, vd);
3347789Sahrens 	vdev_compact_children(pvd);
3348789Sahrens 
3349789Sahrens 	/*
3350789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3351789Sahrens 	 */
3352789Sahrens 	cvd = pvd->vdev_child[0];
3353789Sahrens 
3354789Sahrens 	/*
33552082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
33568241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
33578241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
33588241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
33598241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
33608241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
33618241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
33622082Seschrock 	 */
33632082Seschrock 	if (unspare) {
33642082Seschrock 		ASSERT(cvd->vdev_isspare);
33653377Seschrock 		spa_spare_remove(cvd);
33662082Seschrock 		unspare_guid = cvd->vdev_guid;
33678241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
33682082Seschrock 	}
33692082Seschrock 
33702082Seschrock 	/*
3371789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3372789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3373789Sahrens 	 */
3374789Sahrens 	if (pvd->vdev_children == 1)
3375789Sahrens 		vdev_remove_parent(cvd);
3376789Sahrens 
3377789Sahrens 	/*
3378789Sahrens 	 * We don't set tvd until now because the parent we just removed
3379789Sahrens 	 * may have been the previous top-level vdev.
3380789Sahrens 	 */
3381789Sahrens 	tvd = cvd->vdev_top;
3382789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3383789Sahrens 
3384789Sahrens 	/*
33853377Seschrock 	 * Reevaluate the parent vdev state.
3386789Sahrens 	 */
33874451Seschrock 	vdev_propagate_state(cvd);
3388789Sahrens 
3389789Sahrens 	/*
33909816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
33919816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
33929816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
33939816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
33949816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3395789Sahrens 	 */
33969816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
33979816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
33989816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
33999816SGeorge.Wilson@Sun.COM 	}
3400789Sahrens 
3401789Sahrens 	vdev_config_dirty(tvd);
3402789Sahrens 
3403789Sahrens 	/*
34043377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
34053377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
34063377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
34073377Seschrock 	 * prevent vd from being accessed after it's freed.
3408789Sahrens 	 */
34098241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3410789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
34111732Sbonwick 	vd->vdev_detached = B_TRUE;
34121732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3413789Sahrens 
34144451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
34154451Seschrock 
34162082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
34172082Seschrock 
34182082Seschrock 	/*
34193377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
34203377Seschrock 	 * then we want to go through and remove the device from the hot spare
34213377Seschrock 	 * list of every other pool.
34222082Seschrock 	 */
34232082Seschrock 	if (unspare) {
34248241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
34252082Seschrock 		spa = NULL;
34262082Seschrock 		mutex_enter(&spa_namespace_lock);
34272082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
34282082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
34292082Seschrock 				continue;
34308241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
34318241SJeff.Bonwick@Sun.COM 				continue;
34327793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
34337793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
34342082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
34357793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
34367793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
34372082Seschrock 		}
34382082Seschrock 		mutex_exit(&spa_namespace_lock);
34392082Seschrock 	}
34402082Seschrock 
34412082Seschrock 	return (error);
34422082Seschrock }
34432082Seschrock 
34447754SJeff.Bonwick@Sun.COM static nvlist_t *
34457754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
34462082Seschrock {
34477754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
34487754SJeff.Bonwick@Sun.COM 		uint64_t guid;
34497754SJeff.Bonwick@Sun.COM 
34507754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
34517754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
34527754SJeff.Bonwick@Sun.COM 
34537754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
34547754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
34552082Seschrock 	}
34562082Seschrock 
34577754SJeff.Bonwick@Sun.COM 	return (NULL);
34585450Sbrendan }
34595450Sbrendan 
34607754SJeff.Bonwick@Sun.COM static void
34617754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
34627754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
34635450Sbrendan {
34647754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
34657754SJeff.Bonwick@Sun.COM 
34667754SJeff.Bonwick@Sun.COM 	if (count > 1)
34677754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
34687754SJeff.Bonwick@Sun.COM 
34697754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
34707754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
34717754SJeff.Bonwick@Sun.COM 			continue;
34727754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
34735450Sbrendan 	}
34745450Sbrendan 
34757754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
34767754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
34777754SJeff.Bonwick@Sun.COM 
34787754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
34797754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
34807754SJeff.Bonwick@Sun.COM 
34817754SJeff.Bonwick@Sun.COM 	if (count > 1)
34827754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
34835450Sbrendan }
34845450Sbrendan 
34855450Sbrendan /*
348610594SGeorge.Wilson@Sun.COM  * Removing a device from the vdev namespace requires several steps
348710594SGeorge.Wilson@Sun.COM  * and can take a significant amount of time.  As a result we use
348810594SGeorge.Wilson@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
348910594SGeorge.Wilson@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
349010594SGeorge.Wilson@Sun.COM  * lock.  During each step the configuration is synced out.
349110594SGeorge.Wilson@Sun.COM  */
349210594SGeorge.Wilson@Sun.COM 
349310594SGeorge.Wilson@Sun.COM /*
349410594SGeorge.Wilson@Sun.COM  * Initial phase of device removal - stop future allocations from this device.
349510594SGeorge.Wilson@Sun.COM  */
349610594SGeorge.Wilson@Sun.COM void
349710594SGeorge.Wilson@Sun.COM spa_vdev_remove_start(spa_t *spa, vdev_t *vd)
349810594SGeorge.Wilson@Sun.COM {
349910594SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
350010594SGeorge.Wilson@Sun.COM 
350110594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
350210594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
350310594SGeorge.Wilson@Sun.COM 
350410594SGeorge.Wilson@Sun.COM 	/*
350510594SGeorge.Wilson@Sun.COM 	 * Remove our vdev from the allocatable vdevs
350610594SGeorge.Wilson@Sun.COM 	 */
350710594SGeorge.Wilson@Sun.COM 	if (mg)
350810594SGeorge.Wilson@Sun.COM 		metaslab_class_remove(mg->mg_class, mg);
350910594SGeorge.Wilson@Sun.COM }
351010594SGeorge.Wilson@Sun.COM 
351110594SGeorge.Wilson@Sun.COM /*
351210594SGeorge.Wilson@Sun.COM  * Evacuate the device.
351310594SGeorge.Wilson@Sun.COM  */
351410594SGeorge.Wilson@Sun.COM int
351510594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
351610594SGeorge.Wilson@Sun.COM {
351710594SGeorge.Wilson@Sun.COM 	uint64_t txg;
351810594SGeorge.Wilson@Sun.COM 	int error;
351910594SGeorge.Wilson@Sun.COM 
352010594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
352110594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
352210594SGeorge.Wilson@Sun.COM 
352310594SGeorge.Wilson@Sun.COM 	/*
352410594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
352510594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
352610594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
352710594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
352810594SGeorge.Wilson@Sun.COM 	 */
352910594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
353010594SGeorge.Wilson@Sun.COM 		/*
353110594SGeorge.Wilson@Sun.COM 		 * Evacuate the device.
353210594SGeorge.Wilson@Sun.COM 		 */
353310594SGeorge.Wilson@Sun.COM 		if (error = dmu_objset_find(spa_name(spa),
353410594SGeorge.Wilson@Sun.COM 		    zil_vdev_offline, NULL, DS_FIND_CHILDREN)) {
353510594SGeorge.Wilson@Sun.COM 			uint64_t txg;
353610594SGeorge.Wilson@Sun.COM 
353710594SGeorge.Wilson@Sun.COM 			txg = spa_vdev_config_enter(spa);
353810594SGeorge.Wilson@Sun.COM 			metaslab_class_add(spa->spa_log_class,
353910594SGeorge.Wilson@Sun.COM 			    vd->vdev_mg);
354010594SGeorge.Wilson@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
354110594SGeorge.Wilson@Sun.COM 		}
354210594SGeorge.Wilson@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
354310594SGeorge.Wilson@Sun.COM 	}
354410594SGeorge.Wilson@Sun.COM 
354510594SGeorge.Wilson@Sun.COM 	/*
354610594SGeorge.Wilson@Sun.COM 	 * Remove any remaining MOS metadata associated with the device.
354710594SGeorge.Wilson@Sun.COM 	 */
354810594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
354910594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
355010594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
355110594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
355210594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
355310594SGeorge.Wilson@Sun.COM 
355410594SGeorge.Wilson@Sun.COM 	return (0);
355510594SGeorge.Wilson@Sun.COM }
355610594SGeorge.Wilson@Sun.COM 
355710594SGeorge.Wilson@Sun.COM /*
355810594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
355910594SGeorge.Wilson@Sun.COM  */
356010594SGeorge.Wilson@Sun.COM void
356110594SGeorge.Wilson@Sun.COM spa_vdev_remove_done(spa_t *spa, vdev_t *vd)
356210594SGeorge.Wilson@Sun.COM {
356310594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
356410594SGeorge.Wilson@Sun.COM 	metaslab_group_t *mg = vd->vdev_mg;
356510594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
356610594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
356710594SGeorge.Wilson@Sun.COM 
356810594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
356910594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
357010594SGeorge.Wilson@Sun.COM 
357110594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
357210594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
357310594SGeorge.Wilson@Sun.COM 
357410594SGeorge.Wilson@Sun.COM 	/*
357510594SGeorge.Wilson@Sun.COM 	 * It's possible that another thread is trying todo a spa_vdev_add()
357610594SGeorge.Wilson@Sun.COM 	 * at the same time we're trying remove it. As a result the
357710594SGeorge.Wilson@Sun.COM 	 * added vdev may not have initialized its metaslabs yet.
357810594SGeorge.Wilson@Sun.COM 	 */
357910594SGeorge.Wilson@Sun.COM 	if (mg != NULL)
358010594SGeorge.Wilson@Sun.COM 		metaslab_group_destroy(mg);
358110594SGeorge.Wilson@Sun.COM 
358210594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
358310594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
358410594SGeorge.Wilson@Sun.COM 	} else {
358510594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
358610594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
358710594SGeorge.Wilson@Sun.COM 	}
358810594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(rvd);
358910594SGeorge.Wilson@Sun.COM 
359010594SGeorge.Wilson@Sun.COM 	/*
359110594SGeorge.Wilson@Sun.COM 	 * Reassess the health of our root vdev.
359210594SGeorge.Wilson@Sun.COM 	 */
359310594SGeorge.Wilson@Sun.COM 	vdev_reopen(rvd);
359410594SGeorge.Wilson@Sun.COM }
359510594SGeorge.Wilson@Sun.COM 
359610594SGeorge.Wilson@Sun.COM /*
35975450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
359810594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
35995450Sbrendan  */
36005450Sbrendan int
36015450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
36025450Sbrendan {
36035450Sbrendan 	vdev_t *vd;
36047754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
360510594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
36065450Sbrendan 	uint_t nspares, nl2cache;
36075450Sbrendan 	int error = 0;
36088241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
36098241SJeff.Bonwick@Sun.COM 
36108241SJeff.Bonwick@Sun.COM 	if (!locked)
36118241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
36125450Sbrendan 
36136643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
36145450Sbrendan 
36155450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
36165450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
36177754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
36187754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
36197754SJeff.Bonwick@Sun.COM 		/*
36207754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
36217754SJeff.Bonwick@Sun.COM 		 * in this pool.
36227754SJeff.Bonwick@Sun.COM 		 */
36237754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
36247754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
36257754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
36267754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
36277754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
36287754SJeff.Bonwick@Sun.COM 		} else {
36297754SJeff.Bonwick@Sun.COM 			error = EBUSY;
36307754SJeff.Bonwick@Sun.COM 		}
36317754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
36325450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
36337754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
36347754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
36357754SJeff.Bonwick@Sun.COM 		/*
36367754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
36377754SJeff.Bonwick@Sun.COM 		 */
36387754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
36397754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
36405450Sbrendan 		spa_load_l2cache(spa);
36415450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
364210594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
364310594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
364410594SGeorge.Wilson@Sun.COM 
364510594SGeorge.Wilson@Sun.COM 		/*
364610594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
364710594SGeorge.Wilson@Sun.COM 		 * become the common case.
364810594SGeorge.Wilson@Sun.COM 		 */
364910594SGeorge.Wilson@Sun.COM 
365010594SGeorge.Wilson@Sun.COM 		/*
365110594SGeorge.Wilson@Sun.COM 		 * 1. Stop allocations
365210594SGeorge.Wilson@Sun.COM 		 * 2. Evacuate the device (i.e. kill off stubby and
365310594SGeorge.Wilson@Sun.COM 		 *    metadata) and wait for it to complete (i.e. sync).
365410594SGeorge.Wilson@Sun.COM 		 * 3. Cleanup the vdev namespace.
365510594SGeorge.Wilson@Sun.COM 		 */
365610594SGeorge.Wilson@Sun.COM 		spa_vdev_remove_start(spa, vd);
365710594SGeorge.Wilson@Sun.COM 
365810594SGeorge.Wilson@Sun.COM 		spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
365910594SGeorge.Wilson@Sun.COM 		if ((error = spa_vdev_remove_evacuate(spa, vd)) != 0)
366010594SGeorge.Wilson@Sun.COM 			return (error);
366110594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
366210594SGeorge.Wilson@Sun.COM 
366310594SGeorge.Wilson@Sun.COM 		spa_vdev_remove_done(spa, vd);
366410594SGeorge.Wilson@Sun.COM 
36657754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
36667754SJeff.Bonwick@Sun.COM 		/*
36677754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
36687754SJeff.Bonwick@Sun.COM 		 */
36697754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
36707754SJeff.Bonwick@Sun.COM 	} else {
36717754SJeff.Bonwick@Sun.COM 		/*
36727754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
36737754SJeff.Bonwick@Sun.COM 		 */
36747754SJeff.Bonwick@Sun.COM 		error = ENOENT;
36755450Sbrendan 	}
36762082Seschrock 
36778241SJeff.Bonwick@Sun.COM 	if (!locked)
36788241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
36798241SJeff.Bonwick@Sun.COM 
36808241SJeff.Bonwick@Sun.COM 	return (error);
3681789Sahrens }
3682789Sahrens 
3683789Sahrens /*
36844451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
36854451Seschrock  * current spared, so we can detach it.
3686789Sahrens  */
36871544Seschrock static vdev_t *
36884451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3689789Sahrens {
36901544Seschrock 	vdev_t *newvd, *oldvd;
36919816SGeorge.Wilson@Sun.COM 
36929816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
36934451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
36941544Seschrock 		if (oldvd != NULL)
36951544Seschrock 			return (oldvd);
36961544Seschrock 	}
3697789Sahrens 
36984451Seschrock 	/*
36994451Seschrock 	 * Check for a completed replacement.
37004451Seschrock 	 */
3701789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
37021544Seschrock 		oldvd = vd->vdev_child[0];
37031544Seschrock 		newvd = vd->vdev_child[1];
3704789Sahrens 
37058241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
37068241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
37071544Seschrock 			return (oldvd);
37081544Seschrock 	}
3709789Sahrens 
37104451Seschrock 	/*
37114451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
37124451Seschrock 	 */
37134451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
37144451Seschrock 		newvd = vd->vdev_child[0];
37154451Seschrock 		oldvd = vd->vdev_child[1];
37164451Seschrock 
37174451Seschrock 		if (newvd->vdev_unspare &&
37188241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
37198241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
37204451Seschrock 			newvd->vdev_unspare = 0;
37214451Seschrock 			return (oldvd);
37224451Seschrock 		}
37234451Seschrock 	}
37244451Seschrock 
37251544Seschrock 	return (NULL);
3726789Sahrens }
3727789Sahrens 
37281544Seschrock static void
37294451Seschrock spa_vdev_resilver_done(spa_t *spa)
3730789Sahrens {
37318241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
37328241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
37338241SJeff.Bonwick@Sun.COM 
37348241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3735789Sahrens 
37364451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
37378241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
37388241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
37391544Seschrock 		guid = vd->vdev_guid;
37408241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
37418241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
37428241SJeff.Bonwick@Sun.COM 		sguid = 0;
37432082Seschrock 		/*
37442082Seschrock 		 * If we have just finished replacing a hot spared device, then
37452082Seschrock 		 * we need to detach the parent's first child (the original hot
37462082Seschrock 		 * spare) as well.
37472082Seschrock 		 */
37488241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
37492082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
37508241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
37518241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
37522082Seschrock 		}
37538241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
37548241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
37551544Seschrock 			return;
37568241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
37572082Seschrock 			return;
37588241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3759789Sahrens 	}
3760789Sahrens 
37618241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
3762789Sahrens }
3763789Sahrens 
3764789Sahrens /*
37659425SEric.Schrock@Sun.COM  * Update the stored path or FRU for this vdev.  Dirty the vdev configuration,
37669425SEric.Schrock@Sun.COM  * relying on spa_vdev_enter/exit() to synchronize the labels and cache.
37671354Seschrock  */
37681354Seschrock int
37699425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
37709425SEric.Schrock@Sun.COM     boolean_t ispath)
37711354Seschrock {
37726643Seschrock 	vdev_t *vd;
37731354Seschrock 	uint64_t txg;
37741354Seschrock 
37751354Seschrock 	txg = spa_vdev_enter(spa);
37761354Seschrock 
37779425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
37785450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
37791354Seschrock 
37801585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
37811585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
37821585Sbonwick 
37839425SEric.Schrock@Sun.COM 	if (ispath) {
37849425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_path);
37859425SEric.Schrock@Sun.COM 		vd->vdev_path = spa_strdup(value);
37869425SEric.Schrock@Sun.COM 	} else {
37879425SEric.Schrock@Sun.COM 		if (vd->vdev_fru != NULL)
37889425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
37899425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(value);
37909425SEric.Schrock@Sun.COM 	}
37911354Seschrock 
37921354Seschrock 	vdev_config_dirty(vd->vdev_top);
37931354Seschrock 
37941354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
37951354Seschrock }
37961354Seschrock 
37979425SEric.Schrock@Sun.COM int
37989425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
37999425SEric.Schrock@Sun.COM {
38009425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
38019425SEric.Schrock@Sun.COM }
38029425SEric.Schrock@Sun.COM 
38039425SEric.Schrock@Sun.COM int
38049425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
38059425SEric.Schrock@Sun.COM {
38069425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
38079425SEric.Schrock@Sun.COM }
38089425SEric.Schrock@Sun.COM 
38091354Seschrock /*
3810789Sahrens  * ==========================================================================
3811789Sahrens  * SPA Scrubbing
3812789Sahrens  * ==========================================================================
3813789Sahrens  */
3814789Sahrens 
38157046Sahrens int
38167046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3817789Sahrens {
38187754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
38194808Sek110237 
3820789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3821789Sahrens 		return (ENOTSUP);
3822789Sahrens 
3823789Sahrens 	/*
38247046Sahrens 	 * If a resilver was requested, but there is no DTL on a
38257046Sahrens 	 * writeable leaf device, we have nothing to do.
3826789Sahrens 	 */
38277046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
38287046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
38297046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
38301544Seschrock 		return (0);
38311544Seschrock 	}
3832789Sahrens 
38337046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
38347046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
38357046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
38367046Sahrens 		return (EBUSY);
38377046Sahrens 
38387046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
38397046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
38407046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
38417046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
38421544Seschrock 	} else {
38437046Sahrens 		return (EINVAL);
38441544Seschrock 	}
3845789Sahrens }
3846789Sahrens 
38471544Seschrock /*
38481544Seschrock  * ==========================================================================
38491544Seschrock  * SPA async task processing
38501544Seschrock  * ==========================================================================
38511544Seschrock  */
38521544Seschrock 
38531544Seschrock static void
38544451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3855789Sahrens {
38567361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
38577361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
38587361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
385910575SEric.Schrock@Sun.COM 
386010575SEric.Schrock@Sun.COM 		/*
386110575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
386210575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
386310575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
386410575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
386510575SEric.Schrock@Sun.COM 		 */
386610575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
386710575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
386810575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
386910575SEric.Schrock@Sun.COM 
38707754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
38711544Seschrock 	}
38727361SBrendan.Gregg@Sun.COM 
38737754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
38747361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
38751544Seschrock }
38761544Seschrock 
38771544Seschrock static void
38787754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
38797754SJeff.Bonwick@Sun.COM {
38807754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
38817754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
38827754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
38837754SJeff.Bonwick@Sun.COM 	}
38847754SJeff.Bonwick@Sun.COM 
38857754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
38867754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
38877754SJeff.Bonwick@Sun.COM }
38887754SJeff.Bonwick@Sun.COM 
38897754SJeff.Bonwick@Sun.COM static void
38909816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
38919816SGeorge.Wilson@Sun.COM {
38929816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
38939816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
38949816SGeorge.Wilson@Sun.COM 	char *physpath;
38959816SGeorge.Wilson@Sun.COM 
38969816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
38979816SGeorge.Wilson@Sun.COM 		return;
38989816SGeorge.Wilson@Sun.COM 
38999816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
39009816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
39019816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
39029816SGeorge.Wilson@Sun.COM 	}
39039816SGeorge.Wilson@Sun.COM 
39049816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
39059816SGeorge.Wilson@Sun.COM 		return;
39069816SGeorge.Wilson@Sun.COM 
39079816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
39089816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
39099816SGeorge.Wilson@Sun.COM 
39109816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
39119816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
39129816SGeorge.Wilson@Sun.COM 
39139816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
39149816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
39159816SGeorge.Wilson@Sun.COM 
39169816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
39179816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
39189816SGeorge.Wilson@Sun.COM }
39199816SGeorge.Wilson@Sun.COM 
39209816SGeorge.Wilson@Sun.COM static void
39211544Seschrock spa_async_thread(spa_t *spa)
39221544Seschrock {
39237754SJeff.Bonwick@Sun.COM 	int tasks;
39241544Seschrock 
39251544Seschrock 	ASSERT(spa->spa_sync_on);
3926789Sahrens 
39271544Seschrock 	mutex_enter(&spa->spa_async_lock);
39281544Seschrock 	tasks = spa->spa_async_tasks;
39291544Seschrock 	spa->spa_async_tasks = 0;
39301544Seschrock 	mutex_exit(&spa->spa_async_lock);
39311544Seschrock 
39321544Seschrock 	/*
39331635Sbonwick 	 * See if the config needs to be updated.
39341635Sbonwick 	 */
39351635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
39369816SGeorge.Wilson@Sun.COM 		uint64_t oldsz, space_update;
39379816SGeorge.Wilson@Sun.COM 
39381635Sbonwick 		mutex_enter(&spa_namespace_lock);
39399816SGeorge.Wilson@Sun.COM 		oldsz = spa_get_space(spa);
39401635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
39419816SGeorge.Wilson@Sun.COM 		space_update = spa_get_space(spa) - oldsz;
39421635Sbonwick 		mutex_exit(&spa_namespace_lock);
39439816SGeorge.Wilson@Sun.COM 
39449816SGeorge.Wilson@Sun.COM 		/*
39459816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
39469816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
39479816SGeorge.Wilson@Sun.COM 		 */
39489816SGeorge.Wilson@Sun.COM 		if (space_update) {
39499946SMark.Musante@Sun.COM 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
39509946SMark.Musante@Sun.COM 			    spa, NULL, CRED(),
39519946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
39529946SMark.Musante@Sun.COM 			    spa_name(spa), spa_get_space(spa),
39539946SMark.Musante@Sun.COM 			    space_update);
39549816SGeorge.Wilson@Sun.COM 		}
39551635Sbonwick 	}
39561635Sbonwick 
39571635Sbonwick 	/*
39584451Seschrock 	 * See if any devices need to be marked REMOVED.
39591544Seschrock 	 */
39607754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
3961*10685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
39624451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
39637754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
39647361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
39657754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
39667361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
39677754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
39687754SJeff.Bonwick@Sun.COM 	}
39697754SJeff.Bonwick@Sun.COM 
39709816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
39719816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
39729816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
39739816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
39749816SGeorge.Wilson@Sun.COM 	}
39759816SGeorge.Wilson@Sun.COM 
39767754SJeff.Bonwick@Sun.COM 	/*
39777754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
39787754SJeff.Bonwick@Sun.COM 	 */
39797754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
3980*10685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
39817754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
39827754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
39834451Seschrock 	}
39841544Seschrock 
39851544Seschrock 	/*
39861544Seschrock 	 * If any devices are done replacing, detach them.
39871544Seschrock 	 */
39884451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
39894451Seschrock 		spa_vdev_resilver_done(spa);
3990789Sahrens 
39911544Seschrock 	/*
39921544Seschrock 	 * Kick off a resilver.
39931544Seschrock 	 */
39947046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
39957046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
39961544Seschrock 
39971544Seschrock 	/*
39981544Seschrock 	 * Let the world know that we're done.
39991544Seschrock 	 */
40001544Seschrock 	mutex_enter(&spa->spa_async_lock);
40011544Seschrock 	spa->spa_async_thread = NULL;
40021544Seschrock 	cv_broadcast(&spa->spa_async_cv);
40031544Seschrock 	mutex_exit(&spa->spa_async_lock);
40041544Seschrock 	thread_exit();
40051544Seschrock }
40061544Seschrock 
40071544Seschrock void
40081544Seschrock spa_async_suspend(spa_t *spa)
40091544Seschrock {
40101544Seschrock 	mutex_enter(&spa->spa_async_lock);
40111544Seschrock 	spa->spa_async_suspended++;
40121544Seschrock 	while (spa->spa_async_thread != NULL)
40131544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
40141544Seschrock 	mutex_exit(&spa->spa_async_lock);
40151544Seschrock }
40161544Seschrock 
40171544Seschrock void
40181544Seschrock spa_async_resume(spa_t *spa)
40191544Seschrock {
40201544Seschrock 	mutex_enter(&spa->spa_async_lock);
40211544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
40221544Seschrock 	spa->spa_async_suspended--;
40231544Seschrock 	mutex_exit(&spa->spa_async_lock);
40241544Seschrock }
40251544Seschrock 
40261544Seschrock static void
40271544Seschrock spa_async_dispatch(spa_t *spa)
40281544Seschrock {
40291544Seschrock 	mutex_enter(&spa->spa_async_lock);
40301544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
40311635Sbonwick 	    spa->spa_async_thread == NULL &&
40321635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
40331544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
40341544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
40351544Seschrock 	mutex_exit(&spa->spa_async_lock);
40361544Seschrock }
40371544Seschrock 
40381544Seschrock void
40391544Seschrock spa_async_request(spa_t *spa, int task)
40401544Seschrock {
40411544Seschrock 	mutex_enter(&spa->spa_async_lock);
40421544Seschrock 	spa->spa_async_tasks |= task;
40431544Seschrock 	mutex_exit(&spa->spa_async_lock);
4044789Sahrens }
4045789Sahrens 
4046789Sahrens /*
4047789Sahrens  * ==========================================================================
4048789Sahrens  * SPA syncing routines
4049789Sahrens  * ==========================================================================
4050789Sahrens  */
4051789Sahrens 
4052789Sahrens static void
4053789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
4054789Sahrens {
4055789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
4056789Sahrens 	dmu_tx_t *tx;
4057789Sahrens 	blkptr_t blk;
4058789Sahrens 	uint64_t itor = 0;
4059789Sahrens 	zio_t *zio;
4060789Sahrens 	int error;
4061789Sahrens 	uint8_t c = 1;
4062789Sahrens 
40637754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
40647754SJeff.Bonwick@Sun.COM 
40657754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
40667754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
40677754SJeff.Bonwick@Sun.COM 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
40687754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
40697754SJeff.Bonwick@Sun.COM 	}
4070789Sahrens 
4071789Sahrens 	error = zio_wait(zio);
4072789Sahrens 	ASSERT3U(error, ==, 0);
4073789Sahrens 
4074789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
4075789Sahrens 	bplist_vacate(bpl, tx);
4076789Sahrens 
4077789Sahrens 	/*
4078789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4079789Sahrens 	 * (Usually only the first block is needed.)
4080789Sahrens 	 */
4081789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
4082789Sahrens 	dmu_tx_commit(tx);
4083789Sahrens }
4084789Sahrens 
4085789Sahrens static void
40862082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
40872082Seschrock {
40882082Seschrock 	char *packed = NULL;
40897497STim.Haley@Sun.COM 	size_t bufsize;
40902082Seschrock 	size_t nvsize = 0;
40912082Seschrock 	dmu_buf_t *db;
40922082Seschrock 
40932082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
40942082Seschrock 
40957497STim.Haley@Sun.COM 	/*
40967497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
40977497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
40987497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
40997497STim.Haley@Sun.COM 	 */
41007497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
41017497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
41022082Seschrock 
41032082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
41042082Seschrock 	    KM_SLEEP) == 0);
41057497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
41067497STim.Haley@Sun.COM 
41077497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
41087497STim.Haley@Sun.COM 
41097497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
41102082Seschrock 
41112082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
41122082Seschrock 	dmu_buf_will_dirty(db, tx);
41132082Seschrock 	*(uint64_t *)db->db_data = nvsize;
41142082Seschrock 	dmu_buf_rele(db, FTAG);
41152082Seschrock }
41162082Seschrock 
41172082Seschrock static void
41185450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
41195450Sbrendan     const char *config, const char *entry)
41202082Seschrock {
41212082Seschrock 	nvlist_t *nvroot;
41225450Sbrendan 	nvlist_t **list;
41232082Seschrock 	int i;
41242082Seschrock 
41255450Sbrendan 	if (!sav->sav_sync)
41262082Seschrock 		return;
41272082Seschrock 
41282082Seschrock 	/*
41295450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
41305450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
41314451Seschrock 	 * valid and the vdevs are labeled appropriately.
41322082Seschrock 	 */
41335450Sbrendan 	if (sav->sav_object == 0) {
41345450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
41355450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
41365450Sbrendan 		    sizeof (uint64_t), tx);
41372082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
41385450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
41395450Sbrendan 		    &sav->sav_object, tx) == 0);
41402082Seschrock 	}
41412082Seschrock 
41422082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
41435450Sbrendan 	if (sav->sav_count == 0) {
41445450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
41452082Seschrock 	} else {
41465450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
41475450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
41485450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
41495450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
41505450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
41515450Sbrendan 		    sav->sav_count) == 0);
41525450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
41535450Sbrendan 			nvlist_free(list[i]);
41545450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
41552082Seschrock 	}
41562082Seschrock 
41575450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
41582926Sek110237 	nvlist_free(nvroot);
41592082Seschrock 
41605450Sbrendan 	sav->sav_sync = B_FALSE;
41612082Seschrock }
41622082Seschrock 
41632082Seschrock static void
4164789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
4165789Sahrens {
4166789Sahrens 	nvlist_t *config;
4167789Sahrens 
41687754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
4169789Sahrens 		return;
4170789Sahrens 
41717754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
41727754SJeff.Bonwick@Sun.COM 
41737754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
41747754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
41757754SJeff.Bonwick@Sun.COM 
41767754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
4177789Sahrens 
41781635Sbonwick 	if (spa->spa_config_syncing)
41791635Sbonwick 		nvlist_free(spa->spa_config_syncing);
41801635Sbonwick 	spa->spa_config_syncing = config;
4181789Sahrens 
41822082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
4183789Sahrens }
4184789Sahrens 
41855094Slling /*
41865094Slling  * Set zpool properties.
41875094Slling  */
41883912Slling static void
41894543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
41903912Slling {
41913912Slling 	spa_t *spa = arg1;
41925094Slling 	objset_t *mos = spa->spa_meta_objset;
41933912Slling 	nvlist_t *nvp = arg2;
41945094Slling 	nvpair_t *elem;
41954451Seschrock 	uint64_t intval;
41966643Seschrock 	char *strval;
41975094Slling 	zpool_prop_t prop;
41985094Slling 	const char *propname;
41995094Slling 	zprop_type_t proptype;
42005094Slling 
42017754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
42027754SJeff.Bonwick@Sun.COM 
42035094Slling 	elem = NULL;
42045094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
42055094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
42065094Slling 		case ZPOOL_PROP_VERSION:
42075094Slling 			/*
42085094Slling 			 * Only set version for non-zpool-creation cases
42095094Slling 			 * (set/import). spa_create() needs special care
42105094Slling 			 * for version setting.
42115094Slling 			 */
42125094Slling 			if (tx->tx_txg != TXG_INITIAL) {
42135094Slling 				VERIFY(nvpair_value_uint64(elem,
42145094Slling 				    &intval) == 0);
42155094Slling 				ASSERT(intval <= SPA_VERSION);
42165094Slling 				ASSERT(intval >= spa_version(spa));
42175094Slling 				spa->spa_uberblock.ub_version = intval;
42185094Slling 				vdev_config_dirty(spa->spa_root_vdev);
42195094Slling 			}
42205094Slling 			break;
42215094Slling 
42225094Slling 		case ZPOOL_PROP_ALTROOT:
42235094Slling 			/*
42245094Slling 			 * 'altroot' is a non-persistent property. It should
42255094Slling 			 * have been set temporarily at creation or import time.
42265094Slling 			 */
42275094Slling 			ASSERT(spa->spa_root != NULL);
42285094Slling 			break;
42295094Slling 
42305363Seschrock 		case ZPOOL_PROP_CACHEFILE:
42315094Slling 			/*
42328525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
42335094Slling 			 */
42344543Smarks 			break;
42355094Slling 		default:
42365094Slling 			/*
42375094Slling 			 * Set pool property values in the poolprops mos object.
42385094Slling 			 */
42395094Slling 			if (spa->spa_pool_props_object == 0) {
42405094Slling 				objset_t *mos = spa->spa_meta_objset;
42415094Slling 
42425094Slling 				VERIFY((spa->spa_pool_props_object =
42435094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
42445094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
42455094Slling 
42465094Slling 				VERIFY(zap_update(mos,
42475094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
42485094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
42495094Slling 				    == 0);
42505094Slling 			}
42515094Slling 
42525094Slling 			/* normalize the property name */
42535094Slling 			propname = zpool_prop_to_name(prop);
42545094Slling 			proptype = zpool_prop_get_type(prop);
42555094Slling 
42565094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
42575094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
42585094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
42595094Slling 				VERIFY(zap_update(mos,
42605094Slling 				    spa->spa_pool_props_object, propname,
42615094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
42625094Slling 
42635094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
42645094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
42655094Slling 
42665094Slling 				if (proptype == PROP_TYPE_INDEX) {
42675094Slling 					const char *unused;
42685094Slling 					VERIFY(zpool_prop_index_to_string(
42695094Slling 					    prop, intval, &unused) == 0);
42705094Slling 				}
42715094Slling 				VERIFY(zap_update(mos,
42725094Slling 				    spa->spa_pool_props_object, propname,
42735094Slling 				    8, 1, &intval, tx) == 0);
42745094Slling 			} else {
42755094Slling 				ASSERT(0); /* not allowed */
42765094Slling 			}
42775094Slling 
42785329Sgw25295 			switch (prop) {
42795329Sgw25295 			case ZPOOL_PROP_DELEGATION:
42805094Slling 				spa->spa_delegation = intval;
42815329Sgw25295 				break;
42825329Sgw25295 			case ZPOOL_PROP_BOOTFS:
42835094Slling 				spa->spa_bootfs = intval;
42845329Sgw25295 				break;
42855329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
42865329Sgw25295 				spa->spa_failmode = intval;
42875329Sgw25295 				break;
42889816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
42899816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
42909816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
42919816SGeorge.Wilson@Sun.COM 				break;
42925329Sgw25295 			default:
42935329Sgw25295 				break;
42945329Sgw25295 			}
42953912Slling 		}
42965094Slling 
42975094Slling 		/* log internal history if this is not a zpool create */
42985094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
42995094Slling 		    tx->tx_txg != TXG_INITIAL) {
43005094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
43015094Slling 			    spa, tx, cr, "%s %lld %s",
43027754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
43035094Slling 		}
43043912Slling 	}
43057754SJeff.Bonwick@Sun.COM 
43067754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
43073912Slling }
43083912Slling 
4309789Sahrens /*
4310789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
4311789Sahrens  * part of the process, so we iterate until it converges.
4312789Sahrens  */
4313789Sahrens void
4314789Sahrens spa_sync(spa_t *spa, uint64_t txg)
4315789Sahrens {
4316789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
4317789Sahrens 	objset_t *mos = spa->spa_meta_objset;
4318789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
43191635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4320789Sahrens 	vdev_t *vd;
4321789Sahrens 	dmu_tx_t *tx;
4322789Sahrens 	int dirty_vdevs;
43237754SJeff.Bonwick@Sun.COM 	int error;
4324789Sahrens 
4325789Sahrens 	/*
4326789Sahrens 	 * Lock out configuration changes.
4327789Sahrens 	 */
43287754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4329789Sahrens 
4330789Sahrens 	spa->spa_syncing_txg = txg;
4331789Sahrens 	spa->spa_sync_pass = 0;
4332789Sahrens 
43337754SJeff.Bonwick@Sun.COM 	/*
43347754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
43357754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
43367754SJeff.Bonwick@Sun.COM 	 */
43377754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
43388241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
43398241SJeff.Bonwick@Sun.COM 		/*
43408241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
43418241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
43428241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
43438241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
43448241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
43458241SJeff.Bonwick@Sun.COM 		 */
43468241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
43478241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
43488241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
43498241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
43508241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
43518241SJeff.Bonwick@Sun.COM 		}
43528241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
43538241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
43547754SJeff.Bonwick@Sun.COM 	}
43557754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
43567754SJeff.Bonwick@Sun.COM 
43571544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4358789Sahrens 
43592082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
43602082Seschrock 
43612082Seschrock 	/*
43624577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
43632082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
43642082Seschrock 	 */
43654577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
43664577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
43672082Seschrock 		int i;
43682082Seschrock 
43692082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
43702082Seschrock 			vd = rvd->vdev_child[i];
43712082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
43722082Seschrock 				break;
43732082Seschrock 		}
43742082Seschrock 		if (i == rvd->vdev_children) {
43752082Seschrock 			spa->spa_deflate = TRUE;
43762082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
43772082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
43782082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
43792082Seschrock 		}
43802082Seschrock 	}
43812082Seschrock 
43827046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
43837046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
43847046Sahrens 		dsl_pool_create_origin(dp, tx);
43857046Sahrens 
43867046Sahrens 		/* Keeping the origin open increases spa_minref */
43877046Sahrens 		spa->spa_minref += 3;
43887046Sahrens 	}
43897046Sahrens 
43907046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
43917046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
43927046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
43937046Sahrens 	}
43947046Sahrens 
4395789Sahrens 	/*
4396789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4397789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4398789Sahrens 	 * don't generate work on an otherwise idle system.
4399789Sahrens 	 */
4400789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
44012329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
44022329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4403789Sahrens 		spa_sync_deferred_frees(spa, txg);
4404789Sahrens 
4405789Sahrens 	/*
4406789Sahrens 	 * Iterate to convergence.
4407789Sahrens 	 */
4408789Sahrens 	do {
4409789Sahrens 		spa->spa_sync_pass++;
4410789Sahrens 
4411789Sahrens 		spa_sync_config_object(spa, tx);
44125450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
44135450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
44145450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
44155450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
44161544Seschrock 		spa_errlog_sync(spa, txg);
4417789Sahrens 		dsl_pool_sync(dp, txg);
4418789Sahrens 
4419789Sahrens 		dirty_vdevs = 0;
4420789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4421789Sahrens 			vdev_sync(vd, txg);
4422789Sahrens 			dirty_vdevs++;
4423789Sahrens 		}
4424789Sahrens 
4425789Sahrens 		bplist_sync(bpl, tx);
4426789Sahrens 	} while (dirty_vdevs);
4427789Sahrens 
4428789Sahrens 	bplist_close(bpl);
4429789Sahrens 
4430789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4431789Sahrens 
4432789Sahrens 	/*
4433789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4434789Sahrens 	 * to commit the transaction group.
44351635Sbonwick 	 *
44365688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
44375688Sbonwick 	 * random top-level vdevs that are known to be visible in the
44387754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
44397754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4440789Sahrens 	 */
44417754SJeff.Bonwick@Sun.COM 	for (;;) {
44427754SJeff.Bonwick@Sun.COM 		/*
44437754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
44447754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
44457754SJeff.Bonwick@Sun.COM 		 */
44467754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44477754SJeff.Bonwick@Sun.COM 
44487754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
44497754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
44507754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
44517754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
44527754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
44539816SGeorge.Wilson@Sun.COM 
44549816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
44557754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
44567754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
44577754SJeff.Bonwick@Sun.COM 					continue;
44587754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
44597754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
44607754SJeff.Bonwick@Sun.COM 					break;
44617754SJeff.Bonwick@Sun.COM 			}
44629725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
44639725SEric.Schrock@Sun.COM 			if (error != 0)
44649725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
44659725SEric.Schrock@Sun.COM 				    B_TRUE);
44667754SJeff.Bonwick@Sun.COM 		} else {
44677754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
44689725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
44699725SEric.Schrock@Sun.COM 			if (error != 0)
44709725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
44719725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
44721635Sbonwick 		}
44737754SJeff.Bonwick@Sun.COM 
44747754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
44757754SJeff.Bonwick@Sun.COM 
44767754SJeff.Bonwick@Sun.COM 		if (error == 0)
44777754SJeff.Bonwick@Sun.COM 			break;
44787754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
44797754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
44801635Sbonwick 	}
44812082Seschrock 	dmu_tx_commit(tx);
44822082Seschrock 
44831635Sbonwick 	/*
44841635Sbonwick 	 * Clear the dirty config list.
44851635Sbonwick 	 */
44867754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
44871635Sbonwick 		vdev_config_clean(vd);
44881635Sbonwick 
44891635Sbonwick 	/*
44901635Sbonwick 	 * Now that the new config has synced transactionally,
44911635Sbonwick 	 * let it become visible to the config cache.
44921635Sbonwick 	 */
44931635Sbonwick 	if (spa->spa_config_syncing != NULL) {
44941635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
44951635Sbonwick 		spa->spa_config_txg = txg;
44961635Sbonwick 		spa->spa_config_syncing = NULL;
44971635Sbonwick 	}
4498789Sahrens 
4499789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4500789Sahrens 
4501789Sahrens 	/*
4502789Sahrens 	 * Clean up the ZIL records for the synced txg.
4503789Sahrens 	 */
4504789Sahrens 	dsl_pool_zil_clean(dp);
4505789Sahrens 
4506789Sahrens 	/*
4507789Sahrens 	 * Update usable space statistics.
4508789Sahrens 	 */
4509789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4510789Sahrens 		vdev_sync_done(vd, txg);
4511789Sahrens 
4512789Sahrens 	/*
4513789Sahrens 	 * It had better be the case that we didn't dirty anything
45142082Seschrock 	 * since vdev_config_sync().
4515789Sahrens 	 */
4516789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4517789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4518789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4519789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4520789Sahrens 
45217754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
45221544Seschrock 
45231544Seschrock 	/*
45241544Seschrock 	 * If any async tasks have been requested, kick them off.
45251544Seschrock 	 */
45261544Seschrock 	spa_async_dispatch(spa);
4527789Sahrens }
4528789Sahrens 
4529789Sahrens /*
4530789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4531789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4532789Sahrens  * sync.
4533789Sahrens  */
4534789Sahrens void
4535789Sahrens spa_sync_allpools(void)
4536789Sahrens {
4537789Sahrens 	spa_t *spa = NULL;
4538789Sahrens 	mutex_enter(&spa_namespace_lock);
4539789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
45407754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4541789Sahrens 			continue;
4542789Sahrens 		spa_open_ref(spa, FTAG);
4543789Sahrens 		mutex_exit(&spa_namespace_lock);
4544789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4545789Sahrens 		mutex_enter(&spa_namespace_lock);
4546789Sahrens 		spa_close(spa, FTAG);
4547789Sahrens 	}
4548789Sahrens 	mutex_exit(&spa_namespace_lock);
4549789Sahrens }
4550789Sahrens 
4551789Sahrens /*
4552789Sahrens  * ==========================================================================
4553789Sahrens  * Miscellaneous routines
4554789Sahrens  * ==========================================================================
4555789Sahrens  */
4556789Sahrens 
4557789Sahrens /*
4558789Sahrens  * Remove all pools in the system.
4559789Sahrens  */
4560789Sahrens void
4561789Sahrens spa_evict_all(void)
4562789Sahrens {
4563789Sahrens 	spa_t *spa;
4564789Sahrens 
4565789Sahrens 	/*
4566789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4567789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4568789Sahrens 	 */
4569789Sahrens 	mutex_enter(&spa_namespace_lock);
4570789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4571789Sahrens 		/*
45721544Seschrock 		 * Stop async tasks.  The async thread may need to detach
45731544Seschrock 		 * a device that's been replaced, which requires grabbing
45741544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4575789Sahrens 		 */
4576789Sahrens 		spa_open_ref(spa, FTAG);
4577789Sahrens 		mutex_exit(&spa_namespace_lock);
45781544Seschrock 		spa_async_suspend(spa);
45794808Sek110237 		mutex_enter(&spa_namespace_lock);
4580789Sahrens 		spa_close(spa, FTAG);
4581789Sahrens 
4582789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4583789Sahrens 			spa_unload(spa);
4584789Sahrens 			spa_deactivate(spa);
4585789Sahrens 		}
4586789Sahrens 		spa_remove(spa);
4587789Sahrens 	}
4588789Sahrens 	mutex_exit(&spa_namespace_lock);
4589789Sahrens }
45901544Seschrock 
45911544Seschrock vdev_t *
45929425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
45931544Seschrock {
45946643Seschrock 	vdev_t *vd;
45956643Seschrock 	int i;
45966643Seschrock 
45976643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
45986643Seschrock 		return (vd);
45996643Seschrock 
46009425SEric.Schrock@Sun.COM 	if (aux) {
46016643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
46026643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
46036643Seschrock 			if (vd->vdev_guid == guid)
46046643Seschrock 				return (vd);
46056643Seschrock 		}
46069425SEric.Schrock@Sun.COM 
46079425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
46089425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
46099425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
46109425SEric.Schrock@Sun.COM 				return (vd);
46119425SEric.Schrock@Sun.COM 		}
46126643Seschrock 	}
46136643Seschrock 
46146643Seschrock 	return (NULL);
46151544Seschrock }
46161760Seschrock 
46171760Seschrock void
46185094Slling spa_upgrade(spa_t *spa, uint64_t version)
46191760Seschrock {
46207754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
46211760Seschrock 
46221760Seschrock 	/*
46231760Seschrock 	 * This should only be called for a non-faulted pool, and since a
46241760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
46251760Seschrock 	 * possible.
46261760Seschrock 	 */
46274577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
46285094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
46295094Slling 
46305094Slling 	spa->spa_uberblock.ub_version = version;
46311760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
46321760Seschrock 
46337754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
46342082Seschrock 
46352082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
46361760Seschrock }
46372082Seschrock 
46382082Seschrock boolean_t
46392082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
46402082Seschrock {
46412082Seschrock 	int i;
46423377Seschrock 	uint64_t spareguid;
46435450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
46445450Sbrendan 
46455450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
46465450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
46472082Seschrock 			return (B_TRUE);
46482082Seschrock 
46495450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
46505450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
46515450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
46523377Seschrock 			return (B_TRUE);
46533377Seschrock 	}
46543377Seschrock 
46552082Seschrock 	return (B_FALSE);
46562082Seschrock }
46573912Slling 
46584451Seschrock /*
46597214Slling  * Check if a pool has an active shared spare device.
46607214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
46617214Slling  */
46627214Slling static boolean_t
46637214Slling spa_has_active_shared_spare(spa_t *spa)
46647214Slling {
46657214Slling 	int i, refcnt;
46667214Slling 	uint64_t pool;
46677214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
46687214Slling 
46697214Slling 	for (i = 0; i < sav->sav_count; i++) {
46707214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
46717214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
46727214Slling 		    refcnt > 2)
46737214Slling 			return (B_TRUE);
46747214Slling 	}
46757214Slling 
46767214Slling 	return (B_FALSE);
46777214Slling }
46787214Slling 
46797214Slling /*
46804451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
46814451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
46824451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
46834451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
46844451Seschrock  * or zdb as real changes.
46854451Seschrock  */
46864451Seschrock void
46874451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
46884451Seschrock {
46894451Seschrock #ifdef _KERNEL
46904451Seschrock 	sysevent_t		*ev;
46914451Seschrock 	sysevent_attr_list_t	*attr = NULL;
46924451Seschrock 	sysevent_value_t	value;
46934451Seschrock 	sysevent_id_t		eid;
46944451Seschrock 
46954451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
46964451Seschrock 	    SE_SLEEP);
46974451Seschrock 
46984451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
46994451Seschrock 	value.value.sv_string = spa_name(spa);
47004451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
47014451Seschrock 		goto done;
47024451Seschrock 
47034451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
47044451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
47054451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
47064451Seschrock 		goto done;
47074451Seschrock 
47084451Seschrock 	if (vd) {
47094451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
47104451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
47114451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
47124451Seschrock 		    SE_SLEEP) != 0)
47134451Seschrock 			goto done;
47144451Seschrock 
47154451Seschrock 		if (vd->vdev_path) {
47164451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
47174451Seschrock 			value.value.sv_string = vd->vdev_path;
47184451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
47194451Seschrock 			    &value, SE_SLEEP) != 0)
47204451Seschrock 				goto done;
47214451Seschrock 		}
47224451Seschrock 	}
47234451Seschrock 
47245756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
47255756Seschrock 		goto done;
47265756Seschrock 	attr = NULL;
47275756Seschrock 
47284451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
47294451Seschrock 
47304451Seschrock done:
47314451Seschrock 	if (attr)
47324451Seschrock 		sysevent_free_attr(attr);
47334451Seschrock 	sysevent_free(ev);
47344451Seschrock #endif
47354451Seschrock }
4736