xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 11619:d3fb7ae6fe56)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
2311422SMark.Musante@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens /*
28789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
29789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
30789Sahrens  * pool.
31789Sahrens  */
32789Sahrens 
33789Sahrens #include <sys/zfs_context.h>
341544Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens #include <sys/spa_impl.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/zio_checksum.h>
38789Sahrens #include <sys/dmu.h>
39789Sahrens #include <sys/dmu_tx.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
4210922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
43789Sahrens #include <sys/vdev_impl.h>
44789Sahrens #include <sys/metaslab.h>
4510594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
46789Sahrens #include <sys/uberblock_impl.h>
47789Sahrens #include <sys/txg.h>
48789Sahrens #include <sys/avl.h>
49789Sahrens #include <sys/dmu_traverse.h>
503912Slling #include <sys/dmu_objset.h>
51789Sahrens #include <sys/unique.h>
52789Sahrens #include <sys/dsl_pool.h>
533912Slling #include <sys/dsl_dataset.h>
54789Sahrens #include <sys/dsl_dir.h>
55789Sahrens #include <sys/dsl_prop.h>
563912Slling #include <sys/dsl_synctask.h>
57789Sahrens #include <sys/fs/zfs.h>
585450Sbrendan #include <sys/arc.h>
59789Sahrens #include <sys/callb.h>
603975Sek110237 #include <sys/systeminfo.h>
616423Sgw25295 #include <sys/spa_boot.h>
629816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h>
63789Sahrens 
648662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
6511173SJonathan.Adams@Sun.COM #include <sys/bootprops.h>
6611173SJonathan.Adams@Sun.COM #include <sys/callb.h>
6711173SJonathan.Adams@Sun.COM #include <sys/cpupart.h>
6811173SJonathan.Adams@Sun.COM #include <sys/pool.h>
6911173SJonathan.Adams@Sun.COM #include <sys/sysdc.h>
708662SJordan.Vaughan@Sun.com #include <sys/zone.h>
718662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
728662SJordan.Vaughan@Sun.com 
735094Slling #include "zfs_prop.h"
745913Sperrin #include "zfs_comutil.h"
755094Slling 
7611173SJonathan.Adams@Sun.COM typedef enum zti_modes {
779515SJonathan.Adams@Sun.COM 	zti_mode_fixed,			/* value is # of threads (min 1) */
789515SJonathan.Adams@Sun.COM 	zti_mode_online_percent,	/* value is % of online CPUs */
7911173SJonathan.Adams@Sun.COM 	zti_mode_batch,			/* cpu-intensive; value is ignored */
8011146SGeorge.Wilson@Sun.COM 	zti_mode_null,			/* don't create a taskq */
819515SJonathan.Adams@Sun.COM 	zti_nmodes
8211173SJonathan.Adams@Sun.COM } zti_modes_t;
832986Sek110237 
8411146SGeorge.Wilson@Sun.COM #define	ZTI_FIX(n)	{ zti_mode_fixed, (n) }
8511146SGeorge.Wilson@Sun.COM #define	ZTI_PCT(n)	{ zti_mode_online_percent, (n) }
8611173SJonathan.Adams@Sun.COM #define	ZTI_BATCH	{ zti_mode_batch, 0 }
8711146SGeorge.Wilson@Sun.COM #define	ZTI_NULL	{ zti_mode_null, 0 }
8811146SGeorge.Wilson@Sun.COM 
8911146SGeorge.Wilson@Sun.COM #define	ZTI_ONE		ZTI_FIX(1)
909515SJonathan.Adams@Sun.COM 
919515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info {
9211146SGeorge.Wilson@Sun.COM 	enum zti_modes zti_mode;
9311146SGeorge.Wilson@Sun.COM 	uint_t zti_value;
949515SJonathan.Adams@Sun.COM } zio_taskq_info_t;
959515SJonathan.Adams@Sun.COM 
969515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
9711173SJonathan.Adams@Sun.COM 	"issue", "issue_high", "intr", "intr_high"
989515SJonathan.Adams@Sun.COM };
999515SJonathan.Adams@Sun.COM 
10011146SGeorge.Wilson@Sun.COM /*
10111146SGeorge.Wilson@Sun.COM  * Define the taskq threads for the following I/O types:
10211146SGeorge.Wilson@Sun.COM  * 	NULL, READ, WRITE, FREE, CLAIM, and IOCTL
10311146SGeorge.Wilson@Sun.COM  */
10411146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
10511146SGeorge.Wilson@Sun.COM 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
10611146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
10711173SJonathan.Adams@Sun.COM 	{ ZTI_FIX(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL },
10811173SJonathan.Adams@Sun.COM 	{ ZTI_BATCH,	ZTI_FIX(5),	ZTI_FIX(8),	ZTI_FIX(5) },
10911146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11011146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11111146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
1129515SJonathan.Adams@Sun.COM };
1139515SJonathan.Adams@Sun.COM 
1145094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
1157214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
11611422SMark.Musante@Sun.COM static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
11711422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
11811422SMark.Musante@Sun.COM     char **ereport);
1195094Slling 
12011173SJonathan.Adams@Sun.COM uint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
12111173SJonathan.Adams@Sun.COM id_t		zio_taskq_psrset_bind = PS_NONE;
12211173SJonathan.Adams@Sun.COM boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
12311173SJonathan.Adams@Sun.COM uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
12411173SJonathan.Adams@Sun.COM 
12511173SJonathan.Adams@Sun.COM boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
12611173SJonathan.Adams@Sun.COM 
12711173SJonathan.Adams@Sun.COM /*
12811173SJonathan.Adams@Sun.COM  * This (illegal) pool name is used when temporarily importing a spa_t in order
12911173SJonathan.Adams@Sun.COM  * to get the vdev stats associated with the imported devices.
13011173SJonathan.Adams@Sun.COM  */
13111173SJonathan.Adams@Sun.COM #define	TRYIMPORT_NAME	"$import"
13211173SJonathan.Adams@Sun.COM 
1335094Slling /*
1345094Slling  * ==========================================================================
1355094Slling  * SPA properties routines
1365094Slling  * ==========================================================================
1375094Slling  */
1385094Slling 
1395094Slling /*
1405094Slling  * Add a (source=src, propname=propval) list to an nvlist.
1415094Slling  */
1425949Slling static void
1435094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
1445094Slling     uint64_t intval, zprop_source_t src)
1455094Slling {
1465094Slling 	const char *propname = zpool_prop_to_name(prop);
1475094Slling 	nvlist_t *propval;
1485949Slling 
1495949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1505949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1515949Slling 
1525949Slling 	if (strval != NULL)
1535949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1545949Slling 	else
1555949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1565949Slling 
1575949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1585094Slling 	nvlist_free(propval);
1595094Slling }
1605094Slling 
1615094Slling /*
1625094Slling  * Get property values from the spa configuration.
1635094Slling  */
1645949Slling static void
1655094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1665094Slling {
1678525SEric.Schrock@Sun.COM 	uint64_t size;
16810956SGeorge.Wilson@Sun.COM 	uint64_t alloc;
1695094Slling 	uint64_t cap, version;
1705094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1716643Seschrock 	spa_config_dirent_t *dp;
1725094Slling 
1737754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1747754SJeff.Bonwick@Sun.COM 
1758525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
17610956SGeorge.Wilson@Sun.COM 		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
17710922SJeff.Bonwick@Sun.COM 		size = metaslab_class_get_space(spa_normal_class(spa));
1788525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1798525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
18010956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
18110956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
18210956SGeorge.Wilson@Sun.COM 		    size - alloc, src);
18310956SGeorge.Wilson@Sun.COM 
18410956SGeorge.Wilson@Sun.COM 		cap = (size == 0) ? 0 : (alloc * 100 / size);
1858525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1868525SEric.Schrock@Sun.COM 
18710922SJeff.Bonwick@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
18810922SJeff.Bonwick@Sun.COM 		    ddt_get_pool_dedup_ratio(spa), src);
18910922SJeff.Bonwick@Sun.COM 
1908525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1918525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1928525SEric.Schrock@Sun.COM 
1938525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1948525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1958525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1968525SEric.Schrock@Sun.COM 		else
1978525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1988525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1998525SEric.Schrock@Sun.COM 	}
2005949Slling 
2015949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2025949Slling 
2035949Slling 	if (spa->spa_root != NULL)
2045949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2055949Slling 		    0, ZPROP_SRC_LOCAL);
2065094Slling 
2076643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
2086643Seschrock 		if (dp->scd_path == NULL) {
2095949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2106643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
2116643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2125949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2136643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2145363Seschrock 		}
2155363Seschrock 	}
2165094Slling }
2175094Slling 
2185094Slling /*
2195094Slling  * Get zpool property values.
2205094Slling  */
2215094Slling int
2225094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2235094Slling {
22410922SJeff.Bonwick@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
2255094Slling 	zap_cursor_t zc;
2265094Slling 	zap_attribute_t za;
2275094Slling 	int err;
2285094Slling 
2295949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2305094Slling 
2317754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2327754SJeff.Bonwick@Sun.COM 
2335094Slling 	/*
2345094Slling 	 * Get properties from the spa config.
2355094Slling 	 */
2365949Slling 	spa_prop_get_config(spa, nvp);
2375094Slling 
2385094Slling 	/* If no pool property object, no more prop to get. */
239*11619SGeorge.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;
952*11619SGeorge.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 {
140810921STim.Haley@Sun.COM 	uint64_t	sle_metadata_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)
142310921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_metadata_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 
145710921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
145810921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
145910921STim.Haley@Sun.COM 
146011125SJeff.Bonwick@Sun.COM 	error = traverse_pool(spa, spa->spa_verify_min_txg,
146111125SJeff.Bonwick@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
146210921STim.Haley@Sun.COM 
146310921STim.Haley@Sun.COM 	(void) zio_wait(rio);
146410921STim.Haley@Sun.COM 
146510921STim.Haley@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
146610921STim.Haley@Sun.COM 
146710921STim.Haley@Sun.COM 	spa->spa_load_meta_errors = sle.sle_metadata_count;
146810921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
146910921STim.Haley@Sun.COM 
147010921STim.Haley@Sun.COM 	if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta &&
147110921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
147210921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
147310921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
147410921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
147511026STim.Haley@Sun.COM 	} else {
147611026STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
147710921STim.Haley@Sun.COM 	}
147810921STim.Haley@Sun.COM 
147910921STim.Haley@Sun.COM 	if (error) {
148010921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
148110921STim.Haley@Sun.COM 			error = EIO;
148210921STim.Haley@Sun.COM 		return (error);
148310921STim.Haley@Sun.COM 	}
148410921STim.Haley@Sun.COM 
148510921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
148610921STim.Haley@Sun.COM }
148710921STim.Haley@Sun.COM 
14887294Sperrin /*
148911422SMark.Musante@Sun.COM  * Find a value in the pool props object.
149011422SMark.Musante@Sun.COM  */
149111422SMark.Musante@Sun.COM static void
149211422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
149311422SMark.Musante@Sun.COM {
149411422SMark.Musante@Sun.COM 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
149511422SMark.Musante@Sun.COM 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
149611422SMark.Musante@Sun.COM }
149711422SMark.Musante@Sun.COM 
149811422SMark.Musante@Sun.COM /*
149911422SMark.Musante@Sun.COM  * Find a value in the pool directory object.
150011422SMark.Musante@Sun.COM  */
150111422SMark.Musante@Sun.COM static int
150211422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
150311422SMark.Musante@Sun.COM {
150411422SMark.Musante@Sun.COM 	return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
150511422SMark.Musante@Sun.COM 	    name, sizeof (uint64_t), 1, val));
150611422SMark.Musante@Sun.COM }
150711422SMark.Musante@Sun.COM 
150811422SMark.Musante@Sun.COM static int
150911422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
151011422SMark.Musante@Sun.COM {
151111422SMark.Musante@Sun.COM 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
151211422SMark.Musante@Sun.COM 	return (err);
151311422SMark.Musante@Sun.COM }
151411422SMark.Musante@Sun.COM 
151511422SMark.Musante@Sun.COM /*
151611422SMark.Musante@Sun.COM  * Fix up config after a partly-completed split.  This is done with the
151711422SMark.Musante@Sun.COM  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
151811422SMark.Musante@Sun.COM  * pool have that entry in their config, but only the splitting one contains
151911422SMark.Musante@Sun.COM  * a list of all the guids of the vdevs that are being split off.
152011422SMark.Musante@Sun.COM  *
152111422SMark.Musante@Sun.COM  * This function determines what to do with that list: either rejoin
152211422SMark.Musante@Sun.COM  * all the disks to the pool, or complete the splitting process.  To attempt
152311422SMark.Musante@Sun.COM  * the rejoin, each disk that is offlined is marked online again, and
152411422SMark.Musante@Sun.COM  * we do a reopen() call.  If the vdev label for every disk that was
152511422SMark.Musante@Sun.COM  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
152611422SMark.Musante@Sun.COM  * then we call vdev_split() on each disk, and complete the split.
152711422SMark.Musante@Sun.COM  *
152811497SMark.Musante@Sun.COM  * Otherwise we leave the config alone, with all the vdevs in place in
152911497SMark.Musante@Sun.COM  * the original pool.
153011422SMark.Musante@Sun.COM  */
153111422SMark.Musante@Sun.COM static void
153211422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config)
153311422SMark.Musante@Sun.COM {
153411422SMark.Musante@Sun.COM 	uint_t extracted;
153511422SMark.Musante@Sun.COM 	uint64_t *glist;
153611422SMark.Musante@Sun.COM 	uint_t i, gcount;
153711422SMark.Musante@Sun.COM 	nvlist_t *nvl;
153811422SMark.Musante@Sun.COM 	vdev_t **vd;
153911422SMark.Musante@Sun.COM 	boolean_t attempt_reopen;
154011422SMark.Musante@Sun.COM 
154111422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
154211422SMark.Musante@Sun.COM 		return;
154311422SMark.Musante@Sun.COM 
154411422SMark.Musante@Sun.COM 	/* check that the config is complete */
154511422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
154611422SMark.Musante@Sun.COM 	    &glist, &gcount) != 0)
154711422SMark.Musante@Sun.COM 		return;
154811422SMark.Musante@Sun.COM 
154911422SMark.Musante@Sun.COM 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
155011422SMark.Musante@Sun.COM 
155111422SMark.Musante@Sun.COM 	/* attempt to online all the vdevs & validate */
155211422SMark.Musante@Sun.COM 	attempt_reopen = B_TRUE;
155311422SMark.Musante@Sun.COM 	for (i = 0; i < gcount; i++) {
155411422SMark.Musante@Sun.COM 		if (glist[i] == 0)	/* vdev is hole */
155511422SMark.Musante@Sun.COM 			continue;
155611422SMark.Musante@Sun.COM 
155711422SMark.Musante@Sun.COM 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
155811422SMark.Musante@Sun.COM 		if (vd[i] == NULL) {
155911422SMark.Musante@Sun.COM 			/*
156011422SMark.Musante@Sun.COM 			 * Don't bother attempting to reopen the disks;
156111422SMark.Musante@Sun.COM 			 * just do the split.
156211422SMark.Musante@Sun.COM 			 */
156311422SMark.Musante@Sun.COM 			attempt_reopen = B_FALSE;
156411422SMark.Musante@Sun.COM 		} else {
156511422SMark.Musante@Sun.COM 			/* attempt to re-online it */
156611422SMark.Musante@Sun.COM 			vd[i]->vdev_offline = B_FALSE;
156711422SMark.Musante@Sun.COM 		}
156811422SMark.Musante@Sun.COM 	}
156911422SMark.Musante@Sun.COM 
157011422SMark.Musante@Sun.COM 	if (attempt_reopen) {
157111422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
157211422SMark.Musante@Sun.COM 
157311422SMark.Musante@Sun.COM 		/* check each device to see what state it's in */
157411422SMark.Musante@Sun.COM 		for (extracted = 0, i = 0; i < gcount; i++) {
157511422SMark.Musante@Sun.COM 			if (vd[i] != NULL &&
157611422SMark.Musante@Sun.COM 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
157711422SMark.Musante@Sun.COM 				break;
157811422SMark.Musante@Sun.COM 			++extracted;
157911422SMark.Musante@Sun.COM 		}
158011422SMark.Musante@Sun.COM 	}
158111422SMark.Musante@Sun.COM 
158211422SMark.Musante@Sun.COM 	/*
158311422SMark.Musante@Sun.COM 	 * If every disk has been moved to the new pool, or if we never
158411422SMark.Musante@Sun.COM 	 * even attempted to look at them, then we split them off for
158511422SMark.Musante@Sun.COM 	 * good.
158611422SMark.Musante@Sun.COM 	 */
158711422SMark.Musante@Sun.COM 	if (!attempt_reopen || gcount == extracted) {
158811422SMark.Musante@Sun.COM 		for (i = 0; i < gcount; i++)
158911422SMark.Musante@Sun.COM 			if (vd[i] != NULL)
159011422SMark.Musante@Sun.COM 				vdev_split(vd[i]);
159111422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
159211422SMark.Musante@Sun.COM 	}
159311422SMark.Musante@Sun.COM 
159411422SMark.Musante@Sun.COM 	kmem_free(vd, gcount * sizeof (vdev_t *));
159511422SMark.Musante@Sun.COM }
159611422SMark.Musante@Sun.COM 
159711422SMark.Musante@Sun.COM static int
159811422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
159911422SMark.Musante@Sun.COM     boolean_t mosconfig)
160011422SMark.Musante@Sun.COM {
160111422SMark.Musante@Sun.COM 	nvlist_t *config = spa->spa_config;
160211422SMark.Musante@Sun.COM 	char *ereport = FM_EREPORT_ZFS_POOL;
160311422SMark.Musante@Sun.COM 	int error;
160411422SMark.Musante@Sun.COM 	uint64_t pool_guid;
160511422SMark.Musante@Sun.COM 	nvlist_t *nvl;
160611422SMark.Musante@Sun.COM 
160711422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
160811422SMark.Musante@Sun.COM 		return (EINVAL);
160911422SMark.Musante@Sun.COM 
161011422SMark.Musante@Sun.COM 	/*
161111422SMark.Musante@Sun.COM 	 * Versioning wasn't explicitly added to the label until later, so if
161211422SMark.Musante@Sun.COM 	 * it's not present treat it as the initial version.
161311422SMark.Musante@Sun.COM 	 */
161411422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
161511422SMark.Musante@Sun.COM 	    &spa->spa_ubsync.ub_version) != 0)
161611422SMark.Musante@Sun.COM 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
161711422SMark.Musante@Sun.COM 
161811422SMark.Musante@Sun.COM 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
161911422SMark.Musante@Sun.COM 	    &spa->spa_config_txg);
162011422SMark.Musante@Sun.COM 
162111422SMark.Musante@Sun.COM 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
162211422SMark.Musante@Sun.COM 	    spa_guid_exists(pool_guid, 0)) {
162311422SMark.Musante@Sun.COM 		error = EEXIST;
162411422SMark.Musante@Sun.COM 	} else {
162511422SMark.Musante@Sun.COM 		spa->spa_load_guid = pool_guid;
162611422SMark.Musante@Sun.COM 
162711422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
162811422SMark.Musante@Sun.COM 		    &nvl) == 0) {
162911422SMark.Musante@Sun.COM 			VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
163011422SMark.Musante@Sun.COM 			    KM_SLEEP) == 0);
163111422SMark.Musante@Sun.COM 		}
163211422SMark.Musante@Sun.COM 
163311422SMark.Musante@Sun.COM 		error = spa_load_impl(spa, pool_guid, config, state, type,
163411422SMark.Musante@Sun.COM 		    mosconfig, &ereport);
163511422SMark.Musante@Sun.COM 	}
163611422SMark.Musante@Sun.COM 
163711422SMark.Musante@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
163811422SMark.Musante@Sun.COM 	if (error && error != EBADF)
163911422SMark.Musante@Sun.COM 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
164011422SMark.Musante@Sun.COM 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
164111422SMark.Musante@Sun.COM 	spa->spa_ena = 0;
164211422SMark.Musante@Sun.COM 
164311422SMark.Musante@Sun.COM 	return (error);
164411422SMark.Musante@Sun.COM }
164511422SMark.Musante@Sun.COM 
164611422SMark.Musante@Sun.COM /*
1647789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
16481544Seschrock  * source of configuration information.
1649789Sahrens  */
1650789Sahrens static int
165111422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
165211422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
165311422SMark.Musante@Sun.COM     char **ereport)
1654789Sahrens {
1655789Sahrens 	int error = 0;
165610594SGeorge.Wilson@Sun.COM 	nvlist_t *nvconfig, *nvroot = NULL;
1657789Sahrens 	vdev_t *rvd;
1658789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
16591635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
16608241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
166111422SMark.Musante@Sun.COM 	int parse;
1662789Sahrens 
16638241SJeff.Bonwick@Sun.COM 	/*
16648241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
16658241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
16668241SJeff.Bonwick@Sun.COM 	 */
16678241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
16688241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
16698241SJeff.Bonwick@Sun.COM 
16707754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
16717754SJeff.Bonwick@Sun.COM 
16721544Seschrock 	spa->spa_load_state = state;
16731635Sbonwick 
167411422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
167511422SMark.Musante@Sun.COM 		return (EINVAL);
167611422SMark.Musante@Sun.COM 
167711422SMark.Musante@Sun.COM 	parse = (type == SPA_IMPORT_EXISTING ?
167811422SMark.Musante@Sun.COM 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
16792174Seschrock 
1680789Sahrens 	/*
16819234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
16829234SGeorge.Wilson@Sun.COM 	 */
16839630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
16849630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
16859234SGeorge.Wilson@Sun.COM 
16869234SGeorge.Wilson@Sun.COM 	/*
16872082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
16882082Seschrock 	 * value that will be returned by spa_version() since parsing the
16892082Seschrock 	 * configuration requires knowing the version number.
1690789Sahrens 	 */
16917754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
169211422SMark.Musante@Sun.COM 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
16937754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1694789Sahrens 
16952082Seschrock 	if (error != 0)
169611422SMark.Musante@Sun.COM 		return (error);
1697789Sahrens 
16981585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
169911422SMark.Musante@Sun.COM 
170011422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
170111422SMark.Musante@Sun.COM 		ASSERT(spa_guid(spa) == pool_guid);
170211422SMark.Musante@Sun.COM 	}
1703789Sahrens 
1704789Sahrens 	/*
1705789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1706789Sahrens 	 */
17077754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17084070Smc142369 	error = vdev_open(rvd);
17097754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
17104070Smc142369 	if (error != 0)
171111422SMark.Musante@Sun.COM 		return (error);
1712789Sahrens 
1713789Sahrens 	/*
17149276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
17159276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
17169276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
171711422SMark.Musante@Sun.COM 	 * that config.  Otherwise, we're validating against the cached config
17189276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
17199276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
17209276SMark.Musante@Sun.COM 	 * the vdev config.
172111422SMark.Musante@Sun.COM 	 *
172211422SMark.Musante@Sun.COM 	 * If we're assembling a new pool that's been split off from an
172311422SMark.Musante@Sun.COM 	 * existing pool, the labels haven't yet been updated so we skip
172411422SMark.Musante@Sun.COM 	 * validation for now.
17251986Seschrock 	 */
172611422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
172711422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
172811422SMark.Musante@Sun.COM 		error = vdev_validate(rvd);
172911422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
173011422SMark.Musante@Sun.COM 
173111422SMark.Musante@Sun.COM 		if (error != 0)
173211422SMark.Musante@Sun.COM 			return (error);
173311422SMark.Musante@Sun.COM 
173411422SMark.Musante@Sun.COM 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
173511422SMark.Musante@Sun.COM 			return (ENXIO);
17361986Seschrock 	}
17371986Seschrock 
17381986Seschrock 	/*
1739789Sahrens 	 * Find the best uberblock.
1740789Sahrens 	 */
17417754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1742789Sahrens 
1743789Sahrens 	/*
1744789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1745789Sahrens 	 */
174611422SMark.Musante@Sun.COM 	if (ub->ub_txg == 0)
174711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
17481544Seschrock 
17491544Seschrock 	/*
17501544Seschrock 	 * If the pool is newer than the code, we can't open it.
17511544Seschrock 	 */
175211422SMark.Musante@Sun.COM 	if (ub->ub_version > SPA_VERSION)
175311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1754789Sahrens 
1755789Sahrens 	/*
1756789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1757789Sahrens 	 * incomplete configuration.
1758789Sahrens 	 */
175911422SMark.Musante@Sun.COM 	if (mosconfig && type != SPA_IMPORT_ASSEMBLE &&
176011422SMark.Musante@Sun.COM 	    rvd->vdev_guid_sum != ub->ub_guid_sum)
176111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
176211422SMark.Musante@Sun.COM 
176311422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
176411422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
176511422SMark.Musante@Sun.COM 		spa_try_repair(spa, config);
176611422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
176711422SMark.Musante@Sun.COM 		nvlist_free(spa->spa_config_splitting);
176811422SMark.Musante@Sun.COM 		spa->spa_config_splitting = NULL;
1769789Sahrens 	}
1770789Sahrens 
1771789Sahrens 	/*
1772789Sahrens 	 * Initialize internal SPA structures.
1773789Sahrens 	 */
1774789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1775789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
177610921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
177710921STim.Haley@Sun.COM 	    TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE;
177810921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
177910921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
178010922SJeff.Bonwick@Sun.COM 	spa->spa_claim_max_txg = spa->spa_first_txg;
178110922SJeff.Bonwick@Sun.COM 
17821544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
178311422SMark.Musante@Sun.COM 	if (error)
178411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1785789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1786789Sahrens 
178711422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
178811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
178911422SMark.Musante@Sun.COM 
179011422SMark.Musante@Sun.COM 	if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
179111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
179210594SGeorge.Wilson@Sun.COM 
1793789Sahrens 	if (!mosconfig) {
17943975Sek110237 		uint64_t hostid;
17952082Seschrock 
179610594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
17977706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
17983975Sek110237 			char *hostname;
17993975Sek110237 			unsigned long myhostid = 0;
18003975Sek110237 
180110594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
18023975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
18033975Sek110237 
18048662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
18058662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
18068662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
18078662SJordan.Vaughan@Sun.com 			/*
18088662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
18098662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
18108662SJordan.Vaughan@Sun.com 			 */
18113975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
18128662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
18134178Slling 			if (hostid != 0 && myhostid != 0 &&
18148662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
18153975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
18163975Sek110237 				    "loaded as it was last accessed by "
18177706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
18183975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
18197754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
18203975Sek110237 				    (unsigned long)hostid);
182111422SMark.Musante@Sun.COM 				return (EBADF);
18223975Sek110237 			}
18233975Sek110237 		}
18243975Sek110237 
182510594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1826789Sahrens 		spa_unload(spa);
1827789Sahrens 		spa_deactivate(spa);
18288241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1829789Sahrens 
183011422SMark.Musante@Sun.COM 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
18311544Seschrock 	}
18321544Seschrock 
183311422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPLIST,
183411422SMark.Musante@Sun.COM 	    &spa->spa_deferred_bplist_obj) != 0)
183511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1836789Sahrens 
18371544Seschrock 	/*
18382082Seschrock 	 * Load the bit that tells us to use the new accounting function
18392082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
18402082Seschrock 	 * be present.
18412082Seschrock 	 */
184211422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
184311422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
184411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18452082Seschrock 
18462082Seschrock 	/*
18471544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
18481544Seschrock 	 * not be present.
18491544Seschrock 	 */
185011422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
185111422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185211422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
185311422SMark.Musante@Sun.COM 
185411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
185511422SMark.Musante@Sun.COM 	    &spa->spa_errlog_scrub);
185611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
185711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1858789Sahrens 
1859789Sahrens 	/*
18602926Sek110237 	 * Load the history object.  If we have an older pool, this
18612926Sek110237 	 * will not be present.
18622926Sek110237 	 */
186311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
186411422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
186511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
186611422SMark.Musante@Sun.COM 
186711422SMark.Musante@Sun.COM 	/*
186811422SMark.Musante@Sun.COM 	 * If we're assembling the pool from the split-off vdevs of
186911422SMark.Musante@Sun.COM 	 * an existing pool, we don't want to attach the spares & cache
187011422SMark.Musante@Sun.COM 	 * devices.
187111422SMark.Musante@Sun.COM 	 */
18722926Sek110237 
18732926Sek110237 	/*
18742082Seschrock 	 * Load any hot spares for this pool.
18752082Seschrock 	 */
187611422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
187711422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
187811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
187911422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
18804577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
18815450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
188211422SMark.Musante@Sun.COM 		    &spa->spa_spares.sav_config) != 0)
188311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18842082Seschrock 
18857754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
18862082Seschrock 		spa_load_spares(spa);
18877754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
188811422SMark.Musante@Sun.COM 	} else if (error == 0) {
188911422SMark.Musante@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
18902082Seschrock 	}
18912082Seschrock 
18925450Sbrendan 	/*
18935450Sbrendan 	 * Load any level 2 ARC devices for this pool.
18945450Sbrendan 	 */
189511422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
18965450Sbrendan 	    &spa->spa_l2cache.sav_object);
189711422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
189811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
189911422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19005450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
19015450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
190211422SMark.Musante@Sun.COM 		    &spa->spa_l2cache.sav_config) != 0)
190311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19045450Sbrendan 
19057754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19065450Sbrendan 		spa_load_l2cache(spa);
19077754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
190811422SMark.Musante@Sun.COM 	} else if (error == 0) {
190911422SMark.Musante@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
19105450Sbrendan 	}
19115450Sbrendan 
19125094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19134543Smarks 
191411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
191511422SMark.Musante@Sun.COM 	if (error && error != ENOENT)
191611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19173912Slling 
19183912Slling 	if (error == 0) {
191911422SMark.Musante@Sun.COM 		uint64_t autoreplace;
192011422SMark.Musante@Sun.COM 
192111422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
192211422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
192311422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
192411422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
192511422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
192611422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
192711422SMark.Musante@Sun.COM 		    &spa->spa_dedup_ditto);
192811422SMark.Musante@Sun.COM 
192910672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
19303912Slling 	}
19313912Slling 
19322082Seschrock 	/*
19334451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
19344451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
19354451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
19364451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
19374451Seschrock 	 * over.
19384451Seschrock 	 */
193910672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
19404451Seschrock 		spa_check_removed(spa->spa_root_vdev);
194110672SEric.Schrock@Sun.COM 		/*
194210672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
194310672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
194410672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
194510672SEric.Schrock@Sun.COM 		 */
194610672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
194710672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
194810672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
194910672SEric.Schrock@Sun.COM 		}
195010672SEric.Schrock@Sun.COM 	}
19514451Seschrock 
19524451Seschrock 	/*
19531986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1954789Sahrens 	 */
19551986Seschrock 	vdev_load(rvd);
1956789Sahrens 
1957789Sahrens 	/*
1958789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1959789Sahrens 	 */
19607754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1961789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
19627754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1963789Sahrens 
1964789Sahrens 	/*
1965789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1966789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1967789Sahrens 	 */
196811422SMark.Musante@Sun.COM 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
196911422SMark.Musante@Sun.COM 		return (ENXIO);
1970789Sahrens 
197110922SJeff.Bonwick@Sun.COM 	/*
197210922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
197310922SJeff.Bonwick@Sun.COM 	 */
197410922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
197511422SMark.Musante@Sun.COM 	if (error != 0)
197611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
197710922SJeff.Bonwick@Sun.COM 
197810956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
197910956SGeorge.Wilson@Sun.COM 
198010921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
198110921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
198211422SMark.Musante@Sun.COM 		if (error)
198311422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
198411422SMark.Musante@Sun.COM 			    error));
198510921STim.Haley@Sun.COM 	}
198610921STim.Haley@Sun.COM 
198710922SJeff.Bonwick@Sun.COM 	/*
198811422SMark.Musante@Sun.COM 	 * Load the intent log state and check log integrity.  If we're
198911422SMark.Musante@Sun.COM 	 * assembling a pool from a split, the log is not transferred over.
199010922SJeff.Bonwick@Sun.COM 	 */
199111422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
199211422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
199311422SMark.Musante@Sun.COM 		    &nvroot) == 0);
199411422SMark.Musante@Sun.COM 		spa_load_log_state(spa, nvroot);
199511422SMark.Musante@Sun.COM 		nvlist_free(nvconfig);
199611422SMark.Musante@Sun.COM 
199711422SMark.Musante@Sun.COM 		if (spa_check_logs(spa)) {
199811422SMark.Musante@Sun.COM 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
199911422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
200011422SMark.Musante@Sun.COM 		}
200110922SJeff.Bonwick@Sun.COM 	}
200210922SJeff.Bonwick@Sun.COM 
200310921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
200410921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
20051635Sbonwick 		dmu_tx_t *tx;
20061635Sbonwick 		int need_update = B_FALSE;
20078241SJeff.Bonwick@Sun.COM 
20088241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
20091601Sbonwick 
20101635Sbonwick 		/*
20111635Sbonwick 		 * Claim log blocks that haven't been committed yet.
20121635Sbonwick 		 * This must all happen in a single txg.
201310922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
201410922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
201510921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
20161635Sbonwick 		 */
201710922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
201810922SJeff.Bonwick@Sun.COM 
20191601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2020789Sahrens 		    spa_first_txg(spa));
20217754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
20222417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2023789Sahrens 		dmu_tx_commit(tx);
2024789Sahrens 
202510922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
202610922SJeff.Bonwick@Sun.COM 
202711422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_GOOD);
2028789Sahrens 		spa->spa_sync_on = B_TRUE;
2029789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2030789Sahrens 
2031789Sahrens 		/*
203210922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
203310922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
203410922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
203510922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
203610922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
2037789Sahrens 		 */
203810922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
20391585Sbonwick 
20401585Sbonwick 		/*
20411635Sbonwick 		 * If the config cache is stale, or we have uninitialized
20421635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
204310100SLin.Ling@Sun.COM 		 *
204410100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
204510100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
20461585Sbonwick 		 */
20471635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
204810921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
204910921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
20501635Sbonwick 			need_update = B_TRUE;
20511635Sbonwick 
20528241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
20531635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
20541635Sbonwick 				need_update = B_TRUE;
20551585Sbonwick 
20561585Sbonwick 		/*
20571635Sbonwick 		 * Update the config cache asychronously in case we're the
20581635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
20591585Sbonwick 		 */
20601635Sbonwick 		if (need_update)
20611635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20628241SJeff.Bonwick@Sun.COM 
20638241SJeff.Bonwick@Sun.COM 		/*
20648241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
20658241SJeff.Bonwick@Sun.COM 		 */
20668241SJeff.Bonwick@Sun.COM 		if (vdev_resilver_needed(rvd, NULL, NULL))
20678241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
206810298SMatthew.Ahrens@Sun.COM 
206910298SMatthew.Ahrens@Sun.COM 		/*
207010298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
207110298SMatthew.Ahrens@Sun.COM 		 */
207210298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
207310298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
207410342Schris.kirby@sun.com 
207510342Schris.kirby@sun.com 		/*
207610342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
207710342Schris.kirby@sun.com 		 */
207810342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2079789Sahrens 	}
2080789Sahrens 
208111422SMark.Musante@Sun.COM 	return (0);
2082789Sahrens }
2083789Sahrens 
208410921STim.Haley@Sun.COM static int
208510921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
208610921STim.Haley@Sun.COM {
208710921STim.Haley@Sun.COM 	spa_unload(spa);
208810921STim.Haley@Sun.COM 	spa_deactivate(spa);
208910921STim.Haley@Sun.COM 
209010921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
209110921STim.Haley@Sun.COM 
209210921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
209310921STim.Haley@Sun.COM 	spa_async_suspend(spa);
209410921STim.Haley@Sun.COM 
209511422SMark.Musante@Sun.COM 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
209610921STim.Haley@Sun.COM }
209710921STim.Haley@Sun.COM 
209810921STim.Haley@Sun.COM static int
209910921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
210010921STim.Haley@Sun.COM     uint64_t max_request, boolean_t extreme)
210110921STim.Haley@Sun.COM {
210210921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
210310921STim.Haley@Sun.COM 	int load_error, rewind_error;
210410921STim.Haley@Sun.COM 	uint64_t safe_rollback_txg;
210510921STim.Haley@Sun.COM 	uint64_t min_txg;
210610921STim.Haley@Sun.COM 
210711026STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
210810921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
210911422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
211011026STim.Haley@Sun.COM 	} else {
211110921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
211211026STim.Haley@Sun.COM 	}
211310921STim.Haley@Sun.COM 
211411422SMark.Musante@Sun.COM 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
211511422SMark.Musante@Sun.COM 	    mosconfig);
211610921STim.Haley@Sun.COM 	if (load_error == 0)
211710921STim.Haley@Sun.COM 		return (0);
211810921STim.Haley@Sun.COM 
211910921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
212010921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
212110921STim.Haley@Sun.COM 
212210921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
212310921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
212410921STim.Haley@Sun.COM 
212510921STim.Haley@Sun.COM 	/* specific txg requested */
212610921STim.Haley@Sun.COM 	if (spa->spa_load_max_txg != UINT64_MAX && !extreme) {
212710921STim.Haley@Sun.COM 		nvlist_free(config);
212810921STim.Haley@Sun.COM 		return (load_error);
212910921STim.Haley@Sun.COM 	}
213010921STim.Haley@Sun.COM 
213110921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
213210921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
213311422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
213410921STim.Haley@Sun.COM 
213510921STim.Haley@Sun.COM 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
213610921STim.Haley@Sun.COM 	safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE;
213710921STim.Haley@Sun.COM 
213810921STim.Haley@Sun.COM 	min_txg = extreme ? TXG_INITIAL : safe_rollback_txg;
213910921STim.Haley@Sun.COM 	while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) {
214010921STim.Haley@Sun.COM 		if (spa->spa_load_max_txg < safe_rollback_txg)
214110921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
214210921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
214310921STim.Haley@Sun.COM 	}
214410921STim.Haley@Sun.COM 
214510921STim.Haley@Sun.COM 	if (config)
214610921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
214710921STim.Haley@Sun.COM 
214810921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
214910921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
215010921STim.Haley@Sun.COM 
215110921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
215210921STim.Haley@Sun.COM 		spa_config_set(spa, config);
215310921STim.Haley@Sun.COM 
215410921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
215510921STim.Haley@Sun.COM }
215610921STim.Haley@Sun.COM 
2157789Sahrens /*
2158789Sahrens  * Pool Open/Import
2159789Sahrens  *
2160789Sahrens  * The import case is identical to an open except that the configuration is sent
2161789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2162789Sahrens  * case of an open, the pool configuration will exist in the
21634451Seschrock  * POOL_STATE_UNINITIALIZED state.
2164789Sahrens  *
2165789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2166789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2167789Sahrens  * ambiguous state.
2168789Sahrens  */
2169789Sahrens static int
217010921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
217110921STim.Haley@Sun.COM     nvlist_t **config)
2172789Sahrens {
2173789Sahrens 	spa_t *spa;
217410921STim.Haley@Sun.COM 	boolean_t norewind;
217510921STim.Haley@Sun.COM 	boolean_t extreme;
217610921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
217710921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_OPEN;
2178789Sahrens 	int error;
2179789Sahrens 	int locked = B_FALSE;
2180789Sahrens 
2181789Sahrens 	*spapp = NULL;
2182789Sahrens 
218310921STim.Haley@Sun.COM 	zpool_get_rewind_policy(nvpolicy, &policy);
218410921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
218510921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
218610921STim.Haley@Sun.COM 	norewind = (policy.zrp_request == ZPOOL_NO_REWIND);
218710921STim.Haley@Sun.COM 	extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0);
218810921STim.Haley@Sun.COM 
2189789Sahrens 	/*
2190789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2191789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2192789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2193789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2194789Sahrens 	 */
2195789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2196789Sahrens 		mutex_enter(&spa_namespace_lock);
2197789Sahrens 		locked = B_TRUE;
2198789Sahrens 	}
2199789Sahrens 
2200789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2201789Sahrens 		if (locked)
2202789Sahrens 			mutex_exit(&spa_namespace_lock);
2203789Sahrens 		return (ENOENT);
2204789Sahrens 	}
220510921STim.Haley@Sun.COM 
2206789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2207789Sahrens 
22088241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
2209789Sahrens 
221010921STim.Haley@Sun.COM 		if (spa->spa_last_open_failed && norewind) {
221110921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
221210921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config,
221310921STim.Haley@Sun.COM 				    config, KM_SLEEP) == 0);
221410921STim.Haley@Sun.COM 			spa_deactivate(spa);
221510921STim.Haley@Sun.COM 			if (locked)
221610921STim.Haley@Sun.COM 				mutex_exit(&spa_namespace_lock);
221710921STim.Haley@Sun.COM 			return (spa->spa_last_open_failed);
221810921STim.Haley@Sun.COM 		}
221910921STim.Haley@Sun.COM 
222010921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
222110921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
222210921STim.Haley@Sun.COM 
222310921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
222410921STim.Haley@Sun.COM 		    extreme);
2225789Sahrens 
2226789Sahrens 		if (error == EBADF) {
2227789Sahrens 			/*
22281986Seschrock 			 * If vdev_validate() returns failure (indicated by
22291986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
22301986Seschrock 			 * that the pool has been exported or destroyed.  If
22311986Seschrock 			 * this is the case, the config cache is out of sync and
22321986Seschrock 			 * we should remove the pool from the namespace.
2233789Sahrens 			 */
2234789Sahrens 			spa_unload(spa);
2235789Sahrens 			spa_deactivate(spa);
22366643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2237789Sahrens 			spa_remove(spa);
2238789Sahrens 			if (locked)
2239789Sahrens 				mutex_exit(&spa_namespace_lock);
2240789Sahrens 			return (ENOENT);
22411544Seschrock 		}
22421544Seschrock 
22431544Seschrock 		if (error) {
2244789Sahrens 			/*
2245789Sahrens 			 * We can't open the pool, but we still have useful
2246789Sahrens 			 * information: the state of each vdev after the
2247789Sahrens 			 * attempted vdev_open().  Return this to the user.
2248789Sahrens 			 */
224910921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
225010921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
225110921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
2252789Sahrens 			spa_unload(spa);
2253789Sahrens 			spa_deactivate(spa);
225410921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
2255789Sahrens 			if (locked)
2256789Sahrens 				mutex_exit(&spa_namespace_lock);
2257789Sahrens 			*spapp = NULL;
2258789Sahrens 			return (error);
2259789Sahrens 		}
226010921STim.Haley@Sun.COM 
2261789Sahrens 	}
2262789Sahrens 
2263789Sahrens 	spa_open_ref(spa, tag);
22644451Seschrock 
226510921STim.Haley@Sun.COM 
226610921STim.Haley@Sun.COM 	if (config != NULL)
226710921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
226810921STim.Haley@Sun.COM 
226911026STim.Haley@Sun.COM 	if (locked) {
227011026STim.Haley@Sun.COM 		spa->spa_last_open_failed = 0;
227111026STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = 0;
227211026STim.Haley@Sun.COM 		spa->spa_load_txg = 0;
2273789Sahrens 		mutex_exit(&spa_namespace_lock);
227411026STim.Haley@Sun.COM 	}
2275789Sahrens 
2276789Sahrens 	*spapp = spa;
2277789Sahrens 
2278789Sahrens 	return (0);
2279789Sahrens }
2280789Sahrens 
2281789Sahrens int
228210921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
228310921STim.Haley@Sun.COM     nvlist_t **config)
228410921STim.Haley@Sun.COM {
228510921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
228610921STim.Haley@Sun.COM }
228710921STim.Haley@Sun.COM 
228810921STim.Haley@Sun.COM int
2289789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2290789Sahrens {
229110921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2292789Sahrens }
2293789Sahrens 
22941544Seschrock /*
22951544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
22961544Seschrock  * preventing it from being exported or destroyed.
22971544Seschrock  */
22981544Seschrock spa_t *
22991544Seschrock spa_inject_addref(char *name)
23001544Seschrock {
23011544Seschrock 	spa_t *spa;
23021544Seschrock 
23031544Seschrock 	mutex_enter(&spa_namespace_lock);
23041544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
23051544Seschrock 		mutex_exit(&spa_namespace_lock);
23061544Seschrock 		return (NULL);
23071544Seschrock 	}
23081544Seschrock 	spa->spa_inject_ref++;
23091544Seschrock 	mutex_exit(&spa_namespace_lock);
23101544Seschrock 
23111544Seschrock 	return (spa);
23121544Seschrock }
23131544Seschrock 
23141544Seschrock void
23151544Seschrock spa_inject_delref(spa_t *spa)
23161544Seschrock {
23171544Seschrock 	mutex_enter(&spa_namespace_lock);
23181544Seschrock 	spa->spa_inject_ref--;
23191544Seschrock 	mutex_exit(&spa_namespace_lock);
23201544Seschrock }
23211544Seschrock 
23225450Sbrendan /*
23235450Sbrendan  * Add spares device information to the nvlist.
23245450Sbrendan  */
23252082Seschrock static void
23262082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
23272082Seschrock {
23282082Seschrock 	nvlist_t **spares;
23292082Seschrock 	uint_t i, nspares;
23302082Seschrock 	nvlist_t *nvroot;
23312082Seschrock 	uint64_t guid;
23322082Seschrock 	vdev_stat_t *vs;
23332082Seschrock 	uint_t vsc;
23343377Seschrock 	uint64_t pool;
23352082Seschrock 
23369425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23379425SEric.Schrock@Sun.COM 
23385450Sbrendan 	if (spa->spa_spares.sav_count == 0)
23392082Seschrock 		return;
23402082Seschrock 
23412082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
23422082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23435450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
23442082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23452082Seschrock 	if (nspares != 0) {
23462082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
23472082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
23482082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23492082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23502082Seschrock 
23512082Seschrock 		/*
23522082Seschrock 		 * Go through and find any spares which have since been
23532082Seschrock 		 * repurposed as an active spare.  If this is the case, update
23542082Seschrock 		 * their status appropriately.
23552082Seschrock 		 */
23562082Seschrock 		for (i = 0; i < nspares; i++) {
23572082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
23582082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23597214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
23607214Slling 			    pool != 0ULL) {
23612082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
23622082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
23632082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
23642082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
23652082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
23662082Seschrock 			}
23672082Seschrock 		}
23682082Seschrock 	}
23692082Seschrock }
23702082Seschrock 
23715450Sbrendan /*
23725450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
23735450Sbrendan  */
23745450Sbrendan static void
23755450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
23765450Sbrendan {
23775450Sbrendan 	nvlist_t **l2cache;
23785450Sbrendan 	uint_t i, j, nl2cache;
23795450Sbrendan 	nvlist_t *nvroot;
23805450Sbrendan 	uint64_t guid;
23815450Sbrendan 	vdev_t *vd;
23825450Sbrendan 	vdev_stat_t *vs;
23835450Sbrendan 	uint_t vsc;
23845450Sbrendan 
23859425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23869425SEric.Schrock@Sun.COM 
23875450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
23885450Sbrendan 		return;
23895450Sbrendan 
23905450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
23915450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23925450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
23935450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
23945450Sbrendan 	if (nl2cache != 0) {
23955450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
23965450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
23975450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23985450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
23995450Sbrendan 
24005450Sbrendan 		/*
24015450Sbrendan 		 * Update level 2 cache device stats.
24025450Sbrendan 		 */
24035450Sbrendan 
24045450Sbrendan 		for (i = 0; i < nl2cache; i++) {
24055450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
24065450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
24075450Sbrendan 
24085450Sbrendan 			vd = NULL;
24095450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
24105450Sbrendan 				if (guid ==
24115450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
24125450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
24135450Sbrendan 					break;
24145450Sbrendan 				}
24155450Sbrendan 			}
24165450Sbrendan 			ASSERT(vd != NULL);
24175450Sbrendan 
24185450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
24195450Sbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
24205450Sbrendan 			vdev_get_stats(vd, vs);
24215450Sbrendan 		}
24225450Sbrendan 	}
24235450Sbrendan }
24245450Sbrendan 
2425789Sahrens int
24261544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2427789Sahrens {
2428789Sahrens 	int error;
2429789Sahrens 	spa_t *spa;
2430789Sahrens 
2431789Sahrens 	*config = NULL;
243210921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2433789Sahrens 
24349425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24359425SEric.Schrock@Sun.COM 		/*
24369425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
24379425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
24389425SEric.Schrock@Sun.COM 		 * self-inconsistent.
24399425SEric.Schrock@Sun.COM 		 */
24409425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
24419425SEric.Schrock@Sun.COM 
24429425SEric.Schrock@Sun.COM 		if (*config != NULL) {
24437754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
24449425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
24459425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
24469425SEric.Schrock@Sun.COM 
24479425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
24489425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
24499425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
24509425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
24519425SEric.Schrock@Sun.COM 
24529425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
24539425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
24549425SEric.Schrock@Sun.COM 		}
24552082Seschrock 	}
24562082Seschrock 
24571544Seschrock 	/*
24581544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
24591544Seschrock 	 * and call spa_lookup() directly.
24601544Seschrock 	 */
24611544Seschrock 	if (altroot) {
24621544Seschrock 		if (spa == NULL) {
24631544Seschrock 			mutex_enter(&spa_namespace_lock);
24641544Seschrock 			spa = spa_lookup(name);
24651544Seschrock 			if (spa)
24661544Seschrock 				spa_altroot(spa, altroot, buflen);
24671544Seschrock 			else
24681544Seschrock 				altroot[0] = '\0';
24691544Seschrock 			spa = NULL;
24701544Seschrock 			mutex_exit(&spa_namespace_lock);
24711544Seschrock 		} else {
24721544Seschrock 			spa_altroot(spa, altroot, buflen);
24731544Seschrock 		}
24741544Seschrock 	}
24751544Seschrock 
24769425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24779425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2478789Sahrens 		spa_close(spa, FTAG);
24799425SEric.Schrock@Sun.COM 	}
2480789Sahrens 
2481789Sahrens 	return (error);
2482789Sahrens }
2483789Sahrens 
2484789Sahrens /*
24855450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
24865450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
24875450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
24885450Sbrendan  * specified, as long as they are well-formed.
24892082Seschrock  */
24902082Seschrock static int
24915450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
24925450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
24935450Sbrendan     vdev_labeltype_t label)
24942082Seschrock {
24955450Sbrendan 	nvlist_t **dev;
24965450Sbrendan 	uint_t i, ndev;
24972082Seschrock 	vdev_t *vd;
24982082Seschrock 	int error;
24992082Seschrock 
25007754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25017754SJeff.Bonwick@Sun.COM 
25022082Seschrock 	/*
25035450Sbrendan 	 * It's acceptable to have no devs specified.
25042082Seschrock 	 */
25055450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
25062082Seschrock 		return (0);
25072082Seschrock 
25085450Sbrendan 	if (ndev == 0)
25092082Seschrock 		return (EINVAL);
25102082Seschrock 
25112082Seschrock 	/*
25125450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
25135450Sbrendan 	 * device type.
25142082Seschrock 	 */
25155450Sbrendan 	if (spa_version(spa) < version)
25162082Seschrock 		return (ENOTSUP);
25172082Seschrock 
25183377Seschrock 	/*
25195450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
25203377Seschrock 	 * checking.
25213377Seschrock 	 */
25225450Sbrendan 	sav->sav_pending = dev;
25235450Sbrendan 	sav->sav_npending = ndev;
25245450Sbrendan 
25255450Sbrendan 	for (i = 0; i < ndev; i++) {
25265450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
25272082Seschrock 		    mode)) != 0)
25283377Seschrock 			goto out;
25292082Seschrock 
25302082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
25312082Seschrock 			vdev_free(vd);
25323377Seschrock 			error = EINVAL;
25333377Seschrock 			goto out;
25342082Seschrock 		}
25352082Seschrock 
25365450Sbrendan 		/*
25377754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
25387754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
25395450Sbrendan 		 */
25407754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
25415450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
25425450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
25435450Sbrendan 			error = ENOTBLK;
25445450Sbrendan 			goto out;
25455450Sbrendan 		}
25467754SJeff.Bonwick@Sun.COM #endif
25472082Seschrock 		vd->vdev_top = vd;
25483377Seschrock 
25493377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
25505450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
25515450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
25523377Seschrock 			    vd->vdev_guid) == 0);
25532082Seschrock 		}
25542082Seschrock 
25552082Seschrock 		vdev_free(vd);
25563377Seschrock 
25575450Sbrendan 		if (error &&
25585450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
25593377Seschrock 			goto out;
25603377Seschrock 		else
25613377Seschrock 			error = 0;
25622082Seschrock 	}
25632082Seschrock 
25643377Seschrock out:
25655450Sbrendan 	sav->sav_pending = NULL;
25665450Sbrendan 	sav->sav_npending = 0;
25673377Seschrock 	return (error);
25682082Seschrock }
25692082Seschrock 
25705450Sbrendan static int
25715450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
25725450Sbrendan {
25735450Sbrendan 	int error;
25745450Sbrendan 
25757754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25767754SJeff.Bonwick@Sun.COM 
25775450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
25785450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
25795450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
25805450Sbrendan 		return (error);
25815450Sbrendan 	}
25825450Sbrendan 
25835450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
25845450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
25855450Sbrendan 	    VDEV_LABEL_L2CACHE));
25865450Sbrendan }
25875450Sbrendan 
25885450Sbrendan static void
25895450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
25905450Sbrendan     const char *config)
25915450Sbrendan {
25925450Sbrendan 	int i;
25935450Sbrendan 
25945450Sbrendan 	if (sav->sav_config != NULL) {
25955450Sbrendan 		nvlist_t **olddevs;
25965450Sbrendan 		uint_t oldndevs;
25975450Sbrendan 		nvlist_t **newdevs;
25985450Sbrendan 
25995450Sbrendan 		/*
26005450Sbrendan 		 * Generate new dev list by concatentating with the
26015450Sbrendan 		 * current dev list.
26025450Sbrendan 		 */
26035450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
26045450Sbrendan 		    &olddevs, &oldndevs) == 0);
26055450Sbrendan 
26065450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
26075450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
26085450Sbrendan 		for (i = 0; i < oldndevs; i++)
26095450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
26105450Sbrendan 			    KM_SLEEP) == 0);
26115450Sbrendan 		for (i = 0; i < ndevs; i++)
26125450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
26135450Sbrendan 			    KM_SLEEP) == 0);
26145450Sbrendan 
26155450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
26165450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
26175450Sbrendan 
26185450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
26195450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
26205450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
26215450Sbrendan 			nvlist_free(newdevs[i]);
26225450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
26235450Sbrendan 	} else {
26245450Sbrendan 		/*
26255450Sbrendan 		 * Generate a new dev list.
26265450Sbrendan 		 */
26275450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
26285450Sbrendan 		    KM_SLEEP) == 0);
26295450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
26305450Sbrendan 		    devs, ndevs) == 0);
26315450Sbrendan 	}
26325450Sbrendan }
26335450Sbrendan 
26345450Sbrendan /*
26355450Sbrendan  * Stop and drop level 2 ARC devices
26365450Sbrendan  */
26375450Sbrendan void
26385450Sbrendan spa_l2cache_drop(spa_t *spa)
26395450Sbrendan {
26405450Sbrendan 	vdev_t *vd;
26415450Sbrendan 	int i;
26425450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
26435450Sbrendan 
26445450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
26455450Sbrendan 		uint64_t pool;
26465450Sbrendan 
26475450Sbrendan 		vd = sav->sav_vdevs[i];
26485450Sbrendan 		ASSERT(vd != NULL);
26495450Sbrendan 
26508241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
26518241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
26525450Sbrendan 			l2arc_remove_vdev(vd);
26535450Sbrendan 		if (vd->vdev_isl2cache)
26545450Sbrendan 			spa_l2cache_remove(vd);
26555450Sbrendan 		vdev_clear_stats(vd);
26565450Sbrendan 		(void) vdev_close(vd);
26575450Sbrendan 	}
26585450Sbrendan }
26595450Sbrendan 
26602082Seschrock /*
2661789Sahrens  * Pool Creation
2662789Sahrens  */
2663789Sahrens int
26645094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
26657184Stimh     const char *history_str, nvlist_t *zplprops)
2666789Sahrens {
2667789Sahrens 	spa_t *spa;
26685094Slling 	char *altroot = NULL;
26691635Sbonwick 	vdev_t *rvd;
2670789Sahrens 	dsl_pool_t *dp;
2671789Sahrens 	dmu_tx_t *tx;
26729816SGeorge.Wilson@Sun.COM 	int error = 0;
2673789Sahrens 	uint64_t txg = TXG_INITIAL;
26745450Sbrendan 	nvlist_t **spares, **l2cache;
26755450Sbrendan 	uint_t nspares, nl2cache;
26765094Slling 	uint64_t version;
2677789Sahrens 
2678789Sahrens 	/*
2679789Sahrens 	 * If this pool already exists, return failure.
2680789Sahrens 	 */
2681789Sahrens 	mutex_enter(&spa_namespace_lock);
2682789Sahrens 	if (spa_lookup(pool) != NULL) {
2683789Sahrens 		mutex_exit(&spa_namespace_lock);
2684789Sahrens 		return (EEXIST);
2685789Sahrens 	}
2686789Sahrens 
2687789Sahrens 	/*
2688789Sahrens 	 * Allocate a new spa_t structure.
2689789Sahrens 	 */
26905094Slling 	(void) nvlist_lookup_string(props,
26915094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
269210921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
26938241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2694789Sahrens 
26955094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
26965094Slling 		spa_deactivate(spa);
26975094Slling 		spa_remove(spa);
26986643Seschrock 		mutex_exit(&spa_namespace_lock);
26995094Slling 		return (error);
27005094Slling 	}
27015094Slling 
27025094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
27035094Slling 	    &version) != 0)
27045094Slling 		version = SPA_VERSION;
27055094Slling 	ASSERT(version <= SPA_VERSION);
270610922SJeff.Bonwick@Sun.COM 
270710922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
270810922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
27095094Slling 	spa->spa_uberblock.ub_version = version;
2710789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2711789Sahrens 
27121635Sbonwick 	/*
27139234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
27149234SGeorge.Wilson@Sun.COM 	 */
27159630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
27169630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
27179234SGeorge.Wilson@Sun.COM 
27189234SGeorge.Wilson@Sun.COM 	/*
27191635Sbonwick 	 * Create the root vdev.
27201635Sbonwick 	 */
27217754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27221635Sbonwick 
27232082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
27242082Seschrock 
27252082Seschrock 	ASSERT(error != 0 || rvd != NULL);
27262082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
27272082Seschrock 
27285913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
27291635Sbonwick 		error = EINVAL;
27302082Seschrock 
27312082Seschrock 	if (error == 0 &&
27322082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
27335450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
27342082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
27359816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
27369816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
27379816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
27389816SGeorge.Wilson@Sun.COM 		}
27391635Sbonwick 	}
27401635Sbonwick 
27417754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2742789Sahrens 
27432082Seschrock 	if (error != 0) {
2744789Sahrens 		spa_unload(spa);
2745789Sahrens 		spa_deactivate(spa);
2746789Sahrens 		spa_remove(spa);
2747789Sahrens 		mutex_exit(&spa_namespace_lock);
2748789Sahrens 		return (error);
2749789Sahrens 	}
2750789Sahrens 
27512082Seschrock 	/*
27522082Seschrock 	 * Get the list of spares, if specified.
27532082Seschrock 	 */
27542082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
27552082Seschrock 	    &spares, &nspares) == 0) {
27565450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
27572082Seschrock 		    KM_SLEEP) == 0);
27585450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
27592082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
27607754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27612082Seschrock 		spa_load_spares(spa);
27627754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27635450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27645450Sbrendan 	}
27655450Sbrendan 
27665450Sbrendan 	/*
27675450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
27685450Sbrendan 	 */
27695450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
27705450Sbrendan 	    &l2cache, &nl2cache) == 0) {
27715450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
27725450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
27735450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
27745450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
27757754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27765450Sbrendan 		spa_load_l2cache(spa);
27777754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27785450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
27792082Seschrock 	}
27802082Seschrock 
27817184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2782789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2783789Sahrens 
278410956SGeorge.Wilson@Sun.COM 	/*
278510956SGeorge.Wilson@Sun.COM 	 * Create DDTs (dedup tables).
278610956SGeorge.Wilson@Sun.COM 	 */
278710956SGeorge.Wilson@Sun.COM 	ddt_create(spa);
278810956SGeorge.Wilson@Sun.COM 
278910956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
279010956SGeorge.Wilson@Sun.COM 
2791789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2792789Sahrens 
2793789Sahrens 	/*
2794789Sahrens 	 * Create the pool config object.
2795789Sahrens 	 */
2796789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
27977497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2798789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2799789Sahrens 
28001544Seschrock 	if (zap_add(spa->spa_meta_objset,
2801789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
28021544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
28031544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
28041544Seschrock 	}
2805789Sahrens 
28065094Slling 	/* Newly created pools with the right version are always deflated. */
28075094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
28085094Slling 		spa->spa_deflate = TRUE;
28095094Slling 		if (zap_add(spa->spa_meta_objset,
28105094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
28115094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
28125094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
28135094Slling 		}
28142082Seschrock 	}
28152082Seschrock 
2816789Sahrens 	/*
2817789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2818789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2819789Sahrens 	 * keeps changing.
2820789Sahrens 	 */
282110922SJeff.Bonwick@Sun.COM 	spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset,
2822789Sahrens 	    1 << 14, tx);
282310922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset,
282410922SJeff.Bonwick@Sun.COM 	    spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx);
2825789Sahrens 
28261544Seschrock 	if (zap_add(spa->spa_meta_objset,
2827789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
282810922SJeff.Bonwick@Sun.COM 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) {
28291544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
28301544Seschrock 	}
2831789Sahrens 
28322926Sek110237 	/*
28332926Sek110237 	 * Create the pool's history object.
28342926Sek110237 	 */
28355094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
28365094Slling 		spa_history_create_obj(spa, tx);
28375094Slling 
28385094Slling 	/*
28395094Slling 	 * Set pool properties.
28405094Slling 	 */
28415094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
28425094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
28435329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
28449816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
284510922SJeff.Bonwick@Sun.COM 
28468525SEric.Schrock@Sun.COM 	if (props != NULL) {
28478525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
28485094Slling 		spa_sync_props(spa, props, CRED(), tx);
28498525SEric.Schrock@Sun.COM 	}
28502926Sek110237 
2851789Sahrens 	dmu_tx_commit(tx);
2852789Sahrens 
2853789Sahrens 	spa->spa_sync_on = B_TRUE;
2854789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2855789Sahrens 
2856789Sahrens 	/*
2857789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2858789Sahrens 	 * bean counters are appropriately updated.
2859789Sahrens 	 */
2860789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2861789Sahrens 
28626643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2863789Sahrens 
28645094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
28654715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
28669946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
28674715Sek110237 
28688667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
28698667SGeorge.Wilson@Sun.COM 
2870789Sahrens 	mutex_exit(&spa_namespace_lock);
2871789Sahrens 
2872789Sahrens 	return (0);
2873789Sahrens }
2874789Sahrens 
28756423Sgw25295 #ifdef _KERNEL
28766423Sgw25295 /*
28779790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
28789790SLin.Ling@Sun.COM  * during the system boot up time.
28796423Sgw25295  */
28809790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
28819790SLin.Ling@Sun.COM 
28829790SLin.Ling@Sun.COM static nvlist_t *
28839790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
28846423Sgw25295 {
28859790SLin.Ling@Sun.COM 	nvlist_t *config;
28866423Sgw25295 	nvlist_t *nvtop, *nvroot;
28876423Sgw25295 	uint64_t pgid;
28886423Sgw25295 
28899790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
28909790SLin.Ling@Sun.COM 		return (NULL);
28919790SLin.Ling@Sun.COM 
28926423Sgw25295 	/*
28936423Sgw25295 	 * Add this top-level vdev to the child array.
28946423Sgw25295 	 */
28959790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
28969790SLin.Ling@Sun.COM 	    &nvtop) == 0);
28979790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
28989790SLin.Ling@Sun.COM 	    &pgid) == 0);
28999790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
29006423Sgw25295 
29016423Sgw25295 	/*
29026423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
29036423Sgw25295 	 */
29046423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
29059790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
29069790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
29076423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
29086423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
29096423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
29106423Sgw25295 	    &nvtop, 1) == 0);
29116423Sgw25295 
29126423Sgw25295 	/*
29136423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
29146423Sgw25295 	 * this pool's configuration (remove the old, add the new).
29156423Sgw25295 	 */
29166423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
29176423Sgw25295 	nvlist_free(nvroot);
29189790SLin.Ling@Sun.COM 	return (config);
29196423Sgw25295 }
29206423Sgw25295 
29216423Sgw25295 /*
29229790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
29239790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
29249790SLin.Ling@Sun.COM  * device has a more recent txg.
29256423Sgw25295  */
29269790SLin.Ling@Sun.COM static void
29279790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
29287147Staylor {
29299816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
29309790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
29319790SLin.Ling@Sun.COM 
29329790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
29339790SLin.Ling@Sun.COM 		nvlist_t *label;
29349790SLin.Ling@Sun.COM 		uint64_t label_txg;
29359790SLin.Ling@Sun.COM 
29369790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
29379790SLin.Ling@Sun.COM 		    &label) != 0)
29389790SLin.Ling@Sun.COM 			return;
29399790SLin.Ling@Sun.COM 
29409790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
29419790SLin.Ling@Sun.COM 		    &label_txg) == 0);
29429790SLin.Ling@Sun.COM 
29439790SLin.Ling@Sun.COM 		/*
29449790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
29459790SLin.Ling@Sun.COM 		 */
29469790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
29479790SLin.Ling@Sun.COM 			*txg = label_txg;
29489790SLin.Ling@Sun.COM 			*avd = vd;
29497147Staylor 		}
29509790SLin.Ling@Sun.COM 		nvlist_free(label);
29517147Staylor 	}
29527147Staylor }
29537147Staylor 
29546423Sgw25295 /*
29556423Sgw25295  * Import a root pool.
29566423Sgw25295  *
29577147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
29587147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
29597147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
29606423Sgw25295  *
29616423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
29626423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
29636423Sgw25295  * e.g.
29646423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
29656423Sgw25295  */
29666423Sgw25295 int
29677147Staylor spa_import_rootpool(char *devpath, char *devid)
29686423Sgw25295 {
29699790SLin.Ling@Sun.COM 	spa_t *spa;
29709790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
29719790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
29729790SLin.Ling@Sun.COM 	uint64_t guid, txg;
29736423Sgw25295 	char *pname;
29746423Sgw25295 	int error;
29756423Sgw25295 
29766423Sgw25295 	/*
29779790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
29786423Sgw25295 	 */
297910822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
298010822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
298110822SJack.Meng@Sun.COM 	if (config == NULL) {
298210822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
298310822SJack.Meng@Sun.COM 			/* iscsi boot */
298410822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
298510822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
298610822SJack.Meng@Sun.COM 		}
298710822SJack.Meng@Sun.COM 	}
298810822SJack.Meng@Sun.COM #endif
298910822SJack.Meng@Sun.COM 	if (config == NULL) {
29909790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
29919790SLin.Ling@Sun.COM 		    devpath);
29929790SLin.Ling@Sun.COM 		return (EIO);
29939790SLin.Ling@Sun.COM 	}
29949790SLin.Ling@Sun.COM 
29959790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
29969790SLin.Ling@Sun.COM 	    &pname) == 0);
29979790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
29986423Sgw25295 
29999425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30009425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
30019425SEric.Schrock@Sun.COM 		/*
30029425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
30039425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
30049425SEric.Schrock@Sun.COM 		 */
30059425SEric.Schrock@Sun.COM 		spa_remove(spa);
30069425SEric.Schrock@Sun.COM 	}
30079425SEric.Schrock@Sun.COM 
300810921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
30099425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
301010100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
30119790SLin.Ling@Sun.COM 
30129790SLin.Ling@Sun.COM 	/*
30139790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
30149790SLin.Ling@Sun.COM 	 */
30159790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30169790SLin.Ling@Sun.COM 	    &nvtop) == 0);
30179790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30189790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
30199790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
30209790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30219790SLin.Ling@Sun.COM 	if (error) {
30229790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
30239790SLin.Ling@Sun.COM 		nvlist_free(config);
30249790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
30259790SLin.Ling@Sun.COM 		    pname);
30269790SLin.Ling@Sun.COM 		return (error);
30279790SLin.Ling@Sun.COM 	}
30289790SLin.Ling@Sun.COM 
30299790SLin.Ling@Sun.COM 	/*
30309790SLin.Ling@Sun.COM 	 * Get the boot vdev.
30319790SLin.Ling@Sun.COM 	 */
30329790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
30339790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
30349790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
30359790SLin.Ling@Sun.COM 		error = ENOENT;
30369790SLin.Ling@Sun.COM 		goto out;
30379790SLin.Ling@Sun.COM 	}
30389790SLin.Ling@Sun.COM 
30399790SLin.Ling@Sun.COM 	/*
30409790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
30419790SLin.Ling@Sun.COM 	 */
30429790SLin.Ling@Sun.COM 	avd = bvd;
30439790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
30449790SLin.Ling@Sun.COM 	if (avd != bvd) {
30459790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
30469790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
30479790SLin.Ling@Sun.COM 		error = EINVAL;
30489790SLin.Ling@Sun.COM 		goto out;
30499790SLin.Ling@Sun.COM 	}
30509790SLin.Ling@Sun.COM 
30519790SLin.Ling@Sun.COM 	/*
30529790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
30539790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
30549790SLin.Ling@Sun.COM 	 */
30559790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30569790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
30579790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
30589790SLin.Ling@Sun.COM 		    "try booting from '%s'",
30599790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
30609790SLin.Ling@Sun.COM 		error = EINVAL;
30619790SLin.Ling@Sun.COM 		goto out;
30629790SLin.Ling@Sun.COM 	}
30639790SLin.Ling@Sun.COM 
30649790SLin.Ling@Sun.COM 	error = 0;
30659946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
30669790SLin.Ling@Sun.COM out:
30679790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30689790SLin.Ling@Sun.COM 	vdev_free(rvd);
30699790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30709425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
30716423Sgw25295 
30729790SLin.Ling@Sun.COM 	nvlist_free(config);
30736423Sgw25295 	return (error);
30746423Sgw25295 }
30759790SLin.Ling@Sun.COM 
30766423Sgw25295 #endif
30776423Sgw25295 
30786423Sgw25295 /*
30799425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
30809425SEric.Schrock@Sun.COM  * boot.
30819425SEric.Schrock@Sun.COM  */
30829425SEric.Schrock@Sun.COM int
30839425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
30849425SEric.Schrock@Sun.COM {
30859425SEric.Schrock@Sun.COM 	spa_t *spa;
308610921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
30879425SEric.Schrock@Sun.COM 	char *altroot = NULL;
30889425SEric.Schrock@Sun.COM 
30899425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30909425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
30919425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
30929425SEric.Schrock@Sun.COM 		return (EEXIST);
30939425SEric.Schrock@Sun.COM 	}
30949425SEric.Schrock@Sun.COM 
30959425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
30969425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
309710921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
309810921STim.Haley@Sun.COM 
309910921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
310010921STim.Haley@Sun.COM 	spa->spa_load_max_txg = policy.zrp_txg;
31019425SEric.Schrock@Sun.COM 
310210100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
310310000SVictor.Latushkin@Sun.COM 
31049425SEric.Schrock@Sun.COM 	if (props != NULL)
31059425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31069425SEric.Schrock@Sun.COM 
31079425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
31089425SEric.Schrock@Sun.COM 
31099425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31109946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31119425SEric.Schrock@Sun.COM 
31129425SEric.Schrock@Sun.COM 	return (0);
31139425SEric.Schrock@Sun.COM }
31149425SEric.Schrock@Sun.COM 
31159425SEric.Schrock@Sun.COM /*
31166423Sgw25295  * Import a non-root pool into the system.
31176423Sgw25295  */
31186423Sgw25295 int
31196423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
31206423Sgw25295 {
31219425SEric.Schrock@Sun.COM 	spa_t *spa;
31229425SEric.Schrock@Sun.COM 	char *altroot = NULL;
312310921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
312410921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
31259425SEric.Schrock@Sun.COM 	int error;
31269425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
31279425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
31289425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
31299425SEric.Schrock@Sun.COM 
31309425SEric.Schrock@Sun.COM 	/*
31319425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
31329425SEric.Schrock@Sun.COM 	 */
31339425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
313411422SMark.Musante@Sun.COM 	if (spa_lookup(pool) != NULL) {
31359425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31369425SEric.Schrock@Sun.COM 		return (EEXIST);
31379425SEric.Schrock@Sun.COM 	}
31389425SEric.Schrock@Sun.COM 
313910921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
314010921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
314110921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
314210921STim.Haley@Sun.COM 
31439425SEric.Schrock@Sun.COM 	/*
31449425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
31459425SEric.Schrock@Sun.COM 	 */
31469425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31479425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
314810921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
31499425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
31509425SEric.Schrock@Sun.COM 
31519425SEric.Schrock@Sun.COM 	/*
31529630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
31539630SJeff.Bonwick@Sun.COM 	 */
31549630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
31559630SJeff.Bonwick@Sun.COM 
31569630SJeff.Bonwick@Sun.COM 	/*
31579425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
31589425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
31599425SEric.Schrock@Sun.COM 	 * doing an import.
31609425SEric.Schrock@Sun.COM 	 */
316110921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
316210921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
316310921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
316410921STim.Haley@Sun.COM 	    ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0));
316510921STim.Haley@Sun.COM 
316610921STim.Haley@Sun.COM 	/*
316710921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
316810921STim.Haley@Sun.COM 	 * back to caller
316910921STim.Haley@Sun.COM 	 */
317010921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
31719425SEric.Schrock@Sun.COM 
31729425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31739425SEric.Schrock@Sun.COM 	/*
31749425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
31759425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
31769425SEric.Schrock@Sun.COM 	 */
31779425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
31789425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
31799425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
31809425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
31819425SEric.Schrock@Sun.COM 	}
31829425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
31839425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
31849425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
31859425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
31869425SEric.Schrock@Sun.COM 	}
31879425SEric.Schrock@Sun.COM 
31889425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
31899425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
31909425SEric.Schrock@Sun.COM 	if (error == 0)
31919425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
31929425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
31939425SEric.Schrock@Sun.COM 	if (error == 0)
31949425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
31959425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
31969425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
31979425SEric.Schrock@Sun.COM 
31989425SEric.Schrock@Sun.COM 	if (props != NULL)
31999425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
32009425SEric.Schrock@Sun.COM 
32019425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
32029425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
32039425SEric.Schrock@Sun.COM 		spa_unload(spa);
32049425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
32059425SEric.Schrock@Sun.COM 		spa_remove(spa);
32069425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
32079425SEric.Schrock@Sun.COM 		return (error);
32089425SEric.Schrock@Sun.COM 	}
32099425SEric.Schrock@Sun.COM 
32109630SJeff.Bonwick@Sun.COM 	spa_async_resume(spa);
32119630SJeff.Bonwick@Sun.COM 
32129425SEric.Schrock@Sun.COM 	/*
32139425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
32149425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
32159425SEric.Schrock@Sun.COM 	 */
32169425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32179425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
32189425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
32199425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
32209425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
32219425SEric.Schrock@Sun.COM 		else
32229425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
32239425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32249425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
32259425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
32269425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32279425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32289425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32299425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
32309425SEric.Schrock@Sun.COM 	}
32319425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
32329425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
32339425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
32349425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
32359425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
32369425SEric.Schrock@Sun.COM 		else
32379425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
32389425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32399425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
32409425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
32419425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32429425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32439425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32449425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
32459425SEric.Schrock@Sun.COM 	}
32469425SEric.Schrock@Sun.COM 
324710672SEric.Schrock@Sun.COM 	/*
324810672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
324910672SEric.Schrock@Sun.COM 	 */
325010672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
325110672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
325210672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
325310672SEric.Schrock@Sun.COM 	}
325410672SEric.Schrock@Sun.COM 
32559425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
32569425SEric.Schrock@Sun.COM 		/*
32579425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
32589425SEric.Schrock@Sun.COM 		 */
325910100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32609425SEric.Schrock@Sun.COM 	}
32619425SEric.Schrock@Sun.COM 
32629816SGeorge.Wilson@Sun.COM 	/*
32639816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
32649816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
32659816SGeorge.Wilson@Sun.COM 	 */
32669816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
32679816SGeorge.Wilson@Sun.COM 
32689425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
32699946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
32709425SEric.Schrock@Sun.COM 
32719425SEric.Schrock@Sun.COM 	return (0);
32726643Seschrock }
32736643Seschrock 
3274789Sahrens nvlist_t *
3275789Sahrens spa_tryimport(nvlist_t *tryconfig)
3276789Sahrens {
3277789Sahrens 	nvlist_t *config = NULL;
3278789Sahrens 	char *poolname;
3279789Sahrens 	spa_t *spa;
3280789Sahrens 	uint64_t state;
32818680SLin.Ling@Sun.COM 	int error;
3282789Sahrens 
3283789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3284789Sahrens 		return (NULL);
3285789Sahrens 
3286789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3287789Sahrens 		return (NULL);
3288789Sahrens 
32891635Sbonwick 	/*
32901635Sbonwick 	 * Create and initialize the spa structure.
32911635Sbonwick 	 */
3292789Sahrens 	mutex_enter(&spa_namespace_lock);
329310921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
32948241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3295789Sahrens 
3296789Sahrens 	/*
32971635Sbonwick 	 * Pass off the heavy lifting to spa_load().
32981732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
32991732Sbonwick 	 * is actually the one to trust when doing an import.
3300789Sahrens 	 */
330111422SMark.Musante@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3302789Sahrens 
3303789Sahrens 	/*
3304789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3305789Sahrens 	 */
3306789Sahrens 	if (spa->spa_root_vdev != NULL) {
3307789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3308789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3309789Sahrens 		    poolname) == 0);
3310789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3311789Sahrens 		    state) == 0);
33123975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
33133975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
33142082Seschrock 
33152082Seschrock 		/*
33166423Sgw25295 		 * If the bootfs property exists on this pool then we
33176423Sgw25295 		 * copy it out so that external consumers can tell which
33186423Sgw25295 		 * pools are bootable.
33196423Sgw25295 		 */
33208680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
33216423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33226423Sgw25295 
33236423Sgw25295 			/*
33246423Sgw25295 			 * We have to play games with the name since the
33256423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
33266423Sgw25295 			 */
33277754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
33286423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
33296423Sgw25295 				char *cp;
33306423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33316423Sgw25295 
33326423Sgw25295 				cp = strchr(tmpname, '/');
33336423Sgw25295 				if (cp == NULL) {
33346423Sgw25295 					(void) strlcpy(dsname, tmpname,
33356423Sgw25295 					    MAXPATHLEN);
33366423Sgw25295 				} else {
33376423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
33386423Sgw25295 					    "%s/%s", poolname, ++cp);
33396423Sgw25295 				}
33406423Sgw25295 				VERIFY(nvlist_add_string(config,
33416423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
33426423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
33436423Sgw25295 			}
33446423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
33456423Sgw25295 		}
33466423Sgw25295 
33476423Sgw25295 		/*
33485450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
33492082Seschrock 		 */
33509425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
33512082Seschrock 		spa_add_spares(spa, config);
33525450Sbrendan 		spa_add_l2cache(spa, config);
33539425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3354789Sahrens 	}
3355789Sahrens 
3356789Sahrens 	spa_unload(spa);
3357789Sahrens 	spa_deactivate(spa);
3358789Sahrens 	spa_remove(spa);
3359789Sahrens 	mutex_exit(&spa_namespace_lock);
3360789Sahrens 
3361789Sahrens 	return (config);
3362789Sahrens }
3363789Sahrens 
3364789Sahrens /*
3365789Sahrens  * Pool export/destroy
3366789Sahrens  *
3367789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3368789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3369789Sahrens  * update the pool state and sync all the labels to disk, removing the
33708211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
33718211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3372789Sahrens  */
3373789Sahrens static int
33747214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
33758211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3376789Sahrens {
3377789Sahrens 	spa_t *spa;
3378789Sahrens 
33791775Sbillm 	if (oldconfig)
33801775Sbillm 		*oldconfig = NULL;
33811775Sbillm 
33828241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3383789Sahrens 		return (EROFS);
3384789Sahrens 
3385789Sahrens 	mutex_enter(&spa_namespace_lock);
3386789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3387789Sahrens 		mutex_exit(&spa_namespace_lock);
3388789Sahrens 		return (ENOENT);
3389789Sahrens 	}
3390789Sahrens 
3391789Sahrens 	/*
33921544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
33931544Seschrock 	 * reacquire the namespace lock, and see if we can export.
33941544Seschrock 	 */
33951544Seschrock 	spa_open_ref(spa, FTAG);
33961544Seschrock 	mutex_exit(&spa_namespace_lock);
33971544Seschrock 	spa_async_suspend(spa);
33981544Seschrock 	mutex_enter(&spa_namespace_lock);
33991544Seschrock 	spa_close(spa, FTAG);
34001544Seschrock 
34011544Seschrock 	/*
3402789Sahrens 	 * The pool will be in core if it's openable,
3403789Sahrens 	 * in which case we can modify its state.
3404789Sahrens 	 */
3405789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3406789Sahrens 		/*
3407789Sahrens 		 * Objsets may be open only because they're dirty, so we
3408789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3409789Sahrens 		 */
3410789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3411789Sahrens 
34121544Seschrock 		/*
34131544Seschrock 		 * A pool cannot be exported or destroyed if there are active
34141544Seschrock 		 * references.  If we are resetting a pool, allow references by
34151544Seschrock 		 * fault injection handlers.
34161544Seschrock 		 */
34171544Seschrock 		if (!spa_refcount_zero(spa) ||
34181544Seschrock 		    (spa->spa_inject_ref != 0 &&
34191544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
34201544Seschrock 			spa_async_resume(spa);
3421789Sahrens 			mutex_exit(&spa_namespace_lock);
3422789Sahrens 			return (EBUSY);
3423789Sahrens 		}
3424789Sahrens 
3425789Sahrens 		/*
34267214Slling 		 * A pool cannot be exported if it has an active shared spare.
34277214Slling 		 * This is to prevent other pools stealing the active spare
34287214Slling 		 * from an exported pool. At user's own will, such pool can
34297214Slling 		 * be forcedly exported.
34307214Slling 		 */
34317214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
34327214Slling 		    spa_has_active_shared_spare(spa)) {
34337214Slling 			spa_async_resume(spa);
34347214Slling 			mutex_exit(&spa_namespace_lock);
34357214Slling 			return (EXDEV);
34367214Slling 		}
34377214Slling 
34387214Slling 		/*
3439789Sahrens 		 * We want this to be reflected on every label,
3440789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3441789Sahrens 		 * final sync that pushes these changes out.
3442789Sahrens 		 */
34438211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
34447754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34451544Seschrock 			spa->spa_state = new_state;
34461635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
34471544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
34487754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
34491544Seschrock 		}
3450789Sahrens 	}
3451789Sahrens 
34524451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
34534451Seschrock 
3454789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3455789Sahrens 		spa_unload(spa);
3456789Sahrens 		spa_deactivate(spa);
3457789Sahrens 	}
3458789Sahrens 
34591775Sbillm 	if (oldconfig && spa->spa_config)
34601775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
34611775Sbillm 
34621544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
34638211SGeorge.Wilson@Sun.COM 		if (!hardforce)
34648211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
34651544Seschrock 		spa_remove(spa);
34661544Seschrock 	}
3467789Sahrens 	mutex_exit(&spa_namespace_lock);
3468789Sahrens 
3469789Sahrens 	return (0);
3470789Sahrens }
3471789Sahrens 
3472789Sahrens /*
3473789Sahrens  * Destroy a storage pool.
3474789Sahrens  */
3475789Sahrens int
3476789Sahrens spa_destroy(char *pool)
3477789Sahrens {
34788211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
34798211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3480789Sahrens }
3481789Sahrens 
3482789Sahrens /*
3483789Sahrens  * Export a storage pool.
3484789Sahrens  */
3485789Sahrens int
34868211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
34878211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3488789Sahrens {
34898211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
34908211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3491789Sahrens }
3492789Sahrens 
3493789Sahrens /*
34941544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
34951544Seschrock  * from the namespace in any way.
34961544Seschrock  */
34971544Seschrock int
34981544Seschrock spa_reset(char *pool)
34991544Seschrock {
35007214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
35018211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
35021544Seschrock }
35031544Seschrock 
35041544Seschrock /*
3505789Sahrens  * ==========================================================================
3506789Sahrens  * Device manipulation
3507789Sahrens  * ==========================================================================
3508789Sahrens  */
3509789Sahrens 
3510789Sahrens /*
35114527Sperrin  * Add a device to a storage pool.
3512789Sahrens  */
3513789Sahrens int
3514789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3515789Sahrens {
351610594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
35178241SJeff.Bonwick@Sun.COM 	int error;
3518789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
35191585Sbonwick 	vdev_t *vd, *tvd;
35205450Sbrendan 	nvlist_t **spares, **l2cache;
35215450Sbrendan 	uint_t nspares, nl2cache;
3522789Sahrens 
3523789Sahrens 	txg = spa_vdev_enter(spa);
3524789Sahrens 
35252082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
35262082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
35272082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
35282082Seschrock 
35297754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3530789Sahrens 
35315450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
35325450Sbrendan 	    &nspares) != 0)
35332082Seschrock 		nspares = 0;
35342082Seschrock 
35355450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
35365450Sbrendan 	    &nl2cache) != 0)
35375450Sbrendan 		nl2cache = 0;
35385450Sbrendan 
35397754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
35402082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
35417754SJeff.Bonwick@Sun.COM 
35427754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
35437754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
35447754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
35452082Seschrock 
35463377Seschrock 	/*
35475450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
35485450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
35493377Seschrock 	 */
35507754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
35513377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
35523377Seschrock 
35533377Seschrock 	/*
35543377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
35553377Seschrock 	 */
35568241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
355710594SGeorge.Wilson@Sun.COM 
355810594SGeorge.Wilson@Sun.COM 		/*
355910594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
356010594SGeorge.Wilson@Sun.COM 		 */
356110594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
356210594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
356310594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
356410594SGeorge.Wilson@Sun.COM 				break;
356510594SGeorge.Wilson@Sun.COM 			}
356610594SGeorge.Wilson@Sun.COM 		}
35673377Seschrock 		tvd = vd->vdev_child[c];
35683377Seschrock 		vdev_remove_child(vd, tvd);
356910594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
35703377Seschrock 		vdev_add_child(rvd, tvd);
35713377Seschrock 		vdev_config_dirty(tvd);
35723377Seschrock 	}
35733377Seschrock 
35742082Seschrock 	if (nspares != 0) {
35755450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
35765450Sbrendan 		    ZPOOL_CONFIG_SPARES);
35772082Seschrock 		spa_load_spares(spa);
35785450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
35795450Sbrendan 	}
35805450Sbrendan 
35815450Sbrendan 	if (nl2cache != 0) {
35825450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
35835450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
35845450Sbrendan 		spa_load_l2cache(spa);
35855450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3586789Sahrens 	}
3587789Sahrens 
3588789Sahrens 	/*
35891585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
35901585Sbonwick 	 * If other threads start allocating from these vdevs before we
35911585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
35921585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
35931585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
35941585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
35951635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
35961585Sbonwick 	 *
35971585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
35981585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
35991585Sbonwick 	 * steps will be completed the next time we load the pool.
3600789Sahrens 	 */
36011635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
36021585Sbonwick 
36031635Sbonwick 	mutex_enter(&spa_namespace_lock);
36041635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36051635Sbonwick 	mutex_exit(&spa_namespace_lock);
3606789Sahrens 
36071635Sbonwick 	return (0);
3608789Sahrens }
3609789Sahrens 
3610789Sahrens /*
3611789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3612789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3613789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3614789Sahrens  *
3615789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3616789Sahrens  * existing device; in this case the two devices are made into their own
36174451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3618789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3619789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3620789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3621789Sahrens  * is automatically detached.
3622789Sahrens  */
3623789Sahrens int
36241544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3625789Sahrens {
3626789Sahrens 	uint64_t txg, open_txg;
3627789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3628789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
36292082Seschrock 	vdev_ops_t *pvops;
36307313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
36317313SEric.Kustarz@Sun.COM 	int newvd_isspare;
36327313SEric.Kustarz@Sun.COM 	int error;
3633789Sahrens 
3634789Sahrens 	txg = spa_vdev_enter(spa);
3635789Sahrens 
36366643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3637789Sahrens 
3638789Sahrens 	if (oldvd == NULL)
3639789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3640789Sahrens 
36411585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
36421585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
36431585Sbonwick 
3644789Sahrens 	pvd = oldvd->vdev_parent;
3645789Sahrens 
36462082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
36474451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
36484451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
36494451Seschrock 
36504451Seschrock 	if (newrootvd->vdev_children != 1)
3651789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3652789Sahrens 
3653789Sahrens 	newvd = newrootvd->vdev_child[0];
3654789Sahrens 
3655789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3656789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3657789Sahrens 
36582082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3659789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3660789Sahrens 
36614527Sperrin 	/*
36624527Sperrin 	 * Spares can't replace logs
36634527Sperrin 	 */
36647326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
36654527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36664527Sperrin 
36672082Seschrock 	if (!replacing) {
36682082Seschrock 		/*
36692082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
36702082Seschrock 		 * vdev.
36712082Seschrock 		 */
36722082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
36732082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
36742082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36752082Seschrock 
36762082Seschrock 		pvops = &vdev_mirror_ops;
36772082Seschrock 	} else {
36782082Seschrock 		/*
36792082Seschrock 		 * Active hot spares can only be replaced by inactive hot
36802082Seschrock 		 * spares.
36812082Seschrock 		 */
36822082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
36832082Seschrock 		    pvd->vdev_child[1] == oldvd &&
36842082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
36852082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36862082Seschrock 
36872082Seschrock 		/*
36882082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
36892082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
36903377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
36913377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
36923377Seschrock 		 * the same (spare replaces spare, non-spare replaces
36933377Seschrock 		 * non-spare).
36942082Seschrock 		 */
36952082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
36962082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
36973377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
36983377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
36993377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37002082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
37012082Seschrock 		    newvd->vdev_isspare)
37022082Seschrock 			pvops = &vdev_spare_ops;
37032082Seschrock 		else
37042082Seschrock 			pvops = &vdev_replacing_ops;
37052082Seschrock 	}
37062082Seschrock 
37071175Slling 	/*
37089816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
37091175Slling 	 */
37109816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3711789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3712789Sahrens 
37131732Sbonwick 	/*
37141732Sbonwick 	 * The new device cannot have a higher alignment requirement
37151732Sbonwick 	 * than the top-level vdev.
37161732Sbonwick 	 */
37171732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3718789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3719789Sahrens 
3720789Sahrens 	/*
3721789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3722789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3723789Sahrens 	 */
3724789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3725789Sahrens 		spa_strfree(oldvd->vdev_path);
3726789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3727789Sahrens 		    KM_SLEEP);
3728789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3729789Sahrens 		    newvd->vdev_path, "old");
3730789Sahrens 		if (oldvd->vdev_devid != NULL) {
3731789Sahrens 			spa_strfree(oldvd->vdev_devid);
3732789Sahrens 			oldvd->vdev_devid = NULL;
3733789Sahrens 		}
3734789Sahrens 	}
3735789Sahrens 
3736789Sahrens 	/*
37372082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
37382082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3739789Sahrens 	 */
3740789Sahrens 	if (pvd->vdev_ops != pvops)
3741789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3742789Sahrens 
3743789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3744789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3745789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3746789Sahrens 
3747789Sahrens 	/*
3748789Sahrens 	 * Extract the new device from its root and add it to pvd.
3749789Sahrens 	 */
3750789Sahrens 	vdev_remove_child(newrootvd, newvd);
3751789Sahrens 	newvd->vdev_id = pvd->vdev_children;
375210594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3753789Sahrens 	vdev_add_child(pvd, newvd);
3754789Sahrens 
3755789Sahrens 	tvd = newvd->vdev_top;
3756789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3757789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3758789Sahrens 
3759789Sahrens 	vdev_config_dirty(tvd);
3760789Sahrens 
3761789Sahrens 	/*
3762789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3763789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3764789Sahrens 	 */
3765789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3766789Sahrens 
37678241SJeff.Bonwick@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING,
37688241SJeff.Bonwick@Sun.COM 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3769789Sahrens 
37709425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
37713377Seschrock 		spa_spare_activate(newvd);
37729425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
37739425SEric.Schrock@Sun.COM 	}
37749425SEric.Schrock@Sun.COM 
37757754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
37767754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
37777313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
37781544Seschrock 
3779789Sahrens 	/*
3780789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3781789Sahrens 	 */
37821732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3783789Sahrens 
3784789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3785789Sahrens 
37869946SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
37879946SMark.Musante@Sun.COM 	    CRED(),  "%s vdev=%s %s vdev=%s",
37889946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
37899946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
37909946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
37917313SEric.Kustarz@Sun.COM 
37927313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
37937313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
37947313SEric.Kustarz@Sun.COM 
3795789Sahrens 	/*
37967046Sahrens 	 * Kick off a resilver to update newvd.
3797789Sahrens 	 */
37987046Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3799789Sahrens 
3800789Sahrens 	return (0);
3801789Sahrens }
3802789Sahrens 
3803789Sahrens /*
3804789Sahrens  * Detach a device from a mirror or replacing vdev.
3805789Sahrens  * If 'replace_done' is specified, only detach if the parent
3806789Sahrens  * is a replacing vdev.
3807789Sahrens  */
3808789Sahrens int
38098241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3810789Sahrens {
3811789Sahrens 	uint64_t txg;
38128241SJeff.Bonwick@Sun.COM 	int error;
3813789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3814789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
38152082Seschrock 	boolean_t unspare = B_FALSE;
38162082Seschrock 	uint64_t unspare_guid;
38176673Seschrock 	size_t len;
381811422SMark.Musante@Sun.COM 	char *vdpath;
3819789Sahrens 
3820789Sahrens 	txg = spa_vdev_enter(spa);
3821789Sahrens 
38226643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3823789Sahrens 
3824789Sahrens 	if (vd == NULL)
3825789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3826789Sahrens 
38271585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
38281585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38291585Sbonwick 
3830789Sahrens 	pvd = vd->vdev_parent;
3831789Sahrens 
3832789Sahrens 	/*
38338241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
38348241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
38358241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
38368241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
38378241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
38388241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
38398241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
38408241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
38418241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
38428241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
38438241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
38448241SJeff.Bonwick@Sun.COM 	 */
38458241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
38468241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
38478241SJeff.Bonwick@Sun.COM 
38488241SJeff.Bonwick@Sun.COM 	/*
3849789Sahrens 	 * If replace_done is specified, only remove this device if it's
38502082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
38512082Seschrock 	 * disk can be removed.
3852789Sahrens 	 */
38532082Seschrock 	if (replace_done) {
38542082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
38552082Seschrock 			if (vd->vdev_id != 0)
38562082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38572082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
38582082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38592082Seschrock 		}
38602082Seschrock 	}
38612082Seschrock 
38622082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
38634577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3864789Sahrens 
3865789Sahrens 	/*
38662082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3867789Sahrens 	 */
3868789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
38692082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
38702082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3871789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3872789Sahrens 
3873789Sahrens 	/*
38748241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
38758241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3876789Sahrens 	 */
38778241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3878789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3879789Sahrens 
38808241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
38818241SJeff.Bonwick@Sun.COM 
3882789Sahrens 	/*
38836673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
38846673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
38856673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
38866673Seschrock 	 */
38876673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
38886673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
38896673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
38906673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
38916673Seschrock 		cvd = pvd->vdev_child[0];
38926673Seschrock 		len = strlen(vd->vdev_path);
38936673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
38946673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
38956673Seschrock 			spa_strfree(cvd->vdev_path);
38966673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
38976673Seschrock 		}
38986673Seschrock 	}
38996673Seschrock 
39006673Seschrock 	/*
39012082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
39022082Seschrock 	 * that the spare should become a real disk, and be removed from the
39032082Seschrock 	 * active spare list for the pool.
39042082Seschrock 	 */
39052082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
39068241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
39072082Seschrock 		unspare = B_TRUE;
39082082Seschrock 
39092082Seschrock 	/*
3910789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3911789Sahrens 	 * This must be done after all other error cases are handled,
3912789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3913789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3914789Sahrens 	 * it may be that the unwritability of the disk is the reason
3915789Sahrens 	 * it's being detached!
3916789Sahrens 	 */
39173377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3918789Sahrens 
3919789Sahrens 	/*
3920789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3921789Sahrens 	 */
3922789Sahrens 	vdev_remove_child(pvd, vd);
3923789Sahrens 	vdev_compact_children(pvd);
3924789Sahrens 
3925789Sahrens 	/*
3926789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3927789Sahrens 	 */
3928789Sahrens 	cvd = pvd->vdev_child[0];
3929789Sahrens 
3930789Sahrens 	/*
39312082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
39328241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
39338241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
39348241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
39358241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
39368241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
39378241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
39382082Seschrock 	 */
39392082Seschrock 	if (unspare) {
39402082Seschrock 		ASSERT(cvd->vdev_isspare);
39413377Seschrock 		spa_spare_remove(cvd);
39422082Seschrock 		unspare_guid = cvd->vdev_guid;
39438241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
39442082Seschrock 	}
39452082Seschrock 
39462082Seschrock 	/*
3947789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3948789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3949789Sahrens 	 */
3950789Sahrens 	if (pvd->vdev_children == 1)
3951789Sahrens 		vdev_remove_parent(cvd);
3952789Sahrens 
3953789Sahrens 	/*
3954789Sahrens 	 * We don't set tvd until now because the parent we just removed
3955789Sahrens 	 * may have been the previous top-level vdev.
3956789Sahrens 	 */
3957789Sahrens 	tvd = cvd->vdev_top;
3958789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3959789Sahrens 
3960789Sahrens 	/*
39613377Seschrock 	 * Reevaluate the parent vdev state.
3962789Sahrens 	 */
39634451Seschrock 	vdev_propagate_state(cvd);
3964789Sahrens 
3965789Sahrens 	/*
39669816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
39679816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
39689816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
39699816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
39709816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3971789Sahrens 	 */
39729816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
39739816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
39749816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
39759816SGeorge.Wilson@Sun.COM 	}
3976789Sahrens 
3977789Sahrens 	vdev_config_dirty(tvd);
3978789Sahrens 
3979789Sahrens 	/*
39803377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
39813377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
39823377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
39833377Seschrock 	 * prevent vd from being accessed after it's freed.
3984789Sahrens 	 */
398511422SMark.Musante@Sun.COM 	vdpath = spa_strdup(vd->vdev_path);
39868241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
3987789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
39881732Sbonwick 	vd->vdev_detached = B_TRUE;
39891732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3990789Sahrens 
39914451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
39924451Seschrock 
39932082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
39942082Seschrock 
399511422SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_VDEV_DETACH, spa, NULL, CRED(),
399611422SMark.Musante@Sun.COM 	    "vdev=%s", vdpath);
399711422SMark.Musante@Sun.COM 	spa_strfree(vdpath);
399811422SMark.Musante@Sun.COM 
39992082Seschrock 	/*
40003377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
40013377Seschrock 	 * then we want to go through and remove the device from the hot spare
40023377Seschrock 	 * list of every other pool.
40032082Seschrock 	 */
40042082Seschrock 	if (unspare) {
40058241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
40062082Seschrock 		spa = NULL;
40072082Seschrock 		mutex_enter(&spa_namespace_lock);
40082082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
40092082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
40102082Seschrock 				continue;
40118241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
40128241SJeff.Bonwick@Sun.COM 				continue;
40137793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
40147793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
40152082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
40167793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
40177793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
40182082Seschrock 		}
40192082Seschrock 		mutex_exit(&spa_namespace_lock);
40202082Seschrock 	}
40212082Seschrock 
40222082Seschrock 	return (error);
40232082Seschrock }
40242082Seschrock 
402511422SMark.Musante@Sun.COM /*
402611422SMark.Musante@Sun.COM  * Split a set of devices from their mirrors, and create a new pool from them.
402711422SMark.Musante@Sun.COM  */
402811422SMark.Musante@Sun.COM int
402911422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
403011422SMark.Musante@Sun.COM     nvlist_t *props, boolean_t exp)
403111422SMark.Musante@Sun.COM {
403211422SMark.Musante@Sun.COM 	int error = 0;
403311422SMark.Musante@Sun.COM 	uint64_t txg, *glist;
403411422SMark.Musante@Sun.COM 	spa_t *newspa;
403511422SMark.Musante@Sun.COM 	uint_t c, children, lastlog;
403611422SMark.Musante@Sun.COM 	nvlist_t **child, *nvl, *tmp;
403711422SMark.Musante@Sun.COM 	dmu_tx_t *tx;
403811422SMark.Musante@Sun.COM 	char *altroot = NULL;
403911422SMark.Musante@Sun.COM 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
404011422SMark.Musante@Sun.COM 	boolean_t activate_slog;
404111422SMark.Musante@Sun.COM 
404211422SMark.Musante@Sun.COM 	if (!spa_writeable(spa))
404311422SMark.Musante@Sun.COM 		return (EROFS);
404411422SMark.Musante@Sun.COM 
404511422SMark.Musante@Sun.COM 	txg = spa_vdev_enter(spa);
404611422SMark.Musante@Sun.COM 
404711422SMark.Musante@Sun.COM 	/* clear the log and flush everything up to now */
404811422SMark.Musante@Sun.COM 	activate_slog = spa_passivate_log(spa);
404911422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
405011422SMark.Musante@Sun.COM 	error = spa_offline_log(spa);
405111422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
405211422SMark.Musante@Sun.COM 
405311422SMark.Musante@Sun.COM 	if (activate_slog)
405411422SMark.Musante@Sun.COM 		spa_activate_log(spa);
405511422SMark.Musante@Sun.COM 
405611422SMark.Musante@Sun.COM 	if (error != 0)
405711422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
405811422SMark.Musante@Sun.COM 
405911422SMark.Musante@Sun.COM 	/* check new spa name before going any further */
406011422SMark.Musante@Sun.COM 	if (spa_lookup(newname) != NULL)
406111422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
406211422SMark.Musante@Sun.COM 
406311422SMark.Musante@Sun.COM 	/*
406411422SMark.Musante@Sun.COM 	 * scan through all the children to ensure they're all mirrors
406511422SMark.Musante@Sun.COM 	 */
406611422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
406711422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
406811422SMark.Musante@Sun.COM 	    &children) != 0)
406911422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
407011422SMark.Musante@Sun.COM 
407111422SMark.Musante@Sun.COM 	/* first, check to ensure we've got the right child count */
407211422SMark.Musante@Sun.COM 	rvd = spa->spa_root_vdev;
407311422SMark.Musante@Sun.COM 	lastlog = 0;
407411422SMark.Musante@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
407511422SMark.Musante@Sun.COM 		vdev_t *vd = rvd->vdev_child[c];
407611422SMark.Musante@Sun.COM 
407711422SMark.Musante@Sun.COM 		/* don't count the holes & logs as children */
407811422SMark.Musante@Sun.COM 		if (vd->vdev_islog || vd->vdev_ishole) {
407911422SMark.Musante@Sun.COM 			if (lastlog == 0)
408011422SMark.Musante@Sun.COM 				lastlog = c;
408111422SMark.Musante@Sun.COM 			continue;
408211422SMark.Musante@Sun.COM 		}
408311422SMark.Musante@Sun.COM 
408411422SMark.Musante@Sun.COM 		lastlog = 0;
408511422SMark.Musante@Sun.COM 	}
408611422SMark.Musante@Sun.COM 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
408711422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
408811422SMark.Musante@Sun.COM 
408911422SMark.Musante@Sun.COM 	/* next, ensure no spare or cache devices are part of the split */
409011422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
409111422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
409211422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
409311422SMark.Musante@Sun.COM 
409411422SMark.Musante@Sun.COM 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
409511422SMark.Musante@Sun.COM 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
409611422SMark.Musante@Sun.COM 
409711422SMark.Musante@Sun.COM 	/* then, loop over each vdev and validate it */
409811422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
409911422SMark.Musante@Sun.COM 		uint64_t is_hole = 0;
410011422SMark.Musante@Sun.COM 
410111422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
410211422SMark.Musante@Sun.COM 		    &is_hole);
410311422SMark.Musante@Sun.COM 
410411422SMark.Musante@Sun.COM 		if (is_hole != 0) {
410511422SMark.Musante@Sun.COM 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
410611422SMark.Musante@Sun.COM 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
410711422SMark.Musante@Sun.COM 				continue;
410811422SMark.Musante@Sun.COM 			} else {
410911422SMark.Musante@Sun.COM 				error = EINVAL;
411011422SMark.Musante@Sun.COM 				break;
411111422SMark.Musante@Sun.COM 			}
411211422SMark.Musante@Sun.COM 		}
411311422SMark.Musante@Sun.COM 
411411422SMark.Musante@Sun.COM 		/* which disk is going to be split? */
411511422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
411611422SMark.Musante@Sun.COM 		    &glist[c]) != 0) {
411711422SMark.Musante@Sun.COM 			error = EINVAL;
411811422SMark.Musante@Sun.COM 			break;
411911422SMark.Musante@Sun.COM 		}
412011422SMark.Musante@Sun.COM 
412111422SMark.Musante@Sun.COM 		/* look it up in the spa */
412211422SMark.Musante@Sun.COM 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
412311422SMark.Musante@Sun.COM 		if (vml[c] == NULL) {
412411422SMark.Musante@Sun.COM 			error = ENODEV;
412511422SMark.Musante@Sun.COM 			break;
412611422SMark.Musante@Sun.COM 		}
412711422SMark.Musante@Sun.COM 
412811422SMark.Musante@Sun.COM 		/* make sure there's nothing stopping the split */
412911422SMark.Musante@Sun.COM 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
413011422SMark.Musante@Sun.COM 		    vml[c]->vdev_islog ||
413111422SMark.Musante@Sun.COM 		    vml[c]->vdev_ishole ||
413211422SMark.Musante@Sun.COM 		    vml[c]->vdev_isspare ||
413311422SMark.Musante@Sun.COM 		    vml[c]->vdev_isl2cache ||
413411422SMark.Musante@Sun.COM 		    !vdev_writeable(vml[c]) ||
413511497SMark.Musante@Sun.COM 		    vml[c]->vdev_children != 0 ||
413611422SMark.Musante@Sun.COM 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
413711422SMark.Musante@Sun.COM 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
413811422SMark.Musante@Sun.COM 			error = EINVAL;
413911422SMark.Musante@Sun.COM 			break;
414011422SMark.Musante@Sun.COM 		}
414111422SMark.Musante@Sun.COM 
414211422SMark.Musante@Sun.COM 		if (vdev_dtl_required(vml[c])) {
414311422SMark.Musante@Sun.COM 			error = EBUSY;
414411422SMark.Musante@Sun.COM 			break;
414511422SMark.Musante@Sun.COM 		}
414611422SMark.Musante@Sun.COM 
414711422SMark.Musante@Sun.COM 		/* we need certain info from the top level */
414811422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
414911422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_array) == 0);
415011422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
415111422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
415211422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
415311422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_asize) == 0);
415411422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
415511422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ashift) == 0);
415611422SMark.Musante@Sun.COM 	}
415711422SMark.Musante@Sun.COM 
415811422SMark.Musante@Sun.COM 	if (error != 0) {
415911422SMark.Musante@Sun.COM 		kmem_free(vml, children * sizeof (vdev_t *));
416011422SMark.Musante@Sun.COM 		kmem_free(glist, children * sizeof (uint64_t));
416111422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
416211422SMark.Musante@Sun.COM 	}
416311422SMark.Musante@Sun.COM 
416411422SMark.Musante@Sun.COM 	/* stop writers from using the disks */
416511422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
416611422SMark.Musante@Sun.COM 		if (vml[c] != NULL)
416711422SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_TRUE;
416811422SMark.Musante@Sun.COM 	}
416911422SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
417011422SMark.Musante@Sun.COM 
417111422SMark.Musante@Sun.COM 	/*
417211422SMark.Musante@Sun.COM 	 * Temporarily record the splitting vdevs in the spa config.  This
417311422SMark.Musante@Sun.COM 	 * will disappear once the config is regenerated.
417411422SMark.Musante@Sun.COM 	 */
417511422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
417611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
417711422SMark.Musante@Sun.COM 	    glist, children) == 0);
417811422SMark.Musante@Sun.COM 	kmem_free(glist, children * sizeof (uint64_t));
417911422SMark.Musante@Sun.COM 
418011422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
418111422SMark.Musante@Sun.COM 	    nvl) == 0);
418211422SMark.Musante@Sun.COM 	spa->spa_config_splitting = nvl;
418311422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
418411422SMark.Musante@Sun.COM 
418511422SMark.Musante@Sun.COM 	/* configure and create the new pool */
418611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
418711422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
418811422SMark.Musante@Sun.COM 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
418911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
419011422SMark.Musante@Sun.COM 	    spa_version(spa)) == 0);
419111422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
419211422SMark.Musante@Sun.COM 	    spa->spa_config_txg) == 0);
419311422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
419411422SMark.Musante@Sun.COM 	    spa_generate_guid(NULL)) == 0);
419511422SMark.Musante@Sun.COM 	(void) nvlist_lookup_string(props,
419611422SMark.Musante@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
419711422SMark.Musante@Sun.COM 
419811497SMark.Musante@Sun.COM 	/* add the new pool to the namespace */
419911422SMark.Musante@Sun.COM 	newspa = spa_add(newname, config, altroot);
420011422SMark.Musante@Sun.COM 	newspa->spa_config_txg = spa->spa_config_txg;
420111422SMark.Musante@Sun.COM 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
420211422SMark.Musante@Sun.COM 
420311422SMark.Musante@Sun.COM 	/* release the spa config lock, retaining the namespace lock */
420411422SMark.Musante@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
420511422SMark.Musante@Sun.COM 
420611422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
420711422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 1);
420811422SMark.Musante@Sun.COM 
420911422SMark.Musante@Sun.COM 	spa_activate(newspa, spa_mode_global);
421011422SMark.Musante@Sun.COM 	spa_async_suspend(newspa);
421111422SMark.Musante@Sun.COM 
421211422SMark.Musante@Sun.COM 	/* create the new pool from the disks of the original pool */
421311422SMark.Musante@Sun.COM 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
421411422SMark.Musante@Sun.COM 	if (error)
421511422SMark.Musante@Sun.COM 		goto out;
421611422SMark.Musante@Sun.COM 
421711422SMark.Musante@Sun.COM 	/* if that worked, generate a real config for the new pool */
421811422SMark.Musante@Sun.COM 	if (newspa->spa_root_vdev != NULL) {
421911422SMark.Musante@Sun.COM 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
422011422SMark.Musante@Sun.COM 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
422111422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
422211422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
422311422SMark.Musante@Sun.COM 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
422411422SMark.Musante@Sun.COM 		    B_TRUE));
422511422SMark.Musante@Sun.COM 	}
422611422SMark.Musante@Sun.COM 
422711422SMark.Musante@Sun.COM 	/* set the props */
422811422SMark.Musante@Sun.COM 	if (props != NULL) {
422911422SMark.Musante@Sun.COM 		spa_configfile_set(newspa, props, B_FALSE);
423011422SMark.Musante@Sun.COM 		error = spa_prop_set(newspa, props);
423111422SMark.Musante@Sun.COM 		if (error)
423211422SMark.Musante@Sun.COM 			goto out;
423311422SMark.Musante@Sun.COM 	}
423411422SMark.Musante@Sun.COM 
423511422SMark.Musante@Sun.COM 	/* flush everything */
423611422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(newspa);
423711422SMark.Musante@Sun.COM 	vdev_config_dirty(newspa->spa_root_vdev);
423811422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
423911422SMark.Musante@Sun.COM 
424011422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
424111422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 2);
424211422SMark.Musante@Sun.COM 
424311422SMark.Musante@Sun.COM 	spa_async_resume(newspa);
424411422SMark.Musante@Sun.COM 
424511422SMark.Musante@Sun.COM 	/* finally, update the original pool's config */
424611422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
424711422SMark.Musante@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
424811422SMark.Musante@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
424911422SMark.Musante@Sun.COM 	if (error != 0)
425011422SMark.Musante@Sun.COM 		dmu_tx_abort(tx);
425111422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
425211422SMark.Musante@Sun.COM 		if (vml[c] != NULL) {
425311422SMark.Musante@Sun.COM 			vdev_split(vml[c]);
425411422SMark.Musante@Sun.COM 			if (error == 0)
425511422SMark.Musante@Sun.COM 				spa_history_internal_log(LOG_POOL_VDEV_DETACH,
425611422SMark.Musante@Sun.COM 				    spa, tx, CRED(), "vdev=%s",
425711422SMark.Musante@Sun.COM 				    vml[c]->vdev_path);
425811422SMark.Musante@Sun.COM 			vdev_free(vml[c]);
425911422SMark.Musante@Sun.COM 		}
426011422SMark.Musante@Sun.COM 	}
426111422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
426211422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
426311422SMark.Musante@Sun.COM 	nvlist_free(nvl);
426411422SMark.Musante@Sun.COM 	if (error == 0)
426511422SMark.Musante@Sun.COM 		dmu_tx_commit(tx);
426611422SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, 0);
426711422SMark.Musante@Sun.COM 
426811422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
426911422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 3);
427011422SMark.Musante@Sun.COM 
427111422SMark.Musante@Sun.COM 	/* split is complete; log a history record */
427211422SMark.Musante@Sun.COM 	spa_history_internal_log(LOG_POOL_SPLIT, newspa, NULL, CRED(),
427311422SMark.Musante@Sun.COM 	    "split new pool %s from pool %s", newname, spa_name(spa));
427411422SMark.Musante@Sun.COM 
427511422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
427611422SMark.Musante@Sun.COM 
427711422SMark.Musante@Sun.COM 	/* if we're not going to mount the filesystems in userland, export */
427811422SMark.Musante@Sun.COM 	if (exp)
427911422SMark.Musante@Sun.COM 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
428011422SMark.Musante@Sun.COM 		    B_FALSE, B_FALSE);
428111422SMark.Musante@Sun.COM 
428211422SMark.Musante@Sun.COM 	return (error);
428311422SMark.Musante@Sun.COM 
428411422SMark.Musante@Sun.COM out:
428511422SMark.Musante@Sun.COM 	spa_unload(newspa);
428611422SMark.Musante@Sun.COM 	spa_deactivate(newspa);
428711422SMark.Musante@Sun.COM 	spa_remove(newspa);
428811422SMark.Musante@Sun.COM 
428911422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
429011422SMark.Musante@Sun.COM 	nvlist_free(spa->spa_config_splitting);
429111422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
429211497SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, error);
429311422SMark.Musante@Sun.COM 
429411422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
429511422SMark.Musante@Sun.COM 	return (error);
429611422SMark.Musante@Sun.COM }
429711422SMark.Musante@Sun.COM 
42987754SJeff.Bonwick@Sun.COM static nvlist_t *
42997754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
43002082Seschrock {
43017754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
43027754SJeff.Bonwick@Sun.COM 		uint64_t guid;
43037754SJeff.Bonwick@Sun.COM 
43047754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
43057754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
43067754SJeff.Bonwick@Sun.COM 
43077754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
43087754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
43092082Seschrock 	}
43102082Seschrock 
43117754SJeff.Bonwick@Sun.COM 	return (NULL);
43125450Sbrendan }
43135450Sbrendan 
43147754SJeff.Bonwick@Sun.COM static void
43157754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
43167754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
43175450Sbrendan {
43187754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
43197754SJeff.Bonwick@Sun.COM 
43207754SJeff.Bonwick@Sun.COM 	if (count > 1)
43217754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
43227754SJeff.Bonwick@Sun.COM 
43237754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
43247754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
43257754SJeff.Bonwick@Sun.COM 			continue;
43267754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
43275450Sbrendan 	}
43285450Sbrendan 
43297754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
43307754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
43317754SJeff.Bonwick@Sun.COM 
43327754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
43337754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
43347754SJeff.Bonwick@Sun.COM 
43357754SJeff.Bonwick@Sun.COM 	if (count > 1)
43367754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
43375450Sbrendan }
43385450Sbrendan 
43395450Sbrendan /*
434010594SGeorge.Wilson@Sun.COM  * Removing a device from the vdev namespace requires several steps
434110594SGeorge.Wilson@Sun.COM  * and can take a significant amount of time.  As a result we use
434210594SGeorge.Wilson@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
434310594SGeorge.Wilson@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
434410594SGeorge.Wilson@Sun.COM  * lock.  During each step the configuration is synced out.
434510594SGeorge.Wilson@Sun.COM  */
434610594SGeorge.Wilson@Sun.COM 
434710594SGeorge.Wilson@Sun.COM /*
434810594SGeorge.Wilson@Sun.COM  * Evacuate the device.
434910594SGeorge.Wilson@Sun.COM  */
435010594SGeorge.Wilson@Sun.COM int
435110594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
435210594SGeorge.Wilson@Sun.COM {
435310974SJeff.Bonwick@Sun.COM 	int error = 0;
435410594SGeorge.Wilson@Sun.COM 	uint64_t txg;
435510594SGeorge.Wilson@Sun.COM 
435610594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
435710594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
435810922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
435910594SGeorge.Wilson@Sun.COM 
436010594SGeorge.Wilson@Sun.COM 	/*
436110594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
436210594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
436310594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
436410594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
436510594SGeorge.Wilson@Sun.COM 	 */
436610594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
436710974SJeff.Bonwick@Sun.COM 		error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
436810974SJeff.Bonwick@Sun.COM 		    NULL, DS_FIND_CHILDREN);
436910974SJeff.Bonwick@Sun.COM 	} else {
437010974SJeff.Bonwick@Sun.COM 		error = ENOTSUP;	/* until we have bp rewrite */
437110594SGeorge.Wilson@Sun.COM 	}
437210594SGeorge.Wilson@Sun.COM 
437310974SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
437410974SJeff.Bonwick@Sun.COM 
437510974SJeff.Bonwick@Sun.COM 	if (error)
437610974SJeff.Bonwick@Sun.COM 		return (error);
437710974SJeff.Bonwick@Sun.COM 
437810594SGeorge.Wilson@Sun.COM 	/*
437910974SJeff.Bonwick@Sun.COM 	 * The evacuation succeeded.  Remove any remaining MOS metadata
438010974SJeff.Bonwick@Sun.COM 	 * associated with this vdev, and wait for these changes to sync.
438110594SGeorge.Wilson@Sun.COM 	 */
438210594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
438310594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
438410594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
438510594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
438610594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
438710594SGeorge.Wilson@Sun.COM 
438810594SGeorge.Wilson@Sun.COM 	return (0);
438910594SGeorge.Wilson@Sun.COM }
439010594SGeorge.Wilson@Sun.COM 
439110594SGeorge.Wilson@Sun.COM /*
439210594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
439310594SGeorge.Wilson@Sun.COM  */
439410594SGeorge.Wilson@Sun.COM void
439510974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
439610594SGeorge.Wilson@Sun.COM {
439710594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
439810594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
439910594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
440010594SGeorge.Wilson@Sun.COM 
440110594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
440210594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
440310922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
440410594SGeorge.Wilson@Sun.COM 
440510594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
440610922SJeff.Bonwick@Sun.COM 
440710922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
440810922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
440910922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
441010922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
441110922SJeff.Bonwick@Sun.COM 
441210594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
441310594SGeorge.Wilson@Sun.COM 
441410594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
441510594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
441610594SGeorge.Wilson@Sun.COM 	} else {
441710594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
441810594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
441910594SGeorge.Wilson@Sun.COM 	}
442010594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(rvd);
442110594SGeorge.Wilson@Sun.COM 
442210594SGeorge.Wilson@Sun.COM 	/*
442310594SGeorge.Wilson@Sun.COM 	 * Reassess the health of our root vdev.
442410594SGeorge.Wilson@Sun.COM 	 */
442510594SGeorge.Wilson@Sun.COM 	vdev_reopen(rvd);
442610594SGeorge.Wilson@Sun.COM }
442710594SGeorge.Wilson@Sun.COM 
442810594SGeorge.Wilson@Sun.COM /*
44295450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
443010594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
44315450Sbrendan  */
44325450Sbrendan int
44335450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
44345450Sbrendan {
44355450Sbrendan 	vdev_t *vd;
443610974SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
44377754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
443810594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
44395450Sbrendan 	uint_t nspares, nl2cache;
44405450Sbrendan 	int error = 0;
44418241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
44428241SJeff.Bonwick@Sun.COM 
44438241SJeff.Bonwick@Sun.COM 	if (!locked)
44448241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
44455450Sbrendan 
44466643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
44475450Sbrendan 
44485450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
44495450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
44507754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
44517754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
44527754SJeff.Bonwick@Sun.COM 		/*
44537754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
44547754SJeff.Bonwick@Sun.COM 		 * in this pool.
44557754SJeff.Bonwick@Sun.COM 		 */
44567754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
44577754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
44587754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
44597754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
44607754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
44617754SJeff.Bonwick@Sun.COM 		} else {
44627754SJeff.Bonwick@Sun.COM 			error = EBUSY;
44637754SJeff.Bonwick@Sun.COM 		}
44647754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
44655450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
44667754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
44677754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
44687754SJeff.Bonwick@Sun.COM 		/*
44697754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
44707754SJeff.Bonwick@Sun.COM 		 */
44717754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
44727754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
44735450Sbrendan 		spa_load_l2cache(spa);
44745450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
447510594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
447610594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
447710922SJeff.Bonwick@Sun.COM 		ASSERT(vd == vd->vdev_top);
447810594SGeorge.Wilson@Sun.COM 
447910594SGeorge.Wilson@Sun.COM 		/*
448010594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
448110594SGeorge.Wilson@Sun.COM 		 * become the common case.
448210594SGeorge.Wilson@Sun.COM 		 */
448310594SGeorge.Wilson@Sun.COM 
448410974SJeff.Bonwick@Sun.COM 		mg = vd->vdev_mg;
448510974SJeff.Bonwick@Sun.COM 
448610594SGeorge.Wilson@Sun.COM 		/*
448710974SJeff.Bonwick@Sun.COM 		 * Stop allocating from this vdev.
448810594SGeorge.Wilson@Sun.COM 		 */
448910974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(mg);
449010594SGeorge.Wilson@Sun.COM 
449110922SJeff.Bonwick@Sun.COM 		/*
449210922SJeff.Bonwick@Sun.COM 		 * Wait for the youngest allocations and frees to sync,
449310922SJeff.Bonwick@Sun.COM 		 * and then wait for the deferral of those frees to finish.
449410922SJeff.Bonwick@Sun.COM 		 */
449510922SJeff.Bonwick@Sun.COM 		spa_vdev_config_exit(spa, NULL,
449610922SJeff.Bonwick@Sun.COM 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
449710922SJeff.Bonwick@Sun.COM 
449810974SJeff.Bonwick@Sun.COM 		/*
449910974SJeff.Bonwick@Sun.COM 		 * Attempt to evacuate the vdev.
450010974SJeff.Bonwick@Sun.COM 		 */
450110974SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove_evacuate(spa, vd);
450210974SJeff.Bonwick@Sun.COM 
450310594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
450410594SGeorge.Wilson@Sun.COM 
450510974SJeff.Bonwick@Sun.COM 		/*
450610974SJeff.Bonwick@Sun.COM 		 * If we couldn't evacuate the vdev, unwind.
450710974SJeff.Bonwick@Sun.COM 		 */
450810974SJeff.Bonwick@Sun.COM 		if (error) {
450910974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
451010974SJeff.Bonwick@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
451110974SJeff.Bonwick@Sun.COM 		}
451210974SJeff.Bonwick@Sun.COM 
451310974SJeff.Bonwick@Sun.COM 		/*
451410974SJeff.Bonwick@Sun.COM 		 * Clean up the vdev namespace.
451510974SJeff.Bonwick@Sun.COM 		 */
451610974SJeff.Bonwick@Sun.COM 		spa_vdev_remove_from_namespace(spa, vd);
451710594SGeorge.Wilson@Sun.COM 
45187754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
45197754SJeff.Bonwick@Sun.COM 		/*
45207754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
45217754SJeff.Bonwick@Sun.COM 		 */
45227754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
45237754SJeff.Bonwick@Sun.COM 	} else {
45247754SJeff.Bonwick@Sun.COM 		/*
45257754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
45267754SJeff.Bonwick@Sun.COM 		 */
45277754SJeff.Bonwick@Sun.COM 		error = ENOENT;
45285450Sbrendan 	}
45292082Seschrock 
45308241SJeff.Bonwick@Sun.COM 	if (!locked)
45318241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
45328241SJeff.Bonwick@Sun.COM 
45338241SJeff.Bonwick@Sun.COM 	return (error);
4534789Sahrens }
4535789Sahrens 
4536789Sahrens /*
45374451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
45384451Seschrock  * current spared, so we can detach it.
4539789Sahrens  */
45401544Seschrock static vdev_t *
45414451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
4542789Sahrens {
45431544Seschrock 	vdev_t *newvd, *oldvd;
45449816SGeorge.Wilson@Sun.COM 
45459816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
45464451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
45471544Seschrock 		if (oldvd != NULL)
45481544Seschrock 			return (oldvd);
45491544Seschrock 	}
4550789Sahrens 
45514451Seschrock 	/*
45524451Seschrock 	 * Check for a completed replacement.
45534451Seschrock 	 */
4554789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
45551544Seschrock 		oldvd = vd->vdev_child[0];
45561544Seschrock 		newvd = vd->vdev_child[1];
4557789Sahrens 
45588241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
45598241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
45601544Seschrock 			return (oldvd);
45611544Seschrock 	}
4562789Sahrens 
45634451Seschrock 	/*
45644451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
45654451Seschrock 	 */
45664451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
45674451Seschrock 		newvd = vd->vdev_child[0];
45684451Seschrock 		oldvd = vd->vdev_child[1];
45694451Seschrock 
45704451Seschrock 		if (newvd->vdev_unspare &&
45718241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
45728241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
45734451Seschrock 			newvd->vdev_unspare = 0;
45744451Seschrock 			return (oldvd);
45754451Seschrock 		}
45764451Seschrock 	}
45774451Seschrock 
45781544Seschrock 	return (NULL);
4579789Sahrens }
4580789Sahrens 
45811544Seschrock static void
45824451Seschrock spa_vdev_resilver_done(spa_t *spa)
4583789Sahrens {
45848241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
45858241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
45868241SJeff.Bonwick@Sun.COM 
45878241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4588789Sahrens 
45894451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
45908241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
45918241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
45921544Seschrock 		guid = vd->vdev_guid;
45938241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
45948241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
45958241SJeff.Bonwick@Sun.COM 		sguid = 0;
45962082Seschrock 		/*
45972082Seschrock 		 * If we have just finished replacing a hot spared device, then
45982082Seschrock 		 * we need to detach the parent's first child (the original hot
45992082Seschrock 		 * spare) as well.
46002082Seschrock 		 */
46018241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
46022082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
46038241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
46048241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
46052082Seschrock 		}
46068241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
46078241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
46081544Seschrock 			return;
46098241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
46102082Seschrock 			return;
46118241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4612789Sahrens 	}
4613789Sahrens 
46148241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4615789Sahrens }
4616789Sahrens 
4617789Sahrens /*
461811041SEric.Taylor@Sun.COM  * Update the stored path or FRU for this vdev.
46191354Seschrock  */
46201354Seschrock int
46219425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
46229425SEric.Schrock@Sun.COM     boolean_t ispath)
46231354Seschrock {
46246643Seschrock 	vdev_t *vd;
462511041SEric.Taylor@Sun.COM 
462611041SEric.Taylor@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALL);
46271354Seschrock 
46289425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
462911041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
46301354Seschrock 
46311585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
463211041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
46331585Sbonwick 
46349425SEric.Schrock@Sun.COM 	if (ispath) {
46359425SEric.Schrock@Sun.COM 		spa_strfree(vd->vdev_path);
46369425SEric.Schrock@Sun.COM 		vd->vdev_path = spa_strdup(value);
46379425SEric.Schrock@Sun.COM 	} else {
46389425SEric.Schrock@Sun.COM 		if (vd->vdev_fru != NULL)
46399425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
46409425SEric.Schrock@Sun.COM 		vd->vdev_fru = spa_strdup(value);
46419425SEric.Schrock@Sun.COM 	}
46421354Seschrock 
464311041SEric.Taylor@Sun.COM 	return (spa_vdev_state_exit(spa, vd, 0));
46441354Seschrock }
46451354Seschrock 
46469425SEric.Schrock@Sun.COM int
46479425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
46489425SEric.Schrock@Sun.COM {
46499425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
46509425SEric.Schrock@Sun.COM }
46519425SEric.Schrock@Sun.COM 
46529425SEric.Schrock@Sun.COM int
46539425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
46549425SEric.Schrock@Sun.COM {
46559425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
46569425SEric.Schrock@Sun.COM }
46579425SEric.Schrock@Sun.COM 
46581354Seschrock /*
4659789Sahrens  * ==========================================================================
4660789Sahrens  * SPA Scrubbing
4661789Sahrens  * ==========================================================================
4662789Sahrens  */
4663789Sahrens 
46647046Sahrens int
46657046Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
4666789Sahrens {
46677754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
46684808Sek110237 
4669789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
4670789Sahrens 		return (ENOTSUP);
4671789Sahrens 
4672789Sahrens 	/*
46737046Sahrens 	 * If a resilver was requested, but there is no DTL on a
46747046Sahrens 	 * writeable leaf device, we have nothing to do.
4675789Sahrens 	 */
46767046Sahrens 	if (type == POOL_SCRUB_RESILVER &&
46777046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
46787046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
46791544Seschrock 		return (0);
46801544Seschrock 	}
4681789Sahrens 
46827046Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
46837046Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
46847046Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
46857046Sahrens 		return (EBUSY);
46867046Sahrens 
46877046Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
46887046Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
46897046Sahrens 	} else if (type == POOL_SCRUB_NONE) {
46907046Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
46911544Seschrock 	} else {
46927046Sahrens 		return (EINVAL);
46931544Seschrock 	}
4694789Sahrens }
4695789Sahrens 
46961544Seschrock /*
46971544Seschrock  * ==========================================================================
46981544Seschrock  * SPA async task processing
46991544Seschrock  * ==========================================================================
47001544Seschrock  */
47011544Seschrock 
47021544Seschrock static void
47034451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4704789Sahrens {
47057361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
47067361SBrendan.Gregg@Sun.COM 		vd->vdev_remove_wanted = 0;
47077361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
470810575SEric.Schrock@Sun.COM 
470910575SEric.Schrock@Sun.COM 		/*
471010575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
471110575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
471210575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
471310575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
471410575SEric.Schrock@Sun.COM 		 */
471510575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
471610575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
471710575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
471810575SEric.Schrock@Sun.COM 
47197754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
47201544Seschrock 	}
47217361SBrendan.Gregg@Sun.COM 
47227754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47237361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
47241544Seschrock }
47251544Seschrock 
47261544Seschrock static void
47277754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
47287754SJeff.Bonwick@Sun.COM {
47297754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
47307754SJeff.Bonwick@Sun.COM 		vd->vdev_probe_wanted = 0;
47317754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
47327754SJeff.Bonwick@Sun.COM 	}
47337754SJeff.Bonwick@Sun.COM 
47347754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47357754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
47367754SJeff.Bonwick@Sun.COM }
47377754SJeff.Bonwick@Sun.COM 
47387754SJeff.Bonwick@Sun.COM static void
47399816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
47409816SGeorge.Wilson@Sun.COM {
47419816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
47429816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
47439816SGeorge.Wilson@Sun.COM 	char *physpath;
47449816SGeorge.Wilson@Sun.COM 
47459816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
47469816SGeorge.Wilson@Sun.COM 		return;
47479816SGeorge.Wilson@Sun.COM 
47489816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
47499816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
47509816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
47519816SGeorge.Wilson@Sun.COM 	}
47529816SGeorge.Wilson@Sun.COM 
47539816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
47549816SGeorge.Wilson@Sun.COM 		return;
47559816SGeorge.Wilson@Sun.COM 
47569816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
47579816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
47589816SGeorge.Wilson@Sun.COM 
47599816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
47609816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
47619816SGeorge.Wilson@Sun.COM 
47629816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
47639816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
47649816SGeorge.Wilson@Sun.COM 
47659816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
47669816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
47679816SGeorge.Wilson@Sun.COM }
47689816SGeorge.Wilson@Sun.COM 
47699816SGeorge.Wilson@Sun.COM static void
47701544Seschrock spa_async_thread(spa_t *spa)
47711544Seschrock {
47727754SJeff.Bonwick@Sun.COM 	int tasks;
47731544Seschrock 
47741544Seschrock 	ASSERT(spa->spa_sync_on);
4775789Sahrens 
47761544Seschrock 	mutex_enter(&spa->spa_async_lock);
47771544Seschrock 	tasks = spa->spa_async_tasks;
47781544Seschrock 	spa->spa_async_tasks = 0;
47791544Seschrock 	mutex_exit(&spa->spa_async_lock);
47801544Seschrock 
47811544Seschrock 	/*
47821635Sbonwick 	 * See if the config needs to be updated.
47831635Sbonwick 	 */
47841635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
478510922SJeff.Bonwick@Sun.COM 		uint64_t old_space, new_space;
47869816SGeorge.Wilson@Sun.COM 
47871635Sbonwick 		mutex_enter(&spa_namespace_lock);
478810922SJeff.Bonwick@Sun.COM 		old_space = metaslab_class_get_space(spa_normal_class(spa));
47891635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
479010922SJeff.Bonwick@Sun.COM 		new_space = metaslab_class_get_space(spa_normal_class(spa));
47911635Sbonwick 		mutex_exit(&spa_namespace_lock);
47929816SGeorge.Wilson@Sun.COM 
47939816SGeorge.Wilson@Sun.COM 		/*
47949816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
47959816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
47969816SGeorge.Wilson@Sun.COM 		 */
479710922SJeff.Bonwick@Sun.COM 		if (new_space != old_space) {
47989946SMark.Musante@Sun.COM 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
47999946SMark.Musante@Sun.COM 			    spa, NULL, CRED(),
48009946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
480110922SJeff.Bonwick@Sun.COM 			    spa_name(spa), new_space, new_space - old_space);
48029816SGeorge.Wilson@Sun.COM 		}
48031635Sbonwick 	}
48041635Sbonwick 
48051635Sbonwick 	/*
48064451Seschrock 	 * See if any devices need to be marked REMOVED.
48071544Seschrock 	 */
48087754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
480910685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48104451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
48117754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
48127361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
48137754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
48147361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
48157754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48167754SJeff.Bonwick@Sun.COM 	}
48177754SJeff.Bonwick@Sun.COM 
48189816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
48199816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
48209816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
48219816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
48229816SGeorge.Wilson@Sun.COM 	}
48239816SGeorge.Wilson@Sun.COM 
48247754SJeff.Bonwick@Sun.COM 	/*
48257754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
48267754SJeff.Bonwick@Sun.COM 	 */
48277754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
482810685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48297754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
48307754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48314451Seschrock 	}
48321544Seschrock 
48331544Seschrock 	/*
48341544Seschrock 	 * If any devices are done replacing, detach them.
48351544Seschrock 	 */
48364451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
48374451Seschrock 		spa_vdev_resilver_done(spa);
4838789Sahrens 
48391544Seschrock 	/*
48401544Seschrock 	 * Kick off a resilver.
48411544Seschrock 	 */
48427046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
48437046Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
48441544Seschrock 
48451544Seschrock 	/*
48461544Seschrock 	 * Let the world know that we're done.
48471544Seschrock 	 */
48481544Seschrock 	mutex_enter(&spa->spa_async_lock);
48491544Seschrock 	spa->spa_async_thread = NULL;
48501544Seschrock 	cv_broadcast(&spa->spa_async_cv);
48511544Seschrock 	mutex_exit(&spa->spa_async_lock);
48521544Seschrock 	thread_exit();
48531544Seschrock }
48541544Seschrock 
48551544Seschrock void
48561544Seschrock spa_async_suspend(spa_t *spa)
48571544Seschrock {
48581544Seschrock 	mutex_enter(&spa->spa_async_lock);
48591544Seschrock 	spa->spa_async_suspended++;
48601544Seschrock 	while (spa->spa_async_thread != NULL)
48611544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
48621544Seschrock 	mutex_exit(&spa->spa_async_lock);
48631544Seschrock }
48641544Seschrock 
48651544Seschrock void
48661544Seschrock spa_async_resume(spa_t *spa)
48671544Seschrock {
48681544Seschrock 	mutex_enter(&spa->spa_async_lock);
48691544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
48701544Seschrock 	spa->spa_async_suspended--;
48711544Seschrock 	mutex_exit(&spa->spa_async_lock);
48721544Seschrock }
48731544Seschrock 
48741544Seschrock static void
48751544Seschrock spa_async_dispatch(spa_t *spa)
48761544Seschrock {
48771544Seschrock 	mutex_enter(&spa->spa_async_lock);
48781544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
48791635Sbonwick 	    spa->spa_async_thread == NULL &&
48801635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
48811544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
48821544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
48831544Seschrock 	mutex_exit(&spa->spa_async_lock);
48841544Seschrock }
48851544Seschrock 
48861544Seschrock void
48871544Seschrock spa_async_request(spa_t *spa, int task)
48881544Seschrock {
48891544Seschrock 	mutex_enter(&spa->spa_async_lock);
48901544Seschrock 	spa->spa_async_tasks |= task;
48911544Seschrock 	mutex_exit(&spa->spa_async_lock);
4892789Sahrens }
4893789Sahrens 
4894789Sahrens /*
4895789Sahrens  * ==========================================================================
4896789Sahrens  * SPA syncing routines
4897789Sahrens  * ==========================================================================
4898789Sahrens  */
4899789Sahrens static void
490010922SJeff.Bonwick@Sun.COM spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg)
4901789Sahrens {
4902789Sahrens 	blkptr_t blk;
4903789Sahrens 	uint64_t itor = 0;
4904789Sahrens 	uint8_t c = 1;
4905789Sahrens 
49067754SJeff.Bonwick@Sun.COM 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
49077754SJeff.Bonwick@Sun.COM 		ASSERT(blk.blk_birth < txg);
490810922SJeff.Bonwick@Sun.COM 		zio_free(spa, txg, &blk);
49097754SJeff.Bonwick@Sun.COM 	}
4910789Sahrens 
4911789Sahrens 	bplist_vacate(bpl, tx);
4912789Sahrens 
4913789Sahrens 	/*
4914789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
4915789Sahrens 	 * (Usually only the first block is needed.)
4916789Sahrens 	 */
491710922SJeff.Bonwick@Sun.COM 	dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx);
491810922SJeff.Bonwick@Sun.COM }
491910922SJeff.Bonwick@Sun.COM 
492010922SJeff.Bonwick@Sun.COM static void
492110922SJeff.Bonwick@Sun.COM spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
492210922SJeff.Bonwick@Sun.COM {
492310922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
492410922SJeff.Bonwick@Sun.COM 
492510922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
492610922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
4927789Sahrens }
4928789Sahrens 
4929789Sahrens static void
49302082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
49312082Seschrock {
49322082Seschrock 	char *packed = NULL;
49337497STim.Haley@Sun.COM 	size_t bufsize;
49342082Seschrock 	size_t nvsize = 0;
49352082Seschrock 	dmu_buf_t *db;
49362082Seschrock 
49372082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
49382082Seschrock 
49397497STim.Haley@Sun.COM 	/*
49407497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
49417497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
49427497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
49437497STim.Haley@Sun.COM 	 */
49447497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
49457497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
49462082Seschrock 
49472082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
49482082Seschrock 	    KM_SLEEP) == 0);
49497497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
49507497STim.Haley@Sun.COM 
49517497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
49527497STim.Haley@Sun.COM 
49537497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
49542082Seschrock 
49552082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
49562082Seschrock 	dmu_buf_will_dirty(db, tx);
49572082Seschrock 	*(uint64_t *)db->db_data = nvsize;
49582082Seschrock 	dmu_buf_rele(db, FTAG);
49592082Seschrock }
49602082Seschrock 
49612082Seschrock static void
49625450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
49635450Sbrendan     const char *config, const char *entry)
49642082Seschrock {
49652082Seschrock 	nvlist_t *nvroot;
49665450Sbrendan 	nvlist_t **list;
49672082Seschrock 	int i;
49682082Seschrock 
49695450Sbrendan 	if (!sav->sav_sync)
49702082Seschrock 		return;
49712082Seschrock 
49722082Seschrock 	/*
49735450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
49745450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
49754451Seschrock 	 * valid and the vdevs are labeled appropriately.
49762082Seschrock 	 */
49775450Sbrendan 	if (sav->sav_object == 0) {
49785450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
49795450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
49805450Sbrendan 		    sizeof (uint64_t), tx);
49812082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
49825450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
49835450Sbrendan 		    &sav->sav_object, tx) == 0);
49842082Seschrock 	}
49852082Seschrock 
49862082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
49875450Sbrendan 	if (sav->sav_count == 0) {
49885450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
49892082Seschrock 	} else {
49905450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
49915450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
49925450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
49935450Sbrendan 			    B_FALSE, B_FALSE, B_TRUE);
49945450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
49955450Sbrendan 		    sav->sav_count) == 0);
49965450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
49975450Sbrendan 			nvlist_free(list[i]);
49985450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
49992082Seschrock 	}
50002082Seschrock 
50015450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
50022926Sek110237 	nvlist_free(nvroot);
50032082Seschrock 
50045450Sbrendan 	sav->sav_sync = B_FALSE;
50052082Seschrock }
50062082Seschrock 
50072082Seschrock static void
5008789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5009789Sahrens {
5010789Sahrens 	nvlist_t *config;
5011789Sahrens 
50127754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
5013789Sahrens 		return;
5014789Sahrens 
50157754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
50167754SJeff.Bonwick@Sun.COM 
50177754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
50187754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
50197754SJeff.Bonwick@Sun.COM 
50207754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
5021789Sahrens 
50221635Sbonwick 	if (spa->spa_config_syncing)
50231635Sbonwick 		nvlist_free(spa->spa_config_syncing);
50241635Sbonwick 	spa->spa_config_syncing = config;
5025789Sahrens 
50262082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5027789Sahrens }
5028789Sahrens 
50295094Slling /*
50305094Slling  * Set zpool properties.
50315094Slling  */
50323912Slling static void
50334543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
50343912Slling {
50353912Slling 	spa_t *spa = arg1;
50365094Slling 	objset_t *mos = spa->spa_meta_objset;
50373912Slling 	nvlist_t *nvp = arg2;
50385094Slling 	nvpair_t *elem;
50394451Seschrock 	uint64_t intval;
50406643Seschrock 	char *strval;
50415094Slling 	zpool_prop_t prop;
50425094Slling 	const char *propname;
50435094Slling 	zprop_type_t proptype;
50445094Slling 
50457754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
50467754SJeff.Bonwick@Sun.COM 
50475094Slling 	elem = NULL;
50485094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
50495094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
50505094Slling 		case ZPOOL_PROP_VERSION:
50515094Slling 			/*
50525094Slling 			 * Only set version for non-zpool-creation cases
50535094Slling 			 * (set/import). spa_create() needs special care
50545094Slling 			 * for version setting.
50555094Slling 			 */
50565094Slling 			if (tx->tx_txg != TXG_INITIAL) {
50575094Slling 				VERIFY(nvpair_value_uint64(elem,
50585094Slling 				    &intval) == 0);
50595094Slling 				ASSERT(intval <= SPA_VERSION);
50605094Slling 				ASSERT(intval >= spa_version(spa));
50615094Slling 				spa->spa_uberblock.ub_version = intval;
50625094Slling 				vdev_config_dirty(spa->spa_root_vdev);
50635094Slling 			}
50645094Slling 			break;
50655094Slling 
50665094Slling 		case ZPOOL_PROP_ALTROOT:
50675094Slling 			/*
50685094Slling 			 * 'altroot' is a non-persistent property. It should
50695094Slling 			 * have been set temporarily at creation or import time.
50705094Slling 			 */
50715094Slling 			ASSERT(spa->spa_root != NULL);
50725094Slling 			break;
50735094Slling 
50745363Seschrock 		case ZPOOL_PROP_CACHEFILE:
50755094Slling 			/*
50768525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
50775094Slling 			 */
50784543Smarks 			break;
50795094Slling 		default:
50805094Slling 			/*
50815094Slling 			 * Set pool property values in the poolprops mos object.
50825094Slling 			 */
50835094Slling 			if (spa->spa_pool_props_object == 0) {
50845094Slling 				VERIFY((spa->spa_pool_props_object =
50855094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
50865094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
50875094Slling 
50885094Slling 				VERIFY(zap_update(mos,
50895094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
50905094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
50915094Slling 				    == 0);
50925094Slling 			}
50935094Slling 
50945094Slling 			/* normalize the property name */
50955094Slling 			propname = zpool_prop_to_name(prop);
50965094Slling 			proptype = zpool_prop_get_type(prop);
50975094Slling 
50985094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
50995094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
51005094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
51015094Slling 				VERIFY(zap_update(mos,
51025094Slling 				    spa->spa_pool_props_object, propname,
51035094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
51045094Slling 
51055094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
51065094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
51075094Slling 
51085094Slling 				if (proptype == PROP_TYPE_INDEX) {
51095094Slling 					const char *unused;
51105094Slling 					VERIFY(zpool_prop_index_to_string(
51115094Slling 					    prop, intval, &unused) == 0);
51125094Slling 				}
51135094Slling 				VERIFY(zap_update(mos,
51145094Slling 				    spa->spa_pool_props_object, propname,
51155094Slling 				    8, 1, &intval, tx) == 0);
51165094Slling 			} else {
51175094Slling 				ASSERT(0); /* not allowed */
51185094Slling 			}
51195094Slling 
51205329Sgw25295 			switch (prop) {
51215329Sgw25295 			case ZPOOL_PROP_DELEGATION:
51225094Slling 				spa->spa_delegation = intval;
51235329Sgw25295 				break;
51245329Sgw25295 			case ZPOOL_PROP_BOOTFS:
51255094Slling 				spa->spa_bootfs = intval;
51265329Sgw25295 				break;
51275329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
51285329Sgw25295 				spa->spa_failmode = intval;
51295329Sgw25295 				break;
51309816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
51319816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
51329816SGeorge.Wilson@Sun.COM 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
51339816SGeorge.Wilson@Sun.COM 				break;
513410922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
513510922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
513610922SJeff.Bonwick@Sun.COM 				break;
51375329Sgw25295 			default:
51385329Sgw25295 				break;
51395329Sgw25295 			}
51403912Slling 		}
51415094Slling 
51425094Slling 		/* log internal history if this is not a zpool create */
51435094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
51445094Slling 		    tx->tx_txg != TXG_INITIAL) {
51455094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
51465094Slling 			    spa, tx, cr, "%s %lld %s",
51477754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
51485094Slling 		}
51493912Slling 	}
51507754SJeff.Bonwick@Sun.COM 
51517754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
51523912Slling }
51533912Slling 
5154789Sahrens /*
5155789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
5156789Sahrens  * part of the process, so we iterate until it converges.
5157789Sahrens  */
5158789Sahrens void
5159789Sahrens spa_sync(spa_t *spa, uint64_t txg)
5160789Sahrens {
5161789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
5162789Sahrens 	objset_t *mos = spa->spa_meta_objset;
516310922SJeff.Bonwick@Sun.COM 	bplist_t *defer_bpl = &spa->spa_deferred_bplist;
516410922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
51651635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5166789Sahrens 	vdev_t *vd;
5167789Sahrens 	dmu_tx_t *tx;
51687754SJeff.Bonwick@Sun.COM 	int error;
5169789Sahrens 
5170789Sahrens 	/*
5171789Sahrens 	 * Lock out configuration changes.
5172789Sahrens 	 */
51737754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5174789Sahrens 
5175789Sahrens 	spa->spa_syncing_txg = txg;
5176789Sahrens 	spa->spa_sync_pass = 0;
5177789Sahrens 
51787754SJeff.Bonwick@Sun.COM 	/*
51797754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
51807754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
51817754SJeff.Bonwick@Sun.COM 	 */
51827754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
51838241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
51848241SJeff.Bonwick@Sun.COM 		/*
51858241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
51868241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
51878241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
51888241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
51898241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
51908241SJeff.Bonwick@Sun.COM 		 */
51918241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
51928241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
51938241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
51948241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
51958241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
51968241SJeff.Bonwick@Sun.COM 		}
51978241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
51988241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
51997754SJeff.Bonwick@Sun.COM 	}
52007754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
52017754SJeff.Bonwick@Sun.COM 
520210922SJeff.Bonwick@Sun.COM 	VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj));
5203789Sahrens 
52042082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
52052082Seschrock 
52062082Seschrock 	/*
52074577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
52082082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
52092082Seschrock 	 */
52104577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
52114577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
52122082Seschrock 		int i;
52132082Seschrock 
52142082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
52152082Seschrock 			vd = rvd->vdev_child[i];
52162082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
52172082Seschrock 				break;
52182082Seschrock 		}
52192082Seschrock 		if (i == rvd->vdev_children) {
52202082Seschrock 			spa->spa_deflate = TRUE;
52212082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
52222082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
52232082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
52242082Seschrock 		}
52252082Seschrock 	}
52262082Seschrock 
52277046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
52287046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
52297046Sahrens 		dsl_pool_create_origin(dp, tx);
52307046Sahrens 
52317046Sahrens 		/* Keeping the origin open increases spa_minref */
52327046Sahrens 		spa->spa_minref += 3;
52337046Sahrens 	}
52347046Sahrens 
52357046Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
52367046Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
52377046Sahrens 		dsl_pool_upgrade_clones(dp, tx);
52387046Sahrens 	}
52397046Sahrens 
5240789Sahrens 	/*
5241789Sahrens 	 * If anything has changed in this txg, push the deferred frees
5242789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
5243789Sahrens 	 * don't generate work on an otherwise idle system.
5244789Sahrens 	 */
5245789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
52462329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
52472329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
524810922SJeff.Bonwick@Sun.COM 		spa_sync_deferred_bplist(spa, defer_bpl, tx, txg);
5249789Sahrens 
5250789Sahrens 	/*
5251789Sahrens 	 * Iterate to convergence.
5252789Sahrens 	 */
5253789Sahrens 	do {
525410922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
5255789Sahrens 
5256789Sahrens 		spa_sync_config_object(spa, tx);
52575450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
52585450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
52595450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
52605450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
52611544Seschrock 		spa_errlog_sync(spa, txg);
5262789Sahrens 		dsl_pool_sync(dp, txg);
5263789Sahrens 
526410922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
526510922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
526610922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, spa_sync_free, zio, tx);
526710922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
526810922SJeff.Bonwick@Sun.COM 		} else {
526910922SJeff.Bonwick@Sun.COM 			bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx);
5270789Sahrens 		}
5271789Sahrens 
527210922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
527310922SJeff.Bonwick@Sun.COM 
5274*11619SGeorge.Wilson@Sun.COM 		mutex_enter(&spa->spa_scrub_lock);
5275*11619SGeorge.Wilson@Sun.COM 		while (spa->spa_scrub_inflight > 0)
5276*11619SGeorge.Wilson@Sun.COM 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
5277*11619SGeorge.Wilson@Sun.COM 		mutex_exit(&spa->spa_scrub_lock);
5278*11619SGeorge.Wilson@Sun.COM 
527910922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
528010922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
528110922SJeff.Bonwick@Sun.COM 
528210922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
528310922SJeff.Bonwick@Sun.COM 
528410922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
528510922SJeff.Bonwick@Sun.COM 
528610922SJeff.Bonwick@Sun.COM 	bplist_close(defer_bpl);
5287789Sahrens 
5288789Sahrens 	/*
5289789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
5290789Sahrens 	 * to commit the transaction group.
52911635Sbonwick 	 *
52925688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
52935688Sbonwick 	 * random top-level vdevs that are known to be visible in the
52947754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
52957754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5296789Sahrens 	 */
52977754SJeff.Bonwick@Sun.COM 	for (;;) {
52987754SJeff.Bonwick@Sun.COM 		/*
52997754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
53007754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
53017754SJeff.Bonwick@Sun.COM 		 */
53027754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
53037754SJeff.Bonwick@Sun.COM 
53047754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
53057754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
53067754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
53077754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
53087754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
53099816SGeorge.Wilson@Sun.COM 
53109816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
53117754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
53127754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
53137754SJeff.Bonwick@Sun.COM 					continue;
53147754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
53157754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
53167754SJeff.Bonwick@Sun.COM 					break;
53177754SJeff.Bonwick@Sun.COM 			}
53189725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
53199725SEric.Schrock@Sun.COM 			if (error != 0)
53209725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
53219725SEric.Schrock@Sun.COM 				    B_TRUE);
53227754SJeff.Bonwick@Sun.COM 		} else {
53237754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
53249725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
53259725SEric.Schrock@Sun.COM 			if (error != 0)
53269725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
53279725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
53281635Sbonwick 		}
53297754SJeff.Bonwick@Sun.COM 
53307754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
53317754SJeff.Bonwick@Sun.COM 
53327754SJeff.Bonwick@Sun.COM 		if (error == 0)
53337754SJeff.Bonwick@Sun.COM 			break;
53347754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
53357754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
53361635Sbonwick 	}
53372082Seschrock 	dmu_tx_commit(tx);
53382082Seschrock 
53391635Sbonwick 	/*
53401635Sbonwick 	 * Clear the dirty config list.
53411635Sbonwick 	 */
53427754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
53431635Sbonwick 		vdev_config_clean(vd);
53441635Sbonwick 
53451635Sbonwick 	/*
53461635Sbonwick 	 * Now that the new config has synced transactionally,
53471635Sbonwick 	 * let it become visible to the config cache.
53481635Sbonwick 	 */
53491635Sbonwick 	if (spa->spa_config_syncing != NULL) {
53501635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
53511635Sbonwick 		spa->spa_config_txg = txg;
53521635Sbonwick 		spa->spa_config_syncing = NULL;
53531635Sbonwick 	}
5354789Sahrens 
5355789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
5356789Sahrens 
535710922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
5358789Sahrens 
5359789Sahrens 	/*
5360789Sahrens 	 * Update usable space statistics.
5361789Sahrens 	 */
5362789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5363789Sahrens 		vdev_sync_done(vd, txg);
5364789Sahrens 
536510956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
536610956SGeorge.Wilson@Sun.COM 
5367789Sahrens 	/*
5368789Sahrens 	 * It had better be the case that we didn't dirty anything
53692082Seschrock 	 * since vdev_config_sync().
5370789Sahrens 	 */
5371789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5372789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5373789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
537410922SJeff.Bonwick@Sun.COM 	ASSERT(defer_bpl->bpl_queue == NULL);
537510922SJeff.Bonwick@Sun.COM 	ASSERT(free_bpl->bpl_queue == NULL);
537610922SJeff.Bonwick@Sun.COM 
537710922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
5378789Sahrens 
53797754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
53801544Seschrock 
538110921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
538210921STim.Haley@Sun.COM 
53831544Seschrock 	/*
53841544Seschrock 	 * If any async tasks have been requested, kick them off.
53851544Seschrock 	 */
53861544Seschrock 	spa_async_dispatch(spa);
5387789Sahrens }
5388789Sahrens 
5389789Sahrens /*
5390789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
5391789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
5392789Sahrens  * sync.
5393789Sahrens  */
5394789Sahrens void
5395789Sahrens spa_sync_allpools(void)
5396789Sahrens {
5397789Sahrens 	spa_t *spa = NULL;
5398789Sahrens 	mutex_enter(&spa_namespace_lock);
5399789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
54007754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
5401789Sahrens 			continue;
5402789Sahrens 		spa_open_ref(spa, FTAG);
5403789Sahrens 		mutex_exit(&spa_namespace_lock);
5404789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
5405789Sahrens 		mutex_enter(&spa_namespace_lock);
5406789Sahrens 		spa_close(spa, FTAG);
5407789Sahrens 	}
5408789Sahrens 	mutex_exit(&spa_namespace_lock);
5409789Sahrens }
5410789Sahrens 
5411789Sahrens /*
5412789Sahrens  * ==========================================================================
5413789Sahrens  * Miscellaneous routines
5414789Sahrens  * ==========================================================================
5415789Sahrens  */
5416789Sahrens 
5417789Sahrens /*
5418789Sahrens  * Remove all pools in the system.
5419789Sahrens  */
5420789Sahrens void
5421789Sahrens spa_evict_all(void)
5422789Sahrens {
5423789Sahrens 	spa_t *spa;
5424789Sahrens 
5425789Sahrens 	/*
5426789Sahrens 	 * Remove all cached state.  All pools should be closed now,
5427789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
5428789Sahrens 	 */
5429789Sahrens 	mutex_enter(&spa_namespace_lock);
5430789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
5431789Sahrens 		/*
54321544Seschrock 		 * Stop async tasks.  The async thread may need to detach
54331544Seschrock 		 * a device that's been replaced, which requires grabbing
54341544Seschrock 		 * spa_namespace_lock, so we must drop it here.
5435789Sahrens 		 */
5436789Sahrens 		spa_open_ref(spa, FTAG);
5437789Sahrens 		mutex_exit(&spa_namespace_lock);
54381544Seschrock 		spa_async_suspend(spa);
54394808Sek110237 		mutex_enter(&spa_namespace_lock);
5440789Sahrens 		spa_close(spa, FTAG);
5441789Sahrens 
5442789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5443789Sahrens 			spa_unload(spa);
5444789Sahrens 			spa_deactivate(spa);
5445789Sahrens 		}
5446789Sahrens 		spa_remove(spa);
5447789Sahrens 	}
5448789Sahrens 	mutex_exit(&spa_namespace_lock);
5449789Sahrens }
54501544Seschrock 
54511544Seschrock vdev_t *
54529425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
54531544Seschrock {
54546643Seschrock 	vdev_t *vd;
54556643Seschrock 	int i;
54566643Seschrock 
54576643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
54586643Seschrock 		return (vd);
54596643Seschrock 
54609425SEric.Schrock@Sun.COM 	if (aux) {
54616643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
54626643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
54636643Seschrock 			if (vd->vdev_guid == guid)
54646643Seschrock 				return (vd);
54656643Seschrock 		}
54669425SEric.Schrock@Sun.COM 
54679425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
54689425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
54699425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
54709425SEric.Schrock@Sun.COM 				return (vd);
54719425SEric.Schrock@Sun.COM 		}
54726643Seschrock 	}
54736643Seschrock 
54746643Seschrock 	return (NULL);
54751544Seschrock }
54761760Seschrock 
54771760Seschrock void
54785094Slling spa_upgrade(spa_t *spa, uint64_t version)
54791760Seschrock {
54807754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
54811760Seschrock 
54821760Seschrock 	/*
54831760Seschrock 	 * This should only be called for a non-faulted pool, and since a
54841760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
54851760Seschrock 	 * possible.
54861760Seschrock 	 */
54874577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
54885094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
54895094Slling 
54905094Slling 	spa->spa_uberblock.ub_version = version;
54911760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
54921760Seschrock 
54937754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
54942082Seschrock 
54952082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
54961760Seschrock }
54972082Seschrock 
54982082Seschrock boolean_t
54992082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
55002082Seschrock {
55012082Seschrock 	int i;
55023377Seschrock 	uint64_t spareguid;
55035450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
55045450Sbrendan 
55055450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
55065450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
55072082Seschrock 			return (B_TRUE);
55082082Seschrock 
55095450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
55105450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
55115450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
55123377Seschrock 			return (B_TRUE);
55133377Seschrock 	}
55143377Seschrock 
55152082Seschrock 	return (B_FALSE);
55162082Seschrock }
55173912Slling 
55184451Seschrock /*
55197214Slling  * Check if a pool has an active shared spare device.
55207214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
55217214Slling  */
55227214Slling static boolean_t
55237214Slling spa_has_active_shared_spare(spa_t *spa)
55247214Slling {
55257214Slling 	int i, refcnt;
55267214Slling 	uint64_t pool;
55277214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
55287214Slling 
55297214Slling 	for (i = 0; i < sav->sav_count; i++) {
55307214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
55317214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
55327214Slling 		    refcnt > 2)
55337214Slling 			return (B_TRUE);
55347214Slling 	}
55357214Slling 
55367214Slling 	return (B_FALSE);
55377214Slling }
55387214Slling 
55397214Slling /*
55404451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
55414451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
55424451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
55434451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
55444451Seschrock  * or zdb as real changes.
55454451Seschrock  */
55464451Seschrock void
55474451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
55484451Seschrock {
55494451Seschrock #ifdef _KERNEL
55504451Seschrock 	sysevent_t		*ev;
55514451Seschrock 	sysevent_attr_list_t	*attr = NULL;
55524451Seschrock 	sysevent_value_t	value;
55534451Seschrock 	sysevent_id_t		eid;
55544451Seschrock 
55554451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
55564451Seschrock 	    SE_SLEEP);
55574451Seschrock 
55584451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
55594451Seschrock 	value.value.sv_string = spa_name(spa);
55604451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
55614451Seschrock 		goto done;
55624451Seschrock 
55634451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
55644451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
55654451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
55664451Seschrock 		goto done;
55674451Seschrock 
55684451Seschrock 	if (vd) {
55694451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
55704451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
55714451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
55724451Seschrock 		    SE_SLEEP) != 0)
55734451Seschrock 			goto done;
55744451Seschrock 
55754451Seschrock 		if (vd->vdev_path) {
55764451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
55774451Seschrock 			value.value.sv_string = vd->vdev_path;
55784451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
55794451Seschrock 			    &value, SE_SLEEP) != 0)
55804451Seschrock 				goto done;
55814451Seschrock 		}
55824451Seschrock 	}
55834451Seschrock 
55845756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
55855756Seschrock 		goto done;
55865756Seschrock 	attr = NULL;
55875756Seschrock 
55884451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
55894451Seschrock 
55904451Seschrock done:
55914451Seschrock 	if (attr)
55924451Seschrock 		sysevent_free_attr(attr);
55934451Seschrock 	sysevent_free(ev);
55944451Seschrock #endif
55954451Seschrock }
5596