xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 11864)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
2311422SMark.Musante@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens /*
28789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
29789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
30789Sahrens  * pool.
31789Sahrens  */
32789Sahrens 
33789Sahrens #include <sys/zfs_context.h>
341544Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens #include <sys/spa_impl.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/zio_checksum.h>
38789Sahrens #include <sys/dmu.h>
39789Sahrens #include <sys/dmu_tx.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
4210922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
4510594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
46789Sahrens #include <sys/uberblock_impl.h>
47789Sahrens #include <sys/txg.h>
48789Sahrens #include <sys/avl.h>
49789Sahrens #include <sys/dmu_traverse.h>
503912Slling #include <sys/dmu_objset.h>
51789Sahrens #include <sys/unique.h>
52789Sahrens #include <sys/dsl_pool.h>
533912Slling #include <sys/dsl_dataset.h>
54789Sahrens #include <sys/dsl_dir.h>
55789Sahrens #include <sys/dsl_prop.h>
563912Slling #include <sys/dsl_synctask.h>
57789Sahrens #include <sys/fs/zfs.h>
585450Sbrendan #include <sys/arc.h>
59789Sahrens #include <sys/callb.h>
603975Sek110237 #include <sys/systeminfo.h>
616423Sgw25295 #include <sys/spa_boot.h>
629816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h>
63789Sahrens 
648662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
6511173SJonathan.Adams@Sun.COM #include <sys/bootprops.h>
6611173SJonathan.Adams@Sun.COM #include <sys/callb.h>
6711173SJonathan.Adams@Sun.COM #include <sys/cpupart.h>
6811173SJonathan.Adams@Sun.COM #include <sys/pool.h>
6911173SJonathan.Adams@Sun.COM #include <sys/sysdc.h>
708662SJordan.Vaughan@Sun.com #include <sys/zone.h>
718662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
728662SJordan.Vaughan@Sun.com 
735094Slling #include "zfs_prop.h"
745913Sperrin #include "zfs_comutil.h"
755094Slling 
7611173SJonathan.Adams@Sun.COM typedef enum zti_modes {
779515SJonathan.Adams@Sun.COM 	zti_mode_fixed,			/* value is # of threads (min 1) */
789515SJonathan.Adams@Sun.COM 	zti_mode_online_percent,	/* value is % of online CPUs */
7911173SJonathan.Adams@Sun.COM 	zti_mode_batch,			/* cpu-intensive; value is ignored */
8011146SGeorge.Wilson@Sun.COM 	zti_mode_null,			/* don't create a taskq */
819515SJonathan.Adams@Sun.COM 	zti_nmodes
8211173SJonathan.Adams@Sun.COM } zti_modes_t;
832986Sek110237 
8411146SGeorge.Wilson@Sun.COM #define	ZTI_FIX(n)	{ zti_mode_fixed, (n) }
8511146SGeorge.Wilson@Sun.COM #define	ZTI_PCT(n)	{ zti_mode_online_percent, (n) }
8611173SJonathan.Adams@Sun.COM #define	ZTI_BATCH	{ zti_mode_batch, 0 }
8711146SGeorge.Wilson@Sun.COM #define	ZTI_NULL	{ zti_mode_null, 0 }
8811146SGeorge.Wilson@Sun.COM 
8911146SGeorge.Wilson@Sun.COM #define	ZTI_ONE		ZTI_FIX(1)
909515SJonathan.Adams@Sun.COM 
919515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info {
9211146SGeorge.Wilson@Sun.COM 	enum zti_modes zti_mode;
9311146SGeorge.Wilson@Sun.COM 	uint_t zti_value;
949515SJonathan.Adams@Sun.COM } zio_taskq_info_t;
959515SJonathan.Adams@Sun.COM 
969515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
9711173SJonathan.Adams@Sun.COM 	"issue", "issue_high", "intr", "intr_high"
989515SJonathan.Adams@Sun.COM };
999515SJonathan.Adams@Sun.COM 
10011146SGeorge.Wilson@Sun.COM /*
10111146SGeorge.Wilson@Sun.COM  * Define the taskq threads for the following I/O types:
10211146SGeorge.Wilson@Sun.COM  * 	NULL, READ, WRITE, FREE, CLAIM, and IOCTL
10311146SGeorge.Wilson@Sun.COM  */
10411146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
10511146SGeorge.Wilson@Sun.COM 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
10611146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
10711173SJonathan.Adams@Sun.COM 	{ ZTI_FIX(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL },
10811173SJonathan.Adams@Sun.COM 	{ ZTI_BATCH,	ZTI_FIX(5),	ZTI_FIX(8),	ZTI_FIX(5) },
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 
1145094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
1157214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
11611422SMark.Musante@Sun.COM static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
11711422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
11811422SMark.Musante@Sun.COM     char **ereport);
1195094Slling 
12011173SJonathan.Adams@Sun.COM uint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
12111173SJonathan.Adams@Sun.COM id_t		zio_taskq_psrset_bind = PS_NONE;
12211173SJonathan.Adams@Sun.COM boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
12311173SJonathan.Adams@Sun.COM uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
12411173SJonathan.Adams@Sun.COM 
12511173SJonathan.Adams@Sun.COM boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
12611173SJonathan.Adams@Sun.COM 
12711173SJonathan.Adams@Sun.COM /*
12811173SJonathan.Adams@Sun.COM  * This (illegal) pool name is used when temporarily importing a spa_t in order
12911173SJonathan.Adams@Sun.COM  * to get the vdev stats associated with the imported devices.
13011173SJonathan.Adams@Sun.COM  */
13111173SJonathan.Adams@Sun.COM #define	TRYIMPORT_NAME	"$import"
13211173SJonathan.Adams@Sun.COM 
1335094Slling /*
1345094Slling  * ==========================================================================
1355094Slling  * SPA properties routines
1365094Slling  * ==========================================================================
1375094Slling  */
1385094Slling 
1395094Slling /*
1405094Slling  * Add a (source=src, propname=propval) list to an nvlist.
1415094Slling  */
1425949Slling static void
1435094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
1445094Slling     uint64_t intval, zprop_source_t src)
1455094Slling {
1465094Slling 	const char *propname = zpool_prop_to_name(prop);
1475094Slling 	nvlist_t *propval;
1485949Slling 
1495949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1505949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1515949Slling 
1525949Slling 	if (strval != NULL)
1535949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1545949Slling 	else
1555949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1565949Slling 
1575949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1585094Slling 	nvlist_free(propval);
1595094Slling }
1605094Slling 
1615094Slling /*
1625094Slling  * Get property values from the spa configuration.
1635094Slling  */
1645949Slling static void
1655094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1665094Slling {
1678525SEric.Schrock@Sun.COM 	uint64_t size;
16810956SGeorge.Wilson@Sun.COM 	uint64_t alloc;
1695094Slling 	uint64_t cap, version;
1705094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1716643Seschrock 	spa_config_dirent_t *dp;
1725094Slling 
1737754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1747754SJeff.Bonwick@Sun.COM 
1758525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
17610956SGeorge.Wilson@Sun.COM 		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
17710922SJeff.Bonwick@Sun.COM 		size = metaslab_class_get_space(spa_normal_class(spa));
1788525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1798525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
18010956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
18110956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
18210956SGeorge.Wilson@Sun.COM 		    size - alloc, src);
18310956SGeorge.Wilson@Sun.COM 
18410956SGeorge.Wilson@Sun.COM 		cap = (size == 0) ? 0 : (alloc * 100 / size);
1858525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1868525SEric.Schrock@Sun.COM 
18710922SJeff.Bonwick@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
18810922SJeff.Bonwick@Sun.COM 		    ddt_get_pool_dedup_ratio(spa), src);
18910922SJeff.Bonwick@Sun.COM 
1908525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1918525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1928525SEric.Schrock@Sun.COM 
1938525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1948525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1958525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1968525SEric.Schrock@Sun.COM 		else
1978525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1988525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1998525SEric.Schrock@Sun.COM 	}
2005949Slling 
2015949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2025949Slling 
2035949Slling 	if (spa->spa_root != NULL)
2045949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2055949Slling 		    0, ZPROP_SRC_LOCAL);
2065094Slling 
2076643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
2086643Seschrock 		if (dp->scd_path == NULL) {
2095949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2106643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
2116643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2125949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2136643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2145363Seschrock 		}
2155363Seschrock 	}
2165094Slling }
2175094Slling 
2185094Slling /*
2195094Slling  * Get zpool property values.
2205094Slling  */
2215094Slling int
2225094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2235094Slling {
22410922SJeff.Bonwick@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
2255094Slling 	zap_cursor_t zc;
2265094Slling 	zap_attribute_t za;
2275094Slling 	int err;
2285094Slling 
2295949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2305094Slling 
2317754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2327754SJeff.Bonwick@Sun.COM 
2335094Slling 	/*
2345094Slling 	 * Get properties from the spa config.
2355094Slling 	 */
2365949Slling 	spa_prop_get_config(spa, nvp);
2375094Slling 
2385094Slling 	/* If no pool property object, no more prop to get. */
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,
11095450Sbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
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,
12355450Sbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
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,
143310922SJeff.Bonwick@Sun.COM     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;
178410922SJeff.Bonwick@Sun.COM 
17851544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
178611422SMark.Musante@Sun.COM 	if (error)
178711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1788789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1789789Sahrens 
179011422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
179111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
179211422SMark.Musante@Sun.COM 
1793789Sahrens 	if (!mosconfig) {
17943975Sek110237 		uint64_t hostid;
179511810SMark.Musante@Sun.COM 		nvlist_t *policy = NULL, *nvconfig;
179611810SMark.Musante@Sun.COM 
179711810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
179811810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
17992082Seschrock 
180010594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
18017706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
18023975Sek110237 			char *hostname;
18033975Sek110237 			unsigned long myhostid = 0;
18043975Sek110237 
180510594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
18063975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
18073975Sek110237 
18088662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
18098662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
18108662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
18118662SJordan.Vaughan@Sun.com 			/*
18128662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
18138662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
18148662SJordan.Vaughan@Sun.com 			 */
18153975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
18168662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
18174178Slling 			if (hostid != 0 && myhostid != 0 &&
18188662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
181911810SMark.Musante@Sun.COM 				nvlist_free(nvconfig);
18203975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
18213975Sek110237 				    "loaded as it was last accessed by "
18227706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
18233975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
18247754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
18253975Sek110237 				    (unsigned long)hostid);
182611422SMark.Musante@Sun.COM 				return (EBADF);
18273975Sek110237 			}
18283975Sek110237 		}
182911727SVictor.Latushkin@Sun.COM 		if (nvlist_lookup_nvlist(spa->spa_config,
183011727SVictor.Latushkin@Sun.COM 		    ZPOOL_REWIND_POLICY, &policy) == 0)
183111727SVictor.Latushkin@Sun.COM 			VERIFY(nvlist_add_nvlist(nvconfig,
183211727SVictor.Latushkin@Sun.COM 			    ZPOOL_REWIND_POLICY, policy) == 0);
18333975Sek110237 
183410594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1835789Sahrens 		spa_unload(spa);
1836789Sahrens 		spa_deactivate(spa);
18378241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1838789Sahrens 
183911422SMark.Musante@Sun.COM 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
18401544Seschrock 	}
18411544Seschrock 
184211422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPLIST,
184311422SMark.Musante@Sun.COM 	    &spa->spa_deferred_bplist_obj) != 0)
184411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1845789Sahrens 
18461544Seschrock 	/*
18472082Seschrock 	 * Load the bit that tells us to use the new accounting function
18482082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
18492082Seschrock 	 * be present.
18502082Seschrock 	 */
185111422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
185211422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18542082Seschrock 
18552082Seschrock 	/*
18561544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
18571544Seschrock 	 * not be present.
18581544Seschrock 	 */
185911422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
186011422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
186111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
186211422SMark.Musante@Sun.COM 
186311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
186411422SMark.Musante@Sun.COM 	    &spa->spa_errlog_scrub);
186511422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
186611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1867789Sahrens 
1868789Sahrens 	/*
18692926Sek110237 	 * Load the history object.  If we have an older pool, this
18702926Sek110237 	 * will not be present.
18712926Sek110237 	 */
187211422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
187311422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
187411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
187511422SMark.Musante@Sun.COM 
187611422SMark.Musante@Sun.COM 	/*
187711422SMark.Musante@Sun.COM 	 * If we're assembling the pool from the split-off vdevs of
187811422SMark.Musante@Sun.COM 	 * an existing pool, we don't want to attach the spares & cache
187911422SMark.Musante@Sun.COM 	 * devices.
188011422SMark.Musante@Sun.COM 	 */
18812926Sek110237 
18822926Sek110237 	/*
18832082Seschrock 	 * Load any hot spares for this pool.
18842082Seschrock 	 */
188511422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
188611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
188711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
188811422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
18894577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
18905450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
189111422SMark.Musante@Sun.COM 		    &spa->spa_spares.sav_config) != 0)
189211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18932082Seschrock 
18947754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
18952082Seschrock 		spa_load_spares(spa);
18967754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
189711422SMark.Musante@Sun.COM 	} else if (error == 0) {
189811422SMark.Musante@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
18992082Seschrock 	}
19002082Seschrock 
19015450Sbrendan 	/*
19025450Sbrendan 	 * Load any level 2 ARC devices for this pool.
19035450Sbrendan 	 */
190411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
19055450Sbrendan 	    &spa->spa_l2cache.sav_object);
190611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
190711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
190811422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19095450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
19105450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
191111422SMark.Musante@Sun.COM 		    &spa->spa_l2cache.sav_config) != 0)
191211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19135450Sbrendan 
19147754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19155450Sbrendan 		spa_load_l2cache(spa);
19167754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
191711422SMark.Musante@Sun.COM 	} else if (error == 0) {
191811422SMark.Musante@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
19195450Sbrendan 	}
19205450Sbrendan 
19215094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19224543Smarks 
192311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
192411422SMark.Musante@Sun.COM 	if (error && error != ENOENT)
192511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19263912Slling 
19273912Slling 	if (error == 0) {
192811422SMark.Musante@Sun.COM 		uint64_t autoreplace;
192911422SMark.Musante@Sun.COM 
193011422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
193111422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
193211422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
193311422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
193411422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
193511422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
193611422SMark.Musante@Sun.COM 		    &spa->spa_dedup_ditto);
193711422SMark.Musante@Sun.COM 
193810672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
19393912Slling 	}
19403912Slling 
19412082Seschrock 	/*
19424451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
19434451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
19444451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
19454451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
19464451Seschrock 	 * over.
19474451Seschrock 	 */
194810672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
19494451Seschrock 		spa_check_removed(spa->spa_root_vdev);
195010672SEric.Schrock@Sun.COM 		/*
195110672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
195210672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
195310672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
195410672SEric.Schrock@Sun.COM 		 */
195510672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
195610672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
195710672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
195810672SEric.Schrock@Sun.COM 		}
195910672SEric.Schrock@Sun.COM 	}
19604451Seschrock 
19614451Seschrock 	/*
19621986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1963789Sahrens 	 */
19641986Seschrock 	vdev_load(rvd);
1965789Sahrens 
1966789Sahrens 	/*
1967789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1968789Sahrens 	 */
19697754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1970789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
19717754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1972789Sahrens 
1973789Sahrens 	/*
1974789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1975789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1976789Sahrens 	 */
197711422SMark.Musante@Sun.COM 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
197811422SMark.Musante@Sun.COM 		return (ENXIO);
1979789Sahrens 
198010922SJeff.Bonwick@Sun.COM 	/*
198110922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
198210922SJeff.Bonwick@Sun.COM 	 */
198310922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
198411422SMark.Musante@Sun.COM 	if (error != 0)
198511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
198610922SJeff.Bonwick@Sun.COM 
198710956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
198810956SGeorge.Wilson@Sun.COM 
198910921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
199010921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
199111422SMark.Musante@Sun.COM 		if (error)
199211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
199311422SMark.Musante@Sun.COM 			    error));
199410921STim.Haley@Sun.COM 	}
199510921STim.Haley@Sun.COM 
199610922SJeff.Bonwick@Sun.COM 	/*
199711422SMark.Musante@Sun.COM 	 * Load the intent log state and check log integrity.  If we're
199811422SMark.Musante@Sun.COM 	 * assembling a pool from a split, the log is not transferred over.
199910922SJeff.Bonwick@Sun.COM 	 */
200011422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
200111810SMark.Musante@Sun.COM 		nvlist_t *nvconfig;
200211810SMark.Musante@Sun.COM 
200311810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
200411810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
200511810SMark.Musante@Sun.COM 
200611422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
200711422SMark.Musante@Sun.COM 		    &nvroot) == 0);
200811422SMark.Musante@Sun.COM 		spa_load_log_state(spa, nvroot);
200911422SMark.Musante@Sun.COM 		nvlist_free(nvconfig);
201011422SMark.Musante@Sun.COM 
201111422SMark.Musante@Sun.COM 		if (spa_check_logs(spa)) {
201211422SMark.Musante@Sun.COM 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
201311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
201411422SMark.Musante@Sun.COM 		}
201510922SJeff.Bonwick@Sun.COM 	}
201610922SJeff.Bonwick@Sun.COM 
201710921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
201810921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
20191635Sbonwick 		dmu_tx_t *tx;
20201635Sbonwick 		int need_update = B_FALSE;
20218241SJeff.Bonwick@Sun.COM 
20228241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
20231601Sbonwick 
20241635Sbonwick 		/*
20251635Sbonwick 		 * Claim log blocks that haven't been committed yet.
20261635Sbonwick 		 * This must all happen in a single txg.
202710922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
202810922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
202910921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
20301635Sbonwick 		 */
203110922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
203210922SJeff.Bonwick@Sun.COM 
20331601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2034789Sahrens 		    spa_first_txg(spa));
20357754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
20362417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2037789Sahrens 		dmu_tx_commit(tx);
2038789Sahrens 
203910922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
204010922SJeff.Bonwick@Sun.COM 
204111422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_GOOD);
2042789Sahrens 		spa->spa_sync_on = B_TRUE;
2043789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2044789Sahrens 
2045789Sahrens 		/*
204610922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
204710922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
204810922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
204910922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
205010922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
2051789Sahrens 		 */
205210922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
20531585Sbonwick 
20541585Sbonwick 		/*
20551635Sbonwick 		 * If the config cache is stale, or we have uninitialized
20561635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
205710100SLin.Ling@Sun.COM 		 *
205810100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
205910100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
20601585Sbonwick 		 */
20611635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
206210921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
206310921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
20641635Sbonwick 			need_update = B_TRUE;
20651635Sbonwick 
20668241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
20671635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
20681635Sbonwick 				need_update = B_TRUE;
20691585Sbonwick 
20701585Sbonwick 		/*
20711635Sbonwick 		 * Update the config cache asychronously in case we're the
20721635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
20731585Sbonwick 		 */
20741635Sbonwick 		if (need_update)
20751635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20768241SJeff.Bonwick@Sun.COM 
20778241SJeff.Bonwick@Sun.COM 		/*
20788241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
20798241SJeff.Bonwick@Sun.COM 		 */
20808241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
20818241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
208210298SMatthew.Ahrens@Sun.COM 
208310298SMatthew.Ahrens@Sun.COM 		/*
208410298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
208510298SMatthew.Ahrens@Sun.COM 		 */
208610298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
208710298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
208810342Schris.kirby@sun.com 
208910342Schris.kirby@sun.com 		/*
209010342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
209110342Schris.kirby@sun.com 		 */
209210342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2093789Sahrens 	}
2094789Sahrens 
209511422SMark.Musante@Sun.COM 	return (0);
2096789Sahrens }
2097789Sahrens 
209810921STim.Haley@Sun.COM static int
209910921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
210010921STim.Haley@Sun.COM {
210110921STim.Haley@Sun.COM 	spa_unload(spa);
210210921STim.Haley@Sun.COM 	spa_deactivate(spa);
210310921STim.Haley@Sun.COM 
210410921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
210510921STim.Haley@Sun.COM 
210610921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
210710921STim.Haley@Sun.COM 	spa_async_suspend(spa);
210810921STim.Haley@Sun.COM 
210911422SMark.Musante@Sun.COM 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
211010921STim.Haley@Sun.COM }
211110921STim.Haley@Sun.COM 
211210921STim.Haley@Sun.COM static int
211310921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
211411727SVictor.Latushkin@Sun.COM     uint64_t max_request, int rewind_flags)
211510921STim.Haley@Sun.COM {
211610921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
211710921STim.Haley@Sun.COM 	int load_error, rewind_error;
211811727SVictor.Latushkin@Sun.COM 	uint64_t safe_rewind_txg;
211910921STim.Haley@Sun.COM 	uint64_t min_txg;
212010921STim.Haley@Sun.COM 
212111026STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
212210921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
212311422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
212411026STim.Haley@Sun.COM 	} else {
212510921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
212611026STim.Haley@Sun.COM 	}
212710921STim.Haley@Sun.COM 
212811422SMark.Musante@Sun.COM 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
212911422SMark.Musante@Sun.COM 	    mosconfig);
213010921STim.Haley@Sun.COM 	if (load_error == 0)
213110921STim.Haley@Sun.COM 		return (0);
213210921STim.Haley@Sun.COM 
213310921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
213410921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
213510921STim.Haley@Sun.COM 
213610921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
213710921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
213810921STim.Haley@Sun.COM 
213911727SVictor.Latushkin@Sun.COM 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
214010921STim.Haley@Sun.COM 		nvlist_free(config);
214110921STim.Haley@Sun.COM 		return (load_error);
214210921STim.Haley@Sun.COM 	}
214310921STim.Haley@Sun.COM 
214410921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
214510921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
214611422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
214710921STim.Haley@Sun.COM 
214811727SVictor.Latushkin@Sun.COM 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
214911727SVictor.Latushkin@Sun.COM 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
215011727SVictor.Latushkin@Sun.COM 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
215111727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL : safe_rewind_txg;
215211727SVictor.Latushkin@Sun.COM 
215311727SVictor.Latushkin@Sun.COM 	/*
215411727SVictor.Latushkin@Sun.COM 	 * Continue as long as we're finding errors, we're still within
215511727SVictor.Latushkin@Sun.COM 	 * the acceptable rewind range, and we're still finding uberblocks
215611727SVictor.Latushkin@Sun.COM 	 */
215711727SVictor.Latushkin@Sun.COM 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
215811727SVictor.Latushkin@Sun.COM 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
215911727SVictor.Latushkin@Sun.COM 		if (spa->spa_load_max_txg < safe_rewind_txg)
216010921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
216110921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
216210921STim.Haley@Sun.COM 	}
216310921STim.Haley@Sun.COM 
216410921STim.Haley@Sun.COM 	if (config)
216510921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
216610921STim.Haley@Sun.COM 
216710921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
216810921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
216910921STim.Haley@Sun.COM 
217010921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
217110921STim.Haley@Sun.COM 		spa_config_set(spa, config);
217210921STim.Haley@Sun.COM 
217310921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
217410921STim.Haley@Sun.COM }
217510921STim.Haley@Sun.COM 
2176789Sahrens /*
2177789Sahrens  * Pool Open/Import
2178789Sahrens  *
2179789Sahrens  * The import case is identical to an open except that the configuration is sent
2180789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2181789Sahrens  * case of an open, the pool configuration will exist in the
21824451Seschrock  * POOL_STATE_UNINITIALIZED state.
2183789Sahrens  *
2184789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2185789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2186789Sahrens  * ambiguous state.
2187789Sahrens  */
2188789Sahrens static int
218910921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
219010921STim.Haley@Sun.COM     nvlist_t **config)
2191789Sahrens {
2192789Sahrens 	spa_t *spa;
2193789Sahrens 	int error;
2194789Sahrens 	int locked = B_FALSE;
2195789Sahrens 
2196789Sahrens 	*spapp = NULL;
2197789Sahrens 
2198789Sahrens 	/*
2199789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2200789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2201789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2202789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2203789Sahrens 	 */
2204789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2205789Sahrens 		mutex_enter(&spa_namespace_lock);
2206789Sahrens 		locked = B_TRUE;
2207789Sahrens 	}
2208789Sahrens 
2209789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2210789Sahrens 		if (locked)
2211789Sahrens 			mutex_exit(&spa_namespace_lock);
2212789Sahrens 		return (ENOENT);
2213789Sahrens 	}
221410921STim.Haley@Sun.COM 
2215789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
221611819STim.Haley@Sun.COM 		spa_load_state_t state = SPA_LOAD_OPEN;
221711819STim.Haley@Sun.COM 		zpool_rewind_policy_t policy;
221811819STim.Haley@Sun.COM 
221911819STim.Haley@Sun.COM 		zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
222011819STim.Haley@Sun.COM 		    &policy);
222111819STim.Haley@Sun.COM 		if (policy.zrp_request & ZPOOL_DO_REWIND)
222211819STim.Haley@Sun.COM 			state = SPA_LOAD_RECOVER;
2223789Sahrens 
22248241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
2225789Sahrens 
222611727SVictor.Latushkin@Sun.COM 		if (spa->spa_last_open_failed && (policy.zrp_request &
222711727SVictor.Latushkin@Sun.COM 		    (ZPOOL_NO_REWIND | ZPOOL_NEVER_REWIND))) {
222810921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
222910921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config,
223010921STim.Haley@Sun.COM 				    config, KM_SLEEP) == 0);
223110921STim.Haley@Sun.COM 			spa_deactivate(spa);
223210921STim.Haley@Sun.COM 			if (locked)
223310921STim.Haley@Sun.COM 				mutex_exit(&spa_namespace_lock);
223410921STim.Haley@Sun.COM 			return (spa->spa_last_open_failed);
223510921STim.Haley@Sun.COM 		}
223610921STim.Haley@Sun.COM 
223710921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
223810921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
223910921STim.Haley@Sun.COM 
224010921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
224111727SVictor.Latushkin@Sun.COM 		    policy.zrp_request);
2242789Sahrens 
2243789Sahrens 		if (error == EBADF) {
2244789Sahrens 			/*
22451986Seschrock 			 * If vdev_validate() returns failure (indicated by
22461986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
22471986Seschrock 			 * that the pool has been exported or destroyed.  If
22481986Seschrock 			 * this is the case, the config cache is out of sync and
22491986Seschrock 			 * we should remove the pool from the namespace.
2250789Sahrens 			 */
2251789Sahrens 			spa_unload(spa);
2252789Sahrens 			spa_deactivate(spa);
22536643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2254789Sahrens 			spa_remove(spa);
2255789Sahrens 			if (locked)
2256789Sahrens 				mutex_exit(&spa_namespace_lock);
2257789Sahrens 			return (ENOENT);
22581544Seschrock 		}
22591544Seschrock 
22601544Seschrock 		if (error) {
2261789Sahrens 			/*
2262789Sahrens 			 * We can't open the pool, but we still have useful
2263789Sahrens 			 * information: the state of each vdev after the
2264789Sahrens 			 * attempted vdev_open().  Return this to the user.
2265789Sahrens 			 */
226610921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
226710921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
226810921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
2269789Sahrens 			spa_unload(spa);
2270789Sahrens 			spa_deactivate(spa);
227110921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
2272789Sahrens 			if (locked)
2273789Sahrens 				mutex_exit(&spa_namespace_lock);
2274789Sahrens 			*spapp = NULL;
2275789Sahrens 			return (error);
2276789Sahrens 		}
227710921STim.Haley@Sun.COM 
2278789Sahrens 	}
2279789Sahrens 
2280789Sahrens 	spa_open_ref(spa, tag);
22814451Seschrock 
228210921STim.Haley@Sun.COM 
228310921STim.Haley@Sun.COM 	if (config != NULL)
228410921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
228510921STim.Haley@Sun.COM 
228611026STim.Haley@Sun.COM 	if (locked) {
228711026STim.Haley@Sun.COM 		spa->spa_last_open_failed = 0;
228811026STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = 0;
228911026STim.Haley@Sun.COM 		spa->spa_load_txg = 0;
2290789Sahrens 		mutex_exit(&spa_namespace_lock);
229111026STim.Haley@Sun.COM 	}
2292789Sahrens 
2293789Sahrens 	*spapp = spa;
2294789Sahrens 
2295789Sahrens 	return (0);
2296789Sahrens }
2297789Sahrens 
2298789Sahrens int
229910921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
230010921STim.Haley@Sun.COM     nvlist_t **config)
230110921STim.Haley@Sun.COM {
230210921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
230310921STim.Haley@Sun.COM }
230410921STim.Haley@Sun.COM 
230510921STim.Haley@Sun.COM int
2306789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2307789Sahrens {
230810921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2309789Sahrens }
2310789Sahrens 
23111544Seschrock /*
23121544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
23131544Seschrock  * preventing it from being exported or destroyed.
23141544Seschrock  */
23151544Seschrock spa_t *
23161544Seschrock spa_inject_addref(char *name)
23171544Seschrock {
23181544Seschrock 	spa_t *spa;
23191544Seschrock 
23201544Seschrock 	mutex_enter(&spa_namespace_lock);
23211544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
23221544Seschrock 		mutex_exit(&spa_namespace_lock);
23231544Seschrock 		return (NULL);
23241544Seschrock 	}
23251544Seschrock 	spa->spa_inject_ref++;
23261544Seschrock 	mutex_exit(&spa_namespace_lock);
23271544Seschrock 
23281544Seschrock 	return (spa);
23291544Seschrock }
23301544Seschrock 
23311544Seschrock void
23321544Seschrock spa_inject_delref(spa_t *spa)
23331544Seschrock {
23341544Seschrock 	mutex_enter(&spa_namespace_lock);
23351544Seschrock 	spa->spa_inject_ref--;
23361544Seschrock 	mutex_exit(&spa_namespace_lock);
23371544Seschrock }
23381544Seschrock 
23395450Sbrendan /*
23405450Sbrendan  * Add spares device information to the nvlist.
23415450Sbrendan  */
23422082Seschrock static void
23432082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
23442082Seschrock {
23452082Seschrock 	nvlist_t **spares;
23462082Seschrock 	uint_t i, nspares;
23472082Seschrock 	nvlist_t *nvroot;
23482082Seschrock 	uint64_t guid;
23492082Seschrock 	vdev_stat_t *vs;
23502082Seschrock 	uint_t vsc;
23513377Seschrock 	uint64_t pool;
23522082Seschrock 
23539425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23549425SEric.Schrock@Sun.COM 
23555450Sbrendan 	if (spa->spa_spares.sav_count == 0)
23562082Seschrock 		return;
23572082Seschrock 
23582082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
23592082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23605450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
23612082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23622082Seschrock 	if (nspares != 0) {
23632082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
23642082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
23652082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23662082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23672082Seschrock 
23682082Seschrock 		/*
23692082Seschrock 		 * Go through and find any spares which have since been
23702082Seschrock 		 * repurposed as an active spare.  If this is the case, update
23712082Seschrock 		 * their status appropriately.
23722082Seschrock 		 */
23732082Seschrock 		for (i = 0; i < nspares; i++) {
23742082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
23752082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23767214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
23777214Slling 			    pool != 0ULL) {
23782082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
23792082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
23802082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
23812082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
23822082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
23832082Seschrock 			}
23842082Seschrock 		}
23852082Seschrock 	}
23862082Seschrock }
23872082Seschrock 
23885450Sbrendan /*
23895450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
23905450Sbrendan  */
23915450Sbrendan static void
23925450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
23935450Sbrendan {
23945450Sbrendan 	nvlist_t **l2cache;
23955450Sbrendan 	uint_t i, j, nl2cache;
23965450Sbrendan 	nvlist_t *nvroot;
23975450Sbrendan 	uint64_t guid;
23985450Sbrendan 	vdev_t *vd;
23995450Sbrendan 	vdev_stat_t *vs;
24005450Sbrendan 	uint_t vsc;
24015450Sbrendan 
24029425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
24039425SEric.Schrock@Sun.COM 
24045450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
24055450Sbrendan 		return;
24065450Sbrendan 
24075450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
24085450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
24095450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
24105450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24115450Sbrendan 	if (nl2cache != 0) {
24125450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
24135450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
24145450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
24155450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24165450Sbrendan 
24175450Sbrendan 		/*
24185450Sbrendan 		 * Update level 2 cache device stats.
24195450Sbrendan 		 */
24205450Sbrendan 
24215450Sbrendan 		for (i = 0; i < nl2cache; i++) {
24225450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
24235450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
24245450Sbrendan 
24255450Sbrendan 			vd = NULL;
24265450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
24275450Sbrendan 				if (guid ==
24285450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
24295450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
24305450Sbrendan 					break;
24315450Sbrendan 				}
24325450Sbrendan 			}
24335450Sbrendan 			ASSERT(vd != NULL);
24345450Sbrendan 
24355450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
24365450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
24375450Sbrendan 			vdev_get_stats(vd, vs);
24385450Sbrendan 		}
24395450Sbrendan 	}
24405450Sbrendan }
24415450Sbrendan 
2442789Sahrens int
24431544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2444789Sahrens {
2445789Sahrens 	int error;
2446789Sahrens 	spa_t *spa;
2447789Sahrens 
2448789Sahrens 	*config = NULL;
244910921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2450789Sahrens 
24519425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24529425SEric.Schrock@Sun.COM 		/*
24539425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
24549425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
24559425SEric.Schrock@Sun.COM 		 * self-inconsistent.
24569425SEric.Schrock@Sun.COM 		 */
24579425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
24589425SEric.Schrock@Sun.COM 
24599425SEric.Schrock@Sun.COM 		if (*config != NULL) {
24607754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
24619425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
24629425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
24639425SEric.Schrock@Sun.COM 
24649425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
24659425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
24669425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
24679425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
24689425SEric.Schrock@Sun.COM 
24699425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
24709425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
24719425SEric.Schrock@Sun.COM 		}
24722082Seschrock 	}
24732082Seschrock 
24741544Seschrock 	/*
24751544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
24761544Seschrock 	 * and call spa_lookup() directly.
24771544Seschrock 	 */
24781544Seschrock 	if (altroot) {
24791544Seschrock 		if (spa == NULL) {
24801544Seschrock 			mutex_enter(&spa_namespace_lock);
24811544Seschrock 			spa = spa_lookup(name);
24821544Seschrock 			if (spa)
24831544Seschrock 				spa_altroot(spa, altroot, buflen);
24841544Seschrock 			else
24851544Seschrock 				altroot[0] = '\0';
24861544Seschrock 			spa = NULL;
24871544Seschrock 			mutex_exit(&spa_namespace_lock);
24881544Seschrock 		} else {
24891544Seschrock 			spa_altroot(spa, altroot, buflen);
24901544Seschrock 		}
24911544Seschrock 	}
24921544Seschrock 
24939425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24949425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2495789Sahrens 		spa_close(spa, FTAG);
24969425SEric.Schrock@Sun.COM 	}
2497789Sahrens 
2498789Sahrens 	return (error);
2499789Sahrens }
2500789Sahrens 
2501789Sahrens /*
25025450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
25035450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
25045450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
25055450Sbrendan  * specified, as long as they are well-formed.
25062082Seschrock  */
25072082Seschrock static int
25085450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
25095450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
25105450Sbrendan     vdev_labeltype_t label)
25112082Seschrock {
25125450Sbrendan 	nvlist_t **dev;
25135450Sbrendan 	uint_t i, ndev;
25142082Seschrock 	vdev_t *vd;
25152082Seschrock 	int error;
25162082Seschrock 
25177754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25187754SJeff.Bonwick@Sun.COM 
25192082Seschrock 	/*
25205450Sbrendan 	 * It's acceptable to have no devs specified.
25212082Seschrock 	 */
25225450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
25232082Seschrock 		return (0);
25242082Seschrock 
25255450Sbrendan 	if (ndev == 0)
25262082Seschrock 		return (EINVAL);
25272082Seschrock 
25282082Seschrock 	/*
25295450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
25305450Sbrendan 	 * device type.
25312082Seschrock 	 */
25325450Sbrendan 	if (spa_version(spa) < version)
25332082Seschrock 		return (ENOTSUP);
25342082Seschrock 
25353377Seschrock 	/*
25365450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
25373377Seschrock 	 * checking.
25383377Seschrock 	 */
25395450Sbrendan 	sav->sav_pending = dev;
25405450Sbrendan 	sav->sav_npending = ndev;
25415450Sbrendan 
25425450Sbrendan 	for (i = 0; i < ndev; i++) {
25435450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
25442082Seschrock 		    mode)) != 0)
25453377Seschrock 			goto out;
25462082Seschrock 
25472082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
25482082Seschrock 			vdev_free(vd);
25493377Seschrock 			error = EINVAL;
25503377Seschrock 			goto out;
25512082Seschrock 		}
25522082Seschrock 
25535450Sbrendan 		/*
25547754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
25557754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
25565450Sbrendan 		 */
25577754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
25585450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
25595450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
25605450Sbrendan 			error = ENOTBLK;
25615450Sbrendan 			goto out;
25625450Sbrendan 		}
25637754SJeff.Bonwick@Sun.COM #endif
25642082Seschrock 		vd->vdev_top = vd;
25653377Seschrock 
25663377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
25675450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
25685450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
25693377Seschrock 			    vd->vdev_guid) == 0);
25702082Seschrock 		}
25712082Seschrock 
25722082Seschrock 		vdev_free(vd);
25733377Seschrock 
25745450Sbrendan 		if (error &&
25755450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
25763377Seschrock 			goto out;
25773377Seschrock 		else
25783377Seschrock 			error = 0;
25792082Seschrock 	}
25802082Seschrock 
25813377Seschrock out:
25825450Sbrendan 	sav->sav_pending = NULL;
25835450Sbrendan 	sav->sav_npending = 0;
25843377Seschrock 	return (error);
25852082Seschrock }
25862082Seschrock 
25875450Sbrendan static int
25885450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
25895450Sbrendan {
25905450Sbrendan 	int error;
25915450Sbrendan 
25927754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25937754SJeff.Bonwick@Sun.COM 
25945450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
25955450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
25965450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
25975450Sbrendan 		return (error);
25985450Sbrendan 	}
25995450Sbrendan 
26005450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
26015450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
26025450Sbrendan 	    VDEV_LABEL_L2CACHE));
26035450Sbrendan }
26045450Sbrendan 
26055450Sbrendan static void
26065450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
26075450Sbrendan     const char *config)
26085450Sbrendan {
26095450Sbrendan 	int i;
26105450Sbrendan 
26115450Sbrendan 	if (sav->sav_config != NULL) {
26125450Sbrendan 		nvlist_t **olddevs;
26135450Sbrendan 		uint_t oldndevs;
26145450Sbrendan 		nvlist_t **newdevs;
26155450Sbrendan 
26165450Sbrendan 		/*
26175450Sbrendan 		 * Generate new dev list by concatentating with the
26185450Sbrendan 		 * current dev list.
26195450Sbrendan 		 */
26205450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
26215450Sbrendan 		    &olddevs, &oldndevs) == 0);
26225450Sbrendan 
26235450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
26245450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
26255450Sbrendan 		for (i = 0; i < oldndevs; i++)
26265450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
26275450Sbrendan 			    KM_SLEEP) == 0);
26285450Sbrendan 		for (i = 0; i < ndevs; i++)
26295450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
26305450Sbrendan 			    KM_SLEEP) == 0);
26315450Sbrendan 
26325450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
26335450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
26345450Sbrendan 
26355450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
26365450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
26375450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
26385450Sbrendan 			nvlist_free(newdevs[i]);
26395450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
26405450Sbrendan 	} else {
26415450Sbrendan 		/*
26425450Sbrendan 		 * Generate a new dev list.
26435450Sbrendan 		 */
26445450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
26455450Sbrendan 		    KM_SLEEP) == 0);
26465450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
26475450Sbrendan 		    devs, ndevs) == 0);
26485450Sbrendan 	}
26495450Sbrendan }
26505450Sbrendan 
26515450Sbrendan /*
26525450Sbrendan  * Stop and drop level 2 ARC devices
26535450Sbrendan  */
26545450Sbrendan void
26555450Sbrendan spa_l2cache_drop(spa_t *spa)
26565450Sbrendan {
26575450Sbrendan 	vdev_t *vd;
26585450Sbrendan 	int i;
26595450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
26605450Sbrendan 
26615450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
26625450Sbrendan 		uint64_t pool;
26635450Sbrendan 
26645450Sbrendan 		vd = sav->sav_vdevs[i];
26655450Sbrendan 		ASSERT(vd != NULL);
26665450Sbrendan 
26678241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
26688241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
26695450Sbrendan 			l2arc_remove_vdev(vd);
26705450Sbrendan 		if (vd->vdev_isl2cache)
26715450Sbrendan 			spa_l2cache_remove(vd);
26725450Sbrendan 		vdev_clear_stats(vd);
26735450Sbrendan 		(void) vdev_close(vd);
26745450Sbrendan 	}
26755450Sbrendan }
26765450Sbrendan 
26772082Seschrock /*
2678789Sahrens  * Pool Creation
2679789Sahrens  */
2680789Sahrens int
26815094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
26827184Stimh     const char *history_str, nvlist_t *zplprops)
2683789Sahrens {
2684789Sahrens 	spa_t *spa;
26855094Slling 	char *altroot = NULL;
26861635Sbonwick 	vdev_t *rvd;
2687789Sahrens 	dsl_pool_t *dp;
2688789Sahrens 	dmu_tx_t *tx;
26899816SGeorge.Wilson@Sun.COM 	int error = 0;
2690789Sahrens 	uint64_t txg = TXG_INITIAL;
26915450Sbrendan 	nvlist_t **spares, **l2cache;
26925450Sbrendan 	uint_t nspares, nl2cache;
26935094Slling 	uint64_t version;
2694789Sahrens 
2695789Sahrens 	/*
2696789Sahrens 	 * If this pool already exists, return failure.
2697789Sahrens 	 */
2698789Sahrens 	mutex_enter(&spa_namespace_lock);
2699789Sahrens 	if (spa_lookup(pool) != NULL) {
2700789Sahrens 		mutex_exit(&spa_namespace_lock);
2701789Sahrens 		return (EEXIST);
2702789Sahrens 	}
2703789Sahrens 
2704789Sahrens 	/*
2705789Sahrens 	 * Allocate a new spa_t structure.
2706789Sahrens 	 */
27075094Slling 	(void) nvlist_lookup_string(props,
27085094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
270910921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
27108241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2711789Sahrens 
27125094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
27135094Slling 		spa_deactivate(spa);
27145094Slling 		spa_remove(spa);
27156643Seschrock 		mutex_exit(&spa_namespace_lock);
27165094Slling 		return (error);
27175094Slling 	}
27185094Slling 
27195094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
27205094Slling 	    &version) != 0)
27215094Slling 		version = SPA_VERSION;
27225094Slling 	ASSERT(version <= SPA_VERSION);
272310922SJeff.Bonwick@Sun.COM 
272410922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
272510922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
27265094Slling 	spa->spa_uberblock.ub_version = version;
2727789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2728789Sahrens 
27291635Sbonwick 	/*
27309234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
27319234SGeorge.Wilson@Sun.COM 	 */
27329630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
27339630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
27349234SGeorge.Wilson@Sun.COM 
27359234SGeorge.Wilson@Sun.COM 	/*
27361635Sbonwick 	 * Create the root vdev.
27371635Sbonwick 	 */
27387754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27391635Sbonwick 
27402082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
27412082Seschrock 
27422082Seschrock 	ASSERT(error != 0 || rvd != NULL);
27432082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
27442082Seschrock 
27455913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
27461635Sbonwick 		error = EINVAL;
27472082Seschrock 
27482082Seschrock 	if (error == 0 &&
27492082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
27505450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
27512082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
27529816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
27539816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
27549816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
27559816SGeorge.Wilson@Sun.COM 		}
27561635Sbonwick 	}
27571635Sbonwick 
27587754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2759789Sahrens 
27602082Seschrock 	if (error != 0) {
2761789Sahrens 		spa_unload(spa);
2762789Sahrens 		spa_deactivate(spa);
2763789Sahrens 		spa_remove(spa);
2764789Sahrens 		mutex_exit(&spa_namespace_lock);
2765789Sahrens 		return (error);
2766789Sahrens 	}
2767789Sahrens 
27682082Seschrock 	/*
27692082Seschrock 	 * Get the list of spares, if specified.
27702082Seschrock 	 */
27712082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
27722082Seschrock 	    &spares, &nspares) == 0) {
27735450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
27742082Seschrock 		    KM_SLEEP) == 0);
27755450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
27762082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
27777754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27782082Seschrock 		spa_load_spares(spa);
27797754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27805450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27815450Sbrendan 	}
27825450Sbrendan 
27835450Sbrendan 	/*
27845450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
27855450Sbrendan 	 */
27865450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
27875450Sbrendan 	    &l2cache, &nl2cache) == 0) {
27885450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
27895450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
27905450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
27915450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
27927754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27935450Sbrendan 		spa_load_l2cache(spa);
27947754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27955450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
27962082Seschrock 	}
27972082Seschrock 
27987184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2799789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2800789Sahrens 
280110956SGeorge.Wilson@Sun.COM 	/*
280210956SGeorge.Wilson@Sun.COM 	 * Create DDTs (dedup tables).
280310956SGeorge.Wilson@Sun.COM 	 */
280410956SGeorge.Wilson@Sun.COM 	ddt_create(spa);
280510956SGeorge.Wilson@Sun.COM 
280610956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
280710956SGeorge.Wilson@Sun.COM 
2808789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2809789Sahrens 
2810789Sahrens 	/*
2811789Sahrens 	 * Create the pool config object.
2812789Sahrens 	 */
2813789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
28147497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2815789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2816789Sahrens 
28171544Seschrock 	if (zap_add(spa->spa_meta_objset,
2818789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
28191544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
28201544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
28211544Seschrock 	}
2822789Sahrens 
28235094Slling 	/* Newly created pools with the right version are always deflated. */
28245094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
28255094Slling 		spa->spa_deflate = TRUE;
28265094Slling 		if (zap_add(spa->spa_meta_objset,
28275094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
28285094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
28295094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
28305094Slling 		}
28312082Seschrock 	}
28322082Seschrock 
2833789Sahrens 	/*
2834789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2835789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2836789Sahrens 	 * keeps changing.
2837789Sahrens 	 */
283810922SJeff.Bonwick@Sun.COM 	spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset,
2839789Sahrens 	    1 << 14, tx);
284010922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset,
284110922SJeff.Bonwick@Sun.COM 	    spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx);
2842789Sahrens 
28431544Seschrock 	if (zap_add(spa->spa_meta_objset,
2844789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
284510922SJeff.Bonwick@Sun.COM 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) {
28461544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
28471544Seschrock 	}
2848789Sahrens 
28492926Sek110237 	/*
28502926Sek110237 	 * Create the pool's history object.
28512926Sek110237 	 */
28525094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
28535094Slling 		spa_history_create_obj(spa, tx);
28545094Slling 
28555094Slling 	/*
28565094Slling 	 * Set pool properties.
28575094Slling 	 */
28585094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
28595094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
28605329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
28619816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
286210922SJeff.Bonwick@Sun.COM 
28638525SEric.Schrock@Sun.COM 	if (props != NULL) {
28648525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
28655094Slling 		spa_sync_props(spa, props, CRED(), tx);
28668525SEric.Schrock@Sun.COM 	}
28672926Sek110237 
2868789Sahrens 	dmu_tx_commit(tx);
2869789Sahrens 
2870789Sahrens 	spa->spa_sync_on = B_TRUE;
2871789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2872789Sahrens 
2873789Sahrens 	/*
2874789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2875789Sahrens 	 * bean counters are appropriately updated.
2876789Sahrens 	 */
2877789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2878789Sahrens 
28796643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2880789Sahrens 
28815094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
28824715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
28839946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
28844715Sek110237 
28858667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
28868667SGeorge.Wilson@Sun.COM 
2887789Sahrens 	mutex_exit(&spa_namespace_lock);
2888789Sahrens 
2889789Sahrens 	return (0);
2890789Sahrens }
2891789Sahrens 
28926423Sgw25295 #ifdef _KERNEL
28936423Sgw25295 /*
28949790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
28959790SLin.Ling@Sun.COM  * during the system boot up time.
28966423Sgw25295  */
28979790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
28989790SLin.Ling@Sun.COM 
28999790SLin.Ling@Sun.COM static nvlist_t *
29009790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
29016423Sgw25295 {
29029790SLin.Ling@Sun.COM 	nvlist_t *config;
29036423Sgw25295 	nvlist_t *nvtop, *nvroot;
29046423Sgw25295 	uint64_t pgid;
29056423Sgw25295 
29069790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
29079790SLin.Ling@Sun.COM 		return (NULL);
29089790SLin.Ling@Sun.COM 
29096423Sgw25295 	/*
29106423Sgw25295 	 * Add this top-level vdev to the child array.
29116423Sgw25295 	 */
29129790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29139790SLin.Ling@Sun.COM 	    &nvtop) == 0);
29149790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
29159790SLin.Ling@Sun.COM 	    &pgid) == 0);
29169790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
29176423Sgw25295 
29186423Sgw25295 	/*
29196423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
29206423Sgw25295 	 */
29216423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
29229790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
29239790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
29246423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
29256423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
29266423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
29276423Sgw25295 	    &nvtop, 1) == 0);
29286423Sgw25295 
29296423Sgw25295 	/*
29306423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
29316423Sgw25295 	 * this pool's configuration (remove the old, add the new).
29326423Sgw25295 	 */
29336423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
29346423Sgw25295 	nvlist_free(nvroot);
29359790SLin.Ling@Sun.COM 	return (config);
29366423Sgw25295 }
29376423Sgw25295 
29386423Sgw25295 /*
29399790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
29409790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
29419790SLin.Ling@Sun.COM  * device has a more recent txg.
29426423Sgw25295  */
29439790SLin.Ling@Sun.COM static void
29449790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
29457147Staylor {
29469816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
29479790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
29489790SLin.Ling@Sun.COM 
29499790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
29509790SLin.Ling@Sun.COM 		nvlist_t *label;
29519790SLin.Ling@Sun.COM 		uint64_t label_txg;
29529790SLin.Ling@Sun.COM 
29539790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
29549790SLin.Ling@Sun.COM 		    &label) != 0)
29559790SLin.Ling@Sun.COM 			return;
29569790SLin.Ling@Sun.COM 
29579790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
29589790SLin.Ling@Sun.COM 		    &label_txg) == 0);
29599790SLin.Ling@Sun.COM 
29609790SLin.Ling@Sun.COM 		/*
29619790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
29629790SLin.Ling@Sun.COM 		 */
29639790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
29649790SLin.Ling@Sun.COM 			*txg = label_txg;
29659790SLin.Ling@Sun.COM 			*avd = vd;
29667147Staylor 		}
29679790SLin.Ling@Sun.COM 		nvlist_free(label);
29687147Staylor 	}
29697147Staylor }
29707147Staylor 
29716423Sgw25295 /*
29726423Sgw25295  * Import a root pool.
29736423Sgw25295  *
29747147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
29757147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
29767147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
29776423Sgw25295  *
29786423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
29796423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
29806423Sgw25295  * e.g.
29816423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
29826423Sgw25295  */
29836423Sgw25295 int
29847147Staylor spa_import_rootpool(char *devpath, char *devid)
29856423Sgw25295 {
29869790SLin.Ling@Sun.COM 	spa_t *spa;
29879790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
29889790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
29899790SLin.Ling@Sun.COM 	uint64_t guid, txg;
29906423Sgw25295 	char *pname;
29916423Sgw25295 	int error;
29926423Sgw25295 
29936423Sgw25295 	/*
29949790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
29956423Sgw25295 	 */
299610822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
299710822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
299810822SJack.Meng@Sun.COM 	if (config == NULL) {
299910822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
300010822SJack.Meng@Sun.COM 			/* iscsi boot */
300110822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
300210822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
300310822SJack.Meng@Sun.COM 		}
300410822SJack.Meng@Sun.COM 	}
300510822SJack.Meng@Sun.COM #endif
300610822SJack.Meng@Sun.COM 	if (config == NULL) {
30079790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
30089790SLin.Ling@Sun.COM 		    devpath);
30099790SLin.Ling@Sun.COM 		return (EIO);
30109790SLin.Ling@Sun.COM 	}
30119790SLin.Ling@Sun.COM 
30129790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
30139790SLin.Ling@Sun.COM 	    &pname) == 0);
30149790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
30156423Sgw25295 
30169425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30179425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
30189425SEric.Schrock@Sun.COM 		/*
30199425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
30209425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
30219425SEric.Schrock@Sun.COM 		 */
30229425SEric.Schrock@Sun.COM 		spa_remove(spa);
30239425SEric.Schrock@Sun.COM 	}
30249425SEric.Schrock@Sun.COM 
302510921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
30269425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
302710100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
30289790SLin.Ling@Sun.COM 
30299790SLin.Ling@Sun.COM 	/*
30309790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
30319790SLin.Ling@Sun.COM 	 */
30329790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30339790SLin.Ling@Sun.COM 	    &nvtop) == 0);
30349790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30359790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
30369790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
30379790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30389790SLin.Ling@Sun.COM 	if (error) {
30399790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
30409790SLin.Ling@Sun.COM 		nvlist_free(config);
30419790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
30429790SLin.Ling@Sun.COM 		    pname);
30439790SLin.Ling@Sun.COM 		return (error);
30449790SLin.Ling@Sun.COM 	}
30459790SLin.Ling@Sun.COM 
30469790SLin.Ling@Sun.COM 	/*
30479790SLin.Ling@Sun.COM 	 * Get the boot vdev.
30489790SLin.Ling@Sun.COM 	 */
30499790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
30509790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
30519790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
30529790SLin.Ling@Sun.COM 		error = ENOENT;
30539790SLin.Ling@Sun.COM 		goto out;
30549790SLin.Ling@Sun.COM 	}
30559790SLin.Ling@Sun.COM 
30569790SLin.Ling@Sun.COM 	/*
30579790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
30589790SLin.Ling@Sun.COM 	 */
30599790SLin.Ling@Sun.COM 	avd = bvd;
30609790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
30619790SLin.Ling@Sun.COM 	if (avd != bvd) {
30629790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
30639790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
30649790SLin.Ling@Sun.COM 		error = EINVAL;
30659790SLin.Ling@Sun.COM 		goto out;
30669790SLin.Ling@Sun.COM 	}
30679790SLin.Ling@Sun.COM 
30689790SLin.Ling@Sun.COM 	/*
30699790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
30709790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
30719790SLin.Ling@Sun.COM 	 */
30729790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30739790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
30749790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
30759790SLin.Ling@Sun.COM 		    "try booting from '%s'",
30769790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
30779790SLin.Ling@Sun.COM 		error = EINVAL;
30789790SLin.Ling@Sun.COM 		goto out;
30799790SLin.Ling@Sun.COM 	}
30809790SLin.Ling@Sun.COM 
30819790SLin.Ling@Sun.COM 	error = 0;
30829946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
30839790SLin.Ling@Sun.COM out:
30849790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30859790SLin.Ling@Sun.COM 	vdev_free(rvd);
30869790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30879425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
30886423Sgw25295 
30899790SLin.Ling@Sun.COM 	nvlist_free(config);
30906423Sgw25295 	return (error);
30916423Sgw25295 }
30929790SLin.Ling@Sun.COM 
30936423Sgw25295 #endif
30946423Sgw25295 
30956423Sgw25295 /*
30969425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
30979425SEric.Schrock@Sun.COM  * boot.
30989425SEric.Schrock@Sun.COM  */
30999425SEric.Schrock@Sun.COM int
31009425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
31019425SEric.Schrock@Sun.COM {
31029425SEric.Schrock@Sun.COM 	spa_t *spa;
31039425SEric.Schrock@Sun.COM 	char *altroot = NULL;
31049425SEric.Schrock@Sun.COM 
31059425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
31069425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
31079425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31089425SEric.Schrock@Sun.COM 		return (EEXIST);
31099425SEric.Schrock@Sun.COM 	}
31109425SEric.Schrock@Sun.COM 
31119425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31129425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
311310921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
311410921STim.Haley@Sun.COM 
311510100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
311610000SVictor.Latushkin@Sun.COM 
31179425SEric.Schrock@Sun.COM 	if (props != NULL)
31189425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31199425SEric.Schrock@Sun.COM 
31209425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
31219425SEric.Schrock@Sun.COM 
31229425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31239946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31249425SEric.Schrock@Sun.COM 
31259425SEric.Schrock@Sun.COM 	return (0);
31269425SEric.Schrock@Sun.COM }
31279425SEric.Schrock@Sun.COM 
31289425SEric.Schrock@Sun.COM /*
31296423Sgw25295  * Import a non-root pool into the system.
31306423Sgw25295  */
31316423Sgw25295 int
31326423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
31336423Sgw25295 {
31349425SEric.Schrock@Sun.COM 	spa_t *spa;
31359425SEric.Schrock@Sun.COM 	char *altroot = NULL;
313610921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
313710921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
31389425SEric.Schrock@Sun.COM 	int error;
31399425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
31409425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
31419425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
31429425SEric.Schrock@Sun.COM 
31439425SEric.Schrock@Sun.COM 	/*
31449425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
31459425SEric.Schrock@Sun.COM 	 */
31469425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
314711422SMark.Musante@Sun.COM 	if (spa_lookup(pool) != NULL) {
31489425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31499425SEric.Schrock@Sun.COM 		return (EEXIST);
31509425SEric.Schrock@Sun.COM 	}
31519425SEric.Schrock@Sun.COM 
315210921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
315310921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
315410921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
315510921STim.Haley@Sun.COM 
31569425SEric.Schrock@Sun.COM 	/*
31579425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
31589425SEric.Schrock@Sun.COM 	 */
31599425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31609425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
316110921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
31629425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
31639425SEric.Schrock@Sun.COM 
31649425SEric.Schrock@Sun.COM 	/*
31659630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
31669630SJeff.Bonwick@Sun.COM 	 */
31679630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
31689630SJeff.Bonwick@Sun.COM 
31699630SJeff.Bonwick@Sun.COM 	/*
31709425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
31719425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
31729425SEric.Schrock@Sun.COM 	 * doing an import.
31739425SEric.Schrock@Sun.COM 	 */
317410921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
317510921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
317610921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
317711727SVictor.Latushkin@Sun.COM 	    policy.zrp_request);
317810921STim.Haley@Sun.COM 
317910921STim.Haley@Sun.COM 	/*
318010921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
318110921STim.Haley@Sun.COM 	 * back to caller
318210921STim.Haley@Sun.COM 	 */
318310921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
31849425SEric.Schrock@Sun.COM 
31859425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31869425SEric.Schrock@Sun.COM 	/*
31879425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
31889425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
31899425SEric.Schrock@Sun.COM 	 */
31909425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
31919425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
31929425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
31939425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
31949425SEric.Schrock@Sun.COM 	}
31959425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
31969425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
31979425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
31989425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
31999425SEric.Schrock@Sun.COM 	}
32009425SEric.Schrock@Sun.COM 
32019425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
32029425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
32039425SEric.Schrock@Sun.COM 	if (error == 0)
32049425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32059425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
32069425SEric.Schrock@Sun.COM 	if (error == 0)
32079425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32089425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
32099425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
32109425SEric.Schrock@Sun.COM 
32119425SEric.Schrock@Sun.COM 	if (props != NULL)
32129425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
32139425SEric.Schrock@Sun.COM 
32149425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
32159425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
32169425SEric.Schrock@Sun.COM 		spa_unload(spa);
32179425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
32189425SEric.Schrock@Sun.COM 		spa_remove(spa);
32199425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
32209425SEric.Schrock@Sun.COM 		return (error);
32219425SEric.Schrock@Sun.COM 	}
32229425SEric.Schrock@Sun.COM 
32239630SJeff.Bonwick@Sun.COM 	spa_async_resume(spa);
32249630SJeff.Bonwick@Sun.COM 
32259425SEric.Schrock@Sun.COM 	/*
32269425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
32279425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
32289425SEric.Schrock@Sun.COM 	 */
32299425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32309425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
32319425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
32329425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
32339425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
32349425SEric.Schrock@Sun.COM 		else
32359425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
32369425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32379425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
32389425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
32399425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32409425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32419425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32429425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
32439425SEric.Schrock@Sun.COM 	}
32449425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
32459425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
32469425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
32479425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
32489425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
32499425SEric.Schrock@Sun.COM 		else
32509425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
32519425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32529425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
32539425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
32549425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32559425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32569425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32579425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
32589425SEric.Schrock@Sun.COM 	}
32599425SEric.Schrock@Sun.COM 
326010672SEric.Schrock@Sun.COM 	/*
326110672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
326210672SEric.Schrock@Sun.COM 	 */
326310672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
326410672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
326510672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
326610672SEric.Schrock@Sun.COM 	}
326710672SEric.Schrock@Sun.COM 
32689425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
32699425SEric.Schrock@Sun.COM 		/*
32709425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
32719425SEric.Schrock@Sun.COM 		 */
327210100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32739425SEric.Schrock@Sun.COM 	}
32749425SEric.Schrock@Sun.COM 
32759816SGeorge.Wilson@Sun.COM 	/*
32769816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
32779816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
32789816SGeorge.Wilson@Sun.COM 	 */
32799816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
32809816SGeorge.Wilson@Sun.COM 
32819425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
32829946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
32839425SEric.Schrock@Sun.COM 
32849425SEric.Schrock@Sun.COM 	return (0);
32856643Seschrock }
32866643Seschrock 
3287789Sahrens nvlist_t *
3288789Sahrens spa_tryimport(nvlist_t *tryconfig)
3289789Sahrens {
3290789Sahrens 	nvlist_t *config = NULL;
3291789Sahrens 	char *poolname;
3292789Sahrens 	spa_t *spa;
3293789Sahrens 	uint64_t state;
32948680SLin.Ling@Sun.COM 	int error;
3295789Sahrens 
3296789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3297789Sahrens 		return (NULL);
3298789Sahrens 
3299789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3300789Sahrens 		return (NULL);
3301789Sahrens 
33021635Sbonwick 	/*
33031635Sbonwick 	 * Create and initialize the spa structure.
33041635Sbonwick 	 */
3305789Sahrens 	mutex_enter(&spa_namespace_lock);
330610921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
33078241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3308789Sahrens 
3309789Sahrens 	/*
33101635Sbonwick 	 * Pass off the heavy lifting to spa_load().
33111732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
33121732Sbonwick 	 * is actually the one to trust when doing an import.
3313789Sahrens 	 */
331411422SMark.Musante@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3315789Sahrens 
3316789Sahrens 	/*
3317789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3318789Sahrens 	 */
3319789Sahrens 	if (spa->spa_root_vdev != NULL) {
3320789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3321789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3322789Sahrens 		    poolname) == 0);
3323789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3324789Sahrens 		    state) == 0);
33253975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
33263975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
33272082Seschrock 
33282082Seschrock 		/*
33296423Sgw25295 		 * If the bootfs property exists on this pool then we
33306423Sgw25295 		 * copy it out so that external consumers can tell which
33316423Sgw25295 		 * pools are bootable.
33326423Sgw25295 		 */
33338680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
33346423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33356423Sgw25295 
33366423Sgw25295 			/*
33376423Sgw25295 			 * We have to play games with the name since the
33386423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
33396423Sgw25295 			 */
33407754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
33416423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
33426423Sgw25295 				char *cp;
33436423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33446423Sgw25295 
33456423Sgw25295 				cp = strchr(tmpname, '/');
33466423Sgw25295 				if (cp == NULL) {
33476423Sgw25295 					(void) strlcpy(dsname, tmpname,
33486423Sgw25295 					    MAXPATHLEN);
33496423Sgw25295 				} else {
33506423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
33516423Sgw25295 					    "%s/%s", poolname, ++cp);
33526423Sgw25295 				}
33536423Sgw25295 				VERIFY(nvlist_add_string(config,
33546423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
33556423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
33566423Sgw25295 			}
33576423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
33586423Sgw25295 		}
33596423Sgw25295 
33606423Sgw25295 		/*
33615450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
33622082Seschrock 		 */
33639425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
33642082Seschrock 		spa_add_spares(spa, config);
33655450Sbrendan 		spa_add_l2cache(spa, config);
33669425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3367789Sahrens 	}
3368789Sahrens 
3369789Sahrens 	spa_unload(spa);
3370789Sahrens 	spa_deactivate(spa);
3371789Sahrens 	spa_remove(spa);
3372789Sahrens 	mutex_exit(&spa_namespace_lock);
3373789Sahrens 
3374789Sahrens 	return (config);
3375789Sahrens }
3376789Sahrens 
3377789Sahrens /*
3378789Sahrens  * Pool export/destroy
3379789Sahrens  *
3380789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3381789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3382789Sahrens  * update the pool state and sync all the labels to disk, removing the
33838211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
33848211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3385789Sahrens  */
3386789Sahrens static int
33877214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
33888211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3389789Sahrens {
3390789Sahrens 	spa_t *spa;
3391789Sahrens 
33921775Sbillm 	if (oldconfig)
33931775Sbillm 		*oldconfig = NULL;
33941775Sbillm 
33958241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3396789Sahrens 		return (EROFS);
3397789Sahrens 
3398789Sahrens 	mutex_enter(&spa_namespace_lock);
3399789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3400789Sahrens 		mutex_exit(&spa_namespace_lock);
3401789Sahrens 		return (ENOENT);
3402789Sahrens 	}
3403789Sahrens 
3404789Sahrens 	/*
34051544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
34061544Seschrock 	 * reacquire the namespace lock, and see if we can export.
34071544Seschrock 	 */
34081544Seschrock 	spa_open_ref(spa, FTAG);
34091544Seschrock 	mutex_exit(&spa_namespace_lock);
34101544Seschrock 	spa_async_suspend(spa);
34111544Seschrock 	mutex_enter(&spa_namespace_lock);
34121544Seschrock 	spa_close(spa, FTAG);
34131544Seschrock 
34141544Seschrock 	/*
3415789Sahrens 	 * The pool will be in core if it's openable,
3416789Sahrens 	 * in which case we can modify its state.
3417789Sahrens 	 */
3418789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3419789Sahrens 		/*
3420789Sahrens 		 * Objsets may be open only because they're dirty, so we
3421789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3422789Sahrens 		 */
3423789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3424789Sahrens 
34251544Seschrock 		/*
34261544Seschrock 		 * A pool cannot be exported or destroyed if there are active
34271544Seschrock 		 * references.  If we are resetting a pool, allow references by
34281544Seschrock 		 * fault injection handlers.
34291544Seschrock 		 */
34301544Seschrock 		if (!spa_refcount_zero(spa) ||
34311544Seschrock 		    (spa->spa_inject_ref != 0 &&
34321544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
34331544Seschrock 			spa_async_resume(spa);
3434789Sahrens 			mutex_exit(&spa_namespace_lock);
3435789Sahrens 			return (EBUSY);
3436789Sahrens 		}
3437789Sahrens 
3438789Sahrens 		/*
34397214Slling 		 * A pool cannot be exported if it has an active shared spare.
34407214Slling 		 * This is to prevent other pools stealing the active spare
34417214Slling 		 * from an exported pool. At user's own will, such pool can
34427214Slling 		 * be forcedly exported.
34437214Slling 		 */
34447214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
34457214Slling 		    spa_has_active_shared_spare(spa)) {
34467214Slling 			spa_async_resume(spa);
34477214Slling 			mutex_exit(&spa_namespace_lock);
34487214Slling 			return (EXDEV);
34497214Slling 		}
34507214Slling 
34517214Slling 		/*
3452789Sahrens 		 * We want this to be reflected on every label,
3453789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3454789Sahrens 		 * final sync that pushes these changes out.
3455789Sahrens 		 */
34568211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
34577754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34581544Seschrock 			spa->spa_state = new_state;
34591635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
34601544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
34617754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
34621544Seschrock 		}
3463789Sahrens 	}
3464789Sahrens 
34654451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
34664451Seschrock 
3467789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3468789Sahrens 		spa_unload(spa);
3469789Sahrens 		spa_deactivate(spa);
3470789Sahrens 	}
3471789Sahrens 
34721775Sbillm 	if (oldconfig && spa->spa_config)
34731775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
34741775Sbillm 
34751544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
34768211SGeorge.Wilson@Sun.COM 		if (!hardforce)
34778211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
34781544Seschrock 		spa_remove(spa);
34791544Seschrock 	}
3480789Sahrens 	mutex_exit(&spa_namespace_lock);
3481789Sahrens 
3482789Sahrens 	return (0);
3483789Sahrens }
3484789Sahrens 
3485789Sahrens /*
3486789Sahrens  * Destroy a storage pool.
3487789Sahrens  */
3488789Sahrens int
3489789Sahrens spa_destroy(char *pool)
3490789Sahrens {
34918211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
34928211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3493789Sahrens }
3494789Sahrens 
3495789Sahrens /*
3496789Sahrens  * Export a storage pool.
3497789Sahrens  */
3498789Sahrens int
34998211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
35008211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3501789Sahrens {
35028211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
35038211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3504789Sahrens }
3505789Sahrens 
3506789Sahrens /*
35071544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
35081544Seschrock  * from the namespace in any way.
35091544Seschrock  */
35101544Seschrock int
35111544Seschrock spa_reset(char *pool)
35121544Seschrock {
35137214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
35148211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
35151544Seschrock }
35161544Seschrock 
35171544Seschrock /*
3518789Sahrens  * ==========================================================================
3519789Sahrens  * Device manipulation
3520789Sahrens  * ==========================================================================
3521789Sahrens  */
3522789Sahrens 
3523789Sahrens /*
35244527Sperrin  * Add a device to a storage pool.
3525789Sahrens  */
3526789Sahrens int
3527789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3528789Sahrens {
352910594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
35308241SJeff.Bonwick@Sun.COM 	int error;
3531789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
35321585Sbonwick 	vdev_t *vd, *tvd;
35335450Sbrendan 	nvlist_t **spares, **l2cache;
35345450Sbrendan 	uint_t nspares, nl2cache;
3535789Sahrens 
3536789Sahrens 	txg = spa_vdev_enter(spa);
3537789Sahrens 
35382082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
35392082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
35402082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
35412082Seschrock 
35427754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3543789Sahrens 
35445450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
35455450Sbrendan 	    &nspares) != 0)
35462082Seschrock 		nspares = 0;
35472082Seschrock 
35485450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
35495450Sbrendan 	    &nl2cache) != 0)
35505450Sbrendan 		nl2cache = 0;
35515450Sbrendan 
35527754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
35532082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
35547754SJeff.Bonwick@Sun.COM 
35557754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
35567754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
35577754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
35582082Seschrock 
35593377Seschrock 	/*
35605450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
35615450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
35623377Seschrock 	 */
35637754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
35643377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
35653377Seschrock 
35663377Seschrock 	/*
35673377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
35683377Seschrock 	 */
35698241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
357010594SGeorge.Wilson@Sun.COM 
357110594SGeorge.Wilson@Sun.COM 		/*
357210594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
357310594SGeorge.Wilson@Sun.COM 		 */
357410594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
357510594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
357610594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
357710594SGeorge.Wilson@Sun.COM 				break;
357810594SGeorge.Wilson@Sun.COM 			}
357910594SGeorge.Wilson@Sun.COM 		}
35803377Seschrock 		tvd = vd->vdev_child[c];
35813377Seschrock 		vdev_remove_child(vd, tvd);
358210594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
35833377Seschrock 		vdev_add_child(rvd, tvd);
35843377Seschrock 		vdev_config_dirty(tvd);
35853377Seschrock 	}
35863377Seschrock 
35872082Seschrock 	if (nspares != 0) {
35885450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
35895450Sbrendan 		    ZPOOL_CONFIG_SPARES);
35902082Seschrock 		spa_load_spares(spa);
35915450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
35925450Sbrendan 	}
35935450Sbrendan 
35945450Sbrendan 	if (nl2cache != 0) {
35955450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
35965450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
35975450Sbrendan 		spa_load_l2cache(spa);
35985450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3599789Sahrens 	}
3600789Sahrens 
3601789Sahrens 	/*
36021585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
36031585Sbonwick 	 * If other threads start allocating from these vdevs before we
36041585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
36051585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
36061585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
36071585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
36081635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
36091585Sbonwick 	 *
36101585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
36111585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
36121585Sbonwick 	 * steps will be completed the next time we load the pool.
3613789Sahrens 	 */
36141635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
36151585Sbonwick 
36161635Sbonwick 	mutex_enter(&spa_namespace_lock);
36171635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36181635Sbonwick 	mutex_exit(&spa_namespace_lock);
3619789Sahrens 
36201635Sbonwick 	return (0);
3621789Sahrens }
3622789Sahrens 
3623789Sahrens /*
3624789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3625789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3626789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3627789Sahrens  *
3628789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3629789Sahrens  * existing device; in this case the two devices are made into their own
36304451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3631789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3632789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3633789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3634789Sahrens  * is automatically detached.
3635789Sahrens  */
3636789Sahrens int
36371544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3638789Sahrens {
3639789Sahrens 	uint64_t txg, open_txg;
3640789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3641789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
36422082Seschrock 	vdev_ops_t *pvops;
36437313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
36447313SEric.Kustarz@Sun.COM 	int newvd_isspare;
36457313SEric.Kustarz@Sun.COM 	int error;
3646789Sahrens 
3647789Sahrens 	txg = spa_vdev_enter(spa);
3648789Sahrens 
36496643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3650789Sahrens 
3651789Sahrens 	if (oldvd == NULL)
3652789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3653789Sahrens 
36541585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
36551585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
36561585Sbonwick 
3657789Sahrens 	pvd = oldvd->vdev_parent;
3658789Sahrens 
36592082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
36604451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
36614451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
36624451Seschrock 
36634451Seschrock 	if (newrootvd->vdev_children != 1)
3664789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3665789Sahrens 
3666789Sahrens 	newvd = newrootvd->vdev_child[0];
3667789Sahrens 
3668789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3669789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3670789Sahrens 
36712082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3672789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3673789Sahrens 
36744527Sperrin 	/*
36754527Sperrin 	 * Spares can't replace logs
36764527Sperrin 	 */
36777326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
36784527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36794527Sperrin 
36802082Seschrock 	if (!replacing) {
36812082Seschrock 		/*
36822082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
36832082Seschrock 		 * vdev.
36842082Seschrock 		 */
36852082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
36862082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
36872082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36882082Seschrock 
36892082Seschrock 		pvops = &vdev_mirror_ops;
36902082Seschrock 	} else {
36912082Seschrock 		/*
36922082Seschrock 		 * Active hot spares can only be replaced by inactive hot
36932082Seschrock 		 * spares.
36942082Seschrock 		 */
36952082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
36962082Seschrock 		    pvd->vdev_child[1] == oldvd &&
36972082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
36982082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36992082Seschrock 
37002082Seschrock 		/*
37012082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
37022082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
37033377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
37043377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
37053377Seschrock 		 * the same (spare replaces spare, non-spare replaces
37063377Seschrock 		 * non-spare).
37072082Seschrock 		 */
37082082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
37092082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37103377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
37113377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
37123377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37132082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
37142082Seschrock 		    newvd->vdev_isspare)
37152082Seschrock 			pvops = &vdev_spare_ops;
37162082Seschrock 		else
37172082Seschrock 			pvops = &vdev_replacing_ops;
37182082Seschrock 	}
37192082Seschrock 
37201175Slling 	/*
37219816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
37221175Slling 	 */
37239816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3724789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3725789Sahrens 
37261732Sbonwick 	/*
37271732Sbonwick 	 * The new device cannot have a higher alignment requirement
37281732Sbonwick 	 * than the top-level vdev.
37291732Sbonwick 	 */
37301732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3731789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3732789Sahrens 
3733789Sahrens 	/*
3734789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3735789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3736789Sahrens 	 */
3737789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3738789Sahrens 		spa_strfree(oldvd->vdev_path);
3739789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3740789Sahrens 		    KM_SLEEP);
3741789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3742789Sahrens 		    newvd->vdev_path, "old");
3743789Sahrens 		if (oldvd->vdev_devid != NULL) {
3744789Sahrens 			spa_strfree(oldvd->vdev_devid);
3745789Sahrens 			oldvd->vdev_devid = NULL;
3746789Sahrens 		}
3747789Sahrens 	}
3748789Sahrens 
3749789Sahrens 	/*
37502082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
37512082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3752789Sahrens 	 */
3753789Sahrens 	if (pvd->vdev_ops != pvops)
3754789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3755789Sahrens 
3756789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3757789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3758789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3759789Sahrens 
3760789Sahrens 	/*
3761789Sahrens 	 * Extract the new device from its root and add it to pvd.
3762789Sahrens 	 */
3763789Sahrens 	vdev_remove_child(newrootvd, newvd);
3764789Sahrens 	newvd->vdev_id = pvd->vdev_children;
376510594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3766789Sahrens 	vdev_add_child(pvd, newvd);
3767789Sahrens 
3768789Sahrens 	tvd = newvd->vdev_top;
3769789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3770789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3771789Sahrens 
3772789Sahrens 	vdev_config_dirty(tvd);
3773789Sahrens 
3774789Sahrens 	/*
3775789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3776789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3777789Sahrens 	 */
3778789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3779789Sahrens 
37808241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
37818241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3782789Sahrens 
37839425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
37843377Seschrock 		spa_spare_activate(newvd);
37859425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
37869425SEric.Schrock@Sun.COM 	}
37879425SEric.Schrock@Sun.COM 
37887754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
37897754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
37907313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
37911544Seschrock 
3792789Sahrens 	/*
3793789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3794789Sahrens 	 */
37951732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3796789Sahrens 
3797789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3798789Sahrens 
37999946SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
38009946SMark.Musante@Sun.COM 	    CRED(),  "%s vdev=%s %s vdev=%s",
38019946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
38029946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
38039946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
38047313SEric.Kustarz@Sun.COM 
38057313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
38067313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
38077313SEric.Kustarz@Sun.COM 
3808789Sahrens 	/*
38097046Sahrens 	 * Kick off a resilver to update newvd.
3810789Sahrens 	 */
38117046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3812789Sahrens 
3813789Sahrens 	return (0);
3814789Sahrens }
3815789Sahrens 
3816789Sahrens /*
3817789Sahrens  * Detach a device from a mirror or replacing vdev.
3818789Sahrens  * If 'replace_done' is specified, only detach if the parent
3819789Sahrens  * is a replacing vdev.
3820789Sahrens  */
3821789Sahrens int
38228241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3823789Sahrens {
3824789Sahrens 	uint64_t txg;
38258241SJeff.Bonwick@Sun.COM 	int error;
3826789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3827789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
38282082Seschrock 	boolean_t unspare = B_FALSE;
38292082Seschrock 	uint64_t unspare_guid;
38306673Seschrock 	size_t len;
383111422SMark.Musante@Sun.COM 	char *vdpath;
3832789Sahrens 
3833789Sahrens 	txg = spa_vdev_enter(spa);
3834789Sahrens 
38356643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3836789Sahrens 
3837789Sahrens 	if (vd == NULL)
3838789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3839789Sahrens 
38401585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
38411585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38421585Sbonwick 
3843789Sahrens 	pvd = vd->vdev_parent;
3844789Sahrens 
3845789Sahrens 	/*
38468241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
38478241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
38488241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
38498241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
38508241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
38518241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
38528241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
38538241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
38548241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
38558241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
38568241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
38578241SJeff.Bonwick@Sun.COM 	 */
38588241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
38598241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
38608241SJeff.Bonwick@Sun.COM 
38618241SJeff.Bonwick@Sun.COM 	/*
3862789Sahrens 	 * If replace_done is specified, only remove this device if it's
38632082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
38642082Seschrock 	 * disk can be removed.
3865789Sahrens 	 */
38662082Seschrock 	if (replace_done) {
38672082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
38682082Seschrock 			if (vd->vdev_id != 0)
38692082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38702082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
38712082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38722082Seschrock 		}
38732082Seschrock 	}
38742082Seschrock 
38752082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
38764577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3877789Sahrens 
3878789Sahrens 	/*
38792082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3880789Sahrens 	 */
3881789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
38822082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
38832082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3884789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3885789Sahrens 
3886789Sahrens 	/*
38878241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
38888241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3889789Sahrens 	 */
38908241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3891789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3892789Sahrens 
38938241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
38948241SJeff.Bonwick@Sun.COM 
3895789Sahrens 	/*
38966673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
38976673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
38986673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
38996673Seschrock 	 */
39006673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
39016673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
39026673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
39036673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
39046673Seschrock 		cvd = pvd->vdev_child[0];
39056673Seschrock 		len = strlen(vd->vdev_path);
39066673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
39076673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
39086673Seschrock 			spa_strfree(cvd->vdev_path);
39096673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
39106673Seschrock 		}
39116673Seschrock 	}
39126673Seschrock 
39136673Seschrock 	/*
39142082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
39152082Seschrock 	 * that the spare should become a real disk, and be removed from the
39162082Seschrock 	 * active spare list for the pool.
39172082Seschrock 	 */
39182082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
39198241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
39202082Seschrock 		unspare = B_TRUE;
39212082Seschrock 
39222082Seschrock 	/*
3923789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3924789Sahrens 	 * This must be done after all other error cases are handled,
3925789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3926789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3927789Sahrens 	 * it may be that the unwritability of the disk is the reason
3928789Sahrens 	 * it's being detached!
3929789Sahrens 	 */
39303377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3931789Sahrens 
3932789Sahrens 	/*
3933789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3934789Sahrens 	 */
3935789Sahrens 	vdev_remove_child(pvd, vd);
3936789Sahrens 	vdev_compact_children(pvd);
3937789Sahrens 
3938789Sahrens 	/*
3939789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3940789Sahrens 	 */
3941789Sahrens 	cvd = pvd->vdev_child[0];
3942789Sahrens 
3943789Sahrens 	/*
39442082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
39458241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
39468241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
39478241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
39488241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
39498241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
39508241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
39512082Seschrock 	 */
39522082Seschrock 	if (unspare) {
39532082Seschrock 		ASSERT(cvd->vdev_isspare);
39543377Seschrock 		spa_spare_remove(cvd);
39552082Seschrock 		unspare_guid = cvd->vdev_guid;
39568241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
39572082Seschrock 	}
39582082Seschrock 
39592082Seschrock 	/*
3960789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3961789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3962789Sahrens 	 */
3963789Sahrens 	if (pvd->vdev_children == 1)
3964789Sahrens 		vdev_remove_parent(cvd);
3965789Sahrens 
3966789Sahrens 	/*
3967789Sahrens 	 * We don't set tvd until now because the parent we just removed
3968789Sahrens 	 * may have been the previous top-level vdev.
3969789Sahrens 	 */
3970789Sahrens 	tvd = cvd->vdev_top;
3971789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3972789Sahrens 
3973789Sahrens 	/*
39743377Seschrock 	 * Reevaluate the parent vdev state.
3975789Sahrens 	 */
39764451Seschrock 	vdev_propagate_state(cvd);
3977789Sahrens 
3978789Sahrens 	/*
39799816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
39809816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
39819816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
39829816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
39839816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3984789Sahrens 	 */
39859816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
39869816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
39879816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
39889816SGeorge.Wilson@Sun.COM 	}
3989789Sahrens 
3990789Sahrens 	vdev_config_dirty(tvd);
3991789Sahrens 
3992789Sahrens 	/*
39933377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
39943377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
39953377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
39963377Seschrock 	 * prevent vd from being accessed after it's freed.
3997789Sahrens 	 */
399811422SMark.Musante@Sun.COM 	vdpath = spa_strdup(vd->vdev_path);
39998241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
4000789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
40011732Sbonwick 	vd->vdev_detached = B_TRUE;
40021732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
4003789Sahrens 
40044451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
40054451Seschrock 
40062082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
40072082Seschrock 
400811422SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_DETACH, spa, NULL, CRED(),
400911422SMark.Musante@Sun.COM 	    "vdev=%s", vdpath);
401011422SMark.Musante@Sun.COM 	spa_strfree(vdpath);
401111422SMark.Musante@Sun.COM 
40122082Seschrock 	/*
40133377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
40143377Seschrock 	 * then we want to go through and remove the device from the hot spare
40153377Seschrock 	 * list of every other pool.
40162082Seschrock 	 */
40172082Seschrock 	if (unspare) {
40188241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
40192082Seschrock 		spa = NULL;
40202082Seschrock 		mutex_enter(&spa_namespace_lock);
40212082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
40222082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
40232082Seschrock 				continue;
40248241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
40258241SJeff.Bonwick@Sun.COM 				continue;
40267793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
40277793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
40282082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
40297793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
40307793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
40312082Seschrock 		}
40322082Seschrock 		mutex_exit(&spa_namespace_lock);
40332082Seschrock 	}
40342082Seschrock 
40352082Seschrock 	return (error);
40362082Seschrock }
40372082Seschrock 
403811422SMark.Musante@Sun.COM /*
403911422SMark.Musante@Sun.COM  * Split a set of devices from their mirrors, and create a new pool from them.
404011422SMark.Musante@Sun.COM  */
404111422SMark.Musante@Sun.COM int
404211422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
404311422SMark.Musante@Sun.COM     nvlist_t *props, boolean_t exp)
404411422SMark.Musante@Sun.COM {
404511422SMark.Musante@Sun.COM 	int error = 0;
404611422SMark.Musante@Sun.COM 	uint64_t txg, *glist;
404711422SMark.Musante@Sun.COM 	spa_t *newspa;
404811422SMark.Musante@Sun.COM 	uint_t c, children, lastlog;
404911422SMark.Musante@Sun.COM 	nvlist_t **child, *nvl, *tmp;
405011422SMark.Musante@Sun.COM 	dmu_tx_t *tx;
405111422SMark.Musante@Sun.COM 	char *altroot = NULL;
405211422SMark.Musante@Sun.COM 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
405311422SMark.Musante@Sun.COM 	boolean_t activate_slog;
405411422SMark.Musante@Sun.COM 
405511422SMark.Musante@Sun.COM 	if (!spa_writeable(spa))
405611422SMark.Musante@Sun.COM 		return (EROFS);
405711422SMark.Musante@Sun.COM 
405811422SMark.Musante@Sun.COM 	txg = spa_vdev_enter(spa);
405911422SMark.Musante@Sun.COM 
406011422SMark.Musante@Sun.COM 	/* clear the log and flush everything up to now */
406111422SMark.Musante@Sun.COM 	activate_slog = spa_passivate_log(spa);
406211422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
406311422SMark.Musante@Sun.COM 	error = spa_offline_log(spa);
406411422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
406511422SMark.Musante@Sun.COM 
406611422SMark.Musante@Sun.COM 	if (activate_slog)
406711422SMark.Musante@Sun.COM 		spa_activate_log(spa);
406811422SMark.Musante@Sun.COM 
406911422SMark.Musante@Sun.COM 	if (error != 0)
407011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
407111422SMark.Musante@Sun.COM 
407211422SMark.Musante@Sun.COM 	/* check new spa name before going any further */
407311422SMark.Musante@Sun.COM 	if (spa_lookup(newname) != NULL)
407411422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
407511422SMark.Musante@Sun.COM 
407611422SMark.Musante@Sun.COM 	/*
407711422SMark.Musante@Sun.COM 	 * scan through all the children to ensure they're all mirrors
407811422SMark.Musante@Sun.COM 	 */
407911422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
408011422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
408111422SMark.Musante@Sun.COM 	    &children) != 0)
408211422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
408311422SMark.Musante@Sun.COM 
408411422SMark.Musante@Sun.COM 	/* first, check to ensure we've got the right child count */
408511422SMark.Musante@Sun.COM 	rvd = spa->spa_root_vdev;
408611422SMark.Musante@Sun.COM 	lastlog = 0;
408711422SMark.Musante@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
408811422SMark.Musante@Sun.COM 		vdev_t *vd = rvd->vdev_child[c];
408911422SMark.Musante@Sun.COM 
409011422SMark.Musante@Sun.COM 		/* don't count the holes & logs as children */
409111422SMark.Musante@Sun.COM 		if (vd->vdev_islog || vd->vdev_ishole) {
409211422SMark.Musante@Sun.COM 			if (lastlog == 0)
409311422SMark.Musante@Sun.COM 				lastlog = c;
409411422SMark.Musante@Sun.COM 			continue;
409511422SMark.Musante@Sun.COM 		}
409611422SMark.Musante@Sun.COM 
409711422SMark.Musante@Sun.COM 		lastlog = 0;
409811422SMark.Musante@Sun.COM 	}
409911422SMark.Musante@Sun.COM 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
410011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
410111422SMark.Musante@Sun.COM 
410211422SMark.Musante@Sun.COM 	/* next, ensure no spare or cache devices are part of the split */
410311422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
410411422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
410511422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
410611422SMark.Musante@Sun.COM 
410711422SMark.Musante@Sun.COM 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
410811422SMark.Musante@Sun.COM 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
410911422SMark.Musante@Sun.COM 
411011422SMark.Musante@Sun.COM 	/* then, loop over each vdev and validate it */
411111422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
411211422SMark.Musante@Sun.COM 		uint64_t is_hole = 0;
411311422SMark.Musante@Sun.COM 
411411422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
411511422SMark.Musante@Sun.COM 		    &is_hole);
411611422SMark.Musante@Sun.COM 
411711422SMark.Musante@Sun.COM 		if (is_hole != 0) {
411811422SMark.Musante@Sun.COM 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
411911422SMark.Musante@Sun.COM 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
412011422SMark.Musante@Sun.COM 				continue;
412111422SMark.Musante@Sun.COM 			} else {
412211422SMark.Musante@Sun.COM 				error = EINVAL;
412311422SMark.Musante@Sun.COM 				break;
412411422SMark.Musante@Sun.COM 			}
412511422SMark.Musante@Sun.COM 		}
412611422SMark.Musante@Sun.COM 
412711422SMark.Musante@Sun.COM 		/* which disk is going to be split? */
412811422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
412911422SMark.Musante@Sun.COM 		    &glist[c]) != 0) {
413011422SMark.Musante@Sun.COM 			error = EINVAL;
413111422SMark.Musante@Sun.COM 			break;
413211422SMark.Musante@Sun.COM 		}
413311422SMark.Musante@Sun.COM 
413411422SMark.Musante@Sun.COM 		/* look it up in the spa */
413511422SMark.Musante@Sun.COM 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
413611422SMark.Musante@Sun.COM 		if (vml[c] == NULL) {
413711422SMark.Musante@Sun.COM 			error = ENODEV;
413811422SMark.Musante@Sun.COM 			break;
413911422SMark.Musante@Sun.COM 		}
414011422SMark.Musante@Sun.COM 
414111422SMark.Musante@Sun.COM 		/* make sure there's nothing stopping the split */
414211422SMark.Musante@Sun.COM 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
414311422SMark.Musante@Sun.COM 		    vml[c]->vdev_islog ||
414411422SMark.Musante@Sun.COM 		    vml[c]->vdev_ishole ||
414511422SMark.Musante@Sun.COM 		    vml[c]->vdev_isspare ||
414611422SMark.Musante@Sun.COM 		    vml[c]->vdev_isl2cache ||
414711422SMark.Musante@Sun.COM 		    !vdev_writeable(vml[c]) ||
414811497SMark.Musante@Sun.COM 		    vml[c]->vdev_children != 0 ||
414911422SMark.Musante@Sun.COM 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
415011422SMark.Musante@Sun.COM 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
415111422SMark.Musante@Sun.COM 			error = EINVAL;
415211422SMark.Musante@Sun.COM 			break;
415311422SMark.Musante@Sun.COM 		}
415411422SMark.Musante@Sun.COM 
415511422SMark.Musante@Sun.COM 		if (vdev_dtl_required(vml[c])) {
415611422SMark.Musante@Sun.COM 			error = EBUSY;
415711422SMark.Musante@Sun.COM 			break;
415811422SMark.Musante@Sun.COM 		}
415911422SMark.Musante@Sun.COM 
416011422SMark.Musante@Sun.COM 		/* we need certain info from the top level */
416111422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
416211422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_array) == 0);
416311422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
416411422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
416511422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
416611422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_asize) == 0);
416711422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
416811422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ashift) == 0);
416911422SMark.Musante@Sun.COM 	}
417011422SMark.Musante@Sun.COM 
417111422SMark.Musante@Sun.COM 	if (error != 0) {
417211422SMark.Musante@Sun.COM 		kmem_free(vml, children * sizeof (vdev_t *));
417311422SMark.Musante@Sun.COM 		kmem_free(glist, children * sizeof (uint64_t));
417411422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
417511422SMark.Musante@Sun.COM 	}
417611422SMark.Musante@Sun.COM 
417711422SMark.Musante@Sun.COM 	/* stop writers from using the disks */
417811422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
417911422SMark.Musante@Sun.COM 		if (vml[c] != NULL)
418011422SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_TRUE;
418111422SMark.Musante@Sun.COM 	}
418211422SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
418311422SMark.Musante@Sun.COM 
418411422SMark.Musante@Sun.COM 	/*
418511422SMark.Musante@Sun.COM 	 * Temporarily record the splitting vdevs in the spa config.  This
418611422SMark.Musante@Sun.COM 	 * will disappear once the config is regenerated.
418711422SMark.Musante@Sun.COM 	 */
418811422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
418911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
419011422SMark.Musante@Sun.COM 	    glist, children) == 0);
419111422SMark.Musante@Sun.COM 	kmem_free(glist, children * sizeof (uint64_t));
419211422SMark.Musante@Sun.COM 
4193*11864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
419411422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
419511422SMark.Musante@Sun.COM 	    nvl) == 0);
4196*11864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
419711422SMark.Musante@Sun.COM 	spa->spa_config_splitting = nvl;
419811422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
419911422SMark.Musante@Sun.COM 
420011422SMark.Musante@Sun.COM 	/* configure and create the new pool */
420111422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
420211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
420311422SMark.Musante@Sun.COM 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
420411422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
420511422SMark.Musante@Sun.COM 	    spa_version(spa)) == 0);
420611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
420711422SMark.Musante@Sun.COM 	    spa->spa_config_txg) == 0);
420811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
420911422SMark.Musante@Sun.COM 	    spa_generate_guid(NULL)) == 0);
421011422SMark.Musante@Sun.COM 	(void) nvlist_lookup_string(props,
421111422SMark.Musante@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
421211422SMark.Musante@Sun.COM 
421311497SMark.Musante@Sun.COM 	/* add the new pool to the namespace */
421411422SMark.Musante@Sun.COM 	newspa = spa_add(newname, config, altroot);
421511422SMark.Musante@Sun.COM 	newspa->spa_config_txg = spa->spa_config_txg;
421611422SMark.Musante@Sun.COM 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
421711422SMark.Musante@Sun.COM 
421811422SMark.Musante@Sun.COM 	/* release the spa config lock, retaining the namespace lock */
421911422SMark.Musante@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
422011422SMark.Musante@Sun.COM 
422111422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
422211422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 1);
422311422SMark.Musante@Sun.COM 
422411422SMark.Musante@Sun.COM 	spa_activate(newspa, spa_mode_global);
422511422SMark.Musante@Sun.COM 	spa_async_suspend(newspa);
422611422SMark.Musante@Sun.COM 
422711422SMark.Musante@Sun.COM 	/* create the new pool from the disks of the original pool */
422811422SMark.Musante@Sun.COM 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
422911422SMark.Musante@Sun.COM 	if (error)
423011422SMark.Musante@Sun.COM 		goto out;
423111422SMark.Musante@Sun.COM 
423211422SMark.Musante@Sun.COM 	/* if that worked, generate a real config for the new pool */
423311422SMark.Musante@Sun.COM 	if (newspa->spa_root_vdev != NULL) {
423411422SMark.Musante@Sun.COM 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
423511422SMark.Musante@Sun.COM 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
423611422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
423711422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
423811422SMark.Musante@Sun.COM 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
423911422SMark.Musante@Sun.COM 		    B_TRUE));
424011422SMark.Musante@Sun.COM 	}
424111422SMark.Musante@Sun.COM 
424211422SMark.Musante@Sun.COM 	/* set the props */
424311422SMark.Musante@Sun.COM 	if (props != NULL) {
424411422SMark.Musante@Sun.COM 		spa_configfile_set(newspa, props, B_FALSE);
424511422SMark.Musante@Sun.COM 		error = spa_prop_set(newspa, props);
424611422SMark.Musante@Sun.COM 		if (error)
424711422SMark.Musante@Sun.COM 			goto out;
424811422SMark.Musante@Sun.COM 	}
424911422SMark.Musante@Sun.COM 
425011422SMark.Musante@Sun.COM 	/* flush everything */
425111422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(newspa);
425211422SMark.Musante@Sun.COM 	vdev_config_dirty(newspa->spa_root_vdev);
425311422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
425411422SMark.Musante@Sun.COM 
425511422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
425611422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 2);
425711422SMark.Musante@Sun.COM 
425811422SMark.Musante@Sun.COM 	spa_async_resume(newspa);
425911422SMark.Musante@Sun.COM 
426011422SMark.Musante@Sun.COM 	/* finally, update the original pool's config */
426111422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
426211422SMark.Musante@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
426311422SMark.Musante@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
426411422SMark.Musante@Sun.COM 	if (error != 0)
426511422SMark.Musante@Sun.COM 		dmu_tx_abort(tx);
426611422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
426711422SMark.Musante@Sun.COM 		if (vml[c] != NULL) {
426811422SMark.Musante@Sun.COM 			vdev_split(vml[c]);
426911422SMark.Musante@Sun.COM 			if (error == 0)
427011422SMark.Musante@Sun.COM 				spa_history_internal_log(LOG_POOL_VDEV_DETACH,
427111422SMark.Musante@Sun.COM 				    spa, tx, CRED(), "vdev=%s",
427211422SMark.Musante@Sun.COM 				    vml[c]->vdev_path);
427311422SMark.Musante@Sun.COM 			vdev_free(vml[c]);
427411422SMark.Musante@Sun.COM 		}
427511422SMark.Musante@Sun.COM 	}
427611422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
427711422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
427811422SMark.Musante@Sun.COM 	nvlist_free(nvl);
427911422SMark.Musante@Sun.COM 	if (error == 0)
428011422SMark.Musante@Sun.COM 		dmu_tx_commit(tx);
428111422SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, 0);
428211422SMark.Musante@Sun.COM 
428311422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
428411422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 3);
428511422SMark.Musante@Sun.COM 
428611422SMark.Musante@Sun.COM 	/* split is complete; log a history record */
428711422SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_SPLIT, newspa, NULL, CRED(),
428811422SMark.Musante@Sun.COM 	    "split new pool %s from pool %s", newname, spa_name(spa));
428911422SMark.Musante@Sun.COM 
429011422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
429111422SMark.Musante@Sun.COM 
429211422SMark.Musante@Sun.COM 	/* if we're not going to mount the filesystems in userland, export */
429311422SMark.Musante@Sun.COM 	if (exp)
429411422SMark.Musante@Sun.COM 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
429511422SMark.Musante@Sun.COM 		    B_FALSE, B_FALSE);
429611422SMark.Musante@Sun.COM 
429711422SMark.Musante@Sun.COM 	return (error);
429811422SMark.Musante@Sun.COM 
429911422SMark.Musante@Sun.COM out:
430011422SMark.Musante@Sun.COM 	spa_unload(newspa);
430111422SMark.Musante@Sun.COM 	spa_deactivate(newspa);
430211422SMark.Musante@Sun.COM 	spa_remove(newspa);
430311422SMark.Musante@Sun.COM 
430411422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
4305*11864SMark.Musante@Sun.COM 
4306*11864SMark.Musante@Sun.COM 	/* re-online all offlined disks */
4307*11864SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
4308*11864SMark.Musante@Sun.COM 		if (vml[c] != NULL)
4309*11864SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_FALSE;
4310*11864SMark.Musante@Sun.COM 	}
4311*11864SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
4312*11864SMark.Musante@Sun.COM 
431311422SMark.Musante@Sun.COM 	nvlist_free(spa->spa_config_splitting);
431411422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
431511497SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, error);
431611422SMark.Musante@Sun.COM 
431711422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
431811422SMark.Musante@Sun.COM 	return (error);
431911422SMark.Musante@Sun.COM }
432011422SMark.Musante@Sun.COM 
43217754SJeff.Bonwick@Sun.COM static nvlist_t *
43227754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
43232082Seschrock {
43247754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
43257754SJeff.Bonwick@Sun.COM 		uint64_t guid;
43267754SJeff.Bonwick@Sun.COM 
43277754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
43287754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
43297754SJeff.Bonwick@Sun.COM 
43307754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
43317754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
43322082Seschrock 	}
43332082Seschrock 
43347754SJeff.Bonwick@Sun.COM 	return (NULL);
43355450Sbrendan }
43365450Sbrendan 
43377754SJeff.Bonwick@Sun.COM static void
43387754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
43397754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
43405450Sbrendan {
43417754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
43427754SJeff.Bonwick@Sun.COM 
43437754SJeff.Bonwick@Sun.COM 	if (count > 1)
43447754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
43457754SJeff.Bonwick@Sun.COM 
43467754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
43477754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
43487754SJeff.Bonwick@Sun.COM 			continue;
43497754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
43505450Sbrendan 	}
43515450Sbrendan 
43527754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
43537754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
43547754SJeff.Bonwick@Sun.COM 
43557754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
43567754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
43577754SJeff.Bonwick@Sun.COM 
43587754SJeff.Bonwick@Sun.COM 	if (count > 1)
43597754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
43605450Sbrendan }
43615450Sbrendan 
43625450Sbrendan /*
436310594SGeorge.Wilson@Sun.COM  * Removing a device from the vdev namespace requires several steps
436410594SGeorge.Wilson@Sun.COM  * and can take a significant amount of time.  As a result we use
436510594SGeorge.Wilson@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
436610594SGeorge.Wilson@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
436710594SGeorge.Wilson@Sun.COM  * lock.  During each step the configuration is synced out.
436810594SGeorge.Wilson@Sun.COM  */
436910594SGeorge.Wilson@Sun.COM 
437010594SGeorge.Wilson@Sun.COM /*
437110594SGeorge.Wilson@Sun.COM  * Evacuate the device.
437210594SGeorge.Wilson@Sun.COM  */
437310594SGeorge.Wilson@Sun.COM int
437410594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
437510594SGeorge.Wilson@Sun.COM {
437610974SJeff.Bonwick@Sun.COM 	int error = 0;
437710594SGeorge.Wilson@Sun.COM 	uint64_t txg;
437810594SGeorge.Wilson@Sun.COM 
437910594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
438010594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
438110922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
438210594SGeorge.Wilson@Sun.COM 
438310594SGeorge.Wilson@Sun.COM 	/*
438410594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
438510594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
438610594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
438710594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
438810594SGeorge.Wilson@Sun.COM 	 */
438910594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
439010974SJeff.Bonwick@Sun.COM 		error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
439110974SJeff.Bonwick@Sun.COM 		    NULL, DS_FIND_CHILDREN);
439210974SJeff.Bonwick@Sun.COM 	} else {
439310974SJeff.Bonwick@Sun.COM 		error = ENOTSUP;	/* until we have bp rewrite */
439410594SGeorge.Wilson@Sun.COM 	}
439510594SGeorge.Wilson@Sun.COM 
439610974SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
439710974SJeff.Bonwick@Sun.COM 
439810974SJeff.Bonwick@Sun.COM 	if (error)
439910974SJeff.Bonwick@Sun.COM 		return (error);
440010974SJeff.Bonwick@Sun.COM 
440110594SGeorge.Wilson@Sun.COM 	/*
440210974SJeff.Bonwick@Sun.COM 	 * The evacuation succeeded.  Remove any remaining MOS metadata
440310974SJeff.Bonwick@Sun.COM 	 * associated with this vdev, and wait for these changes to sync.
440410594SGeorge.Wilson@Sun.COM 	 */
440510594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
440610594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
440710594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
440810594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
440910594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
441010594SGeorge.Wilson@Sun.COM 
441110594SGeorge.Wilson@Sun.COM 	return (0);
441210594SGeorge.Wilson@Sun.COM }
441310594SGeorge.Wilson@Sun.COM 
441410594SGeorge.Wilson@Sun.COM /*
441510594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
441610594SGeorge.Wilson@Sun.COM  */
441710594SGeorge.Wilson@Sun.COM void
441810974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
441910594SGeorge.Wilson@Sun.COM {
442010594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
442110594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
442210594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
442310594SGeorge.Wilson@Sun.COM 
442410594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
442510594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
442610922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
442710594SGeorge.Wilson@Sun.COM 
442810594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
442910922SJeff.Bonwick@Sun.COM 
443010922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
443110922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
443210922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
443310922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
443410922SJeff.Bonwick@Sun.COM 
443510594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
443610594SGeorge.Wilson@Sun.COM 
443710594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
443810594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
443910594SGeorge.Wilson@Sun.COM 	} else {
444010594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
444110594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
444210594SGeorge.Wilson@Sun.COM 	}
444310594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(rvd);
444410594SGeorge.Wilson@Sun.COM 
444510594SGeorge.Wilson@Sun.COM 	/*
444610594SGeorge.Wilson@Sun.COM 	 * Reassess the health of our root vdev.
444710594SGeorge.Wilson@Sun.COM 	 */
444810594SGeorge.Wilson@Sun.COM 	vdev_reopen(rvd);
444910594SGeorge.Wilson@Sun.COM }
445010594SGeorge.Wilson@Sun.COM 
445110594SGeorge.Wilson@Sun.COM /*
44525450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
445310594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
44545450Sbrendan  */
44555450Sbrendan int
44565450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
44575450Sbrendan {
44585450Sbrendan 	vdev_t *vd;
445910974SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
44607754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
446110594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
44625450Sbrendan 	uint_t nspares, nl2cache;
44635450Sbrendan 	int error = 0;
44648241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
44658241SJeff.Bonwick@Sun.COM 
44668241SJeff.Bonwick@Sun.COM 	if (!locked)
44678241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
44685450Sbrendan 
44696643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
44705450Sbrendan 
44715450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
44725450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
44737754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
44747754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
44757754SJeff.Bonwick@Sun.COM 		/*
44767754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
44777754SJeff.Bonwick@Sun.COM 		 * in this pool.
44787754SJeff.Bonwick@Sun.COM 		 */
44797754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
44807754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
44817754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
44827754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
44837754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
44847754SJeff.Bonwick@Sun.COM 		} else {
44857754SJeff.Bonwick@Sun.COM 			error = EBUSY;
44867754SJeff.Bonwick@Sun.COM 		}
44877754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
44885450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
44897754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
44907754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
44917754SJeff.Bonwick@Sun.COM 		/*
44927754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
44937754SJeff.Bonwick@Sun.COM 		 */
44947754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
44957754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
44965450Sbrendan 		spa_load_l2cache(spa);
44975450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
449810594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
449910594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
450010922SJeff.Bonwick@Sun.COM 		ASSERT(vd == vd->vdev_top);
450110594SGeorge.Wilson@Sun.COM 
450210594SGeorge.Wilson@Sun.COM 		/*
450310594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
450410594SGeorge.Wilson@Sun.COM 		 * become the common case.
450510594SGeorge.Wilson@Sun.COM 		 */
450610594SGeorge.Wilson@Sun.COM 
450710974SJeff.Bonwick@Sun.COM 		mg = vd->vdev_mg;
450810974SJeff.Bonwick@Sun.COM 
450910594SGeorge.Wilson@Sun.COM 		/*
451010974SJeff.Bonwick@Sun.COM 		 * Stop allocating from this vdev.
451110594SGeorge.Wilson@Sun.COM 		 */
451210974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(mg);
451310594SGeorge.Wilson@Sun.COM 
451410922SJeff.Bonwick@Sun.COM 		/*
451510922SJeff.Bonwick@Sun.COM 		 * Wait for the youngest allocations and frees to sync,
451610922SJeff.Bonwick@Sun.COM 		 * and then wait for the deferral of those frees to finish.
451710922SJeff.Bonwick@Sun.COM 		 */
451810922SJeff.Bonwick@Sun.COM 		spa_vdev_config_exit(spa, NULL,
451910922SJeff.Bonwick@Sun.COM 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
452010922SJeff.Bonwick@Sun.COM 
452110974SJeff.Bonwick@Sun.COM 		/*
452210974SJeff.Bonwick@Sun.COM 		 * Attempt to evacuate the vdev.
452310974SJeff.Bonwick@Sun.COM 		 */
452410974SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove_evacuate(spa, vd);
452510974SJeff.Bonwick@Sun.COM 
452610594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
452710594SGeorge.Wilson@Sun.COM 
452810974SJeff.Bonwick@Sun.COM 		/*
452910974SJeff.Bonwick@Sun.COM 		 * If we couldn't evacuate the vdev, unwind.
453010974SJeff.Bonwick@Sun.COM 		 */
453110974SJeff.Bonwick@Sun.COM 		if (error) {
453210974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
453310974SJeff.Bonwick@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
453410974SJeff.Bonwick@Sun.COM 		}
453510974SJeff.Bonwick@Sun.COM 
453610974SJeff.Bonwick@Sun.COM 		/*
453710974SJeff.Bonwick@Sun.COM 		 * Clean up the vdev namespace.
453810974SJeff.Bonwick@Sun.COM 		 */
453910974SJeff.Bonwick@Sun.COM 		spa_vdev_remove_from_namespace(spa, vd);
454010594SGeorge.Wilson@Sun.COM 
45417754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
45427754SJeff.Bonwick@Sun.COM 		/*
45437754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
45447754SJeff.Bonwick@Sun.COM 		 */
45457754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
45467754SJeff.Bonwick@Sun.COM 	} else {
45477754SJeff.Bonwick@Sun.COM 		/*
45487754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
45497754SJeff.Bonwick@Sun.COM 		 */
45507754SJeff.Bonwick@Sun.COM 		error = ENOENT;
45515450Sbrendan 	}
45522082Seschrock 
45538241SJeff.Bonwick@Sun.COM 	if (!locked)
45548241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
45558241SJeff.Bonwick@Sun.COM 
45568241SJeff.Bonwick@Sun.COM 	return (error);
4557789Sahrens }
4558789Sahrens 
4559789Sahrens /*
45604451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
45614451Seschrock  * current spared, so we can detach it.
4562789Sahrens  */
45631544Seschrock static vdev_t *
45644451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
4565789Sahrens {
45661544Seschrock 	vdev_t *newvd, *oldvd;
45679816SGeorge.Wilson@Sun.COM 
45689816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
45694451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
45701544Seschrock 		if (oldvd != NULL)
45711544Seschrock 			return (oldvd);
45721544Seschrock 	}
4573789Sahrens 
45744451Seschrock 	/*
45754451Seschrock 	 * Check for a completed replacement.
45764451Seschrock 	 */
4577789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
45781544Seschrock 		oldvd = vd->vdev_child[0];
45791544Seschrock 		newvd = vd->vdev_child[1];
4580789Sahrens 
45818241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
458211820SVictor.Latushkin@Sun.COM 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
45838241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
45841544Seschrock 			return (oldvd);
45851544Seschrock 	}
4586789Sahrens 
45874451Seschrock 	/*
45884451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
45894451Seschrock 	 */
45904451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
45914451Seschrock 		newvd = vd->vdev_child[0];
45924451Seschrock 		oldvd = vd->vdev_child[1];
45934451Seschrock 
45944451Seschrock 		if (newvd->vdev_unspare &&
45958241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
459611820SVictor.Latushkin@Sun.COM 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
45978241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
45984451Seschrock 			newvd->vdev_unspare = 0;
45994451Seschrock 			return (oldvd);
46004451Seschrock 		}
46014451Seschrock 	}
46024451Seschrock 
46031544Seschrock 	return (NULL);
4604789Sahrens }
4605789Sahrens 
46061544Seschrock static void
46074451Seschrock spa_vdev_resilver_done(spa_t *spa)
4608789Sahrens {
46098241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
46108241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
46118241SJeff.Bonwick@Sun.COM 
46128241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4613789Sahrens 
46144451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
46158241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
46168241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
46171544Seschrock 		guid = vd->vdev_guid;
46188241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
46198241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
46208241SJeff.Bonwick@Sun.COM 		sguid = 0;
46212082Seschrock 		/*
46222082Seschrock 		 * If we have just finished replacing a hot spared device, then
46232082Seschrock 		 * we need to detach the parent's first child (the original hot
46242082Seschrock 		 * spare) as well.
46252082Seschrock 		 */
46268241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
46272082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
46288241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
46298241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
46302082Seschrock 		}
46318241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
46328241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
46331544Seschrock 			return;
46348241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
46352082Seschrock 			return;
46368241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4637789Sahrens 	}
4638789Sahrens 
46398241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4640789Sahrens }
4641789Sahrens 
4642789Sahrens /*
464311041SEric.Taylor@Sun.COM  * Update the stored path or FRU for this vdev.
46441354Seschrock  */
46451354Seschrock int
46469425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
46479425SEric.Schrock@Sun.COM     boolean_t ispath)
46481354Seschrock {
46496643Seschrock 	vdev_t *vd;
465011817SGeorge.Wilson@Sun.COM 	boolean_t sync = B_FALSE;
465111041SEric.Taylor@Sun.COM 
465211041SEric.Taylor@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALL);
46531354Seschrock 
46549425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
465511041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
46561354Seschrock 
46571585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
465811041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
46591585Sbonwick 
46609425SEric.Schrock@Sun.COM 	if (ispath) {
466111817SGeorge.Wilson@Sun.COM 		if (strcmp(value, vd->vdev_path) != 0) {
466211817SGeorge.Wilson@Sun.COM 			spa_strfree(vd->vdev_path);
466311817SGeorge.Wilson@Sun.COM 			vd->vdev_path = spa_strdup(value);
466411817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
466511817SGeorge.Wilson@Sun.COM 		}
46669425SEric.Schrock@Sun.COM 	} else {
466711817SGeorge.Wilson@Sun.COM 		if (vd->vdev_fru == NULL) {
466811817SGeorge.Wilson@Sun.COM 			vd->vdev_fru = spa_strdup(value);
466911817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
467011817SGeorge.Wilson@Sun.COM 		} else if (strcmp(value, vd->vdev_fru) != 0) {
46719425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
467211817SGeorge.Wilson@Sun.COM 			vd->vdev_fru = spa_strdup(value);
467311817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
467411817SGeorge.Wilson@Sun.COM 		}
46759425SEric.Schrock@Sun.COM 	}
46761354Seschrock 
467711817SGeorge.Wilson@Sun.COM 	return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
46781354Seschrock }
46791354Seschrock 
46809425SEric.Schrock@Sun.COM int
46819425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
46829425SEric.Schrock@Sun.COM {
46839425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
46849425SEric.Schrock@Sun.COM }
46859425SEric.Schrock@Sun.COM 
46869425SEric.Schrock@Sun.COM int
46879425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
46889425SEric.Schrock@Sun.COM {
46899425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
46909425SEric.Schrock@Sun.COM }
46919425SEric.Schrock@Sun.COM 
46921354Seschrock /*
4693789Sahrens  * ==========================================================================
4694789Sahrens  * SPA Scrubbing
4695789Sahrens  * ==========================================================================
4696789Sahrens  */
4697789Sahrens 
46987046Sahrens int
46997046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
4700789Sahrens {
47017754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
47024808Sek110237 
4703789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
4704789Sahrens 		return (ENOTSUP);
4705789Sahrens 
4706789Sahrens 	/*
47077046Sahrens 	 * If a resilver was requested, but there is no DTL on a
47087046Sahrens 	 * writeable leaf device, we have nothing to do.
4709789Sahrens 	 */
47107046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
47117046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
47127046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
47131544Seschrock 		return (0);
47141544Seschrock 	}
4715789Sahrens 
47167046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
47177046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
47187046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
47197046Sahrens 		return (EBUSY);
47207046Sahrens 
47217046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
47227046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
47237046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
47247046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
47251544Seschrock 	} else {
47267046Sahrens 		return (EINVAL);
47271544Seschrock 	}
4728789Sahrens }
4729789Sahrens 
47301544Seschrock /*
47311544Seschrock  * ==========================================================================
47321544Seschrock  * SPA async task processing
47331544Seschrock  * ==========================================================================
47341544Seschrock  */
47351544Seschrock 
47361544Seschrock static void
47374451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4738789Sahrens {
47397361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
47407361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
47417361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
474210575SEric.Schrock@Sun.COM 
474310575SEric.Schrock@Sun.COM 		/*
474410575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
474510575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
474610575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
474710575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
474810575SEric.Schrock@Sun.COM 		 */
474910575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
475010575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
475110575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
475210575SEric.Schrock@Sun.COM 
47537754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
47541544Seschrock 	}
47557361SBrendan.Gregg@Sun.COM 
47567754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47577361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
47581544Seschrock }
47591544Seschrock 
47601544Seschrock static void
47617754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
47627754SJeff.Bonwick@Sun.COM {
47637754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
47647754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
47657754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
47667754SJeff.Bonwick@Sun.COM 	}
47677754SJeff.Bonwick@Sun.COM 
47687754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47697754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
47707754SJeff.Bonwick@Sun.COM }
47717754SJeff.Bonwick@Sun.COM 
47727754SJeff.Bonwick@Sun.COM static void
47739816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
47749816SGeorge.Wilson@Sun.COM {
47759816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
47769816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
47779816SGeorge.Wilson@Sun.COM 	char *physpath;
47789816SGeorge.Wilson@Sun.COM 
47799816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
47809816SGeorge.Wilson@Sun.COM 		return;
47819816SGeorge.Wilson@Sun.COM 
47829816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
47839816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
47849816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
47859816SGeorge.Wilson@Sun.COM 	}
47869816SGeorge.Wilson@Sun.COM 
47879816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
47889816SGeorge.Wilson@Sun.COM 		return;
47899816SGeorge.Wilson@Sun.COM 
47909816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
47919816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
47929816SGeorge.Wilson@Sun.COM 
47939816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
47949816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
47959816SGeorge.Wilson@Sun.COM 
47969816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
47979816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
47989816SGeorge.Wilson@Sun.COM 
47999816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
48009816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
48019816SGeorge.Wilson@Sun.COM }
48029816SGeorge.Wilson@Sun.COM 
48039816SGeorge.Wilson@Sun.COM static void
48041544Seschrock spa_async_thread(spa_t *spa)
48051544Seschrock {
48067754SJeff.Bonwick@Sun.COM 	int tasks;
48071544Seschrock 
48081544Seschrock 	ASSERT(spa->spa_sync_on);
4809789Sahrens 
48101544Seschrock 	mutex_enter(&spa->spa_async_lock);
48111544Seschrock 	tasks = spa->spa_async_tasks;
48121544Seschrock 	spa->spa_async_tasks = 0;
48131544Seschrock 	mutex_exit(&spa->spa_async_lock);
48141544Seschrock 
48151544Seschrock 	/*
48161635Sbonwick 	 * See if the config needs to be updated.
48171635Sbonwick 	 */
48181635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
481910922SJeff.Bonwick@Sun.COM 		uint64_t old_space, new_space;
48209816SGeorge.Wilson@Sun.COM 
48211635Sbonwick 		mutex_enter(&spa_namespace_lock);
482210922SJeff.Bonwick@Sun.COM 		old_space = metaslab_class_get_space(spa_normal_class(spa));
48231635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
482410922SJeff.Bonwick@Sun.COM 		new_space = metaslab_class_get_space(spa_normal_class(spa));
48251635Sbonwick 		mutex_exit(&spa_namespace_lock);
48269816SGeorge.Wilson@Sun.COM 
48279816SGeorge.Wilson@Sun.COM 		/*
48289816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
48299816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
48309816SGeorge.Wilson@Sun.COM 		 */
483110922SJeff.Bonwick@Sun.COM 		if (new_space != old_space) {
48329946SMark.Musante@Sun.COM 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
48339946SMark.Musante@Sun.COM 			    spa, NULL, CRED(),
48349946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
483510922SJeff.Bonwick@Sun.COM 			    spa_name(spa), new_space, new_space - old_space);
48369816SGeorge.Wilson@Sun.COM 		}
48371635Sbonwick 	}
48381635Sbonwick 
48391635Sbonwick 	/*
48404451Seschrock 	 * See if any devices need to be marked REMOVED.
48411544Seschrock 	 */
48427754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
484310685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48444451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
48457754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
48467361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
48477754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
48487361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
48497754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48507754SJeff.Bonwick@Sun.COM 	}
48517754SJeff.Bonwick@Sun.COM 
48529816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
48539816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
48549816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
48559816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
48569816SGeorge.Wilson@Sun.COM 	}
48579816SGeorge.Wilson@Sun.COM 
48587754SJeff.Bonwick@Sun.COM 	/*
48597754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
48607754SJeff.Bonwick@Sun.COM 	 */
48617754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
486210685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48637754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
48647754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48654451Seschrock 	}
48661544Seschrock 
48671544Seschrock 	/*
48681544Seschrock 	 * If any devices are done replacing, detach them.
48691544Seschrock 	 */
48704451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
48714451Seschrock 		spa_vdev_resilver_done(spa);
4872789Sahrens 
48731544Seschrock 	/*
48741544Seschrock 	 * Kick off a resilver.
48751544Seschrock 	 */
48767046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
48777046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
48781544Seschrock 
48791544Seschrock 	/*
48801544Seschrock 	 * Let the world know that we're done.
48811544Seschrock 	 */
48821544Seschrock 	mutex_enter(&spa->spa_async_lock);
48831544Seschrock 	spa->spa_async_thread = NULL;
48841544Seschrock 	cv_broadcast(&spa->spa_async_cv);
48851544Seschrock 	mutex_exit(&spa->spa_async_lock);
48861544Seschrock 	thread_exit();
48871544Seschrock }
48881544Seschrock 
48891544Seschrock void
48901544Seschrock spa_async_suspend(spa_t *spa)
48911544Seschrock {
48921544Seschrock 	mutex_enter(&spa->spa_async_lock);
48931544Seschrock 	spa->spa_async_suspended++;
48941544Seschrock 	while (spa->spa_async_thread != NULL)
48951544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
48961544Seschrock 	mutex_exit(&spa->spa_async_lock);
48971544Seschrock }
48981544Seschrock 
48991544Seschrock void
49001544Seschrock spa_async_resume(spa_t *spa)
49011544Seschrock {
49021544Seschrock 	mutex_enter(&spa->spa_async_lock);
49031544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
49041544Seschrock 	spa->spa_async_suspended--;
49051544Seschrock 	mutex_exit(&spa->spa_async_lock);
49061544Seschrock }
49071544Seschrock 
49081544Seschrock static void
49091544Seschrock spa_async_dispatch(spa_t *spa)
49101544Seschrock {
49111544Seschrock 	mutex_enter(&spa->spa_async_lock);
49121544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
49131635Sbonwick 	    spa->spa_async_thread == NULL &&
49141635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
49151544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
49161544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
49171544Seschrock 	mutex_exit(&spa->spa_async_lock);
49181544Seschrock }
49191544Seschrock 
49201544Seschrock void
49211544Seschrock spa_async_request(spa_t *spa, int task)
49221544Seschrock {
49231544Seschrock 	mutex_enter(&spa->spa_async_lock);
49241544Seschrock 	spa->spa_async_tasks |= task;
49251544Seschrock 	mutex_exit(&spa->spa_async_lock);
4926789Sahrens }
4927789Sahrens 
4928789Sahrens /*
4929789Sahrens  * ==========================================================================
4930789Sahrens  * SPA syncing routines
4931789Sahrens  * ==========================================================================
4932789Sahrens  */
4933789Sahrens static void
493410922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg)
4935789Sahrens {
4936789Sahrens 	blkptr_t blk;
4937789Sahrens 	uint64_t itor = 0;
4938789Sahrens 	uint8_t c = 1;
4939789Sahrens 
49407754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
49417754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
494210922SJeff.Bonwick@Sun.COM 		zio_free(spa, txg, &blk);
49437754SJeff.Bonwick@Sun.COM 	}
4944789Sahrens 
4945789Sahrens 	bplist_vacate(bpl, tx);
4946789Sahrens 
4947789Sahrens 	/*
4948789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4949789Sahrens 	 * (Usually only the first block is needed.)
4950789Sahrens 	 */
495110922SJeff.Bonwick@Sun.COM 	dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx);
495210922SJeff.Bonwick@Sun.COM }
495310922SJeff.Bonwick@Sun.COM 
495410922SJeff.Bonwick@Sun.COM static void
495510922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
495610922SJeff.Bonwick@Sun.COM {
495710922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
495810922SJeff.Bonwick@Sun.COM 
495910922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
496010922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
4961789Sahrens }
4962789Sahrens 
4963789Sahrens static void
49642082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
49652082Seschrock {
49662082Seschrock 	char *packed = NULL;
49677497STim.Haley@Sun.COM 	size_t bufsize;
49682082Seschrock 	size_t nvsize = 0;
49692082Seschrock 	dmu_buf_t *db;
49702082Seschrock 
49712082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
49722082Seschrock 
49737497STim.Haley@Sun.COM 	/*
49747497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
49757497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
49767497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
49777497STim.Haley@Sun.COM 	 */
49787497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
49797497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
49802082Seschrock 
49812082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
49822082Seschrock 	    KM_SLEEP) == 0);
49837497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
49847497STim.Haley@Sun.COM 
49857497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
49867497STim.Haley@Sun.COM 
49877497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
49882082Seschrock 
49892082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
49902082Seschrock 	dmu_buf_will_dirty(db, tx);
49912082Seschrock 	*(uint64_t *)db->db_data = nvsize;
49922082Seschrock 	dmu_buf_rele(db, FTAG);
49932082Seschrock }
49942082Seschrock 
49952082Seschrock static void
49965450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
49975450Sbrendan     const char *config, const char *entry)
49982082Seschrock {
49992082Seschrock 	nvlist_t *nvroot;
50005450Sbrendan 	nvlist_t **list;
50012082Seschrock 	int i;
50022082Seschrock 
50035450Sbrendan 	if (!sav->sav_sync)
50042082Seschrock 		return;
50052082Seschrock 
50062082Seschrock 	/*
50075450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
50085450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
50094451Seschrock 	 * valid and the vdevs are labeled appropriately.
50102082Seschrock 	 */
50115450Sbrendan 	if (sav->sav_object == 0) {
50125450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
50135450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
50145450Sbrendan 		    sizeof (uint64_t), tx);
50152082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
50165450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
50175450Sbrendan 		    &sav->sav_object, tx) == 0);
50182082Seschrock 	}
50192082Seschrock 
50202082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
50215450Sbrendan 	if (sav->sav_count == 0) {
50225450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
50232082Seschrock 	} else {
50245450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
50255450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50265450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
50275450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
50285450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
50295450Sbrendan 		    sav->sav_count) == 0);
50305450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50315450Sbrendan 			nvlist_free(list[i]);
50325450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
50332082Seschrock 	}
50342082Seschrock 
50355450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
50362926Sek110237 	nvlist_free(nvroot);
50372082Seschrock 
50385450Sbrendan 	sav->sav_sync = B_FALSE;
50392082Seschrock }
50402082Seschrock 
50412082Seschrock static void
5042789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5043789Sahrens {
5044789Sahrens 	nvlist_t *config;
5045789Sahrens 
50467754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
5047789Sahrens 		return;
5048789Sahrens 
50497754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
50507754SJeff.Bonwick@Sun.COM 
50517754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
50527754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
50537754SJeff.Bonwick@Sun.COM 
50547754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
5055789Sahrens 
50561635Sbonwick 	if (spa->spa_config_syncing)
50571635Sbonwick 		nvlist_free(spa->spa_config_syncing);
50581635Sbonwick 	spa->spa_config_syncing = config;
5059789Sahrens 
50602082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5061789Sahrens }
5062789Sahrens 
50635094Slling /*
50645094Slling  * Set zpool properties.
50655094Slling  */
50663912Slling static void
50674543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
50683912Slling {
50693912Slling 	spa_t *spa = arg1;
50705094Slling 	objset_t *mos = spa->spa_meta_objset;
50713912Slling 	nvlist_t *nvp = arg2;
50725094Slling 	nvpair_t *elem;
50734451Seschrock 	uint64_t intval;
50746643Seschrock 	char *strval;
50755094Slling 	zpool_prop_t prop;
50765094Slling 	const char *propname;
50775094Slling 	zprop_type_t proptype;
50785094Slling 
50797754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
50807754SJeff.Bonwick@Sun.COM 
50815094Slling 	elem = NULL;
50825094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
50835094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
50845094Slling 		case ZPOOL_PROP_VERSION:
50855094Slling 			/*
50865094Slling 			 * Only set version for non-zpool-creation cases
50875094Slling 			 * (set/import). spa_create() needs special care
50885094Slling 			 * for version setting.
50895094Slling 			 */
50905094Slling 			if (tx->tx_txg != TXG_INITIAL) {
50915094Slling 				VERIFY(nvpair_value_uint64(elem,
50925094Slling 				    &intval) == 0);
50935094Slling 				ASSERT(intval <= SPA_VERSION);
50945094Slling 				ASSERT(intval >= spa_version(spa));
50955094Slling 				spa->spa_uberblock.ub_version = intval;
50965094Slling 				vdev_config_dirty(spa->spa_root_vdev);
50975094Slling 			}
50985094Slling 			break;
50995094Slling 
51005094Slling 		case ZPOOL_PROP_ALTROOT:
51015094Slling 			/*
51025094Slling 			 * 'altroot' is a non-persistent property. It should
51035094Slling 			 * have been set temporarily at creation or import time.
51045094Slling 			 */
51055094Slling 			ASSERT(spa->spa_root != NULL);
51065094Slling 			break;
51075094Slling 
51085363Seschrock 		case ZPOOL_PROP_CACHEFILE:
51095094Slling 			/*
51108525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
51115094Slling 			 */
51124543Smarks 			break;
51135094Slling 		default:
51145094Slling 			/*
51155094Slling 			 * Set pool property values in the poolprops mos object.
51165094Slling 			 */
51175094Slling 			if (spa->spa_pool_props_object == 0) {
51185094Slling 				VERIFY((spa->spa_pool_props_object =
51195094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
51205094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
51215094Slling 
51225094Slling 				VERIFY(zap_update(mos,
51235094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
51245094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
51255094Slling 				    == 0);
51265094Slling 			}
51275094Slling 
51285094Slling 			/* normalize the property name */
51295094Slling 			propname = zpool_prop_to_name(prop);
51305094Slling 			proptype = zpool_prop_get_type(prop);
51315094Slling 
51325094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
51335094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
51345094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
51355094Slling 				VERIFY(zap_update(mos,
51365094Slling 				    spa->spa_pool_props_object, propname,
51375094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
51385094Slling 
51395094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
51405094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
51415094Slling 
51425094Slling 				if (proptype == PROP_TYPE_INDEX) {
51435094Slling 					const char *unused;
51445094Slling 					VERIFY(zpool_prop_index_to_string(
51455094Slling 					    prop, intval, &unused) == 0);
51465094Slling 				}
51475094Slling 				VERIFY(zap_update(mos,
51485094Slling 				    spa->spa_pool_props_object, propname,
51495094Slling 				    8, 1, &intval, tx) == 0);
51505094Slling 			} else {
51515094Slling 				ASSERT(0); /* not allowed */
51525094Slling 			}
51535094Slling 
51545329Sgw25295 			switch (prop) {
51555329Sgw25295 			case ZPOOL_PROP_DELEGATION:
51565094Slling 				spa->spa_delegation = intval;
51575329Sgw25295 				break;
51585329Sgw25295 			case ZPOOL_PROP_BOOTFS:
51595094Slling 				spa->spa_bootfs = intval;
51605329Sgw25295 				break;
51615329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
51625329Sgw25295 				spa->spa_failmode = intval;
51635329Sgw25295 				break;
51649816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
51659816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
51669816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
51679816SGeorge.Wilson@Sun.COM 				break;
516810922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
516910922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
517010922SJeff.Bonwick@Sun.COM 				break;
51715329Sgw25295 			default:
51725329Sgw25295 				break;
51735329Sgw25295 			}
51743912Slling 		}
51755094Slling 
51765094Slling 		/* log internal history if this is not a zpool create */
51775094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
51785094Slling 		    tx->tx_txg != TXG_INITIAL) {
51795094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
51805094Slling 			    spa, tx, cr, "%s %lld %s",
51817754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
51825094Slling 		}
51833912Slling 	}
51847754SJeff.Bonwick@Sun.COM 
51857754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
51863912Slling }
51873912Slling 
5188789Sahrens /*
5189789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
5190789Sahrens  * part of the process, so we iterate until it converges.
5191789Sahrens  */
5192789Sahrens void
5193789Sahrens spa_sync(spa_t *spa, uint64_t txg)
5194789Sahrens {
5195789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
5196789Sahrens 	objset_t *mos = spa->spa_meta_objset;
519710922SJeff.Bonwick@Sun.COM 	bplist_t *defer_bpl = &spa->spa_deferred_bplist;
519810922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
51991635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5200789Sahrens 	vdev_t *vd;
5201789Sahrens 	dmu_tx_t *tx;
52027754SJeff.Bonwick@Sun.COM 	int error;
5203789Sahrens 
5204789Sahrens 	/*
5205789Sahrens 	 * Lock out configuration changes.
5206789Sahrens 	 */
52077754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5208789Sahrens 
5209789Sahrens 	spa->spa_syncing_txg = txg;
5210789Sahrens 	spa->spa_sync_pass = 0;
5211789Sahrens 
52127754SJeff.Bonwick@Sun.COM 	/*
52137754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
52147754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
52157754SJeff.Bonwick@Sun.COM 	 */
52167754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
52178241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
52188241SJeff.Bonwick@Sun.COM 		/*
52198241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
52208241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
52218241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
52228241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
52238241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
52248241SJeff.Bonwick@Sun.COM 		 */
52258241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52268241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
52278241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
52288241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
52298241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
52308241SJeff.Bonwick@Sun.COM 		}
52318241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52328241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
52337754SJeff.Bonwick@Sun.COM 	}
52347754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
52357754SJeff.Bonwick@Sun.COM 
523610922SJeff.Bonwick@Sun.COM 	VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj));
5237789Sahrens 
52382082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
52392082Seschrock 
52402082Seschrock 	/*
52414577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
52422082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
52432082Seschrock 	 */
52444577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
52454577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
52462082Seschrock 		int i;
52472082Seschrock 
52482082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
52492082Seschrock 			vd = rvd->vdev_child[i];
52502082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
52512082Seschrock 				break;
52522082Seschrock 		}
52532082Seschrock 		if (i == rvd->vdev_children) {
52542082Seschrock 			spa->spa_deflate = TRUE;
52552082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
52562082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
52572082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
52582082Seschrock 		}
52592082Seschrock 	}
52602082Seschrock 
52617046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
52627046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
52637046Sahrens 		dsl_pool_create_origin(dp, tx);
52647046Sahrens 
52657046Sahrens 		/* Keeping the origin open increases spa_minref */
52667046Sahrens 		spa->spa_minref += 3;
52677046Sahrens 	}
52687046Sahrens 
52697046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
52707046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
52717046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
52727046Sahrens 	}
52737046Sahrens 
5274789Sahrens 	/*
5275789Sahrens 	 * If anything has changed in this txg, push the deferred frees
5276789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
5277789Sahrens 	 * don't generate work on an otherwise idle system.
5278789Sahrens 	 */
5279789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
52802329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
52812329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
528210922SJeff.Bonwick@Sun.COM 		spa_sync_deferred_bplist(spa, defer_bpl, tx, txg);
5283789Sahrens 
5284789Sahrens 	/*
5285789Sahrens 	 * Iterate to convergence.
5286789Sahrens 	 */
5287789Sahrens 	do {
528810922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
5289789Sahrens 
5290789Sahrens 		spa_sync_config_object(spa, tx);
52915450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
52925450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
52935450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
52945450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
52951544Seschrock 		spa_errlog_sync(spa, txg);
5296789Sahrens 		dsl_pool_sync(dp, txg);
5297789Sahrens 
529810922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
529910922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
530010922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, spa_sync_free, zio, tx);
530110922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
530210922SJeff.Bonwick@Sun.COM 		} else {
530310922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx);
5304789Sahrens 		}
5305789Sahrens 
530610922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
530710922SJeff.Bonwick@Sun.COM 
530811619SGeorge.Wilson@Sun.COM 		mutex_enter(&spa->spa_scrub_lock);
530911619SGeorge.Wilson@Sun.COM 		while (spa->spa_scrub_inflight > 0)
531011619SGeorge.Wilson@Sun.COM 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
531111619SGeorge.Wilson@Sun.COM 		mutex_exit(&spa->spa_scrub_lock);
531211619SGeorge.Wilson@Sun.COM 
531310922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
531410922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
531510922SJeff.Bonwick@Sun.COM 
531610922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
531710922SJeff.Bonwick@Sun.COM 
531810922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
531910922SJeff.Bonwick@Sun.COM 
532010922SJeff.Bonwick@Sun.COM 	bplist_close(defer_bpl);
5321789Sahrens 
5322789Sahrens 	/*
5323789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
5324789Sahrens 	 * to commit the transaction group.
53251635Sbonwick 	 *
53265688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
53275688Sbonwick 	 * random top-level vdevs that are known to be visible in the
53287754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
53297754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5330789Sahrens 	 */
53317754SJeff.Bonwick@Sun.COM 	for (;;) {
53327754SJeff.Bonwick@Sun.COM 		/*
53337754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
53347754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
53357754SJeff.Bonwick@Sun.COM 		 */
53367754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
53377754SJeff.Bonwick@Sun.COM 
53387754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
53397754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
53407754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
53417754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
53427754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
53439816SGeorge.Wilson@Sun.COM 
53449816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
53457754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
53467754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
53477754SJeff.Bonwick@Sun.COM 					continue;
53487754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
53497754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
53507754SJeff.Bonwick@Sun.COM 					break;
53517754SJeff.Bonwick@Sun.COM 			}
53529725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
53539725SEric.Schrock@Sun.COM 			if (error != 0)
53549725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
53559725SEric.Schrock@Sun.COM 				    B_TRUE);
53567754SJeff.Bonwick@Sun.COM 		} else {
53577754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
53589725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
53599725SEric.Schrock@Sun.COM 			if (error != 0)
53609725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
53619725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
53621635Sbonwick 		}
53637754SJeff.Bonwick@Sun.COM 
53647754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
53657754SJeff.Bonwick@Sun.COM 
53667754SJeff.Bonwick@Sun.COM 		if (error == 0)
53677754SJeff.Bonwick@Sun.COM 			break;
53687754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
53697754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
53701635Sbonwick 	}
53712082Seschrock 	dmu_tx_commit(tx);
53722082Seschrock 
53731635Sbonwick 	/*
53741635Sbonwick 	 * Clear the dirty config list.
53751635Sbonwick 	 */
53767754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
53771635Sbonwick 		vdev_config_clean(vd);
53781635Sbonwick 
53791635Sbonwick 	/*
53801635Sbonwick 	 * Now that the new config has synced transactionally,
53811635Sbonwick 	 * let it become visible to the config cache.
53821635Sbonwick 	 */
53831635Sbonwick 	if (spa->spa_config_syncing != NULL) {
53841635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
53851635Sbonwick 		spa->spa_config_txg = txg;
53861635Sbonwick 		spa->spa_config_syncing = NULL;
53871635Sbonwick 	}
5388789Sahrens 
5389789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
5390789Sahrens 
539110922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
5392789Sahrens 
5393789Sahrens 	/*
5394789Sahrens 	 * Update usable space statistics.
5395789Sahrens 	 */
5396789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5397789Sahrens 		vdev_sync_done(vd, txg);
5398789Sahrens 
539910956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
540010956SGeorge.Wilson@Sun.COM 
5401789Sahrens 	/*
5402789Sahrens 	 * It had better be the case that we didn't dirty anything
54032082Seschrock 	 * since vdev_config_sync().
5404789Sahrens 	 */
5405789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5406789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5407789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
540810922SJeff.Bonwick@Sun.COM 	ASSERT(defer_bpl->bpl_queue == NULL);
540910922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
541010922SJeff.Bonwick@Sun.COM 
541110922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
5412789Sahrens 
54137754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
54141544Seschrock 
541510921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
541610921STim.Haley@Sun.COM 
54171544Seschrock 	/*
54181544Seschrock 	 * If any async tasks have been requested, kick them off.
54191544Seschrock 	 */
54201544Seschrock 	spa_async_dispatch(spa);
5421789Sahrens }
5422789Sahrens 
5423789Sahrens /*
5424789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
5425789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
5426789Sahrens  * sync.
5427789Sahrens  */
5428789Sahrens void
5429789Sahrens spa_sync_allpools(void)
5430789Sahrens {
5431789Sahrens 	spa_t *spa = NULL;
5432789Sahrens 	mutex_enter(&spa_namespace_lock);
5433789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
54347754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
5435789Sahrens 			continue;
5436789Sahrens 		spa_open_ref(spa, FTAG);
5437789Sahrens 		mutex_exit(&spa_namespace_lock);
5438789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
5439789Sahrens 		mutex_enter(&spa_namespace_lock);
5440789Sahrens 		spa_close(spa, FTAG);
5441789Sahrens 	}
5442789Sahrens 	mutex_exit(&spa_namespace_lock);
5443789Sahrens }
5444789Sahrens 
5445789Sahrens /*
5446789Sahrens  * ==========================================================================
5447789Sahrens  * Miscellaneous routines
5448789Sahrens  * ==========================================================================
5449789Sahrens  */
5450789Sahrens 
5451789Sahrens /*
5452789Sahrens  * Remove all pools in the system.
5453789Sahrens  */
5454789Sahrens void
5455789Sahrens spa_evict_all(void)
5456789Sahrens {
5457789Sahrens 	spa_t *spa;
5458789Sahrens 
5459789Sahrens 	/*
5460789Sahrens 	 * Remove all cached state.  All pools should be closed now,
5461789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
5462789Sahrens 	 */
5463789Sahrens 	mutex_enter(&spa_namespace_lock);
5464789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
5465789Sahrens 		/*
54661544Seschrock 		 * Stop async tasks.  The async thread may need to detach
54671544Seschrock 		 * a device that's been replaced, which requires grabbing
54681544Seschrock 		 * spa_namespace_lock, so we must drop it here.
5469789Sahrens 		 */
5470789Sahrens 		spa_open_ref(spa, FTAG);
5471789Sahrens 		mutex_exit(&spa_namespace_lock);
54721544Seschrock 		spa_async_suspend(spa);
54734808Sek110237 		mutex_enter(&spa_namespace_lock);
5474789Sahrens 		spa_close(spa, FTAG);
5475789Sahrens 
5476789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5477789Sahrens 			spa_unload(spa);
5478789Sahrens 			spa_deactivate(spa);
5479789Sahrens 		}
5480789Sahrens 		spa_remove(spa);
5481789Sahrens 	}
5482789Sahrens 	mutex_exit(&spa_namespace_lock);
5483789Sahrens }
54841544Seschrock 
54851544Seschrock vdev_t *
54869425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
54871544Seschrock {
54886643Seschrock 	vdev_t *vd;
54896643Seschrock 	int i;
54906643Seschrock 
54916643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
54926643Seschrock 		return (vd);
54936643Seschrock 
54949425SEric.Schrock@Sun.COM 	if (aux) {
54956643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
54966643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
54976643Seschrock 			if (vd->vdev_guid == guid)
54986643Seschrock 				return (vd);
54996643Seschrock 		}
55009425SEric.Schrock@Sun.COM 
55019425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
55029425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
55039425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
55049425SEric.Schrock@Sun.COM 				return (vd);
55059425SEric.Schrock@Sun.COM 		}
55066643Seschrock 	}
55076643Seschrock 
55086643Seschrock 	return (NULL);
55091544Seschrock }
55101760Seschrock 
55111760Seschrock void
55125094Slling spa_upgrade(spa_t *spa, uint64_t version)
55131760Seschrock {
55147754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
55151760Seschrock 
55161760Seschrock 	/*
55171760Seschrock 	 * This should only be called for a non-faulted pool, and since a
55181760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
55191760Seschrock 	 * possible.
55201760Seschrock 	 */
55214577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
55225094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
55235094Slling 
55245094Slling 	spa->spa_uberblock.ub_version = version;
55251760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
55261760Seschrock 
55277754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
55282082Seschrock 
55292082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
55301760Seschrock }
55312082Seschrock 
55322082Seschrock boolean_t
55332082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
55342082Seschrock {
55352082Seschrock 	int i;
55363377Seschrock 	uint64_t spareguid;
55375450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
55385450Sbrendan 
55395450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
55405450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
55412082Seschrock 			return (B_TRUE);
55422082Seschrock 
55435450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
55445450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
55455450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
55463377Seschrock 			return (B_TRUE);
55473377Seschrock 	}
55483377Seschrock 
55492082Seschrock 	return (B_FALSE);
55502082Seschrock }
55513912Slling 
55524451Seschrock /*
55537214Slling  * Check if a pool has an active shared spare device.
55547214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
55557214Slling  */
55567214Slling static boolean_t
55577214Slling spa_has_active_shared_spare(spa_t *spa)
55587214Slling {
55597214Slling 	int i, refcnt;
55607214Slling 	uint64_t pool;
55617214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
55627214Slling 
55637214Slling 	for (i = 0; i < sav->sav_count; i++) {
55647214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
55657214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
55667214Slling 		    refcnt > 2)
55677214Slling 			return (B_TRUE);
55687214Slling 	}
55697214Slling 
55707214Slling 	return (B_FALSE);
55717214Slling }
55727214Slling 
55737214Slling /*
55744451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
55754451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
55764451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
55774451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
55784451Seschrock  * or zdb as real changes.
55794451Seschrock  */
55804451Seschrock void
55814451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
55824451Seschrock {
55834451Seschrock #ifdef _KERNEL
55844451Seschrock 	sysevent_t		*ev;
55854451Seschrock 	sysevent_attr_list_t	*attr = NULL;
55864451Seschrock 	sysevent_value_t	value;
55874451Seschrock 	sysevent_id_t		eid;
55884451Seschrock 
55894451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
55904451Seschrock 	    SE_SLEEP);
55914451Seschrock 
55924451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
55934451Seschrock 	value.value.sv_string = spa_name(spa);
55944451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
55954451Seschrock 		goto done;
55964451Seschrock 
55974451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
55984451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
55994451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
56004451Seschrock 		goto done;
56014451Seschrock 
56024451Seschrock 	if (vd) {
56034451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
56044451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
56054451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
56064451Seschrock 		    SE_SLEEP) != 0)
56074451Seschrock 			goto done;
56084451Seschrock 
56094451Seschrock 		if (vd->vdev_path) {
56104451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
56114451Seschrock 			value.value.sv_string = vd->vdev_path;
56124451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
56134451Seschrock 			    &value, SE_SLEEP) != 0)
56144451Seschrock 				goto done;
56154451Seschrock 		}
56164451Seschrock 	}
56174451Seschrock 
56185756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
56195756Seschrock 		goto done;
56205756Seschrock 	attr = NULL;
56215756Seschrock 
56224451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
56234451Seschrock 
56244451Seschrock done:
56254451Seschrock 	if (attr)
56264451Seschrock 		sysevent_free_attr(attr);
56274451Seschrock 	sysevent_free(ev);
56284451Seschrock #endif
56294451Seschrock }
5630