xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 11497)
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 /*
2311422SMark.Musante@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens /*
28789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
29789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
30789Sahrens  * pool.
31789Sahrens  */
32789Sahrens 
33789Sahrens #include <sys/zfs_context.h>
341544Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens #include <sys/spa_impl.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/zio_checksum.h>
38789Sahrens #include <sys/dmu.h>
39789Sahrens #include <sys/dmu_tx.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
4210922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
4510594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
46789Sahrens #include <sys/uberblock_impl.h>
47789Sahrens #include <sys/txg.h>
48789Sahrens #include <sys/avl.h>
49789Sahrens #include <sys/dmu_traverse.h>
503912Slling #include <sys/dmu_objset.h>
51789Sahrens #include <sys/unique.h>
52789Sahrens #include <sys/dsl_pool.h>
533912Slling #include <sys/dsl_dataset.h>
54789Sahrens #include <sys/dsl_dir.h>
55789Sahrens #include <sys/dsl_prop.h>
563912Slling #include <sys/dsl_synctask.h>
57789Sahrens #include <sys/fs/zfs.h>
585450Sbrendan #include <sys/arc.h>
59789Sahrens #include <sys/callb.h>
603975Sek110237 #include <sys/systeminfo.h>
616423Sgw25295 #include <sys/spa_boot.h>
629816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h>
63789Sahrens 
648662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
6511173SJonathan.Adams@Sun.COM #include <sys/bootprops.h>
6611173SJonathan.Adams@Sun.COM #include <sys/callb.h>
6711173SJonathan.Adams@Sun.COM #include <sys/cpupart.h>
6811173SJonathan.Adams@Sun.COM #include <sys/pool.h>
6911173SJonathan.Adams@Sun.COM #include <sys/sysdc.h>
708662SJordan.Vaughan@Sun.com #include <sys/zone.h>
718662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
728662SJordan.Vaughan@Sun.com 
735094Slling #include "zfs_prop.h"
745913Sperrin #include "zfs_comutil.h"
755094Slling 
7611173SJonathan.Adams@Sun.COM typedef enum zti_modes {
779515SJonathan.Adams@Sun.COM 	zti_mode_fixed,			/* value is # of threads (min 1) */
789515SJonathan.Adams@Sun.COM 	zti_mode_online_percent,	/* value is % of online CPUs */
7911173SJonathan.Adams@Sun.COM 	zti_mode_batch,			/* cpu-intensive; value is ignored */
8011146SGeorge.Wilson@Sun.COM 	zti_mode_null,			/* don't create a taskq */
819515SJonathan.Adams@Sun.COM 	zti_nmodes
8211173SJonathan.Adams@Sun.COM } zti_modes_t;
832986Sek110237 
8411146SGeorge.Wilson@Sun.COM #define	ZTI_FIX(n)	{ zti_mode_fixed, (n) }
8511146SGeorge.Wilson@Sun.COM #define	ZTI_PCT(n)	{ zti_mode_online_percent, (n) }
8611173SJonathan.Adams@Sun.COM #define	ZTI_BATCH	{ zti_mode_batch, 0 }
8711146SGeorge.Wilson@Sun.COM #define	ZTI_NULL	{ zti_mode_null, 0 }
8811146SGeorge.Wilson@Sun.COM 
8911146SGeorge.Wilson@Sun.COM #define	ZTI_ONE		ZTI_FIX(1)
909515SJonathan.Adams@Sun.COM 
919515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info {
9211146SGeorge.Wilson@Sun.COM 	enum zti_modes zti_mode;
9311146SGeorge.Wilson@Sun.COM 	uint_t zti_value;
949515SJonathan.Adams@Sun.COM } zio_taskq_info_t;
959515SJonathan.Adams@Sun.COM 
969515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
9711173SJonathan.Adams@Sun.COM 	"issue", "issue_high", "intr", "intr_high"
989515SJonathan.Adams@Sun.COM };
999515SJonathan.Adams@Sun.COM 
10011146SGeorge.Wilson@Sun.COM /*
10111146SGeorge.Wilson@Sun.COM  * Define the taskq threads for the following I/O types:
10211146SGeorge.Wilson@Sun.COM  * 	NULL, READ, WRITE, FREE, CLAIM, and IOCTL
10311146SGeorge.Wilson@Sun.COM  */
10411146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
10511146SGeorge.Wilson@Sun.COM 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
10611146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
10711173SJonathan.Adams@Sun.COM 	{ ZTI_FIX(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL },
10811173SJonathan.Adams@Sun.COM 	{ ZTI_BATCH,	ZTI_FIX(5),	ZTI_FIX(8),	ZTI_FIX(5) },
10911146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11011146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11111146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
1129515SJonathan.Adams@Sun.COM };
1139515SJonathan.Adams@Sun.COM 
1145094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
1157214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
11611422SMark.Musante@Sun.COM static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
11711422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
11811422SMark.Musante@Sun.COM     char **ereport);
1195094Slling 
12011173SJonathan.Adams@Sun.COM uint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
12111173SJonathan.Adams@Sun.COM id_t		zio_taskq_psrset_bind = PS_NONE;
12211173SJonathan.Adams@Sun.COM boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
12311173SJonathan.Adams@Sun.COM uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
12411173SJonathan.Adams@Sun.COM 
12511173SJonathan.Adams@Sun.COM boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
12611173SJonathan.Adams@Sun.COM 
12711173SJonathan.Adams@Sun.COM /*
12811173SJonathan.Adams@Sun.COM  * This (illegal) pool name is used when temporarily importing a spa_t in order
12911173SJonathan.Adams@Sun.COM  * to get the vdev stats associated with the imported devices.
13011173SJonathan.Adams@Sun.COM  */
13111173SJonathan.Adams@Sun.COM #define	TRYIMPORT_NAME	"$import"
13211173SJonathan.Adams@Sun.COM 
1335094Slling /*
1345094Slling  * ==========================================================================
1355094Slling  * SPA properties routines
1365094Slling  * ==========================================================================
1375094Slling  */
1385094Slling 
1395094Slling /*
1405094Slling  * Add a (source=src, propname=propval) list to an nvlist.
1415094Slling  */
1425949Slling static void
1435094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
1445094Slling     uint64_t intval, zprop_source_t src)
1455094Slling {
1465094Slling 	const char *propname = zpool_prop_to_name(prop);
1475094Slling 	nvlist_t *propval;
1485949Slling 
1495949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1505949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1515949Slling 
1525949Slling 	if (strval != NULL)
1535949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1545949Slling 	else
1555949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1565949Slling 
1575949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1585094Slling 	nvlist_free(propval);
1595094Slling }
1605094Slling 
1615094Slling /*
1625094Slling  * Get property values from the spa configuration.
1635094Slling  */
1645949Slling static void
1655094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1665094Slling {
1678525SEric.Schrock@Sun.COM 	uint64_t size;
16810956SGeorge.Wilson@Sun.COM 	uint64_t alloc;
1695094Slling 	uint64_t cap, version;
1705094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1716643Seschrock 	spa_config_dirent_t *dp;
1725094Slling 
1737754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1747754SJeff.Bonwick@Sun.COM 
1758525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
17610956SGeorge.Wilson@Sun.COM 		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
17710922SJeff.Bonwick@Sun.COM 		size = metaslab_class_get_space(spa_normal_class(spa));
1788525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1798525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
18010956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
18110956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
18210956SGeorge.Wilson@Sun.COM 		    size - alloc, src);
18310956SGeorge.Wilson@Sun.COM 
18410956SGeorge.Wilson@Sun.COM 		cap = (size == 0) ? 0 : (alloc * 100 / size);
1858525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1868525SEric.Schrock@Sun.COM 
18710922SJeff.Bonwick@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
18810922SJeff.Bonwick@Sun.COM 		    ddt_get_pool_dedup_ratio(spa), src);
18910922SJeff.Bonwick@Sun.COM 
1908525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1918525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1928525SEric.Schrock@Sun.COM 
1938525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1948525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1958525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1968525SEric.Schrock@Sun.COM 		else
1978525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1988525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1998525SEric.Schrock@Sun.COM 	}
2005949Slling 
2015949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2025949Slling 
2035949Slling 	if (spa->spa_root != NULL)
2045949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2055949Slling 		    0, ZPROP_SRC_LOCAL);
2065094Slling 
2076643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
2086643Seschrock 		if (dp->scd_path == NULL) {
2095949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2106643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
2116643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2125949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2136643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2145363Seschrock 		}
2155363Seschrock 	}
2165094Slling }
2175094Slling 
2185094Slling /*
2195094Slling  * Get zpool property values.
2205094Slling  */
2215094Slling int
2225094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2235094Slling {
22410922SJeff.Bonwick@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
2255094Slling 	zap_cursor_t zc;
2265094Slling 	zap_attribute_t za;
2275094Slling 	int err;
2285094Slling 
2295949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2305094Slling 
2317754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2327754SJeff.Bonwick@Sun.COM 
2335094Slling 	/*
2345094Slling 	 * Get properties from the spa config.
2355094Slling 	 */
2365949Slling 	spa_prop_get_config(spa, nvp);
2375094Slling 
2385094Slling 	/* If no pool property object, no more prop to get. */
2395094Slling 	if (spa->spa_pool_props_object == 0) {
2405094Slling 		mutex_exit(&spa->spa_props_lock);
2415094Slling 		return (0);
2425094Slling 	}
2435094Slling 
2445094Slling 	/*
2455094Slling 	 * Get properties from the MOS pool property object.
2465094Slling 	 */
2475094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
2485094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
2495094Slling 	    zap_cursor_advance(&zc)) {
2505094Slling 		uint64_t intval = 0;
2515094Slling 		char *strval = NULL;
2525094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
2535094Slling 		zpool_prop_t prop;
2545094Slling 
2555094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
2565094Slling 			continue;
2575094Slling 
2585094Slling 		switch (za.za_integer_length) {
2595094Slling 		case 8:
2605094Slling 			/* integer property */
2615094Slling 			if (za.za_first_integer !=
2625094Slling 			    zpool_prop_default_numeric(prop))
2635094Slling 				src = ZPROP_SRC_LOCAL;
2645094Slling 
2655094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2665094Slling 				dsl_pool_t *dp;
2675094Slling 				dsl_dataset_t *ds = NULL;
2685094Slling 
2695094Slling 				dp = spa_get_dsl(spa);
2705094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2716689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2726689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2735094Slling 					rw_exit(&dp->dp_config_rwlock);
2745094Slling 					break;
2755094Slling 				}
2765094Slling 
2775094Slling 				strval = kmem_alloc(
2785094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2795094Slling 				    KM_SLEEP);
2805094Slling 				dsl_dataset_name(ds, strval);
2816689Smaybee 				dsl_dataset_rele(ds, FTAG);
2825094Slling 				rw_exit(&dp->dp_config_rwlock);
2835094Slling 			} else {
2845094Slling 				strval = NULL;
2855094Slling 				intval = za.za_first_integer;
2865094Slling 			}
2875094Slling 
2885949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2895094Slling 
2905094Slling 			if (strval != NULL)
2915094Slling 				kmem_free(strval,
2925094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2935094Slling 
2945094Slling 			break;
2955094Slling 
2965094Slling 		case 1:
2975094Slling 			/* string property */
2985094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2995094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
3005094Slling 			    za.za_name, 1, za.za_num_integers, strval);
3015094Slling 			if (err) {
3025094Slling 				kmem_free(strval, za.za_num_integers);
3035094Slling 				break;
3045094Slling 			}
3055949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
3065094Slling 			kmem_free(strval, za.za_num_integers);
3075094Slling 			break;
3085094Slling 
3095094Slling 		default:
3105094Slling 			break;
3115094Slling 		}
3125094Slling 	}
3135094Slling 	zap_cursor_fini(&zc);
3145094Slling 	mutex_exit(&spa->spa_props_lock);
3155094Slling out:
3165094Slling 	if (err && err != ENOENT) {
3175094Slling 		nvlist_free(*nvp);
3185949Slling 		*nvp = NULL;
3195094Slling 		return (err);
3205094Slling 	}
3215094Slling 
3225094Slling 	return (0);
3235094Slling }
3245094Slling 
3255094Slling /*
3265094Slling  * Validate the given pool properties nvlist and modify the list
3275094Slling  * for the property values to be set.
3285094Slling  */
3295094Slling static int
3305094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
3315094Slling {
3325094Slling 	nvpair_t *elem;
3335094Slling 	int error = 0, reset_bootfs = 0;
3345094Slling 	uint64_t objnum;
3355094Slling 
3365094Slling 	elem = NULL;
3375094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3385094Slling 		zpool_prop_t prop;
3395094Slling 		char *propname, *strval;
3405094Slling 		uint64_t intval;
3415094Slling 		objset_t *os;
3425363Seschrock 		char *slash;
3435094Slling 
3445094Slling 		propname = nvpair_name(elem);
3455094Slling 
3465094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
3475094Slling 			return (EINVAL);
3485094Slling 
3495094Slling 		switch (prop) {
3505094Slling 		case ZPOOL_PROP_VERSION:
3515094Slling 			error = nvpair_value_uint64(elem, &intval);
3525094Slling 			if (!error &&
3535094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
3545094Slling 				error = EINVAL;
3555094Slling 			break;
3565094Slling 
3575094Slling 		case ZPOOL_PROP_DELEGATION:
3585094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3597538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3609816SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_AUTOEXPAND:
3615094Slling 			error = nvpair_value_uint64(elem, &intval);
3625094Slling 			if (!error && intval > 1)
3635094Slling 				error = EINVAL;
3645094Slling 			break;
3655094Slling 
3665094Slling 		case ZPOOL_PROP_BOOTFS:
3679630SJeff.Bonwick@Sun.COM 			/*
3689630SJeff.Bonwick@Sun.COM 			 * If the pool version is less than SPA_VERSION_BOOTFS,
3699630SJeff.Bonwick@Sun.COM 			 * or the pool is still being created (version == 0),
3709630SJeff.Bonwick@Sun.COM 			 * the bootfs property cannot be set.
3719630SJeff.Bonwick@Sun.COM 			 */
3725094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3735094Slling 				error = ENOTSUP;
3745094Slling 				break;
3755094Slling 			}
3765094Slling 
3775094Slling 			/*
3787042Sgw25295 			 * Make sure the vdev config is bootable
3795094Slling 			 */
3807042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3815094Slling 				error = ENOTSUP;
3825094Slling 				break;
3835094Slling 			}
3845094Slling 
3855094Slling 			reset_bootfs = 1;
3865094Slling 
3875094Slling 			error = nvpair_value_string(elem, &strval);
3885094Slling 
3895094Slling 			if (!error) {
3907042Sgw25295 				uint64_t compress;
3917042Sgw25295 
3925094Slling 				if (strval == NULL || strval[0] == '\0') {
3935094Slling 					objnum = zpool_prop_default_numeric(
3945094Slling 					    ZPOOL_PROP_BOOTFS);
3955094Slling 					break;
3965094Slling 				}
3975094Slling 
39810298SMatthew.Ahrens@Sun.COM 				if (error = dmu_objset_hold(strval, FTAG, &os))
3995094Slling 					break;
4007042Sgw25295 
40110298SMatthew.Ahrens@Sun.COM 				/* Must be ZPL and not gzip compressed. */
40210298SMatthew.Ahrens@Sun.COM 
40310298SMatthew.Ahrens@Sun.COM 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
40410298SMatthew.Ahrens@Sun.COM 					error = ENOTSUP;
40510298SMatthew.Ahrens@Sun.COM 				} else if ((error = dsl_prop_get_integer(strval,
4067042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
4077042Sgw25295 				    &compress, NULL)) == 0 &&
4087042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
4097042Sgw25295 					error = ENOTSUP;
4107042Sgw25295 				} else {
4117042Sgw25295 					objnum = dmu_objset_id(os);
4127042Sgw25295 				}
41310298SMatthew.Ahrens@Sun.COM 				dmu_objset_rele(os, FTAG);
4145094Slling 			}
4155094Slling 			break;
4167754SJeff.Bonwick@Sun.COM 
4175329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
4185329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
4195329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
4205329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
4215329Sgw25295 				error = EINVAL;
4225329Sgw25295 
4235329Sgw25295 			/*
4245329Sgw25295 			 * This is a special case which only occurs when
4255329Sgw25295 			 * the pool has completely failed. This allows
4265329Sgw25295 			 * the user to change the in-core failmode property
4275329Sgw25295 			 * without syncing it out to disk (I/Os might
4285329Sgw25295 			 * currently be blocked). We do this by returning
4295329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
4305329Sgw25295 			 * into thinking we encountered a property validation
4315329Sgw25295 			 * error.
4325329Sgw25295 			 */
4337754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
4345329Sgw25295 				spa->spa_failmode = intval;
4355329Sgw25295 				error = EIO;
4365329Sgw25295 			}
4375329Sgw25295 			break;
4385363Seschrock 
4395363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4405363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
4415363Seschrock 				break;
4425363Seschrock 
4435363Seschrock 			if (strval[0] == '\0')
4445363Seschrock 				break;
4455363Seschrock 
4465363Seschrock 			if (strcmp(strval, "none") == 0)
4475363Seschrock 				break;
4485363Seschrock 
4495363Seschrock 			if (strval[0] != '/') {
4505363Seschrock 				error = EINVAL;
4515363Seschrock 				break;
4525363Seschrock 			}
4535363Seschrock 
4545363Seschrock 			slash = strrchr(strval, '/');
4555363Seschrock 			ASSERT(slash != NULL);
4565363Seschrock 
4575363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4585363Seschrock 			    strcmp(slash, "/..") == 0)
4595363Seschrock 				error = EINVAL;
4605363Seschrock 			break;
46110922SJeff.Bonwick@Sun.COM 
46210922SJeff.Bonwick@Sun.COM 		case ZPOOL_PROP_DEDUPDITTO:
46310922SJeff.Bonwick@Sun.COM 			if (spa_version(spa) < SPA_VERSION_DEDUP)
46410922SJeff.Bonwick@Sun.COM 				error = ENOTSUP;
46510922SJeff.Bonwick@Sun.COM 			else
46610922SJeff.Bonwick@Sun.COM 				error = nvpair_value_uint64(elem, &intval);
46710922SJeff.Bonwick@Sun.COM 			if (error == 0 &&
46810922SJeff.Bonwick@Sun.COM 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
46910922SJeff.Bonwick@Sun.COM 				error = EINVAL;
47010922SJeff.Bonwick@Sun.COM 			break;
4715094Slling 		}
4725094Slling 
4735094Slling 		if (error)
4745094Slling 			break;
4755094Slling 	}
4765094Slling 
4775094Slling 	if (!error && reset_bootfs) {
4785094Slling 		error = nvlist_remove(props,
4795094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4805094Slling 
4815094Slling 		if (!error) {
4825094Slling 			error = nvlist_add_uint64(props,
4835094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4845094Slling 		}
4855094Slling 	}
4865094Slling 
4875094Slling 	return (error);
4885094Slling }
4895094Slling 
4908525SEric.Schrock@Sun.COM void
4918525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
4928525SEric.Schrock@Sun.COM {
4938525SEric.Schrock@Sun.COM 	char *cachefile;
4948525SEric.Schrock@Sun.COM 	spa_config_dirent_t *dp;
4958525SEric.Schrock@Sun.COM 
4968525SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
4978525SEric.Schrock@Sun.COM 	    &cachefile) != 0)
4988525SEric.Schrock@Sun.COM 		return;
4998525SEric.Schrock@Sun.COM 
5008525SEric.Schrock@Sun.COM 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
5018525SEric.Schrock@Sun.COM 	    KM_SLEEP);
5028525SEric.Schrock@Sun.COM 
5038525SEric.Schrock@Sun.COM 	if (cachefile[0] == '\0')
5048525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(spa_config_path);
5058525SEric.Schrock@Sun.COM 	else if (strcmp(cachefile, "none") == 0)
5068525SEric.Schrock@Sun.COM 		dp->scd_path = NULL;
5078525SEric.Schrock@Sun.COM 	else
5088525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(cachefile);
5098525SEric.Schrock@Sun.COM 
5108525SEric.Schrock@Sun.COM 	list_insert_head(&spa->spa_config_list, dp);
5118525SEric.Schrock@Sun.COM 	if (need_sync)
5128525SEric.Schrock@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
5138525SEric.Schrock@Sun.COM }
5148525SEric.Schrock@Sun.COM 
5155094Slling int
5165094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
5175094Slling {
5185094Slling 	int error;
5198525SEric.Schrock@Sun.COM 	nvpair_t *elem;
5208525SEric.Schrock@Sun.COM 	boolean_t need_sync = B_FALSE;
5218525SEric.Schrock@Sun.COM 	zpool_prop_t prop;
5225094Slling 
5235094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
5245094Slling 		return (error);
5255094Slling 
5268525SEric.Schrock@Sun.COM 	elem = NULL;
5278525SEric.Schrock@Sun.COM 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
5288525SEric.Schrock@Sun.COM 		if ((prop = zpool_name_to_prop(
5298525SEric.Schrock@Sun.COM 		    nvpair_name(elem))) == ZPROP_INVAL)
5308525SEric.Schrock@Sun.COM 			return (EINVAL);
5318525SEric.Schrock@Sun.COM 
5328525SEric.Schrock@Sun.COM 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
5338525SEric.Schrock@Sun.COM 			continue;
5348525SEric.Schrock@Sun.COM 
5358525SEric.Schrock@Sun.COM 		need_sync = B_TRUE;
5368525SEric.Schrock@Sun.COM 		break;
5378525SEric.Schrock@Sun.COM 	}
5388525SEric.Schrock@Sun.COM 
5398525SEric.Schrock@Sun.COM 	if (need_sync)
5408525SEric.Schrock@Sun.COM 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
5418525SEric.Schrock@Sun.COM 		    spa, nvp, 3));
5428525SEric.Schrock@Sun.COM 	else
5438525SEric.Schrock@Sun.COM 		return (0);
5445094Slling }
5455094Slling 
5465094Slling /*
5475094Slling  * If the bootfs property value is dsobj, clear it.
5485094Slling  */
5495094Slling void
5505094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
5515094Slling {
5525094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
5535094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
5545094Slling 		    spa->spa_pool_props_object,
5555094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
5565094Slling 		spa->spa_bootfs = 0;
5575094Slling 	}
5585094Slling }
5595094Slling 
560789Sahrens /*
561789Sahrens  * ==========================================================================
562789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
563789Sahrens  * ==========================================================================
564789Sahrens  */
565789Sahrens 
5661544Seschrock static int
5671544Seschrock spa_error_entry_compare(const void *a, const void *b)
5681544Seschrock {
5691544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
5701544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
5711544Seschrock 	int ret;
5721544Seschrock 
5731544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
5741544Seschrock 	    sizeof (zbookmark_t));
5751544Seschrock 
5761544Seschrock 	if (ret < 0)
5771544Seschrock 		return (-1);
5781544Seschrock 	else if (ret > 0)
5791544Seschrock 		return (1);
5801544Seschrock 	else
5811544Seschrock 		return (0);
5821544Seschrock }
5831544Seschrock 
5841544Seschrock /*
5851544Seschrock  * Utility function which retrieves copies of the current logs and
5861544Seschrock  * re-initializes them in the process.
5871544Seschrock  */
5881544Seschrock void
5891544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
5901544Seschrock {
5911544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
5921544Seschrock 
5931544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
5941544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
5951544Seschrock 
5961544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5971544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5981544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5991544Seschrock 	avl_create(&spa->spa_errlist_last,
6001544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6011544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
6021544Seschrock }
6031544Seschrock 
60411173SJonathan.Adams@Sun.COM static taskq_t *
60511173SJonathan.Adams@Sun.COM spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
60611173SJonathan.Adams@Sun.COM     uint_t value)
60711173SJonathan.Adams@Sun.COM {
60811173SJonathan.Adams@Sun.COM 	uint_t flags = TASKQ_PREPOPULATE;
60911173SJonathan.Adams@Sun.COM 	boolean_t batch = B_FALSE;
61011173SJonathan.Adams@Sun.COM 
61111173SJonathan.Adams@Sun.COM 	switch (mode) {
61211173SJonathan.Adams@Sun.COM 	case zti_mode_null:
61311173SJonathan.Adams@Sun.COM 		return (NULL);		/* no taskq needed */
61411173SJonathan.Adams@Sun.COM 
61511173SJonathan.Adams@Sun.COM 	case zti_mode_fixed:
61611173SJonathan.Adams@Sun.COM 		ASSERT3U(value, >=, 1);
61711173SJonathan.Adams@Sun.COM 		value = MAX(value, 1);
61811173SJonathan.Adams@Sun.COM 		break;
61911173SJonathan.Adams@Sun.COM 
62011173SJonathan.Adams@Sun.COM 	case zti_mode_batch:
62111173SJonathan.Adams@Sun.COM 		batch = B_TRUE;
62211173SJonathan.Adams@Sun.COM 		flags |= TASKQ_THREADS_CPU_PCT;
62311173SJonathan.Adams@Sun.COM 		value = zio_taskq_batch_pct;
62411173SJonathan.Adams@Sun.COM 		break;
62511173SJonathan.Adams@Sun.COM 
62611173SJonathan.Adams@Sun.COM 	case zti_mode_online_percent:
62711173SJonathan.Adams@Sun.COM 		flags |= TASKQ_THREADS_CPU_PCT;
62811173SJonathan.Adams@Sun.COM 		break;
62911173SJonathan.Adams@Sun.COM 
63011173SJonathan.Adams@Sun.COM 	default:
63111173SJonathan.Adams@Sun.COM 		panic("unrecognized mode for %s taskq (%u:%u) in "
63211173SJonathan.Adams@Sun.COM 		    "spa_activate()",
63311173SJonathan.Adams@Sun.COM 		    name, mode, value);
63411173SJonathan.Adams@Sun.COM 		break;
63511173SJonathan.Adams@Sun.COM 	}
63611173SJonathan.Adams@Sun.COM 
63711173SJonathan.Adams@Sun.COM 	if (zio_taskq_sysdc && spa->spa_proc != &p0) {
63811173SJonathan.Adams@Sun.COM 		if (batch)
63911173SJonathan.Adams@Sun.COM 			flags |= TASKQ_DC_BATCH;
64011173SJonathan.Adams@Sun.COM 
64111173SJonathan.Adams@Sun.COM 		return (taskq_create_sysdc(name, value, 50, INT_MAX,
64211173SJonathan.Adams@Sun.COM 		    spa->spa_proc, zio_taskq_basedc, flags));
64311173SJonathan.Adams@Sun.COM 	}
64411173SJonathan.Adams@Sun.COM 	return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
64511173SJonathan.Adams@Sun.COM 	    spa->spa_proc, flags));
64611173SJonathan.Adams@Sun.COM }
64711173SJonathan.Adams@Sun.COM 
64811173SJonathan.Adams@Sun.COM static void
64911173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa_t *spa)
65011173SJonathan.Adams@Sun.COM {
65111173SJonathan.Adams@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
65211173SJonathan.Adams@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
65311173SJonathan.Adams@Sun.COM 			const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
65411173SJonathan.Adams@Sun.COM 			enum zti_modes mode = ztip->zti_mode;
65511173SJonathan.Adams@Sun.COM 			uint_t value = ztip->zti_value;
65611173SJonathan.Adams@Sun.COM 			char name[32];
65711173SJonathan.Adams@Sun.COM 
65811173SJonathan.Adams@Sun.COM 			(void) snprintf(name, sizeof (name),
65911173SJonathan.Adams@Sun.COM 			    "%s_%s", zio_type_name[t], zio_taskq_types[q]);
66011173SJonathan.Adams@Sun.COM 
66111173SJonathan.Adams@Sun.COM 			spa->spa_zio_taskq[t][q] =
66211173SJonathan.Adams@Sun.COM 			    spa_taskq_create(spa, name, mode, value);
66311173SJonathan.Adams@Sun.COM 		}
66411173SJonathan.Adams@Sun.COM 	}
66511173SJonathan.Adams@Sun.COM }
66611173SJonathan.Adams@Sun.COM 
66711173SJonathan.Adams@Sun.COM #ifdef _KERNEL
66811173SJonathan.Adams@Sun.COM static void
66911173SJonathan.Adams@Sun.COM spa_thread(void *arg)
67011173SJonathan.Adams@Sun.COM {
67111173SJonathan.Adams@Sun.COM 	callb_cpr_t cprinfo;
67211173SJonathan.Adams@Sun.COM 
67311173SJonathan.Adams@Sun.COM 	spa_t *spa = arg;
67411173SJonathan.Adams@Sun.COM 	user_t *pu = PTOU(curproc);
67511173SJonathan.Adams@Sun.COM 
67611173SJonathan.Adams@Sun.COM 	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
67711173SJonathan.Adams@Sun.COM 	    spa->spa_name);
67811173SJonathan.Adams@Sun.COM 
67911173SJonathan.Adams@Sun.COM 	ASSERT(curproc != &p0);
68011173SJonathan.Adams@Sun.COM 	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
68111173SJonathan.Adams@Sun.COM 	    "zpool-%s", spa->spa_name);
68211173SJonathan.Adams@Sun.COM 	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
68311173SJonathan.Adams@Sun.COM 
68411173SJonathan.Adams@Sun.COM 	/* bind this thread to the requested psrset */
68511173SJonathan.Adams@Sun.COM 	if (zio_taskq_psrset_bind != PS_NONE) {
68611173SJonathan.Adams@Sun.COM 		pool_lock();
68711173SJonathan.Adams@Sun.COM 		mutex_enter(&cpu_lock);
68811173SJonathan.Adams@Sun.COM 		mutex_enter(&pidlock);
68911173SJonathan.Adams@Sun.COM 		mutex_enter(&curproc->p_lock);
69011173SJonathan.Adams@Sun.COM 
69111173SJonathan.Adams@Sun.COM 		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
69211173SJonathan.Adams@Sun.COM 		    0, NULL, NULL) == 0)  {
69311173SJonathan.Adams@Sun.COM 			curthread->t_bind_pset = zio_taskq_psrset_bind;
69411173SJonathan.Adams@Sun.COM 		} else {
69511173SJonathan.Adams@Sun.COM 			cmn_err(CE_WARN,
69611173SJonathan.Adams@Sun.COM 			    "Couldn't bind process for zfs pool \"%s\" to "
69711173SJonathan.Adams@Sun.COM 			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
69811173SJonathan.Adams@Sun.COM 		}
69911173SJonathan.Adams@Sun.COM 
70011173SJonathan.Adams@Sun.COM 		mutex_exit(&curproc->p_lock);
70111173SJonathan.Adams@Sun.COM 		mutex_exit(&pidlock);
70211173SJonathan.Adams@Sun.COM 		mutex_exit(&cpu_lock);
70311173SJonathan.Adams@Sun.COM 		pool_unlock();
70411173SJonathan.Adams@Sun.COM 	}
70511173SJonathan.Adams@Sun.COM 
70611173SJonathan.Adams@Sun.COM 	if (zio_taskq_sysdc) {
70711173SJonathan.Adams@Sun.COM 		sysdc_thread_enter(curthread, 100, 0);
70811173SJonathan.Adams@Sun.COM 	}
70911173SJonathan.Adams@Sun.COM 
71011173SJonathan.Adams@Sun.COM 	spa->spa_proc = curproc;
71111173SJonathan.Adams@Sun.COM 	spa->spa_did = curthread->t_did;
71211173SJonathan.Adams@Sun.COM 
71311173SJonathan.Adams@Sun.COM 	spa_create_zio_taskqs(spa);
71411173SJonathan.Adams@Sun.COM 
71511173SJonathan.Adams@Sun.COM 	mutex_enter(&spa->spa_proc_lock);
71611173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
71711173SJonathan.Adams@Sun.COM 
71811173SJonathan.Adams@Sun.COM 	spa->spa_proc_state = SPA_PROC_ACTIVE;
71911173SJonathan.Adams@Sun.COM 	cv_broadcast(&spa->spa_proc_cv);
72011173SJonathan.Adams@Sun.COM 
72111173SJonathan.Adams@Sun.COM 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
72211173SJonathan.Adams@Sun.COM 	while (spa->spa_proc_state == SPA_PROC_ACTIVE)
72311173SJonathan.Adams@Sun.COM 		cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
72411173SJonathan.Adams@Sun.COM 	CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
72511173SJonathan.Adams@Sun.COM 
72611173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
72711173SJonathan.Adams@Sun.COM 	spa->spa_proc_state = SPA_PROC_GONE;
72811173SJonathan.Adams@Sun.COM 	spa->spa_proc = &p0;
72911173SJonathan.Adams@Sun.COM 	cv_broadcast(&spa->spa_proc_cv);
73011173SJonathan.Adams@Sun.COM 	CALLB_CPR_EXIT(&cprinfo);	/* drops spa_proc_lock */
73111173SJonathan.Adams@Sun.COM 
73211173SJonathan.Adams@Sun.COM 	mutex_enter(&curproc->p_lock);
73311173SJonathan.Adams@Sun.COM 	lwp_exit();
73411173SJonathan.Adams@Sun.COM }
73511173SJonathan.Adams@Sun.COM #endif
73611173SJonathan.Adams@Sun.COM 
737789Sahrens /*
738789Sahrens  * Activate an uninitialized pool.
739789Sahrens  */
740789Sahrens static void
7418241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode)
742789Sahrens {
743789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
744789Sahrens 
745789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
7468241SJeff.Bonwick@Sun.COM 	spa->spa_mode = mode;
747789Sahrens 
74810594SGeorge.Wilson@Sun.COM 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
74910594SGeorge.Wilson@Sun.COM 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
750789Sahrens 
75111173SJonathan.Adams@Sun.COM 	/* Try to create a covering process */
75211173SJonathan.Adams@Sun.COM 	mutex_enter(&spa->spa_proc_lock);
75311173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
75411173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc == &p0);
75511173SJonathan.Adams@Sun.COM 	spa->spa_did = 0;
75611173SJonathan.Adams@Sun.COM 
75711173SJonathan.Adams@Sun.COM 	/* Only create a process if we're going to be around a while. */
75811173SJonathan.Adams@Sun.COM 	if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
75911173SJonathan.Adams@Sun.COM 		if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
76011173SJonathan.Adams@Sun.COM 		    NULL, 0) == 0) {
76111173SJonathan.Adams@Sun.COM 			spa->spa_proc_state = SPA_PROC_CREATED;
76211173SJonathan.Adams@Sun.COM 			while (spa->spa_proc_state == SPA_PROC_CREATED) {
76311173SJonathan.Adams@Sun.COM 				cv_wait(&spa->spa_proc_cv,
76411173SJonathan.Adams@Sun.COM 				    &spa->spa_proc_lock);
7659515SJonathan.Adams@Sun.COM 			}
76611173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
76711173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_proc != &p0);
76811173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_did != 0);
76911173SJonathan.Adams@Sun.COM 		} else {
77011173SJonathan.Adams@Sun.COM #ifdef _KERNEL
77111173SJonathan.Adams@Sun.COM 			cmn_err(CE_WARN,
77211173SJonathan.Adams@Sun.COM 			    "Couldn't create process for zfs pool \"%s\"\n",
77311173SJonathan.Adams@Sun.COM 			    spa->spa_name);
77411173SJonathan.Adams@Sun.COM #endif
7757754SJeff.Bonwick@Sun.COM 		}
776789Sahrens 	}
77711173SJonathan.Adams@Sun.COM 	mutex_exit(&spa->spa_proc_lock);
77811173SJonathan.Adams@Sun.COM 
77911173SJonathan.Adams@Sun.COM 	/* If we didn't create a process, we need to create our taskqs. */
78011173SJonathan.Adams@Sun.COM 	if (spa->spa_proc == &p0) {
78111173SJonathan.Adams@Sun.COM 		spa_create_zio_taskqs(spa);
78211173SJonathan.Adams@Sun.COM 	}
783789Sahrens 
7847754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
7857754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
7867754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
7877754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
788789Sahrens 
789789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
790789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
7911544Seschrock 
7921544Seschrock 	avl_create(&spa->spa_errlist_scrub,
7931544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
7941544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
7951544Seschrock 	avl_create(&spa->spa_errlist_last,
7961544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
7971544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
798789Sahrens }
799789Sahrens 
800789Sahrens /*
801789Sahrens  * Opposite of spa_activate().
802789Sahrens  */
803789Sahrens static void
804789Sahrens spa_deactivate(spa_t *spa)
805789Sahrens {
806789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
807789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
808789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
8099630SJeff.Bonwick@Sun.COM 	ASSERT(spa->spa_async_zio_root == NULL);
810789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
811789Sahrens 
812789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
813789Sahrens 
8147754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
8157754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
8167754SJeff.Bonwick@Sun.COM 
8177754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
8187754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
81911146SGeorge.Wilson@Sun.COM 			if (spa->spa_zio_taskq[t][q] != NULL)
82011146SGeorge.Wilson@Sun.COM 				taskq_destroy(spa->spa_zio_taskq[t][q]);
8217754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
8227754SJeff.Bonwick@Sun.COM 		}
823789Sahrens 	}
824789Sahrens 
825789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
826789Sahrens 	spa->spa_normal_class = NULL;
827789Sahrens 
8284527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
8294527Sperrin 	spa->spa_log_class = NULL;
8304527Sperrin 
8311544Seschrock 	/*
8321544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
8331544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
8341544Seschrock 	 */
8351544Seschrock 	spa_errlog_drain(spa);
8361544Seschrock 
8371544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
8381544Seschrock 	avl_destroy(&spa->spa_errlist_last);
8391544Seschrock 
840789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
84111173SJonathan.Adams@Sun.COM 
84211173SJonathan.Adams@Sun.COM 	mutex_enter(&spa->spa_proc_lock);
84311173SJonathan.Adams@Sun.COM 	if (spa->spa_proc_state != SPA_PROC_NONE) {
84411173SJonathan.Adams@Sun.COM 		ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
84511173SJonathan.Adams@Sun.COM 		spa->spa_proc_state = SPA_PROC_DEACTIVATE;
84611173SJonathan.Adams@Sun.COM 		cv_broadcast(&spa->spa_proc_cv);
84711173SJonathan.Adams@Sun.COM 		while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
84811173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_proc != &p0);
84911173SJonathan.Adams@Sun.COM 			cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
85011173SJonathan.Adams@Sun.COM 		}
85111173SJonathan.Adams@Sun.COM 		ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
85211173SJonathan.Adams@Sun.COM 		spa->spa_proc_state = SPA_PROC_NONE;
85311173SJonathan.Adams@Sun.COM 	}
85411173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc == &p0);
85511173SJonathan.Adams@Sun.COM 	mutex_exit(&spa->spa_proc_lock);
85611173SJonathan.Adams@Sun.COM 
85711173SJonathan.Adams@Sun.COM 	/*
85811173SJonathan.Adams@Sun.COM 	 * We want to make sure spa_thread() has actually exited the ZFS
85911173SJonathan.Adams@Sun.COM 	 * module, so that the module can't be unloaded out from underneath
86011173SJonathan.Adams@Sun.COM 	 * it.
86111173SJonathan.Adams@Sun.COM 	 */
86211173SJonathan.Adams@Sun.COM 	if (spa->spa_did != 0) {
86311173SJonathan.Adams@Sun.COM 		thread_join(spa->spa_did);
86411173SJonathan.Adams@Sun.COM 		spa->spa_did = 0;
86511173SJonathan.Adams@Sun.COM 	}
866789Sahrens }
867789Sahrens 
868789Sahrens /*
869789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
870789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
871789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
872789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
873789Sahrens  */
8742082Seschrock static int
8752082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
8762082Seschrock     uint_t id, int atype)
877789Sahrens {
878789Sahrens 	nvlist_t **child;
8799816SGeorge.Wilson@Sun.COM 	uint_t children;
8802082Seschrock 	int error;
8812082Seschrock 
8822082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
8832082Seschrock 		return (error);
8842082Seschrock 
8852082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
8862082Seschrock 		return (0);
887789Sahrens 
8887754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
8897754SJeff.Bonwick@Sun.COM 	    &child, &children);
8907754SJeff.Bonwick@Sun.COM 
8917754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
8927754SJeff.Bonwick@Sun.COM 		return (0);
8937754SJeff.Bonwick@Sun.COM 
8947754SJeff.Bonwick@Sun.COM 	if (error) {
8952082Seschrock 		vdev_free(*vdp);
8962082Seschrock 		*vdp = NULL;
8972082Seschrock 		return (EINVAL);
898789Sahrens 	}
899789Sahrens 
9009816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < children; c++) {
9012082Seschrock 		vdev_t *vd;
9022082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
9032082Seschrock 		    atype)) != 0) {
9042082Seschrock 			vdev_free(*vdp);
9052082Seschrock 			*vdp = NULL;
9062082Seschrock 			return (error);
907789Sahrens 		}
908789Sahrens 	}
909789Sahrens 
9102082Seschrock 	ASSERT(*vdp != NULL);
9112082Seschrock 
9122082Seschrock 	return (0);
913789Sahrens }
914789Sahrens 
915789Sahrens /*
916789Sahrens  * Opposite of spa_load().
917789Sahrens  */
918789Sahrens static void
919789Sahrens spa_unload(spa_t *spa)
920789Sahrens {
9212082Seschrock 	int i;
9222082Seschrock 
9237754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
9247754SJeff.Bonwick@Sun.COM 
925789Sahrens 	/*
9261544Seschrock 	 * Stop async tasks.
9271544Seschrock 	 */
9281544Seschrock 	spa_async_suspend(spa);
9291544Seschrock 
9301544Seschrock 	/*
931789Sahrens 	 * Stop syncing.
932789Sahrens 	 */
933789Sahrens 	if (spa->spa_sync_on) {
934789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
935789Sahrens 		spa->spa_sync_on = B_FALSE;
936789Sahrens 	}
937789Sahrens 
938789Sahrens 	/*
9397754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
940789Sahrens 	 */
9419234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root != NULL) {
9429234SGeorge.Wilson@Sun.COM 		(void) zio_wait(spa->spa_async_zio_root);
9439234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = NULL;
9449234SGeorge.Wilson@Sun.COM 	}
945789Sahrens 
946789Sahrens 	/*
947789Sahrens 	 * Close the dsl pool.
948789Sahrens 	 */
949789Sahrens 	if (spa->spa_dsl_pool) {
950789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
951789Sahrens 		spa->spa_dsl_pool = NULL;
952789Sahrens 	}
953789Sahrens 
95410922SJeff.Bonwick@Sun.COM 	ddt_unload(spa);
95510922SJeff.Bonwick@Sun.COM 
9568241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9578241SJeff.Bonwick@Sun.COM 
9588241SJeff.Bonwick@Sun.COM 	/*
9598241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
9608241SJeff.Bonwick@Sun.COM 	 */
9618241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
9628241SJeff.Bonwick@Sun.COM 
963789Sahrens 	/*
964789Sahrens 	 * Close all vdevs.
965789Sahrens 	 */
9661585Sbonwick 	if (spa->spa_root_vdev)
967789Sahrens 		vdev_free(spa->spa_root_vdev);
9681585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
9691544Seschrock 
9705450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9715450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
9725450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
9735450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
9745450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
9755450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
9765450Sbrendan 	}
9775450Sbrendan 	if (spa->spa_spares.sav_config) {
9785450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
9795450Sbrendan 		spa->spa_spares.sav_config = NULL;
9802082Seschrock 	}
9817377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
9825450Sbrendan 
9835450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
9845450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
9855450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
9865450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
9875450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
9885450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
9895450Sbrendan 	}
9905450Sbrendan 	if (spa->spa_l2cache.sav_config) {
9915450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
9925450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
9932082Seschrock 	}
9947377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
9952082Seschrock 
9961544Seschrock 	spa->spa_async_suspended = 0;
9978241SJeff.Bonwick@Sun.COM 
9988241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
999789Sahrens }
1000789Sahrens 
1001789Sahrens /*
10022082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
10032082Seschrock  * this pool.  When this is called, we have some form of basic information in
10045450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
10055450Sbrendan  * then re-generate a more complete list including status information.
10062082Seschrock  */
10072082Seschrock static void
10082082Seschrock spa_load_spares(spa_t *spa)
10092082Seschrock {
10102082Seschrock 	nvlist_t **spares;
10112082Seschrock 	uint_t nspares;
10122082Seschrock 	int i;
10133377Seschrock 	vdev_t *vd, *tvd;
10142082Seschrock 
10157754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
10167754SJeff.Bonwick@Sun.COM 
10172082Seschrock 	/*
10182082Seschrock 	 * First, close and free any existing spare vdevs.
10192082Seschrock 	 */
10205450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10215450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
10223377Seschrock 
10233377Seschrock 		/* Undo the call to spa_activate() below */
10246643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10256643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
10263377Seschrock 			spa_spare_remove(tvd);
10273377Seschrock 		vdev_close(vd);
10283377Seschrock 		vdev_free(vd);
10292082Seschrock 	}
10303377Seschrock 
10315450Sbrendan 	if (spa->spa_spares.sav_vdevs)
10325450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
10335450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
10345450Sbrendan 
10355450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
10362082Seschrock 		nspares = 0;
10372082Seschrock 	else
10385450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
10392082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
10402082Seschrock 
10415450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
10425450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
10432082Seschrock 
10442082Seschrock 	if (nspares == 0)
10452082Seschrock 		return;
10462082Seschrock 
10472082Seschrock 	/*
10482082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
10493377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
10503377Seschrock 	 * structures associated with it: one in the list of spares (used only
10513377Seschrock 	 * for basic validation purposes) and one in the active vdev
10523377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
10533377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
10543377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
10552082Seschrock 	 */
10565450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
10575450Sbrendan 	    KM_SLEEP);
10585450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10592082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
10602082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
10612082Seschrock 		ASSERT(vd != NULL);
10622082Seschrock 
10635450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
10642082Seschrock 
10656643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10666643Seschrock 		    B_FALSE)) != NULL) {
10673377Seschrock 			if (!tvd->vdev_isspare)
10683377Seschrock 				spa_spare_add(tvd);
10693377Seschrock 
10703377Seschrock 			/*
10713377Seschrock 			 * We only mark the spare active if we were successfully
10723377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
10733377Seschrock 			 * with a bad active spare would result in strange
10743377Seschrock 			 * behavior, because multiple pool would think the spare
10753377Seschrock 			 * is actively in use.
10763377Seschrock 			 *
10773377Seschrock 			 * There is a vulnerability here to an equally bizarre
10783377Seschrock 			 * circumstance, where a dead active spare is later
10793377Seschrock 			 * brought back to life (onlined or otherwise).  Given
10803377Seschrock 			 * the rarity of this scenario, and the extra complexity
10813377Seschrock 			 * it adds, we ignore the possibility.
10823377Seschrock 			 */
10833377Seschrock 			if (!vdev_is_dead(tvd))
10843377Seschrock 				spa_spare_activate(tvd);
10853377Seschrock 		}
10863377Seschrock 
10877754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
10889425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
10897754SJeff.Bonwick@Sun.COM 
10902082Seschrock 		if (vdev_open(vd) != 0)
10912082Seschrock 			continue;
10922082Seschrock 
10935450Sbrendan 		if (vdev_validate_aux(vd) == 0)
10945450Sbrendan 			spa_spare_add(vd);
10952082Seschrock 	}
10962082Seschrock 
10972082Seschrock 	/*
10982082Seschrock 	 * Recompute the stashed list of spares, with status information
10992082Seschrock 	 * this time.
11002082Seschrock 	 */
11015450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
11022082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
11032082Seschrock 
11045450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
11055450Sbrendan 	    KM_SLEEP);
11065450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11075450Sbrendan 		spares[i] = vdev_config_generate(spa,
11085450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
11095450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
11105450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
11115450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11122082Seschrock 		nvlist_free(spares[i]);
11135450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
11145450Sbrendan }
11155450Sbrendan 
11165450Sbrendan /*
11175450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
11185450Sbrendan  * this pool.  When this is called, we have some form of basic information in
11195450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
11205450Sbrendan  * then re-generate a more complete list including status information.
11215450Sbrendan  * Devices which are already active have their details maintained, and are
11225450Sbrendan  * not re-opened.
11235450Sbrendan  */
11245450Sbrendan static void
11255450Sbrendan spa_load_l2cache(spa_t *spa)
11265450Sbrendan {
11275450Sbrendan 	nvlist_t **l2cache;
11285450Sbrendan 	uint_t nl2cache;
11295450Sbrendan 	int i, j, oldnvdevs;
11309816SGeorge.Wilson@Sun.COM 	uint64_t guid;
11315450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
11325450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
11335450Sbrendan 
11347754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
11357754SJeff.Bonwick@Sun.COM 
11365450Sbrendan 	if (sav->sav_config != NULL) {
11375450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
11385450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
11395450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
11405450Sbrendan 	} else {
11415450Sbrendan 		nl2cache = 0;
11425450Sbrendan 	}
11435450Sbrendan 
11445450Sbrendan 	oldvdevs = sav->sav_vdevs;
11455450Sbrendan 	oldnvdevs = sav->sav_count;
11465450Sbrendan 	sav->sav_vdevs = NULL;
11475450Sbrendan 	sav->sav_count = 0;
11485450Sbrendan 
11495450Sbrendan 	/*
11505450Sbrendan 	 * Process new nvlist of vdevs.
11515450Sbrendan 	 */
11525450Sbrendan 	for (i = 0; i < nl2cache; i++) {
11535450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
11545450Sbrendan 		    &guid) == 0);
11555450Sbrendan 
11565450Sbrendan 		newvdevs[i] = NULL;
11575450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
11585450Sbrendan 			vd = oldvdevs[j];
11595450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
11605450Sbrendan 				/*
11615450Sbrendan 				 * Retain previous vdev for add/remove ops.
11625450Sbrendan 				 */
11635450Sbrendan 				newvdevs[i] = vd;
11645450Sbrendan 				oldvdevs[j] = NULL;
11655450Sbrendan 				break;
11665450Sbrendan 			}
11675450Sbrendan 		}
11685450Sbrendan 
11695450Sbrendan 		if (newvdevs[i] == NULL) {
11705450Sbrendan 			/*
11715450Sbrendan 			 * Create new vdev
11725450Sbrendan 			 */
11735450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
11745450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
11755450Sbrendan 			ASSERT(vd != NULL);
11765450Sbrendan 			newvdevs[i] = vd;
11775450Sbrendan 
11785450Sbrendan 			/*
11795450Sbrendan 			 * Commit this vdev as an l2cache device,
11805450Sbrendan 			 * even if it fails to open.
11815450Sbrendan 			 */
11825450Sbrendan 			spa_l2cache_add(vd);
11835450Sbrendan 
11846643Seschrock 			vd->vdev_top = vd;
11856643Seschrock 			vd->vdev_aux = sav;
11866643Seschrock 
11876643Seschrock 			spa_l2cache_activate(vd);
11886643Seschrock 
11895450Sbrendan 			if (vdev_open(vd) != 0)
11905450Sbrendan 				continue;
11915450Sbrendan 
11925450Sbrendan 			(void) vdev_validate_aux(vd);
11935450Sbrendan 
11949816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
11959816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
11965450Sbrendan 		}
11975450Sbrendan 	}
11985450Sbrendan 
11995450Sbrendan 	/*
12005450Sbrendan 	 * Purge vdevs that were dropped
12015450Sbrendan 	 */
12025450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
12035450Sbrendan 		uint64_t pool;
12045450Sbrendan 
12055450Sbrendan 		vd = oldvdevs[i];
12065450Sbrendan 		if (vd != NULL) {
12078241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
12088241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
12095450Sbrendan 				l2arc_remove_vdev(vd);
12105450Sbrendan 			(void) vdev_close(vd);
12115450Sbrendan 			spa_l2cache_remove(vd);
12125450Sbrendan 		}
12135450Sbrendan 	}
12145450Sbrendan 
12155450Sbrendan 	if (oldvdevs)
12165450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
12175450Sbrendan 
12185450Sbrendan 	if (sav->sav_config == NULL)
12195450Sbrendan 		goto out;
12205450Sbrendan 
12215450Sbrendan 	sav->sav_vdevs = newvdevs;
12225450Sbrendan 	sav->sav_count = (int)nl2cache;
12235450Sbrendan 
12245450Sbrendan 	/*
12255450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
12265450Sbrendan 	 * information this time.
12275450Sbrendan 	 */
12285450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
12295450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
12305450Sbrendan 
12315450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
12325450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12335450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
12345450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
12355450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
12365450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
12375450Sbrendan out:
12385450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12395450Sbrendan 		nvlist_free(l2cache[i]);
12405450Sbrendan 	if (sav->sav_count)
12415450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
12422082Seschrock }
12432082Seschrock 
12442082Seschrock static int
12452082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
12462082Seschrock {
12472082Seschrock 	dmu_buf_t *db;
12482082Seschrock 	char *packed = NULL;
12492082Seschrock 	size_t nvsize = 0;
12502082Seschrock 	int error;
12512082Seschrock 	*value = NULL;
12522082Seschrock 
12532082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
12542082Seschrock 	nvsize = *(uint64_t *)db->db_data;
12552082Seschrock 	dmu_buf_rele(db, FTAG);
12562082Seschrock 
12572082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
12589512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
12599512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
12602082Seschrock 	if (error == 0)
12612082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
12622082Seschrock 	kmem_free(packed, nvsize);
12632082Seschrock 
12642082Seschrock 	return (error);
12652082Seschrock }
12662082Seschrock 
12672082Seschrock /*
12684451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
12694451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
12704451Seschrock  */
12714451Seschrock static void
12724451Seschrock spa_check_removed(vdev_t *vd)
12734451Seschrock {
12749816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12754451Seschrock 		spa_check_removed(vd->vdev_child[c]);
12764451Seschrock 
12774451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
12784451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
12794451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
12804451Seschrock 	}
12814451Seschrock }
12824451Seschrock 
12834451Seschrock /*
12849701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
12859701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
12869701SGeorge.Wilson@Sun.COM  */
12879701SGeorge.Wilson@Sun.COM void
128810594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
12899701SGeorge.Wilson@Sun.COM {
129010594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
129110594SGeorge.Wilson@Sun.COM 
129210594SGeorge.Wilson@Sun.COM 	/*
129310594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
129410594SGeorge.Wilson@Sun.COM 	 */
129510594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
129610594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
129710594SGeorge.Wilson@Sun.COM 
129810594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
129910594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
130010594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
130110594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
13029701SGeorge.Wilson@Sun.COM 	}
130310594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
130410594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13059701SGeorge.Wilson@Sun.COM }
13069701SGeorge.Wilson@Sun.COM 
13079701SGeorge.Wilson@Sun.COM /*
13087294Sperrin  * Check for missing log devices
13097294Sperrin  */
13107294Sperrin int
13117294Sperrin spa_check_logs(spa_t *spa)
13127294Sperrin {
13137294Sperrin 	switch (spa->spa_log_state) {
13147294Sperrin 	case SPA_LOG_MISSING:
13157294Sperrin 		/* need to recheck in case slog has been restored */
13167294Sperrin 	case SPA_LOG_UNKNOWN:
13177294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
13187294Sperrin 		    DS_FIND_CHILDREN)) {
131911422SMark.Musante@Sun.COM 			spa_set_log_state(spa, SPA_LOG_MISSING);
13207294Sperrin 			return (1);
13217294Sperrin 		}
13227294Sperrin 		break;
13237294Sperrin 	}
13247294Sperrin 	return (0);
13257294Sperrin }
13267294Sperrin 
132711422SMark.Musante@Sun.COM static boolean_t
132811422SMark.Musante@Sun.COM spa_passivate_log(spa_t *spa)
132911422SMark.Musante@Sun.COM {
133011422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
133111422SMark.Musante@Sun.COM 	boolean_t slog_found = B_FALSE;
133211422SMark.Musante@Sun.COM 
133311422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
133411422SMark.Musante@Sun.COM 
133511422SMark.Musante@Sun.COM 	if (!spa_has_slogs(spa))
133611422SMark.Musante@Sun.COM 		return (B_FALSE);
133711422SMark.Musante@Sun.COM 
133811422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
133911422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
134011422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
134111422SMark.Musante@Sun.COM 
134211422SMark.Musante@Sun.COM 		if (tvd->vdev_islog) {
134311422SMark.Musante@Sun.COM 			metaslab_group_passivate(mg);
134411422SMark.Musante@Sun.COM 			slog_found = B_TRUE;
134511422SMark.Musante@Sun.COM 		}
134611422SMark.Musante@Sun.COM 	}
134711422SMark.Musante@Sun.COM 
134811422SMark.Musante@Sun.COM 	return (slog_found);
134911422SMark.Musante@Sun.COM }
135011422SMark.Musante@Sun.COM 
135111422SMark.Musante@Sun.COM static void
135211422SMark.Musante@Sun.COM spa_activate_log(spa_t *spa)
135311422SMark.Musante@Sun.COM {
135411422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
135511422SMark.Musante@Sun.COM 
135611422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
135711422SMark.Musante@Sun.COM 
135811422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
135911422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
136011422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
136111422SMark.Musante@Sun.COM 
136211422SMark.Musante@Sun.COM 		if (tvd->vdev_islog)
136311422SMark.Musante@Sun.COM 			metaslab_group_activate(mg);
136411422SMark.Musante@Sun.COM 	}
136511422SMark.Musante@Sun.COM }
136611422SMark.Musante@Sun.COM 
136711422SMark.Musante@Sun.COM int
136811422SMark.Musante@Sun.COM spa_offline_log(spa_t *spa)
136911422SMark.Musante@Sun.COM {
137011422SMark.Musante@Sun.COM 	int error = 0;
137111422SMark.Musante@Sun.COM 
137211422SMark.Musante@Sun.COM 	if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
137311422SMark.Musante@Sun.COM 	    NULL, DS_FIND_CHILDREN)) == 0) {
137411422SMark.Musante@Sun.COM 
137511422SMark.Musante@Sun.COM 		/*
137611422SMark.Musante@Sun.COM 		 * We successfully offlined the log device, sync out the
137711422SMark.Musante@Sun.COM 		 * current txg so that the "stubby" block can be removed
137811422SMark.Musante@Sun.COM 		 * by zil_sync().
137911422SMark.Musante@Sun.COM 		 */
138011422SMark.Musante@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, 0);
138111422SMark.Musante@Sun.COM 	}
138211422SMark.Musante@Sun.COM 	return (error);
138311422SMark.Musante@Sun.COM }
138411422SMark.Musante@Sun.COM 
138510672SEric.Schrock@Sun.COM static void
138610672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
138710672SEric.Schrock@Sun.COM {
138810922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < sav->sav_count; i++)
138910672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
139010672SEric.Schrock@Sun.COM }
139110672SEric.Schrock@Sun.COM 
139210922SJeff.Bonwick@Sun.COM void
139310922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio)
139410922SJeff.Bonwick@Sun.COM {
139510922SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
139610922SJeff.Bonwick@Sun.COM 
139710922SJeff.Bonwick@Sun.COM 	if (zio->io_error)
139810922SJeff.Bonwick@Sun.COM 		return;
139910922SJeff.Bonwick@Sun.COM 
140010922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
140110922SJeff.Bonwick@Sun.COM 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
140210922SJeff.Bonwick@Sun.COM 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
140310922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
140410922SJeff.Bonwick@Sun.COM }
140510922SJeff.Bonwick@Sun.COM 
140610921STim.Haley@Sun.COM typedef struct spa_load_error {
140710921STim.Haley@Sun.COM 	uint64_t	sle_metadata_count;
140810921STim.Haley@Sun.COM 	uint64_t	sle_data_count;
140910921STim.Haley@Sun.COM } spa_load_error_t;
141010921STim.Haley@Sun.COM 
141110921STim.Haley@Sun.COM static void
141210921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio)
141310921STim.Haley@Sun.COM {
141410921STim.Haley@Sun.COM 	blkptr_t *bp = zio->io_bp;
141510921STim.Haley@Sun.COM 	spa_load_error_t *sle = zio->io_private;
141610921STim.Haley@Sun.COM 	dmu_object_type_t type = BP_GET_TYPE(bp);
141710921STim.Haley@Sun.COM 	int error = zio->io_error;
141810921STim.Haley@Sun.COM 
141910921STim.Haley@Sun.COM 	if (error) {
142010921STim.Haley@Sun.COM 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
142110921STim.Haley@Sun.COM 		    type != DMU_OT_INTENT_LOG)
142210921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_metadata_count, 1);
142310921STim.Haley@Sun.COM 		else
142410921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_data_count, 1);
142510921STim.Haley@Sun.COM 	}
142610921STim.Haley@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
142710921STim.Haley@Sun.COM }
142810921STim.Haley@Sun.COM 
142910921STim.Haley@Sun.COM /*ARGSUSED*/
143010921STim.Haley@Sun.COM static int
143110922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
143210922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
143310921STim.Haley@Sun.COM {
143410921STim.Haley@Sun.COM 	if (bp != NULL) {
143510921STim.Haley@Sun.COM 		zio_t *rio = arg;
143610921STim.Haley@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
143710921STim.Haley@Sun.COM 		void *data = zio_data_buf_alloc(size);
143810921STim.Haley@Sun.COM 
143910921STim.Haley@Sun.COM 		zio_nowait(zio_read(rio, spa, bp, data, size,
144010921STim.Haley@Sun.COM 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
144110921STim.Haley@Sun.COM 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
144210921STim.Haley@Sun.COM 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
144310921STim.Haley@Sun.COM 	}
144410921STim.Haley@Sun.COM 	return (0);
144510921STim.Haley@Sun.COM }
144610921STim.Haley@Sun.COM 
144710921STim.Haley@Sun.COM static int
144810921STim.Haley@Sun.COM spa_load_verify(spa_t *spa)
144910921STim.Haley@Sun.COM {
145010921STim.Haley@Sun.COM 	zio_t *rio;
145110921STim.Haley@Sun.COM 	spa_load_error_t sle = { 0 };
145210921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
145310921STim.Haley@Sun.COM 	boolean_t verify_ok = B_FALSE;
145410921STim.Haley@Sun.COM 	int error;
145510921STim.Haley@Sun.COM 
145610921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
145710921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
145810921STim.Haley@Sun.COM 
145911125SJeff.Bonwick@Sun.COM 	error = traverse_pool(spa, spa->spa_verify_min_txg,
146011125SJeff.Bonwick@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
146110921STim.Haley@Sun.COM 
146210921STim.Haley@Sun.COM 	(void) zio_wait(rio);
146310921STim.Haley@Sun.COM 
146410921STim.Haley@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
146510921STim.Haley@Sun.COM 
146610921STim.Haley@Sun.COM 	spa->spa_load_meta_errors = sle.sle_metadata_count;
146710921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
146810921STim.Haley@Sun.COM 
146910921STim.Haley@Sun.COM 	if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta &&
147010921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
147110921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
147210921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
147310921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
147411026STim.Haley@Sun.COM 	} else {
147511026STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
147610921STim.Haley@Sun.COM 	}
147710921STim.Haley@Sun.COM 
147810921STim.Haley@Sun.COM 	if (error) {
147910921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
148010921STim.Haley@Sun.COM 			error = EIO;
148110921STim.Haley@Sun.COM 		return (error);
148210921STim.Haley@Sun.COM 	}
148310921STim.Haley@Sun.COM 
148410921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
148510921STim.Haley@Sun.COM }
148610921STim.Haley@Sun.COM 
14877294Sperrin /*
148811422SMark.Musante@Sun.COM  * Find a value in the pool props object.
148911422SMark.Musante@Sun.COM  */
149011422SMark.Musante@Sun.COM static void
149111422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
149211422SMark.Musante@Sun.COM {
149311422SMark.Musante@Sun.COM 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
149411422SMark.Musante@Sun.COM 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
149511422SMark.Musante@Sun.COM }
149611422SMark.Musante@Sun.COM 
149711422SMark.Musante@Sun.COM /*
149811422SMark.Musante@Sun.COM  * Find a value in the pool directory object.
149911422SMark.Musante@Sun.COM  */
150011422SMark.Musante@Sun.COM static int
150111422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
150211422SMark.Musante@Sun.COM {
150311422SMark.Musante@Sun.COM 	return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
150411422SMark.Musante@Sun.COM 	    name, sizeof (uint64_t), 1, val));
150511422SMark.Musante@Sun.COM }
150611422SMark.Musante@Sun.COM 
150711422SMark.Musante@Sun.COM static int
150811422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
150911422SMark.Musante@Sun.COM {
151011422SMark.Musante@Sun.COM 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
151111422SMark.Musante@Sun.COM 	return (err);
151211422SMark.Musante@Sun.COM }
151311422SMark.Musante@Sun.COM 
151411422SMark.Musante@Sun.COM /*
151511422SMark.Musante@Sun.COM  * Fix up config after a partly-completed split.  This is done with the
151611422SMark.Musante@Sun.COM  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
151711422SMark.Musante@Sun.COM  * pool have that entry in their config, but only the splitting one contains
151811422SMark.Musante@Sun.COM  * a list of all the guids of the vdevs that are being split off.
151911422SMark.Musante@Sun.COM  *
152011422SMark.Musante@Sun.COM  * This function determines what to do with that list: either rejoin
152111422SMark.Musante@Sun.COM  * all the disks to the pool, or complete the splitting process.  To attempt
152211422SMark.Musante@Sun.COM  * the rejoin, each disk that is offlined is marked online again, and
152311422SMark.Musante@Sun.COM  * we do a reopen() call.  If the vdev label for every disk that was
152411422SMark.Musante@Sun.COM  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
152511422SMark.Musante@Sun.COM  * then we call vdev_split() on each disk, and complete the split.
152611422SMark.Musante@Sun.COM  *
1527*11497SMark.Musante@Sun.COM  * Otherwise we leave the config alone, with all the vdevs in place in
1528*11497SMark.Musante@Sun.COM  * the original pool.
152911422SMark.Musante@Sun.COM  */
153011422SMark.Musante@Sun.COM static void
153111422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config)
153211422SMark.Musante@Sun.COM {
153311422SMark.Musante@Sun.COM 	uint_t extracted;
153411422SMark.Musante@Sun.COM 	uint64_t *glist;
153511422SMark.Musante@Sun.COM 	uint_t i, gcount;
153611422SMark.Musante@Sun.COM 	nvlist_t *nvl;
153711422SMark.Musante@Sun.COM 	vdev_t **vd;
153811422SMark.Musante@Sun.COM 	boolean_t attempt_reopen;
153911422SMark.Musante@Sun.COM 
154011422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
154111422SMark.Musante@Sun.COM 		return;
154211422SMark.Musante@Sun.COM 
154311422SMark.Musante@Sun.COM 	/* check that the config is complete */
154411422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
154511422SMark.Musante@Sun.COM 	    &glist, &gcount) != 0)
154611422SMark.Musante@Sun.COM 		return;
154711422SMark.Musante@Sun.COM 
154811422SMark.Musante@Sun.COM 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
154911422SMark.Musante@Sun.COM 
155011422SMark.Musante@Sun.COM 	/* attempt to online all the vdevs & validate */
155111422SMark.Musante@Sun.COM 	attempt_reopen = B_TRUE;
155211422SMark.Musante@Sun.COM 	for (i = 0; i < gcount; i++) {
155311422SMark.Musante@Sun.COM 		if (glist[i] == 0)	/* vdev is hole */
155411422SMark.Musante@Sun.COM 			continue;
155511422SMark.Musante@Sun.COM 
155611422SMark.Musante@Sun.COM 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
155711422SMark.Musante@Sun.COM 		if (vd[i] == NULL) {
155811422SMark.Musante@Sun.COM 			/*
155911422SMark.Musante@Sun.COM 			 * Don't bother attempting to reopen the disks;
156011422SMark.Musante@Sun.COM 			 * just do the split.
156111422SMark.Musante@Sun.COM 			 */
156211422SMark.Musante@Sun.COM 			attempt_reopen = B_FALSE;
156311422SMark.Musante@Sun.COM 		} else {
156411422SMark.Musante@Sun.COM 			/* attempt to re-online it */
156511422SMark.Musante@Sun.COM 			vd[i]->vdev_offline = B_FALSE;
156611422SMark.Musante@Sun.COM 		}
156711422SMark.Musante@Sun.COM 	}
156811422SMark.Musante@Sun.COM 
156911422SMark.Musante@Sun.COM 	if (attempt_reopen) {
157011422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
157111422SMark.Musante@Sun.COM 
157211422SMark.Musante@Sun.COM 		/* check each device to see what state it's in */
157311422SMark.Musante@Sun.COM 		for (extracted = 0, i = 0; i < gcount; i++) {
157411422SMark.Musante@Sun.COM 			if (vd[i] != NULL &&
157511422SMark.Musante@Sun.COM 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
157611422SMark.Musante@Sun.COM 				break;
157711422SMark.Musante@Sun.COM 			++extracted;
157811422SMark.Musante@Sun.COM 		}
157911422SMark.Musante@Sun.COM 	}
158011422SMark.Musante@Sun.COM 
158111422SMark.Musante@Sun.COM 	/*
158211422SMark.Musante@Sun.COM 	 * If every disk has been moved to the new pool, or if we never
158311422SMark.Musante@Sun.COM 	 * even attempted to look at them, then we split them off for
158411422SMark.Musante@Sun.COM 	 * good.
158511422SMark.Musante@Sun.COM 	 */
158611422SMark.Musante@Sun.COM 	if (!attempt_reopen || gcount == extracted) {
158711422SMark.Musante@Sun.COM 		for (i = 0; i < gcount; i++)
158811422SMark.Musante@Sun.COM 			if (vd[i] != NULL)
158911422SMark.Musante@Sun.COM 				vdev_split(vd[i]);
159011422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
159111422SMark.Musante@Sun.COM 	}
159211422SMark.Musante@Sun.COM 
159311422SMark.Musante@Sun.COM 	kmem_free(vd, gcount * sizeof (vdev_t *));
159411422SMark.Musante@Sun.COM }
159511422SMark.Musante@Sun.COM 
159611422SMark.Musante@Sun.COM static int
159711422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
159811422SMark.Musante@Sun.COM     boolean_t mosconfig)
159911422SMark.Musante@Sun.COM {
160011422SMark.Musante@Sun.COM 	nvlist_t *config = spa->spa_config;
160111422SMark.Musante@Sun.COM 	char *ereport = FM_EREPORT_ZFS_POOL;
160211422SMark.Musante@Sun.COM 	int error;
160311422SMark.Musante@Sun.COM 	uint64_t pool_guid;
160411422SMark.Musante@Sun.COM 	nvlist_t *nvl;
160511422SMark.Musante@Sun.COM 
160611422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
160711422SMark.Musante@Sun.COM 		return (EINVAL);
160811422SMark.Musante@Sun.COM 
160911422SMark.Musante@Sun.COM 	/*
161011422SMark.Musante@Sun.COM 	 * Versioning wasn't explicitly added to the label until later, so if
161111422SMark.Musante@Sun.COM 	 * it's not present treat it as the initial version.
161211422SMark.Musante@Sun.COM 	 */
161311422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
161411422SMark.Musante@Sun.COM 	    &spa->spa_ubsync.ub_version) != 0)
161511422SMark.Musante@Sun.COM 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
161611422SMark.Musante@Sun.COM 
161711422SMark.Musante@Sun.COM 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
161811422SMark.Musante@Sun.COM 	    &spa->spa_config_txg);
161911422SMark.Musante@Sun.COM 
162011422SMark.Musante@Sun.COM 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
162111422SMark.Musante@Sun.COM 	    spa_guid_exists(pool_guid, 0)) {
162211422SMark.Musante@Sun.COM 		error = EEXIST;
162311422SMark.Musante@Sun.COM 	} else {
162411422SMark.Musante@Sun.COM 		spa->spa_load_guid = pool_guid;
162511422SMark.Musante@Sun.COM 
162611422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
162711422SMark.Musante@Sun.COM 		    &nvl) == 0) {
162811422SMark.Musante@Sun.COM 			VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
162911422SMark.Musante@Sun.COM 			    KM_SLEEP) == 0);
163011422SMark.Musante@Sun.COM 		}
163111422SMark.Musante@Sun.COM 
163211422SMark.Musante@Sun.COM 		error = spa_load_impl(spa, pool_guid, config, state, type,
163311422SMark.Musante@Sun.COM 		    mosconfig, &ereport);
163411422SMark.Musante@Sun.COM 	}
163511422SMark.Musante@Sun.COM 
163611422SMark.Musante@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
163711422SMark.Musante@Sun.COM 	if (error && error != EBADF)
163811422SMark.Musante@Sun.COM 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
163911422SMark.Musante@Sun.COM 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
164011422SMark.Musante@Sun.COM 	spa->spa_ena = 0;
164111422SMark.Musante@Sun.COM 
164211422SMark.Musante@Sun.COM 	return (error);
164311422SMark.Musante@Sun.COM }
164411422SMark.Musante@Sun.COM 
164511422SMark.Musante@Sun.COM /*
1646789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
16471544Seschrock  * source of configuration information.
1648789Sahrens  */
1649789Sahrens static int
165011422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
165111422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
165211422SMark.Musante@Sun.COM     char **ereport)
1653789Sahrens {
1654789Sahrens 	int error = 0;
165510594SGeorge.Wilson@Sun.COM 	nvlist_t *nvconfig, *nvroot = NULL;
1656789Sahrens 	vdev_t *rvd;
1657789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
16581635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
16598241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
166011422SMark.Musante@Sun.COM 	int parse;
1661789Sahrens 
16628241SJeff.Bonwick@Sun.COM 	/*
16638241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
16648241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
16658241SJeff.Bonwick@Sun.COM 	 */
16668241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
16678241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
16688241SJeff.Bonwick@Sun.COM 
16697754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
16707754SJeff.Bonwick@Sun.COM 
16711544Seschrock 	spa->spa_load_state = state;
16721635Sbonwick 
167311422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
167411422SMark.Musante@Sun.COM 		return (EINVAL);
167511422SMark.Musante@Sun.COM 
167611422SMark.Musante@Sun.COM 	parse = (type == SPA_IMPORT_EXISTING ?
167711422SMark.Musante@Sun.COM 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
16782174Seschrock 
1679789Sahrens 	/*
16809234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
16819234SGeorge.Wilson@Sun.COM 	 */
16829630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
16839630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
16849234SGeorge.Wilson@Sun.COM 
16859234SGeorge.Wilson@Sun.COM 	/*
16862082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
16872082Seschrock 	 * value that will be returned by spa_version() since parsing the
16882082Seschrock 	 * configuration requires knowing the version number.
1689789Sahrens 	 */
16907754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
169111422SMark.Musante@Sun.COM 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
16927754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1693789Sahrens 
16942082Seschrock 	if (error != 0)
169511422SMark.Musante@Sun.COM 		return (error);
1696789Sahrens 
16971585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
169811422SMark.Musante@Sun.COM 
169911422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
170011422SMark.Musante@Sun.COM 		ASSERT(spa_guid(spa) == pool_guid);
170111422SMark.Musante@Sun.COM 	}
1702789Sahrens 
1703789Sahrens 	/*
1704789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1705789Sahrens 	 */
17067754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17074070Smc142369 	error = vdev_open(rvd);
17087754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
17094070Smc142369 	if (error != 0)
171011422SMark.Musante@Sun.COM 		return (error);
1711789Sahrens 
1712789Sahrens 	/*
17139276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
17149276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
17159276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
171611422SMark.Musante@Sun.COM 	 * that config.  Otherwise, we're validating against the cached config
17179276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
17189276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
17199276SMark.Musante@Sun.COM 	 * the vdev config.
172011422SMark.Musante@Sun.COM 	 *
172111422SMark.Musante@Sun.COM 	 * If we're assembling a new pool that's been split off from an
172211422SMark.Musante@Sun.COM 	 * existing pool, the labels haven't yet been updated so we skip
172311422SMark.Musante@Sun.COM 	 * validation for now.
17241986Seschrock 	 */
172511422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
172611422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
172711422SMark.Musante@Sun.COM 		error = vdev_validate(rvd);
172811422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
172911422SMark.Musante@Sun.COM 
173011422SMark.Musante@Sun.COM 		if (error != 0)
173111422SMark.Musante@Sun.COM 			return (error);
173211422SMark.Musante@Sun.COM 
173311422SMark.Musante@Sun.COM 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
173411422SMark.Musante@Sun.COM 			return (ENXIO);
17351986Seschrock 	}
17361986Seschrock 
17371986Seschrock 	/*
1738789Sahrens 	 * Find the best uberblock.
1739789Sahrens 	 */
17407754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1741789Sahrens 
1742789Sahrens 	/*
1743789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1744789Sahrens 	 */
174511422SMark.Musante@Sun.COM 	if (ub->ub_txg == 0)
174611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
17471544Seschrock 
17481544Seschrock 	/*
17491544Seschrock 	 * If the pool is newer than the code, we can't open it.
17501544Seschrock 	 */
175111422SMark.Musante@Sun.COM 	if (ub->ub_version > SPA_VERSION)
175211422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1753789Sahrens 
1754789Sahrens 	/*
1755789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1756789Sahrens 	 * incomplete configuration.
1757789Sahrens 	 */
175811422SMark.Musante@Sun.COM 	if (mosconfig && type != SPA_IMPORT_ASSEMBLE &&
175911422SMark.Musante@Sun.COM 	    rvd->vdev_guid_sum != ub->ub_guid_sum)
176011422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
176111422SMark.Musante@Sun.COM 
176211422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
176311422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
176411422SMark.Musante@Sun.COM 		spa_try_repair(spa, config);
176511422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
176611422SMark.Musante@Sun.COM 		nvlist_free(spa->spa_config_splitting);
176711422SMark.Musante@Sun.COM 		spa->spa_config_splitting = NULL;
1768789Sahrens 	}
1769789Sahrens 
1770789Sahrens 	/*
1771789Sahrens 	 * Initialize internal SPA structures.
1772789Sahrens 	 */
1773789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1774789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
177510921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
177610921STim.Haley@Sun.COM 	    TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE;
177710921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
177810921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
177910922SJeff.Bonwick@Sun.COM 	spa->spa_claim_max_txg = spa->spa_first_txg;
178010922SJeff.Bonwick@Sun.COM 
17811544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
178211422SMark.Musante@Sun.COM 	if (error)
178311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1784789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1785789Sahrens 
178611422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
178711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
178811422SMark.Musante@Sun.COM 
178911422SMark.Musante@Sun.COM 	if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
179011422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
179110594SGeorge.Wilson@Sun.COM 
1792789Sahrens 	if (!mosconfig) {
17933975Sek110237 		uint64_t hostid;
17942082Seschrock 
179510594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
17967706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
17973975Sek110237 			char *hostname;
17983975Sek110237 			unsigned long myhostid = 0;
17993975Sek110237 
180010594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
18013975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
18023975Sek110237 
18038662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
18048662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
18058662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
18068662SJordan.Vaughan@Sun.com 			/*
18078662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
18088662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
18098662SJordan.Vaughan@Sun.com 			 */
18103975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
18118662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
18124178Slling 			if (hostid != 0 && myhostid != 0 &&
18138662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
18143975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
18153975Sek110237 				    "loaded as it was last accessed by "
18167706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
18173975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
18187754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
18193975Sek110237 				    (unsigned long)hostid);
182011422SMark.Musante@Sun.COM 				return (EBADF);
18213975Sek110237 			}
18223975Sek110237 		}
18233975Sek110237 
182410594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1825789Sahrens 		spa_unload(spa);
1826789Sahrens 		spa_deactivate(spa);
18278241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1828789Sahrens 
182911422SMark.Musante@Sun.COM 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
18301544Seschrock 	}
18311544Seschrock 
183211422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPLIST,
183311422SMark.Musante@Sun.COM 	    &spa->spa_deferred_bplist_obj) != 0)
183411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1835789Sahrens 
18361544Seschrock 	/*
18372082Seschrock 	 * Load the bit that tells us to use the new accounting function
18382082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
18392082Seschrock 	 * be present.
18402082Seschrock 	 */
184111422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
184211422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
184311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18442082Seschrock 
18452082Seschrock 	/*
18461544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
18471544Seschrock 	 * not be present.
18481544Seschrock 	 */
184911422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
185011422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
185211422SMark.Musante@Sun.COM 
185311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
185411422SMark.Musante@Sun.COM 	    &spa->spa_errlog_scrub);
185511422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1857789Sahrens 
1858789Sahrens 	/*
18592926Sek110237 	 * Load the history object.  If we have an older pool, this
18602926Sek110237 	 * will not be present.
18612926Sek110237 	 */
186211422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
186311422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
186411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
186511422SMark.Musante@Sun.COM 
186611422SMark.Musante@Sun.COM 	/*
186711422SMark.Musante@Sun.COM 	 * If we're assembling the pool from the split-off vdevs of
186811422SMark.Musante@Sun.COM 	 * an existing pool, we don't want to attach the spares & cache
186911422SMark.Musante@Sun.COM 	 * devices.
187011422SMark.Musante@Sun.COM 	 */
18712926Sek110237 
18722926Sek110237 	/*
18732082Seschrock 	 * Load any hot spares for this pool.
18742082Seschrock 	 */
187511422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
187611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
187711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
187811422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
18794577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
18805450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
188111422SMark.Musante@Sun.COM 		    &spa->spa_spares.sav_config) != 0)
188211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18832082Seschrock 
18847754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
18852082Seschrock 		spa_load_spares(spa);
18867754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
188711422SMark.Musante@Sun.COM 	} else if (error == 0) {
188811422SMark.Musante@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
18892082Seschrock 	}
18902082Seschrock 
18915450Sbrendan 	/*
18925450Sbrendan 	 * Load any level 2 ARC devices for this pool.
18935450Sbrendan 	 */
189411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
18955450Sbrendan 	    &spa->spa_l2cache.sav_object);
189611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
189711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
189811422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
18995450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
19005450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
190111422SMark.Musante@Sun.COM 		    &spa->spa_l2cache.sav_config) != 0)
190211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19035450Sbrendan 
19047754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19055450Sbrendan 		spa_load_l2cache(spa);
19067754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
190711422SMark.Musante@Sun.COM 	} else if (error == 0) {
190811422SMark.Musante@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
19095450Sbrendan 	}
19105450Sbrendan 
19115094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19124543Smarks 
191311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
191411422SMark.Musante@Sun.COM 	if (error && error != ENOENT)
191511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19163912Slling 
19173912Slling 	if (error == 0) {
191811422SMark.Musante@Sun.COM 		uint64_t autoreplace;
191911422SMark.Musante@Sun.COM 
192011422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
192111422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
192211422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
192311422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
192411422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
192511422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
192611422SMark.Musante@Sun.COM 		    &spa->spa_dedup_ditto);
192711422SMark.Musante@Sun.COM 
192810672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
19293912Slling 	}
19303912Slling 
19312082Seschrock 	/*
19324451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
19334451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
19344451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
19354451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
19364451Seschrock 	 * over.
19374451Seschrock 	 */
193810672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
19394451Seschrock 		spa_check_removed(spa->spa_root_vdev);
194010672SEric.Schrock@Sun.COM 		/*
194110672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
194210672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
194310672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
194410672SEric.Schrock@Sun.COM 		 */
194510672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
194610672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
194710672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
194810672SEric.Schrock@Sun.COM 		}
194910672SEric.Schrock@Sun.COM 	}
19504451Seschrock 
19514451Seschrock 	/*
19521986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1953789Sahrens 	 */
19541986Seschrock 	vdev_load(rvd);
1955789Sahrens 
1956789Sahrens 	/*
1957789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1958789Sahrens 	 */
19597754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1960789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
19617754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1962789Sahrens 
1963789Sahrens 	/*
1964789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1965789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1966789Sahrens 	 */
196711422SMark.Musante@Sun.COM 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
196811422SMark.Musante@Sun.COM 		return (ENXIO);
1969789Sahrens 
197010922SJeff.Bonwick@Sun.COM 	/*
197110922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
197210922SJeff.Bonwick@Sun.COM 	 */
197310922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
197411422SMark.Musante@Sun.COM 	if (error != 0)
197511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
197610922SJeff.Bonwick@Sun.COM 
197710956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
197810956SGeorge.Wilson@Sun.COM 
197910921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
198010921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
198111422SMark.Musante@Sun.COM 		if (error)
198211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
198311422SMark.Musante@Sun.COM 			    error));
198410921STim.Haley@Sun.COM 	}
198510921STim.Haley@Sun.COM 
198610922SJeff.Bonwick@Sun.COM 	/*
198711422SMark.Musante@Sun.COM 	 * Load the intent log state and check log integrity.  If we're
198811422SMark.Musante@Sun.COM 	 * assembling a pool from a split, the log is not transferred over.
198910922SJeff.Bonwick@Sun.COM 	 */
199011422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
199111422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
199211422SMark.Musante@Sun.COM 		    &nvroot) == 0);
199311422SMark.Musante@Sun.COM 		spa_load_log_state(spa, nvroot);
199411422SMark.Musante@Sun.COM 		nvlist_free(nvconfig);
199511422SMark.Musante@Sun.COM 
199611422SMark.Musante@Sun.COM 		if (spa_check_logs(spa)) {
199711422SMark.Musante@Sun.COM 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
199811422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
199911422SMark.Musante@Sun.COM 		}
200010922SJeff.Bonwick@Sun.COM 	}
200110922SJeff.Bonwick@Sun.COM 
200210921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
200310921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
20041635Sbonwick 		dmu_tx_t *tx;
20051635Sbonwick 		int need_update = B_FALSE;
20068241SJeff.Bonwick@Sun.COM 
20078241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
20081601Sbonwick 
20091635Sbonwick 		/*
20101635Sbonwick 		 * Claim log blocks that haven't been committed yet.
20111635Sbonwick 		 * This must all happen in a single txg.
201210922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
201310922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
201410921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
20151635Sbonwick 		 */
201610922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
201710922SJeff.Bonwick@Sun.COM 
20181601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2019789Sahrens 		    spa_first_txg(spa));
20207754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
20212417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2022789Sahrens 		dmu_tx_commit(tx);
2023789Sahrens 
202410922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
202510922SJeff.Bonwick@Sun.COM 
202611422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_GOOD);
2027789Sahrens 		spa->spa_sync_on = B_TRUE;
2028789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2029789Sahrens 
2030789Sahrens 		/*
203110922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
203210922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
203310922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
203410922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
203510922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
2036789Sahrens 		 */
203710922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
20381585Sbonwick 
20391585Sbonwick 		/*
20401635Sbonwick 		 * If the config cache is stale, or we have uninitialized
20411635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
204210100SLin.Ling@Sun.COM 		 *
204310100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
204410100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
20451585Sbonwick 		 */
20461635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
204710921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
204810921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
20491635Sbonwick 			need_update = B_TRUE;
20501635Sbonwick 
20518241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
20521635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
20531635Sbonwick 				need_update = B_TRUE;
20541585Sbonwick 
20551585Sbonwick 		/*
20561635Sbonwick 		 * Update the config cache asychronously in case we're the
20571635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
20581585Sbonwick 		 */
20591635Sbonwick 		if (need_update)
20601635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20618241SJeff.Bonwick@Sun.COM 
20628241SJeff.Bonwick@Sun.COM 		/*
20638241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
20648241SJeff.Bonwick@Sun.COM 		 */
20658241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
20668241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
206710298SMatthew.Ahrens@Sun.COM 
206810298SMatthew.Ahrens@Sun.COM 		/*
206910298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
207010298SMatthew.Ahrens@Sun.COM 		 */
207110298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
207210298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
207310342Schris.kirby@sun.com 
207410342Schris.kirby@sun.com 		/*
207510342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
207610342Schris.kirby@sun.com 		 */
207710342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2078789Sahrens 	}
2079789Sahrens 
208011422SMark.Musante@Sun.COM 	return (0);
2081789Sahrens }
2082789Sahrens 
208310921STim.Haley@Sun.COM static int
208410921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
208510921STim.Haley@Sun.COM {
208610921STim.Haley@Sun.COM 	spa_unload(spa);
208710921STim.Haley@Sun.COM 	spa_deactivate(spa);
208810921STim.Haley@Sun.COM 
208910921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
209010921STim.Haley@Sun.COM 
209110921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
209210921STim.Haley@Sun.COM 	spa_async_suspend(spa);
209310921STim.Haley@Sun.COM 
209411422SMark.Musante@Sun.COM 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
209510921STim.Haley@Sun.COM }
209610921STim.Haley@Sun.COM 
209710921STim.Haley@Sun.COM static int
209810921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
209910921STim.Haley@Sun.COM     uint64_t max_request, boolean_t extreme)
210010921STim.Haley@Sun.COM {
210110921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
210210921STim.Haley@Sun.COM 	int load_error, rewind_error;
210310921STim.Haley@Sun.COM 	uint64_t safe_rollback_txg;
210410921STim.Haley@Sun.COM 	uint64_t min_txg;
210510921STim.Haley@Sun.COM 
210611026STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
210710921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
210811422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
210911026STim.Haley@Sun.COM 	} else {
211010921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
211111026STim.Haley@Sun.COM 	}
211210921STim.Haley@Sun.COM 
211311422SMark.Musante@Sun.COM 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
211411422SMark.Musante@Sun.COM 	    mosconfig);
211510921STim.Haley@Sun.COM 	if (load_error == 0)
211610921STim.Haley@Sun.COM 		return (0);
211710921STim.Haley@Sun.COM 
211810921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
211910921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
212010921STim.Haley@Sun.COM 
212110921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
212210921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
212310921STim.Haley@Sun.COM 
212410921STim.Haley@Sun.COM 	/* specific txg requested */
212510921STim.Haley@Sun.COM 	if (spa->spa_load_max_txg != UINT64_MAX && !extreme) {
212610921STim.Haley@Sun.COM 		nvlist_free(config);
212710921STim.Haley@Sun.COM 		return (load_error);
212810921STim.Haley@Sun.COM 	}
212910921STim.Haley@Sun.COM 
213010921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
213110921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
213211422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
213310921STim.Haley@Sun.COM 
213410921STim.Haley@Sun.COM 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
213510921STim.Haley@Sun.COM 	safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE;
213610921STim.Haley@Sun.COM 
213710921STim.Haley@Sun.COM 	min_txg = extreme ? TXG_INITIAL : safe_rollback_txg;
213810921STim.Haley@Sun.COM 	while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) {
213910921STim.Haley@Sun.COM 		if (spa->spa_load_max_txg < safe_rollback_txg)
214010921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
214110921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
214210921STim.Haley@Sun.COM 	}
214310921STim.Haley@Sun.COM 
214410921STim.Haley@Sun.COM 	if (config)
214510921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
214610921STim.Haley@Sun.COM 
214710921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
214810921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
214910921STim.Haley@Sun.COM 
215010921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
215110921STim.Haley@Sun.COM 		spa_config_set(spa, config);
215210921STim.Haley@Sun.COM 
215310921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
215410921STim.Haley@Sun.COM }
215510921STim.Haley@Sun.COM 
2156789Sahrens /*
2157789Sahrens  * Pool Open/Import
2158789Sahrens  *
2159789Sahrens  * The import case is identical to an open except that the configuration is sent
2160789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2161789Sahrens  * case of an open, the pool configuration will exist in the
21624451Seschrock  * POOL_STATE_UNINITIALIZED state.
2163789Sahrens  *
2164789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2165789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2166789Sahrens  * ambiguous state.
2167789Sahrens  */
2168789Sahrens static int
216910921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
217010921STim.Haley@Sun.COM     nvlist_t **config)
2171789Sahrens {
2172789Sahrens 	spa_t *spa;
217310921STim.Haley@Sun.COM 	boolean_t norewind;
217410921STim.Haley@Sun.COM 	boolean_t extreme;
217510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
217610921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_OPEN;
2177789Sahrens 	int error;
2178789Sahrens 	int locked = B_FALSE;
2179789Sahrens 
2180789Sahrens 	*spapp = NULL;
2181789Sahrens 
218210921STim.Haley@Sun.COM 	zpool_get_rewind_policy(nvpolicy, &policy);
218310921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
218410921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
218510921STim.Haley@Sun.COM 	norewind = (policy.zrp_request == ZPOOL_NO_REWIND);
218610921STim.Haley@Sun.COM 	extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0);
218710921STim.Haley@Sun.COM 
2188789Sahrens 	/*
2189789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2190789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2191789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2192789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2193789Sahrens 	 */
2194789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2195789Sahrens 		mutex_enter(&spa_namespace_lock);
2196789Sahrens 		locked = B_TRUE;
2197789Sahrens 	}
2198789Sahrens 
2199789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2200789Sahrens 		if (locked)
2201789Sahrens 			mutex_exit(&spa_namespace_lock);
2202789Sahrens 		return (ENOENT);
2203789Sahrens 	}
220410921STim.Haley@Sun.COM 
2205789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2206789Sahrens 
22078241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
2208789Sahrens 
220910921STim.Haley@Sun.COM 		if (spa->spa_last_open_failed && norewind) {
221010921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
221110921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config,
221210921STim.Haley@Sun.COM 				    config, KM_SLEEP) == 0);
221310921STim.Haley@Sun.COM 			spa_deactivate(spa);
221410921STim.Haley@Sun.COM 			if (locked)
221510921STim.Haley@Sun.COM 				mutex_exit(&spa_namespace_lock);
221610921STim.Haley@Sun.COM 			return (spa->spa_last_open_failed);
221710921STim.Haley@Sun.COM 		}
221810921STim.Haley@Sun.COM 
221910921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
222010921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
222110921STim.Haley@Sun.COM 
222210921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
222310921STim.Haley@Sun.COM 		    extreme);
2224789Sahrens 
2225789Sahrens 		if (error == EBADF) {
2226789Sahrens 			/*
22271986Seschrock 			 * If vdev_validate() returns failure (indicated by
22281986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
22291986Seschrock 			 * that the pool has been exported or destroyed.  If
22301986Seschrock 			 * this is the case, the config cache is out of sync and
22311986Seschrock 			 * we should remove the pool from the namespace.
2232789Sahrens 			 */
2233789Sahrens 			spa_unload(spa);
2234789Sahrens 			spa_deactivate(spa);
22356643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2236789Sahrens 			spa_remove(spa);
2237789Sahrens 			if (locked)
2238789Sahrens 				mutex_exit(&spa_namespace_lock);
2239789Sahrens 			return (ENOENT);
22401544Seschrock 		}
22411544Seschrock 
22421544Seschrock 		if (error) {
2243789Sahrens 			/*
2244789Sahrens 			 * We can't open the pool, but we still have useful
2245789Sahrens 			 * information: the state of each vdev after the
2246789Sahrens 			 * attempted vdev_open().  Return this to the user.
2247789Sahrens 			 */
224810921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
224910921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
225010921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
2251789Sahrens 			spa_unload(spa);
2252789Sahrens 			spa_deactivate(spa);
225310921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
2254789Sahrens 			if (locked)
2255789Sahrens 				mutex_exit(&spa_namespace_lock);
2256789Sahrens 			*spapp = NULL;
2257789Sahrens 			return (error);
2258789Sahrens 		}
225910921STim.Haley@Sun.COM 
2260789Sahrens 	}
2261789Sahrens 
2262789Sahrens 	spa_open_ref(spa, tag);
22634451Seschrock 
226410921STim.Haley@Sun.COM 
226510921STim.Haley@Sun.COM 	if (config != NULL)
226610921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
226710921STim.Haley@Sun.COM 
226811026STim.Haley@Sun.COM 	if (locked) {
226911026STim.Haley@Sun.COM 		spa->spa_last_open_failed = 0;
227011026STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = 0;
227111026STim.Haley@Sun.COM 		spa->spa_load_txg = 0;
2272789Sahrens 		mutex_exit(&spa_namespace_lock);
227311026STim.Haley@Sun.COM 	}
2274789Sahrens 
2275789Sahrens 	*spapp = spa;
2276789Sahrens 
2277789Sahrens 	return (0);
2278789Sahrens }
2279789Sahrens 
2280789Sahrens int
228110921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
228210921STim.Haley@Sun.COM     nvlist_t **config)
228310921STim.Haley@Sun.COM {
228410921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
228510921STim.Haley@Sun.COM }
228610921STim.Haley@Sun.COM 
228710921STim.Haley@Sun.COM int
2288789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2289789Sahrens {
229010921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2291789Sahrens }
2292789Sahrens 
22931544Seschrock /*
22941544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
22951544Seschrock  * preventing it from being exported or destroyed.
22961544Seschrock  */
22971544Seschrock spa_t *
22981544Seschrock spa_inject_addref(char *name)
22991544Seschrock {
23001544Seschrock 	spa_t *spa;
23011544Seschrock 
23021544Seschrock 	mutex_enter(&spa_namespace_lock);
23031544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
23041544Seschrock 		mutex_exit(&spa_namespace_lock);
23051544Seschrock 		return (NULL);
23061544Seschrock 	}
23071544Seschrock 	spa->spa_inject_ref++;
23081544Seschrock 	mutex_exit(&spa_namespace_lock);
23091544Seschrock 
23101544Seschrock 	return (spa);
23111544Seschrock }
23121544Seschrock 
23131544Seschrock void
23141544Seschrock spa_inject_delref(spa_t *spa)
23151544Seschrock {
23161544Seschrock 	mutex_enter(&spa_namespace_lock);
23171544Seschrock 	spa->spa_inject_ref--;
23181544Seschrock 	mutex_exit(&spa_namespace_lock);
23191544Seschrock }
23201544Seschrock 
23215450Sbrendan /*
23225450Sbrendan  * Add spares device information to the nvlist.
23235450Sbrendan  */
23242082Seschrock static void
23252082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
23262082Seschrock {
23272082Seschrock 	nvlist_t **spares;
23282082Seschrock 	uint_t i, nspares;
23292082Seschrock 	nvlist_t *nvroot;
23302082Seschrock 	uint64_t guid;
23312082Seschrock 	vdev_stat_t *vs;
23322082Seschrock 	uint_t vsc;
23333377Seschrock 	uint64_t pool;
23342082Seschrock 
23359425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23369425SEric.Schrock@Sun.COM 
23375450Sbrendan 	if (spa->spa_spares.sav_count == 0)
23382082Seschrock 		return;
23392082Seschrock 
23402082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
23412082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23425450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
23432082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23442082Seschrock 	if (nspares != 0) {
23452082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
23462082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
23472082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23482082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23492082Seschrock 
23502082Seschrock 		/*
23512082Seschrock 		 * Go through and find any spares which have since been
23522082Seschrock 		 * repurposed as an active spare.  If this is the case, update
23532082Seschrock 		 * their status appropriately.
23542082Seschrock 		 */
23552082Seschrock 		for (i = 0; i < nspares; i++) {
23562082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
23572082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23587214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
23597214Slling 			    pool != 0ULL) {
23602082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
23612082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
23622082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
23632082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
23642082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
23652082Seschrock 			}
23662082Seschrock 		}
23672082Seschrock 	}
23682082Seschrock }
23692082Seschrock 
23705450Sbrendan /*
23715450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
23725450Sbrendan  */
23735450Sbrendan static void
23745450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
23755450Sbrendan {
23765450Sbrendan 	nvlist_t **l2cache;
23775450Sbrendan 	uint_t i, j, nl2cache;
23785450Sbrendan 	nvlist_t *nvroot;
23795450Sbrendan 	uint64_t guid;
23805450Sbrendan 	vdev_t *vd;
23815450Sbrendan 	vdev_stat_t *vs;
23825450Sbrendan 	uint_t vsc;
23835450Sbrendan 
23849425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23859425SEric.Schrock@Sun.COM 
23865450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
23875450Sbrendan 		return;
23885450Sbrendan 
23895450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
23905450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23915450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
23925450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
23935450Sbrendan 	if (nl2cache != 0) {
23945450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
23955450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
23965450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23975450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
23985450Sbrendan 
23995450Sbrendan 		/*
24005450Sbrendan 		 * Update level 2 cache device stats.
24015450Sbrendan 		 */
24025450Sbrendan 
24035450Sbrendan 		for (i = 0; i < nl2cache; i++) {
24045450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
24055450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
24065450Sbrendan 
24075450Sbrendan 			vd = NULL;
24085450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
24095450Sbrendan 				if (guid ==
24105450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
24115450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
24125450Sbrendan 					break;
24135450Sbrendan 				}
24145450Sbrendan 			}
24155450Sbrendan 			ASSERT(vd != NULL);
24165450Sbrendan 
24175450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
24185450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
24195450Sbrendan 			vdev_get_stats(vd, vs);
24205450Sbrendan 		}
24215450Sbrendan 	}
24225450Sbrendan }
24235450Sbrendan 
2424789Sahrens int
24251544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2426789Sahrens {
2427789Sahrens 	int error;
2428789Sahrens 	spa_t *spa;
2429789Sahrens 
2430789Sahrens 	*config = NULL;
243110921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2432789Sahrens 
24339425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24349425SEric.Schrock@Sun.COM 		/*
24359425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
24369425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
24379425SEric.Schrock@Sun.COM 		 * self-inconsistent.
24389425SEric.Schrock@Sun.COM 		 */
24399425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
24409425SEric.Schrock@Sun.COM 
24419425SEric.Schrock@Sun.COM 		if (*config != NULL) {
24427754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
24439425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
24449425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
24459425SEric.Schrock@Sun.COM 
24469425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
24479425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
24489425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
24499425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
24509425SEric.Schrock@Sun.COM 
24519425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
24529425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
24539425SEric.Schrock@Sun.COM 		}
24542082Seschrock 	}
24552082Seschrock 
24561544Seschrock 	/*
24571544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
24581544Seschrock 	 * and call spa_lookup() directly.
24591544Seschrock 	 */
24601544Seschrock 	if (altroot) {
24611544Seschrock 		if (spa == NULL) {
24621544Seschrock 			mutex_enter(&spa_namespace_lock);
24631544Seschrock 			spa = spa_lookup(name);
24641544Seschrock 			if (spa)
24651544Seschrock 				spa_altroot(spa, altroot, buflen);
24661544Seschrock 			else
24671544Seschrock 				altroot[0] = '\0';
24681544Seschrock 			spa = NULL;
24691544Seschrock 			mutex_exit(&spa_namespace_lock);
24701544Seschrock 		} else {
24711544Seschrock 			spa_altroot(spa, altroot, buflen);
24721544Seschrock 		}
24731544Seschrock 	}
24741544Seschrock 
24759425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24769425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2477789Sahrens 		spa_close(spa, FTAG);
24789425SEric.Schrock@Sun.COM 	}
2479789Sahrens 
2480789Sahrens 	return (error);
2481789Sahrens }
2482789Sahrens 
2483789Sahrens /*
24845450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
24855450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
24865450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
24875450Sbrendan  * specified, as long as they are well-formed.
24882082Seschrock  */
24892082Seschrock static int
24905450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
24915450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
24925450Sbrendan     vdev_labeltype_t label)
24932082Seschrock {
24945450Sbrendan 	nvlist_t **dev;
24955450Sbrendan 	uint_t i, ndev;
24962082Seschrock 	vdev_t *vd;
24972082Seschrock 	int error;
24982082Seschrock 
24997754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25007754SJeff.Bonwick@Sun.COM 
25012082Seschrock 	/*
25025450Sbrendan 	 * It's acceptable to have no devs specified.
25032082Seschrock 	 */
25045450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
25052082Seschrock 		return (0);
25062082Seschrock 
25075450Sbrendan 	if (ndev == 0)
25082082Seschrock 		return (EINVAL);
25092082Seschrock 
25102082Seschrock 	/*
25115450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
25125450Sbrendan 	 * device type.
25132082Seschrock 	 */
25145450Sbrendan 	if (spa_version(spa) < version)
25152082Seschrock 		return (ENOTSUP);
25162082Seschrock 
25173377Seschrock 	/*
25185450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
25193377Seschrock 	 * checking.
25203377Seschrock 	 */
25215450Sbrendan 	sav->sav_pending = dev;
25225450Sbrendan 	sav->sav_npending = ndev;
25235450Sbrendan 
25245450Sbrendan 	for (i = 0; i < ndev; i++) {
25255450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
25262082Seschrock 		    mode)) != 0)
25273377Seschrock 			goto out;
25282082Seschrock 
25292082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
25302082Seschrock 			vdev_free(vd);
25313377Seschrock 			error = EINVAL;
25323377Seschrock 			goto out;
25332082Seschrock 		}
25342082Seschrock 
25355450Sbrendan 		/*
25367754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
25377754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
25385450Sbrendan 		 */
25397754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
25405450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
25415450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
25425450Sbrendan 			error = ENOTBLK;
25435450Sbrendan 			goto out;
25445450Sbrendan 		}
25457754SJeff.Bonwick@Sun.COM #endif
25462082Seschrock 		vd->vdev_top = vd;
25473377Seschrock 
25483377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
25495450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
25505450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
25513377Seschrock 			    vd->vdev_guid) == 0);
25522082Seschrock 		}
25532082Seschrock 
25542082Seschrock 		vdev_free(vd);
25553377Seschrock 
25565450Sbrendan 		if (error &&
25575450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
25583377Seschrock 			goto out;
25593377Seschrock 		else
25603377Seschrock 			error = 0;
25612082Seschrock 	}
25622082Seschrock 
25633377Seschrock out:
25645450Sbrendan 	sav->sav_pending = NULL;
25655450Sbrendan 	sav->sav_npending = 0;
25663377Seschrock 	return (error);
25672082Seschrock }
25682082Seschrock 
25695450Sbrendan static int
25705450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
25715450Sbrendan {
25725450Sbrendan 	int error;
25735450Sbrendan 
25747754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25757754SJeff.Bonwick@Sun.COM 
25765450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
25775450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
25785450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
25795450Sbrendan 		return (error);
25805450Sbrendan 	}
25815450Sbrendan 
25825450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
25835450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
25845450Sbrendan 	    VDEV_LABEL_L2CACHE));
25855450Sbrendan }
25865450Sbrendan 
25875450Sbrendan static void
25885450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
25895450Sbrendan     const char *config)
25905450Sbrendan {
25915450Sbrendan 	int i;
25925450Sbrendan 
25935450Sbrendan 	if (sav->sav_config != NULL) {
25945450Sbrendan 		nvlist_t **olddevs;
25955450Sbrendan 		uint_t oldndevs;
25965450Sbrendan 		nvlist_t **newdevs;
25975450Sbrendan 
25985450Sbrendan 		/*
25995450Sbrendan 		 * Generate new dev list by concatentating with the
26005450Sbrendan 		 * current dev list.
26015450Sbrendan 		 */
26025450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
26035450Sbrendan 		    &olddevs, &oldndevs) == 0);
26045450Sbrendan 
26055450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
26065450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
26075450Sbrendan 		for (i = 0; i < oldndevs; i++)
26085450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
26095450Sbrendan 			    KM_SLEEP) == 0);
26105450Sbrendan 		for (i = 0; i < ndevs; i++)
26115450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
26125450Sbrendan 			    KM_SLEEP) == 0);
26135450Sbrendan 
26145450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
26155450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
26165450Sbrendan 
26175450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
26185450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
26195450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
26205450Sbrendan 			nvlist_free(newdevs[i]);
26215450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
26225450Sbrendan 	} else {
26235450Sbrendan 		/*
26245450Sbrendan 		 * Generate a new dev list.
26255450Sbrendan 		 */
26265450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
26275450Sbrendan 		    KM_SLEEP) == 0);
26285450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
26295450Sbrendan 		    devs, ndevs) == 0);
26305450Sbrendan 	}
26315450Sbrendan }
26325450Sbrendan 
26335450Sbrendan /*
26345450Sbrendan  * Stop and drop level 2 ARC devices
26355450Sbrendan  */
26365450Sbrendan void
26375450Sbrendan spa_l2cache_drop(spa_t *spa)
26385450Sbrendan {
26395450Sbrendan 	vdev_t *vd;
26405450Sbrendan 	int i;
26415450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
26425450Sbrendan 
26435450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
26445450Sbrendan 		uint64_t pool;
26455450Sbrendan 
26465450Sbrendan 		vd = sav->sav_vdevs[i];
26475450Sbrendan 		ASSERT(vd != NULL);
26485450Sbrendan 
26498241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
26508241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
26515450Sbrendan 			l2arc_remove_vdev(vd);
26525450Sbrendan 		if (vd->vdev_isl2cache)
26535450Sbrendan 			spa_l2cache_remove(vd);
26545450Sbrendan 		vdev_clear_stats(vd);
26555450Sbrendan 		(void) vdev_close(vd);
26565450Sbrendan 	}
26575450Sbrendan }
26585450Sbrendan 
26592082Seschrock /*
2660789Sahrens  * Pool Creation
2661789Sahrens  */
2662789Sahrens int
26635094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
26647184Stimh     const char *history_str, nvlist_t *zplprops)
2665789Sahrens {
2666789Sahrens 	spa_t *spa;
26675094Slling 	char *altroot = NULL;
26681635Sbonwick 	vdev_t *rvd;
2669789Sahrens 	dsl_pool_t *dp;
2670789Sahrens 	dmu_tx_t *tx;
26719816SGeorge.Wilson@Sun.COM 	int error = 0;
2672789Sahrens 	uint64_t txg = TXG_INITIAL;
26735450Sbrendan 	nvlist_t **spares, **l2cache;
26745450Sbrendan 	uint_t nspares, nl2cache;
26755094Slling 	uint64_t version;
2676789Sahrens 
2677789Sahrens 	/*
2678789Sahrens 	 * If this pool already exists, return failure.
2679789Sahrens 	 */
2680789Sahrens 	mutex_enter(&spa_namespace_lock);
2681789Sahrens 	if (spa_lookup(pool) != NULL) {
2682789Sahrens 		mutex_exit(&spa_namespace_lock);
2683789Sahrens 		return (EEXIST);
2684789Sahrens 	}
2685789Sahrens 
2686789Sahrens 	/*
2687789Sahrens 	 * Allocate a new spa_t structure.
2688789Sahrens 	 */
26895094Slling 	(void) nvlist_lookup_string(props,
26905094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
269110921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
26928241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2693789Sahrens 
26945094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
26955094Slling 		spa_deactivate(spa);
26965094Slling 		spa_remove(spa);
26976643Seschrock 		mutex_exit(&spa_namespace_lock);
26985094Slling 		return (error);
26995094Slling 	}
27005094Slling 
27015094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
27025094Slling 	    &version) != 0)
27035094Slling 		version = SPA_VERSION;
27045094Slling 	ASSERT(version <= SPA_VERSION);
270510922SJeff.Bonwick@Sun.COM 
270610922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
270710922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
27085094Slling 	spa->spa_uberblock.ub_version = version;
2709789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2710789Sahrens 
27111635Sbonwick 	/*
27129234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
27139234SGeorge.Wilson@Sun.COM 	 */
27149630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
27159630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
27169234SGeorge.Wilson@Sun.COM 
27179234SGeorge.Wilson@Sun.COM 	/*
27181635Sbonwick 	 * Create the root vdev.
27191635Sbonwick 	 */
27207754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27211635Sbonwick 
27222082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
27232082Seschrock 
27242082Seschrock 	ASSERT(error != 0 || rvd != NULL);
27252082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
27262082Seschrock 
27275913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
27281635Sbonwick 		error = EINVAL;
27292082Seschrock 
27302082Seschrock 	if (error == 0 &&
27312082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
27325450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
27332082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
27349816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
27359816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
27369816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
27379816SGeorge.Wilson@Sun.COM 		}
27381635Sbonwick 	}
27391635Sbonwick 
27407754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2741789Sahrens 
27422082Seschrock 	if (error != 0) {
2743789Sahrens 		spa_unload(spa);
2744789Sahrens 		spa_deactivate(spa);
2745789Sahrens 		spa_remove(spa);
2746789Sahrens 		mutex_exit(&spa_namespace_lock);
2747789Sahrens 		return (error);
2748789Sahrens 	}
2749789Sahrens 
27502082Seschrock 	/*
27512082Seschrock 	 * Get the list of spares, if specified.
27522082Seschrock 	 */
27532082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
27542082Seschrock 	    &spares, &nspares) == 0) {
27555450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
27562082Seschrock 		    KM_SLEEP) == 0);
27575450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
27582082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
27597754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27602082Seschrock 		spa_load_spares(spa);
27617754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27625450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27635450Sbrendan 	}
27645450Sbrendan 
27655450Sbrendan 	/*
27665450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
27675450Sbrendan 	 */
27685450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
27695450Sbrendan 	    &l2cache, &nl2cache) == 0) {
27705450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
27715450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
27725450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
27735450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
27747754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27755450Sbrendan 		spa_load_l2cache(spa);
27767754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27775450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
27782082Seschrock 	}
27792082Seschrock 
27807184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2781789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2782789Sahrens 
278310956SGeorge.Wilson@Sun.COM 	/*
278410956SGeorge.Wilson@Sun.COM 	 * Create DDTs (dedup tables).
278510956SGeorge.Wilson@Sun.COM 	 */
278610956SGeorge.Wilson@Sun.COM 	ddt_create(spa);
278710956SGeorge.Wilson@Sun.COM 
278810956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
278910956SGeorge.Wilson@Sun.COM 
2790789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2791789Sahrens 
2792789Sahrens 	/*
2793789Sahrens 	 * Create the pool config object.
2794789Sahrens 	 */
2795789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
27967497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2797789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2798789Sahrens 
27991544Seschrock 	if (zap_add(spa->spa_meta_objset,
2800789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
28011544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
28021544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
28031544Seschrock 	}
2804789Sahrens 
28055094Slling 	/* Newly created pools with the right version are always deflated. */
28065094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
28075094Slling 		spa->spa_deflate = TRUE;
28085094Slling 		if (zap_add(spa->spa_meta_objset,
28095094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
28105094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
28115094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
28125094Slling 		}
28132082Seschrock 	}
28142082Seschrock 
2815789Sahrens 	/*
2816789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2817789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2818789Sahrens 	 * keeps changing.
2819789Sahrens 	 */
282010922SJeff.Bonwick@Sun.COM 	spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset,
2821789Sahrens 	    1 << 14, tx);
282210922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset,
282310922SJeff.Bonwick@Sun.COM 	    spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx);
2824789Sahrens 
28251544Seschrock 	if (zap_add(spa->spa_meta_objset,
2826789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
282710922SJeff.Bonwick@Sun.COM 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) {
28281544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
28291544Seschrock 	}
2830789Sahrens 
28312926Sek110237 	/*
28322926Sek110237 	 * Create the pool's history object.
28332926Sek110237 	 */
28345094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
28355094Slling 		spa_history_create_obj(spa, tx);
28365094Slling 
28375094Slling 	/*
28385094Slling 	 * Set pool properties.
28395094Slling 	 */
28405094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
28415094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
28425329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
28439816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
284410922SJeff.Bonwick@Sun.COM 
28458525SEric.Schrock@Sun.COM 	if (props != NULL) {
28468525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
28475094Slling 		spa_sync_props(spa, props, CRED(), tx);
28488525SEric.Schrock@Sun.COM 	}
28492926Sek110237 
2850789Sahrens 	dmu_tx_commit(tx);
2851789Sahrens 
2852789Sahrens 	spa->spa_sync_on = B_TRUE;
2853789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2854789Sahrens 
2855789Sahrens 	/*
2856789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2857789Sahrens 	 * bean counters are appropriately updated.
2858789Sahrens 	 */
2859789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2860789Sahrens 
28616643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2862789Sahrens 
28635094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
28644715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
28659946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
28664715Sek110237 
28678667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
28688667SGeorge.Wilson@Sun.COM 
2869789Sahrens 	mutex_exit(&spa_namespace_lock);
2870789Sahrens 
2871789Sahrens 	return (0);
2872789Sahrens }
2873789Sahrens 
28746423Sgw25295 #ifdef _KERNEL
28756423Sgw25295 /*
28769790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
28779790SLin.Ling@Sun.COM  * during the system boot up time.
28786423Sgw25295  */
28799790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
28809790SLin.Ling@Sun.COM 
28819790SLin.Ling@Sun.COM static nvlist_t *
28829790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
28836423Sgw25295 {
28849790SLin.Ling@Sun.COM 	nvlist_t *config;
28856423Sgw25295 	nvlist_t *nvtop, *nvroot;
28866423Sgw25295 	uint64_t pgid;
28876423Sgw25295 
28889790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
28899790SLin.Ling@Sun.COM 		return (NULL);
28909790SLin.Ling@Sun.COM 
28916423Sgw25295 	/*
28926423Sgw25295 	 * Add this top-level vdev to the child array.
28936423Sgw25295 	 */
28949790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28959790SLin.Ling@Sun.COM 	    &nvtop) == 0);
28969790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
28979790SLin.Ling@Sun.COM 	    &pgid) == 0);
28989790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
28996423Sgw25295 
29006423Sgw25295 	/*
29016423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
29026423Sgw25295 	 */
29036423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
29049790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
29059790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
29066423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
29076423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
29086423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
29096423Sgw25295 	    &nvtop, 1) == 0);
29106423Sgw25295 
29116423Sgw25295 	/*
29126423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
29136423Sgw25295 	 * this pool's configuration (remove the old, add the new).
29146423Sgw25295 	 */
29156423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
29166423Sgw25295 	nvlist_free(nvroot);
29179790SLin.Ling@Sun.COM 	return (config);
29186423Sgw25295 }
29196423Sgw25295 
29206423Sgw25295 /*
29219790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
29229790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
29239790SLin.Ling@Sun.COM  * device has a more recent txg.
29246423Sgw25295  */
29259790SLin.Ling@Sun.COM static void
29269790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
29277147Staylor {
29289816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
29299790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
29309790SLin.Ling@Sun.COM 
29319790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
29329790SLin.Ling@Sun.COM 		nvlist_t *label;
29339790SLin.Ling@Sun.COM 		uint64_t label_txg;
29349790SLin.Ling@Sun.COM 
29359790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
29369790SLin.Ling@Sun.COM 		    &label) != 0)
29379790SLin.Ling@Sun.COM 			return;
29389790SLin.Ling@Sun.COM 
29399790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
29409790SLin.Ling@Sun.COM 		    &label_txg) == 0);
29419790SLin.Ling@Sun.COM 
29429790SLin.Ling@Sun.COM 		/*
29439790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
29449790SLin.Ling@Sun.COM 		 */
29459790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
29469790SLin.Ling@Sun.COM 			*txg = label_txg;
29479790SLin.Ling@Sun.COM 			*avd = vd;
29487147Staylor 		}
29499790SLin.Ling@Sun.COM 		nvlist_free(label);
29507147Staylor 	}
29517147Staylor }
29527147Staylor 
29536423Sgw25295 /*
29546423Sgw25295  * Import a root pool.
29556423Sgw25295  *
29567147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
29577147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
29587147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
29596423Sgw25295  *
29606423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
29616423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
29626423Sgw25295  * e.g.
29636423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
29646423Sgw25295  */
29656423Sgw25295 int
29667147Staylor spa_import_rootpool(char *devpath, char *devid)
29676423Sgw25295 {
29689790SLin.Ling@Sun.COM 	spa_t *spa;
29699790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
29709790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
29719790SLin.Ling@Sun.COM 	uint64_t guid, txg;
29726423Sgw25295 	char *pname;
29736423Sgw25295 	int error;
29746423Sgw25295 
29756423Sgw25295 	/*
29769790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
29776423Sgw25295 	 */
297810822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
297910822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
298010822SJack.Meng@Sun.COM 	if (config == NULL) {
298110822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
298210822SJack.Meng@Sun.COM 			/* iscsi boot */
298310822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
298410822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
298510822SJack.Meng@Sun.COM 		}
298610822SJack.Meng@Sun.COM 	}
298710822SJack.Meng@Sun.COM #endif
298810822SJack.Meng@Sun.COM 	if (config == NULL) {
29899790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
29909790SLin.Ling@Sun.COM 		    devpath);
29919790SLin.Ling@Sun.COM 		return (EIO);
29929790SLin.Ling@Sun.COM 	}
29939790SLin.Ling@Sun.COM 
29949790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
29959790SLin.Ling@Sun.COM 	    &pname) == 0);
29969790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
29976423Sgw25295 
29989425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
29999425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
30009425SEric.Schrock@Sun.COM 		/*
30019425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
30029425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
30039425SEric.Schrock@Sun.COM 		 */
30049425SEric.Schrock@Sun.COM 		spa_remove(spa);
30059425SEric.Schrock@Sun.COM 	}
30069425SEric.Schrock@Sun.COM 
300710921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
30089425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
300910100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
30109790SLin.Ling@Sun.COM 
30119790SLin.Ling@Sun.COM 	/*
30129790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
30139790SLin.Ling@Sun.COM 	 */
30149790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30159790SLin.Ling@Sun.COM 	    &nvtop) == 0);
30169790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30179790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
30189790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
30199790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30209790SLin.Ling@Sun.COM 	if (error) {
30219790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
30229790SLin.Ling@Sun.COM 		nvlist_free(config);
30239790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
30249790SLin.Ling@Sun.COM 		    pname);
30259790SLin.Ling@Sun.COM 		return (error);
30269790SLin.Ling@Sun.COM 	}
30279790SLin.Ling@Sun.COM 
30289790SLin.Ling@Sun.COM 	/*
30299790SLin.Ling@Sun.COM 	 * Get the boot vdev.
30309790SLin.Ling@Sun.COM 	 */
30319790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
30329790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
30339790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
30349790SLin.Ling@Sun.COM 		error = ENOENT;
30359790SLin.Ling@Sun.COM 		goto out;
30369790SLin.Ling@Sun.COM 	}
30379790SLin.Ling@Sun.COM 
30389790SLin.Ling@Sun.COM 	/*
30399790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
30409790SLin.Ling@Sun.COM 	 */
30419790SLin.Ling@Sun.COM 	avd = bvd;
30429790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
30439790SLin.Ling@Sun.COM 	if (avd != bvd) {
30449790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
30459790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
30469790SLin.Ling@Sun.COM 		error = EINVAL;
30479790SLin.Ling@Sun.COM 		goto out;
30489790SLin.Ling@Sun.COM 	}
30499790SLin.Ling@Sun.COM 
30509790SLin.Ling@Sun.COM 	/*
30519790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
30529790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
30539790SLin.Ling@Sun.COM 	 */
30549790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30559790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
30569790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
30579790SLin.Ling@Sun.COM 		    "try booting from '%s'",
30589790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
30599790SLin.Ling@Sun.COM 		error = EINVAL;
30609790SLin.Ling@Sun.COM 		goto out;
30619790SLin.Ling@Sun.COM 	}
30629790SLin.Ling@Sun.COM 
30639790SLin.Ling@Sun.COM 	error = 0;
30649946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
30659790SLin.Ling@Sun.COM out:
30669790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30679790SLin.Ling@Sun.COM 	vdev_free(rvd);
30689790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30699425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
30706423Sgw25295 
30719790SLin.Ling@Sun.COM 	nvlist_free(config);
30726423Sgw25295 	return (error);
30736423Sgw25295 }
30749790SLin.Ling@Sun.COM 
30756423Sgw25295 #endif
30766423Sgw25295 
30776423Sgw25295 /*
30789425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
30799425SEric.Schrock@Sun.COM  * boot.
30809425SEric.Schrock@Sun.COM  */
30819425SEric.Schrock@Sun.COM int
30829425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
30839425SEric.Schrock@Sun.COM {
30849425SEric.Schrock@Sun.COM 	spa_t *spa;
308510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
30869425SEric.Schrock@Sun.COM 	char *altroot = NULL;
30879425SEric.Schrock@Sun.COM 
30889425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30899425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
30909425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
30919425SEric.Schrock@Sun.COM 		return (EEXIST);
30929425SEric.Schrock@Sun.COM 	}
30939425SEric.Schrock@Sun.COM 
30949425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
30959425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
309610921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
309710921STim.Haley@Sun.COM 
309810921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
309910921STim.Haley@Sun.COM 	spa->spa_load_max_txg = policy.zrp_txg;
31009425SEric.Schrock@Sun.COM 
310110100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
310210000SVictor.Latushkin@Sun.COM 
31039425SEric.Schrock@Sun.COM 	if (props != NULL)
31049425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31059425SEric.Schrock@Sun.COM 
31069425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
31079425SEric.Schrock@Sun.COM 
31089425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31099946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31109425SEric.Schrock@Sun.COM 
31119425SEric.Schrock@Sun.COM 	return (0);
31129425SEric.Schrock@Sun.COM }
31139425SEric.Schrock@Sun.COM 
31149425SEric.Schrock@Sun.COM /*
31156423Sgw25295  * Import a non-root pool into the system.
31166423Sgw25295  */
31176423Sgw25295 int
31186423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
31196423Sgw25295 {
31209425SEric.Schrock@Sun.COM 	spa_t *spa;
31219425SEric.Schrock@Sun.COM 	char *altroot = NULL;
312210921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
312310921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
31249425SEric.Schrock@Sun.COM 	int error;
31259425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
31269425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
31279425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
31289425SEric.Schrock@Sun.COM 
31299425SEric.Schrock@Sun.COM 	/*
31309425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
31319425SEric.Schrock@Sun.COM 	 */
31329425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
313311422SMark.Musante@Sun.COM 	if (spa_lookup(pool) != NULL) {
31349425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31359425SEric.Schrock@Sun.COM 		return (EEXIST);
31369425SEric.Schrock@Sun.COM 	}
31379425SEric.Schrock@Sun.COM 
313810921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
313910921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
314010921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
314110921STim.Haley@Sun.COM 
31429425SEric.Schrock@Sun.COM 	/*
31439425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
31449425SEric.Schrock@Sun.COM 	 */
31459425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31469425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
314710921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
31489425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
31499425SEric.Schrock@Sun.COM 
31509425SEric.Schrock@Sun.COM 	/*
31519630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
31529630SJeff.Bonwick@Sun.COM 	 */
31539630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
31549630SJeff.Bonwick@Sun.COM 
31559630SJeff.Bonwick@Sun.COM 	/*
31569425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
31579425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
31589425SEric.Schrock@Sun.COM 	 * doing an import.
31599425SEric.Schrock@Sun.COM 	 */
316010921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
316110921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
316210921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
316310921STim.Haley@Sun.COM 	    ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0));
316410921STim.Haley@Sun.COM 
316510921STim.Haley@Sun.COM 	/*
316610921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
316710921STim.Haley@Sun.COM 	 * back to caller
316810921STim.Haley@Sun.COM 	 */
316910921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
31709425SEric.Schrock@Sun.COM 
31719425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31729425SEric.Schrock@Sun.COM 	/*
31739425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
31749425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
31759425SEric.Schrock@Sun.COM 	 */
31769425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
31779425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
31789425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
31799425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
31809425SEric.Schrock@Sun.COM 	}
31819425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
31829425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
31839425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
31849425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
31859425SEric.Schrock@Sun.COM 	}
31869425SEric.Schrock@Sun.COM 
31879425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
31889425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
31899425SEric.Schrock@Sun.COM 	if (error == 0)
31909425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
31919425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
31929425SEric.Schrock@Sun.COM 	if (error == 0)
31939425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
31949425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
31959425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
31969425SEric.Schrock@Sun.COM 
31979425SEric.Schrock@Sun.COM 	if (props != NULL)
31989425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31999425SEric.Schrock@Sun.COM 
32009425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
32019425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
32029425SEric.Schrock@Sun.COM 		spa_unload(spa);
32039425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
32049425SEric.Schrock@Sun.COM 		spa_remove(spa);
32059425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
32069425SEric.Schrock@Sun.COM 		return (error);
32079425SEric.Schrock@Sun.COM 	}
32089425SEric.Schrock@Sun.COM 
32099630SJeff.Bonwick@Sun.COM 	spa_async_resume(spa);
32109630SJeff.Bonwick@Sun.COM 
32119425SEric.Schrock@Sun.COM 	/*
32129425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
32139425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
32149425SEric.Schrock@Sun.COM 	 */
32159425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32169425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
32179425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
32189425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
32199425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
32209425SEric.Schrock@Sun.COM 		else
32219425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
32229425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32239425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
32249425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
32259425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32269425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32279425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32289425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
32299425SEric.Schrock@Sun.COM 	}
32309425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
32319425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
32329425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
32339425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
32349425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
32359425SEric.Schrock@Sun.COM 		else
32369425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
32379425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32389425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
32399425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
32409425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32419425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32429425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32439425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
32449425SEric.Schrock@Sun.COM 	}
32459425SEric.Schrock@Sun.COM 
324610672SEric.Schrock@Sun.COM 	/*
324710672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
324810672SEric.Schrock@Sun.COM 	 */
324910672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
325010672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
325110672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
325210672SEric.Schrock@Sun.COM 	}
325310672SEric.Schrock@Sun.COM 
32549425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
32559425SEric.Schrock@Sun.COM 		/*
32569425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
32579425SEric.Schrock@Sun.COM 		 */
325810100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32599425SEric.Schrock@Sun.COM 	}
32609425SEric.Schrock@Sun.COM 
32619816SGeorge.Wilson@Sun.COM 	/*
32629816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
32639816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
32649816SGeorge.Wilson@Sun.COM 	 */
32659816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
32669816SGeorge.Wilson@Sun.COM 
32679425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
32689946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
32699425SEric.Schrock@Sun.COM 
32709425SEric.Schrock@Sun.COM 	return (0);
32716643Seschrock }
32726643Seschrock 
3273789Sahrens nvlist_t *
3274789Sahrens spa_tryimport(nvlist_t *tryconfig)
3275789Sahrens {
3276789Sahrens 	nvlist_t *config = NULL;
3277789Sahrens 	char *poolname;
3278789Sahrens 	spa_t *spa;
3279789Sahrens 	uint64_t state;
32808680SLin.Ling@Sun.COM 	int error;
3281789Sahrens 
3282789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3283789Sahrens 		return (NULL);
3284789Sahrens 
3285789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3286789Sahrens 		return (NULL);
3287789Sahrens 
32881635Sbonwick 	/*
32891635Sbonwick 	 * Create and initialize the spa structure.
32901635Sbonwick 	 */
3291789Sahrens 	mutex_enter(&spa_namespace_lock);
329210921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
32938241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3294789Sahrens 
3295789Sahrens 	/*
32961635Sbonwick 	 * Pass off the heavy lifting to spa_load().
32971732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
32981732Sbonwick 	 * is actually the one to trust when doing an import.
3299789Sahrens 	 */
330011422SMark.Musante@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3301789Sahrens 
3302789Sahrens 	/*
3303789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3304789Sahrens 	 */
3305789Sahrens 	if (spa->spa_root_vdev != NULL) {
3306789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3307789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3308789Sahrens 		    poolname) == 0);
3309789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3310789Sahrens 		    state) == 0);
33113975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
33123975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
33132082Seschrock 
33142082Seschrock 		/*
33156423Sgw25295 		 * If the bootfs property exists on this pool then we
33166423Sgw25295 		 * copy it out so that external consumers can tell which
33176423Sgw25295 		 * pools are bootable.
33186423Sgw25295 		 */
33198680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
33206423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33216423Sgw25295 
33226423Sgw25295 			/*
33236423Sgw25295 			 * We have to play games with the name since the
33246423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
33256423Sgw25295 			 */
33267754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
33276423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
33286423Sgw25295 				char *cp;
33296423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33306423Sgw25295 
33316423Sgw25295 				cp = strchr(tmpname, '/');
33326423Sgw25295 				if (cp == NULL) {
33336423Sgw25295 					(void) strlcpy(dsname, tmpname,
33346423Sgw25295 					    MAXPATHLEN);
33356423Sgw25295 				} else {
33366423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
33376423Sgw25295 					    "%s/%s", poolname, ++cp);
33386423Sgw25295 				}
33396423Sgw25295 				VERIFY(nvlist_add_string(config,
33406423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
33416423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
33426423Sgw25295 			}
33436423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
33446423Sgw25295 		}
33456423Sgw25295 
33466423Sgw25295 		/*
33475450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
33482082Seschrock 		 */
33499425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
33502082Seschrock 		spa_add_spares(spa, config);
33515450Sbrendan 		spa_add_l2cache(spa, config);
33529425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3353789Sahrens 	}
3354789Sahrens 
3355789Sahrens 	spa_unload(spa);
3356789Sahrens 	spa_deactivate(spa);
3357789Sahrens 	spa_remove(spa);
3358789Sahrens 	mutex_exit(&spa_namespace_lock);
3359789Sahrens 
3360789Sahrens 	return (config);
3361789Sahrens }
3362789Sahrens 
3363789Sahrens /*
3364789Sahrens  * Pool export/destroy
3365789Sahrens  *
3366789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3367789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3368789Sahrens  * update the pool state and sync all the labels to disk, removing the
33698211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
33708211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3371789Sahrens  */
3372789Sahrens static int
33737214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
33748211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3375789Sahrens {
3376789Sahrens 	spa_t *spa;
3377789Sahrens 
33781775Sbillm 	if (oldconfig)
33791775Sbillm 		*oldconfig = NULL;
33801775Sbillm 
33818241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3382789Sahrens 		return (EROFS);
3383789Sahrens 
3384789Sahrens 	mutex_enter(&spa_namespace_lock);
3385789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3386789Sahrens 		mutex_exit(&spa_namespace_lock);
3387789Sahrens 		return (ENOENT);
3388789Sahrens 	}
3389789Sahrens 
3390789Sahrens 	/*
33911544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
33921544Seschrock 	 * reacquire the namespace lock, and see if we can export.
33931544Seschrock 	 */
33941544Seschrock 	spa_open_ref(spa, FTAG);
33951544Seschrock 	mutex_exit(&spa_namespace_lock);
33961544Seschrock 	spa_async_suspend(spa);
33971544Seschrock 	mutex_enter(&spa_namespace_lock);
33981544Seschrock 	spa_close(spa, FTAG);
33991544Seschrock 
34001544Seschrock 	/*
3401789Sahrens 	 * The pool will be in core if it's openable,
3402789Sahrens 	 * in which case we can modify its state.
3403789Sahrens 	 */
3404789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3405789Sahrens 		/*
3406789Sahrens 		 * Objsets may be open only because they're dirty, so we
3407789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3408789Sahrens 		 */
3409789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3410789Sahrens 
34111544Seschrock 		/*
34121544Seschrock 		 * A pool cannot be exported or destroyed if there are active
34131544Seschrock 		 * references.  If we are resetting a pool, allow references by
34141544Seschrock 		 * fault injection handlers.
34151544Seschrock 		 */
34161544Seschrock 		if (!spa_refcount_zero(spa) ||
34171544Seschrock 		    (spa->spa_inject_ref != 0 &&
34181544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
34191544Seschrock 			spa_async_resume(spa);
3420789Sahrens 			mutex_exit(&spa_namespace_lock);
3421789Sahrens 			return (EBUSY);
3422789Sahrens 		}
3423789Sahrens 
3424789Sahrens 		/*
34257214Slling 		 * A pool cannot be exported if it has an active shared spare.
34267214Slling 		 * This is to prevent other pools stealing the active spare
34277214Slling 		 * from an exported pool. At user's own will, such pool can
34287214Slling 		 * be forcedly exported.
34297214Slling 		 */
34307214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
34317214Slling 		    spa_has_active_shared_spare(spa)) {
34327214Slling 			spa_async_resume(spa);
34337214Slling 			mutex_exit(&spa_namespace_lock);
34347214Slling 			return (EXDEV);
34357214Slling 		}
34367214Slling 
34377214Slling 		/*
3438789Sahrens 		 * We want this to be reflected on every label,
3439789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3440789Sahrens 		 * final sync that pushes these changes out.
3441789Sahrens 		 */
34428211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
34437754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34441544Seschrock 			spa->spa_state = new_state;
34451635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
34461544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
34477754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
34481544Seschrock 		}
3449789Sahrens 	}
3450789Sahrens 
34514451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
34524451Seschrock 
3453789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3454789Sahrens 		spa_unload(spa);
3455789Sahrens 		spa_deactivate(spa);
3456789Sahrens 	}
3457789Sahrens 
34581775Sbillm 	if (oldconfig && spa->spa_config)
34591775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
34601775Sbillm 
34611544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
34628211SGeorge.Wilson@Sun.COM 		if (!hardforce)
34638211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
34641544Seschrock 		spa_remove(spa);
34651544Seschrock 	}
3466789Sahrens 	mutex_exit(&spa_namespace_lock);
3467789Sahrens 
3468789Sahrens 	return (0);
3469789Sahrens }
3470789Sahrens 
3471789Sahrens /*
3472789Sahrens  * Destroy a storage pool.
3473789Sahrens  */
3474789Sahrens int
3475789Sahrens spa_destroy(char *pool)
3476789Sahrens {
34778211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
34788211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3479789Sahrens }
3480789Sahrens 
3481789Sahrens /*
3482789Sahrens  * Export a storage pool.
3483789Sahrens  */
3484789Sahrens int
34858211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
34868211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3487789Sahrens {
34888211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
34898211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3490789Sahrens }
3491789Sahrens 
3492789Sahrens /*
34931544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
34941544Seschrock  * from the namespace in any way.
34951544Seschrock  */
34961544Seschrock int
34971544Seschrock spa_reset(char *pool)
34981544Seschrock {
34997214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
35008211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
35011544Seschrock }
35021544Seschrock 
35031544Seschrock /*
3504789Sahrens  * ==========================================================================
3505789Sahrens  * Device manipulation
3506789Sahrens  * ==========================================================================
3507789Sahrens  */
3508789Sahrens 
3509789Sahrens /*
35104527Sperrin  * Add a device to a storage pool.
3511789Sahrens  */
3512789Sahrens int
3513789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3514789Sahrens {
351510594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
35168241SJeff.Bonwick@Sun.COM 	int error;
3517789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
35181585Sbonwick 	vdev_t *vd, *tvd;
35195450Sbrendan 	nvlist_t **spares, **l2cache;
35205450Sbrendan 	uint_t nspares, nl2cache;
3521789Sahrens 
3522789Sahrens 	txg = spa_vdev_enter(spa);
3523789Sahrens 
35242082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
35252082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
35262082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
35272082Seschrock 
35287754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3529789Sahrens 
35305450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
35315450Sbrendan 	    &nspares) != 0)
35322082Seschrock 		nspares = 0;
35332082Seschrock 
35345450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
35355450Sbrendan 	    &nl2cache) != 0)
35365450Sbrendan 		nl2cache = 0;
35375450Sbrendan 
35387754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
35392082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
35407754SJeff.Bonwick@Sun.COM 
35417754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
35427754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
35437754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
35442082Seschrock 
35453377Seschrock 	/*
35465450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
35475450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
35483377Seschrock 	 */
35497754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
35503377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
35513377Seschrock 
35523377Seschrock 	/*
35533377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
35543377Seschrock 	 */
35558241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
355610594SGeorge.Wilson@Sun.COM 
355710594SGeorge.Wilson@Sun.COM 		/*
355810594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
355910594SGeorge.Wilson@Sun.COM 		 */
356010594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
356110594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
356210594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
356310594SGeorge.Wilson@Sun.COM 				break;
356410594SGeorge.Wilson@Sun.COM 			}
356510594SGeorge.Wilson@Sun.COM 		}
35663377Seschrock 		tvd = vd->vdev_child[c];
35673377Seschrock 		vdev_remove_child(vd, tvd);
356810594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
35693377Seschrock 		vdev_add_child(rvd, tvd);
35703377Seschrock 		vdev_config_dirty(tvd);
35713377Seschrock 	}
35723377Seschrock 
35732082Seschrock 	if (nspares != 0) {
35745450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
35755450Sbrendan 		    ZPOOL_CONFIG_SPARES);
35762082Seschrock 		spa_load_spares(spa);
35775450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
35785450Sbrendan 	}
35795450Sbrendan 
35805450Sbrendan 	if (nl2cache != 0) {
35815450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
35825450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
35835450Sbrendan 		spa_load_l2cache(spa);
35845450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3585789Sahrens 	}
3586789Sahrens 
3587789Sahrens 	/*
35881585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
35891585Sbonwick 	 * If other threads start allocating from these vdevs before we
35901585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
35911585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
35921585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
35931585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
35941635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
35951585Sbonwick 	 *
35961585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
35971585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
35981585Sbonwick 	 * steps will be completed the next time we load the pool.
3599789Sahrens 	 */
36001635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
36011585Sbonwick 
36021635Sbonwick 	mutex_enter(&spa_namespace_lock);
36031635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36041635Sbonwick 	mutex_exit(&spa_namespace_lock);
3605789Sahrens 
36061635Sbonwick 	return (0);
3607789Sahrens }
3608789Sahrens 
3609789Sahrens /*
3610789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3611789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3612789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3613789Sahrens  *
3614789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3615789Sahrens  * existing device; in this case the two devices are made into their own
36164451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3617789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3618789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3619789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3620789Sahrens  * is automatically detached.
3621789Sahrens  */
3622789Sahrens int
36231544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3624789Sahrens {
3625789Sahrens 	uint64_t txg, open_txg;
3626789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3627789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
36282082Seschrock 	vdev_ops_t *pvops;
36297313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
36307313SEric.Kustarz@Sun.COM 	int newvd_isspare;
36317313SEric.Kustarz@Sun.COM 	int error;
3632789Sahrens 
3633789Sahrens 	txg = spa_vdev_enter(spa);
3634789Sahrens 
36356643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3636789Sahrens 
3637789Sahrens 	if (oldvd == NULL)
3638789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3639789Sahrens 
36401585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
36411585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
36421585Sbonwick 
3643789Sahrens 	pvd = oldvd->vdev_parent;
3644789Sahrens 
36452082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
36464451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
36474451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
36484451Seschrock 
36494451Seschrock 	if (newrootvd->vdev_children != 1)
3650789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3651789Sahrens 
3652789Sahrens 	newvd = newrootvd->vdev_child[0];
3653789Sahrens 
3654789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3655789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3656789Sahrens 
36572082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3658789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3659789Sahrens 
36604527Sperrin 	/*
36614527Sperrin 	 * Spares can't replace logs
36624527Sperrin 	 */
36637326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
36644527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36654527Sperrin 
36662082Seschrock 	if (!replacing) {
36672082Seschrock 		/*
36682082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
36692082Seschrock 		 * vdev.
36702082Seschrock 		 */
36712082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
36722082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
36732082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36742082Seschrock 
36752082Seschrock 		pvops = &vdev_mirror_ops;
36762082Seschrock 	} else {
36772082Seschrock 		/*
36782082Seschrock 		 * Active hot spares can only be replaced by inactive hot
36792082Seschrock 		 * spares.
36802082Seschrock 		 */
36812082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
36822082Seschrock 		    pvd->vdev_child[1] == oldvd &&
36832082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
36842082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36852082Seschrock 
36862082Seschrock 		/*
36872082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
36882082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
36893377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
36903377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
36913377Seschrock 		 * the same (spare replaces spare, non-spare replaces
36923377Seschrock 		 * non-spare).
36932082Seschrock 		 */
36942082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
36952082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36963377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
36973377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
36983377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36992082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
37002082Seschrock 		    newvd->vdev_isspare)
37012082Seschrock 			pvops = &vdev_spare_ops;
37022082Seschrock 		else
37032082Seschrock 			pvops = &vdev_replacing_ops;
37042082Seschrock 	}
37052082Seschrock 
37061175Slling 	/*
37079816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
37081175Slling 	 */
37099816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3710789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3711789Sahrens 
37121732Sbonwick 	/*
37131732Sbonwick 	 * The new device cannot have a higher alignment requirement
37141732Sbonwick 	 * than the top-level vdev.
37151732Sbonwick 	 */
37161732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3717789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3718789Sahrens 
3719789Sahrens 	/*
3720789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3721789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3722789Sahrens 	 */
3723789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3724789Sahrens 		spa_strfree(oldvd->vdev_path);
3725789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3726789Sahrens 		    KM_SLEEP);
3727789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3728789Sahrens 		    newvd->vdev_path, "old");
3729789Sahrens 		if (oldvd->vdev_devid != NULL) {
3730789Sahrens 			spa_strfree(oldvd->vdev_devid);
3731789Sahrens 			oldvd->vdev_devid = NULL;
3732789Sahrens 		}
3733789Sahrens 	}
3734789Sahrens 
3735789Sahrens 	/*
37362082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
37372082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3738789Sahrens 	 */
3739789Sahrens 	if (pvd->vdev_ops != pvops)
3740789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3741789Sahrens 
3742789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3743789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3744789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3745789Sahrens 
3746789Sahrens 	/*
3747789Sahrens 	 * Extract the new device from its root and add it to pvd.
3748789Sahrens 	 */
3749789Sahrens 	vdev_remove_child(newrootvd, newvd);
3750789Sahrens 	newvd->vdev_id = pvd->vdev_children;
375110594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3752789Sahrens 	vdev_add_child(pvd, newvd);
3753789Sahrens 
3754789Sahrens 	tvd = newvd->vdev_top;
3755789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3756789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3757789Sahrens 
3758789Sahrens 	vdev_config_dirty(tvd);
3759789Sahrens 
3760789Sahrens 	/*
3761789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3762789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3763789Sahrens 	 */
3764789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3765789Sahrens 
37668241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
37678241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3768789Sahrens 
37699425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
37703377Seschrock 		spa_spare_activate(newvd);
37719425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
37729425SEric.Schrock@Sun.COM 	}
37739425SEric.Schrock@Sun.COM 
37747754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
37757754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
37767313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
37771544Seschrock 
3778789Sahrens 	/*
3779789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3780789Sahrens 	 */
37811732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3782789Sahrens 
3783789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3784789Sahrens 
37859946SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
37869946SMark.Musante@Sun.COM 	    CRED(),  "%s vdev=%s %s vdev=%s",
37879946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
37889946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
37899946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
37907313SEric.Kustarz@Sun.COM 
37917313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
37927313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
37937313SEric.Kustarz@Sun.COM 
3794789Sahrens 	/*
37957046Sahrens 	 * Kick off a resilver to update newvd.
3796789Sahrens 	 */
37977046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3798789Sahrens 
3799789Sahrens 	return (0);
3800789Sahrens }
3801789Sahrens 
3802789Sahrens /*
3803789Sahrens  * Detach a device from a mirror or replacing vdev.
3804789Sahrens  * If 'replace_done' is specified, only detach if the parent
3805789Sahrens  * is a replacing vdev.
3806789Sahrens  */
3807789Sahrens int
38088241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3809789Sahrens {
3810789Sahrens 	uint64_t txg;
38118241SJeff.Bonwick@Sun.COM 	int error;
3812789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3813789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
38142082Seschrock 	boolean_t unspare = B_FALSE;
38152082Seschrock 	uint64_t unspare_guid;
38166673Seschrock 	size_t len;
381711422SMark.Musante@Sun.COM 	char *vdpath;
3818789Sahrens 
3819789Sahrens 	txg = spa_vdev_enter(spa);
3820789Sahrens 
38216643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3822789Sahrens 
3823789Sahrens 	if (vd == NULL)
3824789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3825789Sahrens 
38261585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
38271585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38281585Sbonwick 
3829789Sahrens 	pvd = vd->vdev_parent;
3830789Sahrens 
3831789Sahrens 	/*
38328241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
38338241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
38348241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
38358241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
38368241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
38378241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
38388241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
38398241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
38408241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
38418241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
38428241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
38438241SJeff.Bonwick@Sun.COM 	 */
38448241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
38458241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
38468241SJeff.Bonwick@Sun.COM 
38478241SJeff.Bonwick@Sun.COM 	/*
3848789Sahrens 	 * If replace_done is specified, only remove this device if it's
38492082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
38502082Seschrock 	 * disk can be removed.
3851789Sahrens 	 */
38522082Seschrock 	if (replace_done) {
38532082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
38542082Seschrock 			if (vd->vdev_id != 0)
38552082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38562082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
38572082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38582082Seschrock 		}
38592082Seschrock 	}
38602082Seschrock 
38612082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
38624577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3863789Sahrens 
3864789Sahrens 	/*
38652082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3866789Sahrens 	 */
3867789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
38682082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
38692082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3870789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3871789Sahrens 
3872789Sahrens 	/*
38738241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
38748241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3875789Sahrens 	 */
38768241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3877789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3878789Sahrens 
38798241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
38808241SJeff.Bonwick@Sun.COM 
3881789Sahrens 	/*
38826673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
38836673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
38846673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
38856673Seschrock 	 */
38866673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
38876673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
38886673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
38896673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
38906673Seschrock 		cvd = pvd->vdev_child[0];
38916673Seschrock 		len = strlen(vd->vdev_path);
38926673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
38936673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
38946673Seschrock 			spa_strfree(cvd->vdev_path);
38956673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
38966673Seschrock 		}
38976673Seschrock 	}
38986673Seschrock 
38996673Seschrock 	/*
39002082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
39012082Seschrock 	 * that the spare should become a real disk, and be removed from the
39022082Seschrock 	 * active spare list for the pool.
39032082Seschrock 	 */
39042082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
39058241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
39062082Seschrock 		unspare = B_TRUE;
39072082Seschrock 
39082082Seschrock 	/*
3909789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3910789Sahrens 	 * This must be done after all other error cases are handled,
3911789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3912789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3913789Sahrens 	 * it may be that the unwritability of the disk is the reason
3914789Sahrens 	 * it's being detached!
3915789Sahrens 	 */
39163377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3917789Sahrens 
3918789Sahrens 	/*
3919789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3920789Sahrens 	 */
3921789Sahrens 	vdev_remove_child(pvd, vd);
3922789Sahrens 	vdev_compact_children(pvd);
3923789Sahrens 
3924789Sahrens 	/*
3925789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3926789Sahrens 	 */
3927789Sahrens 	cvd = pvd->vdev_child[0];
3928789Sahrens 
3929789Sahrens 	/*
39302082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
39318241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
39328241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
39338241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
39348241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
39358241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
39368241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
39372082Seschrock 	 */
39382082Seschrock 	if (unspare) {
39392082Seschrock 		ASSERT(cvd->vdev_isspare);
39403377Seschrock 		spa_spare_remove(cvd);
39412082Seschrock 		unspare_guid = cvd->vdev_guid;
39428241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
39432082Seschrock 	}
39442082Seschrock 
39452082Seschrock 	/*
3946789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3947789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3948789Sahrens 	 */
3949789Sahrens 	if (pvd->vdev_children == 1)
3950789Sahrens 		vdev_remove_parent(cvd);
3951789Sahrens 
3952789Sahrens 	/*
3953789Sahrens 	 * We don't set tvd until now because the parent we just removed
3954789Sahrens 	 * may have been the previous top-level vdev.
3955789Sahrens 	 */
3956789Sahrens 	tvd = cvd->vdev_top;
3957789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3958789Sahrens 
3959789Sahrens 	/*
39603377Seschrock 	 * Reevaluate the parent vdev state.
3961789Sahrens 	 */
39624451Seschrock 	vdev_propagate_state(cvd);
3963789Sahrens 
3964789Sahrens 	/*
39659816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
39669816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
39679816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
39689816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
39699816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3970789Sahrens 	 */
39719816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
39729816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
39739816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
39749816SGeorge.Wilson@Sun.COM 	}
3975789Sahrens 
3976789Sahrens 	vdev_config_dirty(tvd);
3977789Sahrens 
3978789Sahrens 	/*
39793377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
39803377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
39813377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
39823377Seschrock 	 * prevent vd from being accessed after it's freed.
3983789Sahrens 	 */
398411422SMark.Musante@Sun.COM 	vdpath = spa_strdup(vd->vdev_path);
39858241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3986789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
39871732Sbonwick 	vd->vdev_detached = B_TRUE;
39881732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3989789Sahrens 
39904451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
39914451Seschrock 
39922082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
39932082Seschrock 
399411422SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_DETACH, spa, NULL, CRED(),
399511422SMark.Musante@Sun.COM 	    "vdev=%s", vdpath);
399611422SMark.Musante@Sun.COM 	spa_strfree(vdpath);
399711422SMark.Musante@Sun.COM 
39982082Seschrock 	/*
39993377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
40003377Seschrock 	 * then we want to go through and remove the device from the hot spare
40013377Seschrock 	 * list of every other pool.
40022082Seschrock 	 */
40032082Seschrock 	if (unspare) {
40048241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
40052082Seschrock 		spa = NULL;
40062082Seschrock 		mutex_enter(&spa_namespace_lock);
40072082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
40082082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
40092082Seschrock 				continue;
40108241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
40118241SJeff.Bonwick@Sun.COM 				continue;
40127793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
40137793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
40142082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
40157793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
40167793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
40172082Seschrock 		}
40182082Seschrock 		mutex_exit(&spa_namespace_lock);
40192082Seschrock 	}
40202082Seschrock 
40212082Seschrock 	return (error);
40222082Seschrock }
40232082Seschrock 
402411422SMark.Musante@Sun.COM /*
402511422SMark.Musante@Sun.COM  * Split a set of devices from their mirrors, and create a new pool from them.
402611422SMark.Musante@Sun.COM  */
402711422SMark.Musante@Sun.COM int
402811422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
402911422SMark.Musante@Sun.COM     nvlist_t *props, boolean_t exp)
403011422SMark.Musante@Sun.COM {
403111422SMark.Musante@Sun.COM 	int error = 0;
403211422SMark.Musante@Sun.COM 	uint64_t txg, *glist;
403311422SMark.Musante@Sun.COM 	spa_t *newspa;
403411422SMark.Musante@Sun.COM 	uint_t c, children, lastlog;
403511422SMark.Musante@Sun.COM 	nvlist_t **child, *nvl, *tmp;
403611422SMark.Musante@Sun.COM 	dmu_tx_t *tx;
403711422SMark.Musante@Sun.COM 	char *altroot = NULL;
403811422SMark.Musante@Sun.COM 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
403911422SMark.Musante@Sun.COM 	boolean_t activate_slog;
404011422SMark.Musante@Sun.COM 
404111422SMark.Musante@Sun.COM 	if (!spa_writeable(spa))
404211422SMark.Musante@Sun.COM 		return (EROFS);
404311422SMark.Musante@Sun.COM 
404411422SMark.Musante@Sun.COM 	txg = spa_vdev_enter(spa);
404511422SMark.Musante@Sun.COM 
404611422SMark.Musante@Sun.COM 	/* clear the log and flush everything up to now */
404711422SMark.Musante@Sun.COM 	activate_slog = spa_passivate_log(spa);
404811422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
404911422SMark.Musante@Sun.COM 	error = spa_offline_log(spa);
405011422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
405111422SMark.Musante@Sun.COM 
405211422SMark.Musante@Sun.COM 	if (activate_slog)
405311422SMark.Musante@Sun.COM 		spa_activate_log(spa);
405411422SMark.Musante@Sun.COM 
405511422SMark.Musante@Sun.COM 	if (error != 0)
405611422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
405711422SMark.Musante@Sun.COM 
405811422SMark.Musante@Sun.COM 	/* check new spa name before going any further */
405911422SMark.Musante@Sun.COM 	if (spa_lookup(newname) != NULL)
406011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
406111422SMark.Musante@Sun.COM 
406211422SMark.Musante@Sun.COM 	/*
406311422SMark.Musante@Sun.COM 	 * scan through all the children to ensure they're all mirrors
406411422SMark.Musante@Sun.COM 	 */
406511422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
406611422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
406711422SMark.Musante@Sun.COM 	    &children) != 0)
406811422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
406911422SMark.Musante@Sun.COM 
407011422SMark.Musante@Sun.COM 	/* first, check to ensure we've got the right child count */
407111422SMark.Musante@Sun.COM 	rvd = spa->spa_root_vdev;
407211422SMark.Musante@Sun.COM 	lastlog = 0;
407311422SMark.Musante@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
407411422SMark.Musante@Sun.COM 		vdev_t *vd = rvd->vdev_child[c];
407511422SMark.Musante@Sun.COM 
407611422SMark.Musante@Sun.COM 		/* don't count the holes & logs as children */
407711422SMark.Musante@Sun.COM 		if (vd->vdev_islog || vd->vdev_ishole) {
407811422SMark.Musante@Sun.COM 			if (lastlog == 0)
407911422SMark.Musante@Sun.COM 				lastlog = c;
408011422SMark.Musante@Sun.COM 			continue;
408111422SMark.Musante@Sun.COM 		}
408211422SMark.Musante@Sun.COM 
408311422SMark.Musante@Sun.COM 		lastlog = 0;
408411422SMark.Musante@Sun.COM 	}
408511422SMark.Musante@Sun.COM 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
408611422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
408711422SMark.Musante@Sun.COM 
408811422SMark.Musante@Sun.COM 	/* next, ensure no spare or cache devices are part of the split */
408911422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
409011422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
409111422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
409211422SMark.Musante@Sun.COM 
409311422SMark.Musante@Sun.COM 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
409411422SMark.Musante@Sun.COM 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
409511422SMark.Musante@Sun.COM 
409611422SMark.Musante@Sun.COM 	/* then, loop over each vdev and validate it */
409711422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
409811422SMark.Musante@Sun.COM 		uint64_t is_hole = 0;
409911422SMark.Musante@Sun.COM 
410011422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
410111422SMark.Musante@Sun.COM 		    &is_hole);
410211422SMark.Musante@Sun.COM 
410311422SMark.Musante@Sun.COM 		if (is_hole != 0) {
410411422SMark.Musante@Sun.COM 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
410511422SMark.Musante@Sun.COM 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
410611422SMark.Musante@Sun.COM 				continue;
410711422SMark.Musante@Sun.COM 			} else {
410811422SMark.Musante@Sun.COM 				error = EINVAL;
410911422SMark.Musante@Sun.COM 				break;
411011422SMark.Musante@Sun.COM 			}
411111422SMark.Musante@Sun.COM 		}
411211422SMark.Musante@Sun.COM 
411311422SMark.Musante@Sun.COM 		/* which disk is going to be split? */
411411422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
411511422SMark.Musante@Sun.COM 		    &glist[c]) != 0) {
411611422SMark.Musante@Sun.COM 			error = EINVAL;
411711422SMark.Musante@Sun.COM 			break;
411811422SMark.Musante@Sun.COM 		}
411911422SMark.Musante@Sun.COM 
412011422SMark.Musante@Sun.COM 		/* look it up in the spa */
412111422SMark.Musante@Sun.COM 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
412211422SMark.Musante@Sun.COM 		if (vml[c] == NULL) {
412311422SMark.Musante@Sun.COM 			error = ENODEV;
412411422SMark.Musante@Sun.COM 			break;
412511422SMark.Musante@Sun.COM 		}
412611422SMark.Musante@Sun.COM 
412711422SMark.Musante@Sun.COM 		/* make sure there's nothing stopping the split */
412811422SMark.Musante@Sun.COM 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
412911422SMark.Musante@Sun.COM 		    vml[c]->vdev_islog ||
413011422SMark.Musante@Sun.COM 		    vml[c]->vdev_ishole ||
413111422SMark.Musante@Sun.COM 		    vml[c]->vdev_isspare ||
413211422SMark.Musante@Sun.COM 		    vml[c]->vdev_isl2cache ||
413311422SMark.Musante@Sun.COM 		    !vdev_writeable(vml[c]) ||
4134*11497SMark.Musante@Sun.COM 		    vml[c]->vdev_children != 0 ||
413511422SMark.Musante@Sun.COM 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
413611422SMark.Musante@Sun.COM 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
413711422SMark.Musante@Sun.COM 			error = EINVAL;
413811422SMark.Musante@Sun.COM 			break;
413911422SMark.Musante@Sun.COM 		}
414011422SMark.Musante@Sun.COM 
414111422SMark.Musante@Sun.COM 		if (vdev_dtl_required(vml[c])) {
414211422SMark.Musante@Sun.COM 			error = EBUSY;
414311422SMark.Musante@Sun.COM 			break;
414411422SMark.Musante@Sun.COM 		}
414511422SMark.Musante@Sun.COM 
414611422SMark.Musante@Sun.COM 		/* we need certain info from the top level */
414711422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
414811422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_array) == 0);
414911422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
415011422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
415111422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
415211422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_asize) == 0);
415311422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
415411422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ashift) == 0);
415511422SMark.Musante@Sun.COM 	}
415611422SMark.Musante@Sun.COM 
415711422SMark.Musante@Sun.COM 	if (error != 0) {
415811422SMark.Musante@Sun.COM 		kmem_free(vml, children * sizeof (vdev_t *));
415911422SMark.Musante@Sun.COM 		kmem_free(glist, children * sizeof (uint64_t));
416011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
416111422SMark.Musante@Sun.COM 	}
416211422SMark.Musante@Sun.COM 
416311422SMark.Musante@Sun.COM 	/* stop writers from using the disks */
416411422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
416511422SMark.Musante@Sun.COM 		if (vml[c] != NULL)
416611422SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_TRUE;
416711422SMark.Musante@Sun.COM 	}
416811422SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
416911422SMark.Musante@Sun.COM 
417011422SMark.Musante@Sun.COM 	/*
417111422SMark.Musante@Sun.COM 	 * Temporarily record the splitting vdevs in the spa config.  This
417211422SMark.Musante@Sun.COM 	 * will disappear once the config is regenerated.
417311422SMark.Musante@Sun.COM 	 */
417411422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
417511422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
417611422SMark.Musante@Sun.COM 	    glist, children) == 0);
417711422SMark.Musante@Sun.COM 	kmem_free(glist, children * sizeof (uint64_t));
417811422SMark.Musante@Sun.COM 
417911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
418011422SMark.Musante@Sun.COM 	    nvl) == 0);
418111422SMark.Musante@Sun.COM 	spa->spa_config_splitting = nvl;
418211422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
418311422SMark.Musante@Sun.COM 
418411422SMark.Musante@Sun.COM 	/* configure and create the new pool */
418511422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
418611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
418711422SMark.Musante@Sun.COM 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
418811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
418911422SMark.Musante@Sun.COM 	    spa_version(spa)) == 0);
419011422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
419111422SMark.Musante@Sun.COM 	    spa->spa_config_txg) == 0);
419211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
419311422SMark.Musante@Sun.COM 	    spa_generate_guid(NULL)) == 0);
419411422SMark.Musante@Sun.COM 	(void) nvlist_lookup_string(props,
419511422SMark.Musante@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
419611422SMark.Musante@Sun.COM 
4197*11497SMark.Musante@Sun.COM 	/* add the new pool to the namespace */
419811422SMark.Musante@Sun.COM 	newspa = spa_add(newname, config, altroot);
419911422SMark.Musante@Sun.COM 	newspa->spa_config_txg = spa->spa_config_txg;
420011422SMark.Musante@Sun.COM 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
420111422SMark.Musante@Sun.COM 
420211422SMark.Musante@Sun.COM 	/* release the spa config lock, retaining the namespace lock */
420311422SMark.Musante@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
420411422SMark.Musante@Sun.COM 
420511422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
420611422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 1);
420711422SMark.Musante@Sun.COM 
420811422SMark.Musante@Sun.COM 	spa_activate(newspa, spa_mode_global);
420911422SMark.Musante@Sun.COM 	spa_async_suspend(newspa);
421011422SMark.Musante@Sun.COM 
421111422SMark.Musante@Sun.COM 	/* create the new pool from the disks of the original pool */
421211422SMark.Musante@Sun.COM 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
421311422SMark.Musante@Sun.COM 	if (error)
421411422SMark.Musante@Sun.COM 		goto out;
421511422SMark.Musante@Sun.COM 
421611422SMark.Musante@Sun.COM 	/* if that worked, generate a real config for the new pool */
421711422SMark.Musante@Sun.COM 	if (newspa->spa_root_vdev != NULL) {
421811422SMark.Musante@Sun.COM 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
421911422SMark.Musante@Sun.COM 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
422011422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
422111422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
422211422SMark.Musante@Sun.COM 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
422311422SMark.Musante@Sun.COM 		    B_TRUE));
422411422SMark.Musante@Sun.COM 	}
422511422SMark.Musante@Sun.COM 
422611422SMark.Musante@Sun.COM 	/* set the props */
422711422SMark.Musante@Sun.COM 	if (props != NULL) {
422811422SMark.Musante@Sun.COM 		spa_configfile_set(newspa, props, B_FALSE);
422911422SMark.Musante@Sun.COM 		error = spa_prop_set(newspa, props);
423011422SMark.Musante@Sun.COM 		if (error)
423111422SMark.Musante@Sun.COM 			goto out;
423211422SMark.Musante@Sun.COM 	}
423311422SMark.Musante@Sun.COM 
423411422SMark.Musante@Sun.COM 	/* flush everything */
423511422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(newspa);
423611422SMark.Musante@Sun.COM 	vdev_config_dirty(newspa->spa_root_vdev);
423711422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
423811422SMark.Musante@Sun.COM 
423911422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
424011422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 2);
424111422SMark.Musante@Sun.COM 
424211422SMark.Musante@Sun.COM 	spa_async_resume(newspa);
424311422SMark.Musante@Sun.COM 
424411422SMark.Musante@Sun.COM 	/* finally, update the original pool's config */
424511422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
424611422SMark.Musante@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
424711422SMark.Musante@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
424811422SMark.Musante@Sun.COM 	if (error != 0)
424911422SMark.Musante@Sun.COM 		dmu_tx_abort(tx);
425011422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
425111422SMark.Musante@Sun.COM 		if (vml[c] != NULL) {
425211422SMark.Musante@Sun.COM 			vdev_split(vml[c]);
425311422SMark.Musante@Sun.COM 			if (error == 0)
425411422SMark.Musante@Sun.COM 				spa_history_internal_log(LOG_POOL_VDEV_DETACH,
425511422SMark.Musante@Sun.COM 				    spa, tx, CRED(), "vdev=%s",
425611422SMark.Musante@Sun.COM 				    vml[c]->vdev_path);
425711422SMark.Musante@Sun.COM 			vdev_free(vml[c]);
425811422SMark.Musante@Sun.COM 		}
425911422SMark.Musante@Sun.COM 	}
426011422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
426111422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
426211422SMark.Musante@Sun.COM 	nvlist_free(nvl);
426311422SMark.Musante@Sun.COM 	if (error == 0)
426411422SMark.Musante@Sun.COM 		dmu_tx_commit(tx);
426511422SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, 0);
426611422SMark.Musante@Sun.COM 
426711422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
426811422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 3);
426911422SMark.Musante@Sun.COM 
427011422SMark.Musante@Sun.COM 	/* split is complete; log a history record */
427111422SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_SPLIT, newspa, NULL, CRED(),
427211422SMark.Musante@Sun.COM 	    "split new pool %s from pool %s", newname, spa_name(spa));
427311422SMark.Musante@Sun.COM 
427411422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
427511422SMark.Musante@Sun.COM 
427611422SMark.Musante@Sun.COM 	/* if we're not going to mount the filesystems in userland, export */
427711422SMark.Musante@Sun.COM 	if (exp)
427811422SMark.Musante@Sun.COM 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
427911422SMark.Musante@Sun.COM 		    B_FALSE, B_FALSE);
428011422SMark.Musante@Sun.COM 
428111422SMark.Musante@Sun.COM 	return (error);
428211422SMark.Musante@Sun.COM 
428311422SMark.Musante@Sun.COM out:
428411422SMark.Musante@Sun.COM 	spa_unload(newspa);
428511422SMark.Musante@Sun.COM 	spa_deactivate(newspa);
428611422SMark.Musante@Sun.COM 	spa_remove(newspa);
428711422SMark.Musante@Sun.COM 
428811422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
428911422SMark.Musante@Sun.COM 	nvlist_free(spa->spa_config_splitting);
429011422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
4291*11497SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, error);
429211422SMark.Musante@Sun.COM 
429311422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
429411422SMark.Musante@Sun.COM 	return (error);
429511422SMark.Musante@Sun.COM }
429611422SMark.Musante@Sun.COM 
42977754SJeff.Bonwick@Sun.COM static nvlist_t *
42987754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
42992082Seschrock {
43007754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
43017754SJeff.Bonwick@Sun.COM 		uint64_t guid;
43027754SJeff.Bonwick@Sun.COM 
43037754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
43047754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
43057754SJeff.Bonwick@Sun.COM 
43067754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
43077754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
43082082Seschrock 	}
43092082Seschrock 
43107754SJeff.Bonwick@Sun.COM 	return (NULL);
43115450Sbrendan }
43125450Sbrendan 
43137754SJeff.Bonwick@Sun.COM static void
43147754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
43157754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
43165450Sbrendan {
43177754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
43187754SJeff.Bonwick@Sun.COM 
43197754SJeff.Bonwick@Sun.COM 	if (count > 1)
43207754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
43217754SJeff.Bonwick@Sun.COM 
43227754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
43237754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
43247754SJeff.Bonwick@Sun.COM 			continue;
43257754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
43265450Sbrendan 	}
43275450Sbrendan 
43287754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
43297754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
43307754SJeff.Bonwick@Sun.COM 
43317754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
43327754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
43337754SJeff.Bonwick@Sun.COM 
43347754SJeff.Bonwick@Sun.COM 	if (count > 1)
43357754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
43365450Sbrendan }
43375450Sbrendan 
43385450Sbrendan /*
433910594SGeorge.Wilson@Sun.COM  * Removing a device from the vdev namespace requires several steps
434010594SGeorge.Wilson@Sun.COM  * and can take a significant amount of time.  As a result we use
434110594SGeorge.Wilson@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
434210594SGeorge.Wilson@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
434310594SGeorge.Wilson@Sun.COM  * lock.  During each step the configuration is synced out.
434410594SGeorge.Wilson@Sun.COM  */
434510594SGeorge.Wilson@Sun.COM 
434610594SGeorge.Wilson@Sun.COM /*
434710594SGeorge.Wilson@Sun.COM  * Evacuate the device.
434810594SGeorge.Wilson@Sun.COM  */
434910594SGeorge.Wilson@Sun.COM int
435010594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
435110594SGeorge.Wilson@Sun.COM {
435210974SJeff.Bonwick@Sun.COM 	int error = 0;
435310594SGeorge.Wilson@Sun.COM 	uint64_t txg;
435410594SGeorge.Wilson@Sun.COM 
435510594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
435610594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
435710922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
435810594SGeorge.Wilson@Sun.COM 
435910594SGeorge.Wilson@Sun.COM 	/*
436010594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
436110594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
436210594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
436310594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
436410594SGeorge.Wilson@Sun.COM 	 */
436510594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
436610974SJeff.Bonwick@Sun.COM 		error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
436710974SJeff.Bonwick@Sun.COM 		    NULL, DS_FIND_CHILDREN);
436810974SJeff.Bonwick@Sun.COM 	} else {
436910974SJeff.Bonwick@Sun.COM 		error = ENOTSUP;	/* until we have bp rewrite */
437010594SGeorge.Wilson@Sun.COM 	}
437110594SGeorge.Wilson@Sun.COM 
437210974SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
437310974SJeff.Bonwick@Sun.COM 
437410974SJeff.Bonwick@Sun.COM 	if (error)
437510974SJeff.Bonwick@Sun.COM 		return (error);
437610974SJeff.Bonwick@Sun.COM 
437710594SGeorge.Wilson@Sun.COM 	/*
437810974SJeff.Bonwick@Sun.COM 	 * The evacuation succeeded.  Remove any remaining MOS metadata
437910974SJeff.Bonwick@Sun.COM 	 * associated with this vdev, and wait for these changes to sync.
438010594SGeorge.Wilson@Sun.COM 	 */
438110594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
438210594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
438310594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
438410594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
438510594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
438610594SGeorge.Wilson@Sun.COM 
438710594SGeorge.Wilson@Sun.COM 	return (0);
438810594SGeorge.Wilson@Sun.COM }
438910594SGeorge.Wilson@Sun.COM 
439010594SGeorge.Wilson@Sun.COM /*
439110594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
439210594SGeorge.Wilson@Sun.COM  */
439310594SGeorge.Wilson@Sun.COM void
439410974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
439510594SGeorge.Wilson@Sun.COM {
439610594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
439710594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
439810594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
439910594SGeorge.Wilson@Sun.COM 
440010594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
440110594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
440210922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
440310594SGeorge.Wilson@Sun.COM 
440410594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
440510922SJeff.Bonwick@Sun.COM 
440610922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
440710922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
440810922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
440910922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
441010922SJeff.Bonwick@Sun.COM 
441110594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
441210594SGeorge.Wilson@Sun.COM 
441310594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
441410594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
441510594SGeorge.Wilson@Sun.COM 	} else {
441610594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
441710594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
441810594SGeorge.Wilson@Sun.COM 	}
441910594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(rvd);
442010594SGeorge.Wilson@Sun.COM 
442110594SGeorge.Wilson@Sun.COM 	/*
442210594SGeorge.Wilson@Sun.COM 	 * Reassess the health of our root vdev.
442310594SGeorge.Wilson@Sun.COM 	 */
442410594SGeorge.Wilson@Sun.COM 	vdev_reopen(rvd);
442510594SGeorge.Wilson@Sun.COM }
442610594SGeorge.Wilson@Sun.COM 
442710594SGeorge.Wilson@Sun.COM /*
44285450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
442910594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
44305450Sbrendan  */
44315450Sbrendan int
44325450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
44335450Sbrendan {
44345450Sbrendan 	vdev_t *vd;
443510974SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
44367754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
443710594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
44385450Sbrendan 	uint_t nspares, nl2cache;
44395450Sbrendan 	int error = 0;
44408241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
44418241SJeff.Bonwick@Sun.COM 
44428241SJeff.Bonwick@Sun.COM 	if (!locked)
44438241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
44445450Sbrendan 
44456643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
44465450Sbrendan 
44475450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
44485450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
44497754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
44507754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
44517754SJeff.Bonwick@Sun.COM 		/*
44527754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
44537754SJeff.Bonwick@Sun.COM 		 * in this pool.
44547754SJeff.Bonwick@Sun.COM 		 */
44557754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
44567754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
44577754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
44587754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
44597754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
44607754SJeff.Bonwick@Sun.COM 		} else {
44617754SJeff.Bonwick@Sun.COM 			error = EBUSY;
44627754SJeff.Bonwick@Sun.COM 		}
44637754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
44645450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
44657754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
44667754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
44677754SJeff.Bonwick@Sun.COM 		/*
44687754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
44697754SJeff.Bonwick@Sun.COM 		 */
44707754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
44717754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
44725450Sbrendan 		spa_load_l2cache(spa);
44735450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
447410594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
447510594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
447610922SJeff.Bonwick@Sun.COM 		ASSERT(vd == vd->vdev_top);
447710594SGeorge.Wilson@Sun.COM 
447810594SGeorge.Wilson@Sun.COM 		/*
447910594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
448010594SGeorge.Wilson@Sun.COM 		 * become the common case.
448110594SGeorge.Wilson@Sun.COM 		 */
448210594SGeorge.Wilson@Sun.COM 
448310974SJeff.Bonwick@Sun.COM 		mg = vd->vdev_mg;
448410974SJeff.Bonwick@Sun.COM 
448510594SGeorge.Wilson@Sun.COM 		/*
448610974SJeff.Bonwick@Sun.COM 		 * Stop allocating from this vdev.
448710594SGeorge.Wilson@Sun.COM 		 */
448810974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(mg);
448910594SGeorge.Wilson@Sun.COM 
449010922SJeff.Bonwick@Sun.COM 		/*
449110922SJeff.Bonwick@Sun.COM 		 * Wait for the youngest allocations and frees to sync,
449210922SJeff.Bonwick@Sun.COM 		 * and then wait for the deferral of those frees to finish.
449310922SJeff.Bonwick@Sun.COM 		 */
449410922SJeff.Bonwick@Sun.COM 		spa_vdev_config_exit(spa, NULL,
449510922SJeff.Bonwick@Sun.COM 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
449610922SJeff.Bonwick@Sun.COM 
449710974SJeff.Bonwick@Sun.COM 		/*
449810974SJeff.Bonwick@Sun.COM 		 * Attempt to evacuate the vdev.
449910974SJeff.Bonwick@Sun.COM 		 */
450010974SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove_evacuate(spa, vd);
450110974SJeff.Bonwick@Sun.COM 
450210594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
450310594SGeorge.Wilson@Sun.COM 
450410974SJeff.Bonwick@Sun.COM 		/*
450510974SJeff.Bonwick@Sun.COM 		 * If we couldn't evacuate the vdev, unwind.
450610974SJeff.Bonwick@Sun.COM 		 */
450710974SJeff.Bonwick@Sun.COM 		if (error) {
450810974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
450910974SJeff.Bonwick@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
451010974SJeff.Bonwick@Sun.COM 		}
451110974SJeff.Bonwick@Sun.COM 
451210974SJeff.Bonwick@Sun.COM 		/*
451310974SJeff.Bonwick@Sun.COM 		 * Clean up the vdev namespace.
451410974SJeff.Bonwick@Sun.COM 		 */
451510974SJeff.Bonwick@Sun.COM 		spa_vdev_remove_from_namespace(spa, vd);
451610594SGeorge.Wilson@Sun.COM 
45177754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
45187754SJeff.Bonwick@Sun.COM 		/*
45197754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
45207754SJeff.Bonwick@Sun.COM 		 */
45217754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
45227754SJeff.Bonwick@Sun.COM 	} else {
45237754SJeff.Bonwick@Sun.COM 		/*
45247754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
45257754SJeff.Bonwick@Sun.COM 		 */
45267754SJeff.Bonwick@Sun.COM 		error = ENOENT;
45275450Sbrendan 	}
45282082Seschrock 
45298241SJeff.Bonwick@Sun.COM 	if (!locked)
45308241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
45318241SJeff.Bonwick@Sun.COM 
45328241SJeff.Bonwick@Sun.COM 	return (error);
4533789Sahrens }
4534789Sahrens 
4535789Sahrens /*
45364451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
45374451Seschrock  * current spared, so we can detach it.
4538789Sahrens  */
45391544Seschrock static vdev_t *
45404451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
4541789Sahrens {
45421544Seschrock 	vdev_t *newvd, *oldvd;
45439816SGeorge.Wilson@Sun.COM 
45449816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
45454451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
45461544Seschrock 		if (oldvd != NULL)
45471544Seschrock 			return (oldvd);
45481544Seschrock 	}
4549789Sahrens 
45504451Seschrock 	/*
45514451Seschrock 	 * Check for a completed replacement.
45524451Seschrock 	 */
4553789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
45541544Seschrock 		oldvd = vd->vdev_child[0];
45551544Seschrock 		newvd = vd->vdev_child[1];
4556789Sahrens 
45578241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
45588241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
45591544Seschrock 			return (oldvd);
45601544Seschrock 	}
4561789Sahrens 
45624451Seschrock 	/*
45634451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
45644451Seschrock 	 */
45654451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
45664451Seschrock 		newvd = vd->vdev_child[0];
45674451Seschrock 		oldvd = vd->vdev_child[1];
45684451Seschrock 
45694451Seschrock 		if (newvd->vdev_unspare &&
45708241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
45718241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
45724451Seschrock 			newvd->vdev_unspare = 0;
45734451Seschrock 			return (oldvd);
45744451Seschrock 		}
45754451Seschrock 	}
45764451Seschrock 
45771544Seschrock 	return (NULL);
4578789Sahrens }
4579789Sahrens 
45801544Seschrock static void
45814451Seschrock spa_vdev_resilver_done(spa_t *spa)
4582789Sahrens {
45838241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
45848241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
45858241SJeff.Bonwick@Sun.COM 
45868241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4587789Sahrens 
45884451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
45898241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
45908241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
45911544Seschrock 		guid = vd->vdev_guid;
45928241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
45938241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
45948241SJeff.Bonwick@Sun.COM 		sguid = 0;
45952082Seschrock 		/*
45962082Seschrock 		 * If we have just finished replacing a hot spared device, then
45972082Seschrock 		 * we need to detach the parent's first child (the original hot
45982082Seschrock 		 * spare) as well.
45992082Seschrock 		 */
46008241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
46012082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
46028241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
46038241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
46042082Seschrock 		}
46058241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
46068241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
46071544Seschrock 			return;
46088241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
46092082Seschrock 			return;
46108241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4611789Sahrens 	}
4612789Sahrens 
46138241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4614789Sahrens }
4615789Sahrens 
4616789Sahrens /*
461711041SEric.Taylor@Sun.COM  * Update the stored path or FRU for this vdev.
46181354Seschrock  */
46191354Seschrock int
46209425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
46219425SEric.Schrock@Sun.COM     boolean_t ispath)
46221354Seschrock {
46236643Seschrock 	vdev_t *vd;
462411041SEric.Taylor@Sun.COM 
462511041SEric.Taylor@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALL);
46261354Seschrock 
46279425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
462811041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
46291354Seschrock 
46301585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
463111041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
46321585Sbonwick 
46339425SEric.Schrock@Sun.COM 	if (ispath) {
46349425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_path);
46359425SEric.Schrock@Sun.COM 		vd->vdev_path = spa_strdup(value);
46369425SEric.Schrock@Sun.COM 	} else {
46379425SEric.Schrock@Sun.COM 		if (vd->vdev_fru != NULL)
46389425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
46399425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(value);
46409425SEric.Schrock@Sun.COM 	}
46411354Seschrock 
464211041SEric.Taylor@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
46431354Seschrock }
46441354Seschrock 
46459425SEric.Schrock@Sun.COM int
46469425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
46479425SEric.Schrock@Sun.COM {
46489425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
46499425SEric.Schrock@Sun.COM }
46509425SEric.Schrock@Sun.COM 
46519425SEric.Schrock@Sun.COM int
46529425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
46539425SEric.Schrock@Sun.COM {
46549425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
46559425SEric.Schrock@Sun.COM }
46569425SEric.Schrock@Sun.COM 
46571354Seschrock /*
4658789Sahrens  * ==========================================================================
4659789Sahrens  * SPA Scrubbing
4660789Sahrens  * ==========================================================================
4661789Sahrens  */
4662789Sahrens 
46637046Sahrens int
46647046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
4665789Sahrens {
46667754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
46674808Sek110237 
4668789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
4669789Sahrens 		return (ENOTSUP);
4670789Sahrens 
4671789Sahrens 	/*
46727046Sahrens 	 * If a resilver was requested, but there is no DTL on a
46737046Sahrens 	 * writeable leaf device, we have nothing to do.
4674789Sahrens 	 */
46757046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
46767046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
46777046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
46781544Seschrock 		return (0);
46791544Seschrock 	}
4680789Sahrens 
46817046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
46827046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
46837046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
46847046Sahrens 		return (EBUSY);
46857046Sahrens 
46867046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
46877046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
46887046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
46897046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
46901544Seschrock 	} else {
46917046Sahrens 		return (EINVAL);
46921544Seschrock 	}
4693789Sahrens }
4694789Sahrens 
46951544Seschrock /*
46961544Seschrock  * ==========================================================================
46971544Seschrock  * SPA async task processing
46981544Seschrock  * ==========================================================================
46991544Seschrock  */
47001544Seschrock 
47011544Seschrock static void
47024451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4703789Sahrens {
47047361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
47057361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
47067361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
470710575SEric.Schrock@Sun.COM 
470810575SEric.Schrock@Sun.COM 		/*
470910575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
471010575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
471110575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
471210575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
471310575SEric.Schrock@Sun.COM 		 */
471410575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
471510575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
471610575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
471710575SEric.Schrock@Sun.COM 
47187754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
47191544Seschrock 	}
47207361SBrendan.Gregg@Sun.COM 
47217754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47227361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
47231544Seschrock }
47241544Seschrock 
47251544Seschrock static void
47267754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
47277754SJeff.Bonwick@Sun.COM {
47287754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
47297754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
47307754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
47317754SJeff.Bonwick@Sun.COM 	}
47327754SJeff.Bonwick@Sun.COM 
47337754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47347754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
47357754SJeff.Bonwick@Sun.COM }
47367754SJeff.Bonwick@Sun.COM 
47377754SJeff.Bonwick@Sun.COM static void
47389816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
47399816SGeorge.Wilson@Sun.COM {
47409816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
47419816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
47429816SGeorge.Wilson@Sun.COM 	char *physpath;
47439816SGeorge.Wilson@Sun.COM 
47449816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
47459816SGeorge.Wilson@Sun.COM 		return;
47469816SGeorge.Wilson@Sun.COM 
47479816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
47489816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
47499816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
47509816SGeorge.Wilson@Sun.COM 	}
47519816SGeorge.Wilson@Sun.COM 
47529816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
47539816SGeorge.Wilson@Sun.COM 		return;
47549816SGeorge.Wilson@Sun.COM 
47559816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
47569816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
47579816SGeorge.Wilson@Sun.COM 
47589816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
47599816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
47609816SGeorge.Wilson@Sun.COM 
47619816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
47629816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
47639816SGeorge.Wilson@Sun.COM 
47649816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
47659816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
47669816SGeorge.Wilson@Sun.COM }
47679816SGeorge.Wilson@Sun.COM 
47689816SGeorge.Wilson@Sun.COM static void
47691544Seschrock spa_async_thread(spa_t *spa)
47701544Seschrock {
47717754SJeff.Bonwick@Sun.COM 	int tasks;
47721544Seschrock 
47731544Seschrock 	ASSERT(spa->spa_sync_on);
4774789Sahrens 
47751544Seschrock 	mutex_enter(&spa->spa_async_lock);
47761544Seschrock 	tasks = spa->spa_async_tasks;
47771544Seschrock 	spa->spa_async_tasks = 0;
47781544Seschrock 	mutex_exit(&spa->spa_async_lock);
47791544Seschrock 
47801544Seschrock 	/*
47811635Sbonwick 	 * See if the config needs to be updated.
47821635Sbonwick 	 */
47831635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
478410922SJeff.Bonwick@Sun.COM 		uint64_t old_space, new_space;
47859816SGeorge.Wilson@Sun.COM 
47861635Sbonwick 		mutex_enter(&spa_namespace_lock);
478710922SJeff.Bonwick@Sun.COM 		old_space = metaslab_class_get_space(spa_normal_class(spa));
47881635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
478910922SJeff.Bonwick@Sun.COM 		new_space = metaslab_class_get_space(spa_normal_class(spa));
47901635Sbonwick 		mutex_exit(&spa_namespace_lock);
47919816SGeorge.Wilson@Sun.COM 
47929816SGeorge.Wilson@Sun.COM 		/*
47939816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
47949816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
47959816SGeorge.Wilson@Sun.COM 		 */
479610922SJeff.Bonwick@Sun.COM 		if (new_space != old_space) {
47979946SMark.Musante@Sun.COM 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
47989946SMark.Musante@Sun.COM 			    spa, NULL, CRED(),
47999946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
480010922SJeff.Bonwick@Sun.COM 			    spa_name(spa), new_space, new_space - old_space);
48019816SGeorge.Wilson@Sun.COM 		}
48021635Sbonwick 	}
48031635Sbonwick 
48041635Sbonwick 	/*
48054451Seschrock 	 * See if any devices need to be marked REMOVED.
48061544Seschrock 	 */
48077754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
480810685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48094451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
48107754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
48117361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
48127754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
48137361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
48147754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48157754SJeff.Bonwick@Sun.COM 	}
48167754SJeff.Bonwick@Sun.COM 
48179816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
48189816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
48199816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
48209816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
48219816SGeorge.Wilson@Sun.COM 	}
48229816SGeorge.Wilson@Sun.COM 
48237754SJeff.Bonwick@Sun.COM 	/*
48247754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
48257754SJeff.Bonwick@Sun.COM 	 */
48267754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
482710685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48287754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
48297754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48304451Seschrock 	}
48311544Seschrock 
48321544Seschrock 	/*
48331544Seschrock 	 * If any devices are done replacing, detach them.
48341544Seschrock 	 */
48354451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
48364451Seschrock 		spa_vdev_resilver_done(spa);
4837789Sahrens 
48381544Seschrock 	/*
48391544Seschrock 	 * Kick off a resilver.
48401544Seschrock 	 */
48417046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
48427046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
48431544Seschrock 
48441544Seschrock 	/*
48451544Seschrock 	 * Let the world know that we're done.
48461544Seschrock 	 */
48471544Seschrock 	mutex_enter(&spa->spa_async_lock);
48481544Seschrock 	spa->spa_async_thread = NULL;
48491544Seschrock 	cv_broadcast(&spa->spa_async_cv);
48501544Seschrock 	mutex_exit(&spa->spa_async_lock);
48511544Seschrock 	thread_exit();
48521544Seschrock }
48531544Seschrock 
48541544Seschrock void
48551544Seschrock spa_async_suspend(spa_t *spa)
48561544Seschrock {
48571544Seschrock 	mutex_enter(&spa->spa_async_lock);
48581544Seschrock 	spa->spa_async_suspended++;
48591544Seschrock 	while (spa->spa_async_thread != NULL)
48601544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
48611544Seschrock 	mutex_exit(&spa->spa_async_lock);
48621544Seschrock }
48631544Seschrock 
48641544Seschrock void
48651544Seschrock spa_async_resume(spa_t *spa)
48661544Seschrock {
48671544Seschrock 	mutex_enter(&spa->spa_async_lock);
48681544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
48691544Seschrock 	spa->spa_async_suspended--;
48701544Seschrock 	mutex_exit(&spa->spa_async_lock);
48711544Seschrock }
48721544Seschrock 
48731544Seschrock static void
48741544Seschrock spa_async_dispatch(spa_t *spa)
48751544Seschrock {
48761544Seschrock 	mutex_enter(&spa->spa_async_lock);
48771544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
48781635Sbonwick 	    spa->spa_async_thread == NULL &&
48791635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
48801544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
48811544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
48821544Seschrock 	mutex_exit(&spa->spa_async_lock);
48831544Seschrock }
48841544Seschrock 
48851544Seschrock void
48861544Seschrock spa_async_request(spa_t *spa, int task)
48871544Seschrock {
48881544Seschrock 	mutex_enter(&spa->spa_async_lock);
48891544Seschrock 	spa->spa_async_tasks |= task;
48901544Seschrock 	mutex_exit(&spa->spa_async_lock);
4891789Sahrens }
4892789Sahrens 
4893789Sahrens /*
4894789Sahrens  * ==========================================================================
4895789Sahrens  * SPA syncing routines
4896789Sahrens  * ==========================================================================
4897789Sahrens  */
4898789Sahrens static void
489910922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg)
4900789Sahrens {
4901789Sahrens 	blkptr_t blk;
4902789Sahrens 	uint64_t itor = 0;
4903789Sahrens 	uint8_t c = 1;
4904789Sahrens 
49057754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
49067754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
490710922SJeff.Bonwick@Sun.COM 		zio_free(spa, txg, &blk);
49087754SJeff.Bonwick@Sun.COM 	}
4909789Sahrens 
4910789Sahrens 	bplist_vacate(bpl, tx);
4911789Sahrens 
4912789Sahrens 	/*
4913789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4914789Sahrens 	 * (Usually only the first block is needed.)
4915789Sahrens 	 */
491610922SJeff.Bonwick@Sun.COM 	dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx);
491710922SJeff.Bonwick@Sun.COM }
491810922SJeff.Bonwick@Sun.COM 
491910922SJeff.Bonwick@Sun.COM static void
492010922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
492110922SJeff.Bonwick@Sun.COM {
492210922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
492310922SJeff.Bonwick@Sun.COM 
492410922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
492510922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
4926789Sahrens }
4927789Sahrens 
4928789Sahrens static void
49292082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
49302082Seschrock {
49312082Seschrock 	char *packed = NULL;
49327497STim.Haley@Sun.COM 	size_t bufsize;
49332082Seschrock 	size_t nvsize = 0;
49342082Seschrock 	dmu_buf_t *db;
49352082Seschrock 
49362082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
49372082Seschrock 
49387497STim.Haley@Sun.COM 	/*
49397497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
49407497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
49417497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
49427497STim.Haley@Sun.COM 	 */
49437497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
49447497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
49452082Seschrock 
49462082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
49472082Seschrock 	    KM_SLEEP) == 0);
49487497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
49497497STim.Haley@Sun.COM 
49507497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
49517497STim.Haley@Sun.COM 
49527497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
49532082Seschrock 
49542082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
49552082Seschrock 	dmu_buf_will_dirty(db, tx);
49562082Seschrock 	*(uint64_t *)db->db_data = nvsize;
49572082Seschrock 	dmu_buf_rele(db, FTAG);
49582082Seschrock }
49592082Seschrock 
49602082Seschrock static void
49615450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
49625450Sbrendan     const char *config, const char *entry)
49632082Seschrock {
49642082Seschrock 	nvlist_t *nvroot;
49655450Sbrendan 	nvlist_t **list;
49662082Seschrock 	int i;
49672082Seschrock 
49685450Sbrendan 	if (!sav->sav_sync)
49692082Seschrock 		return;
49702082Seschrock 
49712082Seschrock 	/*
49725450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
49735450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
49744451Seschrock 	 * valid and the vdevs are labeled appropriately.
49752082Seschrock 	 */
49765450Sbrendan 	if (sav->sav_object == 0) {
49775450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
49785450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
49795450Sbrendan 		    sizeof (uint64_t), tx);
49802082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
49815450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
49825450Sbrendan 		    &sav->sav_object, tx) == 0);
49832082Seschrock 	}
49842082Seschrock 
49852082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
49865450Sbrendan 	if (sav->sav_count == 0) {
49875450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
49882082Seschrock 	} else {
49895450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
49905450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
49915450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
49925450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
49935450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
49945450Sbrendan 		    sav->sav_count) == 0);
49955450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
49965450Sbrendan 			nvlist_free(list[i]);
49975450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
49982082Seschrock 	}
49992082Seschrock 
50005450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
50012926Sek110237 	nvlist_free(nvroot);
50022082Seschrock 
50035450Sbrendan 	sav->sav_sync = B_FALSE;
50042082Seschrock }
50052082Seschrock 
50062082Seschrock static void
5007789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5008789Sahrens {
5009789Sahrens 	nvlist_t *config;
5010789Sahrens 
50117754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
5012789Sahrens 		return;
5013789Sahrens 
50147754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
50157754SJeff.Bonwick@Sun.COM 
50167754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
50177754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
50187754SJeff.Bonwick@Sun.COM 
50197754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
5020789Sahrens 
50211635Sbonwick 	if (spa->spa_config_syncing)
50221635Sbonwick 		nvlist_free(spa->spa_config_syncing);
50231635Sbonwick 	spa->spa_config_syncing = config;
5024789Sahrens 
50252082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5026789Sahrens }
5027789Sahrens 
50285094Slling /*
50295094Slling  * Set zpool properties.
50305094Slling  */
50313912Slling static void
50324543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
50333912Slling {
50343912Slling 	spa_t *spa = arg1;
50355094Slling 	objset_t *mos = spa->spa_meta_objset;
50363912Slling 	nvlist_t *nvp = arg2;
50375094Slling 	nvpair_t *elem;
50384451Seschrock 	uint64_t intval;
50396643Seschrock 	char *strval;
50405094Slling 	zpool_prop_t prop;
50415094Slling 	const char *propname;
50425094Slling 	zprop_type_t proptype;
50435094Slling 
50447754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
50457754SJeff.Bonwick@Sun.COM 
50465094Slling 	elem = NULL;
50475094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
50485094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
50495094Slling 		case ZPOOL_PROP_VERSION:
50505094Slling 			/*
50515094Slling 			 * Only set version for non-zpool-creation cases
50525094Slling 			 * (set/import). spa_create() needs special care
50535094Slling 			 * for version setting.
50545094Slling 			 */
50555094Slling 			if (tx->tx_txg != TXG_INITIAL) {
50565094Slling 				VERIFY(nvpair_value_uint64(elem,
50575094Slling 				    &intval) == 0);
50585094Slling 				ASSERT(intval <= SPA_VERSION);
50595094Slling 				ASSERT(intval >= spa_version(spa));
50605094Slling 				spa->spa_uberblock.ub_version = intval;
50615094Slling 				vdev_config_dirty(spa->spa_root_vdev);
50625094Slling 			}
50635094Slling 			break;
50645094Slling 
50655094Slling 		case ZPOOL_PROP_ALTROOT:
50665094Slling 			/*
50675094Slling 			 * 'altroot' is a non-persistent property. It should
50685094Slling 			 * have been set temporarily at creation or import time.
50695094Slling 			 */
50705094Slling 			ASSERT(spa->spa_root != NULL);
50715094Slling 			break;
50725094Slling 
50735363Seschrock 		case ZPOOL_PROP_CACHEFILE:
50745094Slling 			/*
50758525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
50765094Slling 			 */
50774543Smarks 			break;
50785094Slling 		default:
50795094Slling 			/*
50805094Slling 			 * Set pool property values in the poolprops mos object.
50815094Slling 			 */
50825094Slling 			if (spa->spa_pool_props_object == 0) {
50835094Slling 				VERIFY((spa->spa_pool_props_object =
50845094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
50855094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
50865094Slling 
50875094Slling 				VERIFY(zap_update(mos,
50885094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
50895094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
50905094Slling 				    == 0);
50915094Slling 			}
50925094Slling 
50935094Slling 			/* normalize the property name */
50945094Slling 			propname = zpool_prop_to_name(prop);
50955094Slling 			proptype = zpool_prop_get_type(prop);
50965094Slling 
50975094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
50985094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
50995094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
51005094Slling 				VERIFY(zap_update(mos,
51015094Slling 				    spa->spa_pool_props_object, propname,
51025094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
51035094Slling 
51045094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
51055094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
51065094Slling 
51075094Slling 				if (proptype == PROP_TYPE_INDEX) {
51085094Slling 					const char *unused;
51095094Slling 					VERIFY(zpool_prop_index_to_string(
51105094Slling 					    prop, intval, &unused) == 0);
51115094Slling 				}
51125094Slling 				VERIFY(zap_update(mos,
51135094Slling 				    spa->spa_pool_props_object, propname,
51145094Slling 				    8, 1, &intval, tx) == 0);
51155094Slling 			} else {
51165094Slling 				ASSERT(0); /* not allowed */
51175094Slling 			}
51185094Slling 
51195329Sgw25295 			switch (prop) {
51205329Sgw25295 			case ZPOOL_PROP_DELEGATION:
51215094Slling 				spa->spa_delegation = intval;
51225329Sgw25295 				break;
51235329Sgw25295 			case ZPOOL_PROP_BOOTFS:
51245094Slling 				spa->spa_bootfs = intval;
51255329Sgw25295 				break;
51265329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
51275329Sgw25295 				spa->spa_failmode = intval;
51285329Sgw25295 				break;
51299816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
51309816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
51319816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
51329816SGeorge.Wilson@Sun.COM 				break;
513310922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
513410922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
513510922SJeff.Bonwick@Sun.COM 				break;
51365329Sgw25295 			default:
51375329Sgw25295 				break;
51385329Sgw25295 			}
51393912Slling 		}
51405094Slling 
51415094Slling 		/* log internal history if this is not a zpool create */
51425094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
51435094Slling 		    tx->tx_txg != TXG_INITIAL) {
51445094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
51455094Slling 			    spa, tx, cr, "%s %lld %s",
51467754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
51475094Slling 		}
51483912Slling 	}
51497754SJeff.Bonwick@Sun.COM 
51507754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
51513912Slling }
51523912Slling 
5153789Sahrens /*
5154789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
5155789Sahrens  * part of the process, so we iterate until it converges.
5156789Sahrens  */
5157789Sahrens void
5158789Sahrens spa_sync(spa_t *spa, uint64_t txg)
5159789Sahrens {
5160789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
5161789Sahrens 	objset_t *mos = spa->spa_meta_objset;
516210922SJeff.Bonwick@Sun.COM 	bplist_t *defer_bpl = &spa->spa_deferred_bplist;
516310922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
51641635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5165789Sahrens 	vdev_t *vd;
5166789Sahrens 	dmu_tx_t *tx;
51677754SJeff.Bonwick@Sun.COM 	int error;
5168789Sahrens 
5169789Sahrens 	/*
5170789Sahrens 	 * Lock out configuration changes.
5171789Sahrens 	 */
51727754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5173789Sahrens 
5174789Sahrens 	spa->spa_syncing_txg = txg;
5175789Sahrens 	spa->spa_sync_pass = 0;
5176789Sahrens 
51777754SJeff.Bonwick@Sun.COM 	/*
51787754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
51797754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
51807754SJeff.Bonwick@Sun.COM 	 */
51817754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
51828241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
51838241SJeff.Bonwick@Sun.COM 		/*
51848241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
51858241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
51868241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
51878241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
51888241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
51898241SJeff.Bonwick@Sun.COM 		 */
51908241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
51918241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
51928241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
51938241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
51948241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
51958241SJeff.Bonwick@Sun.COM 		}
51968241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
51978241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
51987754SJeff.Bonwick@Sun.COM 	}
51997754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
52007754SJeff.Bonwick@Sun.COM 
520110922SJeff.Bonwick@Sun.COM 	VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj));
5202789Sahrens 
52032082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
52042082Seschrock 
52052082Seschrock 	/*
52064577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
52072082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
52082082Seschrock 	 */
52094577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
52104577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
52112082Seschrock 		int i;
52122082Seschrock 
52132082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
52142082Seschrock 			vd = rvd->vdev_child[i];
52152082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
52162082Seschrock 				break;
52172082Seschrock 		}
52182082Seschrock 		if (i == rvd->vdev_children) {
52192082Seschrock 			spa->spa_deflate = TRUE;
52202082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
52212082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
52222082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
52232082Seschrock 		}
52242082Seschrock 	}
52252082Seschrock 
52267046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
52277046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
52287046Sahrens 		dsl_pool_create_origin(dp, tx);
52297046Sahrens 
52307046Sahrens 		/* Keeping the origin open increases spa_minref */
52317046Sahrens 		spa->spa_minref += 3;
52327046Sahrens 	}
52337046Sahrens 
52347046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
52357046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
52367046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
52377046Sahrens 	}
52387046Sahrens 
5239789Sahrens 	/*
5240789Sahrens 	 * If anything has changed in this txg, push the deferred frees
5241789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
5242789Sahrens 	 * don't generate work on an otherwise idle system.
5243789Sahrens 	 */
5244789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
52452329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
52462329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
524710922SJeff.Bonwick@Sun.COM 		spa_sync_deferred_bplist(spa, defer_bpl, tx, txg);
5248789Sahrens 
5249789Sahrens 	/*
5250789Sahrens 	 * Iterate to convergence.
5251789Sahrens 	 */
5252789Sahrens 	do {
525310922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
5254789Sahrens 
5255789Sahrens 		spa_sync_config_object(spa, tx);
52565450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
52575450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
52585450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
52595450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
52601544Seschrock 		spa_errlog_sync(spa, txg);
5261789Sahrens 		dsl_pool_sync(dp, txg);
5262789Sahrens 
526310922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
526410922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
526510922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, spa_sync_free, zio, tx);
526610922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
526710922SJeff.Bonwick@Sun.COM 		} else {
526810922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx);
5269789Sahrens 		}
5270789Sahrens 
527110922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
527210922SJeff.Bonwick@Sun.COM 
527310922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
527410922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
527510922SJeff.Bonwick@Sun.COM 
527610922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
527710922SJeff.Bonwick@Sun.COM 
527810922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
527910922SJeff.Bonwick@Sun.COM 
528010922SJeff.Bonwick@Sun.COM 	bplist_close(defer_bpl);
5281789Sahrens 
5282789Sahrens 	/*
5283789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
5284789Sahrens 	 * to commit the transaction group.
52851635Sbonwick 	 *
52865688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
52875688Sbonwick 	 * random top-level vdevs that are known to be visible in the
52887754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
52897754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5290789Sahrens 	 */
52917754SJeff.Bonwick@Sun.COM 	for (;;) {
52927754SJeff.Bonwick@Sun.COM 		/*
52937754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
52947754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
52957754SJeff.Bonwick@Sun.COM 		 */
52967754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
52977754SJeff.Bonwick@Sun.COM 
52987754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
52997754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
53007754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
53017754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
53027754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
53039816SGeorge.Wilson@Sun.COM 
53049816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
53057754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
53067754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
53077754SJeff.Bonwick@Sun.COM 					continue;
53087754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
53097754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
53107754SJeff.Bonwick@Sun.COM 					break;
53117754SJeff.Bonwick@Sun.COM 			}
53129725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
53139725SEric.Schrock@Sun.COM 			if (error != 0)
53149725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
53159725SEric.Schrock@Sun.COM 				    B_TRUE);
53167754SJeff.Bonwick@Sun.COM 		} else {
53177754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
53189725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
53199725SEric.Schrock@Sun.COM 			if (error != 0)
53209725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
53219725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
53221635Sbonwick 		}
53237754SJeff.Bonwick@Sun.COM 
53247754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
53257754SJeff.Bonwick@Sun.COM 
53267754SJeff.Bonwick@Sun.COM 		if (error == 0)
53277754SJeff.Bonwick@Sun.COM 			break;
53287754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
53297754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
53301635Sbonwick 	}
53312082Seschrock 	dmu_tx_commit(tx);
53322082Seschrock 
53331635Sbonwick 	/*
53341635Sbonwick 	 * Clear the dirty config list.
53351635Sbonwick 	 */
53367754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
53371635Sbonwick 		vdev_config_clean(vd);
53381635Sbonwick 
53391635Sbonwick 	/*
53401635Sbonwick 	 * Now that the new config has synced transactionally,
53411635Sbonwick 	 * let it become visible to the config cache.
53421635Sbonwick 	 */
53431635Sbonwick 	if (spa->spa_config_syncing != NULL) {
53441635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
53451635Sbonwick 		spa->spa_config_txg = txg;
53461635Sbonwick 		spa->spa_config_syncing = NULL;
53471635Sbonwick 	}
5348789Sahrens 
5349789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
5350789Sahrens 
535110922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
5352789Sahrens 
5353789Sahrens 	/*
5354789Sahrens 	 * Update usable space statistics.
5355789Sahrens 	 */
5356789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5357789Sahrens 		vdev_sync_done(vd, txg);
5358789Sahrens 
535910956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
536010956SGeorge.Wilson@Sun.COM 
5361789Sahrens 	/*
5362789Sahrens 	 * It had better be the case that we didn't dirty anything
53632082Seschrock 	 * since vdev_config_sync().
5364789Sahrens 	 */
5365789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5366789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5367789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
536810922SJeff.Bonwick@Sun.COM 	ASSERT(defer_bpl->bpl_queue == NULL);
536910922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
537010922SJeff.Bonwick@Sun.COM 
537110922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
5372789Sahrens 
53737754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
53741544Seschrock 
537510921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
537610921STim.Haley@Sun.COM 
53771544Seschrock 	/*
53781544Seschrock 	 * If any async tasks have been requested, kick them off.
53791544Seschrock 	 */
53801544Seschrock 	spa_async_dispatch(spa);
5381789Sahrens }
5382789Sahrens 
5383789Sahrens /*
5384789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
5385789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
5386789Sahrens  * sync.
5387789Sahrens  */
5388789Sahrens void
5389789Sahrens spa_sync_allpools(void)
5390789Sahrens {
5391789Sahrens 	spa_t *spa = NULL;
5392789Sahrens 	mutex_enter(&spa_namespace_lock);
5393789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
53947754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
5395789Sahrens 			continue;
5396789Sahrens 		spa_open_ref(spa, FTAG);
5397789Sahrens 		mutex_exit(&spa_namespace_lock);
5398789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
5399789Sahrens 		mutex_enter(&spa_namespace_lock);
5400789Sahrens 		spa_close(spa, FTAG);
5401789Sahrens 	}
5402789Sahrens 	mutex_exit(&spa_namespace_lock);
5403789Sahrens }
5404789Sahrens 
5405789Sahrens /*
5406789Sahrens  * ==========================================================================
5407789Sahrens  * Miscellaneous routines
5408789Sahrens  * ==========================================================================
5409789Sahrens  */
5410789Sahrens 
5411789Sahrens /*
5412789Sahrens  * Remove all pools in the system.
5413789Sahrens  */
5414789Sahrens void
5415789Sahrens spa_evict_all(void)
5416789Sahrens {
5417789Sahrens 	spa_t *spa;
5418789Sahrens 
5419789Sahrens 	/*
5420789Sahrens 	 * Remove all cached state.  All pools should be closed now,
5421789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
5422789Sahrens 	 */
5423789Sahrens 	mutex_enter(&spa_namespace_lock);
5424789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
5425789Sahrens 		/*
54261544Seschrock 		 * Stop async tasks.  The async thread may need to detach
54271544Seschrock 		 * a device that's been replaced, which requires grabbing
54281544Seschrock 		 * spa_namespace_lock, so we must drop it here.
5429789Sahrens 		 */
5430789Sahrens 		spa_open_ref(spa, FTAG);
5431789Sahrens 		mutex_exit(&spa_namespace_lock);
54321544Seschrock 		spa_async_suspend(spa);
54334808Sek110237 		mutex_enter(&spa_namespace_lock);
5434789Sahrens 		spa_close(spa, FTAG);
5435789Sahrens 
5436789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5437789Sahrens 			spa_unload(spa);
5438789Sahrens 			spa_deactivate(spa);
5439789Sahrens 		}
5440789Sahrens 		spa_remove(spa);
5441789Sahrens 	}
5442789Sahrens 	mutex_exit(&spa_namespace_lock);
5443789Sahrens }
54441544Seschrock 
54451544Seschrock vdev_t *
54469425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
54471544Seschrock {
54486643Seschrock 	vdev_t *vd;
54496643Seschrock 	int i;
54506643Seschrock 
54516643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
54526643Seschrock 		return (vd);
54536643Seschrock 
54549425SEric.Schrock@Sun.COM 	if (aux) {
54556643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
54566643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
54576643Seschrock 			if (vd->vdev_guid == guid)
54586643Seschrock 				return (vd);
54596643Seschrock 		}
54609425SEric.Schrock@Sun.COM 
54619425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
54629425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
54639425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
54649425SEric.Schrock@Sun.COM 				return (vd);
54659425SEric.Schrock@Sun.COM 		}
54666643Seschrock 	}
54676643Seschrock 
54686643Seschrock 	return (NULL);
54691544Seschrock }
54701760Seschrock 
54711760Seschrock void
54725094Slling spa_upgrade(spa_t *spa, uint64_t version)
54731760Seschrock {
54747754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
54751760Seschrock 
54761760Seschrock 	/*
54771760Seschrock 	 * This should only be called for a non-faulted pool, and since a
54781760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
54791760Seschrock 	 * possible.
54801760Seschrock 	 */
54814577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
54825094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
54835094Slling 
54845094Slling 	spa->spa_uberblock.ub_version = version;
54851760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
54861760Seschrock 
54877754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
54882082Seschrock 
54892082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
54901760Seschrock }
54912082Seschrock 
54922082Seschrock boolean_t
54932082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
54942082Seschrock {
54952082Seschrock 	int i;
54963377Seschrock 	uint64_t spareguid;
54975450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
54985450Sbrendan 
54995450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
55005450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
55012082Seschrock 			return (B_TRUE);
55022082Seschrock 
55035450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
55045450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
55055450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
55063377Seschrock 			return (B_TRUE);
55073377Seschrock 	}
55083377Seschrock 
55092082Seschrock 	return (B_FALSE);
55102082Seschrock }
55113912Slling 
55124451Seschrock /*
55137214Slling  * Check if a pool has an active shared spare device.
55147214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
55157214Slling  */
55167214Slling static boolean_t
55177214Slling spa_has_active_shared_spare(spa_t *spa)
55187214Slling {
55197214Slling 	int i, refcnt;
55207214Slling 	uint64_t pool;
55217214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
55227214Slling 
55237214Slling 	for (i = 0; i < sav->sav_count; i++) {
55247214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
55257214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
55267214Slling 		    refcnt > 2)
55277214Slling 			return (B_TRUE);
55287214Slling 	}
55297214Slling 
55307214Slling 	return (B_FALSE);
55317214Slling }
55327214Slling 
55337214Slling /*
55344451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
55354451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
55364451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
55374451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
55384451Seschrock  * or zdb as real changes.
55394451Seschrock  */
55404451Seschrock void
55414451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
55424451Seschrock {
55434451Seschrock #ifdef _KERNEL
55444451Seschrock 	sysevent_t		*ev;
55454451Seschrock 	sysevent_attr_list_t	*attr = NULL;
55464451Seschrock 	sysevent_value_t	value;
55474451Seschrock 	sysevent_id_t		eid;
55484451Seschrock 
55494451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
55504451Seschrock 	    SE_SLEEP);
55514451Seschrock 
55524451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
55534451Seschrock 	value.value.sv_string = spa_name(spa);
55544451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
55554451Seschrock 		goto done;
55564451Seschrock 
55574451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
55584451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
55594451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
55604451Seschrock 		goto done;
55614451Seschrock 
55624451Seschrock 	if (vd) {
55634451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
55644451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
55654451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
55664451Seschrock 		    SE_SLEEP) != 0)
55674451Seschrock 			goto done;
55684451Seschrock 
55694451Seschrock 		if (vd->vdev_path) {
55704451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
55714451Seschrock 			value.value.sv_string = vd->vdev_path;
55724451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
55734451Seschrock 			    &value, SE_SLEEP) != 0)
55744451Seschrock 				goto done;
55754451Seschrock 		}
55764451Seschrock 	}
55774451Seschrock 
55785756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
55795756Seschrock 		goto done;
55805756Seschrock 	attr = NULL;
55815756Seschrock 
55824451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
55834451Seschrock 
55844451Seschrock done:
55854451Seschrock 	if (attr)
55864451Seschrock 		sysevent_free_attr(attr);
55874451Seschrock 	sysevent_free(ev);
55884451Seschrock #endif
55894451Seschrock }
5590