xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 12817)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
2312247SGeorge.Wilson@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
28789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
29789Sahrens  * pool.
30789Sahrens  */
31789Sahrens 
32789Sahrens #include <sys/zfs_context.h>
331544Seschrock #include <sys/fm/fs/zfs.h>
34789Sahrens #include <sys/spa_impl.h>
35789Sahrens #include <sys/zio.h>
36789Sahrens #include <sys/zio_checksum.h>
37789Sahrens #include <sys/dmu.h>
38789Sahrens #include <sys/dmu_tx.h>
39789Sahrens #include <sys/zap.h>
40789Sahrens #include <sys/zil.h>
4110922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
42789Sahrens #include <sys/vdev_impl.h>
43789Sahrens #include <sys/metaslab.h>
4410594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
45789Sahrens #include <sys/uberblock_impl.h>
46789Sahrens #include <sys/txg.h>
47789Sahrens #include <sys/avl.h>
48789Sahrens #include <sys/dmu_traverse.h>
493912Slling #include <sys/dmu_objset.h>
50789Sahrens #include <sys/unique.h>
51789Sahrens #include <sys/dsl_pool.h>
523912Slling #include <sys/dsl_dataset.h>
53789Sahrens #include <sys/dsl_dir.h>
54789Sahrens #include <sys/dsl_prop.h>
553912Slling #include <sys/dsl_synctask.h>
56789Sahrens #include <sys/fs/zfs.h>
575450Sbrendan #include <sys/arc.h>
58789Sahrens #include <sys/callb.h>
593975Sek110237 #include <sys/systeminfo.h>
606423Sgw25295 #include <sys/spa_boot.h>
619816SGeorge.Wilson@Sun.COM #include <sys/zfs_ioctl.h>
6212296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
63789Sahrens 
648662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
6511173SJonathan.Adams@Sun.COM #include <sys/bootprops.h>
6611173SJonathan.Adams@Sun.COM #include <sys/callb.h>
6711173SJonathan.Adams@Sun.COM #include <sys/cpupart.h>
6811173SJonathan.Adams@Sun.COM #include <sys/pool.h>
6911173SJonathan.Adams@Sun.COM #include <sys/sysdc.h>
708662SJordan.Vaughan@Sun.com #include <sys/zone.h>
718662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
728662SJordan.Vaughan@Sun.com 
735094Slling #include "zfs_prop.h"
745913Sperrin #include "zfs_comutil.h"
755094Slling 
7611173SJonathan.Adams@Sun.COM typedef enum zti_modes {
779515SJonathan.Adams@Sun.COM 	zti_mode_fixed,			/* value is # of threads (min 1) */
789515SJonathan.Adams@Sun.COM 	zti_mode_online_percent,	/* value is % of online CPUs */
7911173SJonathan.Adams@Sun.COM 	zti_mode_batch,			/* cpu-intensive; value is ignored */
8011146SGeorge.Wilson@Sun.COM 	zti_mode_null,			/* don't create a taskq */
819515SJonathan.Adams@Sun.COM 	zti_nmodes
8211173SJonathan.Adams@Sun.COM } zti_modes_t;
832986Sek110237 
8411146SGeorge.Wilson@Sun.COM #define	ZTI_FIX(n)	{ zti_mode_fixed, (n) }
8511146SGeorge.Wilson@Sun.COM #define	ZTI_PCT(n)	{ zti_mode_online_percent, (n) }
8611173SJonathan.Adams@Sun.COM #define	ZTI_BATCH	{ zti_mode_batch, 0 }
8711146SGeorge.Wilson@Sun.COM #define	ZTI_NULL	{ zti_mode_null, 0 }
8811146SGeorge.Wilson@Sun.COM 
8911146SGeorge.Wilson@Sun.COM #define	ZTI_ONE		ZTI_FIX(1)
909515SJonathan.Adams@Sun.COM 
919515SJonathan.Adams@Sun.COM typedef struct zio_taskq_info {
9211146SGeorge.Wilson@Sun.COM 	enum zti_modes zti_mode;
9311146SGeorge.Wilson@Sun.COM 	uint_t zti_value;
949515SJonathan.Adams@Sun.COM } zio_taskq_info_t;
959515SJonathan.Adams@Sun.COM 
969515SJonathan.Adams@Sun.COM static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
9711173SJonathan.Adams@Sun.COM 	"issue", "issue_high", "intr", "intr_high"
989515SJonathan.Adams@Sun.COM };
999515SJonathan.Adams@Sun.COM 
10011146SGeorge.Wilson@Sun.COM /*
10111146SGeorge.Wilson@Sun.COM  * Define the taskq threads for the following I/O types:
10211146SGeorge.Wilson@Sun.COM  * 	NULL, READ, WRITE, FREE, CLAIM, and IOCTL
10311146SGeorge.Wilson@Sun.COM  */
10411146SGeorge.Wilson@Sun.COM const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
10511146SGeorge.Wilson@Sun.COM 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
10611146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
10711173SJonathan.Adams@Sun.COM 	{ ZTI_FIX(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL },
10811173SJonathan.Adams@Sun.COM 	{ ZTI_BATCH,	ZTI_FIX(5),	ZTI_FIX(8),	ZTI_FIX(5) },
10912450SGeorge.Wilson@Sun.COM 	{ ZTI_FIX(100),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11011146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11111146SGeorge.Wilson@Sun.COM 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
1129515SJonathan.Adams@Sun.COM };
1139515SJonathan.Adams@Sun.COM 
11412296SLin.Ling@Sun.COM static dsl_syncfunc_t spa_sync_props;
1157214Slling static boolean_t spa_has_active_shared_spare(spa_t *spa);
11611422SMark.Musante@Sun.COM static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
11711422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
11811422SMark.Musante@Sun.COM     char **ereport);
1195094Slling 
12011173SJonathan.Adams@Sun.COM uint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
12111173SJonathan.Adams@Sun.COM id_t		zio_taskq_psrset_bind = PS_NONE;
12211173SJonathan.Adams@Sun.COM boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
12311173SJonathan.Adams@Sun.COM uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
12411173SJonathan.Adams@Sun.COM 
12511173SJonathan.Adams@Sun.COM boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
12611173SJonathan.Adams@Sun.COM 
12711173SJonathan.Adams@Sun.COM /*
12811173SJonathan.Adams@Sun.COM  * This (illegal) pool name is used when temporarily importing a spa_t in order
12911173SJonathan.Adams@Sun.COM  * to get the vdev stats associated with the imported devices.
13011173SJonathan.Adams@Sun.COM  */
13111173SJonathan.Adams@Sun.COM #define	TRYIMPORT_NAME	"$import"
13211173SJonathan.Adams@Sun.COM 
1335094Slling /*
1345094Slling  * ==========================================================================
1355094Slling  * SPA properties routines
1365094Slling  * ==========================================================================
1375094Slling  */
1385094Slling 
1395094Slling /*
1405094Slling  * Add a (source=src, propname=propval) list to an nvlist.
1415094Slling  */
1425949Slling static void
1435094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
1445094Slling     uint64_t intval, zprop_source_t src)
1455094Slling {
1465094Slling 	const char *propname = zpool_prop_to_name(prop);
1475094Slling 	nvlist_t *propval;
1485949Slling 
1495949Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1505949Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1515949Slling 
1525949Slling 	if (strval != NULL)
1535949Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1545949Slling 	else
1555949Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1565949Slling 
1575949Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
1585094Slling 	nvlist_free(propval);
1595094Slling }
1605094Slling 
1615094Slling /*
1625094Slling  * Get property values from the spa configuration.
1635094Slling  */
1645949Slling static void
1655094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1665094Slling {
1678525SEric.Schrock@Sun.COM 	uint64_t size;
16810956SGeorge.Wilson@Sun.COM 	uint64_t alloc;
1695094Slling 	uint64_t cap, version;
1705094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
1716643Seschrock 	spa_config_dirent_t *dp;
1725094Slling 
1737754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
1747754SJeff.Bonwick@Sun.COM 
1758525SEric.Schrock@Sun.COM 	if (spa->spa_root_vdev != NULL) {
17610956SGeorge.Wilson@Sun.COM 		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
17710922SJeff.Bonwick@Sun.COM 		size = metaslab_class_get_space(spa_normal_class(spa));
1788525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1798525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
18010956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
18110956SGeorge.Wilson@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
18210956SGeorge.Wilson@Sun.COM 		    size - alloc, src);
18310956SGeorge.Wilson@Sun.COM 
18410956SGeorge.Wilson@Sun.COM 		cap = (size == 0) ? 0 : (alloc * 100 / size);
1858525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
1868525SEric.Schrock@Sun.COM 
18710922SJeff.Bonwick@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
18810922SJeff.Bonwick@Sun.COM 		    ddt_get_pool_dedup_ratio(spa), src);
18910922SJeff.Bonwick@Sun.COM 
1908525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1918525SEric.Schrock@Sun.COM 		    spa->spa_root_vdev->vdev_state, src);
1928525SEric.Schrock@Sun.COM 
1938525SEric.Schrock@Sun.COM 		version = spa_version(spa);
1948525SEric.Schrock@Sun.COM 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
1958525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_DEFAULT;
1968525SEric.Schrock@Sun.COM 		else
1978525SEric.Schrock@Sun.COM 			src = ZPROP_SRC_LOCAL;
1988525SEric.Schrock@Sun.COM 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
1998525SEric.Schrock@Sun.COM 	}
2005949Slling 
2015949Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2025949Slling 
2035949Slling 	if (spa->spa_root != NULL)
2045949Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2055949Slling 		    0, ZPROP_SRC_LOCAL);
2065094Slling 
2076643Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
2086643Seschrock 		if (dp->scd_path == NULL) {
2095949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2106643Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
2116643Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2125949Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
2136643Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2145363Seschrock 		}
2155363Seschrock 	}
2165094Slling }
2175094Slling 
2185094Slling /*
2195094Slling  * Get zpool property values.
2205094Slling  */
2215094Slling int
2225094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
2235094Slling {
22410922SJeff.Bonwick@Sun.COM 	objset_t *mos = spa->spa_meta_objset;
2255094Slling 	zap_cursor_t zc;
2265094Slling 	zap_attribute_t za;
2275094Slling 	int err;
2285094Slling 
2295949Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2305094Slling 
2317754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
2327754SJeff.Bonwick@Sun.COM 
2335094Slling 	/*
2345094Slling 	 * Get properties from the spa config.
2355094Slling 	 */
2365949Slling 	spa_prop_get_config(spa, nvp);
2375094Slling 
2385094Slling 	/* If no pool property object, no more prop to get. */
23911619SGeorge.Wilson@Sun.COM 	if (mos == NULL || spa->spa_pool_props_object == 0) {
2405094Slling 		mutex_exit(&spa->spa_props_lock);
2415094Slling 		return (0);
2425094Slling 	}
2435094Slling 
2445094Slling 	/*
2455094Slling 	 * Get properties from the MOS pool property object.
2465094Slling 	 */
2475094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
2485094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
2495094Slling 	    zap_cursor_advance(&zc)) {
2505094Slling 		uint64_t intval = 0;
2515094Slling 		char *strval = NULL;
2525094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
2535094Slling 		zpool_prop_t prop;
2545094Slling 
2555094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
2565094Slling 			continue;
2575094Slling 
2585094Slling 		switch (za.za_integer_length) {
2595094Slling 		case 8:
2605094Slling 			/* integer property */
2615094Slling 			if (za.za_first_integer !=
2625094Slling 			    zpool_prop_default_numeric(prop))
2635094Slling 				src = ZPROP_SRC_LOCAL;
2645094Slling 
2655094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
2665094Slling 				dsl_pool_t *dp;
2675094Slling 				dsl_dataset_t *ds = NULL;
2685094Slling 
2695094Slling 				dp = spa_get_dsl(spa);
2705094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
2716689Smaybee 				if (err = dsl_dataset_hold_obj(dp,
2726689Smaybee 				    za.za_first_integer, FTAG, &ds)) {
2735094Slling 					rw_exit(&dp->dp_config_rwlock);
2745094Slling 					break;
2755094Slling 				}
2765094Slling 
2775094Slling 				strval = kmem_alloc(
2785094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
2795094Slling 				    KM_SLEEP);
2805094Slling 				dsl_dataset_name(ds, strval);
2816689Smaybee 				dsl_dataset_rele(ds, FTAG);
2825094Slling 				rw_exit(&dp->dp_config_rwlock);
2835094Slling 			} else {
2845094Slling 				strval = NULL;
2855094Slling 				intval = za.za_first_integer;
2865094Slling 			}
2875094Slling 
2885949Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
2895094Slling 
2905094Slling 			if (strval != NULL)
2915094Slling 				kmem_free(strval,
2925094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
2935094Slling 
2945094Slling 			break;
2955094Slling 
2965094Slling 		case 1:
2975094Slling 			/* string property */
2985094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
2995094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
3005094Slling 			    za.za_name, 1, za.za_num_integers, strval);
3015094Slling 			if (err) {
3025094Slling 				kmem_free(strval, za.za_num_integers);
3035094Slling 				break;
3045094Slling 			}
3055949Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
3065094Slling 			kmem_free(strval, za.za_num_integers);
3075094Slling 			break;
3085094Slling 
3095094Slling 		default:
3105094Slling 			break;
3115094Slling 		}
3125094Slling 	}
3135094Slling 	zap_cursor_fini(&zc);
3145094Slling 	mutex_exit(&spa->spa_props_lock);
3155094Slling out:
3165094Slling 	if (err && err != ENOENT) {
3175094Slling 		nvlist_free(*nvp);
3185949Slling 		*nvp = NULL;
3195094Slling 		return (err);
3205094Slling 	}
3215094Slling 
3225094Slling 	return (0);
3235094Slling }
3245094Slling 
3255094Slling /*
3265094Slling  * Validate the given pool properties nvlist and modify the list
3275094Slling  * for the property values to be set.
3285094Slling  */
3295094Slling static int
3305094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
3315094Slling {
3325094Slling 	nvpair_t *elem;
3335094Slling 	int error = 0, reset_bootfs = 0;
3345094Slling 	uint64_t objnum;
3355094Slling 
3365094Slling 	elem = NULL;
3375094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3385094Slling 		zpool_prop_t prop;
3395094Slling 		char *propname, *strval;
3405094Slling 		uint64_t intval;
3415094Slling 		objset_t *os;
3425363Seschrock 		char *slash;
3435094Slling 
3445094Slling 		propname = nvpair_name(elem);
3455094Slling 
3465094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
3475094Slling 			return (EINVAL);
3485094Slling 
3495094Slling 		switch (prop) {
3505094Slling 		case ZPOOL_PROP_VERSION:
3515094Slling 			error = nvpair_value_uint64(elem, &intval);
3525094Slling 			if (!error &&
3535094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
3545094Slling 				error = EINVAL;
3555094Slling 			break;
3565094Slling 
3575094Slling 		case ZPOOL_PROP_DELEGATION:
3585094Slling 		case ZPOOL_PROP_AUTOREPLACE:
3597538SRichard.Morris@Sun.COM 		case ZPOOL_PROP_LISTSNAPS:
3609816SGeorge.Wilson@Sun.COM 		case ZPOOL_PROP_AUTOEXPAND:
3615094Slling 			error = nvpair_value_uint64(elem, &intval);
3625094Slling 			if (!error && intval > 1)
3635094Slling 				error = EINVAL;
3645094Slling 			break;
3655094Slling 
3665094Slling 		case ZPOOL_PROP_BOOTFS:
3679630SJeff.Bonwick@Sun.COM 			/*
3689630SJeff.Bonwick@Sun.COM 			 * If the pool version is less than SPA_VERSION_BOOTFS,
3699630SJeff.Bonwick@Sun.COM 			 * or the pool is still being created (version == 0),
3709630SJeff.Bonwick@Sun.COM 			 * the bootfs property cannot be set.
3719630SJeff.Bonwick@Sun.COM 			 */
3725094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
3735094Slling 				error = ENOTSUP;
3745094Slling 				break;
3755094Slling 			}
3765094Slling 
3775094Slling 			/*
3787042Sgw25295 			 * Make sure the vdev config is bootable
3795094Slling 			 */
3807042Sgw25295 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
3815094Slling 				error = ENOTSUP;
3825094Slling 				break;
3835094Slling 			}
3845094Slling 
3855094Slling 			reset_bootfs = 1;
3865094Slling 
3875094Slling 			error = nvpair_value_string(elem, &strval);
3885094Slling 
3895094Slling 			if (!error) {
3907042Sgw25295 				uint64_t compress;
3917042Sgw25295 
3925094Slling 				if (strval == NULL || strval[0] == '\0') {
3935094Slling 					objnum = zpool_prop_default_numeric(
3945094Slling 					    ZPOOL_PROP_BOOTFS);
3955094Slling 					break;
3965094Slling 				}
3975094Slling 
39810298SMatthew.Ahrens@Sun.COM 				if (error = dmu_objset_hold(strval, FTAG, &os))
3995094Slling 					break;
4007042Sgw25295 
40110298SMatthew.Ahrens@Sun.COM 				/* Must be ZPL and not gzip compressed. */
40210298SMatthew.Ahrens@Sun.COM 
40310298SMatthew.Ahrens@Sun.COM 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
40410298SMatthew.Ahrens@Sun.COM 					error = ENOTSUP;
40510298SMatthew.Ahrens@Sun.COM 				} else if ((error = dsl_prop_get_integer(strval,
4067042Sgw25295 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
4077042Sgw25295 				    &compress, NULL)) == 0 &&
4087042Sgw25295 				    !BOOTFS_COMPRESS_VALID(compress)) {
4097042Sgw25295 					error = ENOTSUP;
4107042Sgw25295 				} else {
4117042Sgw25295 					objnum = dmu_objset_id(os);
4127042Sgw25295 				}
41310298SMatthew.Ahrens@Sun.COM 				dmu_objset_rele(os, FTAG);
4145094Slling 			}
4155094Slling 			break;
4167754SJeff.Bonwick@Sun.COM 
4175329Sgw25295 		case ZPOOL_PROP_FAILUREMODE:
4185329Sgw25295 			error = nvpair_value_uint64(elem, &intval);
4195329Sgw25295 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
4205329Sgw25295 			    intval > ZIO_FAILURE_MODE_PANIC))
4215329Sgw25295 				error = EINVAL;
4225329Sgw25295 
4235329Sgw25295 			/*
4245329Sgw25295 			 * This is a special case which only occurs when
4255329Sgw25295 			 * the pool has completely failed. This allows
4265329Sgw25295 			 * the user to change the in-core failmode property
4275329Sgw25295 			 * without syncing it out to disk (I/Os might
4285329Sgw25295 			 * currently be blocked). We do this by returning
4295329Sgw25295 			 * EIO to the caller (spa_prop_set) to trick it
4305329Sgw25295 			 * into thinking we encountered a property validation
4315329Sgw25295 			 * error.
4325329Sgw25295 			 */
4337754SJeff.Bonwick@Sun.COM 			if (!error && spa_suspended(spa)) {
4345329Sgw25295 				spa->spa_failmode = intval;
4355329Sgw25295 				error = EIO;
4365329Sgw25295 			}
4375329Sgw25295 			break;
4385363Seschrock 
4395363Seschrock 		case ZPOOL_PROP_CACHEFILE:
4405363Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
4415363Seschrock 				break;
4425363Seschrock 
4435363Seschrock 			if (strval[0] == '\0')
4445363Seschrock 				break;
4455363Seschrock 
4465363Seschrock 			if (strcmp(strval, "none") == 0)
4475363Seschrock 				break;
4485363Seschrock 
4495363Seschrock 			if (strval[0] != '/') {
4505363Seschrock 				error = EINVAL;
4515363Seschrock 				break;
4525363Seschrock 			}
4535363Seschrock 
4545363Seschrock 			slash = strrchr(strval, '/');
4555363Seschrock 			ASSERT(slash != NULL);
4565363Seschrock 
4575363Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4585363Seschrock 			    strcmp(slash, "/..") == 0)
4595363Seschrock 				error = EINVAL;
4605363Seschrock 			break;
46110922SJeff.Bonwick@Sun.COM 
46210922SJeff.Bonwick@Sun.COM 		case ZPOOL_PROP_DEDUPDITTO:
46310922SJeff.Bonwick@Sun.COM 			if (spa_version(spa) < SPA_VERSION_DEDUP)
46410922SJeff.Bonwick@Sun.COM 				error = ENOTSUP;
46510922SJeff.Bonwick@Sun.COM 			else
46610922SJeff.Bonwick@Sun.COM 				error = nvpair_value_uint64(elem, &intval);
46710922SJeff.Bonwick@Sun.COM 			if (error == 0 &&
46810922SJeff.Bonwick@Sun.COM 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
46910922SJeff.Bonwick@Sun.COM 				error = EINVAL;
47010922SJeff.Bonwick@Sun.COM 			break;
4715094Slling 		}
4725094Slling 
4735094Slling 		if (error)
4745094Slling 			break;
4755094Slling 	}
4765094Slling 
4775094Slling 	if (!error && reset_bootfs) {
4785094Slling 		error = nvlist_remove(props,
4795094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
4805094Slling 
4815094Slling 		if (!error) {
4825094Slling 			error = nvlist_add_uint64(props,
4835094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
4845094Slling 		}
4855094Slling 	}
4865094Slling 
4875094Slling 	return (error);
4885094Slling }
4895094Slling 
4908525SEric.Schrock@Sun.COM void
4918525SEric.Schrock@Sun.COM spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
4928525SEric.Schrock@Sun.COM {
4938525SEric.Schrock@Sun.COM 	char *cachefile;
4948525SEric.Schrock@Sun.COM 	spa_config_dirent_t *dp;
4958525SEric.Schrock@Sun.COM 
4968525SEric.Schrock@Sun.COM 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
4978525SEric.Schrock@Sun.COM 	    &cachefile) != 0)
4988525SEric.Schrock@Sun.COM 		return;
4998525SEric.Schrock@Sun.COM 
5008525SEric.Schrock@Sun.COM 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
5018525SEric.Schrock@Sun.COM 	    KM_SLEEP);
5028525SEric.Schrock@Sun.COM 
5038525SEric.Schrock@Sun.COM 	if (cachefile[0] == '\0')
5048525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(spa_config_path);
5058525SEric.Schrock@Sun.COM 	else if (strcmp(cachefile, "none") == 0)
5068525SEric.Schrock@Sun.COM 		dp->scd_path = NULL;
5078525SEric.Schrock@Sun.COM 	else
5088525SEric.Schrock@Sun.COM 		dp->scd_path = spa_strdup(cachefile);
5098525SEric.Schrock@Sun.COM 
5108525SEric.Schrock@Sun.COM 	list_insert_head(&spa->spa_config_list, dp);
5118525SEric.Schrock@Sun.COM 	if (need_sync)
5128525SEric.Schrock@Sun.COM 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
5138525SEric.Schrock@Sun.COM }
5148525SEric.Schrock@Sun.COM 
5155094Slling int
5165094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
5175094Slling {
5185094Slling 	int error;
5198525SEric.Schrock@Sun.COM 	nvpair_t *elem;
5208525SEric.Schrock@Sun.COM 	boolean_t need_sync = B_FALSE;
5218525SEric.Schrock@Sun.COM 	zpool_prop_t prop;
5225094Slling 
5235094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
5245094Slling 		return (error);
5255094Slling 
5268525SEric.Schrock@Sun.COM 	elem = NULL;
5278525SEric.Schrock@Sun.COM 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
5288525SEric.Schrock@Sun.COM 		if ((prop = zpool_name_to_prop(
5298525SEric.Schrock@Sun.COM 		    nvpair_name(elem))) == ZPROP_INVAL)
5308525SEric.Schrock@Sun.COM 			return (EINVAL);
5318525SEric.Schrock@Sun.COM 
5328525SEric.Schrock@Sun.COM 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
5338525SEric.Schrock@Sun.COM 			continue;
5348525SEric.Schrock@Sun.COM 
5358525SEric.Schrock@Sun.COM 		need_sync = B_TRUE;
5368525SEric.Schrock@Sun.COM 		break;
5378525SEric.Schrock@Sun.COM 	}
5388525SEric.Schrock@Sun.COM 
5398525SEric.Schrock@Sun.COM 	if (need_sync)
5408525SEric.Schrock@Sun.COM 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
5418525SEric.Schrock@Sun.COM 		    spa, nvp, 3));
5428525SEric.Schrock@Sun.COM 	else
5438525SEric.Schrock@Sun.COM 		return (0);
5445094Slling }
5455094Slling 
5465094Slling /*
5475094Slling  * If the bootfs property value is dsobj, clear it.
5485094Slling  */
5495094Slling void
5505094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
5515094Slling {
5525094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
5535094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
5545094Slling 		    spa->spa_pool_props_object,
5555094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
5565094Slling 		spa->spa_bootfs = 0;
5575094Slling 	}
5585094Slling }
5595094Slling 
560789Sahrens /*
561789Sahrens  * ==========================================================================
562789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
563789Sahrens  * ==========================================================================
564789Sahrens  */
565789Sahrens 
5661544Seschrock static int
5671544Seschrock spa_error_entry_compare(const void *a, const void *b)
5681544Seschrock {
5691544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
5701544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
5711544Seschrock 	int ret;
5721544Seschrock 
5731544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
5741544Seschrock 	    sizeof (zbookmark_t));
5751544Seschrock 
5761544Seschrock 	if (ret < 0)
5771544Seschrock 		return (-1);
5781544Seschrock 	else if (ret > 0)
5791544Seschrock 		return (1);
5801544Seschrock 	else
5811544Seschrock 		return (0);
5821544Seschrock }
5831544Seschrock 
5841544Seschrock /*
5851544Seschrock  * Utility function which retrieves copies of the current logs and
5861544Seschrock  * re-initializes them in the process.
5871544Seschrock  */
5881544Seschrock void
5891544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
5901544Seschrock {
5911544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
5921544Seschrock 
5931544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
5941544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
5951544Seschrock 
5961544Seschrock 	avl_create(&spa->spa_errlist_scrub,
5971544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
5981544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
5991544Seschrock 	avl_create(&spa->spa_errlist_last,
6001544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
6011544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
6021544Seschrock }
6031544Seschrock 
60411173SJonathan.Adams@Sun.COM static taskq_t *
60511173SJonathan.Adams@Sun.COM spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
60611173SJonathan.Adams@Sun.COM     uint_t value)
60711173SJonathan.Adams@Sun.COM {
60811173SJonathan.Adams@Sun.COM 	uint_t flags = TASKQ_PREPOPULATE;
60911173SJonathan.Adams@Sun.COM 	boolean_t batch = B_FALSE;
61011173SJonathan.Adams@Sun.COM 
61111173SJonathan.Adams@Sun.COM 	switch (mode) {
61211173SJonathan.Adams@Sun.COM 	case zti_mode_null:
61311173SJonathan.Adams@Sun.COM 		return (NULL);		/* no taskq needed */
61411173SJonathan.Adams@Sun.COM 
61511173SJonathan.Adams@Sun.COM 	case zti_mode_fixed:
61611173SJonathan.Adams@Sun.COM 		ASSERT3U(value, >=, 1);
61711173SJonathan.Adams@Sun.COM 		value = MAX(value, 1);
61811173SJonathan.Adams@Sun.COM 		break;
61911173SJonathan.Adams@Sun.COM 
62011173SJonathan.Adams@Sun.COM 	case zti_mode_batch:
62111173SJonathan.Adams@Sun.COM 		batch = B_TRUE;
62211173SJonathan.Adams@Sun.COM 		flags |= TASKQ_THREADS_CPU_PCT;
62311173SJonathan.Adams@Sun.COM 		value = zio_taskq_batch_pct;
62411173SJonathan.Adams@Sun.COM 		break;
62511173SJonathan.Adams@Sun.COM 
62611173SJonathan.Adams@Sun.COM 	case zti_mode_online_percent:
62711173SJonathan.Adams@Sun.COM 		flags |= TASKQ_THREADS_CPU_PCT;
62811173SJonathan.Adams@Sun.COM 		break;
62911173SJonathan.Adams@Sun.COM 
63011173SJonathan.Adams@Sun.COM 	default:
63111173SJonathan.Adams@Sun.COM 		panic("unrecognized mode for %s taskq (%u:%u) in "
63211173SJonathan.Adams@Sun.COM 		    "spa_activate()",
63311173SJonathan.Adams@Sun.COM 		    name, mode, value);
63411173SJonathan.Adams@Sun.COM 		break;
63511173SJonathan.Adams@Sun.COM 	}
63611173SJonathan.Adams@Sun.COM 
63711173SJonathan.Adams@Sun.COM 	if (zio_taskq_sysdc && spa->spa_proc != &p0) {
63811173SJonathan.Adams@Sun.COM 		if (batch)
63911173SJonathan.Adams@Sun.COM 			flags |= TASKQ_DC_BATCH;
64011173SJonathan.Adams@Sun.COM 
64111173SJonathan.Adams@Sun.COM 		return (taskq_create_sysdc(name, value, 50, INT_MAX,
64211173SJonathan.Adams@Sun.COM 		    spa->spa_proc, zio_taskq_basedc, flags));
64311173SJonathan.Adams@Sun.COM 	}
64411173SJonathan.Adams@Sun.COM 	return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
64511173SJonathan.Adams@Sun.COM 	    spa->spa_proc, flags));
64611173SJonathan.Adams@Sun.COM }
64711173SJonathan.Adams@Sun.COM 
64811173SJonathan.Adams@Sun.COM static void
64911173SJonathan.Adams@Sun.COM spa_create_zio_taskqs(spa_t *spa)
65011173SJonathan.Adams@Sun.COM {
65111173SJonathan.Adams@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
65211173SJonathan.Adams@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
65311173SJonathan.Adams@Sun.COM 			const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
65411173SJonathan.Adams@Sun.COM 			enum zti_modes mode = ztip->zti_mode;
65511173SJonathan.Adams@Sun.COM 			uint_t value = ztip->zti_value;
65611173SJonathan.Adams@Sun.COM 			char name[32];
65711173SJonathan.Adams@Sun.COM 
65811173SJonathan.Adams@Sun.COM 			(void) snprintf(name, sizeof (name),
65911173SJonathan.Adams@Sun.COM 			    "%s_%s", zio_type_name[t], zio_taskq_types[q]);
66011173SJonathan.Adams@Sun.COM 
66111173SJonathan.Adams@Sun.COM 			spa->spa_zio_taskq[t][q] =
66211173SJonathan.Adams@Sun.COM 			    spa_taskq_create(spa, name, mode, value);
66311173SJonathan.Adams@Sun.COM 		}
66411173SJonathan.Adams@Sun.COM 	}
66511173SJonathan.Adams@Sun.COM }
66611173SJonathan.Adams@Sun.COM 
66711173SJonathan.Adams@Sun.COM #ifdef _KERNEL
66811173SJonathan.Adams@Sun.COM static void
66911173SJonathan.Adams@Sun.COM spa_thread(void *arg)
67011173SJonathan.Adams@Sun.COM {
67111173SJonathan.Adams@Sun.COM 	callb_cpr_t cprinfo;
67211173SJonathan.Adams@Sun.COM 
67311173SJonathan.Adams@Sun.COM 	spa_t *spa = arg;
67411173SJonathan.Adams@Sun.COM 	user_t *pu = PTOU(curproc);
67511173SJonathan.Adams@Sun.COM 
67611173SJonathan.Adams@Sun.COM 	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
67711173SJonathan.Adams@Sun.COM 	    spa->spa_name);
67811173SJonathan.Adams@Sun.COM 
67911173SJonathan.Adams@Sun.COM 	ASSERT(curproc != &p0);
68011173SJonathan.Adams@Sun.COM 	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
68111173SJonathan.Adams@Sun.COM 	    "zpool-%s", spa->spa_name);
68211173SJonathan.Adams@Sun.COM 	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
68311173SJonathan.Adams@Sun.COM 
68411173SJonathan.Adams@Sun.COM 	/* bind this thread to the requested psrset */
68511173SJonathan.Adams@Sun.COM 	if (zio_taskq_psrset_bind != PS_NONE) {
68611173SJonathan.Adams@Sun.COM 		pool_lock();
68711173SJonathan.Adams@Sun.COM 		mutex_enter(&cpu_lock);
68811173SJonathan.Adams@Sun.COM 		mutex_enter(&pidlock);
68911173SJonathan.Adams@Sun.COM 		mutex_enter(&curproc->p_lock);
69011173SJonathan.Adams@Sun.COM 
69111173SJonathan.Adams@Sun.COM 		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
69211173SJonathan.Adams@Sun.COM 		    0, NULL, NULL) == 0)  {
69311173SJonathan.Adams@Sun.COM 			curthread->t_bind_pset = zio_taskq_psrset_bind;
69411173SJonathan.Adams@Sun.COM 		} else {
69511173SJonathan.Adams@Sun.COM 			cmn_err(CE_WARN,
69611173SJonathan.Adams@Sun.COM 			    "Couldn't bind process for zfs pool \"%s\" to "
69711173SJonathan.Adams@Sun.COM 			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
69811173SJonathan.Adams@Sun.COM 		}
69911173SJonathan.Adams@Sun.COM 
70011173SJonathan.Adams@Sun.COM 		mutex_exit(&curproc->p_lock);
70111173SJonathan.Adams@Sun.COM 		mutex_exit(&pidlock);
70211173SJonathan.Adams@Sun.COM 		mutex_exit(&cpu_lock);
70311173SJonathan.Adams@Sun.COM 		pool_unlock();
70411173SJonathan.Adams@Sun.COM 	}
70511173SJonathan.Adams@Sun.COM 
70611173SJonathan.Adams@Sun.COM 	if (zio_taskq_sysdc) {
70711173SJonathan.Adams@Sun.COM 		sysdc_thread_enter(curthread, 100, 0);
70811173SJonathan.Adams@Sun.COM 	}
70911173SJonathan.Adams@Sun.COM 
71011173SJonathan.Adams@Sun.COM 	spa->spa_proc = curproc;
71111173SJonathan.Adams@Sun.COM 	spa->spa_did = curthread->t_did;
71211173SJonathan.Adams@Sun.COM 
71311173SJonathan.Adams@Sun.COM 	spa_create_zio_taskqs(spa);
71411173SJonathan.Adams@Sun.COM 
71511173SJonathan.Adams@Sun.COM 	mutex_enter(&spa->spa_proc_lock);
71611173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
71711173SJonathan.Adams@Sun.COM 
71811173SJonathan.Adams@Sun.COM 	spa->spa_proc_state = SPA_PROC_ACTIVE;
71911173SJonathan.Adams@Sun.COM 	cv_broadcast(&spa->spa_proc_cv);
72011173SJonathan.Adams@Sun.COM 
72111173SJonathan.Adams@Sun.COM 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
72211173SJonathan.Adams@Sun.COM 	while (spa->spa_proc_state == SPA_PROC_ACTIVE)
72311173SJonathan.Adams@Sun.COM 		cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
72411173SJonathan.Adams@Sun.COM 	CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
72511173SJonathan.Adams@Sun.COM 
72611173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
72711173SJonathan.Adams@Sun.COM 	spa->spa_proc_state = SPA_PROC_GONE;
72811173SJonathan.Adams@Sun.COM 	spa->spa_proc = &p0;
72911173SJonathan.Adams@Sun.COM 	cv_broadcast(&spa->spa_proc_cv);
73011173SJonathan.Adams@Sun.COM 	CALLB_CPR_EXIT(&cprinfo);	/* drops spa_proc_lock */
73111173SJonathan.Adams@Sun.COM 
73211173SJonathan.Adams@Sun.COM 	mutex_enter(&curproc->p_lock);
73311173SJonathan.Adams@Sun.COM 	lwp_exit();
73411173SJonathan.Adams@Sun.COM }
73511173SJonathan.Adams@Sun.COM #endif
73611173SJonathan.Adams@Sun.COM 
737789Sahrens /*
738789Sahrens  * Activate an uninitialized pool.
739789Sahrens  */
740789Sahrens static void
7418241SJeff.Bonwick@Sun.COM spa_activate(spa_t *spa, int mode)
742789Sahrens {
743789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
744789Sahrens 
745789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
7468241SJeff.Bonwick@Sun.COM 	spa->spa_mode = mode;
747789Sahrens 
74810594SGeorge.Wilson@Sun.COM 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
74910594SGeorge.Wilson@Sun.COM 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
750789Sahrens 
75111173SJonathan.Adams@Sun.COM 	/* Try to create a covering process */
75211173SJonathan.Adams@Sun.COM 	mutex_enter(&spa->spa_proc_lock);
75311173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
75411173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc == &p0);
75511173SJonathan.Adams@Sun.COM 	spa->spa_did = 0;
75611173SJonathan.Adams@Sun.COM 
75711173SJonathan.Adams@Sun.COM 	/* Only create a process if we're going to be around a while. */
75811173SJonathan.Adams@Sun.COM 	if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
75911173SJonathan.Adams@Sun.COM 		if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
76011173SJonathan.Adams@Sun.COM 		    NULL, 0) == 0) {
76111173SJonathan.Adams@Sun.COM 			spa->spa_proc_state = SPA_PROC_CREATED;
76211173SJonathan.Adams@Sun.COM 			while (spa->spa_proc_state == SPA_PROC_CREATED) {
76311173SJonathan.Adams@Sun.COM 				cv_wait(&spa->spa_proc_cv,
76411173SJonathan.Adams@Sun.COM 				    &spa->spa_proc_lock);
7659515SJonathan.Adams@Sun.COM 			}
76611173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
76711173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_proc != &p0);
76811173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_did != 0);
76911173SJonathan.Adams@Sun.COM 		} else {
77011173SJonathan.Adams@Sun.COM #ifdef _KERNEL
77111173SJonathan.Adams@Sun.COM 			cmn_err(CE_WARN,
77211173SJonathan.Adams@Sun.COM 			    "Couldn't create process for zfs pool \"%s\"\n",
77311173SJonathan.Adams@Sun.COM 			    spa->spa_name);
77411173SJonathan.Adams@Sun.COM #endif
7757754SJeff.Bonwick@Sun.COM 		}
776789Sahrens 	}
77711173SJonathan.Adams@Sun.COM 	mutex_exit(&spa->spa_proc_lock);
77811173SJonathan.Adams@Sun.COM 
77911173SJonathan.Adams@Sun.COM 	/* If we didn't create a process, we need to create our taskqs. */
78011173SJonathan.Adams@Sun.COM 	if (spa->spa_proc == &p0) {
78111173SJonathan.Adams@Sun.COM 		spa_create_zio_taskqs(spa);
78211173SJonathan.Adams@Sun.COM 	}
783789Sahrens 
7847754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
7857754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_config_dirty_node));
7867754SJeff.Bonwick@Sun.COM 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
7877754SJeff.Bonwick@Sun.COM 	    offsetof(vdev_t, vdev_state_dirty_node));
788789Sahrens 
789789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
790789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
7911544Seschrock 
7921544Seschrock 	avl_create(&spa->spa_errlist_scrub,
7931544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
7941544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
7951544Seschrock 	avl_create(&spa->spa_errlist_last,
7961544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
7971544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
798789Sahrens }
799789Sahrens 
800789Sahrens /*
801789Sahrens  * Opposite of spa_activate().
802789Sahrens  */
803789Sahrens static void
804789Sahrens spa_deactivate(spa_t *spa)
805789Sahrens {
806789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
807789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
808789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
8099630SJeff.Bonwick@Sun.COM 	ASSERT(spa->spa_async_zio_root == NULL);
810789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
811789Sahrens 
812789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
813789Sahrens 
8147754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_config_dirty_list);
8157754SJeff.Bonwick@Sun.COM 	list_destroy(&spa->spa_state_dirty_list);
8167754SJeff.Bonwick@Sun.COM 
8177754SJeff.Bonwick@Sun.COM 	for (int t = 0; t < ZIO_TYPES; t++) {
8187754SJeff.Bonwick@Sun.COM 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
81911146SGeorge.Wilson@Sun.COM 			if (spa->spa_zio_taskq[t][q] != NULL)
82011146SGeorge.Wilson@Sun.COM 				taskq_destroy(spa->spa_zio_taskq[t][q]);
8217754SJeff.Bonwick@Sun.COM 			spa->spa_zio_taskq[t][q] = NULL;
8227754SJeff.Bonwick@Sun.COM 		}
823789Sahrens 	}
824789Sahrens 
825789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
826789Sahrens 	spa->spa_normal_class = NULL;
827789Sahrens 
8284527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
8294527Sperrin 	spa->spa_log_class = NULL;
8304527Sperrin 
8311544Seschrock 	/*
8321544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
8331544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
8341544Seschrock 	 */
8351544Seschrock 	spa_errlog_drain(spa);
8361544Seschrock 
8371544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
8381544Seschrock 	avl_destroy(&spa->spa_errlist_last);
8391544Seschrock 
840789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
84111173SJonathan.Adams@Sun.COM 
84211173SJonathan.Adams@Sun.COM 	mutex_enter(&spa->spa_proc_lock);
84311173SJonathan.Adams@Sun.COM 	if (spa->spa_proc_state != SPA_PROC_NONE) {
84411173SJonathan.Adams@Sun.COM 		ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
84511173SJonathan.Adams@Sun.COM 		spa->spa_proc_state = SPA_PROC_DEACTIVATE;
84611173SJonathan.Adams@Sun.COM 		cv_broadcast(&spa->spa_proc_cv);
84711173SJonathan.Adams@Sun.COM 		while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
84811173SJonathan.Adams@Sun.COM 			ASSERT(spa->spa_proc != &p0);
84911173SJonathan.Adams@Sun.COM 			cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
85011173SJonathan.Adams@Sun.COM 		}
85111173SJonathan.Adams@Sun.COM 		ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
85211173SJonathan.Adams@Sun.COM 		spa->spa_proc_state = SPA_PROC_NONE;
85311173SJonathan.Adams@Sun.COM 	}
85411173SJonathan.Adams@Sun.COM 	ASSERT(spa->spa_proc == &p0);
85511173SJonathan.Adams@Sun.COM 	mutex_exit(&spa->spa_proc_lock);
85611173SJonathan.Adams@Sun.COM 
85711173SJonathan.Adams@Sun.COM 	/*
85811173SJonathan.Adams@Sun.COM 	 * We want to make sure spa_thread() has actually exited the ZFS
85911173SJonathan.Adams@Sun.COM 	 * module, so that the module can't be unloaded out from underneath
86011173SJonathan.Adams@Sun.COM 	 * it.
86111173SJonathan.Adams@Sun.COM 	 */
86211173SJonathan.Adams@Sun.COM 	if (spa->spa_did != 0) {
86311173SJonathan.Adams@Sun.COM 		thread_join(spa->spa_did);
86411173SJonathan.Adams@Sun.COM 		spa->spa_did = 0;
86511173SJonathan.Adams@Sun.COM 	}
866789Sahrens }
867789Sahrens 
868789Sahrens /*
869789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
870789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
871789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
872789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
873789Sahrens  */
8742082Seschrock static int
8752082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
8762082Seschrock     uint_t id, int atype)
877789Sahrens {
878789Sahrens 	nvlist_t **child;
8799816SGeorge.Wilson@Sun.COM 	uint_t children;
8802082Seschrock 	int error;
8812082Seschrock 
8822082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
8832082Seschrock 		return (error);
8842082Seschrock 
8852082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
8862082Seschrock 		return (0);
887789Sahrens 
8887754SJeff.Bonwick@Sun.COM 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
8897754SJeff.Bonwick@Sun.COM 	    &child, &children);
8907754SJeff.Bonwick@Sun.COM 
8917754SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
8927754SJeff.Bonwick@Sun.COM 		return (0);
8937754SJeff.Bonwick@Sun.COM 
8947754SJeff.Bonwick@Sun.COM 	if (error) {
8952082Seschrock 		vdev_free(*vdp);
8962082Seschrock 		*vdp = NULL;
8972082Seschrock 		return (EINVAL);
898789Sahrens 	}
899789Sahrens 
9009816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < children; c++) {
9012082Seschrock 		vdev_t *vd;
9022082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
9032082Seschrock 		    atype)) != 0) {
9042082Seschrock 			vdev_free(*vdp);
9052082Seschrock 			*vdp = NULL;
9062082Seschrock 			return (error);
907789Sahrens 		}
908789Sahrens 	}
909789Sahrens 
9102082Seschrock 	ASSERT(*vdp != NULL);
9112082Seschrock 
9122082Seschrock 	return (0);
913789Sahrens }
914789Sahrens 
915789Sahrens /*
916789Sahrens  * Opposite of spa_load().
917789Sahrens  */
918789Sahrens static void
919789Sahrens spa_unload(spa_t *spa)
920789Sahrens {
9212082Seschrock 	int i;
9222082Seschrock 
9237754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
9247754SJeff.Bonwick@Sun.COM 
925789Sahrens 	/*
9261544Seschrock 	 * Stop async tasks.
9271544Seschrock 	 */
9281544Seschrock 	spa_async_suspend(spa);
9291544Seschrock 
9301544Seschrock 	/*
931789Sahrens 	 * Stop syncing.
932789Sahrens 	 */
933789Sahrens 	if (spa->spa_sync_on) {
934789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
935789Sahrens 		spa->spa_sync_on = B_FALSE;
936789Sahrens 	}
937789Sahrens 
938789Sahrens 	/*
9397754SJeff.Bonwick@Sun.COM 	 * Wait for any outstanding async I/O to complete.
940789Sahrens 	 */
9419234SGeorge.Wilson@Sun.COM 	if (spa->spa_async_zio_root != NULL) {
9429234SGeorge.Wilson@Sun.COM 		(void) zio_wait(spa->spa_async_zio_root);
9439234SGeorge.Wilson@Sun.COM 		spa->spa_async_zio_root = NULL;
9449234SGeorge.Wilson@Sun.COM 	}
945789Sahrens 
94612470SMatthew.Ahrens@Sun.COM 	bpobj_close(&spa->spa_deferred_bpobj);
94712470SMatthew.Ahrens@Sun.COM 
948789Sahrens 	/*
949789Sahrens 	 * Close the dsl pool.
950789Sahrens 	 */
951789Sahrens 	if (spa->spa_dsl_pool) {
952789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
953789Sahrens 		spa->spa_dsl_pool = NULL;
95411619SGeorge.Wilson@Sun.COM 		spa->spa_meta_objset = NULL;
955789Sahrens 	}
956789Sahrens 
95710922SJeff.Bonwick@Sun.COM 	ddt_unload(spa);
95810922SJeff.Bonwick@Sun.COM 
9598241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9608241SJeff.Bonwick@Sun.COM 
9618241SJeff.Bonwick@Sun.COM 	/*
9628241SJeff.Bonwick@Sun.COM 	 * Drop and purge level 2 cache
9638241SJeff.Bonwick@Sun.COM 	 */
9648241SJeff.Bonwick@Sun.COM 	spa_l2cache_drop(spa);
9658241SJeff.Bonwick@Sun.COM 
966789Sahrens 	/*
967789Sahrens 	 * Close all vdevs.
968789Sahrens 	 */
9691585Sbonwick 	if (spa->spa_root_vdev)
970789Sahrens 		vdev_free(spa->spa_root_vdev);
9711585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
9721544Seschrock 
9735450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
9745450Sbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
9755450Sbrendan 	if (spa->spa_spares.sav_vdevs) {
9765450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
9775450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
9785450Sbrendan 		spa->spa_spares.sav_vdevs = NULL;
9795450Sbrendan 	}
9805450Sbrendan 	if (spa->spa_spares.sav_config) {
9815450Sbrendan 		nvlist_free(spa->spa_spares.sav_config);
9825450Sbrendan 		spa->spa_spares.sav_config = NULL;
9832082Seschrock 	}
9847377SEric.Schrock@Sun.COM 	spa->spa_spares.sav_count = 0;
9855450Sbrendan 
9865450Sbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
9875450Sbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
9885450Sbrendan 	if (spa->spa_l2cache.sav_vdevs) {
9895450Sbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
9905450Sbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
9915450Sbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
9925450Sbrendan 	}
9935450Sbrendan 	if (spa->spa_l2cache.sav_config) {
9945450Sbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
9955450Sbrendan 		spa->spa_l2cache.sav_config = NULL;
9962082Seschrock 	}
9977377SEric.Schrock@Sun.COM 	spa->spa_l2cache.sav_count = 0;
9982082Seschrock 
9991544Seschrock 	spa->spa_async_suspended = 0;
10008241SJeff.Bonwick@Sun.COM 
10018241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1002789Sahrens }
1003789Sahrens 
1004789Sahrens /*
10052082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
10062082Seschrock  * this pool.  When this is called, we have some form of basic information in
10075450Sbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
10085450Sbrendan  * then re-generate a more complete list including status information.
10092082Seschrock  */
10102082Seschrock static void
10112082Seschrock spa_load_spares(spa_t *spa)
10122082Seschrock {
10132082Seschrock 	nvlist_t **spares;
10142082Seschrock 	uint_t nspares;
10152082Seschrock 	int i;
10163377Seschrock 	vdev_t *vd, *tvd;
10172082Seschrock 
10187754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
10197754SJeff.Bonwick@Sun.COM 
10202082Seschrock 	/*
10212082Seschrock 	 * First, close and free any existing spare vdevs.
10222082Seschrock 	 */
10235450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10245450Sbrendan 		vd = spa->spa_spares.sav_vdevs[i];
10253377Seschrock 
10263377Seschrock 		/* Undo the call to spa_activate() below */
10276643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10286643Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
10293377Seschrock 			spa_spare_remove(tvd);
10303377Seschrock 		vdev_close(vd);
10313377Seschrock 		vdev_free(vd);
10322082Seschrock 	}
10333377Seschrock 
10345450Sbrendan 	if (spa->spa_spares.sav_vdevs)
10355450Sbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
10365450Sbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
10375450Sbrendan 
10385450Sbrendan 	if (spa->spa_spares.sav_config == NULL)
10392082Seschrock 		nspares = 0;
10402082Seschrock 	else
10415450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
10422082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
10432082Seschrock 
10445450Sbrendan 	spa->spa_spares.sav_count = (int)nspares;
10455450Sbrendan 	spa->spa_spares.sav_vdevs = NULL;
10462082Seschrock 
10472082Seschrock 	if (nspares == 0)
10482082Seschrock 		return;
10492082Seschrock 
10502082Seschrock 	/*
10512082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
10523377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
10533377Seschrock 	 * structures associated with it: one in the list of spares (used only
10543377Seschrock 	 * for basic validation purposes) and one in the active vdev
10553377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
10563377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
10573377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
10582082Seschrock 	 */
10595450Sbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
10605450Sbrendan 	    KM_SLEEP);
10615450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
10622082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
10632082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
10642082Seschrock 		ASSERT(vd != NULL);
10652082Seschrock 
10665450Sbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
10672082Seschrock 
10686643Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
10696643Seschrock 		    B_FALSE)) != NULL) {
10703377Seschrock 			if (!tvd->vdev_isspare)
10713377Seschrock 				spa_spare_add(tvd);
10723377Seschrock 
10733377Seschrock 			/*
10743377Seschrock 			 * We only mark the spare active if we were successfully
10753377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
10763377Seschrock 			 * with a bad active spare would result in strange
10773377Seschrock 			 * behavior, because multiple pool would think the spare
10783377Seschrock 			 * is actively in use.
10793377Seschrock 			 *
10803377Seschrock 			 * There is a vulnerability here to an equally bizarre
10813377Seschrock 			 * circumstance, where a dead active spare is later
10823377Seschrock 			 * brought back to life (onlined or otherwise).  Given
10833377Seschrock 			 * the rarity of this scenario, and the extra complexity
10843377Seschrock 			 * it adds, we ignore the possibility.
10853377Seschrock 			 */
10863377Seschrock 			if (!vdev_is_dead(tvd))
10873377Seschrock 				spa_spare_activate(tvd);
10883377Seschrock 		}
10893377Seschrock 
10907754SJeff.Bonwick@Sun.COM 		vd->vdev_top = vd;
10919425SEric.Schrock@Sun.COM 		vd->vdev_aux = &spa->spa_spares;
10927754SJeff.Bonwick@Sun.COM 
10932082Seschrock 		if (vdev_open(vd) != 0)
10942082Seschrock 			continue;
10952082Seschrock 
10965450Sbrendan 		if (vdev_validate_aux(vd) == 0)
10975450Sbrendan 			spa_spare_add(vd);
10982082Seschrock 	}
10992082Seschrock 
11002082Seschrock 	/*
11012082Seschrock 	 * Recompute the stashed list of spares, with status information
11022082Seschrock 	 * this time.
11032082Seschrock 	 */
11045450Sbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
11052082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
11062082Seschrock 
11075450Sbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
11085450Sbrendan 	    KM_SLEEP);
11095450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11105450Sbrendan 		spares[i] = vdev_config_generate(spa,
111112296SLin.Ling@Sun.COM 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
11125450Sbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
11135450Sbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
11145450Sbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
11152082Seschrock 		nvlist_free(spares[i]);
11165450Sbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
11175450Sbrendan }
11185450Sbrendan 
11195450Sbrendan /*
11205450Sbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
11215450Sbrendan  * this pool.  When this is called, we have some form of basic information in
11225450Sbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
11235450Sbrendan  * then re-generate a more complete list including status information.
11245450Sbrendan  * Devices which are already active have their details maintained, and are
11255450Sbrendan  * not re-opened.
11265450Sbrendan  */
11275450Sbrendan static void
11285450Sbrendan spa_load_l2cache(spa_t *spa)
11295450Sbrendan {
11305450Sbrendan 	nvlist_t **l2cache;
11315450Sbrendan 	uint_t nl2cache;
11325450Sbrendan 	int i, j, oldnvdevs;
11339816SGeorge.Wilson@Sun.COM 	uint64_t guid;
11345450Sbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
11355450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
11365450Sbrendan 
11377754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
11387754SJeff.Bonwick@Sun.COM 
11395450Sbrendan 	if (sav->sav_config != NULL) {
11405450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
11415450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
11425450Sbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
11435450Sbrendan 	} else {
11445450Sbrendan 		nl2cache = 0;
11455450Sbrendan 	}
11465450Sbrendan 
11475450Sbrendan 	oldvdevs = sav->sav_vdevs;
11485450Sbrendan 	oldnvdevs = sav->sav_count;
11495450Sbrendan 	sav->sav_vdevs = NULL;
11505450Sbrendan 	sav->sav_count = 0;
11515450Sbrendan 
11525450Sbrendan 	/*
11535450Sbrendan 	 * Process new nvlist of vdevs.
11545450Sbrendan 	 */
11555450Sbrendan 	for (i = 0; i < nl2cache; i++) {
11565450Sbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
11575450Sbrendan 		    &guid) == 0);
11585450Sbrendan 
11595450Sbrendan 		newvdevs[i] = NULL;
11605450Sbrendan 		for (j = 0; j < oldnvdevs; j++) {
11615450Sbrendan 			vd = oldvdevs[j];
11625450Sbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
11635450Sbrendan 				/*
11645450Sbrendan 				 * Retain previous vdev for add/remove ops.
11655450Sbrendan 				 */
11665450Sbrendan 				newvdevs[i] = vd;
11675450Sbrendan 				oldvdevs[j] = NULL;
11685450Sbrendan 				break;
11695450Sbrendan 			}
11705450Sbrendan 		}
11715450Sbrendan 
11725450Sbrendan 		if (newvdevs[i] == NULL) {
11735450Sbrendan 			/*
11745450Sbrendan 			 * Create new vdev
11755450Sbrendan 			 */
11765450Sbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
11775450Sbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
11785450Sbrendan 			ASSERT(vd != NULL);
11795450Sbrendan 			newvdevs[i] = vd;
11805450Sbrendan 
11815450Sbrendan 			/*
11825450Sbrendan 			 * Commit this vdev as an l2cache device,
11835450Sbrendan 			 * even if it fails to open.
11845450Sbrendan 			 */
11855450Sbrendan 			spa_l2cache_add(vd);
11865450Sbrendan 
11876643Seschrock 			vd->vdev_top = vd;
11886643Seschrock 			vd->vdev_aux = sav;
11896643Seschrock 
11906643Seschrock 			spa_l2cache_activate(vd);
11916643Seschrock 
11925450Sbrendan 			if (vdev_open(vd) != 0)
11935450Sbrendan 				continue;
11945450Sbrendan 
11955450Sbrendan 			(void) vdev_validate_aux(vd);
11965450Sbrendan 
11979816SGeorge.Wilson@Sun.COM 			if (!vdev_is_dead(vd))
11989816SGeorge.Wilson@Sun.COM 				l2arc_add_vdev(spa, vd);
11995450Sbrendan 		}
12005450Sbrendan 	}
12015450Sbrendan 
12025450Sbrendan 	/*
12035450Sbrendan 	 * Purge vdevs that were dropped
12045450Sbrendan 	 */
12055450Sbrendan 	for (i = 0; i < oldnvdevs; i++) {
12065450Sbrendan 		uint64_t pool;
12075450Sbrendan 
12085450Sbrendan 		vd = oldvdevs[i];
12095450Sbrendan 		if (vd != NULL) {
12108241SJeff.Bonwick@Sun.COM 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
12118241SJeff.Bonwick@Sun.COM 			    pool != 0ULL && l2arc_vdev_present(vd))
12125450Sbrendan 				l2arc_remove_vdev(vd);
12135450Sbrendan 			(void) vdev_close(vd);
12145450Sbrendan 			spa_l2cache_remove(vd);
12155450Sbrendan 		}
12165450Sbrendan 	}
12175450Sbrendan 
12185450Sbrendan 	if (oldvdevs)
12195450Sbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
12205450Sbrendan 
12215450Sbrendan 	if (sav->sav_config == NULL)
12225450Sbrendan 		goto out;
12235450Sbrendan 
12245450Sbrendan 	sav->sav_vdevs = newvdevs;
12255450Sbrendan 	sav->sav_count = (int)nl2cache;
12265450Sbrendan 
12275450Sbrendan 	/*
12285450Sbrendan 	 * Recompute the stashed list of l2cache devices, with status
12295450Sbrendan 	 * information this time.
12305450Sbrendan 	 */
12315450Sbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
12325450Sbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
12335450Sbrendan 
12345450Sbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
12355450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12365450Sbrendan 		l2cache[i] = vdev_config_generate(spa,
123712296SLin.Ling@Sun.COM 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
12385450Sbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
12395450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
12405450Sbrendan out:
12415450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
12425450Sbrendan 		nvlist_free(l2cache[i]);
12435450Sbrendan 	if (sav->sav_count)
12445450Sbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
12452082Seschrock }
12462082Seschrock 
12472082Seschrock static int
12482082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
12492082Seschrock {
12502082Seschrock 	dmu_buf_t *db;
12512082Seschrock 	char *packed = NULL;
12522082Seschrock 	size_t nvsize = 0;
12532082Seschrock 	int error;
12542082Seschrock 	*value = NULL;
12552082Seschrock 
12562082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
12572082Seschrock 	nvsize = *(uint64_t *)db->db_data;
12582082Seschrock 	dmu_buf_rele(db, FTAG);
12592082Seschrock 
12602082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
12619512SNeil.Perrin@Sun.COM 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
12629512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
12632082Seschrock 	if (error == 0)
12642082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
12652082Seschrock 	kmem_free(packed, nvsize);
12662082Seschrock 
12672082Seschrock 	return (error);
12682082Seschrock }
12692082Seschrock 
12702082Seschrock /*
12714451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
12724451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
12734451Seschrock  */
12744451Seschrock static void
12754451Seschrock spa_check_removed(vdev_t *vd)
12764451Seschrock {
12779816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
12784451Seschrock 		spa_check_removed(vd->vdev_child[c]);
12794451Seschrock 
12804451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
12814451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
12824451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
12834451Seschrock 	}
12844451Seschrock }
12854451Seschrock 
12864451Seschrock /*
12879701SGeorge.Wilson@Sun.COM  * Load the slog device state from the config object since it's possible
12889701SGeorge.Wilson@Sun.COM  * that the label does not contain the most up-to-date information.
12899701SGeorge.Wilson@Sun.COM  */
12909701SGeorge.Wilson@Sun.COM void
129110594SGeorge.Wilson@Sun.COM spa_load_log_state(spa_t *spa, nvlist_t *nv)
12929701SGeorge.Wilson@Sun.COM {
129310594SGeorge.Wilson@Sun.COM 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
129410594SGeorge.Wilson@Sun.COM 
129510594SGeorge.Wilson@Sun.COM 	/*
129610594SGeorge.Wilson@Sun.COM 	 * Load the original root vdev tree from the passed config.
129710594SGeorge.Wilson@Sun.COM 	 */
129810594SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
129910594SGeorge.Wilson@Sun.COM 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
130010594SGeorge.Wilson@Sun.COM 
130110594SGeorge.Wilson@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
130210594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
130310594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_islog)
130410594SGeorge.Wilson@Sun.COM 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
13059701SGeorge.Wilson@Sun.COM 	}
130610594SGeorge.Wilson@Sun.COM 	vdev_free(ovd);
130710594SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
13089701SGeorge.Wilson@Sun.COM }
13099701SGeorge.Wilson@Sun.COM 
13109701SGeorge.Wilson@Sun.COM /*
13117294Sperrin  * Check for missing log devices
13127294Sperrin  */
13137294Sperrin int
13147294Sperrin spa_check_logs(spa_t *spa)
13157294Sperrin {
13167294Sperrin 	switch (spa->spa_log_state) {
13177294Sperrin 	case SPA_LOG_MISSING:
13187294Sperrin 		/* need to recheck in case slog has been restored */
13197294Sperrin 	case SPA_LOG_UNKNOWN:
13207294Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
13217294Sperrin 		    DS_FIND_CHILDREN)) {
132211422SMark.Musante@Sun.COM 			spa_set_log_state(spa, SPA_LOG_MISSING);
13237294Sperrin 			return (1);
13247294Sperrin 		}
13257294Sperrin 		break;
13267294Sperrin 	}
13277294Sperrin 	return (0);
13287294Sperrin }
13297294Sperrin 
133011422SMark.Musante@Sun.COM static boolean_t
133111422SMark.Musante@Sun.COM spa_passivate_log(spa_t *spa)
133211422SMark.Musante@Sun.COM {
133311422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
133411422SMark.Musante@Sun.COM 	boolean_t slog_found = B_FALSE;
133511422SMark.Musante@Sun.COM 
133611422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
133711422SMark.Musante@Sun.COM 
133811422SMark.Musante@Sun.COM 	if (!spa_has_slogs(spa))
133911422SMark.Musante@Sun.COM 		return (B_FALSE);
134011422SMark.Musante@Sun.COM 
134111422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
134211422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
134311422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
134411422SMark.Musante@Sun.COM 
134511422SMark.Musante@Sun.COM 		if (tvd->vdev_islog) {
134611422SMark.Musante@Sun.COM 			metaslab_group_passivate(mg);
134711422SMark.Musante@Sun.COM 			slog_found = B_TRUE;
134811422SMark.Musante@Sun.COM 		}
134911422SMark.Musante@Sun.COM 	}
135011422SMark.Musante@Sun.COM 
135111422SMark.Musante@Sun.COM 	return (slog_found);
135211422SMark.Musante@Sun.COM }
135311422SMark.Musante@Sun.COM 
135411422SMark.Musante@Sun.COM static void
135511422SMark.Musante@Sun.COM spa_activate_log(spa_t *spa)
135611422SMark.Musante@Sun.COM {
135711422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
135811422SMark.Musante@Sun.COM 
135911422SMark.Musante@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
136011422SMark.Musante@Sun.COM 
136111422SMark.Musante@Sun.COM 	for (int c = 0; c < rvd->vdev_children; c++) {
136211422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
136311422SMark.Musante@Sun.COM 		metaslab_group_t *mg = tvd->vdev_mg;
136411422SMark.Musante@Sun.COM 
136511422SMark.Musante@Sun.COM 		if (tvd->vdev_islog)
136611422SMark.Musante@Sun.COM 			metaslab_group_activate(mg);
136711422SMark.Musante@Sun.COM 	}
136811422SMark.Musante@Sun.COM }
136911422SMark.Musante@Sun.COM 
137011422SMark.Musante@Sun.COM int
137111422SMark.Musante@Sun.COM spa_offline_log(spa_t *spa)
137211422SMark.Musante@Sun.COM {
137311422SMark.Musante@Sun.COM 	int error = 0;
137411422SMark.Musante@Sun.COM 
137511422SMark.Musante@Sun.COM 	if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
137611422SMark.Musante@Sun.COM 	    NULL, DS_FIND_CHILDREN)) == 0) {
137711422SMark.Musante@Sun.COM 
137811422SMark.Musante@Sun.COM 		/*
137911422SMark.Musante@Sun.COM 		 * We successfully offlined the log device, sync out the
138011422SMark.Musante@Sun.COM 		 * current txg so that the "stubby" block can be removed
138111422SMark.Musante@Sun.COM 		 * by zil_sync().
138211422SMark.Musante@Sun.COM 		 */
138311422SMark.Musante@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, 0);
138411422SMark.Musante@Sun.COM 	}
138511422SMark.Musante@Sun.COM 	return (error);
138611422SMark.Musante@Sun.COM }
138711422SMark.Musante@Sun.COM 
138810672SEric.Schrock@Sun.COM static void
138910672SEric.Schrock@Sun.COM spa_aux_check_removed(spa_aux_vdev_t *sav)
139010672SEric.Schrock@Sun.COM {
139110922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < sav->sav_count; i++)
139210672SEric.Schrock@Sun.COM 		spa_check_removed(sav->sav_vdevs[i]);
139310672SEric.Schrock@Sun.COM }
139410672SEric.Schrock@Sun.COM 
139510922SJeff.Bonwick@Sun.COM void
139610922SJeff.Bonwick@Sun.COM spa_claim_notify(zio_t *zio)
139710922SJeff.Bonwick@Sun.COM {
139810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zio->io_spa;
139910922SJeff.Bonwick@Sun.COM 
140010922SJeff.Bonwick@Sun.COM 	if (zio->io_error)
140110922SJeff.Bonwick@Sun.COM 		return;
140210922SJeff.Bonwick@Sun.COM 
140310922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
140410922SJeff.Bonwick@Sun.COM 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
140510922SJeff.Bonwick@Sun.COM 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
140610922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
140710922SJeff.Bonwick@Sun.COM }
140810922SJeff.Bonwick@Sun.COM 
140910921STim.Haley@Sun.COM typedef struct spa_load_error {
141011727SVictor.Latushkin@Sun.COM 	uint64_t	sle_meta_count;
141110921STim.Haley@Sun.COM 	uint64_t	sle_data_count;
141210921STim.Haley@Sun.COM } spa_load_error_t;
141310921STim.Haley@Sun.COM 
141410921STim.Haley@Sun.COM static void
141510921STim.Haley@Sun.COM spa_load_verify_done(zio_t *zio)
141610921STim.Haley@Sun.COM {
141710921STim.Haley@Sun.COM 	blkptr_t *bp = zio->io_bp;
141810921STim.Haley@Sun.COM 	spa_load_error_t *sle = zio->io_private;
141910921STim.Haley@Sun.COM 	dmu_object_type_t type = BP_GET_TYPE(bp);
142010921STim.Haley@Sun.COM 	int error = zio->io_error;
142110921STim.Haley@Sun.COM 
142210921STim.Haley@Sun.COM 	if (error) {
142310921STim.Haley@Sun.COM 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
142410921STim.Haley@Sun.COM 		    type != DMU_OT_INTENT_LOG)
142511727SVictor.Latushkin@Sun.COM 			atomic_add_64(&sle->sle_meta_count, 1);
142610921STim.Haley@Sun.COM 		else
142710921STim.Haley@Sun.COM 			atomic_add_64(&sle->sle_data_count, 1);
142810921STim.Haley@Sun.COM 	}
142910921STim.Haley@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
143010921STim.Haley@Sun.COM }
143110921STim.Haley@Sun.COM 
143210921STim.Haley@Sun.COM /*ARGSUSED*/
143310921STim.Haley@Sun.COM static int
143410922SJeff.Bonwick@Sun.COM spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
143512296SLin.Ling@Sun.COM     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
143610921STim.Haley@Sun.COM {
143710921STim.Haley@Sun.COM 	if (bp != NULL) {
143810921STim.Haley@Sun.COM 		zio_t *rio = arg;
143910921STim.Haley@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
144010921STim.Haley@Sun.COM 		void *data = zio_data_buf_alloc(size);
144110921STim.Haley@Sun.COM 
144210921STim.Haley@Sun.COM 		zio_nowait(zio_read(rio, spa, bp, data, size,
144310921STim.Haley@Sun.COM 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
144410921STim.Haley@Sun.COM 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
144510921STim.Haley@Sun.COM 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
144610921STim.Haley@Sun.COM 	}
144710921STim.Haley@Sun.COM 	return (0);
144810921STim.Haley@Sun.COM }
144910921STim.Haley@Sun.COM 
145010921STim.Haley@Sun.COM static int
145110921STim.Haley@Sun.COM spa_load_verify(spa_t *spa)
145210921STim.Haley@Sun.COM {
145310921STim.Haley@Sun.COM 	zio_t *rio;
145410921STim.Haley@Sun.COM 	spa_load_error_t sle = { 0 };
145510921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
145610921STim.Haley@Sun.COM 	boolean_t verify_ok = B_FALSE;
145710921STim.Haley@Sun.COM 	int error;
145810921STim.Haley@Sun.COM 
145911727SVictor.Latushkin@Sun.COM 	zpool_get_rewind_policy(spa->spa_config, &policy);
146011727SVictor.Latushkin@Sun.COM 
146111727SVictor.Latushkin@Sun.COM 	if (policy.zrp_request & ZPOOL_NEVER_REWIND)
146211727SVictor.Latushkin@Sun.COM 		return (0);
146311727SVictor.Latushkin@Sun.COM 
146410921STim.Haley@Sun.COM 	rio = zio_root(spa, NULL, &sle,
146510921STim.Haley@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
146610921STim.Haley@Sun.COM 
146711125SJeff.Bonwick@Sun.COM 	error = traverse_pool(spa, spa->spa_verify_min_txg,
146811125SJeff.Bonwick@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
146910921STim.Haley@Sun.COM 
147010921STim.Haley@Sun.COM 	(void) zio_wait(rio);
147110921STim.Haley@Sun.COM 
147211727SVictor.Latushkin@Sun.COM 	spa->spa_load_meta_errors = sle.sle_meta_count;
147310921STim.Haley@Sun.COM 	spa->spa_load_data_errors = sle.sle_data_count;
147410921STim.Haley@Sun.COM 
147511727SVictor.Latushkin@Sun.COM 	if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
147610921STim.Haley@Sun.COM 	    sle.sle_data_count <= policy.zrp_maxdata) {
147710921STim.Haley@Sun.COM 		verify_ok = B_TRUE;
147810921STim.Haley@Sun.COM 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
147910921STim.Haley@Sun.COM 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
148011026STim.Haley@Sun.COM 	} else {
148111026STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
148210921STim.Haley@Sun.COM 	}
148310921STim.Haley@Sun.COM 
148410921STim.Haley@Sun.COM 	if (error) {
148510921STim.Haley@Sun.COM 		if (error != ENXIO && error != EIO)
148610921STim.Haley@Sun.COM 			error = EIO;
148710921STim.Haley@Sun.COM 		return (error);
148810921STim.Haley@Sun.COM 	}
148910921STim.Haley@Sun.COM 
149010921STim.Haley@Sun.COM 	return (verify_ok ? 0 : EIO);
149110921STim.Haley@Sun.COM }
149210921STim.Haley@Sun.COM 
14937294Sperrin /*
149411422SMark.Musante@Sun.COM  * Find a value in the pool props object.
149511422SMark.Musante@Sun.COM  */
149611422SMark.Musante@Sun.COM static void
149711422SMark.Musante@Sun.COM spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
149811422SMark.Musante@Sun.COM {
149911422SMark.Musante@Sun.COM 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
150011422SMark.Musante@Sun.COM 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
150111422SMark.Musante@Sun.COM }
150211422SMark.Musante@Sun.COM 
150311422SMark.Musante@Sun.COM /*
150411422SMark.Musante@Sun.COM  * Find a value in the pool directory object.
150511422SMark.Musante@Sun.COM  */
150611422SMark.Musante@Sun.COM static int
150711422SMark.Musante@Sun.COM spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
150811422SMark.Musante@Sun.COM {
150911422SMark.Musante@Sun.COM 	return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
151011422SMark.Musante@Sun.COM 	    name, sizeof (uint64_t), 1, val));
151111422SMark.Musante@Sun.COM }
151211422SMark.Musante@Sun.COM 
151311422SMark.Musante@Sun.COM static int
151411422SMark.Musante@Sun.COM spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
151511422SMark.Musante@Sun.COM {
151611422SMark.Musante@Sun.COM 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
151711422SMark.Musante@Sun.COM 	return (err);
151811422SMark.Musante@Sun.COM }
151911422SMark.Musante@Sun.COM 
152011422SMark.Musante@Sun.COM /*
152111422SMark.Musante@Sun.COM  * Fix up config after a partly-completed split.  This is done with the
152211422SMark.Musante@Sun.COM  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
152311422SMark.Musante@Sun.COM  * pool have that entry in their config, but only the splitting one contains
152411422SMark.Musante@Sun.COM  * a list of all the guids of the vdevs that are being split off.
152511422SMark.Musante@Sun.COM  *
152611422SMark.Musante@Sun.COM  * This function determines what to do with that list: either rejoin
152711422SMark.Musante@Sun.COM  * all the disks to the pool, or complete the splitting process.  To attempt
152811422SMark.Musante@Sun.COM  * the rejoin, each disk that is offlined is marked online again, and
152911422SMark.Musante@Sun.COM  * we do a reopen() call.  If the vdev label for every disk that was
153011422SMark.Musante@Sun.COM  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
153111422SMark.Musante@Sun.COM  * then we call vdev_split() on each disk, and complete the split.
153211422SMark.Musante@Sun.COM  *
153311497SMark.Musante@Sun.COM  * Otherwise we leave the config alone, with all the vdevs in place in
153411497SMark.Musante@Sun.COM  * the original pool.
153511422SMark.Musante@Sun.COM  */
153611422SMark.Musante@Sun.COM static void
153711422SMark.Musante@Sun.COM spa_try_repair(spa_t *spa, nvlist_t *config)
153811422SMark.Musante@Sun.COM {
153911422SMark.Musante@Sun.COM 	uint_t extracted;
154011422SMark.Musante@Sun.COM 	uint64_t *glist;
154111422SMark.Musante@Sun.COM 	uint_t i, gcount;
154211422SMark.Musante@Sun.COM 	nvlist_t *nvl;
154311422SMark.Musante@Sun.COM 	vdev_t **vd;
154411422SMark.Musante@Sun.COM 	boolean_t attempt_reopen;
154511422SMark.Musante@Sun.COM 
154611422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
154711422SMark.Musante@Sun.COM 		return;
154811422SMark.Musante@Sun.COM 
154911422SMark.Musante@Sun.COM 	/* check that the config is complete */
155011422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
155111422SMark.Musante@Sun.COM 	    &glist, &gcount) != 0)
155211422SMark.Musante@Sun.COM 		return;
155311422SMark.Musante@Sun.COM 
155411422SMark.Musante@Sun.COM 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
155511422SMark.Musante@Sun.COM 
155611422SMark.Musante@Sun.COM 	/* attempt to online all the vdevs & validate */
155711422SMark.Musante@Sun.COM 	attempt_reopen = B_TRUE;
155811422SMark.Musante@Sun.COM 	for (i = 0; i < gcount; i++) {
155911422SMark.Musante@Sun.COM 		if (glist[i] == 0)	/* vdev is hole */
156011422SMark.Musante@Sun.COM 			continue;
156111422SMark.Musante@Sun.COM 
156211422SMark.Musante@Sun.COM 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
156311422SMark.Musante@Sun.COM 		if (vd[i] == NULL) {
156411422SMark.Musante@Sun.COM 			/*
156511422SMark.Musante@Sun.COM 			 * Don't bother attempting to reopen the disks;
156611422SMark.Musante@Sun.COM 			 * just do the split.
156711422SMark.Musante@Sun.COM 			 */
156811422SMark.Musante@Sun.COM 			attempt_reopen = B_FALSE;
156911422SMark.Musante@Sun.COM 		} else {
157011422SMark.Musante@Sun.COM 			/* attempt to re-online it */
157111422SMark.Musante@Sun.COM 			vd[i]->vdev_offline = B_FALSE;
157211422SMark.Musante@Sun.COM 		}
157311422SMark.Musante@Sun.COM 	}
157411422SMark.Musante@Sun.COM 
157511422SMark.Musante@Sun.COM 	if (attempt_reopen) {
157611422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
157711422SMark.Musante@Sun.COM 
157811422SMark.Musante@Sun.COM 		/* check each device to see what state it's in */
157911422SMark.Musante@Sun.COM 		for (extracted = 0, i = 0; i < gcount; i++) {
158011422SMark.Musante@Sun.COM 			if (vd[i] != NULL &&
158111422SMark.Musante@Sun.COM 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
158211422SMark.Musante@Sun.COM 				break;
158311422SMark.Musante@Sun.COM 			++extracted;
158411422SMark.Musante@Sun.COM 		}
158511422SMark.Musante@Sun.COM 	}
158611422SMark.Musante@Sun.COM 
158711422SMark.Musante@Sun.COM 	/*
158811422SMark.Musante@Sun.COM 	 * If every disk has been moved to the new pool, or if we never
158911422SMark.Musante@Sun.COM 	 * even attempted to look at them, then we split them off for
159011422SMark.Musante@Sun.COM 	 * good.
159111422SMark.Musante@Sun.COM 	 */
159211422SMark.Musante@Sun.COM 	if (!attempt_reopen || gcount == extracted) {
159311422SMark.Musante@Sun.COM 		for (i = 0; i < gcount; i++)
159411422SMark.Musante@Sun.COM 			if (vd[i] != NULL)
159511422SMark.Musante@Sun.COM 				vdev_split(vd[i]);
159611422SMark.Musante@Sun.COM 		vdev_reopen(spa->spa_root_vdev);
159711422SMark.Musante@Sun.COM 	}
159811422SMark.Musante@Sun.COM 
159911422SMark.Musante@Sun.COM 	kmem_free(vd, gcount * sizeof (vdev_t *));
160011422SMark.Musante@Sun.COM }
160111422SMark.Musante@Sun.COM 
160211422SMark.Musante@Sun.COM static int
160311422SMark.Musante@Sun.COM spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
160411422SMark.Musante@Sun.COM     boolean_t mosconfig)
160511422SMark.Musante@Sun.COM {
160611422SMark.Musante@Sun.COM 	nvlist_t *config = spa->spa_config;
160711422SMark.Musante@Sun.COM 	char *ereport = FM_EREPORT_ZFS_POOL;
160811422SMark.Musante@Sun.COM 	int error;
160911422SMark.Musante@Sun.COM 	uint64_t pool_guid;
161011422SMark.Musante@Sun.COM 	nvlist_t *nvl;
161111422SMark.Musante@Sun.COM 
161211422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
161311422SMark.Musante@Sun.COM 		return (EINVAL);
161411422SMark.Musante@Sun.COM 
161511422SMark.Musante@Sun.COM 	/*
161611422SMark.Musante@Sun.COM 	 * Versioning wasn't explicitly added to the label until later, so if
161711422SMark.Musante@Sun.COM 	 * it's not present treat it as the initial version.
161811422SMark.Musante@Sun.COM 	 */
161911422SMark.Musante@Sun.COM 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
162011422SMark.Musante@Sun.COM 	    &spa->spa_ubsync.ub_version) != 0)
162111422SMark.Musante@Sun.COM 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
162211422SMark.Musante@Sun.COM 
162311422SMark.Musante@Sun.COM 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
162411422SMark.Musante@Sun.COM 	    &spa->spa_config_txg);
162511422SMark.Musante@Sun.COM 
162611422SMark.Musante@Sun.COM 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
162711422SMark.Musante@Sun.COM 	    spa_guid_exists(pool_guid, 0)) {
162811422SMark.Musante@Sun.COM 		error = EEXIST;
162911422SMark.Musante@Sun.COM 	} else {
163011422SMark.Musante@Sun.COM 		spa->spa_load_guid = pool_guid;
163111422SMark.Musante@Sun.COM 
163211422SMark.Musante@Sun.COM 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
163311422SMark.Musante@Sun.COM 		    &nvl) == 0) {
163411422SMark.Musante@Sun.COM 			VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
163511422SMark.Musante@Sun.COM 			    KM_SLEEP) == 0);
163611422SMark.Musante@Sun.COM 		}
163711422SMark.Musante@Sun.COM 
1638*12817STim.Haley@Sun.COM 		gethrestime(&spa->spa_loaded_ts);
163911422SMark.Musante@Sun.COM 		error = spa_load_impl(spa, pool_guid, config, state, type,
164011422SMark.Musante@Sun.COM 		    mosconfig, &ereport);
164111422SMark.Musante@Sun.COM 	}
164211422SMark.Musante@Sun.COM 
164311422SMark.Musante@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
1644*12817STim.Haley@Sun.COM 	if (error) {
1645*12817STim.Haley@Sun.COM 		if (error != EEXIST) {
1646*12817STim.Haley@Sun.COM 			spa->spa_loaded_ts.tv_sec = 0;
1647*12817STim.Haley@Sun.COM 			spa->spa_loaded_ts.tv_nsec = 0;
1648*12817STim.Haley@Sun.COM 		}
1649*12817STim.Haley@Sun.COM 		if (error != EBADF) {
1650*12817STim.Haley@Sun.COM 			zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1651*12817STim.Haley@Sun.COM 		}
1652*12817STim.Haley@Sun.COM 	}
165311422SMark.Musante@Sun.COM 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
165411422SMark.Musante@Sun.COM 	spa->spa_ena = 0;
165511422SMark.Musante@Sun.COM 
165611422SMark.Musante@Sun.COM 	return (error);
165711422SMark.Musante@Sun.COM }
165811422SMark.Musante@Sun.COM 
165911422SMark.Musante@Sun.COM /*
1660789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
16611544Seschrock  * source of configuration information.
1662789Sahrens  */
1663789Sahrens static int
166411422SMark.Musante@Sun.COM spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
166511422SMark.Musante@Sun.COM     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
166611422SMark.Musante@Sun.COM     char **ereport)
1667789Sahrens {
1668789Sahrens 	int error = 0;
166911810SMark.Musante@Sun.COM 	nvlist_t *nvroot = NULL;
1670789Sahrens 	vdev_t *rvd;
1671789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
16721635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
16738241SJeff.Bonwick@Sun.COM 	int orig_mode = spa->spa_mode;
167411422SMark.Musante@Sun.COM 	int parse;
167512470SMatthew.Ahrens@Sun.COM 	uint64_t obj;
1676789Sahrens 
16778241SJeff.Bonwick@Sun.COM 	/*
16788241SJeff.Bonwick@Sun.COM 	 * If this is an untrusted config, access the pool in read-only mode.
16798241SJeff.Bonwick@Sun.COM 	 * This prevents things like resilvering recently removed devices.
16808241SJeff.Bonwick@Sun.COM 	 */
16818241SJeff.Bonwick@Sun.COM 	if (!mosconfig)
16828241SJeff.Bonwick@Sun.COM 		spa->spa_mode = FREAD;
16838241SJeff.Bonwick@Sun.COM 
16847754SJeff.Bonwick@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
16857754SJeff.Bonwick@Sun.COM 
16861544Seschrock 	spa->spa_load_state = state;
16871635Sbonwick 
168811422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
168911422SMark.Musante@Sun.COM 		return (EINVAL);
169011422SMark.Musante@Sun.COM 
169111422SMark.Musante@Sun.COM 	parse = (type == SPA_IMPORT_EXISTING ?
169211422SMark.Musante@Sun.COM 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
16932174Seschrock 
1694789Sahrens 	/*
16959234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
16969234SGeorge.Wilson@Sun.COM 	 */
16979630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
16989630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
16999234SGeorge.Wilson@Sun.COM 
17009234SGeorge.Wilson@Sun.COM 	/*
17012082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
17022082Seschrock 	 * value that will be returned by spa_version() since parsing the
17032082Seschrock 	 * configuration requires knowing the version number.
1704789Sahrens 	 */
17057754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
170611422SMark.Musante@Sun.COM 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
17077754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1708789Sahrens 
17092082Seschrock 	if (error != 0)
171011422SMark.Musante@Sun.COM 		return (error);
1711789Sahrens 
17121585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
171311422SMark.Musante@Sun.COM 
171411422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
171511422SMark.Musante@Sun.COM 		ASSERT(spa_guid(spa) == pool_guid);
171611422SMark.Musante@Sun.COM 	}
1717789Sahrens 
1718789Sahrens 	/*
1719789Sahrens 	 * Try to open all vdevs, loading each label in the process.
1720789Sahrens 	 */
17217754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17224070Smc142369 	error = vdev_open(rvd);
17237754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
17244070Smc142369 	if (error != 0)
172511422SMark.Musante@Sun.COM 		return (error);
1726789Sahrens 
1727789Sahrens 	/*
17289276SMark.Musante@Sun.COM 	 * We need to validate the vdev labels against the configuration that
17299276SMark.Musante@Sun.COM 	 * we have in hand, which is dependent on the setting of mosconfig. If
17309276SMark.Musante@Sun.COM 	 * mosconfig is true then we're validating the vdev labels based on
173111422SMark.Musante@Sun.COM 	 * that config.  Otherwise, we're validating against the cached config
17329276SMark.Musante@Sun.COM 	 * (zpool.cache) that was read when we loaded the zfs module, and then
17339276SMark.Musante@Sun.COM 	 * later we will recursively call spa_load() and validate against
17349276SMark.Musante@Sun.COM 	 * the vdev config.
173511422SMark.Musante@Sun.COM 	 *
173611422SMark.Musante@Sun.COM 	 * If we're assembling a new pool that's been split off from an
173711422SMark.Musante@Sun.COM 	 * existing pool, the labels haven't yet been updated so we skip
173811422SMark.Musante@Sun.COM 	 * validation for now.
17391986Seschrock 	 */
174011422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
174111422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
174211422SMark.Musante@Sun.COM 		error = vdev_validate(rvd);
174311422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
174411422SMark.Musante@Sun.COM 
174511422SMark.Musante@Sun.COM 		if (error != 0)
174611422SMark.Musante@Sun.COM 			return (error);
174711422SMark.Musante@Sun.COM 
174811422SMark.Musante@Sun.COM 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
174911422SMark.Musante@Sun.COM 			return (ENXIO);
17501986Seschrock 	}
17511986Seschrock 
17521986Seschrock 	/*
1753789Sahrens 	 * Find the best uberblock.
1754789Sahrens 	 */
17557754SJeff.Bonwick@Sun.COM 	vdev_uberblock_load(NULL, rvd, ub);
1756789Sahrens 
1757789Sahrens 	/*
1758789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1759789Sahrens 	 */
176011422SMark.Musante@Sun.COM 	if (ub->ub_txg == 0)
176111422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
17621544Seschrock 
17631544Seschrock 	/*
17641544Seschrock 	 * If the pool is newer than the code, we can't open it.
17651544Seschrock 	 */
176611422SMark.Musante@Sun.COM 	if (ub->ub_version > SPA_VERSION)
176711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1768789Sahrens 
1769789Sahrens 	/*
1770789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1771789Sahrens 	 * incomplete configuration.
1772789Sahrens 	 */
177311422SMark.Musante@Sun.COM 	if (mosconfig && type != SPA_IMPORT_ASSEMBLE &&
177411422SMark.Musante@Sun.COM 	    rvd->vdev_guid_sum != ub->ub_guid_sum)
177511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
177611422SMark.Musante@Sun.COM 
177711422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
177811422SMark.Musante@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
177911422SMark.Musante@Sun.COM 		spa_try_repair(spa, config);
178011422SMark.Musante@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
178111422SMark.Musante@Sun.COM 		nvlist_free(spa->spa_config_splitting);
178211422SMark.Musante@Sun.COM 		spa->spa_config_splitting = NULL;
1783789Sahrens 	}
1784789Sahrens 
1785789Sahrens 	/*
1786789Sahrens 	 * Initialize internal SPA structures.
1787789Sahrens 	 */
1788789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1789789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
179010921STim.Haley@Sun.COM 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
179111727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
179210921STim.Haley@Sun.COM 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
179310921STim.Haley@Sun.COM 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
179410922SJeff.Bonwick@Sun.COM 	spa->spa_claim_max_txg = spa->spa_first_txg;
179512296SLin.Ling@Sun.COM 	spa->spa_prev_software_version = ub->ub_software_version;
179610922SJeff.Bonwick@Sun.COM 
17971544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
179811422SMark.Musante@Sun.COM 	if (error)
179911422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1800789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1801789Sahrens 
180211422SMark.Musante@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
180311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
180411422SMark.Musante@Sun.COM 
1805789Sahrens 	if (!mosconfig) {
18063975Sek110237 		uint64_t hostid;
180711810SMark.Musante@Sun.COM 		nvlist_t *policy = NULL, *nvconfig;
180811810SMark.Musante@Sun.COM 
180911810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
181011810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18112082Seschrock 
181210594SGeorge.Wilson@Sun.COM 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
18137706SLin.Ling@Sun.COM 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
18143975Sek110237 			char *hostname;
18153975Sek110237 			unsigned long myhostid = 0;
18163975Sek110237 
181710594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_lookup_string(nvconfig,
18183975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
18193975Sek110237 
18208662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
18218662SJordan.Vaughan@Sun.com 			myhostid = zone_get_hostid(NULL);
18228662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
18238662SJordan.Vaughan@Sun.com 			/*
18248662SJordan.Vaughan@Sun.com 			 * We're emulating the system's hostid in userland, so
18258662SJordan.Vaughan@Sun.com 			 * we can't use zone_get_hostid().
18268662SJordan.Vaughan@Sun.com 			 */
18273975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
18288662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
18294178Slling 			if (hostid != 0 && myhostid != 0 &&
18308662SJordan.Vaughan@Sun.com 			    hostid != myhostid) {
183111810SMark.Musante@Sun.COM 				nvlist_free(nvconfig);
18323975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
18333975Sek110237 				    "loaded as it was last accessed by "
18347706SLin.Ling@Sun.COM 				    "another system (host: %s hostid: 0x%lx). "
18353975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
18367754SJeff.Bonwick@Sun.COM 				    spa_name(spa), hostname,
18373975Sek110237 				    (unsigned long)hostid);
183811422SMark.Musante@Sun.COM 				return (EBADF);
18393975Sek110237 			}
18403975Sek110237 		}
184111727SVictor.Latushkin@Sun.COM 		if (nvlist_lookup_nvlist(spa->spa_config,
184211727SVictor.Latushkin@Sun.COM 		    ZPOOL_REWIND_POLICY, &policy) == 0)
184311727SVictor.Latushkin@Sun.COM 			VERIFY(nvlist_add_nvlist(nvconfig,
184411727SVictor.Latushkin@Sun.COM 			    ZPOOL_REWIND_POLICY, policy) == 0);
18453975Sek110237 
184610594SGeorge.Wilson@Sun.COM 		spa_config_set(spa, nvconfig);
1847789Sahrens 		spa_unload(spa);
1848789Sahrens 		spa_deactivate(spa);
18498241SJeff.Bonwick@Sun.COM 		spa_activate(spa, orig_mode);
1850789Sahrens 
185111422SMark.Musante@Sun.COM 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
18521544Seschrock 	}
18531544Seschrock 
185412470SMatthew.Ahrens@Sun.COM 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
185512470SMatthew.Ahrens@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
185612470SMatthew.Ahrens@Sun.COM 	error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
185712470SMatthew.Ahrens@Sun.COM 	if (error != 0)
185811422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1859789Sahrens 
18601544Seschrock 	/*
18612082Seschrock 	 * Load the bit that tells us to use the new accounting function
18622082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
18632082Seschrock 	 * be present.
18642082Seschrock 	 */
186511422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
186611422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
186711422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
18682082Seschrock 
186912296SLin.Ling@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
187012296SLin.Ling@Sun.COM 	    &spa->spa_creation_version);
187112296SLin.Ling@Sun.COM 	if (error != 0 && error != ENOENT)
187212296SLin.Ling@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
187312296SLin.Ling@Sun.COM 
18742082Seschrock 	/*
18751544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
18761544Seschrock 	 * not be present.
18771544Seschrock 	 */
187811422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
187911422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
188011422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
188111422SMark.Musante@Sun.COM 
188211422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
188311422SMark.Musante@Sun.COM 	    &spa->spa_errlog_scrub);
188411422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
188511422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1886789Sahrens 
1887789Sahrens 	/*
18882926Sek110237 	 * Load the history object.  If we have an older pool, this
18892926Sek110237 	 * will not be present.
18902926Sek110237 	 */
189111422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
189211422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
189311422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
189411422SMark.Musante@Sun.COM 
189511422SMark.Musante@Sun.COM 	/*
189611422SMark.Musante@Sun.COM 	 * If we're assembling the pool from the split-off vdevs of
189711422SMark.Musante@Sun.COM 	 * an existing pool, we don't want to attach the spares & cache
189811422SMark.Musante@Sun.COM 	 * devices.
189911422SMark.Musante@Sun.COM 	 */
19002926Sek110237 
19012926Sek110237 	/*
19022082Seschrock 	 * Load any hot spares for this pool.
19032082Seschrock 	 */
190411422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
190511422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
190611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
190711422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19084577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
19095450Sbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
191011422SMark.Musante@Sun.COM 		    &spa->spa_spares.sav_config) != 0)
191111422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19122082Seschrock 
19137754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19142082Seschrock 		spa_load_spares(spa);
19157754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
191611422SMark.Musante@Sun.COM 	} else if (error == 0) {
191711422SMark.Musante@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
19182082Seschrock 	}
19192082Seschrock 
19205450Sbrendan 	/*
19215450Sbrendan 	 * Load any level 2 ARC devices for this pool.
19225450Sbrendan 	 */
192311422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
19245450Sbrendan 	    &spa->spa_l2cache.sav_object);
192511422SMark.Musante@Sun.COM 	if (error != 0 && error != ENOENT)
192611422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
192711422SMark.Musante@Sun.COM 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
19285450Sbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
19295450Sbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
193011422SMark.Musante@Sun.COM 		    &spa->spa_l2cache.sav_config) != 0)
193111422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19325450Sbrendan 
19337754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
19345450Sbrendan 		spa_load_l2cache(spa);
19357754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
193611422SMark.Musante@Sun.COM 	} else if (error == 0) {
193711422SMark.Musante@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
19385450Sbrendan 	}
19395450Sbrendan 
19405094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
19414543Smarks 
194211422SMark.Musante@Sun.COM 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
194311422SMark.Musante@Sun.COM 	if (error && error != ENOENT)
194411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
19453912Slling 
19463912Slling 	if (error == 0) {
194711422SMark.Musante@Sun.COM 		uint64_t autoreplace;
194811422SMark.Musante@Sun.COM 
194911422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
195011422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
195111422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
195211422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
195311422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
195411422SMark.Musante@Sun.COM 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
195511422SMark.Musante@Sun.COM 		    &spa->spa_dedup_ditto);
195611422SMark.Musante@Sun.COM 
195710672SEric.Schrock@Sun.COM 		spa->spa_autoreplace = (autoreplace != 0);
19583912Slling 	}
19593912Slling 
19602082Seschrock 	/*
19614451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
19624451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
19634451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
19644451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
19654451Seschrock 	 * over.
19664451Seschrock 	 */
196710672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
19684451Seschrock 		spa_check_removed(spa->spa_root_vdev);
196910672SEric.Schrock@Sun.COM 		/*
197010672SEric.Schrock@Sun.COM 		 * For the import case, this is done in spa_import(), because
197110672SEric.Schrock@Sun.COM 		 * at this point we're using the spare definitions from
197210672SEric.Schrock@Sun.COM 		 * the MOS config, not necessarily from the userland config.
197310672SEric.Schrock@Sun.COM 		 */
197410672SEric.Schrock@Sun.COM 		if (state != SPA_LOAD_IMPORT) {
197510672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_spares);
197610672SEric.Schrock@Sun.COM 			spa_aux_check_removed(&spa->spa_l2cache);
197710672SEric.Schrock@Sun.COM 		}
197810672SEric.Schrock@Sun.COM 	}
19794451Seschrock 
19804451Seschrock 	/*
19811986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1982789Sahrens 	 */
19831986Seschrock 	vdev_load(rvd);
1984789Sahrens 
1985789Sahrens 	/*
1986789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1987789Sahrens 	 */
19887754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1989789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
19907754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
1991789Sahrens 
1992789Sahrens 	/*
1993789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1994789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1995789Sahrens 	 */
199611422SMark.Musante@Sun.COM 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
199711422SMark.Musante@Sun.COM 		return (ENXIO);
1998789Sahrens 
199910922SJeff.Bonwick@Sun.COM 	/*
200010922SJeff.Bonwick@Sun.COM 	 * Load the DDTs (dedup tables).
200110922SJeff.Bonwick@Sun.COM 	 */
200210922SJeff.Bonwick@Sun.COM 	error = ddt_load(spa);
200311422SMark.Musante@Sun.COM 	if (error != 0)
200411422SMark.Musante@Sun.COM 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
200510922SJeff.Bonwick@Sun.COM 
200610956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
200710956SGeorge.Wilson@Sun.COM 
200810921STim.Haley@Sun.COM 	if (state != SPA_LOAD_TRYIMPORT) {
200910921STim.Haley@Sun.COM 		error = spa_load_verify(spa);
201011422SMark.Musante@Sun.COM 		if (error)
201111422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
201211422SMark.Musante@Sun.COM 			    error));
201310921STim.Haley@Sun.COM 	}
201410921STim.Haley@Sun.COM 
201510922SJeff.Bonwick@Sun.COM 	/*
201611422SMark.Musante@Sun.COM 	 * Load the intent log state and check log integrity.  If we're
201711422SMark.Musante@Sun.COM 	 * assembling a pool from a split, the log is not transferred over.
201810922SJeff.Bonwick@Sun.COM 	 */
201911422SMark.Musante@Sun.COM 	if (type != SPA_IMPORT_ASSEMBLE) {
202011810SMark.Musante@Sun.COM 		nvlist_t *nvconfig;
202111810SMark.Musante@Sun.COM 
202211810SMark.Musante@Sun.COM 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
202311810SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
202411810SMark.Musante@Sun.COM 
202511422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
202611422SMark.Musante@Sun.COM 		    &nvroot) == 0);
202711422SMark.Musante@Sun.COM 		spa_load_log_state(spa, nvroot);
202811422SMark.Musante@Sun.COM 		nvlist_free(nvconfig);
202911422SMark.Musante@Sun.COM 
203011422SMark.Musante@Sun.COM 		if (spa_check_logs(spa)) {
203111422SMark.Musante@Sun.COM 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
203211422SMark.Musante@Sun.COM 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
203311422SMark.Musante@Sun.COM 		}
203410922SJeff.Bonwick@Sun.COM 	}
203510922SJeff.Bonwick@Sun.COM 
203610921STim.Haley@Sun.COM 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
203710921STim.Haley@Sun.COM 	    spa->spa_load_max_txg == UINT64_MAX)) {
20381635Sbonwick 		dmu_tx_t *tx;
20391635Sbonwick 		int need_update = B_FALSE;
20408241SJeff.Bonwick@Sun.COM 
20418241SJeff.Bonwick@Sun.COM 		ASSERT(state != SPA_LOAD_TRYIMPORT);
20421601Sbonwick 
20431635Sbonwick 		/*
20441635Sbonwick 		 * Claim log blocks that haven't been committed yet.
20451635Sbonwick 		 * This must all happen in a single txg.
204610922SJeff.Bonwick@Sun.COM 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
204710922SJeff.Bonwick@Sun.COM 		 * invoked from zil_claim_log_block()'s i/o done callback.
204810921STim.Haley@Sun.COM 		 * Price of rollback is that we abandon the log.
20491635Sbonwick 		 */
205010922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_TRUE;
205110922SJeff.Bonwick@Sun.COM 
20521601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2053789Sahrens 		    spa_first_txg(spa));
20547754SJeff.Bonwick@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
20552417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2056789Sahrens 		dmu_tx_commit(tx);
2057789Sahrens 
205810922SJeff.Bonwick@Sun.COM 		spa->spa_claiming = B_FALSE;
205910922SJeff.Bonwick@Sun.COM 
206011422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_GOOD);
2061789Sahrens 		spa->spa_sync_on = B_TRUE;
2062789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2063789Sahrens 
2064789Sahrens 		/*
206510922SJeff.Bonwick@Sun.COM 		 * Wait for all claims to sync.  We sync up to the highest
206610922SJeff.Bonwick@Sun.COM 		 * claimed log block birth time so that claimed log blocks
206710922SJeff.Bonwick@Sun.COM 		 * don't appear to be from the future.  spa_claim_max_txg
206810922SJeff.Bonwick@Sun.COM 		 * will have been set for us by either zil_check_log_chain()
206910922SJeff.Bonwick@Sun.COM 		 * (invoked from spa_check_logs()) or zil_claim() above.
2070789Sahrens 		 */
207110922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
20721585Sbonwick 
20731585Sbonwick 		/*
20741635Sbonwick 		 * If the config cache is stale, or we have uninitialized
20751635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
207610100SLin.Ling@Sun.COM 		 *
207710100SLin.Ling@Sun.COM 		 * If spa_load_verbatim is true, trust the current
207810100SLin.Ling@Sun.COM 		 * in-core spa_config and update the disk labels.
20791585Sbonwick 		 */
20801635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
208110921STim.Haley@Sun.COM 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
208210921STim.Haley@Sun.COM 		    state == SPA_LOAD_RECOVER)
20831635Sbonwick 			need_update = B_TRUE;
20841635Sbonwick 
20858241SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++)
20861635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
20871635Sbonwick 				need_update = B_TRUE;
20881585Sbonwick 
20891585Sbonwick 		/*
20901635Sbonwick 		 * Update the config cache asychronously in case we're the
20911635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
20921585Sbonwick 		 */
20931635Sbonwick 		if (need_update)
20941635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
20958241SJeff.Bonwick@Sun.COM 
20968241SJeff.Bonwick@Sun.COM 		/*
20978241SJeff.Bonwick@Sun.COM 		 * Check all DTLs to see if anything needs resilvering.
20988241SJeff.Bonwick@Sun.COM 		 */
209912296SLin.Ling@Sun.COM 		if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
210012296SLin.Ling@Sun.COM 		    vdev_resilver_needed(rvd, NULL, NULL))
21018241SJeff.Bonwick@Sun.COM 			spa_async_request(spa, SPA_ASYNC_RESILVER);
210210298SMatthew.Ahrens@Sun.COM 
210310298SMatthew.Ahrens@Sun.COM 		/*
210410298SMatthew.Ahrens@Sun.COM 		 * Delete any inconsistent datasets.
210510298SMatthew.Ahrens@Sun.COM 		 */
210610298SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_find(spa_name(spa),
210710298SMatthew.Ahrens@Sun.COM 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
210810342Schris.kirby@sun.com 
210910342Schris.kirby@sun.com 		/*
211010342Schris.kirby@sun.com 		 * Clean up any stale temporary dataset userrefs.
211110342Schris.kirby@sun.com 		 */
211210342Schris.kirby@sun.com 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2113789Sahrens 	}
2114789Sahrens 
211511422SMark.Musante@Sun.COM 	return (0);
2116789Sahrens }
2117789Sahrens 
211810921STim.Haley@Sun.COM static int
211910921STim.Haley@Sun.COM spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
212010921STim.Haley@Sun.COM {
212110921STim.Haley@Sun.COM 	spa_unload(spa);
212210921STim.Haley@Sun.COM 	spa_deactivate(spa);
212310921STim.Haley@Sun.COM 
212410921STim.Haley@Sun.COM 	spa->spa_load_max_txg--;
212510921STim.Haley@Sun.COM 
212610921STim.Haley@Sun.COM 	spa_activate(spa, spa_mode_global);
212710921STim.Haley@Sun.COM 	spa_async_suspend(spa);
212810921STim.Haley@Sun.COM 
212911422SMark.Musante@Sun.COM 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
213010921STim.Haley@Sun.COM }
213110921STim.Haley@Sun.COM 
213210921STim.Haley@Sun.COM static int
213310921STim.Haley@Sun.COM spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
213411727SVictor.Latushkin@Sun.COM     uint64_t max_request, int rewind_flags)
213510921STim.Haley@Sun.COM {
213610921STim.Haley@Sun.COM 	nvlist_t *config = NULL;
213710921STim.Haley@Sun.COM 	int load_error, rewind_error;
213811727SVictor.Latushkin@Sun.COM 	uint64_t safe_rewind_txg;
213910921STim.Haley@Sun.COM 	uint64_t min_txg;
214010921STim.Haley@Sun.COM 
214111026STim.Haley@Sun.COM 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
214210921STim.Haley@Sun.COM 		spa->spa_load_max_txg = spa->spa_load_txg;
214311422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
214411026STim.Haley@Sun.COM 	} else {
214510921STim.Haley@Sun.COM 		spa->spa_load_max_txg = max_request;
214611026STim.Haley@Sun.COM 	}
214710921STim.Haley@Sun.COM 
214811422SMark.Musante@Sun.COM 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
214911422SMark.Musante@Sun.COM 	    mosconfig);
215010921STim.Haley@Sun.COM 	if (load_error == 0)
215110921STim.Haley@Sun.COM 		return (0);
215210921STim.Haley@Sun.COM 
215310921STim.Haley@Sun.COM 	if (spa->spa_root_vdev != NULL)
215410921STim.Haley@Sun.COM 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
215510921STim.Haley@Sun.COM 
215610921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
215710921STim.Haley@Sun.COM 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
215810921STim.Haley@Sun.COM 
215911727SVictor.Latushkin@Sun.COM 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
216010921STim.Haley@Sun.COM 		nvlist_free(config);
216110921STim.Haley@Sun.COM 		return (load_error);
216210921STim.Haley@Sun.COM 	}
216310921STim.Haley@Sun.COM 
216410921STim.Haley@Sun.COM 	/* Price of rolling back is discarding txgs, including log */
216510921STim.Haley@Sun.COM 	if (state == SPA_LOAD_RECOVER)
216611422SMark.Musante@Sun.COM 		spa_set_log_state(spa, SPA_LOG_CLEAR);
216710921STim.Haley@Sun.COM 
216811727SVictor.Latushkin@Sun.COM 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
216911727SVictor.Latushkin@Sun.COM 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
217011727SVictor.Latushkin@Sun.COM 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
217111727SVictor.Latushkin@Sun.COM 	    TXG_INITIAL : safe_rewind_txg;
217211727SVictor.Latushkin@Sun.COM 
217311727SVictor.Latushkin@Sun.COM 	/*
217411727SVictor.Latushkin@Sun.COM 	 * Continue as long as we're finding errors, we're still within
217511727SVictor.Latushkin@Sun.COM 	 * the acceptable rewind range, and we're still finding uberblocks
217611727SVictor.Latushkin@Sun.COM 	 */
217711727SVictor.Latushkin@Sun.COM 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
217811727SVictor.Latushkin@Sun.COM 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
217911727SVictor.Latushkin@Sun.COM 		if (spa->spa_load_max_txg < safe_rewind_txg)
218010921STim.Haley@Sun.COM 			spa->spa_extreme_rewind = B_TRUE;
218110921STim.Haley@Sun.COM 		rewind_error = spa_load_retry(spa, state, mosconfig);
218210921STim.Haley@Sun.COM 	}
218310921STim.Haley@Sun.COM 
218410921STim.Haley@Sun.COM 	if (config)
218510921STim.Haley@Sun.COM 		spa_rewind_data_to_nvlist(spa, config);
218610921STim.Haley@Sun.COM 
218710921STim.Haley@Sun.COM 	spa->spa_extreme_rewind = B_FALSE;
218810921STim.Haley@Sun.COM 	spa->spa_load_max_txg = UINT64_MAX;
218910921STim.Haley@Sun.COM 
219010921STim.Haley@Sun.COM 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
219110921STim.Haley@Sun.COM 		spa_config_set(spa, config);
219210921STim.Haley@Sun.COM 
219310921STim.Haley@Sun.COM 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
219410921STim.Haley@Sun.COM }
219510921STim.Haley@Sun.COM 
2196789Sahrens /*
2197789Sahrens  * Pool Open/Import
2198789Sahrens  *
2199789Sahrens  * The import case is identical to an open except that the configuration is sent
2200789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2201789Sahrens  * case of an open, the pool configuration will exist in the
22024451Seschrock  * POOL_STATE_UNINITIALIZED state.
2203789Sahrens  *
2204789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2205789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2206789Sahrens  * ambiguous state.
2207789Sahrens  */
2208789Sahrens static int
220910921STim.Haley@Sun.COM spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
221010921STim.Haley@Sun.COM     nvlist_t **config)
2211789Sahrens {
2212789Sahrens 	spa_t *spa;
2213789Sahrens 	int error;
2214789Sahrens 	int locked = B_FALSE;
2215789Sahrens 
2216789Sahrens 	*spapp = NULL;
2217789Sahrens 
2218789Sahrens 	/*
2219789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2220789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2221789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2222789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2223789Sahrens 	 */
2224789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2225789Sahrens 		mutex_enter(&spa_namespace_lock);
2226789Sahrens 		locked = B_TRUE;
2227789Sahrens 	}
2228789Sahrens 
2229789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2230789Sahrens 		if (locked)
2231789Sahrens 			mutex_exit(&spa_namespace_lock);
2232789Sahrens 		return (ENOENT);
2233789Sahrens 	}
223410921STim.Haley@Sun.COM 
2235789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
223611819STim.Haley@Sun.COM 		spa_load_state_t state = SPA_LOAD_OPEN;
223711819STim.Haley@Sun.COM 		zpool_rewind_policy_t policy;
223811819STim.Haley@Sun.COM 
223911819STim.Haley@Sun.COM 		zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
224011819STim.Haley@Sun.COM 		    &policy);
224111819STim.Haley@Sun.COM 		if (policy.zrp_request & ZPOOL_DO_REWIND)
224211819STim.Haley@Sun.COM 			state = SPA_LOAD_RECOVER;
2243789Sahrens 
22448241SJeff.Bonwick@Sun.COM 		spa_activate(spa, spa_mode_global);
2245789Sahrens 
224610921STim.Haley@Sun.COM 		if (state != SPA_LOAD_RECOVER)
224710921STim.Haley@Sun.COM 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
224810921STim.Haley@Sun.COM 
224910921STim.Haley@Sun.COM 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
225011727SVictor.Latushkin@Sun.COM 		    policy.zrp_request);
2251789Sahrens 
2252789Sahrens 		if (error == EBADF) {
2253789Sahrens 			/*
22541986Seschrock 			 * If vdev_validate() returns failure (indicated by
22551986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
22561986Seschrock 			 * that the pool has been exported or destroyed.  If
22571986Seschrock 			 * this is the case, the config cache is out of sync and
22581986Seschrock 			 * we should remove the pool from the namespace.
2259789Sahrens 			 */
2260789Sahrens 			spa_unload(spa);
2261789Sahrens 			spa_deactivate(spa);
22626643Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2263789Sahrens 			spa_remove(spa);
2264789Sahrens 			if (locked)
2265789Sahrens 				mutex_exit(&spa_namespace_lock);
2266789Sahrens 			return (ENOENT);
22671544Seschrock 		}
22681544Seschrock 
22691544Seschrock 		if (error) {
2270789Sahrens 			/*
2271789Sahrens 			 * We can't open the pool, but we still have useful
2272789Sahrens 			 * information: the state of each vdev after the
2273789Sahrens 			 * attempted vdev_open().  Return this to the user.
2274789Sahrens 			 */
227510921STim.Haley@Sun.COM 			if (config != NULL && spa->spa_config)
227610921STim.Haley@Sun.COM 				VERIFY(nvlist_dup(spa->spa_config, config,
227710921STim.Haley@Sun.COM 				    KM_SLEEP) == 0);
2278789Sahrens 			spa_unload(spa);
2279789Sahrens 			spa_deactivate(spa);
228010921STim.Haley@Sun.COM 			spa->spa_last_open_failed = error;
2281789Sahrens 			if (locked)
2282789Sahrens 				mutex_exit(&spa_namespace_lock);
2283789Sahrens 			*spapp = NULL;
2284789Sahrens 			return (error);
2285789Sahrens 		}
228610921STim.Haley@Sun.COM 
2287789Sahrens 	}
2288789Sahrens 
2289789Sahrens 	spa_open_ref(spa, tag);
22904451Seschrock 
229110921STim.Haley@Sun.COM 	if (config != NULL)
229210921STim.Haley@Sun.COM 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
229310921STim.Haley@Sun.COM 
229411026STim.Haley@Sun.COM 	if (locked) {
229511026STim.Haley@Sun.COM 		spa->spa_last_open_failed = 0;
229611026STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = 0;
229711026STim.Haley@Sun.COM 		spa->spa_load_txg = 0;
2298789Sahrens 		mutex_exit(&spa_namespace_lock);
229911026STim.Haley@Sun.COM 	}
2300789Sahrens 
2301789Sahrens 	*spapp = spa;
2302789Sahrens 
2303789Sahrens 	return (0);
2304789Sahrens }
2305789Sahrens 
2306789Sahrens int
230710921STim.Haley@Sun.COM spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
230810921STim.Haley@Sun.COM     nvlist_t **config)
230910921STim.Haley@Sun.COM {
231010921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, policy, config));
231110921STim.Haley@Sun.COM }
231210921STim.Haley@Sun.COM 
231310921STim.Haley@Sun.COM int
2314789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
2315789Sahrens {
231610921STim.Haley@Sun.COM 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2317789Sahrens }
2318789Sahrens 
23191544Seschrock /*
23201544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
23211544Seschrock  * preventing it from being exported or destroyed.
23221544Seschrock  */
23231544Seschrock spa_t *
23241544Seschrock spa_inject_addref(char *name)
23251544Seschrock {
23261544Seschrock 	spa_t *spa;
23271544Seschrock 
23281544Seschrock 	mutex_enter(&spa_namespace_lock);
23291544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
23301544Seschrock 		mutex_exit(&spa_namespace_lock);
23311544Seschrock 		return (NULL);
23321544Seschrock 	}
23331544Seschrock 	spa->spa_inject_ref++;
23341544Seschrock 	mutex_exit(&spa_namespace_lock);
23351544Seschrock 
23361544Seschrock 	return (spa);
23371544Seschrock }
23381544Seschrock 
23391544Seschrock void
23401544Seschrock spa_inject_delref(spa_t *spa)
23411544Seschrock {
23421544Seschrock 	mutex_enter(&spa_namespace_lock);
23431544Seschrock 	spa->spa_inject_ref--;
23441544Seschrock 	mutex_exit(&spa_namespace_lock);
23451544Seschrock }
23461544Seschrock 
23475450Sbrendan /*
23485450Sbrendan  * Add spares device information to the nvlist.
23495450Sbrendan  */
23502082Seschrock static void
23512082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
23522082Seschrock {
23532082Seschrock 	nvlist_t **spares;
23542082Seschrock 	uint_t i, nspares;
23552082Seschrock 	nvlist_t *nvroot;
23562082Seschrock 	uint64_t guid;
23572082Seschrock 	vdev_stat_t *vs;
23582082Seschrock 	uint_t vsc;
23593377Seschrock 	uint64_t pool;
23602082Seschrock 
23619425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
23629425SEric.Schrock@Sun.COM 
23635450Sbrendan 	if (spa->spa_spares.sav_count == 0)
23642082Seschrock 		return;
23652082Seschrock 
23662082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
23672082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
23685450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
23692082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23702082Seschrock 	if (nspares != 0) {
23712082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
23722082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
23732082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
23742082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
23752082Seschrock 
23762082Seschrock 		/*
23772082Seschrock 		 * Go through and find any spares which have since been
23782082Seschrock 		 * repurposed as an active spare.  If this is the case, update
23792082Seschrock 		 * their status appropriately.
23802082Seschrock 		 */
23812082Seschrock 		for (i = 0; i < nspares; i++) {
23822082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
23832082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
23847214Slling 			if (spa_spare_exists(guid, &pool, NULL) &&
23857214Slling 			    pool != 0ULL) {
23862082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
238712296SLin.Ling@Sun.COM 				    spares[i], ZPOOL_CONFIG_VDEV_STATS,
23882082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
23892082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
23902082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
23912082Seschrock 			}
23922082Seschrock 		}
23932082Seschrock 	}
23942082Seschrock }
23952082Seschrock 
23965450Sbrendan /*
23975450Sbrendan  * Add l2cache device information to the nvlist, including vdev stats.
23985450Sbrendan  */
23995450Sbrendan static void
24005450Sbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
24015450Sbrendan {
24025450Sbrendan 	nvlist_t **l2cache;
24035450Sbrendan 	uint_t i, j, nl2cache;
24045450Sbrendan 	nvlist_t *nvroot;
24055450Sbrendan 	uint64_t guid;
24065450Sbrendan 	vdev_t *vd;
24075450Sbrendan 	vdev_stat_t *vs;
24085450Sbrendan 	uint_t vsc;
24095450Sbrendan 
24109425SEric.Schrock@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
24119425SEric.Schrock@Sun.COM 
24125450Sbrendan 	if (spa->spa_l2cache.sav_count == 0)
24135450Sbrendan 		return;
24145450Sbrendan 
24155450Sbrendan 	VERIFY(nvlist_lookup_nvlist(config,
24165450Sbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
24175450Sbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
24185450Sbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24195450Sbrendan 	if (nl2cache != 0) {
24205450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
24215450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
24225450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
24235450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
24245450Sbrendan 
24255450Sbrendan 		/*
24265450Sbrendan 		 * Update level 2 cache device stats.
24275450Sbrendan 		 */
24285450Sbrendan 
24295450Sbrendan 		for (i = 0; i < nl2cache; i++) {
24305450Sbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
24315450Sbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
24325450Sbrendan 
24335450Sbrendan 			vd = NULL;
24345450Sbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
24355450Sbrendan 				if (guid ==
24365450Sbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
24375450Sbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
24385450Sbrendan 					break;
24395450Sbrendan 				}
24405450Sbrendan 			}
24415450Sbrendan 			ASSERT(vd != NULL);
24425450Sbrendan 
24435450Sbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
244412296SLin.Ling@Sun.COM 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
244512296SLin.Ling@Sun.COM 			    == 0);
24465450Sbrendan 			vdev_get_stats(vd, vs);
24475450Sbrendan 		}
24485450Sbrendan 	}
24495450Sbrendan }
24505450Sbrendan 
2451789Sahrens int
24521544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2453789Sahrens {
2454789Sahrens 	int error;
2455789Sahrens 	spa_t *spa;
2456789Sahrens 
2457789Sahrens 	*config = NULL;
245810921STim.Haley@Sun.COM 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2459789Sahrens 
24609425SEric.Schrock@Sun.COM 	if (spa != NULL) {
24619425SEric.Schrock@Sun.COM 		/*
24629425SEric.Schrock@Sun.COM 		 * This still leaves a window of inconsistency where the spares
24639425SEric.Schrock@Sun.COM 		 * or l2cache devices could change and the config would be
24649425SEric.Schrock@Sun.COM 		 * self-inconsistent.
24659425SEric.Schrock@Sun.COM 		 */
24669425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
24679425SEric.Schrock@Sun.COM 
24689425SEric.Schrock@Sun.COM 		if (*config != NULL) {
2469*12817STim.Haley@Sun.COM 			uint64_t loadtimes[2];
2470*12817STim.Haley@Sun.COM 
2471*12817STim.Haley@Sun.COM 			loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2472*12817STim.Haley@Sun.COM 			loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2473*12817STim.Haley@Sun.COM 			VERIFY(nvlist_add_uint64_array(*config,
2474*12817STim.Haley@Sun.COM 			    ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2475*12817STim.Haley@Sun.COM 
24767754SJeff.Bonwick@Sun.COM 			VERIFY(nvlist_add_uint64(*config,
24779425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_ERRCOUNT,
24789425SEric.Schrock@Sun.COM 			    spa_get_errlog_size(spa)) == 0);
24799425SEric.Schrock@Sun.COM 
24809425SEric.Schrock@Sun.COM 			if (spa_suspended(spa))
24819425SEric.Schrock@Sun.COM 				VERIFY(nvlist_add_uint64(*config,
24829425SEric.Schrock@Sun.COM 				    ZPOOL_CONFIG_SUSPENDED,
24839425SEric.Schrock@Sun.COM 				    spa->spa_failmode) == 0);
24849425SEric.Schrock@Sun.COM 
24859425SEric.Schrock@Sun.COM 			spa_add_spares(spa, *config);
24869425SEric.Schrock@Sun.COM 			spa_add_l2cache(spa, *config);
24879425SEric.Schrock@Sun.COM 		}
24882082Seschrock 	}
24892082Seschrock 
24901544Seschrock 	/*
24911544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
24921544Seschrock 	 * and call spa_lookup() directly.
24931544Seschrock 	 */
24941544Seschrock 	if (altroot) {
24951544Seschrock 		if (spa == NULL) {
24961544Seschrock 			mutex_enter(&spa_namespace_lock);
24971544Seschrock 			spa = spa_lookup(name);
24981544Seschrock 			if (spa)
24991544Seschrock 				spa_altroot(spa, altroot, buflen);
25001544Seschrock 			else
25011544Seschrock 				altroot[0] = '\0';
25021544Seschrock 			spa = NULL;
25031544Seschrock 			mutex_exit(&spa_namespace_lock);
25041544Seschrock 		} else {
25051544Seschrock 			spa_altroot(spa, altroot, buflen);
25061544Seschrock 		}
25071544Seschrock 	}
25081544Seschrock 
25099425SEric.Schrock@Sun.COM 	if (spa != NULL) {
25109425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2511789Sahrens 		spa_close(spa, FTAG);
25129425SEric.Schrock@Sun.COM 	}
2513789Sahrens 
2514789Sahrens 	return (error);
2515789Sahrens }
2516789Sahrens 
2517789Sahrens /*
25185450Sbrendan  * Validate that the auxiliary device array is well formed.  We must have an
25195450Sbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
25205450Sbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
25215450Sbrendan  * specified, as long as they are well-formed.
25222082Seschrock  */
25232082Seschrock static int
25245450Sbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
25255450Sbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
25265450Sbrendan     vdev_labeltype_t label)
25272082Seschrock {
25285450Sbrendan 	nvlist_t **dev;
25295450Sbrendan 	uint_t i, ndev;
25302082Seschrock 	vdev_t *vd;
25312082Seschrock 	int error;
25322082Seschrock 
25337754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
25347754SJeff.Bonwick@Sun.COM 
25352082Seschrock 	/*
25365450Sbrendan 	 * It's acceptable to have no devs specified.
25372082Seschrock 	 */
25385450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
25392082Seschrock 		return (0);
25402082Seschrock 
25415450Sbrendan 	if (ndev == 0)
25422082Seschrock 		return (EINVAL);
25432082Seschrock 
25442082Seschrock 	/*
25455450Sbrendan 	 * Make sure the pool is formatted with a version that supports this
25465450Sbrendan 	 * device type.
25472082Seschrock 	 */
25485450Sbrendan 	if (spa_version(spa) < version)
25492082Seschrock 		return (ENOTSUP);
25502082Seschrock 
25513377Seschrock 	/*
25525450Sbrendan 	 * Set the pending device list so we correctly handle device in-use
25533377Seschrock 	 * checking.
25543377Seschrock 	 */
25555450Sbrendan 	sav->sav_pending = dev;
25565450Sbrendan 	sav->sav_npending = ndev;
25575450Sbrendan 
25585450Sbrendan 	for (i = 0; i < ndev; i++) {
25595450Sbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
25602082Seschrock 		    mode)) != 0)
25613377Seschrock 			goto out;
25622082Seschrock 
25632082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
25642082Seschrock 			vdev_free(vd);
25653377Seschrock 			error = EINVAL;
25663377Seschrock 			goto out;
25672082Seschrock 		}
25682082Seschrock 
25695450Sbrendan 		/*
25707754SJeff.Bonwick@Sun.COM 		 * The L2ARC currently only supports disk devices in
25717754SJeff.Bonwick@Sun.COM 		 * kernel context.  For user-level testing, we allow it.
25725450Sbrendan 		 */
25737754SJeff.Bonwick@Sun.COM #ifdef _KERNEL
25745450Sbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
25755450Sbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
25765450Sbrendan 			error = ENOTBLK;
25775450Sbrendan 			goto out;
25785450Sbrendan 		}
25797754SJeff.Bonwick@Sun.COM #endif
25802082Seschrock 		vd->vdev_top = vd;
25813377Seschrock 
25823377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
25835450Sbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
25845450Sbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
25853377Seschrock 			    vd->vdev_guid) == 0);
25862082Seschrock 		}
25872082Seschrock 
25882082Seschrock 		vdev_free(vd);
25893377Seschrock 
25905450Sbrendan 		if (error &&
25915450Sbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
25923377Seschrock 			goto out;
25933377Seschrock 		else
25943377Seschrock 			error = 0;
25952082Seschrock 	}
25962082Seschrock 
25973377Seschrock out:
25985450Sbrendan 	sav->sav_pending = NULL;
25995450Sbrendan 	sav->sav_npending = 0;
26003377Seschrock 	return (error);
26012082Seschrock }
26022082Seschrock 
26035450Sbrendan static int
26045450Sbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
26055450Sbrendan {
26065450Sbrendan 	int error;
26075450Sbrendan 
26087754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
26097754SJeff.Bonwick@Sun.COM 
26105450Sbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
26115450Sbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
26125450Sbrendan 	    VDEV_LABEL_SPARE)) != 0) {
26135450Sbrendan 		return (error);
26145450Sbrendan 	}
26155450Sbrendan 
26165450Sbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
26175450Sbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
26185450Sbrendan 	    VDEV_LABEL_L2CACHE));
26195450Sbrendan }
26205450Sbrendan 
26215450Sbrendan static void
26225450Sbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
26235450Sbrendan     const char *config)
26245450Sbrendan {
26255450Sbrendan 	int i;
26265450Sbrendan 
26275450Sbrendan 	if (sav->sav_config != NULL) {
26285450Sbrendan 		nvlist_t **olddevs;
26295450Sbrendan 		uint_t oldndevs;
26305450Sbrendan 		nvlist_t **newdevs;
26315450Sbrendan 
26325450Sbrendan 		/*
26335450Sbrendan 		 * Generate new dev list by concatentating with the
26345450Sbrendan 		 * current dev list.
26355450Sbrendan 		 */
26365450Sbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
26375450Sbrendan 		    &olddevs, &oldndevs) == 0);
26385450Sbrendan 
26395450Sbrendan 		newdevs = kmem_alloc(sizeof (void *) *
26405450Sbrendan 		    (ndevs + oldndevs), KM_SLEEP);
26415450Sbrendan 		for (i = 0; i < oldndevs; i++)
26425450Sbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
26435450Sbrendan 			    KM_SLEEP) == 0);
26445450Sbrendan 		for (i = 0; i < ndevs; i++)
26455450Sbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
26465450Sbrendan 			    KM_SLEEP) == 0);
26475450Sbrendan 
26485450Sbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
26495450Sbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
26505450Sbrendan 
26515450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
26525450Sbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
26535450Sbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
26545450Sbrendan 			nvlist_free(newdevs[i]);
26555450Sbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
26565450Sbrendan 	} else {
26575450Sbrendan 		/*
26585450Sbrendan 		 * Generate a new dev list.
26595450Sbrendan 		 */
26605450Sbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
26615450Sbrendan 		    KM_SLEEP) == 0);
26625450Sbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
26635450Sbrendan 		    devs, ndevs) == 0);
26645450Sbrendan 	}
26655450Sbrendan }
26665450Sbrendan 
26675450Sbrendan /*
26685450Sbrendan  * Stop and drop level 2 ARC devices
26695450Sbrendan  */
26705450Sbrendan void
26715450Sbrendan spa_l2cache_drop(spa_t *spa)
26725450Sbrendan {
26735450Sbrendan 	vdev_t *vd;
26745450Sbrendan 	int i;
26755450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
26765450Sbrendan 
26775450Sbrendan 	for (i = 0; i < sav->sav_count; i++) {
26785450Sbrendan 		uint64_t pool;
26795450Sbrendan 
26805450Sbrendan 		vd = sav->sav_vdevs[i];
26815450Sbrendan 		ASSERT(vd != NULL);
26825450Sbrendan 
26838241SJeff.Bonwick@Sun.COM 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
26848241SJeff.Bonwick@Sun.COM 		    pool != 0ULL && l2arc_vdev_present(vd))
26855450Sbrendan 			l2arc_remove_vdev(vd);
26865450Sbrendan 		if (vd->vdev_isl2cache)
26875450Sbrendan 			spa_l2cache_remove(vd);
26885450Sbrendan 		vdev_clear_stats(vd);
26895450Sbrendan 		(void) vdev_close(vd);
26905450Sbrendan 	}
26915450Sbrendan }
26925450Sbrendan 
26932082Seschrock /*
2694789Sahrens  * Pool Creation
2695789Sahrens  */
2696789Sahrens int
26975094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
26987184Stimh     const char *history_str, nvlist_t *zplprops)
2699789Sahrens {
2700789Sahrens 	spa_t *spa;
27015094Slling 	char *altroot = NULL;
27021635Sbonwick 	vdev_t *rvd;
2703789Sahrens 	dsl_pool_t *dp;
2704789Sahrens 	dmu_tx_t *tx;
27059816SGeorge.Wilson@Sun.COM 	int error = 0;
2706789Sahrens 	uint64_t txg = TXG_INITIAL;
27075450Sbrendan 	nvlist_t **spares, **l2cache;
27085450Sbrendan 	uint_t nspares, nl2cache;
270912470SMatthew.Ahrens@Sun.COM 	uint64_t version, obj;
2710789Sahrens 
2711789Sahrens 	/*
2712789Sahrens 	 * If this pool already exists, return failure.
2713789Sahrens 	 */
2714789Sahrens 	mutex_enter(&spa_namespace_lock);
2715789Sahrens 	if (spa_lookup(pool) != NULL) {
2716789Sahrens 		mutex_exit(&spa_namespace_lock);
2717789Sahrens 		return (EEXIST);
2718789Sahrens 	}
2719789Sahrens 
2720789Sahrens 	/*
2721789Sahrens 	 * Allocate a new spa_t structure.
2722789Sahrens 	 */
27235094Slling 	(void) nvlist_lookup_string(props,
27245094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
272510921STim.Haley@Sun.COM 	spa = spa_add(pool, NULL, altroot);
27268241SJeff.Bonwick@Sun.COM 	spa_activate(spa, spa_mode_global);
2727789Sahrens 
27285094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
27295094Slling 		spa_deactivate(spa);
27305094Slling 		spa_remove(spa);
27316643Seschrock 		mutex_exit(&spa_namespace_lock);
27325094Slling 		return (error);
27335094Slling 	}
27345094Slling 
27355094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
27365094Slling 	    &version) != 0)
27375094Slling 		version = SPA_VERSION;
27385094Slling 	ASSERT(version <= SPA_VERSION);
273910922SJeff.Bonwick@Sun.COM 
274010922SJeff.Bonwick@Sun.COM 	spa->spa_first_txg = txg;
274110922SJeff.Bonwick@Sun.COM 	spa->spa_uberblock.ub_txg = txg - 1;
27425094Slling 	spa->spa_uberblock.ub_version = version;
2743789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2744789Sahrens 
27451635Sbonwick 	/*
27469234SGeorge.Wilson@Sun.COM 	 * Create "The Godfather" zio to hold all async IOs
27479234SGeorge.Wilson@Sun.COM 	 */
27489630SJeff.Bonwick@Sun.COM 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
27499630SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
27509234SGeorge.Wilson@Sun.COM 
27519234SGeorge.Wilson@Sun.COM 	/*
27521635Sbonwick 	 * Create the root vdev.
27531635Sbonwick 	 */
27547754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27551635Sbonwick 
27562082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
27572082Seschrock 
27582082Seschrock 	ASSERT(error != 0 || rvd != NULL);
27592082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
27602082Seschrock 
27615913Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
27621635Sbonwick 		error = EINVAL;
27632082Seschrock 
27642082Seschrock 	if (error == 0 &&
27652082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
27665450Sbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
27672082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
27689816SGeorge.Wilson@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
27699816SGeorge.Wilson@Sun.COM 			vdev_metaslab_set_size(rvd->vdev_child[c]);
27709816SGeorge.Wilson@Sun.COM 			vdev_expand(rvd->vdev_child[c], txg);
27719816SGeorge.Wilson@Sun.COM 		}
27721635Sbonwick 	}
27731635Sbonwick 
27747754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
2775789Sahrens 
27762082Seschrock 	if (error != 0) {
2777789Sahrens 		spa_unload(spa);
2778789Sahrens 		spa_deactivate(spa);
2779789Sahrens 		spa_remove(spa);
2780789Sahrens 		mutex_exit(&spa_namespace_lock);
2781789Sahrens 		return (error);
2782789Sahrens 	}
2783789Sahrens 
27842082Seschrock 	/*
27852082Seschrock 	 * Get the list of spares, if specified.
27862082Seschrock 	 */
27872082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
27882082Seschrock 	    &spares, &nspares) == 0) {
27895450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
27902082Seschrock 		    KM_SLEEP) == 0);
27915450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
27922082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
27937754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
27942082Seschrock 		spa_load_spares(spa);
27957754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
27965450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
27975450Sbrendan 	}
27985450Sbrendan 
27995450Sbrendan 	/*
28005450Sbrendan 	 * Get the list of level 2 cache devices, if specified.
28015450Sbrendan 	 */
28025450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
28035450Sbrendan 	    &l2cache, &nl2cache) == 0) {
28045450Sbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
28055450Sbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
28065450Sbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
28075450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
28087754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28095450Sbrendan 		spa_load_l2cache(spa);
28107754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
28115450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
28122082Seschrock 	}
28132082Seschrock 
28147184Stimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2815789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2816789Sahrens 
281710956SGeorge.Wilson@Sun.COM 	/*
281810956SGeorge.Wilson@Sun.COM 	 * Create DDTs (dedup tables).
281910956SGeorge.Wilson@Sun.COM 	 */
282010956SGeorge.Wilson@Sun.COM 	ddt_create(spa);
282110956SGeorge.Wilson@Sun.COM 
282210956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
282310956SGeorge.Wilson@Sun.COM 
2824789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2825789Sahrens 
2826789Sahrens 	/*
2827789Sahrens 	 * Create the pool config object.
2828789Sahrens 	 */
2829789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
28307497STim.Haley@Sun.COM 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2831789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2832789Sahrens 
28331544Seschrock 	if (zap_add(spa->spa_meta_objset,
2834789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
28351544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
28361544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
28371544Seschrock 	}
2838789Sahrens 
283912296SLin.Ling@Sun.COM 	if (zap_add(spa->spa_meta_objset,
284012296SLin.Ling@Sun.COM 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
284112296SLin.Ling@Sun.COM 	    sizeof (uint64_t), 1, &version, tx) != 0) {
284212296SLin.Ling@Sun.COM 		cmn_err(CE_PANIC, "failed to add pool version");
284312296SLin.Ling@Sun.COM 	}
284412296SLin.Ling@Sun.COM 
28455094Slling 	/* Newly created pools with the right version are always deflated. */
28465094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
28475094Slling 		spa->spa_deflate = TRUE;
28485094Slling 		if (zap_add(spa->spa_meta_objset,
28495094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
28505094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
28515094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
28525094Slling 		}
28532082Seschrock 	}
28542082Seschrock 
2855789Sahrens 	/*
285612470SMatthew.Ahrens@Sun.COM 	 * Create the deferred-free bpobj.  Turn off compression
2857789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2858789Sahrens 	 * keeps changing.
2859789Sahrens 	 */
286012470SMatthew.Ahrens@Sun.COM 	obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
286112470SMatthew.Ahrens@Sun.COM 	dmu_object_set_compress(spa->spa_meta_objset, obj,
286212470SMatthew.Ahrens@Sun.COM 	    ZIO_COMPRESS_OFF, tx);
28631544Seschrock 	if (zap_add(spa->spa_meta_objset,
286412470SMatthew.Ahrens@Sun.COM 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
286512470SMatthew.Ahrens@Sun.COM 	    sizeof (uint64_t), 1, &obj, tx) != 0) {
286612470SMatthew.Ahrens@Sun.COM 		cmn_err(CE_PANIC, "failed to add bpobj");
28671544Seschrock 	}
286812470SMatthew.Ahrens@Sun.COM 	VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
286912470SMatthew.Ahrens@Sun.COM 	    spa->spa_meta_objset, obj));
2870789Sahrens 
28712926Sek110237 	/*
28722926Sek110237 	 * Create the pool's history object.
28732926Sek110237 	 */
28745094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
28755094Slling 		spa_history_create_obj(spa, tx);
28765094Slling 
28775094Slling 	/*
28785094Slling 	 * Set pool properties.
28795094Slling 	 */
28805094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
28815094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
28825329Sgw25295 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
28839816SGeorge.Wilson@Sun.COM 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
288410922SJeff.Bonwick@Sun.COM 
28858525SEric.Schrock@Sun.COM 	if (props != NULL) {
28868525SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
288712296SLin.Ling@Sun.COM 		spa_sync_props(spa, props, tx);
28888525SEric.Schrock@Sun.COM 	}
28892926Sek110237 
2890789Sahrens 	dmu_tx_commit(tx);
2891789Sahrens 
2892789Sahrens 	spa->spa_sync_on = B_TRUE;
2893789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2894789Sahrens 
2895789Sahrens 	/*
2896789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2897789Sahrens 	 * bean counters are appropriately updated.
2898789Sahrens 	 */
2899789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2900789Sahrens 
29016643Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2902789Sahrens 
29035094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
29044715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
29059946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_CREATE);
29064715Sek110237 
29078667SGeorge.Wilson@Sun.COM 	spa->spa_minref = refcount_count(&spa->spa_refcount);
29088667SGeorge.Wilson@Sun.COM 
2909789Sahrens 	mutex_exit(&spa_namespace_lock);
2910789Sahrens 
2911789Sahrens 	return (0);
2912789Sahrens }
2913789Sahrens 
29146423Sgw25295 #ifdef _KERNEL
29156423Sgw25295 /*
29169790SLin.Ling@Sun.COM  * Get the root pool information from the root disk, then import the root pool
29179790SLin.Ling@Sun.COM  * during the system boot up time.
29186423Sgw25295  */
29199790SLin.Ling@Sun.COM extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
29209790SLin.Ling@Sun.COM 
29219790SLin.Ling@Sun.COM static nvlist_t *
29229790SLin.Ling@Sun.COM spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
29236423Sgw25295 {
29249790SLin.Ling@Sun.COM 	nvlist_t *config;
29256423Sgw25295 	nvlist_t *nvtop, *nvroot;
29266423Sgw25295 	uint64_t pgid;
29276423Sgw25295 
29289790SLin.Ling@Sun.COM 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
29299790SLin.Ling@Sun.COM 		return (NULL);
29309790SLin.Ling@Sun.COM 
29316423Sgw25295 	/*
29326423Sgw25295 	 * Add this top-level vdev to the child array.
29336423Sgw25295 	 */
29349790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
29359790SLin.Ling@Sun.COM 	    &nvtop) == 0);
29369790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
29379790SLin.Ling@Sun.COM 	    &pgid) == 0);
29389790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
29396423Sgw25295 
29406423Sgw25295 	/*
29416423Sgw25295 	 * Put this pool's top-level vdevs into a root vdev.
29426423Sgw25295 	 */
29436423Sgw25295 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
29449790SLin.Ling@Sun.COM 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
29459790SLin.Ling@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
29466423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
29476423Sgw25295 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
29486423Sgw25295 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
29496423Sgw25295 	    &nvtop, 1) == 0);
29506423Sgw25295 
29516423Sgw25295 	/*
29526423Sgw25295 	 * Replace the existing vdev_tree with the new root vdev in
29536423Sgw25295 	 * this pool's configuration (remove the old, add the new).
29546423Sgw25295 	 */
29556423Sgw25295 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
29566423Sgw25295 	nvlist_free(nvroot);
29579790SLin.Ling@Sun.COM 	return (config);
29586423Sgw25295 }
29596423Sgw25295 
29606423Sgw25295 /*
29619790SLin.Ling@Sun.COM  * Walk the vdev tree and see if we can find a device with "better"
29629790SLin.Ling@Sun.COM  * configuration. A configuration is "better" if the label on that
29639790SLin.Ling@Sun.COM  * device has a more recent txg.
29646423Sgw25295  */
29659790SLin.Ling@Sun.COM static void
29669790SLin.Ling@Sun.COM spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
29677147Staylor {
29689816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
29699790SLin.Ling@Sun.COM 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
29709790SLin.Ling@Sun.COM 
29719790SLin.Ling@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
29729790SLin.Ling@Sun.COM 		nvlist_t *label;
29739790SLin.Ling@Sun.COM 		uint64_t label_txg;
29749790SLin.Ling@Sun.COM 
29759790SLin.Ling@Sun.COM 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
29769790SLin.Ling@Sun.COM 		    &label) != 0)
29779790SLin.Ling@Sun.COM 			return;
29789790SLin.Ling@Sun.COM 
29799790SLin.Ling@Sun.COM 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
29809790SLin.Ling@Sun.COM 		    &label_txg) == 0);
29819790SLin.Ling@Sun.COM 
29829790SLin.Ling@Sun.COM 		/*
29839790SLin.Ling@Sun.COM 		 * Do we have a better boot device?
29849790SLin.Ling@Sun.COM 		 */
29859790SLin.Ling@Sun.COM 		if (label_txg > *txg) {
29869790SLin.Ling@Sun.COM 			*txg = label_txg;
29879790SLin.Ling@Sun.COM 			*avd = vd;
29887147Staylor 		}
29899790SLin.Ling@Sun.COM 		nvlist_free(label);
29907147Staylor 	}
29917147Staylor }
29927147Staylor 
29936423Sgw25295 /*
29946423Sgw25295  * Import a root pool.
29956423Sgw25295  *
29967147Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
29977147Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
29987147Staylor  * The GRUB "findroot" command will return the vdev we should boot.
29996423Sgw25295  *
30006423Sgw25295  * For Sparc, devpath_list consists the physpath name of the booting device
30016423Sgw25295  * no matter the rootpool is a single device pool or a mirrored pool.
30026423Sgw25295  * e.g.
30036423Sgw25295  *	"/pci@1f,0/ide@d/disk@0,0:a"
30046423Sgw25295  */
30056423Sgw25295 int
30067147Staylor spa_import_rootpool(char *devpath, char *devid)
30076423Sgw25295 {
30089790SLin.Ling@Sun.COM 	spa_t *spa;
30099790SLin.Ling@Sun.COM 	vdev_t *rvd, *bvd, *avd = NULL;
30109790SLin.Ling@Sun.COM 	nvlist_t *config, *nvtop;
30119790SLin.Ling@Sun.COM 	uint64_t guid, txg;
30126423Sgw25295 	char *pname;
30136423Sgw25295 	int error;
30146423Sgw25295 
30156423Sgw25295 	/*
30169790SLin.Ling@Sun.COM 	 * Read the label from the boot device and generate a configuration.
30176423Sgw25295 	 */
301810822SJack.Meng@Sun.COM 	config = spa_generate_rootconf(devpath, devid, &guid);
301910822SJack.Meng@Sun.COM #if defined(_OBP) && defined(_KERNEL)
302010822SJack.Meng@Sun.COM 	if (config == NULL) {
302110822SJack.Meng@Sun.COM 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
302210822SJack.Meng@Sun.COM 			/* iscsi boot */
302310822SJack.Meng@Sun.COM 			get_iscsi_bootpath_phy(devpath);
302410822SJack.Meng@Sun.COM 			config = spa_generate_rootconf(devpath, devid, &guid);
302510822SJack.Meng@Sun.COM 		}
302610822SJack.Meng@Sun.COM 	}
302710822SJack.Meng@Sun.COM #endif
302810822SJack.Meng@Sun.COM 	if (config == NULL) {
30299790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
30309790SLin.Ling@Sun.COM 		    devpath);
30319790SLin.Ling@Sun.COM 		return (EIO);
30329790SLin.Ling@Sun.COM 	}
30339790SLin.Ling@Sun.COM 
30349790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
30359790SLin.Ling@Sun.COM 	    &pname) == 0);
30369790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
30376423Sgw25295 
30389425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
30399425SEric.Schrock@Sun.COM 	if ((spa = spa_lookup(pname)) != NULL) {
30409425SEric.Schrock@Sun.COM 		/*
30419425SEric.Schrock@Sun.COM 		 * Remove the existing root pool from the namespace so that we
30429425SEric.Schrock@Sun.COM 		 * can replace it with the correct config we just read in.
30439425SEric.Schrock@Sun.COM 		 */
30449425SEric.Schrock@Sun.COM 		spa_remove(spa);
30459425SEric.Schrock@Sun.COM 	}
30469425SEric.Schrock@Sun.COM 
304710921STim.Haley@Sun.COM 	spa = spa_add(pname, config, NULL);
30489425SEric.Schrock@Sun.COM 	spa->spa_is_root = B_TRUE;
304910100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
30509790SLin.Ling@Sun.COM 
30519790SLin.Ling@Sun.COM 	/*
30529790SLin.Ling@Sun.COM 	 * Build up a vdev tree based on the boot device's label config.
30539790SLin.Ling@Sun.COM 	 */
30549790SLin.Ling@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
30559790SLin.Ling@Sun.COM 	    &nvtop) == 0);
30569790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
30579790SLin.Ling@Sun.COM 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
30589790SLin.Ling@Sun.COM 	    VDEV_ALLOC_ROOTPOOL);
30599790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
30609790SLin.Ling@Sun.COM 	if (error) {
30619790SLin.Ling@Sun.COM 		mutex_exit(&spa_namespace_lock);
30629790SLin.Ling@Sun.COM 		nvlist_free(config);
30639790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
30649790SLin.Ling@Sun.COM 		    pname);
30659790SLin.Ling@Sun.COM 		return (error);
30669790SLin.Ling@Sun.COM 	}
30679790SLin.Ling@Sun.COM 
30689790SLin.Ling@Sun.COM 	/*
30699790SLin.Ling@Sun.COM 	 * Get the boot vdev.
30709790SLin.Ling@Sun.COM 	 */
30719790SLin.Ling@Sun.COM 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
30729790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
30739790SLin.Ling@Sun.COM 		    (u_longlong_t)guid);
30749790SLin.Ling@Sun.COM 		error = ENOENT;
30759790SLin.Ling@Sun.COM 		goto out;
30769790SLin.Ling@Sun.COM 	}
30779790SLin.Ling@Sun.COM 
30789790SLin.Ling@Sun.COM 	/*
30799790SLin.Ling@Sun.COM 	 * Determine if there is a better boot device.
30809790SLin.Ling@Sun.COM 	 */
30819790SLin.Ling@Sun.COM 	avd = bvd;
30829790SLin.Ling@Sun.COM 	spa_alt_rootvdev(rvd, &avd, &txg);
30839790SLin.Ling@Sun.COM 	if (avd != bvd) {
30849790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
30859790SLin.Ling@Sun.COM 		    "try booting from '%s'", avd->vdev_path);
30869790SLin.Ling@Sun.COM 		error = EINVAL;
30879790SLin.Ling@Sun.COM 		goto out;
30889790SLin.Ling@Sun.COM 	}
30899790SLin.Ling@Sun.COM 
30909790SLin.Ling@Sun.COM 	/*
30919790SLin.Ling@Sun.COM 	 * If the boot device is part of a spare vdev then ensure that
30929790SLin.Ling@Sun.COM 	 * we're booting off the active spare.
30939790SLin.Ling@Sun.COM 	 */
30949790SLin.Ling@Sun.COM 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30959790SLin.Ling@Sun.COM 	    !bvd->vdev_isspare) {
30969790SLin.Ling@Sun.COM 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
30979790SLin.Ling@Sun.COM 		    "try booting from '%s'",
30989790SLin.Ling@Sun.COM 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
30999790SLin.Ling@Sun.COM 		error = EINVAL;
31009790SLin.Ling@Sun.COM 		goto out;
31019790SLin.Ling@Sun.COM 	}
31029790SLin.Ling@Sun.COM 
31039790SLin.Ling@Sun.COM 	error = 0;
31049946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31059790SLin.Ling@Sun.COM out:
31069790SLin.Ling@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
31079790SLin.Ling@Sun.COM 	vdev_free(rvd);
31089790SLin.Ling@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
31099425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31106423Sgw25295 
31119790SLin.Ling@Sun.COM 	nvlist_free(config);
31126423Sgw25295 	return (error);
31136423Sgw25295 }
31149790SLin.Ling@Sun.COM 
31156423Sgw25295 #endif
31166423Sgw25295 
31176423Sgw25295 /*
31189425SEric.Schrock@Sun.COM  * Take a pool and insert it into the namespace as if it had been loaded at
31199425SEric.Schrock@Sun.COM  * boot.
31209425SEric.Schrock@Sun.COM  */
31219425SEric.Schrock@Sun.COM int
31229425SEric.Schrock@Sun.COM spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
31239425SEric.Schrock@Sun.COM {
31249425SEric.Schrock@Sun.COM 	spa_t *spa;
31259425SEric.Schrock@Sun.COM 	char *altroot = NULL;
31269425SEric.Schrock@Sun.COM 
31279425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
31289425SEric.Schrock@Sun.COM 	if (spa_lookup(pool) != NULL) {
31299425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31309425SEric.Schrock@Sun.COM 		return (EEXIST);
31319425SEric.Schrock@Sun.COM 	}
31329425SEric.Schrock@Sun.COM 
31339425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31349425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
313510921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
313610921STim.Haley@Sun.COM 
313710100SLin.Ling@Sun.COM 	spa->spa_load_verbatim = B_TRUE;
313810000SVictor.Latushkin@Sun.COM 
31399425SEric.Schrock@Sun.COM 	if (props != NULL)
31409425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
31419425SEric.Schrock@Sun.COM 
31429425SEric.Schrock@Sun.COM 	spa_config_sync(spa, B_FALSE, B_TRUE);
31439425SEric.Schrock@Sun.COM 
31449425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
31459946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
31469425SEric.Schrock@Sun.COM 
31479425SEric.Schrock@Sun.COM 	return (0);
31489425SEric.Schrock@Sun.COM }
31499425SEric.Schrock@Sun.COM 
31509425SEric.Schrock@Sun.COM /*
31516423Sgw25295  * Import a non-root pool into the system.
31526423Sgw25295  */
31536423Sgw25295 int
31546423Sgw25295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
31556423Sgw25295 {
31569425SEric.Schrock@Sun.COM 	spa_t *spa;
31579425SEric.Schrock@Sun.COM 	char *altroot = NULL;
315810921STim.Haley@Sun.COM 	spa_load_state_t state = SPA_LOAD_IMPORT;
315910921STim.Haley@Sun.COM 	zpool_rewind_policy_t policy;
31609425SEric.Schrock@Sun.COM 	int error;
31619425SEric.Schrock@Sun.COM 	nvlist_t *nvroot;
31629425SEric.Schrock@Sun.COM 	nvlist_t **spares, **l2cache;
31639425SEric.Schrock@Sun.COM 	uint_t nspares, nl2cache;
31649425SEric.Schrock@Sun.COM 
31659425SEric.Schrock@Sun.COM 	/*
31669425SEric.Schrock@Sun.COM 	 * If a pool with this name exists, return failure.
31679425SEric.Schrock@Sun.COM 	 */
31689425SEric.Schrock@Sun.COM 	mutex_enter(&spa_namespace_lock);
316911422SMark.Musante@Sun.COM 	if (spa_lookup(pool) != NULL) {
31709425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
31719425SEric.Schrock@Sun.COM 		return (EEXIST);
31729425SEric.Schrock@Sun.COM 	}
31739425SEric.Schrock@Sun.COM 
317410921STim.Haley@Sun.COM 	zpool_get_rewind_policy(config, &policy);
317510921STim.Haley@Sun.COM 	if (policy.zrp_request & ZPOOL_DO_REWIND)
317610921STim.Haley@Sun.COM 		state = SPA_LOAD_RECOVER;
317710921STim.Haley@Sun.COM 
31789425SEric.Schrock@Sun.COM 	/*
31799425SEric.Schrock@Sun.COM 	 * Create and initialize the spa structure.
31809425SEric.Schrock@Sun.COM 	 */
31819425SEric.Schrock@Sun.COM 	(void) nvlist_lookup_string(props,
31829425SEric.Schrock@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
318310921STim.Haley@Sun.COM 	spa = spa_add(pool, config, altroot);
31849425SEric.Schrock@Sun.COM 	spa_activate(spa, spa_mode_global);
31859425SEric.Schrock@Sun.COM 
31869425SEric.Schrock@Sun.COM 	/*
31879630SJeff.Bonwick@Sun.COM 	 * Don't start async tasks until we know everything is healthy.
31889630SJeff.Bonwick@Sun.COM 	 */
31899630SJeff.Bonwick@Sun.COM 	spa_async_suspend(spa);
31909630SJeff.Bonwick@Sun.COM 
31919630SJeff.Bonwick@Sun.COM 	/*
31929425SEric.Schrock@Sun.COM 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
31939425SEric.Schrock@Sun.COM 	 * because the user-supplied config is actually the one to trust when
31949425SEric.Schrock@Sun.COM 	 * doing an import.
31959425SEric.Schrock@Sun.COM 	 */
319610921STim.Haley@Sun.COM 	if (state != SPA_LOAD_RECOVER)
319710921STim.Haley@Sun.COM 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
319810921STim.Haley@Sun.COM 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
319911727SVictor.Latushkin@Sun.COM 	    policy.zrp_request);
320010921STim.Haley@Sun.COM 
320110921STim.Haley@Sun.COM 	/*
320210921STim.Haley@Sun.COM 	 * Propagate anything learned about failing or best txgs
320310921STim.Haley@Sun.COM 	 * back to caller
320410921STim.Haley@Sun.COM 	 */
320510921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
32069425SEric.Schrock@Sun.COM 
32079425SEric.Schrock@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32089425SEric.Schrock@Sun.COM 	/*
32099425SEric.Schrock@Sun.COM 	 * Toss any existing sparelist, as it doesn't have any validity
32109425SEric.Schrock@Sun.COM 	 * anymore, and conflicts with spa_has_spare().
32119425SEric.Schrock@Sun.COM 	 */
32129425SEric.Schrock@Sun.COM 	if (spa->spa_spares.sav_config) {
32139425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_spares.sav_config);
32149425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_config = NULL;
32159425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32169425SEric.Schrock@Sun.COM 	}
32179425SEric.Schrock@Sun.COM 	if (spa->spa_l2cache.sav_config) {
32189425SEric.Schrock@Sun.COM 		nvlist_free(spa->spa_l2cache.sav_config);
32199425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_config = NULL;
32209425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32219425SEric.Schrock@Sun.COM 	}
32229425SEric.Schrock@Sun.COM 
32239425SEric.Schrock@Sun.COM 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
32249425SEric.Schrock@Sun.COM 	    &nvroot) == 0);
32259425SEric.Schrock@Sun.COM 	if (error == 0)
32269425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32279425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_SPARE);
32289425SEric.Schrock@Sun.COM 	if (error == 0)
32299425SEric.Schrock@Sun.COM 		error = spa_validate_aux(spa, nvroot, -1ULL,
32309425SEric.Schrock@Sun.COM 		    VDEV_ALLOC_L2CACHE);
32319425SEric.Schrock@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
32329425SEric.Schrock@Sun.COM 
32339425SEric.Schrock@Sun.COM 	if (props != NULL)
32349425SEric.Schrock@Sun.COM 		spa_configfile_set(spa, props, B_FALSE);
32359425SEric.Schrock@Sun.COM 
32369425SEric.Schrock@Sun.COM 	if (error != 0 || (props && spa_writeable(spa) &&
32379425SEric.Schrock@Sun.COM 	    (error = spa_prop_set(spa, props)))) {
32389425SEric.Schrock@Sun.COM 		spa_unload(spa);
32399425SEric.Schrock@Sun.COM 		spa_deactivate(spa);
32409425SEric.Schrock@Sun.COM 		spa_remove(spa);
32419425SEric.Schrock@Sun.COM 		mutex_exit(&spa_namespace_lock);
32429425SEric.Schrock@Sun.COM 		return (error);
32439425SEric.Schrock@Sun.COM 	}
32449425SEric.Schrock@Sun.COM 
324512600SLin.Ling@Sun.COM 	spa_async_resume(spa);
324612600SLin.Ling@Sun.COM 
32479425SEric.Schrock@Sun.COM 	/*
32489425SEric.Schrock@Sun.COM 	 * Override any spares and level 2 cache devices as specified by
32499425SEric.Schrock@Sun.COM 	 * the user, as these may have correct device names/devids, etc.
32509425SEric.Schrock@Sun.COM 	 */
32519425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32529425SEric.Schrock@Sun.COM 	    &spares, &nspares) == 0) {
32539425SEric.Schrock@Sun.COM 		if (spa->spa_spares.sav_config)
32549425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
32559425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
32569425SEric.Schrock@Sun.COM 		else
32579425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
32589425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32599425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
32609425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
32619425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32629425SEric.Schrock@Sun.COM 		spa_load_spares(spa);
32639425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32649425SEric.Schrock@Sun.COM 		spa->spa_spares.sav_sync = B_TRUE;
32659425SEric.Schrock@Sun.COM 	}
32669425SEric.Schrock@Sun.COM 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
32679425SEric.Schrock@Sun.COM 	    &l2cache, &nl2cache) == 0) {
32689425SEric.Schrock@Sun.COM 		if (spa->spa_l2cache.sav_config)
32699425SEric.Schrock@Sun.COM 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
32709425SEric.Schrock@Sun.COM 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
32719425SEric.Schrock@Sun.COM 		else
32729425SEric.Schrock@Sun.COM 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
32739425SEric.Schrock@Sun.COM 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32749425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
32759425SEric.Schrock@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
32769425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32779425SEric.Schrock@Sun.COM 		spa_load_l2cache(spa);
32789425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
32799425SEric.Schrock@Sun.COM 		spa->spa_l2cache.sav_sync = B_TRUE;
32809425SEric.Schrock@Sun.COM 	}
32819425SEric.Schrock@Sun.COM 
328210672SEric.Schrock@Sun.COM 	/*
328310672SEric.Schrock@Sun.COM 	 * Check for any removed devices.
328410672SEric.Schrock@Sun.COM 	 */
328510672SEric.Schrock@Sun.COM 	if (spa->spa_autoreplace) {
328610672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_spares);
328710672SEric.Schrock@Sun.COM 		spa_aux_check_removed(&spa->spa_l2cache);
328810672SEric.Schrock@Sun.COM 	}
328910672SEric.Schrock@Sun.COM 
32909425SEric.Schrock@Sun.COM 	if (spa_writeable(spa)) {
32919425SEric.Schrock@Sun.COM 		/*
32929425SEric.Schrock@Sun.COM 		 * Update the config cache to include the newly-imported pool.
32939425SEric.Schrock@Sun.COM 		 */
329410100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
32959425SEric.Schrock@Sun.COM 	}
32969425SEric.Schrock@Sun.COM 
32979816SGeorge.Wilson@Sun.COM 	/*
32989816SGeorge.Wilson@Sun.COM 	 * It's possible that the pool was expanded while it was exported.
32999816SGeorge.Wilson@Sun.COM 	 * We kick off an async task to handle this for us.
33009816SGeorge.Wilson@Sun.COM 	 */
33019816SGeorge.Wilson@Sun.COM 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
33029816SGeorge.Wilson@Sun.COM 
33039425SEric.Schrock@Sun.COM 	mutex_exit(&spa_namespace_lock);
33049946SMark.Musante@Sun.COM 	spa_history_log_version(spa, LOG_POOL_IMPORT);
33059425SEric.Schrock@Sun.COM 
33069425SEric.Schrock@Sun.COM 	return (0);
33076643Seschrock }
33086643Seschrock 
3309789Sahrens nvlist_t *
3310789Sahrens spa_tryimport(nvlist_t *tryconfig)
3311789Sahrens {
3312789Sahrens 	nvlist_t *config = NULL;
3313789Sahrens 	char *poolname;
3314789Sahrens 	spa_t *spa;
3315789Sahrens 	uint64_t state;
33168680SLin.Ling@Sun.COM 	int error;
3317789Sahrens 
3318789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3319789Sahrens 		return (NULL);
3320789Sahrens 
3321789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3322789Sahrens 		return (NULL);
3323789Sahrens 
33241635Sbonwick 	/*
33251635Sbonwick 	 * Create and initialize the spa structure.
33261635Sbonwick 	 */
3327789Sahrens 	mutex_enter(&spa_namespace_lock);
332810921STim.Haley@Sun.COM 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
33298241SJeff.Bonwick@Sun.COM 	spa_activate(spa, FREAD);
3330789Sahrens 
3331789Sahrens 	/*
33321635Sbonwick 	 * Pass off the heavy lifting to spa_load().
33331732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
33341732Sbonwick 	 * is actually the one to trust when doing an import.
3335789Sahrens 	 */
333611422SMark.Musante@Sun.COM 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3337789Sahrens 
3338789Sahrens 	/*
3339789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
3340789Sahrens 	 */
3341789Sahrens 	if (spa->spa_root_vdev != NULL) {
3342789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3343789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3344789Sahrens 		    poolname) == 0);
3345789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3346789Sahrens 		    state) == 0);
33473975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
33483975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
33492082Seschrock 
33502082Seschrock 		/*
33516423Sgw25295 		 * If the bootfs property exists on this pool then we
33526423Sgw25295 		 * copy it out so that external consumers can tell which
33536423Sgw25295 		 * pools are bootable.
33546423Sgw25295 		 */
33558680SLin.Ling@Sun.COM 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
33566423Sgw25295 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33576423Sgw25295 
33586423Sgw25295 			/*
33596423Sgw25295 			 * We have to play games with the name since the
33606423Sgw25295 			 * pool was opened as TRYIMPORT_NAME.
33616423Sgw25295 			 */
33627754SJeff.Bonwick@Sun.COM 			if (dsl_dsobj_to_dsname(spa_name(spa),
33636423Sgw25295 			    spa->spa_bootfs, tmpname) == 0) {
33646423Sgw25295 				char *cp;
33656423Sgw25295 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
33666423Sgw25295 
33676423Sgw25295 				cp = strchr(tmpname, '/');
33686423Sgw25295 				if (cp == NULL) {
33696423Sgw25295 					(void) strlcpy(dsname, tmpname,
33706423Sgw25295 					    MAXPATHLEN);
33716423Sgw25295 				} else {
33726423Sgw25295 					(void) snprintf(dsname, MAXPATHLEN,
33736423Sgw25295 					    "%s/%s", poolname, ++cp);
33746423Sgw25295 				}
33756423Sgw25295 				VERIFY(nvlist_add_string(config,
33766423Sgw25295 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
33776423Sgw25295 				kmem_free(dsname, MAXPATHLEN);
33786423Sgw25295 			}
33796423Sgw25295 			kmem_free(tmpname, MAXPATHLEN);
33806423Sgw25295 		}
33816423Sgw25295 
33826423Sgw25295 		/*
33835450Sbrendan 		 * Add the list of hot spares and level 2 cache devices.
33842082Seschrock 		 */
33859425SEric.Schrock@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
33862082Seschrock 		spa_add_spares(spa, config);
33875450Sbrendan 		spa_add_l2cache(spa, config);
33889425SEric.Schrock@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3389789Sahrens 	}
3390789Sahrens 
3391789Sahrens 	spa_unload(spa);
3392789Sahrens 	spa_deactivate(spa);
3393789Sahrens 	spa_remove(spa);
3394789Sahrens 	mutex_exit(&spa_namespace_lock);
3395789Sahrens 
3396789Sahrens 	return (config);
3397789Sahrens }
3398789Sahrens 
3399789Sahrens /*
3400789Sahrens  * Pool export/destroy
3401789Sahrens  *
3402789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
3403789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
3404789Sahrens  * update the pool state and sync all the labels to disk, removing the
34058211SGeorge.Wilson@Sun.COM  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
34068211SGeorge.Wilson@Sun.COM  * we don't sync the labels or remove the configuration cache.
3407789Sahrens  */
3408789Sahrens static int
34097214Slling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
34108211SGeorge.Wilson@Sun.COM     boolean_t force, boolean_t hardforce)
3411789Sahrens {
3412789Sahrens 	spa_t *spa;
3413789Sahrens 
34141775Sbillm 	if (oldconfig)
34151775Sbillm 		*oldconfig = NULL;
34161775Sbillm 
34178241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
3418789Sahrens 		return (EROFS);
3419789Sahrens 
3420789Sahrens 	mutex_enter(&spa_namespace_lock);
3421789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
3422789Sahrens 		mutex_exit(&spa_namespace_lock);
3423789Sahrens 		return (ENOENT);
3424789Sahrens 	}
3425789Sahrens 
3426789Sahrens 	/*
34271544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
34281544Seschrock 	 * reacquire the namespace lock, and see if we can export.
34291544Seschrock 	 */
34301544Seschrock 	spa_open_ref(spa, FTAG);
34311544Seschrock 	mutex_exit(&spa_namespace_lock);
34321544Seschrock 	spa_async_suspend(spa);
34331544Seschrock 	mutex_enter(&spa_namespace_lock);
34341544Seschrock 	spa_close(spa, FTAG);
34351544Seschrock 
34361544Seschrock 	/*
3437789Sahrens 	 * The pool will be in core if it's openable,
3438789Sahrens 	 * in which case we can modify its state.
3439789Sahrens 	 */
3440789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3441789Sahrens 		/*
3442789Sahrens 		 * Objsets may be open only because they're dirty, so we
3443789Sahrens 		 * have to force it to sync before checking spa_refcnt.
3444789Sahrens 		 */
3445789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
3446789Sahrens 
34471544Seschrock 		/*
34481544Seschrock 		 * A pool cannot be exported or destroyed if there are active
34491544Seschrock 		 * references.  If we are resetting a pool, allow references by
34501544Seschrock 		 * fault injection handlers.
34511544Seschrock 		 */
34521544Seschrock 		if (!spa_refcount_zero(spa) ||
34531544Seschrock 		    (spa->spa_inject_ref != 0 &&
34541544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
34551544Seschrock 			spa_async_resume(spa);
3456789Sahrens 			mutex_exit(&spa_namespace_lock);
3457789Sahrens 			return (EBUSY);
3458789Sahrens 		}
3459789Sahrens 
3460789Sahrens 		/*
34617214Slling 		 * A pool cannot be exported if it has an active shared spare.
34627214Slling 		 * This is to prevent other pools stealing the active spare
34637214Slling 		 * from an exported pool. At user's own will, such pool can
34647214Slling 		 * be forcedly exported.
34657214Slling 		 */
34667214Slling 		if (!force && new_state == POOL_STATE_EXPORTED &&
34677214Slling 		    spa_has_active_shared_spare(spa)) {
34687214Slling 			spa_async_resume(spa);
34697214Slling 			mutex_exit(&spa_namespace_lock);
34707214Slling 			return (EXDEV);
34717214Slling 		}
34727214Slling 
34737214Slling 		/*
3474789Sahrens 		 * We want this to be reflected on every label,
3475789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
3476789Sahrens 		 * final sync that pushes these changes out.
3477789Sahrens 		 */
34788211SGeorge.Wilson@Sun.COM 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
34797754SJeff.Bonwick@Sun.COM 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34801544Seschrock 			spa->spa_state = new_state;
348112296SLin.Ling@Sun.COM 			spa->spa_final_txg = spa_last_synced_txg(spa) +
348212296SLin.Ling@Sun.COM 			    TXG_DEFER_SIZE + 1;
34831544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
34847754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_ALL, FTAG);
34851544Seschrock 		}
3486789Sahrens 	}
3487789Sahrens 
34884451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
34894451Seschrock 
3490789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3491789Sahrens 		spa_unload(spa);
3492789Sahrens 		spa_deactivate(spa);
3493789Sahrens 	}
3494789Sahrens 
34951775Sbillm 	if (oldconfig && spa->spa_config)
34961775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
34971775Sbillm 
34981544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
34998211SGeorge.Wilson@Sun.COM 		if (!hardforce)
35008211SGeorge.Wilson@Sun.COM 			spa_config_sync(spa, B_TRUE, B_TRUE);
35011544Seschrock 		spa_remove(spa);
35021544Seschrock 	}
3503789Sahrens 	mutex_exit(&spa_namespace_lock);
3504789Sahrens 
3505789Sahrens 	return (0);
3506789Sahrens }
3507789Sahrens 
3508789Sahrens /*
3509789Sahrens  * Destroy a storage pool.
3510789Sahrens  */
3511789Sahrens int
3512789Sahrens spa_destroy(char *pool)
3513789Sahrens {
35148211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
35158211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
3516789Sahrens }
3517789Sahrens 
3518789Sahrens /*
3519789Sahrens  * Export a storage pool.
3520789Sahrens  */
3521789Sahrens int
35228211SGeorge.Wilson@Sun.COM spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
35238211SGeorge.Wilson@Sun.COM     boolean_t hardforce)
3524789Sahrens {
35258211SGeorge.Wilson@Sun.COM 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
35268211SGeorge.Wilson@Sun.COM 	    force, hardforce));
3527789Sahrens }
3528789Sahrens 
3529789Sahrens /*
35301544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
35311544Seschrock  * from the namespace in any way.
35321544Seschrock  */
35331544Seschrock int
35341544Seschrock spa_reset(char *pool)
35351544Seschrock {
35367214Slling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
35378211SGeorge.Wilson@Sun.COM 	    B_FALSE, B_FALSE));
35381544Seschrock }
35391544Seschrock 
35401544Seschrock /*
3541789Sahrens  * ==========================================================================
3542789Sahrens  * Device manipulation
3543789Sahrens  * ==========================================================================
3544789Sahrens  */
3545789Sahrens 
3546789Sahrens /*
35474527Sperrin  * Add a device to a storage pool.
3548789Sahrens  */
3549789Sahrens int
3550789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3551789Sahrens {
355210594SGeorge.Wilson@Sun.COM 	uint64_t txg, id;
35538241SJeff.Bonwick@Sun.COM 	int error;
3554789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
35551585Sbonwick 	vdev_t *vd, *tvd;
35565450Sbrendan 	nvlist_t **spares, **l2cache;
35575450Sbrendan 	uint_t nspares, nl2cache;
3558789Sahrens 
3559789Sahrens 	txg = spa_vdev_enter(spa);
3560789Sahrens 
35612082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
35622082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
35632082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
35642082Seschrock 
35657754SJeff.Bonwick@Sun.COM 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3566789Sahrens 
35675450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
35685450Sbrendan 	    &nspares) != 0)
35692082Seschrock 		nspares = 0;
35702082Seschrock 
35715450Sbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
35725450Sbrendan 	    &nl2cache) != 0)
35735450Sbrendan 		nl2cache = 0;
35745450Sbrendan 
35757754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
35762082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
35777754SJeff.Bonwick@Sun.COM 
35787754SJeff.Bonwick@Sun.COM 	if (vd->vdev_children != 0 &&
35797754SJeff.Bonwick@Sun.COM 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
35807754SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, vd, txg, error));
35812082Seschrock 
35823377Seschrock 	/*
35835450Sbrendan 	 * We must validate the spares and l2cache devices after checking the
35845450Sbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
35853377Seschrock 	 */
35867754SJeff.Bonwick@Sun.COM 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
35873377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
35883377Seschrock 
35893377Seschrock 	/*
35903377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
35913377Seschrock 	 */
35928241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
359310594SGeorge.Wilson@Sun.COM 
359410594SGeorge.Wilson@Sun.COM 		/*
359510594SGeorge.Wilson@Sun.COM 		 * Set the vdev id to the first hole, if one exists.
359610594SGeorge.Wilson@Sun.COM 		 */
359710594SGeorge.Wilson@Sun.COM 		for (id = 0; id < rvd->vdev_children; id++) {
359810594SGeorge.Wilson@Sun.COM 			if (rvd->vdev_child[id]->vdev_ishole) {
359910594SGeorge.Wilson@Sun.COM 				vdev_free(rvd->vdev_child[id]);
360010594SGeorge.Wilson@Sun.COM 				break;
360110594SGeorge.Wilson@Sun.COM 			}
360210594SGeorge.Wilson@Sun.COM 		}
36033377Seschrock 		tvd = vd->vdev_child[c];
36043377Seschrock 		vdev_remove_child(vd, tvd);
360510594SGeorge.Wilson@Sun.COM 		tvd->vdev_id = id;
36063377Seschrock 		vdev_add_child(rvd, tvd);
36073377Seschrock 		vdev_config_dirty(tvd);
36083377Seschrock 	}
36093377Seschrock 
36102082Seschrock 	if (nspares != 0) {
36115450Sbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
36125450Sbrendan 		    ZPOOL_CONFIG_SPARES);
36132082Seschrock 		spa_load_spares(spa);
36145450Sbrendan 		spa->spa_spares.sav_sync = B_TRUE;
36155450Sbrendan 	}
36165450Sbrendan 
36175450Sbrendan 	if (nl2cache != 0) {
36185450Sbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
36195450Sbrendan 		    ZPOOL_CONFIG_L2CACHE);
36205450Sbrendan 		spa_load_l2cache(spa);
36215450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3622789Sahrens 	}
3623789Sahrens 
3624789Sahrens 	/*
36251585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
36261585Sbonwick 	 * If other threads start allocating from these vdevs before we
36271585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
36281585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
36291585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
36301585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
36311635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
36321585Sbonwick 	 *
36331585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
36341585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
36351585Sbonwick 	 * steps will be completed the next time we load the pool.
3636789Sahrens 	 */
36371635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
36381585Sbonwick 
36391635Sbonwick 	mutex_enter(&spa_namespace_lock);
36401635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36411635Sbonwick 	mutex_exit(&spa_namespace_lock);
3642789Sahrens 
36431635Sbonwick 	return (0);
3644789Sahrens }
3645789Sahrens 
3646789Sahrens /*
3647789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
3648789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
3649789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
3650789Sahrens  *
3651789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
3652789Sahrens  * existing device; in this case the two devices are made into their own
36534451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
3654789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
3655789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
3656789Sahrens  * completion of resilvering, the first disk (the one being replaced)
3657789Sahrens  * is automatically detached.
3658789Sahrens  */
3659789Sahrens int
36601544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3661789Sahrens {
366212296SLin.Ling@Sun.COM 	uint64_t txg, dtl_max_txg;
3663789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3664789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
36652082Seschrock 	vdev_ops_t *pvops;
36667313SEric.Kustarz@Sun.COM 	char *oldvdpath, *newvdpath;
36677313SEric.Kustarz@Sun.COM 	int newvd_isspare;
36687313SEric.Kustarz@Sun.COM 	int error;
3669789Sahrens 
3670789Sahrens 	txg = spa_vdev_enter(spa);
3671789Sahrens 
36726643Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3673789Sahrens 
3674789Sahrens 	if (oldvd == NULL)
3675789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3676789Sahrens 
36771585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
36781585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
36791585Sbonwick 
3680789Sahrens 	pvd = oldvd->vdev_parent;
3681789Sahrens 
36822082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
36834451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
36844451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
36854451Seschrock 
36864451Seschrock 	if (newrootvd->vdev_children != 1)
3687789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3688789Sahrens 
3689789Sahrens 	newvd = newrootvd->vdev_child[0];
3690789Sahrens 
3691789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
3692789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3693789Sahrens 
36942082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3695789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3696789Sahrens 
36974527Sperrin 	/*
36984527Sperrin 	 * Spares can't replace logs
36994527Sperrin 	 */
37007326SEric.Schrock@Sun.COM 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
37014527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37024527Sperrin 
37032082Seschrock 	if (!replacing) {
37042082Seschrock 		/*
37052082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
37062082Seschrock 		 * vdev.
37072082Seschrock 		 */
37082082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
37092082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
37102082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37112082Seschrock 
37122082Seschrock 		pvops = &vdev_mirror_ops;
37132082Seschrock 	} else {
37142082Seschrock 		/*
37152082Seschrock 		 * Active hot spares can only be replaced by inactive hot
37162082Seschrock 		 * spares.
37172082Seschrock 		 */
37182082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
37192082Seschrock 		    pvd->vdev_child[1] == oldvd &&
37202082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
37212082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37222082Seschrock 
37232082Seschrock 		/*
37242082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
37252082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
37263377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
37273377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
37283377Seschrock 		 * the same (spare replaces spare, non-spare replaces
37293377Seschrock 		 * non-spare).
37302082Seschrock 		 */
37312082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
37322082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37333377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
37343377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
37353377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
37362082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
37372082Seschrock 		    newvd->vdev_isspare)
37382082Seschrock 			pvops = &vdev_spare_ops;
37392082Seschrock 		else
37402082Seschrock 			pvops = &vdev_replacing_ops;
37412082Seschrock 	}
37422082Seschrock 
37431175Slling 	/*
37449816SGeorge.Wilson@Sun.COM 	 * Make sure the new device is big enough.
37451175Slling 	 */
37469816SGeorge.Wilson@Sun.COM 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3747789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3748789Sahrens 
37491732Sbonwick 	/*
37501732Sbonwick 	 * The new device cannot have a higher alignment requirement
37511732Sbonwick 	 * than the top-level vdev.
37521732Sbonwick 	 */
37531732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3754789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3755789Sahrens 
3756789Sahrens 	/*
3757789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
3758789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
3759789Sahrens 	 */
3760789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3761789Sahrens 		spa_strfree(oldvd->vdev_path);
3762789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3763789Sahrens 		    KM_SLEEP);
3764789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3765789Sahrens 		    newvd->vdev_path, "old");
3766789Sahrens 		if (oldvd->vdev_devid != NULL) {
3767789Sahrens 			spa_strfree(oldvd->vdev_devid);
3768789Sahrens 			oldvd->vdev_devid = NULL;
3769789Sahrens 		}
3770789Sahrens 	}
3771789Sahrens 
3772789Sahrens 	/*
37732082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
37742082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
3775789Sahrens 	 */
3776789Sahrens 	if (pvd->vdev_ops != pvops)
3777789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
3778789Sahrens 
3779789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3780789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
3781789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3782789Sahrens 
3783789Sahrens 	/*
3784789Sahrens 	 * Extract the new device from its root and add it to pvd.
3785789Sahrens 	 */
3786789Sahrens 	vdev_remove_child(newrootvd, newvd);
3787789Sahrens 	newvd->vdev_id = pvd->vdev_children;
378810594SGeorge.Wilson@Sun.COM 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3789789Sahrens 	vdev_add_child(pvd, newvd);
3790789Sahrens 
3791789Sahrens 	tvd = newvd->vdev_top;
3792789Sahrens 	ASSERT(pvd->vdev_top == tvd);
3793789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3794789Sahrens 
3795789Sahrens 	vdev_config_dirty(tvd);
3796789Sahrens 
3797789Sahrens 	/*
379812296SLin.Ling@Sun.COM 	 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
379912296SLin.Ling@Sun.COM 	 * for any dmu_sync-ed blocks.  It will propagate upward when
380012296SLin.Ling@Sun.COM 	 * spa_vdev_exit() calls vdev_dtl_reassess().
3801789Sahrens 	 */
380212296SLin.Ling@Sun.COM 	dtl_max_txg = txg + TXG_CONCURRENT_STATES;
380312296SLin.Ling@Sun.COM 
380412296SLin.Ling@Sun.COM 	vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
380512296SLin.Ling@Sun.COM 	    dtl_max_txg - TXG_INITIAL);
3806789Sahrens 
38079425SEric.Schrock@Sun.COM 	if (newvd->vdev_isspare) {
38083377Seschrock 		spa_spare_activate(newvd);
38099425SEric.Schrock@Sun.COM 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
38109425SEric.Schrock@Sun.COM 	}
38119425SEric.Schrock@Sun.COM 
38127754SJeff.Bonwick@Sun.COM 	oldvdpath = spa_strdup(oldvd->vdev_path);
38137754SJeff.Bonwick@Sun.COM 	newvdpath = spa_strdup(newvd->vdev_path);
38147313SEric.Kustarz@Sun.COM 	newvd_isspare = newvd->vdev_isspare;
38151544Seschrock 
3816789Sahrens 	/*
3817789Sahrens 	 * Mark newvd's DTL dirty in this txg.
3818789Sahrens 	 */
38191732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3820789Sahrens 
382112296SLin.Ling@Sun.COM 	/*
382212296SLin.Ling@Sun.COM 	 * Restart the resilver
382312296SLin.Ling@Sun.COM 	 */
382412296SLin.Ling@Sun.COM 	dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
382512296SLin.Ling@Sun.COM 
382612296SLin.Ling@Sun.COM 	/*
382712296SLin.Ling@Sun.COM 	 * Commit the config
382812296SLin.Ling@Sun.COM 	 */
382912296SLin.Ling@Sun.COM 	(void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
383012296SLin.Ling@Sun.COM 
383112296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
383212296SLin.Ling@Sun.COM 	    "%s vdev=%s %s vdev=%s",
38339946SMark.Musante@Sun.COM 	    replacing && newvd_isspare ? "spare in" :
38349946SMark.Musante@Sun.COM 	    replacing ? "replace" : "attach", newvdpath,
38359946SMark.Musante@Sun.COM 	    replacing ? "for" : "to", oldvdpath);
38367313SEric.Kustarz@Sun.COM 
38377313SEric.Kustarz@Sun.COM 	spa_strfree(oldvdpath);
38387313SEric.Kustarz@Sun.COM 	spa_strfree(newvdpath);
38397313SEric.Kustarz@Sun.COM 
3840789Sahrens 	return (0);
3841789Sahrens }
3842789Sahrens 
3843789Sahrens /*
3844789Sahrens  * Detach a device from a mirror or replacing vdev.
3845789Sahrens  * If 'replace_done' is specified, only detach if the parent
3846789Sahrens  * is a replacing vdev.
3847789Sahrens  */
3848789Sahrens int
38498241SJeff.Bonwick@Sun.COM spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3850789Sahrens {
3851789Sahrens 	uint64_t txg;
38528241SJeff.Bonwick@Sun.COM 	int error;
3853789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3854789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
38552082Seschrock 	boolean_t unspare = B_FALSE;
38562082Seschrock 	uint64_t unspare_guid;
38576673Seschrock 	size_t len;
385811422SMark.Musante@Sun.COM 	char *vdpath;
3859789Sahrens 
3860789Sahrens 	txg = spa_vdev_enter(spa);
3861789Sahrens 
38626643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3863789Sahrens 
3864789Sahrens 	if (vd == NULL)
3865789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3866789Sahrens 
38671585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
38681585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38691585Sbonwick 
3870789Sahrens 	pvd = vd->vdev_parent;
3871789Sahrens 
3872789Sahrens 	/*
38738241SJeff.Bonwick@Sun.COM 	 * If the parent/child relationship is not as expected, don't do it.
38748241SJeff.Bonwick@Sun.COM 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
38758241SJeff.Bonwick@Sun.COM 	 * vdev that's replacing B with C.  The user's intent in replacing
38768241SJeff.Bonwick@Sun.COM 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
38778241SJeff.Bonwick@Sun.COM 	 * the replace by detaching C, the expected behavior is to end up
38788241SJeff.Bonwick@Sun.COM 	 * M(A,B).  But suppose that right after deciding to detach C,
38798241SJeff.Bonwick@Sun.COM 	 * the replacement of B completes.  We would have M(A,C), and then
38808241SJeff.Bonwick@Sun.COM 	 * ask to detach C, which would leave us with just A -- not what
38818241SJeff.Bonwick@Sun.COM 	 * the user wanted.  To prevent this, we make sure that the
38828241SJeff.Bonwick@Sun.COM 	 * parent/child relationship hasn't changed -- in this example,
38838241SJeff.Bonwick@Sun.COM 	 * that C's parent is still the replacing vdev R.
38848241SJeff.Bonwick@Sun.COM 	 */
38858241SJeff.Bonwick@Sun.COM 	if (pvd->vdev_guid != pguid && pguid != 0)
38868241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
38878241SJeff.Bonwick@Sun.COM 
38888241SJeff.Bonwick@Sun.COM 	/*
3889789Sahrens 	 * If replace_done is specified, only remove this device if it's
38902082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
38912082Seschrock 	 * disk can be removed.
3892789Sahrens 	 */
38932082Seschrock 	if (replace_done) {
38942082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
38952082Seschrock 			if (vd->vdev_id != 0)
38962082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38972082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
38982082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
38992082Seschrock 		}
39002082Seschrock 	}
39012082Seschrock 
39022082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
39034577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3904789Sahrens 
3905789Sahrens 	/*
39062082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3907789Sahrens 	 */
3908789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
39092082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
39102082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3911789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3912789Sahrens 
3913789Sahrens 	/*
39148241SJeff.Bonwick@Sun.COM 	 * If this device has the only valid copy of some data,
39158241SJeff.Bonwick@Sun.COM 	 * we cannot safely detach it.
3916789Sahrens 	 */
39178241SJeff.Bonwick@Sun.COM 	if (vdev_dtl_required(vd))
3918789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3919789Sahrens 
39208241SJeff.Bonwick@Sun.COM 	ASSERT(pvd->vdev_children >= 2);
39218241SJeff.Bonwick@Sun.COM 
3922789Sahrens 	/*
39236673Seschrock 	 * If we are detaching the second disk from a replacing vdev, then
39246673Seschrock 	 * check to see if we changed the original vdev's path to have "/old"
39256673Seschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
39266673Seschrock 	 */
39276673Seschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
39286673Seschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
39296673Seschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
39306673Seschrock 		ASSERT(pvd->vdev_child[1] == vd);
39316673Seschrock 		cvd = pvd->vdev_child[0];
39326673Seschrock 		len = strlen(vd->vdev_path);
39336673Seschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
39346673Seschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
39356673Seschrock 			spa_strfree(cvd->vdev_path);
39366673Seschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
39376673Seschrock 		}
39386673Seschrock 	}
39396673Seschrock 
39406673Seschrock 	/*
39412082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
39422082Seschrock 	 * that the spare should become a real disk, and be removed from the
39432082Seschrock 	 * active spare list for the pool.
39442082Seschrock 	 */
39452082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
39468241SJeff.Bonwick@Sun.COM 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
39472082Seschrock 		unspare = B_TRUE;
39482082Seschrock 
39492082Seschrock 	/*
3950789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3951789Sahrens 	 * This must be done after all other error cases are handled,
3952789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3953789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3954789Sahrens 	 * it may be that the unwritability of the disk is the reason
3955789Sahrens 	 * it's being detached!
3956789Sahrens 	 */
39573377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3958789Sahrens 
3959789Sahrens 	/*
3960789Sahrens 	 * Remove vd from its parent and compact the parent's children.
3961789Sahrens 	 */
3962789Sahrens 	vdev_remove_child(pvd, vd);
3963789Sahrens 	vdev_compact_children(pvd);
3964789Sahrens 
3965789Sahrens 	/*
3966789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3967789Sahrens 	 */
3968789Sahrens 	cvd = pvd->vdev_child[0];
3969789Sahrens 
3970789Sahrens 	/*
39712082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
39728241SJeff.Bonwick@Sun.COM 	 * do it now, marking the vdev as no longer a spare in the process.
39738241SJeff.Bonwick@Sun.COM 	 * We must do this before vdev_remove_parent(), because that can
39748241SJeff.Bonwick@Sun.COM 	 * change the GUID if it creates a new toplevel GUID.  For a similar
39758241SJeff.Bonwick@Sun.COM 	 * reason, we must remove the spare now, in the same txg as the detach;
39768241SJeff.Bonwick@Sun.COM 	 * otherwise someone could attach a new sibling, change the GUID, and
39778241SJeff.Bonwick@Sun.COM 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
39782082Seschrock 	 */
39792082Seschrock 	if (unspare) {
39802082Seschrock 		ASSERT(cvd->vdev_isspare);
39813377Seschrock 		spa_spare_remove(cvd);
39822082Seschrock 		unspare_guid = cvd->vdev_guid;
39838241SJeff.Bonwick@Sun.COM 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
39842082Seschrock 	}
39852082Seschrock 
39862082Seschrock 	/*
3987789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3988789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3989789Sahrens 	 */
3990789Sahrens 	if (pvd->vdev_children == 1)
3991789Sahrens 		vdev_remove_parent(cvd);
3992789Sahrens 
3993789Sahrens 	/*
3994789Sahrens 	 * We don't set tvd until now because the parent we just removed
3995789Sahrens 	 * may have been the previous top-level vdev.
3996789Sahrens 	 */
3997789Sahrens 	tvd = cvd->vdev_top;
3998789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3999789Sahrens 
4000789Sahrens 	/*
40013377Seschrock 	 * Reevaluate the parent vdev state.
4002789Sahrens 	 */
40034451Seschrock 	vdev_propagate_state(cvd);
4004789Sahrens 
4005789Sahrens 	/*
40069816SGeorge.Wilson@Sun.COM 	 * If the 'autoexpand' property is set on the pool then automatically
40079816SGeorge.Wilson@Sun.COM 	 * try to expand the size of the pool. For example if the device we
40089816SGeorge.Wilson@Sun.COM 	 * just detached was smaller than the others, it may be possible to
40099816SGeorge.Wilson@Sun.COM 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
40109816SGeorge.Wilson@Sun.COM 	 * first so that we can obtain the updated sizes of the leaf vdevs.
4011789Sahrens 	 */
40129816SGeorge.Wilson@Sun.COM 	if (spa->spa_autoexpand) {
40139816SGeorge.Wilson@Sun.COM 		vdev_reopen(tvd);
40149816SGeorge.Wilson@Sun.COM 		vdev_expand(tvd, txg);
40159816SGeorge.Wilson@Sun.COM 	}
4016789Sahrens 
4017789Sahrens 	vdev_config_dirty(tvd);
4018789Sahrens 
4019789Sahrens 	/*
40203377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
40213377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
40223377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
40233377Seschrock 	 * prevent vd from being accessed after it's freed.
4024789Sahrens 	 */
402511422SMark.Musante@Sun.COM 	vdpath = spa_strdup(vd->vdev_path);
40268241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
4027789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
40281732Sbonwick 	vd->vdev_detached = B_TRUE;
40291732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
4030789Sahrens 
40314451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
40324451Seschrock 
40332082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
40342082Seschrock 
403512296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
403611422SMark.Musante@Sun.COM 	    "vdev=%s", vdpath);
403711422SMark.Musante@Sun.COM 	spa_strfree(vdpath);
403811422SMark.Musante@Sun.COM 
40392082Seschrock 	/*
40403377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
40413377Seschrock 	 * then we want to go through and remove the device from the hot spare
40423377Seschrock 	 * list of every other pool.
40432082Seschrock 	 */
40442082Seschrock 	if (unspare) {
40458241SJeff.Bonwick@Sun.COM 		spa_t *myspa = spa;
40462082Seschrock 		spa = NULL;
40472082Seschrock 		mutex_enter(&spa_namespace_lock);
40482082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
40492082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
40502082Seschrock 				continue;
40518241SJeff.Bonwick@Sun.COM 			if (spa == myspa)
40528241SJeff.Bonwick@Sun.COM 				continue;
40537793SJeff.Bonwick@Sun.COM 			spa_open_ref(spa, FTAG);
40547793SJeff.Bonwick@Sun.COM 			mutex_exit(&spa_namespace_lock);
405512296SLin.Ling@Sun.COM 			(void) spa_vdev_remove(spa, unspare_guid,
405612296SLin.Ling@Sun.COM 			    B_TRUE);
40577793SJeff.Bonwick@Sun.COM 			mutex_enter(&spa_namespace_lock);
40587793SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
40592082Seschrock 		}
40602082Seschrock 		mutex_exit(&spa_namespace_lock);
40612082Seschrock 	}
40622082Seschrock 
40632082Seschrock 	return (error);
40642082Seschrock }
40652082Seschrock 
406611422SMark.Musante@Sun.COM /*
406711422SMark.Musante@Sun.COM  * Split a set of devices from their mirrors, and create a new pool from them.
406811422SMark.Musante@Sun.COM  */
406911422SMark.Musante@Sun.COM int
407011422SMark.Musante@Sun.COM spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
407111422SMark.Musante@Sun.COM     nvlist_t *props, boolean_t exp)
407211422SMark.Musante@Sun.COM {
407311422SMark.Musante@Sun.COM 	int error = 0;
407411422SMark.Musante@Sun.COM 	uint64_t txg, *glist;
407511422SMark.Musante@Sun.COM 	spa_t *newspa;
407611422SMark.Musante@Sun.COM 	uint_t c, children, lastlog;
407711422SMark.Musante@Sun.COM 	nvlist_t **child, *nvl, *tmp;
407811422SMark.Musante@Sun.COM 	dmu_tx_t *tx;
407911422SMark.Musante@Sun.COM 	char *altroot = NULL;
408011422SMark.Musante@Sun.COM 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
408111422SMark.Musante@Sun.COM 	boolean_t activate_slog;
408211422SMark.Musante@Sun.COM 
408311422SMark.Musante@Sun.COM 	if (!spa_writeable(spa))
408411422SMark.Musante@Sun.COM 		return (EROFS);
408511422SMark.Musante@Sun.COM 
408611422SMark.Musante@Sun.COM 	txg = spa_vdev_enter(spa);
408711422SMark.Musante@Sun.COM 
408811422SMark.Musante@Sun.COM 	/* clear the log and flush everything up to now */
408911422SMark.Musante@Sun.COM 	activate_slog = spa_passivate_log(spa);
409011422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
409111422SMark.Musante@Sun.COM 	error = spa_offline_log(spa);
409211422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
409311422SMark.Musante@Sun.COM 
409411422SMark.Musante@Sun.COM 	if (activate_slog)
409511422SMark.Musante@Sun.COM 		spa_activate_log(spa);
409611422SMark.Musante@Sun.COM 
409711422SMark.Musante@Sun.COM 	if (error != 0)
409811422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
409911422SMark.Musante@Sun.COM 
410011422SMark.Musante@Sun.COM 	/* check new spa name before going any further */
410111422SMark.Musante@Sun.COM 	if (spa_lookup(newname) != NULL)
410211422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
410311422SMark.Musante@Sun.COM 
410411422SMark.Musante@Sun.COM 	/*
410511422SMark.Musante@Sun.COM 	 * scan through all the children to ensure they're all mirrors
410611422SMark.Musante@Sun.COM 	 */
410711422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
410811422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
410911422SMark.Musante@Sun.COM 	    &children) != 0)
411011422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
411111422SMark.Musante@Sun.COM 
411211422SMark.Musante@Sun.COM 	/* first, check to ensure we've got the right child count */
411311422SMark.Musante@Sun.COM 	rvd = spa->spa_root_vdev;
411411422SMark.Musante@Sun.COM 	lastlog = 0;
411511422SMark.Musante@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
411611422SMark.Musante@Sun.COM 		vdev_t *vd = rvd->vdev_child[c];
411711422SMark.Musante@Sun.COM 
411811422SMark.Musante@Sun.COM 		/* don't count the holes & logs as children */
411911422SMark.Musante@Sun.COM 		if (vd->vdev_islog || vd->vdev_ishole) {
412011422SMark.Musante@Sun.COM 			if (lastlog == 0)
412111422SMark.Musante@Sun.COM 				lastlog = c;
412211422SMark.Musante@Sun.COM 			continue;
412311422SMark.Musante@Sun.COM 		}
412411422SMark.Musante@Sun.COM 
412511422SMark.Musante@Sun.COM 		lastlog = 0;
412611422SMark.Musante@Sun.COM 	}
412711422SMark.Musante@Sun.COM 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
412811422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
412911422SMark.Musante@Sun.COM 
413011422SMark.Musante@Sun.COM 	/* next, ensure no spare or cache devices are part of the split */
413111422SMark.Musante@Sun.COM 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
413211422SMark.Musante@Sun.COM 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
413311422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
413411422SMark.Musante@Sun.COM 
413511422SMark.Musante@Sun.COM 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
413611422SMark.Musante@Sun.COM 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
413711422SMark.Musante@Sun.COM 
413811422SMark.Musante@Sun.COM 	/* then, loop over each vdev and validate it */
413911422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
414011422SMark.Musante@Sun.COM 		uint64_t is_hole = 0;
414111422SMark.Musante@Sun.COM 
414211422SMark.Musante@Sun.COM 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
414311422SMark.Musante@Sun.COM 		    &is_hole);
414411422SMark.Musante@Sun.COM 
414511422SMark.Musante@Sun.COM 		if (is_hole != 0) {
414611422SMark.Musante@Sun.COM 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
414711422SMark.Musante@Sun.COM 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
414811422SMark.Musante@Sun.COM 				continue;
414911422SMark.Musante@Sun.COM 			} else {
415011422SMark.Musante@Sun.COM 				error = EINVAL;
415111422SMark.Musante@Sun.COM 				break;
415211422SMark.Musante@Sun.COM 			}
415311422SMark.Musante@Sun.COM 		}
415411422SMark.Musante@Sun.COM 
415511422SMark.Musante@Sun.COM 		/* which disk is going to be split? */
415611422SMark.Musante@Sun.COM 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
415711422SMark.Musante@Sun.COM 		    &glist[c]) != 0) {
415811422SMark.Musante@Sun.COM 			error = EINVAL;
415911422SMark.Musante@Sun.COM 			break;
416011422SMark.Musante@Sun.COM 		}
416111422SMark.Musante@Sun.COM 
416211422SMark.Musante@Sun.COM 		/* look it up in the spa */
416311422SMark.Musante@Sun.COM 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
416411422SMark.Musante@Sun.COM 		if (vml[c] == NULL) {
416511422SMark.Musante@Sun.COM 			error = ENODEV;
416611422SMark.Musante@Sun.COM 			break;
416711422SMark.Musante@Sun.COM 		}
416811422SMark.Musante@Sun.COM 
416911422SMark.Musante@Sun.COM 		/* make sure there's nothing stopping the split */
417011422SMark.Musante@Sun.COM 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
417111422SMark.Musante@Sun.COM 		    vml[c]->vdev_islog ||
417211422SMark.Musante@Sun.COM 		    vml[c]->vdev_ishole ||
417311422SMark.Musante@Sun.COM 		    vml[c]->vdev_isspare ||
417411422SMark.Musante@Sun.COM 		    vml[c]->vdev_isl2cache ||
417511422SMark.Musante@Sun.COM 		    !vdev_writeable(vml[c]) ||
417611497SMark.Musante@Sun.COM 		    vml[c]->vdev_children != 0 ||
417711422SMark.Musante@Sun.COM 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
417811422SMark.Musante@Sun.COM 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
417911422SMark.Musante@Sun.COM 			error = EINVAL;
418011422SMark.Musante@Sun.COM 			break;
418111422SMark.Musante@Sun.COM 		}
418211422SMark.Musante@Sun.COM 
418311422SMark.Musante@Sun.COM 		if (vdev_dtl_required(vml[c])) {
418411422SMark.Musante@Sun.COM 			error = EBUSY;
418511422SMark.Musante@Sun.COM 			break;
418611422SMark.Musante@Sun.COM 		}
418711422SMark.Musante@Sun.COM 
418811422SMark.Musante@Sun.COM 		/* we need certain info from the top level */
418911422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
419011422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_array) == 0);
419111422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
419211422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
419311422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
419411422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_asize) == 0);
419511422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
419611422SMark.Musante@Sun.COM 		    vml[c]->vdev_top->vdev_ashift) == 0);
419711422SMark.Musante@Sun.COM 	}
419811422SMark.Musante@Sun.COM 
419911422SMark.Musante@Sun.COM 	if (error != 0) {
420011422SMark.Musante@Sun.COM 		kmem_free(vml, children * sizeof (vdev_t *));
420111422SMark.Musante@Sun.COM 		kmem_free(glist, children * sizeof (uint64_t));
420211422SMark.Musante@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
420311422SMark.Musante@Sun.COM 	}
420411422SMark.Musante@Sun.COM 
420511422SMark.Musante@Sun.COM 	/* stop writers from using the disks */
420611422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
420711422SMark.Musante@Sun.COM 		if (vml[c] != NULL)
420811422SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_TRUE;
420911422SMark.Musante@Sun.COM 	}
421011422SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
421111422SMark.Musante@Sun.COM 
421211422SMark.Musante@Sun.COM 	/*
421311422SMark.Musante@Sun.COM 	 * Temporarily record the splitting vdevs in the spa config.  This
421411422SMark.Musante@Sun.COM 	 * will disappear once the config is regenerated.
421511422SMark.Musante@Sun.COM 	 */
421611422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
421711422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
421811422SMark.Musante@Sun.COM 	    glist, children) == 0);
421911422SMark.Musante@Sun.COM 	kmem_free(glist, children * sizeof (uint64_t));
422011422SMark.Musante@Sun.COM 
422111864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
422211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
422311422SMark.Musante@Sun.COM 	    nvl) == 0);
422411864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
422511422SMark.Musante@Sun.COM 	spa->spa_config_splitting = nvl;
422611422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
422711422SMark.Musante@Sun.COM 
422811422SMark.Musante@Sun.COM 	/* configure and create the new pool */
422911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
423011422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
423111422SMark.Musante@Sun.COM 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
423211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
423311422SMark.Musante@Sun.COM 	    spa_version(spa)) == 0);
423411422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
423511422SMark.Musante@Sun.COM 	    spa->spa_config_txg) == 0);
423611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
423711422SMark.Musante@Sun.COM 	    spa_generate_guid(NULL)) == 0);
423811422SMark.Musante@Sun.COM 	(void) nvlist_lookup_string(props,
423911422SMark.Musante@Sun.COM 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
424011422SMark.Musante@Sun.COM 
424111497SMark.Musante@Sun.COM 	/* add the new pool to the namespace */
424211422SMark.Musante@Sun.COM 	newspa = spa_add(newname, config, altroot);
424311422SMark.Musante@Sun.COM 	newspa->spa_config_txg = spa->spa_config_txg;
424411422SMark.Musante@Sun.COM 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
424511422SMark.Musante@Sun.COM 
424611422SMark.Musante@Sun.COM 	/* release the spa config lock, retaining the namespace lock */
424711422SMark.Musante@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
424811422SMark.Musante@Sun.COM 
424911422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
425011422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 1);
425111422SMark.Musante@Sun.COM 
425211422SMark.Musante@Sun.COM 	spa_activate(newspa, spa_mode_global);
425311422SMark.Musante@Sun.COM 	spa_async_suspend(newspa);
425411422SMark.Musante@Sun.COM 
425511422SMark.Musante@Sun.COM 	/* create the new pool from the disks of the original pool */
425611422SMark.Musante@Sun.COM 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
425711422SMark.Musante@Sun.COM 	if (error)
425811422SMark.Musante@Sun.COM 		goto out;
425911422SMark.Musante@Sun.COM 
426011422SMark.Musante@Sun.COM 	/* if that worked, generate a real config for the new pool */
426111422SMark.Musante@Sun.COM 	if (newspa->spa_root_vdev != NULL) {
426211422SMark.Musante@Sun.COM 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
426311422SMark.Musante@Sun.COM 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
426411422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
426511422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
426611422SMark.Musante@Sun.COM 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
426711422SMark.Musante@Sun.COM 		    B_TRUE));
426811422SMark.Musante@Sun.COM 	}
426911422SMark.Musante@Sun.COM 
427011422SMark.Musante@Sun.COM 	/* set the props */
427111422SMark.Musante@Sun.COM 	if (props != NULL) {
427211422SMark.Musante@Sun.COM 		spa_configfile_set(newspa, props, B_FALSE);
427311422SMark.Musante@Sun.COM 		error = spa_prop_set(newspa, props);
427411422SMark.Musante@Sun.COM 		if (error)
427511422SMark.Musante@Sun.COM 			goto out;
427611422SMark.Musante@Sun.COM 	}
427711422SMark.Musante@Sun.COM 
427811422SMark.Musante@Sun.COM 	/* flush everything */
427911422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(newspa);
428011422SMark.Musante@Sun.COM 	vdev_config_dirty(newspa->spa_root_vdev);
428111422SMark.Musante@Sun.COM 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
428211422SMark.Musante@Sun.COM 
428311422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
428411422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 2);
428511422SMark.Musante@Sun.COM 
428611422SMark.Musante@Sun.COM 	spa_async_resume(newspa);
428711422SMark.Musante@Sun.COM 
428811422SMark.Musante@Sun.COM 	/* finally, update the original pool's config */
428911422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
429011422SMark.Musante@Sun.COM 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
429111422SMark.Musante@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
429211422SMark.Musante@Sun.COM 	if (error != 0)
429311422SMark.Musante@Sun.COM 		dmu_tx_abort(tx);
429411422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
429511422SMark.Musante@Sun.COM 		if (vml[c] != NULL) {
429611422SMark.Musante@Sun.COM 			vdev_split(vml[c]);
429711422SMark.Musante@Sun.COM 			if (error == 0)
429812296SLin.Ling@Sun.COM 				spa_history_log_internal(LOG_POOL_VDEV_DETACH,
429912296SLin.Ling@Sun.COM 				    spa, tx, "vdev=%s",
430011422SMark.Musante@Sun.COM 				    vml[c]->vdev_path);
430111422SMark.Musante@Sun.COM 			vdev_free(vml[c]);
430211422SMark.Musante@Sun.COM 		}
430311422SMark.Musante@Sun.COM 	}
430411422SMark.Musante@Sun.COM 	vdev_config_dirty(spa->spa_root_vdev);
430511422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
430611422SMark.Musante@Sun.COM 	nvlist_free(nvl);
430711422SMark.Musante@Sun.COM 	if (error == 0)
430811422SMark.Musante@Sun.COM 		dmu_tx_commit(tx);
430911422SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, 0);
431011422SMark.Musante@Sun.COM 
431111422SMark.Musante@Sun.COM 	if (zio_injection_enabled)
431211422SMark.Musante@Sun.COM 		zio_handle_panic_injection(spa, FTAG, 3);
431311422SMark.Musante@Sun.COM 
431411422SMark.Musante@Sun.COM 	/* split is complete; log a history record */
431512296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
431611422SMark.Musante@Sun.COM 	    "split new pool %s from pool %s", newname, spa_name(spa));
431711422SMark.Musante@Sun.COM 
431811422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
431911422SMark.Musante@Sun.COM 
432011422SMark.Musante@Sun.COM 	/* if we're not going to mount the filesystems in userland, export */
432111422SMark.Musante@Sun.COM 	if (exp)
432211422SMark.Musante@Sun.COM 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
432311422SMark.Musante@Sun.COM 		    B_FALSE, B_FALSE);
432411422SMark.Musante@Sun.COM 
432511422SMark.Musante@Sun.COM 	return (error);
432611422SMark.Musante@Sun.COM 
432711422SMark.Musante@Sun.COM out:
432811422SMark.Musante@Sun.COM 	spa_unload(newspa);
432911422SMark.Musante@Sun.COM 	spa_deactivate(newspa);
433011422SMark.Musante@Sun.COM 	spa_remove(newspa);
433111422SMark.Musante@Sun.COM 
433211422SMark.Musante@Sun.COM 	txg = spa_vdev_config_enter(spa);
433311864SMark.Musante@Sun.COM 
433411864SMark.Musante@Sun.COM 	/* re-online all offlined disks */
433511864SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
433611864SMark.Musante@Sun.COM 		if (vml[c] != NULL)
433711864SMark.Musante@Sun.COM 			vml[c]->vdev_offline = B_FALSE;
433811864SMark.Musante@Sun.COM 	}
433911864SMark.Musante@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
434011864SMark.Musante@Sun.COM 
434111422SMark.Musante@Sun.COM 	nvlist_free(spa->spa_config_splitting);
434211422SMark.Musante@Sun.COM 	spa->spa_config_splitting = NULL;
434311497SMark.Musante@Sun.COM 	(void) spa_vdev_exit(spa, NULL, txg, error);
434411422SMark.Musante@Sun.COM 
434511422SMark.Musante@Sun.COM 	kmem_free(vml, children * sizeof (vdev_t *));
434611422SMark.Musante@Sun.COM 	return (error);
434711422SMark.Musante@Sun.COM }
434811422SMark.Musante@Sun.COM 
43497754SJeff.Bonwick@Sun.COM static nvlist_t *
43507754SJeff.Bonwick@Sun.COM spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
43512082Seschrock {
43527754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++) {
43537754SJeff.Bonwick@Sun.COM 		uint64_t guid;
43547754SJeff.Bonwick@Sun.COM 
43557754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
43567754SJeff.Bonwick@Sun.COM 		    &guid) == 0);
43577754SJeff.Bonwick@Sun.COM 
43587754SJeff.Bonwick@Sun.COM 		if (guid == target_guid)
43597754SJeff.Bonwick@Sun.COM 			return (nvpp[i]);
43602082Seschrock 	}
43612082Seschrock 
43627754SJeff.Bonwick@Sun.COM 	return (NULL);
43635450Sbrendan }
43645450Sbrendan 
43657754SJeff.Bonwick@Sun.COM static void
43667754SJeff.Bonwick@Sun.COM spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
43677754SJeff.Bonwick@Sun.COM 	nvlist_t *dev_to_remove)
43685450Sbrendan {
43697754SJeff.Bonwick@Sun.COM 	nvlist_t **newdev = NULL;
43707754SJeff.Bonwick@Sun.COM 
43717754SJeff.Bonwick@Sun.COM 	if (count > 1)
43727754SJeff.Bonwick@Sun.COM 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
43737754SJeff.Bonwick@Sun.COM 
43747754SJeff.Bonwick@Sun.COM 	for (int i = 0, j = 0; i < count; i++) {
43757754SJeff.Bonwick@Sun.COM 		if (dev[i] == dev_to_remove)
43767754SJeff.Bonwick@Sun.COM 			continue;
43777754SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
43785450Sbrendan 	}
43795450Sbrendan 
43807754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
43817754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
43827754SJeff.Bonwick@Sun.COM 
43837754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count - 1; i++)
43847754SJeff.Bonwick@Sun.COM 		nvlist_free(newdev[i]);
43857754SJeff.Bonwick@Sun.COM 
43867754SJeff.Bonwick@Sun.COM 	if (count > 1)
43877754SJeff.Bonwick@Sun.COM 		kmem_free(newdev, (count - 1) * sizeof (void *));
43885450Sbrendan }
43895450Sbrendan 
43905450Sbrendan /*
439110594SGeorge.Wilson@Sun.COM  * Evacuate the device.
439210594SGeorge.Wilson@Sun.COM  */
439312296SLin.Ling@Sun.COM static int
439410594SGeorge.Wilson@Sun.COM spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
439510594SGeorge.Wilson@Sun.COM {
439612296SLin.Ling@Sun.COM 	uint64_t txg;
439710974SJeff.Bonwick@Sun.COM 	int error = 0;
439810594SGeorge.Wilson@Sun.COM 
439910594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
440010594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
440110922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
440210594SGeorge.Wilson@Sun.COM 
440310594SGeorge.Wilson@Sun.COM 	/*
440410594SGeorge.Wilson@Sun.COM 	 * Evacuate the device.  We don't hold the config lock as writer
440510594SGeorge.Wilson@Sun.COM 	 * since we need to do I/O but we do keep the
440610594SGeorge.Wilson@Sun.COM 	 * spa_namespace_lock held.  Once this completes the device
440710594SGeorge.Wilson@Sun.COM 	 * should no longer have any blocks allocated on it.
440810594SGeorge.Wilson@Sun.COM 	 */
440910594SGeorge.Wilson@Sun.COM 	if (vd->vdev_islog) {
441012296SLin.Ling@Sun.COM 		if (vd->vdev_stat.vs_alloc != 0)
441112296SLin.Ling@Sun.COM 			error = spa_offline_log(spa);
441210974SJeff.Bonwick@Sun.COM 	} else {
441312296SLin.Ling@Sun.COM 		error = ENOTSUP;
441410594SGeorge.Wilson@Sun.COM 	}
441510594SGeorge.Wilson@Sun.COM 
441610974SJeff.Bonwick@Sun.COM 	if (error)
441710974SJeff.Bonwick@Sun.COM 		return (error);
441810974SJeff.Bonwick@Sun.COM 
441910594SGeorge.Wilson@Sun.COM 	/*
442010974SJeff.Bonwick@Sun.COM 	 * The evacuation succeeded.  Remove any remaining MOS metadata
442110974SJeff.Bonwick@Sun.COM 	 * associated with this vdev, and wait for these changes to sync.
442210594SGeorge.Wilson@Sun.COM 	 */
442312296SLin.Ling@Sun.COM 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
442410594SGeorge.Wilson@Sun.COM 	txg = spa_vdev_config_enter(spa);
442510594SGeorge.Wilson@Sun.COM 	vd->vdev_removing = B_TRUE;
442610594SGeorge.Wilson@Sun.COM 	vdev_dirty(vd, 0, NULL, txg);
442710594SGeorge.Wilson@Sun.COM 	vdev_config_dirty(vd);
442810594SGeorge.Wilson@Sun.COM 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
442910594SGeorge.Wilson@Sun.COM 
443010594SGeorge.Wilson@Sun.COM 	return (0);
443110594SGeorge.Wilson@Sun.COM }
443210594SGeorge.Wilson@Sun.COM 
443310594SGeorge.Wilson@Sun.COM /*
443410594SGeorge.Wilson@Sun.COM  * Complete the removal by cleaning up the namespace.
443510594SGeorge.Wilson@Sun.COM  */
443612296SLin.Ling@Sun.COM static void
443710974SJeff.Bonwick@Sun.COM spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
443810594SGeorge.Wilson@Sun.COM {
443910594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
444010594SGeorge.Wilson@Sun.COM 	uint64_t id = vd->vdev_id;
444110594SGeorge.Wilson@Sun.COM 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
444210594SGeorge.Wilson@Sun.COM 
444310594SGeorge.Wilson@Sun.COM 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
444410594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
444510922SJeff.Bonwick@Sun.COM 	ASSERT(vd == vd->vdev_top);
444610594SGeorge.Wilson@Sun.COM 
444712296SLin.Ling@Sun.COM 	/*
444812296SLin.Ling@Sun.COM 	 * Only remove any devices which are empty.
444912296SLin.Ling@Sun.COM 	 */
445012296SLin.Ling@Sun.COM 	if (vd->vdev_stat.vs_alloc != 0)
445112296SLin.Ling@Sun.COM 		return;
445212296SLin.Ling@Sun.COM 
445310594SGeorge.Wilson@Sun.COM 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
445410922SJeff.Bonwick@Sun.COM 
445510922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_state_dirty_node))
445610922SJeff.Bonwick@Sun.COM 		vdev_state_clean(vd);
445710922SJeff.Bonwick@Sun.COM 	if (list_link_active(&vd->vdev_config_dirty_node))
445810922SJeff.Bonwick@Sun.COM 		vdev_config_clean(vd);
445910922SJeff.Bonwick@Sun.COM 
446010594SGeorge.Wilson@Sun.COM 	vdev_free(vd);
446110594SGeorge.Wilson@Sun.COM 
446210594SGeorge.Wilson@Sun.COM 	if (last_vdev) {
446310594SGeorge.Wilson@Sun.COM 		vdev_compact_children(rvd);
446410594SGeorge.Wilson@Sun.COM 	} else {
446510594SGeorge.Wilson@Sun.COM 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
446610594SGeorge.Wilson@Sun.COM 		vdev_add_child(rvd, vd);
446710594SGeorge.Wilson@Sun.COM 	}
446812352SLin.Ling@Sun.COM 	vdev_config_dirty(rvd);
446912352SLin.Ling@Sun.COM 
447012352SLin.Ling@Sun.COM 	/*
447112352SLin.Ling@Sun.COM 	 * Reassess the health of our root vdev.
447212352SLin.Ling@Sun.COM 	 */
447312352SLin.Ling@Sun.COM 	vdev_reopen(rvd);
447410594SGeorge.Wilson@Sun.COM }
447510594SGeorge.Wilson@Sun.COM 
447610594SGeorge.Wilson@Sun.COM /*
447712296SLin.Ling@Sun.COM  * Remove a device from the pool -
447812296SLin.Ling@Sun.COM  *
447912296SLin.Ling@Sun.COM  * Removing a device from the vdev namespace requires several steps
448012296SLin.Ling@Sun.COM  * and can take a significant amount of time.  As a result we use
448112296SLin.Ling@Sun.COM  * the spa_vdev_config_[enter/exit] functions which allow us to
448212296SLin.Ling@Sun.COM  * grab and release the spa_config_lock while still holding the namespace
448312296SLin.Ling@Sun.COM  * lock.  During each step the configuration is synced out.
448412296SLin.Ling@Sun.COM  */
448512296SLin.Ling@Sun.COM 
448612296SLin.Ling@Sun.COM /*
44875450Sbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
448810594SGeorge.Wilson@Sun.COM  * spares, slogs, and level 2 ARC devices.
44895450Sbrendan  */
44905450Sbrendan int
44915450Sbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
44925450Sbrendan {
44935450Sbrendan 	vdev_t *vd;
449410974SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
44957754SJeff.Bonwick@Sun.COM 	nvlist_t **spares, **l2cache, *nv;
449610594SGeorge.Wilson@Sun.COM 	uint64_t txg = 0;
44975450Sbrendan 	uint_t nspares, nl2cache;
44985450Sbrendan 	int error = 0;
44998241SJeff.Bonwick@Sun.COM 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
45008241SJeff.Bonwick@Sun.COM 
45018241SJeff.Bonwick@Sun.COM 	if (!locked)
45028241SJeff.Bonwick@Sun.COM 		txg = spa_vdev_enter(spa);
45035450Sbrendan 
45046643Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
45055450Sbrendan 
45065450Sbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
45075450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
45087754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
45097754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
45107754SJeff.Bonwick@Sun.COM 		/*
45117754SJeff.Bonwick@Sun.COM 		 * Only remove the hot spare if it's not currently in use
45127754SJeff.Bonwick@Sun.COM 		 * in this pool.
45137754SJeff.Bonwick@Sun.COM 		 */
45147754SJeff.Bonwick@Sun.COM 		if (vd == NULL || unspare) {
45157754SJeff.Bonwick@Sun.COM 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
45167754SJeff.Bonwick@Sun.COM 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
45177754SJeff.Bonwick@Sun.COM 			spa_load_spares(spa);
45187754SJeff.Bonwick@Sun.COM 			spa->spa_spares.sav_sync = B_TRUE;
45197754SJeff.Bonwick@Sun.COM 		} else {
45207754SJeff.Bonwick@Sun.COM 			error = EBUSY;
45217754SJeff.Bonwick@Sun.COM 		}
45227754SJeff.Bonwick@Sun.COM 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
45235450Sbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
45247754SJeff.Bonwick@Sun.COM 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
45257754SJeff.Bonwick@Sun.COM 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
45267754SJeff.Bonwick@Sun.COM 		/*
45277754SJeff.Bonwick@Sun.COM 		 * Cache devices can always be removed.
45287754SJeff.Bonwick@Sun.COM 		 */
45297754SJeff.Bonwick@Sun.COM 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
45307754SJeff.Bonwick@Sun.COM 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
45315450Sbrendan 		spa_load_l2cache(spa);
45325450Sbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
453310594SGeorge.Wilson@Sun.COM 	} else if (vd != NULL && vd->vdev_islog) {
453410594SGeorge.Wilson@Sun.COM 		ASSERT(!locked);
453510922SJeff.Bonwick@Sun.COM 		ASSERT(vd == vd->vdev_top);
453610594SGeorge.Wilson@Sun.COM 
453710594SGeorge.Wilson@Sun.COM 		/*
453810594SGeorge.Wilson@Sun.COM 		 * XXX - Once we have bp-rewrite this should
453910594SGeorge.Wilson@Sun.COM 		 * become the common case.
454010594SGeorge.Wilson@Sun.COM 		 */
454110594SGeorge.Wilson@Sun.COM 
454210974SJeff.Bonwick@Sun.COM 		mg = vd->vdev_mg;
454310974SJeff.Bonwick@Sun.COM 
454410594SGeorge.Wilson@Sun.COM 		/*
454510974SJeff.Bonwick@Sun.COM 		 * Stop allocating from this vdev.
454610594SGeorge.Wilson@Sun.COM 		 */
454710974SJeff.Bonwick@Sun.COM 		metaslab_group_passivate(mg);
454810594SGeorge.Wilson@Sun.COM 
454910922SJeff.Bonwick@Sun.COM 		/*
455010922SJeff.Bonwick@Sun.COM 		 * Wait for the youngest allocations and frees to sync,
455110922SJeff.Bonwick@Sun.COM 		 * and then wait for the deferral of those frees to finish.
455210922SJeff.Bonwick@Sun.COM 		 */
455310922SJeff.Bonwick@Sun.COM 		spa_vdev_config_exit(spa, NULL,
455410922SJeff.Bonwick@Sun.COM 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
455510922SJeff.Bonwick@Sun.COM 
455610974SJeff.Bonwick@Sun.COM 		/*
455710974SJeff.Bonwick@Sun.COM 		 * Attempt to evacuate the vdev.
455810974SJeff.Bonwick@Sun.COM 		 */
455910974SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove_evacuate(spa, vd);
456010974SJeff.Bonwick@Sun.COM 
456110594SGeorge.Wilson@Sun.COM 		txg = spa_vdev_config_enter(spa);
456210594SGeorge.Wilson@Sun.COM 
456310974SJeff.Bonwick@Sun.COM 		/*
456410974SJeff.Bonwick@Sun.COM 		 * If we couldn't evacuate the vdev, unwind.
456510974SJeff.Bonwick@Sun.COM 		 */
456610974SJeff.Bonwick@Sun.COM 		if (error) {
456710974SJeff.Bonwick@Sun.COM 			metaslab_group_activate(mg);
456810974SJeff.Bonwick@Sun.COM 			return (spa_vdev_exit(spa, NULL, txg, error));
456910974SJeff.Bonwick@Sun.COM 		}
457010974SJeff.Bonwick@Sun.COM 
457110974SJeff.Bonwick@Sun.COM 		/*
457210974SJeff.Bonwick@Sun.COM 		 * Clean up the vdev namespace.
457310974SJeff.Bonwick@Sun.COM 		 */
457410974SJeff.Bonwick@Sun.COM 		spa_vdev_remove_from_namespace(spa, vd);
457510594SGeorge.Wilson@Sun.COM 
45767754SJeff.Bonwick@Sun.COM 	} else if (vd != NULL) {
45777754SJeff.Bonwick@Sun.COM 		/*
45787754SJeff.Bonwick@Sun.COM 		 * Normal vdevs cannot be removed (yet).
45797754SJeff.Bonwick@Sun.COM 		 */
45807754SJeff.Bonwick@Sun.COM 		error = ENOTSUP;
45817754SJeff.Bonwick@Sun.COM 	} else {
45827754SJeff.Bonwick@Sun.COM 		/*
45837754SJeff.Bonwick@Sun.COM 		 * There is no vdev of any kind with the specified guid.
45847754SJeff.Bonwick@Sun.COM 		 */
45857754SJeff.Bonwick@Sun.COM 		error = ENOENT;
45865450Sbrendan 	}
45872082Seschrock 
45888241SJeff.Bonwick@Sun.COM 	if (!locked)
45898241SJeff.Bonwick@Sun.COM 		return (spa_vdev_exit(spa, NULL, txg, error));
45908241SJeff.Bonwick@Sun.COM 
45918241SJeff.Bonwick@Sun.COM 	return (error);
4592789Sahrens }
4593789Sahrens 
4594789Sahrens /*
45954451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
45964451Seschrock  * current spared, so we can detach it.
4597789Sahrens  */
45981544Seschrock static vdev_t *
45994451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
4600789Sahrens {
46011544Seschrock 	vdev_t *newvd, *oldvd;
46029816SGeorge.Wilson@Sun.COM 
46039816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
46044451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
46051544Seschrock 		if (oldvd != NULL)
46061544Seschrock 			return (oldvd);
46071544Seschrock 	}
4608789Sahrens 
46094451Seschrock 	/*
46104451Seschrock 	 * Check for a completed replacement.
46114451Seschrock 	 */
4612789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
46131544Seschrock 		oldvd = vd->vdev_child[0];
46141544Seschrock 		newvd = vd->vdev_child[1];
4615789Sahrens 
46168241SJeff.Bonwick@Sun.COM 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
461711820SVictor.Latushkin@Sun.COM 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
46188241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd))
46191544Seschrock 			return (oldvd);
46201544Seschrock 	}
4621789Sahrens 
46224451Seschrock 	/*
46234451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
46244451Seschrock 	 */
46254451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
46264451Seschrock 		newvd = vd->vdev_child[0];
46274451Seschrock 		oldvd = vd->vdev_child[1];
46284451Seschrock 
46294451Seschrock 		if (newvd->vdev_unspare &&
46308241SJeff.Bonwick@Sun.COM 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
463111820SVictor.Latushkin@Sun.COM 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
46328241SJeff.Bonwick@Sun.COM 		    !vdev_dtl_required(oldvd)) {
46334451Seschrock 			newvd->vdev_unspare = 0;
46344451Seschrock 			return (oldvd);
46354451Seschrock 		}
46364451Seschrock 	}
46374451Seschrock 
46381544Seschrock 	return (NULL);
4639789Sahrens }
4640789Sahrens 
46411544Seschrock static void
46424451Seschrock spa_vdev_resilver_done(spa_t *spa)
4643789Sahrens {
46448241SJeff.Bonwick@Sun.COM 	vdev_t *vd, *pvd, *ppvd;
46458241SJeff.Bonwick@Sun.COM 	uint64_t guid, sguid, pguid, ppguid;
46468241SJeff.Bonwick@Sun.COM 
46478241SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4648789Sahrens 
46494451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
46508241SJeff.Bonwick@Sun.COM 		pvd = vd->vdev_parent;
46518241SJeff.Bonwick@Sun.COM 		ppvd = pvd->vdev_parent;
46521544Seschrock 		guid = vd->vdev_guid;
46538241SJeff.Bonwick@Sun.COM 		pguid = pvd->vdev_guid;
46548241SJeff.Bonwick@Sun.COM 		ppguid = ppvd->vdev_guid;
46558241SJeff.Bonwick@Sun.COM 		sguid = 0;
46562082Seschrock 		/*
46572082Seschrock 		 * If we have just finished replacing a hot spared device, then
46582082Seschrock 		 * we need to detach the parent's first child (the original hot
46592082Seschrock 		 * spare) as well.
46602082Seschrock 		 */
46618241SJeff.Bonwick@Sun.COM 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
46622082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
46638241SJeff.Bonwick@Sun.COM 			ASSERT(ppvd->vdev_children == 2);
46648241SJeff.Bonwick@Sun.COM 			sguid = ppvd->vdev_child[1]->vdev_guid;
46652082Seschrock 		}
46668241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
46678241SJeff.Bonwick@Sun.COM 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
46681544Seschrock 			return;
46698241SJeff.Bonwick@Sun.COM 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
46702082Seschrock 			return;
46718241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4672789Sahrens 	}
4673789Sahrens 
46748241SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4675789Sahrens }
4676789Sahrens 
4677789Sahrens /*
467811041SEric.Taylor@Sun.COM  * Update the stored path or FRU for this vdev.
46791354Seschrock  */
46801354Seschrock int
46819425SEric.Schrock@Sun.COM spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
46829425SEric.Schrock@Sun.COM     boolean_t ispath)
46831354Seschrock {
46846643Seschrock 	vdev_t *vd;
468511817SGeorge.Wilson@Sun.COM 	boolean_t sync = B_FALSE;
468611041SEric.Taylor@Sun.COM 
468711041SEric.Taylor@Sun.COM 	spa_vdev_state_enter(spa, SCL_ALL);
46881354Seschrock 
46899425SEric.Schrock@Sun.COM 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
469011041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
46911354Seschrock 
46921585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
469311041SEric.Taylor@Sun.COM 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
46941585Sbonwick 
46959425SEric.Schrock@Sun.COM 	if (ispath) {
469611817SGeorge.Wilson@Sun.COM 		if (strcmp(value, vd->vdev_path) != 0) {
469711817SGeorge.Wilson@Sun.COM 			spa_strfree(vd->vdev_path);
469811817SGeorge.Wilson@Sun.COM 			vd->vdev_path = spa_strdup(value);
469911817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
470011817SGeorge.Wilson@Sun.COM 		}
47019425SEric.Schrock@Sun.COM 	} else {
470211817SGeorge.Wilson@Sun.COM 		if (vd->vdev_fru == NULL) {
470311817SGeorge.Wilson@Sun.COM 			vd->vdev_fru = spa_strdup(value);
470411817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
470511817SGeorge.Wilson@Sun.COM 		} else if (strcmp(value, vd->vdev_fru) != 0) {
47069425SEric.Schrock@Sun.COM 			spa_strfree(vd->vdev_fru);
470711817SGeorge.Wilson@Sun.COM 			vd->vdev_fru = spa_strdup(value);
470811817SGeorge.Wilson@Sun.COM 			sync = B_TRUE;
470911817SGeorge.Wilson@Sun.COM 		}
47109425SEric.Schrock@Sun.COM 	}
47111354Seschrock 
471211817SGeorge.Wilson@Sun.COM 	return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
47131354Seschrock }
47141354Seschrock 
47159425SEric.Schrock@Sun.COM int
47169425SEric.Schrock@Sun.COM spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
47179425SEric.Schrock@Sun.COM {
47189425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
47199425SEric.Schrock@Sun.COM }
47209425SEric.Schrock@Sun.COM 
47219425SEric.Schrock@Sun.COM int
47229425SEric.Schrock@Sun.COM spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
47239425SEric.Schrock@Sun.COM {
47249425SEric.Schrock@Sun.COM 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
47259425SEric.Schrock@Sun.COM }
47269425SEric.Schrock@Sun.COM 
47271354Seschrock /*
4728789Sahrens  * ==========================================================================
472912296SLin.Ling@Sun.COM  * SPA Scanning
4730789Sahrens  * ==========================================================================
4731789Sahrens  */
4732789Sahrens 
47337046Sahrens int
473412296SLin.Ling@Sun.COM spa_scan_stop(spa_t *spa)
4735789Sahrens {
47367754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
473712296SLin.Ling@Sun.COM 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
473812296SLin.Ling@Sun.COM 		return (EBUSY);
473912296SLin.Ling@Sun.COM 	return (dsl_scan_cancel(spa->spa_dsl_pool));
474012296SLin.Ling@Sun.COM }
474112296SLin.Ling@Sun.COM 
474212296SLin.Ling@Sun.COM int
474312296SLin.Ling@Sun.COM spa_scan(spa_t *spa, pool_scan_func_t func)
474412296SLin.Ling@Sun.COM {
474512296SLin.Ling@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
474612296SLin.Ling@Sun.COM 
474712296SLin.Ling@Sun.COM 	if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
4748789Sahrens 		return (ENOTSUP);
4749789Sahrens 
4750789Sahrens 	/*
47517046Sahrens 	 * If a resilver was requested, but there is no DTL on a
47527046Sahrens 	 * writeable leaf device, we have nothing to do.
4753789Sahrens 	 */
475412296SLin.Ling@Sun.COM 	if (func == POOL_SCAN_RESILVER &&
47557046Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
47567046Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
47571544Seschrock 		return (0);
47581544Seschrock 	}
4759789Sahrens 
476012296SLin.Ling@Sun.COM 	return (dsl_scan(spa->spa_dsl_pool, func));
4761789Sahrens }
4762789Sahrens 
47631544Seschrock /*
47641544Seschrock  * ==========================================================================
47651544Seschrock  * SPA async task processing
47661544Seschrock  * ==========================================================================
47671544Seschrock  */
47681544Seschrock 
47691544Seschrock static void
47704451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
4771789Sahrens {
47727361SBrendan.Gregg@Sun.COM 	if (vd->vdev_remove_wanted) {
477312247SGeorge.Wilson@Sun.COM 		vd->vdev_remove_wanted = B_FALSE;
477412247SGeorge.Wilson@Sun.COM 		vd->vdev_delayed_close = B_FALSE;
47757361SBrendan.Gregg@Sun.COM 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
477610575SEric.Schrock@Sun.COM 
477710575SEric.Schrock@Sun.COM 		/*
477810575SEric.Schrock@Sun.COM 		 * We want to clear the stats, but we don't want to do a full
477910575SEric.Schrock@Sun.COM 		 * vdev_clear() as that will cause us to throw away
478010575SEric.Schrock@Sun.COM 		 * degraded/faulted state as well as attempt to reopen the
478110575SEric.Schrock@Sun.COM 		 * device, all of which is a waste.
478210575SEric.Schrock@Sun.COM 		 */
478310575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_read_errors = 0;
478410575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_write_errors = 0;
478510575SEric.Schrock@Sun.COM 		vd->vdev_stat.vs_checksum_errors = 0;
478610575SEric.Schrock@Sun.COM 
47877754SJeff.Bonwick@Sun.COM 		vdev_state_dirty(vd->vdev_top);
47881544Seschrock 	}
47897361SBrendan.Gregg@Sun.COM 
47907754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
47917361SBrendan.Gregg@Sun.COM 		spa_async_remove(spa, vd->vdev_child[c]);
47921544Seschrock }
47931544Seschrock 
47941544Seschrock static void
47957754SJeff.Bonwick@Sun.COM spa_async_probe(spa_t *spa, vdev_t *vd)
47967754SJeff.Bonwick@Sun.COM {
47977754SJeff.Bonwick@Sun.COM 	if (vd->vdev_probe_wanted) {
479812247SGeorge.Wilson@Sun.COM 		vd->vdev_probe_wanted = B_FALSE;
47997754SJeff.Bonwick@Sun.COM 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
48007754SJeff.Bonwick@Sun.COM 	}
48017754SJeff.Bonwick@Sun.COM 
48027754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
48037754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, vd->vdev_child[c]);
48047754SJeff.Bonwick@Sun.COM }
48057754SJeff.Bonwick@Sun.COM 
48067754SJeff.Bonwick@Sun.COM static void
48079816SGeorge.Wilson@Sun.COM spa_async_autoexpand(spa_t *spa, vdev_t *vd)
48089816SGeorge.Wilson@Sun.COM {
48099816SGeorge.Wilson@Sun.COM 	sysevent_id_t eid;
48109816SGeorge.Wilson@Sun.COM 	nvlist_t *attr;
48119816SGeorge.Wilson@Sun.COM 	char *physpath;
48129816SGeorge.Wilson@Sun.COM 
48139816SGeorge.Wilson@Sun.COM 	if (!spa->spa_autoexpand)
48149816SGeorge.Wilson@Sun.COM 		return;
48159816SGeorge.Wilson@Sun.COM 
48169816SGeorge.Wilson@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
48179816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
48189816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, cvd);
48199816SGeorge.Wilson@Sun.COM 	}
48209816SGeorge.Wilson@Sun.COM 
48219816SGeorge.Wilson@Sun.COM 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
48229816SGeorge.Wilson@Sun.COM 		return;
48239816SGeorge.Wilson@Sun.COM 
48249816SGeorge.Wilson@Sun.COM 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
48259816SGeorge.Wilson@Sun.COM 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
48269816SGeorge.Wilson@Sun.COM 
48279816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
48289816SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
48299816SGeorge.Wilson@Sun.COM 
48309816SGeorge.Wilson@Sun.COM 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
48319816SGeorge.Wilson@Sun.COM 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
48329816SGeorge.Wilson@Sun.COM 
48339816SGeorge.Wilson@Sun.COM 	nvlist_free(attr);
48349816SGeorge.Wilson@Sun.COM 	kmem_free(physpath, MAXPATHLEN);
48359816SGeorge.Wilson@Sun.COM }
48369816SGeorge.Wilson@Sun.COM 
48379816SGeorge.Wilson@Sun.COM static void
48381544Seschrock spa_async_thread(spa_t *spa)
48391544Seschrock {
48407754SJeff.Bonwick@Sun.COM 	int tasks;
48411544Seschrock 
48421544Seschrock 	ASSERT(spa->spa_sync_on);
4843789Sahrens 
48441544Seschrock 	mutex_enter(&spa->spa_async_lock);
48451544Seschrock 	tasks = spa->spa_async_tasks;
48461544Seschrock 	spa->spa_async_tasks = 0;
48471544Seschrock 	mutex_exit(&spa->spa_async_lock);
48481544Seschrock 
48491544Seschrock 	/*
48501635Sbonwick 	 * See if the config needs to be updated.
48511635Sbonwick 	 */
48521635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
485310922SJeff.Bonwick@Sun.COM 		uint64_t old_space, new_space;
48549816SGeorge.Wilson@Sun.COM 
48551635Sbonwick 		mutex_enter(&spa_namespace_lock);
485610922SJeff.Bonwick@Sun.COM 		old_space = metaslab_class_get_space(spa_normal_class(spa));
48571635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
485810922SJeff.Bonwick@Sun.COM 		new_space = metaslab_class_get_space(spa_normal_class(spa));
48591635Sbonwick 		mutex_exit(&spa_namespace_lock);
48609816SGeorge.Wilson@Sun.COM 
48619816SGeorge.Wilson@Sun.COM 		/*
48629816SGeorge.Wilson@Sun.COM 		 * If the pool grew as a result of the config update,
48639816SGeorge.Wilson@Sun.COM 		 * then log an internal history event.
48649816SGeorge.Wilson@Sun.COM 		 */
486510922SJeff.Bonwick@Sun.COM 		if (new_space != old_space) {
486612296SLin.Ling@Sun.COM 			spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
486712296SLin.Ling@Sun.COM 			    spa, NULL,
48689946SMark.Musante@Sun.COM 			    "pool '%s' size: %llu(+%llu)",
486910922SJeff.Bonwick@Sun.COM 			    spa_name(spa), new_space, new_space - old_space);
48709816SGeorge.Wilson@Sun.COM 		}
48711635Sbonwick 	}
48721635Sbonwick 
48731635Sbonwick 	/*
48744451Seschrock 	 * See if any devices need to be marked REMOVED.
48751544Seschrock 	 */
48767754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_REMOVE) {
487710685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48784451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
48797754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
48807361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
48817754SJeff.Bonwick@Sun.COM 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
48827361SBrendan.Gregg@Sun.COM 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
48837754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48847754SJeff.Bonwick@Sun.COM 	}
48857754SJeff.Bonwick@Sun.COM 
48869816SGeorge.Wilson@Sun.COM 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
48879816SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
48889816SGeorge.Wilson@Sun.COM 		spa_async_autoexpand(spa, spa->spa_root_vdev);
48899816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_CONFIG, FTAG);
48909816SGeorge.Wilson@Sun.COM 	}
48919816SGeorge.Wilson@Sun.COM 
48927754SJeff.Bonwick@Sun.COM 	/*
48937754SJeff.Bonwick@Sun.COM 	 * See if any devices need to be probed.
48947754SJeff.Bonwick@Sun.COM 	 */
48957754SJeff.Bonwick@Sun.COM 	if (tasks & SPA_ASYNC_PROBE) {
489610685SGeorge.Wilson@Sun.COM 		spa_vdev_state_enter(spa, SCL_NONE);
48977754SJeff.Bonwick@Sun.COM 		spa_async_probe(spa, spa->spa_root_vdev);
48987754SJeff.Bonwick@Sun.COM 		(void) spa_vdev_state_exit(spa, NULL, 0);
48994451Seschrock 	}
49001544Seschrock 
49011544Seschrock 	/*
49021544Seschrock 	 * If any devices are done replacing, detach them.
49031544Seschrock 	 */
49044451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
49054451Seschrock 		spa_vdev_resilver_done(spa);
4906789Sahrens 
49071544Seschrock 	/*
49081544Seschrock 	 * Kick off a resilver.
49091544Seschrock 	 */
49107046Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
491112296SLin.Ling@Sun.COM 		dsl_resilver_restart(spa->spa_dsl_pool, 0);
49121544Seschrock 
49131544Seschrock 	/*
49141544Seschrock 	 * Let the world know that we're done.
49151544Seschrock 	 */
49161544Seschrock 	mutex_enter(&spa->spa_async_lock);
49171544Seschrock 	spa->spa_async_thread = NULL;
49181544Seschrock 	cv_broadcast(&spa->spa_async_cv);
49191544Seschrock 	mutex_exit(&spa->spa_async_lock);
49201544Seschrock 	thread_exit();
49211544Seschrock }
49221544Seschrock 
49231544Seschrock void
49241544Seschrock spa_async_suspend(spa_t *spa)
49251544Seschrock {
49261544Seschrock 	mutex_enter(&spa->spa_async_lock);
49271544Seschrock 	spa->spa_async_suspended++;
49281544Seschrock 	while (spa->spa_async_thread != NULL)
49291544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
49301544Seschrock 	mutex_exit(&spa->spa_async_lock);
49311544Seschrock }
49321544Seschrock 
49331544Seschrock void
49341544Seschrock spa_async_resume(spa_t *spa)
49351544Seschrock {
49361544Seschrock 	mutex_enter(&spa->spa_async_lock);
49371544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
49381544Seschrock 	spa->spa_async_suspended--;
49391544Seschrock 	mutex_exit(&spa->spa_async_lock);
49401544Seschrock }
49411544Seschrock 
49421544Seschrock static void
49431544Seschrock spa_async_dispatch(spa_t *spa)
49441544Seschrock {
49451544Seschrock 	mutex_enter(&spa->spa_async_lock);
49461544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
49471635Sbonwick 	    spa->spa_async_thread == NULL &&
49481635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
49491544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
49501544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
49511544Seschrock 	mutex_exit(&spa->spa_async_lock);
49521544Seschrock }
49531544Seschrock 
49541544Seschrock void
49551544Seschrock spa_async_request(spa_t *spa, int task)
49561544Seschrock {
495712296SLin.Ling@Sun.COM 	zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
49581544Seschrock 	mutex_enter(&spa->spa_async_lock);
49591544Seschrock 	spa->spa_async_tasks |= task;
49601544Seschrock 	mutex_exit(&spa->spa_async_lock);
4961789Sahrens }
4962789Sahrens 
4963789Sahrens /*
4964789Sahrens  * ==========================================================================
4965789Sahrens  * SPA syncing routines
4966789Sahrens  * ==========================================================================
4967789Sahrens  */
496812470SMatthew.Ahrens@Sun.COM 
496912470SMatthew.Ahrens@Sun.COM static int
497012470SMatthew.Ahrens@Sun.COM bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
4971789Sahrens {
497212470SMatthew.Ahrens@Sun.COM 	bpobj_t *bpo = arg;
497312470SMatthew.Ahrens@Sun.COM 	bpobj_enqueue(bpo, bp, tx);
497412470SMatthew.Ahrens@Sun.COM 	return (0);
497510922SJeff.Bonwick@Sun.COM }
497610922SJeff.Bonwick@Sun.COM 
497712470SMatthew.Ahrens@Sun.COM static int
497812470SMatthew.Ahrens@Sun.COM spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
497910922SJeff.Bonwick@Sun.COM {
498010922SJeff.Bonwick@Sun.COM 	zio_t *zio = arg;
498110922SJeff.Bonwick@Sun.COM 
498210922SJeff.Bonwick@Sun.COM 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
498310922SJeff.Bonwick@Sun.COM 	    zio->io_flags));
498412470SMatthew.Ahrens@Sun.COM 	return (0);
4985789Sahrens }
4986789Sahrens 
4987789Sahrens static void
49882082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
49892082Seschrock {
49902082Seschrock 	char *packed = NULL;
49917497STim.Haley@Sun.COM 	size_t bufsize;
49922082Seschrock 	size_t nvsize = 0;
49932082Seschrock 	dmu_buf_t *db;
49942082Seschrock 
49952082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
49962082Seschrock 
49977497STim.Haley@Sun.COM 	/*
49987497STim.Haley@Sun.COM 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
49997497STim.Haley@Sun.COM 	 * information.  This avoids the dbuf_will_dirty() path and
50007497STim.Haley@Sun.COM 	 * saves us a pre-read to get data we don't actually care about.
50017497STim.Haley@Sun.COM 	 */
50027497STim.Haley@Sun.COM 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
50037497STim.Haley@Sun.COM 	packed = kmem_alloc(bufsize, KM_SLEEP);
50042082Seschrock 
50052082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
50062082Seschrock 	    KM_SLEEP) == 0);
50077497STim.Haley@Sun.COM 	bzero(packed + nvsize, bufsize - nvsize);
50087497STim.Haley@Sun.COM 
50097497STim.Haley@Sun.COM 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
50107497STim.Haley@Sun.COM 
50117497STim.Haley@Sun.COM 	kmem_free(packed, bufsize);
50122082Seschrock 
50132082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
50142082Seschrock 	dmu_buf_will_dirty(db, tx);
50152082Seschrock 	*(uint64_t *)db->db_data = nvsize;
50162082Seschrock 	dmu_buf_rele(db, FTAG);
50172082Seschrock }
50182082Seschrock 
50192082Seschrock static void
50205450Sbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
50215450Sbrendan     const char *config, const char *entry)
50222082Seschrock {
50232082Seschrock 	nvlist_t *nvroot;
50245450Sbrendan 	nvlist_t **list;
50252082Seschrock 	int i;
50262082Seschrock 
50275450Sbrendan 	if (!sav->sav_sync)
50282082Seschrock 		return;
50292082Seschrock 
50302082Seschrock 	/*
50315450Sbrendan 	 * Update the MOS nvlist describing the list of available devices.
50325450Sbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
50334451Seschrock 	 * valid and the vdevs are labeled appropriately.
50342082Seschrock 	 */
50355450Sbrendan 	if (sav->sav_object == 0) {
50365450Sbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
50375450Sbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
50385450Sbrendan 		    sizeof (uint64_t), tx);
50392082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
50405450Sbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
50415450Sbrendan 		    &sav->sav_object, tx) == 0);
50422082Seschrock 	}
50432082Seschrock 
50442082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
50455450Sbrendan 	if (sav->sav_count == 0) {
50465450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
50472082Seschrock 	} else {
50485450Sbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
50495450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50505450Sbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
505112296SLin.Ling@Sun.COM 			    B_FALSE, VDEV_CONFIG_L2CACHE);
50525450Sbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
50535450Sbrendan 		    sav->sav_count) == 0);
50545450Sbrendan 		for (i = 0; i < sav->sav_count; i++)
50555450Sbrendan 			nvlist_free(list[i]);
50565450Sbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
50572082Seschrock 	}
50582082Seschrock 
50595450Sbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
50602926Sek110237 	nvlist_free(nvroot);
50612082Seschrock 
50625450Sbrendan 	sav->sav_sync = B_FALSE;
50632082Seschrock }
50642082Seschrock 
50652082Seschrock static void
5066789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5067789Sahrens {
5068789Sahrens 	nvlist_t *config;
5069789Sahrens 
50707754SJeff.Bonwick@Sun.COM 	if (list_is_empty(&spa->spa_config_dirty_list))
5071789Sahrens 		return;
5072789Sahrens 
50737754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
50747754SJeff.Bonwick@Sun.COM 
50757754SJeff.Bonwick@Sun.COM 	config = spa_config_generate(spa, spa->spa_root_vdev,
50767754SJeff.Bonwick@Sun.COM 	    dmu_tx_get_txg(tx), B_FALSE);
50777754SJeff.Bonwick@Sun.COM 
50787754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
5079789Sahrens 
50801635Sbonwick 	if (spa->spa_config_syncing)
50811635Sbonwick 		nvlist_free(spa->spa_config_syncing);
50821635Sbonwick 	spa->spa_config_syncing = config;
5083789Sahrens 
50842082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5085789Sahrens }
5086789Sahrens 
50875094Slling /*
50885094Slling  * Set zpool properties.
50895094Slling  */
50903912Slling static void
509112296SLin.Ling@Sun.COM spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
50923912Slling {
50933912Slling 	spa_t *spa = arg1;
50945094Slling 	objset_t *mos = spa->spa_meta_objset;
50953912Slling 	nvlist_t *nvp = arg2;
50965094Slling 	nvpair_t *elem;
50974451Seschrock 	uint64_t intval;
50986643Seschrock 	char *strval;
50995094Slling 	zpool_prop_t prop;
51005094Slling 	const char *propname;
51015094Slling 	zprop_type_t proptype;
51025094Slling 
51037754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
51047754SJeff.Bonwick@Sun.COM 
51055094Slling 	elem = NULL;
51065094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
51075094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
51085094Slling 		case ZPOOL_PROP_VERSION:
51095094Slling 			/*
51105094Slling 			 * Only set version for non-zpool-creation cases
51115094Slling 			 * (set/import). spa_create() needs special care
51125094Slling 			 * for version setting.
51135094Slling 			 */
51145094Slling 			if (tx->tx_txg != TXG_INITIAL) {
51155094Slling 				VERIFY(nvpair_value_uint64(elem,
51165094Slling 				    &intval) == 0);
51175094Slling 				ASSERT(intval <= SPA_VERSION);
51185094Slling 				ASSERT(intval >= spa_version(spa));
51195094Slling 				spa->spa_uberblock.ub_version = intval;
51205094Slling 				vdev_config_dirty(spa->spa_root_vdev);
51215094Slling 			}
51225094Slling 			break;
51235094Slling 
51245094Slling 		case ZPOOL_PROP_ALTROOT:
51255094Slling 			/*
51265094Slling 			 * 'altroot' is a non-persistent property. It should
51275094Slling 			 * have been set temporarily at creation or import time.
51285094Slling 			 */
51295094Slling 			ASSERT(spa->spa_root != NULL);
51305094Slling 			break;
51315094Slling 
51325363Seschrock 		case ZPOOL_PROP_CACHEFILE:
51335094Slling 			/*
51348525SEric.Schrock@Sun.COM 			 * 'cachefile' is also a non-persisitent property.
51355094Slling 			 */
51364543Smarks 			break;
51375094Slling 		default:
51385094Slling 			/*
51395094Slling 			 * Set pool property values in the poolprops mos object.
51405094Slling 			 */
51415094Slling 			if (spa->spa_pool_props_object == 0) {
51425094Slling 				VERIFY((spa->spa_pool_props_object =
51435094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
51445094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
51455094Slling 
51465094Slling 				VERIFY(zap_update(mos,
51475094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
51485094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
51495094Slling 				    == 0);
51505094Slling 			}
51515094Slling 
51525094Slling 			/* normalize the property name */
51535094Slling 			propname = zpool_prop_to_name(prop);
51545094Slling 			proptype = zpool_prop_get_type(prop);
51555094Slling 
51565094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
51575094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
51585094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
51595094Slling 				VERIFY(zap_update(mos,
51605094Slling 				    spa->spa_pool_props_object, propname,
51615094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
51625094Slling 
51635094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
51645094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
51655094Slling 
51665094Slling 				if (proptype == PROP_TYPE_INDEX) {
51675094Slling 					const char *unused;
51685094Slling 					VERIFY(zpool_prop_index_to_string(
51695094Slling 					    prop, intval, &unused) == 0);
51705094Slling 				}
51715094Slling 				VERIFY(zap_update(mos,
51725094Slling 				    spa->spa_pool_props_object, propname,
51735094Slling 				    8, 1, &intval, tx) == 0);
51745094Slling 			} else {
51755094Slling 				ASSERT(0); /* not allowed */
51765094Slling 			}
51775094Slling 
51785329Sgw25295 			switch (prop) {
51795329Sgw25295 			case ZPOOL_PROP_DELEGATION:
51805094Slling 				spa->spa_delegation = intval;
51815329Sgw25295 				break;
51825329Sgw25295 			case ZPOOL_PROP_BOOTFS:
51835094Slling 				spa->spa_bootfs = intval;
51845329Sgw25295 				break;
51855329Sgw25295 			case ZPOOL_PROP_FAILUREMODE:
51865329Sgw25295 				spa->spa_failmode = intval;
51875329Sgw25295 				break;
51889816SGeorge.Wilson@Sun.COM 			case ZPOOL_PROP_AUTOEXPAND:
51899816SGeorge.Wilson@Sun.COM 				spa->spa_autoexpand = intval;
519012318SEric.Taylor@Sun.COM 				if (tx->tx_txg != TXG_INITIAL)
519112318SEric.Taylor@Sun.COM 					spa_async_request(spa,
519212318SEric.Taylor@Sun.COM 					    SPA_ASYNC_AUTOEXPAND);
51939816SGeorge.Wilson@Sun.COM 				break;
519410922SJeff.Bonwick@Sun.COM 			case ZPOOL_PROP_DEDUPDITTO:
519510922SJeff.Bonwick@Sun.COM 				spa->spa_dedup_ditto = intval;
519610922SJeff.Bonwick@Sun.COM 				break;
51975329Sgw25295 			default:
51985329Sgw25295 				break;
51995329Sgw25295 			}
52003912Slling 		}
52015094Slling 
52025094Slling 		/* log internal history if this is not a zpool create */
52035094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
52045094Slling 		    tx->tx_txg != TXG_INITIAL) {
520512296SLin.Ling@Sun.COM 			spa_history_log_internal(LOG_POOL_PROPSET,
520612296SLin.Ling@Sun.COM 			    spa, tx, "%s %lld %s",
52077754SJeff.Bonwick@Sun.COM 			    nvpair_name(elem), intval, spa_name(spa));
52085094Slling 		}
52093912Slling 	}
52107754SJeff.Bonwick@Sun.COM 
52117754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
52123912Slling }
52133912Slling 
5214789Sahrens /*
521512470SMatthew.Ahrens@Sun.COM  * Perform one-time upgrade on-disk changes.  spa_version() does not
521612470SMatthew.Ahrens@Sun.COM  * reflect the new version this txg, so there must be no changes this
521712470SMatthew.Ahrens@Sun.COM  * txg to anything that the upgrade code depends on after it executes.
521812470SMatthew.Ahrens@Sun.COM  * Therefore this must be called after dsl_pool_sync() does the sync
521912470SMatthew.Ahrens@Sun.COM  * tasks.
522012470SMatthew.Ahrens@Sun.COM  */
522112470SMatthew.Ahrens@Sun.COM static void
522212470SMatthew.Ahrens@Sun.COM spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
522312470SMatthew.Ahrens@Sun.COM {
522412470SMatthew.Ahrens@Sun.COM 	dsl_pool_t *dp = spa->spa_dsl_pool;
522512470SMatthew.Ahrens@Sun.COM 
522612470SMatthew.Ahrens@Sun.COM 	ASSERT(spa->spa_sync_pass == 1);
522712470SMatthew.Ahrens@Sun.COM 
522812470SMatthew.Ahrens@Sun.COM 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
522912470SMatthew.Ahrens@Sun.COM 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
523012470SMatthew.Ahrens@Sun.COM 		dsl_pool_create_origin(dp, tx);
523112470SMatthew.Ahrens@Sun.COM 
523212470SMatthew.Ahrens@Sun.COM 		/* Keeping the origin open increases spa_minref */
523312470SMatthew.Ahrens@Sun.COM 		spa->spa_minref += 3;
523412470SMatthew.Ahrens@Sun.COM 	}
523512470SMatthew.Ahrens@Sun.COM 
523612470SMatthew.Ahrens@Sun.COM 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
523712470SMatthew.Ahrens@Sun.COM 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
523812470SMatthew.Ahrens@Sun.COM 		dsl_pool_upgrade_clones(dp, tx);
523912470SMatthew.Ahrens@Sun.COM 	}
524012470SMatthew.Ahrens@Sun.COM 
524112470SMatthew.Ahrens@Sun.COM 	if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
524212470SMatthew.Ahrens@Sun.COM 	    spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
524312470SMatthew.Ahrens@Sun.COM 		dsl_pool_upgrade_dir_clones(dp, tx);
524412470SMatthew.Ahrens@Sun.COM 
524512470SMatthew.Ahrens@Sun.COM 		/* Keeping the freedir open increases spa_minref */
524612470SMatthew.Ahrens@Sun.COM 		spa->spa_minref += 3;
524712470SMatthew.Ahrens@Sun.COM 	}
524812470SMatthew.Ahrens@Sun.COM }
524912470SMatthew.Ahrens@Sun.COM 
525012470SMatthew.Ahrens@Sun.COM /*
5251789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
5252789Sahrens  * part of the process, so we iterate until it converges.
5253789Sahrens  */
5254789Sahrens void
5255789Sahrens spa_sync(spa_t *spa, uint64_t txg)
5256789Sahrens {
5257789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
5258789Sahrens 	objset_t *mos = spa->spa_meta_objset;
525912470SMatthew.Ahrens@Sun.COM 	bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
526010922SJeff.Bonwick@Sun.COM 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
52611635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5262789Sahrens 	vdev_t *vd;
5263789Sahrens 	dmu_tx_t *tx;
52647754SJeff.Bonwick@Sun.COM 	int error;
5265789Sahrens 
5266789Sahrens 	/*
5267789Sahrens 	 * Lock out configuration changes.
5268789Sahrens 	 */
52697754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5270789Sahrens 
5271789Sahrens 	spa->spa_syncing_txg = txg;
5272789Sahrens 	spa->spa_sync_pass = 0;
5273789Sahrens 
52747754SJeff.Bonwick@Sun.COM 	/*
52757754SJeff.Bonwick@Sun.COM 	 * If there are any pending vdev state changes, convert them
52767754SJeff.Bonwick@Sun.COM 	 * into config changes that go out with this transaction group.
52777754SJeff.Bonwick@Sun.COM 	 */
52787754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
52798241SJeff.Bonwick@Sun.COM 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
52808241SJeff.Bonwick@Sun.COM 		/*
52818241SJeff.Bonwick@Sun.COM 		 * We need the write lock here because, for aux vdevs,
52828241SJeff.Bonwick@Sun.COM 		 * calling vdev_config_dirty() modifies sav_config.
52838241SJeff.Bonwick@Sun.COM 		 * This is ugly and will become unnecessary when we
52848241SJeff.Bonwick@Sun.COM 		 * eliminate the aux vdev wart by integrating all vdevs
52858241SJeff.Bonwick@Sun.COM 		 * into the root vdev tree.
52868241SJeff.Bonwick@Sun.COM 		 */
52878241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52888241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
52898241SJeff.Bonwick@Sun.COM 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
52908241SJeff.Bonwick@Sun.COM 			vdev_state_clean(vd);
52918241SJeff.Bonwick@Sun.COM 			vdev_config_dirty(vd);
52928241SJeff.Bonwick@Sun.COM 		}
52938241SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
52948241SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
52957754SJeff.Bonwick@Sun.COM 	}
52967754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
52977754SJeff.Bonwick@Sun.COM 
52982082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
52992082Seschrock 
53002082Seschrock 	/*
53014577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
53022082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
53032082Seschrock 	 */
53044577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
53054577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
53062082Seschrock 		int i;
53072082Seschrock 
53082082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
53092082Seschrock 			vd = rvd->vdev_child[i];
53102082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
53112082Seschrock 				break;
53122082Seschrock 		}
53132082Seschrock 		if (i == rvd->vdev_children) {
53142082Seschrock 			spa->spa_deflate = TRUE;
53152082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
53162082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
53172082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
53182082Seschrock 		}
53192082Seschrock 	}
53202082Seschrock 
5321789Sahrens 	/*
532212296SLin.Ling@Sun.COM 	 * If anything has changed in this txg, or if someone is waiting
532312296SLin.Ling@Sun.COM 	 * for this txg to sync (eg, spa_vdev_remove()), push the
532412296SLin.Ling@Sun.COM 	 * deferred frees from the previous txg.  If not, leave them
532512296SLin.Ling@Sun.COM 	 * alone so that we don't generate work on an otherwise idle
532612296SLin.Ling@Sun.COM 	 * system.
5327789Sahrens 	 */
5328789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
53292329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
533012296SLin.Ling@Sun.COM 	    !txg_list_empty(&dp->dp_sync_tasks, txg) ||
533112470SMatthew.Ahrens@Sun.COM 	    ((dsl_scan_active(dp->dp_scan) ||
533212470SMatthew.Ahrens@Sun.COM 	    txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
533312470SMatthew.Ahrens@Sun.COM 		zio_t *zio = zio_root(spa, NULL, NULL, 0);
533412470SMatthew.Ahrens@Sun.COM 		VERIFY3U(bpobj_iterate(defer_bpo,
533512470SMatthew.Ahrens@Sun.COM 		    spa_free_sync_cb, zio, tx), ==, 0);
533612470SMatthew.Ahrens@Sun.COM 		VERIFY3U(zio_wait(zio), ==, 0);
533712470SMatthew.Ahrens@Sun.COM 	}
5338789Sahrens 
5339789Sahrens 	/*
5340789Sahrens 	 * Iterate to convergence.
5341789Sahrens 	 */
5342789Sahrens 	do {
534310922SJeff.Bonwick@Sun.COM 		int pass = ++spa->spa_sync_pass;
5344789Sahrens 
5345789Sahrens 		spa_sync_config_object(spa, tx);
53465450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
53475450Sbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
53485450Sbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
53495450Sbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
53501544Seschrock 		spa_errlog_sync(spa, txg);
5351789Sahrens 		dsl_pool_sync(dp, txg);
5352789Sahrens 
535310922SJeff.Bonwick@Sun.COM 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
535410922SJeff.Bonwick@Sun.COM 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
535512470SMatthew.Ahrens@Sun.COM 			bplist_iterate(free_bpl, spa_free_sync_cb,
535612470SMatthew.Ahrens@Sun.COM 			    zio, tx);
535710922SJeff.Bonwick@Sun.COM 			VERIFY(zio_wait(zio) == 0);
535810922SJeff.Bonwick@Sun.COM 		} else {
535912470SMatthew.Ahrens@Sun.COM 			bplist_iterate(free_bpl, bpobj_enqueue_cb,
536012470SMatthew.Ahrens@Sun.COM 			    defer_bpo, tx);
5361789Sahrens 		}
5362789Sahrens 
536310922SJeff.Bonwick@Sun.COM 		ddt_sync(spa, txg);
536412296SLin.Ling@Sun.COM 		dsl_scan_sync(dp, tx);
536511619SGeorge.Wilson@Sun.COM 
536610922SJeff.Bonwick@Sun.COM 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
536710922SJeff.Bonwick@Sun.COM 			vdev_sync(vd, txg);
536810922SJeff.Bonwick@Sun.COM 
536912470SMatthew.Ahrens@Sun.COM 		if (pass == 1)
537012470SMatthew.Ahrens@Sun.COM 			spa_sync_upgrades(spa, tx);
537112470SMatthew.Ahrens@Sun.COM 
537210922SJeff.Bonwick@Sun.COM 	} while (dmu_objset_is_dirty(mos, txg));
537310922SJeff.Bonwick@Sun.COM 
5374789Sahrens 	/*
5375789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
5376789Sahrens 	 * to commit the transaction group.
53771635Sbonwick 	 *
53785688Sbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
53795688Sbonwick 	 * random top-level vdevs that are known to be visible in the
53807754SJeff.Bonwick@Sun.COM 	 * config cache (see spa_vdev_add() for a complete description).
53817754SJeff.Bonwick@Sun.COM 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5382789Sahrens 	 */
53837754SJeff.Bonwick@Sun.COM 	for (;;) {
53847754SJeff.Bonwick@Sun.COM 		/*
53857754SJeff.Bonwick@Sun.COM 		 * We hold SCL_STATE to prevent vdev open/close/etc.
53867754SJeff.Bonwick@Sun.COM 		 * while we're attempting to write the vdev labels.
53877754SJeff.Bonwick@Sun.COM 		 */
53887754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
53897754SJeff.Bonwick@Sun.COM 
53907754SJeff.Bonwick@Sun.COM 		if (list_is_empty(&spa->spa_config_dirty_list)) {
53917754SJeff.Bonwick@Sun.COM 			vdev_t *svd[SPA_DVAS_PER_BP];
53927754SJeff.Bonwick@Sun.COM 			int svdcount = 0;
53937754SJeff.Bonwick@Sun.COM 			int children = rvd->vdev_children;
53947754SJeff.Bonwick@Sun.COM 			int c0 = spa_get_random(children);
53959816SGeorge.Wilson@Sun.COM 
53969816SGeorge.Wilson@Sun.COM 			for (int c = 0; c < children; c++) {
53977754SJeff.Bonwick@Sun.COM 				vd = rvd->vdev_child[(c0 + c) % children];
53987754SJeff.Bonwick@Sun.COM 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
53997754SJeff.Bonwick@Sun.COM 					continue;
54007754SJeff.Bonwick@Sun.COM 				svd[svdcount++] = vd;
54017754SJeff.Bonwick@Sun.COM 				if (svdcount == SPA_DVAS_PER_BP)
54027754SJeff.Bonwick@Sun.COM 					break;
54037754SJeff.Bonwick@Sun.COM 			}
54049725SEric.Schrock@Sun.COM 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
54059725SEric.Schrock@Sun.COM 			if (error != 0)
54069725SEric.Schrock@Sun.COM 				error = vdev_config_sync(svd, svdcount, txg,
54079725SEric.Schrock@Sun.COM 				    B_TRUE);
54087754SJeff.Bonwick@Sun.COM 		} else {
54097754SJeff.Bonwick@Sun.COM 			error = vdev_config_sync(rvd->vdev_child,
54109725SEric.Schrock@Sun.COM 			    rvd->vdev_children, txg, B_FALSE);
54119725SEric.Schrock@Sun.COM 			if (error != 0)
54129725SEric.Schrock@Sun.COM 				error = vdev_config_sync(rvd->vdev_child,
54139725SEric.Schrock@Sun.COM 				    rvd->vdev_children, txg, B_TRUE);
54141635Sbonwick 		}
54157754SJeff.Bonwick@Sun.COM 
54167754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
54177754SJeff.Bonwick@Sun.COM 
54187754SJeff.Bonwick@Sun.COM 		if (error == 0)
54197754SJeff.Bonwick@Sun.COM 			break;
54207754SJeff.Bonwick@Sun.COM 		zio_suspend(spa, NULL);
54217754SJeff.Bonwick@Sun.COM 		zio_resume_wait(spa);
54221635Sbonwick 	}
54232082Seschrock 	dmu_tx_commit(tx);
54242082Seschrock 
54251635Sbonwick 	/*
54261635Sbonwick 	 * Clear the dirty config list.
54271635Sbonwick 	 */
54287754SJeff.Bonwick@Sun.COM 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
54291635Sbonwick 		vdev_config_clean(vd);
54301635Sbonwick 
54311635Sbonwick 	/*
54321635Sbonwick 	 * Now that the new config has synced transactionally,
54331635Sbonwick 	 * let it become visible to the config cache.
54341635Sbonwick 	 */
54351635Sbonwick 	if (spa->spa_config_syncing != NULL) {
54361635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
54371635Sbonwick 		spa->spa_config_txg = txg;
54381635Sbonwick 		spa->spa_config_syncing = NULL;
54391635Sbonwick 	}
5440789Sahrens 
5441789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
5442789Sahrens 
544310922SJeff.Bonwick@Sun.COM 	dsl_pool_sync_done(dp, txg);
5444789Sahrens 
5445789Sahrens 	/*
5446789Sahrens 	 * Update usable space statistics.
5447789Sahrens 	 */
5448789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5449789Sahrens 		vdev_sync_done(vd, txg);
5450789Sahrens 
545110956SGeorge.Wilson@Sun.COM 	spa_update_dspace(spa);
545210956SGeorge.Wilson@Sun.COM 
5453789Sahrens 	/*
5454789Sahrens 	 * It had better be the case that we didn't dirty anything
54552082Seschrock 	 * since vdev_config_sync().
5456789Sahrens 	 */
5457789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5458789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5459789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
546010922SJeff.Bonwick@Sun.COM 
546110922SJeff.Bonwick@Sun.COM 	spa->spa_sync_pass = 0;
5462789Sahrens 
54637754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
54641544Seschrock 
546510921STim.Haley@Sun.COM 	spa_handle_ignored_writes(spa);
546610921STim.Haley@Sun.COM 
54671544Seschrock 	/*
54681544Seschrock 	 * If any async tasks have been requested, kick them off.
54691544Seschrock 	 */
54701544Seschrock 	spa_async_dispatch(spa);
5471789Sahrens }
5472789Sahrens 
5473789Sahrens /*
5474789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
5475789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
5476789Sahrens  * sync.
5477789Sahrens  */
5478789Sahrens void
5479789Sahrens spa_sync_allpools(void)
5480789Sahrens {
5481789Sahrens 	spa_t *spa = NULL;
5482789Sahrens 	mutex_enter(&spa_namespace_lock);
5483789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
54847754SJeff.Bonwick@Sun.COM 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
5485789Sahrens 			continue;
5486789Sahrens 		spa_open_ref(spa, FTAG);
5487789Sahrens 		mutex_exit(&spa_namespace_lock);
5488789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
5489789Sahrens 		mutex_enter(&spa_namespace_lock);
5490789Sahrens 		spa_close(spa, FTAG);
5491789Sahrens 	}
5492789Sahrens 	mutex_exit(&spa_namespace_lock);
5493789Sahrens }
5494789Sahrens 
5495789Sahrens /*
5496789Sahrens  * ==========================================================================
5497789Sahrens  * Miscellaneous routines
5498789Sahrens  * ==========================================================================
5499789Sahrens  */
5500789Sahrens 
5501789Sahrens /*
5502789Sahrens  * Remove all pools in the system.
5503789Sahrens  */
5504789Sahrens void
5505789Sahrens spa_evict_all(void)
5506789Sahrens {
5507789Sahrens 	spa_t *spa;
5508789Sahrens 
5509789Sahrens 	/*
5510789Sahrens 	 * Remove all cached state.  All pools should be closed now,
5511789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
5512789Sahrens 	 */
5513789Sahrens 	mutex_enter(&spa_namespace_lock);
5514789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
5515789Sahrens 		/*
55161544Seschrock 		 * Stop async tasks.  The async thread may need to detach
55171544Seschrock 		 * a device that's been replaced, which requires grabbing
55181544Seschrock 		 * spa_namespace_lock, so we must drop it here.
5519789Sahrens 		 */
5520789Sahrens 		spa_open_ref(spa, FTAG);
5521789Sahrens 		mutex_exit(&spa_namespace_lock);
55221544Seschrock 		spa_async_suspend(spa);
55234808Sek110237 		mutex_enter(&spa_namespace_lock);
5524789Sahrens 		spa_close(spa, FTAG);
5525789Sahrens 
5526789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5527789Sahrens 			spa_unload(spa);
5528789Sahrens 			spa_deactivate(spa);
5529789Sahrens 		}
5530789Sahrens 		spa_remove(spa);
5531789Sahrens 	}
5532789Sahrens 	mutex_exit(&spa_namespace_lock);
5533789Sahrens }
55341544Seschrock 
55351544Seschrock vdev_t *
55369425SEric.Schrock@Sun.COM spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
55371544Seschrock {
55386643Seschrock 	vdev_t *vd;
55396643Seschrock 	int i;
55406643Seschrock 
55416643Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
55426643Seschrock 		return (vd);
55436643Seschrock 
55449425SEric.Schrock@Sun.COM 	if (aux) {
55456643Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
55466643Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
55476643Seschrock 			if (vd->vdev_guid == guid)
55486643Seschrock 				return (vd);
55496643Seschrock 		}
55509425SEric.Schrock@Sun.COM 
55519425SEric.Schrock@Sun.COM 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
55529425SEric.Schrock@Sun.COM 			vd = spa->spa_spares.sav_vdevs[i];
55539425SEric.Schrock@Sun.COM 			if (vd->vdev_guid == guid)
55549425SEric.Schrock@Sun.COM 				return (vd);
55559425SEric.Schrock@Sun.COM 		}
55566643Seschrock 	}
55576643Seschrock 
55586643Seschrock 	return (NULL);
55591544Seschrock }
55601760Seschrock 
55611760Seschrock void
55625094Slling spa_upgrade(spa_t *spa, uint64_t version)
55631760Seschrock {
55647754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
55651760Seschrock 
55661760Seschrock 	/*
55671760Seschrock 	 * This should only be called for a non-faulted pool, and since a
55681760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
55691760Seschrock 	 * possible.
55701760Seschrock 	 */
55714577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
55725094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
55735094Slling 
55745094Slling 	spa->spa_uberblock.ub_version = version;
55751760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
55761760Seschrock 
55777754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
55782082Seschrock 
55792082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
55801760Seschrock }
55812082Seschrock 
55822082Seschrock boolean_t
55832082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
55842082Seschrock {
55852082Seschrock 	int i;
55863377Seschrock 	uint64_t spareguid;
55875450Sbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
55885450Sbrendan 
55895450Sbrendan 	for (i = 0; i < sav->sav_count; i++)
55905450Sbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
55912082Seschrock 			return (B_TRUE);
55922082Seschrock 
55935450Sbrendan 	for (i = 0; i < sav->sav_npending; i++) {
55945450Sbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
55955450Sbrendan 		    &spareguid) == 0 && spareguid == guid)
55963377Seschrock 			return (B_TRUE);
55973377Seschrock 	}
55983377Seschrock 
55992082Seschrock 	return (B_FALSE);
56002082Seschrock }
56013912Slling 
56024451Seschrock /*
56037214Slling  * Check if a pool has an active shared spare device.
56047214Slling  * Note: reference count of an active spare is 2, as a spare and as a replace
56057214Slling  */
56067214Slling static boolean_t
56077214Slling spa_has_active_shared_spare(spa_t *spa)
56087214Slling {
56097214Slling 	int i, refcnt;
56107214Slling 	uint64_t pool;
56117214Slling 	spa_aux_vdev_t *sav = &spa->spa_spares;
56127214Slling 
56137214Slling 	for (i = 0; i < sav->sav_count; i++) {
56147214Slling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
56157214Slling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
56167214Slling 		    refcnt > 2)
56177214Slling 			return (B_TRUE);
56187214Slling 	}
56197214Slling 
56207214Slling 	return (B_FALSE);
56217214Slling }
56227214Slling 
56237214Slling /*
56244451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
56254451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
56264451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
56274451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
56284451Seschrock  * or zdb as real changes.
56294451Seschrock  */
56304451Seschrock void
56314451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
56324451Seschrock {
56334451Seschrock #ifdef _KERNEL
56344451Seschrock 	sysevent_t		*ev;
56354451Seschrock 	sysevent_attr_list_t	*attr = NULL;
56364451Seschrock 	sysevent_value_t	value;
56374451Seschrock 	sysevent_id_t		eid;
56384451Seschrock 
56394451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
56404451Seschrock 	    SE_SLEEP);
56414451Seschrock 
56424451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
56434451Seschrock 	value.value.sv_string = spa_name(spa);
56444451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
56454451Seschrock 		goto done;
56464451Seschrock 
56474451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
56484451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
56494451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
56504451Seschrock 		goto done;
56514451Seschrock 
56524451Seschrock 	if (vd) {
56534451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
56544451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
56554451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
56564451Seschrock 		    SE_SLEEP) != 0)
56574451Seschrock 			goto done;
56584451Seschrock 
56594451Seschrock 		if (vd->vdev_path) {
56604451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
56614451Seschrock 			value.value.sv_string = vd->vdev_path;
56624451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
56634451Seschrock 			    &value, SE_SLEEP) != 0)
56644451Seschrock 				goto done;
56654451Seschrock 		}
56664451Seschrock 	}
56674451Seschrock 
56685756Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
56695756Seschrock 		goto done;
56705756Seschrock 	attr = NULL;
56715756Seschrock 
56724451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
56734451Seschrock 
56744451Seschrock done:
56754451Seschrock 	if (attr)
56764451Seschrock 		sysevent_free_attr(attr);
56774451Seschrock 	sysevent_free(ev);
56784451Seschrock #endif
56794451Seschrock }
5680