xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 12470:54258108784b)
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 /*
2312247SGeorge.Wilson@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
28789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
29789Sahrens  * pool.
30789Sahrens  */
31789Sahrens 
32789Sahrens #include <sys/zfs_context.h>
331544Seschrock #include <sys/fm/fs/zfs.h>
34789Sahrens #include <sys/spa_impl.h>
35789Sahrens #include <sys/zio.h>
36789Sahrens #include <sys/zio_checksum.h>
37789Sahrens #include <sys/dmu.h>
38789Sahrens #include <sys/dmu_tx.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/zil.h>
4110922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
42789Sahrens #include <sys/vdev_impl.h>
43789Sahrens #include <sys/metaslab.h>
4410594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
45789Sahrens #include <sys/uberblock_impl.h>
46789Sahrens #include <sys/txg.h>
47789Sahrens #include <sys/avl.h>
48789Sahrens #include <sys/dmu_traverse.h>
493912Slling #include <sys/dmu_objset.h>
50789Sahrens #include <sys/unique.h>
51789Sahrens #include <sys/dsl_pool.h>
523912Slling #include <sys/dsl_dataset.h>
53789Sahrens #include <sys/dsl_dir.h>
54789Sahrens #include <sys/dsl_prop.h>
553912Slling #include <sys/dsl_synctask.h>
56789Sahrens #include <sys/fs/zfs.h>
575450Sbrendan #include <sys/arc.h>
58789Sahrens #include <sys/callb.h>
593975Sek110237 #include <sys/systeminfo.h>
606423Sgw25295 #include <sys/spa_boot.h>
619816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h>
6212296SLin.Ling@Sun.COM #include <sys/dsl_scan.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) },
10912450SGeorge.Wilson@Sun.COM 	{ ZTI_FIX(100),	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 
11412296SLin.Ling@Sun.COM static dsl_syncfunc_t spa_sync_props;
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. */
23911619SGeorge.Wilson@Sun.COM 	if (mos == NULL || 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 
946*12470SMatthew.Ahrens@Sun.COM 	bpobj_close(&spa->spa_deferred_bpobj);
947*12470SMatthew.Ahrens@Sun.COM 
948789Sahrens 	/*
949789Sahrens 	 * Close the dsl pool.
950789Sahrens 	 */
951789Sahrens 	if (spa->spa_dsl_pool) {
952789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
953789Sahrens 		spa->spa_dsl_pool = NULL;
95411619SGeorge.Wilson@Sun.COM 		spa->spa_meta_objset = NULL;
955789Sahrens 	}
956789Sahrens 
95710922SJeff.Bonwick@Sun.COM 	ddt_unload(spa);
95810922SJeff.Bonwick@Sun.COM 
9598241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9608241SJeff.Bonwick@Sun.COM 
9618241SJeff.Bonwick@Sun.COM 	/*
9628241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
9638241SJeff.Bonwick@Sun.COM 	 */
9648241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
9658241SJeff.Bonwick@Sun.COM 
966789Sahrens 	/*
967789Sahrens 	 * Close all vdevs.
968789Sahrens 	 */
9691585Sbonwick 	if (spa->spa_root_vdev)
970789Sahrens 		vdev_free(spa->spa_root_vdev);
9711585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
9721544Seschrock 
9735450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9745450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
9755450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
9765450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
9775450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
9785450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
9795450Sbrendan 	}
9805450Sbrendan 	if (spa->spa_spares.sav_config) {
9815450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
9825450Sbrendan 		spa->spa_spares.sav_config = NULL;
9832082Seschrock 	}
9847377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
9855450Sbrendan 
9865450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
9875450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
9885450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
9895450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
9905450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
9915450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
9925450Sbrendan 	}
9935450Sbrendan 	if (spa->spa_l2cache.sav_config) {
9945450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
9955450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
9962082Seschrock 	}
9977377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
9982082Seschrock 
9991544Seschrock 	spa->spa_async_suspended = 0;
10008241SJeff.Bonwick@Sun.COM 
10018241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1002789Sahrens }
1003789Sahrens 
1004789Sahrens /*
10052082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
10062082Seschrock  * this pool.  When this is called, we have some form of basic information in
10075450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
10085450Sbrendan  * then re-generate a more complete list including status information.
10092082Seschrock  */
10102082Seschrock static void
10112082Seschrock spa_load_spares(spa_t *spa)
10122082Seschrock {
10132082Seschrock 	nvlist_t **spares;
10142082Seschrock 	uint_t nspares;
10152082Seschrock 	int i;
10163377Seschrock 	vdev_t *vd, *tvd;
10172082Seschrock 
10187754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
10197754SJeff.Bonwick@Sun.COM 
10202082Seschrock 	/*
10212082Seschrock 	 * First, close and free any existing spare vdevs.
10222082Seschrock 	 */
10235450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10245450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
10253377Seschrock 
10263377Seschrock 		/* Undo the call to spa_activate() below */
10276643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10286643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
10293377Seschrock 			spa_spare_remove(tvd);
10303377Seschrock 		vdev_close(vd);
10313377Seschrock 		vdev_free(vd);
10322082Seschrock 	}
10333377Seschrock 
10345450Sbrendan 	if (spa->spa_spares.sav_vdevs)
10355450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
10365450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
10375450Sbrendan 
10385450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
10392082Seschrock 		nspares = 0;
10402082Seschrock 	else
10415450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
10422082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
10432082Seschrock 
10445450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
10455450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
10462082Seschrock 
10472082Seschrock 	if (nspares == 0)
10482082Seschrock 		return;
10492082Seschrock 
10502082Seschrock 	/*
10512082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
10523377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
10533377Seschrock 	 * structures associated with it: one in the list of spares (used only
10543377Seschrock 	 * for basic validation purposes) and one in the active vdev
10553377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
10563377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
10573377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
10582082Seschrock 	 */
10595450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
10605450Sbrendan 	    KM_SLEEP);
10615450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10622082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
10632082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
10642082Seschrock 		ASSERT(vd != NULL);
10652082Seschrock 
10665450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
10672082Seschrock 
10686643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10696643Seschrock 		    B_FALSE)) != NULL) {
10703377Seschrock 			if (!tvd->vdev_isspare)
10713377Seschrock 				spa_spare_add(tvd);
10723377Seschrock 
10733377Seschrock 			/*
10743377Seschrock 			 * We only mark the spare active if we were successfully
10753377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
10763377Seschrock 			 * with a bad active spare would result in strange
10773377Seschrock 			 * behavior, because multiple pool would think the spare
10783377Seschrock 			 * is actively in use.
10793377Seschrock 			 *
10803377Seschrock 			 * There is a vulnerability here to an equally bizarre
10813377Seschrock 			 * circumstance, where a dead active spare is later
10823377Seschrock 			 * brought back to life (onlined or otherwise).  Given
10833377Seschrock 			 * the rarity of this scenario, and the extra complexity
10843377Seschrock 			 * it adds, we ignore the possibility.
10853377Seschrock 			 */
10863377Seschrock 			if (!vdev_is_dead(tvd))
10873377Seschrock 				spa_spare_activate(tvd);
10883377Seschrock 		}
10893377Seschrock 
10907754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
10919425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
10927754SJeff.Bonwick@Sun.COM 
10932082Seschrock 		if (vdev_open(vd) != 0)
10942082Seschrock 			continue;
10952082Seschrock 
10965450Sbrendan 		if (vdev_validate_aux(vd) == 0)
10975450Sbrendan 			spa_spare_add(vd);
10982082Seschrock 	}
10992082Seschrock 
11002082Seschrock 	/*
11012082Seschrock 	 * Recompute the stashed list of spares, with status information
11022082Seschrock 	 * this time.
11032082Seschrock 	 */
11045450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
11052082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
11062082Seschrock 
11075450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
11085450Sbrendan 	    KM_SLEEP);
11095450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11105450Sbrendan 		spares[i] = vdev_config_generate(spa,
111112296SLin.Ling@Sun.COM 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
11125450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
11135450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
11145450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11152082Seschrock 		nvlist_free(spares[i]);
11165450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
11175450Sbrendan }
11185450Sbrendan 
11195450Sbrendan /*
11205450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
11215450Sbrendan  * this pool.  When this is called, we have some form of basic information in
11225450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
11235450Sbrendan  * then re-generate a more complete list including status information.
11245450Sbrendan  * Devices which are already active have their details maintained, and are
11255450Sbrendan  * not re-opened.
11265450Sbrendan  */
11275450Sbrendan static void
11285450Sbrendan spa_load_l2cache(spa_t *spa)
11295450Sbrendan {
11305450Sbrendan 	nvlist_t **l2cache;
11315450Sbrendan 	uint_t nl2cache;
11325450Sbrendan 	int i, j, oldnvdevs;
11339816SGeorge.Wilson@Sun.COM 	uint64_t guid;
11345450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
11355450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
11365450Sbrendan 
11377754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
11387754SJeff.Bonwick@Sun.COM 
11395450Sbrendan 	if (sav->sav_config != NULL) {
11405450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
11415450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
11425450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
11435450Sbrendan 	} else {
11445450Sbrendan 		nl2cache = 0;
11455450Sbrendan 	}
11465450Sbrendan 
11475450Sbrendan 	oldvdevs = sav->sav_vdevs;
11485450Sbrendan 	oldnvdevs = sav->sav_count;
11495450Sbrendan 	sav->sav_vdevs = NULL;
11505450Sbrendan 	sav->sav_count = 0;
11515450Sbrendan 
11525450Sbrendan 	/*
11535450Sbrendan 	 * Process new nvlist of vdevs.
11545450Sbrendan 	 */
11555450Sbrendan 	for (i = 0; i < nl2cache; i++) {
11565450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
11575450Sbrendan 		    &guid) == 0);
11585450Sbrendan 
11595450Sbrendan 		newvdevs[i] = NULL;
11605450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
11615450Sbrendan 			vd = oldvdevs[j];
11625450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
11635450Sbrendan 				/*
11645450Sbrendan 				 * Retain previous vdev for add/remove ops.
11655450Sbrendan 				 */
11665450Sbrendan 				newvdevs[i] = vd;
11675450Sbrendan 				oldvdevs[j] = NULL;
11685450Sbrendan 				break;
11695450Sbrendan 			}
11705450Sbrendan 		}
11715450Sbrendan 
11725450Sbrendan 		if (newvdevs[i] == NULL) {
11735450Sbrendan 			/*
11745450Sbrendan 			 * Create new vdev
11755450Sbrendan 			 */
11765450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
11775450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
11785450Sbrendan 			ASSERT(vd != NULL);
11795450Sbrendan 			newvdevs[i] = vd;
11805450Sbrendan 
11815450Sbrendan 			/*
11825450Sbrendan 			 * Commit this vdev as an l2cache device,
11835450Sbrendan 			 * even if it fails to open.
11845450Sbrendan 			 */
11855450Sbrendan 			spa_l2cache_add(vd);
11865450Sbrendan 
11876643Seschrock 			vd->vdev_top = vd;
11886643Seschrock 			vd->vdev_aux = sav;
11896643Seschrock 
11906643Seschrock 			spa_l2cache_activate(vd);
11916643Seschrock 
11925450Sbrendan 			if (vdev_open(vd) != 0)
11935450Sbrendan 				continue;
11945450Sbrendan 
11955450Sbrendan 			(void) vdev_validate_aux(vd);
11965450Sbrendan 
11979816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
11989816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
11995450Sbrendan 		}
12005450Sbrendan 	}
12015450Sbrendan 
12025450Sbrendan 	/*
12035450Sbrendan 	 * Purge vdevs that were dropped
12045450Sbrendan 	 */
12055450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
12065450Sbrendan 		uint64_t pool;
12075450Sbrendan 
12085450Sbrendan 		vd = oldvdevs[i];
12095450Sbrendan 		if (vd != NULL) {
12108241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
12118241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
12125450Sbrendan 				l2arc_remove_vdev(vd);
12135450Sbrendan 			(void) vdev_close(vd);
12145450Sbrendan 			spa_l2cache_remove(vd);
12155450Sbrendan 		}
12165450Sbrendan 	}
12175450Sbrendan 
12185450Sbrendan 	if (oldvdevs)
12195450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
12205450Sbrendan 
12215450Sbrendan 	if (sav->sav_config == NULL)
12225450Sbrendan 		goto out;
12235450Sbrendan 
12245450Sbrendan 	sav->sav_vdevs = newvdevs;
12255450Sbrendan 	sav->sav_count = (int)nl2cache;
12265450Sbrendan 
12275450Sbrendan 	/*
12285450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
12295450Sbrendan 	 * information this time.
12305450Sbrendan 	 */
12315450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
12325450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
12335450Sbrendan 
12345450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
12355450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12365450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
123712296SLin.Ling@Sun.COM 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
12385450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
12395450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
12405450Sbrendan out:
12415450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12425450Sbrendan 		nvlist_free(l2cache[i]);
12435450Sbrendan 	if (sav->sav_count)
12445450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
12452082Seschrock }
12462082Seschrock 
12472082Seschrock static int
12482082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
12492082Seschrock {
12502082Seschrock 	dmu_buf_t *db;
12512082Seschrock 	char *packed = NULL;
12522082Seschrock 	size_t nvsize = 0;
12532082Seschrock 	int error;
12542082Seschrock 	*value = NULL;
12552082Seschrock 
12562082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
12572082Seschrock 	nvsize = *(uint64_t *)db->db_data;
12582082Seschrock 	dmu_buf_rele(db, FTAG);
12592082Seschrock 
12602082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
12619512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
12629512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
12632082Seschrock 	if (error == 0)
12642082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
12652082Seschrock 	kmem_free(packed, nvsize);
12662082Seschrock 
12672082Seschrock 	return (error);
12682082Seschrock }
12692082Seschrock 
12702082Seschrock /*
12714451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
12724451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
12734451Seschrock  */
12744451Seschrock static void
12754451Seschrock spa_check_removed(vdev_t *vd)
12764451Seschrock {
12779816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12784451Seschrock 		spa_check_removed(vd->vdev_child[c]);
12794451Seschrock 
12804451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
12814451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
12824451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
12834451Seschrock 	}
12844451Seschrock }
12854451Seschrock 
12864451Seschrock /*
12879701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
12889701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
12899701SGeorge.Wilson@Sun.COM  */
12909701SGeorge.Wilson@Sun.COM void
129110594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
12929701SGeorge.Wilson@Sun.COM {
129310594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
129410594SGeorge.Wilson@Sun.COM 
129510594SGeorge.Wilson@Sun.COM 	/*
129610594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
129710594SGeorge.Wilson@Sun.COM 	 */
129810594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
129910594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
130010594SGeorge.Wilson@Sun.COM 
130110594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
130210594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
130310594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
130410594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
13059701SGeorge.Wilson@Sun.COM 	}
130610594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
130710594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13089701SGeorge.Wilson@Sun.COM }
13099701SGeorge.Wilson@Sun.COM 
13109701SGeorge.Wilson@Sun.COM /*
13117294Sperrin  * Check for missing log devices
13127294Sperrin  */
13137294Sperrin int
13147294Sperrin spa_check_logs(spa_t *spa)
13157294Sperrin {
13167294Sperrin 	switch (spa->spa_log_state) {
13177294Sperrin 	case SPA_LOG_MISSING:
13187294Sperrin 		/* need to recheck in case slog has been restored */
13197294Sperrin 	case SPA_LOG_UNKNOWN:
13207294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
13217294Sperrin 		    DS_FIND_CHILDREN)) {
132211422SMark.Musante@Sun.COM 			spa_set_log_state(spa, SPA_LOG_MISSING);
13237294Sperrin 			return (1);
13247294Sperrin 		}
13257294Sperrin 		break;
13267294Sperrin 	}
13277294Sperrin 	return (0);
13287294Sperrin }
13297294Sperrin 
133011422SMark.Musante@Sun.COM static boolean_t
133111422SMark.Musante@Sun.COM spa_passivate_log(spa_t *spa)
133211422SMark.Musante@Sun.COM {
133311422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
133411422SMark.Musante@Sun.COM 	boolean_t slog_found = B_FALSE;
133511422SMark.Musante@Sun.COM 
133611422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
133711422SMark.Musante@Sun.COM 
133811422SMark.Musante@Sun.COM 	if (!spa_has_slogs(spa))
133911422SMark.Musante@Sun.COM 		return (B_FALSE);
134011422SMark.Musante@Sun.COM 
134111422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
134211422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
134311422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
134411422SMark.Musante@Sun.COM 
134511422SMark.Musante@Sun.COM 		if (tvd->vdev_islog) {
134611422SMark.Musante@Sun.COM 			metaslab_group_passivate(mg);
134711422SMark.Musante@Sun.COM 			slog_found = B_TRUE;
134811422SMark.Musante@Sun.COM 		}
134911422SMark.Musante@Sun.COM 	}
135011422SMark.Musante@Sun.COM 
135111422SMark.Musante@Sun.COM 	return (slog_found);
135211422SMark.Musante@Sun.COM }
135311422SMark.Musante@Sun.COM 
135411422SMark.Musante@Sun.COM static void
135511422SMark.Musante@Sun.COM spa_activate_log(spa_t *spa)
135611422SMark.Musante@Sun.COM {
135711422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
135811422SMark.Musante@Sun.COM 
135911422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
136011422SMark.Musante@Sun.COM 
136111422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
136211422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
136311422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
136411422SMark.Musante@Sun.COM 
136511422SMark.Musante@Sun.COM 		if (tvd->vdev_islog)
136611422SMark.Musante@Sun.COM 			metaslab_group_activate(mg);
136711422SMark.Musante@Sun.COM 	}
136811422SMark.Musante@Sun.COM }
136911422SMark.Musante@Sun.COM 
137011422SMark.Musante@Sun.COM int
137111422SMark.Musante@Sun.COM spa_offline_log(spa_t *spa)
137211422SMark.Musante@Sun.COM {
137311422SMark.Musante@Sun.COM 	int error = 0;
137411422SMark.Musante@Sun.COM 
137511422SMark.Musante@Sun.COM 	if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
137611422SMark.Musante@Sun.COM 	    NULL, DS_FIND_CHILDREN)) == 0) {
137711422SMark.Musante@Sun.COM 
137811422SMark.Musante@Sun.COM 		/*
137911422SMark.Musante@Sun.COM 		 * We successfully offlined the log device, sync out the
138011422SMark.Musante@Sun.COM 		 * current txg so that the "stubby" block can be removed
138111422SMark.Musante@Sun.COM 		 * by zil_sync().
138211422SMark.Musante@Sun.COM 		 */
138311422SMark.Musante@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, 0);
138411422SMark.Musante@Sun.COM 	}
138511422SMark.Musante@Sun.COM 	return (error);
138611422SMark.Musante@Sun.COM }
138711422SMark.Musante@Sun.COM 
138810672SEric.Schrock@Sun.COM static void
138910672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
139010672SEric.Schrock@Sun.COM {
139110922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < sav->sav_count; i++)
139210672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
139310672SEric.Schrock@Sun.COM }
139410672SEric.Schrock@Sun.COM 
139510922SJeff.Bonwick@Sun.COM void
139610922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio)
139710922SJeff.Bonwick@Sun.COM {
139810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
139910922SJeff.Bonwick@Sun.COM 
140010922SJeff.Bonwick@Sun.COM 	if (zio->io_error)
140110922SJeff.Bonwick@Sun.COM 		return;
140210922SJeff.Bonwick@Sun.COM 
140310922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
140410922SJeff.Bonwick@Sun.COM 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
140510922SJeff.Bonwick@Sun.COM 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
140610922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
140710922SJeff.Bonwick@Sun.COM }
140810922SJeff.Bonwick@Sun.COM 
140910921STim.Haley@Sun.COM typedef struct spa_load_error {
141011727SVictor.Latushkin@Sun.COM 	uint64_t	sle_meta_count;
141110921STim.Haley@Sun.COM 	uint64_t	sle_data_count;
141210921STim.Haley@Sun.COM } spa_load_error_t;
141310921STim.Haley@Sun.COM 
141410921STim.Haley@Sun.COM static void
141510921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio)
141610921STim.Haley@Sun.COM {
141710921STim.Haley@Sun.COM 	blkptr_t *bp = zio->io_bp;
141810921STim.Haley@Sun.COM 	spa_load_error_t *sle = zio->io_private;
141910921STim.Haley@Sun.COM 	dmu_object_type_t type = BP_GET_TYPE(bp);
142010921STim.Haley@Sun.COM 	int error = zio->io_error;
142110921STim.Haley@Sun.COM 
142210921STim.Haley@Sun.COM 	if (error) {
142310921STim.Haley@Sun.COM 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
142410921STim.Haley@Sun.COM 		    type != DMU_OT_INTENT_LOG)
142511727SVictor.Latushkin@Sun.COM 			atomic_add_64(&sle->sle_meta_count, 1);
142610921STim.Haley@Sun.COM 		else
142710921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_data_count, 1);
142810921STim.Haley@Sun.COM 	}
142910921STim.Haley@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
143010921STim.Haley@Sun.COM }
143110921STim.Haley@Sun.COM 
143210921STim.Haley@Sun.COM /*ARGSUSED*/
143310921STim.Haley@Sun.COM static int
143410922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
143512296SLin.Ling@Sun.COM     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
143610921STim.Haley@Sun.COM {
143710921STim.Haley@Sun.COM 	if (bp != NULL) {
143810921STim.Haley@Sun.COM 		zio_t *rio = arg;
143910921STim.Haley@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
144010921STim.Haley@Sun.COM 		void *data = zio_data_buf_alloc(size);
144110921STim.Haley@Sun.COM 
144210921STim.Haley@Sun.COM 		zio_nowait(zio_read(rio, spa, bp, data, size,
144310921STim.Haley@Sun.COM 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
144410921STim.Haley@Sun.COM 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
144510921STim.Haley@Sun.COM 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
144610921STim.Haley@Sun.COM 	}
144710921STim.Haley@Sun.COM 	return (0);
144810921STim.Haley@Sun.COM }
144910921STim.Haley@Sun.COM 
145010921STim.Haley@Sun.COM static int
145110921STim.Haley@Sun.COM spa_load_verify(spa_t *spa)
145210921STim.Haley@Sun.COM {
145310921STim.Haley@Sun.COM 	zio_t *rio;
145410921STim.Haley@Sun.COM 	spa_load_error_t sle = { 0 };
145510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
145610921STim.Haley@Sun.COM 	boolean_t verify_ok = B_FALSE;
145710921STim.Haley@Sun.COM 	int error;
145810921STim.Haley@Sun.COM 
145911727SVictor.Latushkin@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
146011727SVictor.Latushkin@Sun.COM 
146111727SVictor.Latushkin@Sun.COM 	if (policy.zrp_request & ZPOOL_NEVER_REWIND)
146211727SVictor.Latushkin@Sun.COM 		return (0);
146311727SVictor.Latushkin@Sun.COM 
146410921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
146510921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
146610921STim.Haley@Sun.COM 
146711125SJeff.Bonwick@Sun.COM 	error = traverse_pool(spa, spa->spa_verify_min_txg,
146811125SJeff.Bonwick@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
146910921STim.Haley@Sun.COM 
147010921STim.Haley@Sun.COM 	(void) zio_wait(rio);
147110921STim.Haley@Sun.COM 
147211727SVictor.Latushkin@Sun.COM 	spa->spa_load_meta_errors = sle.sle_meta_count;
147310921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
147410921STim.Haley@Sun.COM 
147511727SVictor.Latushkin@Sun.COM 	if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
147610921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
147710921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
147810921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
147910921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
148011026STim.Haley@Sun.COM 	} else {
148111026STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
148210921STim.Haley@Sun.COM 	}
148310921STim.Haley@Sun.COM 
148410921STim.Haley@Sun.COM 	if (error) {
148510921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
148610921STim.Haley@Sun.COM 			error = EIO;
148710921STim.Haley@Sun.COM 		return (error);
148810921STim.Haley@Sun.COM 	}
148910921STim.Haley@Sun.COM 
149010921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
149110921STim.Haley@Sun.COM }
149210921STim.Haley@Sun.COM 
14937294Sperrin /*
149411422SMark.Musante@Sun.COM  * Find a value in the pool props object.
149511422SMark.Musante@Sun.COM  */
149611422SMark.Musante@Sun.COM static void
149711422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
149811422SMark.Musante@Sun.COM {
149911422SMark.Musante@Sun.COM 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
150011422SMark.Musante@Sun.COM 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
150111422SMark.Musante@Sun.COM }
150211422SMark.Musante@Sun.COM 
150311422SMark.Musante@Sun.COM /*
150411422SMark.Musante@Sun.COM  * Find a value in the pool directory object.
150511422SMark.Musante@Sun.COM  */
150611422SMark.Musante@Sun.COM static int
150711422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
150811422SMark.Musante@Sun.COM {
150911422SMark.Musante@Sun.COM 	return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
151011422SMark.Musante@Sun.COM 	    name, sizeof (uint64_t), 1, val));
151111422SMark.Musante@Sun.COM }
151211422SMark.Musante@Sun.COM 
151311422SMark.Musante@Sun.COM static int
151411422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
151511422SMark.Musante@Sun.COM {
151611422SMark.Musante@Sun.COM 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
151711422SMark.Musante@Sun.COM 	return (err);
151811422SMark.Musante@Sun.COM }
151911422SMark.Musante@Sun.COM 
152011422SMark.Musante@Sun.COM /*
152111422SMark.Musante@Sun.COM  * Fix up config after a partly-completed split.  This is done with the
152211422SMark.Musante@Sun.COM  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
152311422SMark.Musante@Sun.COM  * pool have that entry in their config, but only the splitting one contains
152411422SMark.Musante@Sun.COM  * a list of all the guids of the vdevs that are being split off.
152511422SMark.Musante@Sun.COM  *
152611422SMark.Musante@Sun.COM  * This function determines what to do with that list: either rejoin
152711422SMark.Musante@Sun.COM  * all the disks to the pool, or complete the splitting process.  To attempt
152811422SMark.Musante@Sun.COM  * the rejoin, each disk that is offlined is marked online again, and
152911422SMark.Musante@Sun.COM  * we do a reopen() call.  If the vdev label for every disk that was
153011422SMark.Musante@Sun.COM  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
153111422SMark.Musante@Sun.COM  * then we call vdev_split() on each disk, and complete the split.
153211422SMark.Musante@Sun.COM  *
153311497SMark.Musante@Sun.COM  * Otherwise we leave the config alone, with all the vdevs in place in
153411497SMark.Musante@Sun.COM  * the original pool.
153511422SMark.Musante@Sun.COM  */
153611422SMark.Musante@Sun.COM static void
153711422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config)
153811422SMark.Musante@Sun.COM {
153911422SMark.Musante@Sun.COM 	uint_t extracted;
154011422SMark.Musante@Sun.COM 	uint64_t *glist;
154111422SMark.Musante@Sun.COM 	uint_t i, gcount;
154211422SMark.Musante@Sun.COM 	nvlist_t *nvl;
154311422SMark.Musante@Sun.COM 	vdev_t **vd;
154411422SMark.Musante@Sun.COM 	boolean_t attempt_reopen;
154511422SMark.Musante@Sun.COM 
154611422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
154711422SMark.Musante@Sun.COM 		return;
154811422SMark.Musante@Sun.COM 
154911422SMark.Musante@Sun.COM 	/* check that the config is complete */
155011422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
155111422SMark.Musante@Sun.COM 	    &glist, &gcount) != 0)
155211422SMark.Musante@Sun.COM 		return;
155311422SMark.Musante@Sun.COM 
155411422SMark.Musante@Sun.COM 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
155511422SMark.Musante@Sun.COM 
155611422SMark.Musante@Sun.COM 	/* attempt to online all the vdevs & validate */
155711422SMark.Musante@Sun.COM 	attempt_reopen = B_TRUE;
155811422SMark.Musante@Sun.COM 	for (i = 0; i < gcount; i++) {
155911422SMark.Musante@Sun.COM 		if (glist[i] == 0)	/* vdev is hole */
156011422SMark.Musante@Sun.COM 			continue;
156111422SMark.Musante@Sun.COM 
156211422SMark.Musante@Sun.COM 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
156311422SMark.Musante@Sun.COM 		if (vd[i] == NULL) {
156411422SMark.Musante@Sun.COM 			/*
156511422SMark.Musante@Sun.COM 			 * Don't bother attempting to reopen the disks;
156611422SMark.Musante@Sun.COM 			 * just do the split.
156711422SMark.Musante@Sun.COM 			 */
156811422SMark.Musante@Sun.COM 			attempt_reopen = B_FALSE;
156911422SMark.Musante@Sun.COM 		} else {
157011422SMark.Musante@Sun.COM 			/* attempt to re-online it */
157111422SMark.Musante@Sun.COM 			vd[i]->vdev_offline = B_FALSE;
157211422SMark.Musante@Sun.COM 		}
157311422SMark.Musante@Sun.COM 	}
157411422SMark.Musante@Sun.COM 
157511422SMark.Musante@Sun.COM 	if (attempt_reopen) {
157611422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
157711422SMark.Musante@Sun.COM 
157811422SMark.Musante@Sun.COM 		/* check each device to see what state it's in */
157911422SMark.Musante@Sun.COM 		for (extracted = 0, i = 0; i < gcount; i++) {
158011422SMark.Musante@Sun.COM 			if (vd[i] != NULL &&
158111422SMark.Musante@Sun.COM 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
158211422SMark.Musante@Sun.COM 				break;
158311422SMark.Musante@Sun.COM 			++extracted;
158411422SMark.Musante@Sun.COM 		}
158511422SMark.Musante@Sun.COM 	}
158611422SMark.Musante@Sun.COM 
158711422SMark.Musante@Sun.COM 	/*
158811422SMark.Musante@Sun.COM 	 * If every disk has been moved to the new pool, or if we never
158911422SMark.Musante@Sun.COM 	 * even attempted to look at them, then we split them off for
159011422SMark.Musante@Sun.COM 	 * good.
159111422SMark.Musante@Sun.COM 	 */
159211422SMark.Musante@Sun.COM 	if (!attempt_reopen || gcount == extracted) {
159311422SMark.Musante@Sun.COM 		for (i = 0; i < gcount; i++)
159411422SMark.Musante@Sun.COM 			if (vd[i] != NULL)
159511422SMark.Musante@Sun.COM 				vdev_split(vd[i]);
159611422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
159711422SMark.Musante@Sun.COM 	}
159811422SMark.Musante@Sun.COM 
159911422SMark.Musante@Sun.COM 	kmem_free(vd, gcount * sizeof (vdev_t *));
160011422SMark.Musante@Sun.COM }
160111422SMark.Musante@Sun.COM 
160211422SMark.Musante@Sun.COM static int
160311422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
160411422SMark.Musante@Sun.COM     boolean_t mosconfig)
160511422SMark.Musante@Sun.COM {
160611422SMark.Musante@Sun.COM 	nvlist_t *config = spa->spa_config;
160711422SMark.Musante@Sun.COM 	char *ereport = FM_EREPORT_ZFS_POOL;
160811422SMark.Musante@Sun.COM 	int error;
160911422SMark.Musante@Sun.COM 	uint64_t pool_guid;
161011422SMark.Musante@Sun.COM 	nvlist_t *nvl;
161111422SMark.Musante@Sun.COM 
161211422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
161311422SMark.Musante@Sun.COM 		return (EINVAL);
161411422SMark.Musante@Sun.COM 
161511422SMark.Musante@Sun.COM 	/*
161611422SMark.Musante@Sun.COM 	 * Versioning wasn't explicitly added to the label until later, so if
161711422SMark.Musante@Sun.COM 	 * it's not present treat it as the initial version.
161811422SMark.Musante@Sun.COM 	 */
161911422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
162011422SMark.Musante@Sun.COM 	    &spa->spa_ubsync.ub_version) != 0)
162111422SMark.Musante@Sun.COM 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
162211422SMark.Musante@Sun.COM 
162311422SMark.Musante@Sun.COM 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
162411422SMark.Musante@Sun.COM 	    &spa->spa_config_txg);
162511422SMark.Musante@Sun.COM 
162611422SMark.Musante@Sun.COM 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
162711422SMark.Musante@Sun.COM 	    spa_guid_exists(pool_guid, 0)) {
162811422SMark.Musante@Sun.COM 		error = EEXIST;
162911422SMark.Musante@Sun.COM 	} else {
163011422SMark.Musante@Sun.COM 		spa->spa_load_guid = pool_guid;
163111422SMark.Musante@Sun.COM 
163211422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
163311422SMark.Musante@Sun.COM 		    &nvl) == 0) {
163411422SMark.Musante@Sun.COM 			VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
163511422SMark.Musante@Sun.COM 			    KM_SLEEP) == 0);
163611422SMark.Musante@Sun.COM 		}
163711422SMark.Musante@Sun.COM 
163811422SMark.Musante@Sun.COM 		error = spa_load_impl(spa, pool_guid, config, state, type,
163911422SMark.Musante@Sun.COM 		    mosconfig, &ereport);
164011422SMark.Musante@Sun.COM 	}
164111422SMark.Musante@Sun.COM 
164211422SMark.Musante@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
164311422SMark.Musante@Sun.COM 	if (error && error != EBADF)
164411422SMark.Musante@Sun.COM 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
164511422SMark.Musante@Sun.COM 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
164611422SMark.Musante@Sun.COM 	spa->spa_ena = 0;
164711422SMark.Musante@Sun.COM 
164811422SMark.Musante@Sun.COM 	return (error);
164911422SMark.Musante@Sun.COM }
165011422SMark.Musante@Sun.COM 
165111422SMark.Musante@Sun.COM /*
1652789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
16531544Seschrock  * source of configuration information.
1654789Sahrens  */
1655789Sahrens static int
165611422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
165711422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
165811422SMark.Musante@Sun.COM     char **ereport)
1659789Sahrens {
1660789Sahrens 	int error = 0;
166111810SMark.Musante@Sun.COM 	nvlist_t *nvroot = NULL;
1662789Sahrens 	vdev_t *rvd;
1663789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
16641635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
16658241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
166611422SMark.Musante@Sun.COM 	int parse;
1667*12470SMatthew.Ahrens@Sun.COM 	uint64_t obj;
1668789Sahrens 
16698241SJeff.Bonwick@Sun.COM 	/*
16708241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
16718241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
16728241SJeff.Bonwick@Sun.COM 	 */
16738241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
16748241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
16758241SJeff.Bonwick@Sun.COM 
16767754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
16777754SJeff.Bonwick@Sun.COM 
16781544Seschrock 	spa->spa_load_state = state;
16791635Sbonwick 
168011422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
168111422SMark.Musante@Sun.COM 		return (EINVAL);
168211422SMark.Musante@Sun.COM 
168311422SMark.Musante@Sun.COM 	parse = (type == SPA_IMPORT_EXISTING ?
168411422SMark.Musante@Sun.COM 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
16852174Seschrock 
1686789Sahrens 	/*
16879234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
16889234SGeorge.Wilson@Sun.COM 	 */
16899630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
16909630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
16919234SGeorge.Wilson@Sun.COM 
16929234SGeorge.Wilson@Sun.COM 	/*
16932082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
16942082Seschrock 	 * value that will be returned by spa_version() since parsing the
16952082Seschrock 	 * configuration requires knowing the version number.
1696789Sahrens 	 */
16977754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
169811422SMark.Musante@Sun.COM 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
16997754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1700789Sahrens 
17012082Seschrock 	if (error != 0)
170211422SMark.Musante@Sun.COM 		return (error);
1703789Sahrens 
17041585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
170511422SMark.Musante@Sun.COM 
170611422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
170711422SMark.Musante@Sun.COM 		ASSERT(spa_guid(spa) == pool_guid);
170811422SMark.Musante@Sun.COM 	}
1709789Sahrens 
1710789Sahrens 	/*
1711789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1712789Sahrens 	 */
17137754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17144070Smc142369 	error = vdev_open(rvd);
17157754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
17164070Smc142369 	if (error != 0)
171711422SMark.Musante@Sun.COM 		return (error);
1718789Sahrens 
1719789Sahrens 	/*
17209276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
17219276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
17229276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
172311422SMark.Musante@Sun.COM 	 * that config.  Otherwise, we're validating against the cached config
17249276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
17259276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
17269276SMark.Musante@Sun.COM 	 * the vdev config.
172711422SMark.Musante@Sun.COM 	 *
172811422SMark.Musante@Sun.COM 	 * If we're assembling a new pool that's been split off from an
172911422SMark.Musante@Sun.COM 	 * existing pool, the labels haven't yet been updated so we skip
173011422SMark.Musante@Sun.COM 	 * validation for now.
17311986Seschrock 	 */
173211422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
173311422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
173411422SMark.Musante@Sun.COM 		error = vdev_validate(rvd);
173511422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
173611422SMark.Musante@Sun.COM 
173711422SMark.Musante@Sun.COM 		if (error != 0)
173811422SMark.Musante@Sun.COM 			return (error);
173911422SMark.Musante@Sun.COM 
174011422SMark.Musante@Sun.COM 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
174111422SMark.Musante@Sun.COM 			return (ENXIO);
17421986Seschrock 	}
17431986Seschrock 
17441986Seschrock 	/*
1745789Sahrens 	 * Find the best uberblock.
1746789Sahrens 	 */
17477754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1748789Sahrens 
1749789Sahrens 	/*
1750789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1751789Sahrens 	 */
175211422SMark.Musante@Sun.COM 	if (ub->ub_txg == 0)
175311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
17541544Seschrock 
17551544Seschrock 	/*
17561544Seschrock 	 * If the pool is newer than the code, we can't open it.
17571544Seschrock 	 */
175811422SMark.Musante@Sun.COM 	if (ub->ub_version > SPA_VERSION)
175911422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1760789Sahrens 
1761789Sahrens 	/*
1762789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1763789Sahrens 	 * incomplete configuration.
1764789Sahrens 	 */
176511422SMark.Musante@Sun.COM 	if (mosconfig && type != SPA_IMPORT_ASSEMBLE &&
176611422SMark.Musante@Sun.COM 	    rvd->vdev_guid_sum != ub->ub_guid_sum)
176711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
176811422SMark.Musante@Sun.COM 
176911422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
177011422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
177111422SMark.Musante@Sun.COM 		spa_try_repair(spa, config);
177211422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
177311422SMark.Musante@Sun.COM 		nvlist_free(spa->spa_config_splitting);
177411422SMark.Musante@Sun.COM 		spa->spa_config_splitting = NULL;
1775789Sahrens 	}
1776789Sahrens 
1777789Sahrens 	/*
1778789Sahrens 	 * Initialize internal SPA structures.
1779789Sahrens 	 */
1780789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1781789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
178210921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
178311727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
178410921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
178510921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
178610922SJeff.Bonwick@Sun.COM 	spa->spa_claim_max_txg = spa->spa_first_txg;
178712296SLin.Ling@Sun.COM 	spa->spa_prev_software_version = ub->ub_software_version;
178810922SJeff.Bonwick@Sun.COM 
17891544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
179011422SMark.Musante@Sun.COM 	if (error)
179111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1792789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1793789Sahrens 
179411422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
179511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
179611422SMark.Musante@Sun.COM 
1797789Sahrens 	if (!mosconfig) {
17983975Sek110237 		uint64_t hostid;
179911810SMark.Musante@Sun.COM 		nvlist_t *policy = NULL, *nvconfig;
180011810SMark.Musante@Sun.COM 
180111810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
180211810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18032082Seschrock 
180410594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
18057706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
18063975Sek110237 			char *hostname;
18073975Sek110237 			unsigned long myhostid = 0;
18083975Sek110237 
180910594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
18103975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
18113975Sek110237 
18128662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
18138662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
18148662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
18158662SJordan.Vaughan@Sun.com 			/*
18168662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
18178662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
18188662SJordan.Vaughan@Sun.com 			 */
18193975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
18208662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
18214178Slling 			if (hostid != 0 && myhostid != 0 &&
18228662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
182311810SMark.Musante@Sun.COM 				nvlist_free(nvconfig);
18243975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
18253975Sek110237 				    "loaded as it was last accessed by "
18267706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
18273975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
18287754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
18293975Sek110237 				    (unsigned long)hostid);
183011422SMark.Musante@Sun.COM 				return (EBADF);
18313975Sek110237 			}
18323975Sek110237 		}
183311727SVictor.Latushkin@Sun.COM 		if (nvlist_lookup_nvlist(spa->spa_config,
183411727SVictor.Latushkin@Sun.COM 		    ZPOOL_REWIND_POLICY, &policy) == 0)
183511727SVictor.Latushkin@Sun.COM 			VERIFY(nvlist_add_nvlist(nvconfig,
183611727SVictor.Latushkin@Sun.COM 			    ZPOOL_REWIND_POLICY, policy) == 0);
18373975Sek110237 
183810594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1839789Sahrens 		spa_unload(spa);
1840789Sahrens 		spa_deactivate(spa);
18418241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1842789Sahrens 
184311422SMark.Musante@Sun.COM 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
18441544Seschrock 	}
18451544Seschrock 
1846*12470SMatthew.Ahrens@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
1847*12470SMatthew.Ahrens@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1848*12470SMatthew.Ahrens@Sun.COM 	error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
1849*12470SMatthew.Ahrens@Sun.COM 	if (error != 0)
185011422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1851789Sahrens 
18521544Seschrock 	/*
18532082Seschrock 	 * Load the bit that tells us to use the new accounting function
18542082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
18552082Seschrock 	 * be present.
18562082Seschrock 	 */
185711422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
185811422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185911422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18602082Seschrock 
186112296SLin.Ling@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
186212296SLin.Ling@Sun.COM 	    &spa->spa_creation_version);
186312296SLin.Ling@Sun.COM 	if (error != 0 && error != ENOENT)
186412296SLin.Ling@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
186512296SLin.Ling@Sun.COM 
18662082Seschrock 	/*
18671544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
18681544Seschrock 	 * not be present.
18691544Seschrock 	 */
187011422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
187111422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
187211422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
187311422SMark.Musante@Sun.COM 
187411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
187511422SMark.Musante@Sun.COM 	    &spa->spa_errlog_scrub);
187611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
187711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1878789Sahrens 
1879789Sahrens 	/*
18802926Sek110237 	 * Load the history object.  If we have an older pool, this
18812926Sek110237 	 * will not be present.
18822926Sek110237 	 */
188311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
188411422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
188511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
188611422SMark.Musante@Sun.COM 
188711422SMark.Musante@Sun.COM 	/*
188811422SMark.Musante@Sun.COM 	 * If we're assembling the pool from the split-off vdevs of
188911422SMark.Musante@Sun.COM 	 * an existing pool, we don't want to attach the spares & cache
189011422SMark.Musante@Sun.COM 	 * devices.
189111422SMark.Musante@Sun.COM 	 */
18922926Sek110237 
18932926Sek110237 	/*
18942082Seschrock 	 * Load any hot spares for this pool.
18952082Seschrock 	 */
189611422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
189711422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
189811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
189911422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19004577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
19015450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
190211422SMark.Musante@Sun.COM 		    &spa->spa_spares.sav_config) != 0)
190311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19042082Seschrock 
19057754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19062082Seschrock 		spa_load_spares(spa);
19077754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
190811422SMark.Musante@Sun.COM 	} else if (error == 0) {
190911422SMark.Musante@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
19102082Seschrock 	}
19112082Seschrock 
19125450Sbrendan 	/*
19135450Sbrendan 	 * Load any level 2 ARC devices for this pool.
19145450Sbrendan 	 */
191511422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
19165450Sbrendan 	    &spa->spa_l2cache.sav_object);
191711422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
191811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
191911422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19205450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
19215450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
192211422SMark.Musante@Sun.COM 		    &spa->spa_l2cache.sav_config) != 0)
192311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19245450Sbrendan 
19257754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19265450Sbrendan 		spa_load_l2cache(spa);
19277754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
192811422SMark.Musante@Sun.COM 	} else if (error == 0) {
192911422SMark.Musante@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
19305450Sbrendan 	}
19315450Sbrendan 
19325094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19334543Smarks 
193411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
193511422SMark.Musante@Sun.COM 	if (error && error != ENOENT)
193611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19373912Slling 
19383912Slling 	if (error == 0) {
193911422SMark.Musante@Sun.COM 		uint64_t autoreplace;
194011422SMark.Musante@Sun.COM 
194111422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
194211422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
194311422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
194411422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
194511422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
194611422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
194711422SMark.Musante@Sun.COM 		    &spa->spa_dedup_ditto);
194811422SMark.Musante@Sun.COM 
194910672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
19503912Slling 	}
19513912Slling 
19522082Seschrock 	/*
19534451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
19544451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
19554451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
19564451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
19574451Seschrock 	 * over.
19584451Seschrock 	 */
195910672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
19604451Seschrock 		spa_check_removed(spa->spa_root_vdev);
196110672SEric.Schrock@Sun.COM 		/*
196210672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
196310672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
196410672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
196510672SEric.Schrock@Sun.COM 		 */
196610672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
196710672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
196810672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
196910672SEric.Schrock@Sun.COM 		}
197010672SEric.Schrock@Sun.COM 	}
19714451Seschrock 
19724451Seschrock 	/*
19731986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1974789Sahrens 	 */
19751986Seschrock 	vdev_load(rvd);
1976789Sahrens 
1977789Sahrens 	/*
1978789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1979789Sahrens 	 */
19807754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1981789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
19827754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1983789Sahrens 
1984789Sahrens 	/*
1985789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1986789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1987789Sahrens 	 */
198811422SMark.Musante@Sun.COM 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
198911422SMark.Musante@Sun.COM 		return (ENXIO);
1990789Sahrens 
199110922SJeff.Bonwick@Sun.COM 	/*
199210922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
199310922SJeff.Bonwick@Sun.COM 	 */
199410922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
199511422SMark.Musante@Sun.COM 	if (error != 0)
199611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
199710922SJeff.Bonwick@Sun.COM 
199810956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
199910956SGeorge.Wilson@Sun.COM 
200010921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
200110921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
200211422SMark.Musante@Sun.COM 		if (error)
200311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
200411422SMark.Musante@Sun.COM 			    error));
200510921STim.Haley@Sun.COM 	}
200610921STim.Haley@Sun.COM 
200710922SJeff.Bonwick@Sun.COM 	/*
200811422SMark.Musante@Sun.COM 	 * Load the intent log state and check log integrity.  If we're
200911422SMark.Musante@Sun.COM 	 * assembling a pool from a split, the log is not transferred over.
201010922SJeff.Bonwick@Sun.COM 	 */
201111422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
201211810SMark.Musante@Sun.COM 		nvlist_t *nvconfig;
201311810SMark.Musante@Sun.COM 
201411810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
201511810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
201611810SMark.Musante@Sun.COM 
201711422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
201811422SMark.Musante@Sun.COM 		    &nvroot) == 0);
201911422SMark.Musante@Sun.COM 		spa_load_log_state(spa, nvroot);
202011422SMark.Musante@Sun.COM 		nvlist_free(nvconfig);
202111422SMark.Musante@Sun.COM 
202211422SMark.Musante@Sun.COM 		if (spa_check_logs(spa)) {
202311422SMark.Musante@Sun.COM 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
202411422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
202511422SMark.Musante@Sun.COM 		}
202610922SJeff.Bonwick@Sun.COM 	}
202710922SJeff.Bonwick@Sun.COM 
202810921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
202910921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
20301635Sbonwick 		dmu_tx_t *tx;
20311635Sbonwick 		int need_update = B_FALSE;
20328241SJeff.Bonwick@Sun.COM 
20338241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
20341601Sbonwick 
20351635Sbonwick 		/*
20361635Sbonwick 		 * Claim log blocks that haven't been committed yet.
20371635Sbonwick 		 * This must all happen in a single txg.
203810922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
203910922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
204010921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
20411635Sbonwick 		 */
204210922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
204310922SJeff.Bonwick@Sun.COM 
20441601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2045789Sahrens 		    spa_first_txg(spa));
20467754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
20472417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2048789Sahrens 		dmu_tx_commit(tx);
2049789Sahrens 
205010922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
205110922SJeff.Bonwick@Sun.COM 
205211422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_GOOD);
2053789Sahrens 		spa->spa_sync_on = B_TRUE;
2054789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2055789Sahrens 
2056789Sahrens 		/*
205710922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
205810922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
205910922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
206010922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
206110922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
2062789Sahrens 		 */
206310922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
20641585Sbonwick 
20651585Sbonwick 		/*
20661635Sbonwick 		 * If the config cache is stale, or we have uninitialized
20671635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
206810100SLin.Ling@Sun.COM 		 *
206910100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
207010100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
20711585Sbonwick 		 */
20721635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
207310921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
207410921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
20751635Sbonwick 			need_update = B_TRUE;
20761635Sbonwick 
20778241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
20781635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
20791635Sbonwick 				need_update = B_TRUE;
20801585Sbonwick 
20811585Sbonwick 		/*
20821635Sbonwick 		 * Update the config cache asychronously in case we're the
20831635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
20841585Sbonwick 		 */
20851635Sbonwick 		if (need_update)
20861635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20878241SJeff.Bonwick@Sun.COM 
20888241SJeff.Bonwick@Sun.COM 		/*
20898241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
20908241SJeff.Bonwick@Sun.COM 		 */
209112296SLin.Ling@Sun.COM 		if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
209212296SLin.Ling@Sun.COM 		    vdev_resilver_needed(rvd, NULL, NULL))
20938241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
209410298SMatthew.Ahrens@Sun.COM 
209510298SMatthew.Ahrens@Sun.COM 		/*
209610298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
209710298SMatthew.Ahrens@Sun.COM 		 */
209810298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
209910298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
210010342Schris.kirby@sun.com 
210110342Schris.kirby@sun.com 		/*
210210342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
210310342Schris.kirby@sun.com 		 */
210410342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2105789Sahrens 	}
2106789Sahrens 
210711422SMark.Musante@Sun.COM 	return (0);
2108789Sahrens }
2109789Sahrens 
211010921STim.Haley@Sun.COM static int
211110921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
211210921STim.Haley@Sun.COM {
211310921STim.Haley@Sun.COM 	spa_unload(spa);
211410921STim.Haley@Sun.COM 	spa_deactivate(spa);
211510921STim.Haley@Sun.COM 
211610921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
211710921STim.Haley@Sun.COM 
211810921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
211910921STim.Haley@Sun.COM 	spa_async_suspend(spa);
212010921STim.Haley@Sun.COM 
212111422SMark.Musante@Sun.COM 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
212210921STim.Haley@Sun.COM }
212310921STim.Haley@Sun.COM 
212410921STim.Haley@Sun.COM static int
212510921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
212611727SVictor.Latushkin@Sun.COM     uint64_t max_request, int rewind_flags)
212710921STim.Haley@Sun.COM {
212810921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
212910921STim.Haley@Sun.COM 	int load_error, rewind_error;
213011727SVictor.Latushkin@Sun.COM 	uint64_t safe_rewind_txg;
213110921STim.Haley@Sun.COM 	uint64_t min_txg;
213210921STim.Haley@Sun.COM 
213311026STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
213410921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
213511422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
213611026STim.Haley@Sun.COM 	} else {
213710921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
213811026STim.Haley@Sun.COM 	}
213910921STim.Haley@Sun.COM 
214011422SMark.Musante@Sun.COM 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
214111422SMark.Musante@Sun.COM 	    mosconfig);
214210921STim.Haley@Sun.COM 	if (load_error == 0)
214310921STim.Haley@Sun.COM 		return (0);
214410921STim.Haley@Sun.COM 
214510921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
214610921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
214710921STim.Haley@Sun.COM 
214810921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
214910921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
215010921STim.Haley@Sun.COM 
215111727SVictor.Latushkin@Sun.COM 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
215210921STim.Haley@Sun.COM 		nvlist_free(config);
215310921STim.Haley@Sun.COM 		return (load_error);
215410921STim.Haley@Sun.COM 	}
215510921STim.Haley@Sun.COM 
215610921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
215710921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
215811422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
215910921STim.Haley@Sun.COM 
216011727SVictor.Latushkin@Sun.COM 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
216111727SVictor.Latushkin@Sun.COM 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
216211727SVictor.Latushkin@Sun.COM 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
216311727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL : safe_rewind_txg;
216411727SVictor.Latushkin@Sun.COM 
216511727SVictor.Latushkin@Sun.COM 	/*
216611727SVictor.Latushkin@Sun.COM 	 * Continue as long as we're finding errors, we're still within
216711727SVictor.Latushkin@Sun.COM 	 * the acceptable rewind range, and we're still finding uberblocks
216811727SVictor.Latushkin@Sun.COM 	 */
216911727SVictor.Latushkin@Sun.COM 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
217011727SVictor.Latushkin@Sun.COM 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
217111727SVictor.Latushkin@Sun.COM 		if (spa->spa_load_max_txg < safe_rewind_txg)
217210921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
217310921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
217410921STim.Haley@Sun.COM 	}
217510921STim.Haley@Sun.COM 
217610921STim.Haley@Sun.COM 	if (config)
217710921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
217810921STim.Haley@Sun.COM 
217910921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
218010921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
218110921STim.Haley@Sun.COM 
218210921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
218310921STim.Haley@Sun.COM 		spa_config_set(spa, config);
218410921STim.Haley@Sun.COM 
218510921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
218610921STim.Haley@Sun.COM }
218710921STim.Haley@Sun.COM 
2188789Sahrens /*
2189789Sahrens  * Pool Open/Import
2190789Sahrens  *
2191789Sahrens  * The import case is identical to an open except that the configuration is sent
2192789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2193789Sahrens  * case of an open, the pool configuration will exist in the
21944451Seschrock  * POOL_STATE_UNINITIALIZED state.
2195789Sahrens  *
2196789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2197789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2198789Sahrens  * ambiguous state.
2199789Sahrens  */
2200789Sahrens static int
220110921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
220210921STim.Haley@Sun.COM     nvlist_t **config)
2203789Sahrens {
2204789Sahrens 	spa_t *spa;
2205789Sahrens 	int error;
2206789Sahrens 	int locked = B_FALSE;
2207789Sahrens 
2208789Sahrens 	*spapp = NULL;
2209789Sahrens 
2210789Sahrens 	/*
2211789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2212789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2213789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2214789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2215789Sahrens 	 */
2216789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2217789Sahrens 		mutex_enter(&spa_namespace_lock);
2218789Sahrens 		locked = B_TRUE;
2219789Sahrens 	}
2220789Sahrens 
2221789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2222789Sahrens 		if (locked)
2223789Sahrens 			mutex_exit(&spa_namespace_lock);
2224789Sahrens 		return (ENOENT);
2225789Sahrens 	}
222610921STim.Haley@Sun.COM 
2227789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
222811819STim.Haley@Sun.COM 		spa_load_state_t state = SPA_LOAD_OPEN;
222911819STim.Haley@Sun.COM 		zpool_rewind_policy_t policy;
223011819STim.Haley@Sun.COM 
223111819STim.Haley@Sun.COM 		zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
223211819STim.Haley@Sun.COM 		    &policy);
223311819STim.Haley@Sun.COM 		if (policy.zrp_request & ZPOOL_DO_REWIND)
223411819STim.Haley@Sun.COM 			state = SPA_LOAD_RECOVER;
2235789Sahrens 
22368241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
2237789Sahrens 
223810921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
223910921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
224010921STim.Haley@Sun.COM 
224110921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
224211727SVictor.Latushkin@Sun.COM 		    policy.zrp_request);
2243789Sahrens 
2244789Sahrens 		if (error == EBADF) {
2245789Sahrens 			/*
22461986Seschrock 			 * If vdev_validate() returns failure (indicated by
22471986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
22481986Seschrock 			 * that the pool has been exported or destroyed.  If
22491986Seschrock 			 * this is the case, the config cache is out of sync and
22501986Seschrock 			 * we should remove the pool from the namespace.
2251789Sahrens 			 */
2252789Sahrens 			spa_unload(spa);
2253789Sahrens 			spa_deactivate(spa);
22546643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2255789Sahrens 			spa_remove(spa);
2256789Sahrens 			if (locked)
2257789Sahrens 				mutex_exit(&spa_namespace_lock);
2258789Sahrens 			return (ENOENT);
22591544Seschrock 		}
22601544Seschrock 
22611544Seschrock 		if (error) {
2262789Sahrens 			/*
2263789Sahrens 			 * We can't open the pool, but we still have useful
2264789Sahrens 			 * information: the state of each vdev after the
2265789Sahrens 			 * attempted vdev_open().  Return this to the user.
2266789Sahrens 			 */
226710921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
226810921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
226910921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
2270789Sahrens 			spa_unload(spa);
2271789Sahrens 			spa_deactivate(spa);
227210921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
2273789Sahrens 			if (locked)
2274789Sahrens 				mutex_exit(&spa_namespace_lock);
2275789Sahrens 			*spapp = NULL;
2276789Sahrens 			return (error);
2277789Sahrens 		}
227810921STim.Haley@Sun.COM 
2279789Sahrens 	}
2280789Sahrens 
2281789Sahrens 	spa_open_ref(spa, tag);
22824451Seschrock 
228310921STim.Haley@Sun.COM 
228410921STim.Haley@Sun.COM 	if (config != NULL)
228510921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
228610921STim.Haley@Sun.COM 
228711026STim.Haley@Sun.COM 	if (locked) {
228811026STim.Haley@Sun.COM 		spa->spa_last_open_failed = 0;
228911026STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = 0;
229011026STim.Haley@Sun.COM 		spa->spa_load_txg = 0;
2291789Sahrens 		mutex_exit(&spa_namespace_lock);
229211026STim.Haley@Sun.COM 	}
2293789Sahrens 
2294789Sahrens 	*spapp = spa;
2295789Sahrens 
2296789Sahrens 	return (0);
2297789Sahrens }
2298789Sahrens 
2299789Sahrens int
230010921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
230110921STim.Haley@Sun.COM     nvlist_t **config)
230210921STim.Haley@Sun.COM {
230310921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
230410921STim.Haley@Sun.COM }
230510921STim.Haley@Sun.COM 
230610921STim.Haley@Sun.COM int
2307789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2308789Sahrens {
230910921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2310789Sahrens }
2311789Sahrens 
23121544Seschrock /*
23131544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
23141544Seschrock  * preventing it from being exported or destroyed.
23151544Seschrock  */
23161544Seschrock spa_t *
23171544Seschrock spa_inject_addref(char *name)
23181544Seschrock {
23191544Seschrock 	spa_t *spa;
23201544Seschrock 
23211544Seschrock 	mutex_enter(&spa_namespace_lock);
23221544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
23231544Seschrock 		mutex_exit(&spa_namespace_lock);
23241544Seschrock 		return (NULL);
23251544Seschrock 	}
23261544Seschrock 	spa->spa_inject_ref++;
23271544Seschrock 	mutex_exit(&spa_namespace_lock);
23281544Seschrock 
23291544Seschrock 	return (spa);
23301544Seschrock }
23311544Seschrock 
23321544Seschrock void
23331544Seschrock spa_inject_delref(spa_t *spa)
23341544Seschrock {
23351544Seschrock 	mutex_enter(&spa_namespace_lock);
23361544Seschrock 	spa->spa_inject_ref--;
23371544Seschrock 	mutex_exit(&spa_namespace_lock);
23381544Seschrock }
23391544Seschrock 
23405450Sbrendan /*
23415450Sbrendan  * Add spares device information to the nvlist.
23425450Sbrendan  */
23432082Seschrock static void
23442082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
23452082Seschrock {
23462082Seschrock 	nvlist_t **spares;
23472082Seschrock 	uint_t i, nspares;
23482082Seschrock 	nvlist_t *nvroot;
23492082Seschrock 	uint64_t guid;
23502082Seschrock 	vdev_stat_t *vs;
23512082Seschrock 	uint_t vsc;
23523377Seschrock 	uint64_t pool;
23532082Seschrock 
23549425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23559425SEric.Schrock@Sun.COM 
23565450Sbrendan 	if (spa->spa_spares.sav_count == 0)
23572082Seschrock 		return;
23582082Seschrock 
23592082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
23602082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23615450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
23622082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23632082Seschrock 	if (nspares != 0) {
23642082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
23652082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
23662082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23672082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23682082Seschrock 
23692082Seschrock 		/*
23702082Seschrock 		 * Go through and find any spares which have since been
23712082Seschrock 		 * repurposed as an active spare.  If this is the case, update
23722082Seschrock 		 * their status appropriately.
23732082Seschrock 		 */
23742082Seschrock 		for (i = 0; i < nspares; i++) {
23752082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
23762082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23777214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
23787214Slling 			    pool != 0ULL) {
23792082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
238012296SLin.Ling@Sun.COM 				    spares[i], ZPOOL_CONFIG_VDEV_STATS,
23812082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
23822082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
23832082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
23842082Seschrock 			}
23852082Seschrock 		}
23862082Seschrock 	}
23872082Seschrock }
23882082Seschrock 
23895450Sbrendan /*
23905450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
23915450Sbrendan  */
23925450Sbrendan static void
23935450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
23945450Sbrendan {
23955450Sbrendan 	nvlist_t **l2cache;
23965450Sbrendan 	uint_t i, j, nl2cache;
23975450Sbrendan 	nvlist_t *nvroot;
23985450Sbrendan 	uint64_t guid;
23995450Sbrendan 	vdev_t *vd;
24005450Sbrendan 	vdev_stat_t *vs;
24015450Sbrendan 	uint_t vsc;
24025450Sbrendan 
24039425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
24049425SEric.Schrock@Sun.COM 
24055450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
24065450Sbrendan 		return;
24075450Sbrendan 
24085450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
24095450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
24105450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
24115450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24125450Sbrendan 	if (nl2cache != 0) {
24135450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
24145450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
24155450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
24165450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24175450Sbrendan 
24185450Sbrendan 		/*
24195450Sbrendan 		 * Update level 2 cache device stats.
24205450Sbrendan 		 */
24215450Sbrendan 
24225450Sbrendan 		for (i = 0; i < nl2cache; i++) {
24235450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
24245450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
24255450Sbrendan 
24265450Sbrendan 			vd = NULL;
24275450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
24285450Sbrendan 				if (guid ==
24295450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
24305450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
24315450Sbrendan 					break;
24325450Sbrendan 				}
24335450Sbrendan 			}
24345450Sbrendan 			ASSERT(vd != NULL);
24355450Sbrendan 
24365450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
243712296SLin.Ling@Sun.COM 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
243812296SLin.Ling@Sun.COM 			    == 0);
24395450Sbrendan 			vdev_get_stats(vd, vs);
24405450Sbrendan 		}
24415450Sbrendan 	}
24425450Sbrendan }
24435450Sbrendan 
2444789Sahrens int
24451544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2446789Sahrens {
2447789Sahrens 	int error;
2448789Sahrens 	spa_t *spa;
2449789Sahrens 
2450789Sahrens 	*config = NULL;
245110921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2452789Sahrens 
24539425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24549425SEric.Schrock@Sun.COM 		/*
24559425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
24569425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
24579425SEric.Schrock@Sun.COM 		 * self-inconsistent.
24589425SEric.Schrock@Sun.COM 		 */
24599425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
24609425SEric.Schrock@Sun.COM 
24619425SEric.Schrock@Sun.COM 		if (*config != NULL) {
24627754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
24639425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
24649425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
24659425SEric.Schrock@Sun.COM 
24669425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
24679425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
24689425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
24699425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
24709425SEric.Schrock@Sun.COM 
24719425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
24729425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
24739425SEric.Schrock@Sun.COM 		}
24742082Seschrock 	}
24752082Seschrock 
24761544Seschrock 	/*
24771544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
24781544Seschrock 	 * and call spa_lookup() directly.
24791544Seschrock 	 */
24801544Seschrock 	if (altroot) {
24811544Seschrock 		if (spa == NULL) {
24821544Seschrock 			mutex_enter(&spa_namespace_lock);
24831544Seschrock 			spa = spa_lookup(name);
24841544Seschrock 			if (spa)
24851544Seschrock 				spa_altroot(spa, altroot, buflen);
24861544Seschrock 			else
24871544Seschrock 				altroot[0] = '\0';
24881544Seschrock 			spa = NULL;
24891544Seschrock 			mutex_exit(&spa_namespace_lock);
24901544Seschrock 		} else {
24911544Seschrock 			spa_altroot(spa, altroot, buflen);
24921544Seschrock 		}
24931544Seschrock 	}
24941544Seschrock 
24959425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24969425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2497789Sahrens 		spa_close(spa, FTAG);
24989425SEric.Schrock@Sun.COM 	}
2499789Sahrens 
2500789Sahrens 	return (error);
2501789Sahrens }
2502789Sahrens 
2503789Sahrens /*
25045450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
25055450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
25065450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
25075450Sbrendan  * specified, as long as they are well-formed.
25082082Seschrock  */
25092082Seschrock static int
25105450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
25115450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
25125450Sbrendan     vdev_labeltype_t label)
25132082Seschrock {
25145450Sbrendan 	nvlist_t **dev;
25155450Sbrendan 	uint_t i, ndev;
25162082Seschrock 	vdev_t *vd;
25172082Seschrock 	int error;
25182082Seschrock 
25197754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25207754SJeff.Bonwick@Sun.COM 
25212082Seschrock 	/*
25225450Sbrendan 	 * It's acceptable to have no devs specified.
25232082Seschrock 	 */
25245450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
25252082Seschrock 		return (0);
25262082Seschrock 
25275450Sbrendan 	if (ndev == 0)
25282082Seschrock 		return (EINVAL);
25292082Seschrock 
25302082Seschrock 	/*
25315450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
25325450Sbrendan 	 * device type.
25332082Seschrock 	 */
25345450Sbrendan 	if (spa_version(spa) < version)
25352082Seschrock 		return (ENOTSUP);
25362082Seschrock 
25373377Seschrock 	/*
25385450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
25393377Seschrock 	 * checking.
25403377Seschrock 	 */
25415450Sbrendan 	sav->sav_pending = dev;
25425450Sbrendan 	sav->sav_npending = ndev;
25435450Sbrendan 
25445450Sbrendan 	for (i = 0; i < ndev; i++) {
25455450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
25462082Seschrock 		    mode)) != 0)
25473377Seschrock 			goto out;
25482082Seschrock 
25492082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
25502082Seschrock 			vdev_free(vd);
25513377Seschrock 			error = EINVAL;
25523377Seschrock 			goto out;
25532082Seschrock 		}
25542082Seschrock 
25555450Sbrendan 		/*
25567754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
25577754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
25585450Sbrendan 		 */
25597754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
25605450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
25615450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
25625450Sbrendan 			error = ENOTBLK;
25635450Sbrendan 			goto out;
25645450Sbrendan 		}
25657754SJeff.Bonwick@Sun.COM #endif
25662082Seschrock 		vd->vdev_top = vd;
25673377Seschrock 
25683377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
25695450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
25705450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
25713377Seschrock 			    vd->vdev_guid) == 0);
25722082Seschrock 		}
25732082Seschrock 
25742082Seschrock 		vdev_free(vd);
25753377Seschrock 
25765450Sbrendan 		if (error &&
25775450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
25783377Seschrock 			goto out;
25793377Seschrock 		else
25803377Seschrock 			error = 0;
25812082Seschrock 	}
25822082Seschrock 
25833377Seschrock out:
25845450Sbrendan 	sav->sav_pending = NULL;
25855450Sbrendan 	sav->sav_npending = 0;
25863377Seschrock 	return (error);
25872082Seschrock }
25882082Seschrock 
25895450Sbrendan static int
25905450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
25915450Sbrendan {
25925450Sbrendan 	int error;
25935450Sbrendan 
25947754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25957754SJeff.Bonwick@Sun.COM 
25965450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
25975450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
25985450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
25995450Sbrendan 		return (error);
26005450Sbrendan 	}
26015450Sbrendan 
26025450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
26035450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
26045450Sbrendan 	    VDEV_LABEL_L2CACHE));
26055450Sbrendan }
26065450Sbrendan 
26075450Sbrendan static void
26085450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
26095450Sbrendan     const char *config)
26105450Sbrendan {
26115450Sbrendan 	int i;
26125450Sbrendan 
26135450Sbrendan 	if (sav->sav_config != NULL) {
26145450Sbrendan 		nvlist_t **olddevs;
26155450Sbrendan 		uint_t oldndevs;
26165450Sbrendan 		nvlist_t **newdevs;
26175450Sbrendan 
26185450Sbrendan 		/*
26195450Sbrendan 		 * Generate new dev list by concatentating with the
26205450Sbrendan 		 * current dev list.
26215450Sbrendan 		 */
26225450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
26235450Sbrendan 		    &olddevs, &oldndevs) == 0);
26245450Sbrendan 
26255450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
26265450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
26275450Sbrendan 		for (i = 0; i < oldndevs; i++)
26285450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
26295450Sbrendan 			    KM_SLEEP) == 0);
26305450Sbrendan 		for (i = 0; i < ndevs; i++)
26315450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
26325450Sbrendan 			    KM_SLEEP) == 0);
26335450Sbrendan 
26345450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
26355450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
26365450Sbrendan 
26375450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
26385450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
26395450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
26405450Sbrendan 			nvlist_free(newdevs[i]);
26415450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
26425450Sbrendan 	} else {
26435450Sbrendan 		/*
26445450Sbrendan 		 * Generate a new dev list.
26455450Sbrendan 		 */
26465450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
26475450Sbrendan 		    KM_SLEEP) == 0);
26485450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
26495450Sbrendan 		    devs, ndevs) == 0);
26505450Sbrendan 	}
26515450Sbrendan }
26525450Sbrendan 
26535450Sbrendan /*
26545450Sbrendan  * Stop and drop level 2 ARC devices
26555450Sbrendan  */
26565450Sbrendan void
26575450Sbrendan spa_l2cache_drop(spa_t *spa)
26585450Sbrendan {
26595450Sbrendan 	vdev_t *vd;
26605450Sbrendan 	int i;
26615450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
26625450Sbrendan 
26635450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
26645450Sbrendan 		uint64_t pool;
26655450Sbrendan 
26665450Sbrendan 		vd = sav->sav_vdevs[i];
26675450Sbrendan 		ASSERT(vd != NULL);
26685450Sbrendan 
26698241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
26708241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
26715450Sbrendan 			l2arc_remove_vdev(vd);
26725450Sbrendan 		if (vd->vdev_isl2cache)
26735450Sbrendan 			spa_l2cache_remove(vd);
26745450Sbrendan 		vdev_clear_stats(vd);
26755450Sbrendan 		(void) vdev_close(vd);
26765450Sbrendan 	}
26775450Sbrendan }
26785450Sbrendan 
26792082Seschrock /*
2680789Sahrens  * Pool Creation
2681789Sahrens  */
2682789Sahrens int
26835094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
26847184Stimh     const char *history_str, nvlist_t *zplprops)
2685789Sahrens {
2686789Sahrens 	spa_t *spa;
26875094Slling 	char *altroot = NULL;
26881635Sbonwick 	vdev_t *rvd;
2689789Sahrens 	dsl_pool_t *dp;
2690789Sahrens 	dmu_tx_t *tx;
26919816SGeorge.Wilson@Sun.COM 	int error = 0;
2692789Sahrens 	uint64_t txg = TXG_INITIAL;
26935450Sbrendan 	nvlist_t **spares, **l2cache;
26945450Sbrendan 	uint_t nspares, nl2cache;
2695*12470SMatthew.Ahrens@Sun.COM 	uint64_t version, obj;
2696789Sahrens 
2697789Sahrens 	/*
2698789Sahrens 	 * If this pool already exists, return failure.
2699789Sahrens 	 */
2700789Sahrens 	mutex_enter(&spa_namespace_lock);
2701789Sahrens 	if (spa_lookup(pool) != NULL) {
2702789Sahrens 		mutex_exit(&spa_namespace_lock);
2703789Sahrens 		return (EEXIST);
2704789Sahrens 	}
2705789Sahrens 
2706789Sahrens 	/*
2707789Sahrens 	 * Allocate a new spa_t structure.
2708789Sahrens 	 */
27095094Slling 	(void) nvlist_lookup_string(props,
27105094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
271110921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
27128241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2713789Sahrens 
27145094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
27155094Slling 		spa_deactivate(spa);
27165094Slling 		spa_remove(spa);
27176643Seschrock 		mutex_exit(&spa_namespace_lock);
27185094Slling 		return (error);
27195094Slling 	}
27205094Slling 
27215094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
27225094Slling 	    &version) != 0)
27235094Slling 		version = SPA_VERSION;
27245094Slling 	ASSERT(version <= SPA_VERSION);
272510922SJeff.Bonwick@Sun.COM 
272610922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
272710922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
27285094Slling 	spa->spa_uberblock.ub_version = version;
2729789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2730789Sahrens 
27311635Sbonwick 	/*
27329234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
27339234SGeorge.Wilson@Sun.COM 	 */
27349630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
27359630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
27369234SGeorge.Wilson@Sun.COM 
27379234SGeorge.Wilson@Sun.COM 	/*
27381635Sbonwick 	 * Create the root vdev.
27391635Sbonwick 	 */
27407754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27411635Sbonwick 
27422082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
27432082Seschrock 
27442082Seschrock 	ASSERT(error != 0 || rvd != NULL);
27452082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
27462082Seschrock 
27475913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
27481635Sbonwick 		error = EINVAL;
27492082Seschrock 
27502082Seschrock 	if (error == 0 &&
27512082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
27525450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
27532082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
27549816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
27559816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
27569816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
27579816SGeorge.Wilson@Sun.COM 		}
27581635Sbonwick 	}
27591635Sbonwick 
27607754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2761789Sahrens 
27622082Seschrock 	if (error != 0) {
2763789Sahrens 		spa_unload(spa);
2764789Sahrens 		spa_deactivate(spa);
2765789Sahrens 		spa_remove(spa);
2766789Sahrens 		mutex_exit(&spa_namespace_lock);
2767789Sahrens 		return (error);
2768789Sahrens 	}
2769789Sahrens 
27702082Seschrock 	/*
27712082Seschrock 	 * Get the list of spares, if specified.
27722082Seschrock 	 */
27732082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
27742082Seschrock 	    &spares, &nspares) == 0) {
27755450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
27762082Seschrock 		    KM_SLEEP) == 0);
27775450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
27782082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
27797754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27802082Seschrock 		spa_load_spares(spa);
27817754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27825450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27835450Sbrendan 	}
27845450Sbrendan 
27855450Sbrendan 	/*
27865450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
27875450Sbrendan 	 */
27885450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
27895450Sbrendan 	    &l2cache, &nl2cache) == 0) {
27905450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
27915450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
27925450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
27935450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
27947754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27955450Sbrendan 		spa_load_l2cache(spa);
27967754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27975450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
27982082Seschrock 	}
27992082Seschrock 
28007184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2801789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2802789Sahrens 
280310956SGeorge.Wilson@Sun.COM 	/*
280410956SGeorge.Wilson@Sun.COM 	 * Create DDTs (dedup tables).
280510956SGeorge.Wilson@Sun.COM 	 */
280610956SGeorge.Wilson@Sun.COM 	ddt_create(spa);
280710956SGeorge.Wilson@Sun.COM 
280810956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
280910956SGeorge.Wilson@Sun.COM 
2810789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2811789Sahrens 
2812789Sahrens 	/*
2813789Sahrens 	 * Create the pool config object.
2814789Sahrens 	 */
2815789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
28167497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2817789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2818789Sahrens 
28191544Seschrock 	if (zap_add(spa->spa_meta_objset,
2820789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
28211544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
28221544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
28231544Seschrock 	}
2824789Sahrens 
282512296SLin.Ling@Sun.COM 	if (zap_add(spa->spa_meta_objset,
282612296SLin.Ling@Sun.COM 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
282712296SLin.Ling@Sun.COM 	    sizeof (uint64_t), 1, &version, tx) != 0) {
282812296SLin.Ling@Sun.COM 		cmn_err(CE_PANIC, "failed to add pool version");
282912296SLin.Ling@Sun.COM 	}
283012296SLin.Ling@Sun.COM 
28315094Slling 	/* Newly created pools with the right version are always deflated. */
28325094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
28335094Slling 		spa->spa_deflate = TRUE;
28345094Slling 		if (zap_add(spa->spa_meta_objset,
28355094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
28365094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
28375094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
28385094Slling 		}
28392082Seschrock 	}
28402082Seschrock 
2841789Sahrens 	/*
2842*12470SMatthew.Ahrens@Sun.COM 	 * Create the deferred-free bpobj.  Turn off compression
2843789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2844789Sahrens 	 * keeps changing.
2845789Sahrens 	 */
2846*12470SMatthew.Ahrens@Sun.COM 	obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
2847*12470SMatthew.Ahrens@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset, obj,
2848*12470SMatthew.Ahrens@Sun.COM 	    ZIO_COMPRESS_OFF, tx);
28491544Seschrock 	if (zap_add(spa->spa_meta_objset,
2850*12470SMatthew.Ahrens@Sun.COM 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
2851*12470SMatthew.Ahrens@Sun.COM 	    sizeof (uint64_t), 1, &obj, tx) != 0) {
2852*12470SMatthew.Ahrens@Sun.COM 		cmn_err(CE_PANIC, "failed to add bpobj");
28531544Seschrock 	}
2854*12470SMatthew.Ahrens@Sun.COM 	VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
2855*12470SMatthew.Ahrens@Sun.COM 	    spa->spa_meta_objset, obj));
2856789Sahrens 
28572926Sek110237 	/*
28582926Sek110237 	 * Create the pool's history object.
28592926Sek110237 	 */
28605094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
28615094Slling 		spa_history_create_obj(spa, tx);
28625094Slling 
28635094Slling 	/*
28645094Slling 	 * Set pool properties.
28655094Slling 	 */
28665094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
28675094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
28685329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
28699816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
287010922SJeff.Bonwick@Sun.COM 
28718525SEric.Schrock@Sun.COM 	if (props != NULL) {
28728525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
287312296SLin.Ling@Sun.COM 		spa_sync_props(spa, props, tx);
28748525SEric.Schrock@Sun.COM 	}
28752926Sek110237 
2876789Sahrens 	dmu_tx_commit(tx);
2877789Sahrens 
2878789Sahrens 	spa->spa_sync_on = B_TRUE;
2879789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2880789Sahrens 
2881789Sahrens 	/*
2882789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2883789Sahrens 	 * bean counters are appropriately updated.
2884789Sahrens 	 */
2885789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2886789Sahrens 
28876643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2888789Sahrens 
28895094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
28904715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
28919946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
28924715Sek110237 
28938667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
28948667SGeorge.Wilson@Sun.COM 
2895789Sahrens 	mutex_exit(&spa_namespace_lock);
2896789Sahrens 
2897789Sahrens 	return (0);
2898789Sahrens }
2899789Sahrens 
29006423Sgw25295 #ifdef _KERNEL
29016423Sgw25295 /*
29029790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
29039790SLin.Ling@Sun.COM  * during the system boot up time.
29046423Sgw25295  */
29059790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
29069790SLin.Ling@Sun.COM 
29079790SLin.Ling@Sun.COM static nvlist_t *
29089790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
29096423Sgw25295 {
29109790SLin.Ling@Sun.COM 	nvlist_t *config;
29116423Sgw25295 	nvlist_t *nvtop, *nvroot;
29126423Sgw25295 	uint64_t pgid;
29136423Sgw25295 
29149790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
29159790SLin.Ling@Sun.COM 		return (NULL);
29169790SLin.Ling@Sun.COM 
29176423Sgw25295 	/*
29186423Sgw25295 	 * Add this top-level vdev to the child array.
29196423Sgw25295 	 */
29209790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29219790SLin.Ling@Sun.COM 	    &nvtop) == 0);
29229790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
29239790SLin.Ling@Sun.COM 	    &pgid) == 0);
29249790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
29256423Sgw25295 
29266423Sgw25295 	/*
29276423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
29286423Sgw25295 	 */
29296423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
29309790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
29319790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
29326423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
29336423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
29346423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
29356423Sgw25295 	    &nvtop, 1) == 0);
29366423Sgw25295 
29376423Sgw25295 	/*
29386423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
29396423Sgw25295 	 * this pool's configuration (remove the old, add the new).
29406423Sgw25295 	 */
29416423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
29426423Sgw25295 	nvlist_free(nvroot);
29439790SLin.Ling@Sun.COM 	return (config);
29446423Sgw25295 }
29456423Sgw25295 
29466423Sgw25295 /*
29479790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
29489790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
29499790SLin.Ling@Sun.COM  * device has a more recent txg.
29506423Sgw25295  */
29519790SLin.Ling@Sun.COM static void
29529790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
29537147Staylor {
29549816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
29559790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
29569790SLin.Ling@Sun.COM 
29579790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
29589790SLin.Ling@Sun.COM 		nvlist_t *label;
29599790SLin.Ling@Sun.COM 		uint64_t label_txg;
29609790SLin.Ling@Sun.COM 
29619790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
29629790SLin.Ling@Sun.COM 		    &label) != 0)
29639790SLin.Ling@Sun.COM 			return;
29649790SLin.Ling@Sun.COM 
29659790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
29669790SLin.Ling@Sun.COM 		    &label_txg) == 0);
29679790SLin.Ling@Sun.COM 
29689790SLin.Ling@Sun.COM 		/*
29699790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
29709790SLin.Ling@Sun.COM 		 */
29719790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
29729790SLin.Ling@Sun.COM 			*txg = label_txg;
29739790SLin.Ling@Sun.COM 			*avd = vd;
29747147Staylor 		}
29759790SLin.Ling@Sun.COM 		nvlist_free(label);
29767147Staylor 	}
29777147Staylor }
29787147Staylor 
29796423Sgw25295 /*
29806423Sgw25295  * Import a root pool.
29816423Sgw25295  *
29827147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
29837147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
29847147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
29856423Sgw25295  *
29866423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
29876423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
29886423Sgw25295  * e.g.
29896423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
29906423Sgw25295  */
29916423Sgw25295 int
29927147Staylor spa_import_rootpool(char *devpath, char *devid)
29936423Sgw25295 {
29949790SLin.Ling@Sun.COM 	spa_t *spa;
29959790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
29969790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
29979790SLin.Ling@Sun.COM 	uint64_t guid, txg;
29986423Sgw25295 	char *pname;
29996423Sgw25295 	int error;
30006423Sgw25295 
30016423Sgw25295 	/*
30029790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
30036423Sgw25295 	 */
300410822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
300510822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
300610822SJack.Meng@Sun.COM 	if (config == NULL) {
300710822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
300810822SJack.Meng@Sun.COM 			/* iscsi boot */
300910822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
301010822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
301110822SJack.Meng@Sun.COM 		}
301210822SJack.Meng@Sun.COM 	}
301310822SJack.Meng@Sun.COM #endif
301410822SJack.Meng@Sun.COM 	if (config == NULL) {
30159790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
30169790SLin.Ling@Sun.COM 		    devpath);
30179790SLin.Ling@Sun.COM 		return (EIO);
30189790SLin.Ling@Sun.COM 	}
30199790SLin.Ling@Sun.COM 
30209790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
30219790SLin.Ling@Sun.COM 	    &pname) == 0);
30229790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
30236423Sgw25295 
30249425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30259425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
30269425SEric.Schrock@Sun.COM 		/*
30279425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
30289425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
30299425SEric.Schrock@Sun.COM 		 */
30309425SEric.Schrock@Sun.COM 		spa_remove(spa);
30319425SEric.Schrock@Sun.COM 	}
30329425SEric.Schrock@Sun.COM 
303310921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
30349425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
303510100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
30369790SLin.Ling@Sun.COM 
30379790SLin.Ling@Sun.COM 	/*
30389790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
30399790SLin.Ling@Sun.COM 	 */
30409790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30419790SLin.Ling@Sun.COM 	    &nvtop) == 0);
30429790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30439790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
30449790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
30459790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30469790SLin.Ling@Sun.COM 	if (error) {
30479790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
30489790SLin.Ling@Sun.COM 		nvlist_free(config);
30499790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
30509790SLin.Ling@Sun.COM 		    pname);
30519790SLin.Ling@Sun.COM 		return (error);
30529790SLin.Ling@Sun.COM 	}
30539790SLin.Ling@Sun.COM 
30549790SLin.Ling@Sun.COM 	/*
30559790SLin.Ling@Sun.COM 	 * Get the boot vdev.
30569790SLin.Ling@Sun.COM 	 */
30579790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
30589790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
30599790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
30609790SLin.Ling@Sun.COM 		error = ENOENT;
30619790SLin.Ling@Sun.COM 		goto out;
30629790SLin.Ling@Sun.COM 	}
30639790SLin.Ling@Sun.COM 
30649790SLin.Ling@Sun.COM 	/*
30659790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
30669790SLin.Ling@Sun.COM 	 */
30679790SLin.Ling@Sun.COM 	avd = bvd;
30689790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
30699790SLin.Ling@Sun.COM 	if (avd != bvd) {
30709790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
30719790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
30729790SLin.Ling@Sun.COM 		error = EINVAL;
30739790SLin.Ling@Sun.COM 		goto out;
30749790SLin.Ling@Sun.COM 	}
30759790SLin.Ling@Sun.COM 
30769790SLin.Ling@Sun.COM 	/*
30779790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
30789790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
30799790SLin.Ling@Sun.COM 	 */
30809790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30819790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
30829790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
30839790SLin.Ling@Sun.COM 		    "try booting from '%s'",
30849790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
30859790SLin.Ling@Sun.COM 		error = EINVAL;
30869790SLin.Ling@Sun.COM 		goto out;
30879790SLin.Ling@Sun.COM 	}
30889790SLin.Ling@Sun.COM 
30899790SLin.Ling@Sun.COM 	error = 0;
30909946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
30919790SLin.Ling@Sun.COM out:
30929790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30939790SLin.Ling@Sun.COM 	vdev_free(rvd);
30949790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30959425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
30966423Sgw25295 
30979790SLin.Ling@Sun.COM 	nvlist_free(config);
30986423Sgw25295 	return (error);
30996423Sgw25295 }
31009790SLin.Ling@Sun.COM 
31016423Sgw25295 #endif
31026423Sgw25295 
31036423Sgw25295 /*
31049425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
31059425SEric.Schrock@Sun.COM  * boot.
31069425SEric.Schrock@Sun.COM  */
31079425SEric.Schrock@Sun.COM int
31089425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
31099425SEric.Schrock@Sun.COM {
31109425SEric.Schrock@Sun.COM 	spa_t *spa;
31119425SEric.Schrock@Sun.COM 	char *altroot = NULL;
31129425SEric.Schrock@Sun.COM 
31139425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
31149425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
31159425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31169425SEric.Schrock@Sun.COM 		return (EEXIST);
31179425SEric.Schrock@Sun.COM 	}
31189425SEric.Schrock@Sun.COM 
31199425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31209425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
312110921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
312210921STim.Haley@Sun.COM 
312310100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
312410000SVictor.Latushkin@Sun.COM 
31259425SEric.Schrock@Sun.COM 	if (props != NULL)
31269425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31279425SEric.Schrock@Sun.COM 
31289425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
31299425SEric.Schrock@Sun.COM 
31309425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31319946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31329425SEric.Schrock@Sun.COM 
31339425SEric.Schrock@Sun.COM 	return (0);
31349425SEric.Schrock@Sun.COM }
31359425SEric.Schrock@Sun.COM 
31369425SEric.Schrock@Sun.COM /*
31376423Sgw25295  * Import a non-root pool into the system.
31386423Sgw25295  */
31396423Sgw25295 int
31406423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
31416423Sgw25295 {
31429425SEric.Schrock@Sun.COM 	spa_t *spa;
31439425SEric.Schrock@Sun.COM 	char *altroot = NULL;
314410921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
314510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
31469425SEric.Schrock@Sun.COM 	int error;
31479425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
31489425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
31499425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
31509425SEric.Schrock@Sun.COM 
31519425SEric.Schrock@Sun.COM 	/*
31529425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
31539425SEric.Schrock@Sun.COM 	 */
31549425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
315511422SMark.Musante@Sun.COM 	if (spa_lookup(pool) != NULL) {
31569425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31579425SEric.Schrock@Sun.COM 		return (EEXIST);
31589425SEric.Schrock@Sun.COM 	}
31599425SEric.Schrock@Sun.COM 
316010921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
316110921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
316210921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
316310921STim.Haley@Sun.COM 
31649425SEric.Schrock@Sun.COM 	/*
31659425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
31669425SEric.Schrock@Sun.COM 	 */
31679425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31689425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
316910921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
31709425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
31719425SEric.Schrock@Sun.COM 
31729425SEric.Schrock@Sun.COM 	/*
31739630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
31749630SJeff.Bonwick@Sun.COM 	 */
31759630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
31769630SJeff.Bonwick@Sun.COM 
31779630SJeff.Bonwick@Sun.COM 	/*
31789425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
31799425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
31809425SEric.Schrock@Sun.COM 	 * doing an import.
31819425SEric.Schrock@Sun.COM 	 */
318210921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
318310921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
318410921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
318511727SVictor.Latushkin@Sun.COM 	    policy.zrp_request);
318610921STim.Haley@Sun.COM 
318710921STim.Haley@Sun.COM 	/*
318810921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
318910921STim.Haley@Sun.COM 	 * back to caller
319010921STim.Haley@Sun.COM 	 */
319110921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
31929425SEric.Schrock@Sun.COM 
31939425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31949425SEric.Schrock@Sun.COM 	/*
31959425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
31969425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
31979425SEric.Schrock@Sun.COM 	 */
31989425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
31999425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
32009425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
32019425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32029425SEric.Schrock@Sun.COM 	}
32039425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
32049425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
32059425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
32069425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32079425SEric.Schrock@Sun.COM 	}
32089425SEric.Schrock@Sun.COM 
32099425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
32109425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
32119425SEric.Schrock@Sun.COM 	if (error == 0)
32129425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32139425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
32149425SEric.Schrock@Sun.COM 	if (error == 0)
32159425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32169425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
32179425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
32189425SEric.Schrock@Sun.COM 
32199425SEric.Schrock@Sun.COM 	if (props != NULL)
32209425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
32219425SEric.Schrock@Sun.COM 
32229425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
32239425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
32249425SEric.Schrock@Sun.COM 		spa_unload(spa);
32259425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
32269425SEric.Schrock@Sun.COM 		spa_remove(spa);
32279425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
32289425SEric.Schrock@Sun.COM 		return (error);
32299425SEric.Schrock@Sun.COM 	}
32309425SEric.Schrock@Sun.COM 
32319425SEric.Schrock@Sun.COM 	/*
32329425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
32339425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
32349425SEric.Schrock@Sun.COM 	 */
32359425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32369425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
32379425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
32389425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
32399425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
32409425SEric.Schrock@Sun.COM 		else
32419425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
32429425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32439425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
32449425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
32459425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32469425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32479425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32489425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
32499425SEric.Schrock@Sun.COM 	}
32509425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
32519425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
32529425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
32539425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
32549425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
32559425SEric.Schrock@Sun.COM 		else
32569425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
32579425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32589425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
32599425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
32609425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32619425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32629425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32639425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
32649425SEric.Schrock@Sun.COM 	}
32659425SEric.Schrock@Sun.COM 
326610672SEric.Schrock@Sun.COM 	/*
326710672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
326810672SEric.Schrock@Sun.COM 	 */
326910672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
327010672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
327110672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
327210672SEric.Schrock@Sun.COM 	}
327310672SEric.Schrock@Sun.COM 
32749425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
32759425SEric.Schrock@Sun.COM 		/*
32769425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
32779425SEric.Schrock@Sun.COM 		 */
327810100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32799425SEric.Schrock@Sun.COM 	}
32809425SEric.Schrock@Sun.COM 
328112296SLin.Ling@Sun.COM 	spa_async_resume(spa);
328212296SLin.Ling@Sun.COM 
32839816SGeorge.Wilson@Sun.COM 	/*
32849816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
32859816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
32869816SGeorge.Wilson@Sun.COM 	 */
32879816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
32889816SGeorge.Wilson@Sun.COM 
32899425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
32909946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
32919425SEric.Schrock@Sun.COM 
32929425SEric.Schrock@Sun.COM 	return (0);
32936643Seschrock }
32946643Seschrock 
3295789Sahrens nvlist_t *
3296789Sahrens spa_tryimport(nvlist_t *tryconfig)
3297789Sahrens {
3298789Sahrens 	nvlist_t *config = NULL;
3299789Sahrens 	char *poolname;
3300789Sahrens 	spa_t *spa;
3301789Sahrens 	uint64_t state;
33028680SLin.Ling@Sun.COM 	int error;
3303789Sahrens 
3304789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3305789Sahrens 		return (NULL);
3306789Sahrens 
3307789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3308789Sahrens 		return (NULL);
3309789Sahrens 
33101635Sbonwick 	/*
33111635Sbonwick 	 * Create and initialize the spa structure.
33121635Sbonwick 	 */
3313789Sahrens 	mutex_enter(&spa_namespace_lock);
331410921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
33158241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3316789Sahrens 
3317789Sahrens 	/*
33181635Sbonwick 	 * Pass off the heavy lifting to spa_load().
33191732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
33201732Sbonwick 	 * is actually the one to trust when doing an import.
3321789Sahrens 	 */
332211422SMark.Musante@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3323789Sahrens 
3324789Sahrens 	/*
3325789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3326789Sahrens 	 */
3327789Sahrens 	if (spa->spa_root_vdev != NULL) {
3328789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3329789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3330789Sahrens 		    poolname) == 0);
3331789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3332789Sahrens 		    state) == 0);
33333975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
33343975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
33352082Seschrock 
33362082Seschrock 		/*
33376423Sgw25295 		 * If the bootfs property exists on this pool then we
33386423Sgw25295 		 * copy it out so that external consumers can tell which
33396423Sgw25295 		 * pools are bootable.
33406423Sgw25295 		 */
33418680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
33426423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33436423Sgw25295 
33446423Sgw25295 			/*
33456423Sgw25295 			 * We have to play games with the name since the
33466423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
33476423Sgw25295 			 */
33487754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
33496423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
33506423Sgw25295 				char *cp;
33516423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33526423Sgw25295 
33536423Sgw25295 				cp = strchr(tmpname, '/');
33546423Sgw25295 				if (cp == NULL) {
33556423Sgw25295 					(void) strlcpy(dsname, tmpname,
33566423Sgw25295 					    MAXPATHLEN);
33576423Sgw25295 				} else {
33586423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
33596423Sgw25295 					    "%s/%s", poolname, ++cp);
33606423Sgw25295 				}
33616423Sgw25295 				VERIFY(nvlist_add_string(config,
33626423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
33636423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
33646423Sgw25295 			}
33656423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
33666423Sgw25295 		}
33676423Sgw25295 
33686423Sgw25295 		/*
33695450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
33702082Seschrock 		 */
33719425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
33722082Seschrock 		spa_add_spares(spa, config);
33735450Sbrendan 		spa_add_l2cache(spa, config);
33749425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3375789Sahrens 	}
3376789Sahrens 
3377789Sahrens 	spa_unload(spa);
3378789Sahrens 	spa_deactivate(spa);
3379789Sahrens 	spa_remove(spa);
3380789Sahrens 	mutex_exit(&spa_namespace_lock);
3381789Sahrens 
3382789Sahrens 	return (config);
3383789Sahrens }
3384789Sahrens 
3385789Sahrens /*
3386789Sahrens  * Pool export/destroy
3387789Sahrens  *
3388789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3389789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3390789Sahrens  * update the pool state and sync all the labels to disk, removing the
33918211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
33928211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3393789Sahrens  */
3394789Sahrens static int
33957214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
33968211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3397789Sahrens {
3398789Sahrens 	spa_t *spa;
3399789Sahrens 
34001775Sbillm 	if (oldconfig)
34011775Sbillm 		*oldconfig = NULL;
34021775Sbillm 
34038241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3404789Sahrens 		return (EROFS);
3405789Sahrens 
3406789Sahrens 	mutex_enter(&spa_namespace_lock);
3407789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3408789Sahrens 		mutex_exit(&spa_namespace_lock);
3409789Sahrens 		return (ENOENT);
3410789Sahrens 	}
3411789Sahrens 
3412789Sahrens 	/*
34131544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
34141544Seschrock 	 * reacquire the namespace lock, and see if we can export.
34151544Seschrock 	 */
34161544Seschrock 	spa_open_ref(spa, FTAG);
34171544Seschrock 	mutex_exit(&spa_namespace_lock);
34181544Seschrock 	spa_async_suspend(spa);
34191544Seschrock 	mutex_enter(&spa_namespace_lock);
34201544Seschrock 	spa_close(spa, FTAG);
34211544Seschrock 
34221544Seschrock 	/*
3423789Sahrens 	 * The pool will be in core if it's openable,
3424789Sahrens 	 * in which case we can modify its state.
3425789Sahrens 	 */
3426789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3427789Sahrens 		/*
3428789Sahrens 		 * Objsets may be open only because they're dirty, so we
3429789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3430789Sahrens 		 */
3431789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3432789Sahrens 
34331544Seschrock 		/*
34341544Seschrock 		 * A pool cannot be exported or destroyed if there are active
34351544Seschrock 		 * references.  If we are resetting a pool, allow references by
34361544Seschrock 		 * fault injection handlers.
34371544Seschrock 		 */
34381544Seschrock 		if (!spa_refcount_zero(spa) ||
34391544Seschrock 		    (spa->spa_inject_ref != 0 &&
34401544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
34411544Seschrock 			spa_async_resume(spa);
3442789Sahrens 			mutex_exit(&spa_namespace_lock);
3443789Sahrens 			return (EBUSY);
3444789Sahrens 		}
3445789Sahrens 
3446789Sahrens 		/*
34477214Slling 		 * A pool cannot be exported if it has an active shared spare.
34487214Slling 		 * This is to prevent other pools stealing the active spare
34497214Slling 		 * from an exported pool. At user's own will, such pool can
34507214Slling 		 * be forcedly exported.
34517214Slling 		 */
34527214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
34537214Slling 		    spa_has_active_shared_spare(spa)) {
34547214Slling 			spa_async_resume(spa);
34557214Slling 			mutex_exit(&spa_namespace_lock);
34567214Slling 			return (EXDEV);
34577214Slling 		}
34587214Slling 
34597214Slling 		/*
3460789Sahrens 		 * We want this to be reflected on every label,
3461789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3462789Sahrens 		 * final sync that pushes these changes out.
3463789Sahrens 		 */
34648211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
34657754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34661544Seschrock 			spa->spa_state = new_state;
346712296SLin.Ling@Sun.COM 			spa->spa_final_txg = spa_last_synced_txg(spa) +
346812296SLin.Ling@Sun.COM 			    TXG_DEFER_SIZE + 1;
34691544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
34707754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
34711544Seschrock 		}
3472789Sahrens 	}
3473789Sahrens 
34744451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
34754451Seschrock 
3476789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3477789Sahrens 		spa_unload(spa);
3478789Sahrens 		spa_deactivate(spa);
3479789Sahrens 	}
3480789Sahrens 
34811775Sbillm 	if (oldconfig && spa->spa_config)
34821775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
34831775Sbillm 
34841544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
34858211SGeorge.Wilson@Sun.COM 		if (!hardforce)
34868211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
34871544Seschrock 		spa_remove(spa);
34881544Seschrock 	}
3489789Sahrens 	mutex_exit(&spa_namespace_lock);
3490789Sahrens 
3491789Sahrens 	return (0);
3492789Sahrens }
3493789Sahrens 
3494789Sahrens /*
3495789Sahrens  * Destroy a storage pool.
3496789Sahrens  */
3497789Sahrens int
3498789Sahrens spa_destroy(char *pool)
3499789Sahrens {
35008211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
35018211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3502789Sahrens }
3503789Sahrens 
3504789Sahrens /*
3505789Sahrens  * Export a storage pool.
3506789Sahrens  */
3507789Sahrens int
35088211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
35098211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3510789Sahrens {
35118211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
35128211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3513789Sahrens }
3514789Sahrens 
3515789Sahrens /*
35161544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
35171544Seschrock  * from the namespace in any way.
35181544Seschrock  */
35191544Seschrock int
35201544Seschrock spa_reset(char *pool)
35211544Seschrock {
35227214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
35238211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
35241544Seschrock }
35251544Seschrock 
35261544Seschrock /*
3527789Sahrens  * ==========================================================================
3528789Sahrens  * Device manipulation
3529789Sahrens  * ==========================================================================
3530789Sahrens  */
3531789Sahrens 
3532789Sahrens /*
35334527Sperrin  * Add a device to a storage pool.
3534789Sahrens  */
3535789Sahrens int
3536789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3537789Sahrens {
353810594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
35398241SJeff.Bonwick@Sun.COM 	int error;
3540789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
35411585Sbonwick 	vdev_t *vd, *tvd;
35425450Sbrendan 	nvlist_t **spares, **l2cache;
35435450Sbrendan 	uint_t nspares, nl2cache;
3544789Sahrens 
3545789Sahrens 	txg = spa_vdev_enter(spa);
3546789Sahrens 
35472082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
35482082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
35492082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
35502082Seschrock 
35517754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3552789Sahrens 
35535450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
35545450Sbrendan 	    &nspares) != 0)
35552082Seschrock 		nspares = 0;
35562082Seschrock 
35575450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
35585450Sbrendan 	    &nl2cache) != 0)
35595450Sbrendan 		nl2cache = 0;
35605450Sbrendan 
35617754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
35622082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
35637754SJeff.Bonwick@Sun.COM 
35647754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
35657754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
35667754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
35672082Seschrock 
35683377Seschrock 	/*
35695450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
35705450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
35713377Seschrock 	 */
35727754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
35733377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
35743377Seschrock 
35753377Seschrock 	/*
35763377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
35773377Seschrock 	 */
35788241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
357910594SGeorge.Wilson@Sun.COM 
358010594SGeorge.Wilson@Sun.COM 		/*
358110594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
358210594SGeorge.Wilson@Sun.COM 		 */
358310594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
358410594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
358510594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
358610594SGeorge.Wilson@Sun.COM 				break;
358710594SGeorge.Wilson@Sun.COM 			}
358810594SGeorge.Wilson@Sun.COM 		}
35893377Seschrock 		tvd = vd->vdev_child[c];
35903377Seschrock 		vdev_remove_child(vd, tvd);
359110594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
35923377Seschrock 		vdev_add_child(rvd, tvd);
35933377Seschrock 		vdev_config_dirty(tvd);
35943377Seschrock 	}
35953377Seschrock 
35962082Seschrock 	if (nspares != 0) {
35975450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
35985450Sbrendan 		    ZPOOL_CONFIG_SPARES);
35992082Seschrock 		spa_load_spares(spa);
36005450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
36015450Sbrendan 	}
36025450Sbrendan 
36035450Sbrendan 	if (nl2cache != 0) {
36045450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
36055450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
36065450Sbrendan 		spa_load_l2cache(spa);
36075450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3608789Sahrens 	}
3609789Sahrens 
3610789Sahrens 	/*
36111585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
36121585Sbonwick 	 * If other threads start allocating from these vdevs before we
36131585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
36141585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
36151585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
36161585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
36171635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
36181585Sbonwick 	 *
36191585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
36201585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
36211585Sbonwick 	 * steps will be completed the next time we load the pool.
3622789Sahrens 	 */
36231635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
36241585Sbonwick 
36251635Sbonwick 	mutex_enter(&spa_namespace_lock);
36261635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36271635Sbonwick 	mutex_exit(&spa_namespace_lock);
3628789Sahrens 
36291635Sbonwick 	return (0);
3630789Sahrens }
3631789Sahrens 
3632789Sahrens /*
3633789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3634789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3635789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3636789Sahrens  *
3637789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3638789Sahrens  * existing device; in this case the two devices are made into their own
36394451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3640789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3641789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3642789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3643789Sahrens  * is automatically detached.
3644789Sahrens  */
3645789Sahrens int
36461544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3647789Sahrens {
364812296SLin.Ling@Sun.COM 	uint64_t txg, dtl_max_txg;
3649789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3650789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
36512082Seschrock 	vdev_ops_t *pvops;
36527313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
36537313SEric.Kustarz@Sun.COM 	int newvd_isspare;
36547313SEric.Kustarz@Sun.COM 	int error;
3655789Sahrens 
3656789Sahrens 	txg = spa_vdev_enter(spa);
3657789Sahrens 
36586643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3659789Sahrens 
3660789Sahrens 	if (oldvd == NULL)
3661789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3662789Sahrens 
36631585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
36641585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
36651585Sbonwick 
3666789Sahrens 	pvd = oldvd->vdev_parent;
3667789Sahrens 
36682082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
36694451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
36704451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
36714451Seschrock 
36724451Seschrock 	if (newrootvd->vdev_children != 1)
3673789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3674789Sahrens 
3675789Sahrens 	newvd = newrootvd->vdev_child[0];
3676789Sahrens 
3677789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3678789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3679789Sahrens 
36802082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3681789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3682789Sahrens 
36834527Sperrin 	/*
36844527Sperrin 	 * Spares can't replace logs
36854527Sperrin 	 */
36867326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
36874527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36884527Sperrin 
36892082Seschrock 	if (!replacing) {
36902082Seschrock 		/*
36912082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
36922082Seschrock 		 * vdev.
36932082Seschrock 		 */
36942082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
36952082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
36962082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36972082Seschrock 
36982082Seschrock 		pvops = &vdev_mirror_ops;
36992082Seschrock 	} else {
37002082Seschrock 		/*
37012082Seschrock 		 * Active hot spares can only be replaced by inactive hot
37022082Seschrock 		 * spares.
37032082Seschrock 		 */
37042082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
37052082Seschrock 		    pvd->vdev_child[1] == oldvd &&
37062082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
37072082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37082082Seschrock 
37092082Seschrock 		/*
37102082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
37112082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
37123377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
37133377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
37143377Seschrock 		 * the same (spare replaces spare, non-spare replaces
37153377Seschrock 		 * non-spare).
37162082Seschrock 		 */
37172082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
37182082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37193377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
37203377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
37213377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37222082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
37232082Seschrock 		    newvd->vdev_isspare)
37242082Seschrock 			pvops = &vdev_spare_ops;
37252082Seschrock 		else
37262082Seschrock 			pvops = &vdev_replacing_ops;
37272082Seschrock 	}
37282082Seschrock 
37291175Slling 	/*
37309816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
37311175Slling 	 */
37329816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3733789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3734789Sahrens 
37351732Sbonwick 	/*
37361732Sbonwick 	 * The new device cannot have a higher alignment requirement
37371732Sbonwick 	 * than the top-level vdev.
37381732Sbonwick 	 */
37391732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3740789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3741789Sahrens 
3742789Sahrens 	/*
3743789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3744789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3745789Sahrens 	 */
3746789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3747789Sahrens 		spa_strfree(oldvd->vdev_path);
3748789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3749789Sahrens 		    KM_SLEEP);
3750789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3751789Sahrens 		    newvd->vdev_path, "old");
3752789Sahrens 		if (oldvd->vdev_devid != NULL) {
3753789Sahrens 			spa_strfree(oldvd->vdev_devid);
3754789Sahrens 			oldvd->vdev_devid = NULL;
3755789Sahrens 		}
3756789Sahrens 	}
3757789Sahrens 
3758789Sahrens 	/*
37592082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
37602082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3761789Sahrens 	 */
3762789Sahrens 	if (pvd->vdev_ops != pvops)
3763789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3764789Sahrens 
3765789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3766789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3767789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3768789Sahrens 
3769789Sahrens 	/*
3770789Sahrens 	 * Extract the new device from its root and add it to pvd.
3771789Sahrens 	 */
3772789Sahrens 	vdev_remove_child(newrootvd, newvd);
3773789Sahrens 	newvd->vdev_id = pvd->vdev_children;
377410594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3775789Sahrens 	vdev_add_child(pvd, newvd);
3776789Sahrens 
3777789Sahrens 	tvd = newvd->vdev_top;
3778789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3779789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3780789Sahrens 
3781789Sahrens 	vdev_config_dirty(tvd);
3782789Sahrens 
3783789Sahrens 	/*
378412296SLin.Ling@Sun.COM 	 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
378512296SLin.Ling@Sun.COM 	 * for any dmu_sync-ed blocks.  It will propagate upward when
378612296SLin.Ling@Sun.COM 	 * spa_vdev_exit() calls vdev_dtl_reassess().
3787789Sahrens 	 */
378812296SLin.Ling@Sun.COM 	dtl_max_txg = txg + TXG_CONCURRENT_STATES;
378912296SLin.Ling@Sun.COM 
379012296SLin.Ling@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
379112296SLin.Ling@Sun.COM 	    dtl_max_txg - TXG_INITIAL);
3792789Sahrens 
37939425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
37943377Seschrock 		spa_spare_activate(newvd);
37959425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
37969425SEric.Schrock@Sun.COM 	}
37979425SEric.Schrock@Sun.COM 
37987754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
37997754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
38007313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
38011544Seschrock 
3802789Sahrens 	/*
3803789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3804789Sahrens 	 */
38051732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3806789Sahrens 
380712296SLin.Ling@Sun.COM 	/*
380812296SLin.Ling@Sun.COM 	 * Restart the resilver
380912296SLin.Ling@Sun.COM 	 */
381012296SLin.Ling@Sun.COM 	dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
381112296SLin.Ling@Sun.COM 
381212296SLin.Ling@Sun.COM 	/*
381312296SLin.Ling@Sun.COM 	 * Commit the config
381412296SLin.Ling@Sun.COM 	 */
381512296SLin.Ling@Sun.COM 	(void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
381612296SLin.Ling@Sun.COM 
381712296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
381812296SLin.Ling@Sun.COM 	    "%s vdev=%s %s vdev=%s",
38199946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
38209946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
38219946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
38227313SEric.Kustarz@Sun.COM 
38237313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
38247313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
38257313SEric.Kustarz@Sun.COM 
3826789Sahrens 	return (0);
3827789Sahrens }
3828789Sahrens 
3829789Sahrens /*
3830789Sahrens  * Detach a device from a mirror or replacing vdev.
3831789Sahrens  * If 'replace_done' is specified, only detach if the parent
3832789Sahrens  * is a replacing vdev.
3833789Sahrens  */
3834789Sahrens int
38358241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3836789Sahrens {
3837789Sahrens 	uint64_t txg;
38388241SJeff.Bonwick@Sun.COM 	int error;
3839789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3840789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
38412082Seschrock 	boolean_t unspare = B_FALSE;
38422082Seschrock 	uint64_t unspare_guid;
38436673Seschrock 	size_t len;
384411422SMark.Musante@Sun.COM 	char *vdpath;
3845789Sahrens 
3846789Sahrens 	txg = spa_vdev_enter(spa);
3847789Sahrens 
38486643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3849789Sahrens 
3850789Sahrens 	if (vd == NULL)
3851789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3852789Sahrens 
38531585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
38541585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38551585Sbonwick 
3856789Sahrens 	pvd = vd->vdev_parent;
3857789Sahrens 
3858789Sahrens 	/*
38598241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
38608241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
38618241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
38628241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
38638241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
38648241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
38658241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
38668241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
38678241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
38688241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
38698241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
38708241SJeff.Bonwick@Sun.COM 	 */
38718241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
38728241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
38738241SJeff.Bonwick@Sun.COM 
38748241SJeff.Bonwick@Sun.COM 	/*
3875789Sahrens 	 * If replace_done is specified, only remove this device if it's
38762082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
38772082Seschrock 	 * disk can be removed.
3878789Sahrens 	 */
38792082Seschrock 	if (replace_done) {
38802082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
38812082Seschrock 			if (vd->vdev_id != 0)
38822082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38832082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
38842082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38852082Seschrock 		}
38862082Seschrock 	}
38872082Seschrock 
38882082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
38894577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3890789Sahrens 
3891789Sahrens 	/*
38922082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3893789Sahrens 	 */
3894789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
38952082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
38962082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3897789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3898789Sahrens 
3899789Sahrens 	/*
39008241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
39018241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3902789Sahrens 	 */
39038241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3904789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3905789Sahrens 
39068241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
39078241SJeff.Bonwick@Sun.COM 
3908789Sahrens 	/*
39096673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
39106673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
39116673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
39126673Seschrock 	 */
39136673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
39146673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
39156673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
39166673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
39176673Seschrock 		cvd = pvd->vdev_child[0];
39186673Seschrock 		len = strlen(vd->vdev_path);
39196673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
39206673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
39216673Seschrock 			spa_strfree(cvd->vdev_path);
39226673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
39236673Seschrock 		}
39246673Seschrock 	}
39256673Seschrock 
39266673Seschrock 	/*
39272082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
39282082Seschrock 	 * that the spare should become a real disk, and be removed from the
39292082Seschrock 	 * active spare list for the pool.
39302082Seschrock 	 */
39312082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
39328241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
39332082Seschrock 		unspare = B_TRUE;
39342082Seschrock 
39352082Seschrock 	/*
3936789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3937789Sahrens 	 * This must be done after all other error cases are handled,
3938789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3939789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3940789Sahrens 	 * it may be that the unwritability of the disk is the reason
3941789Sahrens 	 * it's being detached!
3942789Sahrens 	 */
39433377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3944789Sahrens 
3945789Sahrens 	/*
3946789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3947789Sahrens 	 */
3948789Sahrens 	vdev_remove_child(pvd, vd);
3949789Sahrens 	vdev_compact_children(pvd);
3950789Sahrens 
3951789Sahrens 	/*
3952789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3953789Sahrens 	 */
3954789Sahrens 	cvd = pvd->vdev_child[0];
3955789Sahrens 
3956789Sahrens 	/*
39572082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
39588241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
39598241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
39608241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
39618241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
39628241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
39638241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
39642082Seschrock 	 */
39652082Seschrock 	if (unspare) {
39662082Seschrock 		ASSERT(cvd->vdev_isspare);
39673377Seschrock 		spa_spare_remove(cvd);
39682082Seschrock 		unspare_guid = cvd->vdev_guid;
39698241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
39702082Seschrock 	}
39712082Seschrock 
39722082Seschrock 	/*
3973789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3974789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3975789Sahrens 	 */
3976789Sahrens 	if (pvd->vdev_children == 1)
3977789Sahrens 		vdev_remove_parent(cvd);
3978789Sahrens 
3979789Sahrens 	/*
3980789Sahrens 	 * We don't set tvd until now because the parent we just removed
3981789Sahrens 	 * may have been the previous top-level vdev.
3982789Sahrens 	 */
3983789Sahrens 	tvd = cvd->vdev_top;
3984789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3985789Sahrens 
3986789Sahrens 	/*
39873377Seschrock 	 * Reevaluate the parent vdev state.
3988789Sahrens 	 */
39894451Seschrock 	vdev_propagate_state(cvd);
3990789Sahrens 
3991789Sahrens 	/*
39929816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
39939816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
39949816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
39959816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
39969816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3997789Sahrens 	 */
39989816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
39999816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
40009816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
40019816SGeorge.Wilson@Sun.COM 	}
4002789Sahrens 
4003789Sahrens 	vdev_config_dirty(tvd);
4004789Sahrens 
4005789Sahrens 	/*
40063377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
40073377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
40083377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
40093377Seschrock 	 * prevent vd from being accessed after it's freed.
4010789Sahrens 	 */
401111422SMark.Musante@Sun.COM 	vdpath = spa_strdup(vd->vdev_path);
40128241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
4013789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
40141732Sbonwick 	vd->vdev_detached = B_TRUE;
40151732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
4016789Sahrens 
40174451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
40184451Seschrock 
40192082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
40202082Seschrock 
402112296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
402211422SMark.Musante@Sun.COM 	    "vdev=%s", vdpath);
402311422SMark.Musante@Sun.COM 	spa_strfree(vdpath);
402411422SMark.Musante@Sun.COM 
40252082Seschrock 	/*
40263377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
40273377Seschrock 	 * then we want to go through and remove the device from the hot spare
40283377Seschrock 	 * list of every other pool.
40292082Seschrock 	 */
40302082Seschrock 	if (unspare) {
40318241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
40322082Seschrock 		spa = NULL;
40332082Seschrock 		mutex_enter(&spa_namespace_lock);
40342082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
40352082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
40362082Seschrock 				continue;
40378241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
40388241SJeff.Bonwick@Sun.COM 				continue;
40397793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
40407793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
404112296SLin.Ling@Sun.COM 			(void) spa_vdev_remove(spa, unspare_guid,
404212296SLin.Ling@Sun.COM 			    B_TRUE);
40437793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
40447793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
40452082Seschrock 		}
40462082Seschrock 		mutex_exit(&spa_namespace_lock);
40472082Seschrock 	}
40482082Seschrock 
40492082Seschrock 	return (error);
40502082Seschrock }
40512082Seschrock 
405211422SMark.Musante@Sun.COM /*
405311422SMark.Musante@Sun.COM  * Split a set of devices from their mirrors, and create a new pool from them.
405411422SMark.Musante@Sun.COM  */
405511422SMark.Musante@Sun.COM int
405611422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
405711422SMark.Musante@Sun.COM     nvlist_t *props, boolean_t exp)
405811422SMark.Musante@Sun.COM {
405911422SMark.Musante@Sun.COM 	int error = 0;
406011422SMark.Musante@Sun.COM 	uint64_t txg, *glist;
406111422SMark.Musante@Sun.COM 	spa_t *newspa;
406211422SMark.Musante@Sun.COM 	uint_t c, children, lastlog;
406311422SMark.Musante@Sun.COM 	nvlist_t **child, *nvl, *tmp;
406411422SMark.Musante@Sun.COM 	dmu_tx_t *tx;
406511422SMark.Musante@Sun.COM 	char *altroot = NULL;
406611422SMark.Musante@Sun.COM 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
406711422SMark.Musante@Sun.COM 	boolean_t activate_slog;
406811422SMark.Musante@Sun.COM 
406911422SMark.Musante@Sun.COM 	if (!spa_writeable(spa))
407011422SMark.Musante@Sun.COM 		return (EROFS);
407111422SMark.Musante@Sun.COM 
407211422SMark.Musante@Sun.COM 	txg = spa_vdev_enter(spa);
407311422SMark.Musante@Sun.COM 
407411422SMark.Musante@Sun.COM 	/* clear the log and flush everything up to now */
407511422SMark.Musante@Sun.COM 	activate_slog = spa_passivate_log(spa);
407611422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
407711422SMark.Musante@Sun.COM 	error = spa_offline_log(spa);
407811422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
407911422SMark.Musante@Sun.COM 
408011422SMark.Musante@Sun.COM 	if (activate_slog)
408111422SMark.Musante@Sun.COM 		spa_activate_log(spa);
408211422SMark.Musante@Sun.COM 
408311422SMark.Musante@Sun.COM 	if (error != 0)
408411422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
408511422SMark.Musante@Sun.COM 
408611422SMark.Musante@Sun.COM 	/* check new spa name before going any further */
408711422SMark.Musante@Sun.COM 	if (spa_lookup(newname) != NULL)
408811422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
408911422SMark.Musante@Sun.COM 
409011422SMark.Musante@Sun.COM 	/*
409111422SMark.Musante@Sun.COM 	 * scan through all the children to ensure they're all mirrors
409211422SMark.Musante@Sun.COM 	 */
409311422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
409411422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
409511422SMark.Musante@Sun.COM 	    &children) != 0)
409611422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
409711422SMark.Musante@Sun.COM 
409811422SMark.Musante@Sun.COM 	/* first, check to ensure we've got the right child count */
409911422SMark.Musante@Sun.COM 	rvd = spa->spa_root_vdev;
410011422SMark.Musante@Sun.COM 	lastlog = 0;
410111422SMark.Musante@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
410211422SMark.Musante@Sun.COM 		vdev_t *vd = rvd->vdev_child[c];
410311422SMark.Musante@Sun.COM 
410411422SMark.Musante@Sun.COM 		/* don't count the holes & logs as children */
410511422SMark.Musante@Sun.COM 		if (vd->vdev_islog || vd->vdev_ishole) {
410611422SMark.Musante@Sun.COM 			if (lastlog == 0)
410711422SMark.Musante@Sun.COM 				lastlog = c;
410811422SMark.Musante@Sun.COM 			continue;
410911422SMark.Musante@Sun.COM 		}
411011422SMark.Musante@Sun.COM 
411111422SMark.Musante@Sun.COM 		lastlog = 0;
411211422SMark.Musante@Sun.COM 	}
411311422SMark.Musante@Sun.COM 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
411411422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
411511422SMark.Musante@Sun.COM 
411611422SMark.Musante@Sun.COM 	/* next, ensure no spare or cache devices are part of the split */
411711422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
411811422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
411911422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
412011422SMark.Musante@Sun.COM 
412111422SMark.Musante@Sun.COM 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
412211422SMark.Musante@Sun.COM 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
412311422SMark.Musante@Sun.COM 
412411422SMark.Musante@Sun.COM 	/* then, loop over each vdev and validate it */
412511422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
412611422SMark.Musante@Sun.COM 		uint64_t is_hole = 0;
412711422SMark.Musante@Sun.COM 
412811422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
412911422SMark.Musante@Sun.COM 		    &is_hole);
413011422SMark.Musante@Sun.COM 
413111422SMark.Musante@Sun.COM 		if (is_hole != 0) {
413211422SMark.Musante@Sun.COM 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
413311422SMark.Musante@Sun.COM 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
413411422SMark.Musante@Sun.COM 				continue;
413511422SMark.Musante@Sun.COM 			} else {
413611422SMark.Musante@Sun.COM 				error = EINVAL;
413711422SMark.Musante@Sun.COM 				break;
413811422SMark.Musante@Sun.COM 			}
413911422SMark.Musante@Sun.COM 		}
414011422SMark.Musante@Sun.COM 
414111422SMark.Musante@Sun.COM 		/* which disk is going to be split? */
414211422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
414311422SMark.Musante@Sun.COM 		    &glist[c]) != 0) {
414411422SMark.Musante@Sun.COM 			error = EINVAL;
414511422SMark.Musante@Sun.COM 			break;
414611422SMark.Musante@Sun.COM 		}
414711422SMark.Musante@Sun.COM 
414811422SMark.Musante@Sun.COM 		/* look it up in the spa */
414911422SMark.Musante@Sun.COM 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
415011422SMark.Musante@Sun.COM 		if (vml[c] == NULL) {
415111422SMark.Musante@Sun.COM 			error = ENODEV;
415211422SMark.Musante@Sun.COM 			break;
415311422SMark.Musante@Sun.COM 		}
415411422SMark.Musante@Sun.COM 
415511422SMark.Musante@Sun.COM 		/* make sure there's nothing stopping the split */
415611422SMark.Musante@Sun.COM 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
415711422SMark.Musante@Sun.COM 		    vml[c]->vdev_islog ||
415811422SMark.Musante@Sun.COM 		    vml[c]->vdev_ishole ||
415911422SMark.Musante@Sun.COM 		    vml[c]->vdev_isspare ||
416011422SMark.Musante@Sun.COM 		    vml[c]->vdev_isl2cache ||
416111422SMark.Musante@Sun.COM 		    !vdev_writeable(vml[c]) ||
416211497SMark.Musante@Sun.COM 		    vml[c]->vdev_children != 0 ||
416311422SMark.Musante@Sun.COM 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
416411422SMark.Musante@Sun.COM 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
416511422SMark.Musante@Sun.COM 			error = EINVAL;
416611422SMark.Musante@Sun.COM 			break;
416711422SMark.Musante@Sun.COM 		}
416811422SMark.Musante@Sun.COM 
416911422SMark.Musante@Sun.COM 		if (vdev_dtl_required(vml[c])) {
417011422SMark.Musante@Sun.COM 			error = EBUSY;
417111422SMark.Musante@Sun.COM 			break;
417211422SMark.Musante@Sun.COM 		}
417311422SMark.Musante@Sun.COM 
417411422SMark.Musante@Sun.COM 		/* we need certain info from the top level */
417511422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
417611422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_array) == 0);
417711422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
417811422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
417911422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
418011422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_asize) == 0);
418111422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
418211422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ashift) == 0);
418311422SMark.Musante@Sun.COM 	}
418411422SMark.Musante@Sun.COM 
418511422SMark.Musante@Sun.COM 	if (error != 0) {
418611422SMark.Musante@Sun.COM 		kmem_free(vml, children * sizeof (vdev_t *));
418711422SMark.Musante@Sun.COM 		kmem_free(glist, children * sizeof (uint64_t));
418811422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
418911422SMark.Musante@Sun.COM 	}
419011422SMark.Musante@Sun.COM 
419111422SMark.Musante@Sun.COM 	/* stop writers from using the disks */
419211422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
419311422SMark.Musante@Sun.COM 		if (vml[c] != NULL)
419411422SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_TRUE;
419511422SMark.Musante@Sun.COM 	}
419611422SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
419711422SMark.Musante@Sun.COM 
419811422SMark.Musante@Sun.COM 	/*
419911422SMark.Musante@Sun.COM 	 * Temporarily record the splitting vdevs in the spa config.  This
420011422SMark.Musante@Sun.COM 	 * will disappear once the config is regenerated.
420111422SMark.Musante@Sun.COM 	 */
420211422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
420311422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
420411422SMark.Musante@Sun.COM 	    glist, children) == 0);
420511422SMark.Musante@Sun.COM 	kmem_free(glist, children * sizeof (uint64_t));
420611422SMark.Musante@Sun.COM 
420711864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
420811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
420911422SMark.Musante@Sun.COM 	    nvl) == 0);
421011864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
421111422SMark.Musante@Sun.COM 	spa->spa_config_splitting = nvl;
421211422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
421311422SMark.Musante@Sun.COM 
421411422SMark.Musante@Sun.COM 	/* configure and create the new pool */
421511422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
421611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
421711422SMark.Musante@Sun.COM 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
421811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
421911422SMark.Musante@Sun.COM 	    spa_version(spa)) == 0);
422011422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
422111422SMark.Musante@Sun.COM 	    spa->spa_config_txg) == 0);
422211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
422311422SMark.Musante@Sun.COM 	    spa_generate_guid(NULL)) == 0);
422411422SMark.Musante@Sun.COM 	(void) nvlist_lookup_string(props,
422511422SMark.Musante@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
422611422SMark.Musante@Sun.COM 
422711497SMark.Musante@Sun.COM 	/* add the new pool to the namespace */
422811422SMark.Musante@Sun.COM 	newspa = spa_add(newname, config, altroot);
422911422SMark.Musante@Sun.COM 	newspa->spa_config_txg = spa->spa_config_txg;
423011422SMark.Musante@Sun.COM 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
423111422SMark.Musante@Sun.COM 
423211422SMark.Musante@Sun.COM 	/* release the spa config lock, retaining the namespace lock */
423311422SMark.Musante@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
423411422SMark.Musante@Sun.COM 
423511422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
423611422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 1);
423711422SMark.Musante@Sun.COM 
423811422SMark.Musante@Sun.COM 	spa_activate(newspa, spa_mode_global);
423911422SMark.Musante@Sun.COM 	spa_async_suspend(newspa);
424011422SMark.Musante@Sun.COM 
424111422SMark.Musante@Sun.COM 	/* create the new pool from the disks of the original pool */
424211422SMark.Musante@Sun.COM 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
424311422SMark.Musante@Sun.COM 	if (error)
424411422SMark.Musante@Sun.COM 		goto out;
424511422SMark.Musante@Sun.COM 
424611422SMark.Musante@Sun.COM 	/* if that worked, generate a real config for the new pool */
424711422SMark.Musante@Sun.COM 	if (newspa->spa_root_vdev != NULL) {
424811422SMark.Musante@Sun.COM 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
424911422SMark.Musante@Sun.COM 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
425011422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
425111422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
425211422SMark.Musante@Sun.COM 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
425311422SMark.Musante@Sun.COM 		    B_TRUE));
425411422SMark.Musante@Sun.COM 	}
425511422SMark.Musante@Sun.COM 
425611422SMark.Musante@Sun.COM 	/* set the props */
425711422SMark.Musante@Sun.COM 	if (props != NULL) {
425811422SMark.Musante@Sun.COM 		spa_configfile_set(newspa, props, B_FALSE);
425911422SMark.Musante@Sun.COM 		error = spa_prop_set(newspa, props);
426011422SMark.Musante@Sun.COM 		if (error)
426111422SMark.Musante@Sun.COM 			goto out;
426211422SMark.Musante@Sun.COM 	}
426311422SMark.Musante@Sun.COM 
426411422SMark.Musante@Sun.COM 	/* flush everything */
426511422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(newspa);
426611422SMark.Musante@Sun.COM 	vdev_config_dirty(newspa->spa_root_vdev);
426711422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
426811422SMark.Musante@Sun.COM 
426911422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
427011422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 2);
427111422SMark.Musante@Sun.COM 
427211422SMark.Musante@Sun.COM 	spa_async_resume(newspa);
427311422SMark.Musante@Sun.COM 
427411422SMark.Musante@Sun.COM 	/* finally, update the original pool's config */
427511422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
427611422SMark.Musante@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
427711422SMark.Musante@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
427811422SMark.Musante@Sun.COM 	if (error != 0)
427911422SMark.Musante@Sun.COM 		dmu_tx_abort(tx);
428011422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
428111422SMark.Musante@Sun.COM 		if (vml[c] != NULL) {
428211422SMark.Musante@Sun.COM 			vdev_split(vml[c]);
428311422SMark.Musante@Sun.COM 			if (error == 0)
428412296SLin.Ling@Sun.COM 				spa_history_log_internal(LOG_POOL_VDEV_DETACH,
428512296SLin.Ling@Sun.COM 				    spa, tx, "vdev=%s",
428611422SMark.Musante@Sun.COM 				    vml[c]->vdev_path);
428711422SMark.Musante@Sun.COM 			vdev_free(vml[c]);
428811422SMark.Musante@Sun.COM 		}
428911422SMark.Musante@Sun.COM 	}
429011422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
429111422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
429211422SMark.Musante@Sun.COM 	nvlist_free(nvl);
429311422SMark.Musante@Sun.COM 	if (error == 0)
429411422SMark.Musante@Sun.COM 		dmu_tx_commit(tx);
429511422SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, 0);
429611422SMark.Musante@Sun.COM 
429711422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
429811422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 3);
429911422SMark.Musante@Sun.COM 
430011422SMark.Musante@Sun.COM 	/* split is complete; log a history record */
430112296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
430211422SMark.Musante@Sun.COM 	    "split new pool %s from pool %s", newname, spa_name(spa));
430311422SMark.Musante@Sun.COM 
430411422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
430511422SMark.Musante@Sun.COM 
430611422SMark.Musante@Sun.COM 	/* if we're not going to mount the filesystems in userland, export */
430711422SMark.Musante@Sun.COM 	if (exp)
430811422SMark.Musante@Sun.COM 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
430911422SMark.Musante@Sun.COM 		    B_FALSE, B_FALSE);
431011422SMark.Musante@Sun.COM 
431111422SMark.Musante@Sun.COM 	return (error);
431211422SMark.Musante@Sun.COM 
431311422SMark.Musante@Sun.COM out:
431411422SMark.Musante@Sun.COM 	spa_unload(newspa);
431511422SMark.Musante@Sun.COM 	spa_deactivate(newspa);
431611422SMark.Musante@Sun.COM 	spa_remove(newspa);
431711422SMark.Musante@Sun.COM 
431811422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
431911864SMark.Musante@Sun.COM 
432011864SMark.Musante@Sun.COM 	/* re-online all offlined disks */
432111864SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
432211864SMark.Musante@Sun.COM 		if (vml[c] != NULL)
432311864SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_FALSE;
432411864SMark.Musante@Sun.COM 	}
432511864SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
432611864SMark.Musante@Sun.COM 
432711422SMark.Musante@Sun.COM 	nvlist_free(spa->spa_config_splitting);
432811422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
432911497SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, error);
433011422SMark.Musante@Sun.COM 
433111422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
433211422SMark.Musante@Sun.COM 	return (error);
433311422SMark.Musante@Sun.COM }
433411422SMark.Musante@Sun.COM 
43357754SJeff.Bonwick@Sun.COM static nvlist_t *
43367754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
43372082Seschrock {
43387754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
43397754SJeff.Bonwick@Sun.COM 		uint64_t guid;
43407754SJeff.Bonwick@Sun.COM 
43417754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
43427754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
43437754SJeff.Bonwick@Sun.COM 
43447754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
43457754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
43462082Seschrock 	}
43472082Seschrock 
43487754SJeff.Bonwick@Sun.COM 	return (NULL);
43495450Sbrendan }
43505450Sbrendan 
43517754SJeff.Bonwick@Sun.COM static void
43527754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
43537754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
43545450Sbrendan {
43557754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
43567754SJeff.Bonwick@Sun.COM 
43577754SJeff.Bonwick@Sun.COM 	if (count > 1)
43587754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
43597754SJeff.Bonwick@Sun.COM 
43607754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
43617754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
43627754SJeff.Bonwick@Sun.COM 			continue;
43637754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
43645450Sbrendan 	}
43655450Sbrendan 
43667754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
43677754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
43687754SJeff.Bonwick@Sun.COM 
43697754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
43707754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
43717754SJeff.Bonwick@Sun.COM 
43727754SJeff.Bonwick@Sun.COM 	if (count > 1)
43737754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
43745450Sbrendan }
43755450Sbrendan 
43765450Sbrendan /*
437710594SGeorge.Wilson@Sun.COM  * Evacuate the device.
437810594SGeorge.Wilson@Sun.COM  */
437912296SLin.Ling@Sun.COM static int
438010594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
438110594SGeorge.Wilson@Sun.COM {
438212296SLin.Ling@Sun.COM 	uint64_t txg;
438310974SJeff.Bonwick@Sun.COM 	int error = 0;
438410594SGeorge.Wilson@Sun.COM 
438510594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
438610594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
438710922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
438810594SGeorge.Wilson@Sun.COM 
438910594SGeorge.Wilson@Sun.COM 	/*
439010594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
439110594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
439210594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
439310594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
439410594SGeorge.Wilson@Sun.COM 	 */
439510594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
439612296SLin.Ling@Sun.COM 		if (vd->vdev_stat.vs_alloc != 0)
439712296SLin.Ling@Sun.COM 			error = spa_offline_log(spa);
439810974SJeff.Bonwick@Sun.COM 	} else {
439912296SLin.Ling@Sun.COM 		error = ENOTSUP;
440010594SGeorge.Wilson@Sun.COM 	}
440110594SGeorge.Wilson@Sun.COM 
440210974SJeff.Bonwick@Sun.COM 	if (error)
440310974SJeff.Bonwick@Sun.COM 		return (error);
440410974SJeff.Bonwick@Sun.COM 
440510594SGeorge.Wilson@Sun.COM 	/*
440610974SJeff.Bonwick@Sun.COM 	 * The evacuation succeeded.  Remove any remaining MOS metadata
440710974SJeff.Bonwick@Sun.COM 	 * associated with this vdev, and wait for these changes to sync.
440810594SGeorge.Wilson@Sun.COM 	 */
440912296SLin.Ling@Sun.COM 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
441010594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
441110594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
441210594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
441310594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
441410594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
441510594SGeorge.Wilson@Sun.COM 
441610594SGeorge.Wilson@Sun.COM 	return (0);
441710594SGeorge.Wilson@Sun.COM }
441810594SGeorge.Wilson@Sun.COM 
441910594SGeorge.Wilson@Sun.COM /*
442010594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
442110594SGeorge.Wilson@Sun.COM  */
442212296SLin.Ling@Sun.COM static void
442310974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
442410594SGeorge.Wilson@Sun.COM {
442510594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
442610594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
442710594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
442810594SGeorge.Wilson@Sun.COM 
442910594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
443010594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
443110922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
443210594SGeorge.Wilson@Sun.COM 
443312296SLin.Ling@Sun.COM 	/*
443412296SLin.Ling@Sun.COM 	 * Only remove any devices which are empty.
443512296SLin.Ling@Sun.COM 	 */
443612296SLin.Ling@Sun.COM 	if (vd->vdev_stat.vs_alloc != 0)
443712296SLin.Ling@Sun.COM 		return;
443812296SLin.Ling@Sun.COM 
443910594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
444010922SJeff.Bonwick@Sun.COM 
444110922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
444210922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
444310922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
444410922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
444510922SJeff.Bonwick@Sun.COM 
444610594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
444710594SGeorge.Wilson@Sun.COM 
444810594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
444910594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
445010594SGeorge.Wilson@Sun.COM 	} else {
445110594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
445210594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
445310594SGeorge.Wilson@Sun.COM 	}
445412352SLin.Ling@Sun.COM 	vdev_config_dirty(rvd);
445512352SLin.Ling@Sun.COM 
445612352SLin.Ling@Sun.COM 	/*
445712352SLin.Ling@Sun.COM 	 * Reassess the health of our root vdev.
445812352SLin.Ling@Sun.COM 	 */
445912352SLin.Ling@Sun.COM 	vdev_reopen(rvd);
446010594SGeorge.Wilson@Sun.COM }
446110594SGeorge.Wilson@Sun.COM 
446210594SGeorge.Wilson@Sun.COM /*
446312296SLin.Ling@Sun.COM  * Remove a device from the pool -
446412296SLin.Ling@Sun.COM  *
446512296SLin.Ling@Sun.COM  * Removing a device from the vdev namespace requires several steps
446612296SLin.Ling@Sun.COM  * and can take a significant amount of time.  As a result we use
446712296SLin.Ling@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
446812296SLin.Ling@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
446912296SLin.Ling@Sun.COM  * lock.  During each step the configuration is synced out.
447012296SLin.Ling@Sun.COM  */
447112296SLin.Ling@Sun.COM 
447212296SLin.Ling@Sun.COM /*
44735450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
447410594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
44755450Sbrendan  */
44765450Sbrendan int
44775450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
44785450Sbrendan {
44795450Sbrendan 	vdev_t *vd;
448010974SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
44817754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
448210594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
44835450Sbrendan 	uint_t nspares, nl2cache;
44845450Sbrendan 	int error = 0;
44858241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
44868241SJeff.Bonwick@Sun.COM 
44878241SJeff.Bonwick@Sun.COM 	if (!locked)
44888241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
44895450Sbrendan 
44906643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
44915450Sbrendan 
44925450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
44935450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
44947754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
44957754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
44967754SJeff.Bonwick@Sun.COM 		/*
44977754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
44987754SJeff.Bonwick@Sun.COM 		 * in this pool.
44997754SJeff.Bonwick@Sun.COM 		 */
45007754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
45017754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
45027754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
45037754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
45047754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
45057754SJeff.Bonwick@Sun.COM 		} else {
45067754SJeff.Bonwick@Sun.COM 			error = EBUSY;
45077754SJeff.Bonwick@Sun.COM 		}
45087754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
45095450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
45107754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
45117754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
45127754SJeff.Bonwick@Sun.COM 		/*
45137754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
45147754SJeff.Bonwick@Sun.COM 		 */
45157754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
45167754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
45175450Sbrendan 		spa_load_l2cache(spa);
45185450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
451910594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
452010594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
452110922SJeff.Bonwick@Sun.COM 		ASSERT(vd == vd->vdev_top);
452210594SGeorge.Wilson@Sun.COM 
452310594SGeorge.Wilson@Sun.COM 		/*
452410594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
452510594SGeorge.Wilson@Sun.COM 		 * become the common case.
452610594SGeorge.Wilson@Sun.COM 		 */
452710594SGeorge.Wilson@Sun.COM 
452810974SJeff.Bonwick@Sun.COM 		mg = vd->vdev_mg;
452910974SJeff.Bonwick@Sun.COM 
453010594SGeorge.Wilson@Sun.COM 		/*
453110974SJeff.Bonwick@Sun.COM 		 * Stop allocating from this vdev.
453210594SGeorge.Wilson@Sun.COM 		 */
453310974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(mg);
453410594SGeorge.Wilson@Sun.COM 
453510922SJeff.Bonwick@Sun.COM 		/*
453610922SJeff.Bonwick@Sun.COM 		 * Wait for the youngest allocations and frees to sync,
453710922SJeff.Bonwick@Sun.COM 		 * and then wait for the deferral of those frees to finish.
453810922SJeff.Bonwick@Sun.COM 		 */
453910922SJeff.Bonwick@Sun.COM 		spa_vdev_config_exit(spa, NULL,
454010922SJeff.Bonwick@Sun.COM 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
454110922SJeff.Bonwick@Sun.COM 
454210974SJeff.Bonwick@Sun.COM 		/*
454310974SJeff.Bonwick@Sun.COM 		 * Attempt to evacuate the vdev.
454410974SJeff.Bonwick@Sun.COM 		 */
454510974SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove_evacuate(spa, vd);
454610974SJeff.Bonwick@Sun.COM 
454710594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
454810594SGeorge.Wilson@Sun.COM 
454910974SJeff.Bonwick@Sun.COM 		/*
455010974SJeff.Bonwick@Sun.COM 		 * If we couldn't evacuate the vdev, unwind.
455110974SJeff.Bonwick@Sun.COM 		 */
455210974SJeff.Bonwick@Sun.COM 		if (error) {
455310974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
455410974SJeff.Bonwick@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
455510974SJeff.Bonwick@Sun.COM 		}
455610974SJeff.Bonwick@Sun.COM 
455710974SJeff.Bonwick@Sun.COM 		/*
455810974SJeff.Bonwick@Sun.COM 		 * Clean up the vdev namespace.
455910974SJeff.Bonwick@Sun.COM 		 */
456010974SJeff.Bonwick@Sun.COM 		spa_vdev_remove_from_namespace(spa, vd);
456110594SGeorge.Wilson@Sun.COM 
45627754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
45637754SJeff.Bonwick@Sun.COM 		/*
45647754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
45657754SJeff.Bonwick@Sun.COM 		 */
45667754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
45677754SJeff.Bonwick@Sun.COM 	} else {
45687754SJeff.Bonwick@Sun.COM 		/*
45697754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
45707754SJeff.Bonwick@Sun.COM 		 */
45717754SJeff.Bonwick@Sun.COM 		error = ENOENT;
45725450Sbrendan 	}
45732082Seschrock 
45748241SJeff.Bonwick@Sun.COM 	if (!locked)
45758241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
45768241SJeff.Bonwick@Sun.COM 
45778241SJeff.Bonwick@Sun.COM 	return (error);
4578789Sahrens }
4579789Sahrens 
4580789Sahrens /*
45814451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
45824451Seschrock  * current spared, so we can detach it.
4583789Sahrens  */
45841544Seschrock static vdev_t *
45854451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
4586789Sahrens {
45871544Seschrock 	vdev_t *newvd, *oldvd;
45889816SGeorge.Wilson@Sun.COM 
45899816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
45904451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
45911544Seschrock 		if (oldvd != NULL)
45921544Seschrock 			return (oldvd);
45931544Seschrock 	}
4594789Sahrens 
45954451Seschrock 	/*
45964451Seschrock 	 * Check for a completed replacement.
45974451Seschrock 	 */
4598789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
45991544Seschrock 		oldvd = vd->vdev_child[0];
46001544Seschrock 		newvd = vd->vdev_child[1];
4601789Sahrens 
46028241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
460311820SVictor.Latushkin@Sun.COM 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
46048241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
46051544Seschrock 			return (oldvd);
46061544Seschrock 	}
4607789Sahrens 
46084451Seschrock 	/*
46094451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
46104451Seschrock 	 */
46114451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
46124451Seschrock 		newvd = vd->vdev_child[0];
46134451Seschrock 		oldvd = vd->vdev_child[1];
46144451Seschrock 
46154451Seschrock 		if (newvd->vdev_unspare &&
46168241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
461711820SVictor.Latushkin@Sun.COM 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
46188241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
46194451Seschrock 			newvd->vdev_unspare = 0;
46204451Seschrock 			return (oldvd);
46214451Seschrock 		}
46224451Seschrock 	}
46234451Seschrock 
46241544Seschrock 	return (NULL);
4625789Sahrens }
4626789Sahrens 
46271544Seschrock static void
46284451Seschrock spa_vdev_resilver_done(spa_t *spa)
4629789Sahrens {
46308241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
46318241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
46328241SJeff.Bonwick@Sun.COM 
46338241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4634789Sahrens 
46354451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
46368241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
46378241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
46381544Seschrock 		guid = vd->vdev_guid;
46398241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
46408241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
46418241SJeff.Bonwick@Sun.COM 		sguid = 0;
46422082Seschrock 		/*
46432082Seschrock 		 * If we have just finished replacing a hot spared device, then
46442082Seschrock 		 * we need to detach the parent's first child (the original hot
46452082Seschrock 		 * spare) as well.
46462082Seschrock 		 */
46478241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
46482082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
46498241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
46508241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
46512082Seschrock 		}
46528241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
46538241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
46541544Seschrock 			return;
46558241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
46562082Seschrock 			return;
46578241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4658789Sahrens 	}
4659789Sahrens 
46608241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4661789Sahrens }
4662789Sahrens 
4663789Sahrens /*
466411041SEric.Taylor@Sun.COM  * Update the stored path or FRU for this vdev.
46651354Seschrock  */
46661354Seschrock int
46679425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
46689425SEric.Schrock@Sun.COM     boolean_t ispath)
46691354Seschrock {
46706643Seschrock 	vdev_t *vd;
467111817SGeorge.Wilson@Sun.COM 	boolean_t sync = B_FALSE;
467211041SEric.Taylor@Sun.COM 
467311041SEric.Taylor@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALL);
46741354Seschrock 
46759425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
467611041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
46771354Seschrock 
46781585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
467911041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
46801585Sbonwick 
46819425SEric.Schrock@Sun.COM 	if (ispath) {
468211817SGeorge.Wilson@Sun.COM 		if (strcmp(value, vd->vdev_path) != 0) {
468311817SGeorge.Wilson@Sun.COM 			spa_strfree(vd->vdev_path);
468411817SGeorge.Wilson@Sun.COM 			vd->vdev_path = spa_strdup(value);
468511817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
468611817SGeorge.Wilson@Sun.COM 		}
46879425SEric.Schrock@Sun.COM 	} else {
468811817SGeorge.Wilson@Sun.COM 		if (vd->vdev_fru == NULL) {
468911817SGeorge.Wilson@Sun.COM 			vd->vdev_fru = spa_strdup(value);
469011817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
469111817SGeorge.Wilson@Sun.COM 		} else if (strcmp(value, vd->vdev_fru) != 0) {
46929425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
469311817SGeorge.Wilson@Sun.COM 			vd->vdev_fru = spa_strdup(value);
469411817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
469511817SGeorge.Wilson@Sun.COM 		}
46969425SEric.Schrock@Sun.COM 	}
46971354Seschrock 
469811817SGeorge.Wilson@Sun.COM 	return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
46991354Seschrock }
47001354Seschrock 
47019425SEric.Schrock@Sun.COM int
47029425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
47039425SEric.Schrock@Sun.COM {
47049425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
47059425SEric.Schrock@Sun.COM }
47069425SEric.Schrock@Sun.COM 
47079425SEric.Schrock@Sun.COM int
47089425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
47099425SEric.Schrock@Sun.COM {
47109425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
47119425SEric.Schrock@Sun.COM }
47129425SEric.Schrock@Sun.COM 
47131354Seschrock /*
4714789Sahrens  * ==========================================================================
471512296SLin.Ling@Sun.COM  * SPA Scanning
4716789Sahrens  * ==========================================================================
4717789Sahrens  */
4718789Sahrens 
47197046Sahrens int
472012296SLin.Ling@Sun.COM spa_scan_stop(spa_t *spa)
4721789Sahrens {
47227754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
472312296SLin.Ling@Sun.COM 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
472412296SLin.Ling@Sun.COM 		return (EBUSY);
472512296SLin.Ling@Sun.COM 	return (dsl_scan_cancel(spa->spa_dsl_pool));
472612296SLin.Ling@Sun.COM }
472712296SLin.Ling@Sun.COM 
472812296SLin.Ling@Sun.COM int
472912296SLin.Ling@Sun.COM spa_scan(spa_t *spa, pool_scan_func_t func)
473012296SLin.Ling@Sun.COM {
473112296SLin.Ling@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
473212296SLin.Ling@Sun.COM 
473312296SLin.Ling@Sun.COM 	if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
4734789Sahrens 		return (ENOTSUP);
4735789Sahrens 
4736789Sahrens 	/*
47377046Sahrens 	 * If a resilver was requested, but there is no DTL on a
47387046Sahrens 	 * writeable leaf device, we have nothing to do.
4739789Sahrens 	 */
474012296SLin.Ling@Sun.COM 	if (func == POOL_SCAN_RESILVER &&
47417046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
47427046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
47431544Seschrock 		return (0);
47441544Seschrock 	}
4745789Sahrens 
474612296SLin.Ling@Sun.COM 	return (dsl_scan(spa->spa_dsl_pool, func));
4747789Sahrens }
4748789Sahrens 
47491544Seschrock /*
47501544Seschrock  * ==========================================================================
47511544Seschrock  * SPA async task processing
47521544Seschrock  * ==========================================================================
47531544Seschrock  */
47541544Seschrock 
47551544Seschrock static void
47564451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4757789Sahrens {
47587361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
475912247SGeorge.Wilson@Sun.COM 		vd->vdev_remove_wanted = B_FALSE;
476012247SGeorge.Wilson@Sun.COM 		vd->vdev_delayed_close = B_FALSE;
47617361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
476210575SEric.Schrock@Sun.COM 
476310575SEric.Schrock@Sun.COM 		/*
476410575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
476510575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
476610575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
476710575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
476810575SEric.Schrock@Sun.COM 		 */
476910575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
477010575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
477110575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
477210575SEric.Schrock@Sun.COM 
47737754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
47741544Seschrock 	}
47757361SBrendan.Gregg@Sun.COM 
47767754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47777361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
47781544Seschrock }
47791544Seschrock 
47801544Seschrock static void
47817754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
47827754SJeff.Bonwick@Sun.COM {
47837754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
478412247SGeorge.Wilson@Sun.COM 		vd->vdev_probe_wanted = B_FALSE;
47857754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
47867754SJeff.Bonwick@Sun.COM 	}
47877754SJeff.Bonwick@Sun.COM 
47887754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47897754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
47907754SJeff.Bonwick@Sun.COM }
47917754SJeff.Bonwick@Sun.COM 
47927754SJeff.Bonwick@Sun.COM static void
47939816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
47949816SGeorge.Wilson@Sun.COM {
47959816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
47969816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
47979816SGeorge.Wilson@Sun.COM 	char *physpath;
47989816SGeorge.Wilson@Sun.COM 
47999816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
48009816SGeorge.Wilson@Sun.COM 		return;
48019816SGeorge.Wilson@Sun.COM 
48029816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
48039816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
48049816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
48059816SGeorge.Wilson@Sun.COM 	}
48069816SGeorge.Wilson@Sun.COM 
48079816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
48089816SGeorge.Wilson@Sun.COM 		return;
48099816SGeorge.Wilson@Sun.COM 
48109816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
48119816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
48129816SGeorge.Wilson@Sun.COM 
48139816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
48149816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
48159816SGeorge.Wilson@Sun.COM 
48169816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
48179816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
48189816SGeorge.Wilson@Sun.COM 
48199816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
48209816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
48219816SGeorge.Wilson@Sun.COM }
48229816SGeorge.Wilson@Sun.COM 
48239816SGeorge.Wilson@Sun.COM static void
48241544Seschrock spa_async_thread(spa_t *spa)
48251544Seschrock {
48267754SJeff.Bonwick@Sun.COM 	int tasks;
48271544Seschrock 
48281544Seschrock 	ASSERT(spa->spa_sync_on);
4829789Sahrens 
48301544Seschrock 	mutex_enter(&spa->spa_async_lock);
48311544Seschrock 	tasks = spa->spa_async_tasks;
48321544Seschrock 	spa->spa_async_tasks = 0;
48331544Seschrock 	mutex_exit(&spa->spa_async_lock);
48341544Seschrock 
48351544Seschrock 	/*
48361635Sbonwick 	 * See if the config needs to be updated.
48371635Sbonwick 	 */
48381635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
483910922SJeff.Bonwick@Sun.COM 		uint64_t old_space, new_space;
48409816SGeorge.Wilson@Sun.COM 
48411635Sbonwick 		mutex_enter(&spa_namespace_lock);
484210922SJeff.Bonwick@Sun.COM 		old_space = metaslab_class_get_space(spa_normal_class(spa));
48431635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
484410922SJeff.Bonwick@Sun.COM 		new_space = metaslab_class_get_space(spa_normal_class(spa));
48451635Sbonwick 		mutex_exit(&spa_namespace_lock);
48469816SGeorge.Wilson@Sun.COM 
48479816SGeorge.Wilson@Sun.COM 		/*
48489816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
48499816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
48509816SGeorge.Wilson@Sun.COM 		 */
485110922SJeff.Bonwick@Sun.COM 		if (new_space != old_space) {
485212296SLin.Ling@Sun.COM 			spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
485312296SLin.Ling@Sun.COM 			    spa, NULL,
48549946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
485510922SJeff.Bonwick@Sun.COM 			    spa_name(spa), new_space, new_space - old_space);
48569816SGeorge.Wilson@Sun.COM 		}
48571635Sbonwick 	}
48581635Sbonwick 
48591635Sbonwick 	/*
48604451Seschrock 	 * See if any devices need to be marked REMOVED.
48611544Seschrock 	 */
48627754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
486310685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48644451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
48657754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
48667361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
48677754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
48687361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
48697754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48707754SJeff.Bonwick@Sun.COM 	}
48717754SJeff.Bonwick@Sun.COM 
48729816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
48739816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
48749816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
48759816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
48769816SGeorge.Wilson@Sun.COM 	}
48779816SGeorge.Wilson@Sun.COM 
48787754SJeff.Bonwick@Sun.COM 	/*
48797754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
48807754SJeff.Bonwick@Sun.COM 	 */
48817754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
488210685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48837754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
48847754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48854451Seschrock 	}
48861544Seschrock 
48871544Seschrock 	/*
48881544Seschrock 	 * If any devices are done replacing, detach them.
48891544Seschrock 	 */
48904451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
48914451Seschrock 		spa_vdev_resilver_done(spa);
4892789Sahrens 
48931544Seschrock 	/*
48941544Seschrock 	 * Kick off a resilver.
48951544Seschrock 	 */
48967046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
489712296SLin.Ling@Sun.COM 		dsl_resilver_restart(spa->spa_dsl_pool, 0);
48981544Seschrock 
48991544Seschrock 	/*
49001544Seschrock 	 * Let the world know that we're done.
49011544Seschrock 	 */
49021544Seschrock 	mutex_enter(&spa->spa_async_lock);
49031544Seschrock 	spa->spa_async_thread = NULL;
49041544Seschrock 	cv_broadcast(&spa->spa_async_cv);
49051544Seschrock 	mutex_exit(&spa->spa_async_lock);
49061544Seschrock 	thread_exit();
49071544Seschrock }
49081544Seschrock 
49091544Seschrock void
49101544Seschrock spa_async_suspend(spa_t *spa)
49111544Seschrock {
49121544Seschrock 	mutex_enter(&spa->spa_async_lock);
49131544Seschrock 	spa->spa_async_suspended++;
49141544Seschrock 	while (spa->spa_async_thread != NULL)
49151544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
49161544Seschrock 	mutex_exit(&spa->spa_async_lock);
49171544Seschrock }
49181544Seschrock 
49191544Seschrock void
49201544Seschrock spa_async_resume(spa_t *spa)
49211544Seschrock {
49221544Seschrock 	mutex_enter(&spa->spa_async_lock);
49231544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
49241544Seschrock 	spa->spa_async_suspended--;
49251544Seschrock 	mutex_exit(&spa->spa_async_lock);
49261544Seschrock }
49271544Seschrock 
49281544Seschrock static void
49291544Seschrock spa_async_dispatch(spa_t *spa)
49301544Seschrock {
49311544Seschrock 	mutex_enter(&spa->spa_async_lock);
49321544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
49331635Sbonwick 	    spa->spa_async_thread == NULL &&
49341635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
49351544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
49361544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
49371544Seschrock 	mutex_exit(&spa->spa_async_lock);
49381544Seschrock }
49391544Seschrock 
49401544Seschrock void
49411544Seschrock spa_async_request(spa_t *spa, int task)
49421544Seschrock {
494312296SLin.Ling@Sun.COM 	zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
49441544Seschrock 	mutex_enter(&spa->spa_async_lock);
49451544Seschrock 	spa->spa_async_tasks |= task;
49461544Seschrock 	mutex_exit(&spa->spa_async_lock);
4947789Sahrens }
4948789Sahrens 
4949789Sahrens /*
4950789Sahrens  * ==========================================================================
4951789Sahrens  * SPA syncing routines
4952789Sahrens  * ==========================================================================
4953789Sahrens  */
4954*12470SMatthew.Ahrens@Sun.COM 
4955*12470SMatthew.Ahrens@Sun.COM static int
4956*12470SMatthew.Ahrens@Sun.COM bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
4957789Sahrens {
4958*12470SMatthew.Ahrens@Sun.COM 	bpobj_t *bpo = arg;
4959*12470SMatthew.Ahrens@Sun.COM 	bpobj_enqueue(bpo, bp, tx);
4960*12470SMatthew.Ahrens@Sun.COM 	return (0);
496110922SJeff.Bonwick@Sun.COM }
496210922SJeff.Bonwick@Sun.COM 
4963*12470SMatthew.Ahrens@Sun.COM static int
4964*12470SMatthew.Ahrens@Sun.COM spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
496510922SJeff.Bonwick@Sun.COM {
496610922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
496710922SJeff.Bonwick@Sun.COM 
496810922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
496910922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
4970*12470SMatthew.Ahrens@Sun.COM 	return (0);
4971789Sahrens }
4972789Sahrens 
4973789Sahrens static void
49742082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
49752082Seschrock {
49762082Seschrock 	char *packed = NULL;
49777497STim.Haley@Sun.COM 	size_t bufsize;
49782082Seschrock 	size_t nvsize = 0;
49792082Seschrock 	dmu_buf_t *db;
49802082Seschrock 
49812082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
49822082Seschrock 
49837497STim.Haley@Sun.COM 	/*
49847497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
49857497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
49867497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
49877497STim.Haley@Sun.COM 	 */
49887497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
49897497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
49902082Seschrock 
49912082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
49922082Seschrock 	    KM_SLEEP) == 0);
49937497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
49947497STim.Haley@Sun.COM 
49957497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
49967497STim.Haley@Sun.COM 
49977497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
49982082Seschrock 
49992082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
50002082Seschrock 	dmu_buf_will_dirty(db, tx);
50012082Seschrock 	*(uint64_t *)db->db_data = nvsize;
50022082Seschrock 	dmu_buf_rele(db, FTAG);
50032082Seschrock }
50042082Seschrock 
50052082Seschrock static void
50065450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
50075450Sbrendan     const char *config, const char *entry)
50082082Seschrock {
50092082Seschrock 	nvlist_t *nvroot;
50105450Sbrendan 	nvlist_t **list;
50112082Seschrock 	int i;
50122082Seschrock 
50135450Sbrendan 	if (!sav->sav_sync)
50142082Seschrock 		return;
50152082Seschrock 
50162082Seschrock 	/*
50175450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
50185450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
50194451Seschrock 	 * valid and the vdevs are labeled appropriately.
50202082Seschrock 	 */
50215450Sbrendan 	if (sav->sav_object == 0) {
50225450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
50235450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
50245450Sbrendan 		    sizeof (uint64_t), tx);
50252082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
50265450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
50275450Sbrendan 		    &sav->sav_object, tx) == 0);
50282082Seschrock 	}
50292082Seschrock 
50302082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
50315450Sbrendan 	if (sav->sav_count == 0) {
50325450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
50332082Seschrock 	} else {
50345450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
50355450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50365450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
503712296SLin.Ling@Sun.COM 			    B_FALSE, VDEV_CONFIG_L2CACHE);
50385450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
50395450Sbrendan 		    sav->sav_count) == 0);
50405450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50415450Sbrendan 			nvlist_free(list[i]);
50425450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
50432082Seschrock 	}
50442082Seschrock 
50455450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
50462926Sek110237 	nvlist_free(nvroot);
50472082Seschrock 
50485450Sbrendan 	sav->sav_sync = B_FALSE;
50492082Seschrock }
50502082Seschrock 
50512082Seschrock static void
5052789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5053789Sahrens {
5054789Sahrens 	nvlist_t *config;
5055789Sahrens 
50567754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
5057789Sahrens 		return;
5058789Sahrens 
50597754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
50607754SJeff.Bonwick@Sun.COM 
50617754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
50627754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
50637754SJeff.Bonwick@Sun.COM 
50647754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
5065789Sahrens 
50661635Sbonwick 	if (spa->spa_config_syncing)
50671635Sbonwick 		nvlist_free(spa->spa_config_syncing);
50681635Sbonwick 	spa->spa_config_syncing = config;
5069789Sahrens 
50702082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5071789Sahrens }
5072789Sahrens 
50735094Slling /*
50745094Slling  * Set zpool properties.
50755094Slling  */
50763912Slling static void
507712296SLin.Ling@Sun.COM spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
50783912Slling {
50793912Slling 	spa_t *spa = arg1;
50805094Slling 	objset_t *mos = spa->spa_meta_objset;
50813912Slling 	nvlist_t *nvp = arg2;
50825094Slling 	nvpair_t *elem;
50834451Seschrock 	uint64_t intval;
50846643Seschrock 	char *strval;
50855094Slling 	zpool_prop_t prop;
50865094Slling 	const char *propname;
50875094Slling 	zprop_type_t proptype;
50885094Slling 
50897754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
50907754SJeff.Bonwick@Sun.COM 
50915094Slling 	elem = NULL;
50925094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
50935094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
50945094Slling 		case ZPOOL_PROP_VERSION:
50955094Slling 			/*
50965094Slling 			 * Only set version for non-zpool-creation cases
50975094Slling 			 * (set/import). spa_create() needs special care
50985094Slling 			 * for version setting.
50995094Slling 			 */
51005094Slling 			if (tx->tx_txg != TXG_INITIAL) {
51015094Slling 				VERIFY(nvpair_value_uint64(elem,
51025094Slling 				    &intval) == 0);
51035094Slling 				ASSERT(intval <= SPA_VERSION);
51045094Slling 				ASSERT(intval >= spa_version(spa));
51055094Slling 				spa->spa_uberblock.ub_version = intval;
51065094Slling 				vdev_config_dirty(spa->spa_root_vdev);
51075094Slling 			}
51085094Slling 			break;
51095094Slling 
51105094Slling 		case ZPOOL_PROP_ALTROOT:
51115094Slling 			/*
51125094Slling 			 * 'altroot' is a non-persistent property. It should
51135094Slling 			 * have been set temporarily at creation or import time.
51145094Slling 			 */
51155094Slling 			ASSERT(spa->spa_root != NULL);
51165094Slling 			break;
51175094Slling 
51185363Seschrock 		case ZPOOL_PROP_CACHEFILE:
51195094Slling 			/*
51208525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
51215094Slling 			 */
51224543Smarks 			break;
51235094Slling 		default:
51245094Slling 			/*
51255094Slling 			 * Set pool property values in the poolprops mos object.
51265094Slling 			 */
51275094Slling 			if (spa->spa_pool_props_object == 0) {
51285094Slling 				VERIFY((spa->spa_pool_props_object =
51295094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
51305094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
51315094Slling 
51325094Slling 				VERIFY(zap_update(mos,
51335094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
51345094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
51355094Slling 				    == 0);
51365094Slling 			}
51375094Slling 
51385094Slling 			/* normalize the property name */
51395094Slling 			propname = zpool_prop_to_name(prop);
51405094Slling 			proptype = zpool_prop_get_type(prop);
51415094Slling 
51425094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
51435094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
51445094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
51455094Slling 				VERIFY(zap_update(mos,
51465094Slling 				    spa->spa_pool_props_object, propname,
51475094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
51485094Slling 
51495094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
51505094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
51515094Slling 
51525094Slling 				if (proptype == PROP_TYPE_INDEX) {
51535094Slling 					const char *unused;
51545094Slling 					VERIFY(zpool_prop_index_to_string(
51555094Slling 					    prop, intval, &unused) == 0);
51565094Slling 				}
51575094Slling 				VERIFY(zap_update(mos,
51585094Slling 				    spa->spa_pool_props_object, propname,
51595094Slling 				    8, 1, &intval, tx) == 0);
51605094Slling 			} else {
51615094Slling 				ASSERT(0); /* not allowed */
51625094Slling 			}
51635094Slling 
51645329Sgw25295 			switch (prop) {
51655329Sgw25295 			case ZPOOL_PROP_DELEGATION:
51665094Slling 				spa->spa_delegation = intval;
51675329Sgw25295 				break;
51685329Sgw25295 			case ZPOOL_PROP_BOOTFS:
51695094Slling 				spa->spa_bootfs = intval;
51705329Sgw25295 				break;
51715329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
51725329Sgw25295 				spa->spa_failmode = intval;
51735329Sgw25295 				break;
51749816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
51759816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
517612318SEric.Taylor@Sun.COM 				if (tx->tx_txg != TXG_INITIAL)
517712318SEric.Taylor@Sun.COM 					spa_async_request(spa,
517812318SEric.Taylor@Sun.COM 					    SPA_ASYNC_AUTOEXPAND);
51799816SGeorge.Wilson@Sun.COM 				break;
518010922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
518110922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
518210922SJeff.Bonwick@Sun.COM 				break;
51835329Sgw25295 			default:
51845329Sgw25295 				break;
51855329Sgw25295 			}
51863912Slling 		}
51875094Slling 
51885094Slling 		/* log internal history if this is not a zpool create */
51895094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
51905094Slling 		    tx->tx_txg != TXG_INITIAL) {
519112296SLin.Ling@Sun.COM 			spa_history_log_internal(LOG_POOL_PROPSET,
519212296SLin.Ling@Sun.COM 			    spa, tx, "%s %lld %s",
51937754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
51945094Slling 		}
51953912Slling 	}
51967754SJeff.Bonwick@Sun.COM 
51977754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
51983912Slling }
51993912Slling 
5200789Sahrens /*
5201*12470SMatthew.Ahrens@Sun.COM  * Perform one-time upgrade on-disk changes.  spa_version() does not
5202*12470SMatthew.Ahrens@Sun.COM  * reflect the new version this txg, so there must be no changes this
5203*12470SMatthew.Ahrens@Sun.COM  * txg to anything that the upgrade code depends on after it executes.
5204*12470SMatthew.Ahrens@Sun.COM  * Therefore this must be called after dsl_pool_sync() does the sync
5205*12470SMatthew.Ahrens@Sun.COM  * tasks.
5206*12470SMatthew.Ahrens@Sun.COM  */
5207*12470SMatthew.Ahrens@Sun.COM static void
5208*12470SMatthew.Ahrens@Sun.COM spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5209*12470SMatthew.Ahrens@Sun.COM {
5210*12470SMatthew.Ahrens@Sun.COM 	dsl_pool_t *dp = spa->spa_dsl_pool;
5211*12470SMatthew.Ahrens@Sun.COM 
5212*12470SMatthew.Ahrens@Sun.COM 	ASSERT(spa->spa_sync_pass == 1);
5213*12470SMatthew.Ahrens@Sun.COM 
5214*12470SMatthew.Ahrens@Sun.COM 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5215*12470SMatthew.Ahrens@Sun.COM 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5216*12470SMatthew.Ahrens@Sun.COM 		dsl_pool_create_origin(dp, tx);
5217*12470SMatthew.Ahrens@Sun.COM 
5218*12470SMatthew.Ahrens@Sun.COM 		/* Keeping the origin open increases spa_minref */
5219*12470SMatthew.Ahrens@Sun.COM 		spa->spa_minref += 3;
5220*12470SMatthew.Ahrens@Sun.COM 	}
5221*12470SMatthew.Ahrens@Sun.COM 
5222*12470SMatthew.Ahrens@Sun.COM 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5223*12470SMatthew.Ahrens@Sun.COM 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5224*12470SMatthew.Ahrens@Sun.COM 		dsl_pool_upgrade_clones(dp, tx);
5225*12470SMatthew.Ahrens@Sun.COM 	}
5226*12470SMatthew.Ahrens@Sun.COM 
5227*12470SMatthew.Ahrens@Sun.COM 	if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5228*12470SMatthew.Ahrens@Sun.COM 	    spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5229*12470SMatthew.Ahrens@Sun.COM 		dsl_pool_upgrade_dir_clones(dp, tx);
5230*12470SMatthew.Ahrens@Sun.COM 
5231*12470SMatthew.Ahrens@Sun.COM 		/* Keeping the freedir open increases spa_minref */
5232*12470SMatthew.Ahrens@Sun.COM 		spa->spa_minref += 3;
5233*12470SMatthew.Ahrens@Sun.COM 	}
5234*12470SMatthew.Ahrens@Sun.COM }
5235*12470SMatthew.Ahrens@Sun.COM 
5236*12470SMatthew.Ahrens@Sun.COM /*
5237789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
5238789Sahrens  * part of the process, so we iterate until it converges.
5239789Sahrens  */
5240789Sahrens void
5241789Sahrens spa_sync(spa_t *spa, uint64_t txg)
5242789Sahrens {
5243789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
5244789Sahrens 	objset_t *mos = spa->spa_meta_objset;
5245*12470SMatthew.Ahrens@Sun.COM 	bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
524610922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
52471635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5248789Sahrens 	vdev_t *vd;
5249789Sahrens 	dmu_tx_t *tx;
52507754SJeff.Bonwick@Sun.COM 	int error;
5251789Sahrens 
5252789Sahrens 	/*
5253789Sahrens 	 * Lock out configuration changes.
5254789Sahrens 	 */
52557754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5256789Sahrens 
5257789Sahrens 	spa->spa_syncing_txg = txg;
5258789Sahrens 	spa->spa_sync_pass = 0;
5259789Sahrens 
52607754SJeff.Bonwick@Sun.COM 	/*
52617754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
52627754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
52637754SJeff.Bonwick@Sun.COM 	 */
52647754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
52658241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
52668241SJeff.Bonwick@Sun.COM 		/*
52678241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
52688241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
52698241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
52708241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
52718241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
52728241SJeff.Bonwick@Sun.COM 		 */
52738241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52748241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
52758241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
52768241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
52778241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
52788241SJeff.Bonwick@Sun.COM 		}
52798241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52808241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
52817754SJeff.Bonwick@Sun.COM 	}
52827754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
52837754SJeff.Bonwick@Sun.COM 
52842082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
52852082Seschrock 
52862082Seschrock 	/*
52874577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
52882082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
52892082Seschrock 	 */
52904577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
52914577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
52922082Seschrock 		int i;
52932082Seschrock 
52942082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
52952082Seschrock 			vd = rvd->vdev_child[i];
52962082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
52972082Seschrock 				break;
52982082Seschrock 		}
52992082Seschrock 		if (i == rvd->vdev_children) {
53002082Seschrock 			spa->spa_deflate = TRUE;
53012082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
53022082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
53032082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
53042082Seschrock 		}
53052082Seschrock 	}
53062082Seschrock 
5307789Sahrens 	/*
530812296SLin.Ling@Sun.COM 	 * If anything has changed in this txg, or if someone is waiting
530912296SLin.Ling@Sun.COM 	 * for this txg to sync (eg, spa_vdev_remove()), push the
531012296SLin.Ling@Sun.COM 	 * deferred frees from the previous txg.  If not, leave them
531112296SLin.Ling@Sun.COM 	 * alone so that we don't generate work on an otherwise idle
531212296SLin.Ling@Sun.COM 	 * system.
5313789Sahrens 	 */
5314789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
53152329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
531612296SLin.Ling@Sun.COM 	    !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5317*12470SMatthew.Ahrens@Sun.COM 	    ((dsl_scan_active(dp->dp_scan) ||
5318*12470SMatthew.Ahrens@Sun.COM 	    txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5319*12470SMatthew.Ahrens@Sun.COM 		zio_t *zio = zio_root(spa, NULL, NULL, 0);
5320*12470SMatthew.Ahrens@Sun.COM 		VERIFY3U(bpobj_iterate(defer_bpo,
5321*12470SMatthew.Ahrens@Sun.COM 		    spa_free_sync_cb, zio, tx), ==, 0);
5322*12470SMatthew.Ahrens@Sun.COM 		VERIFY3U(zio_wait(zio), ==, 0);
5323*12470SMatthew.Ahrens@Sun.COM 	}
5324789Sahrens 
5325789Sahrens 	/*
5326789Sahrens 	 * Iterate to convergence.
5327789Sahrens 	 */
5328789Sahrens 	do {
532910922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
5330789Sahrens 
5331789Sahrens 		spa_sync_config_object(spa, tx);
53325450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
53335450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
53345450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
53355450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
53361544Seschrock 		spa_errlog_sync(spa, txg);
5337789Sahrens 		dsl_pool_sync(dp, txg);
5338789Sahrens 
533910922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
534010922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
5341*12470SMatthew.Ahrens@Sun.COM 			bplist_iterate(free_bpl, spa_free_sync_cb,
5342*12470SMatthew.Ahrens@Sun.COM 			    zio, tx);
534310922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
534410922SJeff.Bonwick@Sun.COM 		} else {
5345*12470SMatthew.Ahrens@Sun.COM 			bplist_iterate(free_bpl, bpobj_enqueue_cb,
5346*12470SMatthew.Ahrens@Sun.COM 			    defer_bpo, tx);
5347789Sahrens 		}
5348789Sahrens 
534910922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
535012296SLin.Ling@Sun.COM 		dsl_scan_sync(dp, tx);
535111619SGeorge.Wilson@Sun.COM 
535210922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
535310922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
535410922SJeff.Bonwick@Sun.COM 
5355*12470SMatthew.Ahrens@Sun.COM 		if (pass == 1)
5356*12470SMatthew.Ahrens@Sun.COM 			spa_sync_upgrades(spa, tx);
5357*12470SMatthew.Ahrens@Sun.COM 
535810922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
535910922SJeff.Bonwick@Sun.COM 
5360789Sahrens 	/*
5361789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
5362789Sahrens 	 * to commit the transaction group.
53631635Sbonwick 	 *
53645688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
53655688Sbonwick 	 * random top-level vdevs that are known to be visible in the
53667754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
53677754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5368789Sahrens 	 */
53697754SJeff.Bonwick@Sun.COM 	for (;;) {
53707754SJeff.Bonwick@Sun.COM 		/*
53717754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
53727754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
53737754SJeff.Bonwick@Sun.COM 		 */
53747754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
53757754SJeff.Bonwick@Sun.COM 
53767754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
53777754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
53787754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
53797754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
53807754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
53819816SGeorge.Wilson@Sun.COM 
53829816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
53837754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
53847754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
53857754SJeff.Bonwick@Sun.COM 					continue;
53867754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
53877754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
53887754SJeff.Bonwick@Sun.COM 					break;
53897754SJeff.Bonwick@Sun.COM 			}
53909725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
53919725SEric.Schrock@Sun.COM 			if (error != 0)
53929725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
53939725SEric.Schrock@Sun.COM 				    B_TRUE);
53947754SJeff.Bonwick@Sun.COM 		} else {
53957754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
53969725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
53979725SEric.Schrock@Sun.COM 			if (error != 0)
53989725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
53999725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
54001635Sbonwick 		}
54017754SJeff.Bonwick@Sun.COM 
54027754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
54037754SJeff.Bonwick@Sun.COM 
54047754SJeff.Bonwick@Sun.COM 		if (error == 0)
54057754SJeff.Bonwick@Sun.COM 			break;
54067754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
54077754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
54081635Sbonwick 	}
54092082Seschrock 	dmu_tx_commit(tx);
54102082Seschrock 
54111635Sbonwick 	/*
54121635Sbonwick 	 * Clear the dirty config list.
54131635Sbonwick 	 */
54147754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
54151635Sbonwick 		vdev_config_clean(vd);
54161635Sbonwick 
54171635Sbonwick 	/*
54181635Sbonwick 	 * Now that the new config has synced transactionally,
54191635Sbonwick 	 * let it become visible to the config cache.
54201635Sbonwick 	 */
54211635Sbonwick 	if (spa->spa_config_syncing != NULL) {
54221635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
54231635Sbonwick 		spa->spa_config_txg = txg;
54241635Sbonwick 		spa->spa_config_syncing = NULL;
54251635Sbonwick 	}
5426789Sahrens 
5427789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
5428789Sahrens 
542910922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
5430789Sahrens 
5431789Sahrens 	/*
5432789Sahrens 	 * Update usable space statistics.
5433789Sahrens 	 */
5434789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5435789Sahrens 		vdev_sync_done(vd, txg);
5436789Sahrens 
543710956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
543810956SGeorge.Wilson@Sun.COM 
5439789Sahrens 	/*
5440789Sahrens 	 * It had better be the case that we didn't dirty anything
54412082Seschrock 	 * since vdev_config_sync().
5442789Sahrens 	 */
5443789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5444789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5445789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
544610922SJeff.Bonwick@Sun.COM 
544710922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
5448789Sahrens 
54497754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
54501544Seschrock 
545110921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
545210921STim.Haley@Sun.COM 
54531544Seschrock 	/*
54541544Seschrock 	 * If any async tasks have been requested, kick them off.
54551544Seschrock 	 */
54561544Seschrock 	spa_async_dispatch(spa);
5457789Sahrens }
5458789Sahrens 
5459789Sahrens /*
5460789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
5461789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
5462789Sahrens  * sync.
5463789Sahrens  */
5464789Sahrens void
5465789Sahrens spa_sync_allpools(void)
5466789Sahrens {
5467789Sahrens 	spa_t *spa = NULL;
5468789Sahrens 	mutex_enter(&spa_namespace_lock);
5469789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
54707754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
5471789Sahrens 			continue;
5472789Sahrens 		spa_open_ref(spa, FTAG);
5473789Sahrens 		mutex_exit(&spa_namespace_lock);
5474789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
5475789Sahrens 		mutex_enter(&spa_namespace_lock);
5476789Sahrens 		spa_close(spa, FTAG);
5477789Sahrens 	}
5478789Sahrens 	mutex_exit(&spa_namespace_lock);
5479789Sahrens }
5480789Sahrens 
5481789Sahrens /*
5482789Sahrens  * ==========================================================================
5483789Sahrens  * Miscellaneous routines
5484789Sahrens  * ==========================================================================
5485789Sahrens  */
5486789Sahrens 
5487789Sahrens /*
5488789Sahrens  * Remove all pools in the system.
5489789Sahrens  */
5490789Sahrens void
5491789Sahrens spa_evict_all(void)
5492789Sahrens {
5493789Sahrens 	spa_t *spa;
5494789Sahrens 
5495789Sahrens 	/*
5496789Sahrens 	 * Remove all cached state.  All pools should be closed now,
5497789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
5498789Sahrens 	 */
5499789Sahrens 	mutex_enter(&spa_namespace_lock);
5500789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
5501789Sahrens 		/*
55021544Seschrock 		 * Stop async tasks.  The async thread may need to detach
55031544Seschrock 		 * a device that's been replaced, which requires grabbing
55041544Seschrock 		 * spa_namespace_lock, so we must drop it here.
5505789Sahrens 		 */
5506789Sahrens 		spa_open_ref(spa, FTAG);
5507789Sahrens 		mutex_exit(&spa_namespace_lock);
55081544Seschrock 		spa_async_suspend(spa);
55094808Sek110237 		mutex_enter(&spa_namespace_lock);
5510789Sahrens 		spa_close(spa, FTAG);
5511789Sahrens 
5512789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5513789Sahrens 			spa_unload(spa);
5514789Sahrens 			spa_deactivate(spa);
5515789Sahrens 		}
5516789Sahrens 		spa_remove(spa);
5517789Sahrens 	}
5518789Sahrens 	mutex_exit(&spa_namespace_lock);
5519789Sahrens }
55201544Seschrock 
55211544Seschrock vdev_t *
55229425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
55231544Seschrock {
55246643Seschrock 	vdev_t *vd;
55256643Seschrock 	int i;
55266643Seschrock 
55276643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
55286643Seschrock 		return (vd);
55296643Seschrock 
55309425SEric.Schrock@Sun.COM 	if (aux) {
55316643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
55326643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
55336643Seschrock 			if (vd->vdev_guid == guid)
55346643Seschrock 				return (vd);
55356643Seschrock 		}
55369425SEric.Schrock@Sun.COM 
55379425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
55389425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
55399425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
55409425SEric.Schrock@Sun.COM 				return (vd);
55419425SEric.Schrock@Sun.COM 		}
55426643Seschrock 	}
55436643Seschrock 
55446643Seschrock 	return (NULL);
55451544Seschrock }
55461760Seschrock 
55471760Seschrock void
55485094Slling spa_upgrade(spa_t *spa, uint64_t version)
55491760Seschrock {
55507754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
55511760Seschrock 
55521760Seschrock 	/*
55531760Seschrock 	 * This should only be called for a non-faulted pool, and since a
55541760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
55551760Seschrock 	 * possible.
55561760Seschrock 	 */
55574577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
55585094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
55595094Slling 
55605094Slling 	spa->spa_uberblock.ub_version = version;
55611760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
55621760Seschrock 
55637754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
55642082Seschrock 
55652082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
55661760Seschrock }
55672082Seschrock 
55682082Seschrock boolean_t
55692082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
55702082Seschrock {
55712082Seschrock 	int i;
55723377Seschrock 	uint64_t spareguid;
55735450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
55745450Sbrendan 
55755450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
55765450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
55772082Seschrock 			return (B_TRUE);
55782082Seschrock 
55795450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
55805450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
55815450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
55823377Seschrock 			return (B_TRUE);
55833377Seschrock 	}
55843377Seschrock 
55852082Seschrock 	return (B_FALSE);
55862082Seschrock }
55873912Slling 
55884451Seschrock /*
55897214Slling  * Check if a pool has an active shared spare device.
55907214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
55917214Slling  */
55927214Slling static boolean_t
55937214Slling spa_has_active_shared_spare(spa_t *spa)
55947214Slling {
55957214Slling 	int i, refcnt;
55967214Slling 	uint64_t pool;
55977214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
55987214Slling 
55997214Slling 	for (i = 0; i < sav->sav_count; i++) {
56007214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
56017214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
56027214Slling 		    refcnt > 2)
56037214Slling 			return (B_TRUE);
56047214Slling 	}
56057214Slling 
56067214Slling 	return (B_FALSE);
56077214Slling }
56087214Slling 
56097214Slling /*
56104451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
56114451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
56124451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
56134451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
56144451Seschrock  * or zdb as real changes.
56154451Seschrock  */
56164451Seschrock void
56174451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
56184451Seschrock {
56194451Seschrock #ifdef _KERNEL
56204451Seschrock 	sysevent_t		*ev;
56214451Seschrock 	sysevent_attr_list_t	*attr = NULL;
56224451Seschrock 	sysevent_value_t	value;
56234451Seschrock 	sysevent_id_t		eid;
56244451Seschrock 
56254451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
56264451Seschrock 	    SE_SLEEP);
56274451Seschrock 
56284451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
56294451Seschrock 	value.value.sv_string = spa_name(spa);
56304451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
56314451Seschrock 		goto done;
56324451Seschrock 
56334451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
56344451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
56354451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
56364451Seschrock 		goto done;
56374451Seschrock 
56384451Seschrock 	if (vd) {
56394451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
56404451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
56414451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
56424451Seschrock 		    SE_SLEEP) != 0)
56434451Seschrock 			goto done;
56444451Seschrock 
56454451Seschrock 		if (vd->vdev_path) {
56464451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
56474451Seschrock 			value.value.sv_string = vd->vdev_path;
56484451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
56494451Seschrock 			    &value, SE_SLEEP) != 0)
56504451Seschrock 				goto done;
56514451Seschrock 		}
56524451Seschrock 	}
56534451Seschrock 
56545756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
56555756Seschrock 		goto done;
56565756Seschrock 	attr = NULL;
56575756Seschrock 
56584451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
56594451Seschrock 
56604451Seschrock done:
56614451Seschrock 	if (attr)
56624451Seschrock 		sysevent_free_attr(attr);
56634451Seschrock 	sysevent_free(ev);
56644451Seschrock #endif
56654451Seschrock }
5666