xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 7042:46fc4b6db23e)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
235756Seschrock  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens /*
30789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
31789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
32789Sahrens  * pool.
33789Sahrens  */
34789Sahrens 
35789Sahrens #include <sys/zfs_context.h>
361544Seschrock #include <sys/fm/fs/zfs.h>
37789Sahrens #include <sys/spa_impl.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zio_checksum.h>
40789Sahrens #include <sys/zio_compress.h>
41789Sahrens #include <sys/dmu.h>
42789Sahrens #include <sys/dmu_tx.h>
43789Sahrens #include <sys/zap.h>
44789Sahrens #include <sys/zil.h>
45789Sahrens #include <sys/vdev_impl.h>
46789Sahrens #include <sys/metaslab.h>
47789Sahrens #include <sys/uberblock_impl.h>
48789Sahrens #include <sys/txg.h>
49789Sahrens #include <sys/avl.h>
50789Sahrens #include <sys/dmu_traverse.h>
513912Slling #include <sys/dmu_objset.h>
52789Sahrens #include <sys/unique.h>
53789Sahrens #include <sys/dsl_pool.h>
543912Slling #include <sys/dsl_dataset.h>
55789Sahrens #include <sys/dsl_dir.h>
56789Sahrens #include <sys/dsl_prop.h>
573912Slling #include <sys/dsl_synctask.h>
58789Sahrens #include <sys/fs/zfs.h>
595450Sbrendan #include <sys/arc.h>
60789Sahrens #include <sys/callb.h>
613975Sek110237 #include <sys/systeminfo.h>
623975Sek110237 #include <sys/sunddi.h>
636423Sgw25295 #include <sys/spa_boot.h>
64789Sahrens 
655094Slling #include "zfs_prop.h"
665913Sperrin #include "zfs_comutil.h"
675094Slling 
682986Sek110237 int zio_taskq_threads = 8;
692986Sek110237 
705094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
715094Slling 
725094Slling /*
735094Slling  * ==========================================================================
745094Slling  * SPA properties routines
755094Slling  * ==========================================================================
765094Slling  */
775094Slling 
785094Slling /*
795094Slling  * Add a (source=src, propname=propval) list to an nvlist.
805094Slling  */
815949Slling static void
825094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
835094Slling     uint64_t intval, zprop_source_t src)
845094Slling {
855094Slling 	const char *propname = zpool_prop_to_name(prop);
865094Slling 	nvlist_t *propval;
875949Slling 
885949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
895949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
905949Slling 
915949Slling 	if (strval != NULL)
925949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
935949Slling 	else
945949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
955949Slling 
965949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
975094Slling 	nvlist_free(propval);
985094Slling }
995094Slling 
1005094Slling /*
1015094Slling  * Get property values from the spa configuration.
1025094Slling  */
1035949Slling static void
1045094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1055094Slling {
1065094Slling 	uint64_t size = spa_get_space(spa);
1075094Slling 	uint64_t used = spa_get_alloc(spa);
1085094Slling 	uint64_t cap, version;
1095094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1106643Seschrock 	spa_config_dirent_t *dp;
1115094Slling 
1125094Slling 	/*
1135094Slling 	 * readonly properties
1145094Slling 	 */
1155949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src);
1165949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1175949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1185949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
1195094Slling 
1205094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
1215949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1225949Slling 
1235949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1245949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1255949Slling 	    spa->spa_root_vdev->vdev_state, src);
1265094Slling 
1275094Slling 	/*
1285094Slling 	 * settable properties that are not stored in the pool property object.
1295094Slling 	 */
1305094Slling 	version = spa_version(spa);
1315094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1325094Slling 		src = ZPROP_SRC_DEFAULT;
1335094Slling 	else
1345094Slling 		src = ZPROP_SRC_LOCAL;
1355949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1365949Slling 
1375949Slling 	if (spa->spa_root != NULL)
1385949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1395949Slling 		    0, ZPROP_SRC_LOCAL);
1405094Slling 
1416643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
1426643Seschrock 		if (dp->scd_path == NULL) {
1435949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1446643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
1456643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1465949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1476643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1485363Seschrock 		}
1495363Seschrock 	}
1505094Slling }
1515094Slling 
1525094Slling /*
1535094Slling  * Get zpool property values.
1545094Slling  */
1555094Slling int
1565094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
1575094Slling {
1585094Slling 	zap_cursor_t zc;
1595094Slling 	zap_attribute_t za;
1605094Slling 	objset_t *mos = spa->spa_meta_objset;
1615094Slling 	int err;
1625094Slling 
1635949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1645094Slling 
1655094Slling 	/*
1665094Slling 	 * Get properties from the spa config.
1675094Slling 	 */
1685949Slling 	spa_prop_get_config(spa, nvp);
1695094Slling 
1705094Slling 	mutex_enter(&spa->spa_props_lock);
1715094Slling 	/* If no pool property object, no more prop to get. */
1725094Slling 	if (spa->spa_pool_props_object == 0) {
1735094Slling 		mutex_exit(&spa->spa_props_lock);
1745094Slling 		return (0);
1755094Slling 	}
1765094Slling 
1775094Slling 	/*
1785094Slling 	 * Get properties from the MOS pool property object.
1795094Slling 	 */
1805094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
1815094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
1825094Slling 	    zap_cursor_advance(&zc)) {
1835094Slling 		uint64_t intval = 0;
1845094Slling 		char *strval = NULL;
1855094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
1865094Slling 		zpool_prop_t prop;
1875094Slling 
1885094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
1895094Slling 			continue;
1905094Slling 
1915094Slling 		switch (za.za_integer_length) {
1925094Slling 		case 8:
1935094Slling 			/* integer property */
1945094Slling 			if (za.za_first_integer !=
1955094Slling 			    zpool_prop_default_numeric(prop))
1965094Slling 				src = ZPROP_SRC_LOCAL;
1975094Slling 
1985094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
1995094Slling 				dsl_pool_t *dp;
2005094Slling 				dsl_dataset_t *ds = NULL;
2015094Slling 
2025094Slling 				dp = spa_get_dsl(spa);
2035094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2046689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2056689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2065094Slling 					rw_exit(&dp->dp_config_rwlock);
2075094Slling 					break;
2085094Slling 				}
2095094Slling 
2105094Slling 				strval = kmem_alloc(
2115094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2125094Slling 				    KM_SLEEP);
2135094Slling 				dsl_dataset_name(ds, strval);
2146689Smaybee 				dsl_dataset_rele(ds, FTAG);
2155094Slling 				rw_exit(&dp->dp_config_rwlock);
2165094Slling 			} else {
2175094Slling 				strval = NULL;
2185094Slling 				intval = za.za_first_integer;
2195094Slling 			}
2205094Slling 
2215949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2225094Slling 
2235094Slling 			if (strval != NULL)
2245094Slling 				kmem_free(strval,
2255094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2265094Slling 
2275094Slling 			break;
2285094Slling 
2295094Slling 		case 1:
2305094Slling 			/* string property */
2315094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2325094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
2335094Slling 			    za.za_name, 1, za.za_num_integers, strval);
2345094Slling 			if (err) {
2355094Slling 				kmem_free(strval, za.za_num_integers);
2365094Slling 				break;
2375094Slling 			}
2385949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
2395094Slling 			kmem_free(strval, za.za_num_integers);
2405094Slling 			break;
2415094Slling 
2425094Slling 		default:
2435094Slling 			break;
2445094Slling 		}
2455094Slling 	}
2465094Slling 	zap_cursor_fini(&zc);
2475094Slling 	mutex_exit(&spa->spa_props_lock);
2485094Slling out:
2495094Slling 	if (err && err != ENOENT) {
2505094Slling 		nvlist_free(*nvp);
2515949Slling 		*nvp = NULL;
2525094Slling 		return (err);
2535094Slling 	}
2545094Slling 
2555094Slling 	return (0);
2565094Slling }
2575094Slling 
2585094Slling /*
2595094Slling  * Validate the given pool properties nvlist and modify the list
2605094Slling  * for the property values to be set.
2615094Slling  */
2625094Slling static int
2635094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
2645094Slling {
2655094Slling 	nvpair_t *elem;
2665094Slling 	int error = 0, reset_bootfs = 0;
2675094Slling 	uint64_t objnum;
2685094Slling 
2695094Slling 	elem = NULL;
2705094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2715094Slling 		zpool_prop_t prop;
2725094Slling 		char *propname, *strval;
2735094Slling 		uint64_t intval;
2745094Slling 		objset_t *os;
2755363Seschrock 		char *slash;
2765094Slling 
2775094Slling 		propname = nvpair_name(elem);
2785094Slling 
2795094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
2805094Slling 			return (EINVAL);
2815094Slling 
2825094Slling 		switch (prop) {
2835094Slling 		case ZPOOL_PROP_VERSION:
2845094Slling 			error = nvpair_value_uint64(elem, &intval);
2855094Slling 			if (!error &&
2865094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
2875094Slling 				error = EINVAL;
2885094Slling 			break;
2895094Slling 
2905094Slling 		case ZPOOL_PROP_DELEGATION:
2915094Slling 		case ZPOOL_PROP_AUTOREPLACE:
2925094Slling 			error = nvpair_value_uint64(elem, &intval);
2935094Slling 			if (!error && intval > 1)
2945094Slling 				error = EINVAL;
2955094Slling 			break;
2965094Slling 
2975094Slling 		case ZPOOL_PROP_BOOTFS:
2985094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
2995094Slling 				error = ENOTSUP;
3005094Slling 				break;
3015094Slling 			}
3025094Slling 
3035094Slling 			/*
304*7042Sgw25295 			 * Make sure the vdev config is bootable
3055094Slling 			 */
306*7042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3075094Slling 				error = ENOTSUP;
3085094Slling 				break;
3095094Slling 			}
3105094Slling 
3115094Slling 			reset_bootfs = 1;
3125094Slling 
3135094Slling 			error = nvpair_value_string(elem, &strval);
3145094Slling 
3155094Slling 			if (!error) {
316*7042Sgw25295 				uint64_t compress;
317*7042Sgw25295 
3185094Slling 				if (strval == NULL || strval[0] == '\0') {
3195094Slling 					objnum = zpool_prop_default_numeric(
3205094Slling 					    ZPOOL_PROP_BOOTFS);
3215094Slling 					break;
3225094Slling 				}
3235094Slling 
3245094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
3256689Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
3265094Slling 					break;
327*7042Sgw25295 
328*7042Sgw25295 				/* We don't support gzip bootable datasets */
329*7042Sgw25295 				if ((error = dsl_prop_get_integer(strval,
330*7042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
331*7042Sgw25295 				    &compress, NULL)) == 0 &&
332*7042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
333*7042Sgw25295 					error = ENOTSUP;
334*7042Sgw25295 				} else {
335*7042Sgw25295 					objnum = dmu_objset_id(os);
336*7042Sgw25295 				}
3375094Slling 				dmu_objset_close(os);
3385094Slling 			}
3395094Slling 			break;
3405329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
3415329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
3425329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3435329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
3445329Sgw25295 				error = EINVAL;
3455329Sgw25295 
3465329Sgw25295 			/*
3475329Sgw25295 			 * This is a special case which only occurs when
3485329Sgw25295 			 * the pool has completely failed. This allows
3495329Sgw25295 			 * the user to change the in-core failmode property
3505329Sgw25295 			 * without syncing it out to disk (I/Os might
3515329Sgw25295 			 * currently be blocked). We do this by returning
3525329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
3535329Sgw25295 			 * into thinking we encountered a property validation
3545329Sgw25295 			 * error.
3555329Sgw25295 			 */
3565329Sgw25295 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3575329Sgw25295 				spa->spa_failmode = intval;
3585329Sgw25295 				error = EIO;
3595329Sgw25295 			}
3605329Sgw25295 			break;
3615363Seschrock 
3625363Seschrock 		case ZPOOL_PROP_CACHEFILE:
3635363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3645363Seschrock 				break;
3655363Seschrock 
3665363Seschrock 			if (strval[0] == '\0')
3675363Seschrock 				break;
3685363Seschrock 
3695363Seschrock 			if (strcmp(strval, "none") == 0)
3705363Seschrock 				break;
3715363Seschrock 
3725363Seschrock 			if (strval[0] != '/') {
3735363Seschrock 				error = EINVAL;
3745363Seschrock 				break;
3755363Seschrock 			}
3765363Seschrock 
3775363Seschrock 			slash = strrchr(strval, '/');
3785363Seschrock 			ASSERT(slash != NULL);
3795363Seschrock 
3805363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3815363Seschrock 			    strcmp(slash, "/..") == 0)
3825363Seschrock 				error = EINVAL;
3835363Seschrock 			break;
3845094Slling 		}
3855094Slling 
3865094Slling 		if (error)
3875094Slling 			break;
3885094Slling 	}
3895094Slling 
3905094Slling 	if (!error && reset_bootfs) {
3915094Slling 		error = nvlist_remove(props,
3925094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
3935094Slling 
3945094Slling 		if (!error) {
3955094Slling 			error = nvlist_add_uint64(props,
3965094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
3975094Slling 		}
3985094Slling 	}
3995094Slling 
4005094Slling 	return (error);
4015094Slling }
4025094Slling 
4035094Slling int
4045094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
4055094Slling {
4065094Slling 	int error;
4075094Slling 
4085094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
4095094Slling 		return (error);
4105094Slling 
4115094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
4125094Slling 	    spa, nvp, 3));
4135094Slling }
4145094Slling 
4155094Slling /*
4165094Slling  * If the bootfs property value is dsobj, clear it.
4175094Slling  */
4185094Slling void
4195094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
4205094Slling {
4215094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
4225094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
4235094Slling 		    spa->spa_pool_props_object,
4245094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
4255094Slling 		spa->spa_bootfs = 0;
4265094Slling 	}
4275094Slling }
4285094Slling 
429789Sahrens /*
430789Sahrens  * ==========================================================================
431789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
432789Sahrens  * ==========================================================================
433789Sahrens  */
434789Sahrens 
4351544Seschrock static int
4361544Seschrock spa_error_entry_compare(const void *a, const void *b)
4371544Seschrock {
4381544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4391544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4401544Seschrock 	int ret;
4411544Seschrock 
4421544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4431544Seschrock 	    sizeof (zbookmark_t));
4441544Seschrock 
4451544Seschrock 	if (ret < 0)
4461544Seschrock 		return (-1);
4471544Seschrock 	else if (ret > 0)
4481544Seschrock 		return (1);
4491544Seschrock 	else
4501544Seschrock 		return (0);
4511544Seschrock }
4521544Seschrock 
4531544Seschrock /*
4541544Seschrock  * Utility function which retrieves copies of the current logs and
4551544Seschrock  * re-initializes them in the process.
4561544Seschrock  */
4571544Seschrock void
4581544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4591544Seschrock {
4601544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4611544Seschrock 
4621544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4631544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4641544Seschrock 
4651544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4661544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4671544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4681544Seschrock 	avl_create(&spa->spa_errlist_last,
4691544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4701544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4711544Seschrock }
4721544Seschrock 
473789Sahrens /*
474789Sahrens  * Activate an uninitialized pool.
475789Sahrens  */
476789Sahrens static void
477789Sahrens spa_activate(spa_t *spa)
478789Sahrens {
479789Sahrens 	int t;
480789Sahrens 
481789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
482789Sahrens 
483789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
484789Sahrens 
485789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4864527Sperrin 	spa->spa_log_class = metaslab_class_create();
487789Sahrens 
488789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
489789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
4902986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
491789Sahrens 		    TASKQ_PREPOPULATE);
492789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
4932986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
494789Sahrens 		    TASKQ_PREPOPULATE);
495789Sahrens 	}
496789Sahrens 
497789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
498789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
4995329Sgw25295 	list_create(&spa->spa_zio_list, sizeof (zio_t),
5005329Sgw25295 	    offsetof(zio_t, zio_link_node));
501789Sahrens 
502789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
503789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
5041544Seschrock 
5051544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5061544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5071544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5081544Seschrock 	avl_create(&spa->spa_errlist_last,
5091544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5101544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
511789Sahrens }
512789Sahrens 
513789Sahrens /*
514789Sahrens  * Opposite of spa_activate().
515789Sahrens  */
516789Sahrens static void
517789Sahrens spa_deactivate(spa_t *spa)
518789Sahrens {
519789Sahrens 	int t;
520789Sahrens 
521789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
522789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
523789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
524789Sahrens 
525789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
526789Sahrens 
527789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
528789Sahrens 
529789Sahrens 	list_destroy(&spa->spa_dirty_list);
5305329Sgw25295 	list_destroy(&spa->spa_zio_list);
531789Sahrens 
532789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
533789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
534789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
535789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
536789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
537789Sahrens 	}
538789Sahrens 
539789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
540789Sahrens 	spa->spa_normal_class = NULL;
541789Sahrens 
5424527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5434527Sperrin 	spa->spa_log_class = NULL;
5444527Sperrin 
5451544Seschrock 	/*
5461544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5471544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5481544Seschrock 	 */
5491544Seschrock 	spa_errlog_drain(spa);
5501544Seschrock 
5511544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5521544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5531544Seschrock 
554789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
555789Sahrens }
556789Sahrens 
557789Sahrens /*
558789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
559789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
560789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
561789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
562789Sahrens  */
5632082Seschrock static int
5642082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5652082Seschrock     uint_t id, int atype)
566789Sahrens {
567789Sahrens 	nvlist_t **child;
568789Sahrens 	uint_t c, children;
5692082Seschrock 	int error;
5702082Seschrock 
5712082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5722082Seschrock 		return (error);
5732082Seschrock 
5742082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5752082Seschrock 		return (0);
576789Sahrens 
577789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
578789Sahrens 	    &child, &children) != 0) {
5792082Seschrock 		vdev_free(*vdp);
5802082Seschrock 		*vdp = NULL;
5812082Seschrock 		return (EINVAL);
582789Sahrens 	}
583789Sahrens 
584789Sahrens 	for (c = 0; c < children; c++) {
5852082Seschrock 		vdev_t *vd;
5862082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5872082Seschrock 		    atype)) != 0) {
5882082Seschrock 			vdev_free(*vdp);
5892082Seschrock 			*vdp = NULL;
5902082Seschrock 			return (error);
591789Sahrens 		}
592789Sahrens 	}
593789Sahrens 
5942082Seschrock 	ASSERT(*vdp != NULL);
5952082Seschrock 
5962082Seschrock 	return (0);
597789Sahrens }
598789Sahrens 
599789Sahrens /*
600789Sahrens  * Opposite of spa_load().
601789Sahrens  */
602789Sahrens static void
603789Sahrens spa_unload(spa_t *spa)
604789Sahrens {
6052082Seschrock 	int i;
6062082Seschrock 
607789Sahrens 	/*
6081544Seschrock 	 * Stop async tasks.
6091544Seschrock 	 */
6101544Seschrock 	spa_async_suspend(spa);
6111544Seschrock 
6121544Seschrock 	/*
613789Sahrens 	 * Stop syncing.
614789Sahrens 	 */
615789Sahrens 	if (spa->spa_sync_on) {
616789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
617789Sahrens 		spa->spa_sync_on = B_FALSE;
618789Sahrens 	}
619789Sahrens 
620789Sahrens 	/*
621789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
622789Sahrens 	 */
6231544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
6241544Seschrock 	spa_config_exit(spa, FTAG);
625789Sahrens 
626789Sahrens 	/*
6275450Sbrendan 	 * Drop and purge level 2 cache
6285450Sbrendan 	 */
6295450Sbrendan 	spa_l2cache_drop(spa);
6305450Sbrendan 
6315450Sbrendan 	/*
632789Sahrens 	 * Close the dsl pool.
633789Sahrens 	 */
634789Sahrens 	if (spa->spa_dsl_pool) {
635789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
636789Sahrens 		spa->spa_dsl_pool = NULL;
637789Sahrens 	}
638789Sahrens 
639789Sahrens 	/*
640789Sahrens 	 * Close all vdevs.
641789Sahrens 	 */
6421585Sbonwick 	if (spa->spa_root_vdev)
643789Sahrens 		vdev_free(spa->spa_root_vdev);
6441585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6451544Seschrock 
6465450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
6475450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
6485450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
6495450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
6505450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
6515450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
6525450Sbrendan 	}
6535450Sbrendan 	if (spa->spa_spares.sav_config) {
6545450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
6555450Sbrendan 		spa->spa_spares.sav_config = NULL;
6562082Seschrock 	}
6575450Sbrendan 
6585450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
6595450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
6605450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
6615450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
6625450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
6635450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
6645450Sbrendan 	}
6655450Sbrendan 	if (spa->spa_l2cache.sav_config) {
6665450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
6675450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
6682082Seschrock 	}
6692082Seschrock 
6701544Seschrock 	spa->spa_async_suspended = 0;
671789Sahrens }
672789Sahrens 
673789Sahrens /*
6742082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6752082Seschrock  * this pool.  When this is called, we have some form of basic information in
6765450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
6775450Sbrendan  * then re-generate a more complete list including status information.
6782082Seschrock  */
6792082Seschrock static void
6802082Seschrock spa_load_spares(spa_t *spa)
6812082Seschrock {
6822082Seschrock 	nvlist_t **spares;
6832082Seschrock 	uint_t nspares;
6842082Seschrock 	int i;
6853377Seschrock 	vdev_t *vd, *tvd;
6862082Seschrock 
6872082Seschrock 	/*
6882082Seschrock 	 * First, close and free any existing spare vdevs.
6892082Seschrock 	 */
6905450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
6915450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
6923377Seschrock 
6933377Seschrock 		/* Undo the call to spa_activate() below */
6946643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
6956643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
6963377Seschrock 			spa_spare_remove(tvd);
6973377Seschrock 		vdev_close(vd);
6983377Seschrock 		vdev_free(vd);
6992082Seschrock 	}
7003377Seschrock 
7015450Sbrendan 	if (spa->spa_spares.sav_vdevs)
7025450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
7035450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
7045450Sbrendan 
7055450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
7062082Seschrock 		nspares = 0;
7072082Seschrock 	else
7085450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
7092082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
7102082Seschrock 
7115450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
7125450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
7132082Seschrock 
7142082Seschrock 	if (nspares == 0)
7152082Seschrock 		return;
7162082Seschrock 
7172082Seschrock 	/*
7182082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
7193377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
7203377Seschrock 	 * structures associated with it: one in the list of spares (used only
7213377Seschrock 	 * for basic validation purposes) and one in the active vdev
7223377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
7233377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
7243377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
7252082Seschrock 	 */
7265450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
7275450Sbrendan 	    KM_SLEEP);
7285450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
7292082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
7302082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
7312082Seschrock 		ASSERT(vd != NULL);
7322082Seschrock 
7335450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
7342082Seschrock 
7356643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
7366643Seschrock 		    B_FALSE)) != NULL) {
7373377Seschrock 			if (!tvd->vdev_isspare)
7383377Seschrock 				spa_spare_add(tvd);
7393377Seschrock 
7403377Seschrock 			/*
7413377Seschrock 			 * We only mark the spare active if we were successfully
7423377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7433377Seschrock 			 * with a bad active spare would result in strange
7443377Seschrock 			 * behavior, because multiple pool would think the spare
7453377Seschrock 			 * is actively in use.
7463377Seschrock 			 *
7473377Seschrock 			 * There is a vulnerability here to an equally bizarre
7483377Seschrock 			 * circumstance, where a dead active spare is later
7493377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7503377Seschrock 			 * the rarity of this scenario, and the extra complexity
7513377Seschrock 			 * it adds, we ignore the possibility.
7523377Seschrock 			 */
7533377Seschrock 			if (!vdev_is_dead(tvd))
7543377Seschrock 				spa_spare_activate(tvd);
7553377Seschrock 		}
7563377Seschrock 
7572082Seschrock 		if (vdev_open(vd) != 0)
7582082Seschrock 			continue;
7592082Seschrock 
7602082Seschrock 		vd->vdev_top = vd;
7615450Sbrendan 		if (vdev_validate_aux(vd) == 0)
7625450Sbrendan 			spa_spare_add(vd);
7632082Seschrock 	}
7642082Seschrock 
7652082Seschrock 	/*
7662082Seschrock 	 * Recompute the stashed list of spares, with status information
7672082Seschrock 	 * this time.
7682082Seschrock 	 */
7695450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
7702082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7712082Seschrock 
7725450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
7735450Sbrendan 	    KM_SLEEP);
7745450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7755450Sbrendan 		spares[i] = vdev_config_generate(spa,
7765450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
7775450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
7785450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
7795450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
7802082Seschrock 		nvlist_free(spares[i]);
7815450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
7825450Sbrendan }
7835450Sbrendan 
7845450Sbrendan /*
7855450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
7865450Sbrendan  * this pool.  When this is called, we have some form of basic information in
7875450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
7885450Sbrendan  * then re-generate a more complete list including status information.
7895450Sbrendan  * Devices which are already active have their details maintained, and are
7905450Sbrendan  * not re-opened.
7915450Sbrendan  */
7925450Sbrendan static void
7935450Sbrendan spa_load_l2cache(spa_t *spa)
7945450Sbrendan {
7955450Sbrendan 	nvlist_t **l2cache;
7965450Sbrendan 	uint_t nl2cache;
7975450Sbrendan 	int i, j, oldnvdevs;
7986643Seschrock 	uint64_t guid, size;
7995450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
8005450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
8015450Sbrendan 
8025450Sbrendan 	if (sav->sav_config != NULL) {
8035450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
8045450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
8055450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
8065450Sbrendan 	} else {
8075450Sbrendan 		nl2cache = 0;
8085450Sbrendan 	}
8095450Sbrendan 
8105450Sbrendan 	oldvdevs = sav->sav_vdevs;
8115450Sbrendan 	oldnvdevs = sav->sav_count;
8125450Sbrendan 	sav->sav_vdevs = NULL;
8135450Sbrendan 	sav->sav_count = 0;
8145450Sbrendan 
8155450Sbrendan 	/*
8165450Sbrendan 	 * Process new nvlist of vdevs.
8175450Sbrendan 	 */
8185450Sbrendan 	for (i = 0; i < nl2cache; i++) {
8195450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
8205450Sbrendan 		    &guid) == 0);
8215450Sbrendan 
8225450Sbrendan 		newvdevs[i] = NULL;
8235450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
8245450Sbrendan 			vd = oldvdevs[j];
8255450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
8265450Sbrendan 				/*
8275450Sbrendan 				 * Retain previous vdev for add/remove ops.
8285450Sbrendan 				 */
8295450Sbrendan 				newvdevs[i] = vd;
8305450Sbrendan 				oldvdevs[j] = NULL;
8315450Sbrendan 				break;
8325450Sbrendan 			}
8335450Sbrendan 		}
8345450Sbrendan 
8355450Sbrendan 		if (newvdevs[i] == NULL) {
8365450Sbrendan 			/*
8375450Sbrendan 			 * Create new vdev
8385450Sbrendan 			 */
8395450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
8405450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
8415450Sbrendan 			ASSERT(vd != NULL);
8425450Sbrendan 			newvdevs[i] = vd;
8435450Sbrendan 
8445450Sbrendan 			/*
8455450Sbrendan 			 * Commit this vdev as an l2cache device,
8465450Sbrendan 			 * even if it fails to open.
8475450Sbrendan 			 */
8485450Sbrendan 			spa_l2cache_add(vd);
8495450Sbrendan 
8506643Seschrock 			vd->vdev_top = vd;
8516643Seschrock 			vd->vdev_aux = sav;
8526643Seschrock 
8536643Seschrock 			spa_l2cache_activate(vd);
8546643Seschrock 
8555450Sbrendan 			if (vdev_open(vd) != 0)
8565450Sbrendan 				continue;
8575450Sbrendan 
8585450Sbrendan 			(void) vdev_validate_aux(vd);
8595450Sbrendan 
8605450Sbrendan 			if (!vdev_is_dead(vd)) {
8615450Sbrendan 				size = vdev_get_rsize(vd);
8626643Seschrock 				l2arc_add_vdev(spa, vd,
8636643Seschrock 				    VDEV_LABEL_START_SIZE,
8646643Seschrock 				    size - VDEV_LABEL_START_SIZE);
8655450Sbrendan 			}
8665450Sbrendan 		}
8675450Sbrendan 	}
8685450Sbrendan 
8695450Sbrendan 	/*
8705450Sbrendan 	 * Purge vdevs that were dropped
8715450Sbrendan 	 */
8725450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
8735450Sbrendan 		uint64_t pool;
8745450Sbrendan 
8755450Sbrendan 		vd = oldvdevs[i];
8765450Sbrendan 		if (vd != NULL) {
8775450Sbrendan 			if (spa_mode & FWRITE &&
8785450Sbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
8796643Seschrock 			    pool != 0ULL &&
8806643Seschrock 			    l2arc_vdev_present(vd)) {
8815450Sbrendan 				l2arc_remove_vdev(vd);
8825450Sbrendan 			}
8835450Sbrendan 			(void) vdev_close(vd);
8845450Sbrendan 			spa_l2cache_remove(vd);
8855450Sbrendan 		}
8865450Sbrendan 	}
8875450Sbrendan 
8885450Sbrendan 	if (oldvdevs)
8895450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
8905450Sbrendan 
8915450Sbrendan 	if (sav->sav_config == NULL)
8925450Sbrendan 		goto out;
8935450Sbrendan 
8945450Sbrendan 	sav->sav_vdevs = newvdevs;
8955450Sbrendan 	sav->sav_count = (int)nl2cache;
8965450Sbrendan 
8975450Sbrendan 	/*
8985450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
8995450Sbrendan 	 * information this time.
9005450Sbrendan 	 */
9015450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
9025450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
9035450Sbrendan 
9045450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
9055450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9065450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
9075450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
9085450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
9095450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
9105450Sbrendan out:
9115450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
9125450Sbrendan 		nvlist_free(l2cache[i]);
9135450Sbrendan 	if (sav->sav_count)
9145450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
9152082Seschrock }
9162082Seschrock 
9172082Seschrock static int
9182082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
9192082Seschrock {
9202082Seschrock 	dmu_buf_t *db;
9212082Seschrock 	char *packed = NULL;
9222082Seschrock 	size_t nvsize = 0;
9232082Seschrock 	int error;
9242082Seschrock 	*value = NULL;
9252082Seschrock 
9262082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9272082Seschrock 	nvsize = *(uint64_t *)db->db_data;
9282082Seschrock 	dmu_buf_rele(db, FTAG);
9292082Seschrock 
9302082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
9312082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
9322082Seschrock 	if (error == 0)
9332082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
9342082Seschrock 	kmem_free(packed, nvsize);
9352082Seschrock 
9362082Seschrock 	return (error);
9372082Seschrock }
9382082Seschrock 
9392082Seschrock /*
9404451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9414451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9424451Seschrock  */
9434451Seschrock static void
9444451Seschrock spa_check_removed(vdev_t *vd)
9454451Seschrock {
9464451Seschrock 	int c;
9474451Seschrock 
9484451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9494451Seschrock 		spa_check_removed(vd->vdev_child[c]);
9504451Seschrock 
9514451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9524451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9534451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9544451Seschrock 	}
9554451Seschrock }
9564451Seschrock 
9574451Seschrock /*
958789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
9591544Seschrock  * source of configuration information.
960789Sahrens  */
961789Sahrens static int
9621544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
963789Sahrens {
964789Sahrens 	int error = 0;
965789Sahrens 	nvlist_t *nvroot = NULL;
966789Sahrens 	vdev_t *rvd;
967789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
9681635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
969789Sahrens 	uint64_t pool_guid;
9702082Seschrock 	uint64_t version;
971789Sahrens 	zio_t *zio;
9724451Seschrock 	uint64_t autoreplace = 0;
973789Sahrens 
9741544Seschrock 	spa->spa_load_state = state;
9751635Sbonwick 
976789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
9771733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
9781544Seschrock 		error = EINVAL;
9791544Seschrock 		goto out;
9801544Seschrock 	}
981789Sahrens 
9822082Seschrock 	/*
9832082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
9842082Seschrock 	 * it's not present treat it as the initial version.
9852082Seschrock 	 */
9862082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
9874577Sahrens 		version = SPA_VERSION_INITIAL;
9882082Seschrock 
9891733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
9901733Sbonwick 	    &spa->spa_config_txg);
9911733Sbonwick 
9921635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
9931544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
9941544Seschrock 		error = EEXIST;
9951544Seschrock 		goto out;
9961544Seschrock 	}
997789Sahrens 
9982174Seschrock 	spa->spa_load_guid = pool_guid;
9992174Seschrock 
1000789Sahrens 	/*
10012082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
10022082Seschrock 	 * value that will be returned by spa_version() since parsing the
10032082Seschrock 	 * configuration requires knowing the version number.
1004789Sahrens 	 */
10051544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
10062082Seschrock 	spa->spa_ubsync.ub_version = version;
10072082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
10081544Seschrock 	spa_config_exit(spa, FTAG);
1009789Sahrens 
10102082Seschrock 	if (error != 0)
10111544Seschrock 		goto out;
1012789Sahrens 
10131585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1014789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1015789Sahrens 
1016789Sahrens 	/*
1017789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1018789Sahrens 	 */
10194070Smc142369 	error = vdev_open(rvd);
10204070Smc142369 	if (error != 0)
10211544Seschrock 		goto out;
1022789Sahrens 
1023789Sahrens 	/*
10241986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
10251986Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
10261986Seschrock 	 * flag.
10271986Seschrock 	 */
10281986Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
10291986Seschrock 	error = vdev_validate(rvd);
10301986Seschrock 	spa_config_exit(spa, FTAG);
10311986Seschrock 
10324070Smc142369 	if (error != 0)
10331986Seschrock 		goto out;
10341986Seschrock 
10351986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
10361986Seschrock 		error = ENXIO;
10371986Seschrock 		goto out;
10381986Seschrock 	}
10391986Seschrock 
10401986Seschrock 	/*
1041789Sahrens 	 * Find the best uberblock.
1042789Sahrens 	 */
1043789Sahrens 	bzero(ub, sizeof (uberblock_t));
1044789Sahrens 
1045789Sahrens 	zio = zio_root(spa, NULL, NULL,
1046789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1047789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
1048789Sahrens 	error = zio_wait(zio);
1049789Sahrens 
1050789Sahrens 	/*
1051789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1052789Sahrens 	 */
1053789Sahrens 	if (ub->ub_txg == 0) {
10541760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10551760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10561544Seschrock 		error = ENXIO;
10571544Seschrock 		goto out;
10581544Seschrock 	}
10591544Seschrock 
10601544Seschrock 	/*
10611544Seschrock 	 * If the pool is newer than the code, we can't open it.
10621544Seschrock 	 */
10634577Sahrens 	if (ub->ub_version > SPA_VERSION) {
10641760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10651760Seschrock 		    VDEV_AUX_VERSION_NEWER);
10661544Seschrock 		error = ENOTSUP;
10671544Seschrock 		goto out;
1068789Sahrens 	}
1069789Sahrens 
1070789Sahrens 	/*
1071789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1072789Sahrens 	 * incomplete configuration.
1073789Sahrens 	 */
10741732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
10751544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10761544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
10771544Seschrock 		error = ENXIO;
10781544Seschrock 		goto out;
1079789Sahrens 	}
1080789Sahrens 
1081789Sahrens 	/*
1082789Sahrens 	 * Initialize internal SPA structures.
1083789Sahrens 	 */
1084789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1085789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1086789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
10871544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
10881544Seschrock 	if (error) {
10891544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10901544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10911544Seschrock 		goto out;
10921544Seschrock 	}
1093789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1094789Sahrens 
10951544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1096789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
10971544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
10981544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10991544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11001544Seschrock 		error = EIO;
11011544Seschrock 		goto out;
11021544Seschrock 	}
1103789Sahrens 
1104789Sahrens 	if (!mosconfig) {
11052082Seschrock 		nvlist_t *newconfig;
11063975Sek110237 		uint64_t hostid;
11072082Seschrock 
11082082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
11091544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11101544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
11111544Seschrock 			error = EIO;
11121544Seschrock 			goto out;
11131544Seschrock 		}
1114789Sahrens 
11153975Sek110237 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
11163975Sek110237 		    &hostid) == 0) {
11173975Sek110237 			char *hostname;
11183975Sek110237 			unsigned long myhostid = 0;
11193975Sek110237 
11203975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
11213975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
11223975Sek110237 
11233975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
11244178Slling 			if (hostid != 0 && myhostid != 0 &&
11254178Slling 			    (unsigned long)hostid != myhostid) {
11263975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
11273975Sek110237 				    "loaded as it was last accessed by "
11283975Sek110237 				    "another system (host: %s hostid: 0x%lx).  "
11293975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
11303975Sek110237 				    spa->spa_name, hostname,
11313975Sek110237 				    (unsigned long)hostid);
11323975Sek110237 				error = EBADF;
11333975Sek110237 				goto out;
11343975Sek110237 			}
11353975Sek110237 		}
11363975Sek110237 
1137789Sahrens 		spa_config_set(spa, newconfig);
1138789Sahrens 		spa_unload(spa);
1139789Sahrens 		spa_deactivate(spa);
1140789Sahrens 		spa_activate(spa);
1141789Sahrens 
11421544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
11431544Seschrock 	}
11441544Seschrock 
11451544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
11461544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
11471544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
11481544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11491544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11501544Seschrock 		error = EIO;
11511544Seschrock 		goto out;
1152789Sahrens 	}
1153789Sahrens 
11541544Seschrock 	/*
11552082Seschrock 	 * Load the bit that tells us to use the new accounting function
11562082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
11572082Seschrock 	 * be present.
11582082Seschrock 	 */
11592082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11602082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
11612082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
11622082Seschrock 	if (error != 0 && error != ENOENT) {
11632082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11642082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11652082Seschrock 		error = EIO;
11662082Seschrock 		goto out;
11672082Seschrock 	}
11682082Seschrock 
11692082Seschrock 	/*
11701544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
11711544Seschrock 	 * not be present.
11721544Seschrock 	 */
11731544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11741544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
11751544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
11761807Sbonwick 	if (error != 0 && error != ENOENT) {
11771544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11781544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11791544Seschrock 		error = EIO;
11801544Seschrock 		goto out;
11811544Seschrock 	}
11821544Seschrock 
11831544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
11841544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
11851544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
11861544Seschrock 	if (error != 0 && error != ENOENT) {
11871544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
11881544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
11891544Seschrock 		error = EIO;
11901544Seschrock 		goto out;
11911544Seschrock 	}
1192789Sahrens 
1193789Sahrens 	/*
11942926Sek110237 	 * Load the history object.  If we have an older pool, this
11952926Sek110237 	 * will not be present.
11962926Sek110237 	 */
11972926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
11982926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
11992926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
12002926Sek110237 	if (error != 0 && error != ENOENT) {
12012926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12022926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
12032926Sek110237 		error = EIO;
12042926Sek110237 		goto out;
12052926Sek110237 	}
12062926Sek110237 
12072926Sek110237 	/*
12082082Seschrock 	 * Load any hot spares for this pool.
12092082Seschrock 	 */
12102082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12115450Sbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
12122082Seschrock 	if (error != 0 && error != ENOENT) {
12132082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12142082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
12152082Seschrock 		error = EIO;
12162082Seschrock 		goto out;
12172082Seschrock 	}
12182082Seschrock 	if (error == 0) {
12194577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
12205450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
12215450Sbrendan 		    &spa->spa_spares.sav_config) != 0) {
12222082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12232082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
12242082Seschrock 			error = EIO;
12252082Seschrock 			goto out;
12262082Seschrock 		}
12272082Seschrock 
12282082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
12292082Seschrock 		spa_load_spares(spa);
12302082Seschrock 		spa_config_exit(spa, FTAG);
12312082Seschrock 	}
12322082Seschrock 
12335450Sbrendan 	/*
12345450Sbrendan 	 * Load any level 2 ARC devices for this pool.
12355450Sbrendan 	 */
12365450Sbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12375450Sbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
12385450Sbrendan 	    &spa->spa_l2cache.sav_object);
12395450Sbrendan 	if (error != 0 && error != ENOENT) {
12405450Sbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12415450Sbrendan 		    VDEV_AUX_CORRUPT_DATA);
12425450Sbrendan 		error = EIO;
12435450Sbrendan 		goto out;
12445450Sbrendan 	}
12455450Sbrendan 	if (error == 0) {
12465450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
12475450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
12485450Sbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
12495450Sbrendan 			vdev_set_state(rvd, B_TRUE,
12505450Sbrendan 			    VDEV_STATE_CANT_OPEN,
12515450Sbrendan 			    VDEV_AUX_CORRUPT_DATA);
12525450Sbrendan 			error = EIO;
12535450Sbrendan 			goto out;
12545450Sbrendan 		}
12555450Sbrendan 
12565450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
12575450Sbrendan 		spa_load_l2cache(spa);
12585450Sbrendan 		spa_config_exit(spa, FTAG);
12595450Sbrendan 	}
12605450Sbrendan 
12615094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
12624543Smarks 
12633912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
12643912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
12653912Slling 
12663912Slling 	if (error && error != ENOENT) {
12673912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
12683912Slling 		    VDEV_AUX_CORRUPT_DATA);
12693912Slling 		error = EIO;
12703912Slling 		goto out;
12713912Slling 	}
12723912Slling 
12733912Slling 	if (error == 0) {
12743912Slling 		(void) zap_lookup(spa->spa_meta_objset,
12753912Slling 		    spa->spa_pool_props_object,
12764451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
12773912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
12784451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
12794451Seschrock 		    spa->spa_pool_props_object,
12804451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
12814451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
12824543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
12834543Smarks 		    spa->spa_pool_props_object,
12844543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
12854543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
12865329Sgw25295 		(void) zap_lookup(spa->spa_meta_objset,
12875329Sgw25295 		    spa->spa_pool_props_object,
12885329Sgw25295 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
12895329Sgw25295 		    sizeof (uint64_t), 1, &spa->spa_failmode);
12903912Slling 	}
12913912Slling 
12922082Seschrock 	/*
12934451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
12944451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
12954451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
12964451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
12974451Seschrock 	 * over.
12984451Seschrock 	 */
12995756Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
13004451Seschrock 		spa_check_removed(spa->spa_root_vdev);
13014451Seschrock 
13024451Seschrock 	/*
13031986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1304789Sahrens 	 */
13051986Seschrock 	vdev_load(rvd);
1306789Sahrens 
1307789Sahrens 	/*
1308789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1309789Sahrens 	 */
13101544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1311789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
13121544Seschrock 	spa_config_exit(spa, FTAG);
1313789Sahrens 
1314789Sahrens 	/*
1315789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1316789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1317789Sahrens 	 */
13181544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
13191544Seschrock 		error = ENXIO;
13201544Seschrock 		goto out;
13211544Seschrock 	}
1322789Sahrens 
13231544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13241635Sbonwick 		dmu_tx_t *tx;
13251635Sbonwick 		int need_update = B_FALSE;
13261585Sbonwick 		int c;
13271601Sbonwick 
13281635Sbonwick 		/*
13291635Sbonwick 		 * Claim log blocks that haven't been committed yet.
13301635Sbonwick 		 * This must all happen in a single txg.
13311635Sbonwick 		 */
13321601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1333789Sahrens 		    spa_first_txg(spa));
13342417Sahrens 		(void) dmu_objset_find(spa->spa_name,
13352417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1336789Sahrens 		dmu_tx_commit(tx);
1337789Sahrens 
1338789Sahrens 		spa->spa_sync_on = B_TRUE;
1339789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1340789Sahrens 
1341789Sahrens 		/*
1342789Sahrens 		 * Wait for all claims to sync.
1343789Sahrens 		 */
1344789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
13451585Sbonwick 
13461585Sbonwick 		/*
13471635Sbonwick 		 * If the config cache is stale, or we have uninitialized
13481635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
13491585Sbonwick 		 */
13501635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
13511635Sbonwick 		    state == SPA_LOAD_IMPORT)
13521635Sbonwick 			need_update = B_TRUE;
13531635Sbonwick 
13541635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
13551635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
13561635Sbonwick 				need_update = B_TRUE;
13571585Sbonwick 
13581585Sbonwick 		/*
13591635Sbonwick 		 * Update the config cache asychronously in case we're the
13601635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
13611585Sbonwick 		 */
13621635Sbonwick 		if (need_update)
13631635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1364789Sahrens 	}
1365789Sahrens 
13661544Seschrock 	error = 0;
13671544Seschrock out:
13682082Seschrock 	if (error && error != EBADF)
13691544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
13701544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
13711544Seschrock 	spa->spa_ena = 0;
13721544Seschrock 
13731544Seschrock 	return (error);
1374789Sahrens }
1375789Sahrens 
1376789Sahrens /*
1377789Sahrens  * Pool Open/Import
1378789Sahrens  *
1379789Sahrens  * The import case is identical to an open except that the configuration is sent
1380789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1381789Sahrens  * case of an open, the pool configuration will exist in the
13824451Seschrock  * POOL_STATE_UNINITIALIZED state.
1383789Sahrens  *
1384789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1385789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1386789Sahrens  * ambiguous state.
1387789Sahrens  */
1388789Sahrens static int
1389789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1390789Sahrens {
1391789Sahrens 	spa_t *spa;
1392789Sahrens 	int error;
1393789Sahrens 	int loaded = B_FALSE;
1394789Sahrens 	int locked = B_FALSE;
1395789Sahrens 
1396789Sahrens 	*spapp = NULL;
1397789Sahrens 
1398789Sahrens 	/*
1399789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1400789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1401789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1402789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1403789Sahrens 	 */
1404789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1405789Sahrens 		mutex_enter(&spa_namespace_lock);
1406789Sahrens 		locked = B_TRUE;
1407789Sahrens 	}
1408789Sahrens 
1409789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1410789Sahrens 		if (locked)
1411789Sahrens 			mutex_exit(&spa_namespace_lock);
1412789Sahrens 		return (ENOENT);
1413789Sahrens 	}
1414789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1415789Sahrens 
1416789Sahrens 		spa_activate(spa);
1417789Sahrens 
14181635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1419789Sahrens 
1420789Sahrens 		if (error == EBADF) {
1421789Sahrens 			/*
14221986Seschrock 			 * If vdev_validate() returns failure (indicated by
14231986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
14241986Seschrock 			 * that the pool has been exported or destroyed.  If
14251986Seschrock 			 * this is the case, the config cache is out of sync and
14261986Seschrock 			 * we should remove the pool from the namespace.
1427789Sahrens 			 */
1428789Sahrens 			spa_unload(spa);
1429789Sahrens 			spa_deactivate(spa);
14306643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1431789Sahrens 			spa_remove(spa);
1432789Sahrens 			if (locked)
1433789Sahrens 				mutex_exit(&spa_namespace_lock);
1434789Sahrens 			return (ENOENT);
14351544Seschrock 		}
14361544Seschrock 
14371544Seschrock 		if (error) {
1438789Sahrens 			/*
1439789Sahrens 			 * We can't open the pool, but we still have useful
1440789Sahrens 			 * information: the state of each vdev after the
1441789Sahrens 			 * attempted vdev_open().  Return this to the user.
1442789Sahrens 			 */
14431635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
14441635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1445789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1446789Sahrens 				    B_TRUE);
14471635Sbonwick 				spa_config_exit(spa, FTAG);
14481635Sbonwick 			}
1449789Sahrens 			spa_unload(spa);
1450789Sahrens 			spa_deactivate(spa);
14511544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1452789Sahrens 			if (locked)
1453789Sahrens 				mutex_exit(&spa_namespace_lock);
1454789Sahrens 			*spapp = NULL;
1455789Sahrens 			return (error);
14561544Seschrock 		} else {
14571544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1458789Sahrens 		}
1459789Sahrens 
1460789Sahrens 		loaded = B_TRUE;
1461789Sahrens 	}
1462789Sahrens 
1463789Sahrens 	spa_open_ref(spa, tag);
14644451Seschrock 
14654451Seschrock 	/*
14664451Seschrock 	 * If we just loaded the pool, resilver anything that's out of date.
14674451Seschrock 	 */
14684451Seschrock 	if (loaded && (spa_mode & FWRITE))
14694451Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
14704451Seschrock 
1471789Sahrens 	if (locked)
1472789Sahrens 		mutex_exit(&spa_namespace_lock);
1473789Sahrens 
1474789Sahrens 	*spapp = spa;
1475789Sahrens 
1476789Sahrens 	if (config != NULL) {
14771544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1478789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
14791544Seschrock 		spa_config_exit(spa, FTAG);
1480789Sahrens 	}
1481789Sahrens 
1482789Sahrens 	return (0);
1483789Sahrens }
1484789Sahrens 
1485789Sahrens int
1486789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1487789Sahrens {
1488789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1489789Sahrens }
1490789Sahrens 
14911544Seschrock /*
14921544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
14931544Seschrock  * preventing it from being exported or destroyed.
14941544Seschrock  */
14951544Seschrock spa_t *
14961544Seschrock spa_inject_addref(char *name)
14971544Seschrock {
14981544Seschrock 	spa_t *spa;
14991544Seschrock 
15001544Seschrock 	mutex_enter(&spa_namespace_lock);
15011544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
15021544Seschrock 		mutex_exit(&spa_namespace_lock);
15031544Seschrock 		return (NULL);
15041544Seschrock 	}
15051544Seschrock 	spa->spa_inject_ref++;
15061544Seschrock 	mutex_exit(&spa_namespace_lock);
15071544Seschrock 
15081544Seschrock 	return (spa);
15091544Seschrock }
15101544Seschrock 
15111544Seschrock void
15121544Seschrock spa_inject_delref(spa_t *spa)
15131544Seschrock {
15141544Seschrock 	mutex_enter(&spa_namespace_lock);
15151544Seschrock 	spa->spa_inject_ref--;
15161544Seschrock 	mutex_exit(&spa_namespace_lock);
15171544Seschrock }
15181544Seschrock 
15195450Sbrendan /*
15205450Sbrendan  * Add spares device information to the nvlist.
15215450Sbrendan  */
15222082Seschrock static void
15232082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
15242082Seschrock {
15252082Seschrock 	nvlist_t **spares;
15262082Seschrock 	uint_t i, nspares;
15272082Seschrock 	nvlist_t *nvroot;
15282082Seschrock 	uint64_t guid;
15292082Seschrock 	vdev_stat_t *vs;
15302082Seschrock 	uint_t vsc;
15313377Seschrock 	uint64_t pool;
15322082Seschrock 
15335450Sbrendan 	if (spa->spa_spares.sav_count == 0)
15342082Seschrock 		return;
15352082Seschrock 
15362082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
15372082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15385450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
15392082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15402082Seschrock 	if (nspares != 0) {
15412082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
15422082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15432082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15442082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
15452082Seschrock 
15462082Seschrock 		/*
15472082Seschrock 		 * Go through and find any spares which have since been
15482082Seschrock 		 * repurposed as an active spare.  If this is the case, update
15492082Seschrock 		 * their status appropriately.
15502082Seschrock 		 */
15512082Seschrock 		for (i = 0; i < nspares; i++) {
15522082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
15532082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
15543377Seschrock 			if (spa_spare_exists(guid, &pool) && pool != 0ULL) {
15552082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
15562082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
15572082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
15582082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
15592082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
15602082Seschrock 			}
15612082Seschrock 		}
15622082Seschrock 	}
15632082Seschrock }
15642082Seschrock 
15655450Sbrendan /*
15665450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
15675450Sbrendan  */
15685450Sbrendan static void
15695450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
15705450Sbrendan {
15715450Sbrendan 	nvlist_t **l2cache;
15725450Sbrendan 	uint_t i, j, nl2cache;
15735450Sbrendan 	nvlist_t *nvroot;
15745450Sbrendan 	uint64_t guid;
15755450Sbrendan 	vdev_t *vd;
15765450Sbrendan 	vdev_stat_t *vs;
15775450Sbrendan 	uint_t vsc;
15785450Sbrendan 
15795450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
15805450Sbrendan 		return;
15815450Sbrendan 
15825450Sbrendan 	spa_config_enter(spa, RW_READER, FTAG);
15835450Sbrendan 
15845450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
15855450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
15865450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
15875450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
15885450Sbrendan 	if (nl2cache != 0) {
15895450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
15905450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
15915450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
15925450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
15935450Sbrendan 
15945450Sbrendan 		/*
15955450Sbrendan 		 * Update level 2 cache device stats.
15965450Sbrendan 		 */
15975450Sbrendan 
15985450Sbrendan 		for (i = 0; i < nl2cache; i++) {
15995450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
16005450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
16015450Sbrendan 
16025450Sbrendan 			vd = NULL;
16035450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
16045450Sbrendan 				if (guid ==
16055450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
16065450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
16075450Sbrendan 					break;
16085450Sbrendan 				}
16095450Sbrendan 			}
16105450Sbrendan 			ASSERT(vd != NULL);
16115450Sbrendan 
16125450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
16135450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
16145450Sbrendan 			vdev_get_stats(vd, vs);
16155450Sbrendan 		}
16165450Sbrendan 	}
16175450Sbrendan 
16185450Sbrendan 	spa_config_exit(spa, FTAG);
16195450Sbrendan }
16205450Sbrendan 
1621789Sahrens int
16221544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1623789Sahrens {
1624789Sahrens 	int error;
1625789Sahrens 	spa_t *spa;
1626789Sahrens 
1627789Sahrens 	*config = NULL;
1628789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1629789Sahrens 
16302082Seschrock 	if (spa && *config != NULL) {
16311544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
16321544Seschrock 		    spa_get_errlog_size(spa)) == 0);
16331544Seschrock 
16342082Seschrock 		spa_add_spares(spa, *config);
16355450Sbrendan 		spa_add_l2cache(spa, *config);
16362082Seschrock 	}
16372082Seschrock 
16381544Seschrock 	/*
16391544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
16401544Seschrock 	 * and call spa_lookup() directly.
16411544Seschrock 	 */
16421544Seschrock 	if (altroot) {
16431544Seschrock 		if (spa == NULL) {
16441544Seschrock 			mutex_enter(&spa_namespace_lock);
16451544Seschrock 			spa = spa_lookup(name);
16461544Seschrock 			if (spa)
16471544Seschrock 				spa_altroot(spa, altroot, buflen);
16481544Seschrock 			else
16491544Seschrock 				altroot[0] = '\0';
16501544Seschrock 			spa = NULL;
16511544Seschrock 			mutex_exit(&spa_namespace_lock);
16521544Seschrock 		} else {
16531544Seschrock 			spa_altroot(spa, altroot, buflen);
16541544Seschrock 		}
16551544Seschrock 	}
16561544Seschrock 
1657789Sahrens 	if (spa != NULL)
1658789Sahrens 		spa_close(spa, FTAG);
1659789Sahrens 
1660789Sahrens 	return (error);
1661789Sahrens }
1662789Sahrens 
1663789Sahrens /*
16645450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
16655450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
16665450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
16675450Sbrendan  * specified, as long as they are well-formed.
16682082Seschrock  */
16692082Seschrock static int
16705450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
16715450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
16725450Sbrendan     vdev_labeltype_t label)
16732082Seschrock {
16745450Sbrendan 	nvlist_t **dev;
16755450Sbrendan 	uint_t i, ndev;
16762082Seschrock 	vdev_t *vd;
16772082Seschrock 	int error;
16782082Seschrock 
16792082Seschrock 	/*
16805450Sbrendan 	 * It's acceptable to have no devs specified.
16812082Seschrock 	 */
16825450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
16832082Seschrock 		return (0);
16842082Seschrock 
16855450Sbrendan 	if (ndev == 0)
16862082Seschrock 		return (EINVAL);
16872082Seschrock 
16882082Seschrock 	/*
16895450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
16905450Sbrendan 	 * device type.
16912082Seschrock 	 */
16925450Sbrendan 	if (spa_version(spa) < version)
16932082Seschrock 		return (ENOTSUP);
16942082Seschrock 
16953377Seschrock 	/*
16965450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
16973377Seschrock 	 * checking.
16983377Seschrock 	 */
16995450Sbrendan 	sav->sav_pending = dev;
17005450Sbrendan 	sav->sav_npending = ndev;
17015450Sbrendan 
17025450Sbrendan 	for (i = 0; i < ndev; i++) {
17035450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
17042082Seschrock 		    mode)) != 0)
17053377Seschrock 			goto out;
17062082Seschrock 
17072082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
17082082Seschrock 			vdev_free(vd);
17093377Seschrock 			error = EINVAL;
17103377Seschrock 			goto out;
17112082Seschrock 		}
17122082Seschrock 
17135450Sbrendan 		/*
17145450Sbrendan 		 * The L2ARC currently only supports disk devices.
17155450Sbrendan 		 */
17165450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
17175450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
17185450Sbrendan 			error = ENOTBLK;
17195450Sbrendan 			goto out;
17205450Sbrendan 		}
17215450Sbrendan 
17222082Seschrock 		vd->vdev_top = vd;
17233377Seschrock 
17243377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
17255450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
17265450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
17273377Seschrock 			    vd->vdev_guid) == 0);
17282082Seschrock 		}
17292082Seschrock 
17302082Seschrock 		vdev_free(vd);
17313377Seschrock 
17325450Sbrendan 		if (error &&
17335450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
17343377Seschrock 			goto out;
17353377Seschrock 		else
17363377Seschrock 			error = 0;
17372082Seschrock 	}
17382082Seschrock 
17393377Seschrock out:
17405450Sbrendan 	sav->sav_pending = NULL;
17415450Sbrendan 	sav->sav_npending = 0;
17423377Seschrock 	return (error);
17432082Seschrock }
17442082Seschrock 
17455450Sbrendan static int
17465450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
17475450Sbrendan {
17485450Sbrendan 	int error;
17495450Sbrendan 
17505450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17515450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
17525450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
17535450Sbrendan 		return (error);
17545450Sbrendan 	}
17555450Sbrendan 
17565450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
17575450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
17585450Sbrendan 	    VDEV_LABEL_L2CACHE));
17595450Sbrendan }
17605450Sbrendan 
17615450Sbrendan static void
17625450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
17635450Sbrendan     const char *config)
17645450Sbrendan {
17655450Sbrendan 	int i;
17665450Sbrendan 
17675450Sbrendan 	if (sav->sav_config != NULL) {
17685450Sbrendan 		nvlist_t **olddevs;
17695450Sbrendan 		uint_t oldndevs;
17705450Sbrendan 		nvlist_t **newdevs;
17715450Sbrendan 
17725450Sbrendan 		/*
17735450Sbrendan 		 * Generate new dev list by concatentating with the
17745450Sbrendan 		 * current dev list.
17755450Sbrendan 		 */
17765450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
17775450Sbrendan 		    &olddevs, &oldndevs) == 0);
17785450Sbrendan 
17795450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
17805450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
17815450Sbrendan 		for (i = 0; i < oldndevs; i++)
17825450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
17835450Sbrendan 			    KM_SLEEP) == 0);
17845450Sbrendan 		for (i = 0; i < ndevs; i++)
17855450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
17865450Sbrendan 			    KM_SLEEP) == 0);
17875450Sbrendan 
17885450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
17895450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
17905450Sbrendan 
17915450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
17925450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
17935450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
17945450Sbrendan 			nvlist_free(newdevs[i]);
17955450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
17965450Sbrendan 	} else {
17975450Sbrendan 		/*
17985450Sbrendan 		 * Generate a new dev list.
17995450Sbrendan 		 */
18005450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
18015450Sbrendan 		    KM_SLEEP) == 0);
18025450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
18035450Sbrendan 		    devs, ndevs) == 0);
18045450Sbrendan 	}
18055450Sbrendan }
18065450Sbrendan 
18075450Sbrendan /*
18085450Sbrendan  * Stop and drop level 2 ARC devices
18095450Sbrendan  */
18105450Sbrendan void
18115450Sbrendan spa_l2cache_drop(spa_t *spa)
18125450Sbrendan {
18135450Sbrendan 	vdev_t *vd;
18145450Sbrendan 	int i;
18155450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
18165450Sbrendan 
18175450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
18185450Sbrendan 		uint64_t pool;
18195450Sbrendan 
18205450Sbrendan 		vd = sav->sav_vdevs[i];
18215450Sbrendan 		ASSERT(vd != NULL);
18225450Sbrendan 
18235450Sbrendan 		if (spa_mode & FWRITE &&
18246643Seschrock 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL &&
18256643Seschrock 		    l2arc_vdev_present(vd)) {
18265450Sbrendan 			l2arc_remove_vdev(vd);
18275450Sbrendan 		}
18285450Sbrendan 		if (vd->vdev_isl2cache)
18295450Sbrendan 			spa_l2cache_remove(vd);
18305450Sbrendan 		vdev_clear_stats(vd);
18315450Sbrendan 		(void) vdev_close(vd);
18325450Sbrendan 	}
18335450Sbrendan }
18345450Sbrendan 
18352082Seschrock /*
1836789Sahrens  * Pool Creation
1837789Sahrens  */
1838789Sahrens int
18395094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
18404715Sek110237     const char *history_str)
1841789Sahrens {
1842789Sahrens 	spa_t *spa;
18435094Slling 	char *altroot = NULL;
18441635Sbonwick 	vdev_t *rvd;
1845789Sahrens 	dsl_pool_t *dp;
1846789Sahrens 	dmu_tx_t *tx;
18472082Seschrock 	int c, error = 0;
1848789Sahrens 	uint64_t txg = TXG_INITIAL;
18495450Sbrendan 	nvlist_t **spares, **l2cache;
18505450Sbrendan 	uint_t nspares, nl2cache;
18515094Slling 	uint64_t version;
1852789Sahrens 
1853789Sahrens 	/*
1854789Sahrens 	 * If this pool already exists, return failure.
1855789Sahrens 	 */
1856789Sahrens 	mutex_enter(&spa_namespace_lock);
1857789Sahrens 	if (spa_lookup(pool) != NULL) {
1858789Sahrens 		mutex_exit(&spa_namespace_lock);
1859789Sahrens 		return (EEXIST);
1860789Sahrens 	}
1861789Sahrens 
1862789Sahrens 	/*
1863789Sahrens 	 * Allocate a new spa_t structure.
1864789Sahrens 	 */
18655094Slling 	(void) nvlist_lookup_string(props,
18665094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
18671635Sbonwick 	spa = spa_add(pool, altroot);
1868789Sahrens 	spa_activate(spa);
1869789Sahrens 
1870789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
18715094Slling 
18725094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
18735094Slling 		spa_unload(spa);
18745094Slling 		spa_deactivate(spa);
18755094Slling 		spa_remove(spa);
18766643Seschrock 		mutex_exit(&spa_namespace_lock);
18775094Slling 		return (error);
18785094Slling 	}
18795094Slling 
18805094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
18815094Slling 	    &version) != 0)
18825094Slling 		version = SPA_VERSION;
18835094Slling 	ASSERT(version <= SPA_VERSION);
18845094Slling 	spa->spa_uberblock.ub_version = version;
1885789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1886789Sahrens 
18871635Sbonwick 	/*
18881635Sbonwick 	 * Create the root vdev.
18891635Sbonwick 	 */
18901635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
18911635Sbonwick 
18922082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
18932082Seschrock 
18942082Seschrock 	ASSERT(error != 0 || rvd != NULL);
18952082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
18962082Seschrock 
18975913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
18981635Sbonwick 		error = EINVAL;
18992082Seschrock 
19002082Seschrock 	if (error == 0 &&
19012082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
19025450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
19032082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
19042082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
19052082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
19062082Seschrock 		vdev_config_dirty(rvd);
19071635Sbonwick 	}
19081635Sbonwick 
19091635Sbonwick 	spa_config_exit(spa, FTAG);
1910789Sahrens 
19112082Seschrock 	if (error != 0) {
1912789Sahrens 		spa_unload(spa);
1913789Sahrens 		spa_deactivate(spa);
1914789Sahrens 		spa_remove(spa);
1915789Sahrens 		mutex_exit(&spa_namespace_lock);
1916789Sahrens 		return (error);
1917789Sahrens 	}
1918789Sahrens 
19192082Seschrock 	/*
19202082Seschrock 	 * Get the list of spares, if specified.
19212082Seschrock 	 */
19222082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19232082Seschrock 	    &spares, &nspares) == 0) {
19245450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
19252082Seschrock 		    KM_SLEEP) == 0);
19265450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
19272082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
19282082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
19292082Seschrock 		spa_load_spares(spa);
19302082Seschrock 		spa_config_exit(spa, FTAG);
19315450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
19325450Sbrendan 	}
19335450Sbrendan 
19345450Sbrendan 	/*
19355450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
19365450Sbrendan 	 */
19375450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
19385450Sbrendan 	    &l2cache, &nl2cache) == 0) {
19395450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
19405450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
19415450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
19425450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
19435450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
19445450Sbrendan 		spa_load_l2cache(spa);
19455450Sbrendan 		spa_config_exit(spa, FTAG);
19465450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
19472082Seschrock 	}
19482082Seschrock 
1949789Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
1950789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1951789Sahrens 
1952789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1953789Sahrens 
1954789Sahrens 	/*
1955789Sahrens 	 * Create the pool config object.
1956789Sahrens 	 */
1957789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1958789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1959789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1960789Sahrens 
19611544Seschrock 	if (zap_add(spa->spa_meta_objset,
1962789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
19631544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
19641544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
19651544Seschrock 	}
1966789Sahrens 
19675094Slling 	/* Newly created pools with the right version are always deflated. */
19685094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
19695094Slling 		spa->spa_deflate = TRUE;
19705094Slling 		if (zap_add(spa->spa_meta_objset,
19715094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
19725094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
19735094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
19745094Slling 		}
19752082Seschrock 	}
19762082Seschrock 
1977789Sahrens 	/*
1978789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
1979789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
1980789Sahrens 	 * keeps changing.
1981789Sahrens 	 */
1982789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
1983789Sahrens 	    1 << 14, tx);
1984789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
1985789Sahrens 	    ZIO_COMPRESS_OFF, tx);
1986789Sahrens 
19871544Seschrock 	if (zap_add(spa->spa_meta_objset,
1988789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
19891544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
19901544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
19911544Seschrock 	}
1992789Sahrens 
19932926Sek110237 	/*
19942926Sek110237 	 * Create the pool's history object.
19952926Sek110237 	 */
19965094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
19975094Slling 		spa_history_create_obj(spa, tx);
19985094Slling 
19995094Slling 	/*
20005094Slling 	 * Set pool properties.
20015094Slling 	 */
20025094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
20035094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
20045329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
20055094Slling 	if (props)
20065094Slling 		spa_sync_props(spa, props, CRED(), tx);
20072926Sek110237 
2008789Sahrens 	dmu_tx_commit(tx);
2009789Sahrens 
2010789Sahrens 	spa->spa_sync_on = B_TRUE;
2011789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2012789Sahrens 
2013789Sahrens 	/*
2014789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2015789Sahrens 	 * bean counters are appropriately updated.
2016789Sahrens 	 */
2017789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2018789Sahrens 
20196643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2020789Sahrens 
20215094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
20224715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
20234715Sek110237 
2024789Sahrens 	mutex_exit(&spa_namespace_lock);
2025789Sahrens 
2026789Sahrens 	return (0);
2027789Sahrens }
2028789Sahrens 
2029789Sahrens /*
2030789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2031789Sahrens  * then call spa_load() to do the dirty work.
2032789Sahrens  */
20336423Sgw25295 static int
20346423Sgw25295 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
20356643Seschrock     boolean_t isroot, boolean_t allowfaulted)
2036789Sahrens {
2037789Sahrens 	spa_t *spa;
20385094Slling 	char *altroot = NULL;
20396643Seschrock 	int error, loaderr;
20402082Seschrock 	nvlist_t *nvroot;
20415450Sbrendan 	nvlist_t **spares, **l2cache;
20425450Sbrendan 	uint_t nspares, nl2cache;
20436423Sgw25295 	int mosconfig = isroot? B_FALSE : B_TRUE;
2044789Sahrens 
2045789Sahrens 	/*
2046789Sahrens 	 * If a pool with this name exists, return failure.
2047789Sahrens 	 */
2048789Sahrens 	mutex_enter(&spa_namespace_lock);
2049789Sahrens 	if (spa_lookup(pool) != NULL) {
2050789Sahrens 		mutex_exit(&spa_namespace_lock);
2051789Sahrens 		return (EEXIST);
2052789Sahrens 	}
2053789Sahrens 
2054789Sahrens 	/*
20551635Sbonwick 	 * Create and initialize the spa structure.
2056789Sahrens 	 */
20575094Slling 	(void) nvlist_lookup_string(props,
20585094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20591635Sbonwick 	spa = spa_add(pool, altroot);
2060789Sahrens 	spa_activate(spa);
2061789Sahrens 
20626643Seschrock 	if (allowfaulted)
20636643Seschrock 		spa->spa_import_faulted = B_TRUE;
20646673Seschrock 	spa->spa_is_root = isroot;
20656643Seschrock 
2066789Sahrens 	/*
20671635Sbonwick 	 * Pass off the heavy lifting to spa_load().
20681732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
20691732Sbonwick 	 * is actually the one to trust when doing an import.
20701601Sbonwick 	 */
20716643Seschrock 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, mosconfig);
2072789Sahrens 
20732082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
20742082Seschrock 	/*
20752082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
20762082Seschrock 	 * and conflicts with spa_has_spare().
20772082Seschrock 	 */
20786423Sgw25295 	if (!isroot && spa->spa_spares.sav_config) {
20795450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
20805450Sbrendan 		spa->spa_spares.sav_config = NULL;
20812082Seschrock 		spa_load_spares(spa);
20822082Seschrock 	}
20836423Sgw25295 	if (!isroot && spa->spa_l2cache.sav_config) {
20845450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
20855450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
20865450Sbrendan 		spa_load_l2cache(spa);
20875450Sbrendan 	}
20882082Seschrock 
20892082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
20902082Seschrock 	    &nvroot) == 0);
20915450Sbrendan 	if (error == 0)
20925450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
20935450Sbrendan 	if (error == 0)
20945450Sbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
20955450Sbrendan 		    VDEV_ALLOC_L2CACHE);
20962082Seschrock 	spa_config_exit(spa, FTAG);
20972082Seschrock 
20985094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
20996643Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
21006643Seschrock 			/*
21016643Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
21026643Seschrock 			 * set, then manually set the config as if the config
21036643Seschrock 			 * passed in was specified in the cache file.
21046643Seschrock 			 */
21056643Seschrock 			error = 0;
21066643Seschrock 			spa->spa_import_faulted = B_FALSE;
21076643Seschrock 			if (spa->spa_config == NULL) {
21086643Seschrock 				spa_config_enter(spa, RW_READER, FTAG);
21096643Seschrock 				spa->spa_config = spa_config_generate(spa,
21106643Seschrock 				    NULL, -1ULL, B_TRUE);
21116643Seschrock 				spa_config_exit(spa, FTAG);
21126643Seschrock 			}
21136643Seschrock 			spa_unload(spa);
21146643Seschrock 			spa_deactivate(spa);
21156643Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
21166643Seschrock 		} else {
21176643Seschrock 			spa_unload(spa);
21186643Seschrock 			spa_deactivate(spa);
21196643Seschrock 			spa_remove(spa);
21206643Seschrock 		}
2121789Sahrens 		mutex_exit(&spa_namespace_lock);
2122789Sahrens 		return (error);
2123789Sahrens 	}
2124789Sahrens 
21251635Sbonwick 	/*
21265450Sbrendan 	 * Override any spares and level 2 cache devices as specified by
21275450Sbrendan 	 * the user, as these may have correct device names/devids, etc.
21282082Seschrock 	 */
21292082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
21302082Seschrock 	    &spares, &nspares) == 0) {
21315450Sbrendan 		if (spa->spa_spares.sav_config)
21325450Sbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
21332082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
21342082Seschrock 		else
21355450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
21362082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21375450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
21382082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
21392082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
21402082Seschrock 		spa_load_spares(spa);
21412082Seschrock 		spa_config_exit(spa, FTAG);
21425450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
21435450Sbrendan 	}
21445450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
21455450Sbrendan 	    &l2cache, &nl2cache) == 0) {
21465450Sbrendan 		if (spa->spa_l2cache.sav_config)
21475450Sbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
21485450Sbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
21495450Sbrendan 		else
21505450Sbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
21515450Sbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
21525450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
21535450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
21545450Sbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
21555450Sbrendan 		spa_load_l2cache(spa);
21565450Sbrendan 		spa_config_exit(spa, FTAG);
21575450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
21582082Seschrock 	}
21592082Seschrock 
21606643Seschrock 	if (spa_mode & FWRITE) {
21616643Seschrock 		/*
21626643Seschrock 		 * Update the config cache to include the newly-imported pool.
21636643Seschrock 		 */
21646423Sgw25295 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
21651635Sbonwick 
21666643Seschrock 		/*
21676643Seschrock 		 * Resilver anything that's out of date.
21686643Seschrock 		 */
21696643Seschrock 		if (!isroot)
21706643Seschrock 			VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER,
21716643Seschrock 			    B_TRUE) == 0);
21726643Seschrock 	}
21736643Seschrock 
21746643Seschrock 	spa->spa_import_faulted = B_FALSE;
21754451Seschrock 	mutex_exit(&spa_namespace_lock);
21764451Seschrock 
2177789Sahrens 	return (0);
2178789Sahrens }
2179789Sahrens 
21806423Sgw25295 #ifdef _KERNEL
21816423Sgw25295 /*
21826423Sgw25295  * Build a "root" vdev for a top level vdev read in from a rootpool
21836423Sgw25295  * device label.
21846423Sgw25295  */
21856423Sgw25295 static void
21866423Sgw25295 spa_build_rootpool_config(nvlist_t *config)
21876423Sgw25295 {
21886423Sgw25295 	nvlist_t *nvtop, *nvroot;
21896423Sgw25295 	uint64_t pgid;
21906423Sgw25295 
21916423Sgw25295 	/*
21926423Sgw25295 	 * Add this top-level vdev to the child array.
21936423Sgw25295 	 */
21946423Sgw25295 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
21956423Sgw25295 	    == 0);
21966423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
21976423Sgw25295 	    == 0);
21986423Sgw25295 
21996423Sgw25295 	/*
22006423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
22016423Sgw25295 	 */
22026423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
22036423Sgw25295 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
22046423Sgw25295 	    == 0);
22056423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
22066423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
22076423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
22086423Sgw25295 	    &nvtop, 1) == 0);
22096423Sgw25295 
22106423Sgw25295 	/*
22116423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
22126423Sgw25295 	 * this pool's configuration (remove the old, add the new).
22136423Sgw25295 	 */
22146423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
22156423Sgw25295 	nvlist_free(nvroot);
22166423Sgw25295 }
22176423Sgw25295 
22186423Sgw25295 /*
22196423Sgw25295  * Get the root pool information from the root disk, then import the root pool
22206423Sgw25295  * during the system boot up time.
22216423Sgw25295  */
22226423Sgw25295 extern nvlist_t *vdev_disk_read_rootlabel(char *);
22236423Sgw25295 
22246423Sgw25295 void
22256423Sgw25295 spa_check_rootconf(char *devpath, char **bestdev, nvlist_t **bestconf,
22266423Sgw25295     uint64_t *besttxg)
22276423Sgw25295 {
22286423Sgw25295 	nvlist_t *config;
22296423Sgw25295 	uint64_t txg;
22306423Sgw25295 
22316423Sgw25295 	if ((config = vdev_disk_read_rootlabel(devpath)) == NULL)
22326423Sgw25295 		return;
22336423Sgw25295 
22346423Sgw25295 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
22356423Sgw25295 
22366423Sgw25295 	if (txg > *besttxg) {
22376423Sgw25295 		*besttxg = txg;
22386423Sgw25295 		if (*bestconf != NULL)
22396423Sgw25295 			nvlist_free(*bestconf);
22406423Sgw25295 		*bestconf = config;
22416423Sgw25295 		*bestdev = devpath;
22426423Sgw25295 	}
22436423Sgw25295 }
22446423Sgw25295 
22456423Sgw25295 boolean_t
22466423Sgw25295 spa_rootdev_validate(nvlist_t *nv)
22476423Sgw25295 {
22486423Sgw25295 	uint64_t ival;
22496423Sgw25295 
22506423Sgw25295 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
22516423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
22526423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, &ival) == 0 ||
22536423Sgw25295 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
22546423Sgw25295 		return (B_FALSE);
22556423Sgw25295 
22566423Sgw25295 	return (B_TRUE);
22576423Sgw25295 }
22586423Sgw25295 
22596423Sgw25295 /*
22606423Sgw25295  * Import a root pool.
22616423Sgw25295  *
22626423Sgw25295  * For x86. devpath_list will consist the physpath name of the vdev in a single
22636423Sgw25295  * disk root pool or a list of physnames for the vdevs in a mirrored rootpool.
22646423Sgw25295  * e.g.
22656423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a /pci@1f,o/ide@d/disk@2,0:a"
22666423Sgw25295  *
22676423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
22686423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
22696423Sgw25295  * e.g.
22706423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
22716423Sgw25295  */
22726423Sgw25295 int
22736423Sgw25295 spa_import_rootpool(char *devpath_list)
22746423Sgw25295 {
22756423Sgw25295 	nvlist_t *conf = NULL;
22766423Sgw25295 	char *dev = NULL;
22776423Sgw25295 	char *pname;
22786423Sgw25295 	int error;
22796423Sgw25295 
22806423Sgw25295 	/*
22816423Sgw25295 	 * Get the vdev pathname and configuation from the most
22826423Sgw25295 	 * recently updated vdev (highest txg).
22836423Sgw25295 	 */
22846423Sgw25295 	if (error = spa_get_rootconf(devpath_list, &dev, &conf))
22856423Sgw25295 		goto msg_out;
22866423Sgw25295 
22876423Sgw25295 	/*
22886423Sgw25295 	 * Add type "root" vdev to the config.
22896423Sgw25295 	 */
22906423Sgw25295 	spa_build_rootpool_config(conf);
22916423Sgw25295 
22926423Sgw25295 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
22936423Sgw25295 
22946673Seschrock 	/*
22956673Seschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
22966673Seschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
22976673Seschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
22986673Seschrock 	 * pool open, not import.
22996673Seschrock 	 */
23006673Seschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
23016423Sgw25295 	if (error == EEXIST)
23026423Sgw25295 		error = 0;
23036423Sgw25295 
23046423Sgw25295 	nvlist_free(conf);
23056423Sgw25295 	return (error);
23066423Sgw25295 
23076423Sgw25295 msg_out:
23086423Sgw25295 	cmn_err(CE_NOTE, "\n\n"
23096423Sgw25295 	    "  ***************************************************  \n"
23106423Sgw25295 	    "  *  This device is not bootable!                   *  \n"
23116423Sgw25295 	    "  *  It is either offlined or detached or faulted.  *  \n"
23126423Sgw25295 	    "  *  Please try to boot from a different device.    *  \n"
23136423Sgw25295 	    "  ***************************************************  \n\n");
23146423Sgw25295 
23156423Sgw25295 	return (error);
23166423Sgw25295 }
23176423Sgw25295 #endif
23186423Sgw25295 
23196423Sgw25295 /*
23206423Sgw25295  * Import a non-root pool into the system.
23216423Sgw25295  */
23226423Sgw25295 int
23236423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
23246423Sgw25295 {
23256643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
23266423Sgw25295 }
23276423Sgw25295 
23286643Seschrock int
23296643Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
23306643Seschrock {
23316643Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
23326643Seschrock }
23336643Seschrock 
23346643Seschrock 
2335789Sahrens /*
2336789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2337789Sahrens  * to get the vdev stats associated with the imported devices.
2338789Sahrens  */
2339789Sahrens #define	TRYIMPORT_NAME	"$import"
2340789Sahrens 
2341789Sahrens nvlist_t *
2342789Sahrens spa_tryimport(nvlist_t *tryconfig)
2343789Sahrens {
2344789Sahrens 	nvlist_t *config = NULL;
2345789Sahrens 	char *poolname;
2346789Sahrens 	spa_t *spa;
2347789Sahrens 	uint64_t state;
2348789Sahrens 
2349789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2350789Sahrens 		return (NULL);
2351789Sahrens 
2352789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2353789Sahrens 		return (NULL);
2354789Sahrens 
23551635Sbonwick 	/*
23561635Sbonwick 	 * Create and initialize the spa structure.
23571635Sbonwick 	 */
2358789Sahrens 	mutex_enter(&spa_namespace_lock);
23591635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2360789Sahrens 	spa_activate(spa);
2361789Sahrens 
2362789Sahrens 	/*
23631635Sbonwick 	 * Pass off the heavy lifting to spa_load().
23641732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
23651732Sbonwick 	 * is actually the one to trust when doing an import.
2366789Sahrens 	 */
23671732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2368789Sahrens 
2369789Sahrens 	/*
2370789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2371789Sahrens 	 */
2372789Sahrens 	if (spa->spa_root_vdev != NULL) {
23731635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
2374789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
23751635Sbonwick 		spa_config_exit(spa, FTAG);
2376789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2377789Sahrens 		    poolname) == 0);
2378789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2379789Sahrens 		    state) == 0);
23803975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
23813975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
23822082Seschrock 
23832082Seschrock 		/*
23846423Sgw25295 		 * If the bootfs property exists on this pool then we
23856423Sgw25295 		 * copy it out so that external consumers can tell which
23866423Sgw25295 		 * pools are bootable.
23876423Sgw25295 		 */
23886423Sgw25295 		if (spa->spa_bootfs) {
23896423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
23906423Sgw25295 
23916423Sgw25295 			/*
23926423Sgw25295 			 * We have to play games with the name since the
23936423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
23946423Sgw25295 			 */
23956423Sgw25295 			if (dsl_dsobj_to_dsname(spa->spa_name,
23966423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
23976423Sgw25295 				char *cp;
23986423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
23996423Sgw25295 
24006423Sgw25295 				cp = strchr(tmpname, '/');
24016423Sgw25295 				if (cp == NULL) {
24026423Sgw25295 					(void) strlcpy(dsname, tmpname,
24036423Sgw25295 					    MAXPATHLEN);
24046423Sgw25295 				} else {
24056423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
24066423Sgw25295 					    "%s/%s", poolname, ++cp);
24076423Sgw25295 				}
24086423Sgw25295 				VERIFY(nvlist_add_string(config,
24096423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
24106423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
24116423Sgw25295 			}
24126423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
24136423Sgw25295 		}
24146423Sgw25295 
24156423Sgw25295 		/*
24165450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
24172082Seschrock 		 */
24182082Seschrock 		spa_add_spares(spa, config);
24195450Sbrendan 		spa_add_l2cache(spa, config);
2420789Sahrens 	}
2421789Sahrens 
2422789Sahrens 	spa_unload(spa);
2423789Sahrens 	spa_deactivate(spa);
2424789Sahrens 	spa_remove(spa);
2425789Sahrens 	mutex_exit(&spa_namespace_lock);
2426789Sahrens 
2427789Sahrens 	return (config);
2428789Sahrens }
2429789Sahrens 
2430789Sahrens /*
2431789Sahrens  * Pool export/destroy
2432789Sahrens  *
2433789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2434789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2435789Sahrens  * update the pool state and sync all the labels to disk, removing the
2436789Sahrens  * configuration from the cache afterwards.
2437789Sahrens  */
2438789Sahrens static int
24391775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig)
2440789Sahrens {
2441789Sahrens 	spa_t *spa;
2442789Sahrens 
24431775Sbillm 	if (oldconfig)
24441775Sbillm 		*oldconfig = NULL;
24451775Sbillm 
2446789Sahrens 	if (!(spa_mode & FWRITE))
2447789Sahrens 		return (EROFS);
2448789Sahrens 
2449789Sahrens 	mutex_enter(&spa_namespace_lock);
2450789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2451789Sahrens 		mutex_exit(&spa_namespace_lock);
2452789Sahrens 		return (ENOENT);
2453789Sahrens 	}
2454789Sahrens 
2455789Sahrens 	/*
24561544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
24571544Seschrock 	 * reacquire the namespace lock, and see if we can export.
24581544Seschrock 	 */
24591544Seschrock 	spa_open_ref(spa, FTAG);
24601544Seschrock 	mutex_exit(&spa_namespace_lock);
24611544Seschrock 	spa_async_suspend(spa);
24621544Seschrock 	mutex_enter(&spa_namespace_lock);
24631544Seschrock 	spa_close(spa, FTAG);
24641544Seschrock 
24651544Seschrock 	/*
2466789Sahrens 	 * The pool will be in core if it's openable,
2467789Sahrens 	 * in which case we can modify its state.
2468789Sahrens 	 */
2469789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2470789Sahrens 		/*
2471789Sahrens 		 * Objsets may be open only because they're dirty, so we
2472789Sahrens 		 * have to force it to sync before checking spa_refcnt.
2473789Sahrens 		 */
2474789Sahrens 		spa_scrub_suspend(spa);
2475789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2476789Sahrens 
24771544Seschrock 		/*
24781544Seschrock 		 * A pool cannot be exported or destroyed if there are active
24791544Seschrock 		 * references.  If we are resetting a pool, allow references by
24801544Seschrock 		 * fault injection handlers.
24811544Seschrock 		 */
24821544Seschrock 		if (!spa_refcount_zero(spa) ||
24831544Seschrock 		    (spa->spa_inject_ref != 0 &&
24841544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
2485789Sahrens 			spa_scrub_resume(spa);
24861544Seschrock 			spa_async_resume(spa);
2487789Sahrens 			mutex_exit(&spa_namespace_lock);
2488789Sahrens 			return (EBUSY);
2489789Sahrens 		}
2490789Sahrens 
2491789Sahrens 		spa_scrub_resume(spa);
2492789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
2493789Sahrens 
2494789Sahrens 		/*
2495789Sahrens 		 * We want this to be reflected on every label,
2496789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2497789Sahrens 		 * final sync that pushes these changes out.
2498789Sahrens 		 */
24991544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
25001601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
25011544Seschrock 			spa->spa_state = new_state;
25021635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
25031544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
25041601Sbonwick 			spa_config_exit(spa, FTAG);
25051544Seschrock 		}
2506789Sahrens 	}
2507789Sahrens 
25084451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
25094451Seschrock 
2510789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2511789Sahrens 		spa_unload(spa);
2512789Sahrens 		spa_deactivate(spa);
2513789Sahrens 	}
2514789Sahrens 
25151775Sbillm 	if (oldconfig && spa->spa_config)
25161775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
25171775Sbillm 
25181544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
25196643Seschrock 		spa_config_sync(spa, B_TRUE, B_TRUE);
25201544Seschrock 		spa_remove(spa);
25211544Seschrock 	}
2522789Sahrens 	mutex_exit(&spa_namespace_lock);
2523789Sahrens 
2524789Sahrens 	return (0);
2525789Sahrens }
2526789Sahrens 
2527789Sahrens /*
2528789Sahrens  * Destroy a storage pool.
2529789Sahrens  */
2530789Sahrens int
2531789Sahrens spa_destroy(char *pool)
2532789Sahrens {
25331775Sbillm 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL));
2534789Sahrens }
2535789Sahrens 
2536789Sahrens /*
2537789Sahrens  * Export a storage pool.
2538789Sahrens  */
2539789Sahrens int
25401775Sbillm spa_export(char *pool, nvlist_t **oldconfig)
2541789Sahrens {
25421775Sbillm 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig));
2543789Sahrens }
2544789Sahrens 
2545789Sahrens /*
25461544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
25471544Seschrock  * from the namespace in any way.
25481544Seschrock  */
25491544Seschrock int
25501544Seschrock spa_reset(char *pool)
25511544Seschrock {
25521775Sbillm 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL));
25531544Seschrock }
25541544Seschrock 
25551544Seschrock 
25561544Seschrock /*
2557789Sahrens  * ==========================================================================
2558789Sahrens  * Device manipulation
2559789Sahrens  * ==========================================================================
2560789Sahrens  */
2561789Sahrens 
2562789Sahrens /*
25634527Sperrin  * Add a device to a storage pool.
2564789Sahrens  */
2565789Sahrens int
2566789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2567789Sahrens {
2568789Sahrens 	uint64_t txg;
25691635Sbonwick 	int c, error;
2570789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
25711585Sbonwick 	vdev_t *vd, *tvd;
25725450Sbrendan 	nvlist_t **spares, **l2cache;
25735450Sbrendan 	uint_t nspares, nl2cache;
2574789Sahrens 
2575789Sahrens 	txg = spa_vdev_enter(spa);
2576789Sahrens 
25772082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
25782082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
25792082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
25802082Seschrock 
25813377Seschrock 	spa->spa_pending_vdev = vd;
2582789Sahrens 
25835450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
25845450Sbrendan 	    &nspares) != 0)
25852082Seschrock 		nspares = 0;
25862082Seschrock 
25875450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
25885450Sbrendan 	    &nl2cache) != 0)
25895450Sbrendan 		nl2cache = 0;
25905450Sbrendan 
25915450Sbrendan 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) {
25923377Seschrock 		spa->spa_pending_vdev = NULL;
25932082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
25943377Seschrock 	}
25952082Seschrock 
25962082Seschrock 	if (vd->vdev_children != 0) {
25973377Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
25983377Seschrock 			spa->spa_pending_vdev = NULL;
25992082Seschrock 			return (spa_vdev_exit(spa, vd, txg, error));
26002082Seschrock 		}
26012082Seschrock 	}
26022082Seschrock 
26033377Seschrock 	/*
26045450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
26055450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
26063377Seschrock 	 */
26075450Sbrendan 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) {
26083377Seschrock 		spa->spa_pending_vdev = NULL;
26093377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
26103377Seschrock 	}
26113377Seschrock 
26123377Seschrock 	spa->spa_pending_vdev = NULL;
26133377Seschrock 
26143377Seschrock 	/*
26153377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
26163377Seschrock 	 */
26173377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
26183377Seschrock 		tvd = vd->vdev_child[c];
26193377Seschrock 		vdev_remove_child(vd, tvd);
26203377Seschrock 		tvd->vdev_id = rvd->vdev_children;
26213377Seschrock 		vdev_add_child(rvd, tvd);
26223377Seschrock 		vdev_config_dirty(tvd);
26233377Seschrock 	}
26243377Seschrock 
26252082Seschrock 	if (nspares != 0) {
26265450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
26275450Sbrendan 		    ZPOOL_CONFIG_SPARES);
26282082Seschrock 		spa_load_spares(spa);
26295450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
26305450Sbrendan 	}
26315450Sbrendan 
26325450Sbrendan 	if (nl2cache != 0) {
26335450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
26345450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
26355450Sbrendan 		spa_load_l2cache(spa);
26365450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2637789Sahrens 	}
2638789Sahrens 
2639789Sahrens 	/*
26401585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
26411585Sbonwick 	 * If other threads start allocating from these vdevs before we
26421585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
26431585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
26441585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
26451585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
26461635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
26471585Sbonwick 	 *
26481585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
26491585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
26501585Sbonwick 	 * steps will be completed the next time we load the pool.
2651789Sahrens 	 */
26521635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
26531585Sbonwick 
26541635Sbonwick 	mutex_enter(&spa_namespace_lock);
26551635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
26561635Sbonwick 	mutex_exit(&spa_namespace_lock);
2657789Sahrens 
26581635Sbonwick 	return (0);
2659789Sahrens }
2660789Sahrens 
2661789Sahrens /*
2662789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2663789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2664789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2665789Sahrens  *
2666789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2667789Sahrens  * existing device; in this case the two devices are made into their own
26684451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2669789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2670789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2671789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2672789Sahrens  * is automatically detached.
2673789Sahrens  */
2674789Sahrens int
26751544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2676789Sahrens {
2677789Sahrens 	uint64_t txg, open_txg;
2678789Sahrens 	int error;
2679789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2680789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
26812082Seschrock 	vdev_ops_t *pvops;
26824527Sperrin 	int is_log;
2683789Sahrens 
2684789Sahrens 	txg = spa_vdev_enter(spa);
2685789Sahrens 
26866643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2687789Sahrens 
2688789Sahrens 	if (oldvd == NULL)
2689789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2690789Sahrens 
26911585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
26921585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
26931585Sbonwick 
2694789Sahrens 	pvd = oldvd->vdev_parent;
2695789Sahrens 
26962082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
26974451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
26984451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
26994451Seschrock 
27004451Seschrock 	if (newrootvd->vdev_children != 1)
2701789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2702789Sahrens 
2703789Sahrens 	newvd = newrootvd->vdev_child[0];
2704789Sahrens 
2705789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2706789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2707789Sahrens 
27082082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2709789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2710789Sahrens 
27114527Sperrin 	/*
27124527Sperrin 	 * Spares can't replace logs
27134527Sperrin 	 */
27144527Sperrin 	is_log = oldvd->vdev_islog;
27154527Sperrin 	if (is_log && newvd->vdev_isspare)
27164527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27174527Sperrin 
27182082Seschrock 	if (!replacing) {
27192082Seschrock 		/*
27202082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
27212082Seschrock 		 * vdev.
27222082Seschrock 		 */
27232082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
27242082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
27252082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27262082Seschrock 
27272082Seschrock 		pvops = &vdev_mirror_ops;
27282082Seschrock 	} else {
27292082Seschrock 		/*
27302082Seschrock 		 * Active hot spares can only be replaced by inactive hot
27312082Seschrock 		 * spares.
27322082Seschrock 		 */
27332082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
27342082Seschrock 		    pvd->vdev_child[1] == oldvd &&
27352082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
27362082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27372082Seschrock 
27382082Seschrock 		/*
27392082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
27402082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
27413377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
27423377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
27433377Seschrock 		 * the same (spare replaces spare, non-spare replaces
27443377Seschrock 		 * non-spare).
27452082Seschrock 		 */
27462082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
27472082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27483377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
27493377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
27503377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
27512082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
27522082Seschrock 		    newvd->vdev_isspare)
27532082Seschrock 			pvops = &vdev_spare_ops;
27542082Seschrock 		else
27552082Seschrock 			pvops = &vdev_replacing_ops;
27562082Seschrock 	}
27572082Seschrock 
27581175Slling 	/*
27591175Slling 	 * Compare the new device size with the replaceable/attachable
27601175Slling 	 * device size.
27611175Slling 	 */
27621175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2763789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2764789Sahrens 
27651732Sbonwick 	/*
27661732Sbonwick 	 * The new device cannot have a higher alignment requirement
27671732Sbonwick 	 * than the top-level vdev.
27681732Sbonwick 	 */
27691732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2770789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2771789Sahrens 
2772789Sahrens 	/*
2773789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2774789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2775789Sahrens 	 */
2776789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2777789Sahrens 		spa_strfree(oldvd->vdev_path);
2778789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2779789Sahrens 		    KM_SLEEP);
2780789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2781789Sahrens 		    newvd->vdev_path, "old");
2782789Sahrens 		if (oldvd->vdev_devid != NULL) {
2783789Sahrens 			spa_strfree(oldvd->vdev_devid);
2784789Sahrens 			oldvd->vdev_devid = NULL;
2785789Sahrens 		}
2786789Sahrens 	}
2787789Sahrens 
2788789Sahrens 	/*
27892082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
27902082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2791789Sahrens 	 */
2792789Sahrens 	if (pvd->vdev_ops != pvops)
2793789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2794789Sahrens 
2795789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2796789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2797789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2798789Sahrens 
2799789Sahrens 	/*
2800789Sahrens 	 * Extract the new device from its root and add it to pvd.
2801789Sahrens 	 */
2802789Sahrens 	vdev_remove_child(newrootvd, newvd);
2803789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2804789Sahrens 	vdev_add_child(pvd, newvd);
2805789Sahrens 
28061544Seschrock 	/*
28071544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
28081544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
28091544Seschrock 	 */
28101544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
28111544Seschrock 
2812789Sahrens 	tvd = newvd->vdev_top;
2813789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2814789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2815789Sahrens 
2816789Sahrens 	vdev_config_dirty(tvd);
2817789Sahrens 
2818789Sahrens 	/*
2819789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2820789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2821789Sahrens 	 */
2822789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2823789Sahrens 
2824789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2825789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2826789Sahrens 	    open_txg - TXG_INITIAL + 1);
2827789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2828789Sahrens 
28293377Seschrock 	if (newvd->vdev_isspare)
28303377Seschrock 		spa_spare_activate(newvd);
28311544Seschrock 
2832789Sahrens 	/*
2833789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2834789Sahrens 	 */
28351732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2836789Sahrens 
2837789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2838789Sahrens 
2839789Sahrens 	/*
28404451Seschrock 	 * Kick off a resilver to update newvd.  We need to grab the namespace
28414451Seschrock 	 * lock because spa_scrub() needs to post a sysevent with the pool name.
2842789Sahrens 	 */
28434451Seschrock 	mutex_enter(&spa_namespace_lock);
2844789Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
28454451Seschrock 	mutex_exit(&spa_namespace_lock);
2846789Sahrens 
2847789Sahrens 	return (0);
2848789Sahrens }
2849789Sahrens 
2850789Sahrens /*
2851789Sahrens  * Detach a device from a mirror or replacing vdev.
2852789Sahrens  * If 'replace_done' is specified, only detach if the parent
2853789Sahrens  * is a replacing vdev.
2854789Sahrens  */
2855789Sahrens int
28561544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2857789Sahrens {
2858789Sahrens 	uint64_t txg;
2859789Sahrens 	int c, t, error;
2860789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2861789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
28622082Seschrock 	boolean_t unspare = B_FALSE;
28632082Seschrock 	uint64_t unspare_guid;
28646673Seschrock 	size_t len;
2865789Sahrens 
2866789Sahrens 	txg = spa_vdev_enter(spa);
2867789Sahrens 
28686643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
2869789Sahrens 
2870789Sahrens 	if (vd == NULL)
2871789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2872789Sahrens 
28731585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
28741585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28751585Sbonwick 
2876789Sahrens 	pvd = vd->vdev_parent;
2877789Sahrens 
2878789Sahrens 	/*
2879789Sahrens 	 * If replace_done is specified, only remove this device if it's
28802082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
28812082Seschrock 	 * disk can be removed.
2882789Sahrens 	 */
28832082Seschrock 	if (replace_done) {
28842082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
28852082Seschrock 			if (vd->vdev_id != 0)
28862082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28872082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
28882082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28892082Seschrock 		}
28902082Seschrock 	}
28912082Seschrock 
28922082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
28934577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
2894789Sahrens 
2895789Sahrens 	/*
28962082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
2897789Sahrens 	 */
2898789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
28992082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
29002082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
2901789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2902789Sahrens 
2903789Sahrens 	/*
2904789Sahrens 	 * If there's only one replica, you can't detach it.
2905789Sahrens 	 */
2906789Sahrens 	if (pvd->vdev_children <= 1)
2907789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2908789Sahrens 
2909789Sahrens 	/*
2910789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
2911789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
2912789Sahrens 	 *
2913789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
2914789Sahrens 	 * precise DTL check.
2915789Sahrens 	 */
2916789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
2917789Sahrens 		uint64_t dirty;
2918789Sahrens 
2919789Sahrens 		cvd = pvd->vdev_child[c];
2920789Sahrens 		if (cvd == vd)
2921789Sahrens 			continue;
2922789Sahrens 		if (vdev_is_dead(cvd))
2923789Sahrens 			continue;
2924789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
2925789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
2926789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
2927789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
2928789Sahrens 		if (!dirty)
2929789Sahrens 			break;
2930789Sahrens 	}
29312082Seschrock 
29322082Seschrock 	/*
29332082Seschrock 	 * If we are a replacing or spare vdev, then we can always detach the
29342082Seschrock 	 * latter child, as that is how one cancels the operation.
29352082Seschrock 	 */
29362082Seschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
29372082Seschrock 	    c == pvd->vdev_children)
2938789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2939789Sahrens 
2940789Sahrens 	/*
29416673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
29426673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
29436673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
29446673Seschrock 	 */
29456673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
29466673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
29476673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
29486673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
29496673Seschrock 		cvd = pvd->vdev_child[0];
29506673Seschrock 		len = strlen(vd->vdev_path);
29516673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
29526673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
29536673Seschrock 			spa_strfree(cvd->vdev_path);
29546673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
29556673Seschrock 		}
29566673Seschrock 	}
29576673Seschrock 
29586673Seschrock 	/*
29592082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
29602082Seschrock 	 * that the spare should become a real disk, and be removed from the
29612082Seschrock 	 * active spare list for the pool.
29622082Seschrock 	 */
29632082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
29642082Seschrock 	    vd->vdev_id == 0)
29652082Seschrock 		unspare = B_TRUE;
29662082Seschrock 
29672082Seschrock 	/*
2968789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
2969789Sahrens 	 * This must be done after all other error cases are handled,
2970789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
2971789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
2972789Sahrens 	 * it may be that the unwritability of the disk is the reason
2973789Sahrens 	 * it's being detached!
2974789Sahrens 	 */
29753377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
2976789Sahrens 
2977789Sahrens 	/*
2978789Sahrens 	 * Remove vd from its parent and compact the parent's children.
2979789Sahrens 	 */
2980789Sahrens 	vdev_remove_child(pvd, vd);
2981789Sahrens 	vdev_compact_children(pvd);
2982789Sahrens 
2983789Sahrens 	/*
2984789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
2985789Sahrens 	 */
2986789Sahrens 	cvd = pvd->vdev_child[0];
2987789Sahrens 
2988789Sahrens 	/*
29892082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
29902082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
29912082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
29922082Seschrock 	 * GUID if it creates a new toplevel GUID.
29932082Seschrock 	 */
29942082Seschrock 	if (unspare) {
29952082Seschrock 		ASSERT(cvd->vdev_isspare);
29963377Seschrock 		spa_spare_remove(cvd);
29972082Seschrock 		unspare_guid = cvd->vdev_guid;
29982082Seschrock 	}
29992082Seschrock 
30002082Seschrock 	/*
3001789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3002789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3003789Sahrens 	 */
3004789Sahrens 	if (pvd->vdev_children == 1)
3005789Sahrens 		vdev_remove_parent(cvd);
3006789Sahrens 
3007789Sahrens 	/*
3008789Sahrens 	 * We don't set tvd until now because the parent we just removed
3009789Sahrens 	 * may have been the previous top-level vdev.
3010789Sahrens 	 */
3011789Sahrens 	tvd = cvd->vdev_top;
3012789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3013789Sahrens 
3014789Sahrens 	/*
30153377Seschrock 	 * Reevaluate the parent vdev state.
3016789Sahrens 	 */
30174451Seschrock 	vdev_propagate_state(cvd);
3018789Sahrens 
3019789Sahrens 	/*
30203377Seschrock 	 * If the device we just detached was smaller than the others, it may be
30213377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
30223377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
30233377Seschrock 	 * there's nothing to read from disk.
3024789Sahrens 	 */
30251732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3026789Sahrens 
3027789Sahrens 	vdev_config_dirty(tvd);
3028789Sahrens 
3029789Sahrens 	/*
30303377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
30313377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
30323377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
30333377Seschrock 	 * prevent vd from being accessed after it's freed.
3034789Sahrens 	 */
3035789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
3036789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
30371732Sbonwick 	vd->vdev_detached = B_TRUE;
30381732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3039789Sahrens 
30404451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
30414451Seschrock 
30422082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
30432082Seschrock 
30442082Seschrock 	/*
30453377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
30463377Seschrock 	 * then we want to go through and remove the device from the hot spare
30473377Seschrock 	 * list of every other pool.
30482082Seschrock 	 */
30492082Seschrock 	if (unspare) {
30502082Seschrock 		spa = NULL;
30512082Seschrock 		mutex_enter(&spa_namespace_lock);
30522082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
30532082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
30542082Seschrock 				continue;
30552082Seschrock 
30562082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
30572082Seschrock 		}
30582082Seschrock 		mutex_exit(&spa_namespace_lock);
30592082Seschrock 	}
30602082Seschrock 
30612082Seschrock 	return (error);
30622082Seschrock }
30632082Seschrock 
30642082Seschrock /*
30655450Sbrendan  * Remove a spares vdev from the nvlist config.
30662082Seschrock  */
30675450Sbrendan static int
30685450Sbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare,
30695450Sbrendan     nvlist_t **spares, int nspares, vdev_t *vd)
30702082Seschrock {
30715450Sbrendan 	nvlist_t *nv, **newspares;
30725450Sbrendan 	int i, j;
30732082Seschrock 
30742082Seschrock 	nv = NULL;
30755450Sbrendan 	for (i = 0; i < nspares; i++) {
30765450Sbrendan 		uint64_t theguid;
30775450Sbrendan 
30785450Sbrendan 		VERIFY(nvlist_lookup_uint64(spares[i],
30795450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
30805450Sbrendan 		if (theguid == guid) {
30815450Sbrendan 			nv = spares[i];
30825450Sbrendan 			break;
30832082Seschrock 		}
30842082Seschrock 	}
30852082Seschrock 
30862082Seschrock 	/*
30875450Sbrendan 	 * Only remove the hot spare if it's not currently in use in this pool.
30882082Seschrock 	 */
30895450Sbrendan 	if (nv == NULL && vd == NULL)
30905450Sbrendan 		return (ENOENT);
30915450Sbrendan 
30925450Sbrendan 	if (nv == NULL && vd != NULL)
30935450Sbrendan 		return (ENOTSUP);
30945450Sbrendan 
30955450Sbrendan 	if (!unspare && nv != NULL && vd != NULL)
30965450Sbrendan 		return (EBUSY);
30972082Seschrock 
30982082Seschrock 	if (nspares == 1) {
30992082Seschrock 		newspares = NULL;
31002082Seschrock 	} else {
31012082Seschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
31022082Seschrock 		    KM_SLEEP);
31032082Seschrock 		for (i = 0, j = 0; i < nspares; i++) {
31042082Seschrock 			if (spares[i] != nv)
31052082Seschrock 				VERIFY(nvlist_dup(spares[i],
31062082Seschrock 				    &newspares[j++], KM_SLEEP) == 0);
31072082Seschrock 		}
31082082Seschrock 	}
31092082Seschrock 
31105450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES,
31112082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
31125450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
31135450Sbrendan 	    ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0);
31142082Seschrock 	for (i = 0; i < nspares - 1; i++)
31152082Seschrock 		nvlist_free(newspares[i]);
31162082Seschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
31175450Sbrendan 
31185450Sbrendan 	return (0);
31195450Sbrendan }
31205450Sbrendan 
31215450Sbrendan /*
31225450Sbrendan  * Remove an l2cache vdev from the nvlist config.
31235450Sbrendan  */
31245450Sbrendan static int
31255450Sbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache,
31265450Sbrendan     int nl2cache, vdev_t *vd)
31275450Sbrendan {
31285450Sbrendan 	nvlist_t *nv, **newl2cache;
31295450Sbrendan 	int i, j;
31305450Sbrendan 
31315450Sbrendan 	nv = NULL;
31325450Sbrendan 	for (i = 0; i < nl2cache; i++) {
31335450Sbrendan 		uint64_t theguid;
31345450Sbrendan 
31355450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i],
31365450Sbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
31375450Sbrendan 		if (theguid == guid) {
31385450Sbrendan 			nv = l2cache[i];
31395450Sbrendan 			break;
31405450Sbrendan 		}
31415450Sbrendan 	}
31425450Sbrendan 
31435450Sbrendan 	if (vd == NULL) {
31445450Sbrendan 		for (i = 0; i < nl2cache; i++) {
31455450Sbrendan 			if (sav->sav_vdevs[i]->vdev_guid == guid) {
31465450Sbrendan 				vd = sav->sav_vdevs[i];
31475450Sbrendan 				break;
31485450Sbrendan 			}
31495450Sbrendan 		}
31505450Sbrendan 	}
31515450Sbrendan 
31525450Sbrendan 	if (nv == NULL && vd == NULL)
31535450Sbrendan 		return (ENOENT);
31545450Sbrendan 
31555450Sbrendan 	if (nv == NULL && vd != NULL)
31565450Sbrendan 		return (ENOTSUP);
31575450Sbrendan 
31585450Sbrendan 	if (nl2cache == 1) {
31595450Sbrendan 		newl2cache = NULL;
31605450Sbrendan 	} else {
31615450Sbrendan 		newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *),
31625450Sbrendan 		    KM_SLEEP);
31635450Sbrendan 		for (i = 0, j = 0; i < nl2cache; i++) {
31645450Sbrendan 			if (l2cache[i] != nv)
31655450Sbrendan 				VERIFY(nvlist_dup(l2cache[i],
31665450Sbrendan 				    &newl2cache[j++], KM_SLEEP) == 0);
31675450Sbrendan 		}
31685450Sbrendan 	}
31695450Sbrendan 
31705450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
31715450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
31725450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
31735450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0);
31745450Sbrendan 	for (i = 0; i < nl2cache - 1; i++)
31755450Sbrendan 		nvlist_free(newl2cache[i]);
31765450Sbrendan 	kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *));
31775450Sbrendan 
31785450Sbrendan 	return (0);
31795450Sbrendan }
31805450Sbrendan 
31815450Sbrendan /*
31825450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
31835450Sbrendan  * spares and level 2 ARC devices.
31845450Sbrendan  */
31855450Sbrendan int
31865450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
31875450Sbrendan {
31885450Sbrendan 	vdev_t *vd;
31895450Sbrendan 	nvlist_t **spares, **l2cache;
31905450Sbrendan 	uint_t nspares, nl2cache;
31915450Sbrendan 	int error = 0;
31925450Sbrendan 
31935450Sbrendan 	spa_config_enter(spa, RW_WRITER, FTAG);
31945450Sbrendan 
31956643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
31965450Sbrendan 
31975450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
31985450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
31995450Sbrendan 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
32005450Sbrendan 		if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare,
32015450Sbrendan 		    spares, nspares, vd)) != 0)
32025450Sbrendan 			goto out;
32035450Sbrendan 		spa_load_spares(spa);
32045450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
32055450Sbrendan 		goto out;
32065450Sbrendan 	}
32075450Sbrendan 
32085450Sbrendan 	if (spa->spa_l2cache.sav_vdevs != NULL &&
32095450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
32105450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) {
32115450Sbrendan 		if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid,
32125450Sbrendan 		    l2cache, nl2cache, vd)) != 0)
32135450Sbrendan 			goto out;
32145450Sbrendan 		spa_load_l2cache(spa);
32155450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
32165450Sbrendan 	}
32172082Seschrock 
32182082Seschrock out:
32192082Seschrock 	spa_config_exit(spa, FTAG);
32205450Sbrendan 	return (error);
3221789Sahrens }
3222789Sahrens 
3223789Sahrens /*
32244451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
32254451Seschrock  * current spared, so we can detach it.
3226789Sahrens  */
32271544Seschrock static vdev_t *
32284451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3229789Sahrens {
32301544Seschrock 	vdev_t *newvd, *oldvd;
3231789Sahrens 	int c;
3232789Sahrens 
32331544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
32344451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
32351544Seschrock 		if (oldvd != NULL)
32361544Seschrock 			return (oldvd);
32371544Seschrock 	}
3238789Sahrens 
32394451Seschrock 	/*
32404451Seschrock 	 * Check for a completed replacement.
32414451Seschrock 	 */
3242789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
32431544Seschrock 		oldvd = vd->vdev_child[0];
32441544Seschrock 		newvd = vd->vdev_child[1];
3245789Sahrens 
32461544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
32471544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
32481544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
32491544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
32501544Seschrock 			return (oldvd);
32511544Seschrock 		}
32521544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
32531544Seschrock 	}
3254789Sahrens 
32554451Seschrock 	/*
32564451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
32574451Seschrock 	 */
32584451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
32594451Seschrock 		newvd = vd->vdev_child[0];
32604451Seschrock 		oldvd = vd->vdev_child[1];
32614451Seschrock 
32624451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
32634451Seschrock 		if (newvd->vdev_unspare &&
32644451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
32654451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
32664451Seschrock 			newvd->vdev_unspare = 0;
32674451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
32684451Seschrock 			return (oldvd);
32694451Seschrock 		}
32704451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
32714451Seschrock 	}
32724451Seschrock 
32731544Seschrock 	return (NULL);
3274789Sahrens }
3275789Sahrens 
32761544Seschrock static void
32774451Seschrock spa_vdev_resilver_done(spa_t *spa)
3278789Sahrens {
32791544Seschrock 	vdev_t *vd;
32802082Seschrock 	vdev_t *pvd;
32811544Seschrock 	uint64_t guid;
32822082Seschrock 	uint64_t pguid = 0;
3283789Sahrens 
32841544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3285789Sahrens 
32864451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
32871544Seschrock 		guid = vd->vdev_guid;
32882082Seschrock 		/*
32892082Seschrock 		 * If we have just finished replacing a hot spared device, then
32902082Seschrock 		 * we need to detach the parent's first child (the original hot
32912082Seschrock 		 * spare) as well.
32922082Seschrock 		 */
32932082Seschrock 		pvd = vd->vdev_parent;
32942082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
32952082Seschrock 		    pvd->vdev_id == 0) {
32962082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
32972082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
32982082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
32992082Seschrock 		}
33001544Seschrock 		spa_config_exit(spa, FTAG);
33011544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
33021544Seschrock 			return;
33032082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
33042082Seschrock 			return;
33051544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
3306789Sahrens 	}
3307789Sahrens 
33081544Seschrock 	spa_config_exit(spa, FTAG);
3309789Sahrens }
3310789Sahrens 
3311789Sahrens /*
33121354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
33131354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
33141354Seschrock  */
33151354Seschrock int
33161354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
33171354Seschrock {
33186643Seschrock 	vdev_t *vd;
33191354Seschrock 	uint64_t txg;
33201354Seschrock 
33211354Seschrock 	txg = spa_vdev_enter(spa);
33221354Seschrock 
33236643Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
33242082Seschrock 		/*
33256643Seschrock 		 * Determine if this is a reference to a hot spare device.  If
33266643Seschrock 		 * it is, update the path manually as there is no associated
33276643Seschrock 		 * vdev_t that can be synced to disk.
33282082Seschrock 		 */
33296643Seschrock 		nvlist_t **spares;
33306643Seschrock 		uint_t i, nspares;
33315450Sbrendan 
33325450Sbrendan 		if (spa->spa_spares.sav_config != NULL) {
33335450Sbrendan 			VERIFY(nvlist_lookup_nvlist_array(
33345450Sbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
33355450Sbrendan 			    &spares, &nspares) == 0);
33362082Seschrock 			for (i = 0; i < nspares; i++) {
33372082Seschrock 				uint64_t theguid;
33382082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
33392082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
33405450Sbrendan 				if (theguid == guid) {
33415450Sbrendan 					VERIFY(nvlist_add_string(spares[i],
33425450Sbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
33435450Sbrendan 					spa_load_spares(spa);
33445450Sbrendan 					spa->spa_spares.sav_sync = B_TRUE;
33455450Sbrendan 					return (spa_vdev_exit(spa, NULL, txg,
33465450Sbrendan 					    0));
33475450Sbrendan 				}
33482082Seschrock 			}
33492082Seschrock 		}
33505450Sbrendan 
33515450Sbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
33522082Seschrock 	}
33531354Seschrock 
33541585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
33551585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
33561585Sbonwick 
33571354Seschrock 	spa_strfree(vd->vdev_path);
33581354Seschrock 	vd->vdev_path = spa_strdup(newpath);
33591354Seschrock 
33601354Seschrock 	vdev_config_dirty(vd->vdev_top);
33611354Seschrock 
33621354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
33631354Seschrock }
33641354Seschrock 
33651354Seschrock /*
3366789Sahrens  * ==========================================================================
3367789Sahrens  * SPA Scrubbing
3368789Sahrens  * ==========================================================================
3369789Sahrens  */
3370789Sahrens 
3371789Sahrens static void
3372789Sahrens spa_scrub_io_done(zio_t *zio)
3373789Sahrens {
3374789Sahrens 	spa_t *spa = zio->io_spa;
3375789Sahrens 
33764309Smaybee 	arc_data_buf_free(zio->io_data, zio->io_size);
3377789Sahrens 
3378789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
33791544Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
33801775Sbillm 		vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev;
3381789Sahrens 		spa->spa_scrub_errors++;
3382789Sahrens 		mutex_enter(&vd->vdev_stat_lock);
3383789Sahrens 		vd->vdev_stat.vs_scrub_errors++;
3384789Sahrens 		mutex_exit(&vd->vdev_stat_lock);
3385789Sahrens 	}
33863697Smishra 
33873697Smishra 	if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight)
33881544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
33893697Smishra 
33903697Smishra 	ASSERT(spa->spa_scrub_inflight >= 0);
33913697Smishra 
33921544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
3393789Sahrens }
3394789Sahrens 
3395789Sahrens static void
33961544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
33971544Seschrock     zbookmark_t *zb)
3398789Sahrens {
3399789Sahrens 	size_t size = BP_GET_LSIZE(bp);
34003697Smishra 	void *data;
3401789Sahrens 
3402789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
34033697Smishra 	/*
34043697Smishra 	 * Do not give too much work to vdev(s).
34053697Smishra 	 */
34063697Smishra 	while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) {
34073697Smishra 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
34083697Smishra 	}
3409789Sahrens 	spa->spa_scrub_inflight++;
3410789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3411789Sahrens 
34124309Smaybee 	data = arc_data_buf_alloc(size);
34133697Smishra 
34141544Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
34151544Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
34161544Seschrock 
34171807Sbonwick 	flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL;
34181544Seschrock 
3419789Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
34201544Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
3421789Sahrens }
3422789Sahrens 
3423789Sahrens /* ARGSUSED */
3424789Sahrens static int
3425789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
3426789Sahrens {
3427789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
34281775Sbillm 	vdev_t *vd = spa->spa_root_vdev;
34291775Sbillm 	dva_t *dva = bp->blk_dva;
34301775Sbillm 	int needs_resilver = B_FALSE;
34311775Sbillm 	int d;
3432789Sahrens 
34331775Sbillm 	if (bc->bc_errno) {
3434789Sahrens 		/*
3435789Sahrens 		 * We can't scrub this block, but we can continue to scrub
3436789Sahrens 		 * the rest of the pool.  Note the error and move along.
3437789Sahrens 		 */
3438789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
3439789Sahrens 		spa->spa_scrub_errors++;
3440789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
3441789Sahrens 
34421775Sbillm 		mutex_enter(&vd->vdev_stat_lock);
34431775Sbillm 		vd->vdev_stat.vs_scrub_errors++;
34441775Sbillm 		mutex_exit(&vd->vdev_stat_lock);
3445789Sahrens 
3446789Sahrens 		return (ERESTART);
3447789Sahrens 	}
3448789Sahrens 
3449789Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
3450789Sahrens 
34511775Sbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
34521775Sbillm 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]));
34531775Sbillm 
34541775Sbillm 		ASSERT(vd != NULL);
34551775Sbillm 
34561775Sbillm 		/*
34571775Sbillm 		 * Keep track of how much data we've examined so that
34581775Sbillm 		 * zpool(1M) status can make useful progress reports.
34591775Sbillm 		 */
34601775Sbillm 		mutex_enter(&vd->vdev_stat_lock);
34611775Sbillm 		vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]);
34621775Sbillm 		mutex_exit(&vd->vdev_stat_lock);
3463789Sahrens 
34641775Sbillm 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
34651775Sbillm 			if (DVA_GET_GANG(&dva[d])) {
34661775Sbillm 				/*
34671775Sbillm 				 * Gang members may be spread across multiple
34681775Sbillm 				 * vdevs, so the best we can do is look at the
34691775Sbillm 				 * pool-wide DTL.
34701775Sbillm 				 * XXX -- it would be better to change our
34711775Sbillm 				 * allocation policy to ensure that this can't
34721775Sbillm 				 * happen.
34731775Sbillm 				 */
34741775Sbillm 				vd = spa->spa_root_vdev;
34751775Sbillm 			}
34761775Sbillm 			if (vdev_dtl_contains(&vd->vdev_dtl_map,
34771775Sbillm 			    bp->blk_birth, 1))
34781775Sbillm 				needs_resilver = B_TRUE;
3479789Sahrens 		}
34801775Sbillm 	}
34811775Sbillm 
34821775Sbillm 	if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING)
3483789Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
34841544Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
34851775Sbillm 	else if (needs_resilver)
34861775Sbillm 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
34871775Sbillm 		    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
3488789Sahrens 
3489789Sahrens 	return (0);
3490789Sahrens }
3491789Sahrens 
3492789Sahrens static void
3493789Sahrens spa_scrub_thread(spa_t *spa)
3494789Sahrens {
3495789Sahrens 	callb_cpr_t cprinfo;
3496789Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
3497789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3498789Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
3499789Sahrens 	int error = 0;
3500789Sahrens 	boolean_t complete;
3501789Sahrens 
3502789Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
3503789Sahrens 
3504797Sbonwick 	/*
3505797Sbonwick 	 * If we're restarting due to a snapshot create/delete,
3506797Sbonwick 	 * wait for that to complete.
3507797Sbonwick 	 */
3508797Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
3509797Sbonwick 
35101544Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
35111544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
35121544Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
35131544Seschrock 
35141544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
35151544Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
3516789Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
3517789Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
35181544Seschrock 	spa_config_exit(spa, FTAG);
3519789Sahrens 
3520789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3521789Sahrens 	spa->spa_scrub_errors = 0;
3522789Sahrens 	spa->spa_scrub_active = 1;
35231544Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
3524789Sahrens 
3525789Sahrens 	while (!spa->spa_scrub_stop) {
3526789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
35271544Seschrock 		while (spa->spa_scrub_suspended) {
3528789Sahrens 			spa->spa_scrub_active = 0;
3529789Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
3530789Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3531789Sahrens 			spa->spa_scrub_active = 1;
3532789Sahrens 		}
3533789Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
3534789Sahrens 
3535789Sahrens 		if (spa->spa_scrub_restart_txg != 0)
3536789Sahrens 			break;
3537789Sahrens 
3538789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
3539789Sahrens 		error = traverse_more(th);
3540789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
3541789Sahrens 		if (error != EAGAIN)
3542789Sahrens 			break;
3543789Sahrens 	}
3544789Sahrens 
3545789Sahrens 	while (spa->spa_scrub_inflight)
3546789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3547789Sahrens 
35481601Sbonwick 	spa->spa_scrub_active = 0;
35491601Sbonwick 	cv_broadcast(&spa->spa_scrub_cv);
35501601Sbonwick 
35511601Sbonwick 	mutex_exit(&spa->spa_scrub_lock);
35521601Sbonwick 
35531601Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
35541601Sbonwick 
35551601Sbonwick 	mutex_enter(&spa->spa_scrub_lock);
35561601Sbonwick 
35571601Sbonwick 	/*
35581601Sbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
35591601Sbonwick 	 * AND the spa config lock to synchronize with any config changes
35601601Sbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
35611601Sbonwick 	 */
3562789Sahrens 	if (spa->spa_scrub_restart_txg != 0)
3563789Sahrens 		error = ERESTART;
3564789Sahrens 
35651544Seschrock 	if (spa->spa_scrub_stop)
35661544Seschrock 		error = EINTR;
35671544Seschrock 
3568789Sahrens 	/*
35691544Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
35701544Seschrock 	 * completed.  The downside is that if there is a transient error during
35711544Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
35721544Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
35731544Seschrock 	 * which isn't really acceptable.  Since there is enough information for
35741544Seschrock 	 * the user to know what has failed and why, this seems like a more
35751544Seschrock 	 * tractable approach.
3576789Sahrens 	 */
35771544Seschrock 	complete = (error == 0);
3578789Sahrens 
35791544Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
35801544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
3581789Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
3582789Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
3583789Sahrens 
3584789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3585789Sahrens 
3586789Sahrens 	/*
3587789Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
3588789Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
3589789Sahrens 	 */
3590789Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
3591789Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
3592789Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
35931544Seschrock 	spa_errlog_rotate(spa);
35941601Sbonwick 
35954451Seschrock 	if (scrub_type == POOL_SCRUB_RESILVER && complete)
35964451Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH);
35974451Seschrock 
35981544Seschrock 	spa_config_exit(spa, FTAG);
3599789Sahrens 
3600789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3601789Sahrens 
36021544Seschrock 	/*
36031544Seschrock 	 * We may have finished replacing a device.
36041544Seschrock 	 * Let the async thread assess this and handle the detach.
36051544Seschrock 	 */
36064451Seschrock 	spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3607789Sahrens 
3608789Sahrens 	/*
3609789Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
3610789Sahrens 	 */
3611789Sahrens 	if (error == ERESTART)
36121544Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
36131544Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
3614789Sahrens 
36151544Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
36161544Seschrock 	spa->spa_scrub_active = 0;
36171544Seschrock 	spa->spa_scrub_thread = NULL;
36181544Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
3619789Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
3620789Sahrens 	thread_exit();
3621789Sahrens }
3622789Sahrens 
3623789Sahrens void
3624789Sahrens spa_scrub_suspend(spa_t *spa)
3625789Sahrens {
3626789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
36271544Seschrock 	spa->spa_scrub_suspended++;
3628789Sahrens 	while (spa->spa_scrub_active) {
3629789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3630789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3631789Sahrens 	}
3632789Sahrens 	while (spa->spa_scrub_inflight)
3633789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3634789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3635789Sahrens }
3636789Sahrens 
3637789Sahrens void
3638789Sahrens spa_scrub_resume(spa_t *spa)
3639789Sahrens {
3640789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
36411544Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
36421544Seschrock 	if (--spa->spa_scrub_suspended == 0)
3643789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3644789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3645789Sahrens }
3646789Sahrens 
3647789Sahrens void
3648789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
3649789Sahrens {
3650789Sahrens 	/*
3651789Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
3652789Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
3653789Sahrens 	 * fix this properly.
3654789Sahrens 	 */
3655789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3656789Sahrens 	spa->spa_scrub_restart_txg = txg;
3657789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3658789Sahrens }
3659789Sahrens 
36601544Seschrock int
36611544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
3662789Sahrens {
3663789Sahrens 	space_seg_t *ss;
3664789Sahrens 	uint64_t mintxg, maxtxg;
3665789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3666789Sahrens 
36674808Sek110237 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
36684808Sek110237 	ASSERT(!spa_config_held(spa, RW_WRITER));
36694808Sek110237 
3670789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3671789Sahrens 		return (ENOTSUP);
3672789Sahrens 
36731544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
36741544Seschrock 
3675789Sahrens 	/*
3676789Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
3677789Sahrens 	 */
3678789Sahrens 	while (spa->spa_scrub_thread != NULL) {
3679789Sahrens 		/*
3680789Sahrens 		 * Don't stop a resilver unless forced.
3681789Sahrens 		 */
36821544Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
36831544Seschrock 			mutex_exit(&spa->spa_scrub_lock);
3684789Sahrens 			return (EBUSY);
36851544Seschrock 		}
3686789Sahrens 		spa->spa_scrub_stop = 1;
3687789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3688789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3689789Sahrens 	}
3690789Sahrens 
3691789Sahrens 	/*
3692789Sahrens 	 * Terminate the previous traverse.
3693789Sahrens 	 */
3694789Sahrens 	if (spa->spa_scrub_th != NULL) {
3695789Sahrens 		traverse_fini(spa->spa_scrub_th);
3696789Sahrens 		spa->spa_scrub_th = NULL;
3697789Sahrens 	}
3698789Sahrens 
36991544Seschrock 	if (rvd == NULL) {
37001544Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
37011544Seschrock 		ASSERT(spa->spa_scrub_type == type);
37021544Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
37031544Seschrock 		mutex_exit(&spa->spa_scrub_lock);
37041544Seschrock 		return (0);
37051544Seschrock 	}
3706789Sahrens 
3707789Sahrens 	mintxg = TXG_INITIAL - 1;
3708789Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
3709789Sahrens 
37101544Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
3711789Sahrens 
37121544Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
37131544Seschrock 		/*
37141544Seschrock 		 * The pool-wide DTL is empty.
37151732Sbonwick 		 * If this is a resilver, there's nothing to do except
37161732Sbonwick 		 * check whether any in-progress replacements have completed.
37171544Seschrock 		 */
37181732Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
37191544Seschrock 			type = POOL_SCRUB_NONE;
37204451Seschrock 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
37211732Sbonwick 		}
37221544Seschrock 	} else {
37231544Seschrock 		/*
37241544Seschrock 		 * The pool-wide DTL is non-empty.
37251544Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
37261544Seschrock 		 */
37271544Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
37281544Seschrock 			type = POOL_SCRUB_RESILVER;
37291544Seschrock 	}
3730789Sahrens 
37311544Seschrock 	if (type == POOL_SCRUB_RESILVER) {
3732789Sahrens 		/*
3733789Sahrens 		 * Determine the resilvering boundaries.
3734789Sahrens 		 *
3735789Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
3736789Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
3737789Sahrens 		 *
3738789Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
3739789Sahrens 		 * so we don't claim to resilver a txg that's still changing.
3740789Sahrens 		 */
3741789Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
37421544Seschrock 		mintxg = ss->ss_start - 1;
3743789Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
37441544Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
37454451Seschrock 
37464451Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
3747789Sahrens 	}
3748789Sahrens 
37491544Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
37501544Seschrock 
37511544Seschrock 	spa->spa_scrub_stop = 0;
37521544Seschrock 	spa->spa_scrub_type = type;
37531544Seschrock 	spa->spa_scrub_restart_txg = 0;
37541544Seschrock 
37551544Seschrock 	if (type != POOL_SCRUB_NONE) {
37561544Seschrock 		spa->spa_scrub_mintxg = mintxg;
3757789Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
3758789Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
37591635Sbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
37601635Sbonwick 		    ZIO_FLAG_CANFAIL);
3761789Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
3762789Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
3763789Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3764789Sahrens 	}
3765789Sahrens 
37661544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
37671544Seschrock 
3768789Sahrens 	return (0);
3769789Sahrens }
3770789Sahrens 
37711544Seschrock /*
37721544Seschrock  * ==========================================================================
37731544Seschrock  * SPA async task processing
37741544Seschrock  * ==========================================================================
37751544Seschrock  */
37761544Seschrock 
37771544Seschrock static void
37784451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3779789Sahrens {
37801544Seschrock 	vdev_t *tvd;
37811544Seschrock 	int c;
37821544Seschrock 
37834451Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
37844451Seschrock 		tvd = vd->vdev_child[c];
37854451Seschrock 		if (tvd->vdev_remove_wanted) {
37864451Seschrock 			tvd->vdev_remove_wanted = 0;
37874451Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
37884451Seschrock 			    VDEV_AUX_NONE);
37895329Sgw25295 			vdev_clear(spa, tvd, B_TRUE);
37904451Seschrock 			vdev_config_dirty(tvd->vdev_top);
37911544Seschrock 		}
37924451Seschrock 		spa_async_remove(spa, tvd);
37931544Seschrock 	}
37941544Seschrock }
37951544Seschrock 
37961544Seschrock static void
37971544Seschrock spa_async_thread(spa_t *spa)
37981544Seschrock {
37991544Seschrock 	int tasks;
38004451Seschrock 	uint64_t txg;
38011544Seschrock 
38021544Seschrock 	ASSERT(spa->spa_sync_on);
3803789Sahrens 
38041544Seschrock 	mutex_enter(&spa->spa_async_lock);
38051544Seschrock 	tasks = spa->spa_async_tasks;
38061544Seschrock 	spa->spa_async_tasks = 0;
38071544Seschrock 	mutex_exit(&spa->spa_async_lock);
38081544Seschrock 
38091544Seschrock 	/*
38101635Sbonwick 	 * See if the config needs to be updated.
38111635Sbonwick 	 */
38121635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
38131635Sbonwick 		mutex_enter(&spa_namespace_lock);
38141635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
38151635Sbonwick 		mutex_exit(&spa_namespace_lock);
38161635Sbonwick 	}
38171635Sbonwick 
38181635Sbonwick 	/*
38194451Seschrock 	 * See if any devices need to be marked REMOVED.
38205329Sgw25295 	 *
38215329Sgw25295 	 * XXX - We avoid doing this when we are in
38225329Sgw25295 	 * I/O failure state since spa_vdev_enter() grabs
38235329Sgw25295 	 * the namespace lock and would not be able to obtain
38245329Sgw25295 	 * the writer config lock.
38251544Seschrock 	 */
38265329Sgw25295 	if (tasks & SPA_ASYNC_REMOVE &&
38275329Sgw25295 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
38284451Seschrock 		txg = spa_vdev_enter(spa);
38294451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
38304451Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
38314451Seschrock 	}
38321544Seschrock 
38331544Seschrock 	/*
38341544Seschrock 	 * If any devices are done replacing, detach them.
38351544Seschrock 	 */
38364451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
38374451Seschrock 		spa_vdev_resilver_done(spa);
3838789Sahrens 
38391544Seschrock 	/*
38404451Seschrock 	 * Kick off a scrub.  When starting a RESILVER scrub (or an EVERYTHING
38414451Seschrock 	 * scrub which can become a resilver), we need to hold
38424451Seschrock 	 * spa_namespace_lock() because the sysevent we post via
38434451Seschrock 	 * spa_event_notify() needs to get the name of the pool.
38441544Seschrock 	 */
38454451Seschrock 	if (tasks & SPA_ASYNC_SCRUB) {
38464451Seschrock 		mutex_enter(&spa_namespace_lock);
38471544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
38484451Seschrock 		mutex_exit(&spa_namespace_lock);
38494451Seschrock 	}
38501544Seschrock 
38511544Seschrock 	/*
38521544Seschrock 	 * Kick off a resilver.
38531544Seschrock 	 */
38544451Seschrock 	if (tasks & SPA_ASYNC_RESILVER) {
38554451Seschrock 		mutex_enter(&spa_namespace_lock);
38561544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
38574451Seschrock 		mutex_exit(&spa_namespace_lock);
38584451Seschrock 	}
38591544Seschrock 
38601544Seschrock 	/*
38611544Seschrock 	 * Let the world know that we're done.
38621544Seschrock 	 */
38631544Seschrock 	mutex_enter(&spa->spa_async_lock);
38641544Seschrock 	spa->spa_async_thread = NULL;
38651544Seschrock 	cv_broadcast(&spa->spa_async_cv);
38661544Seschrock 	mutex_exit(&spa->spa_async_lock);
38671544Seschrock 	thread_exit();
38681544Seschrock }
38691544Seschrock 
38701544Seschrock void
38711544Seschrock spa_async_suspend(spa_t *spa)
38721544Seschrock {
38731544Seschrock 	mutex_enter(&spa->spa_async_lock);
38741544Seschrock 	spa->spa_async_suspended++;
38751544Seschrock 	while (spa->spa_async_thread != NULL)
38761544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
38771544Seschrock 	mutex_exit(&spa->spa_async_lock);
38781544Seschrock }
38791544Seschrock 
38801544Seschrock void
38811544Seschrock spa_async_resume(spa_t *spa)
38821544Seschrock {
38831544Seschrock 	mutex_enter(&spa->spa_async_lock);
38841544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
38851544Seschrock 	spa->spa_async_suspended--;
38861544Seschrock 	mutex_exit(&spa->spa_async_lock);
38871544Seschrock }
38881544Seschrock 
38891544Seschrock static void
38901544Seschrock spa_async_dispatch(spa_t *spa)
38911544Seschrock {
38921544Seschrock 	mutex_enter(&spa->spa_async_lock);
38931544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
38941635Sbonwick 	    spa->spa_async_thread == NULL &&
38951635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
38961544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
38971544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
38981544Seschrock 	mutex_exit(&spa->spa_async_lock);
38991544Seschrock }
39001544Seschrock 
39011544Seschrock void
39021544Seschrock spa_async_request(spa_t *spa, int task)
39031544Seschrock {
39041544Seschrock 	mutex_enter(&spa->spa_async_lock);
39051544Seschrock 	spa->spa_async_tasks |= task;
39061544Seschrock 	mutex_exit(&spa->spa_async_lock);
3907789Sahrens }
3908789Sahrens 
3909789Sahrens /*
3910789Sahrens  * ==========================================================================
3911789Sahrens  * SPA syncing routines
3912789Sahrens  * ==========================================================================
3913789Sahrens  */
3914789Sahrens 
3915789Sahrens static void
3916789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3917789Sahrens {
3918789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3919789Sahrens 	dmu_tx_t *tx;
3920789Sahrens 	blkptr_t blk;
3921789Sahrens 	uint64_t itor = 0;
3922789Sahrens 	zio_t *zio;
3923789Sahrens 	int error;
3924789Sahrens 	uint8_t c = 1;
3925789Sahrens 
3926789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3927789Sahrens 
3928789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3929789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3930789Sahrens 
3931789Sahrens 	error = zio_wait(zio);
3932789Sahrens 	ASSERT3U(error, ==, 0);
3933789Sahrens 
3934789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3935789Sahrens 	bplist_vacate(bpl, tx);
3936789Sahrens 
3937789Sahrens 	/*
3938789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3939789Sahrens 	 * (Usually only the first block is needed.)
3940789Sahrens 	 */
3941789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3942789Sahrens 	dmu_tx_commit(tx);
3943789Sahrens }
3944789Sahrens 
3945789Sahrens static void
39462082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
39472082Seschrock {
39482082Seschrock 	char *packed = NULL;
39492082Seschrock 	size_t nvsize = 0;
39502082Seschrock 	dmu_buf_t *db;
39512082Seschrock 
39522082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
39532082Seschrock 
39542082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
39552082Seschrock 
39562082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
39572082Seschrock 	    KM_SLEEP) == 0);
39582082Seschrock 
39592082Seschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
39602082Seschrock 
39612082Seschrock 	kmem_free(packed, nvsize);
39622082Seschrock 
39632082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
39642082Seschrock 	dmu_buf_will_dirty(db, tx);
39652082Seschrock 	*(uint64_t *)db->db_data = nvsize;
39662082Seschrock 	dmu_buf_rele(db, FTAG);
39672082Seschrock }
39682082Seschrock 
39692082Seschrock static void
39705450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
39715450Sbrendan     const char *config, const char *entry)
39722082Seschrock {
39732082Seschrock 	nvlist_t *nvroot;
39745450Sbrendan 	nvlist_t **list;
39752082Seschrock 	int i;
39762082Seschrock 
39775450Sbrendan 	if (!sav->sav_sync)
39782082Seschrock 		return;
39792082Seschrock 
39802082Seschrock 	/*
39815450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
39825450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
39834451Seschrock 	 * valid and the vdevs are labeled appropriately.
39842082Seschrock 	 */
39855450Sbrendan 	if (sav->sav_object == 0) {
39865450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
39875450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
39885450Sbrendan 		    sizeof (uint64_t), tx);
39892082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
39905450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
39915450Sbrendan 		    &sav->sav_object, tx) == 0);
39922082Seschrock 	}
39932082Seschrock 
39942082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
39955450Sbrendan 	if (sav->sav_count == 0) {
39965450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
39972082Seschrock 	} else {
39985450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
39995450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
40005450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
40015450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
40025450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
40035450Sbrendan 		    sav->sav_count) == 0);
40045450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
40055450Sbrendan 			nvlist_free(list[i]);
40065450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
40072082Seschrock 	}
40082082Seschrock 
40095450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
40102926Sek110237 	nvlist_free(nvroot);
40112082Seschrock 
40125450Sbrendan 	sav->sav_sync = B_FALSE;
40132082Seschrock }
40142082Seschrock 
40152082Seschrock static void
4016789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
4017789Sahrens {
4018789Sahrens 	nvlist_t *config;
4019789Sahrens 
4020789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
4021789Sahrens 		return;
4022789Sahrens 
4023789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
4024789Sahrens 
40251635Sbonwick 	if (spa->spa_config_syncing)
40261635Sbonwick 		nvlist_free(spa->spa_config_syncing);
40271635Sbonwick 	spa->spa_config_syncing = config;
4028789Sahrens 
40292082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
4030789Sahrens }
4031789Sahrens 
40325094Slling /*
40335094Slling  * Set zpool properties.
40345094Slling  */
40353912Slling static void
40364543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
40373912Slling {
40383912Slling 	spa_t *spa = arg1;
40395094Slling 	objset_t *mos = spa->spa_meta_objset;
40403912Slling 	nvlist_t *nvp = arg2;
40415094Slling 	nvpair_t *elem;
40424451Seschrock 	uint64_t intval;
40436643Seschrock 	char *strval;
40445094Slling 	zpool_prop_t prop;
40455094Slling 	const char *propname;
40465094Slling 	zprop_type_t proptype;
40476643Seschrock 	spa_config_dirent_t *dp;
40485094Slling 
40495094Slling 	elem = NULL;
40505094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
40515094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
40525094Slling 		case ZPOOL_PROP_VERSION:
40535094Slling 			/*
40545094Slling 			 * Only set version for non-zpool-creation cases
40555094Slling 			 * (set/import). spa_create() needs special care
40565094Slling 			 * for version setting.
40575094Slling 			 */
40585094Slling 			if (tx->tx_txg != TXG_INITIAL) {
40595094Slling 				VERIFY(nvpair_value_uint64(elem,
40605094Slling 				    &intval) == 0);
40615094Slling 				ASSERT(intval <= SPA_VERSION);
40625094Slling 				ASSERT(intval >= spa_version(spa));
40635094Slling 				spa->spa_uberblock.ub_version = intval;
40645094Slling 				vdev_config_dirty(spa->spa_root_vdev);
40655094Slling 			}
40665094Slling 			break;
40675094Slling 
40685094Slling 		case ZPOOL_PROP_ALTROOT:
40695094Slling 			/*
40705094Slling 			 * 'altroot' is a non-persistent property. It should
40715094Slling 			 * have been set temporarily at creation or import time.
40725094Slling 			 */
40735094Slling 			ASSERT(spa->spa_root != NULL);
40745094Slling 			break;
40755094Slling 
40765363Seschrock 		case ZPOOL_PROP_CACHEFILE:
40775094Slling 			/*
40785363Seschrock 			 * 'cachefile' is a non-persistent property, but note
40795363Seschrock 			 * an async request that the config cache needs to be
40805363Seschrock 			 * udpated.
40815094Slling 			 */
40825363Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
40836643Seschrock 
40846643Seschrock 			dp = kmem_alloc(sizeof (spa_config_dirent_t),
40856643Seschrock 			    KM_SLEEP);
40866643Seschrock 
40876643Seschrock 			if (strval[0] == '\0')
40886643Seschrock 				dp->scd_path = spa_strdup(spa_config_path);
40896643Seschrock 			else if (strcmp(strval, "none") == 0)
40906643Seschrock 				dp->scd_path = NULL;
40916643Seschrock 			else
40926643Seschrock 				dp->scd_path = spa_strdup(strval);
40936643Seschrock 
40946643Seschrock 			list_insert_head(&spa->spa_config_list, dp);
40955363Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
40964543Smarks 			break;
40975094Slling 		default:
40985094Slling 			/*
40995094Slling 			 * Set pool property values in the poolprops mos object.
41005094Slling 			 */
41015094Slling 			mutex_enter(&spa->spa_props_lock);
41025094Slling 			if (spa->spa_pool_props_object == 0) {
41035094Slling 				objset_t *mos = spa->spa_meta_objset;
41045094Slling 
41055094Slling 				VERIFY((spa->spa_pool_props_object =
41065094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
41075094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
41085094Slling 
41095094Slling 				VERIFY(zap_update(mos,
41105094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
41115094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
41125094Slling 				    == 0);
41135094Slling 			}
41145094Slling 			mutex_exit(&spa->spa_props_lock);
41155094Slling 
41165094Slling 			/* normalize the property name */
41175094Slling 			propname = zpool_prop_to_name(prop);
41185094Slling 			proptype = zpool_prop_get_type(prop);
41195094Slling 
41205094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
41215094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
41225094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
41235094Slling 				VERIFY(zap_update(mos,
41245094Slling 				    spa->spa_pool_props_object, propname,
41255094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
41265094Slling 
41275094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
41285094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
41295094Slling 
41305094Slling 				if (proptype == PROP_TYPE_INDEX) {
41315094Slling 					const char *unused;
41325094Slling 					VERIFY(zpool_prop_index_to_string(
41335094Slling 					    prop, intval, &unused) == 0);
41345094Slling 				}
41355094Slling 				VERIFY(zap_update(mos,
41365094Slling 				    spa->spa_pool_props_object, propname,
41375094Slling 				    8, 1, &intval, tx) == 0);
41385094Slling 			} else {
41395094Slling 				ASSERT(0); /* not allowed */
41405094Slling 			}
41415094Slling 
41425329Sgw25295 			switch (prop) {
41435329Sgw25295 			case ZPOOL_PROP_DELEGATION:
41445094Slling 				spa->spa_delegation = intval;
41455329Sgw25295 				break;
41465329Sgw25295 			case ZPOOL_PROP_BOOTFS:
41475094Slling 				spa->spa_bootfs = intval;
41485329Sgw25295 				break;
41495329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
41505329Sgw25295 				spa->spa_failmode = intval;
41515329Sgw25295 				break;
41525329Sgw25295 			default:
41535329Sgw25295 				break;
41545329Sgw25295 			}
41553912Slling 		}
41565094Slling 
41575094Slling 		/* log internal history if this is not a zpool create */
41585094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
41595094Slling 		    tx->tx_txg != TXG_INITIAL) {
41605094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
41615094Slling 			    spa, tx, cr, "%s %lld %s",
41625094Slling 			    nvpair_name(elem), intval, spa->spa_name);
41635094Slling 		}
41643912Slling 	}
41653912Slling }
41663912Slling 
4167789Sahrens /*
4168789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
4169789Sahrens  * part of the process, so we iterate until it converges.
4170789Sahrens  */
4171789Sahrens void
4172789Sahrens spa_sync(spa_t *spa, uint64_t txg)
4173789Sahrens {
4174789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
4175789Sahrens 	objset_t *mos = spa->spa_meta_objset;
4176789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
41771635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4178789Sahrens 	vdev_t *vd;
4179789Sahrens 	dmu_tx_t *tx;
4180789Sahrens 	int dirty_vdevs;
4181789Sahrens 
4182789Sahrens 	/*
4183789Sahrens 	 * Lock out configuration changes.
4184789Sahrens 	 */
41851544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
4186789Sahrens 
4187789Sahrens 	spa->spa_syncing_txg = txg;
4188789Sahrens 	spa->spa_sync_pass = 0;
4189789Sahrens 
41901544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4191789Sahrens 
41922082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
41932082Seschrock 
41942082Seschrock 	/*
41954577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
41962082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
41972082Seschrock 	 */
41984577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
41994577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
42002082Seschrock 		int i;
42012082Seschrock 
42022082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
42032082Seschrock 			vd = rvd->vdev_child[i];
42042082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
42052082Seschrock 				break;
42062082Seschrock 		}
42072082Seschrock 		if (i == rvd->vdev_children) {
42082082Seschrock 			spa->spa_deflate = TRUE;
42092082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
42102082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
42112082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
42122082Seschrock 		}
42132082Seschrock 	}
42142082Seschrock 
4215789Sahrens 	/*
4216789Sahrens 	 * If anything has changed in this txg, push the deferred frees
4217789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4218789Sahrens 	 * don't generate work on an otherwise idle system.
4219789Sahrens 	 */
4220789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
42212329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
42222329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4223789Sahrens 		spa_sync_deferred_frees(spa, txg);
4224789Sahrens 
4225789Sahrens 	/*
4226789Sahrens 	 * Iterate to convergence.
4227789Sahrens 	 */
4228789Sahrens 	do {
4229789Sahrens 		spa->spa_sync_pass++;
4230789Sahrens 
4231789Sahrens 		spa_sync_config_object(spa, tx);
42325450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
42335450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
42345450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
42355450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
42361544Seschrock 		spa_errlog_sync(spa, txg);
4237789Sahrens 		dsl_pool_sync(dp, txg);
4238789Sahrens 
4239789Sahrens 		dirty_vdevs = 0;
4240789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4241789Sahrens 			vdev_sync(vd, txg);
4242789Sahrens 			dirty_vdevs++;
4243789Sahrens 		}
4244789Sahrens 
4245789Sahrens 		bplist_sync(bpl, tx);
4246789Sahrens 	} while (dirty_vdevs);
4247789Sahrens 
4248789Sahrens 	bplist_close(bpl);
4249789Sahrens 
4250789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4251789Sahrens 
4252789Sahrens 	/*
4253789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4254789Sahrens 	 * to commit the transaction group.
42551635Sbonwick 	 *
42565688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
42575688Sbonwick 	 * random top-level vdevs that are known to be visible in the
42585688Sbonwick 	 * config cache (see spa_vdev_add() for details).  If there *are*
42595688Sbonwick 	 * dirty vdevs -- or if the sync to our random subset fails --
42605688Sbonwick 	 * then sync the uberblock to all vdevs.
4261789Sahrens 	 */
42625688Sbonwick 	if (list_is_empty(&spa->spa_dirty_list)) {
42636615Sgw25295 		vdev_t *svd[SPA_DVAS_PER_BP];
42646615Sgw25295 		int svdcount = 0;
42651635Sbonwick 		int children = rvd->vdev_children;
42661635Sbonwick 		int c0 = spa_get_random(children);
42671635Sbonwick 		int c;
42681635Sbonwick 
42691635Sbonwick 		for (c = 0; c < children; c++) {
42701635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
42715688Sbonwick 			if (vd->vdev_ms_array == 0 || vd->vdev_islog)
42721635Sbonwick 				continue;
42735688Sbonwick 			svd[svdcount++] = vd;
42745688Sbonwick 			if (svdcount == SPA_DVAS_PER_BP)
42751635Sbonwick 				break;
42761635Sbonwick 		}
42776615Sgw25295 		vdev_config_sync(svd, svdcount, txg);
42786615Sgw25295 	} else {
42796615Sgw25295 		vdev_config_sync(rvd->vdev_child, rvd->vdev_children, txg);
42801635Sbonwick 	}
42812082Seschrock 	dmu_tx_commit(tx);
42822082Seschrock 
42831635Sbonwick 	/*
42841635Sbonwick 	 * Clear the dirty config list.
42851635Sbonwick 	 */
42861635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
42871635Sbonwick 		vdev_config_clean(vd);
42881635Sbonwick 
42891635Sbonwick 	/*
42901635Sbonwick 	 * Now that the new config has synced transactionally,
42911635Sbonwick 	 * let it become visible to the config cache.
42921635Sbonwick 	 */
42931635Sbonwick 	if (spa->spa_config_syncing != NULL) {
42941635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
42951635Sbonwick 		spa->spa_config_txg = txg;
42961635Sbonwick 		spa->spa_config_syncing = NULL;
42971635Sbonwick 	}
4298789Sahrens 
4299789Sahrens 	/*
4300789Sahrens 	 * Make a stable copy of the fully synced uberblock.
4301789Sahrens 	 * We use this as the root for pool traversals.
4302789Sahrens 	 */
4303789Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
4304789Sahrens 
4305789Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
4306789Sahrens 
4307789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
4308789Sahrens 	spa->spa_traverse_wanted = 0;
4309789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4310789Sahrens 	rw_exit(&spa->spa_traverse_lock);
4311789Sahrens 
4312789Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
4313789Sahrens 
4314789Sahrens 	/*
4315789Sahrens 	 * Clean up the ZIL records for the synced txg.
4316789Sahrens 	 */
4317789Sahrens 	dsl_pool_zil_clean(dp);
4318789Sahrens 
4319789Sahrens 	/*
4320789Sahrens 	 * Update usable space statistics.
4321789Sahrens 	 */
4322789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4323789Sahrens 		vdev_sync_done(vd, txg);
4324789Sahrens 
4325789Sahrens 	/*
4326789Sahrens 	 * It had better be the case that we didn't dirty anything
43272082Seschrock 	 * since vdev_config_sync().
4328789Sahrens 	 */
4329789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4330789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4331789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4332789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4333789Sahrens 
43341544Seschrock 	spa_config_exit(spa, FTAG);
43351544Seschrock 
43361544Seschrock 	/*
43371544Seschrock 	 * If any async tasks have been requested, kick them off.
43381544Seschrock 	 */
43391544Seschrock 	spa_async_dispatch(spa);
4340789Sahrens }
4341789Sahrens 
4342789Sahrens /*
4343789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4344789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4345789Sahrens  * sync.
4346789Sahrens  */
4347789Sahrens void
4348789Sahrens spa_sync_allpools(void)
4349789Sahrens {
4350789Sahrens 	spa_t *spa = NULL;
4351789Sahrens 	mutex_enter(&spa_namespace_lock);
4352789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4353789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
4354789Sahrens 			continue;
4355789Sahrens 		spa_open_ref(spa, FTAG);
4356789Sahrens 		mutex_exit(&spa_namespace_lock);
4357789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4358789Sahrens 		mutex_enter(&spa_namespace_lock);
4359789Sahrens 		spa_close(spa, FTAG);
4360789Sahrens 	}
4361789Sahrens 	mutex_exit(&spa_namespace_lock);
4362789Sahrens }
4363789Sahrens 
4364789Sahrens /*
4365789Sahrens  * ==========================================================================
4366789Sahrens  * Miscellaneous routines
4367789Sahrens  * ==========================================================================
4368789Sahrens  */
4369789Sahrens 
4370789Sahrens /*
4371789Sahrens  * Remove all pools in the system.
4372789Sahrens  */
4373789Sahrens void
4374789Sahrens spa_evict_all(void)
4375789Sahrens {
4376789Sahrens 	spa_t *spa;
4377789Sahrens 
4378789Sahrens 	/*
4379789Sahrens 	 * Remove all cached state.  All pools should be closed now,
4380789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4381789Sahrens 	 */
4382789Sahrens 	mutex_enter(&spa_namespace_lock);
4383789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4384789Sahrens 		/*
43851544Seschrock 		 * Stop async tasks.  The async thread may need to detach
43861544Seschrock 		 * a device that's been replaced, which requires grabbing
43871544Seschrock 		 * spa_namespace_lock, so we must drop it here.
4388789Sahrens 		 */
4389789Sahrens 		spa_open_ref(spa, FTAG);
4390789Sahrens 		mutex_exit(&spa_namespace_lock);
43911544Seschrock 		spa_async_suspend(spa);
43924808Sek110237 		mutex_enter(&spa_namespace_lock);
4393789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
4394789Sahrens 		spa_close(spa, FTAG);
4395789Sahrens 
4396789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4397789Sahrens 			spa_unload(spa);
4398789Sahrens 			spa_deactivate(spa);
4399789Sahrens 		}
4400789Sahrens 		spa_remove(spa);
4401789Sahrens 	}
4402789Sahrens 	mutex_exit(&spa_namespace_lock);
4403789Sahrens }
44041544Seschrock 
44051544Seschrock vdev_t *
44066643Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
44071544Seschrock {
44086643Seschrock 	vdev_t *vd;
44096643Seschrock 	int i;
44106643Seschrock 
44116643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
44126643Seschrock 		return (vd);
44136643Seschrock 
44146643Seschrock 	if (l2cache) {
44156643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
44166643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
44176643Seschrock 			if (vd->vdev_guid == guid)
44186643Seschrock 				return (vd);
44196643Seschrock 		}
44206643Seschrock 	}
44216643Seschrock 
44226643Seschrock 	return (NULL);
44231544Seschrock }
44241760Seschrock 
44251760Seschrock void
44265094Slling spa_upgrade(spa_t *spa, uint64_t version)
44271760Seschrock {
44281760Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
44291760Seschrock 
44301760Seschrock 	/*
44311760Seschrock 	 * This should only be called for a non-faulted pool, and since a
44321760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
44331760Seschrock 	 * possible.
44341760Seschrock 	 */
44354577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
44365094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
44375094Slling 
44385094Slling 	spa->spa_uberblock.ub_version = version;
44391760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
44401760Seschrock 
44411760Seschrock 	spa_config_exit(spa, FTAG);
44422082Seschrock 
44432082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
44441760Seschrock }
44452082Seschrock 
44462082Seschrock boolean_t
44472082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
44482082Seschrock {
44492082Seschrock 	int i;
44503377Seschrock 	uint64_t spareguid;
44515450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
44525450Sbrendan 
44535450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
44545450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
44552082Seschrock 			return (B_TRUE);
44562082Seschrock 
44575450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
44585450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
44595450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
44603377Seschrock 			return (B_TRUE);
44613377Seschrock 	}
44623377Seschrock 
44632082Seschrock 	return (B_FALSE);
44642082Seschrock }
44653912Slling 
44664451Seschrock /*
44674451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
44684451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
44694451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
44704451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
44714451Seschrock  * or zdb as real changes.
44724451Seschrock  */
44734451Seschrock void
44744451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
44754451Seschrock {
44764451Seschrock #ifdef _KERNEL
44774451Seschrock 	sysevent_t		*ev;
44784451Seschrock 	sysevent_attr_list_t	*attr = NULL;
44794451Seschrock 	sysevent_value_t	value;
44804451Seschrock 	sysevent_id_t		eid;
44814451Seschrock 
44824451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
44834451Seschrock 	    SE_SLEEP);
44844451Seschrock 
44854451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
44864451Seschrock 	value.value.sv_string = spa_name(spa);
44874451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
44884451Seschrock 		goto done;
44894451Seschrock 
44904451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
44914451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
44924451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
44934451Seschrock 		goto done;
44944451Seschrock 
44954451Seschrock 	if (vd) {
44964451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
44974451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
44984451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
44994451Seschrock 		    SE_SLEEP) != 0)
45004451Seschrock 			goto done;
45014451Seschrock 
45024451Seschrock 		if (vd->vdev_path) {
45034451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
45044451Seschrock 			value.value.sv_string = vd->vdev_path;
45054451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
45064451Seschrock 			    &value, SE_SLEEP) != 0)
45074451Seschrock 				goto done;
45084451Seschrock 		}
45094451Seschrock 	}
45104451Seschrock 
45115756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
45125756Seschrock 		goto done;
45135756Seschrock 	attr = NULL;
45145756Seschrock 
45154451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
45164451Seschrock 
45174451Seschrock done:
45184451Seschrock 	if (attr)
45194451Seschrock 		sysevent_free_attr(attr);
45204451Seschrock 	sysevent_free(ev);
45214451Seschrock #endif
45224451Seschrock }
4523