xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 7326)
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 /*
28789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
29789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
30789Sahrens  * pool.
31789Sahrens  */
32789Sahrens 
33789Sahrens #include <sys/zfs_context.h>
341544Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens #include <sys/spa_impl.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/zio_checksum.h>
38789Sahrens #include <sys/zio_compress.h>
39789Sahrens #include <sys/dmu.h>
40789Sahrens #include <sys/dmu_tx.h>
41789Sahrens #include <sys/zap.h>
42789Sahrens #include <sys/zil.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
45789Sahrens #include <sys/uberblock_impl.h>
46789Sahrens #include <sys/txg.h>
47789Sahrens #include <sys/avl.h>
48789Sahrens #include <sys/dmu_traverse.h>
493912Slling #include <sys/dmu_objset.h>
50789Sahrens #include <sys/unique.h>
51789Sahrens #include <sys/dsl_pool.h>
523912Slling #include <sys/dsl_dataset.h>
53789Sahrens #include <sys/dsl_dir.h>
54789Sahrens #include <sys/dsl_prop.h>
553912Slling #include <sys/dsl_synctask.h>
56789Sahrens #include <sys/fs/zfs.h>
575450Sbrendan #include <sys/arc.h>
58789Sahrens #include <sys/callb.h>
593975Sek110237 #include <sys/systeminfo.h>
603975Sek110237 #include <sys/sunddi.h>
616423Sgw25295 #include <sys/spa_boot.h>
62789Sahrens 
635094Slling #include "zfs_prop.h"
645913Sperrin #include "zfs_comutil.h"
655094Slling 
662986Sek110237 int zio_taskq_threads = 8;
672986Sek110237 
685094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
697214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
705094Slling 
715094Slling /*
725094Slling  * ==========================================================================
735094Slling  * SPA properties routines
745094Slling  * ==========================================================================
755094Slling  */
765094Slling 
775094Slling /*
785094Slling  * Add a (source=src, propname=propval) list to an nvlist.
795094Slling  */
805949Slling static void
815094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
825094Slling     uint64_t intval, zprop_source_t src)
835094Slling {
845094Slling 	const char *propname = zpool_prop_to_name(prop);
855094Slling 	nvlist_t *propval;
865949Slling 
875949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
885949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
895949Slling 
905949Slling 	if (strval != NULL)
915949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
925949Slling 	else
935949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
945949Slling 
955949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
965094Slling 	nvlist_free(propval);
975094Slling }
985094Slling 
995094Slling /*
1005094Slling  * Get property values from the spa configuration.
1015094Slling  */
1025949Slling static void
1035094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1045094Slling {
1055094Slling 	uint64_t size = spa_get_space(spa);
1065094Slling 	uint64_t used = spa_get_alloc(spa);
1075094Slling 	uint64_t cap, version;
1085094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1096643Seschrock 	spa_config_dirent_t *dp;
1105094Slling 
1115094Slling 	/*
1125094Slling 	 * readonly properties
1135094Slling 	 */
1145949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src);
1155949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1165949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1175949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
1185094Slling 
1195094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
1205949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1215949Slling 
1225949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1235949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1245949Slling 	    spa->spa_root_vdev->vdev_state, src);
1255094Slling 
1265094Slling 	/*
1275094Slling 	 * settable properties that are not stored in the pool property object.
1285094Slling 	 */
1295094Slling 	version = spa_version(spa);
1305094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1315094Slling 		src = ZPROP_SRC_DEFAULT;
1325094Slling 	else
1335094Slling 		src = ZPROP_SRC_LOCAL;
1345949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1355949Slling 
1365949Slling 	if (spa->spa_root != NULL)
1375949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1385949Slling 		    0, ZPROP_SRC_LOCAL);
1395094Slling 
1406643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1416643Seschrock 		if (dp->scd_path == NULL) {
1425949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1436643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1446643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1455949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1466643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1475363Seschrock 		}
1485363Seschrock 	}
1495094Slling }
1505094Slling 
1515094Slling /*
1525094Slling  * Get zpool property values.
1535094Slling  */
1545094Slling int
1555094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1565094Slling {
1575094Slling 	zap_cursor_t zc;
1585094Slling 	zap_attribute_t za;
1595094Slling 	objset_t *mos = spa->spa_meta_objset;
1605094Slling 	int err;
1615094Slling 
1625949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1635094Slling 
1645094Slling 	/*
1655094Slling 	 * Get properties from the spa config.
1665094Slling 	 */
1675949Slling 	spa_prop_get_config(spa, nvp);
1685094Slling 
1695094Slling 	mutex_enter(&spa->spa_props_lock);
1705094Slling 	/* If no pool property object, no more prop to get. */
1715094Slling 	if (spa->spa_pool_props_object == 0) {
1725094Slling 		mutex_exit(&spa->spa_props_lock);
1735094Slling 		return (0);
1745094Slling 	}
1755094Slling 
1765094Slling 	/*
1775094Slling 	 * Get properties from the MOS pool property object.
1785094Slling 	 */
1795094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1805094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1815094Slling 	    zap_cursor_advance(&zc)) {
1825094Slling 		uint64_t intval = 0;
1835094Slling 		char *strval = NULL;
1845094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
1855094Slling 		zpool_prop_t prop;
1865094Slling 
1875094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
1885094Slling 			continue;
1895094Slling 
1905094Slling 		switch (za.za_integer_length) {
1915094Slling 		case 8:
1925094Slling 			/* integer property */
1935094Slling 			if (za.za_first_integer !=
1945094Slling 			    zpool_prop_default_numeric(prop))
1955094Slling 				src = ZPROP_SRC_LOCAL;
1965094Slling 
1975094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
1985094Slling 				dsl_pool_t *dp;
1995094Slling 				dsl_dataset_t *ds = NULL;
2005094Slling 
2015094Slling 				dp = spa_get_dsl(spa);
2025094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2036689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2046689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2055094Slling 					rw_exit(&dp->dp_config_rwlock);
2065094Slling 					break;
2075094Slling 				}
2085094Slling 
2095094Slling 				strval = kmem_alloc(
2105094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2115094Slling 				    KM_SLEEP);
2125094Slling 				dsl_dataset_name(ds, strval);
2136689Smaybee 				dsl_dataset_rele(ds, FTAG);
2145094Slling 				rw_exit(&dp->dp_config_rwlock);
2155094Slling 			} else {
2165094Slling 				strval = NULL;
2175094Slling 				intval = za.za_first_integer;
2185094Slling 			}
2195094Slling 
2205949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2215094Slling 
2225094Slling 			if (strval != NULL)
2235094Slling 				kmem_free(strval,
2245094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2255094Slling 
2265094Slling 			break;
2275094Slling 
2285094Slling 		case 1:
2295094Slling 			/* string property */
2305094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2315094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2325094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2335094Slling 			if (err) {
2345094Slling 				kmem_free(strval, za.za_num_integers);
2355094Slling 				break;
2365094Slling 			}
2375949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2385094Slling 			kmem_free(strval, za.za_num_integers);
2395094Slling 			break;
2405094Slling 
2415094Slling 		default:
2425094Slling 			break;
2435094Slling 		}
2445094Slling 	}
2455094Slling 	zap_cursor_fini(&zc);
2465094Slling 	mutex_exit(&spa->spa_props_lock);
2475094Slling out:
2485094Slling 	if (err && err != ENOENT) {
2495094Slling 		nvlist_free(*nvp);
2505949Slling 		*nvp = NULL;
2515094Slling 		return (err);
2525094Slling 	}
2535094Slling 
2545094Slling 	return (0);
2555094Slling }
2565094Slling 
2575094Slling /*
2585094Slling  * Validate the given pool properties nvlist and modify the list
2595094Slling  * for the property values to be set.
2605094Slling  */
2615094Slling static int
2625094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2635094Slling {
2645094Slling 	nvpair_t *elem;
2655094Slling 	int error = 0, reset_bootfs = 0;
2665094Slling 	uint64_t objnum;
2675094Slling 
2685094Slling 	elem = NULL;
2695094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2705094Slling 		zpool_prop_t prop;
2715094Slling 		char *propname, *strval;
2725094Slling 		uint64_t intval;
2735094Slling 		objset_t *os;
2745363Seschrock 		char *slash;
2755094Slling 
2765094Slling 		propname = nvpair_name(elem);
2775094Slling 
2785094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2795094Slling 			return (EINVAL);
2805094Slling 
2815094Slling 		switch (prop) {
2825094Slling 		case ZPOOL_PROP_VERSION:
2835094Slling 			error = nvpair_value_uint64(elem, &intval);
2845094Slling 			if (!error &&
2855094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
2865094Slling 				error = EINVAL;
2875094Slling 			break;
2885094Slling 
2895094Slling 		case ZPOOL_PROP_DELEGATION:
2905094Slling 		case ZPOOL_PROP_AUTOREPLACE:
2915094Slling 			error = nvpair_value_uint64(elem, &intval);
2925094Slling 			if (!error && intval > 1)
2935094Slling 				error = EINVAL;
2945094Slling 			break;
2955094Slling 
2965094Slling 		case ZPOOL_PROP_BOOTFS:
2975094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
2985094Slling 				error = ENOTSUP;
2995094Slling 				break;
3005094Slling 			}
3015094Slling 
3025094Slling 			/*
3037042Sgw25295 			 * Make sure the vdev config is bootable
3045094Slling 			 */
3057042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3065094Slling 				error = ENOTSUP;
3075094Slling 				break;
3085094Slling 			}
3095094Slling 
3105094Slling 			reset_bootfs = 1;
3115094Slling 
3125094Slling 			error = nvpair_value_string(elem, &strval);
3135094Slling 
3145094Slling 			if (!error) {
3157042Sgw25295 				uint64_t compress;
3167042Sgw25295 
3175094Slling 				if (strval == NULL || strval[0] == '\0') {
3185094Slling 					objnum = zpool_prop_default_numeric(
3195094Slling 					    ZPOOL_PROP_BOOTFS);
3205094Slling 					break;
3215094Slling 				}
3225094Slling 
3235094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
3246689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3255094Slling 					break;
3267042Sgw25295 
3277042Sgw25295 				/* We don't support gzip bootable datasets */
3287042Sgw25295 				if ((error = dsl_prop_get_integer(strval,
3297042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
3307042Sgw25295 				    &compress, NULL)) == 0 &&
3317042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
3327042Sgw25295 					error = ENOTSUP;
3337042Sgw25295 				} else {
3347042Sgw25295 					objnum = dmu_objset_id(os);
3357042Sgw25295 				}
3365094Slling 				dmu_objset_close(os);
3375094Slling 			}
3385094Slling 			break;
3395329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3405329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3415329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3425329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3435329Sgw25295 				error = EINVAL;
3445329Sgw25295 
3455329Sgw25295 			/*
3465329Sgw25295 			 * This is a special case which only occurs when
3475329Sgw25295 			 * the pool has completely failed. This allows
3485329Sgw25295 			 * the user to change the in-core failmode property
3495329Sgw25295 			 * without syncing it out to disk (I/Os might
3505329Sgw25295 			 * currently be blocked). We do this by returning
3515329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3525329Sgw25295 			 * into thinking we encountered a property validation
3535329Sgw25295 			 * error.
3545329Sgw25295 			 */
3555329Sgw25295 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3565329Sgw25295 				spa->spa_failmode = intval;
3575329Sgw25295 				error = EIO;
3585329Sgw25295 			}
3595329Sgw25295 			break;
3605363Seschrock 
3615363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3625363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3635363Seschrock 				break;
3645363Seschrock 
3655363Seschrock 			if (strval[0] == '\0')
3665363Seschrock 				break;
3675363Seschrock 
3685363Seschrock 			if (strcmp(strval, "none") == 0)
3695363Seschrock 				break;
3705363Seschrock 
3715363Seschrock 			if (strval[0] != '/') {
3725363Seschrock 				error = EINVAL;
3735363Seschrock 				break;
3745363Seschrock 			}
3755363Seschrock 
3765363Seschrock 			slash = strrchr(strval, '/');
3775363Seschrock 			ASSERT(slash != NULL);
3785363Seschrock 
3795363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3805363Seschrock 			    strcmp(slash, "/..") == 0)
3815363Seschrock 				error = EINVAL;
3825363Seschrock 			break;
3835094Slling 		}
3845094Slling 
3855094Slling 		if (error)
3865094Slling 			break;
3875094Slling 	}
3885094Slling 
3895094Slling 	if (!error && reset_bootfs) {
3905094Slling 		error = nvlist_remove(props,
3915094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
3925094Slling 
3935094Slling 		if (!error) {
3945094Slling 			error = nvlist_add_uint64(props,
3955094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
3965094Slling 		}
3975094Slling 	}
3985094Slling 
3995094Slling 	return (error);
4005094Slling }
4015094Slling 
4025094Slling int
4035094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4045094Slling {
4055094Slling 	int error;
4065094Slling 
4075094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4085094Slling 		return (error);
4095094Slling 
4105094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4115094Slling 	    spa, nvp, 3));
4125094Slling }
4135094Slling 
4145094Slling /*
4155094Slling  * If the bootfs property value is dsobj, clear it.
4165094Slling  */
4175094Slling void
4185094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4195094Slling {
4205094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4215094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4225094Slling 		    spa->spa_pool_props_object,
4235094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4245094Slling 		spa->spa_bootfs = 0;
4255094Slling 	}
4265094Slling }
4275094Slling 
428789Sahrens /*
429789Sahrens  * ==========================================================================
430789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
431789Sahrens  * ==========================================================================
432789Sahrens  */
433789Sahrens 
4341544Seschrock static int
4351544Seschrock spa_error_entry_compare(const void *a, const void *b)
4361544Seschrock {
4371544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4381544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4391544Seschrock 	int ret;
4401544Seschrock 
4411544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4421544Seschrock 	    sizeof (zbookmark_t));
4431544Seschrock 
4441544Seschrock 	if (ret < 0)
4451544Seschrock 		return (-1);
4461544Seschrock 	else if (ret > 0)
4471544Seschrock 		return (1);
4481544Seschrock 	else
4491544Seschrock 		return (0);
4501544Seschrock }
4511544Seschrock 
4521544Seschrock /*
4531544Seschrock  * Utility function which retrieves copies of the current logs and
4541544Seschrock  * re-initializes them in the process.
4551544Seschrock  */
4561544Seschrock void
4571544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4581544Seschrock {
4591544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4601544Seschrock 
4611544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4621544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4631544Seschrock 
4641544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4651544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4661544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4671544Seschrock 	avl_create(&spa->spa_errlist_last,
4681544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4691544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4701544Seschrock }
4711544Seschrock 
472789Sahrens /*
473789Sahrens  * Activate an uninitialized pool.
474789Sahrens  */
475789Sahrens static void
476789Sahrens spa_activate(spa_t *spa)
477789Sahrens {
478789Sahrens 	int t;
479789Sahrens 
480789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
481789Sahrens 
482789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
483789Sahrens 
484789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4854527Sperrin 	spa->spa_log_class = metaslab_class_create();
486789Sahrens 
487789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
488789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
4892986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
490789Sahrens 		    TASKQ_PREPOPULATE);
491789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
4922986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
493789Sahrens 		    TASKQ_PREPOPULATE);
494789Sahrens 	}
495789Sahrens 
496789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
497789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
4985329Sgw25295 	list_create(&spa->spa_zio_list, sizeof (zio_t),
4995329Sgw25295 	    offsetof(zio_t, zio_link_node));
500789Sahrens 
501789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
502789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
5031544Seschrock 
5041544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5051544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5061544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5071544Seschrock 	avl_create(&spa->spa_errlist_last,
5081544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5091544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
510789Sahrens }
511789Sahrens 
512789Sahrens /*
513789Sahrens  * Opposite of spa_activate().
514789Sahrens  */
515789Sahrens static void
516789Sahrens spa_deactivate(spa_t *spa)
517789Sahrens {
518789Sahrens 	int t;
519789Sahrens 
520789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
521789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
522789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
523789Sahrens 
524789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
525789Sahrens 
526789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
527789Sahrens 
528789Sahrens 	list_destroy(&spa->spa_dirty_list);
5295329Sgw25295 	list_destroy(&spa->spa_zio_list);
530789Sahrens 
531789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
532789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
533789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
534789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
535789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
536789Sahrens 	}
537789Sahrens 
538789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
539789Sahrens 	spa->spa_normal_class = NULL;
540789Sahrens 
5414527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5424527Sperrin 	spa->spa_log_class = NULL;
5434527Sperrin 
5441544Seschrock 	/*
5451544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5461544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5471544Seschrock 	 */
5481544Seschrock 	spa_errlog_drain(spa);
5491544Seschrock 
5501544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5511544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5521544Seschrock 
553789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
554789Sahrens }
555789Sahrens 
556789Sahrens /*
557789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
558789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
559789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
560789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
561789Sahrens  */
5622082Seschrock static int
5632082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5642082Seschrock     uint_t id, int atype)
565789Sahrens {
566789Sahrens 	nvlist_t **child;
567789Sahrens 	uint_t c, children;
5682082Seschrock 	int error;
5692082Seschrock 
5702082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5712082Seschrock 		return (error);
5722082Seschrock 
5732082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5742082Seschrock 		return (0);
575789Sahrens 
576789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
577789Sahrens 	    &child, &children) != 0) {
5782082Seschrock 		vdev_free(*vdp);
5792082Seschrock 		*vdp = NULL;
5802082Seschrock 		return (EINVAL);
581789Sahrens 	}
582789Sahrens 
583789Sahrens 	for (c = 0; c < children; c++) {
5842082Seschrock 		vdev_t *vd;
5852082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5862082Seschrock 		    atype)) != 0) {
5872082Seschrock 			vdev_free(*vdp);
5882082Seschrock 			*vdp = NULL;
5892082Seschrock 			return (error);
590789Sahrens 		}
591789Sahrens 	}
592789Sahrens 
5932082Seschrock 	ASSERT(*vdp != NULL);
5942082Seschrock 
5952082Seschrock 	return (0);
596789Sahrens }
597789Sahrens 
598789Sahrens /*
599789Sahrens  * Opposite of spa_load().
600789Sahrens  */
601789Sahrens static void
602789Sahrens spa_unload(spa_t *spa)
603789Sahrens {
6042082Seschrock 	int i;
6052082Seschrock 
606789Sahrens 	/*
6071544Seschrock 	 * Stop async tasks.
6081544Seschrock 	 */
6091544Seschrock 	spa_async_suspend(spa);
6101544Seschrock 
6111544Seschrock 	/*
612789Sahrens 	 * Stop syncing.
613789Sahrens 	 */
614789Sahrens 	if (spa->spa_sync_on) {
615789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
616789Sahrens 		spa->spa_sync_on = B_FALSE;
617789Sahrens 	}
618789Sahrens 
619789Sahrens 	/*
620789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
621789Sahrens 	 */
6221544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
6231544Seschrock 	spa_config_exit(spa, FTAG);
624789Sahrens 
625789Sahrens 	/*
6265450Sbrendan 	 * Drop and purge level 2 cache
6275450Sbrendan 	 */
6285450Sbrendan 	spa_l2cache_drop(spa);
6295450Sbrendan 
6305450Sbrendan 	/*
631789Sahrens 	 * Close the dsl pool.
632789Sahrens 	 */
633789Sahrens 	if (spa->spa_dsl_pool) {
634789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
635789Sahrens 		spa->spa_dsl_pool = NULL;
636789Sahrens 	}
637789Sahrens 
638789Sahrens 	/*
639789Sahrens 	 * Close all vdevs.
640789Sahrens 	 */
6411585Sbonwick 	if (spa->spa_root_vdev)
642789Sahrens 		vdev_free(spa->spa_root_vdev);
6431585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6441544Seschrock 
6455450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
6465450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
6475450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
6485450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
6495450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
6505450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
6515450Sbrendan 	}
6525450Sbrendan 	if (spa->spa_spares.sav_config) {
6535450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
6545450Sbrendan 		spa->spa_spares.sav_config = NULL;
6552082Seschrock 	}
6565450Sbrendan 
6575450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
6585450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
6595450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
6605450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
6615450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
6625450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
6635450Sbrendan 	}
6645450Sbrendan 	if (spa->spa_l2cache.sav_config) {
6655450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
6665450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
6672082Seschrock 	}
6682082Seschrock 
6691544Seschrock 	spa->spa_async_suspended = 0;
670789Sahrens }
671789Sahrens 
672789Sahrens /*
6732082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6742082Seschrock  * this pool.  When this is called, we have some form of basic information in
6755450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
6765450Sbrendan  * then re-generate a more complete list including status information.
6772082Seschrock  */
6782082Seschrock static void
6792082Seschrock spa_load_spares(spa_t *spa)
6802082Seschrock {
6812082Seschrock 	nvlist_t **spares;
6822082Seschrock 	uint_t nspares;
6832082Seschrock 	int i;
6843377Seschrock 	vdev_t *vd, *tvd;
6852082Seschrock 
6862082Seschrock 	/*
6872082Seschrock 	 * First, close and free any existing spare vdevs.
6882082Seschrock 	 */
6895450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
6905450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
6913377Seschrock 
6923377Seschrock 		/* Undo the call to spa_activate() below */
6936643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
6946643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
6953377Seschrock 			spa_spare_remove(tvd);
6963377Seschrock 		vdev_close(vd);
6973377Seschrock 		vdev_free(vd);
6982082Seschrock 	}
6993377Seschrock 
7005450Sbrendan 	if (spa->spa_spares.sav_vdevs)
7015450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7025450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7035450Sbrendan 
7045450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7052082Seschrock 		nspares = 0;
7062082Seschrock 	else
7075450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7082082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7092082Seschrock 
7105450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7115450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7122082Seschrock 
7132082Seschrock 	if (nspares == 0)
7142082Seschrock 		return;
7152082Seschrock 
7162082Seschrock 	/*
7172082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7183377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7193377Seschrock 	 * structures associated with it: one in the list of spares (used only
7203377Seschrock 	 * for basic validation purposes) and one in the active vdev
7213377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7223377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7233377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7242082Seschrock 	 */
7255450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
7265450Sbrendan 	    KM_SLEEP);
7275450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7282082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
7292082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
7302082Seschrock 		ASSERT(vd != NULL);
7312082Seschrock 
7325450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
7332082Seschrock 
7346643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7356643Seschrock 		    B_FALSE)) != NULL) {
7363377Seschrock 			if (!tvd->vdev_isspare)
7373377Seschrock 				spa_spare_add(tvd);
7383377Seschrock 
7393377Seschrock 			/*
7403377Seschrock 			 * We only mark the spare active if we were successfully
7413377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7423377Seschrock 			 * with a bad active spare would result in strange
7433377Seschrock 			 * behavior, because multiple pool would think the spare
7443377Seschrock 			 * is actively in use.
7453377Seschrock 			 *
7463377Seschrock 			 * There is a vulnerability here to an equally bizarre
7473377Seschrock 			 * circumstance, where a dead active spare is later
7483377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7493377Seschrock 			 * the rarity of this scenario, and the extra complexity
7503377Seschrock 			 * it adds, we ignore the possibility.
7513377Seschrock 			 */
7523377Seschrock 			if (!vdev_is_dead(tvd))
7533377Seschrock 				spa_spare_activate(tvd);
7543377Seschrock 		}
7553377Seschrock 
7562082Seschrock 		if (vdev_open(vd) != 0)
7572082Seschrock 			continue;
7582082Seschrock 
7592082Seschrock 		vd->vdev_top = vd;
7605450Sbrendan 		if (vdev_validate_aux(vd) == 0)
7615450Sbrendan 			spa_spare_add(vd);
7622082Seschrock 	}
7632082Seschrock 
7642082Seschrock 	/*
7652082Seschrock 	 * Recompute the stashed list of spares, with status information
7662082Seschrock 	 * this time.
7672082Seschrock 	 */
7685450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
7692082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7702082Seschrock 
7715450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
7725450Sbrendan 	    KM_SLEEP);
7735450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7745450Sbrendan 		spares[i] = vdev_config_generate(spa,
7755450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
7765450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
7775450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
7785450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7792082Seschrock 		nvlist_free(spares[i]);
7805450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
7815450Sbrendan }
7825450Sbrendan 
7835450Sbrendan /*
7845450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
7855450Sbrendan  * this pool.  When this is called, we have some form of basic information in
7865450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
7875450Sbrendan  * then re-generate a more complete list including status information.
7885450Sbrendan  * Devices which are already active have their details maintained, and are
7895450Sbrendan  * not re-opened.
7905450Sbrendan  */
7915450Sbrendan static void
7925450Sbrendan spa_load_l2cache(spa_t *spa)
7935450Sbrendan {
7945450Sbrendan 	nvlist_t **l2cache;
7955450Sbrendan 	uint_t nl2cache;
7965450Sbrendan 	int i, j, oldnvdevs;
7976643Seschrock 	uint64_t guid, size;
7985450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
7995450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
8005450Sbrendan 
8015450Sbrendan 	if (sav->sav_config != NULL) {
8025450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8035450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8045450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8055450Sbrendan 	} else {
8065450Sbrendan 		nl2cache = 0;
8075450Sbrendan 	}
8085450Sbrendan 
8095450Sbrendan 	oldvdevs = sav->sav_vdevs;
8105450Sbrendan 	oldnvdevs = sav->sav_count;
8115450Sbrendan 	sav->sav_vdevs = NULL;
8125450Sbrendan 	sav->sav_count = 0;
8135450Sbrendan 
8145450Sbrendan 	/*
8155450Sbrendan 	 * Process new nvlist of vdevs.
8165450Sbrendan 	 */
8175450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8185450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8195450Sbrendan 		    &guid) == 0);
8205450Sbrendan 
8215450Sbrendan 		newvdevs[i] = NULL;
8225450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
8235450Sbrendan 			vd = oldvdevs[j];
8245450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
8255450Sbrendan 				/*
8265450Sbrendan 				 * Retain previous vdev for add/remove ops.
8275450Sbrendan 				 */
8285450Sbrendan 				newvdevs[i] = vd;
8295450Sbrendan 				oldvdevs[j] = NULL;
8305450Sbrendan 				break;
8315450Sbrendan 			}
8325450Sbrendan 		}
8335450Sbrendan 
8345450Sbrendan 		if (newvdevs[i] == NULL) {
8355450Sbrendan 			/*
8365450Sbrendan 			 * Create new vdev
8375450Sbrendan 			 */
8385450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
8395450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
8405450Sbrendan 			ASSERT(vd != NULL);
8415450Sbrendan 			newvdevs[i] = vd;
8425450Sbrendan 
8435450Sbrendan 			/*
8445450Sbrendan 			 * Commit this vdev as an l2cache device,
8455450Sbrendan 			 * even if it fails to open.
8465450Sbrendan 			 */
8475450Sbrendan 			spa_l2cache_add(vd);
8485450Sbrendan 
8496643Seschrock 			vd->vdev_top = vd;
8506643Seschrock 			vd->vdev_aux = sav;
8516643Seschrock 
8526643Seschrock 			spa_l2cache_activate(vd);
8536643Seschrock 
8545450Sbrendan 			if (vdev_open(vd) != 0)
8555450Sbrendan 				continue;
8565450Sbrendan 
8575450Sbrendan 			(void) vdev_validate_aux(vd);
8585450Sbrendan 
8595450Sbrendan 			if (!vdev_is_dead(vd)) {
8605450Sbrendan 				size = vdev_get_rsize(vd);
8616643Seschrock 				l2arc_add_vdev(spa, vd,
8626643Seschrock 				    VDEV_LABEL_START_SIZE,
8636643Seschrock 				    size - VDEV_LABEL_START_SIZE);
8645450Sbrendan 			}
8655450Sbrendan 		}
8665450Sbrendan 	}
8675450Sbrendan 
8685450Sbrendan 	/*
8695450Sbrendan 	 * Purge vdevs that were dropped
8705450Sbrendan 	 */
8715450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
8725450Sbrendan 		uint64_t pool;
8735450Sbrendan 
8745450Sbrendan 		vd = oldvdevs[i];
8755450Sbrendan 		if (vd != NULL) {
8765450Sbrendan 			if (spa_mode & FWRITE &&
8775450Sbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
8786643Seschrock 			    pool != 0ULL &&
8796643Seschrock 			    l2arc_vdev_present(vd)) {
8805450Sbrendan 				l2arc_remove_vdev(vd);
8815450Sbrendan 			}
8825450Sbrendan 			(void) vdev_close(vd);
8835450Sbrendan 			spa_l2cache_remove(vd);
8845450Sbrendan 		}
8855450Sbrendan 	}
8865450Sbrendan 
8875450Sbrendan 	if (oldvdevs)
8885450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
8895450Sbrendan 
8905450Sbrendan 	if (sav->sav_config == NULL)
8915450Sbrendan 		goto out;
8925450Sbrendan 
8935450Sbrendan 	sav->sav_vdevs = newvdevs;
8945450Sbrendan 	sav->sav_count = (int)nl2cache;
8955450Sbrendan 
8965450Sbrendan 	/*
8975450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
8985450Sbrendan 	 * information this time.
8995450Sbrendan 	 */
9005450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
9015450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9025450Sbrendan 
9035450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9045450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9055450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9065450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9075450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9085450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9095450Sbrendan out:
9105450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9115450Sbrendan 		nvlist_free(l2cache[i]);
9125450Sbrendan 	if (sav->sav_count)
9135450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9142082Seschrock }
9152082Seschrock 
9162082Seschrock static int
9172082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9182082Seschrock {
9192082Seschrock 	dmu_buf_t *db;
9202082Seschrock 	char *packed = NULL;
9212082Seschrock 	size_t nvsize = 0;
9222082Seschrock 	int error;
9232082Seschrock 	*value = NULL;
9242082Seschrock 
9252082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9262082Seschrock 	nvsize = *(uint64_t *)db->db_data;
9272082Seschrock 	dmu_buf_rele(db, FTAG);
9282082Seschrock 
9292082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
9302082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
9312082Seschrock 	if (error == 0)
9322082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
9332082Seschrock 	kmem_free(packed, nvsize);
9342082Seschrock 
9352082Seschrock 	return (error);
9362082Seschrock }
9372082Seschrock 
9382082Seschrock /*
9394451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9404451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9414451Seschrock  */
9424451Seschrock static void
9434451Seschrock spa_check_removed(vdev_t *vd)
9444451Seschrock {
9454451Seschrock 	int c;
9464451Seschrock 
9474451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9484451Seschrock 		spa_check_removed(vd->vdev_child[c]);
9494451Seschrock 
9504451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9514451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9524451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9534451Seschrock 	}
9544451Seschrock }
9554451Seschrock 
9564451Seschrock /*
9577294Sperrin  * Check for missing log devices
9587294Sperrin  */
9597294Sperrin int
9607294Sperrin spa_check_logs(spa_t *spa)
9617294Sperrin {
9627294Sperrin 	switch (spa->spa_log_state) {
9637294Sperrin 	case SPA_LOG_MISSING:
9647294Sperrin 		/* need to recheck in case slog has been restored */
9657294Sperrin 	case SPA_LOG_UNKNOWN:
9667294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
9677294Sperrin 		    DS_FIND_CHILDREN)) {
9687294Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
9697294Sperrin 			return (1);
9707294Sperrin 		}
9717294Sperrin 		break;
9727294Sperrin 
9737294Sperrin 	case SPA_LOG_CLEAR:
9747294Sperrin 		(void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
9757294Sperrin 		    DS_FIND_CHILDREN);
9767294Sperrin 		break;
9777294Sperrin 	}
9787294Sperrin 	spa->spa_log_state = SPA_LOG_GOOD;
9797294Sperrin 	return (0);
9807294Sperrin }
9817294Sperrin 
9827294Sperrin /*
983789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
9841544Seschrock  * source of configuration information.
985789Sahrens  */
986789Sahrens static int
9871544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
988789Sahrens {
989789Sahrens 	int error = 0;
990789Sahrens 	nvlist_t *nvroot = NULL;
991789Sahrens 	vdev_t *rvd;
992789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
9931635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
994789Sahrens 	uint64_t pool_guid;
9952082Seschrock 	uint64_t version;
996789Sahrens 	zio_t *zio;
9974451Seschrock 	uint64_t autoreplace = 0;
9987294Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
999789Sahrens 
10001544Seschrock 	spa->spa_load_state = state;
10011635Sbonwick 
1002789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
10031733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
10041544Seschrock 		error = EINVAL;
10051544Seschrock 		goto out;
10061544Seschrock 	}
1007789Sahrens 
10082082Seschrock 	/*
10092082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
10102082Seschrock 	 * it's not present treat it as the initial version.
10112082Seschrock 	 */
10122082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
10134577Sahrens 		version = SPA_VERSION_INITIAL;
10142082Seschrock 
10151733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
10161733Sbonwick 	    &spa->spa_config_txg);
10171733Sbonwick 
10181635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
10191544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
10201544Seschrock 		error = EEXIST;
10211544Seschrock 		goto out;
10221544Seschrock 	}
1023789Sahrens 
10242174Seschrock 	spa->spa_load_guid = pool_guid;
10252174Seschrock 
1026789Sahrens 	/*
10272082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
10282082Seschrock 	 * value that will be returned by spa_version() since parsing the
10292082Seschrock 	 * configuration requires knowing the version number.
1030789Sahrens 	 */
10311544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
10322082Seschrock 	spa->spa_ubsync.ub_version = version;
10332082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
10341544Seschrock 	spa_config_exit(spa, FTAG);
1035789Sahrens 
10362082Seschrock 	if (error != 0)
10371544Seschrock 		goto out;
1038789Sahrens 
10391585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1040789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1041789Sahrens 
1042789Sahrens 	/*
1043789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1044789Sahrens 	 */
10454070Smc142369 	error = vdev_open(rvd);
10464070Smc142369 	if (error != 0)
10471544Seschrock 		goto out;
1048789Sahrens 
1049789Sahrens 	/*
10501986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
10511986Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
10521986Seschrock 	 * flag.
10531986Seschrock 	 */
10541986Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
10551986Seschrock 	error = vdev_validate(rvd);
10561986Seschrock 	spa_config_exit(spa, FTAG);
10571986Seschrock 
10584070Smc142369 	if (error != 0)
10591986Seschrock 		goto out;
10601986Seschrock 
10611986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
10621986Seschrock 		error = ENXIO;
10631986Seschrock 		goto out;
10641986Seschrock 	}
10651986Seschrock 
10661986Seschrock 	/*
1067789Sahrens 	 * Find the best uberblock.
1068789Sahrens 	 */
1069789Sahrens 	bzero(ub, sizeof (uberblock_t));
1070789Sahrens 
1071789Sahrens 	zio = zio_root(spa, NULL, NULL,
1072789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1073789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
1074789Sahrens 	error = zio_wait(zio);
1075789Sahrens 
1076789Sahrens 	/*
1077789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1078789Sahrens 	 */
1079789Sahrens 	if (ub->ub_txg == 0) {
10801760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10811760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10821544Seschrock 		error = ENXIO;
10831544Seschrock 		goto out;
10841544Seschrock 	}
10851544Seschrock 
10861544Seschrock 	/*
10871544Seschrock 	 * If the pool is newer than the code, we can't open it.
10881544Seschrock 	 */
10894577Sahrens 	if (ub->ub_version > SPA_VERSION) {
10901760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10911760Seschrock 		    VDEV_AUX_VERSION_NEWER);
10921544Seschrock 		error = ENOTSUP;
10931544Seschrock 		goto out;
1094789Sahrens 	}
1095789Sahrens 
1096789Sahrens 	/*
1097789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1098789Sahrens 	 * incomplete configuration.
1099789Sahrens 	 */
11001732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
11011544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11021544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
11031544Seschrock 		error = ENXIO;
11041544Seschrock 		goto out;
1105789Sahrens 	}
1106789Sahrens 
1107789Sahrens 	/*
1108789Sahrens 	 * Initialize internal SPA structures.
1109789Sahrens 	 */
1110789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1111789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1112789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
11131544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
11141544Seschrock 	if (error) {
11151544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11161544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11171544Seschrock 		goto out;
11181544Seschrock 	}
1119789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1120789Sahrens 
11211544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1122789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
11231544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
11241544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11251544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11261544Seschrock 		error = EIO;
11271544Seschrock 		goto out;
11281544Seschrock 	}
1129789Sahrens 
1130789Sahrens 	if (!mosconfig) {
11312082Seschrock 		nvlist_t *newconfig;
11323975Sek110237 		uint64_t hostid;
11332082Seschrock 
11342082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
11351544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11361544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11371544Seschrock 			error = EIO;
11381544Seschrock 			goto out;
11391544Seschrock 		}
1140789Sahrens 
11413975Sek110237 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
11423975Sek110237 		    &hostid) == 0) {
11433975Sek110237 			char *hostname;
11443975Sek110237 			unsigned long myhostid = 0;
11453975Sek110237 
11463975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
11473975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
11483975Sek110237 
11493975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
11504178Slling 			if (hostid != 0 && myhostid != 0 &&
11514178Slling 			    (unsigned long)hostid != myhostid) {
11523975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
11533975Sek110237 				    "loaded as it was last accessed by "
11543975Sek110237 				    "another system (host: %s hostid: 0x%lx).  "
11553975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
11563975Sek110237 				    spa->spa_name, hostname,
11573975Sek110237 				    (unsigned long)hostid);
11583975Sek110237 				error = EBADF;
11593975Sek110237 				goto out;
11603975Sek110237 			}
11613975Sek110237 		}
11623975Sek110237 
1163789Sahrens 		spa_config_set(spa, newconfig);
1164789Sahrens 		spa_unload(spa);
1165789Sahrens 		spa_deactivate(spa);
1166789Sahrens 		spa_activate(spa);
1167789Sahrens 
11681544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
11691544Seschrock 	}
11701544Seschrock 
11711544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
11721544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
11731544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
11741544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11751544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11761544Seschrock 		error = EIO;
11771544Seschrock 		goto out;
1178789Sahrens 	}
1179789Sahrens 
11801544Seschrock 	/*
11812082Seschrock 	 * Load the bit that tells us to use the new accounting function
11822082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
11832082Seschrock 	 * be present.
11842082Seschrock 	 */
11852082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11862082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
11872082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
11882082Seschrock 	if (error != 0 && error != ENOENT) {
11892082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11902082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11912082Seschrock 		error = EIO;
11922082Seschrock 		goto out;
11932082Seschrock 	}
11942082Seschrock 
11952082Seschrock 	/*
11961544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
11971544Seschrock 	 * not be present.
11981544Seschrock 	 */
11991544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12001544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
12011544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
12021807Sbonwick 	if (error != 0 && error != ENOENT) {
12031544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12041544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12051544Seschrock 		error = EIO;
12061544Seschrock 		goto out;
12071544Seschrock 	}
12081544Seschrock 
12091544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
12101544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
12111544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
12121544Seschrock 	if (error != 0 && error != ENOENT) {
12131544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12141544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12151544Seschrock 		error = EIO;
12161544Seschrock 		goto out;
12171544Seschrock 	}
1218789Sahrens 
1219789Sahrens 	/*
12202926Sek110237 	 * Load the history object.  If we have an older pool, this
12212926Sek110237 	 * will not be present.
12222926Sek110237 	 */
12232926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
12242926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
12252926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
12262926Sek110237 	if (error != 0 && error != ENOENT) {
12272926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12282926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
12292926Sek110237 		error = EIO;
12302926Sek110237 		goto out;
12312926Sek110237 	}
12322926Sek110237 
12332926Sek110237 	/*
12342082Seschrock 	 * Load any hot spares for this pool.
12352082Seschrock 	 */
12362082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12375450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
12382082Seschrock 	if (error != 0 && error != ENOENT) {
12392082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12402082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12412082Seschrock 		error = EIO;
12422082Seschrock 		goto out;
12432082Seschrock 	}
12442082Seschrock 	if (error == 0) {
12454577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
12465450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
12475450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
12482082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12492082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12502082Seschrock 			error = EIO;
12512082Seschrock 			goto out;
12522082Seschrock 		}
12532082Seschrock 
12542082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
12552082Seschrock 		spa_load_spares(spa);
12562082Seschrock 		spa_config_exit(spa, FTAG);
12572082Seschrock 	}
12582082Seschrock 
12595450Sbrendan 	/*
12605450Sbrendan 	 * Load any level 2 ARC devices for this pool.
12615450Sbrendan 	 */
12625450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12635450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
12645450Sbrendan 	    &spa->spa_l2cache.sav_object);
12655450Sbrendan 	if (error != 0 && error != ENOENT) {
12665450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12675450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
12685450Sbrendan 		error = EIO;
12695450Sbrendan 		goto out;
12705450Sbrendan 	}
12715450Sbrendan 	if (error == 0) {
12725450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
12735450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
12745450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
12755450Sbrendan 			vdev_set_state(rvd, B_TRUE,
12765450Sbrendan 			    VDEV_STATE_CANT_OPEN,
12775450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
12785450Sbrendan 			error = EIO;
12795450Sbrendan 			goto out;
12805450Sbrendan 		}
12815450Sbrendan 
12825450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
12835450Sbrendan 		spa_load_l2cache(spa);
12845450Sbrendan 		spa_config_exit(spa, FTAG);
12855450Sbrendan 	}
12865450Sbrendan 
12877294Sperrin 	if (spa_check_logs(spa)) {
12887294Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12897294Sperrin 		    VDEV_AUX_BAD_LOG);
12907294Sperrin 		error = ENXIO;
12917294Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
12927294Sperrin 		goto out;
12937294Sperrin 	}
12947294Sperrin 
12957294Sperrin 
12965094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
12974543Smarks 
12983912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12993912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
13003912Slling 
13013912Slling 	if (error && error != ENOENT) {
13023912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
13033912Slling 		    VDEV_AUX_CORRUPT_DATA);
13043912Slling 		error = EIO;
13053912Slling 		goto out;
13063912Slling 	}
13073912Slling 
13083912Slling 	if (error == 0) {
13093912Slling 		(void) zap_lookup(spa->spa_meta_objset,
13103912Slling 		    spa->spa_pool_props_object,
13114451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
13123912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
13134451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
13144451Seschrock 		    spa->spa_pool_props_object,
13154451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
13164451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
13174543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
13184543Smarks 		    spa->spa_pool_props_object,
13194543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
13204543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
13215329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
13225329Sgw25295 		    spa->spa_pool_props_object,
13235329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
13245329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
13253912Slling 	}
13263912Slling 
13272082Seschrock 	/*
13284451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
13294451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
13304451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
13314451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
13324451Seschrock 	 * over.
13334451Seschrock 	 */
13345756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
13354451Seschrock 		spa_check_removed(spa->spa_root_vdev);
13364451Seschrock 
13374451Seschrock 	/*
13381986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1339789Sahrens 	 */
13401986Seschrock 	vdev_load(rvd);
1341789Sahrens 
1342789Sahrens 	/*
1343789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1344789Sahrens 	 */
13451544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1346789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
13471544Seschrock 	spa_config_exit(spa, FTAG);
1348789Sahrens 
1349789Sahrens 	/*
1350789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1351789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1352789Sahrens 	 */
13531544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13541544Seschrock 		error = ENXIO;
13551544Seschrock 		goto out;
13561544Seschrock 	}
1357789Sahrens 
13581544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13591635Sbonwick 		dmu_tx_t *tx;
13601635Sbonwick 		int need_update = B_FALSE;
13611585Sbonwick 		int c;
13621601Sbonwick 
13631635Sbonwick 		/*
13641635Sbonwick 		 * Claim log blocks that haven't been committed yet.
13651635Sbonwick 		 * This must all happen in a single txg.
13661635Sbonwick 		 */
13671601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1368789Sahrens 		    spa_first_txg(spa));
13692417Sahrens 		(void) dmu_objset_find(spa->spa_name,
13702417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1371789Sahrens 		dmu_tx_commit(tx);
1372789Sahrens 
1373789Sahrens 		spa->spa_sync_on = B_TRUE;
1374789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1375789Sahrens 
1376789Sahrens 		/*
1377789Sahrens 		 * Wait for all claims to sync.
1378789Sahrens 		 */
1379789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
13801585Sbonwick 
13811585Sbonwick 		/*
13821635Sbonwick 		 * If the config cache is stale, or we have uninitialized
13831635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
13841585Sbonwick 		 */
13851635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
13861635Sbonwick 		    state == SPA_LOAD_IMPORT)
13871635Sbonwick 			need_update = B_TRUE;
13881635Sbonwick 
13891635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
13901635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
13911635Sbonwick 				need_update = B_TRUE;
13921585Sbonwick 
13931585Sbonwick 		/*
13941635Sbonwick 		 * Update the config cache asychronously in case we're the
13951635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
13961585Sbonwick 		 */
13971635Sbonwick 		if (need_update)
13981635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1399789Sahrens 	}
1400789Sahrens 
14011544Seschrock 	error = 0;
14021544Seschrock out:
14037046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
14042082Seschrock 	if (error && error != EBADF)
14057294Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
14061544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
14071544Seschrock 	spa->spa_ena = 0;
14081544Seschrock 
14091544Seschrock 	return (error);
1410789Sahrens }
1411789Sahrens 
1412789Sahrens /*
1413789Sahrens  * Pool Open/Import
1414789Sahrens  *
1415789Sahrens  * The import case is identical to an open except that the configuration is sent
1416789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1417789Sahrens  * case of an open, the pool configuration will exist in the
14184451Seschrock  * POOL_STATE_UNINITIALIZED state.
1419789Sahrens  *
1420789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1421789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1422789Sahrens  * ambiguous state.
1423789Sahrens  */
1424789Sahrens static int
1425789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1426789Sahrens {
1427789Sahrens 	spa_t *spa;
1428789Sahrens 	int error;
1429789Sahrens 	int locked = B_FALSE;
1430789Sahrens 
1431789Sahrens 	*spapp = NULL;
1432789Sahrens 
1433789Sahrens 	/*
1434789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1435789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1436789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1437789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1438789Sahrens 	 */
1439789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1440789Sahrens 		mutex_enter(&spa_namespace_lock);
1441789Sahrens 		locked = B_TRUE;
1442789Sahrens 	}
1443789Sahrens 
1444789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1445789Sahrens 		if (locked)
1446789Sahrens 			mutex_exit(&spa_namespace_lock);
1447789Sahrens 		return (ENOENT);
1448789Sahrens 	}
1449789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1450789Sahrens 
1451789Sahrens 		spa_activate(spa);
1452789Sahrens 
14531635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1454789Sahrens 
1455789Sahrens 		if (error == EBADF) {
1456789Sahrens 			/*
14571986Seschrock 			 * If vdev_validate() returns failure (indicated by
14581986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
14591986Seschrock 			 * that the pool has been exported or destroyed.  If
14601986Seschrock 			 * this is the case, the config cache is out of sync and
14611986Seschrock 			 * we should remove the pool from the namespace.
1462789Sahrens 			 */
1463789Sahrens 			spa_unload(spa);
1464789Sahrens 			spa_deactivate(spa);
14656643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1466789Sahrens 			spa_remove(spa);
1467789Sahrens 			if (locked)
1468789Sahrens 				mutex_exit(&spa_namespace_lock);
1469789Sahrens 			return (ENOENT);
14701544Seschrock 		}
14711544Seschrock 
14721544Seschrock 		if (error) {
1473789Sahrens 			/*
1474789Sahrens 			 * We can't open the pool, but we still have useful
1475789Sahrens 			 * information: the state of each vdev after the
1476789Sahrens 			 * attempted vdev_open().  Return this to the user.
1477789Sahrens 			 */
14781635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
14791635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1480789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1481789Sahrens 				    B_TRUE);
14821635Sbonwick 				spa_config_exit(spa, FTAG);
14831635Sbonwick 			}
1484789Sahrens 			spa_unload(spa);
1485789Sahrens 			spa_deactivate(spa);
14861544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1487789Sahrens 			if (locked)
1488789Sahrens 				mutex_exit(&spa_namespace_lock);
1489789Sahrens 			*spapp = NULL;
1490789Sahrens 			return (error);
14911544Seschrock 		} else {
14921544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1493789Sahrens 		}
1494789Sahrens 	}
1495789Sahrens 
1496789Sahrens 	spa_open_ref(spa, tag);
14974451Seschrock 
1498789Sahrens 	if (locked)
1499789Sahrens 		mutex_exit(&spa_namespace_lock);
1500789Sahrens 
1501789Sahrens 	*spapp = spa;
1502789Sahrens 
1503789Sahrens 	if (config != NULL) {
15041544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1505789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
15061544Seschrock 		spa_config_exit(spa, FTAG);
1507789Sahrens 	}
1508789Sahrens 
1509789Sahrens 	return (0);
1510789Sahrens }
1511789Sahrens 
1512789Sahrens int
1513789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1514789Sahrens {
1515789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1516789Sahrens }
1517789Sahrens 
15181544Seschrock /*
15191544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
15201544Seschrock  * preventing it from being exported or destroyed.
15211544Seschrock  */
15221544Seschrock spa_t *
15231544Seschrock spa_inject_addref(char *name)
15241544Seschrock {
15251544Seschrock 	spa_t *spa;
15261544Seschrock 
15271544Seschrock 	mutex_enter(&spa_namespace_lock);
15281544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
15291544Seschrock 		mutex_exit(&spa_namespace_lock);
15301544Seschrock 		return (NULL);
15311544Seschrock 	}
15321544Seschrock 	spa->spa_inject_ref++;
15331544Seschrock 	mutex_exit(&spa_namespace_lock);
15341544Seschrock 
15351544Seschrock 	return (spa);
15361544Seschrock }
15371544Seschrock 
15381544Seschrock void
15391544Seschrock spa_inject_delref(spa_t *spa)
15401544Seschrock {
15411544Seschrock 	mutex_enter(&spa_namespace_lock);
15421544Seschrock 	spa->spa_inject_ref--;
15431544Seschrock 	mutex_exit(&spa_namespace_lock);
15441544Seschrock }
15451544Seschrock 
15465450Sbrendan /*
15475450Sbrendan  * Add spares device information to the nvlist.
15485450Sbrendan  */
15492082Seschrock static void
15502082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
15512082Seschrock {
15522082Seschrock 	nvlist_t **spares;
15532082Seschrock 	uint_t i, nspares;
15542082Seschrock 	nvlist_t *nvroot;
15552082Seschrock 	uint64_t guid;
15562082Seschrock 	vdev_stat_t *vs;
15572082Seschrock 	uint_t vsc;
15583377Seschrock 	uint64_t pool;
15592082Seschrock 
15605450Sbrendan 	if (spa->spa_spares.sav_count == 0)
15612082Seschrock 		return;
15622082Seschrock 
15632082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
15642082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15655450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
15662082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15672082Seschrock 	if (nspares != 0) {
15682082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
15692082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15702082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15712082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15722082Seschrock 
15732082Seschrock 		/*
15742082Seschrock 		 * Go through and find any spares which have since been
15752082Seschrock 		 * repurposed as an active spare.  If this is the case, update
15762082Seschrock 		 * their status appropriately.
15772082Seschrock 		 */
15782082Seschrock 		for (i = 0; i < nspares; i++) {
15792082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
15802082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15817214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
15827214Slling 			    pool != 0ULL) {
15832082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
15842082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
15852082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
15862082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
15872082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
15882082Seschrock 			}
15892082Seschrock 		}
15902082Seschrock 	}
15912082Seschrock }
15922082Seschrock 
15935450Sbrendan /*
15945450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
15955450Sbrendan  */
15965450Sbrendan static void
15975450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
15985450Sbrendan {
15995450Sbrendan 	nvlist_t **l2cache;
16005450Sbrendan 	uint_t i, j, nl2cache;
16015450Sbrendan 	nvlist_t *nvroot;
16025450Sbrendan 	uint64_t guid;
16035450Sbrendan 	vdev_t *vd;
16045450Sbrendan 	vdev_stat_t *vs;
16055450Sbrendan 	uint_t vsc;
16065450Sbrendan 
16075450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
16085450Sbrendan 		return;
16095450Sbrendan 
16105450Sbrendan 	spa_config_enter(spa, RW_READER, FTAG);
16115450Sbrendan 
16125450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
16135450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16145450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
16155450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
16165450Sbrendan 	if (nl2cache != 0) {
16175450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
16185450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
16195450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
16205450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
16215450Sbrendan 
16225450Sbrendan 		/*
16235450Sbrendan 		 * Update level 2 cache device stats.
16245450Sbrendan 		 */
16255450Sbrendan 
16265450Sbrendan 		for (i = 0; i < nl2cache; i++) {
16275450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
16285450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
16295450Sbrendan 
16305450Sbrendan 			vd = NULL;
16315450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
16325450Sbrendan 				if (guid ==
16335450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
16345450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
16355450Sbrendan 					break;
16365450Sbrendan 				}
16375450Sbrendan 			}
16385450Sbrendan 			ASSERT(vd != NULL);
16395450Sbrendan 
16405450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
16415450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
16425450Sbrendan 			vdev_get_stats(vd, vs);
16435450Sbrendan 		}
16445450Sbrendan 	}
16455450Sbrendan 
16465450Sbrendan 	spa_config_exit(spa, FTAG);
16475450Sbrendan }
16485450Sbrendan 
1649789Sahrens int
16501544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1651789Sahrens {
1652789Sahrens 	int error;
1653789Sahrens 	spa_t *spa;
1654789Sahrens 
1655789Sahrens 	*config = NULL;
1656789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1657789Sahrens 
16582082Seschrock 	if (spa && *config != NULL) {
16591544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
16601544Seschrock 		    spa_get_errlog_size(spa)) == 0);
16611544Seschrock 
16622082Seschrock 		spa_add_spares(spa, *config);
16635450Sbrendan 		spa_add_l2cache(spa, *config);
16642082Seschrock 	}
16652082Seschrock 
16661544Seschrock 	/*
16671544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
16681544Seschrock 	 * and call spa_lookup() directly.
16691544Seschrock 	 */
16701544Seschrock 	if (altroot) {
16711544Seschrock 		if (spa == NULL) {
16721544Seschrock 			mutex_enter(&spa_namespace_lock);
16731544Seschrock 			spa = spa_lookup(name);
16741544Seschrock 			if (spa)
16751544Seschrock 				spa_altroot(spa, altroot, buflen);
16761544Seschrock 			else
16771544Seschrock 				altroot[0] = '\0';
16781544Seschrock 			spa = NULL;
16791544Seschrock 			mutex_exit(&spa_namespace_lock);
16801544Seschrock 		} else {
16811544Seschrock 			spa_altroot(spa, altroot, buflen);
16821544Seschrock 		}
16831544Seschrock 	}
16841544Seschrock 
1685789Sahrens 	if (spa != NULL)
1686789Sahrens 		spa_close(spa, FTAG);
1687789Sahrens 
1688789Sahrens 	return (error);
1689789Sahrens }
1690789Sahrens 
1691789Sahrens /*
16925450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
16935450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
16945450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
16955450Sbrendan  * specified, as long as they are well-formed.
16962082Seschrock  */
16972082Seschrock static int
16985450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
16995450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
17005450Sbrendan     vdev_labeltype_t label)
17012082Seschrock {
17025450Sbrendan 	nvlist_t **dev;
17035450Sbrendan 	uint_t i, ndev;
17042082Seschrock 	vdev_t *vd;
17052082Seschrock 	int error;
17062082Seschrock 
17072082Seschrock 	/*
17085450Sbrendan 	 * It's acceptable to have no devs specified.
17092082Seschrock 	 */
17105450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
17112082Seschrock 		return (0);
17122082Seschrock 
17135450Sbrendan 	if (ndev == 0)
17142082Seschrock 		return (EINVAL);
17152082Seschrock 
17162082Seschrock 	/*
17175450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
17185450Sbrendan 	 * device type.
17192082Seschrock 	 */
17205450Sbrendan 	if (spa_version(spa) < version)
17212082Seschrock 		return (ENOTSUP);
17222082Seschrock 
17233377Seschrock 	/*
17245450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
17253377Seschrock 	 * checking.
17263377Seschrock 	 */
17275450Sbrendan 	sav->sav_pending = dev;
17285450Sbrendan 	sav->sav_npending = ndev;
17295450Sbrendan 
17305450Sbrendan 	for (i = 0; i < ndev; i++) {
17315450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
17322082Seschrock 		    mode)) != 0)
17333377Seschrock 			goto out;
17342082Seschrock 
17352082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
17362082Seschrock 			vdev_free(vd);
17373377Seschrock 			error = EINVAL;
17383377Seschrock 			goto out;
17392082Seschrock 		}
17402082Seschrock 
17415450Sbrendan 		/*
17425450Sbrendan 		 * The L2ARC currently only supports disk devices.
17435450Sbrendan 		 */
17445450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
17455450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
17465450Sbrendan 			error = ENOTBLK;
17475450Sbrendan 			goto out;
17485450Sbrendan 		}
17495450Sbrendan 
17502082Seschrock 		vd->vdev_top = vd;
17513377Seschrock 
17523377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
17535450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
17545450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
17553377Seschrock 			    vd->vdev_guid) == 0);
17562082Seschrock 		}
17572082Seschrock 
17582082Seschrock 		vdev_free(vd);
17593377Seschrock 
17605450Sbrendan 		if (error &&
17615450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
17623377Seschrock 			goto out;
17633377Seschrock 		else
17643377Seschrock 			error = 0;
17652082Seschrock 	}
17662082Seschrock 
17673377Seschrock out:
17685450Sbrendan 	sav->sav_pending = NULL;
17695450Sbrendan 	sav->sav_npending = 0;
17703377Seschrock 	return (error);
17712082Seschrock }
17722082Seschrock 
17735450Sbrendan static int
17745450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
17755450Sbrendan {
17765450Sbrendan 	int error;
17775450Sbrendan 
17785450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17795450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
17805450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
17815450Sbrendan 		return (error);
17825450Sbrendan 	}
17835450Sbrendan 
17845450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17855450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
17865450Sbrendan 	    VDEV_LABEL_L2CACHE));
17875450Sbrendan }
17885450Sbrendan 
17895450Sbrendan static void
17905450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
17915450Sbrendan     const char *config)
17925450Sbrendan {
17935450Sbrendan 	int i;
17945450Sbrendan 
17955450Sbrendan 	if (sav->sav_config != NULL) {
17965450Sbrendan 		nvlist_t **olddevs;
17975450Sbrendan 		uint_t oldndevs;
17985450Sbrendan 		nvlist_t **newdevs;
17995450Sbrendan 
18005450Sbrendan 		/*
18015450Sbrendan 		 * Generate new dev list by concatentating with the
18025450Sbrendan 		 * current dev list.
18035450Sbrendan 		 */
18045450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
18055450Sbrendan 		    &olddevs, &oldndevs) == 0);
18065450Sbrendan 
18075450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
18085450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
18095450Sbrendan 		for (i = 0; i < oldndevs; i++)
18105450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
18115450Sbrendan 			    KM_SLEEP) == 0);
18125450Sbrendan 		for (i = 0; i < ndevs; i++)
18135450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
18145450Sbrendan 			    KM_SLEEP) == 0);
18155450Sbrendan 
18165450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
18175450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
18185450Sbrendan 
18195450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
18205450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
18215450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
18225450Sbrendan 			nvlist_free(newdevs[i]);
18235450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
18245450Sbrendan 	} else {
18255450Sbrendan 		/*
18265450Sbrendan 		 * Generate a new dev list.
18275450Sbrendan 		 */
18285450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
18295450Sbrendan 		    KM_SLEEP) == 0);
18305450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
18315450Sbrendan 		    devs, ndevs) == 0);
18325450Sbrendan 	}
18335450Sbrendan }
18345450Sbrendan 
18355450Sbrendan /*
18365450Sbrendan  * Stop and drop level 2 ARC devices
18375450Sbrendan  */
18385450Sbrendan void
18395450Sbrendan spa_l2cache_drop(spa_t *spa)
18405450Sbrendan {
18415450Sbrendan 	vdev_t *vd;
18425450Sbrendan 	int i;
18435450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
18445450Sbrendan 
18455450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
18465450Sbrendan 		uint64_t pool;
18475450Sbrendan 
18485450Sbrendan 		vd = sav->sav_vdevs[i];
18495450Sbrendan 		ASSERT(vd != NULL);
18505450Sbrendan 
18515450Sbrendan 		if (spa_mode & FWRITE &&
18526643Seschrock 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL &&
18536643Seschrock 		    l2arc_vdev_present(vd)) {
18545450Sbrendan 			l2arc_remove_vdev(vd);
18555450Sbrendan 		}
18565450Sbrendan 		if (vd->vdev_isl2cache)
18575450Sbrendan 			spa_l2cache_remove(vd);
18585450Sbrendan 		vdev_clear_stats(vd);
18595450Sbrendan 		(void) vdev_close(vd);
18605450Sbrendan 	}
18615450Sbrendan }
18625450Sbrendan 
18632082Seschrock /*
1864789Sahrens  * Pool Creation
1865789Sahrens  */
1866789Sahrens int
18675094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
18687184Stimh     const char *history_str, nvlist_t *zplprops)
1869789Sahrens {
1870789Sahrens 	spa_t *spa;
18715094Slling 	char *altroot = NULL;
18721635Sbonwick 	vdev_t *rvd;
1873789Sahrens 	dsl_pool_t *dp;
1874789Sahrens 	dmu_tx_t *tx;
18752082Seschrock 	int c, error = 0;
1876789Sahrens 	uint64_t txg = TXG_INITIAL;
18775450Sbrendan 	nvlist_t **spares, **l2cache;
18785450Sbrendan 	uint_t nspares, nl2cache;
18795094Slling 	uint64_t version;
1880789Sahrens 
1881789Sahrens 	/*
1882789Sahrens 	 * If this pool already exists, return failure.
1883789Sahrens 	 */
1884789Sahrens 	mutex_enter(&spa_namespace_lock);
1885789Sahrens 	if (spa_lookup(pool) != NULL) {
1886789Sahrens 		mutex_exit(&spa_namespace_lock);
1887789Sahrens 		return (EEXIST);
1888789Sahrens 	}
1889789Sahrens 
1890789Sahrens 	/*
1891789Sahrens 	 * Allocate a new spa_t structure.
1892789Sahrens 	 */
18935094Slling 	(void) nvlist_lookup_string(props,
18945094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
18951635Sbonwick 	spa = spa_add(pool, altroot);
1896789Sahrens 	spa_activate(spa);
1897789Sahrens 
1898789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
18995094Slling 
19005094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
19015094Slling 		spa_unload(spa);
19025094Slling 		spa_deactivate(spa);
19035094Slling 		spa_remove(spa);
19046643Seschrock 		mutex_exit(&spa_namespace_lock);
19055094Slling 		return (error);
19065094Slling 	}
19075094Slling 
19085094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
19095094Slling 	    &version) != 0)
19105094Slling 		version = SPA_VERSION;
19115094Slling 	ASSERT(version <= SPA_VERSION);
19125094Slling 	spa->spa_uberblock.ub_version = version;
1913789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1914789Sahrens 
19151635Sbonwick 	/*
19161635Sbonwick 	 * Create the root vdev.
19171635Sbonwick 	 */
19181635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
19191635Sbonwick 
19202082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
19212082Seschrock 
19222082Seschrock 	ASSERT(error != 0 || rvd != NULL);
19232082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
19242082Seschrock 
19255913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
19261635Sbonwick 		error = EINVAL;
19272082Seschrock 
19282082Seschrock 	if (error == 0 &&
19292082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
19305450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
19312082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
19322082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
19332082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
19342082Seschrock 		vdev_config_dirty(rvd);
19351635Sbonwick 	}
19361635Sbonwick 
19371635Sbonwick 	spa_config_exit(spa, FTAG);
1938789Sahrens 
19392082Seschrock 	if (error != 0) {
1940789Sahrens 		spa_unload(spa);
1941789Sahrens 		spa_deactivate(spa);
1942789Sahrens 		spa_remove(spa);
1943789Sahrens 		mutex_exit(&spa_namespace_lock);
1944789Sahrens 		return (error);
1945789Sahrens 	}
1946789Sahrens 
19472082Seschrock 	/*
19482082Seschrock 	 * Get the list of spares, if specified.
19492082Seschrock 	 */
19502082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19512082Seschrock 	    &spares, &nspares) == 0) {
19525450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
19532082Seschrock 		    KM_SLEEP) == 0);
19545450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
19552082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
19562082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
19572082Seschrock 		spa_load_spares(spa);
19582082Seschrock 		spa_config_exit(spa, FTAG);
19595450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
19605450Sbrendan 	}
19615450Sbrendan 
19625450Sbrendan 	/*
19635450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
19645450Sbrendan 	 */
19655450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
19665450Sbrendan 	    &l2cache, &nl2cache) == 0) {
19675450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
19685450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
19695450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
19705450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
19715450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
19725450Sbrendan 		spa_load_l2cache(spa);
19735450Sbrendan 		spa_config_exit(spa, FTAG);
19745450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
19752082Seschrock 	}
19762082Seschrock 
19777184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
1978789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1979789Sahrens 
1980789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1981789Sahrens 
1982789Sahrens 	/*
1983789Sahrens 	 * Create the pool config object.
1984789Sahrens 	 */
1985789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1986789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1987789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1988789Sahrens 
19891544Seschrock 	if (zap_add(spa->spa_meta_objset,
1990789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
19911544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
19921544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
19931544Seschrock 	}
1994789Sahrens 
19955094Slling 	/* Newly created pools with the right version are always deflated. */
19965094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
19975094Slling 		spa->spa_deflate = TRUE;
19985094Slling 		if (zap_add(spa->spa_meta_objset,
19995094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
20005094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
20015094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
20025094Slling 		}
20032082Seschrock 	}
20042082Seschrock 
2005789Sahrens 	/*
2006789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2007789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2008789Sahrens 	 * keeps changing.
2009789Sahrens 	 */
2010789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2011789Sahrens 	    1 << 14, tx);
2012789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2013789Sahrens 	    ZIO_COMPRESS_OFF, tx);
2014789Sahrens 
20151544Seschrock 	if (zap_add(spa->spa_meta_objset,
2016789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
20171544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
20181544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
20191544Seschrock 	}
2020789Sahrens 
20212926Sek110237 	/*
20222926Sek110237 	 * Create the pool's history object.
20232926Sek110237 	 */
20245094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
20255094Slling 		spa_history_create_obj(spa, tx);
20265094Slling 
20275094Slling 	/*
20285094Slling 	 * Set pool properties.
20295094Slling 	 */
20305094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
20315094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
20325329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
20335094Slling 	if (props)
20345094Slling 		spa_sync_props(spa, props, CRED(), tx);
20352926Sek110237 
2036789Sahrens 	dmu_tx_commit(tx);
2037789Sahrens 
2038789Sahrens 	spa->spa_sync_on = B_TRUE;
2039789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2040789Sahrens 
2041789Sahrens 	/*
2042789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2043789Sahrens 	 * bean counters are appropriately updated.
2044789Sahrens 	 */
2045789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2046789Sahrens 
20476643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2048789Sahrens 
20495094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
20504715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
20514715Sek110237 
2052789Sahrens 	mutex_exit(&spa_namespace_lock);
2053789Sahrens 
20547046Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
20557046Sahrens 
2056789Sahrens 	return (0);
2057789Sahrens }
2058789Sahrens 
2059789Sahrens /*
2060789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2061789Sahrens  * then call spa_load() to do the dirty work.
2062789Sahrens  */
20636423Sgw25295 static int
20646423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
20656643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2066789Sahrens {
2067789Sahrens 	spa_t *spa;
20685094Slling 	char *altroot = NULL;
20696643Seschrock 	int error, loaderr;
20702082Seschrock 	nvlist_t *nvroot;
20715450Sbrendan 	nvlist_t **spares, **l2cache;
20725450Sbrendan 	uint_t nspares, nl2cache;
2073789Sahrens 
2074789Sahrens 	/*
2075789Sahrens 	 * If a pool with this name exists, return failure.
2076789Sahrens 	 */
2077789Sahrens 	mutex_enter(&spa_namespace_lock);
2078789Sahrens 	if (spa_lookup(pool) != NULL) {
2079789Sahrens 		mutex_exit(&spa_namespace_lock);
2080789Sahrens 		return (EEXIST);
2081789Sahrens 	}
2082789Sahrens 
2083789Sahrens 	/*
20841635Sbonwick 	 * Create and initialize the spa structure.
2085789Sahrens 	 */
20865094Slling 	(void) nvlist_lookup_string(props,
20875094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20881635Sbonwick 	spa = spa_add(pool, altroot);
2089789Sahrens 	spa_activate(spa);
2090789Sahrens 
20916643Seschrock 	if (allowfaulted)
20926643Seschrock 		spa->spa_import_faulted = B_TRUE;
20936673Seschrock 	spa->spa_is_root = isroot;
20946643Seschrock 
2095789Sahrens 	/*
20961635Sbonwick 	 * Pass off the heavy lifting to spa_load().
20977046Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
20987046Sahrens 	 * the user-supplied config is actually the one to trust when
20997046Sahrens 	 * doing an import.
21001601Sbonwick 	 */
21017046Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2102789Sahrens 
21032082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
21042082Seschrock 	/*
21052082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
21062082Seschrock 	 * and conflicts with spa_has_spare().
21072082Seschrock 	 */
21086423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
21095450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
21105450Sbrendan 		spa->spa_spares.sav_config = NULL;
21112082Seschrock 		spa_load_spares(spa);
21122082Seschrock 	}
21136423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
21145450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
21155450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
21165450Sbrendan 		spa_load_l2cache(spa);
21175450Sbrendan 	}
21182082Seschrock 
21192082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
21202082Seschrock 	    &nvroot) == 0);
21215450Sbrendan 	if (error == 0)
21225450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
21235450Sbrendan 	if (error == 0)
21245450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
21255450Sbrendan 		    VDEV_ALLOC_L2CACHE);
21262082Seschrock 	spa_config_exit(spa, FTAG);
21272082Seschrock 
21285094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
21296643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
21306643Seschrock 			/*
21316643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
21326643Seschrock 			 * set, then manually set the config as if the config
21336643Seschrock 			 * passed in was specified in the cache file.
21346643Seschrock 			 */
21356643Seschrock 			error = 0;
21366643Seschrock 			spa->spa_import_faulted = B_FALSE;
21376643Seschrock 			if (spa->spa_config == NULL) {
21386643Seschrock 				spa_config_enter(spa, RW_READER, FTAG);
21396643Seschrock 				spa->spa_config = spa_config_generate(spa,
21406643Seschrock 				    NULL, -1ULL, B_TRUE);
21416643Seschrock 				spa_config_exit(spa, FTAG);
21426643Seschrock 			}
21436643Seschrock 			spa_unload(spa);
21446643Seschrock 			spa_deactivate(spa);
21456643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
21466643Seschrock 		} else {
21476643Seschrock 			spa_unload(spa);
21486643Seschrock 			spa_deactivate(spa);
21496643Seschrock 			spa_remove(spa);
21506643Seschrock 		}
2151789Sahrens 		mutex_exit(&spa_namespace_lock);
2152789Sahrens 		return (error);
2153789Sahrens 	}
2154789Sahrens 
21551635Sbonwick 	/*
21565450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
21575450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
21582082Seschrock 	 */
21592082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
21602082Seschrock 	    &spares, &nspares) == 0) {
21615450Sbrendan 		if (spa->spa_spares.sav_config)
21625450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
21632082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
21642082Seschrock 		else
21655450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
21662082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21675450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
21682082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
21692082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
21702082Seschrock 		spa_load_spares(spa);
21712082Seschrock 		spa_config_exit(spa, FTAG);
21725450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
21735450Sbrendan 	}
21745450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
21755450Sbrendan 	    &l2cache, &nl2cache) == 0) {
21765450Sbrendan 		if (spa->spa_l2cache.sav_config)
21775450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
21785450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
21795450Sbrendan 		else
21805450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
21815450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21825450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
21835450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
21845450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
21855450Sbrendan 		spa_load_l2cache(spa);
21865450Sbrendan 		spa_config_exit(spa, FTAG);
21875450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
21882082Seschrock 	}
21892082Seschrock 
21906643Seschrock 	if (spa_mode & FWRITE) {
21916643Seschrock 		/*
21926643Seschrock 		 * Update the config cache to include the newly-imported pool.
21936643Seschrock 		 */
21946423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
21956643Seschrock 	}
21966643Seschrock 
21976643Seschrock 	spa->spa_import_faulted = B_FALSE;
21984451Seschrock 	mutex_exit(&spa_namespace_lock);
21994451Seschrock 
2200789Sahrens 	return (0);
2201789Sahrens }
2202789Sahrens 
22036423Sgw25295 #ifdef _KERNEL
22046423Sgw25295 /*
22056423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
22066423Sgw25295  * device label.
22076423Sgw25295  */
22086423Sgw25295 static void
22096423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
22106423Sgw25295 {
22116423Sgw25295 	nvlist_t *nvtop, *nvroot;
22126423Sgw25295 	uint64_t pgid;
22136423Sgw25295 
22146423Sgw25295 	/*
22156423Sgw25295 	 * Add this top-level vdev to the child array.
22166423Sgw25295 	 */
22176423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
22186423Sgw25295 	    == 0);
22196423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
22206423Sgw25295 	    == 0);
22216423Sgw25295 
22226423Sgw25295 	/*
22236423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
22246423Sgw25295 	 */
22256423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
22266423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
22276423Sgw25295 	    == 0);
22286423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
22296423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
22306423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
22316423Sgw25295 	    &nvtop, 1) == 0);
22326423Sgw25295 
22336423Sgw25295 	/*
22346423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
22356423Sgw25295 	 * this pool's configuration (remove the old, add the new).
22366423Sgw25295 	 */
22376423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
22386423Sgw25295 	nvlist_free(nvroot);
22396423Sgw25295 }
22406423Sgw25295 
22416423Sgw25295 /*
22426423Sgw25295  * Get the root pool information from the root disk, then import the root pool
22436423Sgw25295  * during the system boot up time.
22446423Sgw25295  */
22457147Staylor extern nvlist_t *vdev_disk_read_rootlabel(char *, char *);
22467147Staylor 
22477147Staylor int
22487147Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
22496423Sgw25295     uint64_t *besttxg)
22506423Sgw25295 {
22516423Sgw25295 	nvlist_t *config;
22526423Sgw25295 	uint64_t txg;
22536423Sgw25295 
22547147Staylor 	if ((config = vdev_disk_read_rootlabel(devpath, devid)) == NULL)
22557147Staylor 		return (-1);
22566423Sgw25295 
22576423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
22586423Sgw25295 
22597147Staylor 	if (bestconf != NULL)
22606423Sgw25295 		*bestconf = config;
22617147Staylor 	*besttxg = txg;
22627147Staylor 	return (0);
22636423Sgw25295 }
22646423Sgw25295 
22656423Sgw25295 boolean_t
22666423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
22676423Sgw25295 {
22686423Sgw25295 	uint64_t ival;
22696423Sgw25295 
22706423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
22716423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
22726423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
22736423Sgw25295 		return (B_FALSE);
22746423Sgw25295 
22756423Sgw25295 	return (B_TRUE);
22766423Sgw25295 }
22776423Sgw25295 
22787147Staylor 
22797147Staylor /*
22807147Staylor  * Given the boot device's physical path or devid, check if the device
22817147Staylor  * is in a valid state.  If so, return the configuration from the vdev
22827147Staylor  * label.
22837147Staylor  */
22847147Staylor int
22857147Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
22867147Staylor {
22877147Staylor 	nvlist_t *conf = NULL;
22887147Staylor 	uint64_t txg = 0;
22897147Staylor 	nvlist_t *nvtop, **child;
22907147Staylor 	char *type;
22917147Staylor 	char *bootpath = NULL;
22927147Staylor 	uint_t children, c;
22937147Staylor 	char *tmp;
22947147Staylor 
22957147Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
22967147Staylor 		*tmp = '\0';
22977147Staylor 	if (spa_check_rootconf(devpath, devid, &conf, &txg) < 0) {
22987147Staylor 		cmn_err(CE_NOTE, "error reading device label");
22997147Staylor 		nvlist_free(conf);
23007147Staylor 		return (EINVAL);
23017147Staylor 	}
23027147Staylor 	if (txg == 0) {
23037147Staylor 		cmn_err(CE_NOTE, "this device is detached");
23047147Staylor 		nvlist_free(conf);
23057147Staylor 		return (EINVAL);
23067147Staylor 	}
23077147Staylor 
23087147Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
23097147Staylor 	    &nvtop) == 0);
23107147Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
23117147Staylor 
23127147Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
23137147Staylor 		if (spa_rootdev_validate(nvtop)) {
23147147Staylor 			goto out;
23157147Staylor 		} else {
23167147Staylor 			nvlist_free(conf);
23177147Staylor 			return (EINVAL);
23187147Staylor 		}
23197147Staylor 	}
23207147Staylor 
23217147Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
23227147Staylor 
23237147Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
23247147Staylor 	    &child, &children) == 0);
23257147Staylor 
23267147Staylor 	/*
23277147Staylor 	 * Go thru vdevs in the mirror to see if the given device
23287147Staylor 	 * has the most recent txg. Only the device with the most
23297147Staylor 	 * recent txg has valid information and should be booted.
23307147Staylor 	 */
23317147Staylor 	for (c = 0; c < children; c++) {
23327147Staylor 		char *cdevid, *cpath;
23337147Staylor 		uint64_t tmptxg;
23347147Staylor 
23357147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
23367147Staylor 		    &cpath) != 0)
23377147Staylor 			return (EINVAL);
23387147Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_DEVID,
23397147Staylor 		    &cdevid) != 0)
23407147Staylor 			return (EINVAL);
23417147Staylor 		if ((spa_check_rootconf(cpath, cdevid, NULL,
23427147Staylor 		    &tmptxg) == 0) && (tmptxg > txg)) {
23437147Staylor 			txg = tmptxg;
23447147Staylor 			VERIFY(nvlist_lookup_string(child[c],
23457147Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
23467147Staylor 		}
23477147Staylor 	}
23487147Staylor 
23497147Staylor 	/* Does the best device match the one we've booted from? */
23507147Staylor 	if (bootpath) {
23517147Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
23527147Staylor 		return (EINVAL);
23537147Staylor 	}
23547147Staylor out:
23557147Staylor 	*bestconf = conf;
23567147Staylor 	return (0);
23577147Staylor }
23587147Staylor 
23596423Sgw25295 /*
23606423Sgw25295  * Import a root pool.
23616423Sgw25295  *
23627147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
23637147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
23647147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
23656423Sgw25295  *
23666423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
23676423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
23686423Sgw25295  * e.g.
23696423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
23706423Sgw25295  */
23716423Sgw25295 int
23727147Staylor spa_import_rootpool(char *devpath, char *devid)
23736423Sgw25295 {
23746423Sgw25295 	nvlist_t *conf = NULL;
23756423Sgw25295 	char *pname;
23766423Sgw25295 	int error;
23776423Sgw25295 
23786423Sgw25295 	/*
23796423Sgw25295 	 * Get the vdev pathname and configuation from the most
23806423Sgw25295 	 * recently updated vdev (highest txg).
23816423Sgw25295 	 */
23827147Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
23836423Sgw25295 		goto msg_out;
23846423Sgw25295 
23856423Sgw25295 	/*
23866423Sgw25295 	 * Add type "root" vdev to the config.
23876423Sgw25295 	 */
23886423Sgw25295 	spa_build_rootpool_config(conf);
23896423Sgw25295 
23906423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
23916423Sgw25295 
23926673Seschrock 	/*
23936673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
23946673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
23956673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
23966673Seschrock 	 * pool open, not import.
23976673Seschrock 	 */
23986673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
23996423Sgw25295 	if (error == EEXIST)
24006423Sgw25295 		error = 0;
24016423Sgw25295 
24026423Sgw25295 	nvlist_free(conf);
24036423Sgw25295 	return (error);
24046423Sgw25295 
24056423Sgw25295 msg_out:
24067147Staylor 	cmn_err(CE_NOTE, "\n"
24076423Sgw25295 	    "  ***************************************************  \n"
24086423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
24096423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
24106423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
24117147Staylor 	    "  ***************************************************  ");
24126423Sgw25295 
24136423Sgw25295 	return (error);
24146423Sgw25295 }
24156423Sgw25295 #endif
24166423Sgw25295 
24176423Sgw25295 /*
24186423Sgw25295  * Import a non-root pool into the system.
24196423Sgw25295  */
24206423Sgw25295 int
24216423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
24226423Sgw25295 {
24236643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
24246423Sgw25295 }
24256423Sgw25295 
24266643Seschrock int
24276643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
24286643Seschrock {
24296643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
24306643Seschrock }
24316643Seschrock 
24326643Seschrock 
2433789Sahrens /*
2434789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2435789Sahrens  * to get the vdev stats associated with the imported devices.
2436789Sahrens  */
2437789Sahrens #define	TRYIMPORT_NAME	"$import"
2438789Sahrens 
2439789Sahrens nvlist_t *
2440789Sahrens spa_tryimport(nvlist_t *tryconfig)
2441789Sahrens {
2442789Sahrens 	nvlist_t *config = NULL;
2443789Sahrens 	char *poolname;
2444789Sahrens 	spa_t *spa;
2445789Sahrens 	uint64_t state;
2446789Sahrens 
2447789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2448789Sahrens 		return (NULL);
2449789Sahrens 
2450789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2451789Sahrens 		return (NULL);
2452789Sahrens 
24531635Sbonwick 	/*
24541635Sbonwick 	 * Create and initialize the spa structure.
24551635Sbonwick 	 */
2456789Sahrens 	mutex_enter(&spa_namespace_lock);
24571635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2458789Sahrens 	spa_activate(spa);
2459789Sahrens 
2460789Sahrens 	/*
24611635Sbonwick 	 * Pass off the heavy lifting to spa_load().
24621732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
24631732Sbonwick 	 * is actually the one to trust when doing an import.
2464789Sahrens 	 */
24651732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2466789Sahrens 
2467789Sahrens 	/*
2468789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2469789Sahrens 	 */
2470789Sahrens 	if (spa->spa_root_vdev != NULL) {
24711635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
2472789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
24731635Sbonwick 		spa_config_exit(spa, FTAG);
2474789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2475789Sahrens 		    poolname) == 0);
2476789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2477789Sahrens 		    state) == 0);
24783975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
24793975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
24802082Seschrock 
24812082Seschrock 		/*
24826423Sgw25295 		 * If the bootfs property exists on this pool then we
24836423Sgw25295 		 * copy it out so that external consumers can tell which
24846423Sgw25295 		 * pools are bootable.
24856423Sgw25295 		 */
24866423Sgw25295 		if (spa->spa_bootfs) {
24876423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
24886423Sgw25295 
24896423Sgw25295 			/*
24906423Sgw25295 			 * We have to play games with the name since the
24916423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
24926423Sgw25295 			 */
24936423Sgw25295 			if (dsl_dsobj_to_dsname(spa->spa_name,
24946423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
24956423Sgw25295 				char *cp;
24966423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
24976423Sgw25295 
24986423Sgw25295 				cp = strchr(tmpname, '/');
24996423Sgw25295 				if (cp == NULL) {
25006423Sgw25295 					(void) strlcpy(dsname, tmpname,
25016423Sgw25295 					    MAXPATHLEN);
25026423Sgw25295 				} else {
25036423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
25046423Sgw25295 					    "%s/%s", poolname, ++cp);
25056423Sgw25295 				}
25066423Sgw25295 				VERIFY(nvlist_add_string(config,
25076423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
25086423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
25096423Sgw25295 			}
25106423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
25116423Sgw25295 		}
25126423Sgw25295 
25136423Sgw25295 		/*
25145450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
25152082Seschrock 		 */
25162082Seschrock 		spa_add_spares(spa, config);
25175450Sbrendan 		spa_add_l2cache(spa, config);
2518789Sahrens 	}
2519789Sahrens 
2520789Sahrens 	spa_unload(spa);
2521789Sahrens 	spa_deactivate(spa);
2522789Sahrens 	spa_remove(spa);
2523789Sahrens 	mutex_exit(&spa_namespace_lock);
2524789Sahrens 
2525789Sahrens 	return (config);
2526789Sahrens }
2527789Sahrens 
2528789Sahrens /*
2529789Sahrens  * Pool export/destroy
2530789Sahrens  *
2531789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2532789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2533789Sahrens  * update the pool state and sync all the labels to disk, removing the
2534789Sahrens  * configuration from the cache afterwards.
2535789Sahrens  */
2536789Sahrens static int
25377214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
25387214Slling     boolean_t force)
2539789Sahrens {
2540789Sahrens 	spa_t *spa;
2541789Sahrens 
25421775Sbillm 	if (oldconfig)
25431775Sbillm 		*oldconfig = NULL;
25441775Sbillm 
2545789Sahrens 	if (!(spa_mode & FWRITE))
2546789Sahrens 		return (EROFS);
2547789Sahrens 
2548789Sahrens 	mutex_enter(&spa_namespace_lock);
2549789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2550789Sahrens 		mutex_exit(&spa_namespace_lock);
2551789Sahrens 		return (ENOENT);
2552789Sahrens 	}
2553789Sahrens 
2554789Sahrens 	/*
25551544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
25561544Seschrock 	 * reacquire the namespace lock, and see if we can export.
25571544Seschrock 	 */
25581544Seschrock 	spa_open_ref(spa, FTAG);
25591544Seschrock 	mutex_exit(&spa_namespace_lock);
25601544Seschrock 	spa_async_suspend(spa);
25611544Seschrock 	mutex_enter(&spa_namespace_lock);
25621544Seschrock 	spa_close(spa, FTAG);
25631544Seschrock 
25641544Seschrock 	/*
2565789Sahrens 	 * The pool will be in core if it's openable,
2566789Sahrens 	 * in which case we can modify its state.
2567789Sahrens 	 */
2568789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2569789Sahrens 		/*
2570789Sahrens 		 * Objsets may be open only because they're dirty, so we
2571789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2572789Sahrens 		 */
2573789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2574789Sahrens 
25751544Seschrock 		/*
25761544Seschrock 		 * A pool cannot be exported or destroyed if there are active
25771544Seschrock 		 * references.  If we are resetting a pool, allow references by
25781544Seschrock 		 * fault injection handlers.
25791544Seschrock 		 */
25801544Seschrock 		if (!spa_refcount_zero(spa) ||
25811544Seschrock 		    (spa->spa_inject_ref != 0 &&
25821544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
25831544Seschrock 			spa_async_resume(spa);
2584789Sahrens 			mutex_exit(&spa_namespace_lock);
2585789Sahrens 			return (EBUSY);
2586789Sahrens 		}
2587789Sahrens 
2588789Sahrens 		/*
25897214Slling 		 * A pool cannot be exported if it has an active shared spare.
25907214Slling 		 * This is to prevent other pools stealing the active spare
25917214Slling 		 * from an exported pool. At user's own will, such pool can
25927214Slling 		 * be forcedly exported.
25937214Slling 		 */
25947214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
25957214Slling 		    spa_has_active_shared_spare(spa)) {
25967214Slling 			spa_async_resume(spa);
25977214Slling 			mutex_exit(&spa_namespace_lock);
25987214Slling 			return (EXDEV);
25997214Slling 		}
26007214Slling 
26017214Slling 		/*
2602789Sahrens 		 * We want this to be reflected on every label,
2603789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2604789Sahrens 		 * final sync that pushes these changes out.
2605789Sahrens 		 */
26061544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
26071601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
26081544Seschrock 			spa->spa_state = new_state;
26091635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
26101544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
26111601Sbonwick 			spa_config_exit(spa, FTAG);
26121544Seschrock 		}
2613789Sahrens 	}
2614789Sahrens 
26154451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
26164451Seschrock 
2617789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2618789Sahrens 		spa_unload(spa);
2619789Sahrens 		spa_deactivate(spa);
2620789Sahrens 	}
2621789Sahrens 
26221775Sbillm 	if (oldconfig && spa->spa_config)
26231775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
26241775Sbillm 
26251544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
26266643Seschrock 		spa_config_sync(spa, B_TRUE, B_TRUE);
26271544Seschrock 		spa_remove(spa);
26281544Seschrock 	}
2629789Sahrens 	mutex_exit(&spa_namespace_lock);
2630789Sahrens 
2631789Sahrens 	return (0);
2632789Sahrens }
2633789Sahrens 
2634789Sahrens /*
2635789Sahrens  * Destroy a storage pool.
2636789Sahrens  */
2637789Sahrens int
2638789Sahrens spa_destroy(char *pool)
2639789Sahrens {
26407214Slling 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, B_FALSE));
2641789Sahrens }
2642789Sahrens 
2643789Sahrens /*
2644789Sahrens  * Export a storage pool.
2645789Sahrens  */
2646789Sahrens int
26477214Slling spa_export(char *pool, nvlist_t **oldconfig, boolean_t force)
2648789Sahrens {
26497214Slling 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, force));
2650789Sahrens }
2651789Sahrens 
2652789Sahrens /*
26531544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
26541544Seschrock  * from the namespace in any way.
26551544Seschrock  */
26561544Seschrock int
26571544Seschrock spa_reset(char *pool)
26581544Seschrock {
26597214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
26607214Slling 	    B_FALSE));
26611544Seschrock }
26621544Seschrock 
26631544Seschrock /*
2664789Sahrens  * ==========================================================================
2665789Sahrens  * Device manipulation
2666789Sahrens  * ==========================================================================
2667789Sahrens  */
2668789Sahrens 
2669789Sahrens /*
26704527Sperrin  * Add a device to a storage pool.
2671789Sahrens  */
2672789Sahrens int
2673789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2674789Sahrens {
2675789Sahrens 	uint64_t txg;
26761635Sbonwick 	int c, error;
2677789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
26781585Sbonwick 	vdev_t *vd, *tvd;
26795450Sbrendan 	nvlist_t **spares, **l2cache;
26805450Sbrendan 	uint_t nspares, nl2cache;
2681789Sahrens 
2682789Sahrens 	txg = spa_vdev_enter(spa);
2683789Sahrens 
26842082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
26852082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
26862082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
26872082Seschrock 
26883377Seschrock 	spa->spa_pending_vdev = vd;
2689789Sahrens 
26905450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
26915450Sbrendan 	    &nspares) != 0)
26922082Seschrock 		nspares = 0;
26932082Seschrock 
26945450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
26955450Sbrendan 	    &nl2cache) != 0)
26965450Sbrendan 		nl2cache = 0;
26975450Sbrendan 
26985450Sbrendan 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) {
26993377Seschrock 		spa->spa_pending_vdev = NULL;
27002082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
27013377Seschrock 	}
27022082Seschrock 
27032082Seschrock 	if (vd->vdev_children != 0) {
27043377Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
27053377Seschrock 			spa->spa_pending_vdev = NULL;
27062082Seschrock 			return (spa_vdev_exit(spa, vd, txg, error));
27072082Seschrock 		}
27082082Seschrock 	}
27092082Seschrock 
27103377Seschrock 	/*
27115450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
27125450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
27133377Seschrock 	 */
27145450Sbrendan 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) {
27153377Seschrock 		spa->spa_pending_vdev = NULL;
27163377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
27173377Seschrock 	}
27183377Seschrock 
27193377Seschrock 	spa->spa_pending_vdev = NULL;
27203377Seschrock 
27213377Seschrock 	/*
27223377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
27233377Seschrock 	 */
27243377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
27253377Seschrock 		tvd = vd->vdev_child[c];
27263377Seschrock 		vdev_remove_child(vd, tvd);
27273377Seschrock 		tvd->vdev_id = rvd->vdev_children;
27283377Seschrock 		vdev_add_child(rvd, tvd);
27293377Seschrock 		vdev_config_dirty(tvd);
27303377Seschrock 	}
27313377Seschrock 
27322082Seschrock 	if (nspares != 0) {
27335450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
27345450Sbrendan 		    ZPOOL_CONFIG_SPARES);
27352082Seschrock 		spa_load_spares(spa);
27365450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27375450Sbrendan 	}
27385450Sbrendan 
27395450Sbrendan 	if (nl2cache != 0) {
27405450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
27415450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
27425450Sbrendan 		spa_load_l2cache(spa);
27435450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2744789Sahrens 	}
2745789Sahrens 
2746789Sahrens 	/*
27471585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
27481585Sbonwick 	 * If other threads start allocating from these vdevs before we
27491585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
27501585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
27511585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
27521585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
27531635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
27541585Sbonwick 	 *
27551585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
27561585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
27571585Sbonwick 	 * steps will be completed the next time we load the pool.
2758789Sahrens 	 */
27591635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
27601585Sbonwick 
27611635Sbonwick 	mutex_enter(&spa_namespace_lock);
27621635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
27631635Sbonwick 	mutex_exit(&spa_namespace_lock);
2764789Sahrens 
27651635Sbonwick 	return (0);
2766789Sahrens }
2767789Sahrens 
2768789Sahrens /*
2769789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2770789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2771789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2772789Sahrens  *
2773789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2774789Sahrens  * existing device; in this case the two devices are made into their own
27754451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2776789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2777789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2778789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2779789Sahrens  * is automatically detached.
2780789Sahrens  */
2781789Sahrens int
27821544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2783789Sahrens {
2784789Sahrens 	uint64_t txg, open_txg;
2785789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2786789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
27872082Seschrock 	vdev_ops_t *pvops;
27887313SEric.Kustarz@Sun.COM 	dmu_tx_t *tx;
27897313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
27907313SEric.Kustarz@Sun.COM 	int newvd_isspare;
27917313SEric.Kustarz@Sun.COM 	int error;
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 	 */
2823*7326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
28244527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28254527Sperrin 
28262082Seschrock 	if (!replacing) {
28272082Seschrock 		/*
28282082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
28292082Seschrock 		 * vdev.
28302082Seschrock 		 */
28312082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
28322082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
28332082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28342082Seschrock 
28352082Seschrock 		pvops = &vdev_mirror_ops;
28362082Seschrock 	} else {
28372082Seschrock 		/*
28382082Seschrock 		 * Active hot spares can only be replaced by inactive hot
28392082Seschrock 		 * spares.
28402082Seschrock 		 */
28412082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
28422082Seschrock 		    pvd->vdev_child[1] == oldvd &&
28432082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
28442082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28452082Seschrock 
28462082Seschrock 		/*
28472082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
28482082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
28493377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
28503377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
28513377Seschrock 		 * the same (spare replaces spare, non-spare replaces
28523377Seschrock 		 * non-spare).
28532082Seschrock 		 */
28542082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
28552082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28563377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
28573377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
28583377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
28592082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
28602082Seschrock 		    newvd->vdev_isspare)
28612082Seschrock 			pvops = &vdev_spare_ops;
28622082Seschrock 		else
28632082Seschrock 			pvops = &vdev_replacing_ops;
28642082Seschrock 	}
28652082Seschrock 
28661175Slling 	/*
28671175Slling 	 * Compare the new device size with the replaceable/attachable
28681175Slling 	 * device size.
28691175Slling 	 */
28701175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2871789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2872789Sahrens 
28731732Sbonwick 	/*
28741732Sbonwick 	 * The new device cannot have a higher alignment requirement
28751732Sbonwick 	 * than the top-level vdev.
28761732Sbonwick 	 */
28771732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2878789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2879789Sahrens 
2880789Sahrens 	/*
2881789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2882789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2883789Sahrens 	 */
2884789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2885789Sahrens 		spa_strfree(oldvd->vdev_path);
2886789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2887789Sahrens 		    KM_SLEEP);
2888789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2889789Sahrens 		    newvd->vdev_path, "old");
2890789Sahrens 		if (oldvd->vdev_devid != NULL) {
2891789Sahrens 			spa_strfree(oldvd->vdev_devid);
2892789Sahrens 			oldvd->vdev_devid = NULL;
2893789Sahrens 		}
2894789Sahrens 	}
2895789Sahrens 
2896789Sahrens 	/*
28972082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
28982082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2899789Sahrens 	 */
2900789Sahrens 	if (pvd->vdev_ops != pvops)
2901789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2902789Sahrens 
2903789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2904789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2905789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2906789Sahrens 
2907789Sahrens 	/*
2908789Sahrens 	 * Extract the new device from its root and add it to pvd.
2909789Sahrens 	 */
2910789Sahrens 	vdev_remove_child(newrootvd, newvd);
2911789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2912789Sahrens 	vdev_add_child(pvd, newvd);
2913789Sahrens 
29141544Seschrock 	/*
29151544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
29161544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
29171544Seschrock 	 */
29181544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
29191544Seschrock 
2920789Sahrens 	tvd = newvd->vdev_top;
2921789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2922789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2923789Sahrens 
2924789Sahrens 	vdev_config_dirty(tvd);
2925789Sahrens 
2926789Sahrens 	/*
2927789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2928789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2929789Sahrens 	 */
2930789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2931789Sahrens 
2932789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2933789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2934789Sahrens 	    open_txg - TXG_INITIAL + 1);
2935789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2936789Sahrens 
29373377Seschrock 	if (newvd->vdev_isspare)
29383377Seschrock 		spa_spare_activate(newvd);
29397313SEric.Kustarz@Sun.COM 	oldvdpath = spa_strdup(vdev_description(oldvd));
29407313SEric.Kustarz@Sun.COM 	newvdpath = spa_strdup(vdev_description(newvd));
29417313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
29421544Seschrock 
2943789Sahrens 	/*
2944789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2945789Sahrens 	 */
29461732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2947789Sahrens 
2948789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2949789Sahrens 
29507313SEric.Kustarz@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
29517313SEric.Kustarz@Sun.COM 	if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
29527313SEric.Kustarz@Sun.COM 		spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
29537313SEric.Kustarz@Sun.COM 		    CRED(),  "%s vdev=%s %s vdev=%s",
29547313SEric.Kustarz@Sun.COM 		    replacing && newvd_isspare ? "spare in" :
29557313SEric.Kustarz@Sun.COM 		    replacing ? "replace" : "attach", newvdpath,
29567313SEric.Kustarz@Sun.COM 		    replacing ? "for" : "to", oldvdpath);
29577313SEric.Kustarz@Sun.COM 		dmu_tx_commit(tx);
29587313SEric.Kustarz@Sun.COM 	} else {
29597313SEric.Kustarz@Sun.COM 		dmu_tx_abort(tx);
29607313SEric.Kustarz@Sun.COM 	}
29617313SEric.Kustarz@Sun.COM 
29627313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
29637313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
29647313SEric.Kustarz@Sun.COM 
2965789Sahrens 	/*
29667046Sahrens 	 * Kick off a resilver to update newvd.
2967789Sahrens 	 */
29687046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
2969789Sahrens 
2970789Sahrens 	return (0);
2971789Sahrens }
2972789Sahrens 
2973789Sahrens /*
2974789Sahrens  * Detach a device from a mirror or replacing vdev.
2975789Sahrens  * If 'replace_done' is specified, only detach if the parent
2976789Sahrens  * is a replacing vdev.
2977789Sahrens  */
2978789Sahrens int
29791544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2980789Sahrens {
2981789Sahrens 	uint64_t txg;
2982789Sahrens 	int c, t, error;
2983789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2984789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
29852082Seschrock 	boolean_t unspare = B_FALSE;
29862082Seschrock 	uint64_t unspare_guid;
29876673Seschrock 	size_t len;
2988789Sahrens 
2989789Sahrens 	txg = spa_vdev_enter(spa);
2990789Sahrens 
29916643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
2992789Sahrens 
2993789Sahrens 	if (vd == NULL)
2994789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2995789Sahrens 
29961585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
29971585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
29981585Sbonwick 
2999789Sahrens 	pvd = vd->vdev_parent;
3000789Sahrens 
3001789Sahrens 	/*
3002789Sahrens 	 * If replace_done is specified, only remove this device if it's
30032082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
30042082Seschrock 	 * disk can be removed.
3005789Sahrens 	 */
30062082Seschrock 	if (replace_done) {
30072082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
30082082Seschrock 			if (vd->vdev_id != 0)
30092082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30102082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
30112082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30122082Seschrock 		}
30132082Seschrock 	}
30142082Seschrock 
30152082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
30164577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3017789Sahrens 
3018789Sahrens 	/*
30192082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3020789Sahrens 	 */
3021789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
30222082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
30232082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3024789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3025789Sahrens 
3026789Sahrens 	/*
3027789Sahrens 	 * If there's only one replica, you can't detach it.
3028789Sahrens 	 */
3029789Sahrens 	if (pvd->vdev_children <= 1)
3030789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3031789Sahrens 
3032789Sahrens 	/*
3033789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
3034789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
3035789Sahrens 	 *
3036789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
3037789Sahrens 	 * precise DTL check.
3038789Sahrens 	 */
3039789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
3040789Sahrens 		uint64_t dirty;
3041789Sahrens 
3042789Sahrens 		cvd = pvd->vdev_child[c];
3043789Sahrens 		if (cvd == vd)
3044789Sahrens 			continue;
3045789Sahrens 		if (vdev_is_dead(cvd))
3046789Sahrens 			continue;
3047789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
3048789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
3049789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
3050789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
3051789Sahrens 		if (!dirty)
3052789Sahrens 			break;
3053789Sahrens 	}
30542082Seschrock 
30552082Seschrock 	/*
30562082Seschrock 	 * If we are a replacing or spare vdev, then we can always detach the
30572082Seschrock 	 * latter child, as that is how one cancels the operation.
30582082Seschrock 	 */
30592082Seschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
30602082Seschrock 	    c == pvd->vdev_children)
3061789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3062789Sahrens 
3063789Sahrens 	/*
30646673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
30656673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
30666673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
30676673Seschrock 	 */
30686673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
30696673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
30706673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
30716673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
30726673Seschrock 		cvd = pvd->vdev_child[0];
30736673Seschrock 		len = strlen(vd->vdev_path);
30746673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
30756673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
30766673Seschrock 			spa_strfree(cvd->vdev_path);
30776673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
30786673Seschrock 		}
30796673Seschrock 	}
30806673Seschrock 
30816673Seschrock 	/*
30822082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
30832082Seschrock 	 * that the spare should become a real disk, and be removed from the
30842082Seschrock 	 * active spare list for the pool.
30852082Seschrock 	 */
30862082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
30872082Seschrock 	    vd->vdev_id == 0)
30882082Seschrock 		unspare = B_TRUE;
30892082Seschrock 
30902082Seschrock 	/*
3091789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3092789Sahrens 	 * This must be done after all other error cases are handled,
3093789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3094789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3095789Sahrens 	 * it may be that the unwritability of the disk is the reason
3096789Sahrens 	 * it's being detached!
3097789Sahrens 	 */
30983377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3099789Sahrens 
3100789Sahrens 	/*
3101789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3102789Sahrens 	 */
3103789Sahrens 	vdev_remove_child(pvd, vd);
3104789Sahrens 	vdev_compact_children(pvd);
3105789Sahrens 
3106789Sahrens 	/*
3107789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3108789Sahrens 	 */
3109789Sahrens 	cvd = pvd->vdev_child[0];
3110789Sahrens 
3111789Sahrens 	/*
31122082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
31132082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
31142082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
31152082Seschrock 	 * GUID if it creates a new toplevel GUID.
31162082Seschrock 	 */
31172082Seschrock 	if (unspare) {
31182082Seschrock 		ASSERT(cvd->vdev_isspare);
31193377Seschrock 		spa_spare_remove(cvd);
31202082Seschrock 		unspare_guid = cvd->vdev_guid;
31212082Seschrock 	}
31222082Seschrock 
31232082Seschrock 	/*
3124789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3125789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3126789Sahrens 	 */
3127789Sahrens 	if (pvd->vdev_children == 1)
3128789Sahrens 		vdev_remove_parent(cvd);
3129789Sahrens 
3130789Sahrens 	/*
3131789Sahrens 	 * We don't set tvd until now because the parent we just removed
3132789Sahrens 	 * may have been the previous top-level vdev.
3133789Sahrens 	 */
3134789Sahrens 	tvd = cvd->vdev_top;
3135789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3136789Sahrens 
3137789Sahrens 	/*
31383377Seschrock 	 * Reevaluate the parent vdev state.
3139789Sahrens 	 */
31404451Seschrock 	vdev_propagate_state(cvd);
3141789Sahrens 
3142789Sahrens 	/*
31433377Seschrock 	 * If the device we just detached was smaller than the others, it may be
31443377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
31453377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
31463377Seschrock 	 * there's nothing to read from disk.
3147789Sahrens 	 */
31481732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3149789Sahrens 
3150789Sahrens 	vdev_config_dirty(tvd);
3151789Sahrens 
3152789Sahrens 	/*
31533377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
31543377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
31553377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
31563377Seschrock 	 * prevent vd from being accessed after it's freed.
3157789Sahrens 	 */
3158789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
3159789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
31601732Sbonwick 	vd->vdev_detached = B_TRUE;
31611732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3162789Sahrens 
31634451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
31644451Seschrock 
31652082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
31662082Seschrock 
31672082Seschrock 	/*
31683377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
31693377Seschrock 	 * then we want to go through and remove the device from the hot spare
31703377Seschrock 	 * list of every other pool.
31712082Seschrock 	 */
31722082Seschrock 	if (unspare) {
31732082Seschrock 		spa = NULL;
31742082Seschrock 		mutex_enter(&spa_namespace_lock);
31752082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
31762082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
31772082Seschrock 				continue;
31782082Seschrock 
31792082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
31802082Seschrock 		}
31812082Seschrock 		mutex_exit(&spa_namespace_lock);
31822082Seschrock 	}
31832082Seschrock 
31842082Seschrock 	return (error);
31852082Seschrock }
31862082Seschrock 
31872082Seschrock /*
31885450Sbrendan  * Remove a spares vdev from the nvlist config.
31892082Seschrock  */
31905450Sbrendan static int
31915450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare,
31925450Sbrendan     nvlist_t **spares, int nspares, vdev_t *vd)
31932082Seschrock {
31945450Sbrendan 	nvlist_t *nv, **newspares;
31955450Sbrendan 	int i, j;
31962082Seschrock 
31972082Seschrock 	nv = NULL;
31985450Sbrendan 	for (i = 0; i < nspares; i++) {
31995450Sbrendan 		uint64_t theguid;
32005450Sbrendan 
32015450Sbrendan 		VERIFY(nvlist_lookup_uint64(spares[i],
32025450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
32035450Sbrendan 		if (theguid == guid) {
32045450Sbrendan 			nv = spares[i];
32055450Sbrendan 			break;
32062082Seschrock 		}
32072082Seschrock 	}
32082082Seschrock 
32092082Seschrock 	/*
32105450Sbrendan 	 * Only remove the hot spare if it's not currently in use in this pool.
32112082Seschrock 	 */
32125450Sbrendan 	if (nv == NULL && vd == NULL)
32135450Sbrendan 		return (ENOENT);
32145450Sbrendan 
32155450Sbrendan 	if (nv == NULL && vd != NULL)
32165450Sbrendan 		return (ENOTSUP);
32175450Sbrendan 
32185450Sbrendan 	if (!unspare && nv != NULL && vd != NULL)
32195450Sbrendan 		return (EBUSY);
32202082Seschrock 
32212082Seschrock 	if (nspares == 1) {
32222082Seschrock 		newspares = NULL;
32232082Seschrock 	} else {
32242082Seschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
32252082Seschrock 		    KM_SLEEP);
32262082Seschrock 		for (i = 0, j = 0; i < nspares; i++) {
32272082Seschrock 			if (spares[i] != nv)
32282082Seschrock 				VERIFY(nvlist_dup(spares[i],
32292082Seschrock 				    &newspares[j++], KM_SLEEP) == 0);
32302082Seschrock 		}
32312082Seschrock 	}
32322082Seschrock 
32335450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES,
32342082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
32355450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
32365450Sbrendan 	    ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0);
32372082Seschrock 	for (i = 0; i < nspares - 1; i++)
32382082Seschrock 		nvlist_free(newspares[i]);
32392082Seschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
32405450Sbrendan 
32415450Sbrendan 	return (0);
32425450Sbrendan }
32435450Sbrendan 
32445450Sbrendan /*
32455450Sbrendan  * Remove an l2cache vdev from the nvlist config.
32465450Sbrendan  */
32475450Sbrendan static int
32485450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache,
32495450Sbrendan     int nl2cache, vdev_t *vd)
32505450Sbrendan {
32515450Sbrendan 	nvlist_t *nv, **newl2cache;
32525450Sbrendan 	int i, j;
32535450Sbrendan 
32545450Sbrendan 	nv = NULL;
32555450Sbrendan 	for (i = 0; i < nl2cache; i++) {
32565450Sbrendan 		uint64_t theguid;
32575450Sbrendan 
32585450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i],
32595450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
32605450Sbrendan 		if (theguid == guid) {
32615450Sbrendan 			nv = l2cache[i];
32625450Sbrendan 			break;
32635450Sbrendan 		}
32645450Sbrendan 	}
32655450Sbrendan 
32665450Sbrendan 	if (vd == NULL) {
32675450Sbrendan 		for (i = 0; i < nl2cache; i++) {
32685450Sbrendan 			if (sav->sav_vdevs[i]->vdev_guid == guid) {
32695450Sbrendan 				vd = sav->sav_vdevs[i];
32705450Sbrendan 				break;
32715450Sbrendan 			}
32725450Sbrendan 		}
32735450Sbrendan 	}
32745450Sbrendan 
32755450Sbrendan 	if (nv == NULL && vd == NULL)
32765450Sbrendan 		return (ENOENT);
32775450Sbrendan 
32785450Sbrendan 	if (nv == NULL && vd != NULL)
32795450Sbrendan 		return (ENOTSUP);
32805450Sbrendan 
32815450Sbrendan 	if (nl2cache == 1) {
32825450Sbrendan 		newl2cache = NULL;
32835450Sbrendan 	} else {
32845450Sbrendan 		newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *),
32855450Sbrendan 		    KM_SLEEP);
32865450Sbrendan 		for (i = 0, j = 0; i < nl2cache; i++) {
32875450Sbrendan 			if (l2cache[i] != nv)
32885450Sbrendan 				VERIFY(nvlist_dup(l2cache[i],
32895450Sbrendan 				    &newl2cache[j++], KM_SLEEP) == 0);
32905450Sbrendan 		}
32915450Sbrendan 	}
32925450Sbrendan 
32935450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
32945450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
32955450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
32965450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0);
32975450Sbrendan 	for (i = 0; i < nl2cache - 1; i++)
32985450Sbrendan 		nvlist_free(newl2cache[i]);
32995450Sbrendan 	kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *));
33005450Sbrendan 
33015450Sbrendan 	return (0);
33025450Sbrendan }
33035450Sbrendan 
33045450Sbrendan /*
33055450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
33065450Sbrendan  * spares and level 2 ARC devices.
33075450Sbrendan  */
33085450Sbrendan int
33095450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
33105450Sbrendan {
33115450Sbrendan 	vdev_t *vd;
33125450Sbrendan 	nvlist_t **spares, **l2cache;
33135450Sbrendan 	uint_t nspares, nl2cache;
33145450Sbrendan 	int error = 0;
33155450Sbrendan 
33165450Sbrendan 	spa_config_enter(spa, RW_WRITER, FTAG);
33175450Sbrendan 
33186643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
33195450Sbrendan 
33205450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
33215450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
33225450Sbrendan 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
33235450Sbrendan 		if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare,
33245450Sbrendan 		    spares, nspares, vd)) != 0)
33255450Sbrendan 			goto out;
33265450Sbrendan 		spa_load_spares(spa);
33275450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
33285450Sbrendan 		goto out;
33295450Sbrendan 	}
33305450Sbrendan 
33315450Sbrendan 	if (spa->spa_l2cache.sav_vdevs != NULL &&
33325450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
33335450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) {
33345450Sbrendan 		if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid,
33355450Sbrendan 		    l2cache, nl2cache, vd)) != 0)
33365450Sbrendan 			goto out;
33375450Sbrendan 		spa_load_l2cache(spa);
33385450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
33395450Sbrendan 	}
33402082Seschrock 
33412082Seschrock out:
33422082Seschrock 	spa_config_exit(spa, FTAG);
33435450Sbrendan 	return (error);
3344789Sahrens }
3345789Sahrens 
3346789Sahrens /*
33474451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
33484451Seschrock  * current spared, so we can detach it.
3349789Sahrens  */
33501544Seschrock static vdev_t *
33514451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3352789Sahrens {
33531544Seschrock 	vdev_t *newvd, *oldvd;
3354789Sahrens 	int c;
3355789Sahrens 
33561544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
33574451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
33581544Seschrock 		if (oldvd != NULL)
33591544Seschrock 			return (oldvd);
33601544Seschrock 	}
3361789Sahrens 
33624451Seschrock 	/*
33634451Seschrock 	 * Check for a completed replacement.
33644451Seschrock 	 */
3365789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
33661544Seschrock 		oldvd = vd->vdev_child[0];
33671544Seschrock 		newvd = vd->vdev_child[1];
3368789Sahrens 
33691544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33701544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
33711544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33721544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33731544Seschrock 			return (oldvd);
33741544Seschrock 		}
33751544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33761544Seschrock 	}
3377789Sahrens 
33784451Seschrock 	/*
33794451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
33804451Seschrock 	 */
33814451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
33824451Seschrock 		newvd = vd->vdev_child[0];
33834451Seschrock 		oldvd = vd->vdev_child[1];
33844451Seschrock 
33854451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
33864451Seschrock 		if (newvd->vdev_unspare &&
33874451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
33884451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
33894451Seschrock 			newvd->vdev_unspare = 0;
33904451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
33914451Seschrock 			return (oldvd);
33924451Seschrock 		}
33934451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
33944451Seschrock 	}
33954451Seschrock 
33961544Seschrock 	return (NULL);
3397789Sahrens }
3398789Sahrens 
33991544Seschrock static void
34004451Seschrock spa_vdev_resilver_done(spa_t *spa)
3401789Sahrens {
34021544Seschrock 	vdev_t *vd;
34032082Seschrock 	vdev_t *pvd;
34041544Seschrock 	uint64_t guid;
34052082Seschrock 	uint64_t pguid = 0;
3406789Sahrens 
34071544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3408789Sahrens 
34094451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
34101544Seschrock 		guid = vd->vdev_guid;
34112082Seschrock 		/*
34122082Seschrock 		 * If we have just finished replacing a hot spared device, then
34132082Seschrock 		 * we need to detach the parent's first child (the original hot
34142082Seschrock 		 * spare) as well.
34152082Seschrock 		 */
34162082Seschrock 		pvd = vd->vdev_parent;
34172082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
34182082Seschrock 		    pvd->vdev_id == 0) {
34192082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
34202082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
34212082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
34222082Seschrock 		}
34231544Seschrock 		spa_config_exit(spa, FTAG);
34241544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
34251544Seschrock 			return;
34262082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
34272082Seschrock 			return;
34281544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
3429789Sahrens 	}
3430789Sahrens 
34311544Seschrock 	spa_config_exit(spa, FTAG);
3432789Sahrens }
3433789Sahrens 
3434789Sahrens /*
34351354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
34361354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
34371354Seschrock  */
34381354Seschrock int
34391354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
34401354Seschrock {
34416643Seschrock 	vdev_t *vd;
34421354Seschrock 	uint64_t txg;
34431354Seschrock 
34441354Seschrock 	txg = spa_vdev_enter(spa);
34451354Seschrock 
34466643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
34472082Seschrock 		/*
34486643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
34496643Seschrock 		 * it is, update the path manually as there is no associated
34506643Seschrock 		 * vdev_t that can be synced to disk.
34512082Seschrock 		 */
34526643Seschrock 		nvlist_t **spares;
34536643Seschrock 		uint_t i, nspares;
34545450Sbrendan 
34555450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
34565450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
34575450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
34585450Sbrendan 			    &spares, &nspares) == 0);
34592082Seschrock 			for (i = 0; i < nspares; i++) {
34602082Seschrock 				uint64_t theguid;
34612082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
34622082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
34635450Sbrendan 				if (theguid == guid) {
34645450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
34655450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
34665450Sbrendan 					spa_load_spares(spa);
34675450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
34685450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
34695450Sbrendan 					    0));
34705450Sbrendan 				}
34712082Seschrock 			}
34722082Seschrock 		}
34735450Sbrendan 
34745450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
34752082Seschrock 	}
34761354Seschrock 
34771585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
34781585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34791585Sbonwick 
34801354Seschrock 	spa_strfree(vd->vdev_path);
34811354Seschrock 	vd->vdev_path = spa_strdup(newpath);
34821354Seschrock 
34831354Seschrock 	vdev_config_dirty(vd->vdev_top);
34841354Seschrock 
34851354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
34861354Seschrock }
34871354Seschrock 
34881354Seschrock /*
3489789Sahrens  * ==========================================================================
3490789Sahrens  * SPA Scrubbing
3491789Sahrens  * ==========================================================================
3492789Sahrens  */
3493789Sahrens 
34947046Sahrens int
34957046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3496789Sahrens {
34974808Sek110237 	ASSERT(!spa_config_held(spa, RW_WRITER));
34984808Sek110237 
3499789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3500789Sahrens 		return (ENOTSUP);
3501789Sahrens 
3502789Sahrens 	/*
35037046Sahrens 	 * If a resilver was requested, but there is no DTL on a
35047046Sahrens 	 * writeable leaf device, we have nothing to do.
3505789Sahrens 	 */
35067046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
35077046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
35087046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
35091544Seschrock 		return (0);
35101544Seschrock 	}
3511789Sahrens 
35127046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
35137046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
35147046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
35157046Sahrens 		return (EBUSY);
35167046Sahrens 
35177046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
35187046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
35197046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
35207046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
35211544Seschrock 	} else {
35227046Sahrens 		return (EINVAL);
35231544Seschrock 	}
3524789Sahrens }
3525789Sahrens 
35261544Seschrock /*
35271544Seschrock  * ==========================================================================
35281544Seschrock  * SPA async task processing
35291544Seschrock  * ==========================================================================
35301544Seschrock  */
35311544Seschrock 
35321544Seschrock static void
35334451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3534789Sahrens {
35351544Seschrock 	vdev_t *tvd;
35361544Seschrock 	int c;
35371544Seschrock 
35384451Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
35394451Seschrock 		tvd = vd->vdev_child[c];
35404451Seschrock 		if (tvd->vdev_remove_wanted) {
35414451Seschrock 			tvd->vdev_remove_wanted = 0;
35424451Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
35434451Seschrock 			    VDEV_AUX_NONE);
35445329Sgw25295 			vdev_clear(spa, tvd, B_TRUE);
35454451Seschrock 			vdev_config_dirty(tvd->vdev_top);
35461544Seschrock 		}
35474451Seschrock 		spa_async_remove(spa, tvd);
35481544Seschrock 	}
35491544Seschrock }
35501544Seschrock 
35511544Seschrock static void
35521544Seschrock spa_async_thread(spa_t *spa)
35531544Seschrock {
35541544Seschrock 	int tasks;
35554451Seschrock 	uint64_t txg;
35561544Seschrock 
35571544Seschrock 	ASSERT(spa->spa_sync_on);
3558789Sahrens 
35591544Seschrock 	mutex_enter(&spa->spa_async_lock);
35601544Seschrock 	tasks = spa->spa_async_tasks;
35611544Seschrock 	spa->spa_async_tasks = 0;
35621544Seschrock 	mutex_exit(&spa->spa_async_lock);
35631544Seschrock 
35641544Seschrock 	/*
35651635Sbonwick 	 * See if the config needs to be updated.
35661635Sbonwick 	 */
35671635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
35681635Sbonwick 		mutex_enter(&spa_namespace_lock);
35691635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
35701635Sbonwick 		mutex_exit(&spa_namespace_lock);
35711635Sbonwick 	}
35721635Sbonwick 
35731635Sbonwick 	/*
35744451Seschrock 	 * See if any devices need to be marked REMOVED.
35755329Sgw25295 	 *
35765329Sgw25295 	 * XXX - We avoid doing this when we are in
35775329Sgw25295 	 * I/O failure state since spa_vdev_enter() grabs
35785329Sgw25295 	 * the namespace lock and would not be able to obtain
35795329Sgw25295 	 * the writer config lock.
35801544Seschrock 	 */
35815329Sgw25295 	if (tasks & SPA_ASYNC_REMOVE &&
35825329Sgw25295 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
35834451Seschrock 		txg = spa_vdev_enter(spa);
35844451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
35854451Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
35864451Seschrock 	}
35871544Seschrock 
35881544Seschrock 	/*
35891544Seschrock 	 * If any devices are done replacing, detach them.
35901544Seschrock 	 */
35914451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
35924451Seschrock 		spa_vdev_resilver_done(spa);
3593789Sahrens 
35941544Seschrock 	/*
35951544Seschrock 	 * Kick off a resilver.
35961544Seschrock 	 */
35977046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
35987046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
35991544Seschrock 
36001544Seschrock 	/*
36011544Seschrock 	 * Let the world know that we're done.
36021544Seschrock 	 */
36031544Seschrock 	mutex_enter(&spa->spa_async_lock);
36041544Seschrock 	spa->spa_async_thread = NULL;
36051544Seschrock 	cv_broadcast(&spa->spa_async_cv);
36061544Seschrock 	mutex_exit(&spa->spa_async_lock);
36071544Seschrock 	thread_exit();
36081544Seschrock }
36091544Seschrock 
36101544Seschrock void
36111544Seschrock spa_async_suspend(spa_t *spa)
36121544Seschrock {
36131544Seschrock 	mutex_enter(&spa->spa_async_lock);
36141544Seschrock 	spa->spa_async_suspended++;
36151544Seschrock 	while (spa->spa_async_thread != NULL)
36161544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
36171544Seschrock 	mutex_exit(&spa->spa_async_lock);
36181544Seschrock }
36191544Seschrock 
36201544Seschrock void
36211544Seschrock spa_async_resume(spa_t *spa)
36221544Seschrock {
36231544Seschrock 	mutex_enter(&spa->spa_async_lock);
36241544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
36251544Seschrock 	spa->spa_async_suspended--;
36261544Seschrock 	mutex_exit(&spa->spa_async_lock);
36271544Seschrock }
36281544Seschrock 
36291544Seschrock static void
36301544Seschrock spa_async_dispatch(spa_t *spa)
36311544Seschrock {
36321544Seschrock 	mutex_enter(&spa->spa_async_lock);
36331544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
36341635Sbonwick 	    spa->spa_async_thread == NULL &&
36351635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
36361544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
36371544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
36381544Seschrock 	mutex_exit(&spa->spa_async_lock);
36391544Seschrock }
36401544Seschrock 
36411544Seschrock void
36421544Seschrock spa_async_request(spa_t *spa, int task)
36431544Seschrock {
36441544Seschrock 	mutex_enter(&spa->spa_async_lock);
36451544Seschrock 	spa->spa_async_tasks |= task;
36461544Seschrock 	mutex_exit(&spa->spa_async_lock);
3647789Sahrens }
3648789Sahrens 
3649789Sahrens /*
3650789Sahrens  * ==========================================================================
3651789Sahrens  * SPA syncing routines
3652789Sahrens  * ==========================================================================
3653789Sahrens  */
3654789Sahrens 
3655789Sahrens static void
3656789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3657789Sahrens {
3658789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3659789Sahrens 	dmu_tx_t *tx;
3660789Sahrens 	blkptr_t blk;
3661789Sahrens 	uint64_t itor = 0;
3662789Sahrens 	zio_t *zio;
3663789Sahrens 	int error;
3664789Sahrens 	uint8_t c = 1;
3665789Sahrens 
3666789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3667789Sahrens 
3668789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3669789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3670789Sahrens 
3671789Sahrens 	error = zio_wait(zio);
3672789Sahrens 	ASSERT3U(error, ==, 0);
3673789Sahrens 
3674789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3675789Sahrens 	bplist_vacate(bpl, tx);
3676789Sahrens 
3677789Sahrens 	/*
3678789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3679789Sahrens 	 * (Usually only the first block is needed.)
3680789Sahrens 	 */
3681789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3682789Sahrens 	dmu_tx_commit(tx);
3683789Sahrens }
3684789Sahrens 
3685789Sahrens static void
36862082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
36872082Seschrock {
36882082Seschrock 	char *packed = NULL;
36892082Seschrock 	size_t nvsize = 0;
36902082Seschrock 	dmu_buf_t *db;
36912082Seschrock 
36922082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
36932082Seschrock 
36942082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
36952082Seschrock 
36962082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
36972082Seschrock 	    KM_SLEEP) == 0);
36982082Seschrock 
36992082Seschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
37002082Seschrock 
37012082Seschrock 	kmem_free(packed, nvsize);
37022082Seschrock 
37032082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
37042082Seschrock 	dmu_buf_will_dirty(db, tx);
37052082Seschrock 	*(uint64_t *)db->db_data = nvsize;
37062082Seschrock 	dmu_buf_rele(db, FTAG);
37072082Seschrock }
37082082Seschrock 
37092082Seschrock static void
37105450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
37115450Sbrendan     const char *config, const char *entry)
37122082Seschrock {
37132082Seschrock 	nvlist_t *nvroot;
37145450Sbrendan 	nvlist_t **list;
37152082Seschrock 	int i;
37162082Seschrock 
37175450Sbrendan 	if (!sav->sav_sync)
37182082Seschrock 		return;
37192082Seschrock 
37202082Seschrock 	/*
37215450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
37225450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
37234451Seschrock 	 * valid and the vdevs are labeled appropriately.
37242082Seschrock 	 */
37255450Sbrendan 	if (sav->sav_object == 0) {
37265450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
37275450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
37285450Sbrendan 		    sizeof (uint64_t), tx);
37292082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
37305450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
37315450Sbrendan 		    &sav->sav_object, tx) == 0);
37322082Seschrock 	}
37332082Seschrock 
37342082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
37355450Sbrendan 	if (sav->sav_count == 0) {
37365450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
37372082Seschrock 	} else {
37385450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
37395450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
37405450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
37415450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
37425450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
37435450Sbrendan 		    sav->sav_count) == 0);
37445450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
37455450Sbrendan 			nvlist_free(list[i]);
37465450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
37472082Seschrock 	}
37482082Seschrock 
37495450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
37502926Sek110237 	nvlist_free(nvroot);
37512082Seschrock 
37525450Sbrendan 	sav->sav_sync = B_FALSE;
37532082Seschrock }
37542082Seschrock 
37552082Seschrock static void
3756789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3757789Sahrens {
3758789Sahrens 	nvlist_t *config;
3759789Sahrens 
3760789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
3761789Sahrens 		return;
3762789Sahrens 
3763789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
3764789Sahrens 
37651635Sbonwick 	if (spa->spa_config_syncing)
37661635Sbonwick 		nvlist_free(spa->spa_config_syncing);
37671635Sbonwick 	spa->spa_config_syncing = config;
3768789Sahrens 
37692082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3770789Sahrens }
3771789Sahrens 
37725094Slling /*
37735094Slling  * Set zpool properties.
37745094Slling  */
37753912Slling static void
37764543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
37773912Slling {
37783912Slling 	spa_t *spa = arg1;
37795094Slling 	objset_t *mos = spa->spa_meta_objset;
37803912Slling 	nvlist_t *nvp = arg2;
37815094Slling 	nvpair_t *elem;
37824451Seschrock 	uint64_t intval;
37836643Seschrock 	char *strval;
37845094Slling 	zpool_prop_t prop;
37855094Slling 	const char *propname;
37865094Slling 	zprop_type_t proptype;
37876643Seschrock 	spa_config_dirent_t *dp;
37885094Slling 
37895094Slling 	elem = NULL;
37905094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
37915094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
37925094Slling 		case ZPOOL_PROP_VERSION:
37935094Slling 			/*
37945094Slling 			 * Only set version for non-zpool-creation cases
37955094Slling 			 * (set/import). spa_create() needs special care
37965094Slling 			 * for version setting.
37975094Slling 			 */
37985094Slling 			if (tx->tx_txg != TXG_INITIAL) {
37995094Slling 				VERIFY(nvpair_value_uint64(elem,
38005094Slling 				    &intval) == 0);
38015094Slling 				ASSERT(intval <= SPA_VERSION);
38025094Slling 				ASSERT(intval >= spa_version(spa));
38035094Slling 				spa->spa_uberblock.ub_version = intval;
38045094Slling 				vdev_config_dirty(spa->spa_root_vdev);
38055094Slling 			}
38065094Slling 			break;
38075094Slling 
38085094Slling 		case ZPOOL_PROP_ALTROOT:
38095094Slling 			/*
38105094Slling 			 * 'altroot' is a non-persistent property. It should
38115094Slling 			 * have been set temporarily at creation or import time.
38125094Slling 			 */
38135094Slling 			ASSERT(spa->spa_root != NULL);
38145094Slling 			break;
38155094Slling 
38165363Seschrock 		case ZPOOL_PROP_CACHEFILE:
38175094Slling 			/*
38185363Seschrock 			 * 'cachefile' is a non-persistent property, but note
38195363Seschrock 			 * an async request that the config cache needs to be
38205363Seschrock 			 * udpated.
38215094Slling 			 */
38225363Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
38236643Seschrock 
38246643Seschrock 			dp = kmem_alloc(sizeof (spa_config_dirent_t),
38256643Seschrock 			    KM_SLEEP);
38266643Seschrock 
38276643Seschrock 			if (strval[0] == '\0')
38286643Seschrock 				dp->scd_path = spa_strdup(spa_config_path);
38296643Seschrock 			else if (strcmp(strval, "none") == 0)
38306643Seschrock 				dp->scd_path = NULL;
38316643Seschrock 			else
38326643Seschrock 				dp->scd_path = spa_strdup(strval);
38336643Seschrock 
38346643Seschrock 			list_insert_head(&spa->spa_config_list, dp);
38355363Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
38364543Smarks 			break;
38375094Slling 		default:
38385094Slling 			/*
38395094Slling 			 * Set pool property values in the poolprops mos object.
38405094Slling 			 */
38415094Slling 			mutex_enter(&spa->spa_props_lock);
38425094Slling 			if (spa->spa_pool_props_object == 0) {
38435094Slling 				objset_t *mos = spa->spa_meta_objset;
38445094Slling 
38455094Slling 				VERIFY((spa->spa_pool_props_object =
38465094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
38475094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
38485094Slling 
38495094Slling 				VERIFY(zap_update(mos,
38505094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
38515094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
38525094Slling 				    == 0);
38535094Slling 			}
38545094Slling 			mutex_exit(&spa->spa_props_lock);
38555094Slling 
38565094Slling 			/* normalize the property name */
38575094Slling 			propname = zpool_prop_to_name(prop);
38585094Slling 			proptype = zpool_prop_get_type(prop);
38595094Slling 
38605094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
38615094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
38625094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
38635094Slling 				VERIFY(zap_update(mos,
38645094Slling 				    spa->spa_pool_props_object, propname,
38655094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
38665094Slling 
38675094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
38685094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
38695094Slling 
38705094Slling 				if (proptype == PROP_TYPE_INDEX) {
38715094Slling 					const char *unused;
38725094Slling 					VERIFY(zpool_prop_index_to_string(
38735094Slling 					    prop, intval, &unused) == 0);
38745094Slling 				}
38755094Slling 				VERIFY(zap_update(mos,
38765094Slling 				    spa->spa_pool_props_object, propname,
38775094Slling 				    8, 1, &intval, tx) == 0);
38785094Slling 			} else {
38795094Slling 				ASSERT(0); /* not allowed */
38805094Slling 			}
38815094Slling 
38825329Sgw25295 			switch (prop) {
38835329Sgw25295 			case ZPOOL_PROP_DELEGATION:
38845094Slling 				spa->spa_delegation = intval;
38855329Sgw25295 				break;
38865329Sgw25295 			case ZPOOL_PROP_BOOTFS:
38875094Slling 				spa->spa_bootfs = intval;
38885329Sgw25295 				break;
38895329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
38905329Sgw25295 				spa->spa_failmode = intval;
38915329Sgw25295 				break;
38925329Sgw25295 			default:
38935329Sgw25295 				break;
38945329Sgw25295 			}
38953912Slling 		}
38965094Slling 
38975094Slling 		/* log internal history if this is not a zpool create */
38985094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
38995094Slling 		    tx->tx_txg != TXG_INITIAL) {
39005094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
39015094Slling 			    spa, tx, cr, "%s %lld %s",
39025094Slling 			    nvpair_name(elem), intval, spa->spa_name);
39035094Slling 		}
39043912Slling 	}
39053912Slling }
39063912Slling 
3907789Sahrens /*
3908789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3909789Sahrens  * part of the process, so we iterate until it converges.
3910789Sahrens  */
3911789Sahrens void
3912789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3913789Sahrens {
3914789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3915789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3916789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
39171635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3918789Sahrens 	vdev_t *vd;
3919789Sahrens 	dmu_tx_t *tx;
3920789Sahrens 	int dirty_vdevs;
3921789Sahrens 
3922789Sahrens 	/*
3923789Sahrens 	 * Lock out configuration changes.
3924789Sahrens 	 */
39251544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3926789Sahrens 
3927789Sahrens 	spa->spa_syncing_txg = txg;
3928789Sahrens 	spa->spa_sync_pass = 0;
3929789Sahrens 
39301544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3931789Sahrens 
39322082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
39332082Seschrock 
39342082Seschrock 	/*
39354577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
39362082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
39372082Seschrock 	 */
39384577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
39394577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
39402082Seschrock 		int i;
39412082Seschrock 
39422082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
39432082Seschrock 			vd = rvd->vdev_child[i];
39442082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
39452082Seschrock 				break;
39462082Seschrock 		}
39472082Seschrock 		if (i == rvd->vdev_children) {
39482082Seschrock 			spa->spa_deflate = TRUE;
39492082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
39502082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
39512082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
39522082Seschrock 		}
39532082Seschrock 	}
39542082Seschrock 
39557046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
39567046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
39577046Sahrens 		dsl_pool_create_origin(dp, tx);
39587046Sahrens 
39597046Sahrens 		/* Keeping the origin open increases spa_minref */
39607046Sahrens 		spa->spa_minref += 3;
39617046Sahrens 	}
39627046Sahrens 
39637046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
39647046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
39657046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
39667046Sahrens 	}
39677046Sahrens 
3968789Sahrens 	/*
3969789Sahrens 	 * If anything has changed in this txg, push the deferred frees
3970789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
3971789Sahrens 	 * don't generate work on an otherwise idle system.
3972789Sahrens 	 */
3973789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
39742329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
39752329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
3976789Sahrens 		spa_sync_deferred_frees(spa, txg);
3977789Sahrens 
3978789Sahrens 	/*
3979789Sahrens 	 * Iterate to convergence.
3980789Sahrens 	 */
3981789Sahrens 	do {
3982789Sahrens 		spa->spa_sync_pass++;
3983789Sahrens 
3984789Sahrens 		spa_sync_config_object(spa, tx);
39855450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
39865450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
39875450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
39885450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
39891544Seschrock 		spa_errlog_sync(spa, txg);
3990789Sahrens 		dsl_pool_sync(dp, txg);
3991789Sahrens 
3992789Sahrens 		dirty_vdevs = 0;
3993789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
3994789Sahrens 			vdev_sync(vd, txg);
3995789Sahrens 			dirty_vdevs++;
3996789Sahrens 		}
3997789Sahrens 
3998789Sahrens 		bplist_sync(bpl, tx);
3999789Sahrens 	} while (dirty_vdevs);
4000789Sahrens 
4001789Sahrens 	bplist_close(bpl);
4002789Sahrens 
4003789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4004789Sahrens 
4005789Sahrens 	/*
4006789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4007789Sahrens 	 * to commit the transaction group.
40081635Sbonwick 	 *
40095688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
40105688Sbonwick 	 * random top-level vdevs that are known to be visible in the
40115688Sbonwick 	 * config cache (see spa_vdev_add() for details).  If there *are*
40125688Sbonwick 	 * dirty vdevs -- or if the sync to our random subset fails --
40135688Sbonwick 	 * then sync the uberblock to all vdevs.
4014789Sahrens 	 */
40155688Sbonwick 	if (list_is_empty(&spa->spa_dirty_list)) {
40166615Sgw25295 		vdev_t *svd[SPA_DVAS_PER_BP];
40176615Sgw25295 		int svdcount = 0;
40181635Sbonwick 		int children = rvd->vdev_children;
40191635Sbonwick 		int c0 = spa_get_random(children);
40201635Sbonwick 		int c;
40211635Sbonwick 
40221635Sbonwick 		for (c = 0; c < children; c++) {
40231635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
40245688Sbonwick 			if (vd->vdev_ms_array == 0 || vd->vdev_islog)
40251635Sbonwick 				continue;
40265688Sbonwick 			svd[svdcount++] = vd;
40275688Sbonwick 			if (svdcount == SPA_DVAS_PER_BP)
40281635Sbonwick 				break;
40291635Sbonwick 		}
40306615Sgw25295 		vdev_config_sync(svd, svdcount, txg);
40316615Sgw25295 	} else {
40326615Sgw25295 		vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg);
40331635Sbonwick 	}
40342082Seschrock 	dmu_tx_commit(tx);
40352082Seschrock 
40361635Sbonwick 	/*
40371635Sbonwick 	 * Clear the dirty config list.
40381635Sbonwick 	 */
40391635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
40401635Sbonwick 		vdev_config_clean(vd);
40411635Sbonwick 
40421635Sbonwick 	/*
40431635Sbonwick 	 * Now that the new config has synced transactionally,
40441635Sbonwick 	 * let it become visible to the config cache.
40451635Sbonwick 	 */
40461635Sbonwick 	if (spa->spa_config_syncing != NULL) {
40471635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
40481635Sbonwick 		spa->spa_config_txg = txg;
40491635Sbonwick 		spa->spa_config_syncing = NULL;
40501635Sbonwick 	}
4051789Sahrens 
40527046Sahrens 	spa->spa_traverse_wanted = B_TRUE;
4053789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
40547046Sahrens 	spa->spa_traverse_wanted = B_FALSE;
4055789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4056789Sahrens 	rw_exit(&spa->spa_traverse_lock);
4057789Sahrens 
4058789Sahrens 	/*
4059789Sahrens 	 * Clean up the ZIL records for the synced txg.
4060789Sahrens 	 */
4061789Sahrens 	dsl_pool_zil_clean(dp);
4062789Sahrens 
4063789Sahrens 	/*
4064789Sahrens 	 * Update usable space statistics.
4065789Sahrens 	 */
4066789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4067789Sahrens 		vdev_sync_done(vd, txg);
4068789Sahrens 
4069789Sahrens 	/*
4070789Sahrens 	 * It had better be the case that we didn't dirty anything
40712082Seschrock 	 * since vdev_config_sync().
4072789Sahrens 	 */
4073789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4074789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4075789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4076789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4077789Sahrens 
40781544Seschrock 	spa_config_exit(spa, FTAG);
40791544Seschrock 
40801544Seschrock 	/*
40811544Seschrock 	 * If any async tasks have been requested, kick them off.
40821544Seschrock 	 */
40831544Seschrock 	spa_async_dispatch(spa);
4084789Sahrens }
4085789Sahrens 
4086789Sahrens /*
4087789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4088789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4089789Sahrens  * sync.
4090789Sahrens  */
4091789Sahrens void
4092789Sahrens spa_sync_allpools(void)
4093789Sahrens {
4094789Sahrens 	spa_t *spa = NULL;
4095789Sahrens 	mutex_enter(&spa_namespace_lock);
4096789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4097789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
4098789Sahrens 			continue;
4099789Sahrens 		spa_open_ref(spa, FTAG);
4100789Sahrens 		mutex_exit(&spa_namespace_lock);
4101789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4102789Sahrens 		mutex_enter(&spa_namespace_lock);
4103789Sahrens 		spa_close(spa, FTAG);
4104789Sahrens 	}
4105789Sahrens 	mutex_exit(&spa_namespace_lock);
4106789Sahrens }
4107789Sahrens 
4108789Sahrens /*
4109789Sahrens  * ==========================================================================
4110789Sahrens  * Miscellaneous routines
4111789Sahrens  * ==========================================================================
4112789Sahrens  */
4113789Sahrens 
4114789Sahrens /*
4115789Sahrens  * Remove all pools in the system.
4116789Sahrens  */
4117789Sahrens void
4118789Sahrens spa_evict_all(void)
4119789Sahrens {
4120789Sahrens 	spa_t *spa;
4121789Sahrens 
4122789Sahrens 	/*
4123789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4124789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4125789Sahrens 	 */
4126789Sahrens 	mutex_enter(&spa_namespace_lock);
4127789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4128789Sahrens 		/*
41291544Seschrock 		 * Stop async tasks.  The async thread may need to detach
41301544Seschrock 		 * a device that's been replaced, which requires grabbing
41311544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4132789Sahrens 		 */
4133789Sahrens 		spa_open_ref(spa, FTAG);
4134789Sahrens 		mutex_exit(&spa_namespace_lock);
41351544Seschrock 		spa_async_suspend(spa);
41364808Sek110237 		mutex_enter(&spa_namespace_lock);
4137789Sahrens 		spa_close(spa, FTAG);
4138789Sahrens 
4139789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4140789Sahrens 			spa_unload(spa);
4141789Sahrens 			spa_deactivate(spa);
4142789Sahrens 		}
4143789Sahrens 		spa_remove(spa);
4144789Sahrens 	}
4145789Sahrens 	mutex_exit(&spa_namespace_lock);
4146789Sahrens }
41471544Seschrock 
41481544Seschrock vdev_t *
41496643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
41501544Seschrock {
41516643Seschrock 	vdev_t *vd;
41526643Seschrock 	int i;
41536643Seschrock 
41546643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
41556643Seschrock 		return (vd);
41566643Seschrock 
41576643Seschrock 	if (l2cache) {
41586643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
41596643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
41606643Seschrock 			if (vd->vdev_guid == guid)
41616643Seschrock 				return (vd);
41626643Seschrock 		}
41636643Seschrock 	}
41646643Seschrock 
41656643Seschrock 	return (NULL);
41661544Seschrock }
41671760Seschrock 
41681760Seschrock void
41695094Slling spa_upgrade(spa_t *spa, uint64_t version)
41701760Seschrock {
41711760Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
41721760Seschrock 
41731760Seschrock 	/*
41741760Seschrock 	 * This should only be called for a non-faulted pool, and since a
41751760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
41761760Seschrock 	 * possible.
41771760Seschrock 	 */
41784577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
41795094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
41805094Slling 
41815094Slling 	spa->spa_uberblock.ub_version = version;
41821760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
41831760Seschrock 
41841760Seschrock 	spa_config_exit(spa, FTAG);
41852082Seschrock 
41862082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
41871760Seschrock }
41882082Seschrock 
41892082Seschrock boolean_t
41902082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
41912082Seschrock {
41922082Seschrock 	int i;
41933377Seschrock 	uint64_t spareguid;
41945450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
41955450Sbrendan 
41965450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
41975450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
41982082Seschrock 			return (B_TRUE);
41992082Seschrock 
42005450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
42015450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
42025450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
42033377Seschrock 			return (B_TRUE);
42043377Seschrock 	}
42053377Seschrock 
42062082Seschrock 	return (B_FALSE);
42072082Seschrock }
42083912Slling 
42094451Seschrock /*
42107214Slling  * Check if a pool has an active shared spare device.
42117214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
42127214Slling  */
42137214Slling static boolean_t
42147214Slling spa_has_active_shared_spare(spa_t *spa)
42157214Slling {
42167214Slling 	int i, refcnt;
42177214Slling 	uint64_t pool;
42187214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
42197214Slling 
42207214Slling 	for (i = 0; i < sav->sav_count; i++) {
42217214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
42227214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
42237214Slling 		    refcnt > 2)
42247214Slling 			return (B_TRUE);
42257214Slling 	}
42267214Slling 
42277214Slling 	return (B_FALSE);
42287214Slling }
42297214Slling 
42307214Slling /*
42314451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
42324451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
42334451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
42344451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
42354451Seschrock  * or zdb as real changes.
42364451Seschrock  */
42374451Seschrock void
42384451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
42394451Seschrock {
42404451Seschrock #ifdef _KERNEL
42414451Seschrock 	sysevent_t		*ev;
42424451Seschrock 	sysevent_attr_list_t	*attr = NULL;
42434451Seschrock 	sysevent_value_t	value;
42444451Seschrock 	sysevent_id_t		eid;
42454451Seschrock 
42464451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
42474451Seschrock 	    SE_SLEEP);
42484451Seschrock 
42494451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
42504451Seschrock 	value.value.sv_string = spa_name(spa);
42514451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
42524451Seschrock 		goto done;
42534451Seschrock 
42544451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
42554451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
42564451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
42574451Seschrock 		goto done;
42584451Seschrock 
42594451Seschrock 	if (vd) {
42604451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
42614451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
42624451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
42634451Seschrock 		    SE_SLEEP) != 0)
42644451Seschrock 			goto done;
42654451Seschrock 
42664451Seschrock 		if (vd->vdev_path) {
42674451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
42684451Seschrock 			value.value.sv_string = vd->vdev_path;
42694451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
42704451Seschrock 			    &value, SE_SLEEP) != 0)
42714451Seschrock 				goto done;
42724451Seschrock 		}
42734451Seschrock 	}
42744451Seschrock 
42755756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
42765756Seschrock 		goto done;
42775756Seschrock 	attr = NULL;
42785756Seschrock 
42794451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
42804451Seschrock 
42814451Seschrock done:
42824451Seschrock 	if (attr)
42834451Seschrock 		sysevent_free_attr(attr);
42844451Seschrock 	sysevent_free(ev);
42854451Seschrock #endif
42864451Seschrock }
4287