xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 9234)
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>
45789Sahrens #include <sys/uberblock_impl.h>
46789Sahrens #include <sys/txg.h>
47789Sahrens #include <sys/avl.h>
48789Sahrens #include <sys/dmu_traverse.h>
493912Slling #include <sys/dmu_objset.h>
50789Sahrens #include <sys/unique.h>
51789Sahrens #include <sys/dsl_pool.h>
523912Slling #include <sys/dsl_dataset.h>
53789Sahrens #include <sys/dsl_dir.h>
54789Sahrens #include <sys/dsl_prop.h>
553912Slling #include <sys/dsl_synctask.h>
56789Sahrens #include <sys/fs/zfs.h>
575450Sbrendan #include <sys/arc.h>
58789Sahrens #include <sys/callb.h>
593975Sek110237 #include <sys/systeminfo.h>
603975Sek110237 #include <sys/sunddi.h>
616423Sgw25295 #include <sys/spa_boot.h>
62789Sahrens 
638662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
648662SJordan.Vaughan@Sun.com #include <sys/zone.h>
658662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
668662SJordan.Vaughan@Sun.com 
675094Slling #include "zfs_prop.h"
685913Sperrin #include "zfs_comutil.h"
695094Slling 
707754SJeff.Bonwick@Sun.COM int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
717754SJeff.Bonwick@Sun.COM 	/*	ISSUE	INTR					*/
727754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_NULL	*/
739230SGeorge.Wilson@Sun.COM 	{	8,	8	},	/* ZIO_TYPE_READ	*/
749230SGeorge.Wilson@Sun.COM 	{	8,	8	},	/* ZIO_TYPE_WRITE	*/
757754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_FREE	*/
767754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_CLAIM	*/
777754SJeff.Bonwick@Sun.COM 	{	1,	1	},	/* ZIO_TYPE_IOCTL	*/
787754SJeff.Bonwick@Sun.COM };
792986Sek110237 
805094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
817214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
825094Slling 
835094Slling /*
845094Slling  * ==========================================================================
855094Slling  * SPA properties routines
865094Slling  * ==========================================================================
875094Slling  */
885094Slling 
895094Slling /*
905094Slling  * Add a (source=src, propname=propval) list to an nvlist.
915094Slling  */
925949Slling static void
935094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
945094Slling     uint64_t intval, zprop_source_t src)
955094Slling {
965094Slling 	const char *propname = zpool_prop_to_name(prop);
975094Slling 	nvlist_t *propval;
985949Slling 
995949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1005949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1015949Slling 
1025949Slling 	if (strval != NULL)
1035949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1045949Slling 	else
1055949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1065949Slling 
1075949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1085094Slling 	nvlist_free(propval);
1095094Slling }
1105094Slling 
1115094Slling /*
1125094Slling  * Get property values from the spa configuration.
1135094Slling  */
1145949Slling static void
1155094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1165094Slling {
1178525SEric.Schrock@Sun.COM 	uint64_t size;
1188525SEric.Schrock@Sun.COM 	uint64_t used;
1195094Slling 	uint64_t cap, version;
1205094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1216643Seschrock 	spa_config_dirent_t *dp;
1225094Slling 
1237754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1247754SJeff.Bonwick@Sun.COM 
1258525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
1268525SEric.Schrock@Sun.COM 		size = spa_get_space(spa);
1278525SEric.Schrock@Sun.COM 		used = spa_get_alloc(spa);
1288525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1298525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1308525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1318525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
1328525SEric.Schrock@Sun.COM 		    size - used, src);
1338525SEric.Schrock@Sun.COM 
1348525SEric.Schrock@Sun.COM 		cap = (size == 0) ? 0 : (used * 100 / size);
1358525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1368525SEric.Schrock@Sun.COM 
1378525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1388525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1398525SEric.Schrock@Sun.COM 
1408525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1418525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1428525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1438525SEric.Schrock@Sun.COM 		else
1448525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1458525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1468525SEric.Schrock@Sun.COM 	}
1475949Slling 
1485949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1495949Slling 
1505949Slling 	if (spa->spa_root != NULL)
1515949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1525949Slling 		    0, ZPROP_SRC_LOCAL);
1535094Slling 
1546643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1556643Seschrock 		if (dp->scd_path == NULL) {
1565949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1576643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1586643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1595949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1606643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1615363Seschrock 		}
1625363Seschrock 	}
1635094Slling }
1645094Slling 
1655094Slling /*
1665094Slling  * Get zpool property values.
1675094Slling  */
1685094Slling int
1695094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1705094Slling {
1715094Slling 	zap_cursor_t zc;
1725094Slling 	zap_attribute_t za;
1735094Slling 	objset_t *mos = spa->spa_meta_objset;
1745094Slling 	int err;
1755094Slling 
1765949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1775094Slling 
1787754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
1797754SJeff.Bonwick@Sun.COM 
1805094Slling 	/*
1815094Slling 	 * Get properties from the spa config.
1825094Slling 	 */
1835949Slling 	spa_prop_get_config(spa, nvp);
1845094Slling 
1855094Slling 	/* If no pool property object, no more prop to get. */
1865094Slling 	if (spa->spa_pool_props_object == 0) {
1875094Slling 		mutex_exit(&spa->spa_props_lock);
1885094Slling 		return (0);
1895094Slling 	}
1905094Slling 
1915094Slling 	/*
1925094Slling 	 * Get properties from the MOS pool property object.
1935094Slling 	 */
1945094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1955094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1965094Slling 	    zap_cursor_advance(&zc)) {
1975094Slling 		uint64_t intval = 0;
1985094Slling 		char *strval = NULL;
1995094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
2005094Slling 		zpool_prop_t prop;
2015094Slling 
2025094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
2035094Slling 			continue;
2045094Slling 
2055094Slling 		switch (za.za_integer_length) {
2065094Slling 		case 8:
2075094Slling 			/* integer property */
2085094Slling 			if (za.za_first_integer !=
2095094Slling 			    zpool_prop_default_numeric(prop))
2105094Slling 				src = ZPROP_SRC_LOCAL;
2115094Slling 
2125094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2135094Slling 				dsl_pool_t *dp;
2145094Slling 				dsl_dataset_t *ds = NULL;
2155094Slling 
2165094Slling 				dp = spa_get_dsl(spa);
2175094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2186689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2196689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2205094Slling 					rw_exit(&dp->dp_config_rwlock);
2215094Slling 					break;
2225094Slling 				}
2235094Slling 
2245094Slling 				strval = kmem_alloc(
2255094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2265094Slling 				    KM_SLEEP);
2275094Slling 				dsl_dataset_name(ds, strval);
2286689Smaybee 				dsl_dataset_rele(ds, FTAG);
2295094Slling 				rw_exit(&dp->dp_config_rwlock);
2305094Slling 			} else {
2315094Slling 				strval = NULL;
2325094Slling 				intval = za.za_first_integer;
2335094Slling 			}
2345094Slling 
2355949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2365094Slling 
2375094Slling 			if (strval != NULL)
2385094Slling 				kmem_free(strval,
2395094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2405094Slling 
2415094Slling 			break;
2425094Slling 
2435094Slling 		case 1:
2445094Slling 			/* string property */
2455094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2465094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2475094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2485094Slling 			if (err) {
2495094Slling 				kmem_free(strval, za.za_num_integers);
2505094Slling 				break;
2515094Slling 			}
2525949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2535094Slling 			kmem_free(strval, za.za_num_integers);
2545094Slling 			break;
2555094Slling 
2565094Slling 		default:
2575094Slling 			break;
2585094Slling 		}
2595094Slling 	}
2605094Slling 	zap_cursor_fini(&zc);
2615094Slling 	mutex_exit(&spa->spa_props_lock);
2625094Slling out:
2635094Slling 	if (err && err != ENOENT) {
2645094Slling 		nvlist_free(*nvp);
2655949Slling 		*nvp = NULL;
2665094Slling 		return (err);
2675094Slling 	}
2685094Slling 
2695094Slling 	return (0);
2705094Slling }
2715094Slling 
2725094Slling /*
2735094Slling  * Validate the given pool properties nvlist and modify the list
2745094Slling  * for the property values to be set.
2755094Slling  */
2765094Slling static int
2775094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2785094Slling {
2795094Slling 	nvpair_t *elem;
2805094Slling 	int error = 0, reset_bootfs = 0;
2815094Slling 	uint64_t objnum;
2825094Slling 
2835094Slling 	elem = NULL;
2845094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2855094Slling 		zpool_prop_t prop;
2865094Slling 		char *propname, *strval;
2875094Slling 		uint64_t intval;
2885094Slling 		objset_t *os;
2895363Seschrock 		char *slash;
2905094Slling 
2915094Slling 		propname = nvpair_name(elem);
2925094Slling 
2935094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2945094Slling 			return (EINVAL);
2955094Slling 
2965094Slling 		switch (prop) {
2975094Slling 		case ZPOOL_PROP_VERSION:
2985094Slling 			error = nvpair_value_uint64(elem, &intval);
2995094Slling 			if (!error &&
3005094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
3015094Slling 				error = EINVAL;
3025094Slling 			break;
3035094Slling 
3045094Slling 		case ZPOOL_PROP_DELEGATION:
3055094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3067538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3075094Slling 			error = nvpair_value_uint64(elem, &intval);
3085094Slling 			if (!error && intval > 1)
3095094Slling 				error = EINVAL;
3105094Slling 			break;
3115094Slling 
3125094Slling 		case ZPOOL_PROP_BOOTFS:
3135094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3145094Slling 				error = ENOTSUP;
3155094Slling 				break;
3165094Slling 			}
3175094Slling 
3185094Slling 			/*
3197042Sgw25295 			 * Make sure the vdev config is bootable
3205094Slling 			 */
3217042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3225094Slling 				error = ENOTSUP;
3235094Slling 				break;
3245094Slling 			}
3255094Slling 
3265094Slling 			reset_bootfs = 1;
3275094Slling 
3285094Slling 			error = nvpair_value_string(elem, &strval);
3295094Slling 
3305094Slling 			if (!error) {
3317042Sgw25295 				uint64_t compress;
3327042Sgw25295 
3335094Slling 				if (strval == NULL || strval[0] == '\0') {
3345094Slling 					objnum = zpool_prop_default_numeric(
3355094Slling 					    ZPOOL_PROP_BOOTFS);
3365094Slling 					break;
3375094Slling 				}
3385094Slling 
3395094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
3406689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3415094Slling 					break;
3427042Sgw25295 
3437042Sgw25295 				/* We don't support gzip bootable datasets */
3447042Sgw25295 				if ((error = dsl_prop_get_integer(strval,
3457042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3467042Sgw25295 				    &compress, NULL)) == 0 &&
3477042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3487042Sgw25295 					error = ENOTSUP;
3497042Sgw25295 				} else {
3507042Sgw25295 					objnum = dmu_objset_id(os);
3517042Sgw25295 				}
3525094Slling 				dmu_objset_close(os);
3535094Slling 			}
3545094Slling 			break;
3557754SJeff.Bonwick@Sun.COM 
3565329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3575329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3585329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3595329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3605329Sgw25295 				error = EINVAL;
3615329Sgw25295 
3625329Sgw25295 			/*
3635329Sgw25295 			 * This is a special case which only occurs when
3645329Sgw25295 			 * the pool has completely failed. This allows
3655329Sgw25295 			 * the user to change the in-core failmode property
3665329Sgw25295 			 * without syncing it out to disk (I/Os might
3675329Sgw25295 			 * currently be blocked). We do this by returning
3685329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3695329Sgw25295 			 * into thinking we encountered a property validation
3705329Sgw25295 			 * error.
3715329Sgw25295 			 */
3727754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
3735329Sgw25295 				spa->spa_failmode = intval;
3745329Sgw25295 				error = EIO;
3755329Sgw25295 			}
3765329Sgw25295 			break;
3775363Seschrock 
3785363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3795363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3805363Seschrock 				break;
3815363Seschrock 
3825363Seschrock 			if (strval[0] == '\0')
3835363Seschrock 				break;
3845363Seschrock 
3855363Seschrock 			if (strcmp(strval, "none") == 0)
3865363Seschrock 				break;
3875363Seschrock 
3885363Seschrock 			if (strval[0] != '/') {
3895363Seschrock 				error = EINVAL;
3905363Seschrock 				break;
3915363Seschrock 			}
3925363Seschrock 
3935363Seschrock 			slash = strrchr(strval, '/');
3945363Seschrock 			ASSERT(slash != NULL);
3955363Seschrock 
3965363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3975363Seschrock 			    strcmp(slash, "/..") == 0)
3985363Seschrock 				error = EINVAL;
3995363Seschrock 			break;
4005094Slling 		}
4015094Slling 
4025094Slling 		if (error)
4035094Slling 			break;
4045094Slling 	}
4055094Slling 
4065094Slling 	if (!error && reset_bootfs) {
4075094Slling 		error = nvlist_remove(props,
4085094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4095094Slling 
4105094Slling 		if (!error) {
4115094Slling 			error = nvlist_add_uint64(props,
4125094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4135094Slling 		}
4145094Slling 	}
4155094Slling 
4165094Slling 	return (error);
4175094Slling }
4185094Slling 
4198525SEric.Schrock@Sun.COM void
4208525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
4218525SEric.Schrock@Sun.COM {
4228525SEric.Schrock@Sun.COM 	char *cachefile;
4238525SEric.Schrock@Sun.COM 	spa_config_dirent_t *dp;
4248525SEric.Schrock@Sun.COM 
4258525SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
4268525SEric.Schrock@Sun.COM 	    &cachefile) != 0)
4278525SEric.Schrock@Sun.COM 		return;
4288525SEric.Schrock@Sun.COM 
4298525SEric.Schrock@Sun.COM 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
4308525SEric.Schrock@Sun.COM 	    KM_SLEEP);
4318525SEric.Schrock@Sun.COM 
4328525SEric.Schrock@Sun.COM 	if (cachefile[0] == '\0')
4338525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(spa_config_path);
4348525SEric.Schrock@Sun.COM 	else if (strcmp(cachefile, "none") == 0)
4358525SEric.Schrock@Sun.COM 		dp->scd_path = NULL;
4368525SEric.Schrock@Sun.COM 	else
4378525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(cachefile);
4388525SEric.Schrock@Sun.COM 
4398525SEric.Schrock@Sun.COM 	list_insert_head(&spa->spa_config_list, dp);
4408525SEric.Schrock@Sun.COM 	if (need_sync)
4418525SEric.Schrock@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4428525SEric.Schrock@Sun.COM }
4438525SEric.Schrock@Sun.COM 
4445094Slling int
4455094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4465094Slling {
4475094Slling 	int error;
4488525SEric.Schrock@Sun.COM 	nvpair_t *elem;
4498525SEric.Schrock@Sun.COM 	boolean_t need_sync = B_FALSE;
4508525SEric.Schrock@Sun.COM 	zpool_prop_t prop;
4515094Slling 
4525094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4535094Slling 		return (error);
4545094Slling 
4558525SEric.Schrock@Sun.COM 	elem = NULL;
4568525SEric.Schrock@Sun.COM 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
4578525SEric.Schrock@Sun.COM 		if ((prop = zpool_name_to_prop(
4588525SEric.Schrock@Sun.COM 		    nvpair_name(elem))) == ZPROP_INVAL)
4598525SEric.Schrock@Sun.COM 			return (EINVAL);
4608525SEric.Schrock@Sun.COM 
4618525SEric.Schrock@Sun.COM 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
4628525SEric.Schrock@Sun.COM 			continue;
4638525SEric.Schrock@Sun.COM 
4648525SEric.Schrock@Sun.COM 		need_sync = B_TRUE;
4658525SEric.Schrock@Sun.COM 		break;
4668525SEric.Schrock@Sun.COM 	}
4678525SEric.Schrock@Sun.COM 
4688525SEric.Schrock@Sun.COM 	if (need_sync)
4698525SEric.Schrock@Sun.COM 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4708525SEric.Schrock@Sun.COM 		    spa, nvp, 3));
4718525SEric.Schrock@Sun.COM 	else
4728525SEric.Schrock@Sun.COM 		return (0);
4735094Slling }
4745094Slling 
4755094Slling /*
4765094Slling  * If the bootfs property value is dsobj, clear it.
4775094Slling  */
4785094Slling void
4795094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4805094Slling {
4815094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4825094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4835094Slling 		    spa->spa_pool_props_object,
4845094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4855094Slling 		spa->spa_bootfs = 0;
4865094Slling 	}
4875094Slling }
4885094Slling 
489789Sahrens /*
490789Sahrens  * ==========================================================================
491789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
492789Sahrens  * ==========================================================================
493789Sahrens  */
494789Sahrens 
4951544Seschrock static int
4961544Seschrock spa_error_entry_compare(const void *a, const void *b)
4971544Seschrock {
4981544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4991544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
5001544Seschrock 	int ret;
5011544Seschrock 
5021544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
5031544Seschrock 	    sizeof (zbookmark_t));
5041544Seschrock 
5051544Seschrock 	if (ret < 0)
5061544Seschrock 		return (-1);
5071544Seschrock 	else if (ret > 0)
5081544Seschrock 		return (1);
5091544Seschrock 	else
5101544Seschrock 		return (0);
5111544Seschrock }
5121544Seschrock 
5131544Seschrock /*
5141544Seschrock  * Utility function which retrieves copies of the current logs and
5151544Seschrock  * re-initializes them in the process.
5161544Seschrock  */
5171544Seschrock void
5181544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
5191544Seschrock {
5201544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
5211544Seschrock 
5221544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
5231544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
5241544Seschrock 
5251544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5261544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5271544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5281544Seschrock 	avl_create(&spa->spa_errlist_last,
5291544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5301544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5311544Seschrock }
5321544Seschrock 
533789Sahrens /*
534789Sahrens  * Activate an uninitialized pool.
535789Sahrens  */
536789Sahrens static void
5378241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode)
538789Sahrens {
539789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
540789Sahrens 
541789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
5428241SJeff.Bonwick@Sun.COM 	spa->spa_mode = mode;
543789Sahrens 
544789Sahrens 	spa->spa_normal_class = metaslab_class_create();
5454527Sperrin 	spa->spa_log_class = metaslab_class_create();
546789Sahrens 
5477754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
5487754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5497754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = taskq_create("spa_zio",
5507754SJeff.Bonwick@Sun.COM 			    zio_taskq_threads[t][q], maxclsyspri, 50,
5517754SJeff.Bonwick@Sun.COM 			    INT_MAX, TASKQ_PREPOPULATE);
5527754SJeff.Bonwick@Sun.COM 		}
553789Sahrens 	}
554789Sahrens 
5557754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
5567754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
5577754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
5587754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
559789Sahrens 
560789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
561789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
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));
569789Sahrens }
570789Sahrens 
571789Sahrens /*
572789Sahrens  * Opposite of spa_activate().
573789Sahrens  */
574789Sahrens static void
575789Sahrens spa_deactivate(spa_t *spa)
576789Sahrens {
577789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
578789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
579789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
580789Sahrens 
581789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
582789Sahrens 
583789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
584789Sahrens 
5857754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
5867754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
5877754SJeff.Bonwick@Sun.COM 
5887754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
5897754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5907754SJeff.Bonwick@Sun.COM 			taskq_destroy(spa->spa_zio_taskq[t][q]);
5917754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
5927754SJeff.Bonwick@Sun.COM 		}
593789Sahrens 	}
594789Sahrens 
595789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
596789Sahrens 	spa->spa_normal_class = NULL;
597789Sahrens 
5984527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5994527Sperrin 	spa->spa_log_class = NULL;
6004527Sperrin 
6011544Seschrock 	/*
6021544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
6031544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
6041544Seschrock 	 */
6051544Seschrock 	spa_errlog_drain(spa);
6061544Seschrock 
6071544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
6081544Seschrock 	avl_destroy(&spa->spa_errlist_last);
6091544Seschrock 
610789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
611789Sahrens }
612789Sahrens 
613789Sahrens /*
614789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
615789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
616789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
617789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
618789Sahrens  */
6192082Seschrock static int
6202082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
6212082Seschrock     uint_t id, int atype)
622789Sahrens {
623789Sahrens 	nvlist_t **child;
624789Sahrens 	uint_t c, children;
6252082Seschrock 	int error;
6262082Seschrock 
6272082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
6282082Seschrock 		return (error);
6292082Seschrock 
6302082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
6312082Seschrock 		return (0);
632789Sahrens 
6337754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
6347754SJeff.Bonwick@Sun.COM 	    &child, &children);
6357754SJeff.Bonwick@Sun.COM 
6367754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
6377754SJeff.Bonwick@Sun.COM 		return (0);
6387754SJeff.Bonwick@Sun.COM 
6397754SJeff.Bonwick@Sun.COM 	if (error) {
6402082Seschrock 		vdev_free(*vdp);
6412082Seschrock 		*vdp = NULL;
6422082Seschrock 		return (EINVAL);
643789Sahrens 	}
644789Sahrens 
645789Sahrens 	for (c = 0; c < children; c++) {
6462082Seschrock 		vdev_t *vd;
6472082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
6482082Seschrock 		    atype)) != 0) {
6492082Seschrock 			vdev_free(*vdp);
6502082Seschrock 			*vdp = NULL;
6512082Seschrock 			return (error);
652789Sahrens 		}
653789Sahrens 	}
654789Sahrens 
6552082Seschrock 	ASSERT(*vdp != NULL);
6562082Seschrock 
6572082Seschrock 	return (0);
658789Sahrens }
659789Sahrens 
660789Sahrens /*
661789Sahrens  * Opposite of spa_load().
662789Sahrens  */
663789Sahrens static void
664789Sahrens spa_unload(spa_t *spa)
665789Sahrens {
6662082Seschrock 	int i;
6672082Seschrock 
6687754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
6697754SJeff.Bonwick@Sun.COM 
670789Sahrens 	/*
6711544Seschrock 	 * Stop async tasks.
6721544Seschrock 	 */
6731544Seschrock 	spa_async_suspend(spa);
6741544Seschrock 
6751544Seschrock 	/*
676789Sahrens 	 * Stop syncing.
677789Sahrens 	 */
678789Sahrens 	if (spa->spa_sync_on) {
679789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
680789Sahrens 		spa->spa_sync_on = B_FALSE;
681789Sahrens 	}
682789Sahrens 
683789Sahrens 	/*
6847754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
685789Sahrens 	 */
686*9234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root != NULL) {
687*9234SGeorge.Wilson@Sun.COM 		(void) zio_wait(spa->spa_async_zio_root);
688*9234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = NULL;
689*9234SGeorge.Wilson@Sun.COM 	}
690789Sahrens 
691789Sahrens 	/*
692789Sahrens 	 * Close the dsl pool.
693789Sahrens 	 */
694789Sahrens 	if (spa->spa_dsl_pool) {
695789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
696789Sahrens 		spa->spa_dsl_pool = NULL;
697789Sahrens 	}
698789Sahrens 
6998241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7008241SJeff.Bonwick@Sun.COM 
7018241SJeff.Bonwick@Sun.COM 	/*
7028241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
7038241SJeff.Bonwick@Sun.COM 	 */
7048241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
7058241SJeff.Bonwick@Sun.COM 
706789Sahrens 	/*
707789Sahrens 	 * Close all vdevs.
708789Sahrens 	 */
7091585Sbonwick 	if (spa->spa_root_vdev)
710789Sahrens 		vdev_free(spa->spa_root_vdev);
7111585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
7121544Seschrock 
7135450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7145450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
7155450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
7165450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7175450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7185450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
7195450Sbrendan 	}
7205450Sbrendan 	if (spa->spa_spares.sav_config) {
7215450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
7225450Sbrendan 		spa->spa_spares.sav_config = NULL;
7232082Seschrock 	}
7247377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
7255450Sbrendan 
7265450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
7275450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
7285450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
7295450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
7305450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
7315450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
7325450Sbrendan 	}
7335450Sbrendan 	if (spa->spa_l2cache.sav_config) {
7345450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
7355450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
7362082Seschrock 	}
7377377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
7382082Seschrock 
7391544Seschrock 	spa->spa_async_suspended = 0;
7408241SJeff.Bonwick@Sun.COM 
7418241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
742789Sahrens }
743789Sahrens 
744789Sahrens /*
7452082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
7462082Seschrock  * this pool.  When this is called, we have some form of basic information in
7475450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
7485450Sbrendan  * then re-generate a more complete list including status information.
7492082Seschrock  */
7502082Seschrock static void
7512082Seschrock spa_load_spares(spa_t *spa)
7522082Seschrock {
7532082Seschrock 	nvlist_t **spares;
7542082Seschrock 	uint_t nspares;
7552082Seschrock 	int i;
7563377Seschrock 	vdev_t *vd, *tvd;
7572082Seschrock 
7587754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
7597754SJeff.Bonwick@Sun.COM 
7602082Seschrock 	/*
7612082Seschrock 	 * First, close and free any existing spare vdevs.
7622082Seschrock 	 */
7635450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7645450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
7653377Seschrock 
7663377Seschrock 		/* Undo the call to spa_activate() below */
7676643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7686643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
7693377Seschrock 			spa_spare_remove(tvd);
7703377Seschrock 		vdev_close(vd);
7713377Seschrock 		vdev_free(vd);
7722082Seschrock 	}
7733377Seschrock 
7745450Sbrendan 	if (spa->spa_spares.sav_vdevs)
7755450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7765450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7775450Sbrendan 
7785450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7792082Seschrock 		nspares = 0;
7802082Seschrock 	else
7815450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7822082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7832082Seschrock 
7845450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7855450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7862082Seschrock 
7872082Seschrock 	if (nspares == 0)
7882082Seschrock 		return;
7892082Seschrock 
7902082Seschrock 	/*
7912082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7923377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7933377Seschrock 	 * structures associated with it: one in the list of spares (used only
7943377Seschrock 	 * for basic validation purposes) and one in the active vdev
7953377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7963377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7973377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7982082Seschrock 	 */
7995450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
8005450Sbrendan 	    KM_SLEEP);
8015450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
8022082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
8032082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
8042082Seschrock 		ASSERT(vd != NULL);
8052082Seschrock 
8065450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
8072082Seschrock 
8086643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
8096643Seschrock 		    B_FALSE)) != NULL) {
8103377Seschrock 			if (!tvd->vdev_isspare)
8113377Seschrock 				spa_spare_add(tvd);
8123377Seschrock 
8133377Seschrock 			/*
8143377Seschrock 			 * We only mark the spare active if we were successfully
8153377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
8163377Seschrock 			 * with a bad active spare would result in strange
8173377Seschrock 			 * behavior, because multiple pool would think the spare
8183377Seschrock 			 * is actively in use.
8193377Seschrock 			 *
8203377Seschrock 			 * There is a vulnerability here to an equally bizarre
8213377Seschrock 			 * circumstance, where a dead active spare is later
8223377Seschrock 			 * brought back to life (onlined or otherwise).  Given
8233377Seschrock 			 * the rarity of this scenario, and the extra complexity
8243377Seschrock 			 * it adds, we ignore the possibility.
8253377Seschrock 			 */
8263377Seschrock 			if (!vdev_is_dead(tvd))
8273377Seschrock 				spa_spare_activate(tvd);
8283377Seschrock 		}
8293377Seschrock 
8307754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
8317754SJeff.Bonwick@Sun.COM 
8322082Seschrock 		if (vdev_open(vd) != 0)
8332082Seschrock 			continue;
8342082Seschrock 
8355450Sbrendan 		if (vdev_validate_aux(vd) == 0)
8365450Sbrendan 			spa_spare_add(vd);
8372082Seschrock 	}
8382082Seschrock 
8392082Seschrock 	/*
8402082Seschrock 	 * Recompute the stashed list of spares, with status information
8412082Seschrock 	 * this time.
8422082Seschrock 	 */
8435450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
8442082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
8452082Seschrock 
8465450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
8475450Sbrendan 	    KM_SLEEP);
8485450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
8495450Sbrendan 		spares[i] = vdev_config_generate(spa,
8505450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
8515450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
8525450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
8535450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
8542082Seschrock 		nvlist_free(spares[i]);
8555450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
8565450Sbrendan }
8575450Sbrendan 
8585450Sbrendan /*
8595450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
8605450Sbrendan  * this pool.  When this is called, we have some form of basic information in
8615450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
8625450Sbrendan  * then re-generate a more complete list including status information.
8635450Sbrendan  * Devices which are already active have their details maintained, and are
8645450Sbrendan  * not re-opened.
8655450Sbrendan  */
8665450Sbrendan static void
8675450Sbrendan spa_load_l2cache(spa_t *spa)
8685450Sbrendan {
8695450Sbrendan 	nvlist_t **l2cache;
8705450Sbrendan 	uint_t nl2cache;
8715450Sbrendan 	int i, j, oldnvdevs;
8726643Seschrock 	uint64_t guid, size;
8735450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
8745450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
8755450Sbrendan 
8767754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
8777754SJeff.Bonwick@Sun.COM 
8785450Sbrendan 	if (sav->sav_config != NULL) {
8795450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8805450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8815450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8825450Sbrendan 	} else {
8835450Sbrendan 		nl2cache = 0;
8845450Sbrendan 	}
8855450Sbrendan 
8865450Sbrendan 	oldvdevs = sav->sav_vdevs;
8875450Sbrendan 	oldnvdevs = sav->sav_count;
8885450Sbrendan 	sav->sav_vdevs = NULL;
8895450Sbrendan 	sav->sav_count = 0;
8905450Sbrendan 
8915450Sbrendan 	/*
8925450Sbrendan 	 * Process new nvlist of vdevs.
8935450Sbrendan 	 */
8945450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8955450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8965450Sbrendan 		    &guid) == 0);
8975450Sbrendan 
8985450Sbrendan 		newvdevs[i] = NULL;
8995450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
9005450Sbrendan 			vd = oldvdevs[j];
9015450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
9025450Sbrendan 				/*
9035450Sbrendan 				 * Retain previous vdev for add/remove ops.
9045450Sbrendan 				 */
9055450Sbrendan 				newvdevs[i] = vd;
9065450Sbrendan 				oldvdevs[j] = NULL;
9075450Sbrendan 				break;
9085450Sbrendan 			}
9095450Sbrendan 		}
9105450Sbrendan 
9115450Sbrendan 		if (newvdevs[i] == NULL) {
9125450Sbrendan 			/*
9135450Sbrendan 			 * Create new vdev
9145450Sbrendan 			 */
9155450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
9165450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
9175450Sbrendan 			ASSERT(vd != NULL);
9185450Sbrendan 			newvdevs[i] = vd;
9195450Sbrendan 
9205450Sbrendan 			/*
9215450Sbrendan 			 * Commit this vdev as an l2cache device,
9225450Sbrendan 			 * even if it fails to open.
9235450Sbrendan 			 */
9245450Sbrendan 			spa_l2cache_add(vd);
9255450Sbrendan 
9266643Seschrock 			vd->vdev_top = vd;
9276643Seschrock 			vd->vdev_aux = sav;
9286643Seschrock 
9296643Seschrock 			spa_l2cache_activate(vd);
9306643Seschrock 
9315450Sbrendan 			if (vdev_open(vd) != 0)
9325450Sbrendan 				continue;
9335450Sbrendan 
9345450Sbrendan 			(void) vdev_validate_aux(vd);
9355450Sbrendan 
9365450Sbrendan 			if (!vdev_is_dead(vd)) {
9375450Sbrendan 				size = vdev_get_rsize(vd);
9386643Seschrock 				l2arc_add_vdev(spa, vd,
9396643Seschrock 				    VDEV_LABEL_START_SIZE,
9406643Seschrock 				    size - VDEV_LABEL_START_SIZE);
9415450Sbrendan 			}
9425450Sbrendan 		}
9435450Sbrendan 	}
9445450Sbrendan 
9455450Sbrendan 	/*
9465450Sbrendan 	 * Purge vdevs that were dropped
9475450Sbrendan 	 */
9485450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
9495450Sbrendan 		uint64_t pool;
9505450Sbrendan 
9515450Sbrendan 		vd = oldvdevs[i];
9525450Sbrendan 		if (vd != NULL) {
9538241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
9548241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
9555450Sbrendan 				l2arc_remove_vdev(vd);
9565450Sbrendan 			(void) vdev_close(vd);
9575450Sbrendan 			spa_l2cache_remove(vd);
9585450Sbrendan 		}
9595450Sbrendan 	}
9605450Sbrendan 
9615450Sbrendan 	if (oldvdevs)
9625450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
9635450Sbrendan 
9645450Sbrendan 	if (sav->sav_config == NULL)
9655450Sbrendan 		goto out;
9665450Sbrendan 
9675450Sbrendan 	sav->sav_vdevs = newvdevs;
9685450Sbrendan 	sav->sav_count = (int)nl2cache;
9695450Sbrendan 
9705450Sbrendan 	/*
9715450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
9725450Sbrendan 	 * information this time.
9735450Sbrendan 	 */
9745450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
9755450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9765450Sbrendan 
9775450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9785450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9795450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9805450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9815450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9825450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9835450Sbrendan out:
9845450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9855450Sbrendan 		nvlist_free(l2cache[i]);
9865450Sbrendan 	if (sav->sav_count)
9875450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9882082Seschrock }
9892082Seschrock 
9902082Seschrock static int
9912082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9922082Seschrock {
9932082Seschrock 	dmu_buf_t *db;
9942082Seschrock 	char *packed = NULL;
9952082Seschrock 	size_t nvsize = 0;
9962082Seschrock 	int error;
9972082Seschrock 	*value = NULL;
9982082Seschrock 
9992082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
10002082Seschrock 	nvsize = *(uint64_t *)db->db_data;
10012082Seschrock 	dmu_buf_rele(db, FTAG);
10022082Seschrock 
10032082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
10042082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
10052082Seschrock 	if (error == 0)
10062082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
10072082Seschrock 	kmem_free(packed, nvsize);
10082082Seschrock 
10092082Seschrock 	return (error);
10102082Seschrock }
10112082Seschrock 
10122082Seschrock /*
10134451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
10144451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
10154451Seschrock  */
10164451Seschrock static void
10174451Seschrock spa_check_removed(vdev_t *vd)
10184451Seschrock {
10194451Seschrock 	int c;
10204451Seschrock 
10214451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
10224451Seschrock 		spa_check_removed(vd->vdev_child[c]);
10234451Seschrock 
10244451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
10254451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
10264451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
10274451Seschrock 	}
10284451Seschrock }
10294451Seschrock 
10304451Seschrock /*
10317294Sperrin  * Check for missing log devices
10327294Sperrin  */
10337294Sperrin int
10347294Sperrin spa_check_logs(spa_t *spa)
10357294Sperrin {
10367294Sperrin 	switch (spa->spa_log_state) {
10377294Sperrin 	case SPA_LOG_MISSING:
10387294Sperrin 		/* need to recheck in case slog has been restored */
10397294Sperrin 	case SPA_LOG_UNKNOWN:
10407294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
10417294Sperrin 		    DS_FIND_CHILDREN)) {
10427294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
10437294Sperrin 			return (1);
10447294Sperrin 		}
10457294Sperrin 		break;
10467294Sperrin 
10477294Sperrin 	case SPA_LOG_CLEAR:
10487294Sperrin 		(void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
10497294Sperrin 		    DS_FIND_CHILDREN);
10507294Sperrin 		break;
10517294Sperrin 	}
10527294Sperrin 	spa->spa_log_state = SPA_LOG_GOOD;
10537294Sperrin 	return (0);
10547294Sperrin }
10557294Sperrin 
10567294Sperrin /*
1057789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
10581544Seschrock  * source of configuration information.
1059789Sahrens  */
1060789Sahrens static int
10611544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1062789Sahrens {
1063789Sahrens 	int error = 0;
1064789Sahrens 	nvlist_t *nvroot = NULL;
1065789Sahrens 	vdev_t *rvd;
1066789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
10671635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1068789Sahrens 	uint64_t pool_guid;
10692082Seschrock 	uint64_t version;
10704451Seschrock 	uint64_t autoreplace = 0;
10718241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
10727294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
1073789Sahrens 
10748241SJeff.Bonwick@Sun.COM 	/*
10758241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
10768241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
10778241SJeff.Bonwick@Sun.COM 	 */
10788241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
10798241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
10808241SJeff.Bonwick@Sun.COM 
10817754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
10827754SJeff.Bonwick@Sun.COM 
10831544Seschrock 	spa->spa_load_state = state;
10841635Sbonwick 
1085789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
10861733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
10871544Seschrock 		error = EINVAL;
10881544Seschrock 		goto out;
10891544Seschrock 	}
1090789Sahrens 
10912082Seschrock 	/*
10922082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
10932082Seschrock 	 * it's not present treat it as the initial version.
10942082Seschrock 	 */
10952082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
10964577Sahrens 		version = SPA_VERSION_INITIAL;
10972082Seschrock 
10981733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
10991733Sbonwick 	    &spa->spa_config_txg);
11001733Sbonwick 
11011635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
11021544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
11031544Seschrock 		error = EEXIST;
11041544Seschrock 		goto out;
11051544Seschrock 	}
1106789Sahrens 
11072174Seschrock 	spa->spa_load_guid = pool_guid;
11082174Seschrock 
1109789Sahrens 	/*
1110*9234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
1111*9234SGeorge.Wilson@Sun.COM 	 */
1112*9234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root == NULL)
1113*9234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1114*9234SGeorge.Wilson@Sun.COM 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1115*9234SGeorge.Wilson@Sun.COM 		    ZIO_FLAG_GODFATHER);
1116*9234SGeorge.Wilson@Sun.COM 
1117*9234SGeorge.Wilson@Sun.COM 	/*
11182082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
11192082Seschrock 	 * value that will be returned by spa_version() since parsing the
11202082Seschrock 	 * configuration requires knowing the version number.
1121789Sahrens 	 */
11227754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11232082Seschrock 	spa->spa_ubsync.ub_version = version;
11242082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
11257754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1126789Sahrens 
11272082Seschrock 	if (error != 0)
11281544Seschrock 		goto out;
1129789Sahrens 
11301585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1131789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1132789Sahrens 
1133789Sahrens 	/*
1134789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1135789Sahrens 	 */
11367754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11374070Smc142369 	error = vdev_open(rvd);
11387754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
11394070Smc142369 	if (error != 0)
11401544Seschrock 		goto out;
1141789Sahrens 
1142789Sahrens 	/*
11431986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
11447754SJeff.Bonwick@Sun.COM 	 * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER.
11451986Seschrock 	 */
11468241SJeff.Bonwick@Sun.COM 	if (mosconfig) {
11478241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11488241SJeff.Bonwick@Sun.COM 		error = vdev_validate(rvd);
11498241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
11508241SJeff.Bonwick@Sun.COM 		if (error != 0)
11518241SJeff.Bonwick@Sun.COM 			goto out;
11528241SJeff.Bonwick@Sun.COM 	}
11531986Seschrock 
11541986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
11551986Seschrock 		error = ENXIO;
11561986Seschrock 		goto out;
11571986Seschrock 	}
11581986Seschrock 
11591986Seschrock 	/*
1160789Sahrens 	 * Find the best uberblock.
1161789Sahrens 	 */
11627754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1163789Sahrens 
1164789Sahrens 	/*
1165789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1166789Sahrens 	 */
1167789Sahrens 	if (ub->ub_txg == 0) {
11681760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11691760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11701544Seschrock 		error = ENXIO;
11711544Seschrock 		goto out;
11721544Seschrock 	}
11731544Seschrock 
11741544Seschrock 	/*
11751544Seschrock 	 * If the pool is newer than the code, we can't open it.
11761544Seschrock 	 */
11774577Sahrens 	if (ub->ub_version > SPA_VERSION) {
11781760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11791760Seschrock 		    VDEV_AUX_VERSION_NEWER);
11801544Seschrock 		error = ENOTSUP;
11811544Seschrock 		goto out;
1182789Sahrens 	}
1183789Sahrens 
1184789Sahrens 	/*
1185789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1186789Sahrens 	 * incomplete configuration.
1187789Sahrens 	 */
11881732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
11891544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11901544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
11911544Seschrock 		error = ENXIO;
11921544Seschrock 		goto out;
1193789Sahrens 	}
1194789Sahrens 
1195789Sahrens 	/*
1196789Sahrens 	 * Initialize internal SPA structures.
1197789Sahrens 	 */
1198789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1199789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1200789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
12011544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
12021544Seschrock 	if (error) {
12031544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12041544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12051544Seschrock 		goto out;
12061544Seschrock 	}
1207789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1208789Sahrens 
12091544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1210789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
12111544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
12121544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12131544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12141544Seschrock 		error = EIO;
12151544Seschrock 		goto out;
12161544Seschrock 	}
1217789Sahrens 
1218789Sahrens 	if (!mosconfig) {
12192082Seschrock 		nvlist_t *newconfig;
12203975Sek110237 		uint64_t hostid;
12212082Seschrock 
12222082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
12231544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12241544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12251544Seschrock 			error = EIO;
12261544Seschrock 			goto out;
12271544Seschrock 		}
1228789Sahrens 
12297706SLin.Ling@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig,
12307706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
12313975Sek110237 			char *hostname;
12323975Sek110237 			unsigned long myhostid = 0;
12333975Sek110237 
12343975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
12353975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
12363975Sek110237 
12378662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
12388662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
12398662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
12408662SJordan.Vaughan@Sun.com 			/*
12418662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
12428662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
12438662SJordan.Vaughan@Sun.com 			 */
12443975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
12458662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
12464178Slling 			if (hostid != 0 && myhostid != 0 &&
12478662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
12483975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
12493975Sek110237 				    "loaded as it was last accessed by "
12507706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
12513975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
12527754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
12533975Sek110237 				    (unsigned long)hostid);
12543975Sek110237 				error = EBADF;
12553975Sek110237 				goto out;
12563975Sek110237 			}
12573975Sek110237 		}
12583975Sek110237 
1259789Sahrens 		spa_config_set(spa, newconfig);
1260789Sahrens 		spa_unload(spa);
1261789Sahrens 		spa_deactivate(spa);
12628241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1263789Sahrens 
12641544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
12651544Seschrock 	}
12661544Seschrock 
12671544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
12681544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
12691544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
12701544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12711544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12721544Seschrock 		error = EIO;
12731544Seschrock 		goto out;
1274789Sahrens 	}
1275789Sahrens 
12761544Seschrock 	/*
12772082Seschrock 	 * Load the bit that tells us to use the new accounting function
12782082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
12792082Seschrock 	 * be present.
12802082Seschrock 	 */
12812082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12822082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
12832082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
12842082Seschrock 	if (error != 0 && error != ENOENT) {
12852082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12862082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12872082Seschrock 		error = EIO;
12882082Seschrock 		goto out;
12892082Seschrock 	}
12902082Seschrock 
12912082Seschrock 	/*
12921544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
12931544Seschrock 	 * not be present.
12941544Seschrock 	 */
12951544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12961544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
12971544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
12981807Sbonwick 	if (error != 0 && error != ENOENT) {
12991544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13001544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13011544Seschrock 		error = EIO;
13021544Seschrock 		goto out;
13031544Seschrock 	}
13041544Seschrock 
13051544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
13061544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
13071544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
13081544Seschrock 	if (error != 0 && error != ENOENT) {
13091544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13101544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13111544Seschrock 		error = EIO;
13121544Seschrock 		goto out;
13131544Seschrock 	}
1314789Sahrens 
1315789Sahrens 	/*
13162926Sek110237 	 * Load the history object.  If we have an older pool, this
13172926Sek110237 	 * will not be present.
13182926Sek110237 	 */
13192926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
13202926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
13212926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
13222926Sek110237 	if (error != 0 && error != ENOENT) {
13232926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13242926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
13252926Sek110237 		error = EIO;
13262926Sek110237 		goto out;
13272926Sek110237 	}
13282926Sek110237 
13292926Sek110237 	/*
13302082Seschrock 	 * Load any hot spares for this pool.
13312082Seschrock 	 */
13322082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13335450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
13342082Seschrock 	if (error != 0 && error != ENOENT) {
13352082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13362082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13372082Seschrock 		error = EIO;
13382082Seschrock 		goto out;
13392082Seschrock 	}
13402082Seschrock 	if (error == 0) {
13414577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
13425450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
13435450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
13442082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13452082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13462082Seschrock 			error = EIO;
13472082Seschrock 			goto out;
13482082Seschrock 		}
13492082Seschrock 
13507754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13512082Seschrock 		spa_load_spares(spa);
13527754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
13532082Seschrock 	}
13542082Seschrock 
13555450Sbrendan 	/*
13565450Sbrendan 	 * Load any level 2 ARC devices for this pool.
13575450Sbrendan 	 */
13585450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13595450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
13605450Sbrendan 	    &spa->spa_l2cache.sav_object);
13615450Sbrendan 	if (error != 0 && error != ENOENT) {
13625450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13635450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
13645450Sbrendan 		error = EIO;
13655450Sbrendan 		goto out;
13665450Sbrendan 	}
13675450Sbrendan 	if (error == 0) {
13685450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
13695450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
13705450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
13715450Sbrendan 			vdev_set_state(rvd, B_TRUE,
13725450Sbrendan 			    VDEV_STATE_CANT_OPEN,
13735450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
13745450Sbrendan 			error = EIO;
13755450Sbrendan 			goto out;
13765450Sbrendan 		}
13775450Sbrendan 
13787754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13795450Sbrendan 		spa_load_l2cache(spa);
13807754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
13815450Sbrendan 	}
13825450Sbrendan 
13837294Sperrin 	if (spa_check_logs(spa)) {
13847294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13857294Sperrin 		    VDEV_AUX_BAD_LOG);
13867294Sperrin 		error = ENXIO;
13877294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
13887294Sperrin 		goto out;
13897294Sperrin 	}
13907294Sperrin 
13917294Sperrin 
13925094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
13934543Smarks 
13943912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13953912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
13963912Slling 
13973912Slling 	if (error && error != ENOENT) {
13983912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13993912Slling 		    VDEV_AUX_CORRUPT_DATA);
14003912Slling 		error = EIO;
14013912Slling 		goto out;
14023912Slling 	}
14033912Slling 
14043912Slling 	if (error == 0) {
14053912Slling 		(void) zap_lookup(spa->spa_meta_objset,
14063912Slling 		    spa->spa_pool_props_object,
14074451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
14083912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
14094451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
14104451Seschrock 		    spa->spa_pool_props_object,
14114451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
14124451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
14134543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
14144543Smarks 		    spa->spa_pool_props_object,
14154543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
14164543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
14175329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
14185329Sgw25295 		    spa->spa_pool_props_object,
14195329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
14205329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
14213912Slling 	}
14223912Slling 
14232082Seschrock 	/*
14244451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
14254451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
14264451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
14274451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
14284451Seschrock 	 * over.
14294451Seschrock 	 */
14305756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
14314451Seschrock 		spa_check_removed(spa->spa_root_vdev);
14324451Seschrock 
14334451Seschrock 	/*
14341986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1435789Sahrens 	 */
14361986Seschrock 	vdev_load(rvd);
1437789Sahrens 
1438789Sahrens 	/*
1439789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1440789Sahrens 	 */
14417754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1442789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
14437754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1444789Sahrens 
1445789Sahrens 	/*
1446789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1447789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1448789Sahrens 	 */
14491544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
14501544Seschrock 		error = ENXIO;
14511544Seschrock 		goto out;
14521544Seschrock 	}
1453789Sahrens 
14548241SJeff.Bonwick@Sun.COM 	if (spa_writeable(spa)) {
14551635Sbonwick 		dmu_tx_t *tx;
14561635Sbonwick 		int need_update = B_FALSE;
14578241SJeff.Bonwick@Sun.COM 
14588241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
14591601Sbonwick 
14601635Sbonwick 		/*
14611635Sbonwick 		 * Claim log blocks that haven't been committed yet.
14621635Sbonwick 		 * This must all happen in a single txg.
14631635Sbonwick 		 */
14641601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1465789Sahrens 		    spa_first_txg(spa));
14667754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
14672417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1468789Sahrens 		dmu_tx_commit(tx);
1469789Sahrens 
1470789Sahrens 		spa->spa_sync_on = B_TRUE;
1471789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1472789Sahrens 
1473789Sahrens 		/*
1474789Sahrens 		 * Wait for all claims to sync.
1475789Sahrens 		 */
1476789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
14771585Sbonwick 
14781585Sbonwick 		/*
14791635Sbonwick 		 * If the config cache is stale, or we have uninitialized
14801635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
14811585Sbonwick 		 */
14821635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
14831635Sbonwick 		    state == SPA_LOAD_IMPORT)
14841635Sbonwick 			need_update = B_TRUE;
14851635Sbonwick 
14868241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
14871635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
14881635Sbonwick 				need_update = B_TRUE;
14891585Sbonwick 
14901585Sbonwick 		/*
14911635Sbonwick 		 * Update the config cache asychronously in case we're the
14921635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
14931585Sbonwick 		 */
14941635Sbonwick 		if (need_update)
14951635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
14968241SJeff.Bonwick@Sun.COM 
14978241SJeff.Bonwick@Sun.COM 		/*
14988241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
14998241SJeff.Bonwick@Sun.COM 		 */
15008241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
15018241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
1502789Sahrens 	}
1503789Sahrens 
15041544Seschrock 	error = 0;
15051544Seschrock out:
15067046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
15072082Seschrock 	if (error && error != EBADF)
15087294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
15091544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
15101544Seschrock 	spa->spa_ena = 0;
15111544Seschrock 
15121544Seschrock 	return (error);
1513789Sahrens }
1514789Sahrens 
1515789Sahrens /*
1516789Sahrens  * Pool Open/Import
1517789Sahrens  *
1518789Sahrens  * The import case is identical to an open except that the configuration is sent
1519789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1520789Sahrens  * case of an open, the pool configuration will exist in the
15214451Seschrock  * POOL_STATE_UNINITIALIZED state.
1522789Sahrens  *
1523789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1524789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1525789Sahrens  * ambiguous state.
1526789Sahrens  */
1527789Sahrens static int
1528789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1529789Sahrens {
1530789Sahrens 	spa_t *spa;
1531789Sahrens 	int error;
1532789Sahrens 	int locked = B_FALSE;
1533789Sahrens 
1534789Sahrens 	*spapp = NULL;
1535789Sahrens 
1536789Sahrens 	/*
1537789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1538789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1539789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1540789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1541789Sahrens 	 */
1542789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1543789Sahrens 		mutex_enter(&spa_namespace_lock);
1544789Sahrens 		locked = B_TRUE;
1545789Sahrens 	}
1546789Sahrens 
1547789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1548789Sahrens 		if (locked)
1549789Sahrens 			mutex_exit(&spa_namespace_lock);
1550789Sahrens 		return (ENOENT);
1551789Sahrens 	}
1552789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1553789Sahrens 
15548241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
1555789Sahrens 
15561635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1557789Sahrens 
1558789Sahrens 		if (error == EBADF) {
1559789Sahrens 			/*
15601986Seschrock 			 * If vdev_validate() returns failure (indicated by
15611986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
15621986Seschrock 			 * that the pool has been exported or destroyed.  If
15631986Seschrock 			 * this is the case, the config cache is out of sync and
15641986Seschrock 			 * we should remove the pool from the namespace.
1565789Sahrens 			 */
1566789Sahrens 			spa_unload(spa);
1567789Sahrens 			spa_deactivate(spa);
15686643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1569789Sahrens 			spa_remove(spa);
1570789Sahrens 			if (locked)
1571789Sahrens 				mutex_exit(&spa_namespace_lock);
1572789Sahrens 			return (ENOENT);
15731544Seschrock 		}
15741544Seschrock 
15751544Seschrock 		if (error) {
1576789Sahrens 			/*
1577789Sahrens 			 * We can't open the pool, but we still have useful
1578789Sahrens 			 * information: the state of each vdev after the
1579789Sahrens 			 * attempted vdev_open().  Return this to the user.
1580789Sahrens 			 */
15817754SJeff.Bonwick@Sun.COM 			if (config != NULL && spa->spa_root_vdev != NULL)
1582789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1583789Sahrens 				    B_TRUE);
1584789Sahrens 			spa_unload(spa);
1585789Sahrens 			spa_deactivate(spa);
15861544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1587789Sahrens 			if (locked)
1588789Sahrens 				mutex_exit(&spa_namespace_lock);
1589789Sahrens 			*spapp = NULL;
1590789Sahrens 			return (error);
15911544Seschrock 		} else {
15921544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1593789Sahrens 		}
1594789Sahrens 	}
1595789Sahrens 
1596789Sahrens 	spa_open_ref(spa, tag);
15974451Seschrock 
1598789Sahrens 	if (locked)
1599789Sahrens 		mutex_exit(&spa_namespace_lock);
1600789Sahrens 
1601789Sahrens 	*spapp = spa;
1602789Sahrens 
16037754SJeff.Bonwick@Sun.COM 	if (config != NULL)
1604789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1605789Sahrens 
1606789Sahrens 	return (0);
1607789Sahrens }
1608789Sahrens 
1609789Sahrens int
1610789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1611789Sahrens {
1612789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1613789Sahrens }
1614789Sahrens 
16151544Seschrock /*
16161544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
16171544Seschrock  * preventing it from being exported or destroyed.
16181544Seschrock  */
16191544Seschrock spa_t *
16201544Seschrock spa_inject_addref(char *name)
16211544Seschrock {
16221544Seschrock 	spa_t *spa;
16231544Seschrock 
16241544Seschrock 	mutex_enter(&spa_namespace_lock);
16251544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
16261544Seschrock 		mutex_exit(&spa_namespace_lock);
16271544Seschrock 		return (NULL);
16281544Seschrock 	}
16291544Seschrock 	spa->spa_inject_ref++;
16301544Seschrock 	mutex_exit(&spa_namespace_lock);
16311544Seschrock 
16321544Seschrock 	return (spa);
16331544Seschrock }
16341544Seschrock 
16351544Seschrock void
16361544Seschrock spa_inject_delref(spa_t *spa)
16371544Seschrock {
16381544Seschrock 	mutex_enter(&spa_namespace_lock);
16391544Seschrock 	spa->spa_inject_ref--;
16401544Seschrock 	mutex_exit(&spa_namespace_lock);
16411544Seschrock }
16421544Seschrock 
16435450Sbrendan /*
16445450Sbrendan  * Add spares device information to the nvlist.
16455450Sbrendan  */
16462082Seschrock static void
16472082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
16482082Seschrock {
16492082Seschrock 	nvlist_t **spares;
16502082Seschrock 	uint_t i, nspares;
16512082Seschrock 	nvlist_t *nvroot;
16522082Seschrock 	uint64_t guid;
16532082Seschrock 	vdev_stat_t *vs;
16542082Seschrock 	uint_t vsc;
16553377Seschrock 	uint64_t pool;
16562082Seschrock 
16575450Sbrendan 	if (spa->spa_spares.sav_count == 0)
16582082Seschrock 		return;
16592082Seschrock 
16602082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
16612082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16625450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
16632082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
16642082Seschrock 	if (nspares != 0) {
16652082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
16662082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
16672082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
16682082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
16692082Seschrock 
16702082Seschrock 		/*
16712082Seschrock 		 * Go through and find any spares which have since been
16722082Seschrock 		 * repurposed as an active spare.  If this is the case, update
16732082Seschrock 		 * their status appropriately.
16742082Seschrock 		 */
16752082Seschrock 		for (i = 0; i < nspares; i++) {
16762082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
16772082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
16787214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
16797214Slling 			    pool != 0ULL) {
16802082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
16812082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
16822082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
16832082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
16842082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
16852082Seschrock 			}
16862082Seschrock 		}
16872082Seschrock 	}
16882082Seschrock }
16892082Seschrock 
16905450Sbrendan /*
16915450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
16925450Sbrendan  */
16935450Sbrendan static void
16945450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
16955450Sbrendan {
16965450Sbrendan 	nvlist_t **l2cache;
16975450Sbrendan 	uint_t i, j, nl2cache;
16985450Sbrendan 	nvlist_t *nvroot;
16995450Sbrendan 	uint64_t guid;
17005450Sbrendan 	vdev_t *vd;
17015450Sbrendan 	vdev_stat_t *vs;
17025450Sbrendan 	uint_t vsc;
17035450Sbrendan 
17045450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
17055450Sbrendan 		return;
17065450Sbrendan 
17077754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
17085450Sbrendan 
17095450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
17105450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
17115450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
17125450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
17135450Sbrendan 	if (nl2cache != 0) {
17145450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
17155450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
17165450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
17175450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
17185450Sbrendan 
17195450Sbrendan 		/*
17205450Sbrendan 		 * Update level 2 cache device stats.
17215450Sbrendan 		 */
17225450Sbrendan 
17235450Sbrendan 		for (i = 0; i < nl2cache; i++) {
17245450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
17255450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
17265450Sbrendan 
17275450Sbrendan 			vd = NULL;
17285450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
17295450Sbrendan 				if (guid ==
17305450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
17315450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
17325450Sbrendan 					break;
17335450Sbrendan 				}
17345450Sbrendan 			}
17355450Sbrendan 			ASSERT(vd != NULL);
17365450Sbrendan 
17375450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
17385450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
17395450Sbrendan 			vdev_get_stats(vd, vs);
17405450Sbrendan 		}
17415450Sbrendan 	}
17425450Sbrendan 
17437754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
17445450Sbrendan }
17455450Sbrendan 
1746789Sahrens int
17471544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1748789Sahrens {
1749789Sahrens 	int error;
1750789Sahrens 	spa_t *spa;
1751789Sahrens 
1752789Sahrens 	*config = NULL;
1753789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1754789Sahrens 
17552082Seschrock 	if (spa && *config != NULL) {
17561544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
17571544Seschrock 		    spa_get_errlog_size(spa)) == 0);
17581544Seschrock 
17597754SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
17607754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
17617754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0);
17627754SJeff.Bonwick@Sun.COM 
17632082Seschrock 		spa_add_spares(spa, *config);
17645450Sbrendan 		spa_add_l2cache(spa, *config);
17652082Seschrock 	}
17662082Seschrock 
17671544Seschrock 	/*
17681544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
17691544Seschrock 	 * and call spa_lookup() directly.
17701544Seschrock 	 */
17711544Seschrock 	if (altroot) {
17721544Seschrock 		if (spa == NULL) {
17731544Seschrock 			mutex_enter(&spa_namespace_lock);
17741544Seschrock 			spa = spa_lookup(name);
17751544Seschrock 			if (spa)
17761544Seschrock 				spa_altroot(spa, altroot, buflen);
17771544Seschrock 			else
17781544Seschrock 				altroot[0] = '\0';
17791544Seschrock 			spa = NULL;
17801544Seschrock 			mutex_exit(&spa_namespace_lock);
17811544Seschrock 		} else {
17821544Seschrock 			spa_altroot(spa, altroot, buflen);
17831544Seschrock 		}
17841544Seschrock 	}
17851544Seschrock 
1786789Sahrens 	if (spa != NULL)
1787789Sahrens 		spa_close(spa, FTAG);
1788789Sahrens 
1789789Sahrens 	return (error);
1790789Sahrens }
1791789Sahrens 
1792789Sahrens /*
17935450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
17945450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
17955450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
17965450Sbrendan  * specified, as long as they are well-formed.
17972082Seschrock  */
17982082Seschrock static int
17995450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
18005450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
18015450Sbrendan     vdev_labeltype_t label)
18022082Seschrock {
18035450Sbrendan 	nvlist_t **dev;
18045450Sbrendan 	uint_t i, ndev;
18052082Seschrock 	vdev_t *vd;
18062082Seschrock 	int error;
18072082Seschrock 
18087754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18097754SJeff.Bonwick@Sun.COM 
18102082Seschrock 	/*
18115450Sbrendan 	 * It's acceptable to have no devs specified.
18122082Seschrock 	 */
18135450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
18142082Seschrock 		return (0);
18152082Seschrock 
18165450Sbrendan 	if (ndev == 0)
18172082Seschrock 		return (EINVAL);
18182082Seschrock 
18192082Seschrock 	/*
18205450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
18215450Sbrendan 	 * device type.
18222082Seschrock 	 */
18235450Sbrendan 	if (spa_version(spa) < version)
18242082Seschrock 		return (ENOTSUP);
18252082Seschrock 
18263377Seschrock 	/*
18275450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
18283377Seschrock 	 * checking.
18293377Seschrock 	 */
18305450Sbrendan 	sav->sav_pending = dev;
18315450Sbrendan 	sav->sav_npending = ndev;
18325450Sbrendan 
18335450Sbrendan 	for (i = 0; i < ndev; i++) {
18345450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
18352082Seschrock 		    mode)) != 0)
18363377Seschrock 			goto out;
18372082Seschrock 
18382082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
18392082Seschrock 			vdev_free(vd);
18403377Seschrock 			error = EINVAL;
18413377Seschrock 			goto out;
18422082Seschrock 		}
18432082Seschrock 
18445450Sbrendan 		/*
18457754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
18467754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
18475450Sbrendan 		 */
18487754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
18495450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
18505450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
18515450Sbrendan 			error = ENOTBLK;
18525450Sbrendan 			goto out;
18535450Sbrendan 		}
18547754SJeff.Bonwick@Sun.COM #endif
18552082Seschrock 		vd->vdev_top = vd;
18563377Seschrock 
18573377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
18585450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
18595450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
18603377Seschrock 			    vd->vdev_guid) == 0);
18612082Seschrock 		}
18622082Seschrock 
18632082Seschrock 		vdev_free(vd);
18643377Seschrock 
18655450Sbrendan 		if (error &&
18665450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
18673377Seschrock 			goto out;
18683377Seschrock 		else
18693377Seschrock 			error = 0;
18702082Seschrock 	}
18712082Seschrock 
18723377Seschrock out:
18735450Sbrendan 	sav->sav_pending = NULL;
18745450Sbrendan 	sav->sav_npending = 0;
18753377Seschrock 	return (error);
18762082Seschrock }
18772082Seschrock 
18785450Sbrendan static int
18795450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
18805450Sbrendan {
18815450Sbrendan 	int error;
18825450Sbrendan 
18837754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18847754SJeff.Bonwick@Sun.COM 
18855450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
18865450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
18875450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
18885450Sbrendan 		return (error);
18895450Sbrendan 	}
18905450Sbrendan 
18915450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
18925450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
18935450Sbrendan 	    VDEV_LABEL_L2CACHE));
18945450Sbrendan }
18955450Sbrendan 
18965450Sbrendan static void
18975450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
18985450Sbrendan     const char *config)
18995450Sbrendan {
19005450Sbrendan 	int i;
19015450Sbrendan 
19025450Sbrendan 	if (sav->sav_config != NULL) {
19035450Sbrendan 		nvlist_t **olddevs;
19045450Sbrendan 		uint_t oldndevs;
19055450Sbrendan 		nvlist_t **newdevs;
19065450Sbrendan 
19075450Sbrendan 		/*
19085450Sbrendan 		 * Generate new dev list by concatentating with the
19095450Sbrendan 		 * current dev list.
19105450Sbrendan 		 */
19115450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
19125450Sbrendan 		    &olddevs, &oldndevs) == 0);
19135450Sbrendan 
19145450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
19155450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
19165450Sbrendan 		for (i = 0; i < oldndevs; i++)
19175450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
19185450Sbrendan 			    KM_SLEEP) == 0);
19195450Sbrendan 		for (i = 0; i < ndevs; i++)
19205450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
19215450Sbrendan 			    KM_SLEEP) == 0);
19225450Sbrendan 
19235450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
19245450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
19255450Sbrendan 
19265450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
19275450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
19285450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
19295450Sbrendan 			nvlist_free(newdevs[i]);
19305450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
19315450Sbrendan 	} else {
19325450Sbrendan 		/*
19335450Sbrendan 		 * Generate a new dev list.
19345450Sbrendan 		 */
19355450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
19365450Sbrendan 		    KM_SLEEP) == 0);
19375450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
19385450Sbrendan 		    devs, ndevs) == 0);
19395450Sbrendan 	}
19405450Sbrendan }
19415450Sbrendan 
19425450Sbrendan /*
19435450Sbrendan  * Stop and drop level 2 ARC devices
19445450Sbrendan  */
19455450Sbrendan void
19465450Sbrendan spa_l2cache_drop(spa_t *spa)
19475450Sbrendan {
19485450Sbrendan 	vdev_t *vd;
19495450Sbrendan 	int i;
19505450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
19515450Sbrendan 
19525450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
19535450Sbrendan 		uint64_t pool;
19545450Sbrendan 
19555450Sbrendan 		vd = sav->sav_vdevs[i];
19565450Sbrendan 		ASSERT(vd != NULL);
19575450Sbrendan 
19588241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
19598241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
19605450Sbrendan 			l2arc_remove_vdev(vd);
19615450Sbrendan 		if (vd->vdev_isl2cache)
19625450Sbrendan 			spa_l2cache_remove(vd);
19635450Sbrendan 		vdev_clear_stats(vd);
19645450Sbrendan 		(void) vdev_close(vd);
19655450Sbrendan 	}
19665450Sbrendan }
19675450Sbrendan 
19682082Seschrock /*
1969789Sahrens  * Pool Creation
1970789Sahrens  */
1971789Sahrens int
19725094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
19737184Stimh     const char *history_str, nvlist_t *zplprops)
1974789Sahrens {
1975789Sahrens 	spa_t *spa;
19765094Slling 	char *altroot = NULL;
19771635Sbonwick 	vdev_t *rvd;
1978789Sahrens 	dsl_pool_t *dp;
1979789Sahrens 	dmu_tx_t *tx;
19802082Seschrock 	int c, error = 0;
1981789Sahrens 	uint64_t txg = TXG_INITIAL;
19825450Sbrendan 	nvlist_t **spares, **l2cache;
19835450Sbrendan 	uint_t nspares, nl2cache;
19845094Slling 	uint64_t version;
1985789Sahrens 
1986789Sahrens 	/*
1987789Sahrens 	 * If this pool already exists, return failure.
1988789Sahrens 	 */
1989789Sahrens 	mutex_enter(&spa_namespace_lock);
1990789Sahrens 	if (spa_lookup(pool) != NULL) {
1991789Sahrens 		mutex_exit(&spa_namespace_lock);
1992789Sahrens 		return (EEXIST);
1993789Sahrens 	}
1994789Sahrens 
1995789Sahrens 	/*
1996789Sahrens 	 * Allocate a new spa_t structure.
1997789Sahrens 	 */
19985094Slling 	(void) nvlist_lookup_string(props,
19995094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20001635Sbonwick 	spa = spa_add(pool, altroot);
20018241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2002789Sahrens 
2003789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
20045094Slling 
20055094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
20065094Slling 		spa_unload(spa);
20075094Slling 		spa_deactivate(spa);
20085094Slling 		spa_remove(spa);
20096643Seschrock 		mutex_exit(&spa_namespace_lock);
20105094Slling 		return (error);
20115094Slling 	}
20125094Slling 
20135094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
20145094Slling 	    &version) != 0)
20155094Slling 		version = SPA_VERSION;
20165094Slling 	ASSERT(version <= SPA_VERSION);
20175094Slling 	spa->spa_uberblock.ub_version = version;
2018789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2019789Sahrens 
20201635Sbonwick 	/*
2021*9234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
2022*9234SGeorge.Wilson@Sun.COM 	 */
2023*9234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root == NULL)
2024*9234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2025*9234SGeorge.Wilson@Sun.COM 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2026*9234SGeorge.Wilson@Sun.COM 		    ZIO_FLAG_GODFATHER);
2027*9234SGeorge.Wilson@Sun.COM 
2028*9234SGeorge.Wilson@Sun.COM 	/*
20291635Sbonwick 	 * Create the root vdev.
20301635Sbonwick 	 */
20317754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20321635Sbonwick 
20332082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
20342082Seschrock 
20352082Seschrock 	ASSERT(error != 0 || rvd != NULL);
20362082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
20372082Seschrock 
20385913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
20391635Sbonwick 		error = EINVAL;
20402082Seschrock 
20412082Seschrock 	if (error == 0 &&
20422082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
20435450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
20442082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
20452082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
20462082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
20472082Seschrock 		vdev_config_dirty(rvd);
20481635Sbonwick 	}
20491635Sbonwick 
20507754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2051789Sahrens 
20522082Seschrock 	if (error != 0) {
2053789Sahrens 		spa_unload(spa);
2054789Sahrens 		spa_deactivate(spa);
2055789Sahrens 		spa_remove(spa);
2056789Sahrens 		mutex_exit(&spa_namespace_lock);
2057789Sahrens 		return (error);
2058789Sahrens 	}
2059789Sahrens 
20602082Seschrock 	/*
20612082Seschrock 	 * Get the list of spares, if specified.
20622082Seschrock 	 */
20632082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
20642082Seschrock 	    &spares, &nspares) == 0) {
20655450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
20662082Seschrock 		    KM_SLEEP) == 0);
20675450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
20682082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
20697754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20702082Seschrock 		spa_load_spares(spa);
20717754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
20725450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
20735450Sbrendan 	}
20745450Sbrendan 
20755450Sbrendan 	/*
20765450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
20775450Sbrendan 	 */
20785450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
20795450Sbrendan 	    &l2cache, &nl2cache) == 0) {
20805450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
20815450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
20825450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
20835450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
20847754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20855450Sbrendan 		spa_load_l2cache(spa);
20867754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
20875450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
20882082Seschrock 	}
20892082Seschrock 
20907184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2091789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2092789Sahrens 
2093789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2094789Sahrens 
2095789Sahrens 	/*
2096789Sahrens 	 * Create the pool config object.
2097789Sahrens 	 */
2098789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
20997497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2100789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2101789Sahrens 
21021544Seschrock 	if (zap_add(spa->spa_meta_objset,
2103789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
21041544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
21051544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
21061544Seschrock 	}
2107789Sahrens 
21085094Slling 	/* Newly created pools with the right version are always deflated. */
21095094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
21105094Slling 		spa->spa_deflate = TRUE;
21115094Slling 		if (zap_add(spa->spa_meta_objset,
21125094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
21135094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
21145094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
21155094Slling 		}
21162082Seschrock 	}
21172082Seschrock 
2118789Sahrens 	/*
2119789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2120789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2121789Sahrens 	 * keeps changing.
2122789Sahrens 	 */
2123789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2124789Sahrens 	    1 << 14, tx);
2125789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2126789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2127789Sahrens 
21281544Seschrock 	if (zap_add(spa->spa_meta_objset,
2129789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
21301544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
21311544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
21321544Seschrock 	}
2133789Sahrens 
21342926Sek110237 	/*
21352926Sek110237 	 * Create the pool's history object.
21362926Sek110237 	 */
21375094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
21385094Slling 		spa_history_create_obj(spa, tx);
21395094Slling 
21405094Slling 	/*
21415094Slling 	 * Set pool properties.
21425094Slling 	 */
21435094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
21445094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
21455329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
21468525SEric.Schrock@Sun.COM 	if (props != NULL) {
21478525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
21485094Slling 		spa_sync_props(spa, props, CRED(), tx);
21498525SEric.Schrock@Sun.COM 	}
21502926Sek110237 
2151789Sahrens 	dmu_tx_commit(tx);
2152789Sahrens 
2153789Sahrens 	spa->spa_sync_on = B_TRUE;
2154789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2155789Sahrens 
2156789Sahrens 	/*
2157789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2158789Sahrens 	 * bean counters are appropriately updated.
2159789Sahrens 	 */
2160789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2161789Sahrens 
21626643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2163789Sahrens 
21645094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
21654715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
21664715Sek110237 
21678667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
21688667SGeorge.Wilson@Sun.COM 
2169789Sahrens 	mutex_exit(&spa_namespace_lock);
2170789Sahrens 
2171789Sahrens 	return (0);
2172789Sahrens }
2173789Sahrens 
2174789Sahrens /*
2175789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2176789Sahrens  * then call spa_load() to do the dirty work.
2177789Sahrens  */
21786423Sgw25295 static int
21796423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
21806643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2181789Sahrens {
2182789Sahrens 	spa_t *spa;
21835094Slling 	char *altroot = NULL;
21846643Seschrock 	int error, loaderr;
21852082Seschrock 	nvlist_t *nvroot;
21865450Sbrendan 	nvlist_t **spares, **l2cache;
21875450Sbrendan 	uint_t nspares, nl2cache;
2188789Sahrens 
2189789Sahrens 	/*
2190789Sahrens 	 * If a pool with this name exists, return failure.
2191789Sahrens 	 */
2192789Sahrens 	mutex_enter(&spa_namespace_lock);
21937897SLin.Ling@Sun.COM 	if ((spa = spa_lookup(pool)) != NULL) {
21947897SLin.Ling@Sun.COM 		if (isroot) {
21957897SLin.Ling@Sun.COM 			/*
21967897SLin.Ling@Sun.COM 			 * Remove the existing root pool from the
21977897SLin.Ling@Sun.COM 			 * namespace so that we can replace it with
21987897SLin.Ling@Sun.COM 			 * the correct config we just read in.
21997897SLin.Ling@Sun.COM 			 */
22007897SLin.Ling@Sun.COM 			ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
22017897SLin.Ling@Sun.COM 			spa_remove(spa);
22027897SLin.Ling@Sun.COM 		} else {
22037897SLin.Ling@Sun.COM 			mutex_exit(&spa_namespace_lock);
22047897SLin.Ling@Sun.COM 			return (EEXIST);
22057897SLin.Ling@Sun.COM 		}
2206789Sahrens 	}
2207789Sahrens 
2208789Sahrens 	/*
22091635Sbonwick 	 * Create and initialize the spa structure.
2210789Sahrens 	 */
22115094Slling 	(void) nvlist_lookup_string(props,
22125094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
22131635Sbonwick 	spa = spa_add(pool, altroot);
22148241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2215789Sahrens 
22166643Seschrock 	if (allowfaulted)
22176643Seschrock 		spa->spa_import_faulted = B_TRUE;
22186673Seschrock 	spa->spa_is_root = isroot;
22196643Seschrock 
2220789Sahrens 	/*
22211635Sbonwick 	 * Pass off the heavy lifting to spa_load().
22227046Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
22237046Sahrens 	 * the user-supplied config is actually the one to trust when
22247046Sahrens 	 * doing an import.
22251601Sbonwick 	 */
22267046Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2227789Sahrens 
22287754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22292082Seschrock 	/*
22302082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
22312082Seschrock 	 * and conflicts with spa_has_spare().
22322082Seschrock 	 */
22336423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
22345450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
22355450Sbrendan 		spa->spa_spares.sav_config = NULL;
22362082Seschrock 		spa_load_spares(spa);
22372082Seschrock 	}
22386423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
22395450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
22405450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
22415450Sbrendan 		spa_load_l2cache(spa);
22425450Sbrendan 	}
22432082Seschrock 
22442082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
22452082Seschrock 	    &nvroot) == 0);
22465450Sbrendan 	if (error == 0)
22475450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
22485450Sbrendan 	if (error == 0)
22495450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
22505450Sbrendan 		    VDEV_ALLOC_L2CACHE);
22517754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
22522082Seschrock 
22538525SEric.Schrock@Sun.COM 	if (props != NULL)
22548525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
22558525SEric.Schrock@Sun.COM 
22568241SJeff.Bonwick@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
22578241SJeff.Bonwick@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
22586643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
22596643Seschrock 			/*
22606643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
22616643Seschrock 			 * set, then manually set the config as if the config
22626643Seschrock 			 * passed in was specified in the cache file.
22636643Seschrock 			 */
22646643Seschrock 			error = 0;
22656643Seschrock 			spa->spa_import_faulted = B_FALSE;
22667754SJeff.Bonwick@Sun.COM 			if (spa->spa_config == NULL)
22676643Seschrock 				spa->spa_config = spa_config_generate(spa,
22686643Seschrock 				    NULL, -1ULL, B_TRUE);
22696643Seschrock 			spa_unload(spa);
22706643Seschrock 			spa_deactivate(spa);
22716643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
22726643Seschrock 		} else {
22736643Seschrock 			spa_unload(spa);
22746643Seschrock 			spa_deactivate(spa);
22756643Seschrock 			spa_remove(spa);
22766643Seschrock 		}
2277789Sahrens 		mutex_exit(&spa_namespace_lock);
2278789Sahrens 		return (error);
2279789Sahrens 	}
2280789Sahrens 
22811635Sbonwick 	/*
22825450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
22835450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
22842082Seschrock 	 */
22852082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
22862082Seschrock 	    &spares, &nspares) == 0) {
22875450Sbrendan 		if (spa->spa_spares.sav_config)
22885450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
22892082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
22902082Seschrock 		else
22915450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
22922082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
22935450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
22942082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
22957754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22962082Seschrock 		spa_load_spares(spa);
22977754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22985450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
22995450Sbrendan 	}
23005450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
23015450Sbrendan 	    &l2cache, &nl2cache) == 0) {
23025450Sbrendan 		if (spa->spa_l2cache.sav_config)
23035450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
23045450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
23055450Sbrendan 		else
23065450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
23075450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
23085450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
23095450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
23107754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
23115450Sbrendan 		spa_load_l2cache(spa);
23127754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
23135450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
23142082Seschrock 	}
23152082Seschrock 
23168241SJeff.Bonwick@Sun.COM 	if (spa_writeable(spa)) {
23176643Seschrock 		/*
23186643Seschrock 		 * Update the config cache to include the newly-imported pool.
23196643Seschrock 		 */
23206423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
23216643Seschrock 	}
23226643Seschrock 
23236643Seschrock 	spa->spa_import_faulted = B_FALSE;
23244451Seschrock 	mutex_exit(&spa_namespace_lock);
23254451Seschrock 
2326789Sahrens 	return (0);
2327789Sahrens }
2328789Sahrens 
23296423Sgw25295 #ifdef _KERNEL
23306423Sgw25295 /*
23316423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
23326423Sgw25295  * device label.
23336423Sgw25295  */
23346423Sgw25295 static void
23356423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
23366423Sgw25295 {
23376423Sgw25295 	nvlist_t *nvtop, *nvroot;
23386423Sgw25295 	uint64_t pgid;
23396423Sgw25295 
23406423Sgw25295 	/*
23416423Sgw25295 	 * Add this top-level vdev to the child array.
23426423Sgw25295 	 */
23436423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
23446423Sgw25295 	    == 0);
23456423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
23466423Sgw25295 	    == 0);
23476423Sgw25295 
23486423Sgw25295 	/*
23496423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
23506423Sgw25295 	 */
23516423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
23526423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
23536423Sgw25295 	    == 0);
23546423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
23556423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
23566423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
23576423Sgw25295 	    &nvtop, 1) == 0);
23586423Sgw25295 
23596423Sgw25295 	/*
23606423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
23616423Sgw25295 	 * this pool's configuration (remove the old, add the new).
23626423Sgw25295 	 */
23636423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
23646423Sgw25295 	nvlist_free(nvroot);
23656423Sgw25295 }
23666423Sgw25295 
23676423Sgw25295 /*
23686423Sgw25295  * Get the root pool information from the root disk, then import the root pool
23696423Sgw25295  * during the system boot up time.
23706423Sgw25295  */
23717539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
23727147Staylor 
23737147Staylor int
23747147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
23756423Sgw25295     uint64_t *besttxg)
23766423Sgw25295 {
23776423Sgw25295 	nvlist_t *config;
23786423Sgw25295 	uint64_t txg;
23797539SLin.Ling@Sun.COM 	int error;
23807539SLin.Ling@Sun.COM 
23817539SLin.Ling@Sun.COM 	if (error = vdev_disk_read_rootlabel(devpath, devid, &config))
23827539SLin.Ling@Sun.COM 		return (error);
23836423Sgw25295 
23846423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
23856423Sgw25295 
23867147Staylor 	if (bestconf != NULL)
23876423Sgw25295 		*bestconf = config;
23887539SLin.Ling@Sun.COM 	else
23897539SLin.Ling@Sun.COM 		nvlist_free(config);
23907147Staylor 	*besttxg = txg;
23917147Staylor 	return (0);
23926423Sgw25295 }
23936423Sgw25295 
23946423Sgw25295 boolean_t
23956423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
23966423Sgw25295 {
23976423Sgw25295 	uint64_t ival;
23986423Sgw25295 
23996423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
24006423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
24016423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
24026423Sgw25295 		return (B_FALSE);
24036423Sgw25295 
24046423Sgw25295 	return (B_TRUE);
24056423Sgw25295 }
24066423Sgw25295 
24077147Staylor 
24087147Staylor /*
24097147Staylor  * Given the boot device's physical path or devid, check if the device
24107147Staylor  * is in a valid state.  If so, return the configuration from the vdev
24117147Staylor  * label.
24127147Staylor  */
24137147Staylor int
24147147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
24157147Staylor {
24167147Staylor 	nvlist_t *conf = NULL;
24177147Staylor 	uint64_t txg = 0;
24187147Staylor 	nvlist_t *nvtop, **child;
24197147Staylor 	char *type;
24207147Staylor 	char *bootpath = NULL;
24217147Staylor 	uint_t children, c;
24227147Staylor 	char *tmp;
24237539SLin.Ling@Sun.COM 	int error;
24247147Staylor 
24257147Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
24267147Staylor 		*tmp = '\0';
24277539SLin.Ling@Sun.COM 	if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) {
24287147Staylor 		cmn_err(CE_NOTE, "error reading device label");
24297539SLin.Ling@Sun.COM 		return (error);
24307147Staylor 	}
24317147Staylor 	if (txg == 0) {
24327147Staylor 		cmn_err(CE_NOTE, "this device is detached");
24337147Staylor 		nvlist_free(conf);
24347147Staylor 		return (EINVAL);
24357147Staylor 	}
24367147Staylor 
24377147Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
24387147Staylor 	    &nvtop) == 0);
24397147Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
24407147Staylor 
24417147Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
24427147Staylor 		if (spa_rootdev_validate(nvtop)) {
24437147Staylor 			goto out;
24447147Staylor 		} else {
24457147Staylor 			nvlist_free(conf);
24467147Staylor 			return (EINVAL);
24477147Staylor 		}
24487147Staylor 	}
24497147Staylor 
24507147Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
24517147Staylor 
24527147Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
24537147Staylor 	    &child, &children) == 0);
24547147Staylor 
24557147Staylor 	/*
24567147Staylor 	 * Go thru vdevs in the mirror to see if the given device
24577147Staylor 	 * has the most recent txg. Only the device with the most
24587147Staylor 	 * recent txg has valid information and should be booted.
24597147Staylor 	 */
24607147Staylor 	for (c = 0; c < children; c++) {
24617147Staylor 		char *cdevid, *cpath;
24627147Staylor 		uint64_t tmptxg;
24637147Staylor 
24648242SLin.Ling@Sun.COM 		cpath = NULL;
24658242SLin.Ling@Sun.COM 		cdevid = NULL;
24667147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
24678242SLin.Ling@Sun.COM 		    &cpath) != 0 && nvlist_lookup_string(child[c],
24688242SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_DEVID, &cdevid) != 0)
24697147Staylor 			return (EINVAL);
24707687SLin.Ling@Sun.COM 		if ((spa_check_rootconf(cpath, cdevid, NULL,
24717687SLin.Ling@Sun.COM 		    &tmptxg) == 0) && (tmptxg > txg)) {
24727147Staylor 			txg = tmptxg;
24737147Staylor 			VERIFY(nvlist_lookup_string(child[c],
24747147Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
24757147Staylor 		}
24767147Staylor 	}
24777147Staylor 
24787147Staylor 	/* Does the best device match the one we've booted from? */
24797147Staylor 	if (bootpath) {
24807147Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
24817147Staylor 		return (EINVAL);
24827147Staylor 	}
24837147Staylor out:
24847147Staylor 	*bestconf = conf;
24857147Staylor 	return (0);
24867147Staylor }
24877147Staylor 
24886423Sgw25295 /*
24896423Sgw25295  * Import a root pool.
24906423Sgw25295  *
24917147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
24927147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
24937147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
24946423Sgw25295  *
24956423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
24966423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
24976423Sgw25295  * e.g.
24986423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
24996423Sgw25295  */
25006423Sgw25295 int
25017147Staylor spa_import_rootpool(char *devpath, char *devid)
25026423Sgw25295 {
25036423Sgw25295 	nvlist_t *conf = NULL;
25046423Sgw25295 	char *pname;
25056423Sgw25295 	int error;
25066423Sgw25295 
25076423Sgw25295 	/*
25086423Sgw25295 	 * Get the vdev pathname and configuation from the most
25096423Sgw25295 	 * recently updated vdev (highest txg).
25106423Sgw25295 	 */
25117147Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
25126423Sgw25295 		goto msg_out;
25136423Sgw25295 
25146423Sgw25295 	/*
25156423Sgw25295 	 * Add type "root" vdev to the config.
25166423Sgw25295 	 */
25176423Sgw25295 	spa_build_rootpool_config(conf);
25186423Sgw25295 
25196423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
25206423Sgw25295 
25216673Seschrock 	/*
25226673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
25236673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
25246673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
25256673Seschrock 	 * pool open, not import.
25266673Seschrock 	 */
25276673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
25287897SLin.Ling@Sun.COM 	ASSERT(error != EEXIST);
25296423Sgw25295 
25306423Sgw25295 	nvlist_free(conf);
25316423Sgw25295 	return (error);
25326423Sgw25295 
25336423Sgw25295 msg_out:
25347147Staylor 	cmn_err(CE_NOTE, "\n"
25356423Sgw25295 	    "  ***************************************************  \n"
25366423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
25376423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
25386423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
25397147Staylor 	    "  ***************************************************  ");
25406423Sgw25295 
25416423Sgw25295 	return (error);
25426423Sgw25295 }
25436423Sgw25295 #endif
25446423Sgw25295 
25456423Sgw25295 /*
25466423Sgw25295  * Import a non-root pool into the system.
25476423Sgw25295  */
25486423Sgw25295 int
25496423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
25506423Sgw25295 {
25516643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
25526423Sgw25295 }
25536423Sgw25295 
25546643Seschrock int
25556643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
25566643Seschrock {
25576643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
25586643Seschrock }
25596643Seschrock 
25606643Seschrock 
2561789Sahrens /*
2562789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2563789Sahrens  * to get the vdev stats associated with the imported devices.
2564789Sahrens  */
2565789Sahrens #define	TRYIMPORT_NAME	"$import"
2566789Sahrens 
2567789Sahrens nvlist_t *
2568789Sahrens spa_tryimport(nvlist_t *tryconfig)
2569789Sahrens {
2570789Sahrens 	nvlist_t *config = NULL;
2571789Sahrens 	char *poolname;
2572789Sahrens 	spa_t *spa;
2573789Sahrens 	uint64_t state;
25748680SLin.Ling@Sun.COM 	int error;
2575789Sahrens 
2576789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2577789Sahrens 		return (NULL);
2578789Sahrens 
2579789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2580789Sahrens 		return (NULL);
2581789Sahrens 
25821635Sbonwick 	/*
25831635Sbonwick 	 * Create and initialize the spa structure.
25841635Sbonwick 	 */
2585789Sahrens 	mutex_enter(&spa_namespace_lock);
25861635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
25878241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
2588789Sahrens 
2589789Sahrens 	/*
25901635Sbonwick 	 * Pass off the heavy lifting to spa_load().
25911732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
25921732Sbonwick 	 * is actually the one to trust when doing an import.
2593789Sahrens 	 */
25948680SLin.Ling@Sun.COM 	error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2595789Sahrens 
2596789Sahrens 	/*
2597789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2598789Sahrens 	 */
2599789Sahrens 	if (spa->spa_root_vdev != NULL) {
2600789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2601789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2602789Sahrens 		    poolname) == 0);
2603789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2604789Sahrens 		    state) == 0);
26053975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
26063975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
26072082Seschrock 
26082082Seschrock 		/*
26096423Sgw25295 		 * If the bootfs property exists on this pool then we
26106423Sgw25295 		 * copy it out so that external consumers can tell which
26116423Sgw25295 		 * pools are bootable.
26126423Sgw25295 		 */
26138680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
26146423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
26156423Sgw25295 
26166423Sgw25295 			/*
26176423Sgw25295 			 * We have to play games with the name since the
26186423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
26196423Sgw25295 			 */
26207754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
26216423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
26226423Sgw25295 				char *cp;
26236423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
26246423Sgw25295 
26256423Sgw25295 				cp = strchr(tmpname, '/');
26266423Sgw25295 				if (cp == NULL) {
26276423Sgw25295 					(void) strlcpy(dsname, tmpname,
26286423Sgw25295 					    MAXPATHLEN);
26296423Sgw25295 				} else {
26306423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
26316423Sgw25295 					    "%s/%s", poolname, ++cp);
26326423Sgw25295 				}
26336423Sgw25295 				VERIFY(nvlist_add_string(config,
26346423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
26356423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
26366423Sgw25295 			}
26376423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
26386423Sgw25295 		}
26396423Sgw25295 
26406423Sgw25295 		/*
26415450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
26422082Seschrock 		 */
26432082Seschrock 		spa_add_spares(spa, config);
26445450Sbrendan 		spa_add_l2cache(spa, config);
2645789Sahrens 	}
2646789Sahrens 
2647789Sahrens 	spa_unload(spa);
2648789Sahrens 	spa_deactivate(spa);
2649789Sahrens 	spa_remove(spa);
2650789Sahrens 	mutex_exit(&spa_namespace_lock);
2651789Sahrens 
2652789Sahrens 	return (config);
2653789Sahrens }
2654789Sahrens 
2655789Sahrens /*
2656789Sahrens  * Pool export/destroy
2657789Sahrens  *
2658789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2659789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2660789Sahrens  * update the pool state and sync all the labels to disk, removing the
26618211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
26628211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
2663789Sahrens  */
2664789Sahrens static int
26657214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
26668211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
2667789Sahrens {
2668789Sahrens 	spa_t *spa;
2669789Sahrens 
26701775Sbillm 	if (oldconfig)
26711775Sbillm 		*oldconfig = NULL;
26721775Sbillm 
26738241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
2674789Sahrens 		return (EROFS);
2675789Sahrens 
2676789Sahrens 	mutex_enter(&spa_namespace_lock);
2677789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2678789Sahrens 		mutex_exit(&spa_namespace_lock);
2679789Sahrens 		return (ENOENT);
2680789Sahrens 	}
2681789Sahrens 
2682789Sahrens 	/*
26831544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
26841544Seschrock 	 * reacquire the namespace lock, and see if we can export.
26851544Seschrock 	 */
26861544Seschrock 	spa_open_ref(spa, FTAG);
26871544Seschrock 	mutex_exit(&spa_namespace_lock);
26881544Seschrock 	spa_async_suspend(spa);
26891544Seschrock 	mutex_enter(&spa_namespace_lock);
26901544Seschrock 	spa_close(spa, FTAG);
26911544Seschrock 
26921544Seschrock 	/*
2693789Sahrens 	 * The pool will be in core if it's openable,
2694789Sahrens 	 * in which case we can modify its state.
2695789Sahrens 	 */
2696789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2697789Sahrens 		/*
2698789Sahrens 		 * Objsets may be open only because they're dirty, so we
2699789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2700789Sahrens 		 */
2701789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2702789Sahrens 
27031544Seschrock 		/*
27041544Seschrock 		 * A pool cannot be exported or destroyed if there are active
27051544Seschrock 		 * references.  If we are resetting a pool, allow references by
27061544Seschrock 		 * fault injection handlers.
27071544Seschrock 		 */
27081544Seschrock 		if (!spa_refcount_zero(spa) ||
27091544Seschrock 		    (spa->spa_inject_ref != 0 &&
27101544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
27111544Seschrock 			spa_async_resume(spa);
2712789Sahrens 			mutex_exit(&spa_namespace_lock);
2713789Sahrens 			return (EBUSY);
2714789Sahrens 		}
2715789Sahrens 
2716789Sahrens 		/*
27177214Slling 		 * A pool cannot be exported if it has an active shared spare.
27187214Slling 		 * This is to prevent other pools stealing the active spare
27197214Slling 		 * from an exported pool. At user's own will, such pool can
27207214Slling 		 * be forcedly exported.
27217214Slling 		 */
27227214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
27237214Slling 		    spa_has_active_shared_spare(spa)) {
27247214Slling 			spa_async_resume(spa);
27257214Slling 			mutex_exit(&spa_namespace_lock);
27267214Slling 			return (EXDEV);
27277214Slling 		}
27287214Slling 
27297214Slling 		/*
2730789Sahrens 		 * We want this to be reflected on every label,
2731789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2732789Sahrens 		 * final sync that pushes these changes out.
2733789Sahrens 		 */
27348211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
27357754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27361544Seschrock 			spa->spa_state = new_state;
27371635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
27381544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
27397754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
27401544Seschrock 		}
2741789Sahrens 	}
2742789Sahrens 
27434451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
27444451Seschrock 
2745789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2746789Sahrens 		spa_unload(spa);
2747789Sahrens 		spa_deactivate(spa);
2748789Sahrens 	}
2749789Sahrens 
27501775Sbillm 	if (oldconfig && spa->spa_config)
27511775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
27521775Sbillm 
27531544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
27548211SGeorge.Wilson@Sun.COM 		if (!hardforce)
27558211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
27561544Seschrock 		spa_remove(spa);
27571544Seschrock 	}
2758789Sahrens 	mutex_exit(&spa_namespace_lock);
2759789Sahrens 
2760789Sahrens 	return (0);
2761789Sahrens }
2762789Sahrens 
2763789Sahrens /*
2764789Sahrens  * Destroy a storage pool.
2765789Sahrens  */
2766789Sahrens int
2767789Sahrens spa_destroy(char *pool)
2768789Sahrens {
27698211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
27708211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
2771789Sahrens }
2772789Sahrens 
2773789Sahrens /*
2774789Sahrens  * Export a storage pool.
2775789Sahrens  */
2776789Sahrens int
27778211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
27788211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
2779789Sahrens {
27808211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
27818211SGeorge.Wilson@Sun.COM 	    force, hardforce));
2782789Sahrens }
2783789Sahrens 
2784789Sahrens /*
27851544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
27861544Seschrock  * from the namespace in any way.
27871544Seschrock  */
27881544Seschrock int
27891544Seschrock spa_reset(char *pool)
27901544Seschrock {
27917214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
27928211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
27931544Seschrock }
27941544Seschrock 
27951544Seschrock /*
2796789Sahrens  * ==========================================================================
2797789Sahrens  * Device manipulation
2798789Sahrens  * ==========================================================================
2799789Sahrens  */
2800789Sahrens 
2801789Sahrens /*
28024527Sperrin  * Add a device to a storage pool.
2803789Sahrens  */
2804789Sahrens int
2805789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2806789Sahrens {
2807789Sahrens 	uint64_t txg;
28088241SJeff.Bonwick@Sun.COM 	int error;
2809789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
28101585Sbonwick 	vdev_t *vd, *tvd;
28115450Sbrendan 	nvlist_t **spares, **l2cache;
28125450Sbrendan 	uint_t nspares, nl2cache;
2813789Sahrens 
2814789Sahrens 	txg = spa_vdev_enter(spa);
2815789Sahrens 
28162082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
28172082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
28182082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
28192082Seschrock 
28207754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
2821789Sahrens 
28225450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
28235450Sbrendan 	    &nspares) != 0)
28242082Seschrock 		nspares = 0;
28252082Seschrock 
28265450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
28275450Sbrendan 	    &nl2cache) != 0)
28285450Sbrendan 		nl2cache = 0;
28295450Sbrendan 
28307754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
28312082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
28327754SJeff.Bonwick@Sun.COM 
28337754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
28347754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
28357754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
28362082Seschrock 
28373377Seschrock 	/*
28385450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
28395450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
28403377Seschrock 	 */
28417754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
28423377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
28433377Seschrock 
28443377Seschrock 	/*
28453377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
28463377Seschrock 	 */
28478241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
28483377Seschrock 		tvd = vd->vdev_child[c];
28493377Seschrock 		vdev_remove_child(vd, tvd);
28503377Seschrock 		tvd->vdev_id = rvd->vdev_children;
28513377Seschrock 		vdev_add_child(rvd, tvd);
28523377Seschrock 		vdev_config_dirty(tvd);
28533377Seschrock 	}
28543377Seschrock 
28552082Seschrock 	if (nspares != 0) {
28565450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
28575450Sbrendan 		    ZPOOL_CONFIG_SPARES);
28582082Seschrock 		spa_load_spares(spa);
28595450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
28605450Sbrendan 	}
28615450Sbrendan 
28625450Sbrendan 	if (nl2cache != 0) {
28635450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
28645450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
28655450Sbrendan 		spa_load_l2cache(spa);
28665450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2867789Sahrens 	}
2868789Sahrens 
2869789Sahrens 	/*
28701585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
28711585Sbonwick 	 * If other threads start allocating from these vdevs before we
28721585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
28731585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
28741585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
28751585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
28761635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
28771585Sbonwick 	 *
28781585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
28791585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
28801585Sbonwick 	 * steps will be completed the next time we load the pool.
2881789Sahrens 	 */
28821635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
28831585Sbonwick 
28841635Sbonwick 	mutex_enter(&spa_namespace_lock);
28851635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
28861635Sbonwick 	mutex_exit(&spa_namespace_lock);
2887789Sahrens 
28881635Sbonwick 	return (0);
2889789Sahrens }
2890789Sahrens 
2891789Sahrens /*
2892789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2893789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2894789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2895789Sahrens  *
2896789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2897789Sahrens  * existing device; in this case the two devices are made into their own
28984451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2899789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2900789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2901789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2902789Sahrens  * is automatically detached.
2903789Sahrens  */
2904789Sahrens int
29051544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2906789Sahrens {
2907789Sahrens 	uint64_t txg, open_txg;
2908789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2909789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
29102082Seschrock 	vdev_ops_t *pvops;
29117313SEric.Kustarz@Sun.COM 	dmu_tx_t *tx;
29127313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
29137313SEric.Kustarz@Sun.COM 	int newvd_isspare;
29147313SEric.Kustarz@Sun.COM 	int error;
2915789Sahrens 
2916789Sahrens 	txg = spa_vdev_enter(spa);
2917789Sahrens 
29186643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2919789Sahrens 
2920789Sahrens 	if (oldvd == NULL)
2921789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2922789Sahrens 
29231585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
29241585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29251585Sbonwick 
2926789Sahrens 	pvd = oldvd->vdev_parent;
2927789Sahrens 
29282082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
29294451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
29304451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
29314451Seschrock 
29324451Seschrock 	if (newrootvd->vdev_children != 1)
2933789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2934789Sahrens 
2935789Sahrens 	newvd = newrootvd->vdev_child[0];
2936789Sahrens 
2937789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2938789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2939789Sahrens 
29402082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2941789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2942789Sahrens 
29434527Sperrin 	/*
29444527Sperrin 	 * Spares can't replace logs
29454527Sperrin 	 */
29467326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
29474527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29484527Sperrin 
29492082Seschrock 	if (!replacing) {
29502082Seschrock 		/*
29512082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
29522082Seschrock 		 * vdev.
29532082Seschrock 		 */
29542082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
29552082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
29562082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29572082Seschrock 
29582082Seschrock 		pvops = &vdev_mirror_ops;
29592082Seschrock 	} else {
29602082Seschrock 		/*
29612082Seschrock 		 * Active hot spares can only be replaced by inactive hot
29622082Seschrock 		 * spares.
29632082Seschrock 		 */
29642082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
29652082Seschrock 		    pvd->vdev_child[1] == oldvd &&
29662082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
29672082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29682082Seschrock 
29692082Seschrock 		/*
29702082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
29712082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
29723377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
29733377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
29743377Seschrock 		 * the same (spare replaces spare, non-spare replaces
29753377Seschrock 		 * non-spare).
29762082Seschrock 		 */
29772082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
29782082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29793377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
29803377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
29813377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29822082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
29832082Seschrock 		    newvd->vdev_isspare)
29842082Seschrock 			pvops = &vdev_spare_ops;
29852082Seschrock 		else
29862082Seschrock 			pvops = &vdev_replacing_ops;
29872082Seschrock 	}
29882082Seschrock 
29891175Slling 	/*
29901175Slling 	 * Compare the new device size with the replaceable/attachable
29911175Slling 	 * device size.
29921175Slling 	 */
29931175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2994789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2995789Sahrens 
29961732Sbonwick 	/*
29971732Sbonwick 	 * The new device cannot have a higher alignment requirement
29981732Sbonwick 	 * than the top-level vdev.
29991732Sbonwick 	 */
30001732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3001789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3002789Sahrens 
3003789Sahrens 	/*
3004789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3005789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3006789Sahrens 	 */
3007789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3008789Sahrens 		spa_strfree(oldvd->vdev_path);
3009789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3010789Sahrens 		    KM_SLEEP);
3011789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3012789Sahrens 		    newvd->vdev_path, "old");
3013789Sahrens 		if (oldvd->vdev_devid != NULL) {
3014789Sahrens 			spa_strfree(oldvd->vdev_devid);
3015789Sahrens 			oldvd->vdev_devid = NULL;
3016789Sahrens 		}
3017789Sahrens 	}
3018789Sahrens 
3019789Sahrens 	/*
30202082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
30212082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3022789Sahrens 	 */
3023789Sahrens 	if (pvd->vdev_ops != pvops)
3024789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3025789Sahrens 
3026789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3027789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3028789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3029789Sahrens 
3030789Sahrens 	/*
3031789Sahrens 	 * Extract the new device from its root and add it to pvd.
3032789Sahrens 	 */
3033789Sahrens 	vdev_remove_child(newrootvd, newvd);
3034789Sahrens 	newvd->vdev_id = pvd->vdev_children;
3035789Sahrens 	vdev_add_child(pvd, newvd);
3036789Sahrens 
30371544Seschrock 	/*
30381544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
30391544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
30401544Seschrock 	 */
30411544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
30421544Seschrock 
3043789Sahrens 	tvd = newvd->vdev_top;
3044789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3045789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3046789Sahrens 
3047789Sahrens 	vdev_config_dirty(tvd);
3048789Sahrens 
3049789Sahrens 	/*
3050789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3051789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3052789Sahrens 	 */
3053789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3054789Sahrens 
30558241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
30568241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3057789Sahrens 
30583377Seschrock 	if (newvd->vdev_isspare)
30593377Seschrock 		spa_spare_activate(newvd);
30607754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
30617754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
30627313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
30631544Seschrock 
3064789Sahrens 	/*
3065789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3066789Sahrens 	 */
30671732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3068789Sahrens 
3069789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3070789Sahrens 
30717313SEric.Kustarz@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
30727313SEric.Kustarz@Sun.COM 	if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
30737313SEric.Kustarz@Sun.COM 		spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
30747313SEric.Kustarz@Sun.COM 		    CRED(),  "%s vdev=%s %s vdev=%s",
30757313SEric.Kustarz@Sun.COM 		    replacing && newvd_isspare ? "spare in" :
30767313SEric.Kustarz@Sun.COM 		    replacing ? "replace" : "attach", newvdpath,
30777313SEric.Kustarz@Sun.COM 		    replacing ? "for" : "to", oldvdpath);
30787313SEric.Kustarz@Sun.COM 		dmu_tx_commit(tx);
30797313SEric.Kustarz@Sun.COM 	} else {
30807313SEric.Kustarz@Sun.COM 		dmu_tx_abort(tx);
30817313SEric.Kustarz@Sun.COM 	}
30827313SEric.Kustarz@Sun.COM 
30837313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
30847313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
30857313SEric.Kustarz@Sun.COM 
3086789Sahrens 	/*
30877046Sahrens 	 * Kick off a resilver to update newvd.
3088789Sahrens 	 */
30897046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3090789Sahrens 
3091789Sahrens 	return (0);
3092789Sahrens }
3093789Sahrens 
3094789Sahrens /*
3095789Sahrens  * Detach a device from a mirror or replacing vdev.
3096789Sahrens  * If 'replace_done' is specified, only detach if the parent
3097789Sahrens  * is a replacing vdev.
3098789Sahrens  */
3099789Sahrens int
31008241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3101789Sahrens {
3102789Sahrens 	uint64_t txg;
31038241SJeff.Bonwick@Sun.COM 	int error;
3104789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3105789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
31062082Seschrock 	boolean_t unspare = B_FALSE;
31072082Seschrock 	uint64_t unspare_guid;
31086673Seschrock 	size_t len;
3109789Sahrens 
3110789Sahrens 	txg = spa_vdev_enter(spa);
3111789Sahrens 
31126643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3113789Sahrens 
3114789Sahrens 	if (vd == NULL)
3115789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3116789Sahrens 
31171585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
31181585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31191585Sbonwick 
3120789Sahrens 	pvd = vd->vdev_parent;
3121789Sahrens 
3122789Sahrens 	/*
31238241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
31248241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
31258241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
31268241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
31278241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
31288241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
31298241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
31308241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
31318241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
31328241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
31338241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
31348241SJeff.Bonwick@Sun.COM 	 */
31358241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
31368241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
31378241SJeff.Bonwick@Sun.COM 
31388241SJeff.Bonwick@Sun.COM 	/*
3139789Sahrens 	 * If replace_done is specified, only remove this device if it's
31402082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
31412082Seschrock 	 * disk can be removed.
3142789Sahrens 	 */
31432082Seschrock 	if (replace_done) {
31442082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
31452082Seschrock 			if (vd->vdev_id != 0)
31462082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31472082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
31482082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31492082Seschrock 		}
31502082Seschrock 	}
31512082Seschrock 
31522082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
31534577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3154789Sahrens 
3155789Sahrens 	/*
31562082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3157789Sahrens 	 */
3158789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
31592082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
31602082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3161789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3162789Sahrens 
3163789Sahrens 	/*
31648241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
31658241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3166789Sahrens 	 */
31678241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3168789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3169789Sahrens 
31708241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
31718241SJeff.Bonwick@Sun.COM 
3172789Sahrens 	/*
31736673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
31746673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
31756673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
31766673Seschrock 	 */
31776673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
31786673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
31796673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
31806673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
31816673Seschrock 		cvd = pvd->vdev_child[0];
31826673Seschrock 		len = strlen(vd->vdev_path);
31836673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
31846673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
31856673Seschrock 			spa_strfree(cvd->vdev_path);
31866673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
31876673Seschrock 		}
31886673Seschrock 	}
31896673Seschrock 
31906673Seschrock 	/*
31912082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
31922082Seschrock 	 * that the spare should become a real disk, and be removed from the
31932082Seschrock 	 * active spare list for the pool.
31942082Seschrock 	 */
31952082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
31968241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
31972082Seschrock 		unspare = B_TRUE;
31982082Seschrock 
31992082Seschrock 	/*
3200789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3201789Sahrens 	 * This must be done after all other error cases are handled,
3202789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3203789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3204789Sahrens 	 * it may be that the unwritability of the disk is the reason
3205789Sahrens 	 * it's being detached!
3206789Sahrens 	 */
32073377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3208789Sahrens 
3209789Sahrens 	/*
3210789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3211789Sahrens 	 */
3212789Sahrens 	vdev_remove_child(pvd, vd);
3213789Sahrens 	vdev_compact_children(pvd);
3214789Sahrens 
3215789Sahrens 	/*
3216789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3217789Sahrens 	 */
3218789Sahrens 	cvd = pvd->vdev_child[0];
3219789Sahrens 
3220789Sahrens 	/*
32212082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
32228241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
32238241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
32248241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
32258241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
32268241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
32278241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
32282082Seschrock 	 */
32292082Seschrock 	if (unspare) {
32302082Seschrock 		ASSERT(cvd->vdev_isspare);
32313377Seschrock 		spa_spare_remove(cvd);
32322082Seschrock 		unspare_guid = cvd->vdev_guid;
32338241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
32342082Seschrock 	}
32352082Seschrock 
32362082Seschrock 	/*
3237789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3238789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3239789Sahrens 	 */
3240789Sahrens 	if (pvd->vdev_children == 1)
3241789Sahrens 		vdev_remove_parent(cvd);
3242789Sahrens 
3243789Sahrens 	/*
3244789Sahrens 	 * We don't set tvd until now because the parent we just removed
3245789Sahrens 	 * may have been the previous top-level vdev.
3246789Sahrens 	 */
3247789Sahrens 	tvd = cvd->vdev_top;
3248789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3249789Sahrens 
3250789Sahrens 	/*
32513377Seschrock 	 * Reevaluate the parent vdev state.
3252789Sahrens 	 */
32534451Seschrock 	vdev_propagate_state(cvd);
3254789Sahrens 
3255789Sahrens 	/*
32563377Seschrock 	 * If the device we just detached was smaller than the others, it may be
32573377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
32583377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
32593377Seschrock 	 * there's nothing to read from disk.
3260789Sahrens 	 */
32611732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3262789Sahrens 
3263789Sahrens 	vdev_config_dirty(tvd);
3264789Sahrens 
3265789Sahrens 	/*
32663377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
32673377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
32683377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
32693377Seschrock 	 * prevent vd from being accessed after it's freed.
3270789Sahrens 	 */
32718241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3272789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
32731732Sbonwick 	vd->vdev_detached = B_TRUE;
32741732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3275789Sahrens 
32764451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
32774451Seschrock 
32782082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
32792082Seschrock 
32802082Seschrock 	/*
32813377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
32823377Seschrock 	 * then we want to go through and remove the device from the hot spare
32833377Seschrock 	 * list of every other pool.
32842082Seschrock 	 */
32852082Seschrock 	if (unspare) {
32868241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
32872082Seschrock 		spa = NULL;
32882082Seschrock 		mutex_enter(&spa_namespace_lock);
32892082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
32902082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
32912082Seschrock 				continue;
32928241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
32938241SJeff.Bonwick@Sun.COM 				continue;
32947793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
32957793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
32962082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
32977793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
32987793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
32992082Seschrock 		}
33002082Seschrock 		mutex_exit(&spa_namespace_lock);
33012082Seschrock 	}
33022082Seschrock 
33032082Seschrock 	return (error);
33042082Seschrock }
33052082Seschrock 
33067754SJeff.Bonwick@Sun.COM static nvlist_t *
33077754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
33082082Seschrock {
33097754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
33107754SJeff.Bonwick@Sun.COM 		uint64_t guid;
33117754SJeff.Bonwick@Sun.COM 
33127754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
33137754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
33147754SJeff.Bonwick@Sun.COM 
33157754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
33167754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
33172082Seschrock 	}
33182082Seschrock 
33197754SJeff.Bonwick@Sun.COM 	return (NULL);
33205450Sbrendan }
33215450Sbrendan 
33227754SJeff.Bonwick@Sun.COM static void
33237754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
33247754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
33255450Sbrendan {
33267754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
33277754SJeff.Bonwick@Sun.COM 
33287754SJeff.Bonwick@Sun.COM 	if (count > 1)
33297754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
33307754SJeff.Bonwick@Sun.COM 
33317754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
33327754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
33337754SJeff.Bonwick@Sun.COM 			continue;
33347754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
33355450Sbrendan 	}
33365450Sbrendan 
33377754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
33387754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
33397754SJeff.Bonwick@Sun.COM 
33407754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
33417754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
33427754SJeff.Bonwick@Sun.COM 
33437754SJeff.Bonwick@Sun.COM 	if (count > 1)
33447754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
33455450Sbrendan }
33465450Sbrendan 
33475450Sbrendan /*
33485450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
33495450Sbrendan  * spares and level 2 ARC devices.
33505450Sbrendan  */
33515450Sbrendan int
33525450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
33535450Sbrendan {
33545450Sbrendan 	vdev_t *vd;
33557754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
33565450Sbrendan 	uint_t nspares, nl2cache;
33578241SJeff.Bonwick@Sun.COM 	uint64_t txg = 0;
33585450Sbrendan 	int error = 0;
33598241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
33608241SJeff.Bonwick@Sun.COM 
33618241SJeff.Bonwick@Sun.COM 	if (!locked)
33628241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
33635450Sbrendan 
33646643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
33655450Sbrendan 
33665450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
33675450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
33687754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
33697754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
33707754SJeff.Bonwick@Sun.COM 		/*
33717754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
33727754SJeff.Bonwick@Sun.COM 		 * in this pool.
33737754SJeff.Bonwick@Sun.COM 		 */
33747754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
33757754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
33767754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
33777754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
33787754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
33797754SJeff.Bonwick@Sun.COM 		} else {
33807754SJeff.Bonwick@Sun.COM 			error = EBUSY;
33817754SJeff.Bonwick@Sun.COM 		}
33827754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
33835450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
33847754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
33857754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
33867754SJeff.Bonwick@Sun.COM 		/*
33877754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
33887754SJeff.Bonwick@Sun.COM 		 */
33897754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
33907754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
33915450Sbrendan 		spa_load_l2cache(spa);
33925450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
33937754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
33947754SJeff.Bonwick@Sun.COM 		/*
33957754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
33967754SJeff.Bonwick@Sun.COM 		 */
33977754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
33987754SJeff.Bonwick@Sun.COM 	} else {
33997754SJeff.Bonwick@Sun.COM 		/*
34007754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
34017754SJeff.Bonwick@Sun.COM 		 */
34027754SJeff.Bonwick@Sun.COM 		error = ENOENT;
34035450Sbrendan 	}
34042082Seschrock 
34058241SJeff.Bonwick@Sun.COM 	if (!locked)
34068241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
34078241SJeff.Bonwick@Sun.COM 
34088241SJeff.Bonwick@Sun.COM 	return (error);
3409789Sahrens }
3410789Sahrens 
3411789Sahrens /*
34124451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
34134451Seschrock  * current spared, so we can detach it.
3414789Sahrens  */
34151544Seschrock static vdev_t *
34164451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3417789Sahrens {
34181544Seschrock 	vdev_t *newvd, *oldvd;
3419789Sahrens 	int c;
3420789Sahrens 
34211544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
34224451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
34231544Seschrock 		if (oldvd != NULL)
34241544Seschrock 			return (oldvd);
34251544Seschrock 	}
3426789Sahrens 
34274451Seschrock 	/*
34284451Seschrock 	 * Check for a completed replacement.
34294451Seschrock 	 */
3430789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
34311544Seschrock 		oldvd = vd->vdev_child[0];
34321544Seschrock 		newvd = vd->vdev_child[1];
3433789Sahrens 
34348241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
34358241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
34361544Seschrock 			return (oldvd);
34371544Seschrock 	}
3438789Sahrens 
34394451Seschrock 	/*
34404451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
34414451Seschrock 	 */
34424451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
34434451Seschrock 		newvd = vd->vdev_child[0];
34444451Seschrock 		oldvd = vd->vdev_child[1];
34454451Seschrock 
34464451Seschrock 		if (newvd->vdev_unspare &&
34478241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
34488241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
34494451Seschrock 			newvd->vdev_unspare = 0;
34504451Seschrock 			return (oldvd);
34514451Seschrock 		}
34524451Seschrock 	}
34534451Seschrock 
34541544Seschrock 	return (NULL);
3455789Sahrens }
3456789Sahrens 
34571544Seschrock static void
34584451Seschrock spa_vdev_resilver_done(spa_t *spa)
3459789Sahrens {
34608241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
34618241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
34628241SJeff.Bonwick@Sun.COM 
34638241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3464789Sahrens 
34654451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
34668241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
34678241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
34681544Seschrock 		guid = vd->vdev_guid;
34698241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
34708241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
34718241SJeff.Bonwick@Sun.COM 		sguid = 0;
34722082Seschrock 		/*
34732082Seschrock 		 * If we have just finished replacing a hot spared device, then
34742082Seschrock 		 * we need to detach the parent's first child (the original hot
34752082Seschrock 		 * spare) as well.
34762082Seschrock 		 */
34778241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
34782082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
34798241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
34808241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
34812082Seschrock 		}
34828241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
34838241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
34841544Seschrock 			return;
34858241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
34862082Seschrock 			return;
34878241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3488789Sahrens 	}
3489789Sahrens 
34908241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
3491789Sahrens }
3492789Sahrens 
3493789Sahrens /*
34941354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
34951354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
34961354Seschrock  */
34971354Seschrock int
34981354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
34991354Seschrock {
35006643Seschrock 	vdev_t *vd;
35011354Seschrock 	uint64_t txg;
35021354Seschrock 
35031354Seschrock 	txg = spa_vdev_enter(spa);
35041354Seschrock 
35056643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
35062082Seschrock 		/*
35076643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
35086643Seschrock 		 * it is, update the path manually as there is no associated
35096643Seschrock 		 * vdev_t that can be synced to disk.
35102082Seschrock 		 */
35116643Seschrock 		nvlist_t **spares;
35126643Seschrock 		uint_t i, nspares;
35135450Sbrendan 
35145450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
35155450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
35165450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
35175450Sbrendan 			    &spares, &nspares) == 0);
35182082Seschrock 			for (i = 0; i < nspares; i++) {
35192082Seschrock 				uint64_t theguid;
35202082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
35212082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
35225450Sbrendan 				if (theguid == guid) {
35235450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
35245450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
35255450Sbrendan 					spa_load_spares(spa);
35265450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
35275450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
35285450Sbrendan 					    0));
35295450Sbrendan 				}
35302082Seschrock 			}
35312082Seschrock 		}
35325450Sbrendan 
35335450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
35342082Seschrock 	}
35351354Seschrock 
35361585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
35371585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35381585Sbonwick 
35391354Seschrock 	spa_strfree(vd->vdev_path);
35401354Seschrock 	vd->vdev_path = spa_strdup(newpath);
35411354Seschrock 
35421354Seschrock 	vdev_config_dirty(vd->vdev_top);
35431354Seschrock 
35441354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
35451354Seschrock }
35461354Seschrock 
35471354Seschrock /*
3548789Sahrens  * ==========================================================================
3549789Sahrens  * SPA Scrubbing
3550789Sahrens  * ==========================================================================
3551789Sahrens  */
3552789Sahrens 
35537046Sahrens int
35547046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3555789Sahrens {
35567754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
35574808Sek110237 
3558789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3559789Sahrens 		return (ENOTSUP);
3560789Sahrens 
3561789Sahrens 	/*
35627046Sahrens 	 * If a resilver was requested, but there is no DTL on a
35637046Sahrens 	 * writeable leaf device, we have nothing to do.
3564789Sahrens 	 */
35657046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
35667046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
35677046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
35681544Seschrock 		return (0);
35691544Seschrock 	}
3570789Sahrens 
35717046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
35727046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
35737046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
35747046Sahrens 		return (EBUSY);
35757046Sahrens 
35767046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
35777046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
35787046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
35797046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
35801544Seschrock 	} else {
35817046Sahrens 		return (EINVAL);
35821544Seschrock 	}
3583789Sahrens }
3584789Sahrens 
35851544Seschrock /*
35861544Seschrock  * ==========================================================================
35871544Seschrock  * SPA async task processing
35881544Seschrock  * ==========================================================================
35891544Seschrock  */
35901544Seschrock 
35911544Seschrock static void
35924451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3593789Sahrens {
35947361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
35957361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
35967361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
35977754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd);
35987754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
35991544Seschrock 	}
36007361SBrendan.Gregg@Sun.COM 
36017754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
36027361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
36031544Seschrock }
36041544Seschrock 
36051544Seschrock static void
36067754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
36077754SJeff.Bonwick@Sun.COM {
36087754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
36097754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
36107754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
36117754SJeff.Bonwick@Sun.COM 	}
36127754SJeff.Bonwick@Sun.COM 
36137754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
36147754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
36157754SJeff.Bonwick@Sun.COM }
36167754SJeff.Bonwick@Sun.COM 
36177754SJeff.Bonwick@Sun.COM static void
36181544Seschrock spa_async_thread(spa_t *spa)
36191544Seschrock {
36207754SJeff.Bonwick@Sun.COM 	int tasks;
36211544Seschrock 
36221544Seschrock 	ASSERT(spa->spa_sync_on);
3623789Sahrens 
36241544Seschrock 	mutex_enter(&spa->spa_async_lock);
36251544Seschrock 	tasks = spa->spa_async_tasks;
36261544Seschrock 	spa->spa_async_tasks = 0;
36271544Seschrock 	mutex_exit(&spa->spa_async_lock);
36281544Seschrock 
36291544Seschrock 	/*
36301635Sbonwick 	 * See if the config needs to be updated.
36311635Sbonwick 	 */
36321635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
36331635Sbonwick 		mutex_enter(&spa_namespace_lock);
36341635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36351635Sbonwick 		mutex_exit(&spa_namespace_lock);
36361635Sbonwick 	}
36371635Sbonwick 
36381635Sbonwick 	/*
36394451Seschrock 	 * See if any devices need to be marked REMOVED.
36401544Seschrock 	 */
36417754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
36427754SJeff.Bonwick@Sun.COM 		spa_vdev_state_enter(spa);
36434451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
36447754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
36457361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
36467754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
36477361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
36487754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
36497754SJeff.Bonwick@Sun.COM 	}
36507754SJeff.Bonwick@Sun.COM 
36517754SJeff.Bonwick@Sun.COM 	/*
36527754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
36537754SJeff.Bonwick@Sun.COM 	 */
36547754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
36557754SJeff.Bonwick@Sun.COM 		spa_vdev_state_enter(spa);
36567754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
36577754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
36584451Seschrock 	}
36591544Seschrock 
36601544Seschrock 	/*
36611544Seschrock 	 * If any devices are done replacing, detach them.
36621544Seschrock 	 */
36634451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
36644451Seschrock 		spa_vdev_resilver_done(spa);
3665789Sahrens 
36661544Seschrock 	/*
36671544Seschrock 	 * Kick off a resilver.
36681544Seschrock 	 */
36697046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
36707046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
36711544Seschrock 
36721544Seschrock 	/*
36731544Seschrock 	 * Let the world know that we're done.
36741544Seschrock 	 */
36751544Seschrock 	mutex_enter(&spa->spa_async_lock);
36761544Seschrock 	spa->spa_async_thread = NULL;
36771544Seschrock 	cv_broadcast(&spa->spa_async_cv);
36781544Seschrock 	mutex_exit(&spa->spa_async_lock);
36791544Seschrock 	thread_exit();
36801544Seschrock }
36811544Seschrock 
36821544Seschrock void
36831544Seschrock spa_async_suspend(spa_t *spa)
36841544Seschrock {
36851544Seschrock 	mutex_enter(&spa->spa_async_lock);
36861544Seschrock 	spa->spa_async_suspended++;
36871544Seschrock 	while (spa->spa_async_thread != NULL)
36881544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
36891544Seschrock 	mutex_exit(&spa->spa_async_lock);
36901544Seschrock }
36911544Seschrock 
36921544Seschrock void
36931544Seschrock spa_async_resume(spa_t *spa)
36941544Seschrock {
36951544Seschrock 	mutex_enter(&spa->spa_async_lock);
36961544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
36971544Seschrock 	spa->spa_async_suspended--;
36981544Seschrock 	mutex_exit(&spa->spa_async_lock);
36991544Seschrock }
37001544Seschrock 
37011544Seschrock static void
37021544Seschrock spa_async_dispatch(spa_t *spa)
37031544Seschrock {
37041544Seschrock 	mutex_enter(&spa->spa_async_lock);
37051544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
37061635Sbonwick 	    spa->spa_async_thread == NULL &&
37071635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
37081544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
37091544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
37101544Seschrock 	mutex_exit(&spa->spa_async_lock);
37111544Seschrock }
37121544Seschrock 
37131544Seschrock void
37141544Seschrock spa_async_request(spa_t *spa, int task)
37151544Seschrock {
37161544Seschrock 	mutex_enter(&spa->spa_async_lock);
37171544Seschrock 	spa->spa_async_tasks |= task;
37181544Seschrock 	mutex_exit(&spa->spa_async_lock);
3719789Sahrens }
3720789Sahrens 
3721789Sahrens /*
3722789Sahrens  * ==========================================================================
3723789Sahrens  * SPA syncing routines
3724789Sahrens  * ==========================================================================
3725789Sahrens  */
3726789Sahrens 
3727789Sahrens static void
3728789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3729789Sahrens {
3730789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3731789Sahrens 	dmu_tx_t *tx;
3732789Sahrens 	blkptr_t blk;
3733789Sahrens 	uint64_t itor = 0;
3734789Sahrens 	zio_t *zio;
3735789Sahrens 	int error;
3736789Sahrens 	uint8_t c = 1;
3737789Sahrens 
37387754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
37397754SJeff.Bonwick@Sun.COM 
37407754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
37417754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
37427754SJeff.Bonwick@Sun.COM 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
37437754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
37447754SJeff.Bonwick@Sun.COM 	}
3745789Sahrens 
3746789Sahrens 	error = zio_wait(zio);
3747789Sahrens 	ASSERT3U(error, ==, 0);
3748789Sahrens 
3749789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3750789Sahrens 	bplist_vacate(bpl, tx);
3751789Sahrens 
3752789Sahrens 	/*
3753789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3754789Sahrens 	 * (Usually only the first block is needed.)
3755789Sahrens 	 */
3756789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3757789Sahrens 	dmu_tx_commit(tx);
3758789Sahrens }
3759789Sahrens 
3760789Sahrens static void
37612082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
37622082Seschrock {
37632082Seschrock 	char *packed = NULL;
37647497STim.Haley@Sun.COM 	size_t bufsize;
37652082Seschrock 	size_t nvsize = 0;
37662082Seschrock 	dmu_buf_t *db;
37672082Seschrock 
37682082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
37692082Seschrock 
37707497STim.Haley@Sun.COM 	/*
37717497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
37727497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
37737497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
37747497STim.Haley@Sun.COM 	 */
37757497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
37767497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
37772082Seschrock 
37782082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
37792082Seschrock 	    KM_SLEEP) == 0);
37807497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
37817497STim.Haley@Sun.COM 
37827497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
37837497STim.Haley@Sun.COM 
37847497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
37852082Seschrock 
37862082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
37872082Seschrock 	dmu_buf_will_dirty(db, tx);
37882082Seschrock 	*(uint64_t *)db->db_data = nvsize;
37892082Seschrock 	dmu_buf_rele(db, FTAG);
37902082Seschrock }
37912082Seschrock 
37922082Seschrock static void
37935450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
37945450Sbrendan     const char *config, const char *entry)
37952082Seschrock {
37962082Seschrock 	nvlist_t *nvroot;
37975450Sbrendan 	nvlist_t **list;
37982082Seschrock 	int i;
37992082Seschrock 
38005450Sbrendan 	if (!sav->sav_sync)
38012082Seschrock 		return;
38022082Seschrock 
38032082Seschrock 	/*
38045450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
38055450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
38064451Seschrock 	 * valid and the vdevs are labeled appropriately.
38072082Seschrock 	 */
38085450Sbrendan 	if (sav->sav_object == 0) {
38095450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
38105450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
38115450Sbrendan 		    sizeof (uint64_t), tx);
38122082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
38135450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
38145450Sbrendan 		    &sav->sav_object, tx) == 0);
38152082Seschrock 	}
38162082Seschrock 
38172082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
38185450Sbrendan 	if (sav->sav_count == 0) {
38195450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
38202082Seschrock 	} else {
38215450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
38225450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
38235450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
38245450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
38255450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
38265450Sbrendan 		    sav->sav_count) == 0);
38275450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
38285450Sbrendan 			nvlist_free(list[i]);
38295450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
38302082Seschrock 	}
38312082Seschrock 
38325450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
38332926Sek110237 	nvlist_free(nvroot);
38342082Seschrock 
38355450Sbrendan 	sav->sav_sync = B_FALSE;
38362082Seschrock }
38372082Seschrock 
38382082Seschrock static void
3839789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3840789Sahrens {
3841789Sahrens 	nvlist_t *config;
3842789Sahrens 
38437754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
3844789Sahrens 		return;
3845789Sahrens 
38467754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
38477754SJeff.Bonwick@Sun.COM 
38487754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
38497754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
38507754SJeff.Bonwick@Sun.COM 
38517754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
3852789Sahrens 
38531635Sbonwick 	if (spa->spa_config_syncing)
38541635Sbonwick 		nvlist_free(spa->spa_config_syncing);
38551635Sbonwick 	spa->spa_config_syncing = config;
3856789Sahrens 
38572082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3858789Sahrens }
3859789Sahrens 
38605094Slling /*
38615094Slling  * Set zpool properties.
38625094Slling  */
38633912Slling static void
38644543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
38653912Slling {
38663912Slling 	spa_t *spa = arg1;
38675094Slling 	objset_t *mos = spa->spa_meta_objset;
38683912Slling 	nvlist_t *nvp = arg2;
38695094Slling 	nvpair_t *elem;
38704451Seschrock 	uint64_t intval;
38716643Seschrock 	char *strval;
38725094Slling 	zpool_prop_t prop;
38735094Slling 	const char *propname;
38745094Slling 	zprop_type_t proptype;
38755094Slling 
38767754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
38777754SJeff.Bonwick@Sun.COM 
38785094Slling 	elem = NULL;
38795094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
38805094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
38815094Slling 		case ZPOOL_PROP_VERSION:
38825094Slling 			/*
38835094Slling 			 * Only set version for non-zpool-creation cases
38845094Slling 			 * (set/import). spa_create() needs special care
38855094Slling 			 * for version setting.
38865094Slling 			 */
38875094Slling 			if (tx->tx_txg != TXG_INITIAL) {
38885094Slling 				VERIFY(nvpair_value_uint64(elem,
38895094Slling 				    &intval) == 0);
38905094Slling 				ASSERT(intval <= SPA_VERSION);
38915094Slling 				ASSERT(intval >= spa_version(spa));
38925094Slling 				spa->spa_uberblock.ub_version = intval;
38935094Slling 				vdev_config_dirty(spa->spa_root_vdev);
38945094Slling 			}
38955094Slling 			break;
38965094Slling 
38975094Slling 		case ZPOOL_PROP_ALTROOT:
38985094Slling 			/*
38995094Slling 			 * 'altroot' is a non-persistent property. It should
39005094Slling 			 * have been set temporarily at creation or import time.
39015094Slling 			 */
39025094Slling 			ASSERT(spa->spa_root != NULL);
39035094Slling 			break;
39045094Slling 
39055363Seschrock 		case ZPOOL_PROP_CACHEFILE:
39065094Slling 			/*
39078525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
39085094Slling 			 */
39094543Smarks 			break;
39105094Slling 		default:
39115094Slling 			/*
39125094Slling 			 * Set pool property values in the poolprops mos object.
39135094Slling 			 */
39145094Slling 			if (spa->spa_pool_props_object == 0) {
39155094Slling 				objset_t *mos = spa->spa_meta_objset;
39165094Slling 
39175094Slling 				VERIFY((spa->spa_pool_props_object =
39185094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
39195094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
39205094Slling 
39215094Slling 				VERIFY(zap_update(mos,
39225094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
39235094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
39245094Slling 				    == 0);
39255094Slling 			}
39265094Slling 
39275094Slling 			/* normalize the property name */
39285094Slling 			propname = zpool_prop_to_name(prop);
39295094Slling 			proptype = zpool_prop_get_type(prop);
39305094Slling 
39315094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
39325094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
39335094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
39345094Slling 				VERIFY(zap_update(mos,
39355094Slling 				    spa->spa_pool_props_object, propname,
39365094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
39375094Slling 
39385094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
39395094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
39405094Slling 
39415094Slling 				if (proptype == PROP_TYPE_INDEX) {
39425094Slling 					const char *unused;
39435094Slling 					VERIFY(zpool_prop_index_to_string(
39445094Slling 					    prop, intval, &unused) == 0);
39455094Slling 				}
39465094Slling 				VERIFY(zap_update(mos,
39475094Slling 				    spa->spa_pool_props_object, propname,
39485094Slling 				    8, 1, &intval, tx) == 0);
39495094Slling 			} else {
39505094Slling 				ASSERT(0); /* not allowed */
39515094Slling 			}
39525094Slling 
39535329Sgw25295 			switch (prop) {
39545329Sgw25295 			case ZPOOL_PROP_DELEGATION:
39555094Slling 				spa->spa_delegation = intval;
39565329Sgw25295 				break;
39575329Sgw25295 			case ZPOOL_PROP_BOOTFS:
39585094Slling 				spa->spa_bootfs = intval;
39595329Sgw25295 				break;
39605329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
39615329Sgw25295 				spa->spa_failmode = intval;
39625329Sgw25295 				break;
39635329Sgw25295 			default:
39645329Sgw25295 				break;
39655329Sgw25295 			}
39663912Slling 		}
39675094Slling 
39685094Slling 		/* log internal history if this is not a zpool create */
39695094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
39705094Slling 		    tx->tx_txg != TXG_INITIAL) {
39715094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
39725094Slling 			    spa, tx, cr, "%s %lld %s",
39737754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
39745094Slling 		}
39753912Slling 	}
39767754SJeff.Bonwick@Sun.COM 
39777754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
39783912Slling }
39793912Slling 
3980789Sahrens /*
3981789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3982789Sahrens  * part of the process, so we iterate until it converges.
3983789Sahrens  */
3984789Sahrens void
3985789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3986789Sahrens {
3987789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3988789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3989789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
39901635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3991789Sahrens 	vdev_t *vd;
3992789Sahrens 	dmu_tx_t *tx;
3993789Sahrens 	int dirty_vdevs;
39947754SJeff.Bonwick@Sun.COM 	int error;
3995789Sahrens 
3996789Sahrens 	/*
3997789Sahrens 	 * Lock out configuration changes.
3998789Sahrens 	 */
39997754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4000789Sahrens 
4001789Sahrens 	spa->spa_syncing_txg = txg;
4002789Sahrens 	spa->spa_sync_pass = 0;
4003789Sahrens 
40047754SJeff.Bonwick@Sun.COM 	/*
40057754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
40067754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
40077754SJeff.Bonwick@Sun.COM 	 */
40087754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
40098241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
40108241SJeff.Bonwick@Sun.COM 		/*
40118241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
40128241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
40138241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
40148241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
40158241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
40168241SJeff.Bonwick@Sun.COM 		 */
40178241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
40188241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
40198241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
40208241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
40218241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
40228241SJeff.Bonwick@Sun.COM 		}
40238241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
40248241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
40257754SJeff.Bonwick@Sun.COM 	}
40267754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
40277754SJeff.Bonwick@Sun.COM 
40281544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4029789Sahrens 
40302082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
40312082Seschrock 
40322082Seschrock 	/*
40334577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
40342082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
40352082Seschrock 	 */
40364577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
40374577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
40382082Seschrock 		int i;
40392082Seschrock 
40402082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
40412082Seschrock 			vd = rvd->vdev_child[i];
40422082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
40432082Seschrock 				break;
40442082Seschrock 		}
40452082Seschrock 		if (i == rvd->vdev_children) {
40462082Seschrock 			spa->spa_deflate = TRUE;
40472082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
40482082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
40492082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
40502082Seschrock 		}
40512082Seschrock 	}
40522082Seschrock 
40537046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
40547046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
40557046Sahrens 		dsl_pool_create_origin(dp, tx);
40567046Sahrens 
40577046Sahrens 		/* Keeping the origin open increases spa_minref */
40587046Sahrens 		spa->spa_minref += 3;
40597046Sahrens 	}
40607046Sahrens 
40617046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
40627046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
40637046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
40647046Sahrens 	}
40657046Sahrens 
4066789Sahrens 	/*
4067789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4068789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4069789Sahrens 	 * don't generate work on an otherwise idle system.
4070789Sahrens 	 */
4071789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
40722329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
40732329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4074789Sahrens 		spa_sync_deferred_frees(spa, txg);
4075789Sahrens 
4076789Sahrens 	/*
4077789Sahrens 	 * Iterate to convergence.
4078789Sahrens 	 */
4079789Sahrens 	do {
4080789Sahrens 		spa->spa_sync_pass++;
4081789Sahrens 
4082789Sahrens 		spa_sync_config_object(spa, tx);
40835450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
40845450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
40855450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
40865450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
40871544Seschrock 		spa_errlog_sync(spa, txg);
4088789Sahrens 		dsl_pool_sync(dp, txg);
4089789Sahrens 
4090789Sahrens 		dirty_vdevs = 0;
4091789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4092789Sahrens 			vdev_sync(vd, txg);
4093789Sahrens 			dirty_vdevs++;
4094789Sahrens 		}
4095789Sahrens 
4096789Sahrens 		bplist_sync(bpl, tx);
4097789Sahrens 	} while (dirty_vdevs);
4098789Sahrens 
4099789Sahrens 	bplist_close(bpl);
4100789Sahrens 
4101789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4102789Sahrens 
4103789Sahrens 	/*
4104789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4105789Sahrens 	 * to commit the transaction group.
41061635Sbonwick 	 *
41075688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
41085688Sbonwick 	 * random top-level vdevs that are known to be visible in the
41097754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
41107754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4111789Sahrens 	 */
41127754SJeff.Bonwick@Sun.COM 	for (;;) {
41137754SJeff.Bonwick@Sun.COM 		/*
41147754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
41157754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
41167754SJeff.Bonwick@Sun.COM 		 */
41177754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
41187754SJeff.Bonwick@Sun.COM 
41197754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
41207754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
41217754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
41227754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
41237754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
41247754SJeff.Bonwick@Sun.COM 			int c;
41257754SJeff.Bonwick@Sun.COM 
41267754SJeff.Bonwick@Sun.COM 			for (c = 0; c < children; c++) {
41277754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
41287754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
41297754SJeff.Bonwick@Sun.COM 					continue;
41307754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
41317754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
41327754SJeff.Bonwick@Sun.COM 					break;
41337754SJeff.Bonwick@Sun.COM 			}
41347754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg);
41357754SJeff.Bonwick@Sun.COM 		} else {
41367754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
41377754SJeff.Bonwick@Sun.COM 			    rvd->vdev_children, txg);
41381635Sbonwick 		}
41397754SJeff.Bonwick@Sun.COM 
41407754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
41417754SJeff.Bonwick@Sun.COM 
41427754SJeff.Bonwick@Sun.COM 		if (error == 0)
41437754SJeff.Bonwick@Sun.COM 			break;
41447754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
41457754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
41461635Sbonwick 	}
41472082Seschrock 	dmu_tx_commit(tx);
41482082Seschrock 
41491635Sbonwick 	/*
41501635Sbonwick 	 * Clear the dirty config list.
41511635Sbonwick 	 */
41527754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
41531635Sbonwick 		vdev_config_clean(vd);
41541635Sbonwick 
41551635Sbonwick 	/*
41561635Sbonwick 	 * Now that the new config has synced transactionally,
41571635Sbonwick 	 * let it become visible to the config cache.
41581635Sbonwick 	 */
41591635Sbonwick 	if (spa->spa_config_syncing != NULL) {
41601635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
41611635Sbonwick 		spa->spa_config_txg = txg;
41621635Sbonwick 		spa->spa_config_syncing = NULL;
41631635Sbonwick 	}
4164789Sahrens 
4165789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4166789Sahrens 
4167789Sahrens 	/*
4168789Sahrens 	 * Clean up the ZIL records for the synced txg.
4169789Sahrens 	 */
4170789Sahrens 	dsl_pool_zil_clean(dp);
4171789Sahrens 
4172789Sahrens 	/*
4173789Sahrens 	 * Update usable space statistics.
4174789Sahrens 	 */
4175789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4176789Sahrens 		vdev_sync_done(vd, txg);
4177789Sahrens 
4178789Sahrens 	/*
4179789Sahrens 	 * It had better be the case that we didn't dirty anything
41802082Seschrock 	 * since vdev_config_sync().
4181789Sahrens 	 */
4182789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4183789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4184789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4185789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4186789Sahrens 
41877754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
41881544Seschrock 
41891544Seschrock 	/*
41901544Seschrock 	 * If any async tasks have been requested, kick them off.
41911544Seschrock 	 */
41921544Seschrock 	spa_async_dispatch(spa);
4193789Sahrens }
4194789Sahrens 
4195789Sahrens /*
4196789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4197789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4198789Sahrens  * sync.
4199789Sahrens  */
4200789Sahrens void
4201789Sahrens spa_sync_allpools(void)
4202789Sahrens {
4203789Sahrens 	spa_t *spa = NULL;
4204789Sahrens 	mutex_enter(&spa_namespace_lock);
4205789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
42067754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4207789Sahrens 			continue;
4208789Sahrens 		spa_open_ref(spa, FTAG);
4209789Sahrens 		mutex_exit(&spa_namespace_lock);
4210789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4211789Sahrens 		mutex_enter(&spa_namespace_lock);
4212789Sahrens 		spa_close(spa, FTAG);
4213789Sahrens 	}
4214789Sahrens 	mutex_exit(&spa_namespace_lock);
4215789Sahrens }
4216789Sahrens 
4217789Sahrens /*
4218789Sahrens  * ==========================================================================
4219789Sahrens  * Miscellaneous routines
4220789Sahrens  * ==========================================================================
4221789Sahrens  */
4222789Sahrens 
4223789Sahrens /*
4224789Sahrens  * Remove all pools in the system.
4225789Sahrens  */
4226789Sahrens void
4227789Sahrens spa_evict_all(void)
4228789Sahrens {
4229789Sahrens 	spa_t *spa;
4230789Sahrens 
4231789Sahrens 	/*
4232789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4233789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4234789Sahrens 	 */
4235789Sahrens 	mutex_enter(&spa_namespace_lock);
4236789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4237789Sahrens 		/*
42381544Seschrock 		 * Stop async tasks.  The async thread may need to detach
42391544Seschrock 		 * a device that's been replaced, which requires grabbing
42401544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4241789Sahrens 		 */
4242789Sahrens 		spa_open_ref(spa, FTAG);
4243789Sahrens 		mutex_exit(&spa_namespace_lock);
42441544Seschrock 		spa_async_suspend(spa);
42454808Sek110237 		mutex_enter(&spa_namespace_lock);
4246789Sahrens 		spa_close(spa, FTAG);
4247789Sahrens 
4248789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4249789Sahrens 			spa_unload(spa);
4250789Sahrens 			spa_deactivate(spa);
4251789Sahrens 		}
4252789Sahrens 		spa_remove(spa);
4253789Sahrens 	}
4254789Sahrens 	mutex_exit(&spa_namespace_lock);
4255789Sahrens }
42561544Seschrock 
42571544Seschrock vdev_t *
42586643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
42591544Seschrock {
42606643Seschrock 	vdev_t *vd;
42616643Seschrock 	int i;
42626643Seschrock 
42636643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
42646643Seschrock 		return (vd);
42656643Seschrock 
42666643Seschrock 	if (l2cache) {
42676643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
42686643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
42696643Seschrock 			if (vd->vdev_guid == guid)
42706643Seschrock 				return (vd);
42716643Seschrock 		}
42726643Seschrock 	}
42736643Seschrock 
42746643Seschrock 	return (NULL);
42751544Seschrock }
42761760Seschrock 
42771760Seschrock void
42785094Slling spa_upgrade(spa_t *spa, uint64_t version)
42791760Seschrock {
42807754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
42811760Seschrock 
42821760Seschrock 	/*
42831760Seschrock 	 * This should only be called for a non-faulted pool, and since a
42841760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
42851760Seschrock 	 * possible.
42861760Seschrock 	 */
42874577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
42885094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
42895094Slling 
42905094Slling 	spa->spa_uberblock.ub_version = version;
42911760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
42921760Seschrock 
42937754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
42942082Seschrock 
42952082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
42961760Seschrock }
42972082Seschrock 
42982082Seschrock boolean_t
42992082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
43002082Seschrock {
43012082Seschrock 	int i;
43023377Seschrock 	uint64_t spareguid;
43035450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
43045450Sbrendan 
43055450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
43065450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
43072082Seschrock 			return (B_TRUE);
43082082Seschrock 
43095450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
43105450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
43115450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
43123377Seschrock 			return (B_TRUE);
43133377Seschrock 	}
43143377Seschrock 
43152082Seschrock 	return (B_FALSE);
43162082Seschrock }
43173912Slling 
43184451Seschrock /*
43197214Slling  * Check if a pool has an active shared spare device.
43207214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
43217214Slling  */
43227214Slling static boolean_t
43237214Slling spa_has_active_shared_spare(spa_t *spa)
43247214Slling {
43257214Slling 	int i, refcnt;
43267214Slling 	uint64_t pool;
43277214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
43287214Slling 
43297214Slling 	for (i = 0; i < sav->sav_count; i++) {
43307214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
43317214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
43327214Slling 		    refcnt > 2)
43337214Slling 			return (B_TRUE);
43347214Slling 	}
43357214Slling 
43367214Slling 	return (B_FALSE);
43377214Slling }
43387214Slling 
43397214Slling /*
43404451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
43414451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
43424451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
43434451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
43444451Seschrock  * or zdb as real changes.
43454451Seschrock  */
43464451Seschrock void
43474451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
43484451Seschrock {
43494451Seschrock #ifdef _KERNEL
43504451Seschrock 	sysevent_t		*ev;
43514451Seschrock 	sysevent_attr_list_t	*attr = NULL;
43524451Seschrock 	sysevent_value_t	value;
43534451Seschrock 	sysevent_id_t		eid;
43544451Seschrock 
43554451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
43564451Seschrock 	    SE_SLEEP);
43574451Seschrock 
43584451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
43594451Seschrock 	value.value.sv_string = spa_name(spa);
43604451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
43614451Seschrock 		goto done;
43624451Seschrock 
43634451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
43644451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
43654451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
43664451Seschrock 		goto done;
43674451Seschrock 
43684451Seschrock 	if (vd) {
43694451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
43704451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
43714451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
43724451Seschrock 		    SE_SLEEP) != 0)
43734451Seschrock 			goto done;
43744451Seschrock 
43754451Seschrock 		if (vd->vdev_path) {
43764451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
43774451Seschrock 			value.value.sv_string = vd->vdev_path;
43784451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
43794451Seschrock 			    &value, SE_SLEEP) != 0)
43804451Seschrock 				goto done;
43814451Seschrock 		}
43824451Seschrock 	}
43834451Seschrock 
43845756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
43855756Seschrock 		goto done;
43865756Seschrock 	attr = NULL;
43875756Seschrock 
43884451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
43894451Seschrock 
43904451Seschrock done:
43914451Seschrock 	if (attr)
43924451Seschrock 		sysevent_free_attr(attr);
43934451Seschrock 	sysevent_free(ev);
43944451Seschrock #endif
43954451Seschrock }
4396