xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 7184)
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 /*
235756Seschrock  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens /*
30789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
31789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
32789Sahrens  * pool.
33789Sahrens  */
34789Sahrens 
35789Sahrens #include <sys/zfs_context.h>
361544Seschrock #include <sys/fm/fs/zfs.h>
37789Sahrens #include <sys/spa_impl.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zio_checksum.h>
40789Sahrens #include <sys/zio_compress.h>
41789Sahrens #include <sys/dmu.h>
42789Sahrens #include <sys/dmu_tx.h>
43789Sahrens #include <sys/zap.h>
44789Sahrens #include <sys/zil.h>
45789Sahrens #include <sys/vdev_impl.h>
46789Sahrens #include <sys/metaslab.h>
47789Sahrens #include <sys/uberblock_impl.h>
48789Sahrens #include <sys/txg.h>
49789Sahrens #include <sys/avl.h>
50789Sahrens #include <sys/dmu_traverse.h>
513912Slling #include <sys/dmu_objset.h>
52789Sahrens #include <sys/unique.h>
53789Sahrens #include <sys/dsl_pool.h>
543912Slling #include <sys/dsl_dataset.h>
55789Sahrens #include <sys/dsl_dir.h>
56789Sahrens #include <sys/dsl_prop.h>
573912Slling #include <sys/dsl_synctask.h>
58789Sahrens #include <sys/fs/zfs.h>
595450Sbrendan #include <sys/arc.h>
60789Sahrens #include <sys/callb.h>
613975Sek110237 #include <sys/systeminfo.h>
623975Sek110237 #include <sys/sunddi.h>
636423Sgw25295 #include <sys/spa_boot.h>
64789Sahrens 
655094Slling #include "zfs_prop.h"
665913Sperrin #include "zfs_comutil.h"
675094Slling 
682986Sek110237 int zio_taskq_threads = 8;
692986Sek110237 
705094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
715094Slling 
725094Slling /*
735094Slling  * ==========================================================================
745094Slling  * SPA properties routines
755094Slling  * ==========================================================================
765094Slling  */
775094Slling 
785094Slling /*
795094Slling  * Add a (source=src, propname=propval) list to an nvlist.
805094Slling  */
815949Slling static void
825094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
835094Slling     uint64_t intval, zprop_source_t src)
845094Slling {
855094Slling 	const char *propname = zpool_prop_to_name(prop);
865094Slling 	nvlist_t *propval;
875949Slling 
885949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
895949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
905949Slling 
915949Slling 	if (strval != NULL)
925949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
935949Slling 	else
945949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
955949Slling 
965949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
975094Slling 	nvlist_free(propval);
985094Slling }
995094Slling 
1005094Slling /*
1015094Slling  * Get property values from the spa configuration.
1025094Slling  */
1035949Slling static void
1045094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1055094Slling {
1065094Slling 	uint64_t size = spa_get_space(spa);
1075094Slling 	uint64_t used = spa_get_alloc(spa);
1085094Slling 	uint64_t cap, version;
1095094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1106643Seschrock 	spa_config_dirent_t *dp;
1115094Slling 
1125094Slling 	/*
1135094Slling 	 * readonly properties
1145094Slling 	 */
1155949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src);
1165949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1175949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1185949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
1195094Slling 
1205094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
1215949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1225949Slling 
1235949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1245949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1255949Slling 	    spa->spa_root_vdev->vdev_state, src);
1265094Slling 
1275094Slling 	/*
1285094Slling 	 * settable properties that are not stored in the pool property object.
1295094Slling 	 */
1305094Slling 	version = spa_version(spa);
1315094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1325094Slling 		src = ZPROP_SRC_DEFAULT;
1335094Slling 	else
1345094Slling 		src = ZPROP_SRC_LOCAL;
1355949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1365949Slling 
1375949Slling 	if (spa->spa_root != NULL)
1385949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1395949Slling 		    0, ZPROP_SRC_LOCAL);
1405094Slling 
1416643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1426643Seschrock 		if (dp->scd_path == NULL) {
1435949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1446643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1456643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1465949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1476643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1485363Seschrock 		}
1495363Seschrock 	}
1505094Slling }
1515094Slling 
1525094Slling /*
1535094Slling  * Get zpool property values.
1545094Slling  */
1555094Slling int
1565094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1575094Slling {
1585094Slling 	zap_cursor_t zc;
1595094Slling 	zap_attribute_t za;
1605094Slling 	objset_t *mos = spa->spa_meta_objset;
1615094Slling 	int err;
1625094Slling 
1635949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1645094Slling 
1655094Slling 	/*
1665094Slling 	 * Get properties from the spa config.
1675094Slling 	 */
1685949Slling 	spa_prop_get_config(spa, nvp);
1695094Slling 
1705094Slling 	mutex_enter(&spa->spa_props_lock);
1715094Slling 	/* If no pool property object, no more prop to get. */
1725094Slling 	if (spa->spa_pool_props_object == 0) {
1735094Slling 		mutex_exit(&spa->spa_props_lock);
1745094Slling 		return (0);
1755094Slling 	}
1765094Slling 
1775094Slling 	/*
1785094Slling 	 * Get properties from the MOS pool property object.
1795094Slling 	 */
1805094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1815094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1825094Slling 	    zap_cursor_advance(&zc)) {
1835094Slling 		uint64_t intval = 0;
1845094Slling 		char *strval = NULL;
1855094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
1865094Slling 		zpool_prop_t prop;
1875094Slling 
1885094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
1895094Slling 			continue;
1905094Slling 
1915094Slling 		switch (za.za_integer_length) {
1925094Slling 		case 8:
1935094Slling 			/* integer property */
1945094Slling 			if (za.za_first_integer !=
1955094Slling 			    zpool_prop_default_numeric(prop))
1965094Slling 				src = ZPROP_SRC_LOCAL;
1975094Slling 
1985094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
1995094Slling 				dsl_pool_t *dp;
2005094Slling 				dsl_dataset_t *ds = NULL;
2015094Slling 
2025094Slling 				dp = spa_get_dsl(spa);
2035094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2046689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2056689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2065094Slling 					rw_exit(&dp->dp_config_rwlock);
2075094Slling 					break;
2085094Slling 				}
2095094Slling 
2105094Slling 				strval = kmem_alloc(
2115094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2125094Slling 				    KM_SLEEP);
2135094Slling 				dsl_dataset_name(ds, strval);
2146689Smaybee 				dsl_dataset_rele(ds, FTAG);
2155094Slling 				rw_exit(&dp->dp_config_rwlock);
2165094Slling 			} else {
2175094Slling 				strval = NULL;
2185094Slling 				intval = za.za_first_integer;
2195094Slling 			}
2205094Slling 
2215949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2225094Slling 
2235094Slling 			if (strval != NULL)
2245094Slling 				kmem_free(strval,
2255094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2265094Slling 
2275094Slling 			break;
2285094Slling 
2295094Slling 		case 1:
2305094Slling 			/* string property */
2315094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2325094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2335094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2345094Slling 			if (err) {
2355094Slling 				kmem_free(strval, za.za_num_integers);
2365094Slling 				break;
2375094Slling 			}
2385949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2395094Slling 			kmem_free(strval, za.za_num_integers);
2405094Slling 			break;
2415094Slling 
2425094Slling 		default:
2435094Slling 			break;
2445094Slling 		}
2455094Slling 	}
2465094Slling 	zap_cursor_fini(&zc);
2475094Slling 	mutex_exit(&spa->spa_props_lock);
2485094Slling out:
2495094Slling 	if (err && err != ENOENT) {
2505094Slling 		nvlist_free(*nvp);
2515949Slling 		*nvp = NULL;
2525094Slling 		return (err);
2535094Slling 	}
2545094Slling 
2555094Slling 	return (0);
2565094Slling }
2575094Slling 
2585094Slling /*
2595094Slling  * Validate the given pool properties nvlist and modify the list
2605094Slling  * for the property values to be set.
2615094Slling  */
2625094Slling static int
2635094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2645094Slling {
2655094Slling 	nvpair_t *elem;
2665094Slling 	int error = 0, reset_bootfs = 0;
2675094Slling 	uint64_t objnum;
2685094Slling 
2695094Slling 	elem = NULL;
2705094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2715094Slling 		zpool_prop_t prop;
2725094Slling 		char *propname, *strval;
2735094Slling 		uint64_t intval;
2745094Slling 		objset_t *os;
2755363Seschrock 		char *slash;
2765094Slling 
2775094Slling 		propname = nvpair_name(elem);
2785094Slling 
2795094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2805094Slling 			return (EINVAL);
2815094Slling 
2825094Slling 		switch (prop) {
2835094Slling 		case ZPOOL_PROP_VERSION:
2845094Slling 			error = nvpair_value_uint64(elem, &intval);
2855094Slling 			if (!error &&
2865094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
2875094Slling 				error = EINVAL;
2885094Slling 			break;
2895094Slling 
2905094Slling 		case ZPOOL_PROP_DELEGATION:
2915094Slling 		case ZPOOL_PROP_AUTOREPLACE:
2925094Slling 			error = nvpair_value_uint64(elem, &intval);
2935094Slling 			if (!error && intval > 1)
2945094Slling 				error = EINVAL;
2955094Slling 			break;
2965094Slling 
2975094Slling 		case ZPOOL_PROP_BOOTFS:
2985094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
2995094Slling 				error = ENOTSUP;
3005094Slling 				break;
3015094Slling 			}
3025094Slling 
3035094Slling 			/*
3047042Sgw25295 			 * Make sure the vdev config is bootable
3055094Slling 			 */
3067042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3075094Slling 				error = ENOTSUP;
3085094Slling 				break;
3095094Slling 			}
3105094Slling 
3115094Slling 			reset_bootfs = 1;
3125094Slling 
3135094Slling 			error = nvpair_value_string(elem, &strval);
3145094Slling 
3155094Slling 			if (!error) {
3167042Sgw25295 				uint64_t compress;
3177042Sgw25295 
3185094Slling 				if (strval == NULL || strval[0] == '\0') {
3195094Slling 					objnum = zpool_prop_default_numeric(
3205094Slling 					    ZPOOL_PROP_BOOTFS);
3215094Slling 					break;
3225094Slling 				}
3235094Slling 
3245094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
3256689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3265094Slling 					break;
3277042Sgw25295 
3287042Sgw25295 				/* We don't support gzip bootable datasets */
3297042Sgw25295 				if ((error = dsl_prop_get_integer(strval,
3307042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3317042Sgw25295 				    &compress, NULL)) == 0 &&
3327042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3337042Sgw25295 					error = ENOTSUP;
3347042Sgw25295 				} else {
3357042Sgw25295 					objnum = dmu_objset_id(os);
3367042Sgw25295 				}
3375094Slling 				dmu_objset_close(os);
3385094Slling 			}
3395094Slling 			break;
3405329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3415329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3425329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3435329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3445329Sgw25295 				error = EINVAL;
3455329Sgw25295 
3465329Sgw25295 			/*
3475329Sgw25295 			 * This is a special case which only occurs when
3485329Sgw25295 			 * the pool has completely failed. This allows
3495329Sgw25295 			 * the user to change the in-core failmode property
3505329Sgw25295 			 * without syncing it out to disk (I/Os might
3515329Sgw25295 			 * currently be blocked). We do this by returning
3525329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3535329Sgw25295 			 * into thinking we encountered a property validation
3545329Sgw25295 			 * error.
3555329Sgw25295 			 */
3565329Sgw25295 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3575329Sgw25295 				spa->spa_failmode = intval;
3585329Sgw25295 				error = EIO;
3595329Sgw25295 			}
3605329Sgw25295 			break;
3615363Seschrock 
3625363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3635363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3645363Seschrock 				break;
3655363Seschrock 
3665363Seschrock 			if (strval[0] == '\0')
3675363Seschrock 				break;
3685363Seschrock 
3695363Seschrock 			if (strcmp(strval, "none") == 0)
3705363Seschrock 				break;
3715363Seschrock 
3725363Seschrock 			if (strval[0] != '/') {
3735363Seschrock 				error = EINVAL;
3745363Seschrock 				break;
3755363Seschrock 			}
3765363Seschrock 
3775363Seschrock 			slash = strrchr(strval, '/');
3785363Seschrock 			ASSERT(slash != NULL);
3795363Seschrock 
3805363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3815363Seschrock 			    strcmp(slash, "/..") == 0)
3825363Seschrock 				error = EINVAL;
3835363Seschrock 			break;
3845094Slling 		}
3855094Slling 
3865094Slling 		if (error)
3875094Slling 			break;
3885094Slling 	}
3895094Slling 
3905094Slling 	if (!error && reset_bootfs) {
3915094Slling 		error = nvlist_remove(props,
3925094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
3935094Slling 
3945094Slling 		if (!error) {
3955094Slling 			error = nvlist_add_uint64(props,
3965094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
3975094Slling 		}
3985094Slling 	}
3995094Slling 
4005094Slling 	return (error);
4015094Slling }
4025094Slling 
4035094Slling int
4045094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4055094Slling {
4065094Slling 	int error;
4075094Slling 
4085094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4095094Slling 		return (error);
4105094Slling 
4115094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4125094Slling 	    spa, nvp, 3));
4135094Slling }
4145094Slling 
4155094Slling /*
4165094Slling  * If the bootfs property value is dsobj, clear it.
4175094Slling  */
4185094Slling void
4195094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4205094Slling {
4215094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4225094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4235094Slling 		    spa->spa_pool_props_object,
4245094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4255094Slling 		spa->spa_bootfs = 0;
4265094Slling 	}
4275094Slling }
4285094Slling 
429789Sahrens /*
430789Sahrens  * ==========================================================================
431789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
432789Sahrens  * ==========================================================================
433789Sahrens  */
434789Sahrens 
4351544Seschrock static int
4361544Seschrock spa_error_entry_compare(const void *a, const void *b)
4371544Seschrock {
4381544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4391544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4401544Seschrock 	int ret;
4411544Seschrock 
4421544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4431544Seschrock 	    sizeof (zbookmark_t));
4441544Seschrock 
4451544Seschrock 	if (ret < 0)
4461544Seschrock 		return (-1);
4471544Seschrock 	else if (ret > 0)
4481544Seschrock 		return (1);
4491544Seschrock 	else
4501544Seschrock 		return (0);
4511544Seschrock }
4521544Seschrock 
4531544Seschrock /*
4541544Seschrock  * Utility function which retrieves copies of the current logs and
4551544Seschrock  * re-initializes them in the process.
4561544Seschrock  */
4571544Seschrock void
4581544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4591544Seschrock {
4601544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4611544Seschrock 
4621544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4631544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4641544Seschrock 
4651544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4661544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4671544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4681544Seschrock 	avl_create(&spa->spa_errlist_last,
4691544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4701544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4711544Seschrock }
4721544Seschrock 
473789Sahrens /*
474789Sahrens  * Activate an uninitialized pool.
475789Sahrens  */
476789Sahrens static void
477789Sahrens spa_activate(spa_t *spa)
478789Sahrens {
479789Sahrens 	int t;
480789Sahrens 
481789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
482789Sahrens 
483789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
484789Sahrens 
485789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4864527Sperrin 	spa->spa_log_class = metaslab_class_create();
487789Sahrens 
488789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
489789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
4902986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
491789Sahrens 		    TASKQ_PREPOPULATE);
492789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
4932986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
494789Sahrens 		    TASKQ_PREPOPULATE);
495789Sahrens 	}
496789Sahrens 
497789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
498789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
4995329Sgw25295 	list_create(&spa->spa_zio_list, sizeof (zio_t),
5005329Sgw25295 	    offsetof(zio_t, zio_link_node));
501789Sahrens 
502789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
503789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
5041544Seschrock 
5051544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5061544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5071544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5081544Seschrock 	avl_create(&spa->spa_errlist_last,
5091544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5101544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
511789Sahrens }
512789Sahrens 
513789Sahrens /*
514789Sahrens  * Opposite of spa_activate().
515789Sahrens  */
516789Sahrens static void
517789Sahrens spa_deactivate(spa_t *spa)
518789Sahrens {
519789Sahrens 	int t;
520789Sahrens 
521789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
522789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
523789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
524789Sahrens 
525789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
526789Sahrens 
527789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
528789Sahrens 
529789Sahrens 	list_destroy(&spa->spa_dirty_list);
5305329Sgw25295 	list_destroy(&spa->spa_zio_list);
531789Sahrens 
532789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
533789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
534789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
535789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
536789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
537789Sahrens 	}
538789Sahrens 
539789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
540789Sahrens 	spa->spa_normal_class = NULL;
541789Sahrens 
5424527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5434527Sperrin 	spa->spa_log_class = NULL;
5444527Sperrin 
5451544Seschrock 	/*
5461544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5471544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5481544Seschrock 	 */
5491544Seschrock 	spa_errlog_drain(spa);
5501544Seschrock 
5511544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5521544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5531544Seschrock 
554789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
555789Sahrens }
556789Sahrens 
557789Sahrens /*
558789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
559789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
560789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
561789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
562789Sahrens  */
5632082Seschrock static int
5642082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5652082Seschrock     uint_t id, int atype)
566789Sahrens {
567789Sahrens 	nvlist_t **child;
568789Sahrens 	uint_t c, children;
5692082Seschrock 	int error;
5702082Seschrock 
5712082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5722082Seschrock 		return (error);
5732082Seschrock 
5742082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5752082Seschrock 		return (0);
576789Sahrens 
577789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
578789Sahrens 	    &child, &children) != 0) {
5792082Seschrock 		vdev_free(*vdp);
5802082Seschrock 		*vdp = NULL;
5812082Seschrock 		return (EINVAL);
582789Sahrens 	}
583789Sahrens 
584789Sahrens 	for (c = 0; c < children; c++) {
5852082Seschrock 		vdev_t *vd;
5862082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5872082Seschrock 		    atype)) != 0) {
5882082Seschrock 			vdev_free(*vdp);
5892082Seschrock 			*vdp = NULL;
5902082Seschrock 			return (error);
591789Sahrens 		}
592789Sahrens 	}
593789Sahrens 
5942082Seschrock 	ASSERT(*vdp != NULL);
5952082Seschrock 
5962082Seschrock 	return (0);
597789Sahrens }
598789Sahrens 
599789Sahrens /*
600789Sahrens  * Opposite of spa_load().
601789Sahrens  */
602789Sahrens static void
603789Sahrens spa_unload(spa_t *spa)
604789Sahrens {
6052082Seschrock 	int i;
6062082Seschrock 
607789Sahrens 	/*
6081544Seschrock 	 * Stop async tasks.
6091544Seschrock 	 */
6101544Seschrock 	spa_async_suspend(spa);
6111544Seschrock 
6121544Seschrock 	/*
613789Sahrens 	 * Stop syncing.
614789Sahrens 	 */
615789Sahrens 	if (spa->spa_sync_on) {
616789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
617789Sahrens 		spa->spa_sync_on = B_FALSE;
618789Sahrens 	}
619789Sahrens 
620789Sahrens 	/*
621789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
622789Sahrens 	 */
6231544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
6241544Seschrock 	spa_config_exit(spa, FTAG);
625789Sahrens 
626789Sahrens 	/*
6275450Sbrendan 	 * Drop and purge level 2 cache
6285450Sbrendan 	 */
6295450Sbrendan 	spa_l2cache_drop(spa);
6305450Sbrendan 
6315450Sbrendan 	/*
632789Sahrens 	 * Close the dsl pool.
633789Sahrens 	 */
634789Sahrens 	if (spa->spa_dsl_pool) {
635789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
636789Sahrens 		spa->spa_dsl_pool = NULL;
637789Sahrens 	}
638789Sahrens 
639789Sahrens 	/*
640789Sahrens 	 * Close all vdevs.
641789Sahrens 	 */
6421585Sbonwick 	if (spa->spa_root_vdev)
643789Sahrens 		vdev_free(spa->spa_root_vdev);
6441585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6451544Seschrock 
6465450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
6475450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
6485450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
6495450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
6505450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
6515450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
6525450Sbrendan 	}
6535450Sbrendan 	if (spa->spa_spares.sav_config) {
6545450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
6555450Sbrendan 		spa->spa_spares.sav_config = NULL;
6562082Seschrock 	}
6575450Sbrendan 
6585450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
6595450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
6605450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
6615450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
6625450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
6635450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
6645450Sbrendan 	}
6655450Sbrendan 	if (spa->spa_l2cache.sav_config) {
6665450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
6675450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
6682082Seschrock 	}
6692082Seschrock 
6701544Seschrock 	spa->spa_async_suspended = 0;
671789Sahrens }
672789Sahrens 
673789Sahrens /*
6742082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6752082Seschrock  * this pool.  When this is called, we have some form of basic information in
6765450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
6775450Sbrendan  * then re-generate a more complete list including status information.
6782082Seschrock  */
6792082Seschrock static void
6802082Seschrock spa_load_spares(spa_t *spa)
6812082Seschrock {
6822082Seschrock 	nvlist_t **spares;
6832082Seschrock 	uint_t nspares;
6842082Seschrock 	int i;
6853377Seschrock 	vdev_t *vd, *tvd;
6862082Seschrock 
6872082Seschrock 	/*
6882082Seschrock 	 * First, close and free any existing spare vdevs.
6892082Seschrock 	 */
6905450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
6915450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
6923377Seschrock 
6933377Seschrock 		/* Undo the call to spa_activate() below */
6946643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
6956643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
6963377Seschrock 			spa_spare_remove(tvd);
6973377Seschrock 		vdev_close(vd);
6983377Seschrock 		vdev_free(vd);
6992082Seschrock 	}
7003377Seschrock 
7015450Sbrendan 	if (spa->spa_spares.sav_vdevs)
7025450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7035450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7045450Sbrendan 
7055450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7062082Seschrock 		nspares = 0;
7072082Seschrock 	else
7085450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7092082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7102082Seschrock 
7115450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7125450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7132082Seschrock 
7142082Seschrock 	if (nspares == 0)
7152082Seschrock 		return;
7162082Seschrock 
7172082Seschrock 	/*
7182082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7193377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7203377Seschrock 	 * structures associated with it: one in the list of spares (used only
7213377Seschrock 	 * for basic validation purposes) and one in the active vdev
7223377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7233377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7243377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7252082Seschrock 	 */
7265450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
7275450Sbrendan 	    KM_SLEEP);
7285450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7292082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
7302082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
7312082Seschrock 		ASSERT(vd != NULL);
7322082Seschrock 
7335450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
7342082Seschrock 
7356643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7366643Seschrock 		    B_FALSE)) != NULL) {
7373377Seschrock 			if (!tvd->vdev_isspare)
7383377Seschrock 				spa_spare_add(tvd);
7393377Seschrock 
7403377Seschrock 			/*
7413377Seschrock 			 * We only mark the spare active if we were successfully
7423377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7433377Seschrock 			 * with a bad active spare would result in strange
7443377Seschrock 			 * behavior, because multiple pool would think the spare
7453377Seschrock 			 * is actively in use.
7463377Seschrock 			 *
7473377Seschrock 			 * There is a vulnerability here to an equally bizarre
7483377Seschrock 			 * circumstance, where a dead active spare is later
7493377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7503377Seschrock 			 * the rarity of this scenario, and the extra complexity
7513377Seschrock 			 * it adds, we ignore the possibility.
7523377Seschrock 			 */
7533377Seschrock 			if (!vdev_is_dead(tvd))
7543377Seschrock 				spa_spare_activate(tvd);
7553377Seschrock 		}
7563377Seschrock 
7572082Seschrock 		if (vdev_open(vd) != 0)
7582082Seschrock 			continue;
7592082Seschrock 
7602082Seschrock 		vd->vdev_top = vd;
7615450Sbrendan 		if (vdev_validate_aux(vd) == 0)
7625450Sbrendan 			spa_spare_add(vd);
7632082Seschrock 	}
7642082Seschrock 
7652082Seschrock 	/*
7662082Seschrock 	 * Recompute the stashed list of spares, with status information
7672082Seschrock 	 * this time.
7682082Seschrock 	 */
7695450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
7702082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7712082Seschrock 
7725450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
7735450Sbrendan 	    KM_SLEEP);
7745450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7755450Sbrendan 		spares[i] = vdev_config_generate(spa,
7765450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
7775450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
7785450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
7795450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7802082Seschrock 		nvlist_free(spares[i]);
7815450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
7825450Sbrendan }
7835450Sbrendan 
7845450Sbrendan /*
7855450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
7865450Sbrendan  * this pool.  When this is called, we have some form of basic information in
7875450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
7885450Sbrendan  * then re-generate a more complete list including status information.
7895450Sbrendan  * Devices which are already active have their details maintained, and are
7905450Sbrendan  * not re-opened.
7915450Sbrendan  */
7925450Sbrendan static void
7935450Sbrendan spa_load_l2cache(spa_t *spa)
7945450Sbrendan {
7955450Sbrendan 	nvlist_t **l2cache;
7965450Sbrendan 	uint_t nl2cache;
7975450Sbrendan 	int i, j, oldnvdevs;
7986643Seschrock 	uint64_t guid, size;
7995450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
8005450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
8015450Sbrendan 
8025450Sbrendan 	if (sav->sav_config != NULL) {
8035450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8045450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8055450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8065450Sbrendan 	} else {
8075450Sbrendan 		nl2cache = 0;
8085450Sbrendan 	}
8095450Sbrendan 
8105450Sbrendan 	oldvdevs = sav->sav_vdevs;
8115450Sbrendan 	oldnvdevs = sav->sav_count;
8125450Sbrendan 	sav->sav_vdevs = NULL;
8135450Sbrendan 	sav->sav_count = 0;
8145450Sbrendan 
8155450Sbrendan 	/*
8165450Sbrendan 	 * Process new nvlist of vdevs.
8175450Sbrendan 	 */
8185450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8195450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8205450Sbrendan 		    &guid) == 0);
8215450Sbrendan 
8225450Sbrendan 		newvdevs[i] = NULL;
8235450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
8245450Sbrendan 			vd = oldvdevs[j];
8255450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
8265450Sbrendan 				/*
8275450Sbrendan 				 * Retain previous vdev for add/remove ops.
8285450Sbrendan 				 */
8295450Sbrendan 				newvdevs[i] = vd;
8305450Sbrendan 				oldvdevs[j] = NULL;
8315450Sbrendan 				break;
8325450Sbrendan 			}
8335450Sbrendan 		}
8345450Sbrendan 
8355450Sbrendan 		if (newvdevs[i] == NULL) {
8365450Sbrendan 			/*
8375450Sbrendan 			 * Create new vdev
8385450Sbrendan 			 */
8395450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
8405450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
8415450Sbrendan 			ASSERT(vd != NULL);
8425450Sbrendan 			newvdevs[i] = vd;
8435450Sbrendan 
8445450Sbrendan 			/*
8455450Sbrendan 			 * Commit this vdev as an l2cache device,
8465450Sbrendan 			 * even if it fails to open.
8475450Sbrendan 			 */
8485450Sbrendan 			spa_l2cache_add(vd);
8495450Sbrendan 
8506643Seschrock 			vd->vdev_top = vd;
8516643Seschrock 			vd->vdev_aux = sav;
8526643Seschrock 
8536643Seschrock 			spa_l2cache_activate(vd);
8546643Seschrock 
8555450Sbrendan 			if (vdev_open(vd) != 0)
8565450Sbrendan 				continue;
8575450Sbrendan 
8585450Sbrendan 			(void) vdev_validate_aux(vd);
8595450Sbrendan 
8605450Sbrendan 			if (!vdev_is_dead(vd)) {
8615450Sbrendan 				size = vdev_get_rsize(vd);
8626643Seschrock 				l2arc_add_vdev(spa, vd,
8636643Seschrock 				    VDEV_LABEL_START_SIZE,
8646643Seschrock 				    size - VDEV_LABEL_START_SIZE);
8655450Sbrendan 			}
8665450Sbrendan 		}
8675450Sbrendan 	}
8685450Sbrendan 
8695450Sbrendan 	/*
8705450Sbrendan 	 * Purge vdevs that were dropped
8715450Sbrendan 	 */
8725450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
8735450Sbrendan 		uint64_t pool;
8745450Sbrendan 
8755450Sbrendan 		vd = oldvdevs[i];
8765450Sbrendan 		if (vd != NULL) {
8775450Sbrendan 			if (spa_mode & FWRITE &&
8785450Sbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
8796643Seschrock 			    pool != 0ULL &&
8806643Seschrock 			    l2arc_vdev_present(vd)) {
8815450Sbrendan 				l2arc_remove_vdev(vd);
8825450Sbrendan 			}
8835450Sbrendan 			(void) vdev_close(vd);
8845450Sbrendan 			spa_l2cache_remove(vd);
8855450Sbrendan 		}
8865450Sbrendan 	}
8875450Sbrendan 
8885450Sbrendan 	if (oldvdevs)
8895450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
8905450Sbrendan 
8915450Sbrendan 	if (sav->sav_config == NULL)
8925450Sbrendan 		goto out;
8935450Sbrendan 
8945450Sbrendan 	sav->sav_vdevs = newvdevs;
8955450Sbrendan 	sav->sav_count = (int)nl2cache;
8965450Sbrendan 
8975450Sbrendan 	/*
8985450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
8995450Sbrendan 	 * information this time.
9005450Sbrendan 	 */
9015450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
9025450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9035450Sbrendan 
9045450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9055450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9065450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9075450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9085450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9095450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9105450Sbrendan out:
9115450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9125450Sbrendan 		nvlist_free(l2cache[i]);
9135450Sbrendan 	if (sav->sav_count)
9145450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9152082Seschrock }
9162082Seschrock 
9172082Seschrock static int
9182082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9192082Seschrock {
9202082Seschrock 	dmu_buf_t *db;
9212082Seschrock 	char *packed = NULL;
9222082Seschrock 	size_t nvsize = 0;
9232082Seschrock 	int error;
9242082Seschrock 	*value = NULL;
9252082Seschrock 
9262082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9272082Seschrock 	nvsize = *(uint64_t *)db->db_data;
9282082Seschrock 	dmu_buf_rele(db, FTAG);
9292082Seschrock 
9302082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
9312082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
9322082Seschrock 	if (error == 0)
9332082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
9342082Seschrock 	kmem_free(packed, nvsize);
9352082Seschrock 
9362082Seschrock 	return (error);
9372082Seschrock }
9382082Seschrock 
9392082Seschrock /*
9404451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9414451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9424451Seschrock  */
9434451Seschrock static void
9444451Seschrock spa_check_removed(vdev_t *vd)
9454451Seschrock {
9464451Seschrock 	int c;
9474451Seschrock 
9484451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9494451Seschrock 		spa_check_removed(vd->vdev_child[c]);
9504451Seschrock 
9514451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9524451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9534451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9544451Seschrock 	}
9554451Seschrock }
9564451Seschrock 
9574451Seschrock /*
958789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
9591544Seschrock  * source of configuration information.
960789Sahrens  */
961789Sahrens static int
9621544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
963789Sahrens {
964789Sahrens 	int error = 0;
965789Sahrens 	nvlist_t *nvroot = NULL;
966789Sahrens 	vdev_t *rvd;
967789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
9681635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
969789Sahrens 	uint64_t pool_guid;
9702082Seschrock 	uint64_t version;
971789Sahrens 	zio_t *zio;
9724451Seschrock 	uint64_t autoreplace = 0;
973789Sahrens 
9741544Seschrock 	spa->spa_load_state = state;
9751635Sbonwick 
976789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
9771733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
9781544Seschrock 		error = EINVAL;
9791544Seschrock 		goto out;
9801544Seschrock 	}
981789Sahrens 
9822082Seschrock 	/*
9832082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
9842082Seschrock 	 * it's not present treat it as the initial version.
9852082Seschrock 	 */
9862082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
9874577Sahrens 		version = SPA_VERSION_INITIAL;
9882082Seschrock 
9891733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
9901733Sbonwick 	    &spa->spa_config_txg);
9911733Sbonwick 
9921635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
9931544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
9941544Seschrock 		error = EEXIST;
9951544Seschrock 		goto out;
9961544Seschrock 	}
997789Sahrens 
9982174Seschrock 	spa->spa_load_guid = pool_guid;
9992174Seschrock 
1000789Sahrens 	/*
10012082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
10022082Seschrock 	 * value that will be returned by spa_version() since parsing the
10032082Seschrock 	 * configuration requires knowing the version number.
1004789Sahrens 	 */
10051544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
10062082Seschrock 	spa->spa_ubsync.ub_version = version;
10072082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
10081544Seschrock 	spa_config_exit(spa, FTAG);
1009789Sahrens 
10102082Seschrock 	if (error != 0)
10111544Seschrock 		goto out;
1012789Sahrens 
10131585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1014789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1015789Sahrens 
1016789Sahrens 	/*
1017789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1018789Sahrens 	 */
10194070Smc142369 	error = vdev_open(rvd);
10204070Smc142369 	if (error != 0)
10211544Seschrock 		goto out;
1022789Sahrens 
1023789Sahrens 	/*
10241986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
10251986Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
10261986Seschrock 	 * flag.
10271986Seschrock 	 */
10281986Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
10291986Seschrock 	error = vdev_validate(rvd);
10301986Seschrock 	spa_config_exit(spa, FTAG);
10311986Seschrock 
10324070Smc142369 	if (error != 0)
10331986Seschrock 		goto out;
10341986Seschrock 
10351986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
10361986Seschrock 		error = ENXIO;
10371986Seschrock 		goto out;
10381986Seschrock 	}
10391986Seschrock 
10401986Seschrock 	/*
1041789Sahrens 	 * Find the best uberblock.
1042789Sahrens 	 */
1043789Sahrens 	bzero(ub, sizeof (uberblock_t));
1044789Sahrens 
1045789Sahrens 	zio = zio_root(spa, NULL, NULL,
1046789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1047789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
1048789Sahrens 	error = zio_wait(zio);
1049789Sahrens 
1050789Sahrens 	/*
1051789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1052789Sahrens 	 */
1053789Sahrens 	if (ub->ub_txg == 0) {
10541760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10551760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10561544Seschrock 		error = ENXIO;
10571544Seschrock 		goto out;
10581544Seschrock 	}
10591544Seschrock 
10601544Seschrock 	/*
10611544Seschrock 	 * If the pool is newer than the code, we can't open it.
10621544Seschrock 	 */
10634577Sahrens 	if (ub->ub_version > SPA_VERSION) {
10641760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10651760Seschrock 		    VDEV_AUX_VERSION_NEWER);
10661544Seschrock 		error = ENOTSUP;
10671544Seschrock 		goto out;
1068789Sahrens 	}
1069789Sahrens 
1070789Sahrens 	/*
1071789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1072789Sahrens 	 * incomplete configuration.
1073789Sahrens 	 */
10741732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
10751544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10761544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
10771544Seschrock 		error = ENXIO;
10781544Seschrock 		goto out;
1079789Sahrens 	}
1080789Sahrens 
1081789Sahrens 	/*
1082789Sahrens 	 * Initialize internal SPA structures.
1083789Sahrens 	 */
1084789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1085789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1086789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
10871544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
10881544Seschrock 	if (error) {
10891544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10901544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10911544Seschrock 		goto out;
10921544Seschrock 	}
1093789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1094789Sahrens 
10951544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1096789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
10971544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
10981544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10991544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11001544Seschrock 		error = EIO;
11011544Seschrock 		goto out;
11021544Seschrock 	}
1103789Sahrens 
1104789Sahrens 	if (!mosconfig) {
11052082Seschrock 		nvlist_t *newconfig;
11063975Sek110237 		uint64_t hostid;
11072082Seschrock 
11082082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
11091544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11101544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11111544Seschrock 			error = EIO;
11121544Seschrock 			goto out;
11131544Seschrock 		}
1114789Sahrens 
11153975Sek110237 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
11163975Sek110237 		    &hostid) == 0) {
11173975Sek110237 			char *hostname;
11183975Sek110237 			unsigned long myhostid = 0;
11193975Sek110237 
11203975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
11213975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
11223975Sek110237 
11233975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
11244178Slling 			if (hostid != 0 && myhostid != 0 &&
11254178Slling 			    (unsigned long)hostid != myhostid) {
11263975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
11273975Sek110237 				    "loaded as it was last accessed by "
11283975Sek110237 				    "another system (host: %s hostid: 0x%lx).  "
11293975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
11303975Sek110237 				    spa->spa_name, hostname,
11313975Sek110237 				    (unsigned long)hostid);
11323975Sek110237 				error = EBADF;
11333975Sek110237 				goto out;
11343975Sek110237 			}
11353975Sek110237 		}
11363975Sek110237 
1137789Sahrens 		spa_config_set(spa, newconfig);
1138789Sahrens 		spa_unload(spa);
1139789Sahrens 		spa_deactivate(spa);
1140789Sahrens 		spa_activate(spa);
1141789Sahrens 
11421544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
11431544Seschrock 	}
11441544Seschrock 
11451544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
11461544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
11471544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
11481544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11491544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11501544Seschrock 		error = EIO;
11511544Seschrock 		goto out;
1152789Sahrens 	}
1153789Sahrens 
11541544Seschrock 	/*
11552082Seschrock 	 * Load the bit that tells us to use the new accounting function
11562082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
11572082Seschrock 	 * be present.
11582082Seschrock 	 */
11592082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11602082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
11612082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
11622082Seschrock 	if (error != 0 && error != ENOENT) {
11632082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11642082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11652082Seschrock 		error = EIO;
11662082Seschrock 		goto out;
11672082Seschrock 	}
11682082Seschrock 
11692082Seschrock 	/*
11701544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
11711544Seschrock 	 * not be present.
11721544Seschrock 	 */
11731544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11741544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
11751544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
11761807Sbonwick 	if (error != 0 && error != ENOENT) {
11771544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11781544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11791544Seschrock 		error = EIO;
11801544Seschrock 		goto out;
11811544Seschrock 	}
11821544Seschrock 
11831544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11841544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
11851544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
11861544Seschrock 	if (error != 0 && error != ENOENT) {
11871544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11881544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11891544Seschrock 		error = EIO;
11901544Seschrock 		goto out;
11911544Seschrock 	}
1192789Sahrens 
1193789Sahrens 	/*
11942926Sek110237 	 * Load the history object.  If we have an older pool, this
11952926Sek110237 	 * will not be present.
11962926Sek110237 	 */
11972926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
11982926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
11992926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
12002926Sek110237 	if (error != 0 && error != ENOENT) {
12012926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12022926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
12032926Sek110237 		error = EIO;
12042926Sek110237 		goto out;
12052926Sek110237 	}
12062926Sek110237 
12072926Sek110237 	/*
12082082Seschrock 	 * Load any hot spares for this pool.
12092082Seschrock 	 */
12102082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12115450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
12122082Seschrock 	if (error != 0 && error != ENOENT) {
12132082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12142082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12152082Seschrock 		error = EIO;
12162082Seschrock 		goto out;
12172082Seschrock 	}
12182082Seschrock 	if (error == 0) {
12194577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
12205450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
12215450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
12222082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12232082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12242082Seschrock 			error = EIO;
12252082Seschrock 			goto out;
12262082Seschrock 		}
12272082Seschrock 
12282082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
12292082Seschrock 		spa_load_spares(spa);
12302082Seschrock 		spa_config_exit(spa, FTAG);
12312082Seschrock 	}
12322082Seschrock 
12335450Sbrendan 	/*
12345450Sbrendan 	 * Load any level 2 ARC devices for this pool.
12355450Sbrendan 	 */
12365450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12375450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
12385450Sbrendan 	    &spa->spa_l2cache.sav_object);
12395450Sbrendan 	if (error != 0 && error != ENOENT) {
12405450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12415450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
12425450Sbrendan 		error = EIO;
12435450Sbrendan 		goto out;
12445450Sbrendan 	}
12455450Sbrendan 	if (error == 0) {
12465450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
12475450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
12485450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
12495450Sbrendan 			vdev_set_state(rvd, B_TRUE,
12505450Sbrendan 			    VDEV_STATE_CANT_OPEN,
12515450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
12525450Sbrendan 			error = EIO;
12535450Sbrendan 			goto out;
12545450Sbrendan 		}
12555450Sbrendan 
12565450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
12575450Sbrendan 		spa_load_l2cache(spa);
12585450Sbrendan 		spa_config_exit(spa, FTAG);
12595450Sbrendan 	}
12605450Sbrendan 
12615094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
12624543Smarks 
12633912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12643912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
12653912Slling 
12663912Slling 	if (error && error != ENOENT) {
12673912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12683912Slling 		    VDEV_AUX_CORRUPT_DATA);
12693912Slling 		error = EIO;
12703912Slling 		goto out;
12713912Slling 	}
12723912Slling 
12733912Slling 	if (error == 0) {
12743912Slling 		(void) zap_lookup(spa->spa_meta_objset,
12753912Slling 		    spa->spa_pool_props_object,
12764451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
12773912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
12784451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
12794451Seschrock 		    spa->spa_pool_props_object,
12804451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
12814451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
12824543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
12834543Smarks 		    spa->spa_pool_props_object,
12844543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
12854543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
12865329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
12875329Sgw25295 		    spa->spa_pool_props_object,
12885329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
12895329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
12903912Slling 	}
12913912Slling 
12922082Seschrock 	/*
12934451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
12944451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
12954451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
12964451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
12974451Seschrock 	 * over.
12984451Seschrock 	 */
12995756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
13004451Seschrock 		spa_check_removed(spa->spa_root_vdev);
13014451Seschrock 
13024451Seschrock 	/*
13031986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1304789Sahrens 	 */
13051986Seschrock 	vdev_load(rvd);
1306789Sahrens 
1307789Sahrens 	/*
1308789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1309789Sahrens 	 */
13101544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1311789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
13121544Seschrock 	spa_config_exit(spa, FTAG);
1313789Sahrens 
1314789Sahrens 	/*
1315789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1316789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1317789Sahrens 	 */
13181544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13191544Seschrock 		error = ENXIO;
13201544Seschrock 		goto out;
13211544Seschrock 	}
1322789Sahrens 
13231544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13241635Sbonwick 		dmu_tx_t *tx;
13251635Sbonwick 		int need_update = B_FALSE;
13261585Sbonwick 		int c;
13271601Sbonwick 
13281635Sbonwick 		/*
13291635Sbonwick 		 * Claim log blocks that haven't been committed yet.
13301635Sbonwick 		 * This must all happen in a single txg.
13311635Sbonwick 		 */
13321601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1333789Sahrens 		    spa_first_txg(spa));
13342417Sahrens 		(void) dmu_objset_find(spa->spa_name,
13352417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1336789Sahrens 		dmu_tx_commit(tx);
1337789Sahrens 
1338789Sahrens 		spa->spa_sync_on = B_TRUE;
1339789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1340789Sahrens 
1341789Sahrens 		/*
1342789Sahrens 		 * Wait for all claims to sync.
1343789Sahrens 		 */
1344789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
13451585Sbonwick 
13461585Sbonwick 		/*
13471635Sbonwick 		 * If the config cache is stale, or we have uninitialized
13481635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
13491585Sbonwick 		 */
13501635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
13511635Sbonwick 		    state == SPA_LOAD_IMPORT)
13521635Sbonwick 			need_update = B_TRUE;
13531635Sbonwick 
13541635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
13551635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
13561635Sbonwick 				need_update = B_TRUE;
13571585Sbonwick 
13581585Sbonwick 		/*
13591635Sbonwick 		 * Update the config cache asychronously in case we're the
13601635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
13611585Sbonwick 		 */
13621635Sbonwick 		if (need_update)
13631635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1364789Sahrens 	}
1365789Sahrens 
13661544Seschrock 	error = 0;
13671544Seschrock out:
13687046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
13692082Seschrock 	if (error && error != EBADF)
13701544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
13711544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
13721544Seschrock 	spa->spa_ena = 0;
13731544Seschrock 
13741544Seschrock 	return (error);
1375789Sahrens }
1376789Sahrens 
1377789Sahrens /*
1378789Sahrens  * Pool Open/Import
1379789Sahrens  *
1380789Sahrens  * The import case is identical to an open except that the configuration is sent
1381789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1382789Sahrens  * case of an open, the pool configuration will exist in the
13834451Seschrock  * POOL_STATE_UNINITIALIZED state.
1384789Sahrens  *
1385789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1386789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1387789Sahrens  * ambiguous state.
1388789Sahrens  */
1389789Sahrens static int
1390789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1391789Sahrens {
1392789Sahrens 	spa_t *spa;
1393789Sahrens 	int error;
1394789Sahrens 	int locked = B_FALSE;
1395789Sahrens 
1396789Sahrens 	*spapp = NULL;
1397789Sahrens 
1398789Sahrens 	/*
1399789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1400789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1401789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1402789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1403789Sahrens 	 */
1404789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1405789Sahrens 		mutex_enter(&spa_namespace_lock);
1406789Sahrens 		locked = B_TRUE;
1407789Sahrens 	}
1408789Sahrens 
1409789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1410789Sahrens 		if (locked)
1411789Sahrens 			mutex_exit(&spa_namespace_lock);
1412789Sahrens 		return (ENOENT);
1413789Sahrens 	}
1414789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1415789Sahrens 
1416789Sahrens 		spa_activate(spa);
1417789Sahrens 
14181635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1419789Sahrens 
1420789Sahrens 		if (error == EBADF) {
1421789Sahrens 			/*
14221986Seschrock 			 * If vdev_validate() returns failure (indicated by
14231986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
14241986Seschrock 			 * that the pool has been exported or destroyed.  If
14251986Seschrock 			 * this is the case, the config cache is out of sync and
14261986Seschrock 			 * we should remove the pool from the namespace.
1427789Sahrens 			 */
1428789Sahrens 			spa_unload(spa);
1429789Sahrens 			spa_deactivate(spa);
14306643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1431789Sahrens 			spa_remove(spa);
1432789Sahrens 			if (locked)
1433789Sahrens 				mutex_exit(&spa_namespace_lock);
1434789Sahrens 			return (ENOENT);
14351544Seschrock 		}
14361544Seschrock 
14371544Seschrock 		if (error) {
1438789Sahrens 			/*
1439789Sahrens 			 * We can't open the pool, but we still have useful
1440789Sahrens 			 * information: the state of each vdev after the
1441789Sahrens 			 * attempted vdev_open().  Return this to the user.
1442789Sahrens 			 */
14431635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
14441635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1445789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1446789Sahrens 				    B_TRUE);
14471635Sbonwick 				spa_config_exit(spa, FTAG);
14481635Sbonwick 			}
1449789Sahrens 			spa_unload(spa);
1450789Sahrens 			spa_deactivate(spa);
14511544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1452789Sahrens 			if (locked)
1453789Sahrens 				mutex_exit(&spa_namespace_lock);
1454789Sahrens 			*spapp = NULL;
1455789Sahrens 			return (error);
14561544Seschrock 		} else {
14571544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1458789Sahrens 		}
1459789Sahrens 	}
1460789Sahrens 
1461789Sahrens 	spa_open_ref(spa, tag);
14624451Seschrock 
1463789Sahrens 	if (locked)
1464789Sahrens 		mutex_exit(&spa_namespace_lock);
1465789Sahrens 
1466789Sahrens 	*spapp = spa;
1467789Sahrens 
1468789Sahrens 	if (config != NULL) {
14691544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1470789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
14711544Seschrock 		spa_config_exit(spa, FTAG);
1472789Sahrens 	}
1473789Sahrens 
1474789Sahrens 	return (0);
1475789Sahrens }
1476789Sahrens 
1477789Sahrens int
1478789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1479789Sahrens {
1480789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1481789Sahrens }
1482789Sahrens 
14831544Seschrock /*
14841544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
14851544Seschrock  * preventing it from being exported or destroyed.
14861544Seschrock  */
14871544Seschrock spa_t *
14881544Seschrock spa_inject_addref(char *name)
14891544Seschrock {
14901544Seschrock 	spa_t *spa;
14911544Seschrock 
14921544Seschrock 	mutex_enter(&spa_namespace_lock);
14931544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
14941544Seschrock 		mutex_exit(&spa_namespace_lock);
14951544Seschrock 		return (NULL);
14961544Seschrock 	}
14971544Seschrock 	spa->spa_inject_ref++;
14981544Seschrock 	mutex_exit(&spa_namespace_lock);
14991544Seschrock 
15001544Seschrock 	return (spa);
15011544Seschrock }
15021544Seschrock 
15031544Seschrock void
15041544Seschrock spa_inject_delref(spa_t *spa)
15051544Seschrock {
15061544Seschrock 	mutex_enter(&spa_namespace_lock);
15071544Seschrock 	spa->spa_inject_ref--;
15081544Seschrock 	mutex_exit(&spa_namespace_lock);
15091544Seschrock }
15101544Seschrock 
15115450Sbrendan /*
15125450Sbrendan  * Add spares device information to the nvlist.
15135450Sbrendan  */
15142082Seschrock static void
15152082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
15162082Seschrock {
15172082Seschrock 	nvlist_t **spares;
15182082Seschrock 	uint_t i, nspares;
15192082Seschrock 	nvlist_t *nvroot;
15202082Seschrock 	uint64_t guid;
15212082Seschrock 	vdev_stat_t *vs;
15222082Seschrock 	uint_t vsc;
15233377Seschrock 	uint64_t pool;
15242082Seschrock 
15255450Sbrendan 	if (spa->spa_spares.sav_count == 0)
15262082Seschrock 		return;
15272082Seschrock 
15282082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
15292082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15305450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
15312082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15322082Seschrock 	if (nspares != 0) {
15332082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
15342082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15352082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15362082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15372082Seschrock 
15382082Seschrock 		/*
15392082Seschrock 		 * Go through and find any spares which have since been
15402082Seschrock 		 * repurposed as an active spare.  If this is the case, update
15412082Seschrock 		 * their status appropriately.
15422082Seschrock 		 */
15432082Seschrock 		for (i = 0; i < nspares; i++) {
15442082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
15452082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15463377Seschrock 			if (spa_spare_exists(guid, &pool) && pool != 0ULL) {
15472082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
15482082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
15492082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
15502082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
15512082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
15522082Seschrock 			}
15532082Seschrock 		}
15542082Seschrock 	}
15552082Seschrock }
15562082Seschrock 
15575450Sbrendan /*
15585450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
15595450Sbrendan  */
15605450Sbrendan static void
15615450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
15625450Sbrendan {
15635450Sbrendan 	nvlist_t **l2cache;
15645450Sbrendan 	uint_t i, j, nl2cache;
15655450Sbrendan 	nvlist_t *nvroot;
15665450Sbrendan 	uint64_t guid;
15675450Sbrendan 	vdev_t *vd;
15685450Sbrendan 	vdev_stat_t *vs;
15695450Sbrendan 	uint_t vsc;
15705450Sbrendan 
15715450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
15725450Sbrendan 		return;
15735450Sbrendan 
15745450Sbrendan 	spa_config_enter(spa, RW_READER, FTAG);
15755450Sbrendan 
15765450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
15775450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15785450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
15795450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
15805450Sbrendan 	if (nl2cache != 0) {
15815450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
15825450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
15835450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15845450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
15855450Sbrendan 
15865450Sbrendan 		/*
15875450Sbrendan 		 * Update level 2 cache device stats.
15885450Sbrendan 		 */
15895450Sbrendan 
15905450Sbrendan 		for (i = 0; i < nl2cache; i++) {
15915450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
15925450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15935450Sbrendan 
15945450Sbrendan 			vd = NULL;
15955450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
15965450Sbrendan 				if (guid ==
15975450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
15985450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
15995450Sbrendan 					break;
16005450Sbrendan 				}
16015450Sbrendan 			}
16025450Sbrendan 			ASSERT(vd != NULL);
16035450Sbrendan 
16045450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
16055450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
16065450Sbrendan 			vdev_get_stats(vd, vs);
16075450Sbrendan 		}
16085450Sbrendan 	}
16095450Sbrendan 
16105450Sbrendan 	spa_config_exit(spa, FTAG);
16115450Sbrendan }
16125450Sbrendan 
1613789Sahrens int
16141544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1615789Sahrens {
1616789Sahrens 	int error;
1617789Sahrens 	spa_t *spa;
1618789Sahrens 
1619789Sahrens 	*config = NULL;
1620789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1621789Sahrens 
16222082Seschrock 	if (spa && *config != NULL) {
16231544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
16241544Seschrock 		    spa_get_errlog_size(spa)) == 0);
16251544Seschrock 
16262082Seschrock 		spa_add_spares(spa, *config);
16275450Sbrendan 		spa_add_l2cache(spa, *config);
16282082Seschrock 	}
16292082Seschrock 
16301544Seschrock 	/*
16311544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
16321544Seschrock 	 * and call spa_lookup() directly.
16331544Seschrock 	 */
16341544Seschrock 	if (altroot) {
16351544Seschrock 		if (spa == NULL) {
16361544Seschrock 			mutex_enter(&spa_namespace_lock);
16371544Seschrock 			spa = spa_lookup(name);
16381544Seschrock 			if (spa)
16391544Seschrock 				spa_altroot(spa, altroot, buflen);
16401544Seschrock 			else
16411544Seschrock 				altroot[0] = '\0';
16421544Seschrock 			spa = NULL;
16431544Seschrock 			mutex_exit(&spa_namespace_lock);
16441544Seschrock 		} else {
16451544Seschrock 			spa_altroot(spa, altroot, buflen);
16461544Seschrock 		}
16471544Seschrock 	}
16481544Seschrock 
1649789Sahrens 	if (spa != NULL)
1650789Sahrens 		spa_close(spa, FTAG);
1651789Sahrens 
1652789Sahrens 	return (error);
1653789Sahrens }
1654789Sahrens 
1655789Sahrens /*
16565450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
16575450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
16585450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
16595450Sbrendan  * specified, as long as they are well-formed.
16602082Seschrock  */
16612082Seschrock static int
16625450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
16635450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
16645450Sbrendan     vdev_labeltype_t label)
16652082Seschrock {
16665450Sbrendan 	nvlist_t **dev;
16675450Sbrendan 	uint_t i, ndev;
16682082Seschrock 	vdev_t *vd;
16692082Seschrock 	int error;
16702082Seschrock 
16712082Seschrock 	/*
16725450Sbrendan 	 * It's acceptable to have no devs specified.
16732082Seschrock 	 */
16745450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
16752082Seschrock 		return (0);
16762082Seschrock 
16775450Sbrendan 	if (ndev == 0)
16782082Seschrock 		return (EINVAL);
16792082Seschrock 
16802082Seschrock 	/*
16815450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
16825450Sbrendan 	 * device type.
16832082Seschrock 	 */
16845450Sbrendan 	if (spa_version(spa) < version)
16852082Seschrock 		return (ENOTSUP);
16862082Seschrock 
16873377Seschrock 	/*
16885450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
16893377Seschrock 	 * checking.
16903377Seschrock 	 */
16915450Sbrendan 	sav->sav_pending = dev;
16925450Sbrendan 	sav->sav_npending = ndev;
16935450Sbrendan 
16945450Sbrendan 	for (i = 0; i < ndev; i++) {
16955450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
16962082Seschrock 		    mode)) != 0)
16973377Seschrock 			goto out;
16982082Seschrock 
16992082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
17002082Seschrock 			vdev_free(vd);
17013377Seschrock 			error = EINVAL;
17023377Seschrock 			goto out;
17032082Seschrock 		}
17042082Seschrock 
17055450Sbrendan 		/*
17065450Sbrendan 		 * The L2ARC currently only supports disk devices.
17075450Sbrendan 		 */
17085450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
17095450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
17105450Sbrendan 			error = ENOTBLK;
17115450Sbrendan 			goto out;
17125450Sbrendan 		}
17135450Sbrendan 
17142082Seschrock 		vd->vdev_top = vd;
17153377Seschrock 
17163377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
17175450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
17185450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
17193377Seschrock 			    vd->vdev_guid) == 0);
17202082Seschrock 		}
17212082Seschrock 
17222082Seschrock 		vdev_free(vd);
17233377Seschrock 
17245450Sbrendan 		if (error &&
17255450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
17263377Seschrock 			goto out;
17273377Seschrock 		else
17283377Seschrock 			error = 0;
17292082Seschrock 	}
17302082Seschrock 
17313377Seschrock out:
17325450Sbrendan 	sav->sav_pending = NULL;
17335450Sbrendan 	sav->sav_npending = 0;
17343377Seschrock 	return (error);
17352082Seschrock }
17362082Seschrock 
17375450Sbrendan static int
17385450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
17395450Sbrendan {
17405450Sbrendan 	int error;
17415450Sbrendan 
17425450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17435450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
17445450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
17455450Sbrendan 		return (error);
17465450Sbrendan 	}
17475450Sbrendan 
17485450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17495450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
17505450Sbrendan 	    VDEV_LABEL_L2CACHE));
17515450Sbrendan }
17525450Sbrendan 
17535450Sbrendan static void
17545450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
17555450Sbrendan     const char *config)
17565450Sbrendan {
17575450Sbrendan 	int i;
17585450Sbrendan 
17595450Sbrendan 	if (sav->sav_config != NULL) {
17605450Sbrendan 		nvlist_t **olddevs;
17615450Sbrendan 		uint_t oldndevs;
17625450Sbrendan 		nvlist_t **newdevs;
17635450Sbrendan 
17645450Sbrendan 		/*
17655450Sbrendan 		 * Generate new dev list by concatentating with the
17665450Sbrendan 		 * current dev list.
17675450Sbrendan 		 */
17685450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
17695450Sbrendan 		    &olddevs, &oldndevs) == 0);
17705450Sbrendan 
17715450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
17725450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
17735450Sbrendan 		for (i = 0; i < oldndevs; i++)
17745450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
17755450Sbrendan 			    KM_SLEEP) == 0);
17765450Sbrendan 		for (i = 0; i < ndevs; i++)
17775450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
17785450Sbrendan 			    KM_SLEEP) == 0);
17795450Sbrendan 
17805450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
17815450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
17825450Sbrendan 
17835450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
17845450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
17855450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
17865450Sbrendan 			nvlist_free(newdevs[i]);
17875450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
17885450Sbrendan 	} else {
17895450Sbrendan 		/*
17905450Sbrendan 		 * Generate a new dev list.
17915450Sbrendan 		 */
17925450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
17935450Sbrendan 		    KM_SLEEP) == 0);
17945450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
17955450Sbrendan 		    devs, ndevs) == 0);
17965450Sbrendan 	}
17975450Sbrendan }
17985450Sbrendan 
17995450Sbrendan /*
18005450Sbrendan  * Stop and drop level 2 ARC devices
18015450Sbrendan  */
18025450Sbrendan void
18035450Sbrendan spa_l2cache_drop(spa_t *spa)
18045450Sbrendan {
18055450Sbrendan 	vdev_t *vd;
18065450Sbrendan 	int i;
18075450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
18085450Sbrendan 
18095450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
18105450Sbrendan 		uint64_t pool;
18115450Sbrendan 
18125450Sbrendan 		vd = sav->sav_vdevs[i];
18135450Sbrendan 		ASSERT(vd != NULL);
18145450Sbrendan 
18155450Sbrendan 		if (spa_mode & FWRITE &&
18166643Seschrock 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL &&
18176643Seschrock 		    l2arc_vdev_present(vd)) {
18185450Sbrendan 			l2arc_remove_vdev(vd);
18195450Sbrendan 		}
18205450Sbrendan 		if (vd->vdev_isl2cache)
18215450Sbrendan 			spa_l2cache_remove(vd);
18225450Sbrendan 		vdev_clear_stats(vd);
18235450Sbrendan 		(void) vdev_close(vd);
18245450Sbrendan 	}
18255450Sbrendan }
18265450Sbrendan 
18272082Seschrock /*
1828789Sahrens  * Pool Creation
1829789Sahrens  */
1830789Sahrens int
18315094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
1832*7184Stimh     const char *history_str, nvlist_t *zplprops)
1833789Sahrens {
1834789Sahrens 	spa_t *spa;
18355094Slling 	char *altroot = NULL;
18361635Sbonwick 	vdev_t *rvd;
1837789Sahrens 	dsl_pool_t *dp;
1838789Sahrens 	dmu_tx_t *tx;
18392082Seschrock 	int c, error = 0;
1840789Sahrens 	uint64_t txg = TXG_INITIAL;
18415450Sbrendan 	nvlist_t **spares, **l2cache;
18425450Sbrendan 	uint_t nspares, nl2cache;
18435094Slling 	uint64_t version;
1844789Sahrens 
1845789Sahrens 	/*
1846789Sahrens 	 * If this pool already exists, return failure.
1847789Sahrens 	 */
1848789Sahrens 	mutex_enter(&spa_namespace_lock);
1849789Sahrens 	if (spa_lookup(pool) != NULL) {
1850789Sahrens 		mutex_exit(&spa_namespace_lock);
1851789Sahrens 		return (EEXIST);
1852789Sahrens 	}
1853789Sahrens 
1854789Sahrens 	/*
1855789Sahrens 	 * Allocate a new spa_t structure.
1856789Sahrens 	 */
18575094Slling 	(void) nvlist_lookup_string(props,
18585094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
18591635Sbonwick 	spa = spa_add(pool, altroot);
1860789Sahrens 	spa_activate(spa);
1861789Sahrens 
1862789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
18635094Slling 
18645094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
18655094Slling 		spa_unload(spa);
18665094Slling 		spa_deactivate(spa);
18675094Slling 		spa_remove(spa);
18686643Seschrock 		mutex_exit(&spa_namespace_lock);
18695094Slling 		return (error);
18705094Slling 	}
18715094Slling 
18725094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
18735094Slling 	    &version) != 0)
18745094Slling 		version = SPA_VERSION;
18755094Slling 	ASSERT(version <= SPA_VERSION);
18765094Slling 	spa->spa_uberblock.ub_version = version;
1877789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1878789Sahrens 
18791635Sbonwick 	/*
18801635Sbonwick 	 * Create the root vdev.
18811635Sbonwick 	 */
18821635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
18831635Sbonwick 
18842082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
18852082Seschrock 
18862082Seschrock 	ASSERT(error != 0 || rvd != NULL);
18872082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
18882082Seschrock 
18895913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
18901635Sbonwick 		error = EINVAL;
18912082Seschrock 
18922082Seschrock 	if (error == 0 &&
18932082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
18945450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
18952082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
18962082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
18972082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
18982082Seschrock 		vdev_config_dirty(rvd);
18991635Sbonwick 	}
19001635Sbonwick 
19011635Sbonwick 	spa_config_exit(spa, FTAG);
1902789Sahrens 
19032082Seschrock 	if (error != 0) {
1904789Sahrens 		spa_unload(spa);
1905789Sahrens 		spa_deactivate(spa);
1906789Sahrens 		spa_remove(spa);
1907789Sahrens 		mutex_exit(&spa_namespace_lock);
1908789Sahrens 		return (error);
1909789Sahrens 	}
1910789Sahrens 
19112082Seschrock 	/*
19122082Seschrock 	 * Get the list of spares, if specified.
19132082Seschrock 	 */
19142082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19152082Seschrock 	    &spares, &nspares) == 0) {
19165450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
19172082Seschrock 		    KM_SLEEP) == 0);
19185450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
19192082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
19202082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
19212082Seschrock 		spa_load_spares(spa);
19222082Seschrock 		spa_config_exit(spa, FTAG);
19235450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
19245450Sbrendan 	}
19255450Sbrendan 
19265450Sbrendan 	/*
19275450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
19285450Sbrendan 	 */
19295450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
19305450Sbrendan 	    &l2cache, &nl2cache) == 0) {
19315450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
19325450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
19335450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
19345450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
19355450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
19365450Sbrendan 		spa_load_l2cache(spa);
19375450Sbrendan 		spa_config_exit(spa, FTAG);
19385450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
19392082Seschrock 	}
19402082Seschrock 
1941*7184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
1942789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1943789Sahrens 
1944789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1945789Sahrens 
1946789Sahrens 	/*
1947789Sahrens 	 * Create the pool config object.
1948789Sahrens 	 */
1949789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1950789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1951789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1952789Sahrens 
19531544Seschrock 	if (zap_add(spa->spa_meta_objset,
1954789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
19551544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
19561544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
19571544Seschrock 	}
1958789Sahrens 
19595094Slling 	/* Newly created pools with the right version are always deflated. */
19605094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
19615094Slling 		spa->spa_deflate = TRUE;
19625094Slling 		if (zap_add(spa->spa_meta_objset,
19635094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
19645094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
19655094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
19665094Slling 		}
19672082Seschrock 	}
19682082Seschrock 
1969789Sahrens 	/*
1970789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
1971789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
1972789Sahrens 	 * keeps changing.
1973789Sahrens 	 */
1974789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
1975789Sahrens 	    1 << 14, tx);
1976789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
1977789Sahrens 	    ZIO_COMPRESS_OFF, tx);
1978789Sahrens 
19791544Seschrock 	if (zap_add(spa->spa_meta_objset,
1980789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
19811544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
19821544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
19831544Seschrock 	}
1984789Sahrens 
19852926Sek110237 	/*
19862926Sek110237 	 * Create the pool's history object.
19872926Sek110237 	 */
19885094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
19895094Slling 		spa_history_create_obj(spa, tx);
19905094Slling 
19915094Slling 	/*
19925094Slling 	 * Set pool properties.
19935094Slling 	 */
19945094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
19955094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19965329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
19975094Slling 	if (props)
19985094Slling 		spa_sync_props(spa, props, CRED(), tx);
19992926Sek110237 
2000789Sahrens 	dmu_tx_commit(tx);
2001789Sahrens 
2002789Sahrens 	spa->spa_sync_on = B_TRUE;
2003789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2004789Sahrens 
2005789Sahrens 	/*
2006789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2007789Sahrens 	 * bean counters are appropriately updated.
2008789Sahrens 	 */
2009789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2010789Sahrens 
20116643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2012789Sahrens 
20135094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
20144715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
20154715Sek110237 
2016789Sahrens 	mutex_exit(&spa_namespace_lock);
2017789Sahrens 
20187046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
20197046Sahrens 
2020789Sahrens 	return (0);
2021789Sahrens }
2022789Sahrens 
2023789Sahrens /*
2024789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2025789Sahrens  * then call spa_load() to do the dirty work.
2026789Sahrens  */
20276423Sgw25295 static int
20286423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
20296643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2030789Sahrens {
2031789Sahrens 	spa_t *spa;
20325094Slling 	char *altroot = NULL;
20336643Seschrock 	int error, loaderr;
20342082Seschrock 	nvlist_t *nvroot;
20355450Sbrendan 	nvlist_t **spares, **l2cache;
20365450Sbrendan 	uint_t nspares, nl2cache;
2037789Sahrens 
2038789Sahrens 	/*
2039789Sahrens 	 * If a pool with this name exists, return failure.
2040789Sahrens 	 */
2041789Sahrens 	mutex_enter(&spa_namespace_lock);
2042789Sahrens 	if (spa_lookup(pool) != NULL) {
2043789Sahrens 		mutex_exit(&spa_namespace_lock);
2044789Sahrens 		return (EEXIST);
2045789Sahrens 	}
2046789Sahrens 
2047789Sahrens 	/*
20481635Sbonwick 	 * Create and initialize the spa structure.
2049789Sahrens 	 */
20505094Slling 	(void) nvlist_lookup_string(props,
20515094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20521635Sbonwick 	spa = spa_add(pool, altroot);
2053789Sahrens 	spa_activate(spa);
2054789Sahrens 
20556643Seschrock 	if (allowfaulted)
20566643Seschrock 		spa->spa_import_faulted = B_TRUE;
20576673Seschrock 	spa->spa_is_root = isroot;
20586643Seschrock 
2059789Sahrens 	/*
20601635Sbonwick 	 * Pass off the heavy lifting to spa_load().
20617046Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
20627046Sahrens 	 * the user-supplied config is actually the one to trust when
20637046Sahrens 	 * doing an import.
20641601Sbonwick 	 */
20657046Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2066789Sahrens 
20672082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
20682082Seschrock 	/*
20692082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
20702082Seschrock 	 * and conflicts with spa_has_spare().
20712082Seschrock 	 */
20726423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
20735450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
20745450Sbrendan 		spa->spa_spares.sav_config = NULL;
20752082Seschrock 		spa_load_spares(spa);
20762082Seschrock 	}
20776423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
20785450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
20795450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
20805450Sbrendan 		spa_load_l2cache(spa);
20815450Sbrendan 	}
20822082Seschrock 
20832082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
20842082Seschrock 	    &nvroot) == 0);
20855450Sbrendan 	if (error == 0)
20865450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
20875450Sbrendan 	if (error == 0)
20885450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
20895450Sbrendan 		    VDEV_ALLOC_L2CACHE);
20902082Seschrock 	spa_config_exit(spa, FTAG);
20912082Seschrock 
20925094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
20936643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
20946643Seschrock 			/*
20956643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
20966643Seschrock 			 * set, then manually set the config as if the config
20976643Seschrock 			 * passed in was specified in the cache file.
20986643Seschrock 			 */
20996643Seschrock 			error = 0;
21006643Seschrock 			spa->spa_import_faulted = B_FALSE;
21016643Seschrock 			if (spa->spa_config == NULL) {
21026643Seschrock 				spa_config_enter(spa, RW_READER, FTAG);
21036643Seschrock 				spa->spa_config = spa_config_generate(spa,
21046643Seschrock 				    NULL, -1ULL, B_TRUE);
21056643Seschrock 				spa_config_exit(spa, FTAG);
21066643Seschrock 			}
21076643Seschrock 			spa_unload(spa);
21086643Seschrock 			spa_deactivate(spa);
21096643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
21106643Seschrock 		} else {
21116643Seschrock 			spa_unload(spa);
21126643Seschrock 			spa_deactivate(spa);
21136643Seschrock 			spa_remove(spa);
21146643Seschrock 		}
2115789Sahrens 		mutex_exit(&spa_namespace_lock);
2116789Sahrens 		return (error);
2117789Sahrens 	}
2118789Sahrens 
21191635Sbonwick 	/*
21205450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
21215450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
21222082Seschrock 	 */
21232082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
21242082Seschrock 	    &spares, &nspares) == 0) {
21255450Sbrendan 		if (spa->spa_spares.sav_config)
21265450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
21272082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
21282082Seschrock 		else
21295450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
21302082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21315450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
21322082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
21332082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
21342082Seschrock 		spa_load_spares(spa);
21352082Seschrock 		spa_config_exit(spa, FTAG);
21365450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
21375450Sbrendan 	}
21385450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
21395450Sbrendan 	    &l2cache, &nl2cache) == 0) {
21405450Sbrendan 		if (spa->spa_l2cache.sav_config)
21415450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
21425450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
21435450Sbrendan 		else
21445450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
21455450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21465450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
21475450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
21485450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
21495450Sbrendan 		spa_load_l2cache(spa);
21505450Sbrendan 		spa_config_exit(spa, FTAG);
21515450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
21522082Seschrock 	}
21532082Seschrock 
21546643Seschrock 	if (spa_mode & FWRITE) {
21556643Seschrock 		/*
21566643Seschrock 		 * Update the config cache to include the newly-imported pool.
21576643Seschrock 		 */
21586423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
21596643Seschrock 	}
21606643Seschrock 
21616643Seschrock 	spa->spa_import_faulted = B_FALSE;
21624451Seschrock 	mutex_exit(&spa_namespace_lock);
21634451Seschrock 
2164789Sahrens 	return (0);
2165789Sahrens }
2166789Sahrens 
21676423Sgw25295 #ifdef _KERNEL
21686423Sgw25295 /*
21696423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
21706423Sgw25295  * device label.
21716423Sgw25295  */
21726423Sgw25295 static void
21736423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
21746423Sgw25295 {
21756423Sgw25295 	nvlist_t *nvtop, *nvroot;
21766423Sgw25295 	uint64_t pgid;
21776423Sgw25295 
21786423Sgw25295 	/*
21796423Sgw25295 	 * Add this top-level vdev to the child array.
21806423Sgw25295 	 */
21816423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
21826423Sgw25295 	    == 0);
21836423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
21846423Sgw25295 	    == 0);
21856423Sgw25295 
21866423Sgw25295 	/*
21876423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
21886423Sgw25295 	 */
21896423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
21906423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
21916423Sgw25295 	    == 0);
21926423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
21936423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
21946423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
21956423Sgw25295 	    &nvtop, 1) == 0);
21966423Sgw25295 
21976423Sgw25295 	/*
21986423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
21996423Sgw25295 	 * this pool's configuration (remove the old, add the new).
22006423Sgw25295 	 */
22016423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
22026423Sgw25295 	nvlist_free(nvroot);
22036423Sgw25295 }
22046423Sgw25295 
22056423Sgw25295 /*
22066423Sgw25295  * Get the root pool information from the root disk, then import the root pool
22076423Sgw25295  * during the system boot up time.
22086423Sgw25295  */
22097147Staylor extern nvlist_t *vdev_disk_read_rootlabel(char *, char *);
22107147Staylor 
22117147Staylor int
22127147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
22136423Sgw25295     uint64_t *besttxg)
22146423Sgw25295 {
22156423Sgw25295 	nvlist_t *config;
22166423Sgw25295 	uint64_t txg;
22176423Sgw25295 
22187147Staylor 	if ((config = vdev_disk_read_rootlabel(devpath, devid)) == NULL)
22197147Staylor 		return (-1);
22206423Sgw25295 
22216423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
22226423Sgw25295 
22237147Staylor 	if (bestconf != NULL)
22246423Sgw25295 		*bestconf = config;
22257147Staylor 	*besttxg = txg;
22267147Staylor 	return (0);
22276423Sgw25295 }
22286423Sgw25295 
22296423Sgw25295 boolean_t
22306423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
22316423Sgw25295 {
22326423Sgw25295 	uint64_t ival;
22336423Sgw25295 
22346423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
22356423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
22366423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
22376423Sgw25295 		return (B_FALSE);
22386423Sgw25295 
22396423Sgw25295 	return (B_TRUE);
22406423Sgw25295 }
22416423Sgw25295 
22427147Staylor 
22437147Staylor /*
22447147Staylor  * Given the boot device's physical path or devid, check if the device
22457147Staylor  * is in a valid state.  If so, return the configuration from the vdev
22467147Staylor  * label.
22477147Staylor  */
22487147Staylor int
22497147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
22507147Staylor {
22517147Staylor 	nvlist_t *conf = NULL;
22527147Staylor 	uint64_t txg = 0;
22537147Staylor 	nvlist_t *nvtop, **child;
22547147Staylor 	char *type;
22557147Staylor 	char *bootpath = NULL;
22567147Staylor 	uint_t children, c;
22577147Staylor 	char *tmp;
22587147Staylor 
22597147Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
22607147Staylor 		*tmp = '\0';
22617147Staylor 	if (spa_check_rootconf(devpath, devid, &conf, &txg) < 0) {
22627147Staylor 		cmn_err(CE_NOTE, "error reading device label");
22637147Staylor 		nvlist_free(conf);
22647147Staylor 		return (EINVAL);
22657147Staylor 	}
22667147Staylor 	if (txg == 0) {
22677147Staylor 		cmn_err(CE_NOTE, "this device is detached");
22687147Staylor 		nvlist_free(conf);
22697147Staylor 		return (EINVAL);
22707147Staylor 	}
22717147Staylor 
22727147Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
22737147Staylor 	    &nvtop) == 0);
22747147Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
22757147Staylor 
22767147Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
22777147Staylor 		if (spa_rootdev_validate(nvtop)) {
22787147Staylor 			goto out;
22797147Staylor 		} else {
22807147Staylor 			nvlist_free(conf);
22817147Staylor 			return (EINVAL);
22827147Staylor 		}
22837147Staylor 	}
22847147Staylor 
22857147Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
22867147Staylor 
22877147Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
22887147Staylor 	    &child, &children) == 0);
22897147Staylor 
22907147Staylor 	/*
22917147Staylor 	 * Go thru vdevs in the mirror to see if the given device
22927147Staylor 	 * has the most recent txg. Only the device with the most
22937147Staylor 	 * recent txg has valid information and should be booted.
22947147Staylor 	 */
22957147Staylor 	for (c = 0; c < children; c++) {
22967147Staylor 		char *cdevid, *cpath;
22977147Staylor 		uint64_t tmptxg;
22987147Staylor 
22997147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
23007147Staylor 		    &cpath) != 0)
23017147Staylor 			return (EINVAL);
23027147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID,
23037147Staylor 		    &cdevid) != 0)
23047147Staylor 			return (EINVAL);
23057147Staylor 		if ((spa_check_rootconf(cpath, cdevid, NULL,
23067147Staylor 		    &tmptxg) == 0) && (tmptxg > txg)) {
23077147Staylor 			txg = tmptxg;
23087147Staylor 			VERIFY(nvlist_lookup_string(child[c],
23097147Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
23107147Staylor 		}
23117147Staylor 	}
23127147Staylor 
23137147Staylor 	/* Does the best device match the one we've booted from? */
23147147Staylor 	if (bootpath) {
23157147Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
23167147Staylor 		return (EINVAL);
23177147Staylor 	}
23187147Staylor out:
23197147Staylor 	*bestconf = conf;
23207147Staylor 	return (0);
23217147Staylor }
23227147Staylor 
23236423Sgw25295 /*
23246423Sgw25295  * Import a root pool.
23256423Sgw25295  *
23267147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
23277147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
23287147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
23296423Sgw25295  *
23306423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
23316423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
23326423Sgw25295  * e.g.
23336423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
23346423Sgw25295  */
23356423Sgw25295 int
23367147Staylor spa_import_rootpool(char *devpath, char *devid)
23376423Sgw25295 {
23386423Sgw25295 	nvlist_t *conf = NULL;
23396423Sgw25295 	char *pname;
23406423Sgw25295 	int error;
23416423Sgw25295 
23426423Sgw25295 	/*
23436423Sgw25295 	 * Get the vdev pathname and configuation from the most
23446423Sgw25295 	 * recently updated vdev (highest txg).
23456423Sgw25295 	 */
23467147Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
23476423Sgw25295 		goto msg_out;
23486423Sgw25295 
23496423Sgw25295 	/*
23506423Sgw25295 	 * Add type "root" vdev to the config.
23516423Sgw25295 	 */
23526423Sgw25295 	spa_build_rootpool_config(conf);
23536423Sgw25295 
23546423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
23556423Sgw25295 
23566673Seschrock 	/*
23576673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
23586673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
23596673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
23606673Seschrock 	 * pool open, not import.
23616673Seschrock 	 */
23626673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
23636423Sgw25295 	if (error == EEXIST)
23646423Sgw25295 		error = 0;
23656423Sgw25295 
23666423Sgw25295 	nvlist_free(conf);
23676423Sgw25295 	return (error);
23686423Sgw25295 
23696423Sgw25295 msg_out:
23707147Staylor 	cmn_err(CE_NOTE, "\n"
23716423Sgw25295 	    "  ***************************************************  \n"
23726423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
23736423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
23746423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
23757147Staylor 	    "  ***************************************************  ");
23766423Sgw25295 
23776423Sgw25295 	return (error);
23786423Sgw25295 }
23796423Sgw25295 #endif
23806423Sgw25295 
23816423Sgw25295 /*
23826423Sgw25295  * Import a non-root pool into the system.
23836423Sgw25295  */
23846423Sgw25295 int
23856423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
23866423Sgw25295 {
23876643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
23886423Sgw25295 }
23896423Sgw25295 
23906643Seschrock int
23916643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
23926643Seschrock {
23936643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
23946643Seschrock }
23956643Seschrock 
23966643Seschrock 
2397789Sahrens /*
2398789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2399789Sahrens  * to get the vdev stats associated with the imported devices.
2400789Sahrens  */
2401789Sahrens #define	TRYIMPORT_NAME	"$import"
2402789Sahrens 
2403789Sahrens nvlist_t *
2404789Sahrens spa_tryimport(nvlist_t *tryconfig)
2405789Sahrens {
2406789Sahrens 	nvlist_t *config = NULL;
2407789Sahrens 	char *poolname;
2408789Sahrens 	spa_t *spa;
2409789Sahrens 	uint64_t state;
2410789Sahrens 
2411789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2412789Sahrens 		return (NULL);
2413789Sahrens 
2414789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2415789Sahrens 		return (NULL);
2416789Sahrens 
24171635Sbonwick 	/*
24181635Sbonwick 	 * Create and initialize the spa structure.
24191635Sbonwick 	 */
2420789Sahrens 	mutex_enter(&spa_namespace_lock);
24211635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2422789Sahrens 	spa_activate(spa);
2423789Sahrens 
2424789Sahrens 	/*
24251635Sbonwick 	 * Pass off the heavy lifting to spa_load().
24261732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
24271732Sbonwick 	 * is actually the one to trust when doing an import.
2428789Sahrens 	 */
24291732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2430789Sahrens 
2431789Sahrens 	/*
2432789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2433789Sahrens 	 */
2434789Sahrens 	if (spa->spa_root_vdev != NULL) {
24351635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
2436789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
24371635Sbonwick 		spa_config_exit(spa, FTAG);
2438789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2439789Sahrens 		    poolname) == 0);
2440789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2441789Sahrens 		    state) == 0);
24423975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
24433975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
24442082Seschrock 
24452082Seschrock 		/*
24466423Sgw25295 		 * If the bootfs property exists on this pool then we
24476423Sgw25295 		 * copy it out so that external consumers can tell which
24486423Sgw25295 		 * pools are bootable.
24496423Sgw25295 		 */
24506423Sgw25295 		if (spa->spa_bootfs) {
24516423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
24526423Sgw25295 
24536423Sgw25295 			/*
24546423Sgw25295 			 * We have to play games with the name since the
24556423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
24566423Sgw25295 			 */
24576423Sgw25295 			if (dsl_dsobj_to_dsname(spa->spa_name,
24586423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
24596423Sgw25295 				char *cp;
24606423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
24616423Sgw25295 
24626423Sgw25295 				cp = strchr(tmpname, '/');
24636423Sgw25295 				if (cp == NULL) {
24646423Sgw25295 					(void) strlcpy(dsname, tmpname,
24656423Sgw25295 					    MAXPATHLEN);
24666423Sgw25295 				} else {
24676423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
24686423Sgw25295 					    "%s/%s", poolname, ++cp);
24696423Sgw25295 				}
24706423Sgw25295 				VERIFY(nvlist_add_string(config,
24716423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
24726423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
24736423Sgw25295 			}
24746423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
24756423Sgw25295 		}
24766423Sgw25295 
24776423Sgw25295 		/*
24785450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
24792082Seschrock 		 */
24802082Seschrock 		spa_add_spares(spa, config);
24815450Sbrendan 		spa_add_l2cache(spa, config);
2482789Sahrens 	}
2483789Sahrens 
2484789Sahrens 	spa_unload(spa);
2485789Sahrens 	spa_deactivate(spa);
2486789Sahrens 	spa_remove(spa);
2487789Sahrens 	mutex_exit(&spa_namespace_lock);
2488789Sahrens 
2489789Sahrens 	return (config);
2490789Sahrens }
2491789Sahrens 
2492789Sahrens /*
2493789Sahrens  * Pool export/destroy
2494789Sahrens  *
2495789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2496789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2497789Sahrens  * update the pool state and sync all the labels to disk, removing the
2498789Sahrens  * configuration from the cache afterwards.
2499789Sahrens  */
2500789Sahrens static int
25011775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig)
2502789Sahrens {
2503789Sahrens 	spa_t *spa;
2504789Sahrens 
25051775Sbillm 	if (oldconfig)
25061775Sbillm 		*oldconfig = NULL;
25071775Sbillm 
2508789Sahrens 	if (!(spa_mode & FWRITE))
2509789Sahrens 		return (EROFS);
2510789Sahrens 
2511789Sahrens 	mutex_enter(&spa_namespace_lock);
2512789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2513789Sahrens 		mutex_exit(&spa_namespace_lock);
2514789Sahrens 		return (ENOENT);
2515789Sahrens 	}
2516789Sahrens 
2517789Sahrens 	/*
25181544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
25191544Seschrock 	 * reacquire the namespace lock, and see if we can export.
25201544Seschrock 	 */
25211544Seschrock 	spa_open_ref(spa, FTAG);
25221544Seschrock 	mutex_exit(&spa_namespace_lock);
25231544Seschrock 	spa_async_suspend(spa);
25241544Seschrock 	mutex_enter(&spa_namespace_lock);
25251544Seschrock 	spa_close(spa, FTAG);
25261544Seschrock 
25271544Seschrock 	/*
2528789Sahrens 	 * The pool will be in core if it's openable,
2529789Sahrens 	 * in which case we can modify its state.
2530789Sahrens 	 */
2531789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2532789Sahrens 		/*
2533789Sahrens 		 * Objsets may be open only because they're dirty, so we
2534789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2535789Sahrens 		 */
2536789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2537789Sahrens 
25381544Seschrock 		/*
25391544Seschrock 		 * A pool cannot be exported or destroyed if there are active
25401544Seschrock 		 * references.  If we are resetting a pool, allow references by
25411544Seschrock 		 * fault injection handlers.
25421544Seschrock 		 */
25431544Seschrock 		if (!spa_refcount_zero(spa) ||
25441544Seschrock 		    (spa->spa_inject_ref != 0 &&
25451544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
25461544Seschrock 			spa_async_resume(spa);
2547789Sahrens 			mutex_exit(&spa_namespace_lock);
2548789Sahrens 			return (EBUSY);
2549789Sahrens 		}
2550789Sahrens 
2551789Sahrens 		/*
2552789Sahrens 		 * We want this to be reflected on every label,
2553789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2554789Sahrens 		 * final sync that pushes these changes out.
2555789Sahrens 		 */
25561544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
25571601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
25581544Seschrock 			spa->spa_state = new_state;
25591635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
25601544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
25611601Sbonwick 			spa_config_exit(spa, FTAG);
25621544Seschrock 		}
2563789Sahrens 	}
2564789Sahrens 
25654451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
25664451Seschrock 
2567789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2568789Sahrens 		spa_unload(spa);
2569789Sahrens 		spa_deactivate(spa);
2570789Sahrens 	}
2571789Sahrens 
25721775Sbillm 	if (oldconfig && spa->spa_config)
25731775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
25741775Sbillm 
25751544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
25766643Seschrock 		spa_config_sync(spa, B_TRUE, B_TRUE);
25771544Seschrock 		spa_remove(spa);
25781544Seschrock 	}
2579789Sahrens 	mutex_exit(&spa_namespace_lock);
2580789Sahrens 
2581789Sahrens 	return (0);
2582789Sahrens }
2583789Sahrens 
2584789Sahrens /*
2585789Sahrens  * Destroy a storage pool.
2586789Sahrens  */
2587789Sahrens int
2588789Sahrens spa_destroy(char *pool)
2589789Sahrens {
25901775Sbillm 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL));
2591789Sahrens }
2592789Sahrens 
2593789Sahrens /*
2594789Sahrens  * Export a storage pool.
2595789Sahrens  */
2596789Sahrens int
25971775Sbillm spa_export(char *pool, nvlist_t **oldconfig)
2598789Sahrens {
25991775Sbillm 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig));
2600789Sahrens }
2601789Sahrens 
2602789Sahrens /*
26031544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
26041544Seschrock  * from the namespace in any way.
26051544Seschrock  */
26061544Seschrock int
26071544Seschrock spa_reset(char *pool)
26081544Seschrock {
26091775Sbillm 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL));
26101544Seschrock }
26111544Seschrock 
26121544Seschrock /*
2613789Sahrens  * ==========================================================================
2614789Sahrens  * Device manipulation
2615789Sahrens  * ==========================================================================
2616789Sahrens  */
2617789Sahrens 
2618789Sahrens /*
26194527Sperrin  * Add a device to a storage pool.
2620789Sahrens  */
2621789Sahrens int
2622789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2623789Sahrens {
2624789Sahrens 	uint64_t txg;
26251635Sbonwick 	int c, error;
2626789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
26271585Sbonwick 	vdev_t *vd, *tvd;
26285450Sbrendan 	nvlist_t **spares, **l2cache;
26295450Sbrendan 	uint_t nspares, nl2cache;
2630789Sahrens 
2631789Sahrens 	txg = spa_vdev_enter(spa);
2632789Sahrens 
26332082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
26342082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
26352082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
26362082Seschrock 
26373377Seschrock 	spa->spa_pending_vdev = vd;
2638789Sahrens 
26395450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
26405450Sbrendan 	    &nspares) != 0)
26412082Seschrock 		nspares = 0;
26422082Seschrock 
26435450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
26445450Sbrendan 	    &nl2cache) != 0)
26455450Sbrendan 		nl2cache = 0;
26465450Sbrendan 
26475450Sbrendan 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) {
26483377Seschrock 		spa->spa_pending_vdev = NULL;
26492082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
26503377Seschrock 	}
26512082Seschrock 
26522082Seschrock 	if (vd->vdev_children != 0) {
26533377Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
26543377Seschrock 			spa->spa_pending_vdev = NULL;
26552082Seschrock 			return (spa_vdev_exit(spa, vd, txg, error));
26562082Seschrock 		}
26572082Seschrock 	}
26582082Seschrock 
26593377Seschrock 	/*
26605450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
26615450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
26623377Seschrock 	 */
26635450Sbrendan 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) {
26643377Seschrock 		spa->spa_pending_vdev = NULL;
26653377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
26663377Seschrock 	}
26673377Seschrock 
26683377Seschrock 	spa->spa_pending_vdev = NULL;
26693377Seschrock 
26703377Seschrock 	/*
26713377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
26723377Seschrock 	 */
26733377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
26743377Seschrock 		tvd = vd->vdev_child[c];
26753377Seschrock 		vdev_remove_child(vd, tvd);
26763377Seschrock 		tvd->vdev_id = rvd->vdev_children;
26773377Seschrock 		vdev_add_child(rvd, tvd);
26783377Seschrock 		vdev_config_dirty(tvd);
26793377Seschrock 	}
26803377Seschrock 
26812082Seschrock 	if (nspares != 0) {
26825450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
26835450Sbrendan 		    ZPOOL_CONFIG_SPARES);
26842082Seschrock 		spa_load_spares(spa);
26855450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
26865450Sbrendan 	}
26875450Sbrendan 
26885450Sbrendan 	if (nl2cache != 0) {
26895450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
26905450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
26915450Sbrendan 		spa_load_l2cache(spa);
26925450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2693789Sahrens 	}
2694789Sahrens 
2695789Sahrens 	/*
26961585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
26971585Sbonwick 	 * If other threads start allocating from these vdevs before we
26981585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
26991585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
27001585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
27011585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
27021635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
27031585Sbonwick 	 *
27041585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
27051585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
27061585Sbonwick 	 * steps will be completed the next time we load the pool.
2707789Sahrens 	 */
27081635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
27091585Sbonwick 
27101635Sbonwick 	mutex_enter(&spa_namespace_lock);
27111635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
27121635Sbonwick 	mutex_exit(&spa_namespace_lock);
2713789Sahrens 
27141635Sbonwick 	return (0);
2715789Sahrens }
2716789Sahrens 
2717789Sahrens /*
2718789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2719789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2720789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2721789Sahrens  *
2722789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2723789Sahrens  * existing device; in this case the two devices are made into their own
27244451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2725789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2726789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2727789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2728789Sahrens  * is automatically detached.
2729789Sahrens  */
2730789Sahrens int
27311544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2732789Sahrens {
2733789Sahrens 	uint64_t txg, open_txg;
2734789Sahrens 	int error;
2735789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2736789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
27372082Seschrock 	vdev_ops_t *pvops;
27384527Sperrin 	int is_log;
2739789Sahrens 
2740789Sahrens 	txg = spa_vdev_enter(spa);
2741789Sahrens 
27426643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2743789Sahrens 
2744789Sahrens 	if (oldvd == NULL)
2745789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2746789Sahrens 
27471585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
27481585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
27491585Sbonwick 
2750789Sahrens 	pvd = oldvd->vdev_parent;
2751789Sahrens 
27522082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
27534451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
27544451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
27554451Seschrock 
27564451Seschrock 	if (newrootvd->vdev_children != 1)
2757789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2758789Sahrens 
2759789Sahrens 	newvd = newrootvd->vdev_child[0];
2760789Sahrens 
2761789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2762789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2763789Sahrens 
27642082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2765789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2766789Sahrens 
27674527Sperrin 	/*
27684527Sperrin 	 * Spares can't replace logs
27694527Sperrin 	 */
27704527Sperrin 	is_log = oldvd->vdev_islog;
27714527Sperrin 	if (is_log && newvd->vdev_isspare)
27724527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27734527Sperrin 
27742082Seschrock 	if (!replacing) {
27752082Seschrock 		/*
27762082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
27772082Seschrock 		 * vdev.
27782082Seschrock 		 */
27792082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
27802082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
27812082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27822082Seschrock 
27832082Seschrock 		pvops = &vdev_mirror_ops;
27842082Seschrock 	} else {
27852082Seschrock 		/*
27862082Seschrock 		 * Active hot spares can only be replaced by inactive hot
27872082Seschrock 		 * spares.
27882082Seschrock 		 */
27892082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
27902082Seschrock 		    pvd->vdev_child[1] == oldvd &&
27912082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
27922082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27932082Seschrock 
27942082Seschrock 		/*
27952082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
27962082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
27973377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
27983377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
27993377Seschrock 		 * the same (spare replaces spare, non-spare replaces
28003377Seschrock 		 * non-spare).
28012082Seschrock 		 */
28022082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
28032082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28043377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
28053377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
28063377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28072082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
28082082Seschrock 		    newvd->vdev_isspare)
28092082Seschrock 			pvops = &vdev_spare_ops;
28102082Seschrock 		else
28112082Seschrock 			pvops = &vdev_replacing_ops;
28122082Seschrock 	}
28132082Seschrock 
28141175Slling 	/*
28151175Slling 	 * Compare the new device size with the replaceable/attachable
28161175Slling 	 * device size.
28171175Slling 	 */
28181175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2819789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2820789Sahrens 
28211732Sbonwick 	/*
28221732Sbonwick 	 * The new device cannot have a higher alignment requirement
28231732Sbonwick 	 * than the top-level vdev.
28241732Sbonwick 	 */
28251732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2826789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2827789Sahrens 
2828789Sahrens 	/*
2829789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2830789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2831789Sahrens 	 */
2832789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2833789Sahrens 		spa_strfree(oldvd->vdev_path);
2834789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2835789Sahrens 		    KM_SLEEP);
2836789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2837789Sahrens 		    newvd->vdev_path, "old");
2838789Sahrens 		if (oldvd->vdev_devid != NULL) {
2839789Sahrens 			spa_strfree(oldvd->vdev_devid);
2840789Sahrens 			oldvd->vdev_devid = NULL;
2841789Sahrens 		}
2842789Sahrens 	}
2843789Sahrens 
2844789Sahrens 	/*
28452082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
28462082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2847789Sahrens 	 */
2848789Sahrens 	if (pvd->vdev_ops != pvops)
2849789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2850789Sahrens 
2851789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2852789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2853789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2854789Sahrens 
2855789Sahrens 	/*
2856789Sahrens 	 * Extract the new device from its root and add it to pvd.
2857789Sahrens 	 */
2858789Sahrens 	vdev_remove_child(newrootvd, newvd);
2859789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2860789Sahrens 	vdev_add_child(pvd, newvd);
2861789Sahrens 
28621544Seschrock 	/*
28631544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
28641544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
28651544Seschrock 	 */
28661544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
28671544Seschrock 
2868789Sahrens 	tvd = newvd->vdev_top;
2869789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2870789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2871789Sahrens 
2872789Sahrens 	vdev_config_dirty(tvd);
2873789Sahrens 
2874789Sahrens 	/*
2875789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2876789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2877789Sahrens 	 */
2878789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2879789Sahrens 
2880789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2881789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2882789Sahrens 	    open_txg - TXG_INITIAL + 1);
2883789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2884789Sahrens 
28853377Seschrock 	if (newvd->vdev_isspare)
28863377Seschrock 		spa_spare_activate(newvd);
28871544Seschrock 
2888789Sahrens 	/*
2889789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2890789Sahrens 	 */
28911732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2892789Sahrens 
2893789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2894789Sahrens 
2895789Sahrens 	/*
28967046Sahrens 	 * Kick off a resilver to update newvd.
2897789Sahrens 	 */
28987046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
2899789Sahrens 
2900789Sahrens 	return (0);
2901789Sahrens }
2902789Sahrens 
2903789Sahrens /*
2904789Sahrens  * Detach a device from a mirror or replacing vdev.
2905789Sahrens  * If 'replace_done' is specified, only detach if the parent
2906789Sahrens  * is a replacing vdev.
2907789Sahrens  */
2908789Sahrens int
29091544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2910789Sahrens {
2911789Sahrens 	uint64_t txg;
2912789Sahrens 	int c, t, error;
2913789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2914789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
29152082Seschrock 	boolean_t unspare = B_FALSE;
29162082Seschrock 	uint64_t unspare_guid;
29176673Seschrock 	size_t len;
2918789Sahrens 
2919789Sahrens 	txg = spa_vdev_enter(spa);
2920789Sahrens 
29216643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
2922789Sahrens 
2923789Sahrens 	if (vd == NULL)
2924789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2925789Sahrens 
29261585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
29271585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29281585Sbonwick 
2929789Sahrens 	pvd = vd->vdev_parent;
2930789Sahrens 
2931789Sahrens 	/*
2932789Sahrens 	 * If replace_done is specified, only remove this device if it's
29332082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
29342082Seschrock 	 * disk can be removed.
2935789Sahrens 	 */
29362082Seschrock 	if (replace_done) {
29372082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
29382082Seschrock 			if (vd->vdev_id != 0)
29392082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29402082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
29412082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29422082Seschrock 		}
29432082Seschrock 	}
29442082Seschrock 
29452082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
29464577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
2947789Sahrens 
2948789Sahrens 	/*
29492082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
2950789Sahrens 	 */
2951789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
29522082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
29532082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
2954789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2955789Sahrens 
2956789Sahrens 	/*
2957789Sahrens 	 * If there's only one replica, you can't detach it.
2958789Sahrens 	 */
2959789Sahrens 	if (pvd->vdev_children <= 1)
2960789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2961789Sahrens 
2962789Sahrens 	/*
2963789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
2964789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
2965789Sahrens 	 *
2966789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
2967789Sahrens 	 * precise DTL check.
2968789Sahrens 	 */
2969789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
2970789Sahrens 		uint64_t dirty;
2971789Sahrens 
2972789Sahrens 		cvd = pvd->vdev_child[c];
2973789Sahrens 		if (cvd == vd)
2974789Sahrens 			continue;
2975789Sahrens 		if (vdev_is_dead(cvd))
2976789Sahrens 			continue;
2977789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
2978789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
2979789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
2980789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
2981789Sahrens 		if (!dirty)
2982789Sahrens 			break;
2983789Sahrens 	}
29842082Seschrock 
29852082Seschrock 	/*
29862082Seschrock 	 * If we are a replacing or spare vdev, then we can always detach the
29872082Seschrock 	 * latter child, as that is how one cancels the operation.
29882082Seschrock 	 */
29892082Seschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
29902082Seschrock 	    c == pvd->vdev_children)
2991789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2992789Sahrens 
2993789Sahrens 	/*
29946673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
29956673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
29966673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
29976673Seschrock 	 */
29986673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
29996673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
30006673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
30016673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
30026673Seschrock 		cvd = pvd->vdev_child[0];
30036673Seschrock 		len = strlen(vd->vdev_path);
30046673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
30056673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
30066673Seschrock 			spa_strfree(cvd->vdev_path);
30076673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
30086673Seschrock 		}
30096673Seschrock 	}
30106673Seschrock 
30116673Seschrock 	/*
30122082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
30132082Seschrock 	 * that the spare should become a real disk, and be removed from the
30142082Seschrock 	 * active spare list for the pool.
30152082Seschrock 	 */
30162082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
30172082Seschrock 	    vd->vdev_id == 0)
30182082Seschrock 		unspare = B_TRUE;
30192082Seschrock 
30202082Seschrock 	/*
3021789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3022789Sahrens 	 * This must be done after all other error cases are handled,
3023789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3024789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3025789Sahrens 	 * it may be that the unwritability of the disk is the reason
3026789Sahrens 	 * it's being detached!
3027789Sahrens 	 */
30283377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3029789Sahrens 
3030789Sahrens 	/*
3031789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3032789Sahrens 	 */
3033789Sahrens 	vdev_remove_child(pvd, vd);
3034789Sahrens 	vdev_compact_children(pvd);
3035789Sahrens 
3036789Sahrens 	/*
3037789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3038789Sahrens 	 */
3039789Sahrens 	cvd = pvd->vdev_child[0];
3040789Sahrens 
3041789Sahrens 	/*
30422082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
30432082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
30442082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
30452082Seschrock 	 * GUID if it creates a new toplevel GUID.
30462082Seschrock 	 */
30472082Seschrock 	if (unspare) {
30482082Seschrock 		ASSERT(cvd->vdev_isspare);
30493377Seschrock 		spa_spare_remove(cvd);
30502082Seschrock 		unspare_guid = cvd->vdev_guid;
30512082Seschrock 	}
30522082Seschrock 
30532082Seschrock 	/*
3054789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3055789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3056789Sahrens 	 */
3057789Sahrens 	if (pvd->vdev_children == 1)
3058789Sahrens 		vdev_remove_parent(cvd);
3059789Sahrens 
3060789Sahrens 	/*
3061789Sahrens 	 * We don't set tvd until now because the parent we just removed
3062789Sahrens 	 * may have been the previous top-level vdev.
3063789Sahrens 	 */
3064789Sahrens 	tvd = cvd->vdev_top;
3065789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3066789Sahrens 
3067789Sahrens 	/*
30683377Seschrock 	 * Reevaluate the parent vdev state.
3069789Sahrens 	 */
30704451Seschrock 	vdev_propagate_state(cvd);
3071789Sahrens 
3072789Sahrens 	/*
30733377Seschrock 	 * If the device we just detached was smaller than the others, it may be
30743377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
30753377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
30763377Seschrock 	 * there's nothing to read from disk.
3077789Sahrens 	 */
30781732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3079789Sahrens 
3080789Sahrens 	vdev_config_dirty(tvd);
3081789Sahrens 
3082789Sahrens 	/*
30833377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
30843377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
30853377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
30863377Seschrock 	 * prevent vd from being accessed after it's freed.
3087789Sahrens 	 */
3088789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
3089789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
30901732Sbonwick 	vd->vdev_detached = B_TRUE;
30911732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3092789Sahrens 
30934451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
30944451Seschrock 
30952082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
30962082Seschrock 
30972082Seschrock 	/*
30983377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
30993377Seschrock 	 * then we want to go through and remove the device from the hot spare
31003377Seschrock 	 * list of every other pool.
31012082Seschrock 	 */
31022082Seschrock 	if (unspare) {
31032082Seschrock 		spa = NULL;
31042082Seschrock 		mutex_enter(&spa_namespace_lock);
31052082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
31062082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
31072082Seschrock 				continue;
31082082Seschrock 
31092082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
31102082Seschrock 		}
31112082Seschrock 		mutex_exit(&spa_namespace_lock);
31122082Seschrock 	}
31132082Seschrock 
31142082Seschrock 	return (error);
31152082Seschrock }
31162082Seschrock 
31172082Seschrock /*
31185450Sbrendan  * Remove a spares vdev from the nvlist config.
31192082Seschrock  */
31205450Sbrendan static int
31215450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare,
31225450Sbrendan     nvlist_t **spares, int nspares, vdev_t *vd)
31232082Seschrock {
31245450Sbrendan 	nvlist_t *nv, **newspares;
31255450Sbrendan 	int i, j;
31262082Seschrock 
31272082Seschrock 	nv = NULL;
31285450Sbrendan 	for (i = 0; i < nspares; i++) {
31295450Sbrendan 		uint64_t theguid;
31305450Sbrendan 
31315450Sbrendan 		VERIFY(nvlist_lookup_uint64(spares[i],
31325450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
31335450Sbrendan 		if (theguid == guid) {
31345450Sbrendan 			nv = spares[i];
31355450Sbrendan 			break;
31362082Seschrock 		}
31372082Seschrock 	}
31382082Seschrock 
31392082Seschrock 	/*
31405450Sbrendan 	 * Only remove the hot spare if it's not currently in use in this pool.
31412082Seschrock 	 */
31425450Sbrendan 	if (nv == NULL && vd == NULL)
31435450Sbrendan 		return (ENOENT);
31445450Sbrendan 
31455450Sbrendan 	if (nv == NULL && vd != NULL)
31465450Sbrendan 		return (ENOTSUP);
31475450Sbrendan 
31485450Sbrendan 	if (!unspare && nv != NULL && vd != NULL)
31495450Sbrendan 		return (EBUSY);
31502082Seschrock 
31512082Seschrock 	if (nspares == 1) {
31522082Seschrock 		newspares = NULL;
31532082Seschrock 	} else {
31542082Seschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
31552082Seschrock 		    KM_SLEEP);
31562082Seschrock 		for (i = 0, j = 0; i < nspares; i++) {
31572082Seschrock 			if (spares[i] != nv)
31582082Seschrock 				VERIFY(nvlist_dup(spares[i],
31592082Seschrock 				    &newspares[j++], KM_SLEEP) == 0);
31602082Seschrock 		}
31612082Seschrock 	}
31622082Seschrock 
31635450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES,
31642082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
31655450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
31665450Sbrendan 	    ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0);
31672082Seschrock 	for (i = 0; i < nspares - 1; i++)
31682082Seschrock 		nvlist_free(newspares[i]);
31692082Seschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
31705450Sbrendan 
31715450Sbrendan 	return (0);
31725450Sbrendan }
31735450Sbrendan 
31745450Sbrendan /*
31755450Sbrendan  * Remove an l2cache vdev from the nvlist config.
31765450Sbrendan  */
31775450Sbrendan static int
31785450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache,
31795450Sbrendan     int nl2cache, vdev_t *vd)
31805450Sbrendan {
31815450Sbrendan 	nvlist_t *nv, **newl2cache;
31825450Sbrendan 	int i, j;
31835450Sbrendan 
31845450Sbrendan 	nv = NULL;
31855450Sbrendan 	for (i = 0; i < nl2cache; i++) {
31865450Sbrendan 		uint64_t theguid;
31875450Sbrendan 
31885450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i],
31895450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
31905450Sbrendan 		if (theguid == guid) {
31915450Sbrendan 			nv = l2cache[i];
31925450Sbrendan 			break;
31935450Sbrendan 		}
31945450Sbrendan 	}
31955450Sbrendan 
31965450Sbrendan 	if (vd == NULL) {
31975450Sbrendan 		for (i = 0; i < nl2cache; i++) {
31985450Sbrendan 			if (sav->sav_vdevs[i]->vdev_guid == guid) {
31995450Sbrendan 				vd = sav->sav_vdevs[i];
32005450Sbrendan 				break;
32015450Sbrendan 			}
32025450Sbrendan 		}
32035450Sbrendan 	}
32045450Sbrendan 
32055450Sbrendan 	if (nv == NULL && vd == NULL)
32065450Sbrendan 		return (ENOENT);
32075450Sbrendan 
32085450Sbrendan 	if (nv == NULL && vd != NULL)
32095450Sbrendan 		return (ENOTSUP);
32105450Sbrendan 
32115450Sbrendan 	if (nl2cache == 1) {
32125450Sbrendan 		newl2cache = NULL;
32135450Sbrendan 	} else {
32145450Sbrendan 		newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *),
32155450Sbrendan 		    KM_SLEEP);
32165450Sbrendan 		for (i = 0, j = 0; i < nl2cache; i++) {
32175450Sbrendan 			if (l2cache[i] != nv)
32185450Sbrendan 				VERIFY(nvlist_dup(l2cache[i],
32195450Sbrendan 				    &newl2cache[j++], KM_SLEEP) == 0);
32205450Sbrendan 		}
32215450Sbrendan 	}
32225450Sbrendan 
32235450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
32245450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
32255450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
32265450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0);
32275450Sbrendan 	for (i = 0; i < nl2cache - 1; i++)
32285450Sbrendan 		nvlist_free(newl2cache[i]);
32295450Sbrendan 	kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *));
32305450Sbrendan 
32315450Sbrendan 	return (0);
32325450Sbrendan }
32335450Sbrendan 
32345450Sbrendan /*
32355450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
32365450Sbrendan  * spares and level 2 ARC devices.
32375450Sbrendan  */
32385450Sbrendan int
32395450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
32405450Sbrendan {
32415450Sbrendan 	vdev_t *vd;
32425450Sbrendan 	nvlist_t **spares, **l2cache;
32435450Sbrendan 	uint_t nspares, nl2cache;
32445450Sbrendan 	int error = 0;
32455450Sbrendan 
32465450Sbrendan 	spa_config_enter(spa, RW_WRITER, FTAG);
32475450Sbrendan 
32486643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
32495450Sbrendan 
32505450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
32515450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
32525450Sbrendan 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
32535450Sbrendan 		if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare,
32545450Sbrendan 		    spares, nspares, vd)) != 0)
32555450Sbrendan 			goto out;
32565450Sbrendan 		spa_load_spares(spa);
32575450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
32585450Sbrendan 		goto out;
32595450Sbrendan 	}
32605450Sbrendan 
32615450Sbrendan 	if (spa->spa_l2cache.sav_vdevs != NULL &&
32625450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
32635450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) {
32645450Sbrendan 		if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid,
32655450Sbrendan 		    l2cache, nl2cache, vd)) != 0)
32665450Sbrendan 			goto out;
32675450Sbrendan 		spa_load_l2cache(spa);
32685450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
32695450Sbrendan 	}
32702082Seschrock 
32712082Seschrock out:
32722082Seschrock 	spa_config_exit(spa, FTAG);
32735450Sbrendan 	return (error);
3274789Sahrens }
3275789Sahrens 
3276789Sahrens /*
32774451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
32784451Seschrock  * current spared, so we can detach it.
3279789Sahrens  */
32801544Seschrock static vdev_t *
32814451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3282789Sahrens {
32831544Seschrock 	vdev_t *newvd, *oldvd;
3284789Sahrens 	int c;
3285789Sahrens 
32861544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
32874451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
32881544Seschrock 		if (oldvd != NULL)
32891544Seschrock 			return (oldvd);
32901544Seschrock 	}
3291789Sahrens 
32924451Seschrock 	/*
32934451Seschrock 	 * Check for a completed replacement.
32944451Seschrock 	 */
3295789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
32961544Seschrock 		oldvd = vd->vdev_child[0];
32971544Seschrock 		newvd = vd->vdev_child[1];
3298789Sahrens 
32991544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33001544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
33011544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33021544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33031544Seschrock 			return (oldvd);
33041544Seschrock 		}
33051544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33061544Seschrock 	}
3307789Sahrens 
33084451Seschrock 	/*
33094451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
33104451Seschrock 	 */
33114451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
33124451Seschrock 		newvd = vd->vdev_child[0];
33134451Seschrock 		oldvd = vd->vdev_child[1];
33144451Seschrock 
33154451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33164451Seschrock 		if (newvd->vdev_unspare &&
33174451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
33184451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33194451Seschrock 			newvd->vdev_unspare = 0;
33204451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33214451Seschrock 			return (oldvd);
33224451Seschrock 		}
33234451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33244451Seschrock 	}
33254451Seschrock 
33261544Seschrock 	return (NULL);
3327789Sahrens }
3328789Sahrens 
33291544Seschrock static void
33304451Seschrock spa_vdev_resilver_done(spa_t *spa)
3331789Sahrens {
33321544Seschrock 	vdev_t *vd;
33332082Seschrock 	vdev_t *pvd;
33341544Seschrock 	uint64_t guid;
33352082Seschrock 	uint64_t pguid = 0;
3336789Sahrens 
33371544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3338789Sahrens 
33394451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
33401544Seschrock 		guid = vd->vdev_guid;
33412082Seschrock 		/*
33422082Seschrock 		 * If we have just finished replacing a hot spared device, then
33432082Seschrock 		 * we need to detach the parent's first child (the original hot
33442082Seschrock 		 * spare) as well.
33452082Seschrock 		 */
33462082Seschrock 		pvd = vd->vdev_parent;
33472082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
33482082Seschrock 		    pvd->vdev_id == 0) {
33492082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
33502082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
33512082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
33522082Seschrock 		}
33531544Seschrock 		spa_config_exit(spa, FTAG);
33541544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
33551544Seschrock 			return;
33562082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
33572082Seschrock 			return;
33581544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
3359789Sahrens 	}
3360789Sahrens 
33611544Seschrock 	spa_config_exit(spa, FTAG);
3362789Sahrens }
3363789Sahrens 
3364789Sahrens /*
33651354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
33661354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
33671354Seschrock  */
33681354Seschrock int
33691354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
33701354Seschrock {
33716643Seschrock 	vdev_t *vd;
33721354Seschrock 	uint64_t txg;
33731354Seschrock 
33741354Seschrock 	txg = spa_vdev_enter(spa);
33751354Seschrock 
33766643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
33772082Seschrock 		/*
33786643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
33796643Seschrock 		 * it is, update the path manually as there is no associated
33806643Seschrock 		 * vdev_t that can be synced to disk.
33812082Seschrock 		 */
33826643Seschrock 		nvlist_t **spares;
33836643Seschrock 		uint_t i, nspares;
33845450Sbrendan 
33855450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
33865450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
33875450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
33885450Sbrendan 			    &spares, &nspares) == 0);
33892082Seschrock 			for (i = 0; i < nspares; i++) {
33902082Seschrock 				uint64_t theguid;
33912082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
33922082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
33935450Sbrendan 				if (theguid == guid) {
33945450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
33955450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
33965450Sbrendan 					spa_load_spares(spa);
33975450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
33985450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
33995450Sbrendan 					    0));
34005450Sbrendan 				}
34012082Seschrock 			}
34022082Seschrock 		}
34035450Sbrendan 
34045450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
34052082Seschrock 	}
34061354Seschrock 
34071585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
34081585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34091585Sbonwick 
34101354Seschrock 	spa_strfree(vd->vdev_path);
34111354Seschrock 	vd->vdev_path = spa_strdup(newpath);
34121354Seschrock 
34131354Seschrock 	vdev_config_dirty(vd->vdev_top);
34141354Seschrock 
34151354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
34161354Seschrock }
34171354Seschrock 
34181354Seschrock /*
3419789Sahrens  * ==========================================================================
3420789Sahrens  * SPA Scrubbing
3421789Sahrens  * ==========================================================================
3422789Sahrens  */
3423789Sahrens 
34247046Sahrens int
34257046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3426789Sahrens {
34274808Sek110237 	ASSERT(!spa_config_held(spa, RW_WRITER));
34284808Sek110237 
3429789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3430789Sahrens 		return (ENOTSUP);
3431789Sahrens 
3432789Sahrens 	/*
34337046Sahrens 	 * If a resilver was requested, but there is no DTL on a
34347046Sahrens 	 * writeable leaf device, we have nothing to do.
3435789Sahrens 	 */
34367046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
34377046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
34387046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
34391544Seschrock 		return (0);
34401544Seschrock 	}
3441789Sahrens 
34427046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
34437046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
34447046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
34457046Sahrens 		return (EBUSY);
34467046Sahrens 
34477046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
34487046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
34497046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
34507046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
34511544Seschrock 	} else {
34527046Sahrens 		return (EINVAL);
34531544Seschrock 	}
3454789Sahrens }
3455789Sahrens 
34561544Seschrock /*
34571544Seschrock  * ==========================================================================
34581544Seschrock  * SPA async task processing
34591544Seschrock  * ==========================================================================
34601544Seschrock  */
34611544Seschrock 
34621544Seschrock static void
34634451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3464789Sahrens {
34651544Seschrock 	vdev_t *tvd;
34661544Seschrock 	int c;
34671544Seschrock 
34684451Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
34694451Seschrock 		tvd = vd->vdev_child[c];
34704451Seschrock 		if (tvd->vdev_remove_wanted) {
34714451Seschrock 			tvd->vdev_remove_wanted = 0;
34724451Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
34734451Seschrock 			    VDEV_AUX_NONE);
34745329Sgw25295 			vdev_clear(spa, tvd, B_TRUE);
34754451Seschrock 			vdev_config_dirty(tvd->vdev_top);
34761544Seschrock 		}
34774451Seschrock 		spa_async_remove(spa, tvd);
34781544Seschrock 	}
34791544Seschrock }
34801544Seschrock 
34811544Seschrock static void
34821544Seschrock spa_async_thread(spa_t *spa)
34831544Seschrock {
34841544Seschrock 	int tasks;
34854451Seschrock 	uint64_t txg;
34861544Seschrock 
34871544Seschrock 	ASSERT(spa->spa_sync_on);
3488789Sahrens 
34891544Seschrock 	mutex_enter(&spa->spa_async_lock);
34901544Seschrock 	tasks = spa->spa_async_tasks;
34911544Seschrock 	spa->spa_async_tasks = 0;
34921544Seschrock 	mutex_exit(&spa->spa_async_lock);
34931544Seschrock 
34941544Seschrock 	/*
34951635Sbonwick 	 * See if the config needs to be updated.
34961635Sbonwick 	 */
34971635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
34981635Sbonwick 		mutex_enter(&spa_namespace_lock);
34991635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
35001635Sbonwick 		mutex_exit(&spa_namespace_lock);
35011635Sbonwick 	}
35021635Sbonwick 
35031635Sbonwick 	/*
35044451Seschrock 	 * See if any devices need to be marked REMOVED.
35055329Sgw25295 	 *
35065329Sgw25295 	 * XXX - We avoid doing this when we are in
35075329Sgw25295 	 * I/O failure state since spa_vdev_enter() grabs
35085329Sgw25295 	 * the namespace lock and would not be able to obtain
35095329Sgw25295 	 * the writer config lock.
35101544Seschrock 	 */
35115329Sgw25295 	if (tasks & SPA_ASYNC_REMOVE &&
35125329Sgw25295 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
35134451Seschrock 		txg = spa_vdev_enter(spa);
35144451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
35154451Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
35164451Seschrock 	}
35171544Seschrock 
35181544Seschrock 	/*
35191544Seschrock 	 * If any devices are done replacing, detach them.
35201544Seschrock 	 */
35214451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
35224451Seschrock 		spa_vdev_resilver_done(spa);
3523789Sahrens 
35241544Seschrock 	/*
35251544Seschrock 	 * Kick off a resilver.
35261544Seschrock 	 */
35277046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
35287046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
35291544Seschrock 
35301544Seschrock 	/*
35311544Seschrock 	 * Let the world know that we're done.
35321544Seschrock 	 */
35331544Seschrock 	mutex_enter(&spa->spa_async_lock);
35341544Seschrock 	spa->spa_async_thread = NULL;
35351544Seschrock 	cv_broadcast(&spa->spa_async_cv);
35361544Seschrock 	mutex_exit(&spa->spa_async_lock);
35371544Seschrock 	thread_exit();
35381544Seschrock }
35391544Seschrock 
35401544Seschrock void
35411544Seschrock spa_async_suspend(spa_t *spa)
35421544Seschrock {
35431544Seschrock 	mutex_enter(&spa->spa_async_lock);
35441544Seschrock 	spa->spa_async_suspended++;
35451544Seschrock 	while (spa->spa_async_thread != NULL)
35461544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
35471544Seschrock 	mutex_exit(&spa->spa_async_lock);
35481544Seschrock }
35491544Seschrock 
35501544Seschrock void
35511544Seschrock spa_async_resume(spa_t *spa)
35521544Seschrock {
35531544Seschrock 	mutex_enter(&spa->spa_async_lock);
35541544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
35551544Seschrock 	spa->spa_async_suspended--;
35561544Seschrock 	mutex_exit(&spa->spa_async_lock);
35571544Seschrock }
35581544Seschrock 
35591544Seschrock static void
35601544Seschrock spa_async_dispatch(spa_t *spa)
35611544Seschrock {
35621544Seschrock 	mutex_enter(&spa->spa_async_lock);
35631544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
35641635Sbonwick 	    spa->spa_async_thread == NULL &&
35651635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
35661544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
35671544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
35681544Seschrock 	mutex_exit(&spa->spa_async_lock);
35691544Seschrock }
35701544Seschrock 
35711544Seschrock void
35721544Seschrock spa_async_request(spa_t *spa, int task)
35731544Seschrock {
35741544Seschrock 	mutex_enter(&spa->spa_async_lock);
35751544Seschrock 	spa->spa_async_tasks |= task;
35761544Seschrock 	mutex_exit(&spa->spa_async_lock);
3577789Sahrens }
3578789Sahrens 
3579789Sahrens /*
3580789Sahrens  * ==========================================================================
3581789Sahrens  * SPA syncing routines
3582789Sahrens  * ==========================================================================
3583789Sahrens  */
3584789Sahrens 
3585789Sahrens static void
3586789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3587789Sahrens {
3588789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3589789Sahrens 	dmu_tx_t *tx;
3590789Sahrens 	blkptr_t blk;
3591789Sahrens 	uint64_t itor = 0;
3592789Sahrens 	zio_t *zio;
3593789Sahrens 	int error;
3594789Sahrens 	uint8_t c = 1;
3595789Sahrens 
3596789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3597789Sahrens 
3598789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3599789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3600789Sahrens 
3601789Sahrens 	error = zio_wait(zio);
3602789Sahrens 	ASSERT3U(error, ==, 0);
3603789Sahrens 
3604789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3605789Sahrens 	bplist_vacate(bpl, tx);
3606789Sahrens 
3607789Sahrens 	/*
3608789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3609789Sahrens 	 * (Usually only the first block is needed.)
3610789Sahrens 	 */
3611789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3612789Sahrens 	dmu_tx_commit(tx);
3613789Sahrens }
3614789Sahrens 
3615789Sahrens static void
36162082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
36172082Seschrock {
36182082Seschrock 	char *packed = NULL;
36192082Seschrock 	size_t nvsize = 0;
36202082Seschrock 	dmu_buf_t *db;
36212082Seschrock 
36222082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
36232082Seschrock 
36242082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
36252082Seschrock 
36262082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
36272082Seschrock 	    KM_SLEEP) == 0);
36282082Seschrock 
36292082Seschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
36302082Seschrock 
36312082Seschrock 	kmem_free(packed, nvsize);
36322082Seschrock 
36332082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
36342082Seschrock 	dmu_buf_will_dirty(db, tx);
36352082Seschrock 	*(uint64_t *)db->db_data = nvsize;
36362082Seschrock 	dmu_buf_rele(db, FTAG);
36372082Seschrock }
36382082Seschrock 
36392082Seschrock static void
36405450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
36415450Sbrendan     const char *config, const char *entry)
36422082Seschrock {
36432082Seschrock 	nvlist_t *nvroot;
36445450Sbrendan 	nvlist_t **list;
36452082Seschrock 	int i;
36462082Seschrock 
36475450Sbrendan 	if (!sav->sav_sync)
36482082Seschrock 		return;
36492082Seschrock 
36502082Seschrock 	/*
36515450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
36525450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
36534451Seschrock 	 * valid and the vdevs are labeled appropriately.
36542082Seschrock 	 */
36555450Sbrendan 	if (sav->sav_object == 0) {
36565450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
36575450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
36585450Sbrendan 		    sizeof (uint64_t), tx);
36592082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
36605450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
36615450Sbrendan 		    &sav->sav_object, tx) == 0);
36622082Seschrock 	}
36632082Seschrock 
36642082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
36655450Sbrendan 	if (sav->sav_count == 0) {
36665450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
36672082Seschrock 	} else {
36685450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
36695450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
36705450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
36715450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
36725450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
36735450Sbrendan 		    sav->sav_count) == 0);
36745450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
36755450Sbrendan 			nvlist_free(list[i]);
36765450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
36772082Seschrock 	}
36782082Seschrock 
36795450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
36802926Sek110237 	nvlist_free(nvroot);
36812082Seschrock 
36825450Sbrendan 	sav->sav_sync = B_FALSE;
36832082Seschrock }
36842082Seschrock 
36852082Seschrock static void
3686789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3687789Sahrens {
3688789Sahrens 	nvlist_t *config;
3689789Sahrens 
3690789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
3691789Sahrens 		return;
3692789Sahrens 
3693789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
3694789Sahrens 
36951635Sbonwick 	if (spa->spa_config_syncing)
36961635Sbonwick 		nvlist_free(spa->spa_config_syncing);
36971635Sbonwick 	spa->spa_config_syncing = config;
3698789Sahrens 
36992082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3700789Sahrens }
3701789Sahrens 
37025094Slling /*
37035094Slling  * Set zpool properties.
37045094Slling  */
37053912Slling static void
37064543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
37073912Slling {
37083912Slling 	spa_t *spa = arg1;
37095094Slling 	objset_t *mos = spa->spa_meta_objset;
37103912Slling 	nvlist_t *nvp = arg2;
37115094Slling 	nvpair_t *elem;
37124451Seschrock 	uint64_t intval;
37136643Seschrock 	char *strval;
37145094Slling 	zpool_prop_t prop;
37155094Slling 	const char *propname;
37165094Slling 	zprop_type_t proptype;
37176643Seschrock 	spa_config_dirent_t *dp;
37185094Slling 
37195094Slling 	elem = NULL;
37205094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
37215094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
37225094Slling 		case ZPOOL_PROP_VERSION:
37235094Slling 			/*
37245094Slling 			 * Only set version for non-zpool-creation cases
37255094Slling 			 * (set/import). spa_create() needs special care
37265094Slling 			 * for version setting.
37275094Slling 			 */
37285094Slling 			if (tx->tx_txg != TXG_INITIAL) {
37295094Slling 				VERIFY(nvpair_value_uint64(elem,
37305094Slling 				    &intval) == 0);
37315094Slling 				ASSERT(intval <= SPA_VERSION);
37325094Slling 				ASSERT(intval >= spa_version(spa));
37335094Slling 				spa->spa_uberblock.ub_version = intval;
37345094Slling 				vdev_config_dirty(spa->spa_root_vdev);
37355094Slling 			}
37365094Slling 			break;
37375094Slling 
37385094Slling 		case ZPOOL_PROP_ALTROOT:
37395094Slling 			/*
37405094Slling 			 * 'altroot' is a non-persistent property. It should
37415094Slling 			 * have been set temporarily at creation or import time.
37425094Slling 			 */
37435094Slling 			ASSERT(spa->spa_root != NULL);
37445094Slling 			break;
37455094Slling 
37465363Seschrock 		case ZPOOL_PROP_CACHEFILE:
37475094Slling 			/*
37485363Seschrock 			 * 'cachefile' is a non-persistent property, but note
37495363Seschrock 			 * an async request that the config cache needs to be
37505363Seschrock 			 * udpated.
37515094Slling 			 */
37525363Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
37536643Seschrock 
37546643Seschrock 			dp = kmem_alloc(sizeof (spa_config_dirent_t),
37556643Seschrock 			    KM_SLEEP);
37566643Seschrock 
37576643Seschrock 			if (strval[0] == '\0')
37586643Seschrock 				dp->scd_path = spa_strdup(spa_config_path);
37596643Seschrock 			else if (strcmp(strval, "none") == 0)
37606643Seschrock 				dp->scd_path = NULL;
37616643Seschrock 			else
37626643Seschrock 				dp->scd_path = spa_strdup(strval);
37636643Seschrock 
37646643Seschrock 			list_insert_head(&spa->spa_config_list, dp);
37655363Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
37664543Smarks 			break;
37675094Slling 		default:
37685094Slling 			/*
37695094Slling 			 * Set pool property values in the poolprops mos object.
37705094Slling 			 */
37715094Slling 			mutex_enter(&spa->spa_props_lock);
37725094Slling 			if (spa->spa_pool_props_object == 0) {
37735094Slling 				objset_t *mos = spa->spa_meta_objset;
37745094Slling 
37755094Slling 				VERIFY((spa->spa_pool_props_object =
37765094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
37775094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
37785094Slling 
37795094Slling 				VERIFY(zap_update(mos,
37805094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
37815094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
37825094Slling 				    == 0);
37835094Slling 			}
37845094Slling 			mutex_exit(&spa->spa_props_lock);
37855094Slling 
37865094Slling 			/* normalize the property name */
37875094Slling 			propname = zpool_prop_to_name(prop);
37885094Slling 			proptype = zpool_prop_get_type(prop);
37895094Slling 
37905094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
37915094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
37925094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
37935094Slling 				VERIFY(zap_update(mos,
37945094Slling 				    spa->spa_pool_props_object, propname,
37955094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
37965094Slling 
37975094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
37985094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
37995094Slling 
38005094Slling 				if (proptype == PROP_TYPE_INDEX) {
38015094Slling 					const char *unused;
38025094Slling 					VERIFY(zpool_prop_index_to_string(
38035094Slling 					    prop, intval, &unused) == 0);
38045094Slling 				}
38055094Slling 				VERIFY(zap_update(mos,
38065094Slling 				    spa->spa_pool_props_object, propname,
38075094Slling 				    8, 1, &intval, tx) == 0);
38085094Slling 			} else {
38095094Slling 				ASSERT(0); /* not allowed */
38105094Slling 			}
38115094Slling 
38125329Sgw25295 			switch (prop) {
38135329Sgw25295 			case ZPOOL_PROP_DELEGATION:
38145094Slling 				spa->spa_delegation = intval;
38155329Sgw25295 				break;
38165329Sgw25295 			case ZPOOL_PROP_BOOTFS:
38175094Slling 				spa->spa_bootfs = intval;
38185329Sgw25295 				break;
38195329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
38205329Sgw25295 				spa->spa_failmode = intval;
38215329Sgw25295 				break;
38225329Sgw25295 			default:
38235329Sgw25295 				break;
38245329Sgw25295 			}
38253912Slling 		}
38265094Slling 
38275094Slling 		/* log internal history if this is not a zpool create */
38285094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
38295094Slling 		    tx->tx_txg != TXG_INITIAL) {
38305094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
38315094Slling 			    spa, tx, cr, "%s %lld %s",
38325094Slling 			    nvpair_name(elem), intval, spa->spa_name);
38335094Slling 		}
38343912Slling 	}
38353912Slling }
38363912Slling 
3837789Sahrens /*
3838789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3839789Sahrens  * part of the process, so we iterate until it converges.
3840789Sahrens  */
3841789Sahrens void
3842789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3843789Sahrens {
3844789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3845789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3846789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
38471635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3848789Sahrens 	vdev_t *vd;
3849789Sahrens 	dmu_tx_t *tx;
3850789Sahrens 	int dirty_vdevs;
3851789Sahrens 
3852789Sahrens 	/*
3853789Sahrens 	 * Lock out configuration changes.
3854789Sahrens 	 */
38551544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3856789Sahrens 
3857789Sahrens 	spa->spa_syncing_txg = txg;
3858789Sahrens 	spa->spa_sync_pass = 0;
3859789Sahrens 
38601544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3861789Sahrens 
38622082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
38632082Seschrock 
38642082Seschrock 	/*
38654577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
38662082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
38672082Seschrock 	 */
38684577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
38694577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
38702082Seschrock 		int i;
38712082Seschrock 
38722082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
38732082Seschrock 			vd = rvd->vdev_child[i];
38742082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
38752082Seschrock 				break;
38762082Seschrock 		}
38772082Seschrock 		if (i == rvd->vdev_children) {
38782082Seschrock 			spa->spa_deflate = TRUE;
38792082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
38802082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
38812082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
38822082Seschrock 		}
38832082Seschrock 	}
38842082Seschrock 
38857046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
38867046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
38877046Sahrens 		dsl_pool_create_origin(dp, tx);
38887046Sahrens 
38897046Sahrens 		/* Keeping the origin open increases spa_minref */
38907046Sahrens 		spa->spa_minref += 3;
38917046Sahrens 	}
38927046Sahrens 
38937046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
38947046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
38957046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
38967046Sahrens 	}
38977046Sahrens 
3898789Sahrens 	/*
3899789Sahrens 	 * If anything has changed in this txg, push the deferred frees
3900789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
3901789Sahrens 	 * don't generate work on an otherwise idle system.
3902789Sahrens 	 */
3903789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
39042329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
39052329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
3906789Sahrens 		spa_sync_deferred_frees(spa, txg);
3907789Sahrens 
3908789Sahrens 	/*
3909789Sahrens 	 * Iterate to convergence.
3910789Sahrens 	 */
3911789Sahrens 	do {
3912789Sahrens 		spa->spa_sync_pass++;
3913789Sahrens 
3914789Sahrens 		spa_sync_config_object(spa, tx);
39155450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
39165450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
39175450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
39185450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
39191544Seschrock 		spa_errlog_sync(spa, txg);
3920789Sahrens 		dsl_pool_sync(dp, txg);
3921789Sahrens 
3922789Sahrens 		dirty_vdevs = 0;
3923789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
3924789Sahrens 			vdev_sync(vd, txg);
3925789Sahrens 			dirty_vdevs++;
3926789Sahrens 		}
3927789Sahrens 
3928789Sahrens 		bplist_sync(bpl, tx);
3929789Sahrens 	} while (dirty_vdevs);
3930789Sahrens 
3931789Sahrens 	bplist_close(bpl);
3932789Sahrens 
3933789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
3934789Sahrens 
3935789Sahrens 	/*
3936789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
3937789Sahrens 	 * to commit the transaction group.
39381635Sbonwick 	 *
39395688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
39405688Sbonwick 	 * random top-level vdevs that are known to be visible in the
39415688Sbonwick 	 * config cache (see spa_vdev_add() for details).  If there *are*
39425688Sbonwick 	 * dirty vdevs -- or if the sync to our random subset fails --
39435688Sbonwick 	 * then sync the uberblock to all vdevs.
3944789Sahrens 	 */
39455688Sbonwick 	if (list_is_empty(&spa->spa_dirty_list)) {
39466615Sgw25295 		vdev_t *svd[SPA_DVAS_PER_BP];
39476615Sgw25295 		int svdcount = 0;
39481635Sbonwick 		int children = rvd->vdev_children;
39491635Sbonwick 		int c0 = spa_get_random(children);
39501635Sbonwick 		int c;
39511635Sbonwick 
39521635Sbonwick 		for (c = 0; c < children; c++) {
39531635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
39545688Sbonwick 			if (vd->vdev_ms_array == 0 || vd->vdev_islog)
39551635Sbonwick 				continue;
39565688Sbonwick 			svd[svdcount++] = vd;
39575688Sbonwick 			if (svdcount == SPA_DVAS_PER_BP)
39581635Sbonwick 				break;
39591635Sbonwick 		}
39606615Sgw25295 		vdev_config_sync(svd, svdcount, txg);
39616615Sgw25295 	} else {
39626615Sgw25295 		vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg);
39631635Sbonwick 	}
39642082Seschrock 	dmu_tx_commit(tx);
39652082Seschrock 
39661635Sbonwick 	/*
39671635Sbonwick 	 * Clear the dirty config list.
39681635Sbonwick 	 */
39691635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
39701635Sbonwick 		vdev_config_clean(vd);
39711635Sbonwick 
39721635Sbonwick 	/*
39731635Sbonwick 	 * Now that the new config has synced transactionally,
39741635Sbonwick 	 * let it become visible to the config cache.
39751635Sbonwick 	 */
39761635Sbonwick 	if (spa->spa_config_syncing != NULL) {
39771635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
39781635Sbonwick 		spa->spa_config_txg = txg;
39791635Sbonwick 		spa->spa_config_syncing = NULL;
39801635Sbonwick 	}
3981789Sahrens 
39827046Sahrens 	spa->spa_traverse_wanted = B_TRUE;
3983789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
39847046Sahrens 	spa->spa_traverse_wanted = B_FALSE;
3985789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
3986789Sahrens 	rw_exit(&spa->spa_traverse_lock);
3987789Sahrens 
3988789Sahrens 	/*
3989789Sahrens 	 * Clean up the ZIL records for the synced txg.
3990789Sahrens 	 */
3991789Sahrens 	dsl_pool_zil_clean(dp);
3992789Sahrens 
3993789Sahrens 	/*
3994789Sahrens 	 * Update usable space statistics.
3995789Sahrens 	 */
3996789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
3997789Sahrens 		vdev_sync_done(vd, txg);
3998789Sahrens 
3999789Sahrens 	/*
4000789Sahrens 	 * It had better be the case that we didn't dirty anything
40012082Seschrock 	 * since vdev_config_sync().
4002789Sahrens 	 */
4003789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4004789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4005789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4006789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4007789Sahrens 
40081544Seschrock 	spa_config_exit(spa, FTAG);
40091544Seschrock 
40101544Seschrock 	/*
40111544Seschrock 	 * If any async tasks have been requested, kick them off.
40121544Seschrock 	 */
40131544Seschrock 	spa_async_dispatch(spa);
4014789Sahrens }
4015789Sahrens 
4016789Sahrens /*
4017789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4018789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4019789Sahrens  * sync.
4020789Sahrens  */
4021789Sahrens void
4022789Sahrens spa_sync_allpools(void)
4023789Sahrens {
4024789Sahrens 	spa_t *spa = NULL;
4025789Sahrens 	mutex_enter(&spa_namespace_lock);
4026789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4027789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
4028789Sahrens 			continue;
4029789Sahrens 		spa_open_ref(spa, FTAG);
4030789Sahrens 		mutex_exit(&spa_namespace_lock);
4031789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4032789Sahrens 		mutex_enter(&spa_namespace_lock);
4033789Sahrens 		spa_close(spa, FTAG);
4034789Sahrens 	}
4035789Sahrens 	mutex_exit(&spa_namespace_lock);
4036789Sahrens }
4037789Sahrens 
4038789Sahrens /*
4039789Sahrens  * ==========================================================================
4040789Sahrens  * Miscellaneous routines
4041789Sahrens  * ==========================================================================
4042789Sahrens  */
4043789Sahrens 
4044789Sahrens /*
4045789Sahrens  * Remove all pools in the system.
4046789Sahrens  */
4047789Sahrens void
4048789Sahrens spa_evict_all(void)
4049789Sahrens {
4050789Sahrens 	spa_t *spa;
4051789Sahrens 
4052789Sahrens 	/*
4053789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4054789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4055789Sahrens 	 */
4056789Sahrens 	mutex_enter(&spa_namespace_lock);
4057789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4058789Sahrens 		/*
40591544Seschrock 		 * Stop async tasks.  The async thread may need to detach
40601544Seschrock 		 * a device that's been replaced, which requires grabbing
40611544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4062789Sahrens 		 */
4063789Sahrens 		spa_open_ref(spa, FTAG);
4064789Sahrens 		mutex_exit(&spa_namespace_lock);
40651544Seschrock 		spa_async_suspend(spa);
40664808Sek110237 		mutex_enter(&spa_namespace_lock);
4067789Sahrens 		spa_close(spa, FTAG);
4068789Sahrens 
4069789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4070789Sahrens 			spa_unload(spa);
4071789Sahrens 			spa_deactivate(spa);
4072789Sahrens 		}
4073789Sahrens 		spa_remove(spa);
4074789Sahrens 	}
4075789Sahrens 	mutex_exit(&spa_namespace_lock);
4076789Sahrens }
40771544Seschrock 
40781544Seschrock vdev_t *
40796643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
40801544Seschrock {
40816643Seschrock 	vdev_t *vd;
40826643Seschrock 	int i;
40836643Seschrock 
40846643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
40856643Seschrock 		return (vd);
40866643Seschrock 
40876643Seschrock 	if (l2cache) {
40886643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
40896643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
40906643Seschrock 			if (vd->vdev_guid == guid)
40916643Seschrock 				return (vd);
40926643Seschrock 		}
40936643Seschrock 	}
40946643Seschrock 
40956643Seschrock 	return (NULL);
40961544Seschrock }
40971760Seschrock 
40981760Seschrock void
40995094Slling spa_upgrade(spa_t *spa, uint64_t version)
41001760Seschrock {
41011760Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
41021760Seschrock 
41031760Seschrock 	/*
41041760Seschrock 	 * This should only be called for a non-faulted pool, and since a
41051760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
41061760Seschrock 	 * possible.
41071760Seschrock 	 */
41084577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
41095094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
41105094Slling 
41115094Slling 	spa->spa_uberblock.ub_version = version;
41121760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
41131760Seschrock 
41141760Seschrock 	spa_config_exit(spa, FTAG);
41152082Seschrock 
41162082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
41171760Seschrock }
41182082Seschrock 
41192082Seschrock boolean_t
41202082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
41212082Seschrock {
41222082Seschrock 	int i;
41233377Seschrock 	uint64_t spareguid;
41245450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
41255450Sbrendan 
41265450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
41275450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
41282082Seschrock 			return (B_TRUE);
41292082Seschrock 
41305450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
41315450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
41325450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
41333377Seschrock 			return (B_TRUE);
41343377Seschrock 	}
41353377Seschrock 
41362082Seschrock 	return (B_FALSE);
41372082Seschrock }
41383912Slling 
41394451Seschrock /*
41404451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
41414451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
41424451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
41434451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
41444451Seschrock  * or zdb as real changes.
41454451Seschrock  */
41464451Seschrock void
41474451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
41484451Seschrock {
41494451Seschrock #ifdef _KERNEL
41504451Seschrock 	sysevent_t		*ev;
41514451Seschrock 	sysevent_attr_list_t	*attr = NULL;
41524451Seschrock 	sysevent_value_t	value;
41534451Seschrock 	sysevent_id_t		eid;
41544451Seschrock 
41554451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
41564451Seschrock 	    SE_SLEEP);
41574451Seschrock 
41584451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
41594451Seschrock 	value.value.sv_string = spa_name(spa);
41604451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
41614451Seschrock 		goto done;
41624451Seschrock 
41634451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
41644451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
41654451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
41664451Seschrock 		goto done;
41674451Seschrock 
41684451Seschrock 	if (vd) {
41694451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
41704451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
41714451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
41724451Seschrock 		    SE_SLEEP) != 0)
41734451Seschrock 			goto done;
41744451Seschrock 
41754451Seschrock 		if (vd->vdev_path) {
41764451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
41774451Seschrock 			value.value.sv_string = vd->vdev_path;
41784451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
41794451Seschrock 			    &value, SE_SLEEP) != 0)
41804451Seschrock 				goto done;
41814451Seschrock 		}
41824451Seschrock 	}
41834451Seschrock 
41845756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
41855756Seschrock 		goto done;
41865756Seschrock 	attr = NULL;
41875756Seschrock 
41884451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
41894451Seschrock 
41904451Seschrock done:
41914451Seschrock 	if (attr)
41924451Seschrock 		sysevent_free_attr(attr);
41934451Seschrock 	sysevent_free(ev);
41944451Seschrock #endif
41954451Seschrock }
4196