xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 7294)
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);
717214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
725094Slling 
735094Slling /*
745094Slling  * ==========================================================================
755094Slling  * SPA properties routines
765094Slling  * ==========================================================================
775094Slling  */
785094Slling 
795094Slling /*
805094Slling  * Add a (source=src, propname=propval) list to an nvlist.
815094Slling  */
825949Slling static void
835094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
845094Slling     uint64_t intval, zprop_source_t src)
855094Slling {
865094Slling 	const char *propname = zpool_prop_to_name(prop);
875094Slling 	nvlist_t *propval;
885949Slling 
895949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
905949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
915949Slling 
925949Slling 	if (strval != NULL)
935949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
945949Slling 	else
955949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
965949Slling 
975949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
985094Slling 	nvlist_free(propval);
995094Slling }
1005094Slling 
1015094Slling /*
1025094Slling  * Get property values from the spa configuration.
1035094Slling  */
1045949Slling static void
1055094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1065094Slling {
1075094Slling 	uint64_t size = spa_get_space(spa);
1085094Slling 	uint64_t used = spa_get_alloc(spa);
1095094Slling 	uint64_t cap, version;
1105094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1116643Seschrock 	spa_config_dirent_t *dp;
1125094Slling 
1135094Slling 	/*
1145094Slling 	 * readonly properties
1155094Slling 	 */
1165949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src);
1175949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1185949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1195949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
1205094Slling 
1215094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
1225949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1235949Slling 
1245949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1255949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1265949Slling 	    spa->spa_root_vdev->vdev_state, src);
1275094Slling 
1285094Slling 	/*
1295094Slling 	 * settable properties that are not stored in the pool property object.
1305094Slling 	 */
1315094Slling 	version = spa_version(spa);
1325094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1335094Slling 		src = ZPROP_SRC_DEFAULT;
1345094Slling 	else
1355094Slling 		src = ZPROP_SRC_LOCAL;
1365949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1375949Slling 
1385949Slling 	if (spa->spa_root != NULL)
1395949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1405949Slling 		    0, ZPROP_SRC_LOCAL);
1415094Slling 
1426643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1436643Seschrock 		if (dp->scd_path == NULL) {
1445949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1456643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1466643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1475949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1486643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1495363Seschrock 		}
1505363Seschrock 	}
1515094Slling }
1525094Slling 
1535094Slling /*
1545094Slling  * Get zpool property values.
1555094Slling  */
1565094Slling int
1575094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1585094Slling {
1595094Slling 	zap_cursor_t zc;
1605094Slling 	zap_attribute_t za;
1615094Slling 	objset_t *mos = spa->spa_meta_objset;
1625094Slling 	int err;
1635094Slling 
1645949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1655094Slling 
1665094Slling 	/*
1675094Slling 	 * Get properties from the spa config.
1685094Slling 	 */
1695949Slling 	spa_prop_get_config(spa, nvp);
1705094Slling 
1715094Slling 	mutex_enter(&spa->spa_props_lock);
1725094Slling 	/* If no pool property object, no more prop to get. */
1735094Slling 	if (spa->spa_pool_props_object == 0) {
1745094Slling 		mutex_exit(&spa->spa_props_lock);
1755094Slling 		return (0);
1765094Slling 	}
1775094Slling 
1785094Slling 	/*
1795094Slling 	 * Get properties from the MOS pool property object.
1805094Slling 	 */
1815094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1825094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1835094Slling 	    zap_cursor_advance(&zc)) {
1845094Slling 		uint64_t intval = 0;
1855094Slling 		char *strval = NULL;
1865094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
1875094Slling 		zpool_prop_t prop;
1885094Slling 
1895094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
1905094Slling 			continue;
1915094Slling 
1925094Slling 		switch (za.za_integer_length) {
1935094Slling 		case 8:
1945094Slling 			/* integer property */
1955094Slling 			if (za.za_first_integer !=
1965094Slling 			    zpool_prop_default_numeric(prop))
1975094Slling 				src = ZPROP_SRC_LOCAL;
1985094Slling 
1995094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2005094Slling 				dsl_pool_t *dp;
2015094Slling 				dsl_dataset_t *ds = NULL;
2025094Slling 
2035094Slling 				dp = spa_get_dsl(spa);
2045094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2056689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2066689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2075094Slling 					rw_exit(&dp->dp_config_rwlock);
2085094Slling 					break;
2095094Slling 				}
2105094Slling 
2115094Slling 				strval = kmem_alloc(
2125094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2135094Slling 				    KM_SLEEP);
2145094Slling 				dsl_dataset_name(ds, strval);
2156689Smaybee 				dsl_dataset_rele(ds, FTAG);
2165094Slling 				rw_exit(&dp->dp_config_rwlock);
2175094Slling 			} else {
2185094Slling 				strval = NULL;
2195094Slling 				intval = za.za_first_integer;
2205094Slling 			}
2215094Slling 
2225949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2235094Slling 
2245094Slling 			if (strval != NULL)
2255094Slling 				kmem_free(strval,
2265094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2275094Slling 
2285094Slling 			break;
2295094Slling 
2305094Slling 		case 1:
2315094Slling 			/* string property */
2325094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2335094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2345094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2355094Slling 			if (err) {
2365094Slling 				kmem_free(strval, za.za_num_integers);
2375094Slling 				break;
2385094Slling 			}
2395949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2405094Slling 			kmem_free(strval, za.za_num_integers);
2415094Slling 			break;
2425094Slling 
2435094Slling 		default:
2445094Slling 			break;
2455094Slling 		}
2465094Slling 	}
2475094Slling 	zap_cursor_fini(&zc);
2485094Slling 	mutex_exit(&spa->spa_props_lock);
2495094Slling out:
2505094Slling 	if (err && err != ENOENT) {
2515094Slling 		nvlist_free(*nvp);
2525949Slling 		*nvp = NULL;
2535094Slling 		return (err);
2545094Slling 	}
2555094Slling 
2565094Slling 	return (0);
2575094Slling }
2585094Slling 
2595094Slling /*
2605094Slling  * Validate the given pool properties nvlist and modify the list
2615094Slling  * for the property values to be set.
2625094Slling  */
2635094Slling static int
2645094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2655094Slling {
2665094Slling 	nvpair_t *elem;
2675094Slling 	int error = 0, reset_bootfs = 0;
2685094Slling 	uint64_t objnum;
2695094Slling 
2705094Slling 	elem = NULL;
2715094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2725094Slling 		zpool_prop_t prop;
2735094Slling 		char *propname, *strval;
2745094Slling 		uint64_t intval;
2755094Slling 		objset_t *os;
2765363Seschrock 		char *slash;
2775094Slling 
2785094Slling 		propname = nvpair_name(elem);
2795094Slling 
2805094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2815094Slling 			return (EINVAL);
2825094Slling 
2835094Slling 		switch (prop) {
2845094Slling 		case ZPOOL_PROP_VERSION:
2855094Slling 			error = nvpair_value_uint64(elem, &intval);
2865094Slling 			if (!error &&
2875094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
2885094Slling 				error = EINVAL;
2895094Slling 			break;
2905094Slling 
2915094Slling 		case ZPOOL_PROP_DELEGATION:
2925094Slling 		case ZPOOL_PROP_AUTOREPLACE:
2935094Slling 			error = nvpair_value_uint64(elem, &intval);
2945094Slling 			if (!error && intval > 1)
2955094Slling 				error = EINVAL;
2965094Slling 			break;
2975094Slling 
2985094Slling 		case ZPOOL_PROP_BOOTFS:
2995094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3005094Slling 				error = ENOTSUP;
3015094Slling 				break;
3025094Slling 			}
3035094Slling 
3045094Slling 			/*
3057042Sgw25295 			 * Make sure the vdev config is bootable
3065094Slling 			 */
3077042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3085094Slling 				error = ENOTSUP;
3095094Slling 				break;
3105094Slling 			}
3115094Slling 
3125094Slling 			reset_bootfs = 1;
3135094Slling 
3145094Slling 			error = nvpair_value_string(elem, &strval);
3155094Slling 
3165094Slling 			if (!error) {
3177042Sgw25295 				uint64_t compress;
3187042Sgw25295 
3195094Slling 				if (strval == NULL || strval[0] == '\0') {
3205094Slling 					objnum = zpool_prop_default_numeric(
3215094Slling 					    ZPOOL_PROP_BOOTFS);
3225094Slling 					break;
3235094Slling 				}
3245094Slling 
3255094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
3266689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3275094Slling 					break;
3287042Sgw25295 
3297042Sgw25295 				/* We don't support gzip bootable datasets */
3307042Sgw25295 				if ((error = dsl_prop_get_integer(strval,
3317042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3327042Sgw25295 				    &compress, NULL)) == 0 &&
3337042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3347042Sgw25295 					error = ENOTSUP;
3357042Sgw25295 				} else {
3367042Sgw25295 					objnum = dmu_objset_id(os);
3377042Sgw25295 				}
3385094Slling 				dmu_objset_close(os);
3395094Slling 			}
3405094Slling 			break;
3415329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3425329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3435329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3445329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3455329Sgw25295 				error = EINVAL;
3465329Sgw25295 
3475329Sgw25295 			/*
3485329Sgw25295 			 * This is a special case which only occurs when
3495329Sgw25295 			 * the pool has completely failed. This allows
3505329Sgw25295 			 * the user to change the in-core failmode property
3515329Sgw25295 			 * without syncing it out to disk (I/Os might
3525329Sgw25295 			 * currently be blocked). We do this by returning
3535329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3545329Sgw25295 			 * into thinking we encountered a property validation
3555329Sgw25295 			 * error.
3565329Sgw25295 			 */
3575329Sgw25295 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3585329Sgw25295 				spa->spa_failmode = intval;
3595329Sgw25295 				error = EIO;
3605329Sgw25295 			}
3615329Sgw25295 			break;
3625363Seschrock 
3635363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3645363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3655363Seschrock 				break;
3665363Seschrock 
3675363Seschrock 			if (strval[0] == '\0')
3685363Seschrock 				break;
3695363Seschrock 
3705363Seschrock 			if (strcmp(strval, "none") == 0)
3715363Seschrock 				break;
3725363Seschrock 
3735363Seschrock 			if (strval[0] != '/') {
3745363Seschrock 				error = EINVAL;
3755363Seschrock 				break;
3765363Seschrock 			}
3775363Seschrock 
3785363Seschrock 			slash = strrchr(strval, '/');
3795363Seschrock 			ASSERT(slash != NULL);
3805363Seschrock 
3815363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3825363Seschrock 			    strcmp(slash, "/..") == 0)
3835363Seschrock 				error = EINVAL;
3845363Seschrock 			break;
3855094Slling 		}
3865094Slling 
3875094Slling 		if (error)
3885094Slling 			break;
3895094Slling 	}
3905094Slling 
3915094Slling 	if (!error && reset_bootfs) {
3925094Slling 		error = nvlist_remove(props,
3935094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
3945094Slling 
3955094Slling 		if (!error) {
3965094Slling 			error = nvlist_add_uint64(props,
3975094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
3985094Slling 		}
3995094Slling 	}
4005094Slling 
4015094Slling 	return (error);
4025094Slling }
4035094Slling 
4045094Slling int
4055094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4065094Slling {
4075094Slling 	int error;
4085094Slling 
4095094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4105094Slling 		return (error);
4115094Slling 
4125094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4135094Slling 	    spa, nvp, 3));
4145094Slling }
4155094Slling 
4165094Slling /*
4175094Slling  * If the bootfs property value is dsobj, clear it.
4185094Slling  */
4195094Slling void
4205094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4215094Slling {
4225094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4235094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4245094Slling 		    spa->spa_pool_props_object,
4255094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4265094Slling 		spa->spa_bootfs = 0;
4275094Slling 	}
4285094Slling }
4295094Slling 
430789Sahrens /*
431789Sahrens  * ==========================================================================
432789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
433789Sahrens  * ==========================================================================
434789Sahrens  */
435789Sahrens 
4361544Seschrock static int
4371544Seschrock spa_error_entry_compare(const void *a, const void *b)
4381544Seschrock {
4391544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4401544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4411544Seschrock 	int ret;
4421544Seschrock 
4431544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4441544Seschrock 	    sizeof (zbookmark_t));
4451544Seschrock 
4461544Seschrock 	if (ret < 0)
4471544Seschrock 		return (-1);
4481544Seschrock 	else if (ret > 0)
4491544Seschrock 		return (1);
4501544Seschrock 	else
4511544Seschrock 		return (0);
4521544Seschrock }
4531544Seschrock 
4541544Seschrock /*
4551544Seschrock  * Utility function which retrieves copies of the current logs and
4561544Seschrock  * re-initializes them in the process.
4571544Seschrock  */
4581544Seschrock void
4591544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4601544Seschrock {
4611544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4621544Seschrock 
4631544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4641544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4651544Seschrock 
4661544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4671544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4681544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4691544Seschrock 	avl_create(&spa->spa_errlist_last,
4701544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4711544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4721544Seschrock }
4731544Seschrock 
474789Sahrens /*
475789Sahrens  * Activate an uninitialized pool.
476789Sahrens  */
477789Sahrens static void
478789Sahrens spa_activate(spa_t *spa)
479789Sahrens {
480789Sahrens 	int t;
481789Sahrens 
482789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
483789Sahrens 
484789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
485789Sahrens 
486789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4874527Sperrin 	spa->spa_log_class = metaslab_class_create();
488789Sahrens 
489789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
490789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
4912986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
492789Sahrens 		    TASKQ_PREPOPULATE);
493789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
4942986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
495789Sahrens 		    TASKQ_PREPOPULATE);
496789Sahrens 	}
497789Sahrens 
498789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
499789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
5005329Sgw25295 	list_create(&spa->spa_zio_list, sizeof (zio_t),
5015329Sgw25295 	    offsetof(zio_t, zio_link_node));
502789Sahrens 
503789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
504789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
5051544Seschrock 
5061544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5071544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5081544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5091544Seschrock 	avl_create(&spa->spa_errlist_last,
5101544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5111544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
512789Sahrens }
513789Sahrens 
514789Sahrens /*
515789Sahrens  * Opposite of spa_activate().
516789Sahrens  */
517789Sahrens static void
518789Sahrens spa_deactivate(spa_t *spa)
519789Sahrens {
520789Sahrens 	int t;
521789Sahrens 
522789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
523789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
524789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
525789Sahrens 
526789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
527789Sahrens 
528789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
529789Sahrens 
530789Sahrens 	list_destroy(&spa->spa_dirty_list);
5315329Sgw25295 	list_destroy(&spa->spa_zio_list);
532789Sahrens 
533789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
534789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
535789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
536789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
537789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
538789Sahrens 	}
539789Sahrens 
540789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
541789Sahrens 	spa->spa_normal_class = NULL;
542789Sahrens 
5434527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5444527Sperrin 	spa->spa_log_class = NULL;
5454527Sperrin 
5461544Seschrock 	/*
5471544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5481544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5491544Seschrock 	 */
5501544Seschrock 	spa_errlog_drain(spa);
5511544Seschrock 
5521544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5531544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5541544Seschrock 
555789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
556789Sahrens }
557789Sahrens 
558789Sahrens /*
559789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
560789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
561789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
562789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
563789Sahrens  */
5642082Seschrock static int
5652082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5662082Seschrock     uint_t id, int atype)
567789Sahrens {
568789Sahrens 	nvlist_t **child;
569789Sahrens 	uint_t c, children;
5702082Seschrock 	int error;
5712082Seschrock 
5722082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5732082Seschrock 		return (error);
5742082Seschrock 
5752082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5762082Seschrock 		return (0);
577789Sahrens 
578789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
579789Sahrens 	    &child, &children) != 0) {
5802082Seschrock 		vdev_free(*vdp);
5812082Seschrock 		*vdp = NULL;
5822082Seschrock 		return (EINVAL);
583789Sahrens 	}
584789Sahrens 
585789Sahrens 	for (c = 0; c < children; c++) {
5862082Seschrock 		vdev_t *vd;
5872082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5882082Seschrock 		    atype)) != 0) {
5892082Seschrock 			vdev_free(*vdp);
5902082Seschrock 			*vdp = NULL;
5912082Seschrock 			return (error);
592789Sahrens 		}
593789Sahrens 	}
594789Sahrens 
5952082Seschrock 	ASSERT(*vdp != NULL);
5962082Seschrock 
5972082Seschrock 	return (0);
598789Sahrens }
599789Sahrens 
600789Sahrens /*
601789Sahrens  * Opposite of spa_load().
602789Sahrens  */
603789Sahrens static void
604789Sahrens spa_unload(spa_t *spa)
605789Sahrens {
6062082Seschrock 	int i;
6072082Seschrock 
608789Sahrens 	/*
6091544Seschrock 	 * Stop async tasks.
6101544Seschrock 	 */
6111544Seschrock 	spa_async_suspend(spa);
6121544Seschrock 
6131544Seschrock 	/*
614789Sahrens 	 * Stop syncing.
615789Sahrens 	 */
616789Sahrens 	if (spa->spa_sync_on) {
617789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
618789Sahrens 		spa->spa_sync_on = B_FALSE;
619789Sahrens 	}
620789Sahrens 
621789Sahrens 	/*
622789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
623789Sahrens 	 */
6241544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
6251544Seschrock 	spa_config_exit(spa, FTAG);
626789Sahrens 
627789Sahrens 	/*
6285450Sbrendan 	 * Drop and purge level 2 cache
6295450Sbrendan 	 */
6305450Sbrendan 	spa_l2cache_drop(spa);
6315450Sbrendan 
6325450Sbrendan 	/*
633789Sahrens 	 * Close the dsl pool.
634789Sahrens 	 */
635789Sahrens 	if (spa->spa_dsl_pool) {
636789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
637789Sahrens 		spa->spa_dsl_pool = NULL;
638789Sahrens 	}
639789Sahrens 
640789Sahrens 	/*
641789Sahrens 	 * Close all vdevs.
642789Sahrens 	 */
6431585Sbonwick 	if (spa->spa_root_vdev)
644789Sahrens 		vdev_free(spa->spa_root_vdev);
6451585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6461544Seschrock 
6475450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
6485450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
6495450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
6505450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
6515450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
6525450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
6535450Sbrendan 	}
6545450Sbrendan 	if (spa->spa_spares.sav_config) {
6555450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
6565450Sbrendan 		spa->spa_spares.sav_config = NULL;
6572082Seschrock 	}
6585450Sbrendan 
6595450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
6605450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
6615450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
6625450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
6635450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
6645450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
6655450Sbrendan 	}
6665450Sbrendan 	if (spa->spa_l2cache.sav_config) {
6675450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
6685450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
6692082Seschrock 	}
6702082Seschrock 
6711544Seschrock 	spa->spa_async_suspended = 0;
672789Sahrens }
673789Sahrens 
674789Sahrens /*
6752082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6762082Seschrock  * this pool.  When this is called, we have some form of basic information in
6775450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
6785450Sbrendan  * then re-generate a more complete list including status information.
6792082Seschrock  */
6802082Seschrock static void
6812082Seschrock spa_load_spares(spa_t *spa)
6822082Seschrock {
6832082Seschrock 	nvlist_t **spares;
6842082Seschrock 	uint_t nspares;
6852082Seschrock 	int i;
6863377Seschrock 	vdev_t *vd, *tvd;
6872082Seschrock 
6882082Seschrock 	/*
6892082Seschrock 	 * First, close and free any existing spare vdevs.
6902082Seschrock 	 */
6915450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
6925450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
6933377Seschrock 
6943377Seschrock 		/* Undo the call to spa_activate() below */
6956643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
6966643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
6973377Seschrock 			spa_spare_remove(tvd);
6983377Seschrock 		vdev_close(vd);
6993377Seschrock 		vdev_free(vd);
7002082Seschrock 	}
7013377Seschrock 
7025450Sbrendan 	if (spa->spa_spares.sav_vdevs)
7035450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7045450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7055450Sbrendan 
7065450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7072082Seschrock 		nspares = 0;
7082082Seschrock 	else
7095450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7102082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7112082Seschrock 
7125450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7135450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7142082Seschrock 
7152082Seschrock 	if (nspares == 0)
7162082Seschrock 		return;
7172082Seschrock 
7182082Seschrock 	/*
7192082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7203377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7213377Seschrock 	 * structures associated with it: one in the list of spares (used only
7223377Seschrock 	 * for basic validation purposes) and one in the active vdev
7233377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7243377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7253377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7262082Seschrock 	 */
7275450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
7285450Sbrendan 	    KM_SLEEP);
7295450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7302082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
7312082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
7322082Seschrock 		ASSERT(vd != NULL);
7332082Seschrock 
7345450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
7352082Seschrock 
7366643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7376643Seschrock 		    B_FALSE)) != NULL) {
7383377Seschrock 			if (!tvd->vdev_isspare)
7393377Seschrock 				spa_spare_add(tvd);
7403377Seschrock 
7413377Seschrock 			/*
7423377Seschrock 			 * We only mark the spare active if we were successfully
7433377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7443377Seschrock 			 * with a bad active spare would result in strange
7453377Seschrock 			 * behavior, because multiple pool would think the spare
7463377Seschrock 			 * is actively in use.
7473377Seschrock 			 *
7483377Seschrock 			 * There is a vulnerability here to an equally bizarre
7493377Seschrock 			 * circumstance, where a dead active spare is later
7503377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7513377Seschrock 			 * the rarity of this scenario, and the extra complexity
7523377Seschrock 			 * it adds, we ignore the possibility.
7533377Seschrock 			 */
7543377Seschrock 			if (!vdev_is_dead(tvd))
7553377Seschrock 				spa_spare_activate(tvd);
7563377Seschrock 		}
7573377Seschrock 
7582082Seschrock 		if (vdev_open(vd) != 0)
7592082Seschrock 			continue;
7602082Seschrock 
7612082Seschrock 		vd->vdev_top = vd;
7625450Sbrendan 		if (vdev_validate_aux(vd) == 0)
7635450Sbrendan 			spa_spare_add(vd);
7642082Seschrock 	}
7652082Seschrock 
7662082Seschrock 	/*
7672082Seschrock 	 * Recompute the stashed list of spares, with status information
7682082Seschrock 	 * this time.
7692082Seschrock 	 */
7705450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
7712082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7722082Seschrock 
7735450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
7745450Sbrendan 	    KM_SLEEP);
7755450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7765450Sbrendan 		spares[i] = vdev_config_generate(spa,
7775450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
7785450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
7795450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
7805450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7812082Seschrock 		nvlist_free(spares[i]);
7825450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
7835450Sbrendan }
7845450Sbrendan 
7855450Sbrendan /*
7865450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
7875450Sbrendan  * this pool.  When this is called, we have some form of basic information in
7885450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
7895450Sbrendan  * then re-generate a more complete list including status information.
7905450Sbrendan  * Devices which are already active have their details maintained, and are
7915450Sbrendan  * not re-opened.
7925450Sbrendan  */
7935450Sbrendan static void
7945450Sbrendan spa_load_l2cache(spa_t *spa)
7955450Sbrendan {
7965450Sbrendan 	nvlist_t **l2cache;
7975450Sbrendan 	uint_t nl2cache;
7985450Sbrendan 	int i, j, oldnvdevs;
7996643Seschrock 	uint64_t guid, size;
8005450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
8015450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
8025450Sbrendan 
8035450Sbrendan 	if (sav->sav_config != NULL) {
8045450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8055450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8065450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8075450Sbrendan 	} else {
8085450Sbrendan 		nl2cache = 0;
8095450Sbrendan 	}
8105450Sbrendan 
8115450Sbrendan 	oldvdevs = sav->sav_vdevs;
8125450Sbrendan 	oldnvdevs = sav->sav_count;
8135450Sbrendan 	sav->sav_vdevs = NULL;
8145450Sbrendan 	sav->sav_count = 0;
8155450Sbrendan 
8165450Sbrendan 	/*
8175450Sbrendan 	 * Process new nvlist of vdevs.
8185450Sbrendan 	 */
8195450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8205450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8215450Sbrendan 		    &guid) == 0);
8225450Sbrendan 
8235450Sbrendan 		newvdevs[i] = NULL;
8245450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
8255450Sbrendan 			vd = oldvdevs[j];
8265450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
8275450Sbrendan 				/*
8285450Sbrendan 				 * Retain previous vdev for add/remove ops.
8295450Sbrendan 				 */
8305450Sbrendan 				newvdevs[i] = vd;
8315450Sbrendan 				oldvdevs[j] = NULL;
8325450Sbrendan 				break;
8335450Sbrendan 			}
8345450Sbrendan 		}
8355450Sbrendan 
8365450Sbrendan 		if (newvdevs[i] == NULL) {
8375450Sbrendan 			/*
8385450Sbrendan 			 * Create new vdev
8395450Sbrendan 			 */
8405450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
8415450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
8425450Sbrendan 			ASSERT(vd != NULL);
8435450Sbrendan 			newvdevs[i] = vd;
8445450Sbrendan 
8455450Sbrendan 			/*
8465450Sbrendan 			 * Commit this vdev as an l2cache device,
8475450Sbrendan 			 * even if it fails to open.
8485450Sbrendan 			 */
8495450Sbrendan 			spa_l2cache_add(vd);
8505450Sbrendan 
8516643Seschrock 			vd->vdev_top = vd;
8526643Seschrock 			vd->vdev_aux = sav;
8536643Seschrock 
8546643Seschrock 			spa_l2cache_activate(vd);
8556643Seschrock 
8565450Sbrendan 			if (vdev_open(vd) != 0)
8575450Sbrendan 				continue;
8585450Sbrendan 
8595450Sbrendan 			(void) vdev_validate_aux(vd);
8605450Sbrendan 
8615450Sbrendan 			if (!vdev_is_dead(vd)) {
8625450Sbrendan 				size = vdev_get_rsize(vd);
8636643Seschrock 				l2arc_add_vdev(spa, vd,
8646643Seschrock 				    VDEV_LABEL_START_SIZE,
8656643Seschrock 				    size - VDEV_LABEL_START_SIZE);
8665450Sbrendan 			}
8675450Sbrendan 		}
8685450Sbrendan 	}
8695450Sbrendan 
8705450Sbrendan 	/*
8715450Sbrendan 	 * Purge vdevs that were dropped
8725450Sbrendan 	 */
8735450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
8745450Sbrendan 		uint64_t pool;
8755450Sbrendan 
8765450Sbrendan 		vd = oldvdevs[i];
8775450Sbrendan 		if (vd != NULL) {
8785450Sbrendan 			if (spa_mode & FWRITE &&
8795450Sbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
8806643Seschrock 			    pool != 0ULL &&
8816643Seschrock 			    l2arc_vdev_present(vd)) {
8825450Sbrendan 				l2arc_remove_vdev(vd);
8835450Sbrendan 			}
8845450Sbrendan 			(void) vdev_close(vd);
8855450Sbrendan 			spa_l2cache_remove(vd);
8865450Sbrendan 		}
8875450Sbrendan 	}
8885450Sbrendan 
8895450Sbrendan 	if (oldvdevs)
8905450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
8915450Sbrendan 
8925450Sbrendan 	if (sav->sav_config == NULL)
8935450Sbrendan 		goto out;
8945450Sbrendan 
8955450Sbrendan 	sav->sav_vdevs = newvdevs;
8965450Sbrendan 	sav->sav_count = (int)nl2cache;
8975450Sbrendan 
8985450Sbrendan 	/*
8995450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
9005450Sbrendan 	 * information this time.
9015450Sbrendan 	 */
9025450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
9035450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9045450Sbrendan 
9055450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9065450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9075450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9085450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9095450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9105450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9115450Sbrendan out:
9125450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9135450Sbrendan 		nvlist_free(l2cache[i]);
9145450Sbrendan 	if (sav->sav_count)
9155450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9162082Seschrock }
9172082Seschrock 
9182082Seschrock static int
9192082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9202082Seschrock {
9212082Seschrock 	dmu_buf_t *db;
9222082Seschrock 	char *packed = NULL;
9232082Seschrock 	size_t nvsize = 0;
9242082Seschrock 	int error;
9252082Seschrock 	*value = NULL;
9262082Seschrock 
9272082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9282082Seschrock 	nvsize = *(uint64_t *)db->db_data;
9292082Seschrock 	dmu_buf_rele(db, FTAG);
9302082Seschrock 
9312082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
9322082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
9332082Seschrock 	if (error == 0)
9342082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
9352082Seschrock 	kmem_free(packed, nvsize);
9362082Seschrock 
9372082Seschrock 	return (error);
9382082Seschrock }
9392082Seschrock 
9402082Seschrock /*
9414451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9424451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9434451Seschrock  */
9444451Seschrock static void
9454451Seschrock spa_check_removed(vdev_t *vd)
9464451Seschrock {
9474451Seschrock 	int c;
9484451Seschrock 
9494451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9504451Seschrock 		spa_check_removed(vd->vdev_child[c]);
9514451Seschrock 
9524451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9534451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9544451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9554451Seschrock 	}
9564451Seschrock }
9574451Seschrock 
9584451Seschrock /*
959*7294Sperrin  * Check for missing log devices
960*7294Sperrin  */
961*7294Sperrin int
962*7294Sperrin spa_check_logs(spa_t *spa)
963*7294Sperrin {
964*7294Sperrin 	switch (spa->spa_log_state) {
965*7294Sperrin 	case SPA_LOG_MISSING:
966*7294Sperrin 		/* need to recheck in case slog has been restored */
967*7294Sperrin 	case SPA_LOG_UNKNOWN:
968*7294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
969*7294Sperrin 		    DS_FIND_CHILDREN)) {
970*7294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
971*7294Sperrin 			return (1);
972*7294Sperrin 		}
973*7294Sperrin 		break;
974*7294Sperrin 
975*7294Sperrin 	case SPA_LOG_CLEAR:
976*7294Sperrin 		(void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
977*7294Sperrin 		    DS_FIND_CHILDREN);
978*7294Sperrin 		break;
979*7294Sperrin 	}
980*7294Sperrin 	spa->spa_log_state = SPA_LOG_GOOD;
981*7294Sperrin 	return (0);
982*7294Sperrin }
983*7294Sperrin 
984*7294Sperrin /*
985789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
9861544Seschrock  * source of configuration information.
987789Sahrens  */
988789Sahrens static int
9891544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
990789Sahrens {
991789Sahrens 	int error = 0;
992789Sahrens 	nvlist_t *nvroot = NULL;
993789Sahrens 	vdev_t *rvd;
994789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
9951635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
996789Sahrens 	uint64_t pool_guid;
9972082Seschrock 	uint64_t version;
998789Sahrens 	zio_t *zio;
9994451Seschrock 	uint64_t autoreplace = 0;
1000*7294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
1001789Sahrens 
10021544Seschrock 	spa->spa_load_state = state;
10031635Sbonwick 
1004789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
10051733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
10061544Seschrock 		error = EINVAL;
10071544Seschrock 		goto out;
10081544Seschrock 	}
1009789Sahrens 
10102082Seschrock 	/*
10112082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
10122082Seschrock 	 * it's not present treat it as the initial version.
10132082Seschrock 	 */
10142082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
10154577Sahrens 		version = SPA_VERSION_INITIAL;
10162082Seschrock 
10171733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
10181733Sbonwick 	    &spa->spa_config_txg);
10191733Sbonwick 
10201635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
10211544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
10221544Seschrock 		error = EEXIST;
10231544Seschrock 		goto out;
10241544Seschrock 	}
1025789Sahrens 
10262174Seschrock 	spa->spa_load_guid = pool_guid;
10272174Seschrock 
1028789Sahrens 	/*
10292082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
10302082Seschrock 	 * value that will be returned by spa_version() since parsing the
10312082Seschrock 	 * configuration requires knowing the version number.
1032789Sahrens 	 */
10331544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
10342082Seschrock 	spa->spa_ubsync.ub_version = version;
10352082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
10361544Seschrock 	spa_config_exit(spa, FTAG);
1037789Sahrens 
10382082Seschrock 	if (error != 0)
10391544Seschrock 		goto out;
1040789Sahrens 
10411585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1042789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1043789Sahrens 
1044789Sahrens 	/*
1045789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1046789Sahrens 	 */
10474070Smc142369 	error = vdev_open(rvd);
10484070Smc142369 	if (error != 0)
10491544Seschrock 		goto out;
1050789Sahrens 
1051789Sahrens 	/*
10521986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
10531986Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
10541986Seschrock 	 * flag.
10551986Seschrock 	 */
10561986Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
10571986Seschrock 	error = vdev_validate(rvd);
10581986Seschrock 	spa_config_exit(spa, FTAG);
10591986Seschrock 
10604070Smc142369 	if (error != 0)
10611986Seschrock 		goto out;
10621986Seschrock 
10631986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
10641986Seschrock 		error = ENXIO;
10651986Seschrock 		goto out;
10661986Seschrock 	}
10671986Seschrock 
10681986Seschrock 	/*
1069789Sahrens 	 * Find the best uberblock.
1070789Sahrens 	 */
1071789Sahrens 	bzero(ub, sizeof (uberblock_t));
1072789Sahrens 
1073789Sahrens 	zio = zio_root(spa, NULL, NULL,
1074789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1075789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
1076789Sahrens 	error = zio_wait(zio);
1077789Sahrens 
1078789Sahrens 	/*
1079789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1080789Sahrens 	 */
1081789Sahrens 	if (ub->ub_txg == 0) {
10821760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10831760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10841544Seschrock 		error = ENXIO;
10851544Seschrock 		goto out;
10861544Seschrock 	}
10871544Seschrock 
10881544Seschrock 	/*
10891544Seschrock 	 * If the pool is newer than the code, we can't open it.
10901544Seschrock 	 */
10914577Sahrens 	if (ub->ub_version > SPA_VERSION) {
10921760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10931760Seschrock 		    VDEV_AUX_VERSION_NEWER);
10941544Seschrock 		error = ENOTSUP;
10951544Seschrock 		goto out;
1096789Sahrens 	}
1097789Sahrens 
1098789Sahrens 	/*
1099789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1100789Sahrens 	 * incomplete configuration.
1101789Sahrens 	 */
11021732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
11031544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11041544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
11051544Seschrock 		error = ENXIO;
11061544Seschrock 		goto out;
1107789Sahrens 	}
1108789Sahrens 
1109789Sahrens 	/*
1110789Sahrens 	 * Initialize internal SPA structures.
1111789Sahrens 	 */
1112789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1113789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1114789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
11151544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
11161544Seschrock 	if (error) {
11171544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11181544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11191544Seschrock 		goto out;
11201544Seschrock 	}
1121789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1122789Sahrens 
11231544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1124789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
11251544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
11261544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11271544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11281544Seschrock 		error = EIO;
11291544Seschrock 		goto out;
11301544Seschrock 	}
1131789Sahrens 
1132789Sahrens 	if (!mosconfig) {
11332082Seschrock 		nvlist_t *newconfig;
11343975Sek110237 		uint64_t hostid;
11352082Seschrock 
11362082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
11371544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11381544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11391544Seschrock 			error = EIO;
11401544Seschrock 			goto out;
11411544Seschrock 		}
1142789Sahrens 
11433975Sek110237 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
11443975Sek110237 		    &hostid) == 0) {
11453975Sek110237 			char *hostname;
11463975Sek110237 			unsigned long myhostid = 0;
11473975Sek110237 
11483975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
11493975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
11503975Sek110237 
11513975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
11524178Slling 			if (hostid != 0 && myhostid != 0 &&
11534178Slling 			    (unsigned long)hostid != myhostid) {
11543975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
11553975Sek110237 				    "loaded as it was last accessed by "
11563975Sek110237 				    "another system (host: %s hostid: 0x%lx).  "
11573975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
11583975Sek110237 				    spa->spa_name, hostname,
11593975Sek110237 				    (unsigned long)hostid);
11603975Sek110237 				error = EBADF;
11613975Sek110237 				goto out;
11623975Sek110237 			}
11633975Sek110237 		}
11643975Sek110237 
1165789Sahrens 		spa_config_set(spa, newconfig);
1166789Sahrens 		spa_unload(spa);
1167789Sahrens 		spa_deactivate(spa);
1168789Sahrens 		spa_activate(spa);
1169789Sahrens 
11701544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
11711544Seschrock 	}
11721544Seschrock 
11731544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
11741544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
11751544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
11761544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11771544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11781544Seschrock 		error = EIO;
11791544Seschrock 		goto out;
1180789Sahrens 	}
1181789Sahrens 
11821544Seschrock 	/*
11832082Seschrock 	 * Load the bit that tells us to use the new accounting function
11842082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
11852082Seschrock 	 * be present.
11862082Seschrock 	 */
11872082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11882082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
11892082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
11902082Seschrock 	if (error != 0 && error != ENOENT) {
11912082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11922082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11932082Seschrock 		error = EIO;
11942082Seschrock 		goto out;
11952082Seschrock 	}
11962082Seschrock 
11972082Seschrock 	/*
11981544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
11991544Seschrock 	 * not be present.
12001544Seschrock 	 */
12011544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12021544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
12031544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
12041807Sbonwick 	if (error != 0 && error != ENOENT) {
12051544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12061544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12071544Seschrock 		error = EIO;
12081544Seschrock 		goto out;
12091544Seschrock 	}
12101544Seschrock 
12111544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12121544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
12131544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
12141544Seschrock 	if (error != 0 && error != ENOENT) {
12151544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12161544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12171544Seschrock 		error = EIO;
12181544Seschrock 		goto out;
12191544Seschrock 	}
1220789Sahrens 
1221789Sahrens 	/*
12222926Sek110237 	 * Load the history object.  If we have an older pool, this
12232926Sek110237 	 * will not be present.
12242926Sek110237 	 */
12252926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
12262926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
12272926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
12282926Sek110237 	if (error != 0 && error != ENOENT) {
12292926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12302926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
12312926Sek110237 		error = EIO;
12322926Sek110237 		goto out;
12332926Sek110237 	}
12342926Sek110237 
12352926Sek110237 	/*
12362082Seschrock 	 * Load any hot spares for this pool.
12372082Seschrock 	 */
12382082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12395450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
12402082Seschrock 	if (error != 0 && error != ENOENT) {
12412082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12422082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12432082Seschrock 		error = EIO;
12442082Seschrock 		goto out;
12452082Seschrock 	}
12462082Seschrock 	if (error == 0) {
12474577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
12485450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
12495450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
12502082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12512082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12522082Seschrock 			error = EIO;
12532082Seschrock 			goto out;
12542082Seschrock 		}
12552082Seschrock 
12562082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
12572082Seschrock 		spa_load_spares(spa);
12582082Seschrock 		spa_config_exit(spa, FTAG);
12592082Seschrock 	}
12602082Seschrock 
12615450Sbrendan 	/*
12625450Sbrendan 	 * Load any level 2 ARC devices for this pool.
12635450Sbrendan 	 */
12645450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12655450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
12665450Sbrendan 	    &spa->spa_l2cache.sav_object);
12675450Sbrendan 	if (error != 0 && error != ENOENT) {
12685450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12695450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
12705450Sbrendan 		error = EIO;
12715450Sbrendan 		goto out;
12725450Sbrendan 	}
12735450Sbrendan 	if (error == 0) {
12745450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
12755450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
12765450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
12775450Sbrendan 			vdev_set_state(rvd, B_TRUE,
12785450Sbrendan 			    VDEV_STATE_CANT_OPEN,
12795450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
12805450Sbrendan 			error = EIO;
12815450Sbrendan 			goto out;
12825450Sbrendan 		}
12835450Sbrendan 
12845450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
12855450Sbrendan 		spa_load_l2cache(spa);
12865450Sbrendan 		spa_config_exit(spa, FTAG);
12875450Sbrendan 	}
12885450Sbrendan 
1289*7294Sperrin 	if (spa_check_logs(spa)) {
1290*7294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1291*7294Sperrin 		    VDEV_AUX_BAD_LOG);
1292*7294Sperrin 		error = ENXIO;
1293*7294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
1294*7294Sperrin 		goto out;
1295*7294Sperrin 	}
1296*7294Sperrin 
1297*7294Sperrin 
12985094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
12994543Smarks 
13003912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
13013912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
13023912Slling 
13033912Slling 	if (error && error != ENOENT) {
13043912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13053912Slling 		    VDEV_AUX_CORRUPT_DATA);
13063912Slling 		error = EIO;
13073912Slling 		goto out;
13083912Slling 	}
13093912Slling 
13103912Slling 	if (error == 0) {
13113912Slling 		(void) zap_lookup(spa->spa_meta_objset,
13123912Slling 		    spa->spa_pool_props_object,
13134451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
13143912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
13154451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
13164451Seschrock 		    spa->spa_pool_props_object,
13174451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
13184451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
13194543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
13204543Smarks 		    spa->spa_pool_props_object,
13214543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
13224543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
13235329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
13245329Sgw25295 		    spa->spa_pool_props_object,
13255329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
13265329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
13273912Slling 	}
13283912Slling 
13292082Seschrock 	/*
13304451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
13314451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
13324451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
13334451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
13344451Seschrock 	 * over.
13354451Seschrock 	 */
13365756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
13374451Seschrock 		spa_check_removed(spa->spa_root_vdev);
13384451Seschrock 
13394451Seschrock 	/*
13401986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1341789Sahrens 	 */
13421986Seschrock 	vdev_load(rvd);
1343789Sahrens 
1344789Sahrens 	/*
1345789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1346789Sahrens 	 */
13471544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1348789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
13491544Seschrock 	spa_config_exit(spa, FTAG);
1350789Sahrens 
1351789Sahrens 	/*
1352789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1353789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1354789Sahrens 	 */
13551544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13561544Seschrock 		error = ENXIO;
13571544Seschrock 		goto out;
13581544Seschrock 	}
1359789Sahrens 
13601544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13611635Sbonwick 		dmu_tx_t *tx;
13621635Sbonwick 		int need_update = B_FALSE;
13631585Sbonwick 		int c;
13641601Sbonwick 
13651635Sbonwick 		/*
13661635Sbonwick 		 * Claim log blocks that haven't been committed yet.
13671635Sbonwick 		 * This must all happen in a single txg.
13681635Sbonwick 		 */
13691601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1370789Sahrens 		    spa_first_txg(spa));
13712417Sahrens 		(void) dmu_objset_find(spa->spa_name,
13722417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1373789Sahrens 		dmu_tx_commit(tx);
1374789Sahrens 
1375789Sahrens 		spa->spa_sync_on = B_TRUE;
1376789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1377789Sahrens 
1378789Sahrens 		/*
1379789Sahrens 		 * Wait for all claims to sync.
1380789Sahrens 		 */
1381789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
13821585Sbonwick 
13831585Sbonwick 		/*
13841635Sbonwick 		 * If the config cache is stale, or we have uninitialized
13851635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
13861585Sbonwick 		 */
13871635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
13881635Sbonwick 		    state == SPA_LOAD_IMPORT)
13891635Sbonwick 			need_update = B_TRUE;
13901635Sbonwick 
13911635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
13921635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
13931635Sbonwick 				need_update = B_TRUE;
13941585Sbonwick 
13951585Sbonwick 		/*
13961635Sbonwick 		 * Update the config cache asychronously in case we're the
13971635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
13981585Sbonwick 		 */
13991635Sbonwick 		if (need_update)
14001635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1401789Sahrens 	}
1402789Sahrens 
14031544Seschrock 	error = 0;
14041544Seschrock out:
14057046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
14062082Seschrock 	if (error && error != EBADF)
1407*7294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
14081544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
14091544Seschrock 	spa->spa_ena = 0;
14101544Seschrock 
14111544Seschrock 	return (error);
1412789Sahrens }
1413789Sahrens 
1414789Sahrens /*
1415789Sahrens  * Pool Open/Import
1416789Sahrens  *
1417789Sahrens  * The import case is identical to an open except that the configuration is sent
1418789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1419789Sahrens  * case of an open, the pool configuration will exist in the
14204451Seschrock  * POOL_STATE_UNINITIALIZED state.
1421789Sahrens  *
1422789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1423789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1424789Sahrens  * ambiguous state.
1425789Sahrens  */
1426789Sahrens static int
1427789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1428789Sahrens {
1429789Sahrens 	spa_t *spa;
1430789Sahrens 	int error;
1431789Sahrens 	int locked = B_FALSE;
1432789Sahrens 
1433789Sahrens 	*spapp = NULL;
1434789Sahrens 
1435789Sahrens 	/*
1436789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1437789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1438789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1439789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1440789Sahrens 	 */
1441789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1442789Sahrens 		mutex_enter(&spa_namespace_lock);
1443789Sahrens 		locked = B_TRUE;
1444789Sahrens 	}
1445789Sahrens 
1446789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1447789Sahrens 		if (locked)
1448789Sahrens 			mutex_exit(&spa_namespace_lock);
1449789Sahrens 		return (ENOENT);
1450789Sahrens 	}
1451789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1452789Sahrens 
1453789Sahrens 		spa_activate(spa);
1454789Sahrens 
14551635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1456789Sahrens 
1457789Sahrens 		if (error == EBADF) {
1458789Sahrens 			/*
14591986Seschrock 			 * If vdev_validate() returns failure (indicated by
14601986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
14611986Seschrock 			 * that the pool has been exported or destroyed.  If
14621986Seschrock 			 * this is the case, the config cache is out of sync and
14631986Seschrock 			 * we should remove the pool from the namespace.
1464789Sahrens 			 */
1465789Sahrens 			spa_unload(spa);
1466789Sahrens 			spa_deactivate(spa);
14676643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1468789Sahrens 			spa_remove(spa);
1469789Sahrens 			if (locked)
1470789Sahrens 				mutex_exit(&spa_namespace_lock);
1471789Sahrens 			return (ENOENT);
14721544Seschrock 		}
14731544Seschrock 
14741544Seschrock 		if (error) {
1475789Sahrens 			/*
1476789Sahrens 			 * We can't open the pool, but we still have useful
1477789Sahrens 			 * information: the state of each vdev after the
1478789Sahrens 			 * attempted vdev_open().  Return this to the user.
1479789Sahrens 			 */
14801635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
14811635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1482789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1483789Sahrens 				    B_TRUE);
14841635Sbonwick 				spa_config_exit(spa, FTAG);
14851635Sbonwick 			}
1486789Sahrens 			spa_unload(spa);
1487789Sahrens 			spa_deactivate(spa);
14881544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1489789Sahrens 			if (locked)
1490789Sahrens 				mutex_exit(&spa_namespace_lock);
1491789Sahrens 			*spapp = NULL;
1492789Sahrens 			return (error);
14931544Seschrock 		} else {
14941544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1495789Sahrens 		}
1496789Sahrens 	}
1497789Sahrens 
1498789Sahrens 	spa_open_ref(spa, tag);
14994451Seschrock 
1500789Sahrens 	if (locked)
1501789Sahrens 		mutex_exit(&spa_namespace_lock);
1502789Sahrens 
1503789Sahrens 	*spapp = spa;
1504789Sahrens 
1505789Sahrens 	if (config != NULL) {
15061544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1507789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
15081544Seschrock 		spa_config_exit(spa, FTAG);
1509789Sahrens 	}
1510789Sahrens 
1511789Sahrens 	return (0);
1512789Sahrens }
1513789Sahrens 
1514789Sahrens int
1515789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1516789Sahrens {
1517789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1518789Sahrens }
1519789Sahrens 
15201544Seschrock /*
15211544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
15221544Seschrock  * preventing it from being exported or destroyed.
15231544Seschrock  */
15241544Seschrock spa_t *
15251544Seschrock spa_inject_addref(char *name)
15261544Seschrock {
15271544Seschrock 	spa_t *spa;
15281544Seschrock 
15291544Seschrock 	mutex_enter(&spa_namespace_lock);
15301544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
15311544Seschrock 		mutex_exit(&spa_namespace_lock);
15321544Seschrock 		return (NULL);
15331544Seschrock 	}
15341544Seschrock 	spa->spa_inject_ref++;
15351544Seschrock 	mutex_exit(&spa_namespace_lock);
15361544Seschrock 
15371544Seschrock 	return (spa);
15381544Seschrock }
15391544Seschrock 
15401544Seschrock void
15411544Seschrock spa_inject_delref(spa_t *spa)
15421544Seschrock {
15431544Seschrock 	mutex_enter(&spa_namespace_lock);
15441544Seschrock 	spa->spa_inject_ref--;
15451544Seschrock 	mutex_exit(&spa_namespace_lock);
15461544Seschrock }
15471544Seschrock 
15485450Sbrendan /*
15495450Sbrendan  * Add spares device information to the nvlist.
15505450Sbrendan  */
15512082Seschrock static void
15522082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
15532082Seschrock {
15542082Seschrock 	nvlist_t **spares;
15552082Seschrock 	uint_t i, nspares;
15562082Seschrock 	nvlist_t *nvroot;
15572082Seschrock 	uint64_t guid;
15582082Seschrock 	vdev_stat_t *vs;
15592082Seschrock 	uint_t vsc;
15603377Seschrock 	uint64_t pool;
15612082Seschrock 
15625450Sbrendan 	if (spa->spa_spares.sav_count == 0)
15632082Seschrock 		return;
15642082Seschrock 
15652082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
15662082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15675450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
15682082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15692082Seschrock 	if (nspares != 0) {
15702082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
15712082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15722082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15732082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15742082Seschrock 
15752082Seschrock 		/*
15762082Seschrock 		 * Go through and find any spares which have since been
15772082Seschrock 		 * repurposed as an active spare.  If this is the case, update
15782082Seschrock 		 * their status appropriately.
15792082Seschrock 		 */
15802082Seschrock 		for (i = 0; i < nspares; i++) {
15812082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
15822082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15837214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
15847214Slling 			    pool != 0ULL) {
15852082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
15862082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
15872082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
15882082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
15892082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
15902082Seschrock 			}
15912082Seschrock 		}
15922082Seschrock 	}
15932082Seschrock }
15942082Seschrock 
15955450Sbrendan /*
15965450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
15975450Sbrendan  */
15985450Sbrendan static void
15995450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
16005450Sbrendan {
16015450Sbrendan 	nvlist_t **l2cache;
16025450Sbrendan 	uint_t i, j, nl2cache;
16035450Sbrendan 	nvlist_t *nvroot;
16045450Sbrendan 	uint64_t guid;
16055450Sbrendan 	vdev_t *vd;
16065450Sbrendan 	vdev_stat_t *vs;
16075450Sbrendan 	uint_t vsc;
16085450Sbrendan 
16095450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
16105450Sbrendan 		return;
16115450Sbrendan 
16125450Sbrendan 	spa_config_enter(spa, RW_READER, FTAG);
16135450Sbrendan 
16145450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
16155450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16165450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
16175450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
16185450Sbrendan 	if (nl2cache != 0) {
16195450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
16205450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
16215450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
16225450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
16235450Sbrendan 
16245450Sbrendan 		/*
16255450Sbrendan 		 * Update level 2 cache device stats.
16265450Sbrendan 		 */
16275450Sbrendan 
16285450Sbrendan 		for (i = 0; i < nl2cache; i++) {
16295450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
16305450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
16315450Sbrendan 
16325450Sbrendan 			vd = NULL;
16335450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
16345450Sbrendan 				if (guid ==
16355450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
16365450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
16375450Sbrendan 					break;
16385450Sbrendan 				}
16395450Sbrendan 			}
16405450Sbrendan 			ASSERT(vd != NULL);
16415450Sbrendan 
16425450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
16435450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
16445450Sbrendan 			vdev_get_stats(vd, vs);
16455450Sbrendan 		}
16465450Sbrendan 	}
16475450Sbrendan 
16485450Sbrendan 	spa_config_exit(spa, FTAG);
16495450Sbrendan }
16505450Sbrendan 
1651789Sahrens int
16521544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1653789Sahrens {
1654789Sahrens 	int error;
1655789Sahrens 	spa_t *spa;
1656789Sahrens 
1657789Sahrens 	*config = NULL;
1658789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1659789Sahrens 
16602082Seschrock 	if (spa && *config != NULL) {
16611544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
16621544Seschrock 		    spa_get_errlog_size(spa)) == 0);
16631544Seschrock 
16642082Seschrock 		spa_add_spares(spa, *config);
16655450Sbrendan 		spa_add_l2cache(spa, *config);
16662082Seschrock 	}
16672082Seschrock 
16681544Seschrock 	/*
16691544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
16701544Seschrock 	 * and call spa_lookup() directly.
16711544Seschrock 	 */
16721544Seschrock 	if (altroot) {
16731544Seschrock 		if (spa == NULL) {
16741544Seschrock 			mutex_enter(&spa_namespace_lock);
16751544Seschrock 			spa = spa_lookup(name);
16761544Seschrock 			if (spa)
16771544Seschrock 				spa_altroot(spa, altroot, buflen);
16781544Seschrock 			else
16791544Seschrock 				altroot[0] = '\0';
16801544Seschrock 			spa = NULL;
16811544Seschrock 			mutex_exit(&spa_namespace_lock);
16821544Seschrock 		} else {
16831544Seschrock 			spa_altroot(spa, altroot, buflen);
16841544Seschrock 		}
16851544Seschrock 	}
16861544Seschrock 
1687789Sahrens 	if (spa != NULL)
1688789Sahrens 		spa_close(spa, FTAG);
1689789Sahrens 
1690789Sahrens 	return (error);
1691789Sahrens }
1692789Sahrens 
1693789Sahrens /*
16945450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
16955450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
16965450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
16975450Sbrendan  * specified, as long as they are well-formed.
16982082Seschrock  */
16992082Seschrock static int
17005450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
17015450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
17025450Sbrendan     vdev_labeltype_t label)
17032082Seschrock {
17045450Sbrendan 	nvlist_t **dev;
17055450Sbrendan 	uint_t i, ndev;
17062082Seschrock 	vdev_t *vd;
17072082Seschrock 	int error;
17082082Seschrock 
17092082Seschrock 	/*
17105450Sbrendan 	 * It's acceptable to have no devs specified.
17112082Seschrock 	 */
17125450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
17132082Seschrock 		return (0);
17142082Seschrock 
17155450Sbrendan 	if (ndev == 0)
17162082Seschrock 		return (EINVAL);
17172082Seschrock 
17182082Seschrock 	/*
17195450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
17205450Sbrendan 	 * device type.
17212082Seschrock 	 */
17225450Sbrendan 	if (spa_version(spa) < version)
17232082Seschrock 		return (ENOTSUP);
17242082Seschrock 
17253377Seschrock 	/*
17265450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
17273377Seschrock 	 * checking.
17283377Seschrock 	 */
17295450Sbrendan 	sav->sav_pending = dev;
17305450Sbrendan 	sav->sav_npending = ndev;
17315450Sbrendan 
17325450Sbrendan 	for (i = 0; i < ndev; i++) {
17335450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
17342082Seschrock 		    mode)) != 0)
17353377Seschrock 			goto out;
17362082Seschrock 
17372082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
17382082Seschrock 			vdev_free(vd);
17393377Seschrock 			error = EINVAL;
17403377Seschrock 			goto out;
17412082Seschrock 		}
17422082Seschrock 
17435450Sbrendan 		/*
17445450Sbrendan 		 * The L2ARC currently only supports disk devices.
17455450Sbrendan 		 */
17465450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
17475450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
17485450Sbrendan 			error = ENOTBLK;
17495450Sbrendan 			goto out;
17505450Sbrendan 		}
17515450Sbrendan 
17522082Seschrock 		vd->vdev_top = vd;
17533377Seschrock 
17543377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
17555450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
17565450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
17573377Seschrock 			    vd->vdev_guid) == 0);
17582082Seschrock 		}
17592082Seschrock 
17602082Seschrock 		vdev_free(vd);
17613377Seschrock 
17625450Sbrendan 		if (error &&
17635450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
17643377Seschrock 			goto out;
17653377Seschrock 		else
17663377Seschrock 			error = 0;
17672082Seschrock 	}
17682082Seschrock 
17693377Seschrock out:
17705450Sbrendan 	sav->sav_pending = NULL;
17715450Sbrendan 	sav->sav_npending = 0;
17723377Seschrock 	return (error);
17732082Seschrock }
17742082Seschrock 
17755450Sbrendan static int
17765450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
17775450Sbrendan {
17785450Sbrendan 	int error;
17795450Sbrendan 
17805450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17815450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
17825450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
17835450Sbrendan 		return (error);
17845450Sbrendan 	}
17855450Sbrendan 
17865450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17875450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
17885450Sbrendan 	    VDEV_LABEL_L2CACHE));
17895450Sbrendan }
17905450Sbrendan 
17915450Sbrendan static void
17925450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
17935450Sbrendan     const char *config)
17945450Sbrendan {
17955450Sbrendan 	int i;
17965450Sbrendan 
17975450Sbrendan 	if (sav->sav_config != NULL) {
17985450Sbrendan 		nvlist_t **olddevs;
17995450Sbrendan 		uint_t oldndevs;
18005450Sbrendan 		nvlist_t **newdevs;
18015450Sbrendan 
18025450Sbrendan 		/*
18035450Sbrendan 		 * Generate new dev list by concatentating with the
18045450Sbrendan 		 * current dev list.
18055450Sbrendan 		 */
18065450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
18075450Sbrendan 		    &olddevs, &oldndevs) == 0);
18085450Sbrendan 
18095450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
18105450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
18115450Sbrendan 		for (i = 0; i < oldndevs; i++)
18125450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
18135450Sbrendan 			    KM_SLEEP) == 0);
18145450Sbrendan 		for (i = 0; i < ndevs; i++)
18155450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
18165450Sbrendan 			    KM_SLEEP) == 0);
18175450Sbrendan 
18185450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
18195450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
18205450Sbrendan 
18215450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
18225450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
18235450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
18245450Sbrendan 			nvlist_free(newdevs[i]);
18255450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
18265450Sbrendan 	} else {
18275450Sbrendan 		/*
18285450Sbrendan 		 * Generate a new dev list.
18295450Sbrendan 		 */
18305450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
18315450Sbrendan 		    KM_SLEEP) == 0);
18325450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
18335450Sbrendan 		    devs, ndevs) == 0);
18345450Sbrendan 	}
18355450Sbrendan }
18365450Sbrendan 
18375450Sbrendan /*
18385450Sbrendan  * Stop and drop level 2 ARC devices
18395450Sbrendan  */
18405450Sbrendan void
18415450Sbrendan spa_l2cache_drop(spa_t *spa)
18425450Sbrendan {
18435450Sbrendan 	vdev_t *vd;
18445450Sbrendan 	int i;
18455450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
18465450Sbrendan 
18475450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
18485450Sbrendan 		uint64_t pool;
18495450Sbrendan 
18505450Sbrendan 		vd = sav->sav_vdevs[i];
18515450Sbrendan 		ASSERT(vd != NULL);
18525450Sbrendan 
18535450Sbrendan 		if (spa_mode & FWRITE &&
18546643Seschrock 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL &&
18556643Seschrock 		    l2arc_vdev_present(vd)) {
18565450Sbrendan 			l2arc_remove_vdev(vd);
18575450Sbrendan 		}
18585450Sbrendan 		if (vd->vdev_isl2cache)
18595450Sbrendan 			spa_l2cache_remove(vd);
18605450Sbrendan 		vdev_clear_stats(vd);
18615450Sbrendan 		(void) vdev_close(vd);
18625450Sbrendan 	}
18635450Sbrendan }
18645450Sbrendan 
18652082Seschrock /*
1866789Sahrens  * Pool Creation
1867789Sahrens  */
1868789Sahrens int
18695094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
18707184Stimh     const char *history_str, nvlist_t *zplprops)
1871789Sahrens {
1872789Sahrens 	spa_t *spa;
18735094Slling 	char *altroot = NULL;
18741635Sbonwick 	vdev_t *rvd;
1875789Sahrens 	dsl_pool_t *dp;
1876789Sahrens 	dmu_tx_t *tx;
18772082Seschrock 	int c, error = 0;
1878789Sahrens 	uint64_t txg = TXG_INITIAL;
18795450Sbrendan 	nvlist_t **spares, **l2cache;
18805450Sbrendan 	uint_t nspares, nl2cache;
18815094Slling 	uint64_t version;
1882789Sahrens 
1883789Sahrens 	/*
1884789Sahrens 	 * If this pool already exists, return failure.
1885789Sahrens 	 */
1886789Sahrens 	mutex_enter(&spa_namespace_lock);
1887789Sahrens 	if (spa_lookup(pool) != NULL) {
1888789Sahrens 		mutex_exit(&spa_namespace_lock);
1889789Sahrens 		return (EEXIST);
1890789Sahrens 	}
1891789Sahrens 
1892789Sahrens 	/*
1893789Sahrens 	 * Allocate a new spa_t structure.
1894789Sahrens 	 */
18955094Slling 	(void) nvlist_lookup_string(props,
18965094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
18971635Sbonwick 	spa = spa_add(pool, altroot);
1898789Sahrens 	spa_activate(spa);
1899789Sahrens 
1900789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
19015094Slling 
19025094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
19035094Slling 		spa_unload(spa);
19045094Slling 		spa_deactivate(spa);
19055094Slling 		spa_remove(spa);
19066643Seschrock 		mutex_exit(&spa_namespace_lock);
19075094Slling 		return (error);
19085094Slling 	}
19095094Slling 
19105094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
19115094Slling 	    &version) != 0)
19125094Slling 		version = SPA_VERSION;
19135094Slling 	ASSERT(version <= SPA_VERSION);
19145094Slling 	spa->spa_uberblock.ub_version = version;
1915789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1916789Sahrens 
19171635Sbonwick 	/*
19181635Sbonwick 	 * Create the root vdev.
19191635Sbonwick 	 */
19201635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
19211635Sbonwick 
19222082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
19232082Seschrock 
19242082Seschrock 	ASSERT(error != 0 || rvd != NULL);
19252082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
19262082Seschrock 
19275913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
19281635Sbonwick 		error = EINVAL;
19292082Seschrock 
19302082Seschrock 	if (error == 0 &&
19312082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
19325450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
19332082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
19342082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
19352082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
19362082Seschrock 		vdev_config_dirty(rvd);
19371635Sbonwick 	}
19381635Sbonwick 
19391635Sbonwick 	spa_config_exit(spa, FTAG);
1940789Sahrens 
19412082Seschrock 	if (error != 0) {
1942789Sahrens 		spa_unload(spa);
1943789Sahrens 		spa_deactivate(spa);
1944789Sahrens 		spa_remove(spa);
1945789Sahrens 		mutex_exit(&spa_namespace_lock);
1946789Sahrens 		return (error);
1947789Sahrens 	}
1948789Sahrens 
19492082Seschrock 	/*
19502082Seschrock 	 * Get the list of spares, if specified.
19512082Seschrock 	 */
19522082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19532082Seschrock 	    &spares, &nspares) == 0) {
19545450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
19552082Seschrock 		    KM_SLEEP) == 0);
19565450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
19572082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
19582082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
19592082Seschrock 		spa_load_spares(spa);
19602082Seschrock 		spa_config_exit(spa, FTAG);
19615450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
19625450Sbrendan 	}
19635450Sbrendan 
19645450Sbrendan 	/*
19655450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
19665450Sbrendan 	 */
19675450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
19685450Sbrendan 	    &l2cache, &nl2cache) == 0) {
19695450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
19705450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
19715450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
19725450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
19735450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
19745450Sbrendan 		spa_load_l2cache(spa);
19755450Sbrendan 		spa_config_exit(spa, FTAG);
19765450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
19772082Seschrock 	}
19782082Seschrock 
19797184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
1980789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1981789Sahrens 
1982789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1983789Sahrens 
1984789Sahrens 	/*
1985789Sahrens 	 * Create the pool config object.
1986789Sahrens 	 */
1987789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1988789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1989789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1990789Sahrens 
19911544Seschrock 	if (zap_add(spa->spa_meta_objset,
1992789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
19931544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
19941544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
19951544Seschrock 	}
1996789Sahrens 
19975094Slling 	/* Newly created pools with the right version are always deflated. */
19985094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
19995094Slling 		spa->spa_deflate = TRUE;
20005094Slling 		if (zap_add(spa->spa_meta_objset,
20015094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
20025094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
20035094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
20045094Slling 		}
20052082Seschrock 	}
20062082Seschrock 
2007789Sahrens 	/*
2008789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2009789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2010789Sahrens 	 * keeps changing.
2011789Sahrens 	 */
2012789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2013789Sahrens 	    1 << 14, tx);
2014789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2015789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2016789Sahrens 
20171544Seschrock 	if (zap_add(spa->spa_meta_objset,
2018789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
20191544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
20201544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
20211544Seschrock 	}
2022789Sahrens 
20232926Sek110237 	/*
20242926Sek110237 	 * Create the pool's history object.
20252926Sek110237 	 */
20265094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
20275094Slling 		spa_history_create_obj(spa, tx);
20285094Slling 
20295094Slling 	/*
20305094Slling 	 * Set pool properties.
20315094Slling 	 */
20325094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
20335094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
20345329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
20355094Slling 	if (props)
20365094Slling 		spa_sync_props(spa, props, CRED(), tx);
20372926Sek110237 
2038789Sahrens 	dmu_tx_commit(tx);
2039789Sahrens 
2040789Sahrens 	spa->spa_sync_on = B_TRUE;
2041789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2042789Sahrens 
2043789Sahrens 	/*
2044789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2045789Sahrens 	 * bean counters are appropriately updated.
2046789Sahrens 	 */
2047789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2048789Sahrens 
20496643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2050789Sahrens 
20515094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
20524715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
20534715Sek110237 
2054789Sahrens 	mutex_exit(&spa_namespace_lock);
2055789Sahrens 
20567046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
20577046Sahrens 
2058789Sahrens 	return (0);
2059789Sahrens }
2060789Sahrens 
2061789Sahrens /*
2062789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2063789Sahrens  * then call spa_load() to do the dirty work.
2064789Sahrens  */
20656423Sgw25295 static int
20666423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
20676643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2068789Sahrens {
2069789Sahrens 	spa_t *spa;
20705094Slling 	char *altroot = NULL;
20716643Seschrock 	int error, loaderr;
20722082Seschrock 	nvlist_t *nvroot;
20735450Sbrendan 	nvlist_t **spares, **l2cache;
20745450Sbrendan 	uint_t nspares, nl2cache;
2075789Sahrens 
2076789Sahrens 	/*
2077789Sahrens 	 * If a pool with this name exists, return failure.
2078789Sahrens 	 */
2079789Sahrens 	mutex_enter(&spa_namespace_lock);
2080789Sahrens 	if (spa_lookup(pool) != NULL) {
2081789Sahrens 		mutex_exit(&spa_namespace_lock);
2082789Sahrens 		return (EEXIST);
2083789Sahrens 	}
2084789Sahrens 
2085789Sahrens 	/*
20861635Sbonwick 	 * Create and initialize the spa structure.
2087789Sahrens 	 */
20885094Slling 	(void) nvlist_lookup_string(props,
20895094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20901635Sbonwick 	spa = spa_add(pool, altroot);
2091789Sahrens 	spa_activate(spa);
2092789Sahrens 
20936643Seschrock 	if (allowfaulted)
20946643Seschrock 		spa->spa_import_faulted = B_TRUE;
20956673Seschrock 	spa->spa_is_root = isroot;
20966643Seschrock 
2097789Sahrens 	/*
20981635Sbonwick 	 * Pass off the heavy lifting to spa_load().
20997046Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
21007046Sahrens 	 * the user-supplied config is actually the one to trust when
21017046Sahrens 	 * doing an import.
21021601Sbonwick 	 */
21037046Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2104789Sahrens 
21052082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
21062082Seschrock 	/*
21072082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
21082082Seschrock 	 * and conflicts with spa_has_spare().
21092082Seschrock 	 */
21106423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
21115450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
21125450Sbrendan 		spa->spa_spares.sav_config = NULL;
21132082Seschrock 		spa_load_spares(spa);
21142082Seschrock 	}
21156423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
21165450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
21175450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
21185450Sbrendan 		spa_load_l2cache(spa);
21195450Sbrendan 	}
21202082Seschrock 
21212082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
21222082Seschrock 	    &nvroot) == 0);
21235450Sbrendan 	if (error == 0)
21245450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
21255450Sbrendan 	if (error == 0)
21265450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
21275450Sbrendan 		    VDEV_ALLOC_L2CACHE);
21282082Seschrock 	spa_config_exit(spa, FTAG);
21292082Seschrock 
21305094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
21316643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
21326643Seschrock 			/*
21336643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
21346643Seschrock 			 * set, then manually set the config as if the config
21356643Seschrock 			 * passed in was specified in the cache file.
21366643Seschrock 			 */
21376643Seschrock 			error = 0;
21386643Seschrock 			spa->spa_import_faulted = B_FALSE;
21396643Seschrock 			if (spa->spa_config == NULL) {
21406643Seschrock 				spa_config_enter(spa, RW_READER, FTAG);
21416643Seschrock 				spa->spa_config = spa_config_generate(spa,
21426643Seschrock 				    NULL, -1ULL, B_TRUE);
21436643Seschrock 				spa_config_exit(spa, FTAG);
21446643Seschrock 			}
21456643Seschrock 			spa_unload(spa);
21466643Seschrock 			spa_deactivate(spa);
21476643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
21486643Seschrock 		} else {
21496643Seschrock 			spa_unload(spa);
21506643Seschrock 			spa_deactivate(spa);
21516643Seschrock 			spa_remove(spa);
21526643Seschrock 		}
2153789Sahrens 		mutex_exit(&spa_namespace_lock);
2154789Sahrens 		return (error);
2155789Sahrens 	}
2156789Sahrens 
21571635Sbonwick 	/*
21585450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
21595450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
21602082Seschrock 	 */
21612082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
21622082Seschrock 	    &spares, &nspares) == 0) {
21635450Sbrendan 		if (spa->spa_spares.sav_config)
21645450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
21652082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
21662082Seschrock 		else
21675450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
21682082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21695450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
21702082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
21712082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
21722082Seschrock 		spa_load_spares(spa);
21732082Seschrock 		spa_config_exit(spa, FTAG);
21745450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
21755450Sbrendan 	}
21765450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
21775450Sbrendan 	    &l2cache, &nl2cache) == 0) {
21785450Sbrendan 		if (spa->spa_l2cache.sav_config)
21795450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
21805450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
21815450Sbrendan 		else
21825450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
21835450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21845450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
21855450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
21865450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
21875450Sbrendan 		spa_load_l2cache(spa);
21885450Sbrendan 		spa_config_exit(spa, FTAG);
21895450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
21902082Seschrock 	}
21912082Seschrock 
21926643Seschrock 	if (spa_mode & FWRITE) {
21936643Seschrock 		/*
21946643Seschrock 		 * Update the config cache to include the newly-imported pool.
21956643Seschrock 		 */
21966423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
21976643Seschrock 	}
21986643Seschrock 
21996643Seschrock 	spa->spa_import_faulted = B_FALSE;
22004451Seschrock 	mutex_exit(&spa_namespace_lock);
22014451Seschrock 
2202789Sahrens 	return (0);
2203789Sahrens }
2204789Sahrens 
22056423Sgw25295 #ifdef _KERNEL
22066423Sgw25295 /*
22076423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
22086423Sgw25295  * device label.
22096423Sgw25295  */
22106423Sgw25295 static void
22116423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
22126423Sgw25295 {
22136423Sgw25295 	nvlist_t *nvtop, *nvroot;
22146423Sgw25295 	uint64_t pgid;
22156423Sgw25295 
22166423Sgw25295 	/*
22176423Sgw25295 	 * Add this top-level vdev to the child array.
22186423Sgw25295 	 */
22196423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
22206423Sgw25295 	    == 0);
22216423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
22226423Sgw25295 	    == 0);
22236423Sgw25295 
22246423Sgw25295 	/*
22256423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
22266423Sgw25295 	 */
22276423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
22286423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
22296423Sgw25295 	    == 0);
22306423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
22316423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
22326423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
22336423Sgw25295 	    &nvtop, 1) == 0);
22346423Sgw25295 
22356423Sgw25295 	/*
22366423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
22376423Sgw25295 	 * this pool's configuration (remove the old, add the new).
22386423Sgw25295 	 */
22396423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
22406423Sgw25295 	nvlist_free(nvroot);
22416423Sgw25295 }
22426423Sgw25295 
22436423Sgw25295 /*
22446423Sgw25295  * Get the root pool information from the root disk, then import the root pool
22456423Sgw25295  * during the system boot up time.
22466423Sgw25295  */
22477147Staylor extern nvlist_t *vdev_disk_read_rootlabel(char *, char *);
22487147Staylor 
22497147Staylor int
22507147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
22516423Sgw25295     uint64_t *besttxg)
22526423Sgw25295 {
22536423Sgw25295 	nvlist_t *config;
22546423Sgw25295 	uint64_t txg;
22556423Sgw25295 
22567147Staylor 	if ((config = vdev_disk_read_rootlabel(devpath, devid)) == NULL)
22577147Staylor 		return (-1);
22586423Sgw25295 
22596423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
22606423Sgw25295 
22617147Staylor 	if (bestconf != NULL)
22626423Sgw25295 		*bestconf = config;
22637147Staylor 	*besttxg = txg;
22647147Staylor 	return (0);
22656423Sgw25295 }
22666423Sgw25295 
22676423Sgw25295 boolean_t
22686423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
22696423Sgw25295 {
22706423Sgw25295 	uint64_t ival;
22716423Sgw25295 
22726423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
22736423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
22746423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
22756423Sgw25295 		return (B_FALSE);
22766423Sgw25295 
22776423Sgw25295 	return (B_TRUE);
22786423Sgw25295 }
22796423Sgw25295 
22807147Staylor 
22817147Staylor /*
22827147Staylor  * Given the boot device's physical path or devid, check if the device
22837147Staylor  * is in a valid state.  If so, return the configuration from the vdev
22847147Staylor  * label.
22857147Staylor  */
22867147Staylor int
22877147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
22887147Staylor {
22897147Staylor 	nvlist_t *conf = NULL;
22907147Staylor 	uint64_t txg = 0;
22917147Staylor 	nvlist_t *nvtop, **child;
22927147Staylor 	char *type;
22937147Staylor 	char *bootpath = NULL;
22947147Staylor 	uint_t children, c;
22957147Staylor 	char *tmp;
22967147Staylor 
22977147Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
22987147Staylor 		*tmp = '\0';
22997147Staylor 	if (spa_check_rootconf(devpath, devid, &conf, &txg) < 0) {
23007147Staylor 		cmn_err(CE_NOTE, "error reading device label");
23017147Staylor 		nvlist_free(conf);
23027147Staylor 		return (EINVAL);
23037147Staylor 	}
23047147Staylor 	if (txg == 0) {
23057147Staylor 		cmn_err(CE_NOTE, "this device is detached");
23067147Staylor 		nvlist_free(conf);
23077147Staylor 		return (EINVAL);
23087147Staylor 	}
23097147Staylor 
23107147Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
23117147Staylor 	    &nvtop) == 0);
23127147Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
23137147Staylor 
23147147Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
23157147Staylor 		if (spa_rootdev_validate(nvtop)) {
23167147Staylor 			goto out;
23177147Staylor 		} else {
23187147Staylor 			nvlist_free(conf);
23197147Staylor 			return (EINVAL);
23207147Staylor 		}
23217147Staylor 	}
23227147Staylor 
23237147Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
23247147Staylor 
23257147Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
23267147Staylor 	    &child, &children) == 0);
23277147Staylor 
23287147Staylor 	/*
23297147Staylor 	 * Go thru vdevs in the mirror to see if the given device
23307147Staylor 	 * has the most recent txg. Only the device with the most
23317147Staylor 	 * recent txg has valid information and should be booted.
23327147Staylor 	 */
23337147Staylor 	for (c = 0; c < children; c++) {
23347147Staylor 		char *cdevid, *cpath;
23357147Staylor 		uint64_t tmptxg;
23367147Staylor 
23377147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
23387147Staylor 		    &cpath) != 0)
23397147Staylor 			return (EINVAL);
23407147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID,
23417147Staylor 		    &cdevid) != 0)
23427147Staylor 			return (EINVAL);
23437147Staylor 		if ((spa_check_rootconf(cpath, cdevid, NULL,
23447147Staylor 		    &tmptxg) == 0) && (tmptxg > txg)) {
23457147Staylor 			txg = tmptxg;
23467147Staylor 			VERIFY(nvlist_lookup_string(child[c],
23477147Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
23487147Staylor 		}
23497147Staylor 	}
23507147Staylor 
23517147Staylor 	/* Does the best device match the one we've booted from? */
23527147Staylor 	if (bootpath) {
23537147Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
23547147Staylor 		return (EINVAL);
23557147Staylor 	}
23567147Staylor out:
23577147Staylor 	*bestconf = conf;
23587147Staylor 	return (0);
23597147Staylor }
23607147Staylor 
23616423Sgw25295 /*
23626423Sgw25295  * Import a root pool.
23636423Sgw25295  *
23647147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
23657147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
23667147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
23676423Sgw25295  *
23686423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
23696423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
23706423Sgw25295  * e.g.
23716423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
23726423Sgw25295  */
23736423Sgw25295 int
23747147Staylor spa_import_rootpool(char *devpath, char *devid)
23756423Sgw25295 {
23766423Sgw25295 	nvlist_t *conf = NULL;
23776423Sgw25295 	char *pname;
23786423Sgw25295 	int error;
23796423Sgw25295 
23806423Sgw25295 	/*
23816423Sgw25295 	 * Get the vdev pathname and configuation from the most
23826423Sgw25295 	 * recently updated vdev (highest txg).
23836423Sgw25295 	 */
23847147Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
23856423Sgw25295 		goto msg_out;
23866423Sgw25295 
23876423Sgw25295 	/*
23886423Sgw25295 	 * Add type "root" vdev to the config.
23896423Sgw25295 	 */
23906423Sgw25295 	spa_build_rootpool_config(conf);
23916423Sgw25295 
23926423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
23936423Sgw25295 
23946673Seschrock 	/*
23956673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
23966673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
23976673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
23986673Seschrock 	 * pool open, not import.
23996673Seschrock 	 */
24006673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
24016423Sgw25295 	if (error == EEXIST)
24026423Sgw25295 		error = 0;
24036423Sgw25295 
24046423Sgw25295 	nvlist_free(conf);
24056423Sgw25295 	return (error);
24066423Sgw25295 
24076423Sgw25295 msg_out:
24087147Staylor 	cmn_err(CE_NOTE, "\n"
24096423Sgw25295 	    "  ***************************************************  \n"
24106423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
24116423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
24126423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
24137147Staylor 	    "  ***************************************************  ");
24146423Sgw25295 
24156423Sgw25295 	return (error);
24166423Sgw25295 }
24176423Sgw25295 #endif
24186423Sgw25295 
24196423Sgw25295 /*
24206423Sgw25295  * Import a non-root pool into the system.
24216423Sgw25295  */
24226423Sgw25295 int
24236423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
24246423Sgw25295 {
24256643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
24266423Sgw25295 }
24276423Sgw25295 
24286643Seschrock int
24296643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
24306643Seschrock {
24316643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
24326643Seschrock }
24336643Seschrock 
24346643Seschrock 
2435789Sahrens /*
2436789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2437789Sahrens  * to get the vdev stats associated with the imported devices.
2438789Sahrens  */
2439789Sahrens #define	TRYIMPORT_NAME	"$import"
2440789Sahrens 
2441789Sahrens nvlist_t *
2442789Sahrens spa_tryimport(nvlist_t *tryconfig)
2443789Sahrens {
2444789Sahrens 	nvlist_t *config = NULL;
2445789Sahrens 	char *poolname;
2446789Sahrens 	spa_t *spa;
2447789Sahrens 	uint64_t state;
2448789Sahrens 
2449789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2450789Sahrens 		return (NULL);
2451789Sahrens 
2452789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2453789Sahrens 		return (NULL);
2454789Sahrens 
24551635Sbonwick 	/*
24561635Sbonwick 	 * Create and initialize the spa structure.
24571635Sbonwick 	 */
2458789Sahrens 	mutex_enter(&spa_namespace_lock);
24591635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2460789Sahrens 	spa_activate(spa);
2461789Sahrens 
2462789Sahrens 	/*
24631635Sbonwick 	 * Pass off the heavy lifting to spa_load().
24641732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
24651732Sbonwick 	 * is actually the one to trust when doing an import.
2466789Sahrens 	 */
24671732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2468789Sahrens 
2469789Sahrens 	/*
2470789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2471789Sahrens 	 */
2472789Sahrens 	if (spa->spa_root_vdev != NULL) {
24731635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
2474789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
24751635Sbonwick 		spa_config_exit(spa, FTAG);
2476789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2477789Sahrens 		    poolname) == 0);
2478789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2479789Sahrens 		    state) == 0);
24803975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
24813975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
24822082Seschrock 
24832082Seschrock 		/*
24846423Sgw25295 		 * If the bootfs property exists on this pool then we
24856423Sgw25295 		 * copy it out so that external consumers can tell which
24866423Sgw25295 		 * pools are bootable.
24876423Sgw25295 		 */
24886423Sgw25295 		if (spa->spa_bootfs) {
24896423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
24906423Sgw25295 
24916423Sgw25295 			/*
24926423Sgw25295 			 * We have to play games with the name since the
24936423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
24946423Sgw25295 			 */
24956423Sgw25295 			if (dsl_dsobj_to_dsname(spa->spa_name,
24966423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
24976423Sgw25295 				char *cp;
24986423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
24996423Sgw25295 
25006423Sgw25295 				cp = strchr(tmpname, '/');
25016423Sgw25295 				if (cp == NULL) {
25026423Sgw25295 					(void) strlcpy(dsname, tmpname,
25036423Sgw25295 					    MAXPATHLEN);
25046423Sgw25295 				} else {
25056423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
25066423Sgw25295 					    "%s/%s", poolname, ++cp);
25076423Sgw25295 				}
25086423Sgw25295 				VERIFY(nvlist_add_string(config,
25096423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
25106423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
25116423Sgw25295 			}
25126423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
25136423Sgw25295 		}
25146423Sgw25295 
25156423Sgw25295 		/*
25165450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
25172082Seschrock 		 */
25182082Seschrock 		spa_add_spares(spa, config);
25195450Sbrendan 		spa_add_l2cache(spa, config);
2520789Sahrens 	}
2521789Sahrens 
2522789Sahrens 	spa_unload(spa);
2523789Sahrens 	spa_deactivate(spa);
2524789Sahrens 	spa_remove(spa);
2525789Sahrens 	mutex_exit(&spa_namespace_lock);
2526789Sahrens 
2527789Sahrens 	return (config);
2528789Sahrens }
2529789Sahrens 
2530789Sahrens /*
2531789Sahrens  * Pool export/destroy
2532789Sahrens  *
2533789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2534789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2535789Sahrens  * update the pool state and sync all the labels to disk, removing the
2536789Sahrens  * configuration from the cache afterwards.
2537789Sahrens  */
2538789Sahrens static int
25397214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
25407214Slling     boolean_t force)
2541789Sahrens {
2542789Sahrens 	spa_t *spa;
2543789Sahrens 
25441775Sbillm 	if (oldconfig)
25451775Sbillm 		*oldconfig = NULL;
25461775Sbillm 
2547789Sahrens 	if (!(spa_mode & FWRITE))
2548789Sahrens 		return (EROFS);
2549789Sahrens 
2550789Sahrens 	mutex_enter(&spa_namespace_lock);
2551789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2552789Sahrens 		mutex_exit(&spa_namespace_lock);
2553789Sahrens 		return (ENOENT);
2554789Sahrens 	}
2555789Sahrens 
2556789Sahrens 	/*
25571544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
25581544Seschrock 	 * reacquire the namespace lock, and see if we can export.
25591544Seschrock 	 */
25601544Seschrock 	spa_open_ref(spa, FTAG);
25611544Seschrock 	mutex_exit(&spa_namespace_lock);
25621544Seschrock 	spa_async_suspend(spa);
25631544Seschrock 	mutex_enter(&spa_namespace_lock);
25641544Seschrock 	spa_close(spa, FTAG);
25651544Seschrock 
25661544Seschrock 	/*
2567789Sahrens 	 * The pool will be in core if it's openable,
2568789Sahrens 	 * in which case we can modify its state.
2569789Sahrens 	 */
2570789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2571789Sahrens 		/*
2572789Sahrens 		 * Objsets may be open only because they're dirty, so we
2573789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2574789Sahrens 		 */
2575789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2576789Sahrens 
25771544Seschrock 		/*
25781544Seschrock 		 * A pool cannot be exported or destroyed if there are active
25791544Seschrock 		 * references.  If we are resetting a pool, allow references by
25801544Seschrock 		 * fault injection handlers.
25811544Seschrock 		 */
25821544Seschrock 		if (!spa_refcount_zero(spa) ||
25831544Seschrock 		    (spa->spa_inject_ref != 0 &&
25841544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
25851544Seschrock 			spa_async_resume(spa);
2586789Sahrens 			mutex_exit(&spa_namespace_lock);
2587789Sahrens 			return (EBUSY);
2588789Sahrens 		}
2589789Sahrens 
2590789Sahrens 		/*
25917214Slling 		 * A pool cannot be exported if it has an active shared spare.
25927214Slling 		 * This is to prevent other pools stealing the active spare
25937214Slling 		 * from an exported pool. At user's own will, such pool can
25947214Slling 		 * be forcedly exported.
25957214Slling 		 */
25967214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
25977214Slling 		    spa_has_active_shared_spare(spa)) {
25987214Slling 			spa_async_resume(spa);
25997214Slling 			mutex_exit(&spa_namespace_lock);
26007214Slling 			return (EXDEV);
26017214Slling 		}
26027214Slling 
26037214Slling 		/*
2604789Sahrens 		 * We want this to be reflected on every label,
2605789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2606789Sahrens 		 * final sync that pushes these changes out.
2607789Sahrens 		 */
26081544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
26091601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
26101544Seschrock 			spa->spa_state = new_state;
26111635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
26121544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
26131601Sbonwick 			spa_config_exit(spa, FTAG);
26141544Seschrock 		}
2615789Sahrens 	}
2616789Sahrens 
26174451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
26184451Seschrock 
2619789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2620789Sahrens 		spa_unload(spa);
2621789Sahrens 		spa_deactivate(spa);
2622789Sahrens 	}
2623789Sahrens 
26241775Sbillm 	if (oldconfig && spa->spa_config)
26251775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
26261775Sbillm 
26271544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
26286643Seschrock 		spa_config_sync(spa, B_TRUE, B_TRUE);
26291544Seschrock 		spa_remove(spa);
26301544Seschrock 	}
2631789Sahrens 	mutex_exit(&spa_namespace_lock);
2632789Sahrens 
2633789Sahrens 	return (0);
2634789Sahrens }
2635789Sahrens 
2636789Sahrens /*
2637789Sahrens  * Destroy a storage pool.
2638789Sahrens  */
2639789Sahrens int
2640789Sahrens spa_destroy(char *pool)
2641789Sahrens {
26427214Slling 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, B_FALSE));
2643789Sahrens }
2644789Sahrens 
2645789Sahrens /*
2646789Sahrens  * Export a storage pool.
2647789Sahrens  */
2648789Sahrens int
26497214Slling spa_export(char *pool, nvlist_t **oldconfig, boolean_t force)
2650789Sahrens {
26517214Slling 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, force));
2652789Sahrens }
2653789Sahrens 
2654789Sahrens /*
26551544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
26561544Seschrock  * from the namespace in any way.
26571544Seschrock  */
26581544Seschrock int
26591544Seschrock spa_reset(char *pool)
26601544Seschrock {
26617214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
26627214Slling 	    B_FALSE));
26631544Seschrock }
26641544Seschrock 
26651544Seschrock /*
2666789Sahrens  * ==========================================================================
2667789Sahrens  * Device manipulation
2668789Sahrens  * ==========================================================================
2669789Sahrens  */
2670789Sahrens 
2671789Sahrens /*
26724527Sperrin  * Add a device to a storage pool.
2673789Sahrens  */
2674789Sahrens int
2675789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2676789Sahrens {
2677789Sahrens 	uint64_t txg;
26781635Sbonwick 	int c, error;
2679789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
26801585Sbonwick 	vdev_t *vd, *tvd;
26815450Sbrendan 	nvlist_t **spares, **l2cache;
26825450Sbrendan 	uint_t nspares, nl2cache;
2683789Sahrens 
2684789Sahrens 	txg = spa_vdev_enter(spa);
2685789Sahrens 
26862082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
26872082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
26882082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
26892082Seschrock 
26903377Seschrock 	spa->spa_pending_vdev = vd;
2691789Sahrens 
26925450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
26935450Sbrendan 	    &nspares) != 0)
26942082Seschrock 		nspares = 0;
26952082Seschrock 
26965450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
26975450Sbrendan 	    &nl2cache) != 0)
26985450Sbrendan 		nl2cache = 0;
26995450Sbrendan 
27005450Sbrendan 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) {
27013377Seschrock 		spa->spa_pending_vdev = NULL;
27022082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
27033377Seschrock 	}
27042082Seschrock 
27052082Seschrock 	if (vd->vdev_children != 0) {
27063377Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
27073377Seschrock 			spa->spa_pending_vdev = NULL;
27082082Seschrock 			return (spa_vdev_exit(spa, vd, txg, error));
27092082Seschrock 		}
27102082Seschrock 	}
27112082Seschrock 
27123377Seschrock 	/*
27135450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
27145450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
27153377Seschrock 	 */
27165450Sbrendan 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) {
27173377Seschrock 		spa->spa_pending_vdev = NULL;
27183377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
27193377Seschrock 	}
27203377Seschrock 
27213377Seschrock 	spa->spa_pending_vdev = NULL;
27223377Seschrock 
27233377Seschrock 	/*
27243377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
27253377Seschrock 	 */
27263377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
27273377Seschrock 		tvd = vd->vdev_child[c];
27283377Seschrock 		vdev_remove_child(vd, tvd);
27293377Seschrock 		tvd->vdev_id = rvd->vdev_children;
27303377Seschrock 		vdev_add_child(rvd, tvd);
27313377Seschrock 		vdev_config_dirty(tvd);
27323377Seschrock 	}
27333377Seschrock 
27342082Seschrock 	if (nspares != 0) {
27355450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
27365450Sbrendan 		    ZPOOL_CONFIG_SPARES);
27372082Seschrock 		spa_load_spares(spa);
27385450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27395450Sbrendan 	}
27405450Sbrendan 
27415450Sbrendan 	if (nl2cache != 0) {
27425450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
27435450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
27445450Sbrendan 		spa_load_l2cache(spa);
27455450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2746789Sahrens 	}
2747789Sahrens 
2748789Sahrens 	/*
27491585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
27501585Sbonwick 	 * If other threads start allocating from these vdevs before we
27511585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
27521585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
27531585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
27541585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
27551635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
27561585Sbonwick 	 *
27571585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
27581585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
27591585Sbonwick 	 * steps will be completed the next time we load the pool.
2760789Sahrens 	 */
27611635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
27621585Sbonwick 
27631635Sbonwick 	mutex_enter(&spa_namespace_lock);
27641635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
27651635Sbonwick 	mutex_exit(&spa_namespace_lock);
2766789Sahrens 
27671635Sbonwick 	return (0);
2768789Sahrens }
2769789Sahrens 
2770789Sahrens /*
2771789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2772789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2773789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2774789Sahrens  *
2775789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2776789Sahrens  * existing device; in this case the two devices are made into their own
27774451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2778789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2779789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2780789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2781789Sahrens  * is automatically detached.
2782789Sahrens  */
2783789Sahrens int
27841544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2785789Sahrens {
2786789Sahrens 	uint64_t txg, open_txg;
2787789Sahrens 	int error;
2788789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2789789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
27902082Seschrock 	vdev_ops_t *pvops;
27914527Sperrin 	int is_log;
2792789Sahrens 
2793789Sahrens 	txg = spa_vdev_enter(spa);
2794789Sahrens 
27956643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2796789Sahrens 
2797789Sahrens 	if (oldvd == NULL)
2798789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2799789Sahrens 
28001585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
28011585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28021585Sbonwick 
2803789Sahrens 	pvd = oldvd->vdev_parent;
2804789Sahrens 
28052082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
28064451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
28074451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
28084451Seschrock 
28094451Seschrock 	if (newrootvd->vdev_children != 1)
2810789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2811789Sahrens 
2812789Sahrens 	newvd = newrootvd->vdev_child[0];
2813789Sahrens 
2814789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2815789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2816789Sahrens 
28172082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2818789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2819789Sahrens 
28204527Sperrin 	/*
28214527Sperrin 	 * Spares can't replace logs
28224527Sperrin 	 */
28234527Sperrin 	is_log = oldvd->vdev_islog;
28244527Sperrin 	if (is_log && newvd->vdev_isspare)
28254527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28264527Sperrin 
28272082Seschrock 	if (!replacing) {
28282082Seschrock 		/*
28292082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
28302082Seschrock 		 * vdev.
28312082Seschrock 		 */
28322082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
28332082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
28342082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28352082Seschrock 
28362082Seschrock 		pvops = &vdev_mirror_ops;
28372082Seschrock 	} else {
28382082Seschrock 		/*
28392082Seschrock 		 * Active hot spares can only be replaced by inactive hot
28402082Seschrock 		 * spares.
28412082Seschrock 		 */
28422082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
28432082Seschrock 		    pvd->vdev_child[1] == oldvd &&
28442082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
28452082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28462082Seschrock 
28472082Seschrock 		/*
28482082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
28492082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
28503377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
28513377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
28523377Seschrock 		 * the same (spare replaces spare, non-spare replaces
28533377Seschrock 		 * non-spare).
28542082Seschrock 		 */
28552082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
28562082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28573377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
28583377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
28593377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28602082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
28612082Seschrock 		    newvd->vdev_isspare)
28622082Seschrock 			pvops = &vdev_spare_ops;
28632082Seschrock 		else
28642082Seschrock 			pvops = &vdev_replacing_ops;
28652082Seschrock 	}
28662082Seschrock 
28671175Slling 	/*
28681175Slling 	 * Compare the new device size with the replaceable/attachable
28691175Slling 	 * device size.
28701175Slling 	 */
28711175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2872789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2873789Sahrens 
28741732Sbonwick 	/*
28751732Sbonwick 	 * The new device cannot have a higher alignment requirement
28761732Sbonwick 	 * than the top-level vdev.
28771732Sbonwick 	 */
28781732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2879789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2880789Sahrens 
2881789Sahrens 	/*
2882789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2883789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2884789Sahrens 	 */
2885789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2886789Sahrens 		spa_strfree(oldvd->vdev_path);
2887789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2888789Sahrens 		    KM_SLEEP);
2889789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2890789Sahrens 		    newvd->vdev_path, "old");
2891789Sahrens 		if (oldvd->vdev_devid != NULL) {
2892789Sahrens 			spa_strfree(oldvd->vdev_devid);
2893789Sahrens 			oldvd->vdev_devid = NULL;
2894789Sahrens 		}
2895789Sahrens 	}
2896789Sahrens 
2897789Sahrens 	/*
28982082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
28992082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2900789Sahrens 	 */
2901789Sahrens 	if (pvd->vdev_ops != pvops)
2902789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2903789Sahrens 
2904789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2905789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2906789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2907789Sahrens 
2908789Sahrens 	/*
2909789Sahrens 	 * Extract the new device from its root and add it to pvd.
2910789Sahrens 	 */
2911789Sahrens 	vdev_remove_child(newrootvd, newvd);
2912789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2913789Sahrens 	vdev_add_child(pvd, newvd);
2914789Sahrens 
29151544Seschrock 	/*
29161544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
29171544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
29181544Seschrock 	 */
29191544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
29201544Seschrock 
2921789Sahrens 	tvd = newvd->vdev_top;
2922789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2923789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2924789Sahrens 
2925789Sahrens 	vdev_config_dirty(tvd);
2926789Sahrens 
2927789Sahrens 	/*
2928789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2929789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2930789Sahrens 	 */
2931789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2932789Sahrens 
2933789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2934789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2935789Sahrens 	    open_txg - TXG_INITIAL + 1);
2936789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2937789Sahrens 
29383377Seschrock 	if (newvd->vdev_isspare)
29393377Seschrock 		spa_spare_activate(newvd);
29401544Seschrock 
2941789Sahrens 	/*
2942789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2943789Sahrens 	 */
29441732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2945789Sahrens 
2946789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2947789Sahrens 
2948789Sahrens 	/*
29497046Sahrens 	 * Kick off a resilver to update newvd.
2950789Sahrens 	 */
29517046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
2952789Sahrens 
2953789Sahrens 	return (0);
2954789Sahrens }
2955789Sahrens 
2956789Sahrens /*
2957789Sahrens  * Detach a device from a mirror or replacing vdev.
2958789Sahrens  * If 'replace_done' is specified, only detach if the parent
2959789Sahrens  * is a replacing vdev.
2960789Sahrens  */
2961789Sahrens int
29621544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2963789Sahrens {
2964789Sahrens 	uint64_t txg;
2965789Sahrens 	int c, t, error;
2966789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2967789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
29682082Seschrock 	boolean_t unspare = B_FALSE;
29692082Seschrock 	uint64_t unspare_guid;
29706673Seschrock 	size_t len;
2971789Sahrens 
2972789Sahrens 	txg = spa_vdev_enter(spa);
2973789Sahrens 
29746643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
2975789Sahrens 
2976789Sahrens 	if (vd == NULL)
2977789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2978789Sahrens 
29791585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
29801585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29811585Sbonwick 
2982789Sahrens 	pvd = vd->vdev_parent;
2983789Sahrens 
2984789Sahrens 	/*
2985789Sahrens 	 * If replace_done is specified, only remove this device if it's
29862082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
29872082Seschrock 	 * disk can be removed.
2988789Sahrens 	 */
29892082Seschrock 	if (replace_done) {
29902082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
29912082Seschrock 			if (vd->vdev_id != 0)
29922082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29932082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
29942082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29952082Seschrock 		}
29962082Seschrock 	}
29972082Seschrock 
29982082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
29994577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3000789Sahrens 
3001789Sahrens 	/*
30022082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3003789Sahrens 	 */
3004789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
30052082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
30062082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3007789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3008789Sahrens 
3009789Sahrens 	/*
3010789Sahrens 	 * If there's only one replica, you can't detach it.
3011789Sahrens 	 */
3012789Sahrens 	if (pvd->vdev_children <= 1)
3013789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3014789Sahrens 
3015789Sahrens 	/*
3016789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
3017789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
3018789Sahrens 	 *
3019789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
3020789Sahrens 	 * precise DTL check.
3021789Sahrens 	 */
3022789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
3023789Sahrens 		uint64_t dirty;
3024789Sahrens 
3025789Sahrens 		cvd = pvd->vdev_child[c];
3026789Sahrens 		if (cvd == vd)
3027789Sahrens 			continue;
3028789Sahrens 		if (vdev_is_dead(cvd))
3029789Sahrens 			continue;
3030789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
3031789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
3032789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
3033789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
3034789Sahrens 		if (!dirty)
3035789Sahrens 			break;
3036789Sahrens 	}
30372082Seschrock 
30382082Seschrock 	/*
30392082Seschrock 	 * If we are a replacing or spare vdev, then we can always detach the
30402082Seschrock 	 * latter child, as that is how one cancels the operation.
30412082Seschrock 	 */
30422082Seschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
30432082Seschrock 	    c == pvd->vdev_children)
3044789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3045789Sahrens 
3046789Sahrens 	/*
30476673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
30486673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
30496673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
30506673Seschrock 	 */
30516673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
30526673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
30536673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
30546673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
30556673Seschrock 		cvd = pvd->vdev_child[0];
30566673Seschrock 		len = strlen(vd->vdev_path);
30576673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
30586673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
30596673Seschrock 			spa_strfree(cvd->vdev_path);
30606673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
30616673Seschrock 		}
30626673Seschrock 	}
30636673Seschrock 
30646673Seschrock 	/*
30652082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
30662082Seschrock 	 * that the spare should become a real disk, and be removed from the
30672082Seschrock 	 * active spare list for the pool.
30682082Seschrock 	 */
30692082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
30702082Seschrock 	    vd->vdev_id == 0)
30712082Seschrock 		unspare = B_TRUE;
30722082Seschrock 
30732082Seschrock 	/*
3074789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3075789Sahrens 	 * This must be done after all other error cases are handled,
3076789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3077789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3078789Sahrens 	 * it may be that the unwritability of the disk is the reason
3079789Sahrens 	 * it's being detached!
3080789Sahrens 	 */
30813377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3082789Sahrens 
3083789Sahrens 	/*
3084789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3085789Sahrens 	 */
3086789Sahrens 	vdev_remove_child(pvd, vd);
3087789Sahrens 	vdev_compact_children(pvd);
3088789Sahrens 
3089789Sahrens 	/*
3090789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3091789Sahrens 	 */
3092789Sahrens 	cvd = pvd->vdev_child[0];
3093789Sahrens 
3094789Sahrens 	/*
30952082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
30962082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
30972082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
30982082Seschrock 	 * GUID if it creates a new toplevel GUID.
30992082Seschrock 	 */
31002082Seschrock 	if (unspare) {
31012082Seschrock 		ASSERT(cvd->vdev_isspare);
31023377Seschrock 		spa_spare_remove(cvd);
31032082Seschrock 		unspare_guid = cvd->vdev_guid;
31042082Seschrock 	}
31052082Seschrock 
31062082Seschrock 	/*
3107789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3108789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3109789Sahrens 	 */
3110789Sahrens 	if (pvd->vdev_children == 1)
3111789Sahrens 		vdev_remove_parent(cvd);
3112789Sahrens 
3113789Sahrens 	/*
3114789Sahrens 	 * We don't set tvd until now because the parent we just removed
3115789Sahrens 	 * may have been the previous top-level vdev.
3116789Sahrens 	 */
3117789Sahrens 	tvd = cvd->vdev_top;
3118789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3119789Sahrens 
3120789Sahrens 	/*
31213377Seschrock 	 * Reevaluate the parent vdev state.
3122789Sahrens 	 */
31234451Seschrock 	vdev_propagate_state(cvd);
3124789Sahrens 
3125789Sahrens 	/*
31263377Seschrock 	 * If the device we just detached was smaller than the others, it may be
31273377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
31283377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
31293377Seschrock 	 * there's nothing to read from disk.
3130789Sahrens 	 */
31311732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3132789Sahrens 
3133789Sahrens 	vdev_config_dirty(tvd);
3134789Sahrens 
3135789Sahrens 	/*
31363377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
31373377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
31383377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
31393377Seschrock 	 * prevent vd from being accessed after it's freed.
3140789Sahrens 	 */
3141789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
3142789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
31431732Sbonwick 	vd->vdev_detached = B_TRUE;
31441732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3145789Sahrens 
31464451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
31474451Seschrock 
31482082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
31492082Seschrock 
31502082Seschrock 	/*
31513377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
31523377Seschrock 	 * then we want to go through and remove the device from the hot spare
31533377Seschrock 	 * list of every other pool.
31542082Seschrock 	 */
31552082Seschrock 	if (unspare) {
31562082Seschrock 		spa = NULL;
31572082Seschrock 		mutex_enter(&spa_namespace_lock);
31582082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
31592082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
31602082Seschrock 				continue;
31612082Seschrock 
31622082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
31632082Seschrock 		}
31642082Seschrock 		mutex_exit(&spa_namespace_lock);
31652082Seschrock 	}
31662082Seschrock 
31672082Seschrock 	return (error);
31682082Seschrock }
31692082Seschrock 
31702082Seschrock /*
31715450Sbrendan  * Remove a spares vdev from the nvlist config.
31722082Seschrock  */
31735450Sbrendan static int
31745450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare,
31755450Sbrendan     nvlist_t **spares, int nspares, vdev_t *vd)
31762082Seschrock {
31775450Sbrendan 	nvlist_t *nv, **newspares;
31785450Sbrendan 	int i, j;
31792082Seschrock 
31802082Seschrock 	nv = NULL;
31815450Sbrendan 	for (i = 0; i < nspares; i++) {
31825450Sbrendan 		uint64_t theguid;
31835450Sbrendan 
31845450Sbrendan 		VERIFY(nvlist_lookup_uint64(spares[i],
31855450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
31865450Sbrendan 		if (theguid == guid) {
31875450Sbrendan 			nv = spares[i];
31885450Sbrendan 			break;
31892082Seschrock 		}
31902082Seschrock 	}
31912082Seschrock 
31922082Seschrock 	/*
31935450Sbrendan 	 * Only remove the hot spare if it's not currently in use in this pool.
31942082Seschrock 	 */
31955450Sbrendan 	if (nv == NULL && vd == NULL)
31965450Sbrendan 		return (ENOENT);
31975450Sbrendan 
31985450Sbrendan 	if (nv == NULL && vd != NULL)
31995450Sbrendan 		return (ENOTSUP);
32005450Sbrendan 
32015450Sbrendan 	if (!unspare && nv != NULL && vd != NULL)
32025450Sbrendan 		return (EBUSY);
32032082Seschrock 
32042082Seschrock 	if (nspares == 1) {
32052082Seschrock 		newspares = NULL;
32062082Seschrock 	} else {
32072082Seschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
32082082Seschrock 		    KM_SLEEP);
32092082Seschrock 		for (i = 0, j = 0; i < nspares; i++) {
32102082Seschrock 			if (spares[i] != nv)
32112082Seschrock 				VERIFY(nvlist_dup(spares[i],
32122082Seschrock 				    &newspares[j++], KM_SLEEP) == 0);
32132082Seschrock 		}
32142082Seschrock 	}
32152082Seschrock 
32165450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES,
32172082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
32185450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
32195450Sbrendan 	    ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0);
32202082Seschrock 	for (i = 0; i < nspares - 1; i++)
32212082Seschrock 		nvlist_free(newspares[i]);
32222082Seschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
32235450Sbrendan 
32245450Sbrendan 	return (0);
32255450Sbrendan }
32265450Sbrendan 
32275450Sbrendan /*
32285450Sbrendan  * Remove an l2cache vdev from the nvlist config.
32295450Sbrendan  */
32305450Sbrendan static int
32315450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache,
32325450Sbrendan     int nl2cache, vdev_t *vd)
32335450Sbrendan {
32345450Sbrendan 	nvlist_t *nv, **newl2cache;
32355450Sbrendan 	int i, j;
32365450Sbrendan 
32375450Sbrendan 	nv = NULL;
32385450Sbrendan 	for (i = 0; i < nl2cache; i++) {
32395450Sbrendan 		uint64_t theguid;
32405450Sbrendan 
32415450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i],
32425450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
32435450Sbrendan 		if (theguid == guid) {
32445450Sbrendan 			nv = l2cache[i];
32455450Sbrendan 			break;
32465450Sbrendan 		}
32475450Sbrendan 	}
32485450Sbrendan 
32495450Sbrendan 	if (vd == NULL) {
32505450Sbrendan 		for (i = 0; i < nl2cache; i++) {
32515450Sbrendan 			if (sav->sav_vdevs[i]->vdev_guid == guid) {
32525450Sbrendan 				vd = sav->sav_vdevs[i];
32535450Sbrendan 				break;
32545450Sbrendan 			}
32555450Sbrendan 		}
32565450Sbrendan 	}
32575450Sbrendan 
32585450Sbrendan 	if (nv == NULL && vd == NULL)
32595450Sbrendan 		return (ENOENT);
32605450Sbrendan 
32615450Sbrendan 	if (nv == NULL && vd != NULL)
32625450Sbrendan 		return (ENOTSUP);
32635450Sbrendan 
32645450Sbrendan 	if (nl2cache == 1) {
32655450Sbrendan 		newl2cache = NULL;
32665450Sbrendan 	} else {
32675450Sbrendan 		newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *),
32685450Sbrendan 		    KM_SLEEP);
32695450Sbrendan 		for (i = 0, j = 0; i < nl2cache; i++) {
32705450Sbrendan 			if (l2cache[i] != nv)
32715450Sbrendan 				VERIFY(nvlist_dup(l2cache[i],
32725450Sbrendan 				    &newl2cache[j++], KM_SLEEP) == 0);
32735450Sbrendan 		}
32745450Sbrendan 	}
32755450Sbrendan 
32765450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
32775450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
32785450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
32795450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0);
32805450Sbrendan 	for (i = 0; i < nl2cache - 1; i++)
32815450Sbrendan 		nvlist_free(newl2cache[i]);
32825450Sbrendan 	kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *));
32835450Sbrendan 
32845450Sbrendan 	return (0);
32855450Sbrendan }
32865450Sbrendan 
32875450Sbrendan /*
32885450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
32895450Sbrendan  * spares and level 2 ARC devices.
32905450Sbrendan  */
32915450Sbrendan int
32925450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
32935450Sbrendan {
32945450Sbrendan 	vdev_t *vd;
32955450Sbrendan 	nvlist_t **spares, **l2cache;
32965450Sbrendan 	uint_t nspares, nl2cache;
32975450Sbrendan 	int error = 0;
32985450Sbrendan 
32995450Sbrendan 	spa_config_enter(spa, RW_WRITER, FTAG);
33005450Sbrendan 
33016643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
33025450Sbrendan 
33035450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
33045450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
33055450Sbrendan 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
33065450Sbrendan 		if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare,
33075450Sbrendan 		    spares, nspares, vd)) != 0)
33085450Sbrendan 			goto out;
33095450Sbrendan 		spa_load_spares(spa);
33105450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
33115450Sbrendan 		goto out;
33125450Sbrendan 	}
33135450Sbrendan 
33145450Sbrendan 	if (spa->spa_l2cache.sav_vdevs != NULL &&
33155450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
33165450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) {
33175450Sbrendan 		if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid,
33185450Sbrendan 		    l2cache, nl2cache, vd)) != 0)
33195450Sbrendan 			goto out;
33205450Sbrendan 		spa_load_l2cache(spa);
33215450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
33225450Sbrendan 	}
33232082Seschrock 
33242082Seschrock out:
33252082Seschrock 	spa_config_exit(spa, FTAG);
33265450Sbrendan 	return (error);
3327789Sahrens }
3328789Sahrens 
3329789Sahrens /*
33304451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
33314451Seschrock  * current spared, so we can detach it.
3332789Sahrens  */
33331544Seschrock static vdev_t *
33344451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3335789Sahrens {
33361544Seschrock 	vdev_t *newvd, *oldvd;
3337789Sahrens 	int c;
3338789Sahrens 
33391544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
33404451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
33411544Seschrock 		if (oldvd != NULL)
33421544Seschrock 			return (oldvd);
33431544Seschrock 	}
3344789Sahrens 
33454451Seschrock 	/*
33464451Seschrock 	 * Check for a completed replacement.
33474451Seschrock 	 */
3348789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
33491544Seschrock 		oldvd = vd->vdev_child[0];
33501544Seschrock 		newvd = vd->vdev_child[1];
3351789Sahrens 
33521544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33531544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
33541544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33551544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33561544Seschrock 			return (oldvd);
33571544Seschrock 		}
33581544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33591544Seschrock 	}
3360789Sahrens 
33614451Seschrock 	/*
33624451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
33634451Seschrock 	 */
33644451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
33654451Seschrock 		newvd = vd->vdev_child[0];
33664451Seschrock 		oldvd = vd->vdev_child[1];
33674451Seschrock 
33684451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33694451Seschrock 		if (newvd->vdev_unspare &&
33704451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
33714451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33724451Seschrock 			newvd->vdev_unspare = 0;
33734451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33744451Seschrock 			return (oldvd);
33754451Seschrock 		}
33764451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33774451Seschrock 	}
33784451Seschrock 
33791544Seschrock 	return (NULL);
3380789Sahrens }
3381789Sahrens 
33821544Seschrock static void
33834451Seschrock spa_vdev_resilver_done(spa_t *spa)
3384789Sahrens {
33851544Seschrock 	vdev_t *vd;
33862082Seschrock 	vdev_t *pvd;
33871544Seschrock 	uint64_t guid;
33882082Seschrock 	uint64_t pguid = 0;
3389789Sahrens 
33901544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3391789Sahrens 
33924451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
33931544Seschrock 		guid = vd->vdev_guid;
33942082Seschrock 		/*
33952082Seschrock 		 * If we have just finished replacing a hot spared device, then
33962082Seschrock 		 * we need to detach the parent's first child (the original hot
33972082Seschrock 		 * spare) as well.
33982082Seschrock 		 */
33992082Seschrock 		pvd = vd->vdev_parent;
34002082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
34012082Seschrock 		    pvd->vdev_id == 0) {
34022082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
34032082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
34042082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
34052082Seschrock 		}
34061544Seschrock 		spa_config_exit(spa, FTAG);
34071544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
34081544Seschrock 			return;
34092082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
34102082Seschrock 			return;
34111544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
3412789Sahrens 	}
3413789Sahrens 
34141544Seschrock 	spa_config_exit(spa, FTAG);
3415789Sahrens }
3416789Sahrens 
3417789Sahrens /*
34181354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
34191354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
34201354Seschrock  */
34211354Seschrock int
34221354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
34231354Seschrock {
34246643Seschrock 	vdev_t *vd;
34251354Seschrock 	uint64_t txg;
34261354Seschrock 
34271354Seschrock 	txg = spa_vdev_enter(spa);
34281354Seschrock 
34296643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
34302082Seschrock 		/*
34316643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
34326643Seschrock 		 * it is, update the path manually as there is no associated
34336643Seschrock 		 * vdev_t that can be synced to disk.
34342082Seschrock 		 */
34356643Seschrock 		nvlist_t **spares;
34366643Seschrock 		uint_t i, nspares;
34375450Sbrendan 
34385450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
34395450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
34405450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
34415450Sbrendan 			    &spares, &nspares) == 0);
34422082Seschrock 			for (i = 0; i < nspares; i++) {
34432082Seschrock 				uint64_t theguid;
34442082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
34452082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
34465450Sbrendan 				if (theguid == guid) {
34475450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
34485450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
34495450Sbrendan 					spa_load_spares(spa);
34505450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
34515450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
34525450Sbrendan 					    0));
34535450Sbrendan 				}
34542082Seschrock 			}
34552082Seschrock 		}
34565450Sbrendan 
34575450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
34582082Seschrock 	}
34591354Seschrock 
34601585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
34611585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34621585Sbonwick 
34631354Seschrock 	spa_strfree(vd->vdev_path);
34641354Seschrock 	vd->vdev_path = spa_strdup(newpath);
34651354Seschrock 
34661354Seschrock 	vdev_config_dirty(vd->vdev_top);
34671354Seschrock 
34681354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
34691354Seschrock }
34701354Seschrock 
34711354Seschrock /*
3472789Sahrens  * ==========================================================================
3473789Sahrens  * SPA Scrubbing
3474789Sahrens  * ==========================================================================
3475789Sahrens  */
3476789Sahrens 
34777046Sahrens int
34787046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3479789Sahrens {
34804808Sek110237 	ASSERT(!spa_config_held(spa, RW_WRITER));
34814808Sek110237 
3482789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3483789Sahrens 		return (ENOTSUP);
3484789Sahrens 
3485789Sahrens 	/*
34867046Sahrens 	 * If a resilver was requested, but there is no DTL on a
34877046Sahrens 	 * writeable leaf device, we have nothing to do.
3488789Sahrens 	 */
34897046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
34907046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
34917046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
34921544Seschrock 		return (0);
34931544Seschrock 	}
3494789Sahrens 
34957046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
34967046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
34977046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
34987046Sahrens 		return (EBUSY);
34997046Sahrens 
35007046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
35017046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
35027046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
35037046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
35041544Seschrock 	} else {
35057046Sahrens 		return (EINVAL);
35061544Seschrock 	}
3507789Sahrens }
3508789Sahrens 
35091544Seschrock /*
35101544Seschrock  * ==========================================================================
35111544Seschrock  * SPA async task processing
35121544Seschrock  * ==========================================================================
35131544Seschrock  */
35141544Seschrock 
35151544Seschrock static void
35164451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3517789Sahrens {
35181544Seschrock 	vdev_t *tvd;
35191544Seschrock 	int c;
35201544Seschrock 
35214451Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
35224451Seschrock 		tvd = vd->vdev_child[c];
35234451Seschrock 		if (tvd->vdev_remove_wanted) {
35244451Seschrock 			tvd->vdev_remove_wanted = 0;
35254451Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
35264451Seschrock 			    VDEV_AUX_NONE);
35275329Sgw25295 			vdev_clear(spa, tvd, B_TRUE);
35284451Seschrock 			vdev_config_dirty(tvd->vdev_top);
35291544Seschrock 		}
35304451Seschrock 		spa_async_remove(spa, tvd);
35311544Seschrock 	}
35321544Seschrock }
35331544Seschrock 
35341544Seschrock static void
35351544Seschrock spa_async_thread(spa_t *spa)
35361544Seschrock {
35371544Seschrock 	int tasks;
35384451Seschrock 	uint64_t txg;
35391544Seschrock 
35401544Seschrock 	ASSERT(spa->spa_sync_on);
3541789Sahrens 
35421544Seschrock 	mutex_enter(&spa->spa_async_lock);
35431544Seschrock 	tasks = spa->spa_async_tasks;
35441544Seschrock 	spa->spa_async_tasks = 0;
35451544Seschrock 	mutex_exit(&spa->spa_async_lock);
35461544Seschrock 
35471544Seschrock 	/*
35481635Sbonwick 	 * See if the config needs to be updated.
35491635Sbonwick 	 */
35501635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
35511635Sbonwick 		mutex_enter(&spa_namespace_lock);
35521635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
35531635Sbonwick 		mutex_exit(&spa_namespace_lock);
35541635Sbonwick 	}
35551635Sbonwick 
35561635Sbonwick 	/*
35574451Seschrock 	 * See if any devices need to be marked REMOVED.
35585329Sgw25295 	 *
35595329Sgw25295 	 * XXX - We avoid doing this when we are in
35605329Sgw25295 	 * I/O failure state since spa_vdev_enter() grabs
35615329Sgw25295 	 * the namespace lock and would not be able to obtain
35625329Sgw25295 	 * the writer config lock.
35631544Seschrock 	 */
35645329Sgw25295 	if (tasks & SPA_ASYNC_REMOVE &&
35655329Sgw25295 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
35664451Seschrock 		txg = spa_vdev_enter(spa);
35674451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
35684451Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
35694451Seschrock 	}
35701544Seschrock 
35711544Seschrock 	/*
35721544Seschrock 	 * If any devices are done replacing, detach them.
35731544Seschrock 	 */
35744451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
35754451Seschrock 		spa_vdev_resilver_done(spa);
3576789Sahrens 
35771544Seschrock 	/*
35781544Seschrock 	 * Kick off a resilver.
35791544Seschrock 	 */
35807046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
35817046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
35821544Seschrock 
35831544Seschrock 	/*
35841544Seschrock 	 * Let the world know that we're done.
35851544Seschrock 	 */
35861544Seschrock 	mutex_enter(&spa->spa_async_lock);
35871544Seschrock 	spa->spa_async_thread = NULL;
35881544Seschrock 	cv_broadcast(&spa->spa_async_cv);
35891544Seschrock 	mutex_exit(&spa->spa_async_lock);
35901544Seschrock 	thread_exit();
35911544Seschrock }
35921544Seschrock 
35931544Seschrock void
35941544Seschrock spa_async_suspend(spa_t *spa)
35951544Seschrock {
35961544Seschrock 	mutex_enter(&spa->spa_async_lock);
35971544Seschrock 	spa->spa_async_suspended++;
35981544Seschrock 	while (spa->spa_async_thread != NULL)
35991544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
36001544Seschrock 	mutex_exit(&spa->spa_async_lock);
36011544Seschrock }
36021544Seschrock 
36031544Seschrock void
36041544Seschrock spa_async_resume(spa_t *spa)
36051544Seschrock {
36061544Seschrock 	mutex_enter(&spa->spa_async_lock);
36071544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
36081544Seschrock 	spa->spa_async_suspended--;
36091544Seschrock 	mutex_exit(&spa->spa_async_lock);
36101544Seschrock }
36111544Seschrock 
36121544Seschrock static void
36131544Seschrock spa_async_dispatch(spa_t *spa)
36141544Seschrock {
36151544Seschrock 	mutex_enter(&spa->spa_async_lock);
36161544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
36171635Sbonwick 	    spa->spa_async_thread == NULL &&
36181635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
36191544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
36201544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
36211544Seschrock 	mutex_exit(&spa->spa_async_lock);
36221544Seschrock }
36231544Seschrock 
36241544Seschrock void
36251544Seschrock spa_async_request(spa_t *spa, int task)
36261544Seschrock {
36271544Seschrock 	mutex_enter(&spa->spa_async_lock);
36281544Seschrock 	spa->spa_async_tasks |= task;
36291544Seschrock 	mutex_exit(&spa->spa_async_lock);
3630789Sahrens }
3631789Sahrens 
3632789Sahrens /*
3633789Sahrens  * ==========================================================================
3634789Sahrens  * SPA syncing routines
3635789Sahrens  * ==========================================================================
3636789Sahrens  */
3637789Sahrens 
3638789Sahrens static void
3639789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3640789Sahrens {
3641789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3642789Sahrens 	dmu_tx_t *tx;
3643789Sahrens 	blkptr_t blk;
3644789Sahrens 	uint64_t itor = 0;
3645789Sahrens 	zio_t *zio;
3646789Sahrens 	int error;
3647789Sahrens 	uint8_t c = 1;
3648789Sahrens 
3649789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3650789Sahrens 
3651789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3652789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3653789Sahrens 
3654789Sahrens 	error = zio_wait(zio);
3655789Sahrens 	ASSERT3U(error, ==, 0);
3656789Sahrens 
3657789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3658789Sahrens 	bplist_vacate(bpl, tx);
3659789Sahrens 
3660789Sahrens 	/*
3661789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3662789Sahrens 	 * (Usually only the first block is needed.)
3663789Sahrens 	 */
3664789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3665789Sahrens 	dmu_tx_commit(tx);
3666789Sahrens }
3667789Sahrens 
3668789Sahrens static void
36692082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
36702082Seschrock {
36712082Seschrock 	char *packed = NULL;
36722082Seschrock 	size_t nvsize = 0;
36732082Seschrock 	dmu_buf_t *db;
36742082Seschrock 
36752082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
36762082Seschrock 
36772082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
36782082Seschrock 
36792082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
36802082Seschrock 	    KM_SLEEP) == 0);
36812082Seschrock 
36822082Seschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
36832082Seschrock 
36842082Seschrock 	kmem_free(packed, nvsize);
36852082Seschrock 
36862082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
36872082Seschrock 	dmu_buf_will_dirty(db, tx);
36882082Seschrock 	*(uint64_t *)db->db_data = nvsize;
36892082Seschrock 	dmu_buf_rele(db, FTAG);
36902082Seschrock }
36912082Seschrock 
36922082Seschrock static void
36935450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
36945450Sbrendan     const char *config, const char *entry)
36952082Seschrock {
36962082Seschrock 	nvlist_t *nvroot;
36975450Sbrendan 	nvlist_t **list;
36982082Seschrock 	int i;
36992082Seschrock 
37005450Sbrendan 	if (!sav->sav_sync)
37012082Seschrock 		return;
37022082Seschrock 
37032082Seschrock 	/*
37045450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
37055450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
37064451Seschrock 	 * valid and the vdevs are labeled appropriately.
37072082Seschrock 	 */
37085450Sbrendan 	if (sav->sav_object == 0) {
37095450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
37105450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
37115450Sbrendan 		    sizeof (uint64_t), tx);
37122082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
37135450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
37145450Sbrendan 		    &sav->sav_object, tx) == 0);
37152082Seschrock 	}
37162082Seschrock 
37172082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
37185450Sbrendan 	if (sav->sav_count == 0) {
37195450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
37202082Seschrock 	} else {
37215450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
37225450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
37235450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
37245450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
37255450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
37265450Sbrendan 		    sav->sav_count) == 0);
37275450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
37285450Sbrendan 			nvlist_free(list[i]);
37295450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
37302082Seschrock 	}
37312082Seschrock 
37325450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
37332926Sek110237 	nvlist_free(nvroot);
37342082Seschrock 
37355450Sbrendan 	sav->sav_sync = B_FALSE;
37362082Seschrock }
37372082Seschrock 
37382082Seschrock static void
3739789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3740789Sahrens {
3741789Sahrens 	nvlist_t *config;
3742789Sahrens 
3743789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
3744789Sahrens 		return;
3745789Sahrens 
3746789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
3747789Sahrens 
37481635Sbonwick 	if (spa->spa_config_syncing)
37491635Sbonwick 		nvlist_free(spa->spa_config_syncing);
37501635Sbonwick 	spa->spa_config_syncing = config;
3751789Sahrens 
37522082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3753789Sahrens }
3754789Sahrens 
37555094Slling /*
37565094Slling  * Set zpool properties.
37575094Slling  */
37583912Slling static void
37594543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
37603912Slling {
37613912Slling 	spa_t *spa = arg1;
37625094Slling 	objset_t *mos = spa->spa_meta_objset;
37633912Slling 	nvlist_t *nvp = arg2;
37645094Slling 	nvpair_t *elem;
37654451Seschrock 	uint64_t intval;
37666643Seschrock 	char *strval;
37675094Slling 	zpool_prop_t prop;
37685094Slling 	const char *propname;
37695094Slling 	zprop_type_t proptype;
37706643Seschrock 	spa_config_dirent_t *dp;
37715094Slling 
37725094Slling 	elem = NULL;
37735094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
37745094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
37755094Slling 		case ZPOOL_PROP_VERSION:
37765094Slling 			/*
37775094Slling 			 * Only set version for non-zpool-creation cases
37785094Slling 			 * (set/import). spa_create() needs special care
37795094Slling 			 * for version setting.
37805094Slling 			 */
37815094Slling 			if (tx->tx_txg != TXG_INITIAL) {
37825094Slling 				VERIFY(nvpair_value_uint64(elem,
37835094Slling 				    &intval) == 0);
37845094Slling 				ASSERT(intval <= SPA_VERSION);
37855094Slling 				ASSERT(intval >= spa_version(spa));
37865094Slling 				spa->spa_uberblock.ub_version = intval;
37875094Slling 				vdev_config_dirty(spa->spa_root_vdev);
37885094Slling 			}
37895094Slling 			break;
37905094Slling 
37915094Slling 		case ZPOOL_PROP_ALTROOT:
37925094Slling 			/*
37935094Slling 			 * 'altroot' is a non-persistent property. It should
37945094Slling 			 * have been set temporarily at creation or import time.
37955094Slling 			 */
37965094Slling 			ASSERT(spa->spa_root != NULL);
37975094Slling 			break;
37985094Slling 
37995363Seschrock 		case ZPOOL_PROP_CACHEFILE:
38005094Slling 			/*
38015363Seschrock 			 * 'cachefile' is a non-persistent property, but note
38025363Seschrock 			 * an async request that the config cache needs to be
38035363Seschrock 			 * udpated.
38045094Slling 			 */
38055363Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
38066643Seschrock 
38076643Seschrock 			dp = kmem_alloc(sizeof (spa_config_dirent_t),
38086643Seschrock 			    KM_SLEEP);
38096643Seschrock 
38106643Seschrock 			if (strval[0] == '\0')
38116643Seschrock 				dp->scd_path = spa_strdup(spa_config_path);
38126643Seschrock 			else if (strcmp(strval, "none") == 0)
38136643Seschrock 				dp->scd_path = NULL;
38146643Seschrock 			else
38156643Seschrock 				dp->scd_path = spa_strdup(strval);
38166643Seschrock 
38176643Seschrock 			list_insert_head(&spa->spa_config_list, dp);
38185363Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
38194543Smarks 			break;
38205094Slling 		default:
38215094Slling 			/*
38225094Slling 			 * Set pool property values in the poolprops mos object.
38235094Slling 			 */
38245094Slling 			mutex_enter(&spa->spa_props_lock);
38255094Slling 			if (spa->spa_pool_props_object == 0) {
38265094Slling 				objset_t *mos = spa->spa_meta_objset;
38275094Slling 
38285094Slling 				VERIFY((spa->spa_pool_props_object =
38295094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
38305094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
38315094Slling 
38325094Slling 				VERIFY(zap_update(mos,
38335094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
38345094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
38355094Slling 				    == 0);
38365094Slling 			}
38375094Slling 			mutex_exit(&spa->spa_props_lock);
38385094Slling 
38395094Slling 			/* normalize the property name */
38405094Slling 			propname = zpool_prop_to_name(prop);
38415094Slling 			proptype = zpool_prop_get_type(prop);
38425094Slling 
38435094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
38445094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
38455094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
38465094Slling 				VERIFY(zap_update(mos,
38475094Slling 				    spa->spa_pool_props_object, propname,
38485094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
38495094Slling 
38505094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
38515094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
38525094Slling 
38535094Slling 				if (proptype == PROP_TYPE_INDEX) {
38545094Slling 					const char *unused;
38555094Slling 					VERIFY(zpool_prop_index_to_string(
38565094Slling 					    prop, intval, &unused) == 0);
38575094Slling 				}
38585094Slling 				VERIFY(zap_update(mos,
38595094Slling 				    spa->spa_pool_props_object, propname,
38605094Slling 				    8, 1, &intval, tx) == 0);
38615094Slling 			} else {
38625094Slling 				ASSERT(0); /* not allowed */
38635094Slling 			}
38645094Slling 
38655329Sgw25295 			switch (prop) {
38665329Sgw25295 			case ZPOOL_PROP_DELEGATION:
38675094Slling 				spa->spa_delegation = intval;
38685329Sgw25295 				break;
38695329Sgw25295 			case ZPOOL_PROP_BOOTFS:
38705094Slling 				spa->spa_bootfs = intval;
38715329Sgw25295 				break;
38725329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
38735329Sgw25295 				spa->spa_failmode = intval;
38745329Sgw25295 				break;
38755329Sgw25295 			default:
38765329Sgw25295 				break;
38775329Sgw25295 			}
38783912Slling 		}
38795094Slling 
38805094Slling 		/* log internal history if this is not a zpool create */
38815094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
38825094Slling 		    tx->tx_txg != TXG_INITIAL) {
38835094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
38845094Slling 			    spa, tx, cr, "%s %lld %s",
38855094Slling 			    nvpair_name(elem), intval, spa->spa_name);
38865094Slling 		}
38873912Slling 	}
38883912Slling }
38893912Slling 
3890789Sahrens /*
3891789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3892789Sahrens  * part of the process, so we iterate until it converges.
3893789Sahrens  */
3894789Sahrens void
3895789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3896789Sahrens {
3897789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3898789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3899789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
39001635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3901789Sahrens 	vdev_t *vd;
3902789Sahrens 	dmu_tx_t *tx;
3903789Sahrens 	int dirty_vdevs;
3904789Sahrens 
3905789Sahrens 	/*
3906789Sahrens 	 * Lock out configuration changes.
3907789Sahrens 	 */
39081544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3909789Sahrens 
3910789Sahrens 	spa->spa_syncing_txg = txg;
3911789Sahrens 	spa->spa_sync_pass = 0;
3912789Sahrens 
39131544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3914789Sahrens 
39152082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
39162082Seschrock 
39172082Seschrock 	/*
39184577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
39192082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
39202082Seschrock 	 */
39214577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
39224577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
39232082Seschrock 		int i;
39242082Seschrock 
39252082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
39262082Seschrock 			vd = rvd->vdev_child[i];
39272082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
39282082Seschrock 				break;
39292082Seschrock 		}
39302082Seschrock 		if (i == rvd->vdev_children) {
39312082Seschrock 			spa->spa_deflate = TRUE;
39322082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
39332082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
39342082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
39352082Seschrock 		}
39362082Seschrock 	}
39372082Seschrock 
39387046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
39397046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
39407046Sahrens 		dsl_pool_create_origin(dp, tx);
39417046Sahrens 
39427046Sahrens 		/* Keeping the origin open increases spa_minref */
39437046Sahrens 		spa->spa_minref += 3;
39447046Sahrens 	}
39457046Sahrens 
39467046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
39477046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
39487046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
39497046Sahrens 	}
39507046Sahrens 
3951789Sahrens 	/*
3952789Sahrens 	 * If anything has changed in this txg, push the deferred frees
3953789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
3954789Sahrens 	 * don't generate work on an otherwise idle system.
3955789Sahrens 	 */
3956789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
39572329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
39582329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
3959789Sahrens 		spa_sync_deferred_frees(spa, txg);
3960789Sahrens 
3961789Sahrens 	/*
3962789Sahrens 	 * Iterate to convergence.
3963789Sahrens 	 */
3964789Sahrens 	do {
3965789Sahrens 		spa->spa_sync_pass++;
3966789Sahrens 
3967789Sahrens 		spa_sync_config_object(spa, tx);
39685450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
39695450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
39705450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
39715450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
39721544Seschrock 		spa_errlog_sync(spa, txg);
3973789Sahrens 		dsl_pool_sync(dp, txg);
3974789Sahrens 
3975789Sahrens 		dirty_vdevs = 0;
3976789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
3977789Sahrens 			vdev_sync(vd, txg);
3978789Sahrens 			dirty_vdevs++;
3979789Sahrens 		}
3980789Sahrens 
3981789Sahrens 		bplist_sync(bpl, tx);
3982789Sahrens 	} while (dirty_vdevs);
3983789Sahrens 
3984789Sahrens 	bplist_close(bpl);
3985789Sahrens 
3986789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
3987789Sahrens 
3988789Sahrens 	/*
3989789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
3990789Sahrens 	 * to commit the transaction group.
39911635Sbonwick 	 *
39925688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
39935688Sbonwick 	 * random top-level vdevs that are known to be visible in the
39945688Sbonwick 	 * config cache (see spa_vdev_add() for details).  If there *are*
39955688Sbonwick 	 * dirty vdevs -- or if the sync to our random subset fails --
39965688Sbonwick 	 * then sync the uberblock to all vdevs.
3997789Sahrens 	 */
39985688Sbonwick 	if (list_is_empty(&spa->spa_dirty_list)) {
39996615Sgw25295 		vdev_t *svd[SPA_DVAS_PER_BP];
40006615Sgw25295 		int svdcount = 0;
40011635Sbonwick 		int children = rvd->vdev_children;
40021635Sbonwick 		int c0 = spa_get_random(children);
40031635Sbonwick 		int c;
40041635Sbonwick 
40051635Sbonwick 		for (c = 0; c < children; c++) {
40061635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
40075688Sbonwick 			if (vd->vdev_ms_array == 0 || vd->vdev_islog)
40081635Sbonwick 				continue;
40095688Sbonwick 			svd[svdcount++] = vd;
40105688Sbonwick 			if (svdcount == SPA_DVAS_PER_BP)
40111635Sbonwick 				break;
40121635Sbonwick 		}
40136615Sgw25295 		vdev_config_sync(svd, svdcount, txg);
40146615Sgw25295 	} else {
40156615Sgw25295 		vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg);
40161635Sbonwick 	}
40172082Seschrock 	dmu_tx_commit(tx);
40182082Seschrock 
40191635Sbonwick 	/*
40201635Sbonwick 	 * Clear the dirty config list.
40211635Sbonwick 	 */
40221635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
40231635Sbonwick 		vdev_config_clean(vd);
40241635Sbonwick 
40251635Sbonwick 	/*
40261635Sbonwick 	 * Now that the new config has synced transactionally,
40271635Sbonwick 	 * let it become visible to the config cache.
40281635Sbonwick 	 */
40291635Sbonwick 	if (spa->spa_config_syncing != NULL) {
40301635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
40311635Sbonwick 		spa->spa_config_txg = txg;
40321635Sbonwick 		spa->spa_config_syncing = NULL;
40331635Sbonwick 	}
4034789Sahrens 
40357046Sahrens 	spa->spa_traverse_wanted = B_TRUE;
4036789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
40377046Sahrens 	spa->spa_traverse_wanted = B_FALSE;
4038789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4039789Sahrens 	rw_exit(&spa->spa_traverse_lock);
4040789Sahrens 
4041789Sahrens 	/*
4042789Sahrens 	 * Clean up the ZIL records for the synced txg.
4043789Sahrens 	 */
4044789Sahrens 	dsl_pool_zil_clean(dp);
4045789Sahrens 
4046789Sahrens 	/*
4047789Sahrens 	 * Update usable space statistics.
4048789Sahrens 	 */
4049789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4050789Sahrens 		vdev_sync_done(vd, txg);
4051789Sahrens 
4052789Sahrens 	/*
4053789Sahrens 	 * It had better be the case that we didn't dirty anything
40542082Seschrock 	 * since vdev_config_sync().
4055789Sahrens 	 */
4056789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4057789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4058789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4059789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4060789Sahrens 
40611544Seschrock 	spa_config_exit(spa, FTAG);
40621544Seschrock 
40631544Seschrock 	/*
40641544Seschrock 	 * If any async tasks have been requested, kick them off.
40651544Seschrock 	 */
40661544Seschrock 	spa_async_dispatch(spa);
4067789Sahrens }
4068789Sahrens 
4069789Sahrens /*
4070789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4071789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4072789Sahrens  * sync.
4073789Sahrens  */
4074789Sahrens void
4075789Sahrens spa_sync_allpools(void)
4076789Sahrens {
4077789Sahrens 	spa_t *spa = NULL;
4078789Sahrens 	mutex_enter(&spa_namespace_lock);
4079789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4080789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
4081789Sahrens 			continue;
4082789Sahrens 		spa_open_ref(spa, FTAG);
4083789Sahrens 		mutex_exit(&spa_namespace_lock);
4084789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4085789Sahrens 		mutex_enter(&spa_namespace_lock);
4086789Sahrens 		spa_close(spa, FTAG);
4087789Sahrens 	}
4088789Sahrens 	mutex_exit(&spa_namespace_lock);
4089789Sahrens }
4090789Sahrens 
4091789Sahrens /*
4092789Sahrens  * ==========================================================================
4093789Sahrens  * Miscellaneous routines
4094789Sahrens  * ==========================================================================
4095789Sahrens  */
4096789Sahrens 
4097789Sahrens /*
4098789Sahrens  * Remove all pools in the system.
4099789Sahrens  */
4100789Sahrens void
4101789Sahrens spa_evict_all(void)
4102789Sahrens {
4103789Sahrens 	spa_t *spa;
4104789Sahrens 
4105789Sahrens 	/*
4106789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4107789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4108789Sahrens 	 */
4109789Sahrens 	mutex_enter(&spa_namespace_lock);
4110789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4111789Sahrens 		/*
41121544Seschrock 		 * Stop async tasks.  The async thread may need to detach
41131544Seschrock 		 * a device that's been replaced, which requires grabbing
41141544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4115789Sahrens 		 */
4116789Sahrens 		spa_open_ref(spa, FTAG);
4117789Sahrens 		mutex_exit(&spa_namespace_lock);
41181544Seschrock 		spa_async_suspend(spa);
41194808Sek110237 		mutex_enter(&spa_namespace_lock);
4120789Sahrens 		spa_close(spa, FTAG);
4121789Sahrens 
4122789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4123789Sahrens 			spa_unload(spa);
4124789Sahrens 			spa_deactivate(spa);
4125789Sahrens 		}
4126789Sahrens 		spa_remove(spa);
4127789Sahrens 	}
4128789Sahrens 	mutex_exit(&spa_namespace_lock);
4129789Sahrens }
41301544Seschrock 
41311544Seschrock vdev_t *
41326643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
41331544Seschrock {
41346643Seschrock 	vdev_t *vd;
41356643Seschrock 	int i;
41366643Seschrock 
41376643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
41386643Seschrock 		return (vd);
41396643Seschrock 
41406643Seschrock 	if (l2cache) {
41416643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
41426643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
41436643Seschrock 			if (vd->vdev_guid == guid)
41446643Seschrock 				return (vd);
41456643Seschrock 		}
41466643Seschrock 	}
41476643Seschrock 
41486643Seschrock 	return (NULL);
41491544Seschrock }
41501760Seschrock 
41511760Seschrock void
41525094Slling spa_upgrade(spa_t *spa, uint64_t version)
41531760Seschrock {
41541760Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
41551760Seschrock 
41561760Seschrock 	/*
41571760Seschrock 	 * This should only be called for a non-faulted pool, and since a
41581760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
41591760Seschrock 	 * possible.
41601760Seschrock 	 */
41614577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
41625094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
41635094Slling 
41645094Slling 	spa->spa_uberblock.ub_version = version;
41651760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
41661760Seschrock 
41671760Seschrock 	spa_config_exit(spa, FTAG);
41682082Seschrock 
41692082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
41701760Seschrock }
41712082Seschrock 
41722082Seschrock boolean_t
41732082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
41742082Seschrock {
41752082Seschrock 	int i;
41763377Seschrock 	uint64_t spareguid;
41775450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
41785450Sbrendan 
41795450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
41805450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
41812082Seschrock 			return (B_TRUE);
41822082Seschrock 
41835450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
41845450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
41855450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
41863377Seschrock 			return (B_TRUE);
41873377Seschrock 	}
41883377Seschrock 
41892082Seschrock 	return (B_FALSE);
41902082Seschrock }
41913912Slling 
41924451Seschrock /*
41937214Slling  * Check if a pool has an active shared spare device.
41947214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
41957214Slling  */
41967214Slling static boolean_t
41977214Slling spa_has_active_shared_spare(spa_t *spa)
41987214Slling {
41997214Slling 	int i, refcnt;
42007214Slling 	uint64_t pool;
42017214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
42027214Slling 
42037214Slling 	for (i = 0; i < sav->sav_count; i++) {
42047214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
42057214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
42067214Slling 		    refcnt > 2)
42077214Slling 			return (B_TRUE);
42087214Slling 	}
42097214Slling 
42107214Slling 	return (B_FALSE);
42117214Slling }
42127214Slling 
42137214Slling /*
42144451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
42154451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
42164451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
42174451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
42184451Seschrock  * or zdb as real changes.
42194451Seschrock  */
42204451Seschrock void
42214451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
42224451Seschrock {
42234451Seschrock #ifdef _KERNEL
42244451Seschrock 	sysevent_t		*ev;
42254451Seschrock 	sysevent_attr_list_t	*attr = NULL;
42264451Seschrock 	sysevent_value_t	value;
42274451Seschrock 	sysevent_id_t		eid;
42284451Seschrock 
42294451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
42304451Seschrock 	    SE_SLEEP);
42314451Seschrock 
42324451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
42334451Seschrock 	value.value.sv_string = spa_name(spa);
42344451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
42354451Seschrock 		goto done;
42364451Seschrock 
42374451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
42384451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
42394451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
42404451Seschrock 		goto done;
42414451Seschrock 
42424451Seschrock 	if (vd) {
42434451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
42444451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
42454451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
42464451Seschrock 		    SE_SLEEP) != 0)
42474451Seschrock 			goto done;
42484451Seschrock 
42494451Seschrock 		if (vd->vdev_path) {
42504451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
42514451Seschrock 			value.value.sv_string = vd->vdev_path;
42524451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
42534451Seschrock 			    &value, SE_SLEEP) != 0)
42544451Seschrock 				goto done;
42554451Seschrock 		}
42564451Seschrock 	}
42574451Seschrock 
42585756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
42595756Seschrock 		goto done;
42605756Seschrock 	attr = NULL;
42615756Seschrock 
42624451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
42634451Seschrock 
42644451Seschrock done:
42654451Seschrock 	if (attr)
42664451Seschrock 		sysevent_free_attr(attr);
42674451Seschrock 	sysevent_free(ev);
42684451Seschrock #endif
42694451Seschrock }
4270