xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 12296)
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>
62*12296SLin.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) },
10911826SGeorge.Wilson@Sun.COM 	{ ZTI_FIX(10),	ZTI_NULL,	ZTI_FIX(10),	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 
114*12296SLin.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 
946789Sahrens 	/*
947789Sahrens 	 * Close the dsl pool.
948789Sahrens 	 */
949789Sahrens 	if (spa->spa_dsl_pool) {
950789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
951789Sahrens 		spa->spa_dsl_pool = NULL;
95211619SGeorge.Wilson@Sun.COM 		spa->spa_meta_objset = NULL;
953789Sahrens 	}
954789Sahrens 
95510922SJeff.Bonwick@Sun.COM 	ddt_unload(spa);
95610922SJeff.Bonwick@Sun.COM 
9578241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9588241SJeff.Bonwick@Sun.COM 
9598241SJeff.Bonwick@Sun.COM 	/*
9608241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
9618241SJeff.Bonwick@Sun.COM 	 */
9628241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
9638241SJeff.Bonwick@Sun.COM 
964789Sahrens 	/*
965789Sahrens 	 * Close all vdevs.
966789Sahrens 	 */
9671585Sbonwick 	if (spa->spa_root_vdev)
968789Sahrens 		vdev_free(spa->spa_root_vdev);
9691585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
9701544Seschrock 
9715450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9725450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
9735450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
9745450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
9755450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
9765450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
9775450Sbrendan 	}
9785450Sbrendan 	if (spa->spa_spares.sav_config) {
9795450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
9805450Sbrendan 		spa->spa_spares.sav_config = NULL;
9812082Seschrock 	}
9827377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
9835450Sbrendan 
9845450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
9855450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
9865450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
9875450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
9885450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
9895450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
9905450Sbrendan 	}
9915450Sbrendan 	if (spa->spa_l2cache.sav_config) {
9925450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
9935450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
9942082Seschrock 	}
9957377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
9962082Seschrock 
9971544Seschrock 	spa->spa_async_suspended = 0;
9988241SJeff.Bonwick@Sun.COM 
9998241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1000789Sahrens }
1001789Sahrens 
1002789Sahrens /*
10032082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
10042082Seschrock  * this pool.  When this is called, we have some form of basic information in
10055450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
10065450Sbrendan  * then re-generate a more complete list including status information.
10072082Seschrock  */
10082082Seschrock static void
10092082Seschrock spa_load_spares(spa_t *spa)
10102082Seschrock {
10112082Seschrock 	nvlist_t **spares;
10122082Seschrock 	uint_t nspares;
10132082Seschrock 	int i;
10143377Seschrock 	vdev_t *vd, *tvd;
10152082Seschrock 
10167754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
10177754SJeff.Bonwick@Sun.COM 
10182082Seschrock 	/*
10192082Seschrock 	 * First, close and free any existing spare vdevs.
10202082Seschrock 	 */
10215450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10225450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
10233377Seschrock 
10243377Seschrock 		/* Undo the call to spa_activate() below */
10256643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10266643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
10273377Seschrock 			spa_spare_remove(tvd);
10283377Seschrock 		vdev_close(vd);
10293377Seschrock 		vdev_free(vd);
10302082Seschrock 	}
10313377Seschrock 
10325450Sbrendan 	if (spa->spa_spares.sav_vdevs)
10335450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
10345450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
10355450Sbrendan 
10365450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
10372082Seschrock 		nspares = 0;
10382082Seschrock 	else
10395450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
10402082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
10412082Seschrock 
10425450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
10435450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
10442082Seschrock 
10452082Seschrock 	if (nspares == 0)
10462082Seschrock 		return;
10472082Seschrock 
10482082Seschrock 	/*
10492082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
10503377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
10513377Seschrock 	 * structures associated with it: one in the list of spares (used only
10523377Seschrock 	 * for basic validation purposes) and one in the active vdev
10533377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
10543377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
10553377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
10562082Seschrock 	 */
10575450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
10585450Sbrendan 	    KM_SLEEP);
10595450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10602082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
10612082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
10622082Seschrock 		ASSERT(vd != NULL);
10632082Seschrock 
10645450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
10652082Seschrock 
10666643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10676643Seschrock 		    B_FALSE)) != NULL) {
10683377Seschrock 			if (!tvd->vdev_isspare)
10693377Seschrock 				spa_spare_add(tvd);
10703377Seschrock 
10713377Seschrock 			/*
10723377Seschrock 			 * We only mark the spare active if we were successfully
10733377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
10743377Seschrock 			 * with a bad active spare would result in strange
10753377Seschrock 			 * behavior, because multiple pool would think the spare
10763377Seschrock 			 * is actively in use.
10773377Seschrock 			 *
10783377Seschrock 			 * There is a vulnerability here to an equally bizarre
10793377Seschrock 			 * circumstance, where a dead active spare is later
10803377Seschrock 			 * brought back to life (onlined or otherwise).  Given
10813377Seschrock 			 * the rarity of this scenario, and the extra complexity
10823377Seschrock 			 * it adds, we ignore the possibility.
10833377Seschrock 			 */
10843377Seschrock 			if (!vdev_is_dead(tvd))
10853377Seschrock 				spa_spare_activate(tvd);
10863377Seschrock 		}
10873377Seschrock 
10887754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
10899425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
10907754SJeff.Bonwick@Sun.COM 
10912082Seschrock 		if (vdev_open(vd) != 0)
10922082Seschrock 			continue;
10932082Seschrock 
10945450Sbrendan 		if (vdev_validate_aux(vd) == 0)
10955450Sbrendan 			spa_spare_add(vd);
10962082Seschrock 	}
10972082Seschrock 
10982082Seschrock 	/*
10992082Seschrock 	 * Recompute the stashed list of spares, with status information
11002082Seschrock 	 * this time.
11012082Seschrock 	 */
11025450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
11032082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
11042082Seschrock 
11055450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
11065450Sbrendan 	    KM_SLEEP);
11075450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11085450Sbrendan 		spares[i] = vdev_config_generate(spa,
1109*12296SLin.Ling@Sun.COM 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
11105450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
11115450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
11125450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11132082Seschrock 		nvlist_free(spares[i]);
11145450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
11155450Sbrendan }
11165450Sbrendan 
11175450Sbrendan /*
11185450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
11195450Sbrendan  * this pool.  When this is called, we have some form of basic information in
11205450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
11215450Sbrendan  * then re-generate a more complete list including status information.
11225450Sbrendan  * Devices which are already active have their details maintained, and are
11235450Sbrendan  * not re-opened.
11245450Sbrendan  */
11255450Sbrendan static void
11265450Sbrendan spa_load_l2cache(spa_t *spa)
11275450Sbrendan {
11285450Sbrendan 	nvlist_t **l2cache;
11295450Sbrendan 	uint_t nl2cache;
11305450Sbrendan 	int i, j, oldnvdevs;
11319816SGeorge.Wilson@Sun.COM 	uint64_t guid;
11325450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
11335450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
11345450Sbrendan 
11357754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
11367754SJeff.Bonwick@Sun.COM 
11375450Sbrendan 	if (sav->sav_config != NULL) {
11385450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
11395450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
11405450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
11415450Sbrendan 	} else {
11425450Sbrendan 		nl2cache = 0;
11435450Sbrendan 	}
11445450Sbrendan 
11455450Sbrendan 	oldvdevs = sav->sav_vdevs;
11465450Sbrendan 	oldnvdevs = sav->sav_count;
11475450Sbrendan 	sav->sav_vdevs = NULL;
11485450Sbrendan 	sav->sav_count = 0;
11495450Sbrendan 
11505450Sbrendan 	/*
11515450Sbrendan 	 * Process new nvlist of vdevs.
11525450Sbrendan 	 */
11535450Sbrendan 	for (i = 0; i < nl2cache; i++) {
11545450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
11555450Sbrendan 		    &guid) == 0);
11565450Sbrendan 
11575450Sbrendan 		newvdevs[i] = NULL;
11585450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
11595450Sbrendan 			vd = oldvdevs[j];
11605450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
11615450Sbrendan 				/*
11625450Sbrendan 				 * Retain previous vdev for add/remove ops.
11635450Sbrendan 				 */
11645450Sbrendan 				newvdevs[i] = vd;
11655450Sbrendan 				oldvdevs[j] = NULL;
11665450Sbrendan 				break;
11675450Sbrendan 			}
11685450Sbrendan 		}
11695450Sbrendan 
11705450Sbrendan 		if (newvdevs[i] == NULL) {
11715450Sbrendan 			/*
11725450Sbrendan 			 * Create new vdev
11735450Sbrendan 			 */
11745450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
11755450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
11765450Sbrendan 			ASSERT(vd != NULL);
11775450Sbrendan 			newvdevs[i] = vd;
11785450Sbrendan 
11795450Sbrendan 			/*
11805450Sbrendan 			 * Commit this vdev as an l2cache device,
11815450Sbrendan 			 * even if it fails to open.
11825450Sbrendan 			 */
11835450Sbrendan 			spa_l2cache_add(vd);
11845450Sbrendan 
11856643Seschrock 			vd->vdev_top = vd;
11866643Seschrock 			vd->vdev_aux = sav;
11876643Seschrock 
11886643Seschrock 			spa_l2cache_activate(vd);
11896643Seschrock 
11905450Sbrendan 			if (vdev_open(vd) != 0)
11915450Sbrendan 				continue;
11925450Sbrendan 
11935450Sbrendan 			(void) vdev_validate_aux(vd);
11945450Sbrendan 
11959816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
11969816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
11975450Sbrendan 		}
11985450Sbrendan 	}
11995450Sbrendan 
12005450Sbrendan 	/*
12015450Sbrendan 	 * Purge vdevs that were dropped
12025450Sbrendan 	 */
12035450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
12045450Sbrendan 		uint64_t pool;
12055450Sbrendan 
12065450Sbrendan 		vd = oldvdevs[i];
12075450Sbrendan 		if (vd != NULL) {
12088241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
12098241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
12105450Sbrendan 				l2arc_remove_vdev(vd);
12115450Sbrendan 			(void) vdev_close(vd);
12125450Sbrendan 			spa_l2cache_remove(vd);
12135450Sbrendan 		}
12145450Sbrendan 	}
12155450Sbrendan 
12165450Sbrendan 	if (oldvdevs)
12175450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
12185450Sbrendan 
12195450Sbrendan 	if (sav->sav_config == NULL)
12205450Sbrendan 		goto out;
12215450Sbrendan 
12225450Sbrendan 	sav->sav_vdevs = newvdevs;
12235450Sbrendan 	sav->sav_count = (int)nl2cache;
12245450Sbrendan 
12255450Sbrendan 	/*
12265450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
12275450Sbrendan 	 * information this time.
12285450Sbrendan 	 */
12295450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
12305450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
12315450Sbrendan 
12325450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
12335450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12345450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
1235*12296SLin.Ling@Sun.COM 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
12365450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
12375450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
12385450Sbrendan out:
12395450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12405450Sbrendan 		nvlist_free(l2cache[i]);
12415450Sbrendan 	if (sav->sav_count)
12425450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
12432082Seschrock }
12442082Seschrock 
12452082Seschrock static int
12462082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
12472082Seschrock {
12482082Seschrock 	dmu_buf_t *db;
12492082Seschrock 	char *packed = NULL;
12502082Seschrock 	size_t nvsize = 0;
12512082Seschrock 	int error;
12522082Seschrock 	*value = NULL;
12532082Seschrock 
12542082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
12552082Seschrock 	nvsize = *(uint64_t *)db->db_data;
12562082Seschrock 	dmu_buf_rele(db, FTAG);
12572082Seschrock 
12582082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
12599512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
12609512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
12612082Seschrock 	if (error == 0)
12622082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
12632082Seschrock 	kmem_free(packed, nvsize);
12642082Seschrock 
12652082Seschrock 	return (error);
12662082Seschrock }
12672082Seschrock 
12682082Seschrock /*
12694451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
12704451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
12714451Seschrock  */
12724451Seschrock static void
12734451Seschrock spa_check_removed(vdev_t *vd)
12744451Seschrock {
12759816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12764451Seschrock 		spa_check_removed(vd->vdev_child[c]);
12774451Seschrock 
12784451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
12794451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
12804451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
12814451Seschrock 	}
12824451Seschrock }
12834451Seschrock 
12844451Seschrock /*
12859701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
12869701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
12879701SGeorge.Wilson@Sun.COM  */
12889701SGeorge.Wilson@Sun.COM void
128910594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
12909701SGeorge.Wilson@Sun.COM {
129110594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
129210594SGeorge.Wilson@Sun.COM 
129310594SGeorge.Wilson@Sun.COM 	/*
129410594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
129510594SGeorge.Wilson@Sun.COM 	 */
129610594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
129710594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
129810594SGeorge.Wilson@Sun.COM 
129910594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
130010594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
130110594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
130210594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
13039701SGeorge.Wilson@Sun.COM 	}
130410594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
130510594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13069701SGeorge.Wilson@Sun.COM }
13079701SGeorge.Wilson@Sun.COM 
13089701SGeorge.Wilson@Sun.COM /*
13097294Sperrin  * Check for missing log devices
13107294Sperrin  */
13117294Sperrin int
13127294Sperrin spa_check_logs(spa_t *spa)
13137294Sperrin {
13147294Sperrin 	switch (spa->spa_log_state) {
13157294Sperrin 	case SPA_LOG_MISSING:
13167294Sperrin 		/* need to recheck in case slog has been restored */
13177294Sperrin 	case SPA_LOG_UNKNOWN:
13187294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
13197294Sperrin 		    DS_FIND_CHILDREN)) {
132011422SMark.Musante@Sun.COM 			spa_set_log_state(spa, SPA_LOG_MISSING);
13217294Sperrin 			return (1);
13227294Sperrin 		}
13237294Sperrin 		break;
13247294Sperrin 	}
13257294Sperrin 	return (0);
13267294Sperrin }
13277294Sperrin 
132811422SMark.Musante@Sun.COM static boolean_t
132911422SMark.Musante@Sun.COM spa_passivate_log(spa_t *spa)
133011422SMark.Musante@Sun.COM {
133111422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
133211422SMark.Musante@Sun.COM 	boolean_t slog_found = B_FALSE;
133311422SMark.Musante@Sun.COM 
133411422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
133511422SMark.Musante@Sun.COM 
133611422SMark.Musante@Sun.COM 	if (!spa_has_slogs(spa))
133711422SMark.Musante@Sun.COM 		return (B_FALSE);
133811422SMark.Musante@Sun.COM 
133911422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
134011422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
134111422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
134211422SMark.Musante@Sun.COM 
134311422SMark.Musante@Sun.COM 		if (tvd->vdev_islog) {
134411422SMark.Musante@Sun.COM 			metaslab_group_passivate(mg);
134511422SMark.Musante@Sun.COM 			slog_found = B_TRUE;
134611422SMark.Musante@Sun.COM 		}
134711422SMark.Musante@Sun.COM 	}
134811422SMark.Musante@Sun.COM 
134911422SMark.Musante@Sun.COM 	return (slog_found);
135011422SMark.Musante@Sun.COM }
135111422SMark.Musante@Sun.COM 
135211422SMark.Musante@Sun.COM static void
135311422SMark.Musante@Sun.COM spa_activate_log(spa_t *spa)
135411422SMark.Musante@Sun.COM {
135511422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
135611422SMark.Musante@Sun.COM 
135711422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
135811422SMark.Musante@Sun.COM 
135911422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
136011422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
136111422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
136211422SMark.Musante@Sun.COM 
136311422SMark.Musante@Sun.COM 		if (tvd->vdev_islog)
136411422SMark.Musante@Sun.COM 			metaslab_group_activate(mg);
136511422SMark.Musante@Sun.COM 	}
136611422SMark.Musante@Sun.COM }
136711422SMark.Musante@Sun.COM 
136811422SMark.Musante@Sun.COM int
136911422SMark.Musante@Sun.COM spa_offline_log(spa_t *spa)
137011422SMark.Musante@Sun.COM {
137111422SMark.Musante@Sun.COM 	int error = 0;
137211422SMark.Musante@Sun.COM 
137311422SMark.Musante@Sun.COM 	if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
137411422SMark.Musante@Sun.COM 	    NULL, DS_FIND_CHILDREN)) == 0) {
137511422SMark.Musante@Sun.COM 
137611422SMark.Musante@Sun.COM 		/*
137711422SMark.Musante@Sun.COM 		 * We successfully offlined the log device, sync out the
137811422SMark.Musante@Sun.COM 		 * current txg so that the "stubby" block can be removed
137911422SMark.Musante@Sun.COM 		 * by zil_sync().
138011422SMark.Musante@Sun.COM 		 */
138111422SMark.Musante@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, 0);
138211422SMark.Musante@Sun.COM 	}
138311422SMark.Musante@Sun.COM 	return (error);
138411422SMark.Musante@Sun.COM }
138511422SMark.Musante@Sun.COM 
138610672SEric.Schrock@Sun.COM static void
138710672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
138810672SEric.Schrock@Sun.COM {
138910922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < sav->sav_count; i++)
139010672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
139110672SEric.Schrock@Sun.COM }
139210672SEric.Schrock@Sun.COM 
139310922SJeff.Bonwick@Sun.COM void
139410922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio)
139510922SJeff.Bonwick@Sun.COM {
139610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
139710922SJeff.Bonwick@Sun.COM 
139810922SJeff.Bonwick@Sun.COM 	if (zio->io_error)
139910922SJeff.Bonwick@Sun.COM 		return;
140010922SJeff.Bonwick@Sun.COM 
140110922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
140210922SJeff.Bonwick@Sun.COM 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
140310922SJeff.Bonwick@Sun.COM 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
140410922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
140510922SJeff.Bonwick@Sun.COM }
140610922SJeff.Bonwick@Sun.COM 
140710921STim.Haley@Sun.COM typedef struct spa_load_error {
140811727SVictor.Latushkin@Sun.COM 	uint64_t	sle_meta_count;
140910921STim.Haley@Sun.COM 	uint64_t	sle_data_count;
141010921STim.Haley@Sun.COM } spa_load_error_t;
141110921STim.Haley@Sun.COM 
141210921STim.Haley@Sun.COM static void
141310921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio)
141410921STim.Haley@Sun.COM {
141510921STim.Haley@Sun.COM 	blkptr_t *bp = zio->io_bp;
141610921STim.Haley@Sun.COM 	spa_load_error_t *sle = zio->io_private;
141710921STim.Haley@Sun.COM 	dmu_object_type_t type = BP_GET_TYPE(bp);
141810921STim.Haley@Sun.COM 	int error = zio->io_error;
141910921STim.Haley@Sun.COM 
142010921STim.Haley@Sun.COM 	if (error) {
142110921STim.Haley@Sun.COM 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
142210921STim.Haley@Sun.COM 		    type != DMU_OT_INTENT_LOG)
142311727SVictor.Latushkin@Sun.COM 			atomic_add_64(&sle->sle_meta_count, 1);
142410921STim.Haley@Sun.COM 		else
142510921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_data_count, 1);
142610921STim.Haley@Sun.COM 	}
142710921STim.Haley@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
142810921STim.Haley@Sun.COM }
142910921STim.Haley@Sun.COM 
143010921STim.Haley@Sun.COM /*ARGSUSED*/
143110921STim.Haley@Sun.COM static int
143210922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1433*12296SLin.Ling@Sun.COM     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
143410921STim.Haley@Sun.COM {
143510921STim.Haley@Sun.COM 	if (bp != NULL) {
143610921STim.Haley@Sun.COM 		zio_t *rio = arg;
143710921STim.Haley@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
143810921STim.Haley@Sun.COM 		void *data = zio_data_buf_alloc(size);
143910921STim.Haley@Sun.COM 
144010921STim.Haley@Sun.COM 		zio_nowait(zio_read(rio, spa, bp, data, size,
144110921STim.Haley@Sun.COM 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
144210921STim.Haley@Sun.COM 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
144310921STim.Haley@Sun.COM 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
144410921STim.Haley@Sun.COM 	}
144510921STim.Haley@Sun.COM 	return (0);
144610921STim.Haley@Sun.COM }
144710921STim.Haley@Sun.COM 
144810921STim.Haley@Sun.COM static int
144910921STim.Haley@Sun.COM spa_load_verify(spa_t *spa)
145010921STim.Haley@Sun.COM {
145110921STim.Haley@Sun.COM 	zio_t *rio;
145210921STim.Haley@Sun.COM 	spa_load_error_t sle = { 0 };
145310921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
145410921STim.Haley@Sun.COM 	boolean_t verify_ok = B_FALSE;
145510921STim.Haley@Sun.COM 	int error;
145610921STim.Haley@Sun.COM 
145711727SVictor.Latushkin@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
145811727SVictor.Latushkin@Sun.COM 
145911727SVictor.Latushkin@Sun.COM 	if (policy.zrp_request & ZPOOL_NEVER_REWIND)
146011727SVictor.Latushkin@Sun.COM 		return (0);
146111727SVictor.Latushkin@Sun.COM 
146210921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
146310921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
146410921STim.Haley@Sun.COM 
146511125SJeff.Bonwick@Sun.COM 	error = traverse_pool(spa, spa->spa_verify_min_txg,
146611125SJeff.Bonwick@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
146710921STim.Haley@Sun.COM 
146810921STim.Haley@Sun.COM 	(void) zio_wait(rio);
146910921STim.Haley@Sun.COM 
147011727SVictor.Latushkin@Sun.COM 	spa->spa_load_meta_errors = sle.sle_meta_count;
147110921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
147210921STim.Haley@Sun.COM 
147311727SVictor.Latushkin@Sun.COM 	if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
147410921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
147510921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
147610921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
147710921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
147811026STim.Haley@Sun.COM 	} else {
147911026STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
148010921STim.Haley@Sun.COM 	}
148110921STim.Haley@Sun.COM 
148210921STim.Haley@Sun.COM 	if (error) {
148310921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
148410921STim.Haley@Sun.COM 			error = EIO;
148510921STim.Haley@Sun.COM 		return (error);
148610921STim.Haley@Sun.COM 	}
148710921STim.Haley@Sun.COM 
148810921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
148910921STim.Haley@Sun.COM }
149010921STim.Haley@Sun.COM 
14917294Sperrin /*
149211422SMark.Musante@Sun.COM  * Find a value in the pool props object.
149311422SMark.Musante@Sun.COM  */
149411422SMark.Musante@Sun.COM static void
149511422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
149611422SMark.Musante@Sun.COM {
149711422SMark.Musante@Sun.COM 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
149811422SMark.Musante@Sun.COM 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
149911422SMark.Musante@Sun.COM }
150011422SMark.Musante@Sun.COM 
150111422SMark.Musante@Sun.COM /*
150211422SMark.Musante@Sun.COM  * Find a value in the pool directory object.
150311422SMark.Musante@Sun.COM  */
150411422SMark.Musante@Sun.COM static int
150511422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
150611422SMark.Musante@Sun.COM {
150711422SMark.Musante@Sun.COM 	return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
150811422SMark.Musante@Sun.COM 	    name, sizeof (uint64_t), 1, val));
150911422SMark.Musante@Sun.COM }
151011422SMark.Musante@Sun.COM 
151111422SMark.Musante@Sun.COM static int
151211422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
151311422SMark.Musante@Sun.COM {
151411422SMark.Musante@Sun.COM 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
151511422SMark.Musante@Sun.COM 	return (err);
151611422SMark.Musante@Sun.COM }
151711422SMark.Musante@Sun.COM 
151811422SMark.Musante@Sun.COM /*
151911422SMark.Musante@Sun.COM  * Fix up config after a partly-completed split.  This is done with the
152011422SMark.Musante@Sun.COM  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
152111422SMark.Musante@Sun.COM  * pool have that entry in their config, but only the splitting one contains
152211422SMark.Musante@Sun.COM  * a list of all the guids of the vdevs that are being split off.
152311422SMark.Musante@Sun.COM  *
152411422SMark.Musante@Sun.COM  * This function determines what to do with that list: either rejoin
152511422SMark.Musante@Sun.COM  * all the disks to the pool, or complete the splitting process.  To attempt
152611422SMark.Musante@Sun.COM  * the rejoin, each disk that is offlined is marked online again, and
152711422SMark.Musante@Sun.COM  * we do a reopen() call.  If the vdev label for every disk that was
152811422SMark.Musante@Sun.COM  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
152911422SMark.Musante@Sun.COM  * then we call vdev_split() on each disk, and complete the split.
153011422SMark.Musante@Sun.COM  *
153111497SMark.Musante@Sun.COM  * Otherwise we leave the config alone, with all the vdevs in place in
153211497SMark.Musante@Sun.COM  * the original pool.
153311422SMark.Musante@Sun.COM  */
153411422SMark.Musante@Sun.COM static void
153511422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config)
153611422SMark.Musante@Sun.COM {
153711422SMark.Musante@Sun.COM 	uint_t extracted;
153811422SMark.Musante@Sun.COM 	uint64_t *glist;
153911422SMark.Musante@Sun.COM 	uint_t i, gcount;
154011422SMark.Musante@Sun.COM 	nvlist_t *nvl;
154111422SMark.Musante@Sun.COM 	vdev_t **vd;
154211422SMark.Musante@Sun.COM 	boolean_t attempt_reopen;
154311422SMark.Musante@Sun.COM 
154411422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
154511422SMark.Musante@Sun.COM 		return;
154611422SMark.Musante@Sun.COM 
154711422SMark.Musante@Sun.COM 	/* check that the config is complete */
154811422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
154911422SMark.Musante@Sun.COM 	    &glist, &gcount) != 0)
155011422SMark.Musante@Sun.COM 		return;
155111422SMark.Musante@Sun.COM 
155211422SMark.Musante@Sun.COM 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
155311422SMark.Musante@Sun.COM 
155411422SMark.Musante@Sun.COM 	/* attempt to online all the vdevs & validate */
155511422SMark.Musante@Sun.COM 	attempt_reopen = B_TRUE;
155611422SMark.Musante@Sun.COM 	for (i = 0; i < gcount; i++) {
155711422SMark.Musante@Sun.COM 		if (glist[i] == 0)	/* vdev is hole */
155811422SMark.Musante@Sun.COM 			continue;
155911422SMark.Musante@Sun.COM 
156011422SMark.Musante@Sun.COM 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
156111422SMark.Musante@Sun.COM 		if (vd[i] == NULL) {
156211422SMark.Musante@Sun.COM 			/*
156311422SMark.Musante@Sun.COM 			 * Don't bother attempting to reopen the disks;
156411422SMark.Musante@Sun.COM 			 * just do the split.
156511422SMark.Musante@Sun.COM 			 */
156611422SMark.Musante@Sun.COM 			attempt_reopen = B_FALSE;
156711422SMark.Musante@Sun.COM 		} else {
156811422SMark.Musante@Sun.COM 			/* attempt to re-online it */
156911422SMark.Musante@Sun.COM 			vd[i]->vdev_offline = B_FALSE;
157011422SMark.Musante@Sun.COM 		}
157111422SMark.Musante@Sun.COM 	}
157211422SMark.Musante@Sun.COM 
157311422SMark.Musante@Sun.COM 	if (attempt_reopen) {
157411422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
157511422SMark.Musante@Sun.COM 
157611422SMark.Musante@Sun.COM 		/* check each device to see what state it's in */
157711422SMark.Musante@Sun.COM 		for (extracted = 0, i = 0; i < gcount; i++) {
157811422SMark.Musante@Sun.COM 			if (vd[i] != NULL &&
157911422SMark.Musante@Sun.COM 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
158011422SMark.Musante@Sun.COM 				break;
158111422SMark.Musante@Sun.COM 			++extracted;
158211422SMark.Musante@Sun.COM 		}
158311422SMark.Musante@Sun.COM 	}
158411422SMark.Musante@Sun.COM 
158511422SMark.Musante@Sun.COM 	/*
158611422SMark.Musante@Sun.COM 	 * If every disk has been moved to the new pool, or if we never
158711422SMark.Musante@Sun.COM 	 * even attempted to look at them, then we split them off for
158811422SMark.Musante@Sun.COM 	 * good.
158911422SMark.Musante@Sun.COM 	 */
159011422SMark.Musante@Sun.COM 	if (!attempt_reopen || gcount == extracted) {
159111422SMark.Musante@Sun.COM 		for (i = 0; i < gcount; i++)
159211422SMark.Musante@Sun.COM 			if (vd[i] != NULL)
159311422SMark.Musante@Sun.COM 				vdev_split(vd[i]);
159411422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
159511422SMark.Musante@Sun.COM 	}
159611422SMark.Musante@Sun.COM 
159711422SMark.Musante@Sun.COM 	kmem_free(vd, gcount * sizeof (vdev_t *));
159811422SMark.Musante@Sun.COM }
159911422SMark.Musante@Sun.COM 
160011422SMark.Musante@Sun.COM static int
160111422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
160211422SMark.Musante@Sun.COM     boolean_t mosconfig)
160311422SMark.Musante@Sun.COM {
160411422SMark.Musante@Sun.COM 	nvlist_t *config = spa->spa_config;
160511422SMark.Musante@Sun.COM 	char *ereport = FM_EREPORT_ZFS_POOL;
160611422SMark.Musante@Sun.COM 	int error;
160711422SMark.Musante@Sun.COM 	uint64_t pool_guid;
160811422SMark.Musante@Sun.COM 	nvlist_t *nvl;
160911422SMark.Musante@Sun.COM 
161011422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
161111422SMark.Musante@Sun.COM 		return (EINVAL);
161211422SMark.Musante@Sun.COM 
161311422SMark.Musante@Sun.COM 	/*
161411422SMark.Musante@Sun.COM 	 * Versioning wasn't explicitly added to the label until later, so if
161511422SMark.Musante@Sun.COM 	 * it's not present treat it as the initial version.
161611422SMark.Musante@Sun.COM 	 */
161711422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
161811422SMark.Musante@Sun.COM 	    &spa->spa_ubsync.ub_version) != 0)
161911422SMark.Musante@Sun.COM 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
162011422SMark.Musante@Sun.COM 
162111422SMark.Musante@Sun.COM 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
162211422SMark.Musante@Sun.COM 	    &spa->spa_config_txg);
162311422SMark.Musante@Sun.COM 
162411422SMark.Musante@Sun.COM 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
162511422SMark.Musante@Sun.COM 	    spa_guid_exists(pool_guid, 0)) {
162611422SMark.Musante@Sun.COM 		error = EEXIST;
162711422SMark.Musante@Sun.COM 	} else {
162811422SMark.Musante@Sun.COM 		spa->spa_load_guid = pool_guid;
162911422SMark.Musante@Sun.COM 
163011422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
163111422SMark.Musante@Sun.COM 		    &nvl) == 0) {
163211422SMark.Musante@Sun.COM 			VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
163311422SMark.Musante@Sun.COM 			    KM_SLEEP) == 0);
163411422SMark.Musante@Sun.COM 		}
163511422SMark.Musante@Sun.COM 
163611422SMark.Musante@Sun.COM 		error = spa_load_impl(spa, pool_guid, config, state, type,
163711422SMark.Musante@Sun.COM 		    mosconfig, &ereport);
163811422SMark.Musante@Sun.COM 	}
163911422SMark.Musante@Sun.COM 
164011422SMark.Musante@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
164111422SMark.Musante@Sun.COM 	if (error && error != EBADF)
164211422SMark.Musante@Sun.COM 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
164311422SMark.Musante@Sun.COM 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
164411422SMark.Musante@Sun.COM 	spa->spa_ena = 0;
164511422SMark.Musante@Sun.COM 
164611422SMark.Musante@Sun.COM 	return (error);
164711422SMark.Musante@Sun.COM }
164811422SMark.Musante@Sun.COM 
164911422SMark.Musante@Sun.COM /*
1650789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
16511544Seschrock  * source of configuration information.
1652789Sahrens  */
1653789Sahrens static int
165411422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
165511422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
165611422SMark.Musante@Sun.COM     char **ereport)
1657789Sahrens {
1658789Sahrens 	int error = 0;
165911810SMark.Musante@Sun.COM 	nvlist_t *nvroot = NULL;
1660789Sahrens 	vdev_t *rvd;
1661789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
16621635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
16638241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
166411422SMark.Musante@Sun.COM 	int parse;
1665789Sahrens 
16668241SJeff.Bonwick@Sun.COM 	/*
16678241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
16688241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
16698241SJeff.Bonwick@Sun.COM 	 */
16708241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
16718241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
16728241SJeff.Bonwick@Sun.COM 
16737754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
16747754SJeff.Bonwick@Sun.COM 
16751544Seschrock 	spa->spa_load_state = state;
16761635Sbonwick 
167711422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
167811422SMark.Musante@Sun.COM 		return (EINVAL);
167911422SMark.Musante@Sun.COM 
168011422SMark.Musante@Sun.COM 	parse = (type == SPA_IMPORT_EXISTING ?
168111422SMark.Musante@Sun.COM 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
16822174Seschrock 
1683789Sahrens 	/*
16849234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
16859234SGeorge.Wilson@Sun.COM 	 */
16869630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
16879630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
16889234SGeorge.Wilson@Sun.COM 
16899234SGeorge.Wilson@Sun.COM 	/*
16902082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
16912082Seschrock 	 * value that will be returned by spa_version() since parsing the
16922082Seschrock 	 * configuration requires knowing the version number.
1693789Sahrens 	 */
16947754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
169511422SMark.Musante@Sun.COM 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
16967754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1697789Sahrens 
16982082Seschrock 	if (error != 0)
169911422SMark.Musante@Sun.COM 		return (error);
1700789Sahrens 
17011585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
170211422SMark.Musante@Sun.COM 
170311422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
170411422SMark.Musante@Sun.COM 		ASSERT(spa_guid(spa) == pool_guid);
170511422SMark.Musante@Sun.COM 	}
1706789Sahrens 
1707789Sahrens 	/*
1708789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1709789Sahrens 	 */
17107754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17114070Smc142369 	error = vdev_open(rvd);
17127754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
17134070Smc142369 	if (error != 0)
171411422SMark.Musante@Sun.COM 		return (error);
1715789Sahrens 
1716789Sahrens 	/*
17179276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
17189276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
17199276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
172011422SMark.Musante@Sun.COM 	 * that config.  Otherwise, we're validating against the cached config
17219276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
17229276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
17239276SMark.Musante@Sun.COM 	 * the vdev config.
172411422SMark.Musante@Sun.COM 	 *
172511422SMark.Musante@Sun.COM 	 * If we're assembling a new pool that's been split off from an
172611422SMark.Musante@Sun.COM 	 * existing pool, the labels haven't yet been updated so we skip
172711422SMark.Musante@Sun.COM 	 * validation for now.
17281986Seschrock 	 */
172911422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
173011422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
173111422SMark.Musante@Sun.COM 		error = vdev_validate(rvd);
173211422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
173311422SMark.Musante@Sun.COM 
173411422SMark.Musante@Sun.COM 		if (error != 0)
173511422SMark.Musante@Sun.COM 			return (error);
173611422SMark.Musante@Sun.COM 
173711422SMark.Musante@Sun.COM 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
173811422SMark.Musante@Sun.COM 			return (ENXIO);
17391986Seschrock 	}
17401986Seschrock 
17411986Seschrock 	/*
1742789Sahrens 	 * Find the best uberblock.
1743789Sahrens 	 */
17447754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1745789Sahrens 
1746789Sahrens 	/*
1747789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1748789Sahrens 	 */
174911422SMark.Musante@Sun.COM 	if (ub->ub_txg == 0)
175011422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
17511544Seschrock 
17521544Seschrock 	/*
17531544Seschrock 	 * If the pool is newer than the code, we can't open it.
17541544Seschrock 	 */
175511422SMark.Musante@Sun.COM 	if (ub->ub_version > SPA_VERSION)
175611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1757789Sahrens 
1758789Sahrens 	/*
1759789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1760789Sahrens 	 * incomplete configuration.
1761789Sahrens 	 */
176211422SMark.Musante@Sun.COM 	if (mosconfig && type != SPA_IMPORT_ASSEMBLE &&
176311422SMark.Musante@Sun.COM 	    rvd->vdev_guid_sum != ub->ub_guid_sum)
176411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
176511422SMark.Musante@Sun.COM 
176611422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
176711422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
176811422SMark.Musante@Sun.COM 		spa_try_repair(spa, config);
176911422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
177011422SMark.Musante@Sun.COM 		nvlist_free(spa->spa_config_splitting);
177111422SMark.Musante@Sun.COM 		spa->spa_config_splitting = NULL;
1772789Sahrens 	}
1773789Sahrens 
1774789Sahrens 	/*
1775789Sahrens 	 * Initialize internal SPA structures.
1776789Sahrens 	 */
1777789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1778789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
177910921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
178011727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
178110921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
178210921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
178310922SJeff.Bonwick@Sun.COM 	spa->spa_claim_max_txg = spa->spa_first_txg;
1784*12296SLin.Ling@Sun.COM 	spa->spa_prev_software_version = ub->ub_software_version;
178510922SJeff.Bonwick@Sun.COM 
17861544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
178711422SMark.Musante@Sun.COM 	if (error)
178811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1789789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1790789Sahrens 
179111422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
179211422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
179311422SMark.Musante@Sun.COM 
1794789Sahrens 	if (!mosconfig) {
17953975Sek110237 		uint64_t hostid;
179611810SMark.Musante@Sun.COM 		nvlist_t *policy = NULL, *nvconfig;
179711810SMark.Musante@Sun.COM 
179811810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
179911810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18002082Seschrock 
180110594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
18027706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
18033975Sek110237 			char *hostname;
18043975Sek110237 			unsigned long myhostid = 0;
18053975Sek110237 
180610594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
18073975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
18083975Sek110237 
18098662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
18108662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
18118662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
18128662SJordan.Vaughan@Sun.com 			/*
18138662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
18148662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
18158662SJordan.Vaughan@Sun.com 			 */
18163975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
18178662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
18184178Slling 			if (hostid != 0 && myhostid != 0 &&
18198662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
182011810SMark.Musante@Sun.COM 				nvlist_free(nvconfig);
18213975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
18223975Sek110237 				    "loaded as it was last accessed by "
18237706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
18243975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
18257754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
18263975Sek110237 				    (unsigned long)hostid);
182711422SMark.Musante@Sun.COM 				return (EBADF);
18283975Sek110237 			}
18293975Sek110237 		}
183011727SVictor.Latushkin@Sun.COM 		if (nvlist_lookup_nvlist(spa->spa_config,
183111727SVictor.Latushkin@Sun.COM 		    ZPOOL_REWIND_POLICY, &policy) == 0)
183211727SVictor.Latushkin@Sun.COM 			VERIFY(nvlist_add_nvlist(nvconfig,
183311727SVictor.Latushkin@Sun.COM 			    ZPOOL_REWIND_POLICY, policy) == 0);
18343975Sek110237 
183510594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1836789Sahrens 		spa_unload(spa);
1837789Sahrens 		spa_deactivate(spa);
18388241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1839789Sahrens 
184011422SMark.Musante@Sun.COM 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
18411544Seschrock 	}
18421544Seschrock 
184311422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPLIST,
184411422SMark.Musante@Sun.COM 	    &spa->spa_deferred_bplist_obj) != 0)
184511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1846789Sahrens 
18471544Seschrock 	/*
18482082Seschrock 	 * Load the bit that tells us to use the new accounting function
18492082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
18502082Seschrock 	 * be present.
18512082Seschrock 	 */
185211422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
185311422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18552082Seschrock 
1856*12296SLin.Ling@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
1857*12296SLin.Ling@Sun.COM 	    &spa->spa_creation_version);
1858*12296SLin.Ling@Sun.COM 	if (error != 0 && error != ENOENT)
1859*12296SLin.Ling@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1860*12296SLin.Ling@Sun.COM 
18612082Seschrock 	/*
18621544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
18631544Seschrock 	 * not be present.
18641544Seschrock 	 */
186511422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
186611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
186711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
186811422SMark.Musante@Sun.COM 
186911422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
187011422SMark.Musante@Sun.COM 	    &spa->spa_errlog_scrub);
187111422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
187211422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1873789Sahrens 
1874789Sahrens 	/*
18752926Sek110237 	 * Load the history object.  If we have an older pool, this
18762926Sek110237 	 * will not be present.
18772926Sek110237 	 */
187811422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
187911422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
188011422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
188111422SMark.Musante@Sun.COM 
188211422SMark.Musante@Sun.COM 	/*
188311422SMark.Musante@Sun.COM 	 * If we're assembling the pool from the split-off vdevs of
188411422SMark.Musante@Sun.COM 	 * an existing pool, we don't want to attach the spares & cache
188511422SMark.Musante@Sun.COM 	 * devices.
188611422SMark.Musante@Sun.COM 	 */
18872926Sek110237 
18882926Sek110237 	/*
18892082Seschrock 	 * Load any hot spares for this pool.
18902082Seschrock 	 */
189111422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
189211422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
189311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
189411422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
18954577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
18965450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
189711422SMark.Musante@Sun.COM 		    &spa->spa_spares.sav_config) != 0)
189811422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18992082Seschrock 
19007754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19012082Seschrock 		spa_load_spares(spa);
19027754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
190311422SMark.Musante@Sun.COM 	} else if (error == 0) {
190411422SMark.Musante@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
19052082Seschrock 	}
19062082Seschrock 
19075450Sbrendan 	/*
19085450Sbrendan 	 * Load any level 2 ARC devices for this pool.
19095450Sbrendan 	 */
191011422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
19115450Sbrendan 	    &spa->spa_l2cache.sav_object);
191211422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
191311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
191411422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19155450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
19165450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
191711422SMark.Musante@Sun.COM 		    &spa->spa_l2cache.sav_config) != 0)
191811422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19195450Sbrendan 
19207754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19215450Sbrendan 		spa_load_l2cache(spa);
19227754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
192311422SMark.Musante@Sun.COM 	} else if (error == 0) {
192411422SMark.Musante@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
19255450Sbrendan 	}
19265450Sbrendan 
19275094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19284543Smarks 
192911422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
193011422SMark.Musante@Sun.COM 	if (error && error != ENOENT)
193111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19323912Slling 
19333912Slling 	if (error == 0) {
193411422SMark.Musante@Sun.COM 		uint64_t autoreplace;
193511422SMark.Musante@Sun.COM 
193611422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
193711422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
193811422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
193911422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
194011422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
194111422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
194211422SMark.Musante@Sun.COM 		    &spa->spa_dedup_ditto);
194311422SMark.Musante@Sun.COM 
194410672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
19453912Slling 	}
19463912Slling 
19472082Seschrock 	/*
19484451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
19494451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
19504451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
19514451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
19524451Seschrock 	 * over.
19534451Seschrock 	 */
195410672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
19554451Seschrock 		spa_check_removed(spa->spa_root_vdev);
195610672SEric.Schrock@Sun.COM 		/*
195710672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
195810672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
195910672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
196010672SEric.Schrock@Sun.COM 		 */
196110672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
196210672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
196310672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
196410672SEric.Schrock@Sun.COM 		}
196510672SEric.Schrock@Sun.COM 	}
19664451Seschrock 
19674451Seschrock 	/*
19681986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1969789Sahrens 	 */
19701986Seschrock 	vdev_load(rvd);
1971789Sahrens 
1972789Sahrens 	/*
1973789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1974789Sahrens 	 */
19757754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1976789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
19777754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1978789Sahrens 
1979789Sahrens 	/*
1980789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1981789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1982789Sahrens 	 */
198311422SMark.Musante@Sun.COM 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
198411422SMark.Musante@Sun.COM 		return (ENXIO);
1985789Sahrens 
198610922SJeff.Bonwick@Sun.COM 	/*
198710922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
198810922SJeff.Bonwick@Sun.COM 	 */
198910922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
199011422SMark.Musante@Sun.COM 	if (error != 0)
199111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
199210922SJeff.Bonwick@Sun.COM 
199310956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
199410956SGeorge.Wilson@Sun.COM 
199510921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
199610921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
199711422SMark.Musante@Sun.COM 		if (error)
199811422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
199911422SMark.Musante@Sun.COM 			    error));
200010921STim.Haley@Sun.COM 	}
200110921STim.Haley@Sun.COM 
200210922SJeff.Bonwick@Sun.COM 	/*
200311422SMark.Musante@Sun.COM 	 * Load the intent log state and check log integrity.  If we're
200411422SMark.Musante@Sun.COM 	 * assembling a pool from a split, the log is not transferred over.
200510922SJeff.Bonwick@Sun.COM 	 */
200611422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
200711810SMark.Musante@Sun.COM 		nvlist_t *nvconfig;
200811810SMark.Musante@Sun.COM 
200911810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
201011810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
201111810SMark.Musante@Sun.COM 
201211422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
201311422SMark.Musante@Sun.COM 		    &nvroot) == 0);
201411422SMark.Musante@Sun.COM 		spa_load_log_state(spa, nvroot);
201511422SMark.Musante@Sun.COM 		nvlist_free(nvconfig);
201611422SMark.Musante@Sun.COM 
201711422SMark.Musante@Sun.COM 		if (spa_check_logs(spa)) {
201811422SMark.Musante@Sun.COM 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
201911422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
202011422SMark.Musante@Sun.COM 		}
202110922SJeff.Bonwick@Sun.COM 	}
202210922SJeff.Bonwick@Sun.COM 
202310921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
202410921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
20251635Sbonwick 		dmu_tx_t *tx;
20261635Sbonwick 		int need_update = B_FALSE;
20278241SJeff.Bonwick@Sun.COM 
20288241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
20291601Sbonwick 
20301635Sbonwick 		/*
20311635Sbonwick 		 * Claim log blocks that haven't been committed yet.
20321635Sbonwick 		 * This must all happen in a single txg.
203310922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
203410922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
203510921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
20361635Sbonwick 		 */
203710922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
203810922SJeff.Bonwick@Sun.COM 
20391601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2040789Sahrens 		    spa_first_txg(spa));
20417754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
20422417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2043789Sahrens 		dmu_tx_commit(tx);
2044789Sahrens 
204510922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
204610922SJeff.Bonwick@Sun.COM 
204711422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_GOOD);
2048789Sahrens 		spa->spa_sync_on = B_TRUE;
2049789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2050789Sahrens 
2051789Sahrens 		/*
205210922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
205310922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
205410922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
205510922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
205610922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
2057789Sahrens 		 */
205810922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
20591585Sbonwick 
20601585Sbonwick 		/*
20611635Sbonwick 		 * If the config cache is stale, or we have uninitialized
20621635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
206310100SLin.Ling@Sun.COM 		 *
206410100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
206510100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
20661585Sbonwick 		 */
20671635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
206810921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
206910921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
20701635Sbonwick 			need_update = B_TRUE;
20711635Sbonwick 
20728241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
20731635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
20741635Sbonwick 				need_update = B_TRUE;
20751585Sbonwick 
20761585Sbonwick 		/*
20771635Sbonwick 		 * Update the config cache asychronously in case we're the
20781635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
20791585Sbonwick 		 */
20801635Sbonwick 		if (need_update)
20811635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20828241SJeff.Bonwick@Sun.COM 
20838241SJeff.Bonwick@Sun.COM 		/*
20848241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
20858241SJeff.Bonwick@Sun.COM 		 */
2086*12296SLin.Ling@Sun.COM 		if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2087*12296SLin.Ling@Sun.COM 		    vdev_resilver_needed(rvd, NULL, NULL))
20888241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
208910298SMatthew.Ahrens@Sun.COM 
209010298SMatthew.Ahrens@Sun.COM 		/*
209110298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
209210298SMatthew.Ahrens@Sun.COM 		 */
209310298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
209410298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
209510342Schris.kirby@sun.com 
209610342Schris.kirby@sun.com 		/*
209710342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
209810342Schris.kirby@sun.com 		 */
209910342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2100789Sahrens 	}
2101789Sahrens 
210211422SMark.Musante@Sun.COM 	return (0);
2103789Sahrens }
2104789Sahrens 
210510921STim.Haley@Sun.COM static int
210610921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
210710921STim.Haley@Sun.COM {
210810921STim.Haley@Sun.COM 	spa_unload(spa);
210910921STim.Haley@Sun.COM 	spa_deactivate(spa);
211010921STim.Haley@Sun.COM 
211110921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
211210921STim.Haley@Sun.COM 
211310921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
211410921STim.Haley@Sun.COM 	spa_async_suspend(spa);
211510921STim.Haley@Sun.COM 
211611422SMark.Musante@Sun.COM 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
211710921STim.Haley@Sun.COM }
211810921STim.Haley@Sun.COM 
211910921STim.Haley@Sun.COM static int
212010921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
212111727SVictor.Latushkin@Sun.COM     uint64_t max_request, int rewind_flags)
212210921STim.Haley@Sun.COM {
212310921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
212410921STim.Haley@Sun.COM 	int load_error, rewind_error;
212511727SVictor.Latushkin@Sun.COM 	uint64_t safe_rewind_txg;
212610921STim.Haley@Sun.COM 	uint64_t min_txg;
212710921STim.Haley@Sun.COM 
212811026STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
212910921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
213011422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
213111026STim.Haley@Sun.COM 	} else {
213210921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
213311026STim.Haley@Sun.COM 	}
213410921STim.Haley@Sun.COM 
213511422SMark.Musante@Sun.COM 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
213611422SMark.Musante@Sun.COM 	    mosconfig);
213710921STim.Haley@Sun.COM 	if (load_error == 0)
213810921STim.Haley@Sun.COM 		return (0);
213910921STim.Haley@Sun.COM 
214010921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
214110921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
214210921STim.Haley@Sun.COM 
214310921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
214410921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
214510921STim.Haley@Sun.COM 
214611727SVictor.Latushkin@Sun.COM 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
214710921STim.Haley@Sun.COM 		nvlist_free(config);
214810921STim.Haley@Sun.COM 		return (load_error);
214910921STim.Haley@Sun.COM 	}
215010921STim.Haley@Sun.COM 
215110921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
215210921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
215311422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
215410921STim.Haley@Sun.COM 
215511727SVictor.Latushkin@Sun.COM 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
215611727SVictor.Latushkin@Sun.COM 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
215711727SVictor.Latushkin@Sun.COM 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
215811727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL : safe_rewind_txg;
215911727SVictor.Latushkin@Sun.COM 
216011727SVictor.Latushkin@Sun.COM 	/*
216111727SVictor.Latushkin@Sun.COM 	 * Continue as long as we're finding errors, we're still within
216211727SVictor.Latushkin@Sun.COM 	 * the acceptable rewind range, and we're still finding uberblocks
216311727SVictor.Latushkin@Sun.COM 	 */
216411727SVictor.Latushkin@Sun.COM 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
216511727SVictor.Latushkin@Sun.COM 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
216611727SVictor.Latushkin@Sun.COM 		if (spa->spa_load_max_txg < safe_rewind_txg)
216710921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
216810921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
216910921STim.Haley@Sun.COM 	}
217010921STim.Haley@Sun.COM 
217110921STim.Haley@Sun.COM 	if (config)
217210921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
217310921STim.Haley@Sun.COM 
217410921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
217510921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
217610921STim.Haley@Sun.COM 
217710921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
217810921STim.Haley@Sun.COM 		spa_config_set(spa, config);
217910921STim.Haley@Sun.COM 
218010921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
218110921STim.Haley@Sun.COM }
218210921STim.Haley@Sun.COM 
2183789Sahrens /*
2184789Sahrens  * Pool Open/Import
2185789Sahrens  *
2186789Sahrens  * The import case is identical to an open except that the configuration is sent
2187789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2188789Sahrens  * case of an open, the pool configuration will exist in the
21894451Seschrock  * POOL_STATE_UNINITIALIZED state.
2190789Sahrens  *
2191789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2192789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2193789Sahrens  * ambiguous state.
2194789Sahrens  */
2195789Sahrens static int
219610921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
219710921STim.Haley@Sun.COM     nvlist_t **config)
2198789Sahrens {
2199789Sahrens 	spa_t *spa;
2200789Sahrens 	int error;
2201789Sahrens 	int locked = B_FALSE;
2202789Sahrens 
2203789Sahrens 	*spapp = NULL;
2204789Sahrens 
2205789Sahrens 	/*
2206789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2207789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2208789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2209789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2210789Sahrens 	 */
2211789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2212789Sahrens 		mutex_enter(&spa_namespace_lock);
2213789Sahrens 		locked = B_TRUE;
2214789Sahrens 	}
2215789Sahrens 
2216789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2217789Sahrens 		if (locked)
2218789Sahrens 			mutex_exit(&spa_namespace_lock);
2219789Sahrens 		return (ENOENT);
2220789Sahrens 	}
222110921STim.Haley@Sun.COM 
2222789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
222311819STim.Haley@Sun.COM 		spa_load_state_t state = SPA_LOAD_OPEN;
222411819STim.Haley@Sun.COM 		zpool_rewind_policy_t policy;
222511819STim.Haley@Sun.COM 
222611819STim.Haley@Sun.COM 		zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
222711819STim.Haley@Sun.COM 		    &policy);
222811819STim.Haley@Sun.COM 		if (policy.zrp_request & ZPOOL_DO_REWIND)
222911819STim.Haley@Sun.COM 			state = SPA_LOAD_RECOVER;
2230789Sahrens 
22318241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
2232789Sahrens 
223311727SVictor.Latushkin@Sun.COM 		if (spa->spa_last_open_failed && (policy.zrp_request &
223411727SVictor.Latushkin@Sun.COM 		    (ZPOOL_NO_REWIND | ZPOOL_NEVER_REWIND))) {
223510921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
223610921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config,
223710921STim.Haley@Sun.COM 				    config, KM_SLEEP) == 0);
223810921STim.Haley@Sun.COM 			spa_deactivate(spa);
223910921STim.Haley@Sun.COM 			if (locked)
224010921STim.Haley@Sun.COM 				mutex_exit(&spa_namespace_lock);
224110921STim.Haley@Sun.COM 			return (spa->spa_last_open_failed);
224210921STim.Haley@Sun.COM 		}
224310921STim.Haley@Sun.COM 
224410921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
224510921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
224610921STim.Haley@Sun.COM 
224710921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
224811727SVictor.Latushkin@Sun.COM 		    policy.zrp_request);
2249789Sahrens 
2250789Sahrens 		if (error == EBADF) {
2251789Sahrens 			/*
22521986Seschrock 			 * If vdev_validate() returns failure (indicated by
22531986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
22541986Seschrock 			 * that the pool has been exported or destroyed.  If
22551986Seschrock 			 * this is the case, the config cache is out of sync and
22561986Seschrock 			 * we should remove the pool from the namespace.
2257789Sahrens 			 */
2258789Sahrens 			spa_unload(spa);
2259789Sahrens 			spa_deactivate(spa);
22606643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2261789Sahrens 			spa_remove(spa);
2262789Sahrens 			if (locked)
2263789Sahrens 				mutex_exit(&spa_namespace_lock);
2264789Sahrens 			return (ENOENT);
22651544Seschrock 		}
22661544Seschrock 
22671544Seschrock 		if (error) {
2268789Sahrens 			/*
2269789Sahrens 			 * We can't open the pool, but we still have useful
2270789Sahrens 			 * information: the state of each vdev after the
2271789Sahrens 			 * attempted vdev_open().  Return this to the user.
2272789Sahrens 			 */
227310921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
227410921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
227510921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
2276789Sahrens 			spa_unload(spa);
2277789Sahrens 			spa_deactivate(spa);
227810921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
2279789Sahrens 			if (locked)
2280789Sahrens 				mutex_exit(&spa_namespace_lock);
2281789Sahrens 			*spapp = NULL;
2282789Sahrens 			return (error);
2283789Sahrens 		}
228410921STim.Haley@Sun.COM 
2285789Sahrens 	}
2286789Sahrens 
2287789Sahrens 	spa_open_ref(spa, tag);
22884451Seschrock 
228910921STim.Haley@Sun.COM 
229010921STim.Haley@Sun.COM 	if (config != NULL)
229110921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
229210921STim.Haley@Sun.COM 
229311026STim.Haley@Sun.COM 	if (locked) {
229411026STim.Haley@Sun.COM 		spa->spa_last_open_failed = 0;
229511026STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = 0;
229611026STim.Haley@Sun.COM 		spa->spa_load_txg = 0;
2297789Sahrens 		mutex_exit(&spa_namespace_lock);
229811026STim.Haley@Sun.COM 	}
2299789Sahrens 
2300789Sahrens 	*spapp = spa;
2301789Sahrens 
2302789Sahrens 	return (0);
2303789Sahrens }
2304789Sahrens 
2305789Sahrens int
230610921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
230710921STim.Haley@Sun.COM     nvlist_t **config)
230810921STim.Haley@Sun.COM {
230910921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
231010921STim.Haley@Sun.COM }
231110921STim.Haley@Sun.COM 
231210921STim.Haley@Sun.COM int
2313789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2314789Sahrens {
231510921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2316789Sahrens }
2317789Sahrens 
23181544Seschrock /*
23191544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
23201544Seschrock  * preventing it from being exported or destroyed.
23211544Seschrock  */
23221544Seschrock spa_t *
23231544Seschrock spa_inject_addref(char *name)
23241544Seschrock {
23251544Seschrock 	spa_t *spa;
23261544Seschrock 
23271544Seschrock 	mutex_enter(&spa_namespace_lock);
23281544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
23291544Seschrock 		mutex_exit(&spa_namespace_lock);
23301544Seschrock 		return (NULL);
23311544Seschrock 	}
23321544Seschrock 	spa->spa_inject_ref++;
23331544Seschrock 	mutex_exit(&spa_namespace_lock);
23341544Seschrock 
23351544Seschrock 	return (spa);
23361544Seschrock }
23371544Seschrock 
23381544Seschrock void
23391544Seschrock spa_inject_delref(spa_t *spa)
23401544Seschrock {
23411544Seschrock 	mutex_enter(&spa_namespace_lock);
23421544Seschrock 	spa->spa_inject_ref--;
23431544Seschrock 	mutex_exit(&spa_namespace_lock);
23441544Seschrock }
23451544Seschrock 
23465450Sbrendan /*
23475450Sbrendan  * Add spares device information to the nvlist.
23485450Sbrendan  */
23492082Seschrock static void
23502082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
23512082Seschrock {
23522082Seschrock 	nvlist_t **spares;
23532082Seschrock 	uint_t i, nspares;
23542082Seschrock 	nvlist_t *nvroot;
23552082Seschrock 	uint64_t guid;
23562082Seschrock 	vdev_stat_t *vs;
23572082Seschrock 	uint_t vsc;
23583377Seschrock 	uint64_t pool;
23592082Seschrock 
23609425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23619425SEric.Schrock@Sun.COM 
23625450Sbrendan 	if (spa->spa_spares.sav_count == 0)
23632082Seschrock 		return;
23642082Seschrock 
23652082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
23662082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23675450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
23682082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23692082Seschrock 	if (nspares != 0) {
23702082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
23712082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
23722082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23732082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23742082Seschrock 
23752082Seschrock 		/*
23762082Seschrock 		 * Go through and find any spares which have since been
23772082Seschrock 		 * repurposed as an active spare.  If this is the case, update
23782082Seschrock 		 * their status appropriately.
23792082Seschrock 		 */
23802082Seschrock 		for (i = 0; i < nspares; i++) {
23812082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
23822082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23837214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
23847214Slling 			    pool != 0ULL) {
23852082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
2386*12296SLin.Ling@Sun.COM 				    spares[i], ZPOOL_CONFIG_VDEV_STATS,
23872082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
23882082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
23892082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
23902082Seschrock 			}
23912082Seschrock 		}
23922082Seschrock 	}
23932082Seschrock }
23942082Seschrock 
23955450Sbrendan /*
23965450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
23975450Sbrendan  */
23985450Sbrendan static void
23995450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
24005450Sbrendan {
24015450Sbrendan 	nvlist_t **l2cache;
24025450Sbrendan 	uint_t i, j, nl2cache;
24035450Sbrendan 	nvlist_t *nvroot;
24045450Sbrendan 	uint64_t guid;
24055450Sbrendan 	vdev_t *vd;
24065450Sbrendan 	vdev_stat_t *vs;
24075450Sbrendan 	uint_t vsc;
24085450Sbrendan 
24099425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
24109425SEric.Schrock@Sun.COM 
24115450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
24125450Sbrendan 		return;
24135450Sbrendan 
24145450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
24155450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
24165450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
24175450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24185450Sbrendan 	if (nl2cache != 0) {
24195450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
24205450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
24215450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
24225450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24235450Sbrendan 
24245450Sbrendan 		/*
24255450Sbrendan 		 * Update level 2 cache device stats.
24265450Sbrendan 		 */
24275450Sbrendan 
24285450Sbrendan 		for (i = 0; i < nl2cache; i++) {
24295450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
24305450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
24315450Sbrendan 
24325450Sbrendan 			vd = NULL;
24335450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
24345450Sbrendan 				if (guid ==
24355450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
24365450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
24375450Sbrendan 					break;
24385450Sbrendan 				}
24395450Sbrendan 			}
24405450Sbrendan 			ASSERT(vd != NULL);
24415450Sbrendan 
24425450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2443*12296SLin.Ling@Sun.COM 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2444*12296SLin.Ling@Sun.COM 			    == 0);
24455450Sbrendan 			vdev_get_stats(vd, vs);
24465450Sbrendan 		}
24475450Sbrendan 	}
24485450Sbrendan }
24495450Sbrendan 
2450789Sahrens int
24511544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2452789Sahrens {
2453789Sahrens 	int error;
2454789Sahrens 	spa_t *spa;
2455789Sahrens 
2456789Sahrens 	*config = NULL;
245710921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2458789Sahrens 
24599425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24609425SEric.Schrock@Sun.COM 		/*
24619425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
24629425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
24639425SEric.Schrock@Sun.COM 		 * self-inconsistent.
24649425SEric.Schrock@Sun.COM 		 */
24659425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
24669425SEric.Schrock@Sun.COM 
24679425SEric.Schrock@Sun.COM 		if (*config != NULL) {
24687754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
24699425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
24709425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
24719425SEric.Schrock@Sun.COM 
24729425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
24739425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
24749425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
24759425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
24769425SEric.Schrock@Sun.COM 
24779425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
24789425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
24799425SEric.Schrock@Sun.COM 		}
24802082Seschrock 	}
24812082Seschrock 
24821544Seschrock 	/*
24831544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
24841544Seschrock 	 * and call spa_lookup() directly.
24851544Seschrock 	 */
24861544Seschrock 	if (altroot) {
24871544Seschrock 		if (spa == NULL) {
24881544Seschrock 			mutex_enter(&spa_namespace_lock);
24891544Seschrock 			spa = spa_lookup(name);
24901544Seschrock 			if (spa)
24911544Seschrock 				spa_altroot(spa, altroot, buflen);
24921544Seschrock 			else
24931544Seschrock 				altroot[0] = '\0';
24941544Seschrock 			spa = NULL;
24951544Seschrock 			mutex_exit(&spa_namespace_lock);
24961544Seschrock 		} else {
24971544Seschrock 			spa_altroot(spa, altroot, buflen);
24981544Seschrock 		}
24991544Seschrock 	}
25001544Seschrock 
25019425SEric.Schrock@Sun.COM 	if (spa != NULL) {
25029425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2503789Sahrens 		spa_close(spa, FTAG);
25049425SEric.Schrock@Sun.COM 	}
2505789Sahrens 
2506789Sahrens 	return (error);
2507789Sahrens }
2508789Sahrens 
2509789Sahrens /*
25105450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
25115450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
25125450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
25135450Sbrendan  * specified, as long as they are well-formed.
25142082Seschrock  */
25152082Seschrock static int
25165450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
25175450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
25185450Sbrendan     vdev_labeltype_t label)
25192082Seschrock {
25205450Sbrendan 	nvlist_t **dev;
25215450Sbrendan 	uint_t i, ndev;
25222082Seschrock 	vdev_t *vd;
25232082Seschrock 	int error;
25242082Seschrock 
25257754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25267754SJeff.Bonwick@Sun.COM 
25272082Seschrock 	/*
25285450Sbrendan 	 * It's acceptable to have no devs specified.
25292082Seschrock 	 */
25305450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
25312082Seschrock 		return (0);
25322082Seschrock 
25335450Sbrendan 	if (ndev == 0)
25342082Seschrock 		return (EINVAL);
25352082Seschrock 
25362082Seschrock 	/*
25375450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
25385450Sbrendan 	 * device type.
25392082Seschrock 	 */
25405450Sbrendan 	if (spa_version(spa) < version)
25412082Seschrock 		return (ENOTSUP);
25422082Seschrock 
25433377Seschrock 	/*
25445450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
25453377Seschrock 	 * checking.
25463377Seschrock 	 */
25475450Sbrendan 	sav->sav_pending = dev;
25485450Sbrendan 	sav->sav_npending = ndev;
25495450Sbrendan 
25505450Sbrendan 	for (i = 0; i < ndev; i++) {
25515450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
25522082Seschrock 		    mode)) != 0)
25533377Seschrock 			goto out;
25542082Seschrock 
25552082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
25562082Seschrock 			vdev_free(vd);
25573377Seschrock 			error = EINVAL;
25583377Seschrock 			goto out;
25592082Seschrock 		}
25602082Seschrock 
25615450Sbrendan 		/*
25627754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
25637754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
25645450Sbrendan 		 */
25657754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
25665450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
25675450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
25685450Sbrendan 			error = ENOTBLK;
25695450Sbrendan 			goto out;
25705450Sbrendan 		}
25717754SJeff.Bonwick@Sun.COM #endif
25722082Seschrock 		vd->vdev_top = vd;
25733377Seschrock 
25743377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
25755450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
25765450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
25773377Seschrock 			    vd->vdev_guid) == 0);
25782082Seschrock 		}
25792082Seschrock 
25802082Seschrock 		vdev_free(vd);
25813377Seschrock 
25825450Sbrendan 		if (error &&
25835450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
25843377Seschrock 			goto out;
25853377Seschrock 		else
25863377Seschrock 			error = 0;
25872082Seschrock 	}
25882082Seschrock 
25893377Seschrock out:
25905450Sbrendan 	sav->sav_pending = NULL;
25915450Sbrendan 	sav->sav_npending = 0;
25923377Seschrock 	return (error);
25932082Seschrock }
25942082Seschrock 
25955450Sbrendan static int
25965450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
25975450Sbrendan {
25985450Sbrendan 	int error;
25995450Sbrendan 
26007754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
26017754SJeff.Bonwick@Sun.COM 
26025450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
26035450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
26045450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
26055450Sbrendan 		return (error);
26065450Sbrendan 	}
26075450Sbrendan 
26085450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
26095450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
26105450Sbrendan 	    VDEV_LABEL_L2CACHE));
26115450Sbrendan }
26125450Sbrendan 
26135450Sbrendan static void
26145450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
26155450Sbrendan     const char *config)
26165450Sbrendan {
26175450Sbrendan 	int i;
26185450Sbrendan 
26195450Sbrendan 	if (sav->sav_config != NULL) {
26205450Sbrendan 		nvlist_t **olddevs;
26215450Sbrendan 		uint_t oldndevs;
26225450Sbrendan 		nvlist_t **newdevs;
26235450Sbrendan 
26245450Sbrendan 		/*
26255450Sbrendan 		 * Generate new dev list by concatentating with the
26265450Sbrendan 		 * current dev list.
26275450Sbrendan 		 */
26285450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
26295450Sbrendan 		    &olddevs, &oldndevs) == 0);
26305450Sbrendan 
26315450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
26325450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
26335450Sbrendan 		for (i = 0; i < oldndevs; i++)
26345450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
26355450Sbrendan 			    KM_SLEEP) == 0);
26365450Sbrendan 		for (i = 0; i < ndevs; i++)
26375450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
26385450Sbrendan 			    KM_SLEEP) == 0);
26395450Sbrendan 
26405450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
26415450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
26425450Sbrendan 
26435450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
26445450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
26455450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
26465450Sbrendan 			nvlist_free(newdevs[i]);
26475450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
26485450Sbrendan 	} else {
26495450Sbrendan 		/*
26505450Sbrendan 		 * Generate a new dev list.
26515450Sbrendan 		 */
26525450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
26535450Sbrendan 		    KM_SLEEP) == 0);
26545450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
26555450Sbrendan 		    devs, ndevs) == 0);
26565450Sbrendan 	}
26575450Sbrendan }
26585450Sbrendan 
26595450Sbrendan /*
26605450Sbrendan  * Stop and drop level 2 ARC devices
26615450Sbrendan  */
26625450Sbrendan void
26635450Sbrendan spa_l2cache_drop(spa_t *spa)
26645450Sbrendan {
26655450Sbrendan 	vdev_t *vd;
26665450Sbrendan 	int i;
26675450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
26685450Sbrendan 
26695450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
26705450Sbrendan 		uint64_t pool;
26715450Sbrendan 
26725450Sbrendan 		vd = sav->sav_vdevs[i];
26735450Sbrendan 		ASSERT(vd != NULL);
26745450Sbrendan 
26758241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
26768241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
26775450Sbrendan 			l2arc_remove_vdev(vd);
26785450Sbrendan 		if (vd->vdev_isl2cache)
26795450Sbrendan 			spa_l2cache_remove(vd);
26805450Sbrendan 		vdev_clear_stats(vd);
26815450Sbrendan 		(void) vdev_close(vd);
26825450Sbrendan 	}
26835450Sbrendan }
26845450Sbrendan 
26852082Seschrock /*
2686789Sahrens  * Pool Creation
2687789Sahrens  */
2688789Sahrens int
26895094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
26907184Stimh     const char *history_str, nvlist_t *zplprops)
2691789Sahrens {
2692789Sahrens 	spa_t *spa;
26935094Slling 	char *altroot = NULL;
26941635Sbonwick 	vdev_t *rvd;
2695789Sahrens 	dsl_pool_t *dp;
2696789Sahrens 	dmu_tx_t *tx;
26979816SGeorge.Wilson@Sun.COM 	int error = 0;
2698789Sahrens 	uint64_t txg = TXG_INITIAL;
26995450Sbrendan 	nvlist_t **spares, **l2cache;
27005450Sbrendan 	uint_t nspares, nl2cache;
27015094Slling 	uint64_t version;
2702789Sahrens 
2703789Sahrens 	/*
2704789Sahrens 	 * If this pool already exists, return failure.
2705789Sahrens 	 */
2706789Sahrens 	mutex_enter(&spa_namespace_lock);
2707789Sahrens 	if (spa_lookup(pool) != NULL) {
2708789Sahrens 		mutex_exit(&spa_namespace_lock);
2709789Sahrens 		return (EEXIST);
2710789Sahrens 	}
2711789Sahrens 
2712789Sahrens 	/*
2713789Sahrens 	 * Allocate a new spa_t structure.
2714789Sahrens 	 */
27155094Slling 	(void) nvlist_lookup_string(props,
27165094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
271710921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
27188241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2719789Sahrens 
27205094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
27215094Slling 		spa_deactivate(spa);
27225094Slling 		spa_remove(spa);
27236643Seschrock 		mutex_exit(&spa_namespace_lock);
27245094Slling 		return (error);
27255094Slling 	}
27265094Slling 
27275094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
27285094Slling 	    &version) != 0)
27295094Slling 		version = SPA_VERSION;
27305094Slling 	ASSERT(version <= SPA_VERSION);
273110922SJeff.Bonwick@Sun.COM 
273210922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
273310922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
27345094Slling 	spa->spa_uberblock.ub_version = version;
2735789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2736789Sahrens 
27371635Sbonwick 	/*
27389234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
27399234SGeorge.Wilson@Sun.COM 	 */
27409630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
27419630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
27429234SGeorge.Wilson@Sun.COM 
27439234SGeorge.Wilson@Sun.COM 	/*
27441635Sbonwick 	 * Create the root vdev.
27451635Sbonwick 	 */
27467754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27471635Sbonwick 
27482082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
27492082Seschrock 
27502082Seschrock 	ASSERT(error != 0 || rvd != NULL);
27512082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
27522082Seschrock 
27535913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
27541635Sbonwick 		error = EINVAL;
27552082Seschrock 
27562082Seschrock 	if (error == 0 &&
27572082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
27585450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
27592082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
27609816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
27619816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
27629816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
27639816SGeorge.Wilson@Sun.COM 		}
27641635Sbonwick 	}
27651635Sbonwick 
27667754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2767789Sahrens 
27682082Seschrock 	if (error != 0) {
2769789Sahrens 		spa_unload(spa);
2770789Sahrens 		spa_deactivate(spa);
2771789Sahrens 		spa_remove(spa);
2772789Sahrens 		mutex_exit(&spa_namespace_lock);
2773789Sahrens 		return (error);
2774789Sahrens 	}
2775789Sahrens 
27762082Seschrock 	/*
27772082Seschrock 	 * Get the list of spares, if specified.
27782082Seschrock 	 */
27792082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
27802082Seschrock 	    &spares, &nspares) == 0) {
27815450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
27822082Seschrock 		    KM_SLEEP) == 0);
27835450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
27842082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
27857754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27862082Seschrock 		spa_load_spares(spa);
27877754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27885450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27895450Sbrendan 	}
27905450Sbrendan 
27915450Sbrendan 	/*
27925450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
27935450Sbrendan 	 */
27945450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
27955450Sbrendan 	    &l2cache, &nl2cache) == 0) {
27965450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
27975450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
27985450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
27995450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
28007754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28015450Sbrendan 		spa_load_l2cache(spa);
28027754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
28035450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
28042082Seschrock 	}
28052082Seschrock 
28067184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2807789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2808789Sahrens 
280910956SGeorge.Wilson@Sun.COM 	/*
281010956SGeorge.Wilson@Sun.COM 	 * Create DDTs (dedup tables).
281110956SGeorge.Wilson@Sun.COM 	 */
281210956SGeorge.Wilson@Sun.COM 	ddt_create(spa);
281310956SGeorge.Wilson@Sun.COM 
281410956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
281510956SGeorge.Wilson@Sun.COM 
2816789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2817789Sahrens 
2818789Sahrens 	/*
2819789Sahrens 	 * Create the pool config object.
2820789Sahrens 	 */
2821789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
28227497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2823789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2824789Sahrens 
28251544Seschrock 	if (zap_add(spa->spa_meta_objset,
2826789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
28271544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
28281544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
28291544Seschrock 	}
2830789Sahrens 
2831*12296SLin.Ling@Sun.COM 	if (zap_add(spa->spa_meta_objset,
2832*12296SLin.Ling@Sun.COM 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
2833*12296SLin.Ling@Sun.COM 	    sizeof (uint64_t), 1, &version, tx) != 0) {
2834*12296SLin.Ling@Sun.COM 		cmn_err(CE_PANIC, "failed to add pool version");
2835*12296SLin.Ling@Sun.COM 	}
2836*12296SLin.Ling@Sun.COM 
28375094Slling 	/* Newly created pools with the right version are always deflated. */
28385094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
28395094Slling 		spa->spa_deflate = TRUE;
28405094Slling 		if (zap_add(spa->spa_meta_objset,
28415094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
28425094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
28435094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
28445094Slling 		}
28452082Seschrock 	}
28462082Seschrock 
2847789Sahrens 	/*
2848789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2849789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2850789Sahrens 	 * keeps changing.
2851789Sahrens 	 */
285210922SJeff.Bonwick@Sun.COM 	spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset,
2853789Sahrens 	    1 << 14, tx);
285410922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset,
285510922SJeff.Bonwick@Sun.COM 	    spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx);
2856789Sahrens 
28571544Seschrock 	if (zap_add(spa->spa_meta_objset,
2858789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
285910922SJeff.Bonwick@Sun.COM 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) {
28601544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
28611544Seschrock 	}
2862789Sahrens 
28632926Sek110237 	/*
28642926Sek110237 	 * Create the pool's history object.
28652926Sek110237 	 */
28665094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
28675094Slling 		spa_history_create_obj(spa, tx);
28685094Slling 
28695094Slling 	/*
28705094Slling 	 * Set pool properties.
28715094Slling 	 */
28725094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
28735094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
28745329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
28759816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
287610922SJeff.Bonwick@Sun.COM 
28778525SEric.Schrock@Sun.COM 	if (props != NULL) {
28788525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
2879*12296SLin.Ling@Sun.COM 		spa_sync_props(spa, props, tx);
28808525SEric.Schrock@Sun.COM 	}
28812926Sek110237 
2882789Sahrens 	dmu_tx_commit(tx);
2883789Sahrens 
2884789Sahrens 	spa->spa_sync_on = B_TRUE;
2885789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2886789Sahrens 
2887789Sahrens 	/*
2888789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2889789Sahrens 	 * bean counters are appropriately updated.
2890789Sahrens 	 */
2891789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2892789Sahrens 
28936643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2894789Sahrens 
28955094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
28964715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
28979946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
28984715Sek110237 
28998667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
29008667SGeorge.Wilson@Sun.COM 
2901789Sahrens 	mutex_exit(&spa_namespace_lock);
2902789Sahrens 
2903789Sahrens 	return (0);
2904789Sahrens }
2905789Sahrens 
29066423Sgw25295 #ifdef _KERNEL
29076423Sgw25295 /*
29089790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
29099790SLin.Ling@Sun.COM  * during the system boot up time.
29106423Sgw25295  */
29119790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
29129790SLin.Ling@Sun.COM 
29139790SLin.Ling@Sun.COM static nvlist_t *
29149790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
29156423Sgw25295 {
29169790SLin.Ling@Sun.COM 	nvlist_t *config;
29176423Sgw25295 	nvlist_t *nvtop, *nvroot;
29186423Sgw25295 	uint64_t pgid;
29196423Sgw25295 
29209790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
29219790SLin.Ling@Sun.COM 		return (NULL);
29229790SLin.Ling@Sun.COM 
29236423Sgw25295 	/*
29246423Sgw25295 	 * Add this top-level vdev to the child array.
29256423Sgw25295 	 */
29269790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29279790SLin.Ling@Sun.COM 	    &nvtop) == 0);
29289790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
29299790SLin.Ling@Sun.COM 	    &pgid) == 0);
29309790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
29316423Sgw25295 
29326423Sgw25295 	/*
29336423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
29346423Sgw25295 	 */
29356423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
29369790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
29379790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
29386423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
29396423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
29406423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
29416423Sgw25295 	    &nvtop, 1) == 0);
29426423Sgw25295 
29436423Sgw25295 	/*
29446423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
29456423Sgw25295 	 * this pool's configuration (remove the old, add the new).
29466423Sgw25295 	 */
29476423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
29486423Sgw25295 	nvlist_free(nvroot);
29499790SLin.Ling@Sun.COM 	return (config);
29506423Sgw25295 }
29516423Sgw25295 
29526423Sgw25295 /*
29539790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
29549790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
29559790SLin.Ling@Sun.COM  * device has a more recent txg.
29566423Sgw25295  */
29579790SLin.Ling@Sun.COM static void
29589790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
29597147Staylor {
29609816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
29619790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
29629790SLin.Ling@Sun.COM 
29639790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
29649790SLin.Ling@Sun.COM 		nvlist_t *label;
29659790SLin.Ling@Sun.COM 		uint64_t label_txg;
29669790SLin.Ling@Sun.COM 
29679790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
29689790SLin.Ling@Sun.COM 		    &label) != 0)
29699790SLin.Ling@Sun.COM 			return;
29709790SLin.Ling@Sun.COM 
29719790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
29729790SLin.Ling@Sun.COM 		    &label_txg) == 0);
29739790SLin.Ling@Sun.COM 
29749790SLin.Ling@Sun.COM 		/*
29759790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
29769790SLin.Ling@Sun.COM 		 */
29779790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
29789790SLin.Ling@Sun.COM 			*txg = label_txg;
29799790SLin.Ling@Sun.COM 			*avd = vd;
29807147Staylor 		}
29819790SLin.Ling@Sun.COM 		nvlist_free(label);
29827147Staylor 	}
29837147Staylor }
29847147Staylor 
29856423Sgw25295 /*
29866423Sgw25295  * Import a root pool.
29876423Sgw25295  *
29887147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
29897147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
29907147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
29916423Sgw25295  *
29926423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
29936423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
29946423Sgw25295  * e.g.
29956423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
29966423Sgw25295  */
29976423Sgw25295 int
29987147Staylor spa_import_rootpool(char *devpath, char *devid)
29996423Sgw25295 {
30009790SLin.Ling@Sun.COM 	spa_t *spa;
30019790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
30029790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
30039790SLin.Ling@Sun.COM 	uint64_t guid, txg;
30046423Sgw25295 	char *pname;
30056423Sgw25295 	int error;
30066423Sgw25295 
30076423Sgw25295 	/*
30089790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
30096423Sgw25295 	 */
301010822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
301110822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
301210822SJack.Meng@Sun.COM 	if (config == NULL) {
301310822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
301410822SJack.Meng@Sun.COM 			/* iscsi boot */
301510822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
301610822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
301710822SJack.Meng@Sun.COM 		}
301810822SJack.Meng@Sun.COM 	}
301910822SJack.Meng@Sun.COM #endif
302010822SJack.Meng@Sun.COM 	if (config == NULL) {
30219790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
30229790SLin.Ling@Sun.COM 		    devpath);
30239790SLin.Ling@Sun.COM 		return (EIO);
30249790SLin.Ling@Sun.COM 	}
30259790SLin.Ling@Sun.COM 
30269790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
30279790SLin.Ling@Sun.COM 	    &pname) == 0);
30289790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
30296423Sgw25295 
30309425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30319425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
30329425SEric.Schrock@Sun.COM 		/*
30339425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
30349425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
30359425SEric.Schrock@Sun.COM 		 */
30369425SEric.Schrock@Sun.COM 		spa_remove(spa);
30379425SEric.Schrock@Sun.COM 	}
30389425SEric.Schrock@Sun.COM 
303910921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
30409425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
304110100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
30429790SLin.Ling@Sun.COM 
30439790SLin.Ling@Sun.COM 	/*
30449790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
30459790SLin.Ling@Sun.COM 	 */
30469790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30479790SLin.Ling@Sun.COM 	    &nvtop) == 0);
30489790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30499790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
30509790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
30519790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30529790SLin.Ling@Sun.COM 	if (error) {
30539790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
30549790SLin.Ling@Sun.COM 		nvlist_free(config);
30559790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
30569790SLin.Ling@Sun.COM 		    pname);
30579790SLin.Ling@Sun.COM 		return (error);
30589790SLin.Ling@Sun.COM 	}
30599790SLin.Ling@Sun.COM 
30609790SLin.Ling@Sun.COM 	/*
30619790SLin.Ling@Sun.COM 	 * Get the boot vdev.
30629790SLin.Ling@Sun.COM 	 */
30639790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
30649790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
30659790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
30669790SLin.Ling@Sun.COM 		error = ENOENT;
30679790SLin.Ling@Sun.COM 		goto out;
30689790SLin.Ling@Sun.COM 	}
30699790SLin.Ling@Sun.COM 
30709790SLin.Ling@Sun.COM 	/*
30719790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
30729790SLin.Ling@Sun.COM 	 */
30739790SLin.Ling@Sun.COM 	avd = bvd;
30749790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
30759790SLin.Ling@Sun.COM 	if (avd != bvd) {
30769790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
30779790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
30789790SLin.Ling@Sun.COM 		error = EINVAL;
30799790SLin.Ling@Sun.COM 		goto out;
30809790SLin.Ling@Sun.COM 	}
30819790SLin.Ling@Sun.COM 
30829790SLin.Ling@Sun.COM 	/*
30839790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
30849790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
30859790SLin.Ling@Sun.COM 	 */
30869790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30879790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
30889790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
30899790SLin.Ling@Sun.COM 		    "try booting from '%s'",
30909790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
30919790SLin.Ling@Sun.COM 		error = EINVAL;
30929790SLin.Ling@Sun.COM 		goto out;
30939790SLin.Ling@Sun.COM 	}
30949790SLin.Ling@Sun.COM 
30959790SLin.Ling@Sun.COM 	error = 0;
30969946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
30979790SLin.Ling@Sun.COM out:
30989790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30999790SLin.Ling@Sun.COM 	vdev_free(rvd);
31009790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
31019425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31026423Sgw25295 
31039790SLin.Ling@Sun.COM 	nvlist_free(config);
31046423Sgw25295 	return (error);
31056423Sgw25295 }
31069790SLin.Ling@Sun.COM 
31076423Sgw25295 #endif
31086423Sgw25295 
31096423Sgw25295 /*
31109425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
31119425SEric.Schrock@Sun.COM  * boot.
31129425SEric.Schrock@Sun.COM  */
31139425SEric.Schrock@Sun.COM int
31149425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
31159425SEric.Schrock@Sun.COM {
31169425SEric.Schrock@Sun.COM 	spa_t *spa;
31179425SEric.Schrock@Sun.COM 	char *altroot = NULL;
31189425SEric.Schrock@Sun.COM 
31199425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
31209425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
31219425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31229425SEric.Schrock@Sun.COM 		return (EEXIST);
31239425SEric.Schrock@Sun.COM 	}
31249425SEric.Schrock@Sun.COM 
31259425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31269425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
312710921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
312810921STim.Haley@Sun.COM 
312910100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
313010000SVictor.Latushkin@Sun.COM 
31319425SEric.Schrock@Sun.COM 	if (props != NULL)
31329425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31339425SEric.Schrock@Sun.COM 
31349425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
31359425SEric.Schrock@Sun.COM 
31369425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31379946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31389425SEric.Schrock@Sun.COM 
31399425SEric.Schrock@Sun.COM 	return (0);
31409425SEric.Schrock@Sun.COM }
31419425SEric.Schrock@Sun.COM 
31429425SEric.Schrock@Sun.COM /*
31436423Sgw25295  * Import a non-root pool into the system.
31446423Sgw25295  */
31456423Sgw25295 int
31466423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
31476423Sgw25295 {
31489425SEric.Schrock@Sun.COM 	spa_t *spa;
31499425SEric.Schrock@Sun.COM 	char *altroot = NULL;
315010921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
315110921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
31529425SEric.Schrock@Sun.COM 	int error;
31539425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
31549425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
31559425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
31569425SEric.Schrock@Sun.COM 
31579425SEric.Schrock@Sun.COM 	/*
31589425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
31599425SEric.Schrock@Sun.COM 	 */
31609425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
316111422SMark.Musante@Sun.COM 	if (spa_lookup(pool) != NULL) {
31629425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31639425SEric.Schrock@Sun.COM 		return (EEXIST);
31649425SEric.Schrock@Sun.COM 	}
31659425SEric.Schrock@Sun.COM 
316610921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
316710921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
316810921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
316910921STim.Haley@Sun.COM 
31709425SEric.Schrock@Sun.COM 	/*
31719425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
31729425SEric.Schrock@Sun.COM 	 */
31739425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31749425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
317510921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
31769425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
31779425SEric.Schrock@Sun.COM 
31789425SEric.Schrock@Sun.COM 	/*
31799630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
31809630SJeff.Bonwick@Sun.COM 	 */
31819630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
31829630SJeff.Bonwick@Sun.COM 
31839630SJeff.Bonwick@Sun.COM 	/*
31849425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
31859425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
31869425SEric.Schrock@Sun.COM 	 * doing an import.
31879425SEric.Schrock@Sun.COM 	 */
318810921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
318910921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
319010921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
319111727SVictor.Latushkin@Sun.COM 	    policy.zrp_request);
319210921STim.Haley@Sun.COM 
319310921STim.Haley@Sun.COM 	/*
319410921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
319510921STim.Haley@Sun.COM 	 * back to caller
319610921STim.Haley@Sun.COM 	 */
319710921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
31989425SEric.Schrock@Sun.COM 
31999425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32009425SEric.Schrock@Sun.COM 	/*
32019425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
32029425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
32039425SEric.Schrock@Sun.COM 	 */
32049425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
32059425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
32069425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
32079425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32089425SEric.Schrock@Sun.COM 	}
32099425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
32109425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
32119425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
32129425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32139425SEric.Schrock@Sun.COM 	}
32149425SEric.Schrock@Sun.COM 
32159425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
32169425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
32179425SEric.Schrock@Sun.COM 	if (error == 0)
32189425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32199425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
32209425SEric.Schrock@Sun.COM 	if (error == 0)
32219425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32229425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
32239425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
32249425SEric.Schrock@Sun.COM 
32259425SEric.Schrock@Sun.COM 	if (props != NULL)
32269425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
32279425SEric.Schrock@Sun.COM 
32289425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
32299425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
32309425SEric.Schrock@Sun.COM 		spa_unload(spa);
32319425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
32329425SEric.Schrock@Sun.COM 		spa_remove(spa);
32339425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
32349425SEric.Schrock@Sun.COM 		return (error);
32359425SEric.Schrock@Sun.COM 	}
32369425SEric.Schrock@Sun.COM 
32379425SEric.Schrock@Sun.COM 	/*
32389425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
32399425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
32409425SEric.Schrock@Sun.COM 	 */
32419425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32429425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
32439425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
32449425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
32459425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
32469425SEric.Schrock@Sun.COM 		else
32479425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
32489425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32499425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
32509425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
32519425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32529425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32539425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32549425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
32559425SEric.Schrock@Sun.COM 	}
32569425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
32579425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
32589425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
32599425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
32609425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
32619425SEric.Schrock@Sun.COM 		else
32629425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
32639425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32649425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
32659425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
32669425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32679425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32689425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32699425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
32709425SEric.Schrock@Sun.COM 	}
32719425SEric.Schrock@Sun.COM 
327210672SEric.Schrock@Sun.COM 	/*
327310672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
327410672SEric.Schrock@Sun.COM 	 */
327510672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
327610672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
327710672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
327810672SEric.Schrock@Sun.COM 	}
327910672SEric.Schrock@Sun.COM 
32809425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
32819425SEric.Schrock@Sun.COM 		/*
32829425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
32839425SEric.Schrock@Sun.COM 		 */
328410100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32859425SEric.Schrock@Sun.COM 	}
32869425SEric.Schrock@Sun.COM 
3287*12296SLin.Ling@Sun.COM 	spa_async_resume(spa);
3288*12296SLin.Ling@Sun.COM 
32899816SGeorge.Wilson@Sun.COM 	/*
32909816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
32919816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
32929816SGeorge.Wilson@Sun.COM 	 */
32939816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
32949816SGeorge.Wilson@Sun.COM 
32959425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
32969946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
32979425SEric.Schrock@Sun.COM 
32989425SEric.Schrock@Sun.COM 	return (0);
32996643Seschrock }
33006643Seschrock 
3301789Sahrens nvlist_t *
3302789Sahrens spa_tryimport(nvlist_t *tryconfig)
3303789Sahrens {
3304789Sahrens 	nvlist_t *config = NULL;
3305789Sahrens 	char *poolname;
3306789Sahrens 	spa_t *spa;
3307789Sahrens 	uint64_t state;
33088680SLin.Ling@Sun.COM 	int error;
3309789Sahrens 
3310789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3311789Sahrens 		return (NULL);
3312789Sahrens 
3313789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3314789Sahrens 		return (NULL);
3315789Sahrens 
33161635Sbonwick 	/*
33171635Sbonwick 	 * Create and initialize the spa structure.
33181635Sbonwick 	 */
3319789Sahrens 	mutex_enter(&spa_namespace_lock);
332010921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
33218241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3322789Sahrens 
3323789Sahrens 	/*
33241635Sbonwick 	 * Pass off the heavy lifting to spa_load().
33251732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
33261732Sbonwick 	 * is actually the one to trust when doing an import.
3327789Sahrens 	 */
332811422SMark.Musante@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3329789Sahrens 
3330789Sahrens 	/*
3331789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3332789Sahrens 	 */
3333789Sahrens 	if (spa->spa_root_vdev != NULL) {
3334789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3335789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3336789Sahrens 		    poolname) == 0);
3337789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3338789Sahrens 		    state) == 0);
33393975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
33403975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
33412082Seschrock 
33422082Seschrock 		/*
33436423Sgw25295 		 * If the bootfs property exists on this pool then we
33446423Sgw25295 		 * copy it out so that external consumers can tell which
33456423Sgw25295 		 * pools are bootable.
33466423Sgw25295 		 */
33478680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
33486423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33496423Sgw25295 
33506423Sgw25295 			/*
33516423Sgw25295 			 * We have to play games with the name since the
33526423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
33536423Sgw25295 			 */
33547754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
33556423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
33566423Sgw25295 				char *cp;
33576423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33586423Sgw25295 
33596423Sgw25295 				cp = strchr(tmpname, '/');
33606423Sgw25295 				if (cp == NULL) {
33616423Sgw25295 					(void) strlcpy(dsname, tmpname,
33626423Sgw25295 					    MAXPATHLEN);
33636423Sgw25295 				} else {
33646423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
33656423Sgw25295 					    "%s/%s", poolname, ++cp);
33666423Sgw25295 				}
33676423Sgw25295 				VERIFY(nvlist_add_string(config,
33686423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
33696423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
33706423Sgw25295 			}
33716423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
33726423Sgw25295 		}
33736423Sgw25295 
33746423Sgw25295 		/*
33755450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
33762082Seschrock 		 */
33779425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
33782082Seschrock 		spa_add_spares(spa, config);
33795450Sbrendan 		spa_add_l2cache(spa, config);
33809425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3381789Sahrens 	}
3382789Sahrens 
3383789Sahrens 	spa_unload(spa);
3384789Sahrens 	spa_deactivate(spa);
3385789Sahrens 	spa_remove(spa);
3386789Sahrens 	mutex_exit(&spa_namespace_lock);
3387789Sahrens 
3388789Sahrens 	return (config);
3389789Sahrens }
3390789Sahrens 
3391789Sahrens /*
3392789Sahrens  * Pool export/destroy
3393789Sahrens  *
3394789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3395789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3396789Sahrens  * update the pool state and sync all the labels to disk, removing the
33978211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
33988211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3399789Sahrens  */
3400789Sahrens static int
34017214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
34028211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3403789Sahrens {
3404789Sahrens 	spa_t *spa;
3405789Sahrens 
34061775Sbillm 	if (oldconfig)
34071775Sbillm 		*oldconfig = NULL;
34081775Sbillm 
34098241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3410789Sahrens 		return (EROFS);
3411789Sahrens 
3412789Sahrens 	mutex_enter(&spa_namespace_lock);
3413789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3414789Sahrens 		mutex_exit(&spa_namespace_lock);
3415789Sahrens 		return (ENOENT);
3416789Sahrens 	}
3417789Sahrens 
3418789Sahrens 	/*
34191544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
34201544Seschrock 	 * reacquire the namespace lock, and see if we can export.
34211544Seschrock 	 */
34221544Seschrock 	spa_open_ref(spa, FTAG);
34231544Seschrock 	mutex_exit(&spa_namespace_lock);
34241544Seschrock 	spa_async_suspend(spa);
34251544Seschrock 	mutex_enter(&spa_namespace_lock);
34261544Seschrock 	spa_close(spa, FTAG);
34271544Seschrock 
34281544Seschrock 	/*
3429789Sahrens 	 * The pool will be in core if it's openable,
3430789Sahrens 	 * in which case we can modify its state.
3431789Sahrens 	 */
3432789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3433789Sahrens 		/*
3434789Sahrens 		 * Objsets may be open only because they're dirty, so we
3435789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3436789Sahrens 		 */
3437789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3438789Sahrens 
34391544Seschrock 		/*
34401544Seschrock 		 * A pool cannot be exported or destroyed if there are active
34411544Seschrock 		 * references.  If we are resetting a pool, allow references by
34421544Seschrock 		 * fault injection handlers.
34431544Seschrock 		 */
34441544Seschrock 		if (!spa_refcount_zero(spa) ||
34451544Seschrock 		    (spa->spa_inject_ref != 0 &&
34461544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
34471544Seschrock 			spa_async_resume(spa);
3448789Sahrens 			mutex_exit(&spa_namespace_lock);
3449789Sahrens 			return (EBUSY);
3450789Sahrens 		}
3451789Sahrens 
3452789Sahrens 		/*
34537214Slling 		 * A pool cannot be exported if it has an active shared spare.
34547214Slling 		 * This is to prevent other pools stealing the active spare
34557214Slling 		 * from an exported pool. At user's own will, such pool can
34567214Slling 		 * be forcedly exported.
34577214Slling 		 */
34587214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
34597214Slling 		    spa_has_active_shared_spare(spa)) {
34607214Slling 			spa_async_resume(spa);
34617214Slling 			mutex_exit(&spa_namespace_lock);
34627214Slling 			return (EXDEV);
34637214Slling 		}
34647214Slling 
34657214Slling 		/*
3466789Sahrens 		 * We want this to be reflected on every label,
3467789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3468789Sahrens 		 * final sync that pushes these changes out.
3469789Sahrens 		 */
34708211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
34717754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34721544Seschrock 			spa->spa_state = new_state;
3473*12296SLin.Ling@Sun.COM 			spa->spa_final_txg = spa_last_synced_txg(spa) +
3474*12296SLin.Ling@Sun.COM 			    TXG_DEFER_SIZE + 1;
34751544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
34767754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
34771544Seschrock 		}
3478789Sahrens 	}
3479789Sahrens 
34804451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
34814451Seschrock 
3482789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3483789Sahrens 		spa_unload(spa);
3484789Sahrens 		spa_deactivate(spa);
3485789Sahrens 	}
3486789Sahrens 
34871775Sbillm 	if (oldconfig && spa->spa_config)
34881775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
34891775Sbillm 
34901544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
34918211SGeorge.Wilson@Sun.COM 		if (!hardforce)
34928211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
34931544Seschrock 		spa_remove(spa);
34941544Seschrock 	}
3495789Sahrens 	mutex_exit(&spa_namespace_lock);
3496789Sahrens 
3497789Sahrens 	return (0);
3498789Sahrens }
3499789Sahrens 
3500789Sahrens /*
3501789Sahrens  * Destroy a storage pool.
3502789Sahrens  */
3503789Sahrens int
3504789Sahrens spa_destroy(char *pool)
3505789Sahrens {
35068211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
35078211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3508789Sahrens }
3509789Sahrens 
3510789Sahrens /*
3511789Sahrens  * Export a storage pool.
3512789Sahrens  */
3513789Sahrens int
35148211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
35158211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3516789Sahrens {
35178211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
35188211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3519789Sahrens }
3520789Sahrens 
3521789Sahrens /*
35221544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
35231544Seschrock  * from the namespace in any way.
35241544Seschrock  */
35251544Seschrock int
35261544Seschrock spa_reset(char *pool)
35271544Seschrock {
35287214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
35298211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
35301544Seschrock }
35311544Seschrock 
35321544Seschrock /*
3533789Sahrens  * ==========================================================================
3534789Sahrens  * Device manipulation
3535789Sahrens  * ==========================================================================
3536789Sahrens  */
3537789Sahrens 
3538789Sahrens /*
35394527Sperrin  * Add a device to a storage pool.
3540789Sahrens  */
3541789Sahrens int
3542789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3543789Sahrens {
354410594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
35458241SJeff.Bonwick@Sun.COM 	int error;
3546789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
35471585Sbonwick 	vdev_t *vd, *tvd;
35485450Sbrendan 	nvlist_t **spares, **l2cache;
35495450Sbrendan 	uint_t nspares, nl2cache;
3550789Sahrens 
3551789Sahrens 	txg = spa_vdev_enter(spa);
3552789Sahrens 
35532082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
35542082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
35552082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
35562082Seschrock 
35577754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3558789Sahrens 
35595450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
35605450Sbrendan 	    &nspares) != 0)
35612082Seschrock 		nspares = 0;
35622082Seschrock 
35635450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
35645450Sbrendan 	    &nl2cache) != 0)
35655450Sbrendan 		nl2cache = 0;
35665450Sbrendan 
35677754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
35682082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
35697754SJeff.Bonwick@Sun.COM 
35707754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
35717754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
35727754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
35732082Seschrock 
35743377Seschrock 	/*
35755450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
35765450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
35773377Seschrock 	 */
35787754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
35793377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
35803377Seschrock 
35813377Seschrock 	/*
35823377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
35833377Seschrock 	 */
35848241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
358510594SGeorge.Wilson@Sun.COM 
358610594SGeorge.Wilson@Sun.COM 		/*
358710594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
358810594SGeorge.Wilson@Sun.COM 		 */
358910594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
359010594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
359110594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
359210594SGeorge.Wilson@Sun.COM 				break;
359310594SGeorge.Wilson@Sun.COM 			}
359410594SGeorge.Wilson@Sun.COM 		}
35953377Seschrock 		tvd = vd->vdev_child[c];
35963377Seschrock 		vdev_remove_child(vd, tvd);
359710594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
35983377Seschrock 		vdev_add_child(rvd, tvd);
35993377Seschrock 		vdev_config_dirty(tvd);
36003377Seschrock 	}
36013377Seschrock 
36022082Seschrock 	if (nspares != 0) {
36035450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
36045450Sbrendan 		    ZPOOL_CONFIG_SPARES);
36052082Seschrock 		spa_load_spares(spa);
36065450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
36075450Sbrendan 	}
36085450Sbrendan 
36095450Sbrendan 	if (nl2cache != 0) {
36105450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
36115450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
36125450Sbrendan 		spa_load_l2cache(spa);
36135450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3614789Sahrens 	}
3615789Sahrens 
3616789Sahrens 	/*
36171585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
36181585Sbonwick 	 * If other threads start allocating from these vdevs before we
36191585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
36201585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
36211585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
36221585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
36231635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
36241585Sbonwick 	 *
36251585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
36261585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
36271585Sbonwick 	 * steps will be completed the next time we load the pool.
3628789Sahrens 	 */
36291635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
36301585Sbonwick 
36311635Sbonwick 	mutex_enter(&spa_namespace_lock);
36321635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36331635Sbonwick 	mutex_exit(&spa_namespace_lock);
3634789Sahrens 
36351635Sbonwick 	return (0);
3636789Sahrens }
3637789Sahrens 
3638789Sahrens /*
3639789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3640789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3641789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3642789Sahrens  *
3643789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3644789Sahrens  * existing device; in this case the two devices are made into their own
36454451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3646789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3647789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3648789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3649789Sahrens  * is automatically detached.
3650789Sahrens  */
3651789Sahrens int
36521544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3653789Sahrens {
3654*12296SLin.Ling@Sun.COM 	uint64_t txg, dtl_max_txg;
3655789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3656789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
36572082Seschrock 	vdev_ops_t *pvops;
36587313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
36597313SEric.Kustarz@Sun.COM 	int newvd_isspare;
36607313SEric.Kustarz@Sun.COM 	int error;
3661789Sahrens 
3662789Sahrens 	txg = spa_vdev_enter(spa);
3663789Sahrens 
36646643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3665789Sahrens 
3666789Sahrens 	if (oldvd == NULL)
3667789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3668789Sahrens 
36691585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
36701585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
36711585Sbonwick 
3672789Sahrens 	pvd = oldvd->vdev_parent;
3673789Sahrens 
36742082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
36754451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
36764451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
36774451Seschrock 
36784451Seschrock 	if (newrootvd->vdev_children != 1)
3679789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3680789Sahrens 
3681789Sahrens 	newvd = newrootvd->vdev_child[0];
3682789Sahrens 
3683789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3684789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3685789Sahrens 
36862082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3687789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3688789Sahrens 
36894527Sperrin 	/*
36904527Sperrin 	 * Spares can't replace logs
36914527Sperrin 	 */
36927326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
36934527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36944527Sperrin 
36952082Seschrock 	if (!replacing) {
36962082Seschrock 		/*
36972082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
36982082Seschrock 		 * vdev.
36992082Seschrock 		 */
37002082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
37012082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
37022082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37032082Seschrock 
37042082Seschrock 		pvops = &vdev_mirror_ops;
37052082Seschrock 	} else {
37062082Seschrock 		/*
37072082Seschrock 		 * Active hot spares can only be replaced by inactive hot
37082082Seschrock 		 * spares.
37092082Seschrock 		 */
37102082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
37112082Seschrock 		    pvd->vdev_child[1] == oldvd &&
37122082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
37132082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37142082Seschrock 
37152082Seschrock 		/*
37162082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
37172082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
37183377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
37193377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
37203377Seschrock 		 * the same (spare replaces spare, non-spare replaces
37213377Seschrock 		 * non-spare).
37222082Seschrock 		 */
37232082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
37242082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37253377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
37263377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
37273377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37282082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
37292082Seschrock 		    newvd->vdev_isspare)
37302082Seschrock 			pvops = &vdev_spare_ops;
37312082Seschrock 		else
37322082Seschrock 			pvops = &vdev_replacing_ops;
37332082Seschrock 	}
37342082Seschrock 
37351175Slling 	/*
37369816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
37371175Slling 	 */
37389816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3739789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3740789Sahrens 
37411732Sbonwick 	/*
37421732Sbonwick 	 * The new device cannot have a higher alignment requirement
37431732Sbonwick 	 * than the top-level vdev.
37441732Sbonwick 	 */
37451732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3746789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3747789Sahrens 
3748789Sahrens 	/*
3749789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3750789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3751789Sahrens 	 */
3752789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3753789Sahrens 		spa_strfree(oldvd->vdev_path);
3754789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3755789Sahrens 		    KM_SLEEP);
3756789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3757789Sahrens 		    newvd->vdev_path, "old");
3758789Sahrens 		if (oldvd->vdev_devid != NULL) {
3759789Sahrens 			spa_strfree(oldvd->vdev_devid);
3760789Sahrens 			oldvd->vdev_devid = NULL;
3761789Sahrens 		}
3762789Sahrens 	}
3763789Sahrens 
3764789Sahrens 	/*
37652082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
37662082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3767789Sahrens 	 */
3768789Sahrens 	if (pvd->vdev_ops != pvops)
3769789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3770789Sahrens 
3771789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3772789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3773789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3774789Sahrens 
3775789Sahrens 	/*
3776789Sahrens 	 * Extract the new device from its root and add it to pvd.
3777789Sahrens 	 */
3778789Sahrens 	vdev_remove_child(newrootvd, newvd);
3779789Sahrens 	newvd->vdev_id = pvd->vdev_children;
378010594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3781789Sahrens 	vdev_add_child(pvd, newvd);
3782789Sahrens 
3783789Sahrens 	tvd = newvd->vdev_top;
3784789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3785789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3786789Sahrens 
3787789Sahrens 	vdev_config_dirty(tvd);
3788789Sahrens 
3789789Sahrens 	/*
3790*12296SLin.Ling@Sun.COM 	 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
3791*12296SLin.Ling@Sun.COM 	 * for any dmu_sync-ed blocks.  It will propagate upward when
3792*12296SLin.Ling@Sun.COM 	 * spa_vdev_exit() calls vdev_dtl_reassess().
3793789Sahrens 	 */
3794*12296SLin.Ling@Sun.COM 	dtl_max_txg = txg + TXG_CONCURRENT_STATES;
3795*12296SLin.Ling@Sun.COM 
3796*12296SLin.Ling@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
3797*12296SLin.Ling@Sun.COM 	    dtl_max_txg - TXG_INITIAL);
3798789Sahrens 
37999425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
38003377Seschrock 		spa_spare_activate(newvd);
38019425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
38029425SEric.Schrock@Sun.COM 	}
38039425SEric.Schrock@Sun.COM 
38047754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
38057754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
38067313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
38071544Seschrock 
3808789Sahrens 	/*
3809789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3810789Sahrens 	 */
38111732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3812789Sahrens 
3813*12296SLin.Ling@Sun.COM 	/*
3814*12296SLin.Ling@Sun.COM 	 * Restart the resilver
3815*12296SLin.Ling@Sun.COM 	 */
3816*12296SLin.Ling@Sun.COM 	dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
3817*12296SLin.Ling@Sun.COM 
3818*12296SLin.Ling@Sun.COM 	/*
3819*12296SLin.Ling@Sun.COM 	 * Commit the config
3820*12296SLin.Ling@Sun.COM 	 */
3821*12296SLin.Ling@Sun.COM 	(void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
3822*12296SLin.Ling@Sun.COM 
3823*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
3824*12296SLin.Ling@Sun.COM 	    "%s vdev=%s %s vdev=%s",
38259946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
38269946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
38279946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
38287313SEric.Kustarz@Sun.COM 
38297313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
38307313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
38317313SEric.Kustarz@Sun.COM 
3832789Sahrens 	return (0);
3833789Sahrens }
3834789Sahrens 
3835789Sahrens /*
3836789Sahrens  * Detach a device from a mirror or replacing vdev.
3837789Sahrens  * If 'replace_done' is specified, only detach if the parent
3838789Sahrens  * is a replacing vdev.
3839789Sahrens  */
3840789Sahrens int
38418241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3842789Sahrens {
3843789Sahrens 	uint64_t txg;
38448241SJeff.Bonwick@Sun.COM 	int error;
3845789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3846789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
38472082Seschrock 	boolean_t unspare = B_FALSE;
38482082Seschrock 	uint64_t unspare_guid;
38496673Seschrock 	size_t len;
385011422SMark.Musante@Sun.COM 	char *vdpath;
3851789Sahrens 
3852789Sahrens 	txg = spa_vdev_enter(spa);
3853789Sahrens 
38546643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3855789Sahrens 
3856789Sahrens 	if (vd == NULL)
3857789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3858789Sahrens 
38591585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
38601585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38611585Sbonwick 
3862789Sahrens 	pvd = vd->vdev_parent;
3863789Sahrens 
3864789Sahrens 	/*
38658241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
38668241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
38678241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
38688241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
38698241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
38708241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
38718241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
38728241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
38738241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
38748241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
38758241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
38768241SJeff.Bonwick@Sun.COM 	 */
38778241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
38788241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
38798241SJeff.Bonwick@Sun.COM 
38808241SJeff.Bonwick@Sun.COM 	/*
3881789Sahrens 	 * If replace_done is specified, only remove this device if it's
38822082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
38832082Seschrock 	 * disk can be removed.
3884789Sahrens 	 */
38852082Seschrock 	if (replace_done) {
38862082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
38872082Seschrock 			if (vd->vdev_id != 0)
38882082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38892082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
38902082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38912082Seschrock 		}
38922082Seschrock 	}
38932082Seschrock 
38942082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
38954577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3896789Sahrens 
3897789Sahrens 	/*
38982082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3899789Sahrens 	 */
3900789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
39012082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
39022082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3903789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3904789Sahrens 
3905789Sahrens 	/*
39068241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
39078241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3908789Sahrens 	 */
39098241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3910789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3911789Sahrens 
39128241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
39138241SJeff.Bonwick@Sun.COM 
3914789Sahrens 	/*
39156673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
39166673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
39176673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
39186673Seschrock 	 */
39196673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
39206673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
39216673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
39226673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
39236673Seschrock 		cvd = pvd->vdev_child[0];
39246673Seschrock 		len = strlen(vd->vdev_path);
39256673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
39266673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
39276673Seschrock 			spa_strfree(cvd->vdev_path);
39286673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
39296673Seschrock 		}
39306673Seschrock 	}
39316673Seschrock 
39326673Seschrock 	/*
39332082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
39342082Seschrock 	 * that the spare should become a real disk, and be removed from the
39352082Seschrock 	 * active spare list for the pool.
39362082Seschrock 	 */
39372082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
39388241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
39392082Seschrock 		unspare = B_TRUE;
39402082Seschrock 
39412082Seschrock 	/*
3942789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3943789Sahrens 	 * This must be done after all other error cases are handled,
3944789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3945789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3946789Sahrens 	 * it may be that the unwritability of the disk is the reason
3947789Sahrens 	 * it's being detached!
3948789Sahrens 	 */
39493377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3950789Sahrens 
3951789Sahrens 	/*
3952789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3953789Sahrens 	 */
3954789Sahrens 	vdev_remove_child(pvd, vd);
3955789Sahrens 	vdev_compact_children(pvd);
3956789Sahrens 
3957789Sahrens 	/*
3958789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3959789Sahrens 	 */
3960789Sahrens 	cvd = pvd->vdev_child[0];
3961789Sahrens 
3962789Sahrens 	/*
39632082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
39648241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
39658241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
39668241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
39678241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
39688241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
39698241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
39702082Seschrock 	 */
39712082Seschrock 	if (unspare) {
39722082Seschrock 		ASSERT(cvd->vdev_isspare);
39733377Seschrock 		spa_spare_remove(cvd);
39742082Seschrock 		unspare_guid = cvd->vdev_guid;
39758241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
39762082Seschrock 	}
39772082Seschrock 
39782082Seschrock 	/*
3979789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3980789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3981789Sahrens 	 */
3982789Sahrens 	if (pvd->vdev_children == 1)
3983789Sahrens 		vdev_remove_parent(cvd);
3984789Sahrens 
3985789Sahrens 	/*
3986789Sahrens 	 * We don't set tvd until now because the parent we just removed
3987789Sahrens 	 * may have been the previous top-level vdev.
3988789Sahrens 	 */
3989789Sahrens 	tvd = cvd->vdev_top;
3990789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3991789Sahrens 
3992789Sahrens 	/*
39933377Seschrock 	 * Reevaluate the parent vdev state.
3994789Sahrens 	 */
39954451Seschrock 	vdev_propagate_state(cvd);
3996789Sahrens 
3997789Sahrens 	/*
39989816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
39999816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
40009816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
40019816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
40029816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
4003789Sahrens 	 */
40049816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
40059816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
40069816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
40079816SGeorge.Wilson@Sun.COM 	}
4008789Sahrens 
4009789Sahrens 	vdev_config_dirty(tvd);
4010789Sahrens 
4011789Sahrens 	/*
40123377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
40133377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
40143377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
40153377Seschrock 	 * prevent vd from being accessed after it's freed.
4016789Sahrens 	 */
401711422SMark.Musante@Sun.COM 	vdpath = spa_strdup(vd->vdev_path);
40188241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
4019789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
40201732Sbonwick 	vd->vdev_detached = B_TRUE;
40211732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
4022789Sahrens 
40234451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
40244451Seschrock 
40252082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
40262082Seschrock 
4027*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
402811422SMark.Musante@Sun.COM 	    "vdev=%s", vdpath);
402911422SMark.Musante@Sun.COM 	spa_strfree(vdpath);
403011422SMark.Musante@Sun.COM 
40312082Seschrock 	/*
40323377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
40333377Seschrock 	 * then we want to go through and remove the device from the hot spare
40343377Seschrock 	 * list of every other pool.
40352082Seschrock 	 */
40362082Seschrock 	if (unspare) {
40378241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
40382082Seschrock 		spa = NULL;
40392082Seschrock 		mutex_enter(&spa_namespace_lock);
40402082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
40412082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
40422082Seschrock 				continue;
40438241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
40448241SJeff.Bonwick@Sun.COM 				continue;
40457793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
40467793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
4047*12296SLin.Ling@Sun.COM 			(void) spa_vdev_remove(spa, unspare_guid,
4048*12296SLin.Ling@Sun.COM 			    B_TRUE);
40497793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
40507793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
40512082Seschrock 		}
40522082Seschrock 		mutex_exit(&spa_namespace_lock);
40532082Seschrock 	}
40542082Seschrock 
40552082Seschrock 	return (error);
40562082Seschrock }
40572082Seschrock 
405811422SMark.Musante@Sun.COM /*
405911422SMark.Musante@Sun.COM  * Split a set of devices from their mirrors, and create a new pool from them.
406011422SMark.Musante@Sun.COM  */
406111422SMark.Musante@Sun.COM int
406211422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
406311422SMark.Musante@Sun.COM     nvlist_t *props, boolean_t exp)
406411422SMark.Musante@Sun.COM {
406511422SMark.Musante@Sun.COM 	int error = 0;
406611422SMark.Musante@Sun.COM 	uint64_t txg, *glist;
406711422SMark.Musante@Sun.COM 	spa_t *newspa;
406811422SMark.Musante@Sun.COM 	uint_t c, children, lastlog;
406911422SMark.Musante@Sun.COM 	nvlist_t **child, *nvl, *tmp;
407011422SMark.Musante@Sun.COM 	dmu_tx_t *tx;
407111422SMark.Musante@Sun.COM 	char *altroot = NULL;
407211422SMark.Musante@Sun.COM 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
407311422SMark.Musante@Sun.COM 	boolean_t activate_slog;
407411422SMark.Musante@Sun.COM 
407511422SMark.Musante@Sun.COM 	if (!spa_writeable(spa))
407611422SMark.Musante@Sun.COM 		return (EROFS);
407711422SMark.Musante@Sun.COM 
407811422SMark.Musante@Sun.COM 	txg = spa_vdev_enter(spa);
407911422SMark.Musante@Sun.COM 
408011422SMark.Musante@Sun.COM 	/* clear the log and flush everything up to now */
408111422SMark.Musante@Sun.COM 	activate_slog = spa_passivate_log(spa);
408211422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
408311422SMark.Musante@Sun.COM 	error = spa_offline_log(spa);
408411422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
408511422SMark.Musante@Sun.COM 
408611422SMark.Musante@Sun.COM 	if (activate_slog)
408711422SMark.Musante@Sun.COM 		spa_activate_log(spa);
408811422SMark.Musante@Sun.COM 
408911422SMark.Musante@Sun.COM 	if (error != 0)
409011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
409111422SMark.Musante@Sun.COM 
409211422SMark.Musante@Sun.COM 	/* check new spa name before going any further */
409311422SMark.Musante@Sun.COM 	if (spa_lookup(newname) != NULL)
409411422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
409511422SMark.Musante@Sun.COM 
409611422SMark.Musante@Sun.COM 	/*
409711422SMark.Musante@Sun.COM 	 * scan through all the children to ensure they're all mirrors
409811422SMark.Musante@Sun.COM 	 */
409911422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
410011422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
410111422SMark.Musante@Sun.COM 	    &children) != 0)
410211422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
410311422SMark.Musante@Sun.COM 
410411422SMark.Musante@Sun.COM 	/* first, check to ensure we've got the right child count */
410511422SMark.Musante@Sun.COM 	rvd = spa->spa_root_vdev;
410611422SMark.Musante@Sun.COM 	lastlog = 0;
410711422SMark.Musante@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
410811422SMark.Musante@Sun.COM 		vdev_t *vd = rvd->vdev_child[c];
410911422SMark.Musante@Sun.COM 
411011422SMark.Musante@Sun.COM 		/* don't count the holes & logs as children */
411111422SMark.Musante@Sun.COM 		if (vd->vdev_islog || vd->vdev_ishole) {
411211422SMark.Musante@Sun.COM 			if (lastlog == 0)
411311422SMark.Musante@Sun.COM 				lastlog = c;
411411422SMark.Musante@Sun.COM 			continue;
411511422SMark.Musante@Sun.COM 		}
411611422SMark.Musante@Sun.COM 
411711422SMark.Musante@Sun.COM 		lastlog = 0;
411811422SMark.Musante@Sun.COM 	}
411911422SMark.Musante@Sun.COM 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
412011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
412111422SMark.Musante@Sun.COM 
412211422SMark.Musante@Sun.COM 	/* next, ensure no spare or cache devices are part of the split */
412311422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
412411422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
412511422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
412611422SMark.Musante@Sun.COM 
412711422SMark.Musante@Sun.COM 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
412811422SMark.Musante@Sun.COM 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
412911422SMark.Musante@Sun.COM 
413011422SMark.Musante@Sun.COM 	/* then, loop over each vdev and validate it */
413111422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
413211422SMark.Musante@Sun.COM 		uint64_t is_hole = 0;
413311422SMark.Musante@Sun.COM 
413411422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
413511422SMark.Musante@Sun.COM 		    &is_hole);
413611422SMark.Musante@Sun.COM 
413711422SMark.Musante@Sun.COM 		if (is_hole != 0) {
413811422SMark.Musante@Sun.COM 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
413911422SMark.Musante@Sun.COM 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
414011422SMark.Musante@Sun.COM 				continue;
414111422SMark.Musante@Sun.COM 			} else {
414211422SMark.Musante@Sun.COM 				error = EINVAL;
414311422SMark.Musante@Sun.COM 				break;
414411422SMark.Musante@Sun.COM 			}
414511422SMark.Musante@Sun.COM 		}
414611422SMark.Musante@Sun.COM 
414711422SMark.Musante@Sun.COM 		/* which disk is going to be split? */
414811422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
414911422SMark.Musante@Sun.COM 		    &glist[c]) != 0) {
415011422SMark.Musante@Sun.COM 			error = EINVAL;
415111422SMark.Musante@Sun.COM 			break;
415211422SMark.Musante@Sun.COM 		}
415311422SMark.Musante@Sun.COM 
415411422SMark.Musante@Sun.COM 		/* look it up in the spa */
415511422SMark.Musante@Sun.COM 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
415611422SMark.Musante@Sun.COM 		if (vml[c] == NULL) {
415711422SMark.Musante@Sun.COM 			error = ENODEV;
415811422SMark.Musante@Sun.COM 			break;
415911422SMark.Musante@Sun.COM 		}
416011422SMark.Musante@Sun.COM 
416111422SMark.Musante@Sun.COM 		/* make sure there's nothing stopping the split */
416211422SMark.Musante@Sun.COM 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
416311422SMark.Musante@Sun.COM 		    vml[c]->vdev_islog ||
416411422SMark.Musante@Sun.COM 		    vml[c]->vdev_ishole ||
416511422SMark.Musante@Sun.COM 		    vml[c]->vdev_isspare ||
416611422SMark.Musante@Sun.COM 		    vml[c]->vdev_isl2cache ||
416711422SMark.Musante@Sun.COM 		    !vdev_writeable(vml[c]) ||
416811497SMark.Musante@Sun.COM 		    vml[c]->vdev_children != 0 ||
416911422SMark.Musante@Sun.COM 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
417011422SMark.Musante@Sun.COM 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
417111422SMark.Musante@Sun.COM 			error = EINVAL;
417211422SMark.Musante@Sun.COM 			break;
417311422SMark.Musante@Sun.COM 		}
417411422SMark.Musante@Sun.COM 
417511422SMark.Musante@Sun.COM 		if (vdev_dtl_required(vml[c])) {
417611422SMark.Musante@Sun.COM 			error = EBUSY;
417711422SMark.Musante@Sun.COM 			break;
417811422SMark.Musante@Sun.COM 		}
417911422SMark.Musante@Sun.COM 
418011422SMark.Musante@Sun.COM 		/* we need certain info from the top level */
418111422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
418211422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_array) == 0);
418311422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
418411422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
418511422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
418611422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_asize) == 0);
418711422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
418811422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ashift) == 0);
418911422SMark.Musante@Sun.COM 	}
419011422SMark.Musante@Sun.COM 
419111422SMark.Musante@Sun.COM 	if (error != 0) {
419211422SMark.Musante@Sun.COM 		kmem_free(vml, children * sizeof (vdev_t *));
419311422SMark.Musante@Sun.COM 		kmem_free(glist, children * sizeof (uint64_t));
419411422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
419511422SMark.Musante@Sun.COM 	}
419611422SMark.Musante@Sun.COM 
419711422SMark.Musante@Sun.COM 	/* stop writers from using the disks */
419811422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
419911422SMark.Musante@Sun.COM 		if (vml[c] != NULL)
420011422SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_TRUE;
420111422SMark.Musante@Sun.COM 	}
420211422SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
420311422SMark.Musante@Sun.COM 
420411422SMark.Musante@Sun.COM 	/*
420511422SMark.Musante@Sun.COM 	 * Temporarily record the splitting vdevs in the spa config.  This
420611422SMark.Musante@Sun.COM 	 * will disappear once the config is regenerated.
420711422SMark.Musante@Sun.COM 	 */
420811422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
420911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
421011422SMark.Musante@Sun.COM 	    glist, children) == 0);
421111422SMark.Musante@Sun.COM 	kmem_free(glist, children * sizeof (uint64_t));
421211422SMark.Musante@Sun.COM 
421311864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
421411422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
421511422SMark.Musante@Sun.COM 	    nvl) == 0);
421611864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
421711422SMark.Musante@Sun.COM 	spa->spa_config_splitting = nvl;
421811422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
421911422SMark.Musante@Sun.COM 
422011422SMark.Musante@Sun.COM 	/* configure and create the new pool */
422111422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
422211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
422311422SMark.Musante@Sun.COM 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
422411422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
422511422SMark.Musante@Sun.COM 	    spa_version(spa)) == 0);
422611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
422711422SMark.Musante@Sun.COM 	    spa->spa_config_txg) == 0);
422811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
422911422SMark.Musante@Sun.COM 	    spa_generate_guid(NULL)) == 0);
423011422SMark.Musante@Sun.COM 	(void) nvlist_lookup_string(props,
423111422SMark.Musante@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
423211422SMark.Musante@Sun.COM 
423311497SMark.Musante@Sun.COM 	/* add the new pool to the namespace */
423411422SMark.Musante@Sun.COM 	newspa = spa_add(newname, config, altroot);
423511422SMark.Musante@Sun.COM 	newspa->spa_config_txg = spa->spa_config_txg;
423611422SMark.Musante@Sun.COM 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
423711422SMark.Musante@Sun.COM 
423811422SMark.Musante@Sun.COM 	/* release the spa config lock, retaining the namespace lock */
423911422SMark.Musante@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
424011422SMark.Musante@Sun.COM 
424111422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
424211422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 1);
424311422SMark.Musante@Sun.COM 
424411422SMark.Musante@Sun.COM 	spa_activate(newspa, spa_mode_global);
424511422SMark.Musante@Sun.COM 	spa_async_suspend(newspa);
424611422SMark.Musante@Sun.COM 
424711422SMark.Musante@Sun.COM 	/* create the new pool from the disks of the original pool */
424811422SMark.Musante@Sun.COM 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
424911422SMark.Musante@Sun.COM 	if (error)
425011422SMark.Musante@Sun.COM 		goto out;
425111422SMark.Musante@Sun.COM 
425211422SMark.Musante@Sun.COM 	/* if that worked, generate a real config for the new pool */
425311422SMark.Musante@Sun.COM 	if (newspa->spa_root_vdev != NULL) {
425411422SMark.Musante@Sun.COM 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
425511422SMark.Musante@Sun.COM 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
425611422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
425711422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
425811422SMark.Musante@Sun.COM 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
425911422SMark.Musante@Sun.COM 		    B_TRUE));
426011422SMark.Musante@Sun.COM 	}
426111422SMark.Musante@Sun.COM 
426211422SMark.Musante@Sun.COM 	/* set the props */
426311422SMark.Musante@Sun.COM 	if (props != NULL) {
426411422SMark.Musante@Sun.COM 		spa_configfile_set(newspa, props, B_FALSE);
426511422SMark.Musante@Sun.COM 		error = spa_prop_set(newspa, props);
426611422SMark.Musante@Sun.COM 		if (error)
426711422SMark.Musante@Sun.COM 			goto out;
426811422SMark.Musante@Sun.COM 	}
426911422SMark.Musante@Sun.COM 
427011422SMark.Musante@Sun.COM 	/* flush everything */
427111422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(newspa);
427211422SMark.Musante@Sun.COM 	vdev_config_dirty(newspa->spa_root_vdev);
427311422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
427411422SMark.Musante@Sun.COM 
427511422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
427611422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 2);
427711422SMark.Musante@Sun.COM 
427811422SMark.Musante@Sun.COM 	spa_async_resume(newspa);
427911422SMark.Musante@Sun.COM 
428011422SMark.Musante@Sun.COM 	/* finally, update the original pool's config */
428111422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
428211422SMark.Musante@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
428311422SMark.Musante@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
428411422SMark.Musante@Sun.COM 	if (error != 0)
428511422SMark.Musante@Sun.COM 		dmu_tx_abort(tx);
428611422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
428711422SMark.Musante@Sun.COM 		if (vml[c] != NULL) {
428811422SMark.Musante@Sun.COM 			vdev_split(vml[c]);
428911422SMark.Musante@Sun.COM 			if (error == 0)
4290*12296SLin.Ling@Sun.COM 				spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4291*12296SLin.Ling@Sun.COM 				    spa, tx, "vdev=%s",
429211422SMark.Musante@Sun.COM 				    vml[c]->vdev_path);
429311422SMark.Musante@Sun.COM 			vdev_free(vml[c]);
429411422SMark.Musante@Sun.COM 		}
429511422SMark.Musante@Sun.COM 	}
429611422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
429711422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
429811422SMark.Musante@Sun.COM 	nvlist_free(nvl);
429911422SMark.Musante@Sun.COM 	if (error == 0)
430011422SMark.Musante@Sun.COM 		dmu_tx_commit(tx);
430111422SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, 0);
430211422SMark.Musante@Sun.COM 
430311422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
430411422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 3);
430511422SMark.Musante@Sun.COM 
430611422SMark.Musante@Sun.COM 	/* split is complete; log a history record */
4307*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
430811422SMark.Musante@Sun.COM 	    "split new pool %s from pool %s", newname, spa_name(spa));
430911422SMark.Musante@Sun.COM 
431011422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
431111422SMark.Musante@Sun.COM 
431211422SMark.Musante@Sun.COM 	/* if we're not going to mount the filesystems in userland, export */
431311422SMark.Musante@Sun.COM 	if (exp)
431411422SMark.Musante@Sun.COM 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
431511422SMark.Musante@Sun.COM 		    B_FALSE, B_FALSE);
431611422SMark.Musante@Sun.COM 
431711422SMark.Musante@Sun.COM 	return (error);
431811422SMark.Musante@Sun.COM 
431911422SMark.Musante@Sun.COM out:
432011422SMark.Musante@Sun.COM 	spa_unload(newspa);
432111422SMark.Musante@Sun.COM 	spa_deactivate(newspa);
432211422SMark.Musante@Sun.COM 	spa_remove(newspa);
432311422SMark.Musante@Sun.COM 
432411422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
432511864SMark.Musante@Sun.COM 
432611864SMark.Musante@Sun.COM 	/* re-online all offlined disks */
432711864SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
432811864SMark.Musante@Sun.COM 		if (vml[c] != NULL)
432911864SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_FALSE;
433011864SMark.Musante@Sun.COM 	}
433111864SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
433211864SMark.Musante@Sun.COM 
433311422SMark.Musante@Sun.COM 	nvlist_free(spa->spa_config_splitting);
433411422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
433511497SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, error);
433611422SMark.Musante@Sun.COM 
433711422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
433811422SMark.Musante@Sun.COM 	return (error);
433911422SMark.Musante@Sun.COM }
434011422SMark.Musante@Sun.COM 
43417754SJeff.Bonwick@Sun.COM static nvlist_t *
43427754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
43432082Seschrock {
43447754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
43457754SJeff.Bonwick@Sun.COM 		uint64_t guid;
43467754SJeff.Bonwick@Sun.COM 
43477754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
43487754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
43497754SJeff.Bonwick@Sun.COM 
43507754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
43517754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
43522082Seschrock 	}
43532082Seschrock 
43547754SJeff.Bonwick@Sun.COM 	return (NULL);
43555450Sbrendan }
43565450Sbrendan 
43577754SJeff.Bonwick@Sun.COM static void
43587754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
43597754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
43605450Sbrendan {
43617754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
43627754SJeff.Bonwick@Sun.COM 
43637754SJeff.Bonwick@Sun.COM 	if (count > 1)
43647754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
43657754SJeff.Bonwick@Sun.COM 
43667754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
43677754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
43687754SJeff.Bonwick@Sun.COM 			continue;
43697754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
43705450Sbrendan 	}
43715450Sbrendan 
43727754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
43737754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
43747754SJeff.Bonwick@Sun.COM 
43757754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
43767754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
43777754SJeff.Bonwick@Sun.COM 
43787754SJeff.Bonwick@Sun.COM 	if (count > 1)
43797754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
43805450Sbrendan }
43815450Sbrendan 
43825450Sbrendan /*
438310594SGeorge.Wilson@Sun.COM  * Evacuate the device.
438410594SGeorge.Wilson@Sun.COM  */
4385*12296SLin.Ling@Sun.COM static int
438610594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
438710594SGeorge.Wilson@Sun.COM {
4388*12296SLin.Ling@Sun.COM 	uint64_t txg;
438910974SJeff.Bonwick@Sun.COM 	int error = 0;
439010594SGeorge.Wilson@Sun.COM 
439110594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
439210594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
439310922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
439410594SGeorge.Wilson@Sun.COM 
439510594SGeorge.Wilson@Sun.COM 	/*
439610594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
439710594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
439810594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
439910594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
440010594SGeorge.Wilson@Sun.COM 	 */
440110594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
4402*12296SLin.Ling@Sun.COM 		if (vd->vdev_stat.vs_alloc != 0)
4403*12296SLin.Ling@Sun.COM 			error = spa_offline_log(spa);
440410974SJeff.Bonwick@Sun.COM 	} else {
4405*12296SLin.Ling@Sun.COM 		error = ENOTSUP;
440610594SGeorge.Wilson@Sun.COM 	}
440710594SGeorge.Wilson@Sun.COM 
440810974SJeff.Bonwick@Sun.COM 	if (error)
440910974SJeff.Bonwick@Sun.COM 		return (error);
441010974SJeff.Bonwick@Sun.COM 
441110594SGeorge.Wilson@Sun.COM 	/*
441210974SJeff.Bonwick@Sun.COM 	 * The evacuation succeeded.  Remove any remaining MOS metadata
441310974SJeff.Bonwick@Sun.COM 	 * associated with this vdev, and wait for these changes to sync.
441410594SGeorge.Wilson@Sun.COM 	 */
4415*12296SLin.Ling@Sun.COM 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
441610594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
441710594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
441810594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
441910594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
442010594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
442110594SGeorge.Wilson@Sun.COM 
442210594SGeorge.Wilson@Sun.COM 	return (0);
442310594SGeorge.Wilson@Sun.COM }
442410594SGeorge.Wilson@Sun.COM 
442510594SGeorge.Wilson@Sun.COM /*
442610594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
442710594SGeorge.Wilson@Sun.COM  */
4428*12296SLin.Ling@Sun.COM static void
442910974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
443010594SGeorge.Wilson@Sun.COM {
443110594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
443210594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
443310594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
443410594SGeorge.Wilson@Sun.COM 
443510594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
443610594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
443710922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
443810594SGeorge.Wilson@Sun.COM 
4439*12296SLin.Ling@Sun.COM 	/*
4440*12296SLin.Ling@Sun.COM 	 * Only remove any devices which are empty.
4441*12296SLin.Ling@Sun.COM 	 */
4442*12296SLin.Ling@Sun.COM 	if (vd->vdev_stat.vs_alloc != 0)
4443*12296SLin.Ling@Sun.COM 		return;
4444*12296SLin.Ling@Sun.COM 
444510594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
444610922SJeff.Bonwick@Sun.COM 
444710922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
444810922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
444910922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
445010922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
445110922SJeff.Bonwick@Sun.COM 
445210594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
445310594SGeorge.Wilson@Sun.COM 
445410594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
445510594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
445610594SGeorge.Wilson@Sun.COM 	} else {
445710594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
445810594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
445910594SGeorge.Wilson@Sun.COM 	}
446010594SGeorge.Wilson@Sun.COM }
446110594SGeorge.Wilson@Sun.COM 
446210594SGeorge.Wilson@Sun.COM /*
4463*12296SLin.Ling@Sun.COM  * Remove a device from the pool -
4464*12296SLin.Ling@Sun.COM  *
4465*12296SLin.Ling@Sun.COM  * Removing a device from the vdev namespace requires several steps
4466*12296SLin.Ling@Sun.COM  * and can take a significant amount of time.  As a result we use
4467*12296SLin.Ling@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
4468*12296SLin.Ling@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
4469*12296SLin.Ling@Sun.COM  * lock.  During each step the configuration is synced out.
4470*12296SLin.Ling@Sun.COM  */
4471*12296SLin.Ling@Sun.COM 
4472*12296SLin.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  * ==========================================================================
4715*12296SLin.Ling@Sun.COM  * SPA Scanning
4716789Sahrens  * ==========================================================================
4717789Sahrens  */
4718789Sahrens 
47197046Sahrens int
4720*12296SLin.Ling@Sun.COM spa_scan_stop(spa_t *spa)
4721789Sahrens {
47227754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4723*12296SLin.Ling@Sun.COM 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
4724*12296SLin.Ling@Sun.COM 		return (EBUSY);
4725*12296SLin.Ling@Sun.COM 	return (dsl_scan_cancel(spa->spa_dsl_pool));
4726*12296SLin.Ling@Sun.COM }
4727*12296SLin.Ling@Sun.COM 
4728*12296SLin.Ling@Sun.COM int
4729*12296SLin.Ling@Sun.COM spa_scan(spa_t *spa, pool_scan_func_t func)
4730*12296SLin.Ling@Sun.COM {
4731*12296SLin.Ling@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4732*12296SLin.Ling@Sun.COM 
4733*12296SLin.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 	 */
4740*12296SLin.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 
4746*12296SLin.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) {
4852*12296SLin.Ling@Sun.COM 			spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
4853*12296SLin.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)
4897*12296SLin.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 {
4943*12296SLin.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  */
4954789Sahrens static void
495510922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg)
4956789Sahrens {
4957789Sahrens 	blkptr_t blk;
4958789Sahrens 	uint64_t itor = 0;
4959789Sahrens 	uint8_t c = 1;
4960789Sahrens 
49617754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
49627754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
496310922SJeff.Bonwick@Sun.COM 		zio_free(spa, txg, &blk);
49647754SJeff.Bonwick@Sun.COM 	}
4965789Sahrens 
4966789Sahrens 	bplist_vacate(bpl, tx);
4967789Sahrens 
4968789Sahrens 	/*
4969789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4970789Sahrens 	 * (Usually only the first block is needed.)
4971789Sahrens 	 */
497210922SJeff.Bonwick@Sun.COM 	dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx);
497310922SJeff.Bonwick@Sun.COM }
497410922SJeff.Bonwick@Sun.COM 
497510922SJeff.Bonwick@Sun.COM static void
497610922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
497710922SJeff.Bonwick@Sun.COM {
497810922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
497910922SJeff.Bonwick@Sun.COM 
498010922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
498110922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
4982789Sahrens }
4983789Sahrens 
4984789Sahrens static void
49852082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
49862082Seschrock {
49872082Seschrock 	char *packed = NULL;
49887497STim.Haley@Sun.COM 	size_t bufsize;
49892082Seschrock 	size_t nvsize = 0;
49902082Seschrock 	dmu_buf_t *db;
49912082Seschrock 
49922082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
49932082Seschrock 
49947497STim.Haley@Sun.COM 	/*
49957497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
49967497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
49977497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
49987497STim.Haley@Sun.COM 	 */
49997497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
50007497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
50012082Seschrock 
50022082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
50032082Seschrock 	    KM_SLEEP) == 0);
50047497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
50057497STim.Haley@Sun.COM 
50067497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
50077497STim.Haley@Sun.COM 
50087497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
50092082Seschrock 
50102082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
50112082Seschrock 	dmu_buf_will_dirty(db, tx);
50122082Seschrock 	*(uint64_t *)db->db_data = nvsize;
50132082Seschrock 	dmu_buf_rele(db, FTAG);
50142082Seschrock }
50152082Seschrock 
50162082Seschrock static void
50175450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
50185450Sbrendan     const char *config, const char *entry)
50192082Seschrock {
50202082Seschrock 	nvlist_t *nvroot;
50215450Sbrendan 	nvlist_t **list;
50222082Seschrock 	int i;
50232082Seschrock 
50245450Sbrendan 	if (!sav->sav_sync)
50252082Seschrock 		return;
50262082Seschrock 
50272082Seschrock 	/*
50285450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
50295450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
50304451Seschrock 	 * valid and the vdevs are labeled appropriately.
50312082Seschrock 	 */
50325450Sbrendan 	if (sav->sav_object == 0) {
50335450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
50345450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
50355450Sbrendan 		    sizeof (uint64_t), tx);
50362082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
50375450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
50385450Sbrendan 		    &sav->sav_object, tx) == 0);
50392082Seschrock 	}
50402082Seschrock 
50412082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
50425450Sbrendan 	if (sav->sav_count == 0) {
50435450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
50442082Seschrock 	} else {
50455450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
50465450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50475450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5048*12296SLin.Ling@Sun.COM 			    B_FALSE, VDEV_CONFIG_L2CACHE);
50495450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
50505450Sbrendan 		    sav->sav_count) == 0);
50515450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50525450Sbrendan 			nvlist_free(list[i]);
50535450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
50542082Seschrock 	}
50552082Seschrock 
50565450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
50572926Sek110237 	nvlist_free(nvroot);
50582082Seschrock 
50595450Sbrendan 	sav->sav_sync = B_FALSE;
50602082Seschrock }
50612082Seschrock 
50622082Seschrock static void
5063789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5064789Sahrens {
5065789Sahrens 	nvlist_t *config;
5066789Sahrens 
50677754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
5068789Sahrens 		return;
5069789Sahrens 
50707754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
50717754SJeff.Bonwick@Sun.COM 
50727754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
50737754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
50747754SJeff.Bonwick@Sun.COM 
50757754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
5076789Sahrens 
50771635Sbonwick 	if (spa->spa_config_syncing)
50781635Sbonwick 		nvlist_free(spa->spa_config_syncing);
50791635Sbonwick 	spa->spa_config_syncing = config;
5080789Sahrens 
50812082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5082789Sahrens }
5083789Sahrens 
50845094Slling /*
50855094Slling  * Set zpool properties.
50865094Slling  */
50873912Slling static void
5088*12296SLin.Ling@Sun.COM spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
50893912Slling {
50903912Slling 	spa_t *spa = arg1;
50915094Slling 	objset_t *mos = spa->spa_meta_objset;
50923912Slling 	nvlist_t *nvp = arg2;
50935094Slling 	nvpair_t *elem;
50944451Seschrock 	uint64_t intval;
50956643Seschrock 	char *strval;
50965094Slling 	zpool_prop_t prop;
50975094Slling 	const char *propname;
50985094Slling 	zprop_type_t proptype;
50995094Slling 
51007754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
51017754SJeff.Bonwick@Sun.COM 
51025094Slling 	elem = NULL;
51035094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
51045094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
51055094Slling 		case ZPOOL_PROP_VERSION:
51065094Slling 			/*
51075094Slling 			 * Only set version for non-zpool-creation cases
51085094Slling 			 * (set/import). spa_create() needs special care
51095094Slling 			 * for version setting.
51105094Slling 			 */
51115094Slling 			if (tx->tx_txg != TXG_INITIAL) {
51125094Slling 				VERIFY(nvpair_value_uint64(elem,
51135094Slling 				    &intval) == 0);
51145094Slling 				ASSERT(intval <= SPA_VERSION);
51155094Slling 				ASSERT(intval >= spa_version(spa));
51165094Slling 				spa->spa_uberblock.ub_version = intval;
51175094Slling 				vdev_config_dirty(spa->spa_root_vdev);
51185094Slling 			}
51195094Slling 			break;
51205094Slling 
51215094Slling 		case ZPOOL_PROP_ALTROOT:
51225094Slling 			/*
51235094Slling 			 * 'altroot' is a non-persistent property. It should
51245094Slling 			 * have been set temporarily at creation or import time.
51255094Slling 			 */
51265094Slling 			ASSERT(spa->spa_root != NULL);
51275094Slling 			break;
51285094Slling 
51295363Seschrock 		case ZPOOL_PROP_CACHEFILE:
51305094Slling 			/*
51318525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
51325094Slling 			 */
51334543Smarks 			break;
51345094Slling 		default:
51355094Slling 			/*
51365094Slling 			 * Set pool property values in the poolprops mos object.
51375094Slling 			 */
51385094Slling 			if (spa->spa_pool_props_object == 0) {
51395094Slling 				VERIFY((spa->spa_pool_props_object =
51405094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
51415094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
51425094Slling 
51435094Slling 				VERIFY(zap_update(mos,
51445094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
51455094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
51465094Slling 				    == 0);
51475094Slling 			}
51485094Slling 
51495094Slling 			/* normalize the property name */
51505094Slling 			propname = zpool_prop_to_name(prop);
51515094Slling 			proptype = zpool_prop_get_type(prop);
51525094Slling 
51535094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
51545094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
51555094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
51565094Slling 				VERIFY(zap_update(mos,
51575094Slling 				    spa->spa_pool_props_object, propname,
51585094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
51595094Slling 
51605094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
51615094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
51625094Slling 
51635094Slling 				if (proptype == PROP_TYPE_INDEX) {
51645094Slling 					const char *unused;
51655094Slling 					VERIFY(zpool_prop_index_to_string(
51665094Slling 					    prop, intval, &unused) == 0);
51675094Slling 				}
51685094Slling 				VERIFY(zap_update(mos,
51695094Slling 				    spa->spa_pool_props_object, propname,
51705094Slling 				    8, 1, &intval, tx) == 0);
51715094Slling 			} else {
51725094Slling 				ASSERT(0); /* not allowed */
51735094Slling 			}
51745094Slling 
51755329Sgw25295 			switch (prop) {
51765329Sgw25295 			case ZPOOL_PROP_DELEGATION:
51775094Slling 				spa->spa_delegation = intval;
51785329Sgw25295 				break;
51795329Sgw25295 			case ZPOOL_PROP_BOOTFS:
51805094Slling 				spa->spa_bootfs = intval;
51815329Sgw25295 				break;
51825329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
51835329Sgw25295 				spa->spa_failmode = intval;
51845329Sgw25295 				break;
51859816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
51869816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
51879816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
51889816SGeorge.Wilson@Sun.COM 				break;
518910922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
519010922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
519110922SJeff.Bonwick@Sun.COM 				break;
51925329Sgw25295 			default:
51935329Sgw25295 				break;
51945329Sgw25295 			}
51953912Slling 		}
51965094Slling 
51975094Slling 		/* log internal history if this is not a zpool create */
51985094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
51995094Slling 		    tx->tx_txg != TXG_INITIAL) {
5200*12296SLin.Ling@Sun.COM 			spa_history_log_internal(LOG_POOL_PROPSET,
5201*12296SLin.Ling@Sun.COM 			    spa, tx, "%s %lld %s",
52027754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
52035094Slling 		}
52043912Slling 	}
52057754SJeff.Bonwick@Sun.COM 
52067754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
52073912Slling }
52083912Slling 
5209789Sahrens /*
5210789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
5211789Sahrens  * part of the process, so we iterate until it converges.
5212789Sahrens  */
5213789Sahrens void
5214789Sahrens spa_sync(spa_t *spa, uint64_t txg)
5215789Sahrens {
5216789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
5217789Sahrens 	objset_t *mos = spa->spa_meta_objset;
521810922SJeff.Bonwick@Sun.COM 	bplist_t *defer_bpl = &spa->spa_deferred_bplist;
521910922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
52201635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5221789Sahrens 	vdev_t *vd;
5222789Sahrens 	dmu_tx_t *tx;
52237754SJeff.Bonwick@Sun.COM 	int error;
5224789Sahrens 
5225789Sahrens 	/*
5226789Sahrens 	 * Lock out configuration changes.
5227789Sahrens 	 */
52287754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5229789Sahrens 
5230789Sahrens 	spa->spa_syncing_txg = txg;
5231789Sahrens 	spa->spa_sync_pass = 0;
5232789Sahrens 
52337754SJeff.Bonwick@Sun.COM 	/*
52347754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
52357754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
52367754SJeff.Bonwick@Sun.COM 	 */
52377754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
52388241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
52398241SJeff.Bonwick@Sun.COM 		/*
52408241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
52418241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
52428241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
52438241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
52448241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
52458241SJeff.Bonwick@Sun.COM 		 */
52468241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52478241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
52488241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
52498241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
52508241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
52518241SJeff.Bonwick@Sun.COM 		}
52528241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52538241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
52547754SJeff.Bonwick@Sun.COM 	}
52557754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
52567754SJeff.Bonwick@Sun.COM 
525710922SJeff.Bonwick@Sun.COM 	VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj));
5258789Sahrens 
52592082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
52602082Seschrock 
52612082Seschrock 	/*
52624577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
52632082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
52642082Seschrock 	 */
52654577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
52664577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
52672082Seschrock 		int i;
52682082Seschrock 
52692082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
52702082Seschrock 			vd = rvd->vdev_child[i];
52712082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
52722082Seschrock 				break;
52732082Seschrock 		}
52742082Seschrock 		if (i == rvd->vdev_children) {
52752082Seschrock 			spa->spa_deflate = TRUE;
52762082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
52772082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
52782082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
52792082Seschrock 		}
52802082Seschrock 	}
52812082Seschrock 
52827046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
52837046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
52847046Sahrens 		dsl_pool_create_origin(dp, tx);
52857046Sahrens 
52867046Sahrens 		/* Keeping the origin open increases spa_minref */
52877046Sahrens 		spa->spa_minref += 3;
52887046Sahrens 	}
52897046Sahrens 
52907046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
52917046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
52927046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
52937046Sahrens 	}
52947046Sahrens 
5295789Sahrens 	/*
5296*12296SLin.Ling@Sun.COM 	 * If anything has changed in this txg, or if someone is waiting
5297*12296SLin.Ling@Sun.COM 	 * for this txg to sync (eg, spa_vdev_remove()), push the
5298*12296SLin.Ling@Sun.COM 	 * deferred frees from the previous txg.  If not, leave them
5299*12296SLin.Ling@Sun.COM 	 * alone so that we don't generate work on an otherwise idle
5300*12296SLin.Ling@Sun.COM 	 * system.
5301789Sahrens 	 */
5302789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
53032329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5304*12296SLin.Ling@Sun.COM 	    !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5305*12296SLin.Ling@Sun.COM 	    ((dp->dp_scan->scn_phys.scn_state == DSS_SCANNING ||
5306*12296SLin.Ling@Sun.COM 	    txg_sync_waiting(dp)) && !spa_shutting_down(spa)))
530710922SJeff.Bonwick@Sun.COM 		spa_sync_deferred_bplist(spa, defer_bpl, tx, txg);
5308789Sahrens 
5309789Sahrens 	/*
5310789Sahrens 	 * Iterate to convergence.
5311789Sahrens 	 */
5312789Sahrens 	do {
531310922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
5314789Sahrens 
5315789Sahrens 		spa_sync_config_object(spa, tx);
53165450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
53175450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
53185450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
53195450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
53201544Seschrock 		spa_errlog_sync(spa, txg);
5321789Sahrens 		dsl_pool_sync(dp, txg);
5322789Sahrens 
532310922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
532410922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
532510922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, spa_sync_free, zio, tx);
532610922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
532710922SJeff.Bonwick@Sun.COM 		} else {
532810922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx);
5329789Sahrens 		}
5330789Sahrens 
533110922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
5332*12296SLin.Ling@Sun.COM 		dsl_scan_sync(dp, tx);
533311619SGeorge.Wilson@Sun.COM 
533410922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
533510922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
533610922SJeff.Bonwick@Sun.COM 
533710922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
533810922SJeff.Bonwick@Sun.COM 
533911932SGeorge.Wilson@Sun.COM 	ASSERT(list_is_empty(&free_bpl->bpl_queue));
534010922SJeff.Bonwick@Sun.COM 
534110922SJeff.Bonwick@Sun.COM 	bplist_close(defer_bpl);
5342789Sahrens 
5343789Sahrens 	/*
5344789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
5345789Sahrens 	 * to commit the transaction group.
53461635Sbonwick 	 *
53475688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
53485688Sbonwick 	 * random top-level vdevs that are known to be visible in the
53497754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
53507754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5351789Sahrens 	 */
53527754SJeff.Bonwick@Sun.COM 	for (;;) {
53537754SJeff.Bonwick@Sun.COM 		/*
53547754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
53557754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
53567754SJeff.Bonwick@Sun.COM 		 */
53577754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
53587754SJeff.Bonwick@Sun.COM 
53597754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
53607754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
53617754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
53627754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
53637754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
53649816SGeorge.Wilson@Sun.COM 
53659816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
53667754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
53677754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
53687754SJeff.Bonwick@Sun.COM 					continue;
53697754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
53707754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
53717754SJeff.Bonwick@Sun.COM 					break;
53727754SJeff.Bonwick@Sun.COM 			}
53739725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
53749725SEric.Schrock@Sun.COM 			if (error != 0)
53759725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
53769725SEric.Schrock@Sun.COM 				    B_TRUE);
53777754SJeff.Bonwick@Sun.COM 		} else {
53787754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
53799725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
53809725SEric.Schrock@Sun.COM 			if (error != 0)
53819725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
53829725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
53831635Sbonwick 		}
53847754SJeff.Bonwick@Sun.COM 
53857754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
53867754SJeff.Bonwick@Sun.COM 
53877754SJeff.Bonwick@Sun.COM 		if (error == 0)
53887754SJeff.Bonwick@Sun.COM 			break;
53897754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
53907754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
53911635Sbonwick 	}
53922082Seschrock 	dmu_tx_commit(tx);
53932082Seschrock 
53941635Sbonwick 	/*
53951635Sbonwick 	 * Clear the dirty config list.
53961635Sbonwick 	 */
53977754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
53981635Sbonwick 		vdev_config_clean(vd);
53991635Sbonwick 
54001635Sbonwick 	/*
54011635Sbonwick 	 * Now that the new config has synced transactionally,
54021635Sbonwick 	 * let it become visible to the config cache.
54031635Sbonwick 	 */
54041635Sbonwick 	if (spa->spa_config_syncing != NULL) {
54051635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
54061635Sbonwick 		spa->spa_config_txg = txg;
54071635Sbonwick 		spa->spa_config_syncing = NULL;
54081635Sbonwick 	}
5409789Sahrens 
5410789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
5411789Sahrens 
541210922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
5413789Sahrens 
5414789Sahrens 	/*
5415789Sahrens 	 * Update usable space statistics.
5416789Sahrens 	 */
5417789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5418789Sahrens 		vdev_sync_done(vd, txg);
5419789Sahrens 
542010956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
542110956SGeorge.Wilson@Sun.COM 
5422789Sahrens 	/*
5423789Sahrens 	 * It had better be the case that we didn't dirty anything
54242082Seschrock 	 * since vdev_config_sync().
5425789Sahrens 	 */
5426789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5427789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5428789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
542911932SGeorge.Wilson@Sun.COM 	ASSERT(list_is_empty(&defer_bpl->bpl_queue));
543011932SGeorge.Wilson@Sun.COM 	ASSERT(list_is_empty(&free_bpl->bpl_queue));
543110922SJeff.Bonwick@Sun.COM 
543210922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
5433789Sahrens 
54347754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
54351544Seschrock 
543610921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
543710921STim.Haley@Sun.COM 
54381544Seschrock 	/*
54391544Seschrock 	 * If any async tasks have been requested, kick them off.
54401544Seschrock 	 */
54411544Seschrock 	spa_async_dispatch(spa);
5442789Sahrens }
5443789Sahrens 
5444789Sahrens /*
5445789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
5446789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
5447789Sahrens  * sync.
5448789Sahrens  */
5449789Sahrens void
5450789Sahrens spa_sync_allpools(void)
5451789Sahrens {
5452789Sahrens 	spa_t *spa = NULL;
5453789Sahrens 	mutex_enter(&spa_namespace_lock);
5454789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
54557754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
5456789Sahrens 			continue;
5457789Sahrens 		spa_open_ref(spa, FTAG);
5458789Sahrens 		mutex_exit(&spa_namespace_lock);
5459789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
5460789Sahrens 		mutex_enter(&spa_namespace_lock);
5461789Sahrens 		spa_close(spa, FTAG);
5462789Sahrens 	}
5463789Sahrens 	mutex_exit(&spa_namespace_lock);
5464789Sahrens }
5465789Sahrens 
5466789Sahrens /*
5467789Sahrens  * ==========================================================================
5468789Sahrens  * Miscellaneous routines
5469789Sahrens  * ==========================================================================
5470789Sahrens  */
5471789Sahrens 
5472789Sahrens /*
5473789Sahrens  * Remove all pools in the system.
5474789Sahrens  */
5475789Sahrens void
5476789Sahrens spa_evict_all(void)
5477789Sahrens {
5478789Sahrens 	spa_t *spa;
5479789Sahrens 
5480789Sahrens 	/*
5481789Sahrens 	 * Remove all cached state.  All pools should be closed now,
5482789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
5483789Sahrens 	 */
5484789Sahrens 	mutex_enter(&spa_namespace_lock);
5485789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
5486789Sahrens 		/*
54871544Seschrock 		 * Stop async tasks.  The async thread may need to detach
54881544Seschrock 		 * a device that's been replaced, which requires grabbing
54891544Seschrock 		 * spa_namespace_lock, so we must drop it here.
5490789Sahrens 		 */
5491789Sahrens 		spa_open_ref(spa, FTAG);
5492789Sahrens 		mutex_exit(&spa_namespace_lock);
54931544Seschrock 		spa_async_suspend(spa);
54944808Sek110237 		mutex_enter(&spa_namespace_lock);
5495789Sahrens 		spa_close(spa, FTAG);
5496789Sahrens 
5497789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5498789Sahrens 			spa_unload(spa);
5499789Sahrens 			spa_deactivate(spa);
5500789Sahrens 		}
5501789Sahrens 		spa_remove(spa);
5502789Sahrens 	}
5503789Sahrens 	mutex_exit(&spa_namespace_lock);
5504789Sahrens }
55051544Seschrock 
55061544Seschrock vdev_t *
55079425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
55081544Seschrock {
55096643Seschrock 	vdev_t *vd;
55106643Seschrock 	int i;
55116643Seschrock 
55126643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
55136643Seschrock 		return (vd);
55146643Seschrock 
55159425SEric.Schrock@Sun.COM 	if (aux) {
55166643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
55176643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
55186643Seschrock 			if (vd->vdev_guid == guid)
55196643Seschrock 				return (vd);
55206643Seschrock 		}
55219425SEric.Schrock@Sun.COM 
55229425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
55239425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
55249425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
55259425SEric.Schrock@Sun.COM 				return (vd);
55269425SEric.Schrock@Sun.COM 		}
55276643Seschrock 	}
55286643Seschrock 
55296643Seschrock 	return (NULL);
55301544Seschrock }
55311760Seschrock 
55321760Seschrock void
55335094Slling spa_upgrade(spa_t *spa, uint64_t version)
55341760Seschrock {
55357754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
55361760Seschrock 
55371760Seschrock 	/*
55381760Seschrock 	 * This should only be called for a non-faulted pool, and since a
55391760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
55401760Seschrock 	 * possible.
55411760Seschrock 	 */
55424577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
55435094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
55445094Slling 
55455094Slling 	spa->spa_uberblock.ub_version = version;
55461760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
55471760Seschrock 
55487754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
55492082Seschrock 
55502082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
55511760Seschrock }
55522082Seschrock 
55532082Seschrock boolean_t
55542082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
55552082Seschrock {
55562082Seschrock 	int i;
55573377Seschrock 	uint64_t spareguid;
55585450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
55595450Sbrendan 
55605450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
55615450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
55622082Seschrock 			return (B_TRUE);
55632082Seschrock 
55645450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
55655450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
55665450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
55673377Seschrock 			return (B_TRUE);
55683377Seschrock 	}
55693377Seschrock 
55702082Seschrock 	return (B_FALSE);
55712082Seschrock }
55723912Slling 
55734451Seschrock /*
55747214Slling  * Check if a pool has an active shared spare device.
55757214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
55767214Slling  */
55777214Slling static boolean_t
55787214Slling spa_has_active_shared_spare(spa_t *spa)
55797214Slling {
55807214Slling 	int i, refcnt;
55817214Slling 	uint64_t pool;
55827214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
55837214Slling 
55847214Slling 	for (i = 0; i < sav->sav_count; i++) {
55857214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
55867214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
55877214Slling 		    refcnt > 2)
55887214Slling 			return (B_TRUE);
55897214Slling 	}
55907214Slling 
55917214Slling 	return (B_FALSE);
55927214Slling }
55937214Slling 
55947214Slling /*
55954451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
55964451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
55974451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
55984451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
55994451Seschrock  * or zdb as real changes.
56004451Seschrock  */
56014451Seschrock void
56024451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
56034451Seschrock {
56044451Seschrock #ifdef _KERNEL
56054451Seschrock 	sysevent_t		*ev;
56064451Seschrock 	sysevent_attr_list_t	*attr = NULL;
56074451Seschrock 	sysevent_value_t	value;
56084451Seschrock 	sysevent_id_t		eid;
56094451Seschrock 
56104451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
56114451Seschrock 	    SE_SLEEP);
56124451Seschrock 
56134451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
56144451Seschrock 	value.value.sv_string = spa_name(spa);
56154451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
56164451Seschrock 		goto done;
56174451Seschrock 
56184451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
56194451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
56204451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
56214451Seschrock 		goto done;
56224451Seschrock 
56234451Seschrock 	if (vd) {
56244451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
56254451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
56264451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
56274451Seschrock 		    SE_SLEEP) != 0)
56284451Seschrock 			goto done;
56294451Seschrock 
56304451Seschrock 		if (vd->vdev_path) {
56314451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
56324451Seschrock 			value.value.sv_string = vd->vdev_path;
56334451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
56344451Seschrock 			    &value, SE_SLEEP) != 0)
56354451Seschrock 				goto done;
56364451Seschrock 		}
56374451Seschrock 	}
56384451Seschrock 
56395756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
56405756Seschrock 		goto done;
56415756Seschrock 	attr = NULL;
56425756Seschrock 
56434451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
56444451Seschrock 
56454451Seschrock done:
56464451Seschrock 	if (attr)
56474451Seschrock 		sysevent_free_attr(attr);
56484451Seschrock 	sysevent_free(ev);
56494451Seschrock #endif
56504451Seschrock }
5651