xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 6689)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
235756Seschrock  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens /*
30789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
31789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
32789Sahrens  * pool.
33789Sahrens  */
34789Sahrens 
35789Sahrens #include <sys/zfs_context.h>
361544Seschrock #include <sys/fm/fs/zfs.h>
37789Sahrens #include <sys/spa_impl.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zio_checksum.h>
40789Sahrens #include <sys/zio_compress.h>
41789Sahrens #include <sys/dmu.h>
42789Sahrens #include <sys/dmu_tx.h>
43789Sahrens #include <sys/zap.h>
44789Sahrens #include <sys/zil.h>
45789Sahrens #include <sys/vdev_impl.h>
46789Sahrens #include <sys/metaslab.h>
47789Sahrens #include <sys/uberblock_impl.h>
48789Sahrens #include <sys/txg.h>
49789Sahrens #include <sys/avl.h>
50789Sahrens #include <sys/dmu_traverse.h>
513912Slling #include <sys/dmu_objset.h>
52789Sahrens #include <sys/unique.h>
53789Sahrens #include <sys/dsl_pool.h>
543912Slling #include <sys/dsl_dataset.h>
55789Sahrens #include <sys/dsl_dir.h>
56789Sahrens #include <sys/dsl_prop.h>
573912Slling #include <sys/dsl_synctask.h>
58789Sahrens #include <sys/fs/zfs.h>
595450Sbrendan #include <sys/arc.h>
60789Sahrens #include <sys/callb.h>
613975Sek110237 #include <sys/systeminfo.h>
623975Sek110237 #include <sys/sunddi.h>
636423Sgw25295 #include <sys/spa_boot.h>
64789Sahrens 
655094Slling #include "zfs_prop.h"
665913Sperrin #include "zfs_comutil.h"
675094Slling 
682986Sek110237 int zio_taskq_threads = 8;
692986Sek110237 
705094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
715094Slling 
725094Slling /*
735094Slling  * ==========================================================================
745094Slling  * SPA properties routines
755094Slling  * ==========================================================================
765094Slling  */
775094Slling 
785094Slling /*
795094Slling  * Add a (source=src, propname=propval) list to an nvlist.
805094Slling  */
815949Slling static void
825094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
835094Slling     uint64_t intval, zprop_source_t src)
845094Slling {
855094Slling 	const char *propname = zpool_prop_to_name(prop);
865094Slling 	nvlist_t *propval;
875949Slling 
885949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
895949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
905949Slling 
915949Slling 	if (strval != NULL)
925949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
935949Slling 	else
945949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
955949Slling 
965949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
975094Slling 	nvlist_free(propval);
985094Slling }
995094Slling 
1005094Slling /*
1015094Slling  * Get property values from the spa configuration.
1025094Slling  */
1035949Slling static void
1045094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1055094Slling {
1065094Slling 	uint64_t size = spa_get_space(spa);
1075094Slling 	uint64_t used = spa_get_alloc(spa);
1085094Slling 	uint64_t cap, version;
1095094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1106643Seschrock 	spa_config_dirent_t *dp;
1115094Slling 
1125094Slling 	/*
1135094Slling 	 * readonly properties
1145094Slling 	 */
1155949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src);
1165949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1175949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1185949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
1195094Slling 
1205094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
1215949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1225949Slling 
1235949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1245949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1255949Slling 	    spa->spa_root_vdev->vdev_state, src);
1265094Slling 
1275094Slling 	/*
1285094Slling 	 * settable properties that are not stored in the pool property object.
1295094Slling 	 */
1305094Slling 	version = spa_version(spa);
1315094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1325094Slling 		src = ZPROP_SRC_DEFAULT;
1335094Slling 	else
1345094Slling 		src = ZPROP_SRC_LOCAL;
1355949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1365949Slling 
1375949Slling 	if (spa->spa_root != NULL)
1385949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1395949Slling 		    0, ZPROP_SRC_LOCAL);
1405094Slling 
1416643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1426643Seschrock 		if (dp->scd_path == NULL) {
1435949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1446643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1456643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1465949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1476643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1485363Seschrock 		}
1495363Seschrock 	}
1505094Slling }
1515094Slling 
1525094Slling /*
1535094Slling  * Get zpool property values.
1545094Slling  */
1555094Slling int
1565094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1575094Slling {
1585094Slling 	zap_cursor_t zc;
1595094Slling 	zap_attribute_t za;
1605094Slling 	objset_t *mos = spa->spa_meta_objset;
1615094Slling 	int err;
1625094Slling 
1635949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1645094Slling 
1655094Slling 	/*
1665094Slling 	 * Get properties from the spa config.
1675094Slling 	 */
1685949Slling 	spa_prop_get_config(spa, nvp);
1695094Slling 
1705094Slling 	mutex_enter(&spa->spa_props_lock);
1715094Slling 	/* If no pool property object, no more prop to get. */
1725094Slling 	if (spa->spa_pool_props_object == 0) {
1735094Slling 		mutex_exit(&spa->spa_props_lock);
1745094Slling 		return (0);
1755094Slling 	}
1765094Slling 
1775094Slling 	/*
1785094Slling 	 * Get properties from the MOS pool property object.
1795094Slling 	 */
1805094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1815094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1825094Slling 	    zap_cursor_advance(&zc)) {
1835094Slling 		uint64_t intval = 0;
1845094Slling 		char *strval = NULL;
1855094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
1865094Slling 		zpool_prop_t prop;
1875094Slling 
1885094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
1895094Slling 			continue;
1905094Slling 
1915094Slling 		switch (za.za_integer_length) {
1925094Slling 		case 8:
1935094Slling 			/* integer property */
1945094Slling 			if (za.za_first_integer !=
1955094Slling 			    zpool_prop_default_numeric(prop))
1965094Slling 				src = ZPROP_SRC_LOCAL;
1975094Slling 
1985094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
1995094Slling 				dsl_pool_t *dp;
2005094Slling 				dsl_dataset_t *ds = NULL;
2015094Slling 
2025094Slling 				dp = spa_get_dsl(spa);
2035094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
204*6689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
205*6689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2065094Slling 					rw_exit(&dp->dp_config_rwlock);
2075094Slling 					break;
2085094Slling 				}
2095094Slling 
2105094Slling 				strval = kmem_alloc(
2115094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2125094Slling 				    KM_SLEEP);
2135094Slling 				dsl_dataset_name(ds, strval);
214*6689Smaybee 				dsl_dataset_rele(ds, FTAG);
2155094Slling 				rw_exit(&dp->dp_config_rwlock);
2165094Slling 			} else {
2175094Slling 				strval = NULL;
2185094Slling 				intval = za.za_first_integer;
2195094Slling 			}
2205094Slling 
2215949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2225094Slling 
2235094Slling 			if (strval != NULL)
2245094Slling 				kmem_free(strval,
2255094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2265094Slling 
2275094Slling 			break;
2285094Slling 
2295094Slling 		case 1:
2305094Slling 			/* string property */
2315094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2325094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2335094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2345094Slling 			if (err) {
2355094Slling 				kmem_free(strval, za.za_num_integers);
2365094Slling 				break;
2375094Slling 			}
2385949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2395094Slling 			kmem_free(strval, za.za_num_integers);
2405094Slling 			break;
2415094Slling 
2425094Slling 		default:
2435094Slling 			break;
2445094Slling 		}
2455094Slling 	}
2465094Slling 	zap_cursor_fini(&zc);
2475094Slling 	mutex_exit(&spa->spa_props_lock);
2485094Slling out:
2495094Slling 	if (err && err != ENOENT) {
2505094Slling 		nvlist_free(*nvp);
2515949Slling 		*nvp = NULL;
2525094Slling 		return (err);
2535094Slling 	}
2545094Slling 
2555094Slling 	return (0);
2565094Slling }
2575094Slling 
2585094Slling /*
2595094Slling  * Validate the given pool properties nvlist and modify the list
2605094Slling  * for the property values to be set.
2615094Slling  */
2625094Slling static int
2635094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2645094Slling {
2655094Slling 	nvpair_t *elem;
2665094Slling 	int error = 0, reset_bootfs = 0;
2675094Slling 	uint64_t objnum;
2685094Slling 
2695094Slling 	elem = NULL;
2705094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2715094Slling 		zpool_prop_t prop;
2725094Slling 		char *propname, *strval;
2735094Slling 		uint64_t intval;
2745094Slling 		vdev_t *rvdev;
2755094Slling 		char *vdev_type;
2765094Slling 		objset_t *os;
2775363Seschrock 		char *slash;
2785094Slling 
2795094Slling 		propname = nvpair_name(elem);
2805094Slling 
2815094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2825094Slling 			return (EINVAL);
2835094Slling 
2845094Slling 		switch (prop) {
2855094Slling 		case ZPOOL_PROP_VERSION:
2865094Slling 			error = nvpair_value_uint64(elem, &intval);
2875094Slling 			if (!error &&
2885094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
2895094Slling 				error = EINVAL;
2905094Slling 			break;
2915094Slling 
2925094Slling 		case ZPOOL_PROP_DELEGATION:
2935094Slling 		case ZPOOL_PROP_AUTOREPLACE:
2945094Slling 			error = nvpair_value_uint64(elem, &intval);
2955094Slling 			if (!error && intval > 1)
2965094Slling 				error = EINVAL;
2975094Slling 			break;
2985094Slling 
2995094Slling 		case ZPOOL_PROP_BOOTFS:
3005094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3015094Slling 				error = ENOTSUP;
3025094Slling 				break;
3035094Slling 			}
3045094Slling 
3055094Slling 			/*
3065094Slling 			 * A bootable filesystem can not be on a RAIDZ pool
3075094Slling 			 * nor a striped pool with more than 1 device.
3085094Slling 			 */
3095094Slling 			rvdev = spa->spa_root_vdev;
3105094Slling 			vdev_type =
3115094Slling 			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
3125094Slling 			if (rvdev->vdev_children > 1 ||
3135094Slling 			    strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
3145094Slling 			    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3155094Slling 				error = ENOTSUP;
3165094Slling 				break;
3175094Slling 			}
3185094Slling 
3195094Slling 			reset_bootfs = 1;
3205094Slling 
3215094Slling 			error = nvpair_value_string(elem, &strval);
3225094Slling 
3235094Slling 			if (!error) {
3245094Slling 				if (strval == NULL || strval[0] == '\0') {
3255094Slling 					objnum = zpool_prop_default_numeric(
3265094Slling 					    ZPOOL_PROP_BOOTFS);
3275094Slling 					break;
3285094Slling 				}
3295094Slling 
3305094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
331*6689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3325094Slling 					break;
3335094Slling 				objnum = dmu_objset_id(os);
3345094Slling 				dmu_objset_close(os);
3355094Slling 			}
3365094Slling 			break;
3375329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3385329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3395329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3405329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3415329Sgw25295 				error = EINVAL;
3425329Sgw25295 
3435329Sgw25295 			/*
3445329Sgw25295 			 * This is a special case which only occurs when
3455329Sgw25295 			 * the pool has completely failed. This allows
3465329Sgw25295 			 * the user to change the in-core failmode property
3475329Sgw25295 			 * without syncing it out to disk (I/Os might
3485329Sgw25295 			 * currently be blocked). We do this by returning
3495329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3505329Sgw25295 			 * into thinking we encountered a property validation
3515329Sgw25295 			 * error.
3525329Sgw25295 			 */
3535329Sgw25295 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3545329Sgw25295 				spa->spa_failmode = intval;
3555329Sgw25295 				error = EIO;
3565329Sgw25295 			}
3575329Sgw25295 			break;
3585363Seschrock 
3595363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3605363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3615363Seschrock 				break;
3625363Seschrock 
3635363Seschrock 			if (strval[0] == '\0')
3645363Seschrock 				break;
3655363Seschrock 
3665363Seschrock 			if (strcmp(strval, "none") == 0)
3675363Seschrock 				break;
3685363Seschrock 
3695363Seschrock 			if (strval[0] != '/') {
3705363Seschrock 				error = EINVAL;
3715363Seschrock 				break;
3725363Seschrock 			}
3735363Seschrock 
3745363Seschrock 			slash = strrchr(strval, '/');
3755363Seschrock 			ASSERT(slash != NULL);
3765363Seschrock 
3775363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3785363Seschrock 			    strcmp(slash, "/..") == 0)
3795363Seschrock 				error = EINVAL;
3805363Seschrock 			break;
3815094Slling 		}
3825094Slling 
3835094Slling 		if (error)
3845094Slling 			break;
3855094Slling 	}
3865094Slling 
3875094Slling 	if (!error && reset_bootfs) {
3885094Slling 		error = nvlist_remove(props,
3895094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
3905094Slling 
3915094Slling 		if (!error) {
3925094Slling 			error = nvlist_add_uint64(props,
3935094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
3945094Slling 		}
3955094Slling 	}
3965094Slling 
3975094Slling 	return (error);
3985094Slling }
3995094Slling 
4005094Slling int
4015094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4025094Slling {
4035094Slling 	int error;
4045094Slling 
4055094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4065094Slling 		return (error);
4075094Slling 
4085094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4095094Slling 	    spa, nvp, 3));
4105094Slling }
4115094Slling 
4125094Slling /*
4135094Slling  * If the bootfs property value is dsobj, clear it.
4145094Slling  */
4155094Slling void
4165094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4175094Slling {
4185094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4195094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4205094Slling 		    spa->spa_pool_props_object,
4215094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4225094Slling 		spa->spa_bootfs = 0;
4235094Slling 	}
4245094Slling }
4255094Slling 
426789Sahrens /*
427789Sahrens  * ==========================================================================
428789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
429789Sahrens  * ==========================================================================
430789Sahrens  */
431789Sahrens 
4321544Seschrock static int
4331544Seschrock spa_error_entry_compare(const void *a, const void *b)
4341544Seschrock {
4351544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4361544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4371544Seschrock 	int ret;
4381544Seschrock 
4391544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4401544Seschrock 	    sizeof (zbookmark_t));
4411544Seschrock 
4421544Seschrock 	if (ret < 0)
4431544Seschrock 		return (-1);
4441544Seschrock 	else if (ret > 0)
4451544Seschrock 		return (1);
4461544Seschrock 	else
4471544Seschrock 		return (0);
4481544Seschrock }
4491544Seschrock 
4501544Seschrock /*
4511544Seschrock  * Utility function which retrieves copies of the current logs and
4521544Seschrock  * re-initializes them in the process.
4531544Seschrock  */
4541544Seschrock void
4551544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4561544Seschrock {
4571544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4581544Seschrock 
4591544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4601544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4611544Seschrock 
4621544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4631544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4641544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4651544Seschrock 	avl_create(&spa->spa_errlist_last,
4661544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4671544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4681544Seschrock }
4691544Seschrock 
470789Sahrens /*
471789Sahrens  * Activate an uninitialized pool.
472789Sahrens  */
473789Sahrens static void
474789Sahrens spa_activate(spa_t *spa)
475789Sahrens {
476789Sahrens 	int t;
477789Sahrens 
478789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
479789Sahrens 
480789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
481789Sahrens 
482789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4834527Sperrin 	spa->spa_log_class = metaslab_class_create();
484789Sahrens 
485789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
486789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
4872986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
488789Sahrens 		    TASKQ_PREPOPULATE);
489789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
4902986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
491789Sahrens 		    TASKQ_PREPOPULATE);
492789Sahrens 	}
493789Sahrens 
494789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
495789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
4965329Sgw25295 	list_create(&spa->spa_zio_list, sizeof (zio_t),
4975329Sgw25295 	    offsetof(zio_t, zio_link_node));
498789Sahrens 
499789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
500789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
5011544Seschrock 
5021544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5031544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5041544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5051544Seschrock 	avl_create(&spa->spa_errlist_last,
5061544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5071544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
508789Sahrens }
509789Sahrens 
510789Sahrens /*
511789Sahrens  * Opposite of spa_activate().
512789Sahrens  */
513789Sahrens static void
514789Sahrens spa_deactivate(spa_t *spa)
515789Sahrens {
516789Sahrens 	int t;
517789Sahrens 
518789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
519789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
520789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
521789Sahrens 
522789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
523789Sahrens 
524789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
525789Sahrens 
526789Sahrens 	list_destroy(&spa->spa_dirty_list);
5275329Sgw25295 	list_destroy(&spa->spa_zio_list);
528789Sahrens 
529789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
530789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
531789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
532789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
533789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
534789Sahrens 	}
535789Sahrens 
536789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
537789Sahrens 	spa->spa_normal_class = NULL;
538789Sahrens 
5394527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5404527Sperrin 	spa->spa_log_class = NULL;
5414527Sperrin 
5421544Seschrock 	/*
5431544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5441544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5451544Seschrock 	 */
5461544Seschrock 	spa_errlog_drain(spa);
5471544Seschrock 
5481544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5491544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5501544Seschrock 
551789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
552789Sahrens }
553789Sahrens 
554789Sahrens /*
555789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
556789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
557789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
558789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
559789Sahrens  */
5602082Seschrock static int
5612082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5622082Seschrock     uint_t id, int atype)
563789Sahrens {
564789Sahrens 	nvlist_t **child;
565789Sahrens 	uint_t c, children;
5662082Seschrock 	int error;
5672082Seschrock 
5682082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5692082Seschrock 		return (error);
5702082Seschrock 
5712082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5722082Seschrock 		return (0);
573789Sahrens 
574789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
575789Sahrens 	    &child, &children) != 0) {
5762082Seschrock 		vdev_free(*vdp);
5772082Seschrock 		*vdp = NULL;
5782082Seschrock 		return (EINVAL);
579789Sahrens 	}
580789Sahrens 
581789Sahrens 	for (c = 0; c < children; c++) {
5822082Seschrock 		vdev_t *vd;
5832082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5842082Seschrock 		    atype)) != 0) {
5852082Seschrock 			vdev_free(*vdp);
5862082Seschrock 			*vdp = NULL;
5872082Seschrock 			return (error);
588789Sahrens 		}
589789Sahrens 	}
590789Sahrens 
5912082Seschrock 	ASSERT(*vdp != NULL);
5922082Seschrock 
5932082Seschrock 	return (0);
594789Sahrens }
595789Sahrens 
596789Sahrens /*
597789Sahrens  * Opposite of spa_load().
598789Sahrens  */
599789Sahrens static void
600789Sahrens spa_unload(spa_t *spa)
601789Sahrens {
6022082Seschrock 	int i;
6032082Seschrock 
604789Sahrens 	/*
6051544Seschrock 	 * Stop async tasks.
6061544Seschrock 	 */
6071544Seschrock 	spa_async_suspend(spa);
6081544Seschrock 
6091544Seschrock 	/*
610789Sahrens 	 * Stop syncing.
611789Sahrens 	 */
612789Sahrens 	if (spa->spa_sync_on) {
613789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
614789Sahrens 		spa->spa_sync_on = B_FALSE;
615789Sahrens 	}
616789Sahrens 
617789Sahrens 	/*
618789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
619789Sahrens 	 */
6201544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
6211544Seschrock 	spa_config_exit(spa, FTAG);
622789Sahrens 
623789Sahrens 	/*
6245450Sbrendan 	 * Drop and purge level 2 cache
6255450Sbrendan 	 */
6265450Sbrendan 	spa_l2cache_drop(spa);
6275450Sbrendan 
6285450Sbrendan 	/*
629789Sahrens 	 * Close the dsl pool.
630789Sahrens 	 */
631789Sahrens 	if (spa->spa_dsl_pool) {
632789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
633789Sahrens 		spa->spa_dsl_pool = NULL;
634789Sahrens 	}
635789Sahrens 
636789Sahrens 	/*
637789Sahrens 	 * Close all vdevs.
638789Sahrens 	 */
6391585Sbonwick 	if (spa->spa_root_vdev)
640789Sahrens 		vdev_free(spa->spa_root_vdev);
6411585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6421544Seschrock 
6435450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
6445450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
6455450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
6465450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
6475450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
6485450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
6495450Sbrendan 	}
6505450Sbrendan 	if (spa->spa_spares.sav_config) {
6515450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
6525450Sbrendan 		spa->spa_spares.sav_config = NULL;
6532082Seschrock 	}
6545450Sbrendan 
6555450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
6565450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
6575450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
6585450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
6595450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
6605450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
6615450Sbrendan 	}
6625450Sbrendan 	if (spa->spa_l2cache.sav_config) {
6635450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
6645450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
6652082Seschrock 	}
6662082Seschrock 
6671544Seschrock 	spa->spa_async_suspended = 0;
668789Sahrens }
669789Sahrens 
670789Sahrens /*
6712082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6722082Seschrock  * this pool.  When this is called, we have some form of basic information in
6735450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
6745450Sbrendan  * then re-generate a more complete list including status information.
6752082Seschrock  */
6762082Seschrock static void
6772082Seschrock spa_load_spares(spa_t *spa)
6782082Seschrock {
6792082Seschrock 	nvlist_t **spares;
6802082Seschrock 	uint_t nspares;
6812082Seschrock 	int i;
6823377Seschrock 	vdev_t *vd, *tvd;
6832082Seschrock 
6842082Seschrock 	/*
6852082Seschrock 	 * First, close and free any existing spare vdevs.
6862082Seschrock 	 */
6875450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
6885450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
6893377Seschrock 
6903377Seschrock 		/* Undo the call to spa_activate() below */
6916643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
6926643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
6933377Seschrock 			spa_spare_remove(tvd);
6943377Seschrock 		vdev_close(vd);
6953377Seschrock 		vdev_free(vd);
6962082Seschrock 	}
6973377Seschrock 
6985450Sbrendan 	if (spa->spa_spares.sav_vdevs)
6995450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7005450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7015450Sbrendan 
7025450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7032082Seschrock 		nspares = 0;
7042082Seschrock 	else
7055450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7062082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7072082Seschrock 
7085450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7095450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7102082Seschrock 
7112082Seschrock 	if (nspares == 0)
7122082Seschrock 		return;
7132082Seschrock 
7142082Seschrock 	/*
7152082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7163377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7173377Seschrock 	 * structures associated with it: one in the list of spares (used only
7183377Seschrock 	 * for basic validation purposes) and one in the active vdev
7193377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7203377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7213377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7222082Seschrock 	 */
7235450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
7245450Sbrendan 	    KM_SLEEP);
7255450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7262082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
7272082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
7282082Seschrock 		ASSERT(vd != NULL);
7292082Seschrock 
7305450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
7312082Seschrock 
7326643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7336643Seschrock 		    B_FALSE)) != NULL) {
7343377Seschrock 			if (!tvd->vdev_isspare)
7353377Seschrock 				spa_spare_add(tvd);
7363377Seschrock 
7373377Seschrock 			/*
7383377Seschrock 			 * We only mark the spare active if we were successfully
7393377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7403377Seschrock 			 * with a bad active spare would result in strange
7413377Seschrock 			 * behavior, because multiple pool would think the spare
7423377Seschrock 			 * is actively in use.
7433377Seschrock 			 *
7443377Seschrock 			 * There is a vulnerability here to an equally bizarre
7453377Seschrock 			 * circumstance, where a dead active spare is later
7463377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7473377Seschrock 			 * the rarity of this scenario, and the extra complexity
7483377Seschrock 			 * it adds, we ignore the possibility.
7493377Seschrock 			 */
7503377Seschrock 			if (!vdev_is_dead(tvd))
7513377Seschrock 				spa_spare_activate(tvd);
7523377Seschrock 		}
7533377Seschrock 
7542082Seschrock 		if (vdev_open(vd) != 0)
7552082Seschrock 			continue;
7562082Seschrock 
7572082Seschrock 		vd->vdev_top = vd;
7585450Sbrendan 		if (vdev_validate_aux(vd) == 0)
7595450Sbrendan 			spa_spare_add(vd);
7602082Seschrock 	}
7612082Seschrock 
7622082Seschrock 	/*
7632082Seschrock 	 * Recompute the stashed list of spares, with status information
7642082Seschrock 	 * this time.
7652082Seschrock 	 */
7665450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
7672082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7682082Seschrock 
7695450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
7705450Sbrendan 	    KM_SLEEP);
7715450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7725450Sbrendan 		spares[i] = vdev_config_generate(spa,
7735450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
7745450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
7755450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
7765450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7772082Seschrock 		nvlist_free(spares[i]);
7785450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
7795450Sbrendan }
7805450Sbrendan 
7815450Sbrendan /*
7825450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
7835450Sbrendan  * this pool.  When this is called, we have some form of basic information in
7845450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
7855450Sbrendan  * then re-generate a more complete list including status information.
7865450Sbrendan  * Devices which are already active have their details maintained, and are
7875450Sbrendan  * not re-opened.
7885450Sbrendan  */
7895450Sbrendan static void
7905450Sbrendan spa_load_l2cache(spa_t *spa)
7915450Sbrendan {
7925450Sbrendan 	nvlist_t **l2cache;
7935450Sbrendan 	uint_t nl2cache;
7945450Sbrendan 	int i, j, oldnvdevs;
7956643Seschrock 	uint64_t guid, size;
7965450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
7975450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
7985450Sbrendan 
7995450Sbrendan 	if (sav->sav_config != NULL) {
8005450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8015450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8025450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8035450Sbrendan 	} else {
8045450Sbrendan 		nl2cache = 0;
8055450Sbrendan 	}
8065450Sbrendan 
8075450Sbrendan 	oldvdevs = sav->sav_vdevs;
8085450Sbrendan 	oldnvdevs = sav->sav_count;
8095450Sbrendan 	sav->sav_vdevs = NULL;
8105450Sbrendan 	sav->sav_count = 0;
8115450Sbrendan 
8125450Sbrendan 	/*
8135450Sbrendan 	 * Process new nvlist of vdevs.
8145450Sbrendan 	 */
8155450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8165450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8175450Sbrendan 		    &guid) == 0);
8185450Sbrendan 
8195450Sbrendan 		newvdevs[i] = NULL;
8205450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
8215450Sbrendan 			vd = oldvdevs[j];
8225450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
8235450Sbrendan 				/*
8245450Sbrendan 				 * Retain previous vdev for add/remove ops.
8255450Sbrendan 				 */
8265450Sbrendan 				newvdevs[i] = vd;
8275450Sbrendan 				oldvdevs[j] = NULL;
8285450Sbrendan 				break;
8295450Sbrendan 			}
8305450Sbrendan 		}
8315450Sbrendan 
8325450Sbrendan 		if (newvdevs[i] == NULL) {
8335450Sbrendan 			/*
8345450Sbrendan 			 * Create new vdev
8355450Sbrendan 			 */
8365450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
8375450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
8385450Sbrendan 			ASSERT(vd != NULL);
8395450Sbrendan 			newvdevs[i] = vd;
8405450Sbrendan 
8415450Sbrendan 			/*
8425450Sbrendan 			 * Commit this vdev as an l2cache device,
8435450Sbrendan 			 * even if it fails to open.
8445450Sbrendan 			 */
8455450Sbrendan 			spa_l2cache_add(vd);
8465450Sbrendan 
8476643Seschrock 			vd->vdev_top = vd;
8486643Seschrock 			vd->vdev_aux = sav;
8496643Seschrock 
8506643Seschrock 			spa_l2cache_activate(vd);
8516643Seschrock 
8525450Sbrendan 			if (vdev_open(vd) != 0)
8535450Sbrendan 				continue;
8545450Sbrendan 
8555450Sbrendan 			(void) vdev_validate_aux(vd);
8565450Sbrendan 
8575450Sbrendan 			if (!vdev_is_dead(vd)) {
8585450Sbrendan 				size = vdev_get_rsize(vd);
8596643Seschrock 				l2arc_add_vdev(spa, vd,
8606643Seschrock 				    VDEV_LABEL_START_SIZE,
8616643Seschrock 				    size - VDEV_LABEL_START_SIZE);
8625450Sbrendan 			}
8635450Sbrendan 		}
8645450Sbrendan 	}
8655450Sbrendan 
8665450Sbrendan 	/*
8675450Sbrendan 	 * Purge vdevs that were dropped
8685450Sbrendan 	 */
8695450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
8705450Sbrendan 		uint64_t pool;
8715450Sbrendan 
8725450Sbrendan 		vd = oldvdevs[i];
8735450Sbrendan 		if (vd != NULL) {
8745450Sbrendan 			if (spa_mode & FWRITE &&
8755450Sbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
8766643Seschrock 			    pool != 0ULL &&
8776643Seschrock 			    l2arc_vdev_present(vd)) {
8785450Sbrendan 				l2arc_remove_vdev(vd);
8795450Sbrendan 			}
8805450Sbrendan 			(void) vdev_close(vd);
8815450Sbrendan 			spa_l2cache_remove(vd);
8825450Sbrendan 		}
8835450Sbrendan 	}
8845450Sbrendan 
8855450Sbrendan 	if (oldvdevs)
8865450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
8875450Sbrendan 
8885450Sbrendan 	if (sav->sav_config == NULL)
8895450Sbrendan 		goto out;
8905450Sbrendan 
8915450Sbrendan 	sav->sav_vdevs = newvdevs;
8925450Sbrendan 	sav->sav_count = (int)nl2cache;
8935450Sbrendan 
8945450Sbrendan 	/*
8955450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
8965450Sbrendan 	 * information this time.
8975450Sbrendan 	 */
8985450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
8995450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9005450Sbrendan 
9015450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9025450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9035450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9045450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9055450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9065450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9075450Sbrendan out:
9085450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9095450Sbrendan 		nvlist_free(l2cache[i]);
9105450Sbrendan 	if (sav->sav_count)
9115450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9122082Seschrock }
9132082Seschrock 
9142082Seschrock static int
9152082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9162082Seschrock {
9172082Seschrock 	dmu_buf_t *db;
9182082Seschrock 	char *packed = NULL;
9192082Seschrock 	size_t nvsize = 0;
9202082Seschrock 	int error;
9212082Seschrock 	*value = NULL;
9222082Seschrock 
9232082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9242082Seschrock 	nvsize = *(uint64_t *)db->db_data;
9252082Seschrock 	dmu_buf_rele(db, FTAG);
9262082Seschrock 
9272082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
9282082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
9292082Seschrock 	if (error == 0)
9302082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
9312082Seschrock 	kmem_free(packed, nvsize);
9322082Seschrock 
9332082Seschrock 	return (error);
9342082Seschrock }
9352082Seschrock 
9362082Seschrock /*
9374451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9384451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9394451Seschrock  */
9404451Seschrock static void
9414451Seschrock spa_check_removed(vdev_t *vd)
9424451Seschrock {
9434451Seschrock 	int c;
9444451Seschrock 
9454451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9464451Seschrock 		spa_check_removed(vd->vdev_child[c]);
9474451Seschrock 
9484451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9494451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9504451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9514451Seschrock 	}
9524451Seschrock }
9534451Seschrock 
9544451Seschrock /*
955789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
9561544Seschrock  * source of configuration information.
957789Sahrens  */
958789Sahrens static int
9591544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
960789Sahrens {
961789Sahrens 	int error = 0;
962789Sahrens 	nvlist_t *nvroot = NULL;
963789Sahrens 	vdev_t *rvd;
964789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
9651635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
966789Sahrens 	uint64_t pool_guid;
9672082Seschrock 	uint64_t version;
968789Sahrens 	zio_t *zio;
9694451Seschrock 	uint64_t autoreplace = 0;
970789Sahrens 
9711544Seschrock 	spa->spa_load_state = state;
9721635Sbonwick 
973789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
9741733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
9751544Seschrock 		error = EINVAL;
9761544Seschrock 		goto out;
9771544Seschrock 	}
978789Sahrens 
9792082Seschrock 	/*
9802082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
9812082Seschrock 	 * it's not present treat it as the initial version.
9822082Seschrock 	 */
9832082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
9844577Sahrens 		version = SPA_VERSION_INITIAL;
9852082Seschrock 
9861733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
9871733Sbonwick 	    &spa->spa_config_txg);
9881733Sbonwick 
9891635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
9901544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
9911544Seschrock 		error = EEXIST;
9921544Seschrock 		goto out;
9931544Seschrock 	}
994789Sahrens 
9952174Seschrock 	spa->spa_load_guid = pool_guid;
9962174Seschrock 
997789Sahrens 	/*
9982082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
9992082Seschrock 	 * value that will be returned by spa_version() since parsing the
10002082Seschrock 	 * configuration requires knowing the version number.
1001789Sahrens 	 */
10021544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
10032082Seschrock 	spa->spa_ubsync.ub_version = version;
10042082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
10051544Seschrock 	spa_config_exit(spa, FTAG);
1006789Sahrens 
10072082Seschrock 	if (error != 0)
10081544Seschrock 		goto out;
1009789Sahrens 
10101585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1011789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1012789Sahrens 
1013789Sahrens 	/*
1014789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1015789Sahrens 	 */
10164070Smc142369 	error = vdev_open(rvd);
10174070Smc142369 	if (error != 0)
10181544Seschrock 		goto out;
1019789Sahrens 
1020789Sahrens 	/*
10211986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
10221986Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
10231986Seschrock 	 * flag.
10241986Seschrock 	 */
10251986Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
10261986Seschrock 	error = vdev_validate(rvd);
10271986Seschrock 	spa_config_exit(spa, FTAG);
10281986Seschrock 
10294070Smc142369 	if (error != 0)
10301986Seschrock 		goto out;
10311986Seschrock 
10321986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
10331986Seschrock 		error = ENXIO;
10341986Seschrock 		goto out;
10351986Seschrock 	}
10361986Seschrock 
10371986Seschrock 	/*
1038789Sahrens 	 * Find the best uberblock.
1039789Sahrens 	 */
1040789Sahrens 	bzero(ub, sizeof (uberblock_t));
1041789Sahrens 
1042789Sahrens 	zio = zio_root(spa, NULL, NULL,
1043789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1044789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
1045789Sahrens 	error = zio_wait(zio);
1046789Sahrens 
1047789Sahrens 	/*
1048789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1049789Sahrens 	 */
1050789Sahrens 	if (ub->ub_txg == 0) {
10511760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10521760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10531544Seschrock 		error = ENXIO;
10541544Seschrock 		goto out;
10551544Seschrock 	}
10561544Seschrock 
10571544Seschrock 	/*
10581544Seschrock 	 * If the pool is newer than the code, we can't open it.
10591544Seschrock 	 */
10604577Sahrens 	if (ub->ub_version > SPA_VERSION) {
10611760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10621760Seschrock 		    VDEV_AUX_VERSION_NEWER);
10631544Seschrock 		error = ENOTSUP;
10641544Seschrock 		goto out;
1065789Sahrens 	}
1066789Sahrens 
1067789Sahrens 	/*
1068789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1069789Sahrens 	 * incomplete configuration.
1070789Sahrens 	 */
10711732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
10721544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10731544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
10741544Seschrock 		error = ENXIO;
10751544Seschrock 		goto out;
1076789Sahrens 	}
1077789Sahrens 
1078789Sahrens 	/*
1079789Sahrens 	 * Initialize internal SPA structures.
1080789Sahrens 	 */
1081789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1082789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1083789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
10841544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
10851544Seschrock 	if (error) {
10861544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10871544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10881544Seschrock 		goto out;
10891544Seschrock 	}
1090789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1091789Sahrens 
10921544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1093789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
10941544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
10951544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10961544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10971544Seschrock 		error = EIO;
10981544Seschrock 		goto out;
10991544Seschrock 	}
1100789Sahrens 
1101789Sahrens 	if (!mosconfig) {
11022082Seschrock 		nvlist_t *newconfig;
11033975Sek110237 		uint64_t hostid;
11042082Seschrock 
11052082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
11061544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11071544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11081544Seschrock 			error = EIO;
11091544Seschrock 			goto out;
11101544Seschrock 		}
1111789Sahrens 
11123975Sek110237 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
11133975Sek110237 		    &hostid) == 0) {
11143975Sek110237 			char *hostname;
11153975Sek110237 			unsigned long myhostid = 0;
11163975Sek110237 
11173975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
11183975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
11193975Sek110237 
11203975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
11214178Slling 			if (hostid != 0 && myhostid != 0 &&
11224178Slling 			    (unsigned long)hostid != myhostid) {
11233975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
11243975Sek110237 				    "loaded as it was last accessed by "
11253975Sek110237 				    "another system (host: %s hostid: 0x%lx).  "
11263975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
11273975Sek110237 				    spa->spa_name, hostname,
11283975Sek110237 				    (unsigned long)hostid);
11293975Sek110237 				error = EBADF;
11303975Sek110237 				goto out;
11313975Sek110237 			}
11323975Sek110237 		}
11333975Sek110237 
1134789Sahrens 		spa_config_set(spa, newconfig);
1135789Sahrens 		spa_unload(spa);
1136789Sahrens 		spa_deactivate(spa);
1137789Sahrens 		spa_activate(spa);
1138789Sahrens 
11391544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
11401544Seschrock 	}
11411544Seschrock 
11421544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
11431544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
11441544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
11451544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11461544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11471544Seschrock 		error = EIO;
11481544Seschrock 		goto out;
1149789Sahrens 	}
1150789Sahrens 
11511544Seschrock 	/*
11522082Seschrock 	 * Load the bit that tells us to use the new accounting function
11532082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
11542082Seschrock 	 * be present.
11552082Seschrock 	 */
11562082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11572082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
11582082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
11592082Seschrock 	if (error != 0 && error != ENOENT) {
11602082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11612082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11622082Seschrock 		error = EIO;
11632082Seschrock 		goto out;
11642082Seschrock 	}
11652082Seschrock 
11662082Seschrock 	/*
11671544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
11681544Seschrock 	 * not be present.
11691544Seschrock 	 */
11701544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11711544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
11721544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
11731807Sbonwick 	if (error != 0 && error != ENOENT) {
11741544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11751544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11761544Seschrock 		error = EIO;
11771544Seschrock 		goto out;
11781544Seschrock 	}
11791544Seschrock 
11801544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11811544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
11821544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
11831544Seschrock 	if (error != 0 && error != ENOENT) {
11841544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11851544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11861544Seschrock 		error = EIO;
11871544Seschrock 		goto out;
11881544Seschrock 	}
1189789Sahrens 
1190789Sahrens 	/*
11912926Sek110237 	 * Load the history object.  If we have an older pool, this
11922926Sek110237 	 * will not be present.
11932926Sek110237 	 */
11942926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
11952926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
11962926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
11972926Sek110237 	if (error != 0 && error != ENOENT) {
11982926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11992926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
12002926Sek110237 		error = EIO;
12012926Sek110237 		goto out;
12022926Sek110237 	}
12032926Sek110237 
12042926Sek110237 	/*
12052082Seschrock 	 * Load any hot spares for this pool.
12062082Seschrock 	 */
12072082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12085450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
12092082Seschrock 	if (error != 0 && error != ENOENT) {
12102082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12112082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12122082Seschrock 		error = EIO;
12132082Seschrock 		goto out;
12142082Seschrock 	}
12152082Seschrock 	if (error == 0) {
12164577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
12175450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
12185450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
12192082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12202082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12212082Seschrock 			error = EIO;
12222082Seschrock 			goto out;
12232082Seschrock 		}
12242082Seschrock 
12252082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
12262082Seschrock 		spa_load_spares(spa);
12272082Seschrock 		spa_config_exit(spa, FTAG);
12282082Seschrock 	}
12292082Seschrock 
12305450Sbrendan 	/*
12315450Sbrendan 	 * Load any level 2 ARC devices for this pool.
12325450Sbrendan 	 */
12335450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12345450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
12355450Sbrendan 	    &spa->spa_l2cache.sav_object);
12365450Sbrendan 	if (error != 0 && error != ENOENT) {
12375450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12385450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
12395450Sbrendan 		error = EIO;
12405450Sbrendan 		goto out;
12415450Sbrendan 	}
12425450Sbrendan 	if (error == 0) {
12435450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
12445450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
12455450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
12465450Sbrendan 			vdev_set_state(rvd, B_TRUE,
12475450Sbrendan 			    VDEV_STATE_CANT_OPEN,
12485450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
12495450Sbrendan 			error = EIO;
12505450Sbrendan 			goto out;
12515450Sbrendan 		}
12525450Sbrendan 
12535450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
12545450Sbrendan 		spa_load_l2cache(spa);
12555450Sbrendan 		spa_config_exit(spa, FTAG);
12565450Sbrendan 	}
12575450Sbrendan 
12585094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
12594543Smarks 
12603912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12613912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
12623912Slling 
12633912Slling 	if (error && error != ENOENT) {
12643912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12653912Slling 		    VDEV_AUX_CORRUPT_DATA);
12663912Slling 		error = EIO;
12673912Slling 		goto out;
12683912Slling 	}
12693912Slling 
12703912Slling 	if (error == 0) {
12713912Slling 		(void) zap_lookup(spa->spa_meta_objset,
12723912Slling 		    spa->spa_pool_props_object,
12734451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
12743912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
12754451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
12764451Seschrock 		    spa->spa_pool_props_object,
12774451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
12784451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
12794543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
12804543Smarks 		    spa->spa_pool_props_object,
12814543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
12824543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
12835329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
12845329Sgw25295 		    spa->spa_pool_props_object,
12855329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
12865329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
12873912Slling 	}
12883912Slling 
12892082Seschrock 	/*
12904451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
12914451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
12924451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
12934451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
12944451Seschrock 	 * over.
12954451Seschrock 	 */
12965756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
12974451Seschrock 		spa_check_removed(spa->spa_root_vdev);
12984451Seschrock 
12994451Seschrock 	/*
13001986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1301789Sahrens 	 */
13021986Seschrock 	vdev_load(rvd);
1303789Sahrens 
1304789Sahrens 	/*
1305789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1306789Sahrens 	 */
13071544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1308789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
13091544Seschrock 	spa_config_exit(spa, FTAG);
1310789Sahrens 
1311789Sahrens 	/*
1312789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1313789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1314789Sahrens 	 */
13151544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13161544Seschrock 		error = ENXIO;
13171544Seschrock 		goto out;
13181544Seschrock 	}
1319789Sahrens 
13201544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13211635Sbonwick 		dmu_tx_t *tx;
13221635Sbonwick 		int need_update = B_FALSE;
13231585Sbonwick 		int c;
13241601Sbonwick 
13251635Sbonwick 		/*
13261635Sbonwick 		 * Claim log blocks that haven't been committed yet.
13271635Sbonwick 		 * This must all happen in a single txg.
13281635Sbonwick 		 */
13291601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1330789Sahrens 		    spa_first_txg(spa));
13312417Sahrens 		(void) dmu_objset_find(spa->spa_name,
13322417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1333789Sahrens 		dmu_tx_commit(tx);
1334789Sahrens 
1335789Sahrens 		spa->spa_sync_on = B_TRUE;
1336789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1337789Sahrens 
1338789Sahrens 		/*
1339789Sahrens 		 * Wait for all claims to sync.
1340789Sahrens 		 */
1341789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
13421585Sbonwick 
13431585Sbonwick 		/*
13441635Sbonwick 		 * If the config cache is stale, or we have uninitialized
13451635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
13461585Sbonwick 		 */
13471635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
13481635Sbonwick 		    state == SPA_LOAD_IMPORT)
13491635Sbonwick 			need_update = B_TRUE;
13501635Sbonwick 
13511635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
13521635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
13531635Sbonwick 				need_update = B_TRUE;
13541585Sbonwick 
13551585Sbonwick 		/*
13561635Sbonwick 		 * Update the config cache asychronously in case we're the
13571635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
13581585Sbonwick 		 */
13591635Sbonwick 		if (need_update)
13601635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1361789Sahrens 	}
1362789Sahrens 
13631544Seschrock 	error = 0;
13641544Seschrock out:
13652082Seschrock 	if (error && error != EBADF)
13661544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
13671544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
13681544Seschrock 	spa->spa_ena = 0;
13691544Seschrock 
13701544Seschrock 	return (error);
1371789Sahrens }
1372789Sahrens 
1373789Sahrens /*
1374789Sahrens  * Pool Open/Import
1375789Sahrens  *
1376789Sahrens  * The import case is identical to an open except that the configuration is sent
1377789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1378789Sahrens  * case of an open, the pool configuration will exist in the
13794451Seschrock  * POOL_STATE_UNINITIALIZED state.
1380789Sahrens  *
1381789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1382789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1383789Sahrens  * ambiguous state.
1384789Sahrens  */
1385789Sahrens static int
1386789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1387789Sahrens {
1388789Sahrens 	spa_t *spa;
1389789Sahrens 	int error;
1390789Sahrens 	int loaded = B_FALSE;
1391789Sahrens 	int locked = B_FALSE;
1392789Sahrens 
1393789Sahrens 	*spapp = NULL;
1394789Sahrens 
1395789Sahrens 	/*
1396789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1397789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1398789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1399789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1400789Sahrens 	 */
1401789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1402789Sahrens 		mutex_enter(&spa_namespace_lock);
1403789Sahrens 		locked = B_TRUE;
1404789Sahrens 	}
1405789Sahrens 
1406789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1407789Sahrens 		if (locked)
1408789Sahrens 			mutex_exit(&spa_namespace_lock);
1409789Sahrens 		return (ENOENT);
1410789Sahrens 	}
1411789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1412789Sahrens 
1413789Sahrens 		spa_activate(spa);
1414789Sahrens 
14151635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1416789Sahrens 
1417789Sahrens 		if (error == EBADF) {
1418789Sahrens 			/*
14191986Seschrock 			 * If vdev_validate() returns failure (indicated by
14201986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
14211986Seschrock 			 * that the pool has been exported or destroyed.  If
14221986Seschrock 			 * this is the case, the config cache is out of sync and
14231986Seschrock 			 * we should remove the pool from the namespace.
1424789Sahrens 			 */
1425789Sahrens 			spa_unload(spa);
1426789Sahrens 			spa_deactivate(spa);
14276643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1428789Sahrens 			spa_remove(spa);
1429789Sahrens 			if (locked)
1430789Sahrens 				mutex_exit(&spa_namespace_lock);
1431789Sahrens 			return (ENOENT);
14321544Seschrock 		}
14331544Seschrock 
14341544Seschrock 		if (error) {
1435789Sahrens 			/*
1436789Sahrens 			 * We can't open the pool, but we still have useful
1437789Sahrens 			 * information: the state of each vdev after the
1438789Sahrens 			 * attempted vdev_open().  Return this to the user.
1439789Sahrens 			 */
14401635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
14411635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1442789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1443789Sahrens 				    B_TRUE);
14441635Sbonwick 				spa_config_exit(spa, FTAG);
14451635Sbonwick 			}
1446789Sahrens 			spa_unload(spa);
1447789Sahrens 			spa_deactivate(spa);
14481544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1449789Sahrens 			if (locked)
1450789Sahrens 				mutex_exit(&spa_namespace_lock);
1451789Sahrens 			*spapp = NULL;
1452789Sahrens 			return (error);
14531544Seschrock 		} else {
14541544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1455789Sahrens 		}
1456789Sahrens 
1457789Sahrens 		loaded = B_TRUE;
1458789Sahrens 	}
1459789Sahrens 
1460789Sahrens 	spa_open_ref(spa, tag);
14614451Seschrock 
14624451Seschrock 	/*
14634451Seschrock 	 * If we just loaded the pool, resilver anything that's out of date.
14644451Seschrock 	 */
14654451Seschrock 	if (loaded && (spa_mode & FWRITE))
14664451Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
14674451Seschrock 
1468789Sahrens 	if (locked)
1469789Sahrens 		mutex_exit(&spa_namespace_lock);
1470789Sahrens 
1471789Sahrens 	*spapp = spa;
1472789Sahrens 
1473789Sahrens 	if (config != NULL) {
14741544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1475789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
14761544Seschrock 		spa_config_exit(spa, FTAG);
1477789Sahrens 	}
1478789Sahrens 
1479789Sahrens 	return (0);
1480789Sahrens }
1481789Sahrens 
1482789Sahrens int
1483789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1484789Sahrens {
1485789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1486789Sahrens }
1487789Sahrens 
14881544Seschrock /*
14891544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
14901544Seschrock  * preventing it from being exported or destroyed.
14911544Seschrock  */
14921544Seschrock spa_t *
14931544Seschrock spa_inject_addref(char *name)
14941544Seschrock {
14951544Seschrock 	spa_t *spa;
14961544Seschrock 
14971544Seschrock 	mutex_enter(&spa_namespace_lock);
14981544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
14991544Seschrock 		mutex_exit(&spa_namespace_lock);
15001544Seschrock 		return (NULL);
15011544Seschrock 	}
15021544Seschrock 	spa->spa_inject_ref++;
15031544Seschrock 	mutex_exit(&spa_namespace_lock);
15041544Seschrock 
15051544Seschrock 	return (spa);
15061544Seschrock }
15071544Seschrock 
15081544Seschrock void
15091544Seschrock spa_inject_delref(spa_t *spa)
15101544Seschrock {
15111544Seschrock 	mutex_enter(&spa_namespace_lock);
15121544Seschrock 	spa->spa_inject_ref--;
15131544Seschrock 	mutex_exit(&spa_namespace_lock);
15141544Seschrock }
15151544Seschrock 
15165450Sbrendan /*
15175450Sbrendan  * Add spares device information to the nvlist.
15185450Sbrendan  */
15192082Seschrock static void
15202082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
15212082Seschrock {
15222082Seschrock 	nvlist_t **spares;
15232082Seschrock 	uint_t i, nspares;
15242082Seschrock 	nvlist_t *nvroot;
15252082Seschrock 	uint64_t guid;
15262082Seschrock 	vdev_stat_t *vs;
15272082Seschrock 	uint_t vsc;
15283377Seschrock 	uint64_t pool;
15292082Seschrock 
15305450Sbrendan 	if (spa->spa_spares.sav_count == 0)
15312082Seschrock 		return;
15322082Seschrock 
15332082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
15342082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15355450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
15362082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15372082Seschrock 	if (nspares != 0) {
15382082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
15392082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15402082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15412082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15422082Seschrock 
15432082Seschrock 		/*
15442082Seschrock 		 * Go through and find any spares which have since been
15452082Seschrock 		 * repurposed as an active spare.  If this is the case, update
15462082Seschrock 		 * their status appropriately.
15472082Seschrock 		 */
15482082Seschrock 		for (i = 0; i < nspares; i++) {
15492082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
15502082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15513377Seschrock 			if (spa_spare_exists(guid, &pool) && pool != 0ULL) {
15522082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
15532082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
15542082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
15552082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
15562082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
15572082Seschrock 			}
15582082Seschrock 		}
15592082Seschrock 	}
15602082Seschrock }
15612082Seschrock 
15625450Sbrendan /*
15635450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
15645450Sbrendan  */
15655450Sbrendan static void
15665450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
15675450Sbrendan {
15685450Sbrendan 	nvlist_t **l2cache;
15695450Sbrendan 	uint_t i, j, nl2cache;
15705450Sbrendan 	nvlist_t *nvroot;
15715450Sbrendan 	uint64_t guid;
15725450Sbrendan 	vdev_t *vd;
15735450Sbrendan 	vdev_stat_t *vs;
15745450Sbrendan 	uint_t vsc;
15755450Sbrendan 
15765450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
15775450Sbrendan 		return;
15785450Sbrendan 
15795450Sbrendan 	spa_config_enter(spa, RW_READER, FTAG);
15805450Sbrendan 
15815450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
15825450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15835450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
15845450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
15855450Sbrendan 	if (nl2cache != 0) {
15865450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
15875450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
15885450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15895450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
15905450Sbrendan 
15915450Sbrendan 		/*
15925450Sbrendan 		 * Update level 2 cache device stats.
15935450Sbrendan 		 */
15945450Sbrendan 
15955450Sbrendan 		for (i = 0; i < nl2cache; i++) {
15965450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
15975450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15985450Sbrendan 
15995450Sbrendan 			vd = NULL;
16005450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
16015450Sbrendan 				if (guid ==
16025450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
16035450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
16045450Sbrendan 					break;
16055450Sbrendan 				}
16065450Sbrendan 			}
16075450Sbrendan 			ASSERT(vd != NULL);
16085450Sbrendan 
16095450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
16105450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
16115450Sbrendan 			vdev_get_stats(vd, vs);
16125450Sbrendan 		}
16135450Sbrendan 	}
16145450Sbrendan 
16155450Sbrendan 	spa_config_exit(spa, FTAG);
16165450Sbrendan }
16175450Sbrendan 
1618789Sahrens int
16191544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1620789Sahrens {
1621789Sahrens 	int error;
1622789Sahrens 	spa_t *spa;
1623789Sahrens 
1624789Sahrens 	*config = NULL;
1625789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1626789Sahrens 
16272082Seschrock 	if (spa && *config != NULL) {
16281544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
16291544Seschrock 		    spa_get_errlog_size(spa)) == 0);
16301544Seschrock 
16312082Seschrock 		spa_add_spares(spa, *config);
16325450Sbrendan 		spa_add_l2cache(spa, *config);
16332082Seschrock 	}
16342082Seschrock 
16351544Seschrock 	/*
16361544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
16371544Seschrock 	 * and call spa_lookup() directly.
16381544Seschrock 	 */
16391544Seschrock 	if (altroot) {
16401544Seschrock 		if (spa == NULL) {
16411544Seschrock 			mutex_enter(&spa_namespace_lock);
16421544Seschrock 			spa = spa_lookup(name);
16431544Seschrock 			if (spa)
16441544Seschrock 				spa_altroot(spa, altroot, buflen);
16451544Seschrock 			else
16461544Seschrock 				altroot[0] = '\0';
16471544Seschrock 			spa = NULL;
16481544Seschrock 			mutex_exit(&spa_namespace_lock);
16491544Seschrock 		} else {
16501544Seschrock 			spa_altroot(spa, altroot, buflen);
16511544Seschrock 		}
16521544Seschrock 	}
16531544Seschrock 
1654789Sahrens 	if (spa != NULL)
1655789Sahrens 		spa_close(spa, FTAG);
1656789Sahrens 
1657789Sahrens 	return (error);
1658789Sahrens }
1659789Sahrens 
1660789Sahrens /*
16615450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
16625450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
16635450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
16645450Sbrendan  * specified, as long as they are well-formed.
16652082Seschrock  */
16662082Seschrock static int
16675450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
16685450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
16695450Sbrendan     vdev_labeltype_t label)
16702082Seschrock {
16715450Sbrendan 	nvlist_t **dev;
16725450Sbrendan 	uint_t i, ndev;
16732082Seschrock 	vdev_t *vd;
16742082Seschrock 	int error;
16752082Seschrock 
16762082Seschrock 	/*
16775450Sbrendan 	 * It's acceptable to have no devs specified.
16782082Seschrock 	 */
16795450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
16802082Seschrock 		return (0);
16812082Seschrock 
16825450Sbrendan 	if (ndev == 0)
16832082Seschrock 		return (EINVAL);
16842082Seschrock 
16852082Seschrock 	/*
16865450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
16875450Sbrendan 	 * device type.
16882082Seschrock 	 */
16895450Sbrendan 	if (spa_version(spa) < version)
16902082Seschrock 		return (ENOTSUP);
16912082Seschrock 
16923377Seschrock 	/*
16935450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
16943377Seschrock 	 * checking.
16953377Seschrock 	 */
16965450Sbrendan 	sav->sav_pending = dev;
16975450Sbrendan 	sav->sav_npending = ndev;
16985450Sbrendan 
16995450Sbrendan 	for (i = 0; i < ndev; i++) {
17005450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
17012082Seschrock 		    mode)) != 0)
17023377Seschrock 			goto out;
17032082Seschrock 
17042082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
17052082Seschrock 			vdev_free(vd);
17063377Seschrock 			error = EINVAL;
17073377Seschrock 			goto out;
17082082Seschrock 		}
17092082Seschrock 
17105450Sbrendan 		/*
17115450Sbrendan 		 * The L2ARC currently only supports disk devices.
17125450Sbrendan 		 */
17135450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
17145450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
17155450Sbrendan 			error = ENOTBLK;
17165450Sbrendan 			goto out;
17175450Sbrendan 		}
17185450Sbrendan 
17192082Seschrock 		vd->vdev_top = vd;
17203377Seschrock 
17213377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
17225450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
17235450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
17243377Seschrock 			    vd->vdev_guid) == 0);
17252082Seschrock 		}
17262082Seschrock 
17272082Seschrock 		vdev_free(vd);
17283377Seschrock 
17295450Sbrendan 		if (error &&
17305450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
17313377Seschrock 			goto out;
17323377Seschrock 		else
17333377Seschrock 			error = 0;
17342082Seschrock 	}
17352082Seschrock 
17363377Seschrock out:
17375450Sbrendan 	sav->sav_pending = NULL;
17385450Sbrendan 	sav->sav_npending = 0;
17393377Seschrock 	return (error);
17402082Seschrock }
17412082Seschrock 
17425450Sbrendan static int
17435450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
17445450Sbrendan {
17455450Sbrendan 	int error;
17465450Sbrendan 
17475450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17485450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
17495450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
17505450Sbrendan 		return (error);
17515450Sbrendan 	}
17525450Sbrendan 
17535450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17545450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
17555450Sbrendan 	    VDEV_LABEL_L2CACHE));
17565450Sbrendan }
17575450Sbrendan 
17585450Sbrendan static void
17595450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
17605450Sbrendan     const char *config)
17615450Sbrendan {
17625450Sbrendan 	int i;
17635450Sbrendan 
17645450Sbrendan 	if (sav->sav_config != NULL) {
17655450Sbrendan 		nvlist_t **olddevs;
17665450Sbrendan 		uint_t oldndevs;
17675450Sbrendan 		nvlist_t **newdevs;
17685450Sbrendan 
17695450Sbrendan 		/*
17705450Sbrendan 		 * Generate new dev list by concatentating with the
17715450Sbrendan 		 * current dev list.
17725450Sbrendan 		 */
17735450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
17745450Sbrendan 		    &olddevs, &oldndevs) == 0);
17755450Sbrendan 
17765450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
17775450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
17785450Sbrendan 		for (i = 0; i < oldndevs; i++)
17795450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
17805450Sbrendan 			    KM_SLEEP) == 0);
17815450Sbrendan 		for (i = 0; i < ndevs; i++)
17825450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
17835450Sbrendan 			    KM_SLEEP) == 0);
17845450Sbrendan 
17855450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
17865450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
17875450Sbrendan 
17885450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
17895450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
17905450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
17915450Sbrendan 			nvlist_free(newdevs[i]);
17925450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
17935450Sbrendan 	} else {
17945450Sbrendan 		/*
17955450Sbrendan 		 * Generate a new dev list.
17965450Sbrendan 		 */
17975450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
17985450Sbrendan 		    KM_SLEEP) == 0);
17995450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
18005450Sbrendan 		    devs, ndevs) == 0);
18015450Sbrendan 	}
18025450Sbrendan }
18035450Sbrendan 
18045450Sbrendan /*
18055450Sbrendan  * Stop and drop level 2 ARC devices
18065450Sbrendan  */
18075450Sbrendan void
18085450Sbrendan spa_l2cache_drop(spa_t *spa)
18095450Sbrendan {
18105450Sbrendan 	vdev_t *vd;
18115450Sbrendan 	int i;
18125450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
18135450Sbrendan 
18145450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
18155450Sbrendan 		uint64_t pool;
18165450Sbrendan 
18175450Sbrendan 		vd = sav->sav_vdevs[i];
18185450Sbrendan 		ASSERT(vd != NULL);
18195450Sbrendan 
18205450Sbrendan 		if (spa_mode & FWRITE &&
18216643Seschrock 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL &&
18226643Seschrock 		    l2arc_vdev_present(vd)) {
18235450Sbrendan 			l2arc_remove_vdev(vd);
18245450Sbrendan 		}
18255450Sbrendan 		if (vd->vdev_isl2cache)
18265450Sbrendan 			spa_l2cache_remove(vd);
18275450Sbrendan 		vdev_clear_stats(vd);
18285450Sbrendan 		(void) vdev_close(vd);
18295450Sbrendan 	}
18305450Sbrendan }
18315450Sbrendan 
18322082Seschrock /*
1833789Sahrens  * Pool Creation
1834789Sahrens  */
1835789Sahrens int
18365094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
18374715Sek110237     const char *history_str)
1838789Sahrens {
1839789Sahrens 	spa_t *spa;
18405094Slling 	char *altroot = NULL;
18411635Sbonwick 	vdev_t *rvd;
1842789Sahrens 	dsl_pool_t *dp;
1843789Sahrens 	dmu_tx_t *tx;
18442082Seschrock 	int c, error = 0;
1845789Sahrens 	uint64_t txg = TXG_INITIAL;
18465450Sbrendan 	nvlist_t **spares, **l2cache;
18475450Sbrendan 	uint_t nspares, nl2cache;
18485094Slling 	uint64_t version;
1849789Sahrens 
1850789Sahrens 	/*
1851789Sahrens 	 * If this pool already exists, return failure.
1852789Sahrens 	 */
1853789Sahrens 	mutex_enter(&spa_namespace_lock);
1854789Sahrens 	if (spa_lookup(pool) != NULL) {
1855789Sahrens 		mutex_exit(&spa_namespace_lock);
1856789Sahrens 		return (EEXIST);
1857789Sahrens 	}
1858789Sahrens 
1859789Sahrens 	/*
1860789Sahrens 	 * Allocate a new spa_t structure.
1861789Sahrens 	 */
18625094Slling 	(void) nvlist_lookup_string(props,
18635094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
18641635Sbonwick 	spa = spa_add(pool, altroot);
1865789Sahrens 	spa_activate(spa);
1866789Sahrens 
1867789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
18685094Slling 
18695094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
18705094Slling 		spa_unload(spa);
18715094Slling 		spa_deactivate(spa);
18725094Slling 		spa_remove(spa);
18736643Seschrock 		mutex_exit(&spa_namespace_lock);
18745094Slling 		return (error);
18755094Slling 	}
18765094Slling 
18775094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
18785094Slling 	    &version) != 0)
18795094Slling 		version = SPA_VERSION;
18805094Slling 	ASSERT(version <= SPA_VERSION);
18815094Slling 	spa->spa_uberblock.ub_version = version;
1882789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1883789Sahrens 
18841635Sbonwick 	/*
18851635Sbonwick 	 * Create the root vdev.
18861635Sbonwick 	 */
18871635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
18881635Sbonwick 
18892082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
18902082Seschrock 
18912082Seschrock 	ASSERT(error != 0 || rvd != NULL);
18922082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
18932082Seschrock 
18945913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
18951635Sbonwick 		error = EINVAL;
18962082Seschrock 
18972082Seschrock 	if (error == 0 &&
18982082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
18995450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
19002082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
19012082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
19022082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
19032082Seschrock 		vdev_config_dirty(rvd);
19041635Sbonwick 	}
19051635Sbonwick 
19061635Sbonwick 	spa_config_exit(spa, FTAG);
1907789Sahrens 
19082082Seschrock 	if (error != 0) {
1909789Sahrens 		spa_unload(spa);
1910789Sahrens 		spa_deactivate(spa);
1911789Sahrens 		spa_remove(spa);
1912789Sahrens 		mutex_exit(&spa_namespace_lock);
1913789Sahrens 		return (error);
1914789Sahrens 	}
1915789Sahrens 
19162082Seschrock 	/*
19172082Seschrock 	 * Get the list of spares, if specified.
19182082Seschrock 	 */
19192082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19202082Seschrock 	    &spares, &nspares) == 0) {
19215450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
19222082Seschrock 		    KM_SLEEP) == 0);
19235450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
19242082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
19252082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
19262082Seschrock 		spa_load_spares(spa);
19272082Seschrock 		spa_config_exit(spa, FTAG);
19285450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
19295450Sbrendan 	}
19305450Sbrendan 
19315450Sbrendan 	/*
19325450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
19335450Sbrendan 	 */
19345450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
19355450Sbrendan 	    &l2cache, &nl2cache) == 0) {
19365450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
19375450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
19385450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
19395450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
19405450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
19415450Sbrendan 		spa_load_l2cache(spa);
19425450Sbrendan 		spa_config_exit(spa, FTAG);
19435450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
19442082Seschrock 	}
19452082Seschrock 
1946789Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
1947789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1948789Sahrens 
1949789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1950789Sahrens 
1951789Sahrens 	/*
1952789Sahrens 	 * Create the pool config object.
1953789Sahrens 	 */
1954789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1955789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1956789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1957789Sahrens 
19581544Seschrock 	if (zap_add(spa->spa_meta_objset,
1959789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
19601544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
19611544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
19621544Seschrock 	}
1963789Sahrens 
19645094Slling 	/* Newly created pools with the right version are always deflated. */
19655094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
19665094Slling 		spa->spa_deflate = TRUE;
19675094Slling 		if (zap_add(spa->spa_meta_objset,
19685094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
19695094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
19705094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
19715094Slling 		}
19722082Seschrock 	}
19732082Seschrock 
1974789Sahrens 	/*
1975789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
1976789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
1977789Sahrens 	 * keeps changing.
1978789Sahrens 	 */
1979789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
1980789Sahrens 	    1 << 14, tx);
1981789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
1982789Sahrens 	    ZIO_COMPRESS_OFF, tx);
1983789Sahrens 
19841544Seschrock 	if (zap_add(spa->spa_meta_objset,
1985789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
19861544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
19871544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
19881544Seschrock 	}
1989789Sahrens 
19902926Sek110237 	/*
19912926Sek110237 	 * Create the pool's history object.
19922926Sek110237 	 */
19935094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
19945094Slling 		spa_history_create_obj(spa, tx);
19955094Slling 
19965094Slling 	/*
19975094Slling 	 * Set pool properties.
19985094Slling 	 */
19995094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
20005094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
20015329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
20025094Slling 	if (props)
20035094Slling 		spa_sync_props(spa, props, CRED(), tx);
20042926Sek110237 
2005789Sahrens 	dmu_tx_commit(tx);
2006789Sahrens 
2007789Sahrens 	spa->spa_sync_on = B_TRUE;
2008789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2009789Sahrens 
2010789Sahrens 	/*
2011789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2012789Sahrens 	 * bean counters are appropriately updated.
2013789Sahrens 	 */
2014789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2015789Sahrens 
20166643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2017789Sahrens 
20185094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
20194715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
20204715Sek110237 
2021789Sahrens 	mutex_exit(&spa_namespace_lock);
2022789Sahrens 
2023789Sahrens 	return (0);
2024789Sahrens }
2025789Sahrens 
2026789Sahrens /*
2027789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2028789Sahrens  * then call spa_load() to do the dirty work.
2029789Sahrens  */
20306423Sgw25295 static int
20316423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
20326643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2033789Sahrens {
2034789Sahrens 	spa_t *spa;
20355094Slling 	char *altroot = NULL;
20366643Seschrock 	int error, loaderr;
20372082Seschrock 	nvlist_t *nvroot;
20385450Sbrendan 	nvlist_t **spares, **l2cache;
20395450Sbrendan 	uint_t nspares, nl2cache;
20406423Sgw25295 	int mosconfig = isroot? B_FALSE : B_TRUE;
2041789Sahrens 
2042789Sahrens 	/*
2043789Sahrens 	 * If a pool with this name exists, return failure.
2044789Sahrens 	 */
2045789Sahrens 	mutex_enter(&spa_namespace_lock);
2046789Sahrens 	if (spa_lookup(pool) != NULL) {
2047789Sahrens 		mutex_exit(&spa_namespace_lock);
2048789Sahrens 		return (EEXIST);
2049789Sahrens 	}
2050789Sahrens 
2051789Sahrens 	/*
20521635Sbonwick 	 * Create and initialize the spa structure.
2053789Sahrens 	 */
20545094Slling 	(void) nvlist_lookup_string(props,
20555094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20561635Sbonwick 	spa = spa_add(pool, altroot);
2057789Sahrens 	spa_activate(spa);
2058789Sahrens 
20596643Seschrock 	if (allowfaulted)
20606643Seschrock 		spa->spa_import_faulted = B_TRUE;
20616673Seschrock 	spa->spa_is_root = isroot;
20626643Seschrock 
2063789Sahrens 	/*
20641635Sbonwick 	 * Pass off the heavy lifting to spa_load().
20651732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
20661732Sbonwick 	 * is actually the one to trust when doing an import.
20671601Sbonwick 	 */
20686643Seschrock 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, mosconfig);
2069789Sahrens 
20702082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
20712082Seschrock 	/*
20722082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
20732082Seschrock 	 * and conflicts with spa_has_spare().
20742082Seschrock 	 */
20756423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
20765450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
20775450Sbrendan 		spa->spa_spares.sav_config = NULL;
20782082Seschrock 		spa_load_spares(spa);
20792082Seschrock 	}
20806423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
20815450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
20825450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
20835450Sbrendan 		spa_load_l2cache(spa);
20845450Sbrendan 	}
20852082Seschrock 
20862082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
20872082Seschrock 	    &nvroot) == 0);
20885450Sbrendan 	if (error == 0)
20895450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
20905450Sbrendan 	if (error == 0)
20915450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
20925450Sbrendan 		    VDEV_ALLOC_L2CACHE);
20932082Seschrock 	spa_config_exit(spa, FTAG);
20942082Seschrock 
20955094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
20966643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
20976643Seschrock 			/*
20986643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
20996643Seschrock 			 * set, then manually set the config as if the config
21006643Seschrock 			 * passed in was specified in the cache file.
21016643Seschrock 			 */
21026643Seschrock 			error = 0;
21036643Seschrock 			spa->spa_import_faulted = B_FALSE;
21046643Seschrock 			if (spa->spa_config == NULL) {
21056643Seschrock 				spa_config_enter(spa, RW_READER, FTAG);
21066643Seschrock 				spa->spa_config = spa_config_generate(spa,
21076643Seschrock 				    NULL, -1ULL, B_TRUE);
21086643Seschrock 				spa_config_exit(spa, FTAG);
21096643Seschrock 			}
21106643Seschrock 			spa_unload(spa);
21116643Seschrock 			spa_deactivate(spa);
21126643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
21136643Seschrock 		} else {
21146643Seschrock 			spa_unload(spa);
21156643Seschrock 			spa_deactivate(spa);
21166643Seschrock 			spa_remove(spa);
21176643Seschrock 		}
2118789Sahrens 		mutex_exit(&spa_namespace_lock);
2119789Sahrens 		return (error);
2120789Sahrens 	}
2121789Sahrens 
21221635Sbonwick 	/*
21235450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
21245450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
21252082Seschrock 	 */
21262082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
21272082Seschrock 	    &spares, &nspares) == 0) {
21285450Sbrendan 		if (spa->spa_spares.sav_config)
21295450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
21302082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
21312082Seschrock 		else
21325450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
21332082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21345450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
21352082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
21362082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
21372082Seschrock 		spa_load_spares(spa);
21382082Seschrock 		spa_config_exit(spa, FTAG);
21395450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
21405450Sbrendan 	}
21415450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
21425450Sbrendan 	    &l2cache, &nl2cache) == 0) {
21435450Sbrendan 		if (spa->spa_l2cache.sav_config)
21445450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
21455450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
21465450Sbrendan 		else
21475450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
21485450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21495450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
21505450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
21515450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
21525450Sbrendan 		spa_load_l2cache(spa);
21535450Sbrendan 		spa_config_exit(spa, FTAG);
21545450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
21552082Seschrock 	}
21562082Seschrock 
21576643Seschrock 	if (spa_mode & FWRITE) {
21586643Seschrock 		/*
21596643Seschrock 		 * Update the config cache to include the newly-imported pool.
21606643Seschrock 		 */
21616423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
21621635Sbonwick 
21636643Seschrock 		/*
21646643Seschrock 		 * Resilver anything that's out of date.
21656643Seschrock 		 */
21666643Seschrock 		if (!isroot)
21676643Seschrock 			VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER,
21686643Seschrock 			    B_TRUE) == 0);
21696643Seschrock 	}
21706643Seschrock 
21716643Seschrock 	spa->spa_import_faulted = B_FALSE;
21724451Seschrock 	mutex_exit(&spa_namespace_lock);
21734451Seschrock 
2174789Sahrens 	return (0);
2175789Sahrens }
2176789Sahrens 
21776423Sgw25295 #ifdef _KERNEL
21786423Sgw25295 /*
21796423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
21806423Sgw25295  * device label.
21816423Sgw25295  */
21826423Sgw25295 static void
21836423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
21846423Sgw25295 {
21856423Sgw25295 	nvlist_t *nvtop, *nvroot;
21866423Sgw25295 	uint64_t pgid;
21876423Sgw25295 
21886423Sgw25295 	/*
21896423Sgw25295 	 * Add this top-level vdev to the child array.
21906423Sgw25295 	 */
21916423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
21926423Sgw25295 	    == 0);
21936423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
21946423Sgw25295 	    == 0);
21956423Sgw25295 
21966423Sgw25295 	/*
21976423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
21986423Sgw25295 	 */
21996423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
22006423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
22016423Sgw25295 	    == 0);
22026423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
22036423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
22046423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
22056423Sgw25295 	    &nvtop, 1) == 0);
22066423Sgw25295 
22076423Sgw25295 	/*
22086423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
22096423Sgw25295 	 * this pool's configuration (remove the old, add the new).
22106423Sgw25295 	 */
22116423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
22126423Sgw25295 	nvlist_free(nvroot);
22136423Sgw25295 }
22146423Sgw25295 
22156423Sgw25295 /*
22166423Sgw25295  * Get the root pool information from the root disk, then import the root pool
22176423Sgw25295  * during the system boot up time.
22186423Sgw25295  */
22196423Sgw25295 extern nvlist_t *vdev_disk_read_rootlabel(char *);
22206423Sgw25295 
22216423Sgw25295 void
22226423Sgw25295 spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf,
22236423Sgw25295     uint64_t *besttxg)
22246423Sgw25295 {
22256423Sgw25295 	nvlist_t *config;
22266423Sgw25295 	uint64_t txg;
22276423Sgw25295 
22286423Sgw25295 	if ((config = vdev_disk_read_rootlabel(devpath)) == NULL)
22296423Sgw25295 		return;
22306423Sgw25295 
22316423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
22326423Sgw25295 
22336423Sgw25295 	if (txg > *besttxg) {
22346423Sgw25295 		*besttxg = txg;
22356423Sgw25295 		if (*bestconf != NULL)
22366423Sgw25295 			nvlist_free(*bestconf);
22376423Sgw25295 		*bestconf = config;
22386423Sgw25295 		*bestdev = devpath;
22396423Sgw25295 	}
22406423Sgw25295 }
22416423Sgw25295 
22426423Sgw25295 boolean_t
22436423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
22446423Sgw25295 {
22456423Sgw25295 	uint64_t ival;
22466423Sgw25295 
22476423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
22486423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
22496423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 ||
22506423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
22516423Sgw25295 		return (B_FALSE);
22526423Sgw25295 
22536423Sgw25295 	return (B_TRUE);
22546423Sgw25295 }
22556423Sgw25295 
22566423Sgw25295 /*
22576423Sgw25295  * Import a root pool.
22586423Sgw25295  *
22596423Sgw25295  * For x86. devpath_list will consist the physpath name of the vdev in a single
22606423Sgw25295  * disk root pool or a list of physnames for the vdevs in a mirrored rootpool.
22616423Sgw25295  * e.g.
22626423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a"
22636423Sgw25295  *
22646423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
22656423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
22666423Sgw25295  * e.g.
22676423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
22686423Sgw25295  */
22696423Sgw25295 int
22706423Sgw25295 spa_import_rootpool(char *devpath_list)
22716423Sgw25295 {
22726423Sgw25295 	nvlist_t *conf = NULL;
22736423Sgw25295 	char *dev = NULL;
22746423Sgw25295 	char *pname;
22756423Sgw25295 	int error;
22766423Sgw25295 
22776423Sgw25295 	/*
22786423Sgw25295 	 * Get the vdev pathname and configuation from the most
22796423Sgw25295 	 * recently updated vdev (highest txg).
22806423Sgw25295 	 */
22816423Sgw25295 	if (error = spa_get_rootconf(devpath_list, &dev, &conf))
22826423Sgw25295 		goto msg_out;
22836423Sgw25295 
22846423Sgw25295 	/*
22856423Sgw25295 	 * Add type "root" vdev to the config.
22866423Sgw25295 	 */
22876423Sgw25295 	spa_build_rootpool_config(conf);
22886423Sgw25295 
22896423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
22906423Sgw25295 
22916673Seschrock 	/*
22926673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
22936673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
22946673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
22956673Seschrock 	 * pool open, not import.
22966673Seschrock 	 */
22976673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
22986423Sgw25295 	if (error == EEXIST)
22996423Sgw25295 		error = 0;
23006423Sgw25295 
23016423Sgw25295 	nvlist_free(conf);
23026423Sgw25295 	return (error);
23036423Sgw25295 
23046423Sgw25295 msg_out:
23056423Sgw25295 	cmn_err(CE_NOTE, "\n\n"
23066423Sgw25295 	    "  ***************************************************  \n"
23076423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
23086423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
23096423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
23106423Sgw25295 	    "  ***************************************************  \n\n");
23116423Sgw25295 
23126423Sgw25295 	return (error);
23136423Sgw25295 }
23146423Sgw25295 #endif
23156423Sgw25295 
23166423Sgw25295 /*
23176423Sgw25295  * Import a non-root pool into the system.
23186423Sgw25295  */
23196423Sgw25295 int
23206423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
23216423Sgw25295 {
23226643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
23236423Sgw25295 }
23246423Sgw25295 
23256643Seschrock int
23266643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
23276643Seschrock {
23286643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
23296643Seschrock }
23306643Seschrock 
23316643Seschrock 
2332789Sahrens /*
2333789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2334789Sahrens  * to get the vdev stats associated with the imported devices.
2335789Sahrens  */
2336789Sahrens #define	TRYIMPORT_NAME	"$import"
2337789Sahrens 
2338789Sahrens nvlist_t *
2339789Sahrens spa_tryimport(nvlist_t *tryconfig)
2340789Sahrens {
2341789Sahrens 	nvlist_t *config = NULL;
2342789Sahrens 	char *poolname;
2343789Sahrens 	spa_t *spa;
2344789Sahrens 	uint64_t state;
2345789Sahrens 
2346789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2347789Sahrens 		return (NULL);
2348789Sahrens 
2349789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2350789Sahrens 		return (NULL);
2351789Sahrens 
23521635Sbonwick 	/*
23531635Sbonwick 	 * Create and initialize the spa structure.
23541635Sbonwick 	 */
2355789Sahrens 	mutex_enter(&spa_namespace_lock);
23561635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2357789Sahrens 	spa_activate(spa);
2358789Sahrens 
2359789Sahrens 	/*
23601635Sbonwick 	 * Pass off the heavy lifting to spa_load().
23611732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
23621732Sbonwick 	 * is actually the one to trust when doing an import.
2363789Sahrens 	 */
23641732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2365789Sahrens 
2366789Sahrens 	/*
2367789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2368789Sahrens 	 */
2369789Sahrens 	if (spa->spa_root_vdev != NULL) {
23701635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
2371789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
23721635Sbonwick 		spa_config_exit(spa, FTAG);
2373789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2374789Sahrens 		    poolname) == 0);
2375789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2376789Sahrens 		    state) == 0);
23773975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
23783975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
23792082Seschrock 
23802082Seschrock 		/*
23816423Sgw25295 		 * If the bootfs property exists on this pool then we
23826423Sgw25295 		 * copy it out so that external consumers can tell which
23836423Sgw25295 		 * pools are bootable.
23846423Sgw25295 		 */
23856423Sgw25295 		if (spa->spa_bootfs) {
23866423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
23876423Sgw25295 
23886423Sgw25295 			/*
23896423Sgw25295 			 * We have to play games with the name since the
23906423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
23916423Sgw25295 			 */
23926423Sgw25295 			if (dsl_dsobj_to_dsname(spa->spa_name,
23936423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
23946423Sgw25295 				char *cp;
23956423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
23966423Sgw25295 
23976423Sgw25295 				cp = strchr(tmpname, '/');
23986423Sgw25295 				if (cp == NULL) {
23996423Sgw25295 					(void) strlcpy(dsname, tmpname,
24006423Sgw25295 					    MAXPATHLEN);
24016423Sgw25295 				} else {
24026423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
24036423Sgw25295 					    "%s/%s", poolname, ++cp);
24046423Sgw25295 				}
24056423Sgw25295 				VERIFY(nvlist_add_string(config,
24066423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
24076423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
24086423Sgw25295 			}
24096423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
24106423Sgw25295 		}
24116423Sgw25295 
24126423Sgw25295 		/*
24135450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
24142082Seschrock 		 */
24152082Seschrock 		spa_add_spares(spa, config);
24165450Sbrendan 		spa_add_l2cache(spa, config);
2417789Sahrens 	}
2418789Sahrens 
2419789Sahrens 	spa_unload(spa);
2420789Sahrens 	spa_deactivate(spa);
2421789Sahrens 	spa_remove(spa);
2422789Sahrens 	mutex_exit(&spa_namespace_lock);
2423789Sahrens 
2424789Sahrens 	return (config);
2425789Sahrens }
2426789Sahrens 
2427789Sahrens /*
2428789Sahrens  * Pool export/destroy
2429789Sahrens  *
2430789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2431789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2432789Sahrens  * update the pool state and sync all the labels to disk, removing the
2433789Sahrens  * configuration from the cache afterwards.
2434789Sahrens  */
2435789Sahrens static int
24361775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig)
2437789Sahrens {
2438789Sahrens 	spa_t *spa;
2439789Sahrens 
24401775Sbillm 	if (oldconfig)
24411775Sbillm 		*oldconfig = NULL;
24421775Sbillm 
2443789Sahrens 	if (!(spa_mode & FWRITE))
2444789Sahrens 		return (EROFS);
2445789Sahrens 
2446789Sahrens 	mutex_enter(&spa_namespace_lock);
2447789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2448789Sahrens 		mutex_exit(&spa_namespace_lock);
2449789Sahrens 		return (ENOENT);
2450789Sahrens 	}
2451789Sahrens 
2452789Sahrens 	/*
24531544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
24541544Seschrock 	 * reacquire the namespace lock, and see if we can export.
24551544Seschrock 	 */
24561544Seschrock 	spa_open_ref(spa, FTAG);
24571544Seschrock 	mutex_exit(&spa_namespace_lock);
24581544Seschrock 	spa_async_suspend(spa);
24591544Seschrock 	mutex_enter(&spa_namespace_lock);
24601544Seschrock 	spa_close(spa, FTAG);
24611544Seschrock 
24621544Seschrock 	/*
2463789Sahrens 	 * The pool will be in core if it's openable,
2464789Sahrens 	 * in which case we can modify its state.
2465789Sahrens 	 */
2466789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2467789Sahrens 		/*
2468789Sahrens 		 * Objsets may be open only because they're dirty, so we
2469789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2470789Sahrens 		 */
2471789Sahrens 		spa_scrub_suspend(spa);
2472789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2473789Sahrens 
24741544Seschrock 		/*
24751544Seschrock 		 * A pool cannot be exported or destroyed if there are active
24761544Seschrock 		 * references.  If we are resetting a pool, allow references by
24771544Seschrock 		 * fault injection handlers.
24781544Seschrock 		 */
24791544Seschrock 		if (!spa_refcount_zero(spa) ||
24801544Seschrock 		    (spa->spa_inject_ref != 0 &&
24811544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
2482789Sahrens 			spa_scrub_resume(spa);
24831544Seschrock 			spa_async_resume(spa);
2484789Sahrens 			mutex_exit(&spa_namespace_lock);
2485789Sahrens 			return (EBUSY);
2486789Sahrens 		}
2487789Sahrens 
2488789Sahrens 		spa_scrub_resume(spa);
2489789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
2490789Sahrens 
2491789Sahrens 		/*
2492789Sahrens 		 * We want this to be reflected on every label,
2493789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2494789Sahrens 		 * final sync that pushes these changes out.
2495789Sahrens 		 */
24961544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
24971601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
24981544Seschrock 			spa->spa_state = new_state;
24991635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
25001544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
25011601Sbonwick 			spa_config_exit(spa, FTAG);
25021544Seschrock 		}
2503789Sahrens 	}
2504789Sahrens 
25054451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
25064451Seschrock 
2507789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2508789Sahrens 		spa_unload(spa);
2509789Sahrens 		spa_deactivate(spa);
2510789Sahrens 	}
2511789Sahrens 
25121775Sbillm 	if (oldconfig && spa->spa_config)
25131775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
25141775Sbillm 
25151544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
25166643Seschrock 		spa_config_sync(spa, B_TRUE, B_TRUE);
25171544Seschrock 		spa_remove(spa);
25181544Seschrock 	}
2519789Sahrens 	mutex_exit(&spa_namespace_lock);
2520789Sahrens 
2521789Sahrens 	return (0);
2522789Sahrens }
2523789Sahrens 
2524789Sahrens /*
2525789Sahrens  * Destroy a storage pool.
2526789Sahrens  */
2527789Sahrens int
2528789Sahrens spa_destroy(char *pool)
2529789Sahrens {
25301775Sbillm 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL));
2531789Sahrens }
2532789Sahrens 
2533789Sahrens /*
2534789Sahrens  * Export a storage pool.
2535789Sahrens  */
2536789Sahrens int
25371775Sbillm spa_export(char *pool, nvlist_t **oldconfig)
2538789Sahrens {
25391775Sbillm 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig));
2540789Sahrens }
2541789Sahrens 
2542789Sahrens /*
25431544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
25441544Seschrock  * from the namespace in any way.
25451544Seschrock  */
25461544Seschrock int
25471544Seschrock spa_reset(char *pool)
25481544Seschrock {
25491775Sbillm 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL));
25501544Seschrock }
25511544Seschrock 
25521544Seschrock 
25531544Seschrock /*
2554789Sahrens  * ==========================================================================
2555789Sahrens  * Device manipulation
2556789Sahrens  * ==========================================================================
2557789Sahrens  */
2558789Sahrens 
2559789Sahrens /*
25604527Sperrin  * Add a device to a storage pool.
2561789Sahrens  */
2562789Sahrens int
2563789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2564789Sahrens {
2565789Sahrens 	uint64_t txg;
25661635Sbonwick 	int c, error;
2567789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
25681585Sbonwick 	vdev_t *vd, *tvd;
25695450Sbrendan 	nvlist_t **spares, **l2cache;
25705450Sbrendan 	uint_t nspares, nl2cache;
2571789Sahrens 
2572789Sahrens 	txg = spa_vdev_enter(spa);
2573789Sahrens 
25742082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
25752082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
25762082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
25772082Seschrock 
25783377Seschrock 	spa->spa_pending_vdev = vd;
2579789Sahrens 
25805450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
25815450Sbrendan 	    &nspares) != 0)
25822082Seschrock 		nspares = 0;
25832082Seschrock 
25845450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
25855450Sbrendan 	    &nl2cache) != 0)
25865450Sbrendan 		nl2cache = 0;
25875450Sbrendan 
25885450Sbrendan 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) {
25893377Seschrock 		spa->spa_pending_vdev = NULL;
25902082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
25913377Seschrock 	}
25922082Seschrock 
25932082Seschrock 	if (vd->vdev_children != 0) {
25943377Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
25953377Seschrock 			spa->spa_pending_vdev = NULL;
25962082Seschrock 			return (spa_vdev_exit(spa, vd, txg, error));
25972082Seschrock 		}
25982082Seschrock 	}
25992082Seschrock 
26003377Seschrock 	/*
26015450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
26025450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
26033377Seschrock 	 */
26045450Sbrendan 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) {
26053377Seschrock 		spa->spa_pending_vdev = NULL;
26063377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
26073377Seschrock 	}
26083377Seschrock 
26093377Seschrock 	spa->spa_pending_vdev = NULL;
26103377Seschrock 
26113377Seschrock 	/*
26123377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
26133377Seschrock 	 */
26143377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
26153377Seschrock 		tvd = vd->vdev_child[c];
26163377Seschrock 		vdev_remove_child(vd, tvd);
26173377Seschrock 		tvd->vdev_id = rvd->vdev_children;
26183377Seschrock 		vdev_add_child(rvd, tvd);
26193377Seschrock 		vdev_config_dirty(tvd);
26203377Seschrock 	}
26213377Seschrock 
26222082Seschrock 	if (nspares != 0) {
26235450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
26245450Sbrendan 		    ZPOOL_CONFIG_SPARES);
26252082Seschrock 		spa_load_spares(spa);
26265450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
26275450Sbrendan 	}
26285450Sbrendan 
26295450Sbrendan 	if (nl2cache != 0) {
26305450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
26315450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
26325450Sbrendan 		spa_load_l2cache(spa);
26335450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2634789Sahrens 	}
2635789Sahrens 
2636789Sahrens 	/*
26371585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
26381585Sbonwick 	 * If other threads start allocating from these vdevs before we
26391585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
26401585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
26411585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
26421585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
26431635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
26441585Sbonwick 	 *
26451585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
26461585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
26471585Sbonwick 	 * steps will be completed the next time we load the pool.
2648789Sahrens 	 */
26491635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
26501585Sbonwick 
26511635Sbonwick 	mutex_enter(&spa_namespace_lock);
26521635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
26531635Sbonwick 	mutex_exit(&spa_namespace_lock);
2654789Sahrens 
26551635Sbonwick 	return (0);
2656789Sahrens }
2657789Sahrens 
2658789Sahrens /*
2659789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2660789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2661789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2662789Sahrens  *
2663789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2664789Sahrens  * existing device; in this case the two devices are made into their own
26654451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2666789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2667789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2668789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2669789Sahrens  * is automatically detached.
2670789Sahrens  */
2671789Sahrens int
26721544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2673789Sahrens {
2674789Sahrens 	uint64_t txg, open_txg;
2675789Sahrens 	int error;
2676789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2677789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
26782082Seschrock 	vdev_ops_t *pvops;
26794527Sperrin 	int is_log;
2680789Sahrens 
2681789Sahrens 	txg = spa_vdev_enter(spa);
2682789Sahrens 
26836643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2684789Sahrens 
2685789Sahrens 	if (oldvd == NULL)
2686789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2687789Sahrens 
26881585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
26891585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
26901585Sbonwick 
2691789Sahrens 	pvd = oldvd->vdev_parent;
2692789Sahrens 
26932082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
26944451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
26954451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
26964451Seschrock 
26974451Seschrock 	if (newrootvd->vdev_children != 1)
2698789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2699789Sahrens 
2700789Sahrens 	newvd = newrootvd->vdev_child[0];
2701789Sahrens 
2702789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2703789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2704789Sahrens 
27052082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2706789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2707789Sahrens 
27084527Sperrin 	/*
27094527Sperrin 	 * Spares can't replace logs
27104527Sperrin 	 */
27114527Sperrin 	is_log = oldvd->vdev_islog;
27124527Sperrin 	if (is_log && newvd->vdev_isspare)
27134527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27144527Sperrin 
27152082Seschrock 	if (!replacing) {
27162082Seschrock 		/*
27172082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
27182082Seschrock 		 * vdev.
27192082Seschrock 		 */
27202082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
27212082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
27222082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27232082Seschrock 
27242082Seschrock 		pvops = &vdev_mirror_ops;
27252082Seschrock 	} else {
27262082Seschrock 		/*
27272082Seschrock 		 * Active hot spares can only be replaced by inactive hot
27282082Seschrock 		 * spares.
27292082Seschrock 		 */
27302082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
27312082Seschrock 		    pvd->vdev_child[1] == oldvd &&
27322082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
27332082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27342082Seschrock 
27352082Seschrock 		/*
27362082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
27372082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
27383377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
27393377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
27403377Seschrock 		 * the same (spare replaces spare, non-spare replaces
27413377Seschrock 		 * non-spare).
27422082Seschrock 		 */
27432082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
27442082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27453377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
27463377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
27473377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27482082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
27492082Seschrock 		    newvd->vdev_isspare)
27502082Seschrock 			pvops = &vdev_spare_ops;
27512082Seschrock 		else
27522082Seschrock 			pvops = &vdev_replacing_ops;
27532082Seschrock 	}
27542082Seschrock 
27551175Slling 	/*
27561175Slling 	 * Compare the new device size with the replaceable/attachable
27571175Slling 	 * device size.
27581175Slling 	 */
27591175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2760789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2761789Sahrens 
27621732Sbonwick 	/*
27631732Sbonwick 	 * The new device cannot have a higher alignment requirement
27641732Sbonwick 	 * than the top-level vdev.
27651732Sbonwick 	 */
27661732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2767789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2768789Sahrens 
2769789Sahrens 	/*
2770789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2771789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2772789Sahrens 	 */
2773789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2774789Sahrens 		spa_strfree(oldvd->vdev_path);
2775789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2776789Sahrens 		    KM_SLEEP);
2777789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2778789Sahrens 		    newvd->vdev_path, "old");
2779789Sahrens 		if (oldvd->vdev_devid != NULL) {
2780789Sahrens 			spa_strfree(oldvd->vdev_devid);
2781789Sahrens 			oldvd->vdev_devid = NULL;
2782789Sahrens 		}
2783789Sahrens 	}
2784789Sahrens 
2785789Sahrens 	/*
27862082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
27872082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2788789Sahrens 	 */
2789789Sahrens 	if (pvd->vdev_ops != pvops)
2790789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2791789Sahrens 
2792789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2793789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2794789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2795789Sahrens 
2796789Sahrens 	/*
2797789Sahrens 	 * Extract the new device from its root and add it to pvd.
2798789Sahrens 	 */
2799789Sahrens 	vdev_remove_child(newrootvd, newvd);
2800789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2801789Sahrens 	vdev_add_child(pvd, newvd);
2802789Sahrens 
28031544Seschrock 	/*
28041544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
28051544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
28061544Seschrock 	 */
28071544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
28081544Seschrock 
2809789Sahrens 	tvd = newvd->vdev_top;
2810789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2811789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2812789Sahrens 
2813789Sahrens 	vdev_config_dirty(tvd);
2814789Sahrens 
2815789Sahrens 	/*
2816789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2817789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2818789Sahrens 	 */
2819789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2820789Sahrens 
2821789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2822789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2823789Sahrens 	    open_txg - TXG_INITIAL + 1);
2824789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2825789Sahrens 
28263377Seschrock 	if (newvd->vdev_isspare)
28273377Seschrock 		spa_spare_activate(newvd);
28281544Seschrock 
2829789Sahrens 	/*
2830789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2831789Sahrens 	 */
28321732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2833789Sahrens 
2834789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2835789Sahrens 
2836789Sahrens 	/*
28374451Seschrock 	 * Kick off a resilver to update newvd.  We need to grab the namespace
28384451Seschrock 	 * lock because spa_scrub() needs to post a sysevent with the pool name.
2839789Sahrens 	 */
28404451Seschrock 	mutex_enter(&spa_namespace_lock);
2841789Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
28424451Seschrock 	mutex_exit(&spa_namespace_lock);
2843789Sahrens 
2844789Sahrens 	return (0);
2845789Sahrens }
2846789Sahrens 
2847789Sahrens /*
2848789Sahrens  * Detach a device from a mirror or replacing vdev.
2849789Sahrens  * If 'replace_done' is specified, only detach if the parent
2850789Sahrens  * is a replacing vdev.
2851789Sahrens  */
2852789Sahrens int
28531544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2854789Sahrens {
2855789Sahrens 	uint64_t txg;
2856789Sahrens 	int c, t, error;
2857789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2858789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
28592082Seschrock 	boolean_t unspare = B_FALSE;
28602082Seschrock 	uint64_t unspare_guid;
28616673Seschrock 	size_t len;
2862789Sahrens 
2863789Sahrens 	txg = spa_vdev_enter(spa);
2864789Sahrens 
28656643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
2866789Sahrens 
2867789Sahrens 	if (vd == NULL)
2868789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2869789Sahrens 
28701585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
28711585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28721585Sbonwick 
2873789Sahrens 	pvd = vd->vdev_parent;
2874789Sahrens 
2875789Sahrens 	/*
2876789Sahrens 	 * If replace_done is specified, only remove this device if it's
28772082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
28782082Seschrock 	 * disk can be removed.
2879789Sahrens 	 */
28802082Seschrock 	if (replace_done) {
28812082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
28822082Seschrock 			if (vd->vdev_id != 0)
28832082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28842082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
28852082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28862082Seschrock 		}
28872082Seschrock 	}
28882082Seschrock 
28892082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
28904577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
2891789Sahrens 
2892789Sahrens 	/*
28932082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
2894789Sahrens 	 */
2895789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
28962082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
28972082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
2898789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2899789Sahrens 
2900789Sahrens 	/*
2901789Sahrens 	 * If there's only one replica, you can't detach it.
2902789Sahrens 	 */
2903789Sahrens 	if (pvd->vdev_children <= 1)
2904789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2905789Sahrens 
2906789Sahrens 	/*
2907789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
2908789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
2909789Sahrens 	 *
2910789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
2911789Sahrens 	 * precise DTL check.
2912789Sahrens 	 */
2913789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
2914789Sahrens 		uint64_t dirty;
2915789Sahrens 
2916789Sahrens 		cvd = pvd->vdev_child[c];
2917789Sahrens 		if (cvd == vd)
2918789Sahrens 			continue;
2919789Sahrens 		if (vdev_is_dead(cvd))
2920789Sahrens 			continue;
2921789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
2922789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
2923789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
2924789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
2925789Sahrens 		if (!dirty)
2926789Sahrens 			break;
2927789Sahrens 	}
29282082Seschrock 
29292082Seschrock 	/*
29302082Seschrock 	 * If we are a replacing or spare vdev, then we can always detach the
29312082Seschrock 	 * latter child, as that is how one cancels the operation.
29322082Seschrock 	 */
29332082Seschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
29342082Seschrock 	    c == pvd->vdev_children)
2935789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2936789Sahrens 
2937789Sahrens 	/*
29386673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
29396673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
29406673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
29416673Seschrock 	 */
29426673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
29436673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
29446673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
29456673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
29466673Seschrock 		cvd = pvd->vdev_child[0];
29476673Seschrock 		len = strlen(vd->vdev_path);
29486673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
29496673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
29506673Seschrock 			spa_strfree(cvd->vdev_path);
29516673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
29526673Seschrock 		}
29536673Seschrock 	}
29546673Seschrock 
29556673Seschrock 	/*
29562082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
29572082Seschrock 	 * that the spare should become a real disk, and be removed from the
29582082Seschrock 	 * active spare list for the pool.
29592082Seschrock 	 */
29602082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
29612082Seschrock 	    vd->vdev_id == 0)
29622082Seschrock 		unspare = B_TRUE;
29632082Seschrock 
29642082Seschrock 	/*
2965789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
2966789Sahrens 	 * This must be done after all other error cases are handled,
2967789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
2968789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
2969789Sahrens 	 * it may be that the unwritability of the disk is the reason
2970789Sahrens 	 * it's being detached!
2971789Sahrens 	 */
29723377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
2973789Sahrens 
2974789Sahrens 	/*
2975789Sahrens 	 * Remove vd from its parent and compact the parent's children.
2976789Sahrens 	 */
2977789Sahrens 	vdev_remove_child(pvd, vd);
2978789Sahrens 	vdev_compact_children(pvd);
2979789Sahrens 
2980789Sahrens 	/*
2981789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
2982789Sahrens 	 */
2983789Sahrens 	cvd = pvd->vdev_child[0];
2984789Sahrens 
2985789Sahrens 	/*
29862082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
29872082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
29882082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
29892082Seschrock 	 * GUID if it creates a new toplevel GUID.
29902082Seschrock 	 */
29912082Seschrock 	if (unspare) {
29922082Seschrock 		ASSERT(cvd->vdev_isspare);
29933377Seschrock 		spa_spare_remove(cvd);
29942082Seschrock 		unspare_guid = cvd->vdev_guid;
29952082Seschrock 	}
29962082Seschrock 
29972082Seschrock 	/*
2998789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
2999789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3000789Sahrens 	 */
3001789Sahrens 	if (pvd->vdev_children == 1)
3002789Sahrens 		vdev_remove_parent(cvd);
3003789Sahrens 
3004789Sahrens 	/*
3005789Sahrens 	 * We don't set tvd until now because the parent we just removed
3006789Sahrens 	 * may have been the previous top-level vdev.
3007789Sahrens 	 */
3008789Sahrens 	tvd = cvd->vdev_top;
3009789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3010789Sahrens 
3011789Sahrens 	/*
30123377Seschrock 	 * Reevaluate the parent vdev state.
3013789Sahrens 	 */
30144451Seschrock 	vdev_propagate_state(cvd);
3015789Sahrens 
3016789Sahrens 	/*
30173377Seschrock 	 * If the device we just detached was smaller than the others, it may be
30183377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
30193377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
30203377Seschrock 	 * there's nothing to read from disk.
3021789Sahrens 	 */
30221732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3023789Sahrens 
3024789Sahrens 	vdev_config_dirty(tvd);
3025789Sahrens 
3026789Sahrens 	/*
30273377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
30283377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
30293377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
30303377Seschrock 	 * prevent vd from being accessed after it's freed.
3031789Sahrens 	 */
3032789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
3033789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
30341732Sbonwick 	vd->vdev_detached = B_TRUE;
30351732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3036789Sahrens 
30374451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
30384451Seschrock 
30392082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
30402082Seschrock 
30412082Seschrock 	/*
30423377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
30433377Seschrock 	 * then we want to go through and remove the device from the hot spare
30443377Seschrock 	 * list of every other pool.
30452082Seschrock 	 */
30462082Seschrock 	if (unspare) {
30472082Seschrock 		spa = NULL;
30482082Seschrock 		mutex_enter(&spa_namespace_lock);
30492082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
30502082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
30512082Seschrock 				continue;
30522082Seschrock 
30532082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
30542082Seschrock 		}
30552082Seschrock 		mutex_exit(&spa_namespace_lock);
30562082Seschrock 	}
30572082Seschrock 
30582082Seschrock 	return (error);
30592082Seschrock }
30602082Seschrock 
30612082Seschrock /*
30625450Sbrendan  * Remove a spares vdev from the nvlist config.
30632082Seschrock  */
30645450Sbrendan static int
30655450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare,
30665450Sbrendan     nvlist_t **spares, int nspares, vdev_t *vd)
30672082Seschrock {
30685450Sbrendan 	nvlist_t *nv, **newspares;
30695450Sbrendan 	int i, j;
30702082Seschrock 
30712082Seschrock 	nv = NULL;
30725450Sbrendan 	for (i = 0; i < nspares; i++) {
30735450Sbrendan 		uint64_t theguid;
30745450Sbrendan 
30755450Sbrendan 		VERIFY(nvlist_lookup_uint64(spares[i],
30765450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
30775450Sbrendan 		if (theguid == guid) {
30785450Sbrendan 			nv = spares[i];
30795450Sbrendan 			break;
30802082Seschrock 		}
30812082Seschrock 	}
30822082Seschrock 
30832082Seschrock 	/*
30845450Sbrendan 	 * Only remove the hot spare if it's not currently in use in this pool.
30852082Seschrock 	 */
30865450Sbrendan 	if (nv == NULL && vd == NULL)
30875450Sbrendan 		return (ENOENT);
30885450Sbrendan 
30895450Sbrendan 	if (nv == NULL && vd != NULL)
30905450Sbrendan 		return (ENOTSUP);
30915450Sbrendan 
30925450Sbrendan 	if (!unspare && nv != NULL && vd != NULL)
30935450Sbrendan 		return (EBUSY);
30942082Seschrock 
30952082Seschrock 	if (nspares == 1) {
30962082Seschrock 		newspares = NULL;
30972082Seschrock 	} else {
30982082Seschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
30992082Seschrock 		    KM_SLEEP);
31002082Seschrock 		for (i = 0, j = 0; i < nspares; i++) {
31012082Seschrock 			if (spares[i] != nv)
31022082Seschrock 				VERIFY(nvlist_dup(spares[i],
31032082Seschrock 				    &newspares[j++], KM_SLEEP) == 0);
31042082Seschrock 		}
31052082Seschrock 	}
31062082Seschrock 
31075450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES,
31082082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
31095450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
31105450Sbrendan 	    ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0);
31112082Seschrock 	for (i = 0; i < nspares - 1; i++)
31122082Seschrock 		nvlist_free(newspares[i]);
31132082Seschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
31145450Sbrendan 
31155450Sbrendan 	return (0);
31165450Sbrendan }
31175450Sbrendan 
31185450Sbrendan /*
31195450Sbrendan  * Remove an l2cache vdev from the nvlist config.
31205450Sbrendan  */
31215450Sbrendan static int
31225450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache,
31235450Sbrendan     int nl2cache, vdev_t *vd)
31245450Sbrendan {
31255450Sbrendan 	nvlist_t *nv, **newl2cache;
31265450Sbrendan 	int i, j;
31275450Sbrendan 
31285450Sbrendan 	nv = NULL;
31295450Sbrendan 	for (i = 0; i < nl2cache; i++) {
31305450Sbrendan 		uint64_t theguid;
31315450Sbrendan 
31325450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i],
31335450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
31345450Sbrendan 		if (theguid == guid) {
31355450Sbrendan 			nv = l2cache[i];
31365450Sbrendan 			break;
31375450Sbrendan 		}
31385450Sbrendan 	}
31395450Sbrendan 
31405450Sbrendan 	if (vd == NULL) {
31415450Sbrendan 		for (i = 0; i < nl2cache; i++) {
31425450Sbrendan 			if (sav->sav_vdevs[i]->vdev_guid == guid) {
31435450Sbrendan 				vd = sav->sav_vdevs[i];
31445450Sbrendan 				break;
31455450Sbrendan 			}
31465450Sbrendan 		}
31475450Sbrendan 	}
31485450Sbrendan 
31495450Sbrendan 	if (nv == NULL && vd == NULL)
31505450Sbrendan 		return (ENOENT);
31515450Sbrendan 
31525450Sbrendan 	if (nv == NULL && vd != NULL)
31535450Sbrendan 		return (ENOTSUP);
31545450Sbrendan 
31555450Sbrendan 	if (nl2cache == 1) {
31565450Sbrendan 		newl2cache = NULL;
31575450Sbrendan 	} else {
31585450Sbrendan 		newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *),
31595450Sbrendan 		    KM_SLEEP);
31605450Sbrendan 		for (i = 0, j = 0; i < nl2cache; i++) {
31615450Sbrendan 			if (l2cache[i] != nv)
31625450Sbrendan 				VERIFY(nvlist_dup(l2cache[i],
31635450Sbrendan 				    &newl2cache[j++], KM_SLEEP) == 0);
31645450Sbrendan 		}
31655450Sbrendan 	}
31665450Sbrendan 
31675450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
31685450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
31695450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
31705450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0);
31715450Sbrendan 	for (i = 0; i < nl2cache - 1; i++)
31725450Sbrendan 		nvlist_free(newl2cache[i]);
31735450Sbrendan 	kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *));
31745450Sbrendan 
31755450Sbrendan 	return (0);
31765450Sbrendan }
31775450Sbrendan 
31785450Sbrendan /*
31795450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
31805450Sbrendan  * spares and level 2 ARC devices.
31815450Sbrendan  */
31825450Sbrendan int
31835450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
31845450Sbrendan {
31855450Sbrendan 	vdev_t *vd;
31865450Sbrendan 	nvlist_t **spares, **l2cache;
31875450Sbrendan 	uint_t nspares, nl2cache;
31885450Sbrendan 	int error = 0;
31895450Sbrendan 
31905450Sbrendan 	spa_config_enter(spa, RW_WRITER, FTAG);
31915450Sbrendan 
31926643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
31935450Sbrendan 
31945450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
31955450Sbrendan 	    spa_spare_exists(guid, NULL) &&
31965450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
31975450Sbrendan 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
31985450Sbrendan 		if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare,
31995450Sbrendan 		    spares, nspares, vd)) != 0)
32005450Sbrendan 			goto out;
32015450Sbrendan 		spa_load_spares(spa);
32025450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
32035450Sbrendan 		goto out;
32045450Sbrendan 	}
32055450Sbrendan 
32065450Sbrendan 	if (spa->spa_l2cache.sav_vdevs != NULL &&
32075450Sbrendan 	    spa_l2cache_exists(guid, NULL) &&
32085450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
32095450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) {
32105450Sbrendan 		if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid,
32115450Sbrendan 		    l2cache, nl2cache, vd)) != 0)
32125450Sbrendan 			goto out;
32135450Sbrendan 		spa_load_l2cache(spa);
32145450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
32155450Sbrendan 	}
32162082Seschrock 
32172082Seschrock out:
32182082Seschrock 	spa_config_exit(spa, FTAG);
32195450Sbrendan 	return (error);
3220789Sahrens }
3221789Sahrens 
3222789Sahrens /*
32234451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
32244451Seschrock  * current spared, so we can detach it.
3225789Sahrens  */
32261544Seschrock static vdev_t *
32274451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3228789Sahrens {
32291544Seschrock 	vdev_t *newvd, *oldvd;
3230789Sahrens 	int c;
3231789Sahrens 
32321544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
32334451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
32341544Seschrock 		if (oldvd != NULL)
32351544Seschrock 			return (oldvd);
32361544Seschrock 	}
3237789Sahrens 
32384451Seschrock 	/*
32394451Seschrock 	 * Check for a completed replacement.
32404451Seschrock 	 */
3241789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
32421544Seschrock 		oldvd = vd->vdev_child[0];
32431544Seschrock 		newvd = vd->vdev_child[1];
3244789Sahrens 
32451544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
32461544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
32471544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
32481544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
32491544Seschrock 			return (oldvd);
32501544Seschrock 		}
32511544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
32521544Seschrock 	}
3253789Sahrens 
32544451Seschrock 	/*
32554451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
32564451Seschrock 	 */
32574451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
32584451Seschrock 		newvd = vd->vdev_child[0];
32594451Seschrock 		oldvd = vd->vdev_child[1];
32604451Seschrock 
32614451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
32624451Seschrock 		if (newvd->vdev_unspare &&
32634451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
32644451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
32654451Seschrock 			newvd->vdev_unspare = 0;
32664451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
32674451Seschrock 			return (oldvd);
32684451Seschrock 		}
32694451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
32704451Seschrock 	}
32714451Seschrock 
32721544Seschrock 	return (NULL);
3273789Sahrens }
3274789Sahrens 
32751544Seschrock static void
32764451Seschrock spa_vdev_resilver_done(spa_t *spa)
3277789Sahrens {
32781544Seschrock 	vdev_t *vd;
32792082Seschrock 	vdev_t *pvd;
32801544Seschrock 	uint64_t guid;
32812082Seschrock 	uint64_t pguid = 0;
3282789Sahrens 
32831544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3284789Sahrens 
32854451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
32861544Seschrock 		guid = vd->vdev_guid;
32872082Seschrock 		/*
32882082Seschrock 		 * If we have just finished replacing a hot spared device, then
32892082Seschrock 		 * we need to detach the parent's first child (the original hot
32902082Seschrock 		 * spare) as well.
32912082Seschrock 		 */
32922082Seschrock 		pvd = vd->vdev_parent;
32932082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
32942082Seschrock 		    pvd->vdev_id == 0) {
32952082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
32962082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
32972082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
32982082Seschrock 		}
32991544Seschrock 		spa_config_exit(spa, FTAG);
33001544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
33011544Seschrock 			return;
33022082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
33032082Seschrock 			return;
33041544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
3305789Sahrens 	}
3306789Sahrens 
33071544Seschrock 	spa_config_exit(spa, FTAG);
3308789Sahrens }
3309789Sahrens 
3310789Sahrens /*
33111354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
33121354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
33131354Seschrock  */
33141354Seschrock int
33151354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
33161354Seschrock {
33176643Seschrock 	vdev_t *vd;
33181354Seschrock 	uint64_t txg;
33191354Seschrock 
33201354Seschrock 	txg = spa_vdev_enter(spa);
33211354Seschrock 
33226643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
33232082Seschrock 		/*
33246643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
33256643Seschrock 		 * it is, update the path manually as there is no associated
33266643Seschrock 		 * vdev_t that can be synced to disk.
33272082Seschrock 		 */
33286643Seschrock 		nvlist_t **spares;
33296643Seschrock 		uint_t i, nspares;
33305450Sbrendan 
33315450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
33325450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
33335450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
33345450Sbrendan 			    &spares, &nspares) == 0);
33352082Seschrock 			for (i = 0; i < nspares; i++) {
33362082Seschrock 				uint64_t theguid;
33372082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
33382082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
33395450Sbrendan 				if (theguid == guid) {
33405450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
33415450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
33425450Sbrendan 					spa_load_spares(spa);
33435450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
33445450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
33455450Sbrendan 					    0));
33465450Sbrendan 				}
33472082Seschrock 			}
33482082Seschrock 		}
33495450Sbrendan 
33505450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
33512082Seschrock 	}
33521354Seschrock 
33531585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
33541585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
33551585Sbonwick 
33561354Seschrock 	spa_strfree(vd->vdev_path);
33571354Seschrock 	vd->vdev_path = spa_strdup(newpath);
33581354Seschrock 
33591354Seschrock 	vdev_config_dirty(vd->vdev_top);
33601354Seschrock 
33611354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
33621354Seschrock }
33631354Seschrock 
33641354Seschrock /*
3365789Sahrens  * ==========================================================================
3366789Sahrens  * SPA Scrubbing
3367789Sahrens  * ==========================================================================
3368789Sahrens  */
3369789Sahrens 
3370789Sahrens static void
3371789Sahrens spa_scrub_io_done(zio_t *zio)
3372789Sahrens {
3373789Sahrens 	spa_t *spa = zio->io_spa;
3374789Sahrens 
33754309Smaybee 	arc_data_buf_free(zio->io_data, zio->io_size);
3376789Sahrens 
3377789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
33781544Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
33791775Sbillm 		vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev;
3380789Sahrens 		spa->spa_scrub_errors++;
3381789Sahrens 		mutex_enter(&vd->vdev_stat_lock);
3382789Sahrens 		vd->vdev_stat.vs_scrub_errors++;
3383789Sahrens 		mutex_exit(&vd->vdev_stat_lock);
3384789Sahrens 	}
33853697Smishra 
33863697Smishra 	if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight)
33871544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
33883697Smishra 
33893697Smishra 	ASSERT(spa->spa_scrub_inflight >= 0);
33903697Smishra 
33911544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
3392789Sahrens }
3393789Sahrens 
3394789Sahrens static void
33951544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
33961544Seschrock     zbookmark_t *zb)
3397789Sahrens {
3398789Sahrens 	size_t size = BP_GET_LSIZE(bp);
33993697Smishra 	void *data;
3400789Sahrens 
3401789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
34023697Smishra 	/*
34033697Smishra 	 * Do not give too much work to vdev(s).
34043697Smishra 	 */
34053697Smishra 	while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) {
34063697Smishra 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
34073697Smishra 	}
3408789Sahrens 	spa->spa_scrub_inflight++;
3409789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3410789Sahrens 
34114309Smaybee 	data = arc_data_buf_alloc(size);
34123697Smishra 
34131544Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
34141544Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
34151544Seschrock 
34161807Sbonwick 	flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL;
34171544Seschrock 
3418789Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
34191544Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
3420789Sahrens }
3421789Sahrens 
3422789Sahrens /* ARGSUSED */
3423789Sahrens static int
3424789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
3425789Sahrens {
3426789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
34271775Sbillm 	vdev_t *vd = spa->spa_root_vdev;
34281775Sbillm 	dva_t *dva = bp->blk_dva;
34291775Sbillm 	int needs_resilver = B_FALSE;
34301775Sbillm 	int d;
3431789Sahrens 
34321775Sbillm 	if (bc->bc_errno) {
3433789Sahrens 		/*
3434789Sahrens 		 * We can't scrub this block, but we can continue to scrub
3435789Sahrens 		 * the rest of the pool.  Note the error and move along.
3436789Sahrens 		 */
3437789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
3438789Sahrens 		spa->spa_scrub_errors++;
3439789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
3440789Sahrens 
34411775Sbillm 		mutex_enter(&vd->vdev_stat_lock);
34421775Sbillm 		vd->vdev_stat.vs_scrub_errors++;
34431775Sbillm 		mutex_exit(&vd->vdev_stat_lock);
3444789Sahrens 
3445789Sahrens 		return (ERESTART);
3446789Sahrens 	}
3447789Sahrens 
3448789Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
3449789Sahrens 
34501775Sbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
34511775Sbillm 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]));
34521775Sbillm 
34531775Sbillm 		ASSERT(vd != NULL);
34541775Sbillm 
34551775Sbillm 		/*
34561775Sbillm 		 * Keep track of how much data we've examined so that
34571775Sbillm 		 * zpool(1M) status can make useful progress reports.
34581775Sbillm 		 */
34591775Sbillm 		mutex_enter(&vd->vdev_stat_lock);
34601775Sbillm 		vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]);
34611775Sbillm 		mutex_exit(&vd->vdev_stat_lock);
3462789Sahrens 
34631775Sbillm 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
34641775Sbillm 			if (DVA_GET_GANG(&dva[d])) {
34651775Sbillm 				/*
34661775Sbillm 				 * Gang members may be spread across multiple
34671775Sbillm 				 * vdevs, so the best we can do is look at the
34681775Sbillm 				 * pool-wide DTL.
34691775Sbillm 				 * XXX -- it would be better to change our
34701775Sbillm 				 * allocation policy to ensure that this can't
34711775Sbillm 				 * happen.
34721775Sbillm 				 */
34731775Sbillm 				vd = spa->spa_root_vdev;
34741775Sbillm 			}
34751775Sbillm 			if (vdev_dtl_contains(&vd->vdev_dtl_map,
34761775Sbillm 			    bp->blk_birth, 1))
34771775Sbillm 				needs_resilver = B_TRUE;
3478789Sahrens 		}
34791775Sbillm 	}
34801775Sbillm 
34811775Sbillm 	if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING)
3482789Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
34831544Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
34841775Sbillm 	else if (needs_resilver)
34851775Sbillm 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
34861775Sbillm 		    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
3487789Sahrens 
3488789Sahrens 	return (0);
3489789Sahrens }
3490789Sahrens 
3491789Sahrens static void
3492789Sahrens spa_scrub_thread(spa_t *spa)
3493789Sahrens {
3494789Sahrens 	callb_cpr_t cprinfo;
3495789Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
3496789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3497789Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
3498789Sahrens 	int error = 0;
3499789Sahrens 	boolean_t complete;
3500789Sahrens 
3501789Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
3502789Sahrens 
3503797Sbonwick 	/*
3504797Sbonwick 	 * If we're restarting due to a snapshot create/delete,
3505797Sbonwick 	 * wait for that to complete.
3506797Sbonwick 	 */
3507797Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
3508797Sbonwick 
35091544Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
35101544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
35111544Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
35121544Seschrock 
35131544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
35141544Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
3515789Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
3516789Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
35171544Seschrock 	spa_config_exit(spa, FTAG);
3518789Sahrens 
3519789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3520789Sahrens 	spa->spa_scrub_errors = 0;
3521789Sahrens 	spa->spa_scrub_active = 1;
35221544Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
3523789Sahrens 
3524789Sahrens 	while (!spa->spa_scrub_stop) {
3525789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
35261544Seschrock 		while (spa->spa_scrub_suspended) {
3527789Sahrens 			spa->spa_scrub_active = 0;
3528789Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
3529789Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3530789Sahrens 			spa->spa_scrub_active = 1;
3531789Sahrens 		}
3532789Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
3533789Sahrens 
3534789Sahrens 		if (spa->spa_scrub_restart_txg != 0)
3535789Sahrens 			break;
3536789Sahrens 
3537789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
3538789Sahrens 		error = traverse_more(th);
3539789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
3540789Sahrens 		if (error != EAGAIN)
3541789Sahrens 			break;
3542789Sahrens 	}
3543789Sahrens 
3544789Sahrens 	while (spa->spa_scrub_inflight)
3545789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3546789Sahrens 
35471601Sbonwick 	spa->spa_scrub_active = 0;
35481601Sbonwick 	cv_broadcast(&spa->spa_scrub_cv);
35491601Sbonwick 
35501601Sbonwick 	mutex_exit(&spa->spa_scrub_lock);
35511601Sbonwick 
35521601Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
35531601Sbonwick 
35541601Sbonwick 	mutex_enter(&spa->spa_scrub_lock);
35551601Sbonwick 
35561601Sbonwick 	/*
35571601Sbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
35581601Sbonwick 	 * AND the spa config lock to synchronize with any config changes
35591601Sbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
35601601Sbonwick 	 */
3561789Sahrens 	if (spa->spa_scrub_restart_txg != 0)
3562789Sahrens 		error = ERESTART;
3563789Sahrens 
35641544Seschrock 	if (spa->spa_scrub_stop)
35651544Seschrock 		error = EINTR;
35661544Seschrock 
3567789Sahrens 	/*
35681544Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
35691544Seschrock 	 * completed.  The downside is that if there is a transient error during
35701544Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
35711544Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
35721544Seschrock 	 * which isn't really acceptable.  Since there is enough information for
35731544Seschrock 	 * the user to know what has failed and why, this seems like a more
35741544Seschrock 	 * tractable approach.
3575789Sahrens 	 */
35761544Seschrock 	complete = (error == 0);
3577789Sahrens 
35781544Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
35791544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
3580789Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
3581789Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
3582789Sahrens 
3583789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3584789Sahrens 
3585789Sahrens 	/*
3586789Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
3587789Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
3588789Sahrens 	 */
3589789Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
3590789Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
3591789Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
35921544Seschrock 	spa_errlog_rotate(spa);
35931601Sbonwick 
35944451Seschrock 	if (scrub_type == POOL_SCRUB_RESILVER && complete)
35954451Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH);
35964451Seschrock 
35971544Seschrock 	spa_config_exit(spa, FTAG);
3598789Sahrens 
3599789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3600789Sahrens 
36011544Seschrock 	/*
36021544Seschrock 	 * We may have finished replacing a device.
36031544Seschrock 	 * Let the async thread assess this and handle the detach.
36041544Seschrock 	 */
36054451Seschrock 	spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3606789Sahrens 
3607789Sahrens 	/*
3608789Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
3609789Sahrens 	 */
3610789Sahrens 	if (error == ERESTART)
36111544Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
36121544Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
3613789Sahrens 
36141544Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
36151544Seschrock 	spa->spa_scrub_active = 0;
36161544Seschrock 	spa->spa_scrub_thread = NULL;
36171544Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
3618789Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
3619789Sahrens 	thread_exit();
3620789Sahrens }
3621789Sahrens 
3622789Sahrens void
3623789Sahrens spa_scrub_suspend(spa_t *spa)
3624789Sahrens {
3625789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
36261544Seschrock 	spa->spa_scrub_suspended++;
3627789Sahrens 	while (spa->spa_scrub_active) {
3628789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3629789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3630789Sahrens 	}
3631789Sahrens 	while (spa->spa_scrub_inflight)
3632789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3633789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3634789Sahrens }
3635789Sahrens 
3636789Sahrens void
3637789Sahrens spa_scrub_resume(spa_t *spa)
3638789Sahrens {
3639789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
36401544Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
36411544Seschrock 	if (--spa->spa_scrub_suspended == 0)
3642789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3643789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3644789Sahrens }
3645789Sahrens 
3646789Sahrens void
3647789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
3648789Sahrens {
3649789Sahrens 	/*
3650789Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
3651789Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
3652789Sahrens 	 * fix this properly.
3653789Sahrens 	 */
3654789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3655789Sahrens 	spa->spa_scrub_restart_txg = txg;
3656789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3657789Sahrens }
3658789Sahrens 
36591544Seschrock int
36601544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
3661789Sahrens {
3662789Sahrens 	space_seg_t *ss;
3663789Sahrens 	uint64_t mintxg, maxtxg;
3664789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3665789Sahrens 
36664808Sek110237 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
36674808Sek110237 	ASSERT(!spa_config_held(spa, RW_WRITER));
36684808Sek110237 
3669789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3670789Sahrens 		return (ENOTSUP);
3671789Sahrens 
36721544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
36731544Seschrock 
3674789Sahrens 	/*
3675789Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
3676789Sahrens 	 */
3677789Sahrens 	while (spa->spa_scrub_thread != NULL) {
3678789Sahrens 		/*
3679789Sahrens 		 * Don't stop a resilver unless forced.
3680789Sahrens 		 */
36811544Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
36821544Seschrock 			mutex_exit(&spa->spa_scrub_lock);
3683789Sahrens 			return (EBUSY);
36841544Seschrock 		}
3685789Sahrens 		spa->spa_scrub_stop = 1;
3686789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3687789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3688789Sahrens 	}
3689789Sahrens 
3690789Sahrens 	/*
3691789Sahrens 	 * Terminate the previous traverse.
3692789Sahrens 	 */
3693789Sahrens 	if (spa->spa_scrub_th != NULL) {
3694789Sahrens 		traverse_fini(spa->spa_scrub_th);
3695789Sahrens 		spa->spa_scrub_th = NULL;
3696789Sahrens 	}
3697789Sahrens 
36981544Seschrock 	if (rvd == NULL) {
36991544Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
37001544Seschrock 		ASSERT(spa->spa_scrub_type == type);
37011544Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
37021544Seschrock 		mutex_exit(&spa->spa_scrub_lock);
37031544Seschrock 		return (0);
37041544Seschrock 	}
3705789Sahrens 
3706789Sahrens 	mintxg = TXG_INITIAL - 1;
3707789Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
3708789Sahrens 
37091544Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
3710789Sahrens 
37111544Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
37121544Seschrock 		/*
37131544Seschrock 		 * The pool-wide DTL is empty.
37141732Sbonwick 		 * If this is a resilver, there's nothing to do except
37151732Sbonwick 		 * check whether any in-progress replacements have completed.
37161544Seschrock 		 */
37171732Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
37181544Seschrock 			type = POOL_SCRUB_NONE;
37194451Seschrock 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
37201732Sbonwick 		}
37211544Seschrock 	} else {
37221544Seschrock 		/*
37231544Seschrock 		 * The pool-wide DTL is non-empty.
37241544Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
37251544Seschrock 		 */
37261544Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
37271544Seschrock 			type = POOL_SCRUB_RESILVER;
37281544Seschrock 	}
3729789Sahrens 
37301544Seschrock 	if (type == POOL_SCRUB_RESILVER) {
3731789Sahrens 		/*
3732789Sahrens 		 * Determine the resilvering boundaries.
3733789Sahrens 		 *
3734789Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
3735789Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
3736789Sahrens 		 *
3737789Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
3738789Sahrens 		 * so we don't claim to resilver a txg that's still changing.
3739789Sahrens 		 */
3740789Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
37411544Seschrock 		mintxg = ss->ss_start - 1;
3742789Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
37431544Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
37444451Seschrock 
37454451Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
3746789Sahrens 	}
3747789Sahrens 
37481544Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
37491544Seschrock 
37501544Seschrock 	spa->spa_scrub_stop = 0;
37511544Seschrock 	spa->spa_scrub_type = type;
37521544Seschrock 	spa->spa_scrub_restart_txg = 0;
37531544Seschrock 
37541544Seschrock 	if (type != POOL_SCRUB_NONE) {
37551544Seschrock 		spa->spa_scrub_mintxg = mintxg;
3756789Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
3757789Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
37581635Sbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
37591635Sbonwick 		    ZIO_FLAG_CANFAIL);
3760789Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
3761789Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
3762789Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3763789Sahrens 	}
3764789Sahrens 
37651544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
37661544Seschrock 
3767789Sahrens 	return (0);
3768789Sahrens }
3769789Sahrens 
37701544Seschrock /*
37711544Seschrock  * ==========================================================================
37721544Seschrock  * SPA async task processing
37731544Seschrock  * ==========================================================================
37741544Seschrock  */
37751544Seschrock 
37761544Seschrock static void
37774451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3778789Sahrens {
37791544Seschrock 	vdev_t *tvd;
37801544Seschrock 	int c;
37811544Seschrock 
37824451Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
37834451Seschrock 		tvd = vd->vdev_child[c];
37844451Seschrock 		if (tvd->vdev_remove_wanted) {
37854451Seschrock 			tvd->vdev_remove_wanted = 0;
37864451Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
37874451Seschrock 			    VDEV_AUX_NONE);
37885329Sgw25295 			vdev_clear(spa, tvd, B_TRUE);
37894451Seschrock 			vdev_config_dirty(tvd->vdev_top);
37901544Seschrock 		}
37914451Seschrock 		spa_async_remove(spa, tvd);
37921544Seschrock 	}
37931544Seschrock }
37941544Seschrock 
37951544Seschrock static void
37961544Seschrock spa_async_thread(spa_t *spa)
37971544Seschrock {
37981544Seschrock 	int tasks;
37994451Seschrock 	uint64_t txg;
38001544Seschrock 
38011544Seschrock 	ASSERT(spa->spa_sync_on);
3802789Sahrens 
38031544Seschrock 	mutex_enter(&spa->spa_async_lock);
38041544Seschrock 	tasks = spa->spa_async_tasks;
38051544Seschrock 	spa->spa_async_tasks = 0;
38061544Seschrock 	mutex_exit(&spa->spa_async_lock);
38071544Seschrock 
38081544Seschrock 	/*
38091635Sbonwick 	 * See if the config needs to be updated.
38101635Sbonwick 	 */
38111635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
38121635Sbonwick 		mutex_enter(&spa_namespace_lock);
38131635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
38141635Sbonwick 		mutex_exit(&spa_namespace_lock);
38151635Sbonwick 	}
38161635Sbonwick 
38171635Sbonwick 	/*
38184451Seschrock 	 * See if any devices need to be marked REMOVED.
38195329Sgw25295 	 *
38205329Sgw25295 	 * XXX - We avoid doing this when we are in
38215329Sgw25295 	 * I/O failure state since spa_vdev_enter() grabs
38225329Sgw25295 	 * the namespace lock and would not be able to obtain
38235329Sgw25295 	 * the writer config lock.
38241544Seschrock 	 */
38255329Sgw25295 	if (tasks & SPA_ASYNC_REMOVE &&
38265329Sgw25295 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
38274451Seschrock 		txg = spa_vdev_enter(spa);
38284451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
38294451Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
38304451Seschrock 	}
38311544Seschrock 
38321544Seschrock 	/*
38331544Seschrock 	 * If any devices are done replacing, detach them.
38341544Seschrock 	 */
38354451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
38364451Seschrock 		spa_vdev_resilver_done(spa);
3837789Sahrens 
38381544Seschrock 	/*
38394451Seschrock 	 * Kick off a scrub.  When starting a RESILVER scrub (or an EVERYTHING
38404451Seschrock 	 * scrub which can become a resilver), we need to hold
38414451Seschrock 	 * spa_namespace_lock() because the sysevent we post via
38424451Seschrock 	 * spa_event_notify() needs to get the name of the pool.
38431544Seschrock 	 */
38444451Seschrock 	if (tasks & SPA_ASYNC_SCRUB) {
38454451Seschrock 		mutex_enter(&spa_namespace_lock);
38461544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
38474451Seschrock 		mutex_exit(&spa_namespace_lock);
38484451Seschrock 	}
38491544Seschrock 
38501544Seschrock 	/*
38511544Seschrock 	 * Kick off a resilver.
38521544Seschrock 	 */
38534451Seschrock 	if (tasks & SPA_ASYNC_RESILVER) {
38544451Seschrock 		mutex_enter(&spa_namespace_lock);
38551544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
38564451Seschrock 		mutex_exit(&spa_namespace_lock);
38574451Seschrock 	}
38581544Seschrock 
38591544Seschrock 	/*
38601544Seschrock 	 * Let the world know that we're done.
38611544Seschrock 	 */
38621544Seschrock 	mutex_enter(&spa->spa_async_lock);
38631544Seschrock 	spa->spa_async_thread = NULL;
38641544Seschrock 	cv_broadcast(&spa->spa_async_cv);
38651544Seschrock 	mutex_exit(&spa->spa_async_lock);
38661544Seschrock 	thread_exit();
38671544Seschrock }
38681544Seschrock 
38691544Seschrock void
38701544Seschrock spa_async_suspend(spa_t *spa)
38711544Seschrock {
38721544Seschrock 	mutex_enter(&spa->spa_async_lock);
38731544Seschrock 	spa->spa_async_suspended++;
38741544Seschrock 	while (spa->spa_async_thread != NULL)
38751544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
38761544Seschrock 	mutex_exit(&spa->spa_async_lock);
38771544Seschrock }
38781544Seschrock 
38791544Seschrock void
38801544Seschrock spa_async_resume(spa_t *spa)
38811544Seschrock {
38821544Seschrock 	mutex_enter(&spa->spa_async_lock);
38831544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
38841544Seschrock 	spa->spa_async_suspended--;
38851544Seschrock 	mutex_exit(&spa->spa_async_lock);
38861544Seschrock }
38871544Seschrock 
38881544Seschrock static void
38891544Seschrock spa_async_dispatch(spa_t *spa)
38901544Seschrock {
38911544Seschrock 	mutex_enter(&spa->spa_async_lock);
38921544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
38931635Sbonwick 	    spa->spa_async_thread == NULL &&
38941635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
38951544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
38961544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
38971544Seschrock 	mutex_exit(&spa->spa_async_lock);
38981544Seschrock }
38991544Seschrock 
39001544Seschrock void
39011544Seschrock spa_async_request(spa_t *spa, int task)
39021544Seschrock {
39031544Seschrock 	mutex_enter(&spa->spa_async_lock);
39041544Seschrock 	spa->spa_async_tasks |= task;
39051544Seschrock 	mutex_exit(&spa->spa_async_lock);
3906789Sahrens }
3907789Sahrens 
3908789Sahrens /*
3909789Sahrens  * ==========================================================================
3910789Sahrens  * SPA syncing routines
3911789Sahrens  * ==========================================================================
3912789Sahrens  */
3913789Sahrens 
3914789Sahrens static void
3915789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3916789Sahrens {
3917789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3918789Sahrens 	dmu_tx_t *tx;
3919789Sahrens 	blkptr_t blk;
3920789Sahrens 	uint64_t itor = 0;
3921789Sahrens 	zio_t *zio;
3922789Sahrens 	int error;
3923789Sahrens 	uint8_t c = 1;
3924789Sahrens 
3925789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3926789Sahrens 
3927789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3928789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3929789Sahrens 
3930789Sahrens 	error = zio_wait(zio);
3931789Sahrens 	ASSERT3U(error, ==, 0);
3932789Sahrens 
3933789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3934789Sahrens 	bplist_vacate(bpl, tx);
3935789Sahrens 
3936789Sahrens 	/*
3937789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3938789Sahrens 	 * (Usually only the first block is needed.)
3939789Sahrens 	 */
3940789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3941789Sahrens 	dmu_tx_commit(tx);
3942789Sahrens }
3943789Sahrens 
3944789Sahrens static void
39452082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
39462082Seschrock {
39472082Seschrock 	char *packed = NULL;
39482082Seschrock 	size_t nvsize = 0;
39492082Seschrock 	dmu_buf_t *db;
39502082Seschrock 
39512082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
39522082Seschrock 
39532082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
39542082Seschrock 
39552082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
39562082Seschrock 	    KM_SLEEP) == 0);
39572082Seschrock 
39582082Seschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
39592082Seschrock 
39602082Seschrock 	kmem_free(packed, nvsize);
39612082Seschrock 
39622082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
39632082Seschrock 	dmu_buf_will_dirty(db, tx);
39642082Seschrock 	*(uint64_t *)db->db_data = nvsize;
39652082Seschrock 	dmu_buf_rele(db, FTAG);
39662082Seschrock }
39672082Seschrock 
39682082Seschrock static void
39695450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
39705450Sbrendan     const char *config, const char *entry)
39712082Seschrock {
39722082Seschrock 	nvlist_t *nvroot;
39735450Sbrendan 	nvlist_t **list;
39742082Seschrock 	int i;
39752082Seschrock 
39765450Sbrendan 	if (!sav->sav_sync)
39772082Seschrock 		return;
39782082Seschrock 
39792082Seschrock 	/*
39805450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
39815450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
39824451Seschrock 	 * valid and the vdevs are labeled appropriately.
39832082Seschrock 	 */
39845450Sbrendan 	if (sav->sav_object == 0) {
39855450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
39865450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
39875450Sbrendan 		    sizeof (uint64_t), tx);
39882082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
39895450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
39905450Sbrendan 		    &sav->sav_object, tx) == 0);
39912082Seschrock 	}
39922082Seschrock 
39932082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
39945450Sbrendan 	if (sav->sav_count == 0) {
39955450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
39962082Seschrock 	} else {
39975450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
39985450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
39995450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
40005450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
40015450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
40025450Sbrendan 		    sav->sav_count) == 0);
40035450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
40045450Sbrendan 			nvlist_free(list[i]);
40055450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
40062082Seschrock 	}
40072082Seschrock 
40085450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
40092926Sek110237 	nvlist_free(nvroot);
40102082Seschrock 
40115450Sbrendan 	sav->sav_sync = B_FALSE;
40122082Seschrock }
40132082Seschrock 
40142082Seschrock static void
4015789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
4016789Sahrens {
4017789Sahrens 	nvlist_t *config;
4018789Sahrens 
4019789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
4020789Sahrens 		return;
4021789Sahrens 
4022789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
4023789Sahrens 
40241635Sbonwick 	if (spa->spa_config_syncing)
40251635Sbonwick 		nvlist_free(spa->spa_config_syncing);
40261635Sbonwick 	spa->spa_config_syncing = config;
4027789Sahrens 
40282082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
4029789Sahrens }
4030789Sahrens 
40315094Slling /*
40325094Slling  * Set zpool properties.
40335094Slling  */
40343912Slling static void
40354543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
40363912Slling {
40373912Slling 	spa_t *spa = arg1;
40385094Slling 	objset_t *mos = spa->spa_meta_objset;
40393912Slling 	nvlist_t *nvp = arg2;
40405094Slling 	nvpair_t *elem;
40414451Seschrock 	uint64_t intval;
40426643Seschrock 	char *strval;
40435094Slling 	zpool_prop_t prop;
40445094Slling 	const char *propname;
40455094Slling 	zprop_type_t proptype;
40466643Seschrock 	spa_config_dirent_t *dp;
40475094Slling 
40485094Slling 	elem = NULL;
40495094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
40505094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
40515094Slling 		case ZPOOL_PROP_VERSION:
40525094Slling 			/*
40535094Slling 			 * Only set version for non-zpool-creation cases
40545094Slling 			 * (set/import). spa_create() needs special care
40555094Slling 			 * for version setting.
40565094Slling 			 */
40575094Slling 			if (tx->tx_txg != TXG_INITIAL) {
40585094Slling 				VERIFY(nvpair_value_uint64(elem,
40595094Slling 				    &intval) == 0);
40605094Slling 				ASSERT(intval <= SPA_VERSION);
40615094Slling 				ASSERT(intval >= spa_version(spa));
40625094Slling 				spa->spa_uberblock.ub_version = intval;
40635094Slling 				vdev_config_dirty(spa->spa_root_vdev);
40645094Slling 			}
40655094Slling 			break;
40665094Slling 
40675094Slling 		case ZPOOL_PROP_ALTROOT:
40685094Slling 			/*
40695094Slling 			 * 'altroot' is a non-persistent property. It should
40705094Slling 			 * have been set temporarily at creation or import time.
40715094Slling 			 */
40725094Slling 			ASSERT(spa->spa_root != NULL);
40735094Slling 			break;
40745094Slling 
40755363Seschrock 		case ZPOOL_PROP_CACHEFILE:
40765094Slling 			/*
40775363Seschrock 			 * 'cachefile' is a non-persistent property, but note
40785363Seschrock 			 * an async request that the config cache needs to be
40795363Seschrock 			 * udpated.
40805094Slling 			 */
40815363Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
40826643Seschrock 
40836643Seschrock 			dp = kmem_alloc(sizeof (spa_config_dirent_t),
40846643Seschrock 			    KM_SLEEP);
40856643Seschrock 
40866643Seschrock 			if (strval[0] == '\0')
40876643Seschrock 				dp->scd_path = spa_strdup(spa_config_path);
40886643Seschrock 			else if (strcmp(strval, "none") == 0)
40896643Seschrock 				dp->scd_path = NULL;
40906643Seschrock 			else
40916643Seschrock 				dp->scd_path = spa_strdup(strval);
40926643Seschrock 
40936643Seschrock 			list_insert_head(&spa->spa_config_list, dp);
40945363Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
40954543Smarks 			break;
40965094Slling 		default:
40975094Slling 			/*
40985094Slling 			 * Set pool property values in the poolprops mos object.
40995094Slling 			 */
41005094Slling 			mutex_enter(&spa->spa_props_lock);
41015094Slling 			if (spa->spa_pool_props_object == 0) {
41025094Slling 				objset_t *mos = spa->spa_meta_objset;
41035094Slling 
41045094Slling 				VERIFY((spa->spa_pool_props_object =
41055094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
41065094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
41075094Slling 
41085094Slling 				VERIFY(zap_update(mos,
41095094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
41105094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
41115094Slling 				    == 0);
41125094Slling 			}
41135094Slling 			mutex_exit(&spa->spa_props_lock);
41145094Slling 
41155094Slling 			/* normalize the property name */
41165094Slling 			propname = zpool_prop_to_name(prop);
41175094Slling 			proptype = zpool_prop_get_type(prop);
41185094Slling 
41195094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
41205094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
41215094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
41225094Slling 				VERIFY(zap_update(mos,
41235094Slling 				    spa->spa_pool_props_object, propname,
41245094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
41255094Slling 
41265094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
41275094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
41285094Slling 
41295094Slling 				if (proptype == PROP_TYPE_INDEX) {
41305094Slling 					const char *unused;
41315094Slling 					VERIFY(zpool_prop_index_to_string(
41325094Slling 					    prop, intval, &unused) == 0);
41335094Slling 				}
41345094Slling 				VERIFY(zap_update(mos,
41355094Slling 				    spa->spa_pool_props_object, propname,
41365094Slling 				    8, 1, &intval, tx) == 0);
41375094Slling 			} else {
41385094Slling 				ASSERT(0); /* not allowed */
41395094Slling 			}
41405094Slling 
41415329Sgw25295 			switch (prop) {
41425329Sgw25295 			case ZPOOL_PROP_DELEGATION:
41435094Slling 				spa->spa_delegation = intval;
41445329Sgw25295 				break;
41455329Sgw25295 			case ZPOOL_PROP_BOOTFS:
41465094Slling 				spa->spa_bootfs = intval;
41475329Sgw25295 				break;
41485329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
41495329Sgw25295 				spa->spa_failmode = intval;
41505329Sgw25295 				break;
41515329Sgw25295 			default:
41525329Sgw25295 				break;
41535329Sgw25295 			}
41543912Slling 		}
41555094Slling 
41565094Slling 		/* log internal history if this is not a zpool create */
41575094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
41585094Slling 		    tx->tx_txg != TXG_INITIAL) {
41595094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
41605094Slling 			    spa, tx, cr, "%s %lld %s",
41615094Slling 			    nvpair_name(elem), intval, spa->spa_name);
41625094Slling 		}
41633912Slling 	}
41643912Slling }
41653912Slling 
4166789Sahrens /*
4167789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
4168789Sahrens  * part of the process, so we iterate until it converges.
4169789Sahrens  */
4170789Sahrens void
4171789Sahrens spa_sync(spa_t *spa, uint64_t txg)
4172789Sahrens {
4173789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
4174789Sahrens 	objset_t *mos = spa->spa_meta_objset;
4175789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
41761635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4177789Sahrens 	vdev_t *vd;
4178789Sahrens 	dmu_tx_t *tx;
4179789Sahrens 	int dirty_vdevs;
4180789Sahrens 
4181789Sahrens 	/*
4182789Sahrens 	 * Lock out configuration changes.
4183789Sahrens 	 */
41841544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
4185789Sahrens 
4186789Sahrens 	spa->spa_syncing_txg = txg;
4187789Sahrens 	spa->spa_sync_pass = 0;
4188789Sahrens 
41891544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4190789Sahrens 
41912082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
41922082Seschrock 
41932082Seschrock 	/*
41944577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
41952082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
41962082Seschrock 	 */
41974577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
41984577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
41992082Seschrock 		int i;
42002082Seschrock 
42012082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
42022082Seschrock 			vd = rvd->vdev_child[i];
42032082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
42042082Seschrock 				break;
42052082Seschrock 		}
42062082Seschrock 		if (i == rvd->vdev_children) {
42072082Seschrock 			spa->spa_deflate = TRUE;
42082082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
42092082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
42102082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
42112082Seschrock 		}
42122082Seschrock 	}
42132082Seschrock 
4214789Sahrens 	/*
4215789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4216789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4217789Sahrens 	 * don't generate work on an otherwise idle system.
4218789Sahrens 	 */
4219789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
42202329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
42212329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4222789Sahrens 		spa_sync_deferred_frees(spa, txg);
4223789Sahrens 
4224789Sahrens 	/*
4225789Sahrens 	 * Iterate to convergence.
4226789Sahrens 	 */
4227789Sahrens 	do {
4228789Sahrens 		spa->spa_sync_pass++;
4229789Sahrens 
4230789Sahrens 		spa_sync_config_object(spa, tx);
42315450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
42325450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
42335450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
42345450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
42351544Seschrock 		spa_errlog_sync(spa, txg);
4236789Sahrens 		dsl_pool_sync(dp, txg);
4237789Sahrens 
4238789Sahrens 		dirty_vdevs = 0;
4239789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4240789Sahrens 			vdev_sync(vd, txg);
4241789Sahrens 			dirty_vdevs++;
4242789Sahrens 		}
4243789Sahrens 
4244789Sahrens 		bplist_sync(bpl, tx);
4245789Sahrens 	} while (dirty_vdevs);
4246789Sahrens 
4247789Sahrens 	bplist_close(bpl);
4248789Sahrens 
4249789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4250789Sahrens 
4251789Sahrens 	/*
4252789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4253789Sahrens 	 * to commit the transaction group.
42541635Sbonwick 	 *
42555688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
42565688Sbonwick 	 * random top-level vdevs that are known to be visible in the
42575688Sbonwick 	 * config cache (see spa_vdev_add() for details).  If there *are*
42585688Sbonwick 	 * dirty vdevs -- or if the sync to our random subset fails --
42595688Sbonwick 	 * then sync the uberblock to all vdevs.
4260789Sahrens 	 */
42615688Sbonwick 	if (list_is_empty(&spa->spa_dirty_list)) {
42626615Sgw25295 		vdev_t *svd[SPA_DVAS_PER_BP];
42636615Sgw25295 		int svdcount = 0;
42641635Sbonwick 		int children = rvd->vdev_children;
42651635Sbonwick 		int c0 = spa_get_random(children);
42661635Sbonwick 		int c;
42671635Sbonwick 
42681635Sbonwick 		for (c = 0; c < children; c++) {
42691635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
42705688Sbonwick 			if (vd->vdev_ms_array == 0 || vd->vdev_islog)
42711635Sbonwick 				continue;
42725688Sbonwick 			svd[svdcount++] = vd;
42735688Sbonwick 			if (svdcount == SPA_DVAS_PER_BP)
42741635Sbonwick 				break;
42751635Sbonwick 		}
42766615Sgw25295 		vdev_config_sync(svd, svdcount, txg);
42776615Sgw25295 	} else {
42786615Sgw25295 		vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg);
42791635Sbonwick 	}
42802082Seschrock 	dmu_tx_commit(tx);
42812082Seschrock 
42821635Sbonwick 	/*
42831635Sbonwick 	 * Clear the dirty config list.
42841635Sbonwick 	 */
42851635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
42861635Sbonwick 		vdev_config_clean(vd);
42871635Sbonwick 
42881635Sbonwick 	/*
42891635Sbonwick 	 * Now that the new config has synced transactionally,
42901635Sbonwick 	 * let it become visible to the config cache.
42911635Sbonwick 	 */
42921635Sbonwick 	if (spa->spa_config_syncing != NULL) {
42931635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
42941635Sbonwick 		spa->spa_config_txg = txg;
42951635Sbonwick 		spa->spa_config_syncing = NULL;
42961635Sbonwick 	}
4297789Sahrens 
4298789Sahrens 	/*
4299789Sahrens 	 * Make a stable copy of the fully synced uberblock.
4300789Sahrens 	 * We use this as the root for pool traversals.
4301789Sahrens 	 */
4302789Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
4303789Sahrens 
4304789Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
4305789Sahrens 
4306789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
4307789Sahrens 	spa->spa_traverse_wanted = 0;
4308789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4309789Sahrens 	rw_exit(&spa->spa_traverse_lock);
4310789Sahrens 
4311789Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
4312789Sahrens 
4313789Sahrens 	/*
4314789Sahrens 	 * Clean up the ZIL records for the synced txg.
4315789Sahrens 	 */
4316789Sahrens 	dsl_pool_zil_clean(dp);
4317789Sahrens 
4318789Sahrens 	/*
4319789Sahrens 	 * Update usable space statistics.
4320789Sahrens 	 */
4321789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4322789Sahrens 		vdev_sync_done(vd, txg);
4323789Sahrens 
4324789Sahrens 	/*
4325789Sahrens 	 * It had better be the case that we didn't dirty anything
43262082Seschrock 	 * since vdev_config_sync().
4327789Sahrens 	 */
4328789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4329789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4330789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4331789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4332789Sahrens 
43331544Seschrock 	spa_config_exit(spa, FTAG);
43341544Seschrock 
43351544Seschrock 	/*
43361544Seschrock 	 * If any async tasks have been requested, kick them off.
43371544Seschrock 	 */
43381544Seschrock 	spa_async_dispatch(spa);
4339789Sahrens }
4340789Sahrens 
4341789Sahrens /*
4342789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4343789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4344789Sahrens  * sync.
4345789Sahrens  */
4346789Sahrens void
4347789Sahrens spa_sync_allpools(void)
4348789Sahrens {
4349789Sahrens 	spa_t *spa = NULL;
4350789Sahrens 	mutex_enter(&spa_namespace_lock);
4351789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4352789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
4353789Sahrens 			continue;
4354789Sahrens 		spa_open_ref(spa, FTAG);
4355789Sahrens 		mutex_exit(&spa_namespace_lock);
4356789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4357789Sahrens 		mutex_enter(&spa_namespace_lock);
4358789Sahrens 		spa_close(spa, FTAG);
4359789Sahrens 	}
4360789Sahrens 	mutex_exit(&spa_namespace_lock);
4361789Sahrens }
4362789Sahrens 
4363789Sahrens /*
4364789Sahrens  * ==========================================================================
4365789Sahrens  * Miscellaneous routines
4366789Sahrens  * ==========================================================================
4367789Sahrens  */
4368789Sahrens 
4369789Sahrens /*
4370789Sahrens  * Remove all pools in the system.
4371789Sahrens  */
4372789Sahrens void
4373789Sahrens spa_evict_all(void)
4374789Sahrens {
4375789Sahrens 	spa_t *spa;
4376789Sahrens 
4377789Sahrens 	/*
4378789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4379789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4380789Sahrens 	 */
4381789Sahrens 	mutex_enter(&spa_namespace_lock);
4382789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4383789Sahrens 		/*
43841544Seschrock 		 * Stop async tasks.  The async thread may need to detach
43851544Seschrock 		 * a device that's been replaced, which requires grabbing
43861544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4387789Sahrens 		 */
4388789Sahrens 		spa_open_ref(spa, FTAG);
4389789Sahrens 		mutex_exit(&spa_namespace_lock);
43901544Seschrock 		spa_async_suspend(spa);
43914808Sek110237 		mutex_enter(&spa_namespace_lock);
4392789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
4393789Sahrens 		spa_close(spa, FTAG);
4394789Sahrens 
4395789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4396789Sahrens 			spa_unload(spa);
4397789Sahrens 			spa_deactivate(spa);
4398789Sahrens 		}
4399789Sahrens 		spa_remove(spa);
4400789Sahrens 	}
4401789Sahrens 	mutex_exit(&spa_namespace_lock);
4402789Sahrens }
44031544Seschrock 
44041544Seschrock vdev_t *
44056643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
44061544Seschrock {
44076643Seschrock 	vdev_t *vd;
44086643Seschrock 	int i;
44096643Seschrock 
44106643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
44116643Seschrock 		return (vd);
44126643Seschrock 
44136643Seschrock 	if (l2cache) {
44146643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
44156643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
44166643Seschrock 			if (vd->vdev_guid == guid)
44176643Seschrock 				return (vd);
44186643Seschrock 		}
44196643Seschrock 	}
44206643Seschrock 
44216643Seschrock 	return (NULL);
44221544Seschrock }
44231760Seschrock 
44241760Seschrock void
44255094Slling spa_upgrade(spa_t *spa, uint64_t version)
44261760Seschrock {
44271760Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
44281760Seschrock 
44291760Seschrock 	/*
44301760Seschrock 	 * This should only be called for a non-faulted pool, and since a
44311760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
44321760Seschrock 	 * possible.
44331760Seschrock 	 */
44344577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
44355094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
44365094Slling 
44375094Slling 	spa->spa_uberblock.ub_version = version;
44381760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
44391760Seschrock 
44401760Seschrock 	spa_config_exit(spa, FTAG);
44412082Seschrock 
44422082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
44431760Seschrock }
44442082Seschrock 
44452082Seschrock boolean_t
44462082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
44472082Seschrock {
44482082Seschrock 	int i;
44493377Seschrock 	uint64_t spareguid;
44505450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
44515450Sbrendan 
44525450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
44535450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
44542082Seschrock 			return (B_TRUE);
44552082Seschrock 
44565450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
44575450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
44585450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
44593377Seschrock 			return (B_TRUE);
44603377Seschrock 	}
44613377Seschrock 
44622082Seschrock 	return (B_FALSE);
44632082Seschrock }
44643912Slling 
44654451Seschrock /*
44664451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
44674451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
44684451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
44694451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
44704451Seschrock  * or zdb as real changes.
44714451Seschrock  */
44724451Seschrock void
44734451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
44744451Seschrock {
44754451Seschrock #ifdef _KERNEL
44764451Seschrock 	sysevent_t		*ev;
44774451Seschrock 	sysevent_attr_list_t	*attr = NULL;
44784451Seschrock 	sysevent_value_t	value;
44794451Seschrock 	sysevent_id_t		eid;
44804451Seschrock 
44814451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
44824451Seschrock 	    SE_SLEEP);
44834451Seschrock 
44844451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
44854451Seschrock 	value.value.sv_string = spa_name(spa);
44864451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
44874451Seschrock 		goto done;
44884451Seschrock 
44894451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
44904451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
44914451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
44924451Seschrock 		goto done;
44934451Seschrock 
44944451Seschrock 	if (vd) {
44954451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
44964451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
44974451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
44984451Seschrock 		    SE_SLEEP) != 0)
44994451Seschrock 			goto done;
45004451Seschrock 
45014451Seschrock 		if (vd->vdev_path) {
45024451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
45034451Seschrock 			value.value.sv_string = vd->vdev_path;
45044451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
45054451Seschrock 			    &value, SE_SLEEP) != 0)
45064451Seschrock 				goto done;
45074451Seschrock 		}
45084451Seschrock 	}
45094451Seschrock 
45105756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
45115756Seschrock 		goto done;
45125756Seschrock 	attr = NULL;
45135756Seschrock 
45144451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
45154451Seschrock 
45164451Seschrock done:
45174451Seschrock 	if (attr)
45184451Seschrock 		sysevent_free_attr(attr);
45194451Seschrock 	sysevent_free(ev);
45204451Seschrock #endif
45214451Seschrock }
4522