xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 8662)
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 
63*8662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
64*8662SJordan.Vaughan@Sun.com #include <sys/zone.h>
65*8662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
66*8662SJordan.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	*/
737754SJeff.Bonwick@Sun.COM 	{	1,	8	},	/* ZIO_TYPE_READ	*/
747754SJeff.Bonwick@Sun.COM 	{	8,	1	},	/* 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 	 */
6867754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_async_root_lock);
6877754SJeff.Bonwick@Sun.COM 	while (spa->spa_async_root_count != 0)
6887754SJeff.Bonwick@Sun.COM 		cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock);
6897754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_async_root_lock);
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 	/*
11102082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
11112082Seschrock 	 * value that will be returned by spa_version() since parsing the
11122082Seschrock 	 * configuration requires knowing the version number.
1113789Sahrens 	 */
11147754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11152082Seschrock 	spa->spa_ubsync.ub_version = version;
11162082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
11177754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1118789Sahrens 
11192082Seschrock 	if (error != 0)
11201544Seschrock 		goto out;
1121789Sahrens 
11221585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1123789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1124789Sahrens 
1125789Sahrens 	/*
1126789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1127789Sahrens 	 */
11287754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11294070Smc142369 	error = vdev_open(rvd);
11307754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
11314070Smc142369 	if (error != 0)
11321544Seschrock 		goto out;
1133789Sahrens 
1134789Sahrens 	/*
11351986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
11367754SJeff.Bonwick@Sun.COM 	 * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER.
11371986Seschrock 	 */
11388241SJeff.Bonwick@Sun.COM 	if (mosconfig) {
11398241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11408241SJeff.Bonwick@Sun.COM 		error = vdev_validate(rvd);
11418241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
11428241SJeff.Bonwick@Sun.COM 		if (error != 0)
11438241SJeff.Bonwick@Sun.COM 			goto out;
11448241SJeff.Bonwick@Sun.COM 	}
11451986Seschrock 
11461986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
11471986Seschrock 		error = ENXIO;
11481986Seschrock 		goto out;
11491986Seschrock 	}
11501986Seschrock 
11511986Seschrock 	/*
1152789Sahrens 	 * Find the best uberblock.
1153789Sahrens 	 */
11547754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1155789Sahrens 
1156789Sahrens 	/*
1157789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1158789Sahrens 	 */
1159789Sahrens 	if (ub->ub_txg == 0) {
11601760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11611760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11621544Seschrock 		error = ENXIO;
11631544Seschrock 		goto out;
11641544Seschrock 	}
11651544Seschrock 
11661544Seschrock 	/*
11671544Seschrock 	 * If the pool is newer than the code, we can't open it.
11681544Seschrock 	 */
11694577Sahrens 	if (ub->ub_version > SPA_VERSION) {
11701760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11711760Seschrock 		    VDEV_AUX_VERSION_NEWER);
11721544Seschrock 		error = ENOTSUP;
11731544Seschrock 		goto out;
1174789Sahrens 	}
1175789Sahrens 
1176789Sahrens 	/*
1177789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1178789Sahrens 	 * incomplete configuration.
1179789Sahrens 	 */
11801732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
11811544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11821544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
11831544Seschrock 		error = ENXIO;
11841544Seschrock 		goto out;
1185789Sahrens 	}
1186789Sahrens 
1187789Sahrens 	/*
1188789Sahrens 	 * Initialize internal SPA structures.
1189789Sahrens 	 */
1190789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1191789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1192789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
11931544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
11941544Seschrock 	if (error) {
11951544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11961544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11971544Seschrock 		goto out;
11981544Seschrock 	}
1199789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1200789Sahrens 
12011544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1202789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
12031544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
12041544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12051544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12061544Seschrock 		error = EIO;
12071544Seschrock 		goto out;
12081544Seschrock 	}
1209789Sahrens 
1210789Sahrens 	if (!mosconfig) {
12112082Seschrock 		nvlist_t *newconfig;
12123975Sek110237 		uint64_t hostid;
12132082Seschrock 
12142082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
12151544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12161544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12171544Seschrock 			error = EIO;
12181544Seschrock 			goto out;
12191544Seschrock 		}
1220789Sahrens 
12217706SLin.Ling@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig,
12227706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
12233975Sek110237 			char *hostname;
12243975Sek110237 			unsigned long myhostid = 0;
12253975Sek110237 
12263975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
12273975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
12283975Sek110237 
1229*8662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
1230*8662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
1231*8662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
1232*8662SJordan.Vaughan@Sun.com 			/*
1233*8662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
1234*8662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
1235*8662SJordan.Vaughan@Sun.com 			 */
12363975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1237*8662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
12384178Slling 			if (hostid != 0 && myhostid != 0 &&
1239*8662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
12403975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
12413975Sek110237 				    "loaded as it was last accessed by "
12427706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
12433975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
12447754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
12453975Sek110237 				    (unsigned long)hostid);
12463975Sek110237 				error = EBADF;
12473975Sek110237 				goto out;
12483975Sek110237 			}
12493975Sek110237 		}
12503975Sek110237 
1251789Sahrens 		spa_config_set(spa, newconfig);
1252789Sahrens 		spa_unload(spa);
1253789Sahrens 		spa_deactivate(spa);
12548241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1255789Sahrens 
12561544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
12571544Seschrock 	}
12581544Seschrock 
12591544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
12601544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
12611544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
12621544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12631544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12641544Seschrock 		error = EIO;
12651544Seschrock 		goto out;
1266789Sahrens 	}
1267789Sahrens 
12681544Seschrock 	/*
12692082Seschrock 	 * Load the bit that tells us to use the new accounting function
12702082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
12712082Seschrock 	 * be present.
12722082Seschrock 	 */
12732082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12742082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
12752082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
12762082Seschrock 	if (error != 0 && error != ENOENT) {
12772082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12782082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12792082Seschrock 		error = EIO;
12802082Seschrock 		goto out;
12812082Seschrock 	}
12822082Seschrock 
12832082Seschrock 	/*
12841544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
12851544Seschrock 	 * not be present.
12861544Seschrock 	 */
12871544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12881544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
12891544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
12901807Sbonwick 	if (error != 0 && error != ENOENT) {
12911544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12921544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12931544Seschrock 		error = EIO;
12941544Seschrock 		goto out;
12951544Seschrock 	}
12961544Seschrock 
12971544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12981544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
12991544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
13001544Seschrock 	if (error != 0 && error != ENOENT) {
13011544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13021544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13031544Seschrock 		error = EIO;
13041544Seschrock 		goto out;
13051544Seschrock 	}
1306789Sahrens 
1307789Sahrens 	/*
13082926Sek110237 	 * Load the history object.  If we have an older pool, this
13092926Sek110237 	 * will not be present.
13102926Sek110237 	 */
13112926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
13122926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
13132926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
13142926Sek110237 	if (error != 0 && error != ENOENT) {
13152926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13162926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
13172926Sek110237 		error = EIO;
13182926Sek110237 		goto out;
13192926Sek110237 	}
13202926Sek110237 
13212926Sek110237 	/*
13222082Seschrock 	 * Load any hot spares for this pool.
13232082Seschrock 	 */
13242082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13255450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
13262082Seschrock 	if (error != 0 && error != ENOENT) {
13272082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13282082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
13292082Seschrock 		error = EIO;
13302082Seschrock 		goto out;
13312082Seschrock 	}
13322082Seschrock 	if (error == 0) {
13334577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
13345450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
13355450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
13362082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13372082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
13382082Seschrock 			error = EIO;
13392082Seschrock 			goto out;
13402082Seschrock 		}
13412082Seschrock 
13427754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13432082Seschrock 		spa_load_spares(spa);
13447754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
13452082Seschrock 	}
13462082Seschrock 
13475450Sbrendan 	/*
13485450Sbrendan 	 * Load any level 2 ARC devices for this pool.
13495450Sbrendan 	 */
13505450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13515450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
13525450Sbrendan 	    &spa->spa_l2cache.sav_object);
13535450Sbrendan 	if (error != 0 && error != ENOENT) {
13545450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13555450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
13565450Sbrendan 		error = EIO;
13575450Sbrendan 		goto out;
13585450Sbrendan 	}
13595450Sbrendan 	if (error == 0) {
13605450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
13615450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
13625450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
13635450Sbrendan 			vdev_set_state(rvd, B_TRUE,
13645450Sbrendan 			    VDEV_STATE_CANT_OPEN,
13655450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
13665450Sbrendan 			error = EIO;
13675450Sbrendan 			goto out;
13685450Sbrendan 		}
13695450Sbrendan 
13707754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
13715450Sbrendan 		spa_load_l2cache(spa);
13727754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
13735450Sbrendan 	}
13745450Sbrendan 
13757294Sperrin 	if (spa_check_logs(spa)) {
13767294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13777294Sperrin 		    VDEV_AUX_BAD_LOG);
13787294Sperrin 		error = ENXIO;
13797294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
13807294Sperrin 		goto out;
13817294Sperrin 	}
13827294Sperrin 
13837294Sperrin 
13845094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
13854543Smarks 
13863912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13873912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
13883912Slling 
13893912Slling 	if (error && error != ENOENT) {
13903912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13913912Slling 		    VDEV_AUX_CORRUPT_DATA);
13923912Slling 		error = EIO;
13933912Slling 		goto out;
13943912Slling 	}
13953912Slling 
13963912Slling 	if (error == 0) {
13973912Slling 		(void) zap_lookup(spa->spa_meta_objset,
13983912Slling 		    spa->spa_pool_props_object,
13994451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
14003912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
14014451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
14024451Seschrock 		    spa->spa_pool_props_object,
14034451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
14044451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
14054543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
14064543Smarks 		    spa->spa_pool_props_object,
14074543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
14084543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
14095329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
14105329Sgw25295 		    spa->spa_pool_props_object,
14115329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
14125329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
14133912Slling 	}
14143912Slling 
14152082Seschrock 	/*
14164451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
14174451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
14184451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
14194451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
14204451Seschrock 	 * over.
14214451Seschrock 	 */
14225756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
14234451Seschrock 		spa_check_removed(spa->spa_root_vdev);
14244451Seschrock 
14254451Seschrock 	/*
14261986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1427789Sahrens 	 */
14281986Seschrock 	vdev_load(rvd);
1429789Sahrens 
1430789Sahrens 	/*
1431789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1432789Sahrens 	 */
14337754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1434789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
14357754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1436789Sahrens 
1437789Sahrens 	/*
1438789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1439789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1440789Sahrens 	 */
14411544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
14421544Seschrock 		error = ENXIO;
14431544Seschrock 		goto out;
14441544Seschrock 	}
1445789Sahrens 
14468241SJeff.Bonwick@Sun.COM 	if (spa_writeable(spa)) {
14471635Sbonwick 		dmu_tx_t *tx;
14481635Sbonwick 		int need_update = B_FALSE;
14498241SJeff.Bonwick@Sun.COM 
14508241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
14511601Sbonwick 
14521635Sbonwick 		/*
14531635Sbonwick 		 * Claim log blocks that haven't been committed yet.
14541635Sbonwick 		 * This must all happen in a single txg.
14551635Sbonwick 		 */
14561601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1457789Sahrens 		    spa_first_txg(spa));
14587754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
14592417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1460789Sahrens 		dmu_tx_commit(tx);
1461789Sahrens 
1462789Sahrens 		spa->spa_sync_on = B_TRUE;
1463789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1464789Sahrens 
1465789Sahrens 		/*
1466789Sahrens 		 * Wait for all claims to sync.
1467789Sahrens 		 */
1468789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
14691585Sbonwick 
14701585Sbonwick 		/*
14711635Sbonwick 		 * If the config cache is stale, or we have uninitialized
14721635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
14731585Sbonwick 		 */
14741635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
14751635Sbonwick 		    state == SPA_LOAD_IMPORT)
14761635Sbonwick 			need_update = B_TRUE;
14771635Sbonwick 
14788241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
14791635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
14801635Sbonwick 				need_update = B_TRUE;
14811585Sbonwick 
14821585Sbonwick 		/*
14831635Sbonwick 		 * Update the config cache asychronously in case we're the
14841635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
14851585Sbonwick 		 */
14861635Sbonwick 		if (need_update)
14871635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
14888241SJeff.Bonwick@Sun.COM 
14898241SJeff.Bonwick@Sun.COM 		/*
14908241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
14918241SJeff.Bonwick@Sun.COM 		 */
14928241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
14938241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
1494789Sahrens 	}
1495789Sahrens 
14961544Seschrock 	error = 0;
14971544Seschrock out:
14987046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
14992082Seschrock 	if (error && error != EBADF)
15007294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
15011544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
15021544Seschrock 	spa->spa_ena = 0;
15031544Seschrock 
15041544Seschrock 	return (error);
1505789Sahrens }
1506789Sahrens 
1507789Sahrens /*
1508789Sahrens  * Pool Open/Import
1509789Sahrens  *
1510789Sahrens  * The import case is identical to an open except that the configuration is sent
1511789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1512789Sahrens  * case of an open, the pool configuration will exist in the
15134451Seschrock  * POOL_STATE_UNINITIALIZED state.
1514789Sahrens  *
1515789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1516789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1517789Sahrens  * ambiguous state.
1518789Sahrens  */
1519789Sahrens static int
1520789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1521789Sahrens {
1522789Sahrens 	spa_t *spa;
1523789Sahrens 	int error;
1524789Sahrens 	int locked = B_FALSE;
1525789Sahrens 
1526789Sahrens 	*spapp = NULL;
1527789Sahrens 
1528789Sahrens 	/*
1529789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1530789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1531789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1532789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1533789Sahrens 	 */
1534789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1535789Sahrens 		mutex_enter(&spa_namespace_lock);
1536789Sahrens 		locked = B_TRUE;
1537789Sahrens 	}
1538789Sahrens 
1539789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1540789Sahrens 		if (locked)
1541789Sahrens 			mutex_exit(&spa_namespace_lock);
1542789Sahrens 		return (ENOENT);
1543789Sahrens 	}
1544789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1545789Sahrens 
15468241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
1547789Sahrens 
15481635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1549789Sahrens 
1550789Sahrens 		if (error == EBADF) {
1551789Sahrens 			/*
15521986Seschrock 			 * If vdev_validate() returns failure (indicated by
15531986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
15541986Seschrock 			 * that the pool has been exported or destroyed.  If
15551986Seschrock 			 * this is the case, the config cache is out of sync and
15561986Seschrock 			 * we should remove the pool from the namespace.
1557789Sahrens 			 */
1558789Sahrens 			spa_unload(spa);
1559789Sahrens 			spa_deactivate(spa);
15606643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1561789Sahrens 			spa_remove(spa);
1562789Sahrens 			if (locked)
1563789Sahrens 				mutex_exit(&spa_namespace_lock);
1564789Sahrens 			return (ENOENT);
15651544Seschrock 		}
15661544Seschrock 
15671544Seschrock 		if (error) {
1568789Sahrens 			/*
1569789Sahrens 			 * We can't open the pool, but we still have useful
1570789Sahrens 			 * information: the state of each vdev after the
1571789Sahrens 			 * attempted vdev_open().  Return this to the user.
1572789Sahrens 			 */
15737754SJeff.Bonwick@Sun.COM 			if (config != NULL && spa->spa_root_vdev != NULL)
1574789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1575789Sahrens 				    B_TRUE);
1576789Sahrens 			spa_unload(spa);
1577789Sahrens 			spa_deactivate(spa);
15781544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1579789Sahrens 			if (locked)
1580789Sahrens 				mutex_exit(&spa_namespace_lock);
1581789Sahrens 			*spapp = NULL;
1582789Sahrens 			return (error);
15831544Seschrock 		} else {
15841544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1585789Sahrens 		}
1586789Sahrens 	}
1587789Sahrens 
1588789Sahrens 	spa_open_ref(spa, tag);
15894451Seschrock 
1590789Sahrens 	if (locked)
1591789Sahrens 		mutex_exit(&spa_namespace_lock);
1592789Sahrens 
1593789Sahrens 	*spapp = spa;
1594789Sahrens 
15957754SJeff.Bonwick@Sun.COM 	if (config != NULL)
1596789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1597789Sahrens 
1598789Sahrens 	return (0);
1599789Sahrens }
1600789Sahrens 
1601789Sahrens int
1602789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1603789Sahrens {
1604789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1605789Sahrens }
1606789Sahrens 
16071544Seschrock /*
16081544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
16091544Seschrock  * preventing it from being exported or destroyed.
16101544Seschrock  */
16111544Seschrock spa_t *
16121544Seschrock spa_inject_addref(char *name)
16131544Seschrock {
16141544Seschrock 	spa_t *spa;
16151544Seschrock 
16161544Seschrock 	mutex_enter(&spa_namespace_lock);
16171544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
16181544Seschrock 		mutex_exit(&spa_namespace_lock);
16191544Seschrock 		return (NULL);
16201544Seschrock 	}
16211544Seschrock 	spa->spa_inject_ref++;
16221544Seschrock 	mutex_exit(&spa_namespace_lock);
16231544Seschrock 
16241544Seschrock 	return (spa);
16251544Seschrock }
16261544Seschrock 
16271544Seschrock void
16281544Seschrock spa_inject_delref(spa_t *spa)
16291544Seschrock {
16301544Seschrock 	mutex_enter(&spa_namespace_lock);
16311544Seschrock 	spa->spa_inject_ref--;
16321544Seschrock 	mutex_exit(&spa_namespace_lock);
16331544Seschrock }
16341544Seschrock 
16355450Sbrendan /*
16365450Sbrendan  * Add spares device information to the nvlist.
16375450Sbrendan  */
16382082Seschrock static void
16392082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
16402082Seschrock {
16412082Seschrock 	nvlist_t **spares;
16422082Seschrock 	uint_t i, nspares;
16432082Seschrock 	nvlist_t *nvroot;
16442082Seschrock 	uint64_t guid;
16452082Seschrock 	vdev_stat_t *vs;
16462082Seschrock 	uint_t vsc;
16473377Seschrock 	uint64_t pool;
16482082Seschrock 
16495450Sbrendan 	if (spa->spa_spares.sav_count == 0)
16502082Seschrock 		return;
16512082Seschrock 
16522082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
16532082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16545450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
16552082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
16562082Seschrock 	if (nspares != 0) {
16572082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
16582082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
16592082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
16602082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
16612082Seschrock 
16622082Seschrock 		/*
16632082Seschrock 		 * Go through and find any spares which have since been
16642082Seschrock 		 * repurposed as an active spare.  If this is the case, update
16652082Seschrock 		 * their status appropriately.
16662082Seschrock 		 */
16672082Seschrock 		for (i = 0; i < nspares; i++) {
16682082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
16692082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
16707214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
16717214Slling 			    pool != 0ULL) {
16722082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
16732082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
16742082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
16752082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
16762082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
16772082Seschrock 			}
16782082Seschrock 		}
16792082Seschrock 	}
16802082Seschrock }
16812082Seschrock 
16825450Sbrendan /*
16835450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
16845450Sbrendan  */
16855450Sbrendan static void
16865450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
16875450Sbrendan {
16885450Sbrendan 	nvlist_t **l2cache;
16895450Sbrendan 	uint_t i, j, nl2cache;
16905450Sbrendan 	nvlist_t *nvroot;
16915450Sbrendan 	uint64_t guid;
16925450Sbrendan 	vdev_t *vd;
16935450Sbrendan 	vdev_stat_t *vs;
16945450Sbrendan 	uint_t vsc;
16955450Sbrendan 
16965450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
16975450Sbrendan 		return;
16985450Sbrendan 
16997754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
17005450Sbrendan 
17015450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
17025450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
17035450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
17045450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
17055450Sbrendan 	if (nl2cache != 0) {
17065450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
17075450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
17085450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
17095450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
17105450Sbrendan 
17115450Sbrendan 		/*
17125450Sbrendan 		 * Update level 2 cache device stats.
17135450Sbrendan 		 */
17145450Sbrendan 
17155450Sbrendan 		for (i = 0; i < nl2cache; i++) {
17165450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
17175450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
17185450Sbrendan 
17195450Sbrendan 			vd = NULL;
17205450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
17215450Sbrendan 				if (guid ==
17225450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
17235450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
17245450Sbrendan 					break;
17255450Sbrendan 				}
17265450Sbrendan 			}
17275450Sbrendan 			ASSERT(vd != NULL);
17285450Sbrendan 
17295450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
17305450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
17315450Sbrendan 			vdev_get_stats(vd, vs);
17325450Sbrendan 		}
17335450Sbrendan 	}
17345450Sbrendan 
17357754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
17365450Sbrendan }
17375450Sbrendan 
1738789Sahrens int
17391544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1740789Sahrens {
1741789Sahrens 	int error;
1742789Sahrens 	spa_t *spa;
1743789Sahrens 
1744789Sahrens 	*config = NULL;
1745789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1746789Sahrens 
17472082Seschrock 	if (spa && *config != NULL) {
17481544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
17491544Seschrock 		    spa_get_errlog_size(spa)) == 0);
17501544Seschrock 
17517754SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
17527754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
17537754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0);
17547754SJeff.Bonwick@Sun.COM 
17552082Seschrock 		spa_add_spares(spa, *config);
17565450Sbrendan 		spa_add_l2cache(spa, *config);
17572082Seschrock 	}
17582082Seschrock 
17591544Seschrock 	/*
17601544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
17611544Seschrock 	 * and call spa_lookup() directly.
17621544Seschrock 	 */
17631544Seschrock 	if (altroot) {
17641544Seschrock 		if (spa == NULL) {
17651544Seschrock 			mutex_enter(&spa_namespace_lock);
17661544Seschrock 			spa = spa_lookup(name);
17671544Seschrock 			if (spa)
17681544Seschrock 				spa_altroot(spa, altroot, buflen);
17691544Seschrock 			else
17701544Seschrock 				altroot[0] = '\0';
17711544Seschrock 			spa = NULL;
17721544Seschrock 			mutex_exit(&spa_namespace_lock);
17731544Seschrock 		} else {
17741544Seschrock 			spa_altroot(spa, altroot, buflen);
17751544Seschrock 		}
17761544Seschrock 	}
17771544Seschrock 
1778789Sahrens 	if (spa != NULL)
1779789Sahrens 		spa_close(spa, FTAG);
1780789Sahrens 
1781789Sahrens 	return (error);
1782789Sahrens }
1783789Sahrens 
1784789Sahrens /*
17855450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
17865450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
17875450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
17885450Sbrendan  * specified, as long as they are well-formed.
17892082Seschrock  */
17902082Seschrock static int
17915450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
17925450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
17935450Sbrendan     vdev_labeltype_t label)
17942082Seschrock {
17955450Sbrendan 	nvlist_t **dev;
17965450Sbrendan 	uint_t i, ndev;
17972082Seschrock 	vdev_t *vd;
17982082Seschrock 	int error;
17992082Seschrock 
18007754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18017754SJeff.Bonwick@Sun.COM 
18022082Seschrock 	/*
18035450Sbrendan 	 * It's acceptable to have no devs specified.
18042082Seschrock 	 */
18055450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
18062082Seschrock 		return (0);
18072082Seschrock 
18085450Sbrendan 	if (ndev == 0)
18092082Seschrock 		return (EINVAL);
18102082Seschrock 
18112082Seschrock 	/*
18125450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
18135450Sbrendan 	 * device type.
18142082Seschrock 	 */
18155450Sbrendan 	if (spa_version(spa) < version)
18162082Seschrock 		return (ENOTSUP);
18172082Seschrock 
18183377Seschrock 	/*
18195450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
18203377Seschrock 	 * checking.
18213377Seschrock 	 */
18225450Sbrendan 	sav->sav_pending = dev;
18235450Sbrendan 	sav->sav_npending = ndev;
18245450Sbrendan 
18255450Sbrendan 	for (i = 0; i < ndev; i++) {
18265450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
18272082Seschrock 		    mode)) != 0)
18283377Seschrock 			goto out;
18292082Seschrock 
18302082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
18312082Seschrock 			vdev_free(vd);
18323377Seschrock 			error = EINVAL;
18333377Seschrock 			goto out;
18342082Seschrock 		}
18352082Seschrock 
18365450Sbrendan 		/*
18377754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
18387754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
18395450Sbrendan 		 */
18407754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
18415450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
18425450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
18435450Sbrendan 			error = ENOTBLK;
18445450Sbrendan 			goto out;
18455450Sbrendan 		}
18467754SJeff.Bonwick@Sun.COM #endif
18472082Seschrock 		vd->vdev_top = vd;
18483377Seschrock 
18493377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
18505450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
18515450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
18523377Seschrock 			    vd->vdev_guid) == 0);
18532082Seschrock 		}
18542082Seschrock 
18552082Seschrock 		vdev_free(vd);
18563377Seschrock 
18575450Sbrendan 		if (error &&
18585450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
18593377Seschrock 			goto out;
18603377Seschrock 		else
18613377Seschrock 			error = 0;
18622082Seschrock 	}
18632082Seschrock 
18643377Seschrock out:
18655450Sbrendan 	sav->sav_pending = NULL;
18665450Sbrendan 	sav->sav_npending = 0;
18673377Seschrock 	return (error);
18682082Seschrock }
18692082Seschrock 
18705450Sbrendan static int
18715450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
18725450Sbrendan {
18735450Sbrendan 	int error;
18745450Sbrendan 
18757754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18767754SJeff.Bonwick@Sun.COM 
18775450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
18785450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
18795450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
18805450Sbrendan 		return (error);
18815450Sbrendan 	}
18825450Sbrendan 
18835450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
18845450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
18855450Sbrendan 	    VDEV_LABEL_L2CACHE));
18865450Sbrendan }
18875450Sbrendan 
18885450Sbrendan static void
18895450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
18905450Sbrendan     const char *config)
18915450Sbrendan {
18925450Sbrendan 	int i;
18935450Sbrendan 
18945450Sbrendan 	if (sav->sav_config != NULL) {
18955450Sbrendan 		nvlist_t **olddevs;
18965450Sbrendan 		uint_t oldndevs;
18975450Sbrendan 		nvlist_t **newdevs;
18985450Sbrendan 
18995450Sbrendan 		/*
19005450Sbrendan 		 * Generate new dev list by concatentating with the
19015450Sbrendan 		 * current dev list.
19025450Sbrendan 		 */
19035450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
19045450Sbrendan 		    &olddevs, &oldndevs) == 0);
19055450Sbrendan 
19065450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
19075450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
19085450Sbrendan 		for (i = 0; i < oldndevs; i++)
19095450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
19105450Sbrendan 			    KM_SLEEP) == 0);
19115450Sbrendan 		for (i = 0; i < ndevs; i++)
19125450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
19135450Sbrendan 			    KM_SLEEP) == 0);
19145450Sbrendan 
19155450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
19165450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
19175450Sbrendan 
19185450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
19195450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
19205450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
19215450Sbrendan 			nvlist_free(newdevs[i]);
19225450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
19235450Sbrendan 	} else {
19245450Sbrendan 		/*
19255450Sbrendan 		 * Generate a new dev list.
19265450Sbrendan 		 */
19275450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
19285450Sbrendan 		    KM_SLEEP) == 0);
19295450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
19305450Sbrendan 		    devs, ndevs) == 0);
19315450Sbrendan 	}
19325450Sbrendan }
19335450Sbrendan 
19345450Sbrendan /*
19355450Sbrendan  * Stop and drop level 2 ARC devices
19365450Sbrendan  */
19375450Sbrendan void
19385450Sbrendan spa_l2cache_drop(spa_t *spa)
19395450Sbrendan {
19405450Sbrendan 	vdev_t *vd;
19415450Sbrendan 	int i;
19425450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
19435450Sbrendan 
19445450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
19455450Sbrendan 		uint64_t pool;
19465450Sbrendan 
19475450Sbrendan 		vd = sav->sav_vdevs[i];
19485450Sbrendan 		ASSERT(vd != NULL);
19495450Sbrendan 
19508241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
19518241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
19525450Sbrendan 			l2arc_remove_vdev(vd);
19535450Sbrendan 		if (vd->vdev_isl2cache)
19545450Sbrendan 			spa_l2cache_remove(vd);
19555450Sbrendan 		vdev_clear_stats(vd);
19565450Sbrendan 		(void) vdev_close(vd);
19575450Sbrendan 	}
19585450Sbrendan }
19595450Sbrendan 
19602082Seschrock /*
1961789Sahrens  * Pool Creation
1962789Sahrens  */
1963789Sahrens int
19645094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
19657184Stimh     const char *history_str, nvlist_t *zplprops)
1966789Sahrens {
1967789Sahrens 	spa_t *spa;
19685094Slling 	char *altroot = NULL;
19691635Sbonwick 	vdev_t *rvd;
1970789Sahrens 	dsl_pool_t *dp;
1971789Sahrens 	dmu_tx_t *tx;
19722082Seschrock 	int c, error = 0;
1973789Sahrens 	uint64_t txg = TXG_INITIAL;
19745450Sbrendan 	nvlist_t **spares, **l2cache;
19755450Sbrendan 	uint_t nspares, nl2cache;
19765094Slling 	uint64_t version;
1977789Sahrens 
1978789Sahrens 	/*
1979789Sahrens 	 * If this pool already exists, return failure.
1980789Sahrens 	 */
1981789Sahrens 	mutex_enter(&spa_namespace_lock);
1982789Sahrens 	if (spa_lookup(pool) != NULL) {
1983789Sahrens 		mutex_exit(&spa_namespace_lock);
1984789Sahrens 		return (EEXIST);
1985789Sahrens 	}
1986789Sahrens 
1987789Sahrens 	/*
1988789Sahrens 	 * Allocate a new spa_t structure.
1989789Sahrens 	 */
19905094Slling 	(void) nvlist_lookup_string(props,
19915094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
19921635Sbonwick 	spa = spa_add(pool, altroot);
19938241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
1994789Sahrens 
1995789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
19965094Slling 
19975094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
19985094Slling 		spa_unload(spa);
19995094Slling 		spa_deactivate(spa);
20005094Slling 		spa_remove(spa);
20016643Seschrock 		mutex_exit(&spa_namespace_lock);
20025094Slling 		return (error);
20035094Slling 	}
20045094Slling 
20055094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
20065094Slling 	    &version) != 0)
20075094Slling 		version = SPA_VERSION;
20085094Slling 	ASSERT(version <= SPA_VERSION);
20095094Slling 	spa->spa_uberblock.ub_version = version;
2010789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2011789Sahrens 
20121635Sbonwick 	/*
20131635Sbonwick 	 * Create the root vdev.
20141635Sbonwick 	 */
20157754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20161635Sbonwick 
20172082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
20182082Seschrock 
20192082Seschrock 	ASSERT(error != 0 || rvd != NULL);
20202082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
20212082Seschrock 
20225913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
20231635Sbonwick 		error = EINVAL;
20242082Seschrock 
20252082Seschrock 	if (error == 0 &&
20262082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
20275450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
20282082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
20292082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
20302082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
20312082Seschrock 		vdev_config_dirty(rvd);
20321635Sbonwick 	}
20331635Sbonwick 
20347754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2035789Sahrens 
20362082Seschrock 	if (error != 0) {
2037789Sahrens 		spa_unload(spa);
2038789Sahrens 		spa_deactivate(spa);
2039789Sahrens 		spa_remove(spa);
2040789Sahrens 		mutex_exit(&spa_namespace_lock);
2041789Sahrens 		return (error);
2042789Sahrens 	}
2043789Sahrens 
20442082Seschrock 	/*
20452082Seschrock 	 * Get the list of spares, if specified.
20462082Seschrock 	 */
20472082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
20482082Seschrock 	    &spares, &nspares) == 0) {
20495450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
20502082Seschrock 		    KM_SLEEP) == 0);
20515450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
20522082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
20537754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20542082Seschrock 		spa_load_spares(spa);
20557754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
20565450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
20575450Sbrendan 	}
20585450Sbrendan 
20595450Sbrendan 	/*
20605450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
20615450Sbrendan 	 */
20625450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
20635450Sbrendan 	    &l2cache, &nl2cache) == 0) {
20645450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
20655450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
20665450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
20675450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
20687754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20695450Sbrendan 		spa_load_l2cache(spa);
20707754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
20715450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
20722082Seschrock 	}
20732082Seschrock 
20747184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2075789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2076789Sahrens 
2077789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2078789Sahrens 
2079789Sahrens 	/*
2080789Sahrens 	 * Create the pool config object.
2081789Sahrens 	 */
2082789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
20837497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2084789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2085789Sahrens 
20861544Seschrock 	if (zap_add(spa->spa_meta_objset,
2087789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
20881544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
20891544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
20901544Seschrock 	}
2091789Sahrens 
20925094Slling 	/* Newly created pools with the right version are always deflated. */
20935094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
20945094Slling 		spa->spa_deflate = TRUE;
20955094Slling 		if (zap_add(spa->spa_meta_objset,
20965094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
20975094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
20985094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
20995094Slling 		}
21002082Seschrock 	}
21012082Seschrock 
2102789Sahrens 	/*
2103789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2104789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2105789Sahrens 	 * keeps changing.
2106789Sahrens 	 */
2107789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2108789Sahrens 	    1 << 14, tx);
2109789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2110789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2111789Sahrens 
21121544Seschrock 	if (zap_add(spa->spa_meta_objset,
2113789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
21141544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
21151544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
21161544Seschrock 	}
2117789Sahrens 
21182926Sek110237 	/*
21192926Sek110237 	 * Create the pool's history object.
21202926Sek110237 	 */
21215094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
21225094Slling 		spa_history_create_obj(spa, tx);
21235094Slling 
21245094Slling 	/*
21255094Slling 	 * Set pool properties.
21265094Slling 	 */
21275094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
21285094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
21295329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
21308525SEric.Schrock@Sun.COM 	if (props != NULL) {
21318525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
21325094Slling 		spa_sync_props(spa, props, CRED(), tx);
21338525SEric.Schrock@Sun.COM 	}
21342926Sek110237 
2135789Sahrens 	dmu_tx_commit(tx);
2136789Sahrens 
2137789Sahrens 	spa->spa_sync_on = B_TRUE;
2138789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2139789Sahrens 
2140789Sahrens 	/*
2141789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2142789Sahrens 	 * bean counters are appropriately updated.
2143789Sahrens 	 */
2144789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2145789Sahrens 
21466643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2147789Sahrens 
21485094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
21494715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
21504715Sek110237 
2151789Sahrens 	mutex_exit(&spa_namespace_lock);
2152789Sahrens 
21537046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
21547046Sahrens 
2155789Sahrens 	return (0);
2156789Sahrens }
2157789Sahrens 
2158789Sahrens /*
2159789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2160789Sahrens  * then call spa_load() to do the dirty work.
2161789Sahrens  */
21626423Sgw25295 static int
21636423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
21646643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2165789Sahrens {
2166789Sahrens 	spa_t *spa;
21675094Slling 	char *altroot = NULL;
21686643Seschrock 	int error, loaderr;
21692082Seschrock 	nvlist_t *nvroot;
21705450Sbrendan 	nvlist_t **spares, **l2cache;
21715450Sbrendan 	uint_t nspares, nl2cache;
2172789Sahrens 
2173789Sahrens 	/*
2174789Sahrens 	 * If a pool with this name exists, return failure.
2175789Sahrens 	 */
2176789Sahrens 	mutex_enter(&spa_namespace_lock);
21777897SLin.Ling@Sun.COM 	if ((spa = spa_lookup(pool)) != NULL) {
21787897SLin.Ling@Sun.COM 		if (isroot) {
21797897SLin.Ling@Sun.COM 			/*
21807897SLin.Ling@Sun.COM 			 * Remove the existing root pool from the
21817897SLin.Ling@Sun.COM 			 * namespace so that we can replace it with
21827897SLin.Ling@Sun.COM 			 * the correct config we just read in.
21837897SLin.Ling@Sun.COM 			 */
21847897SLin.Ling@Sun.COM 			ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
21857897SLin.Ling@Sun.COM 			spa_remove(spa);
21867897SLin.Ling@Sun.COM 		} else {
21877897SLin.Ling@Sun.COM 			mutex_exit(&spa_namespace_lock);
21887897SLin.Ling@Sun.COM 			return (EEXIST);
21897897SLin.Ling@Sun.COM 		}
2190789Sahrens 	}
2191789Sahrens 
2192789Sahrens 	/*
21931635Sbonwick 	 * Create and initialize the spa structure.
2194789Sahrens 	 */
21955094Slling 	(void) nvlist_lookup_string(props,
21965094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
21971635Sbonwick 	spa = spa_add(pool, altroot);
21988241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2199789Sahrens 
22006643Seschrock 	if (allowfaulted)
22016643Seschrock 		spa->spa_import_faulted = B_TRUE;
22026673Seschrock 	spa->spa_is_root = isroot;
22036643Seschrock 
2204789Sahrens 	/*
22051635Sbonwick 	 * Pass off the heavy lifting to spa_load().
22067046Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
22077046Sahrens 	 * the user-supplied config is actually the one to trust when
22087046Sahrens 	 * doing an import.
22091601Sbonwick 	 */
22107046Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2211789Sahrens 
22127754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22132082Seschrock 	/*
22142082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
22152082Seschrock 	 * and conflicts with spa_has_spare().
22162082Seschrock 	 */
22176423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
22185450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
22195450Sbrendan 		spa->spa_spares.sav_config = NULL;
22202082Seschrock 		spa_load_spares(spa);
22212082Seschrock 	}
22226423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
22235450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
22245450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
22255450Sbrendan 		spa_load_l2cache(spa);
22265450Sbrendan 	}
22272082Seschrock 
22282082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
22292082Seschrock 	    &nvroot) == 0);
22305450Sbrendan 	if (error == 0)
22315450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
22325450Sbrendan 	if (error == 0)
22335450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
22345450Sbrendan 		    VDEV_ALLOC_L2CACHE);
22357754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
22362082Seschrock 
22378525SEric.Schrock@Sun.COM 	if (props != NULL)
22388525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
22398525SEric.Schrock@Sun.COM 
22408241SJeff.Bonwick@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
22418241SJeff.Bonwick@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
22426643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
22436643Seschrock 			/*
22446643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
22456643Seschrock 			 * set, then manually set the config as if the config
22466643Seschrock 			 * passed in was specified in the cache file.
22476643Seschrock 			 */
22486643Seschrock 			error = 0;
22496643Seschrock 			spa->spa_import_faulted = B_FALSE;
22507754SJeff.Bonwick@Sun.COM 			if (spa->spa_config == NULL)
22516643Seschrock 				spa->spa_config = spa_config_generate(spa,
22526643Seschrock 				    NULL, -1ULL, B_TRUE);
22536643Seschrock 			spa_unload(spa);
22546643Seschrock 			spa_deactivate(spa);
22556643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
22566643Seschrock 		} else {
22576643Seschrock 			spa_unload(spa);
22586643Seschrock 			spa_deactivate(spa);
22596643Seschrock 			spa_remove(spa);
22606643Seschrock 		}
2261789Sahrens 		mutex_exit(&spa_namespace_lock);
2262789Sahrens 		return (error);
2263789Sahrens 	}
2264789Sahrens 
22651635Sbonwick 	/*
22665450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
22675450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
22682082Seschrock 	 */
22692082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
22702082Seschrock 	    &spares, &nspares) == 0) {
22715450Sbrendan 		if (spa->spa_spares.sav_config)
22725450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
22732082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
22742082Seschrock 		else
22755450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
22762082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
22775450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
22782082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
22797754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22802082Seschrock 		spa_load_spares(spa);
22817754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22825450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
22835450Sbrendan 	}
22845450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
22855450Sbrendan 	    &l2cache, &nl2cache) == 0) {
22865450Sbrendan 		if (spa->spa_l2cache.sav_config)
22875450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
22885450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
22895450Sbrendan 		else
22905450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
22915450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
22925450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
22935450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
22947754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22955450Sbrendan 		spa_load_l2cache(spa);
22967754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
22975450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
22982082Seschrock 	}
22992082Seschrock 
23008241SJeff.Bonwick@Sun.COM 	if (spa_writeable(spa)) {
23016643Seschrock 		/*
23026643Seschrock 		 * Update the config cache to include the newly-imported pool.
23036643Seschrock 		 */
23046423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
23056643Seschrock 	}
23066643Seschrock 
23076643Seschrock 	spa->spa_import_faulted = B_FALSE;
23084451Seschrock 	mutex_exit(&spa_namespace_lock);
23094451Seschrock 
2310789Sahrens 	return (0);
2311789Sahrens }
2312789Sahrens 
23136423Sgw25295 #ifdef _KERNEL
23146423Sgw25295 /*
23156423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
23166423Sgw25295  * device label.
23176423Sgw25295  */
23186423Sgw25295 static void
23196423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
23206423Sgw25295 {
23216423Sgw25295 	nvlist_t *nvtop, *nvroot;
23226423Sgw25295 	uint64_t pgid;
23236423Sgw25295 
23246423Sgw25295 	/*
23256423Sgw25295 	 * Add this top-level vdev to the child array.
23266423Sgw25295 	 */
23276423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
23286423Sgw25295 	    == 0);
23296423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
23306423Sgw25295 	    == 0);
23316423Sgw25295 
23326423Sgw25295 	/*
23336423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
23346423Sgw25295 	 */
23356423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
23366423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
23376423Sgw25295 	    == 0);
23386423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
23396423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
23406423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
23416423Sgw25295 	    &nvtop, 1) == 0);
23426423Sgw25295 
23436423Sgw25295 	/*
23446423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
23456423Sgw25295 	 * this pool's configuration (remove the old, add the new).
23466423Sgw25295 	 */
23476423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
23486423Sgw25295 	nvlist_free(nvroot);
23496423Sgw25295 }
23506423Sgw25295 
23516423Sgw25295 /*
23526423Sgw25295  * Get the root pool information from the root disk, then import the root pool
23536423Sgw25295  * during the system boot up time.
23546423Sgw25295  */
23557539SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
23567147Staylor 
23577147Staylor int
23587147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
23596423Sgw25295     uint64_t *besttxg)
23606423Sgw25295 {
23616423Sgw25295 	nvlist_t *config;
23626423Sgw25295 	uint64_t txg;
23637539SLin.Ling@Sun.COM 	int error;
23647539SLin.Ling@Sun.COM 
23657539SLin.Ling@Sun.COM 	if (error = vdev_disk_read_rootlabel(devpath, devid, &config))
23667539SLin.Ling@Sun.COM 		return (error);
23676423Sgw25295 
23686423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
23696423Sgw25295 
23707147Staylor 	if (bestconf != NULL)
23716423Sgw25295 		*bestconf = config;
23727539SLin.Ling@Sun.COM 	else
23737539SLin.Ling@Sun.COM 		nvlist_free(config);
23747147Staylor 	*besttxg = txg;
23757147Staylor 	return (0);
23766423Sgw25295 }
23776423Sgw25295 
23786423Sgw25295 boolean_t
23796423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
23806423Sgw25295 {
23816423Sgw25295 	uint64_t ival;
23826423Sgw25295 
23836423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
23846423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
23856423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
23866423Sgw25295 		return (B_FALSE);
23876423Sgw25295 
23886423Sgw25295 	return (B_TRUE);
23896423Sgw25295 }
23906423Sgw25295 
23917147Staylor 
23927147Staylor /*
23937147Staylor  * Given the boot device's physical path or devid, check if the device
23947147Staylor  * is in a valid state.  If so, return the configuration from the vdev
23957147Staylor  * label.
23967147Staylor  */
23977147Staylor int
23987147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
23997147Staylor {
24007147Staylor 	nvlist_t *conf = NULL;
24017147Staylor 	uint64_t txg = 0;
24027147Staylor 	nvlist_t *nvtop, **child;
24037147Staylor 	char *type;
24047147Staylor 	char *bootpath = NULL;
24057147Staylor 	uint_t children, c;
24067147Staylor 	char *tmp;
24077539SLin.Ling@Sun.COM 	int error;
24087147Staylor 
24097147Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
24107147Staylor 		*tmp = '\0';
24117539SLin.Ling@Sun.COM 	if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) {
24127147Staylor 		cmn_err(CE_NOTE, "error reading device label");
24137539SLin.Ling@Sun.COM 		return (error);
24147147Staylor 	}
24157147Staylor 	if (txg == 0) {
24167147Staylor 		cmn_err(CE_NOTE, "this device is detached");
24177147Staylor 		nvlist_free(conf);
24187147Staylor 		return (EINVAL);
24197147Staylor 	}
24207147Staylor 
24217147Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
24227147Staylor 	    &nvtop) == 0);
24237147Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
24247147Staylor 
24257147Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
24267147Staylor 		if (spa_rootdev_validate(nvtop)) {
24277147Staylor 			goto out;
24287147Staylor 		} else {
24297147Staylor 			nvlist_free(conf);
24307147Staylor 			return (EINVAL);
24317147Staylor 		}
24327147Staylor 	}
24337147Staylor 
24347147Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
24357147Staylor 
24367147Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
24377147Staylor 	    &child, &children) == 0);
24387147Staylor 
24397147Staylor 	/*
24407147Staylor 	 * Go thru vdevs in the mirror to see if the given device
24417147Staylor 	 * has the most recent txg. Only the device with the most
24427147Staylor 	 * recent txg has valid information and should be booted.
24437147Staylor 	 */
24447147Staylor 	for (c = 0; c < children; c++) {
24457147Staylor 		char *cdevid, *cpath;
24467147Staylor 		uint64_t tmptxg;
24477147Staylor 
24488242SLin.Ling@Sun.COM 		cpath = NULL;
24498242SLin.Ling@Sun.COM 		cdevid = NULL;
24507147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
24518242SLin.Ling@Sun.COM 		    &cpath) != 0 && nvlist_lookup_string(child[c],
24528242SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_DEVID, &cdevid) != 0)
24537147Staylor 			return (EINVAL);
24547687SLin.Ling@Sun.COM 		if ((spa_check_rootconf(cpath, cdevid, NULL,
24557687SLin.Ling@Sun.COM 		    &tmptxg) == 0) && (tmptxg > txg)) {
24567147Staylor 			txg = tmptxg;
24577147Staylor 			VERIFY(nvlist_lookup_string(child[c],
24587147Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
24597147Staylor 		}
24607147Staylor 	}
24617147Staylor 
24627147Staylor 	/* Does the best device match the one we've booted from? */
24637147Staylor 	if (bootpath) {
24647147Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
24657147Staylor 		return (EINVAL);
24667147Staylor 	}
24677147Staylor out:
24687147Staylor 	*bestconf = conf;
24697147Staylor 	return (0);
24707147Staylor }
24717147Staylor 
24726423Sgw25295 /*
24736423Sgw25295  * Import a root pool.
24746423Sgw25295  *
24757147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
24767147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
24777147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
24786423Sgw25295  *
24796423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
24806423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
24816423Sgw25295  * e.g.
24826423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
24836423Sgw25295  */
24846423Sgw25295 int
24857147Staylor spa_import_rootpool(char *devpath, char *devid)
24866423Sgw25295 {
24876423Sgw25295 	nvlist_t *conf = NULL;
24886423Sgw25295 	char *pname;
24896423Sgw25295 	int error;
24906423Sgw25295 
24916423Sgw25295 	/*
24926423Sgw25295 	 * Get the vdev pathname and configuation from the most
24936423Sgw25295 	 * recently updated vdev (highest txg).
24946423Sgw25295 	 */
24957147Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
24966423Sgw25295 		goto msg_out;
24976423Sgw25295 
24986423Sgw25295 	/*
24996423Sgw25295 	 * Add type "root" vdev to the config.
25006423Sgw25295 	 */
25016423Sgw25295 	spa_build_rootpool_config(conf);
25026423Sgw25295 
25036423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
25046423Sgw25295 
25056673Seschrock 	/*
25066673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
25076673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
25086673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
25096673Seschrock 	 * pool open, not import.
25106673Seschrock 	 */
25116673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
25127897SLin.Ling@Sun.COM 	ASSERT(error != EEXIST);
25136423Sgw25295 
25146423Sgw25295 	nvlist_free(conf);
25156423Sgw25295 	return (error);
25166423Sgw25295 
25176423Sgw25295 msg_out:
25187147Staylor 	cmn_err(CE_NOTE, "\n"
25196423Sgw25295 	    "  ***************************************************  \n"
25206423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
25216423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
25226423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
25237147Staylor 	    "  ***************************************************  ");
25246423Sgw25295 
25256423Sgw25295 	return (error);
25266423Sgw25295 }
25276423Sgw25295 #endif
25286423Sgw25295 
25296423Sgw25295 /*
25306423Sgw25295  * Import a non-root pool into the system.
25316423Sgw25295  */
25326423Sgw25295 int
25336423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
25346423Sgw25295 {
25356643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
25366423Sgw25295 }
25376423Sgw25295 
25386643Seschrock int
25396643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
25406643Seschrock {
25416643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
25426643Seschrock }
25436643Seschrock 
25446643Seschrock 
2545789Sahrens /*
2546789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2547789Sahrens  * to get the vdev stats associated with the imported devices.
2548789Sahrens  */
2549789Sahrens #define	TRYIMPORT_NAME	"$import"
2550789Sahrens 
2551789Sahrens nvlist_t *
2552789Sahrens spa_tryimport(nvlist_t *tryconfig)
2553789Sahrens {
2554789Sahrens 	nvlist_t *config = NULL;
2555789Sahrens 	char *poolname;
2556789Sahrens 	spa_t *spa;
2557789Sahrens 	uint64_t state;
2558789Sahrens 
2559789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2560789Sahrens 		return (NULL);
2561789Sahrens 
2562789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2563789Sahrens 		return (NULL);
2564789Sahrens 
25651635Sbonwick 	/*
25661635Sbonwick 	 * Create and initialize the spa structure.
25671635Sbonwick 	 */
2568789Sahrens 	mutex_enter(&spa_namespace_lock);
25691635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
25708241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
2571789Sahrens 
2572789Sahrens 	/*
25731635Sbonwick 	 * Pass off the heavy lifting to spa_load().
25741732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
25751732Sbonwick 	 * is actually the one to trust when doing an import.
2576789Sahrens 	 */
25771732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2578789Sahrens 
2579789Sahrens 	/*
2580789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2581789Sahrens 	 */
2582789Sahrens 	if (spa->spa_root_vdev != NULL) {
2583789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2584789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2585789Sahrens 		    poolname) == 0);
2586789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2587789Sahrens 		    state) == 0);
25883975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
25893975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
25902082Seschrock 
25912082Seschrock 		/*
25926423Sgw25295 		 * If the bootfs property exists on this pool then we
25936423Sgw25295 		 * copy it out so that external consumers can tell which
25946423Sgw25295 		 * pools are bootable.
25956423Sgw25295 		 */
25966423Sgw25295 		if (spa->spa_bootfs) {
25976423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
25986423Sgw25295 
25996423Sgw25295 			/*
26006423Sgw25295 			 * We have to play games with the name since the
26016423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
26026423Sgw25295 			 */
26037754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
26046423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
26056423Sgw25295 				char *cp;
26066423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
26076423Sgw25295 
26086423Sgw25295 				cp = strchr(tmpname, '/');
26096423Sgw25295 				if (cp == NULL) {
26106423Sgw25295 					(void) strlcpy(dsname, tmpname,
26116423Sgw25295 					    MAXPATHLEN);
26126423Sgw25295 				} else {
26136423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
26146423Sgw25295 					    "%s/%s", poolname, ++cp);
26156423Sgw25295 				}
26166423Sgw25295 				VERIFY(nvlist_add_string(config,
26176423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
26186423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
26196423Sgw25295 			}
26206423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
26216423Sgw25295 		}
26226423Sgw25295 
26236423Sgw25295 		/*
26245450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
26252082Seschrock 		 */
26262082Seschrock 		spa_add_spares(spa, config);
26275450Sbrendan 		spa_add_l2cache(spa, config);
2628789Sahrens 	}
2629789Sahrens 
2630789Sahrens 	spa_unload(spa);
2631789Sahrens 	spa_deactivate(spa);
2632789Sahrens 	spa_remove(spa);
2633789Sahrens 	mutex_exit(&spa_namespace_lock);
2634789Sahrens 
2635789Sahrens 	return (config);
2636789Sahrens }
2637789Sahrens 
2638789Sahrens /*
2639789Sahrens  * Pool export/destroy
2640789Sahrens  *
2641789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2642789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2643789Sahrens  * update the pool state and sync all the labels to disk, removing the
26448211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
26458211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
2646789Sahrens  */
2647789Sahrens static int
26487214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
26498211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
2650789Sahrens {
2651789Sahrens 	spa_t *spa;
2652789Sahrens 
26531775Sbillm 	if (oldconfig)
26541775Sbillm 		*oldconfig = NULL;
26551775Sbillm 
26568241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
2657789Sahrens 		return (EROFS);
2658789Sahrens 
2659789Sahrens 	mutex_enter(&spa_namespace_lock);
2660789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2661789Sahrens 		mutex_exit(&spa_namespace_lock);
2662789Sahrens 		return (ENOENT);
2663789Sahrens 	}
2664789Sahrens 
2665789Sahrens 	/*
26661544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
26671544Seschrock 	 * reacquire the namespace lock, and see if we can export.
26681544Seschrock 	 */
26691544Seschrock 	spa_open_ref(spa, FTAG);
26701544Seschrock 	mutex_exit(&spa_namespace_lock);
26711544Seschrock 	spa_async_suspend(spa);
26721544Seschrock 	mutex_enter(&spa_namespace_lock);
26731544Seschrock 	spa_close(spa, FTAG);
26741544Seschrock 
26751544Seschrock 	/*
2676789Sahrens 	 * The pool will be in core if it's openable,
2677789Sahrens 	 * in which case we can modify its state.
2678789Sahrens 	 */
2679789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2680789Sahrens 		/*
2681789Sahrens 		 * Objsets may be open only because they're dirty, so we
2682789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2683789Sahrens 		 */
2684789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2685789Sahrens 
26861544Seschrock 		/*
26871544Seschrock 		 * A pool cannot be exported or destroyed if there are active
26881544Seschrock 		 * references.  If we are resetting a pool, allow references by
26891544Seschrock 		 * fault injection handlers.
26901544Seschrock 		 */
26911544Seschrock 		if (!spa_refcount_zero(spa) ||
26921544Seschrock 		    (spa->spa_inject_ref != 0 &&
26931544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
26941544Seschrock 			spa_async_resume(spa);
2695789Sahrens 			mutex_exit(&spa_namespace_lock);
2696789Sahrens 			return (EBUSY);
2697789Sahrens 		}
2698789Sahrens 
2699789Sahrens 		/*
27007214Slling 		 * A pool cannot be exported if it has an active shared spare.
27017214Slling 		 * This is to prevent other pools stealing the active spare
27027214Slling 		 * from an exported pool. At user's own will, such pool can
27037214Slling 		 * be forcedly exported.
27047214Slling 		 */
27057214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
27067214Slling 		    spa_has_active_shared_spare(spa)) {
27077214Slling 			spa_async_resume(spa);
27087214Slling 			mutex_exit(&spa_namespace_lock);
27097214Slling 			return (EXDEV);
27107214Slling 		}
27117214Slling 
27127214Slling 		/*
2713789Sahrens 		 * We want this to be reflected on every label,
2714789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2715789Sahrens 		 * final sync that pushes these changes out.
2716789Sahrens 		 */
27178211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
27187754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27191544Seschrock 			spa->spa_state = new_state;
27201635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
27211544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
27227754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
27231544Seschrock 		}
2724789Sahrens 	}
2725789Sahrens 
27264451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
27274451Seschrock 
2728789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2729789Sahrens 		spa_unload(spa);
2730789Sahrens 		spa_deactivate(spa);
2731789Sahrens 	}
2732789Sahrens 
27331775Sbillm 	if (oldconfig && spa->spa_config)
27341775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
27351775Sbillm 
27361544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
27378211SGeorge.Wilson@Sun.COM 		if (!hardforce)
27388211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
27391544Seschrock 		spa_remove(spa);
27401544Seschrock 	}
2741789Sahrens 	mutex_exit(&spa_namespace_lock);
2742789Sahrens 
2743789Sahrens 	return (0);
2744789Sahrens }
2745789Sahrens 
2746789Sahrens /*
2747789Sahrens  * Destroy a storage pool.
2748789Sahrens  */
2749789Sahrens int
2750789Sahrens spa_destroy(char *pool)
2751789Sahrens {
27528211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
27538211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
2754789Sahrens }
2755789Sahrens 
2756789Sahrens /*
2757789Sahrens  * Export a storage pool.
2758789Sahrens  */
2759789Sahrens int
27608211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
27618211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
2762789Sahrens {
27638211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
27648211SGeorge.Wilson@Sun.COM 	    force, hardforce));
2765789Sahrens }
2766789Sahrens 
2767789Sahrens /*
27681544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
27691544Seschrock  * from the namespace in any way.
27701544Seschrock  */
27711544Seschrock int
27721544Seschrock spa_reset(char *pool)
27731544Seschrock {
27747214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
27758211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
27761544Seschrock }
27771544Seschrock 
27781544Seschrock /*
2779789Sahrens  * ==========================================================================
2780789Sahrens  * Device manipulation
2781789Sahrens  * ==========================================================================
2782789Sahrens  */
2783789Sahrens 
2784789Sahrens /*
27854527Sperrin  * Add a device to a storage pool.
2786789Sahrens  */
2787789Sahrens int
2788789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2789789Sahrens {
2790789Sahrens 	uint64_t txg;
27918241SJeff.Bonwick@Sun.COM 	int error;
2792789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
27931585Sbonwick 	vdev_t *vd, *tvd;
27945450Sbrendan 	nvlist_t **spares, **l2cache;
27955450Sbrendan 	uint_t nspares, nl2cache;
2796789Sahrens 
2797789Sahrens 	txg = spa_vdev_enter(spa);
2798789Sahrens 
27992082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
28002082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
28012082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
28022082Seschrock 
28037754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
2804789Sahrens 
28055450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
28065450Sbrendan 	    &nspares) != 0)
28072082Seschrock 		nspares = 0;
28082082Seschrock 
28095450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
28105450Sbrendan 	    &nl2cache) != 0)
28115450Sbrendan 		nl2cache = 0;
28125450Sbrendan 
28137754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
28142082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
28157754SJeff.Bonwick@Sun.COM 
28167754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
28177754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
28187754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
28192082Seschrock 
28203377Seschrock 	/*
28215450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
28225450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
28233377Seschrock 	 */
28247754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
28253377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
28263377Seschrock 
28273377Seschrock 	/*
28283377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
28293377Seschrock 	 */
28308241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
28313377Seschrock 		tvd = vd->vdev_child[c];
28323377Seschrock 		vdev_remove_child(vd, tvd);
28333377Seschrock 		tvd->vdev_id = rvd->vdev_children;
28343377Seschrock 		vdev_add_child(rvd, tvd);
28353377Seschrock 		vdev_config_dirty(tvd);
28363377Seschrock 	}
28373377Seschrock 
28382082Seschrock 	if (nspares != 0) {
28395450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
28405450Sbrendan 		    ZPOOL_CONFIG_SPARES);
28412082Seschrock 		spa_load_spares(spa);
28425450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
28435450Sbrendan 	}
28445450Sbrendan 
28455450Sbrendan 	if (nl2cache != 0) {
28465450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
28475450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
28485450Sbrendan 		spa_load_l2cache(spa);
28495450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2850789Sahrens 	}
2851789Sahrens 
2852789Sahrens 	/*
28531585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
28541585Sbonwick 	 * If other threads start allocating from these vdevs before we
28551585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
28561585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
28571585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
28581585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
28591635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
28601585Sbonwick 	 *
28611585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
28621585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
28631585Sbonwick 	 * steps will be completed the next time we load the pool.
2864789Sahrens 	 */
28651635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
28661585Sbonwick 
28671635Sbonwick 	mutex_enter(&spa_namespace_lock);
28681635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
28691635Sbonwick 	mutex_exit(&spa_namespace_lock);
2870789Sahrens 
28711635Sbonwick 	return (0);
2872789Sahrens }
2873789Sahrens 
2874789Sahrens /*
2875789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2876789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2877789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2878789Sahrens  *
2879789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2880789Sahrens  * existing device; in this case the two devices are made into their own
28814451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2882789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2883789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2884789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2885789Sahrens  * is automatically detached.
2886789Sahrens  */
2887789Sahrens int
28881544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2889789Sahrens {
2890789Sahrens 	uint64_t txg, open_txg;
2891789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2892789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
28932082Seschrock 	vdev_ops_t *pvops;
28947313SEric.Kustarz@Sun.COM 	dmu_tx_t *tx;
28957313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
28967313SEric.Kustarz@Sun.COM 	int newvd_isspare;
28977313SEric.Kustarz@Sun.COM 	int error;
2898789Sahrens 
2899789Sahrens 	txg = spa_vdev_enter(spa);
2900789Sahrens 
29016643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2902789Sahrens 
2903789Sahrens 	if (oldvd == NULL)
2904789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2905789Sahrens 
29061585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
29071585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29081585Sbonwick 
2909789Sahrens 	pvd = oldvd->vdev_parent;
2910789Sahrens 
29112082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
29124451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
29134451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
29144451Seschrock 
29154451Seschrock 	if (newrootvd->vdev_children != 1)
2916789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2917789Sahrens 
2918789Sahrens 	newvd = newrootvd->vdev_child[0];
2919789Sahrens 
2920789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2921789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2922789Sahrens 
29232082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2924789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2925789Sahrens 
29264527Sperrin 	/*
29274527Sperrin 	 * Spares can't replace logs
29284527Sperrin 	 */
29297326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
29304527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29314527Sperrin 
29322082Seschrock 	if (!replacing) {
29332082Seschrock 		/*
29342082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
29352082Seschrock 		 * vdev.
29362082Seschrock 		 */
29372082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
29382082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
29392082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29402082Seschrock 
29412082Seschrock 		pvops = &vdev_mirror_ops;
29422082Seschrock 	} else {
29432082Seschrock 		/*
29442082Seschrock 		 * Active hot spares can only be replaced by inactive hot
29452082Seschrock 		 * spares.
29462082Seschrock 		 */
29472082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
29482082Seschrock 		    pvd->vdev_child[1] == oldvd &&
29492082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
29502082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29512082Seschrock 
29522082Seschrock 		/*
29532082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
29542082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
29553377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
29563377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
29573377Seschrock 		 * the same (spare replaces spare, non-spare replaces
29583377Seschrock 		 * non-spare).
29592082Seschrock 		 */
29602082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
29612082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29623377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
29633377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
29643377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29652082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
29662082Seschrock 		    newvd->vdev_isspare)
29672082Seschrock 			pvops = &vdev_spare_ops;
29682082Seschrock 		else
29692082Seschrock 			pvops = &vdev_replacing_ops;
29702082Seschrock 	}
29712082Seschrock 
29721175Slling 	/*
29731175Slling 	 * Compare the new device size with the replaceable/attachable
29741175Slling 	 * device size.
29751175Slling 	 */
29761175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2977789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2978789Sahrens 
29791732Sbonwick 	/*
29801732Sbonwick 	 * The new device cannot have a higher alignment requirement
29811732Sbonwick 	 * than the top-level vdev.
29821732Sbonwick 	 */
29831732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2984789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2985789Sahrens 
2986789Sahrens 	/*
2987789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2988789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2989789Sahrens 	 */
2990789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2991789Sahrens 		spa_strfree(oldvd->vdev_path);
2992789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2993789Sahrens 		    KM_SLEEP);
2994789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2995789Sahrens 		    newvd->vdev_path, "old");
2996789Sahrens 		if (oldvd->vdev_devid != NULL) {
2997789Sahrens 			spa_strfree(oldvd->vdev_devid);
2998789Sahrens 			oldvd->vdev_devid = NULL;
2999789Sahrens 		}
3000789Sahrens 	}
3001789Sahrens 
3002789Sahrens 	/*
30032082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
30042082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3005789Sahrens 	 */
3006789Sahrens 	if (pvd->vdev_ops != pvops)
3007789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3008789Sahrens 
3009789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3010789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3011789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3012789Sahrens 
3013789Sahrens 	/*
3014789Sahrens 	 * Extract the new device from its root and add it to pvd.
3015789Sahrens 	 */
3016789Sahrens 	vdev_remove_child(newrootvd, newvd);
3017789Sahrens 	newvd->vdev_id = pvd->vdev_children;
3018789Sahrens 	vdev_add_child(pvd, newvd);
3019789Sahrens 
30201544Seschrock 	/*
30211544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
30221544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
30231544Seschrock 	 */
30241544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
30251544Seschrock 
3026789Sahrens 	tvd = newvd->vdev_top;
3027789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3028789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3029789Sahrens 
3030789Sahrens 	vdev_config_dirty(tvd);
3031789Sahrens 
3032789Sahrens 	/*
3033789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3034789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3035789Sahrens 	 */
3036789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3037789Sahrens 
30388241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
30398241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3040789Sahrens 
30413377Seschrock 	if (newvd->vdev_isspare)
30423377Seschrock 		spa_spare_activate(newvd);
30437754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
30447754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
30457313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
30461544Seschrock 
3047789Sahrens 	/*
3048789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3049789Sahrens 	 */
30501732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3051789Sahrens 
3052789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3053789Sahrens 
30547313SEric.Kustarz@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
30557313SEric.Kustarz@Sun.COM 	if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
30567313SEric.Kustarz@Sun.COM 		spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
30577313SEric.Kustarz@Sun.COM 		    CRED(),  "%s vdev=%s %s vdev=%s",
30587313SEric.Kustarz@Sun.COM 		    replacing && newvd_isspare ? "spare in" :
30597313SEric.Kustarz@Sun.COM 		    replacing ? "replace" : "attach", newvdpath,
30607313SEric.Kustarz@Sun.COM 		    replacing ? "for" : "to", oldvdpath);
30617313SEric.Kustarz@Sun.COM 		dmu_tx_commit(tx);
30627313SEric.Kustarz@Sun.COM 	} else {
30637313SEric.Kustarz@Sun.COM 		dmu_tx_abort(tx);
30647313SEric.Kustarz@Sun.COM 	}
30657313SEric.Kustarz@Sun.COM 
30667313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
30677313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
30687313SEric.Kustarz@Sun.COM 
3069789Sahrens 	/*
30707046Sahrens 	 * Kick off a resilver to update newvd.
3071789Sahrens 	 */
30727046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3073789Sahrens 
3074789Sahrens 	return (0);
3075789Sahrens }
3076789Sahrens 
3077789Sahrens /*
3078789Sahrens  * Detach a device from a mirror or replacing vdev.
3079789Sahrens  * If 'replace_done' is specified, only detach if the parent
3080789Sahrens  * is a replacing vdev.
3081789Sahrens  */
3082789Sahrens int
30838241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3084789Sahrens {
3085789Sahrens 	uint64_t txg;
30868241SJeff.Bonwick@Sun.COM 	int error;
3087789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3088789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
30892082Seschrock 	boolean_t unspare = B_FALSE;
30902082Seschrock 	uint64_t unspare_guid;
30916673Seschrock 	size_t len;
3092789Sahrens 
3093789Sahrens 	txg = spa_vdev_enter(spa);
3094789Sahrens 
30956643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3096789Sahrens 
3097789Sahrens 	if (vd == NULL)
3098789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3099789Sahrens 
31001585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
31011585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31021585Sbonwick 
3103789Sahrens 	pvd = vd->vdev_parent;
3104789Sahrens 
3105789Sahrens 	/*
31068241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
31078241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
31088241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
31098241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
31108241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
31118241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
31128241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
31138241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
31148241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
31158241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
31168241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
31178241SJeff.Bonwick@Sun.COM 	 */
31188241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
31198241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
31208241SJeff.Bonwick@Sun.COM 
31218241SJeff.Bonwick@Sun.COM 	/*
3122789Sahrens 	 * If replace_done is specified, only remove this device if it's
31232082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
31242082Seschrock 	 * disk can be removed.
3125789Sahrens 	 */
31262082Seschrock 	if (replace_done) {
31272082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
31282082Seschrock 			if (vd->vdev_id != 0)
31292082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31302082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
31312082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31322082Seschrock 		}
31332082Seschrock 	}
31342082Seschrock 
31352082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
31364577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3137789Sahrens 
3138789Sahrens 	/*
31392082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3140789Sahrens 	 */
3141789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
31422082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
31432082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3144789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3145789Sahrens 
3146789Sahrens 	/*
31478241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
31488241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3149789Sahrens 	 */
31508241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3151789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3152789Sahrens 
31538241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
31548241SJeff.Bonwick@Sun.COM 
3155789Sahrens 	/*
31566673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
31576673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
31586673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
31596673Seschrock 	 */
31606673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
31616673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
31626673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
31636673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
31646673Seschrock 		cvd = pvd->vdev_child[0];
31656673Seschrock 		len = strlen(vd->vdev_path);
31666673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
31676673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
31686673Seschrock 			spa_strfree(cvd->vdev_path);
31696673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
31706673Seschrock 		}
31716673Seschrock 	}
31726673Seschrock 
31736673Seschrock 	/*
31742082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
31752082Seschrock 	 * that the spare should become a real disk, and be removed from the
31762082Seschrock 	 * active spare list for the pool.
31772082Seschrock 	 */
31782082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
31798241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
31802082Seschrock 		unspare = B_TRUE;
31812082Seschrock 
31822082Seschrock 	/*
3183789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3184789Sahrens 	 * This must be done after all other error cases are handled,
3185789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3186789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3187789Sahrens 	 * it may be that the unwritability of the disk is the reason
3188789Sahrens 	 * it's being detached!
3189789Sahrens 	 */
31903377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3191789Sahrens 
3192789Sahrens 	/*
3193789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3194789Sahrens 	 */
3195789Sahrens 	vdev_remove_child(pvd, vd);
3196789Sahrens 	vdev_compact_children(pvd);
3197789Sahrens 
3198789Sahrens 	/*
3199789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3200789Sahrens 	 */
3201789Sahrens 	cvd = pvd->vdev_child[0];
3202789Sahrens 
3203789Sahrens 	/*
32042082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
32058241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
32068241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
32078241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
32088241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
32098241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
32108241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
32112082Seschrock 	 */
32122082Seschrock 	if (unspare) {
32132082Seschrock 		ASSERT(cvd->vdev_isspare);
32143377Seschrock 		spa_spare_remove(cvd);
32152082Seschrock 		unspare_guid = cvd->vdev_guid;
32168241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
32172082Seschrock 	}
32182082Seschrock 
32192082Seschrock 	/*
3220789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3221789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3222789Sahrens 	 */
3223789Sahrens 	if (pvd->vdev_children == 1)
3224789Sahrens 		vdev_remove_parent(cvd);
3225789Sahrens 
3226789Sahrens 	/*
3227789Sahrens 	 * We don't set tvd until now because the parent we just removed
3228789Sahrens 	 * may have been the previous top-level vdev.
3229789Sahrens 	 */
3230789Sahrens 	tvd = cvd->vdev_top;
3231789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3232789Sahrens 
3233789Sahrens 	/*
32343377Seschrock 	 * Reevaluate the parent vdev state.
3235789Sahrens 	 */
32364451Seschrock 	vdev_propagate_state(cvd);
3237789Sahrens 
3238789Sahrens 	/*
32393377Seschrock 	 * If the device we just detached was smaller than the others, it may be
32403377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
32413377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
32423377Seschrock 	 * there's nothing to read from disk.
3243789Sahrens 	 */
32441732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3245789Sahrens 
3246789Sahrens 	vdev_config_dirty(tvd);
3247789Sahrens 
3248789Sahrens 	/*
32493377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
32503377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
32513377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
32523377Seschrock 	 * prevent vd from being accessed after it's freed.
3253789Sahrens 	 */
32548241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3255789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
32561732Sbonwick 	vd->vdev_detached = B_TRUE;
32571732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3258789Sahrens 
32594451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
32604451Seschrock 
32612082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
32622082Seschrock 
32632082Seschrock 	/*
32643377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
32653377Seschrock 	 * then we want to go through and remove the device from the hot spare
32663377Seschrock 	 * list of every other pool.
32672082Seschrock 	 */
32682082Seschrock 	if (unspare) {
32698241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
32702082Seschrock 		spa = NULL;
32712082Seschrock 		mutex_enter(&spa_namespace_lock);
32722082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
32732082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
32742082Seschrock 				continue;
32758241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
32768241SJeff.Bonwick@Sun.COM 				continue;
32777793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
32787793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
32792082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
32807793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
32817793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
32822082Seschrock 		}
32832082Seschrock 		mutex_exit(&spa_namespace_lock);
32842082Seschrock 	}
32852082Seschrock 
32862082Seschrock 	return (error);
32872082Seschrock }
32882082Seschrock 
32897754SJeff.Bonwick@Sun.COM static nvlist_t *
32907754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
32912082Seschrock {
32927754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
32937754SJeff.Bonwick@Sun.COM 		uint64_t guid;
32947754SJeff.Bonwick@Sun.COM 
32957754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
32967754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
32977754SJeff.Bonwick@Sun.COM 
32987754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
32997754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
33002082Seschrock 	}
33012082Seschrock 
33027754SJeff.Bonwick@Sun.COM 	return (NULL);
33035450Sbrendan }
33045450Sbrendan 
33057754SJeff.Bonwick@Sun.COM static void
33067754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
33077754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
33085450Sbrendan {
33097754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
33107754SJeff.Bonwick@Sun.COM 
33117754SJeff.Bonwick@Sun.COM 	if (count > 1)
33127754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
33137754SJeff.Bonwick@Sun.COM 
33147754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
33157754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
33167754SJeff.Bonwick@Sun.COM 			continue;
33177754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
33185450Sbrendan 	}
33195450Sbrendan 
33207754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
33217754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
33227754SJeff.Bonwick@Sun.COM 
33237754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
33247754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
33257754SJeff.Bonwick@Sun.COM 
33267754SJeff.Bonwick@Sun.COM 	if (count > 1)
33277754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
33285450Sbrendan }
33295450Sbrendan 
33305450Sbrendan /*
33315450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
33325450Sbrendan  * spares and level 2 ARC devices.
33335450Sbrendan  */
33345450Sbrendan int
33355450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
33365450Sbrendan {
33375450Sbrendan 	vdev_t *vd;
33387754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
33395450Sbrendan 	uint_t nspares, nl2cache;
33408241SJeff.Bonwick@Sun.COM 	uint64_t txg = 0;
33415450Sbrendan 	int error = 0;
33428241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
33438241SJeff.Bonwick@Sun.COM 
33448241SJeff.Bonwick@Sun.COM 	if (!locked)
33458241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
33465450Sbrendan 
33476643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
33485450Sbrendan 
33495450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
33505450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
33517754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
33527754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
33537754SJeff.Bonwick@Sun.COM 		/*
33547754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
33557754SJeff.Bonwick@Sun.COM 		 * in this pool.
33567754SJeff.Bonwick@Sun.COM 		 */
33577754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
33587754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
33597754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
33607754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
33617754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
33627754SJeff.Bonwick@Sun.COM 		} else {
33637754SJeff.Bonwick@Sun.COM 			error = EBUSY;
33647754SJeff.Bonwick@Sun.COM 		}
33657754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
33665450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
33677754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
33687754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
33697754SJeff.Bonwick@Sun.COM 		/*
33707754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
33717754SJeff.Bonwick@Sun.COM 		 */
33727754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
33737754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
33745450Sbrendan 		spa_load_l2cache(spa);
33755450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
33767754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
33777754SJeff.Bonwick@Sun.COM 		/*
33787754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
33797754SJeff.Bonwick@Sun.COM 		 */
33807754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
33817754SJeff.Bonwick@Sun.COM 	} else {
33827754SJeff.Bonwick@Sun.COM 		/*
33837754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
33847754SJeff.Bonwick@Sun.COM 		 */
33857754SJeff.Bonwick@Sun.COM 		error = ENOENT;
33865450Sbrendan 	}
33872082Seschrock 
33888241SJeff.Bonwick@Sun.COM 	if (!locked)
33898241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
33908241SJeff.Bonwick@Sun.COM 
33918241SJeff.Bonwick@Sun.COM 	return (error);
3392789Sahrens }
3393789Sahrens 
3394789Sahrens /*
33954451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
33964451Seschrock  * current spared, so we can detach it.
3397789Sahrens  */
33981544Seschrock static vdev_t *
33994451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3400789Sahrens {
34011544Seschrock 	vdev_t *newvd, *oldvd;
3402789Sahrens 	int c;
3403789Sahrens 
34041544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
34054451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
34061544Seschrock 		if (oldvd != NULL)
34071544Seschrock 			return (oldvd);
34081544Seschrock 	}
3409789Sahrens 
34104451Seschrock 	/*
34114451Seschrock 	 * Check for a completed replacement.
34124451Seschrock 	 */
3413789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
34141544Seschrock 		oldvd = vd->vdev_child[0];
34151544Seschrock 		newvd = vd->vdev_child[1];
3416789Sahrens 
34178241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
34188241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
34191544Seschrock 			return (oldvd);
34201544Seschrock 	}
3421789Sahrens 
34224451Seschrock 	/*
34234451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
34244451Seschrock 	 */
34254451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
34264451Seschrock 		newvd = vd->vdev_child[0];
34274451Seschrock 		oldvd = vd->vdev_child[1];
34284451Seschrock 
34294451Seschrock 		if (newvd->vdev_unspare &&
34308241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
34318241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
34324451Seschrock 			newvd->vdev_unspare = 0;
34334451Seschrock 			return (oldvd);
34344451Seschrock 		}
34354451Seschrock 	}
34364451Seschrock 
34371544Seschrock 	return (NULL);
3438789Sahrens }
3439789Sahrens 
34401544Seschrock static void
34414451Seschrock spa_vdev_resilver_done(spa_t *spa)
3442789Sahrens {
34438241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
34448241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
34458241SJeff.Bonwick@Sun.COM 
34468241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3447789Sahrens 
34484451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
34498241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
34508241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
34511544Seschrock 		guid = vd->vdev_guid;
34528241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
34538241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
34548241SJeff.Bonwick@Sun.COM 		sguid = 0;
34552082Seschrock 		/*
34562082Seschrock 		 * If we have just finished replacing a hot spared device, then
34572082Seschrock 		 * we need to detach the parent's first child (the original hot
34582082Seschrock 		 * spare) as well.
34592082Seschrock 		 */
34608241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
34612082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
34628241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
34638241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
34642082Seschrock 		}
34658241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
34668241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
34671544Seschrock 			return;
34688241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
34692082Seschrock 			return;
34708241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3471789Sahrens 	}
3472789Sahrens 
34738241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
3474789Sahrens }
3475789Sahrens 
3476789Sahrens /*
34771354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
34781354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
34791354Seschrock  */
34801354Seschrock int
34811354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
34821354Seschrock {
34836643Seschrock 	vdev_t *vd;
34841354Seschrock 	uint64_t txg;
34851354Seschrock 
34861354Seschrock 	txg = spa_vdev_enter(spa);
34871354Seschrock 
34886643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
34892082Seschrock 		/*
34906643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
34916643Seschrock 		 * it is, update the path manually as there is no associated
34926643Seschrock 		 * vdev_t that can be synced to disk.
34932082Seschrock 		 */
34946643Seschrock 		nvlist_t **spares;
34956643Seschrock 		uint_t i, nspares;
34965450Sbrendan 
34975450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
34985450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
34995450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
35005450Sbrendan 			    &spares, &nspares) == 0);
35012082Seschrock 			for (i = 0; i < nspares; i++) {
35022082Seschrock 				uint64_t theguid;
35032082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
35042082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
35055450Sbrendan 				if (theguid == guid) {
35065450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
35075450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
35085450Sbrendan 					spa_load_spares(spa);
35095450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
35105450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
35115450Sbrendan 					    0));
35125450Sbrendan 				}
35132082Seschrock 			}
35142082Seschrock 		}
35155450Sbrendan 
35165450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
35172082Seschrock 	}
35181354Seschrock 
35191585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
35201585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35211585Sbonwick 
35221354Seschrock 	spa_strfree(vd->vdev_path);
35231354Seschrock 	vd->vdev_path = spa_strdup(newpath);
35241354Seschrock 
35251354Seschrock 	vdev_config_dirty(vd->vdev_top);
35261354Seschrock 
35271354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
35281354Seschrock }
35291354Seschrock 
35301354Seschrock /*
3531789Sahrens  * ==========================================================================
3532789Sahrens  * SPA Scrubbing
3533789Sahrens  * ==========================================================================
3534789Sahrens  */
3535789Sahrens 
35367046Sahrens int
35377046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3538789Sahrens {
35397754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
35404808Sek110237 
3541789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3542789Sahrens 		return (ENOTSUP);
3543789Sahrens 
3544789Sahrens 	/*
35457046Sahrens 	 * If a resilver was requested, but there is no DTL on a
35467046Sahrens 	 * writeable leaf device, we have nothing to do.
3547789Sahrens 	 */
35487046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
35497046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
35507046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
35511544Seschrock 		return (0);
35521544Seschrock 	}
3553789Sahrens 
35547046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
35557046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
35567046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
35577046Sahrens 		return (EBUSY);
35587046Sahrens 
35597046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
35607046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
35617046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
35627046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
35631544Seschrock 	} else {
35647046Sahrens 		return (EINVAL);
35651544Seschrock 	}
3566789Sahrens }
3567789Sahrens 
35681544Seschrock /*
35691544Seschrock  * ==========================================================================
35701544Seschrock  * SPA async task processing
35711544Seschrock  * ==========================================================================
35721544Seschrock  */
35731544Seschrock 
35741544Seschrock static void
35754451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3576789Sahrens {
35777361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
35787361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
35797361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
35807754SJeff.Bonwick@Sun.COM 		vdev_clear(spa, vd);
35817754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
35821544Seschrock 	}
35837361SBrendan.Gregg@Sun.COM 
35847754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
35857361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
35861544Seschrock }
35871544Seschrock 
35881544Seschrock static void
35897754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
35907754SJeff.Bonwick@Sun.COM {
35917754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
35927754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
35937754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
35947754SJeff.Bonwick@Sun.COM 	}
35957754SJeff.Bonwick@Sun.COM 
35967754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
35977754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
35987754SJeff.Bonwick@Sun.COM }
35997754SJeff.Bonwick@Sun.COM 
36007754SJeff.Bonwick@Sun.COM static void
36011544Seschrock spa_async_thread(spa_t *spa)
36021544Seschrock {
36037754SJeff.Bonwick@Sun.COM 	int tasks;
36041544Seschrock 
36051544Seschrock 	ASSERT(spa->spa_sync_on);
3606789Sahrens 
36071544Seschrock 	mutex_enter(&spa->spa_async_lock);
36081544Seschrock 	tasks = spa->spa_async_tasks;
36091544Seschrock 	spa->spa_async_tasks = 0;
36101544Seschrock 	mutex_exit(&spa->spa_async_lock);
36111544Seschrock 
36121544Seschrock 	/*
36131635Sbonwick 	 * See if the config needs to be updated.
36141635Sbonwick 	 */
36151635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
36161635Sbonwick 		mutex_enter(&spa_namespace_lock);
36171635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36181635Sbonwick 		mutex_exit(&spa_namespace_lock);
36191635Sbonwick 	}
36201635Sbonwick 
36211635Sbonwick 	/*
36224451Seschrock 	 * See if any devices need to be marked REMOVED.
36231544Seschrock 	 */
36247754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
36257754SJeff.Bonwick@Sun.COM 		spa_vdev_state_enter(spa);
36264451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
36277754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
36287361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
36297754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
36307361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
36317754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
36327754SJeff.Bonwick@Sun.COM 	}
36337754SJeff.Bonwick@Sun.COM 
36347754SJeff.Bonwick@Sun.COM 	/*
36357754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
36367754SJeff.Bonwick@Sun.COM 	 */
36377754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
36387754SJeff.Bonwick@Sun.COM 		spa_vdev_state_enter(spa);
36397754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
36407754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
36414451Seschrock 	}
36421544Seschrock 
36431544Seschrock 	/*
36441544Seschrock 	 * If any devices are done replacing, detach them.
36451544Seschrock 	 */
36464451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
36474451Seschrock 		spa_vdev_resilver_done(spa);
3648789Sahrens 
36491544Seschrock 	/*
36501544Seschrock 	 * Kick off a resilver.
36511544Seschrock 	 */
36527046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
36537046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
36541544Seschrock 
36551544Seschrock 	/*
36561544Seschrock 	 * Let the world know that we're done.
36571544Seschrock 	 */
36581544Seschrock 	mutex_enter(&spa->spa_async_lock);
36591544Seschrock 	spa->spa_async_thread = NULL;
36601544Seschrock 	cv_broadcast(&spa->spa_async_cv);
36611544Seschrock 	mutex_exit(&spa->spa_async_lock);
36621544Seschrock 	thread_exit();
36631544Seschrock }
36641544Seschrock 
36651544Seschrock void
36661544Seschrock spa_async_suspend(spa_t *spa)
36671544Seschrock {
36681544Seschrock 	mutex_enter(&spa->spa_async_lock);
36691544Seschrock 	spa->spa_async_suspended++;
36701544Seschrock 	while (spa->spa_async_thread != NULL)
36711544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
36721544Seschrock 	mutex_exit(&spa->spa_async_lock);
36731544Seschrock }
36741544Seschrock 
36751544Seschrock void
36761544Seschrock spa_async_resume(spa_t *spa)
36771544Seschrock {
36781544Seschrock 	mutex_enter(&spa->spa_async_lock);
36791544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
36801544Seschrock 	spa->spa_async_suspended--;
36811544Seschrock 	mutex_exit(&spa->spa_async_lock);
36821544Seschrock }
36831544Seschrock 
36841544Seschrock static void
36851544Seschrock spa_async_dispatch(spa_t *spa)
36861544Seschrock {
36871544Seschrock 	mutex_enter(&spa->spa_async_lock);
36881544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
36891635Sbonwick 	    spa->spa_async_thread == NULL &&
36901635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
36911544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
36921544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
36931544Seschrock 	mutex_exit(&spa->spa_async_lock);
36941544Seschrock }
36951544Seschrock 
36961544Seschrock void
36971544Seschrock spa_async_request(spa_t *spa, int task)
36981544Seschrock {
36991544Seschrock 	mutex_enter(&spa->spa_async_lock);
37001544Seschrock 	spa->spa_async_tasks |= task;
37011544Seschrock 	mutex_exit(&spa->spa_async_lock);
3702789Sahrens }
3703789Sahrens 
3704789Sahrens /*
3705789Sahrens  * ==========================================================================
3706789Sahrens  * SPA syncing routines
3707789Sahrens  * ==========================================================================
3708789Sahrens  */
3709789Sahrens 
3710789Sahrens static void
3711789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3712789Sahrens {
3713789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3714789Sahrens 	dmu_tx_t *tx;
3715789Sahrens 	blkptr_t blk;
3716789Sahrens 	uint64_t itor = 0;
3717789Sahrens 	zio_t *zio;
3718789Sahrens 	int error;
3719789Sahrens 	uint8_t c = 1;
3720789Sahrens 
37217754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
37227754SJeff.Bonwick@Sun.COM 
37237754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
37247754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
37257754SJeff.Bonwick@Sun.COM 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
37267754SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_MUSTSUCCEED));
37277754SJeff.Bonwick@Sun.COM 	}
3728789Sahrens 
3729789Sahrens 	error = zio_wait(zio);
3730789Sahrens 	ASSERT3U(error, ==, 0);
3731789Sahrens 
3732789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3733789Sahrens 	bplist_vacate(bpl, tx);
3734789Sahrens 
3735789Sahrens 	/*
3736789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3737789Sahrens 	 * (Usually only the first block is needed.)
3738789Sahrens 	 */
3739789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3740789Sahrens 	dmu_tx_commit(tx);
3741789Sahrens }
3742789Sahrens 
3743789Sahrens static void
37442082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
37452082Seschrock {
37462082Seschrock 	char *packed = NULL;
37477497STim.Haley@Sun.COM 	size_t bufsize;
37482082Seschrock 	size_t nvsize = 0;
37492082Seschrock 	dmu_buf_t *db;
37502082Seschrock 
37512082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
37522082Seschrock 
37537497STim.Haley@Sun.COM 	/*
37547497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
37557497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
37567497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
37577497STim.Haley@Sun.COM 	 */
37587497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
37597497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
37602082Seschrock 
37612082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
37622082Seschrock 	    KM_SLEEP) == 0);
37637497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
37647497STim.Haley@Sun.COM 
37657497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
37667497STim.Haley@Sun.COM 
37677497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
37682082Seschrock 
37692082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
37702082Seschrock 	dmu_buf_will_dirty(db, tx);
37712082Seschrock 	*(uint64_t *)db->db_data = nvsize;
37722082Seschrock 	dmu_buf_rele(db, FTAG);
37732082Seschrock }
37742082Seschrock 
37752082Seschrock static void
37765450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
37775450Sbrendan     const char *config, const char *entry)
37782082Seschrock {
37792082Seschrock 	nvlist_t *nvroot;
37805450Sbrendan 	nvlist_t **list;
37812082Seschrock 	int i;
37822082Seschrock 
37835450Sbrendan 	if (!sav->sav_sync)
37842082Seschrock 		return;
37852082Seschrock 
37862082Seschrock 	/*
37875450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
37885450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
37894451Seschrock 	 * valid and the vdevs are labeled appropriately.
37902082Seschrock 	 */
37915450Sbrendan 	if (sav->sav_object == 0) {
37925450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
37935450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
37945450Sbrendan 		    sizeof (uint64_t), tx);
37952082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
37965450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
37975450Sbrendan 		    &sav->sav_object, tx) == 0);
37982082Seschrock 	}
37992082Seschrock 
38002082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
38015450Sbrendan 	if (sav->sav_count == 0) {
38025450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
38032082Seschrock 	} else {
38045450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
38055450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
38065450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
38075450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
38085450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
38095450Sbrendan 		    sav->sav_count) == 0);
38105450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
38115450Sbrendan 			nvlist_free(list[i]);
38125450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
38132082Seschrock 	}
38142082Seschrock 
38155450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
38162926Sek110237 	nvlist_free(nvroot);
38172082Seschrock 
38185450Sbrendan 	sav->sav_sync = B_FALSE;
38192082Seschrock }
38202082Seschrock 
38212082Seschrock static void
3822789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3823789Sahrens {
3824789Sahrens 	nvlist_t *config;
3825789Sahrens 
38267754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
3827789Sahrens 		return;
3828789Sahrens 
38297754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
38307754SJeff.Bonwick@Sun.COM 
38317754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
38327754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
38337754SJeff.Bonwick@Sun.COM 
38347754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
3835789Sahrens 
38361635Sbonwick 	if (spa->spa_config_syncing)
38371635Sbonwick 		nvlist_free(spa->spa_config_syncing);
38381635Sbonwick 	spa->spa_config_syncing = config;
3839789Sahrens 
38402082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3841789Sahrens }
3842789Sahrens 
38435094Slling /*
38445094Slling  * Set zpool properties.
38455094Slling  */
38463912Slling static void
38474543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
38483912Slling {
38493912Slling 	spa_t *spa = arg1;
38505094Slling 	objset_t *mos = spa->spa_meta_objset;
38513912Slling 	nvlist_t *nvp = arg2;
38525094Slling 	nvpair_t *elem;
38534451Seschrock 	uint64_t intval;
38546643Seschrock 	char *strval;
38555094Slling 	zpool_prop_t prop;
38565094Slling 	const char *propname;
38575094Slling 	zprop_type_t proptype;
38585094Slling 
38597754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
38607754SJeff.Bonwick@Sun.COM 
38615094Slling 	elem = NULL;
38625094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
38635094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
38645094Slling 		case ZPOOL_PROP_VERSION:
38655094Slling 			/*
38665094Slling 			 * Only set version for non-zpool-creation cases
38675094Slling 			 * (set/import). spa_create() needs special care
38685094Slling 			 * for version setting.
38695094Slling 			 */
38705094Slling 			if (tx->tx_txg != TXG_INITIAL) {
38715094Slling 				VERIFY(nvpair_value_uint64(elem,
38725094Slling 				    &intval) == 0);
38735094Slling 				ASSERT(intval <= SPA_VERSION);
38745094Slling 				ASSERT(intval >= spa_version(spa));
38755094Slling 				spa->spa_uberblock.ub_version = intval;
38765094Slling 				vdev_config_dirty(spa->spa_root_vdev);
38775094Slling 			}
38785094Slling 			break;
38795094Slling 
38805094Slling 		case ZPOOL_PROP_ALTROOT:
38815094Slling 			/*
38825094Slling 			 * 'altroot' is a non-persistent property. It should
38835094Slling 			 * have been set temporarily at creation or import time.
38845094Slling 			 */
38855094Slling 			ASSERT(spa->spa_root != NULL);
38865094Slling 			break;
38875094Slling 
38885363Seschrock 		case ZPOOL_PROP_CACHEFILE:
38895094Slling 			/*
38908525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
38915094Slling 			 */
38924543Smarks 			break;
38935094Slling 		default:
38945094Slling 			/*
38955094Slling 			 * Set pool property values in the poolprops mos object.
38965094Slling 			 */
38975094Slling 			if (spa->spa_pool_props_object == 0) {
38985094Slling 				objset_t *mos = spa->spa_meta_objset;
38995094Slling 
39005094Slling 				VERIFY((spa->spa_pool_props_object =
39015094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
39025094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
39035094Slling 
39045094Slling 				VERIFY(zap_update(mos,
39055094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
39065094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
39075094Slling 				    == 0);
39085094Slling 			}
39095094Slling 
39105094Slling 			/* normalize the property name */
39115094Slling 			propname = zpool_prop_to_name(prop);
39125094Slling 			proptype = zpool_prop_get_type(prop);
39135094Slling 
39145094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
39155094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
39165094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
39175094Slling 				VERIFY(zap_update(mos,
39185094Slling 				    spa->spa_pool_props_object, propname,
39195094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
39205094Slling 
39215094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
39225094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
39235094Slling 
39245094Slling 				if (proptype == PROP_TYPE_INDEX) {
39255094Slling 					const char *unused;
39265094Slling 					VERIFY(zpool_prop_index_to_string(
39275094Slling 					    prop, intval, &unused) == 0);
39285094Slling 				}
39295094Slling 				VERIFY(zap_update(mos,
39305094Slling 				    spa->spa_pool_props_object, propname,
39315094Slling 				    8, 1, &intval, tx) == 0);
39325094Slling 			} else {
39335094Slling 				ASSERT(0); /* not allowed */
39345094Slling 			}
39355094Slling 
39365329Sgw25295 			switch (prop) {
39375329Sgw25295 			case ZPOOL_PROP_DELEGATION:
39385094Slling 				spa->spa_delegation = intval;
39395329Sgw25295 				break;
39405329Sgw25295 			case ZPOOL_PROP_BOOTFS:
39415094Slling 				spa->spa_bootfs = intval;
39425329Sgw25295 				break;
39435329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
39445329Sgw25295 				spa->spa_failmode = intval;
39455329Sgw25295 				break;
39465329Sgw25295 			default:
39475329Sgw25295 				break;
39485329Sgw25295 			}
39493912Slling 		}
39505094Slling 
39515094Slling 		/* log internal history if this is not a zpool create */
39525094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
39535094Slling 		    tx->tx_txg != TXG_INITIAL) {
39545094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
39555094Slling 			    spa, tx, cr, "%s %lld %s",
39567754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
39575094Slling 		}
39583912Slling 	}
39597754SJeff.Bonwick@Sun.COM 
39607754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
39613912Slling }
39623912Slling 
3963789Sahrens /*
3964789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3965789Sahrens  * part of the process, so we iterate until it converges.
3966789Sahrens  */
3967789Sahrens void
3968789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3969789Sahrens {
3970789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3971789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3972789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
39731635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3974789Sahrens 	vdev_t *vd;
3975789Sahrens 	dmu_tx_t *tx;
3976789Sahrens 	int dirty_vdevs;
39777754SJeff.Bonwick@Sun.COM 	int error;
3978789Sahrens 
3979789Sahrens 	/*
3980789Sahrens 	 * Lock out configuration changes.
3981789Sahrens 	 */
39827754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3983789Sahrens 
3984789Sahrens 	spa->spa_syncing_txg = txg;
3985789Sahrens 	spa->spa_sync_pass = 0;
3986789Sahrens 
39877754SJeff.Bonwick@Sun.COM 	/*
39887754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
39897754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
39907754SJeff.Bonwick@Sun.COM 	 */
39917754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
39928241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
39938241SJeff.Bonwick@Sun.COM 		/*
39948241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
39958241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
39968241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
39978241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
39988241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
39998241SJeff.Bonwick@Sun.COM 		 */
40008241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
40018241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
40028241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
40038241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
40048241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
40058241SJeff.Bonwick@Sun.COM 		}
40068241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
40078241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
40087754SJeff.Bonwick@Sun.COM 	}
40097754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
40107754SJeff.Bonwick@Sun.COM 
40111544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4012789Sahrens 
40132082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
40142082Seschrock 
40152082Seschrock 	/*
40164577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
40172082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
40182082Seschrock 	 */
40194577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
40204577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
40212082Seschrock 		int i;
40222082Seschrock 
40232082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
40242082Seschrock 			vd = rvd->vdev_child[i];
40252082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
40262082Seschrock 				break;
40272082Seschrock 		}
40282082Seschrock 		if (i == rvd->vdev_children) {
40292082Seschrock 			spa->spa_deflate = TRUE;
40302082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
40312082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
40322082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
40332082Seschrock 		}
40342082Seschrock 	}
40352082Seschrock 
40367046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
40377046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
40387046Sahrens 		dsl_pool_create_origin(dp, tx);
40397046Sahrens 
40407046Sahrens 		/* Keeping the origin open increases spa_minref */
40417046Sahrens 		spa->spa_minref += 3;
40427046Sahrens 	}
40437046Sahrens 
40447046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
40457046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
40467046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
40477046Sahrens 	}
40487046Sahrens 
4049789Sahrens 	/*
4050789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4051789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4052789Sahrens 	 * don't generate work on an otherwise idle system.
4053789Sahrens 	 */
4054789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
40552329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
40562329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4057789Sahrens 		spa_sync_deferred_frees(spa, txg);
4058789Sahrens 
4059789Sahrens 	/*
4060789Sahrens 	 * Iterate to convergence.
4061789Sahrens 	 */
4062789Sahrens 	do {
4063789Sahrens 		spa->spa_sync_pass++;
4064789Sahrens 
4065789Sahrens 		spa_sync_config_object(spa, tx);
40665450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
40675450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
40685450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
40695450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
40701544Seschrock 		spa_errlog_sync(spa, txg);
4071789Sahrens 		dsl_pool_sync(dp, txg);
4072789Sahrens 
4073789Sahrens 		dirty_vdevs = 0;
4074789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4075789Sahrens 			vdev_sync(vd, txg);
4076789Sahrens 			dirty_vdevs++;
4077789Sahrens 		}
4078789Sahrens 
4079789Sahrens 		bplist_sync(bpl, tx);
4080789Sahrens 	} while (dirty_vdevs);
4081789Sahrens 
4082789Sahrens 	bplist_close(bpl);
4083789Sahrens 
4084789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4085789Sahrens 
4086789Sahrens 	/*
4087789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4088789Sahrens 	 * to commit the transaction group.
40891635Sbonwick 	 *
40905688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
40915688Sbonwick 	 * random top-level vdevs that are known to be visible in the
40927754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
40937754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4094789Sahrens 	 */
40957754SJeff.Bonwick@Sun.COM 	for (;;) {
40967754SJeff.Bonwick@Sun.COM 		/*
40977754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
40987754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
40997754SJeff.Bonwick@Sun.COM 		 */
41007754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
41017754SJeff.Bonwick@Sun.COM 
41027754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
41037754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
41047754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
41057754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
41067754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
41077754SJeff.Bonwick@Sun.COM 			int c;
41087754SJeff.Bonwick@Sun.COM 
41097754SJeff.Bonwick@Sun.COM 			for (c = 0; c < children; c++) {
41107754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
41117754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
41127754SJeff.Bonwick@Sun.COM 					continue;
41137754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
41147754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
41157754SJeff.Bonwick@Sun.COM 					break;
41167754SJeff.Bonwick@Sun.COM 			}
41177754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg);
41187754SJeff.Bonwick@Sun.COM 		} else {
41197754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
41207754SJeff.Bonwick@Sun.COM 			    rvd->vdev_children, txg);
41211635Sbonwick 		}
41227754SJeff.Bonwick@Sun.COM 
41237754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
41247754SJeff.Bonwick@Sun.COM 
41257754SJeff.Bonwick@Sun.COM 		if (error == 0)
41267754SJeff.Bonwick@Sun.COM 			break;
41277754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
41287754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
41291635Sbonwick 	}
41302082Seschrock 	dmu_tx_commit(tx);
41312082Seschrock 
41321635Sbonwick 	/*
41331635Sbonwick 	 * Clear the dirty config list.
41341635Sbonwick 	 */
41357754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
41361635Sbonwick 		vdev_config_clean(vd);
41371635Sbonwick 
41381635Sbonwick 	/*
41391635Sbonwick 	 * Now that the new config has synced transactionally,
41401635Sbonwick 	 * let it become visible to the config cache.
41411635Sbonwick 	 */
41421635Sbonwick 	if (spa->spa_config_syncing != NULL) {
41431635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
41441635Sbonwick 		spa->spa_config_txg = txg;
41451635Sbonwick 		spa->spa_config_syncing = NULL;
41461635Sbonwick 	}
4147789Sahrens 
4148789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4149789Sahrens 
4150789Sahrens 	/*
4151789Sahrens 	 * Clean up the ZIL records for the synced txg.
4152789Sahrens 	 */
4153789Sahrens 	dsl_pool_zil_clean(dp);
4154789Sahrens 
4155789Sahrens 	/*
4156789Sahrens 	 * Update usable space statistics.
4157789Sahrens 	 */
4158789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4159789Sahrens 		vdev_sync_done(vd, txg);
4160789Sahrens 
4161789Sahrens 	/*
4162789Sahrens 	 * It had better be the case that we didn't dirty anything
41632082Seschrock 	 * since vdev_config_sync().
4164789Sahrens 	 */
4165789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4166789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4167789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4168789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4169789Sahrens 
41707754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
41711544Seschrock 
41721544Seschrock 	/*
41731544Seschrock 	 * If any async tasks have been requested, kick them off.
41741544Seschrock 	 */
41751544Seschrock 	spa_async_dispatch(spa);
4176789Sahrens }
4177789Sahrens 
4178789Sahrens /*
4179789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4180789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4181789Sahrens  * sync.
4182789Sahrens  */
4183789Sahrens void
4184789Sahrens spa_sync_allpools(void)
4185789Sahrens {
4186789Sahrens 	spa_t *spa = NULL;
4187789Sahrens 	mutex_enter(&spa_namespace_lock);
4188789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
41897754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4190789Sahrens 			continue;
4191789Sahrens 		spa_open_ref(spa, FTAG);
4192789Sahrens 		mutex_exit(&spa_namespace_lock);
4193789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4194789Sahrens 		mutex_enter(&spa_namespace_lock);
4195789Sahrens 		spa_close(spa, FTAG);
4196789Sahrens 	}
4197789Sahrens 	mutex_exit(&spa_namespace_lock);
4198789Sahrens }
4199789Sahrens 
4200789Sahrens /*
4201789Sahrens  * ==========================================================================
4202789Sahrens  * Miscellaneous routines
4203789Sahrens  * ==========================================================================
4204789Sahrens  */
4205789Sahrens 
4206789Sahrens /*
4207789Sahrens  * Remove all pools in the system.
4208789Sahrens  */
4209789Sahrens void
4210789Sahrens spa_evict_all(void)
4211789Sahrens {
4212789Sahrens 	spa_t *spa;
4213789Sahrens 
4214789Sahrens 	/*
4215789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4216789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4217789Sahrens 	 */
4218789Sahrens 	mutex_enter(&spa_namespace_lock);
4219789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4220789Sahrens 		/*
42211544Seschrock 		 * Stop async tasks.  The async thread may need to detach
42221544Seschrock 		 * a device that's been replaced, which requires grabbing
42231544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4224789Sahrens 		 */
4225789Sahrens 		spa_open_ref(spa, FTAG);
4226789Sahrens 		mutex_exit(&spa_namespace_lock);
42271544Seschrock 		spa_async_suspend(spa);
42284808Sek110237 		mutex_enter(&spa_namespace_lock);
4229789Sahrens 		spa_close(spa, FTAG);
4230789Sahrens 
4231789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4232789Sahrens 			spa_unload(spa);
4233789Sahrens 			spa_deactivate(spa);
4234789Sahrens 		}
4235789Sahrens 		spa_remove(spa);
4236789Sahrens 	}
4237789Sahrens 	mutex_exit(&spa_namespace_lock);
4238789Sahrens }
42391544Seschrock 
42401544Seschrock vdev_t *
42416643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
42421544Seschrock {
42436643Seschrock 	vdev_t *vd;
42446643Seschrock 	int i;
42456643Seschrock 
42466643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
42476643Seschrock 		return (vd);
42486643Seschrock 
42496643Seschrock 	if (l2cache) {
42506643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
42516643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
42526643Seschrock 			if (vd->vdev_guid == guid)
42536643Seschrock 				return (vd);
42546643Seschrock 		}
42556643Seschrock 	}
42566643Seschrock 
42576643Seschrock 	return (NULL);
42581544Seschrock }
42591760Seschrock 
42601760Seschrock void
42615094Slling spa_upgrade(spa_t *spa, uint64_t version)
42621760Seschrock {
42637754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
42641760Seschrock 
42651760Seschrock 	/*
42661760Seschrock 	 * This should only be called for a non-faulted pool, and since a
42671760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
42681760Seschrock 	 * possible.
42691760Seschrock 	 */
42704577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
42715094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
42725094Slling 
42735094Slling 	spa->spa_uberblock.ub_version = version;
42741760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
42751760Seschrock 
42767754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
42772082Seschrock 
42782082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
42791760Seschrock }
42802082Seschrock 
42812082Seschrock boolean_t
42822082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
42832082Seschrock {
42842082Seschrock 	int i;
42853377Seschrock 	uint64_t spareguid;
42865450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
42875450Sbrendan 
42885450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
42895450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
42902082Seschrock 			return (B_TRUE);
42912082Seschrock 
42925450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
42935450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
42945450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
42953377Seschrock 			return (B_TRUE);
42963377Seschrock 	}
42973377Seschrock 
42982082Seschrock 	return (B_FALSE);
42992082Seschrock }
43003912Slling 
43014451Seschrock /*
43027214Slling  * Check if a pool has an active shared spare device.
43037214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
43047214Slling  */
43057214Slling static boolean_t
43067214Slling spa_has_active_shared_spare(spa_t *spa)
43077214Slling {
43087214Slling 	int i, refcnt;
43097214Slling 	uint64_t pool;
43107214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
43117214Slling 
43127214Slling 	for (i = 0; i < sav->sav_count; i++) {
43137214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
43147214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
43157214Slling 		    refcnt > 2)
43167214Slling 			return (B_TRUE);
43177214Slling 	}
43187214Slling 
43197214Slling 	return (B_FALSE);
43207214Slling }
43217214Slling 
43227214Slling /*
43234451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
43244451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
43254451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
43264451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
43274451Seschrock  * or zdb as real changes.
43284451Seschrock  */
43294451Seschrock void
43304451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
43314451Seschrock {
43324451Seschrock #ifdef _KERNEL
43334451Seschrock 	sysevent_t		*ev;
43344451Seschrock 	sysevent_attr_list_t	*attr = NULL;
43354451Seschrock 	sysevent_value_t	value;
43364451Seschrock 	sysevent_id_t		eid;
43374451Seschrock 
43384451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
43394451Seschrock 	    SE_SLEEP);
43404451Seschrock 
43414451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
43424451Seschrock 	value.value.sv_string = spa_name(spa);
43434451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
43444451Seschrock 		goto done;
43454451Seschrock 
43464451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
43474451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
43484451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
43494451Seschrock 		goto done;
43504451Seschrock 
43514451Seschrock 	if (vd) {
43524451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
43534451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
43544451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
43554451Seschrock 		    SE_SLEEP) != 0)
43564451Seschrock 			goto done;
43574451Seschrock 
43584451Seschrock 		if (vd->vdev_path) {
43594451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
43604451Seschrock 			value.value.sv_string = vd->vdev_path;
43614451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
43624451Seschrock 			    &value, SE_SLEEP) != 0)
43634451Seschrock 				goto done;
43644451Seschrock 		}
43654451Seschrock 	}
43664451Seschrock 
43675756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
43685756Seschrock 		goto done;
43695756Seschrock 	attr = NULL;
43705756Seschrock 
43714451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
43724451Seschrock 
43734451Seschrock done:
43744451Seschrock 	if (attr)
43754451Seschrock 		sysevent_free_attr(attr);
43764451Seschrock 	sysevent_free(ev);
43774451Seschrock #endif
43784451Seschrock }
4379