xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision 1437283407f89cab03860accf49408f94559bc34)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2398d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
241b497ab8SAdam H. Leventhal  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25*14372834SHans Rosenfeld  * Copyright (c) 2015, Nexenta Systems, Inc.  All rights reserved.
26bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
275aeb9474SGarrett D'Amore  */
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
303e30c24aSWill Andrews  * SPA: Storage Pool Allocator
313e30c24aSWill Andrews  *
32fa9e4066Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
33fa9e4066Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
34fa9e4066Sahrens  * pool.
35fa9e4066Sahrens  */
36fa9e4066Sahrens 
37fa9e4066Sahrens #include <sys/zfs_context.h>
38ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
39fa9e4066Sahrens #include <sys/spa_impl.h>
40fa9e4066Sahrens #include <sys/zio.h>
41fa9e4066Sahrens #include <sys/zio_checksum.h>
42fa9e4066Sahrens #include <sys/dmu.h>
43fa9e4066Sahrens #include <sys/dmu_tx.h>
44fa9e4066Sahrens #include <sys/zap.h>
45fa9e4066Sahrens #include <sys/zil.h>
46b24ab676SJeff Bonwick #include <sys/ddt.h>
47fa9e4066Sahrens #include <sys/vdev_impl.h>
48fa9e4066Sahrens #include <sys/metaslab.h>
4988ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
50fa9e4066Sahrens #include <sys/uberblock_impl.h>
51fa9e4066Sahrens #include <sys/txg.h>
52fa9e4066Sahrens #include <sys/avl.h>
53fa9e4066Sahrens #include <sys/dmu_traverse.h>
54b1b8ab34Slling #include <sys/dmu_objset.h>
55fa9e4066Sahrens #include <sys/unique.h>
56fa9e4066Sahrens #include <sys/dsl_pool.h>
57b1b8ab34Slling #include <sys/dsl_dataset.h>
58fa9e4066Sahrens #include <sys/dsl_dir.h>
59fa9e4066Sahrens #include <sys/dsl_prop.h>
60b1b8ab34Slling #include <sys/dsl_synctask.h>
61fa9e4066Sahrens #include <sys/fs/zfs.h>
62fa94a07fSbrendan #include <sys/arc.h>
63fa9e4066Sahrens #include <sys/callb.h>
6495173954Sek #include <sys/systeminfo.h>
65e7cbe64fSgw #include <sys/spa_boot.h>
66573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h>
673f9d6ad7SLin Ling #include <sys/dsl_scan.h>
68ad135b5dSChristopher Siden #include <sys/zfeature.h>
693b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
70fa9e4066Sahrens 
715679c89fSjv #ifdef	_KERNEL
72dedec472SJack Meng #include <sys/bootprops.h>
7335a5a358SJonathan Adams #include <sys/callb.h>
7435a5a358SJonathan Adams #include <sys/cpupart.h>
7535a5a358SJonathan Adams #include <sys/pool.h>
7635a5a358SJonathan Adams #include <sys/sysdc.h>
7735a5a358SJonathan Adams #include <sys/zone.h>
785679c89fSjv #endif	/* _KERNEL */
795679c89fSjv 
80990b4856Slling #include "zfs_prop.h"
81b7b97454Sperrin #include "zfs_comutil.h"
82990b4856Slling 
833cb69f73SWill Andrews /*
843cb69f73SWill Andrews  * The interval, in seconds, at which failed configuration cache file writes
853cb69f73SWill Andrews  * should be retried.
863cb69f73SWill Andrews  */
873cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300;
883cb69f73SWill Andrews 
8935a5a358SJonathan Adams typedef enum zti_modes {
90ec94d322SAdam Leventhal 	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
91ec94d322SAdam Leventhal 	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
92ec94d322SAdam Leventhal 	ZTI_MODE_NULL,			/* don't create a taskq */
93ec94d322SAdam Leventhal 	ZTI_NMODES
9435a5a358SJonathan Adams } zti_modes_t;
95416e0cd8Sek 
96ec94d322SAdam Leventhal #define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
97ec94d322SAdam Leventhal #define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
98ec94d322SAdam Leventhal #define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
992e0c549eSJonathan Adams 
100ec94d322SAdam Leventhal #define	ZTI_N(n)	ZTI_P(n, 1)
101ec94d322SAdam Leventhal #define	ZTI_ONE		ZTI_N(1)
1022e0c549eSJonathan Adams 
1032e0c549eSJonathan Adams typedef struct zio_taskq_info {
104ec94d322SAdam Leventhal 	zti_modes_t zti_mode;
10580eb36f2SGeorge Wilson 	uint_t zti_value;
106ec94d322SAdam Leventhal 	uint_t zti_count;
1072e0c549eSJonathan Adams } zio_taskq_info_t;
1082e0c549eSJonathan Adams 
1092e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
11035a5a358SJonathan Adams 	"issue", "issue_high", "intr", "intr_high"
1112e0c549eSJonathan Adams };
1122e0c549eSJonathan Adams 
11380eb36f2SGeorge Wilson /*
114ec94d322SAdam Leventhal  * This table defines the taskq settings for each ZFS I/O type. When
115ec94d322SAdam Leventhal  * initializing a pool, we use this table to create an appropriately sized
116ec94d322SAdam Leventhal  * taskq. Some operations are low volume and therefore have a small, static
117ec94d322SAdam Leventhal  * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
118ec94d322SAdam Leventhal  * macros. Other operations process a large amount of data; the ZTI_BATCH
119ec94d322SAdam Leventhal  * macro causes us to create a taskq oriented for throughput. Some operations
120ec94d322SAdam Leventhal  * are so high frequency and short-lived that the taskq itself can become a a
121ec94d322SAdam Leventhal  * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
122ec94d322SAdam Leventhal  * additional degree of parallelism specified by the number of threads per-
123ec94d322SAdam Leventhal  * taskq and the number of taskqs; when dispatching an event in this case, the
124ec94d322SAdam Leventhal  * particular taskq is chosen at random.
125ec94d322SAdam Leventhal  *
126ec94d322SAdam Leventhal  * The different taskq priorities are to handle the different contexts (issue
127ec94d322SAdam Leventhal  * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
128ec94d322SAdam Leventhal  * need to be handled with minimum delay.
12980eb36f2SGeorge Wilson  */
13080eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
13180eb36f2SGeorge Wilson 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
132ec94d322SAdam Leventhal 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
1331b497ab8SAdam H. Leventhal 	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
134ec94d322SAdam Leventhal 	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
135ec94d322SAdam Leventhal 	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
136ec94d322SAdam Leventhal 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
137ec94d322SAdam Leventhal 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1382e0c549eSJonathan Adams };
1392e0c549eSJonathan Adams 
1403b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx);
1413b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx);
14289a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa);
1431195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1441195e687SMark J Musante     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1451195e687SMark J Musante     char **ereport);
146cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa);
147990b4856Slling 
14869962b56SMatthew Ahrens uint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
14935a5a358SJonathan Adams id_t		zio_taskq_psrset_bind = PS_NONE;
15035a5a358SJonathan Adams boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
15135a5a358SJonathan Adams uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
15235a5a358SJonathan Adams 
15335a5a358SJonathan Adams boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
15401f55e48SGeorge Wilson extern int	zfs_sync_pass_deferred_free;
15535a5a358SJonathan Adams 
15635a5a358SJonathan Adams /*
15735a5a358SJonathan Adams  * This (illegal) pool name is used when temporarily importing a spa_t in order
15835a5a358SJonathan Adams  * to get the vdev stats associated with the imported devices.
15935a5a358SJonathan Adams  */
16035a5a358SJonathan Adams #define	TRYIMPORT_NAME	"$import"
16135a5a358SJonathan Adams 
162990b4856Slling /*
163990b4856Slling  * ==========================================================================
164990b4856Slling  * SPA properties routines
165990b4856Slling  * ==========================================================================
166990b4856Slling  */
167990b4856Slling 
168990b4856Slling /*
169990b4856Slling  * Add a (source=src, propname=propval) list to an nvlist.
170990b4856Slling  */
1719d82f4f6Slling static void
172990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
173990b4856Slling     uint64_t intval, zprop_source_t src)
174990b4856Slling {
175990b4856Slling 	const char *propname = zpool_prop_to_name(prop);
176990b4856Slling 	nvlist_t *propval;
177990b4856Slling 
1789d82f4f6Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1799d82f4f6Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
180990b4856Slling 
1819d82f4f6Slling 	if (strval != NULL)
1829d82f4f6Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1839d82f4f6Slling 	else
1849d82f4f6Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
185990b4856Slling 
1869d82f4f6Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
187990b4856Slling 	nvlist_free(propval);
188990b4856Slling }
189990b4856Slling 
190990b4856Slling /*
191990b4856Slling  * Get property values from the spa configuration.
192990b4856Slling  */
1939d82f4f6Slling static void
194990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
195990b4856Slling {
1964263d13fSGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
197ad135b5dSChristopher Siden 	dsl_pool_t *pool = spa->spa_dsl_pool;
1982e4c9986SGeorge Wilson 	uint64_t size, alloc, cap, version;
199990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
200c5904d13Seschrock 	spa_config_dirent_t *dp;
2012e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
202990b4856Slling 
203e14bb325SJeff Bonwick 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
204e14bb325SJeff Bonwick 
2054263d13fSGeorge Wilson 	if (rvd != NULL) {
206485bbbf5SGeorge Wilson 		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
207b24ab676SJeff Bonwick 		size = metaslab_class_get_space(spa_normal_class(spa));
208379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
209379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
210485bbbf5SGeorge Wilson 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
211485bbbf5SGeorge Wilson 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
212485bbbf5SGeorge Wilson 		    size - alloc, src);
2134263d13fSGeorge Wilson 
2142e4c9986SGeorge Wilson 		spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
2152e4c9986SGeorge Wilson 		    metaslab_class_fragmentation(mc), src);
2162e4c9986SGeorge Wilson 		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
2172e4c9986SGeorge Wilson 		    metaslab_class_expandable_space(mc), src);
218f9af39baSGeorge Wilson 		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
219f9af39baSGeorge Wilson 		    (spa_mode(spa) == FREAD), src);
220379c004dSEric Schrock 
221485bbbf5SGeorge Wilson 		cap = (size == 0) ? 0 : (alloc * 100 / size);
222379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
223379c004dSEric Schrock 
224b24ab676SJeff Bonwick 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
225b24ab676SJeff Bonwick 		    ddt_get_pool_dedup_ratio(spa), src);
226b24ab676SJeff Bonwick 
227379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2284263d13fSGeorge Wilson 		    rvd->vdev_state, src);
229379c004dSEric Schrock 
230379c004dSEric Schrock 		version = spa_version(spa);
231379c004dSEric Schrock 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
232379c004dSEric Schrock 			src = ZPROP_SRC_DEFAULT;
233379c004dSEric Schrock 		else
234379c004dSEric Schrock 			src = ZPROP_SRC_LOCAL;
235379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
236379c004dSEric Schrock 	}
237990b4856Slling 
238ad135b5dSChristopher Siden 	if (pool != NULL) {
239ad135b5dSChristopher Siden 		/*
240ad135b5dSChristopher Siden 		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
241ad135b5dSChristopher Siden 		 * when opening pools before this version freedir will be NULL.
242ad135b5dSChristopher Siden 		 */
2437fd05ac4SMatthew Ahrens 		if (pool->dp_free_dir != NULL) {
244ad135b5dSChristopher Siden 			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
245c1379625SJustin T. Gibbs 			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
246c1379625SJustin T. Gibbs 			    src);
247ad135b5dSChristopher Siden 		} else {
248ad135b5dSChristopher Siden 			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
249ad135b5dSChristopher Siden 			    NULL, 0, src);
250ad135b5dSChristopher Siden 		}
2517fd05ac4SMatthew Ahrens 
2527fd05ac4SMatthew Ahrens 		if (pool->dp_leak_dir != NULL) {
2537fd05ac4SMatthew Ahrens 			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
254c1379625SJustin T. Gibbs 			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
255c1379625SJustin T. Gibbs 			    src);
2567fd05ac4SMatthew Ahrens 		} else {
2577fd05ac4SMatthew Ahrens 			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
2587fd05ac4SMatthew Ahrens 			    NULL, 0, src);
2597fd05ac4SMatthew Ahrens 		}
260ad135b5dSChristopher Siden 	}
261ad135b5dSChristopher Siden 
2629d82f4f6Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
263990b4856Slling 
2648704186eSDan McDonald 	if (spa->spa_comment != NULL) {
2658704186eSDan McDonald 		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2668704186eSDan McDonald 		    0, ZPROP_SRC_LOCAL);
2678704186eSDan McDonald 	}
2688704186eSDan McDonald 
2699d82f4f6Slling 	if (spa->spa_root != NULL)
2709d82f4f6Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2719d82f4f6Slling 		    0, ZPROP_SRC_LOCAL);
272990b4856Slling 
273b5152584SMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
274b5152584SMatthew Ahrens 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
275b5152584SMatthew Ahrens 		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
276b5152584SMatthew Ahrens 	} else {
277b5152584SMatthew Ahrens 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
278b5152584SMatthew Ahrens 		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
279b5152584SMatthew Ahrens 	}
280b5152584SMatthew Ahrens 
281c5904d13Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
282c5904d13Seschrock 		if (dp->scd_path == NULL) {
2839d82f4f6Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
284c5904d13Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
285c5904d13Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2869d82f4f6Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
287c5904d13Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2882f8aaab3Seschrock 		}
2892f8aaab3Seschrock 	}
290990b4856Slling }
291990b4856Slling 
292990b4856Slling /*
293990b4856Slling  * Get zpool property values.
294990b4856Slling  */
295990b4856Slling int
296990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
297990b4856Slling {
298b24ab676SJeff Bonwick 	objset_t *mos = spa->spa_meta_objset;
299990b4856Slling 	zap_cursor_t zc;
300990b4856Slling 	zap_attribute_t za;
301990b4856Slling 	int err;
302990b4856Slling 
3039d82f4f6Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
304990b4856Slling 
305e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
306e14bb325SJeff Bonwick 
307990b4856Slling 	/*
308990b4856Slling 	 * Get properties from the spa config.
309990b4856Slling 	 */
3109d82f4f6Slling 	spa_prop_get_config(spa, nvp);
311990b4856Slling 
312990b4856Slling 	/* If no pool property object, no more prop to get. */
313afee20e4SGeorge Wilson 	if (mos == NULL || spa->spa_pool_props_object == 0) {
314990b4856Slling 		mutex_exit(&spa->spa_props_lock);
315990b4856Slling 		return (0);
316990b4856Slling 	}
317990b4856Slling 
318990b4856Slling 	/*
319990b4856Slling 	 * Get properties from the MOS pool property object.
320990b4856Slling 	 */
321990b4856Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
322990b4856Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
323990b4856Slling 	    zap_cursor_advance(&zc)) {
324990b4856Slling 		uint64_t intval = 0;
325990b4856Slling 		char *strval = NULL;
326990b4856Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
327990b4856Slling 		zpool_prop_t prop;
328990b4856Slling 
329990b4856Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
330990b4856Slling 			continue;
331990b4856Slling 
332990b4856Slling 		switch (za.za_integer_length) {
333990b4856Slling 		case 8:
334990b4856Slling 			/* integer property */
335990b4856Slling 			if (za.za_first_integer !=
336990b4856Slling 			    zpool_prop_default_numeric(prop))
337990b4856Slling 				src = ZPROP_SRC_LOCAL;
338990b4856Slling 
339990b4856Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
340990b4856Slling 				dsl_pool_t *dp;
341990b4856Slling 				dsl_dataset_t *ds = NULL;
342990b4856Slling 
343990b4856Slling 				dp = spa_get_dsl(spa);
3443b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
345745cd3c5Smaybee 				if (err = dsl_dataset_hold_obj(dp,
346745cd3c5Smaybee 				    za.za_first_integer, FTAG, &ds)) {
3473b2aab18SMatthew Ahrens 					dsl_pool_config_exit(dp, FTAG);
348990b4856Slling 					break;
349990b4856Slling 				}
350990b4856Slling 
351990b4856Slling 				strval = kmem_alloc(
352990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
353990b4856Slling 				    KM_SLEEP);
354990b4856Slling 				dsl_dataset_name(ds, strval);
355745cd3c5Smaybee 				dsl_dataset_rele(ds, FTAG);
3563b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
357990b4856Slling 			} else {
358990b4856Slling 				strval = NULL;
359990b4856Slling 				intval = za.za_first_integer;
360990b4856Slling 			}
361990b4856Slling 
3629d82f4f6Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
363990b4856Slling 
364990b4856Slling 			if (strval != NULL)
365990b4856Slling 				kmem_free(strval,
366990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
367990b4856Slling 
368990b4856Slling 			break;
369990b4856Slling 
370990b4856Slling 		case 1:
371990b4856Slling 			/* string property */
372990b4856Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
373990b4856Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
374990b4856Slling 			    za.za_name, 1, za.za_num_integers, strval);
375990b4856Slling 			if (err) {
376990b4856Slling 				kmem_free(strval, za.za_num_integers);
377990b4856Slling 				break;
378990b4856Slling 			}
3799d82f4f6Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
380990b4856Slling 			kmem_free(strval, za.za_num_integers);
381990b4856Slling 			break;
382990b4856Slling 
383990b4856Slling 		default:
384990b4856Slling 			break;
385990b4856Slling 		}
386990b4856Slling 	}
387990b4856Slling 	zap_cursor_fini(&zc);
388990b4856Slling 	mutex_exit(&spa->spa_props_lock);
389990b4856Slling out:
390990b4856Slling 	if (err && err != ENOENT) {
391990b4856Slling 		nvlist_free(*nvp);
3929d82f4f6Slling 		*nvp = NULL;
393990b4856Slling 		return (err);
394990b4856Slling 	}
395990b4856Slling 
396990b4856Slling 	return (0);
397990b4856Slling }
398990b4856Slling 
399990b4856Slling /*
400990b4856Slling  * Validate the given pool properties nvlist and modify the list
401990b4856Slling  * for the property values to be set.
402990b4856Slling  */
403990b4856Slling static int
404990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
405990b4856Slling {
406990b4856Slling 	nvpair_t *elem;
407990b4856Slling 	int error = 0, reset_bootfs = 0;
408d5285caeSGeorge Wilson 	uint64_t objnum = 0;
409ad135b5dSChristopher Siden 	boolean_t has_feature = B_FALSE;
410990b4856Slling 
411990b4856Slling 	elem = NULL;
412990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
413990b4856Slling 		uint64_t intval;
414ad135b5dSChristopher Siden 		char *strval, *slash, *check, *fname;
415ad135b5dSChristopher Siden 		const char *propname = nvpair_name(elem);
416ad135b5dSChristopher Siden 		zpool_prop_t prop = zpool_name_to_prop(propname);
417ad135b5dSChristopher Siden 
418ad135b5dSChristopher Siden 		switch (prop) {
419ad135b5dSChristopher Siden 		case ZPROP_INVAL:
420ad135b5dSChristopher Siden 			if (!zpool_prop_feature(propname)) {
421be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
422ad135b5dSChristopher Siden 				break;
423ad135b5dSChristopher Siden 			}
424990b4856Slling 
425ad135b5dSChristopher Siden 			/*
426ad135b5dSChristopher Siden 			 * Sanitize the input.
427ad135b5dSChristopher Siden 			 */
428ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
429be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
430ad135b5dSChristopher Siden 				break;
431ad135b5dSChristopher Siden 			}
432990b4856Slling 
433ad135b5dSChristopher Siden 			if (nvpair_value_uint64(elem, &intval) != 0) {
434be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
435ad135b5dSChristopher Siden 				break;
436ad135b5dSChristopher Siden 			}
437ad135b5dSChristopher Siden 
438ad135b5dSChristopher Siden 			if (intval != 0) {
439be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
440ad135b5dSChristopher Siden 				break;
441ad135b5dSChristopher Siden 			}
442ad135b5dSChristopher Siden 
443ad135b5dSChristopher Siden 			fname = strchr(propname, '@') + 1;
444ad135b5dSChristopher Siden 			if (zfeature_lookup_name(fname, NULL) != 0) {
445be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
446ad135b5dSChristopher Siden 				break;
447ad135b5dSChristopher Siden 			}
448ad135b5dSChristopher Siden 
449ad135b5dSChristopher Siden 			has_feature = B_TRUE;
450ad135b5dSChristopher Siden 			break;
451990b4856Slling 
452990b4856Slling 		case ZPOOL_PROP_VERSION:
453990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
454990b4856Slling 			if (!error &&
455ad135b5dSChristopher Siden 			    (intval < spa_version(spa) ||
456ad135b5dSChristopher Siden 			    intval > SPA_VERSION_BEFORE_FEATURES ||
457ad135b5dSChristopher Siden 			    has_feature))
458be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
459990b4856Slling 			break;
460990b4856Slling 
461990b4856Slling 		case ZPOOL_PROP_DELEGATION:
462990b4856Slling 		case ZPOOL_PROP_AUTOREPLACE:
463d5b5bb25SRich Morris 		case ZPOOL_PROP_LISTSNAPS:
464573ca77eSGeorge Wilson 		case ZPOOL_PROP_AUTOEXPAND:
465990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
466990b4856Slling 			if (!error && intval > 1)
467be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
468990b4856Slling 			break;
469990b4856Slling 
470990b4856Slling 		case ZPOOL_PROP_BOOTFS:
47125f89ee2SJeff Bonwick 			/*
47225f89ee2SJeff Bonwick 			 * If the pool version is less than SPA_VERSION_BOOTFS,
47325f89ee2SJeff Bonwick 			 * or the pool is still being created (version == 0),
47425f89ee2SJeff Bonwick 			 * the bootfs property cannot be set.
47525f89ee2SJeff Bonwick 			 */
476990b4856Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
477be6fd75aSMatthew Ahrens 				error = SET_ERROR(ENOTSUP);
478990b4856Slling 				break;
479990b4856Slling 			}
480990b4856Slling 
481990b4856Slling 			/*
48215e6edf1Sgw 			 * Make sure the vdev config is bootable
483990b4856Slling 			 */
48415e6edf1Sgw 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
485be6fd75aSMatthew Ahrens 				error = SET_ERROR(ENOTSUP);
486990b4856Slling 				break;
487990b4856Slling 			}
488990b4856Slling 
489990b4856Slling 			reset_bootfs = 1;
490990b4856Slling 
491990b4856Slling 			error = nvpair_value_string(elem, &strval);
492990b4856Slling 
493990b4856Slling 			if (!error) {
494ad135b5dSChristopher Siden 				objset_t *os;
495b5152584SMatthew Ahrens 				uint64_t propval;
49615e6edf1Sgw 
497990b4856Slling 				if (strval == NULL || strval[0] == '\0') {
498990b4856Slling 					objnum = zpool_prop_default_numeric(
499990b4856Slling 					    ZPOOL_PROP_BOOTFS);
500990b4856Slling 					break;
501990b4856Slling 				}
502990b4856Slling 
503503ad85cSMatthew Ahrens 				if (error = dmu_objset_hold(strval, FTAG, &os))
504990b4856Slling 					break;
50515e6edf1Sgw 
506b5152584SMatthew Ahrens 				/*
507b5152584SMatthew Ahrens 				 * Must be ZPL, and its property settings
508b5152584SMatthew Ahrens 				 * must be supported by GRUB (compression
509b5152584SMatthew Ahrens 				 * is not gzip, and large blocks are not used).
510b5152584SMatthew Ahrens 				 */
511503ad85cSMatthew Ahrens 
512503ad85cSMatthew Ahrens 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
513be6fd75aSMatthew Ahrens 					error = SET_ERROR(ENOTSUP);
5143b2aab18SMatthew Ahrens 				} else if ((error =
5153b2aab18SMatthew Ahrens 				    dsl_prop_get_int_ds(dmu_objset_ds(os),
51615e6edf1Sgw 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
517b5152584SMatthew Ahrens 				    &propval)) == 0 &&
518b5152584SMatthew Ahrens 				    !BOOTFS_COMPRESS_VALID(propval)) {
519b5152584SMatthew Ahrens 					error = SET_ERROR(ENOTSUP);
520b5152584SMatthew Ahrens 				} else if ((error =
521b5152584SMatthew Ahrens 				    dsl_prop_get_int_ds(dmu_objset_ds(os),
522b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
523b5152584SMatthew Ahrens 				    &propval)) == 0 &&
524b5152584SMatthew Ahrens 				    propval > SPA_OLD_MAXBLOCKSIZE) {
525be6fd75aSMatthew Ahrens 					error = SET_ERROR(ENOTSUP);
52615e6edf1Sgw 				} else {
52715e6edf1Sgw 					objnum = dmu_objset_id(os);
52815e6edf1Sgw 				}
529503ad85cSMatthew Ahrens 				dmu_objset_rele(os, FTAG);
530990b4856Slling 			}
531990b4856Slling 			break;
532e14bb325SJeff Bonwick 
5330a4e9518Sgw 		case ZPOOL_PROP_FAILUREMODE:
5340a4e9518Sgw 			error = nvpair_value_uint64(elem, &intval);
5350a4e9518Sgw 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5360a4e9518Sgw 			    intval > ZIO_FAILURE_MODE_PANIC))
537be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
5380a4e9518Sgw 
5390a4e9518Sgw 			/*
5400a4e9518Sgw 			 * This is a special case which only occurs when
5410a4e9518Sgw 			 * the pool has completely failed. This allows
5420a4e9518Sgw 			 * the user to change the in-core failmode property
5430a4e9518Sgw 			 * without syncing it out to disk (I/Os might
5440a4e9518Sgw 			 * currently be blocked). We do this by returning
5450a4e9518Sgw 			 * EIO to the caller (spa_prop_set) to trick it
5460a4e9518Sgw 			 * into thinking we encountered a property validation
5470a4e9518Sgw 			 * error.
5480a4e9518Sgw 			 */
549e14bb325SJeff Bonwick 			if (!error && spa_suspended(spa)) {
5500a4e9518Sgw 				spa->spa_failmode = intval;
551be6fd75aSMatthew Ahrens 				error = SET_ERROR(EIO);
5520a4e9518Sgw 			}
5530a4e9518Sgw 			break;
5542f8aaab3Seschrock 
5552f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5562f8aaab3Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
5572f8aaab3Seschrock 				break;
5582f8aaab3Seschrock 
5592f8aaab3Seschrock 			if (strval[0] == '\0')
5602f8aaab3Seschrock 				break;
5612f8aaab3Seschrock 
5622f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5632f8aaab3Seschrock 				break;
5642f8aaab3Seschrock 
5652f8aaab3Seschrock 			if (strval[0] != '/') {
566be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
5672f8aaab3Seschrock 				break;
5682f8aaab3Seschrock 			}
5692f8aaab3Seschrock 
5702f8aaab3Seschrock 			slash = strrchr(strval, '/');
5712f8aaab3Seschrock 			ASSERT(slash != NULL);
5722f8aaab3Seschrock 
5732f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5742f8aaab3Seschrock 			    strcmp(slash, "/..") == 0)
575be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
5762f8aaab3Seschrock 			break;
577b24ab676SJeff Bonwick 
5788704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
5798704186eSDan McDonald 			if ((error = nvpair_value_string(elem, &strval)) != 0)
5808704186eSDan McDonald 				break;
5818704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
5828704186eSDan McDonald 				/*
5838704186eSDan McDonald 				 * The kernel doesn't have an easy isprint()
5848704186eSDan McDonald 				 * check.  For this kernel check, we merely
5858704186eSDan McDonald 				 * check ASCII apart from DEL.  Fix this if
5868704186eSDan McDonald 				 * there is an easy-to-use kernel isprint().
5878704186eSDan McDonald 				 */
5888704186eSDan McDonald 				if (*check >= 0x7f) {
589be6fd75aSMatthew Ahrens 					error = SET_ERROR(EINVAL);
5908704186eSDan McDonald 					break;
5918704186eSDan McDonald 				}
5928704186eSDan McDonald 				check++;
5938704186eSDan McDonald 			}
5948704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT)
5958704186eSDan McDonald 				error = E2BIG;
5968704186eSDan McDonald 			break;
5978704186eSDan McDonald 
598b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPDITTO:
599b24ab676SJeff Bonwick 			if (spa_version(spa) < SPA_VERSION_DEDUP)
600be6fd75aSMatthew Ahrens 				error = SET_ERROR(ENOTSUP);
601b24ab676SJeff Bonwick 			else
602b24ab676SJeff Bonwick 				error = nvpair_value_uint64(elem, &intval);
603b24ab676SJeff Bonwick 			if (error == 0 &&
604b24ab676SJeff Bonwick 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
605be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
606b24ab676SJeff Bonwick 			break;
607990b4856Slling 		}
608990b4856Slling 
609990b4856Slling 		if (error)
610990b4856Slling 			break;
611990b4856Slling 	}
612990b4856Slling 
613990b4856Slling 	if (!error && reset_bootfs) {
614990b4856Slling 		error = nvlist_remove(props,
615990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
616990b4856Slling 
617990b4856Slling 		if (!error) {
618990b4856Slling 			error = nvlist_add_uint64(props,
619990b4856Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
620990b4856Slling 		}
621990b4856Slling 	}
622990b4856Slling 
623990b4856Slling 	return (error);
624990b4856Slling }
625990b4856Slling 
626379c004dSEric Schrock void
627379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
628379c004dSEric Schrock {
629379c004dSEric Schrock 	char *cachefile;
630379c004dSEric Schrock 	spa_config_dirent_t *dp;
631379c004dSEric Schrock 
632379c004dSEric Schrock 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
633379c004dSEric Schrock 	    &cachefile) != 0)
634379c004dSEric Schrock 		return;
635379c004dSEric Schrock 
636379c004dSEric Schrock 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
637379c004dSEric Schrock 	    KM_SLEEP);
638379c004dSEric Schrock 
639379c004dSEric Schrock 	if (cachefile[0] == '\0')
640379c004dSEric Schrock 		dp->scd_path = spa_strdup(spa_config_path);
641379c004dSEric Schrock 	else if (strcmp(cachefile, "none") == 0)
642379c004dSEric Schrock 		dp->scd_path = NULL;
643379c004dSEric Schrock 	else
644379c004dSEric Schrock 		dp->scd_path = spa_strdup(cachefile);
645379c004dSEric Schrock 
646379c004dSEric Schrock 	list_insert_head(&spa->spa_config_list, dp);
647379c004dSEric Schrock 	if (need_sync)
648379c004dSEric Schrock 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
649379c004dSEric Schrock }
650379c004dSEric Schrock 
651990b4856Slling int
652990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
653990b4856Slling {
654990b4856Slling 	int error;
655ad135b5dSChristopher Siden 	nvpair_t *elem = NULL;
656379c004dSEric Schrock 	boolean_t need_sync = B_FALSE;
657990b4856Slling 
658990b4856Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
659990b4856Slling 		return (error);
660990b4856Slling 
661379c004dSEric Schrock 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
662ad135b5dSChristopher Siden 		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
663379c004dSEric Schrock 
664f9af39baSGeorge Wilson 		if (prop == ZPOOL_PROP_CACHEFILE ||
665f9af39baSGeorge Wilson 		    prop == ZPOOL_PROP_ALTROOT ||
666f9af39baSGeorge Wilson 		    prop == ZPOOL_PROP_READONLY)
667379c004dSEric Schrock 			continue;
668379c004dSEric Schrock 
669ad135b5dSChristopher Siden 		if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
670ad135b5dSChristopher Siden 			uint64_t ver;
671ad135b5dSChristopher Siden 
672ad135b5dSChristopher Siden 			if (prop == ZPOOL_PROP_VERSION) {
673ad135b5dSChristopher Siden 				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
674ad135b5dSChristopher Siden 			} else {
675ad135b5dSChristopher Siden 				ASSERT(zpool_prop_feature(nvpair_name(elem)));
676ad135b5dSChristopher Siden 				ver = SPA_VERSION_FEATURES;
677ad135b5dSChristopher Siden 				need_sync = B_TRUE;
678ad135b5dSChristopher Siden 			}
679ad135b5dSChristopher Siden 
680ad135b5dSChristopher Siden 			/* Save time if the version is already set. */
681ad135b5dSChristopher Siden 			if (ver == spa_version(spa))
682ad135b5dSChristopher Siden 				continue;
683ad135b5dSChristopher Siden 
684ad135b5dSChristopher Siden 			/*
685ad135b5dSChristopher Siden 			 * In addition to the pool directory object, we might
686ad135b5dSChristopher Siden 			 * create the pool properties object, the features for
687ad135b5dSChristopher Siden 			 * read object, the features for write object, or the
688ad135b5dSChristopher Siden 			 * feature descriptions object.
689ad135b5dSChristopher Siden 			 */
6903b2aab18SMatthew Ahrens 			error = dsl_sync_task(spa->spa_name, NULL,
6917d46dc6cSMatthew Ahrens 			    spa_sync_version, &ver,
6927d46dc6cSMatthew Ahrens 			    6, ZFS_SPACE_CHECK_RESERVED);
693ad135b5dSChristopher Siden 			if (error)
694ad135b5dSChristopher Siden 				return (error);
695ad135b5dSChristopher Siden 			continue;
696ad135b5dSChristopher Siden 		}
697ad135b5dSChristopher Siden 
698379c004dSEric Schrock 		need_sync = B_TRUE;
699379c004dSEric Schrock 		break;
700379c004dSEric Schrock 	}
701379c004dSEric Schrock 
702ad135b5dSChristopher Siden 	if (need_sync) {
7033b2aab18SMatthew Ahrens 		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
7047d46dc6cSMatthew Ahrens 		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
705ad135b5dSChristopher Siden 	}
706ad135b5dSChristopher Siden 
707ad135b5dSChristopher Siden 	return (0);
708990b4856Slling }
709990b4856Slling 
710990b4856Slling /*
711990b4856Slling  * If the bootfs property value is dsobj, clear it.
712990b4856Slling  */
713990b4856Slling void
714990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
715990b4856Slling {
716990b4856Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
717990b4856Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
718990b4856Slling 		    spa->spa_pool_props_object,
719990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
720990b4856Slling 		spa->spa_bootfs = 0;
721990b4856Slling 	}
722990b4856Slling }
723990b4856Slling 
724dfbb9432SGeorge Wilson /*ARGSUSED*/
725dfbb9432SGeorge Wilson static int
7263b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx)
727dfbb9432SGeorge Wilson {
7283b2aab18SMatthew Ahrens 	uint64_t *newguid = arg;
7293b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
730dfbb9432SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
731dfbb9432SGeorge Wilson 	uint64_t vdev_state;
732dfbb9432SGeorge Wilson 
733dfbb9432SGeorge Wilson 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
734dfbb9432SGeorge Wilson 	vdev_state = rvd->vdev_state;
735dfbb9432SGeorge Wilson 	spa_config_exit(spa, SCL_STATE, FTAG);
736dfbb9432SGeorge Wilson 
737dfbb9432SGeorge Wilson 	if (vdev_state != VDEV_STATE_HEALTHY)
738be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
739dfbb9432SGeorge Wilson 
740dfbb9432SGeorge Wilson 	ASSERT3U(spa_guid(spa), !=, *newguid);
741dfbb9432SGeorge Wilson 
742dfbb9432SGeorge Wilson 	return (0);
743dfbb9432SGeorge Wilson }
744dfbb9432SGeorge Wilson 
745dfbb9432SGeorge Wilson static void
7463b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx)
747dfbb9432SGeorge Wilson {
7483b2aab18SMatthew Ahrens 	uint64_t *newguid = arg;
7493b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
750dfbb9432SGeorge Wilson 	uint64_t oldguid;
751dfbb9432SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
752dfbb9432SGeorge Wilson 
753dfbb9432SGeorge Wilson 	oldguid = spa_guid(spa);
754dfbb9432SGeorge Wilson 
755dfbb9432SGeorge Wilson 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
756dfbb9432SGeorge Wilson 	rvd->vdev_guid = *newguid;
757dfbb9432SGeorge Wilson 	rvd->vdev_guid_sum += (*newguid - oldguid);
758dfbb9432SGeorge Wilson 	vdev_config_dirty(rvd);
759dfbb9432SGeorge Wilson 	spa_config_exit(spa, SCL_STATE, FTAG);
760dfbb9432SGeorge Wilson 
76120128a08SGeorge Wilson 	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
762dfbb9432SGeorge Wilson 	    oldguid, *newguid);
763dfbb9432SGeorge Wilson }
764dfbb9432SGeorge Wilson 
765e9103aaeSGarrett D'Amore /*
766e9103aaeSGarrett D'Amore  * Change the GUID for the pool.  This is done so that we can later
767e9103aaeSGarrett D'Amore  * re-import a pool built from a clone of our own vdevs.  We will modify
768e9103aaeSGarrett D'Amore  * the root vdev's guid, our own pool guid, and then mark all of our
769e9103aaeSGarrett D'Amore  * vdevs dirty.  Note that we must make sure that all our vdevs are
770e9103aaeSGarrett D'Amore  * online when we do this, or else any vdevs that weren't present
771e9103aaeSGarrett D'Amore  * would be orphaned from our pool.  We are also going to issue a
772e9103aaeSGarrett D'Amore  * sysevent to update any watchers.
773e9103aaeSGarrett D'Amore  */
774e9103aaeSGarrett D'Amore int
775e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa)
776e9103aaeSGarrett D'Amore {
777dfbb9432SGeorge Wilson 	int error;
778dfbb9432SGeorge Wilson 	uint64_t guid;
779e9103aaeSGarrett D'Amore 
7802c1e2b44SGeorge Wilson 	mutex_enter(&spa->spa_vdev_top_lock);
781dfbb9432SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
782dfbb9432SGeorge Wilson 	guid = spa_generate_guid(NULL);
783e9103aaeSGarrett D'Amore 
7843b2aab18SMatthew Ahrens 	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
7857d46dc6cSMatthew Ahrens 	    spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
786e9103aaeSGarrett D'Amore 
787dfbb9432SGeorge Wilson 	if (error == 0) {
788dfbb9432SGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
789dfbb9432SGeorge Wilson 		spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
790dfbb9432SGeorge Wilson 	}
791e9103aaeSGarrett D'Amore 
792dfbb9432SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
7932c1e2b44SGeorge Wilson 	mutex_exit(&spa->spa_vdev_top_lock);
794e9103aaeSGarrett D'Amore 
795dfbb9432SGeorge Wilson 	return (error);
796e9103aaeSGarrett D'Amore }
797e9103aaeSGarrett D'Amore 
798fa9e4066Sahrens /*
799fa9e4066Sahrens  * ==========================================================================
800fa9e4066Sahrens  * SPA state manipulation (open/create/destroy/import/export)
801fa9e4066Sahrens  * ==========================================================================
802fa9e4066Sahrens  */
803fa9e4066Sahrens 
804ea8dc4b6Seschrock static int
805ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b)
806ea8dc4b6Seschrock {
807ea8dc4b6Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
808ea8dc4b6Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
809ea8dc4b6Seschrock 	int ret;
810ea8dc4b6Seschrock 
811ea8dc4b6Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
8127802d7bfSMatthew Ahrens 	    sizeof (zbookmark_phys_t));
813ea8dc4b6Seschrock 
814ea8dc4b6Seschrock 	if (ret < 0)
815ea8dc4b6Seschrock 		return (-1);
816ea8dc4b6Seschrock 	else if (ret > 0)
817ea8dc4b6Seschrock 		return (1);
818ea8dc4b6Seschrock 	else
819ea8dc4b6Seschrock 		return (0);
820ea8dc4b6Seschrock }
821ea8dc4b6Seschrock 
822ea8dc4b6Seschrock /*
823ea8dc4b6Seschrock  * Utility function which retrieves copies of the current logs and
824ea8dc4b6Seschrock  * re-initializes them in the process.
825ea8dc4b6Seschrock  */
826ea8dc4b6Seschrock void
827ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
828ea8dc4b6Seschrock {
829ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
830ea8dc4b6Seschrock 
831ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
832ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
833ea8dc4b6Seschrock 
834ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
835ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
836ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
837ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
838ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
839ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
840ea8dc4b6Seschrock }
841ea8dc4b6Seschrock 
842ec94d322SAdam Leventhal static void
843ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
844fa9e4066Sahrens {
845ec94d322SAdam Leventhal 	const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
846ec94d322SAdam Leventhal 	enum zti_modes mode = ztip->zti_mode;
847ec94d322SAdam Leventhal 	uint_t value = ztip->zti_value;
848ec94d322SAdam Leventhal 	uint_t count = ztip->zti_count;
849ec94d322SAdam Leventhal 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
850ec94d322SAdam Leventhal 	char name[32];
8515aeb9474SGarrett D'Amore 	uint_t flags = 0;
85235a5a358SJonathan Adams 	boolean_t batch = B_FALSE;
853fa9e4066Sahrens 
854ec94d322SAdam Leventhal 	if (mode == ZTI_MODE_NULL) {
855ec94d322SAdam Leventhal 		tqs->stqs_count = 0;
856ec94d322SAdam Leventhal 		tqs->stqs_taskq = NULL;
857ec94d322SAdam Leventhal 		return;
858ec94d322SAdam Leventhal 	}
859fa9e4066Sahrens 
860ec94d322SAdam Leventhal 	ASSERT3U(count, >, 0);
861fa9e4066Sahrens 
862ec94d322SAdam Leventhal 	tqs->stqs_count = count;
863ec94d322SAdam Leventhal 	tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
86435a5a358SJonathan Adams 
86569962b56SMatthew Ahrens 	switch (mode) {
86669962b56SMatthew Ahrens 	case ZTI_MODE_FIXED:
86769962b56SMatthew Ahrens 		ASSERT3U(value, >=, 1);
86869962b56SMatthew Ahrens 		value = MAX(value, 1);
86969962b56SMatthew Ahrens 		break;
870ec94d322SAdam Leventhal 
87169962b56SMatthew Ahrens 	case ZTI_MODE_BATCH:
87269962b56SMatthew Ahrens 		batch = B_TRUE;
87369962b56SMatthew Ahrens 		flags |= TASKQ_THREADS_CPU_PCT;
87469962b56SMatthew Ahrens 		value = zio_taskq_batch_pct;
87569962b56SMatthew Ahrens 		break;
876ec94d322SAdam Leventhal 
87769962b56SMatthew Ahrens 	default:
87869962b56SMatthew Ahrens 		panic("unrecognized mode for %s_%s taskq (%u:%u) in "
87969962b56SMatthew Ahrens 		    "spa_activate()",
88069962b56SMatthew Ahrens 		    zio_type_name[t], zio_taskq_types[q], mode, value);
88169962b56SMatthew Ahrens 		break;
88269962b56SMatthew Ahrens 	}
883ec94d322SAdam Leventhal 
88469962b56SMatthew Ahrens 	for (uint_t i = 0; i < count; i++) {
88569962b56SMatthew Ahrens 		taskq_t *tq;
886ec94d322SAdam Leventhal 
887ec94d322SAdam Leventhal 		if (count > 1) {
888ec94d322SAdam Leventhal 			(void) snprintf(name, sizeof (name), "%s_%s_%u",
889ec94d322SAdam Leventhal 			    zio_type_name[t], zio_taskq_types[q], i);
890ec94d322SAdam Leventhal 		} else {
891ec94d322SAdam Leventhal 			(void) snprintf(name, sizeof (name), "%s_%s",
892ec94d322SAdam Leventhal 			    zio_type_name[t], zio_taskq_types[q]);
893ec94d322SAdam Leventhal 		}
894ec94d322SAdam Leventhal 
895ec94d322SAdam Leventhal 		if (zio_taskq_sysdc && spa->spa_proc != &p0) {
896ec94d322SAdam Leventhal 			if (batch)
897ec94d322SAdam Leventhal 				flags |= TASKQ_DC_BATCH;
898ec94d322SAdam Leventhal 
899ec94d322SAdam Leventhal 			tq = taskq_create_sysdc(name, value, 50, INT_MAX,
900ec94d322SAdam Leventhal 			    spa->spa_proc, zio_taskq_basedc, flags);
901ec94d322SAdam Leventhal 		} else {
90269962b56SMatthew Ahrens 			pri_t pri = maxclsyspri;
90369962b56SMatthew Ahrens 			/*
90469962b56SMatthew Ahrens 			 * The write issue taskq can be extremely CPU
90569962b56SMatthew Ahrens 			 * intensive.  Run it at slightly lower priority
90669962b56SMatthew Ahrens 			 * than the other taskqs.
90769962b56SMatthew Ahrens 			 */
90869962b56SMatthew Ahrens 			if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
90969962b56SMatthew Ahrens 				pri--;
91069962b56SMatthew Ahrens 
91169962b56SMatthew Ahrens 			tq = taskq_create_proc(name, value, pri, 50,
912ec94d322SAdam Leventhal 			    INT_MAX, spa->spa_proc, flags);
913ec94d322SAdam Leventhal 		}
914ec94d322SAdam Leventhal 
915ec94d322SAdam Leventhal 		tqs->stqs_taskq[i] = tq;
916ec94d322SAdam Leventhal 	}
917ec94d322SAdam Leventhal }
918ec94d322SAdam Leventhal 
919ec94d322SAdam Leventhal static void
920ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
921ec94d322SAdam Leventhal {
922ec94d322SAdam Leventhal 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
923ec94d322SAdam Leventhal 
924ec94d322SAdam Leventhal 	if (tqs->stqs_taskq == NULL) {
925ec94d322SAdam Leventhal 		ASSERT0(tqs->stqs_count);
926ec94d322SAdam Leventhal 		return;
927ec94d322SAdam Leventhal 	}
928ec94d322SAdam Leventhal 
929ec94d322SAdam Leventhal 	for (uint_t i = 0; i < tqs->stqs_count; i++) {
930ec94d322SAdam Leventhal 		ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
931ec94d322SAdam Leventhal 		taskq_destroy(tqs->stqs_taskq[i]);
93235a5a358SJonathan Adams 	}
93335a5a358SJonathan Adams 
934ec94d322SAdam Leventhal 	kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
935ec94d322SAdam Leventhal 	tqs->stqs_taskq = NULL;
936ec94d322SAdam Leventhal }
93735a5a358SJonathan Adams 
938ec94d322SAdam Leventhal /*
939ec94d322SAdam Leventhal  * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
940ec94d322SAdam Leventhal  * Note that a type may have multiple discrete taskqs to avoid lock contention
941ec94d322SAdam Leventhal  * on the taskq itself. In that case we choose which taskq at random by using
942ec94d322SAdam Leventhal  * the low bits of gethrtime().
943ec94d322SAdam Leventhal  */
944ec94d322SAdam Leventhal void
945ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
946ec94d322SAdam Leventhal     task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
947ec94d322SAdam Leventhal {
948ec94d322SAdam Leventhal 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
949ec94d322SAdam Leventhal 	taskq_t *tq;
950ec94d322SAdam Leventhal 
951ec94d322SAdam Leventhal 	ASSERT3P(tqs->stqs_taskq, !=, NULL);
952ec94d322SAdam Leventhal 	ASSERT3U(tqs->stqs_count, !=, 0);
953ec94d322SAdam Leventhal 
954ec94d322SAdam Leventhal 	if (tqs->stqs_count == 1) {
955ec94d322SAdam Leventhal 		tq = tqs->stqs_taskq[0];
956ec94d322SAdam Leventhal 	} else {
957ec94d322SAdam Leventhal 		tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
95835a5a358SJonathan Adams 	}
959ec94d322SAdam Leventhal 
960ec94d322SAdam Leventhal 	taskq_dispatch_ent(tq, func, arg, flags, ent);
96135a5a358SJonathan Adams }
96235a5a358SJonathan Adams 
96335a5a358SJonathan Adams static void
96435a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa)
96535a5a358SJonathan Adams {
966e14bb325SJeff Bonwick 	for (int t = 0; t < ZIO_TYPES; t++) {
967e14bb325SJeff Bonwick 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
968ec94d322SAdam Leventhal 			spa_taskqs_init(spa, t, q);
96935a5a358SJonathan Adams 		}
97035a5a358SJonathan Adams 	}
97135a5a358SJonathan Adams }
97235a5a358SJonathan Adams 
97335a5a358SJonathan Adams #ifdef _KERNEL
97435a5a358SJonathan Adams static void
97535a5a358SJonathan Adams spa_thread(void *arg)
97635a5a358SJonathan Adams {
97735a5a358SJonathan Adams 	callb_cpr_t cprinfo;
9782e0c549eSJonathan Adams 
97935a5a358SJonathan Adams 	spa_t *spa = arg;
98035a5a358SJonathan Adams 	user_t *pu = PTOU(curproc);
9812e0c549eSJonathan Adams 
98235a5a358SJonathan Adams 	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
98335a5a358SJonathan Adams 	    spa->spa_name);
9842e0c549eSJonathan Adams 
98535a5a358SJonathan Adams 	ASSERT(curproc != &p0);
98635a5a358SJonathan Adams 	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
98735a5a358SJonathan Adams 	    "zpool-%s", spa->spa_name);
98835a5a358SJonathan Adams 	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
9892e0c549eSJonathan Adams 
99035a5a358SJonathan Adams 	/* bind this thread to the requested psrset */
99135a5a358SJonathan Adams 	if (zio_taskq_psrset_bind != PS_NONE) {
99235a5a358SJonathan Adams 		pool_lock();
99335a5a358SJonathan Adams 		mutex_enter(&cpu_lock);
99435a5a358SJonathan Adams 		mutex_enter(&pidlock);
99535a5a358SJonathan Adams 		mutex_enter(&curproc->p_lock);
99680eb36f2SGeorge Wilson 
99735a5a358SJonathan Adams 		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
99835a5a358SJonathan Adams 		    0, NULL, NULL) == 0)  {
99935a5a358SJonathan Adams 			curthread->t_bind_pset = zio_taskq_psrset_bind;
100035a5a358SJonathan Adams 		} else {
100135a5a358SJonathan Adams 			cmn_err(CE_WARN,
100235a5a358SJonathan Adams 			    "Couldn't bind process for zfs pool \"%s\" to "
100335a5a358SJonathan Adams 			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
100435a5a358SJonathan Adams 		}
100535a5a358SJonathan Adams 
100635a5a358SJonathan Adams 		mutex_exit(&curproc->p_lock);
100735a5a358SJonathan Adams 		mutex_exit(&pidlock);
100835a5a358SJonathan Adams 		mutex_exit(&cpu_lock);
100935a5a358SJonathan Adams 		pool_unlock();
101035a5a358SJonathan Adams 	}
101135a5a358SJonathan Adams 
101235a5a358SJonathan Adams 	if (zio_taskq_sysdc) {
101335a5a358SJonathan Adams 		sysdc_thread_enter(curthread, 100, 0);
101435a5a358SJonathan Adams 	}
101535a5a358SJonathan Adams 
101635a5a358SJonathan Adams 	spa->spa_proc = curproc;
101735a5a358SJonathan Adams 	spa->spa_did = curthread->t_did;
101835a5a358SJonathan Adams 
101935a5a358SJonathan Adams 	spa_create_zio_taskqs(spa);
102035a5a358SJonathan Adams 
102135a5a358SJonathan Adams 	mutex_enter(&spa->spa_proc_lock);
102235a5a358SJonathan Adams 	ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
102335a5a358SJonathan Adams 
102435a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_ACTIVE;
102535a5a358SJonathan Adams 	cv_broadcast(&spa->spa_proc_cv);
102635a5a358SJonathan Adams 
102735a5a358SJonathan Adams 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
102835a5a358SJonathan Adams 	while (spa->spa_proc_state == SPA_PROC_ACTIVE)
102935a5a358SJonathan Adams 		cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
103035a5a358SJonathan Adams 	CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
103135a5a358SJonathan Adams 
103235a5a358SJonathan Adams 	ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
103335a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_GONE;
103435a5a358SJonathan Adams 	spa->spa_proc = &p0;
103535a5a358SJonathan Adams 	cv_broadcast(&spa->spa_proc_cv);
103635a5a358SJonathan Adams 	CALLB_CPR_EXIT(&cprinfo);	/* drops spa_proc_lock */
103735a5a358SJonathan Adams 
103835a5a358SJonathan Adams 	mutex_enter(&curproc->p_lock);
103935a5a358SJonathan Adams 	lwp_exit();
104035a5a358SJonathan Adams }
104135a5a358SJonathan Adams #endif
104235a5a358SJonathan Adams 
104335a5a358SJonathan Adams /*
104435a5a358SJonathan Adams  * Activate an uninitialized pool.
104535a5a358SJonathan Adams  */
104635a5a358SJonathan Adams static void
104735a5a358SJonathan Adams spa_activate(spa_t *spa, int mode)
104835a5a358SJonathan Adams {
104935a5a358SJonathan Adams 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
105035a5a358SJonathan Adams 
105135a5a358SJonathan Adams 	spa->spa_state = POOL_STATE_ACTIVE;
105235a5a358SJonathan Adams 	spa->spa_mode = mode;
105335a5a358SJonathan Adams 
105435a5a358SJonathan Adams 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
105535a5a358SJonathan Adams 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
105635a5a358SJonathan Adams 
105735a5a358SJonathan Adams 	/* Try to create a covering process */
105835a5a358SJonathan Adams 	mutex_enter(&spa->spa_proc_lock);
105935a5a358SJonathan Adams 	ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
106035a5a358SJonathan Adams 	ASSERT(spa->spa_proc == &p0);
106135a5a358SJonathan Adams 	spa->spa_did = 0;
106235a5a358SJonathan Adams 
106335a5a358SJonathan Adams 	/* Only create a process if we're going to be around a while. */
106435a5a358SJonathan Adams 	if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
106535a5a358SJonathan Adams 		if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
106635a5a358SJonathan Adams 		    NULL, 0) == 0) {
106735a5a358SJonathan Adams 			spa->spa_proc_state = SPA_PROC_CREATED;
106835a5a358SJonathan Adams 			while (spa->spa_proc_state == SPA_PROC_CREATED) {
106935a5a358SJonathan Adams 				cv_wait(&spa->spa_proc_cv,
107035a5a358SJonathan Adams 				    &spa->spa_proc_lock);
10712e0c549eSJonathan Adams 			}
107235a5a358SJonathan Adams 			ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
107335a5a358SJonathan Adams 			ASSERT(spa->spa_proc != &p0);
107435a5a358SJonathan Adams 			ASSERT(spa->spa_did != 0);
107535a5a358SJonathan Adams 		} else {
107635a5a358SJonathan Adams #ifdef _KERNEL
107735a5a358SJonathan Adams 			cmn_err(CE_WARN,
107835a5a358SJonathan Adams 			    "Couldn't create process for zfs pool \"%s\"\n",
107935a5a358SJonathan Adams 			    spa->spa_name);
108035a5a358SJonathan Adams #endif
1081e14bb325SJeff Bonwick 		}
1082fa9e4066Sahrens 	}
108335a5a358SJonathan Adams 	mutex_exit(&spa->spa_proc_lock);
108435a5a358SJonathan Adams 
108535a5a358SJonathan Adams 	/* If we didn't create a process, we need to create our taskqs. */
108635a5a358SJonathan Adams 	if (spa->spa_proc == &p0) {
108735a5a358SJonathan Adams 		spa_create_zio_taskqs(spa);
108835a5a358SJonathan Adams 	}
1089fa9e4066Sahrens 
1090e14bb325SJeff Bonwick 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1091e14bb325SJeff Bonwick 	    offsetof(vdev_t, vdev_config_dirty_node));
1092bc9014e6SJustin Gibbs 	list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1093bc9014e6SJustin Gibbs 	    offsetof(objset_t, os_evicting_node));
1094e14bb325SJeff Bonwick 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1095e14bb325SJeff Bonwick 	    offsetof(vdev_t, vdev_state_dirty_node));
1096fa9e4066Sahrens 
1097fa9e4066Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
1098fa9e4066Sahrens 	    offsetof(struct vdev, vdev_txg_node));
1099ea8dc4b6Seschrock 
1100ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
1101ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1102ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
1103ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
1104ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1105ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
1106fa9e4066Sahrens }
1107fa9e4066Sahrens 
1108fa9e4066Sahrens /*
1109fa9e4066Sahrens  * Opposite of spa_activate().
1110fa9e4066Sahrens  */
1111fa9e4066Sahrens static void
1112fa9e4066Sahrens spa_deactivate(spa_t *spa)
1113fa9e4066Sahrens {
1114fa9e4066Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
1115fa9e4066Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
1116fa9e4066Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
111725f89ee2SJeff Bonwick 	ASSERT(spa->spa_async_zio_root == NULL);
1118fa9e4066Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1119fa9e4066Sahrens 
1120bc9014e6SJustin Gibbs 	spa_evicting_os_wait(spa);
1121bc9014e6SJustin Gibbs 
1122fa9e4066Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
1123fa9e4066Sahrens 
1124e14bb325SJeff Bonwick 	list_destroy(&spa->spa_config_dirty_list);
1125bc9014e6SJustin Gibbs 	list_destroy(&spa->spa_evicting_os_list);
1126e14bb325SJeff Bonwick 	list_destroy(&spa->spa_state_dirty_list);
1127fa9e4066Sahrens 
1128e14bb325SJeff Bonwick 	for (int t = 0; t < ZIO_TYPES; t++) {
1129e14bb325SJeff Bonwick 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1130ec94d322SAdam Leventhal 			spa_taskqs_fini(spa, t, q);
1131e14bb325SJeff Bonwick 		}
1132fa9e4066Sahrens 	}
1133fa9e4066Sahrens 
1134fa9e4066Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
1135fa9e4066Sahrens 	spa->spa_normal_class = NULL;
1136fa9e4066Sahrens 
11378654d025Sperrin 	metaslab_class_destroy(spa->spa_log_class);
11388654d025Sperrin 	spa->spa_log_class = NULL;
11398654d025Sperrin 
1140ea8dc4b6Seschrock 	/*
1141ea8dc4b6Seschrock 	 * If this was part of an import or the open otherwise failed, we may
1142ea8dc4b6Seschrock 	 * still have errors left in the queues.  Empty them just in case.
1143ea8dc4b6Seschrock 	 */
1144ea8dc4b6Seschrock 	spa_errlog_drain(spa);
1145ea8dc4b6Seschrock 
1146ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
1147ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_last);
1148ea8dc4b6Seschrock 
1149fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
115035a5a358SJonathan Adams 
115135a5a358SJonathan Adams 	mutex_enter(&spa->spa_proc_lock);
115235a5a358SJonathan Adams 	if (spa->spa_proc_state != SPA_PROC_NONE) {
115335a5a358SJonathan Adams 		ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
115435a5a358SJonathan Adams 		spa->spa_proc_state = SPA_PROC_DEACTIVATE;
115535a5a358SJonathan Adams 		cv_broadcast(&spa->spa_proc_cv);
115635a5a358SJonathan Adams 		while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
115735a5a358SJonathan Adams 			ASSERT(spa->spa_proc != &p0);
115835a5a358SJonathan Adams 			cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
115935a5a358SJonathan Adams 		}
116035a5a358SJonathan Adams 		ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
116135a5a358SJonathan Adams 		spa->spa_proc_state = SPA_PROC_NONE;
116235a5a358SJonathan Adams 	}
116335a5a358SJonathan Adams 	ASSERT(spa->spa_proc == &p0);
116435a5a358SJonathan Adams 	mutex_exit(&spa->spa_proc_lock);
116535a5a358SJonathan Adams 
116635a5a358SJonathan Adams 	/*
116735a5a358SJonathan Adams 	 * We want to make sure spa_thread() has actually exited the ZFS
116835a5a358SJonathan Adams 	 * module, so that the module can't be unloaded out from underneath
116935a5a358SJonathan Adams 	 * it.
117035a5a358SJonathan Adams 	 */
117135a5a358SJonathan Adams 	if (spa->spa_did != 0) {
117235a5a358SJonathan Adams 		thread_join(spa->spa_did);
117335a5a358SJonathan Adams 		spa->spa_did = 0;
117435a5a358SJonathan Adams 	}
1175fa9e4066Sahrens }
1176fa9e4066Sahrens 
1177fa9e4066Sahrens /*
1178fa9e4066Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
1179fa9e4066Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
1180fa9e4066Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
1181fa9e4066Sahrens  * All vdev validation is done by the vdev_alloc() routine.
1182fa9e4066Sahrens  */
118399653d4eSeschrock static int
118499653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
118599653d4eSeschrock     uint_t id, int atype)
1186fa9e4066Sahrens {
1187fa9e4066Sahrens 	nvlist_t **child;
1188573ca77eSGeorge Wilson 	uint_t children;
118999653d4eSeschrock 	int error;
1190fa9e4066Sahrens 
119199653d4eSeschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
119299653d4eSeschrock 		return (error);
1193fa9e4066Sahrens 
119499653d4eSeschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
119599653d4eSeschrock 		return (0);
1196fa9e4066Sahrens 
1197e14bb325SJeff Bonwick 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1198e14bb325SJeff Bonwick 	    &child, &children);
1199e14bb325SJeff Bonwick 
1200e14bb325SJeff Bonwick 	if (error == ENOENT)
1201e14bb325SJeff Bonwick 		return (0);
1202e14bb325SJeff Bonwick 
1203e14bb325SJeff Bonwick 	if (error) {
120499653d4eSeschrock 		vdev_free(*vdp);
120599653d4eSeschrock 		*vdp = NULL;
1206be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1207fa9e4066Sahrens 	}
1208fa9e4066Sahrens 
1209573ca77eSGeorge Wilson 	for (int c = 0; c < children; c++) {
121099653d4eSeschrock 		vdev_t *vd;
121199653d4eSeschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
121299653d4eSeschrock 		    atype)) != 0) {
121399653d4eSeschrock 			vdev_free(*vdp);
121499653d4eSeschrock 			*vdp = NULL;
121599653d4eSeschrock 			return (error);
1216fa9e4066Sahrens 		}
1217fa9e4066Sahrens 	}
1218fa9e4066Sahrens 
121999653d4eSeschrock 	ASSERT(*vdp != NULL);
122099653d4eSeschrock 
122199653d4eSeschrock 	return (0);
1222fa9e4066Sahrens }
1223fa9e4066Sahrens 
1224fa9e4066Sahrens /*
1225fa9e4066Sahrens  * Opposite of spa_load().
1226fa9e4066Sahrens  */
1227fa9e4066Sahrens static void
1228fa9e4066Sahrens spa_unload(spa_t *spa)
1229fa9e4066Sahrens {
123099653d4eSeschrock 	int i;
123199653d4eSeschrock 
1232e14bb325SJeff Bonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1233e14bb325SJeff Bonwick 
1234ea8dc4b6Seschrock 	/*
1235ea8dc4b6Seschrock 	 * Stop async tasks.
1236ea8dc4b6Seschrock 	 */
1237ea8dc4b6Seschrock 	spa_async_suspend(spa);
1238ea8dc4b6Seschrock 
1239fa9e4066Sahrens 	/*
1240fa9e4066Sahrens 	 * Stop syncing.
1241fa9e4066Sahrens 	 */
1242fa9e4066Sahrens 	if (spa->spa_sync_on) {
1243fa9e4066Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
1244fa9e4066Sahrens 		spa->spa_sync_on = B_FALSE;
1245fa9e4066Sahrens 	}
1246fa9e4066Sahrens 
1247fa9e4066Sahrens 	/*
1248e14bb325SJeff Bonwick 	 * Wait for any outstanding async I/O to complete.
1249fa9e4066Sahrens 	 */
125054d692b7SGeorge Wilson 	if (spa->spa_async_zio_root != NULL) {
12516f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++)
12526f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
12536f834bc1SMatthew Ahrens 		kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
125454d692b7SGeorge Wilson 		spa->spa_async_zio_root = NULL;
125554d692b7SGeorge Wilson 	}
1256fa9e4066Sahrens 
1257cde58dbcSMatthew Ahrens 	bpobj_close(&spa->spa_deferred_bpobj);
1258cde58dbcSMatthew Ahrens 
12590713e232SGeorge Wilson 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12600713e232SGeorge Wilson 
12610713e232SGeorge Wilson 	/*
12620713e232SGeorge Wilson 	 * Close all vdevs.
12630713e232SGeorge Wilson 	 */
12640713e232SGeorge Wilson 	if (spa->spa_root_vdev)
12650713e232SGeorge Wilson 		vdev_free(spa->spa_root_vdev);
12660713e232SGeorge Wilson 	ASSERT(spa->spa_root_vdev == NULL);
12670713e232SGeorge Wilson 
1268fa9e4066Sahrens 	/*
1269fa9e4066Sahrens 	 * Close the dsl pool.
1270fa9e4066Sahrens 	 */
1271fa9e4066Sahrens 	if (spa->spa_dsl_pool) {
1272fa9e4066Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
1273fa9e4066Sahrens 		spa->spa_dsl_pool = NULL;
1274afee20e4SGeorge Wilson 		spa->spa_meta_objset = NULL;
1275fa9e4066Sahrens 	}
1276fa9e4066Sahrens 
1277b24ab676SJeff Bonwick 	ddt_unload(spa);
1278b24ab676SJeff Bonwick 
12798ad4d6ddSJeff Bonwick 
12808ad4d6ddSJeff Bonwick 	/*
12818ad4d6ddSJeff Bonwick 	 * Drop and purge level 2 cache
12828ad4d6ddSJeff Bonwick 	 */
12838ad4d6ddSJeff Bonwick 	spa_l2cache_drop(spa);
12848ad4d6ddSJeff Bonwick 
1285fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1286fa94a07fSbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
1287fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs) {
1288fa94a07fSbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
1289fa94a07fSbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
1290fa94a07fSbrendan 		spa->spa_spares.sav_vdevs = NULL;
129199653d4eSeschrock 	}
1292fa94a07fSbrendan 	if (spa->spa_spares.sav_config) {
1293fa94a07fSbrendan 		nvlist_free(spa->spa_spares.sav_config);
1294fa94a07fSbrendan 		spa->spa_spares.sav_config = NULL;
1295fa94a07fSbrendan 	}
12962ce8af81SEric Schrock 	spa->spa_spares.sav_count = 0;
1297fa94a07fSbrendan 
1298cd0837ccSGeorge Wilson 	for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1299cd0837ccSGeorge Wilson 		vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1300fa94a07fSbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1301cd0837ccSGeorge Wilson 	}
1302fa94a07fSbrendan 	if (spa->spa_l2cache.sav_vdevs) {
1303fa94a07fSbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
1304fa94a07fSbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
1305fa94a07fSbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
1306fa94a07fSbrendan 	}
1307fa94a07fSbrendan 	if (spa->spa_l2cache.sav_config) {
1308fa94a07fSbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
1309fa94a07fSbrendan 		spa->spa_l2cache.sav_config = NULL;
131099653d4eSeschrock 	}
13112ce8af81SEric Schrock 	spa->spa_l2cache.sav_count = 0;
131299653d4eSeschrock 
1313ea8dc4b6Seschrock 	spa->spa_async_suspended = 0;
13148ad4d6ddSJeff Bonwick 
13158704186eSDan McDonald 	if (spa->spa_comment != NULL) {
13168704186eSDan McDonald 		spa_strfree(spa->spa_comment);
13178704186eSDan McDonald 		spa->spa_comment = NULL;
13188704186eSDan McDonald 	}
13198704186eSDan McDonald 
13208ad4d6ddSJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1321fa9e4066Sahrens }
1322fa9e4066Sahrens 
132399653d4eSeschrock /*
132499653d4eSeschrock  * Load (or re-load) the current list of vdevs describing the active spares for
132599653d4eSeschrock  * this pool.  When this is called, we have some form of basic information in
1326fa94a07fSbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
1327fa94a07fSbrendan  * then re-generate a more complete list including status information.
132899653d4eSeschrock  */
132999653d4eSeschrock static void
133099653d4eSeschrock spa_load_spares(spa_t *spa)
133199653d4eSeschrock {
133299653d4eSeschrock 	nvlist_t **spares;
133399653d4eSeschrock 	uint_t nspares;
133499653d4eSeschrock 	int i;
133539c23413Seschrock 	vdev_t *vd, *tvd;
133699653d4eSeschrock 
1337e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1338e14bb325SJeff Bonwick 
133999653d4eSeschrock 	/*
134099653d4eSeschrock 	 * First, close and free any existing spare vdevs.
134199653d4eSeschrock 	 */
1342fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
1343fa94a07fSbrendan 		vd = spa->spa_spares.sav_vdevs[i];
134439c23413Seschrock 
134539c23413Seschrock 		/* Undo the call to spa_activate() below */
1346c5904d13Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1347c5904d13Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
134839c23413Seschrock 			spa_spare_remove(tvd);
134939c23413Seschrock 		vdev_close(vd);
135039c23413Seschrock 		vdev_free(vd);
135199653d4eSeschrock 	}
135239c23413Seschrock 
1353fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs)
1354fa94a07fSbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
1355fa94a07fSbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
135699653d4eSeschrock 
1357fa94a07fSbrendan 	if (spa->spa_spares.sav_config == NULL)
135899653d4eSeschrock 		nspares = 0;
135999653d4eSeschrock 	else
1360fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
136199653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
136299653d4eSeschrock 
1363fa94a07fSbrendan 	spa->spa_spares.sav_count = (int)nspares;
1364fa94a07fSbrendan 	spa->spa_spares.sav_vdevs = NULL;
136599653d4eSeschrock 
136699653d4eSeschrock 	if (nspares == 0)
136799653d4eSeschrock 		return;
136899653d4eSeschrock 
136999653d4eSeschrock 	/*
137099653d4eSeschrock 	 * Construct the array of vdevs, opening them to get status in the
137139c23413Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
137239c23413Seschrock 	 * structures associated with it: one in the list of spares (used only
137339c23413Seschrock 	 * for basic validation purposes) and one in the active vdev
137439c23413Seschrock 	 * configuration (if it's spared in).  During this phase we open and
137539c23413Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
137639c23413Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
137799653d4eSeschrock 	 */
1378fa94a07fSbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1379fa94a07fSbrendan 	    KM_SLEEP);
1380fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
138199653d4eSeschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
138299653d4eSeschrock 		    VDEV_ALLOC_SPARE) == 0);
138399653d4eSeschrock 		ASSERT(vd != NULL);
138499653d4eSeschrock 
1385fa94a07fSbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
138699653d4eSeschrock 
1387c5904d13Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1388c5904d13Seschrock 		    B_FALSE)) != NULL) {
138939c23413Seschrock 			if (!tvd->vdev_isspare)
139039c23413Seschrock 				spa_spare_add(tvd);
139139c23413Seschrock 
139239c23413Seschrock 			/*
139339c23413Seschrock 			 * We only mark the spare active if we were successfully
139439c23413Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
139539c23413Seschrock 			 * with a bad active spare would result in strange
139639c23413Seschrock 			 * behavior, because multiple pool would think the spare
139739c23413Seschrock 			 * is actively in use.
139839c23413Seschrock 			 *
139939c23413Seschrock 			 * There is a vulnerability here to an equally bizarre
140039c23413Seschrock 			 * circumstance, where a dead active spare is later
140139c23413Seschrock 			 * brought back to life (onlined or otherwise).  Given
140239c23413Seschrock 			 * the rarity of this scenario, and the extra complexity
140339c23413Seschrock 			 * it adds, we ignore the possibility.
140439c23413Seschrock 			 */
140539c23413Seschrock 			if (!vdev_is_dead(tvd))
140639c23413Seschrock 				spa_spare_activate(tvd);
140739c23413Seschrock 		}
140839c23413Seschrock 
1409e14bb325SJeff Bonwick 		vd->vdev_top = vd;
14106809eb4eSEric Schrock 		vd->vdev_aux = &spa->spa_spares;
1411e14bb325SJeff Bonwick 
141299653d4eSeschrock 		if (vdev_open(vd) != 0)
141399653d4eSeschrock 			continue;
141499653d4eSeschrock 
1415fa94a07fSbrendan 		if (vdev_validate_aux(vd) == 0)
1416fa94a07fSbrendan 			spa_spare_add(vd);
141799653d4eSeschrock 	}
141899653d4eSeschrock 
141999653d4eSeschrock 	/*
142099653d4eSeschrock 	 * Recompute the stashed list of spares, with status information
142199653d4eSeschrock 	 * this time.
142299653d4eSeschrock 	 */
1423fa94a07fSbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
142499653d4eSeschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
142599653d4eSeschrock 
1426fa94a07fSbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1427fa94a07fSbrendan 	    KM_SLEEP);
1428fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1429fa94a07fSbrendan 		spares[i] = vdev_config_generate(spa,
14303f9d6ad7SLin Ling 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1431fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1432fa94a07fSbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1433fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
143499653d4eSeschrock 		nvlist_free(spares[i]);
1435fa94a07fSbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1436fa94a07fSbrendan }
1437fa94a07fSbrendan 
1438fa94a07fSbrendan /*
1439fa94a07fSbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
1440fa94a07fSbrendan  * this pool.  When this is called, we have some form of basic information in
1441fa94a07fSbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
1442fa94a07fSbrendan  * then re-generate a more complete list including status information.
1443fa94a07fSbrendan  * Devices which are already active have their details maintained, and are
1444fa94a07fSbrendan  * not re-opened.
1445fa94a07fSbrendan  */
1446fa94a07fSbrendan static void
1447fa94a07fSbrendan spa_load_l2cache(spa_t *spa)
1448fa94a07fSbrendan {
1449fa94a07fSbrendan 	nvlist_t **l2cache;
1450fa94a07fSbrendan 	uint_t nl2cache;
1451fa94a07fSbrendan 	int i, j, oldnvdevs;
1452573ca77eSGeorge Wilson 	uint64_t guid;
1453fa94a07fSbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
1454fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
1455fa94a07fSbrendan 
1456e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1457e14bb325SJeff Bonwick 
1458fa94a07fSbrendan 	if (sav->sav_config != NULL) {
1459fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1460fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1461fa94a07fSbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1462fa94a07fSbrendan 	} else {
1463fa94a07fSbrendan 		nl2cache = 0;
1464d5285caeSGeorge Wilson 		newvdevs = NULL;
1465fa94a07fSbrendan 	}
1466fa94a07fSbrendan 
1467fa94a07fSbrendan 	oldvdevs = sav->sav_vdevs;
1468fa94a07fSbrendan 	oldnvdevs = sav->sav_count;
1469fa94a07fSbrendan 	sav->sav_vdevs = NULL;
1470fa94a07fSbrendan 	sav->sav_count = 0;
1471fa94a07fSbrendan 
1472fa94a07fSbrendan 	/*
1473fa94a07fSbrendan 	 * Process new nvlist of vdevs.
1474fa94a07fSbrendan 	 */
1475fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
1476fa94a07fSbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1477fa94a07fSbrendan 		    &guid) == 0);
1478fa94a07fSbrendan 
1479fa94a07fSbrendan 		newvdevs[i] = NULL;
1480fa94a07fSbrendan 		for (j = 0; j < oldnvdevs; j++) {
1481fa94a07fSbrendan 			vd = oldvdevs[j];
1482fa94a07fSbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
1483fa94a07fSbrendan 				/*
1484fa94a07fSbrendan 				 * Retain previous vdev for add/remove ops.
1485fa94a07fSbrendan 				 */
1486fa94a07fSbrendan 				newvdevs[i] = vd;
1487fa94a07fSbrendan 				oldvdevs[j] = NULL;
1488fa94a07fSbrendan 				break;
1489fa94a07fSbrendan 			}
1490fa94a07fSbrendan 		}
1491fa94a07fSbrendan 
1492fa94a07fSbrendan 		if (newvdevs[i] == NULL) {
1493fa94a07fSbrendan 			/*
1494fa94a07fSbrendan 			 * Create new vdev
1495fa94a07fSbrendan 			 */
1496fa94a07fSbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1497fa94a07fSbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
1498fa94a07fSbrendan 			ASSERT(vd != NULL);
1499fa94a07fSbrendan 			newvdevs[i] = vd;
1500fa94a07fSbrendan 
1501fa94a07fSbrendan 			/*
1502fa94a07fSbrendan 			 * Commit this vdev as an l2cache device,
1503fa94a07fSbrendan 			 * even if it fails to open.
1504fa94a07fSbrendan 			 */
1505fa94a07fSbrendan 			spa_l2cache_add(vd);
1506fa94a07fSbrendan 
1507c5904d13Seschrock 			vd->vdev_top = vd;
1508c5904d13Seschrock 			vd->vdev_aux = sav;
1509c5904d13Seschrock 
1510c5904d13Seschrock 			spa_l2cache_activate(vd);
1511c5904d13Seschrock 
1512fa94a07fSbrendan 			if (vdev_open(vd) != 0)
1513fa94a07fSbrendan 				continue;
1514fa94a07fSbrendan 
1515fa94a07fSbrendan 			(void) vdev_validate_aux(vd);
1516fa94a07fSbrendan 
1517573ca77eSGeorge Wilson 			if (!vdev_is_dead(vd))
1518573ca77eSGeorge Wilson 				l2arc_add_vdev(spa, vd);
1519fa94a07fSbrendan 		}
1520fa94a07fSbrendan 	}
1521fa94a07fSbrendan 
1522fa94a07fSbrendan 	/*
1523fa94a07fSbrendan 	 * Purge vdevs that were dropped
1524fa94a07fSbrendan 	 */
1525fa94a07fSbrendan 	for (i = 0; i < oldnvdevs; i++) {
1526fa94a07fSbrendan 		uint64_t pool;
1527fa94a07fSbrendan 
1528fa94a07fSbrendan 		vd = oldvdevs[i];
1529fa94a07fSbrendan 		if (vd != NULL) {
1530cd0837ccSGeorge Wilson 			ASSERT(vd->vdev_isl2cache);
1531cd0837ccSGeorge Wilson 
15328ad4d6ddSJeff Bonwick 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
15338ad4d6ddSJeff Bonwick 			    pool != 0ULL && l2arc_vdev_present(vd))
1534fa94a07fSbrendan 				l2arc_remove_vdev(vd);
1535cd0837ccSGeorge Wilson 			vdev_clear_stats(vd);
1536cd0837ccSGeorge Wilson 			vdev_free(vd);
1537fa94a07fSbrendan 		}
1538fa94a07fSbrendan 	}
1539fa94a07fSbrendan 
1540fa94a07fSbrendan 	if (oldvdevs)
1541fa94a07fSbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1542fa94a07fSbrendan 
1543fa94a07fSbrendan 	if (sav->sav_config == NULL)
1544fa94a07fSbrendan 		goto out;
1545fa94a07fSbrendan 
1546fa94a07fSbrendan 	sav->sav_vdevs = newvdevs;
1547fa94a07fSbrendan 	sav->sav_count = (int)nl2cache;
1548fa94a07fSbrendan 
1549fa94a07fSbrendan 	/*
1550fa94a07fSbrendan 	 * Recompute the stashed list of l2cache devices, with status
1551fa94a07fSbrendan 	 * information this time.
1552fa94a07fSbrendan 	 */
1553fa94a07fSbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1554fa94a07fSbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
1555fa94a07fSbrendan 
1556fa94a07fSbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1557fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
1558fa94a07fSbrendan 		l2cache[i] = vdev_config_generate(spa,
15593f9d6ad7SLin Ling 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1560fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1561fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1562fa94a07fSbrendan out:
1563fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
1564fa94a07fSbrendan 		nvlist_free(l2cache[i]);
1565fa94a07fSbrendan 	if (sav->sav_count)
1566fa94a07fSbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
156799653d4eSeschrock }
156899653d4eSeschrock 
156999653d4eSeschrock static int
157099653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
157199653d4eSeschrock {
157299653d4eSeschrock 	dmu_buf_t *db;
157399653d4eSeschrock 	char *packed = NULL;
157499653d4eSeschrock 	size_t nvsize = 0;
157599653d4eSeschrock 	int error;
157699653d4eSeschrock 	*value = NULL;
157799653d4eSeschrock 
1578a45f1c3cSBrian Behlendorf 	error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1579a45f1c3cSBrian Behlendorf 	if (error != 0)
1580a45f1c3cSBrian Behlendorf 		return (error);
1581a45f1c3cSBrian Behlendorf 
158299653d4eSeschrock 	nvsize = *(uint64_t *)db->db_data;
158399653d4eSeschrock 	dmu_buf_rele(db, FTAG);
158499653d4eSeschrock 
158599653d4eSeschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
15867bfdf011SNeil Perrin 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
15877bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
158899653d4eSeschrock 	if (error == 0)
158999653d4eSeschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
159099653d4eSeschrock 	kmem_free(packed, nvsize);
159199653d4eSeschrock 
159299653d4eSeschrock 	return (error);
159399653d4eSeschrock }
159499653d4eSeschrock 
15953d7072f8Seschrock /*
15963d7072f8Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
15973d7072f8Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
15983d7072f8Seschrock  */
15993d7072f8Seschrock static void
16003d7072f8Seschrock spa_check_removed(vdev_t *vd)
16013d7072f8Seschrock {
1602573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
16033d7072f8Seschrock 		spa_check_removed(vd->vdev_child[c]);
16043d7072f8Seschrock 
1605efb4a871SYuri Pankov 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1606efb4a871SYuri Pankov 	    !vd->vdev_ishole) {
16073d7072f8Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
16083d7072f8Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
16093d7072f8Seschrock 	}
16103d7072f8Seschrock }
16113d7072f8Seschrock 
1612e6ca193dSGeorge Wilson /*
16134b964adaSGeorge Wilson  * Validate the current config against the MOS config
1614e6ca193dSGeorge Wilson  */
16154b964adaSGeorge Wilson static boolean_t
16164b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config)
1617e6ca193dSGeorge Wilson {
16184b964adaSGeorge Wilson 	vdev_t *mrvd, *rvd = spa->spa_root_vdev;
16194b964adaSGeorge Wilson 	nvlist_t *nv;
16204b964adaSGeorge Wilson 
16214b964adaSGeorge Wilson 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
16224b964adaSGeorge Wilson 
16234b964adaSGeorge Wilson 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
16244b964adaSGeorge Wilson 	VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
16254b964adaSGeorge Wilson 
16264b964adaSGeorge Wilson 	ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1627e6ca193dSGeorge Wilson 
162888ecc943SGeorge Wilson 	/*
16294b964adaSGeorge Wilson 	 * If we're doing a normal import, then build up any additional
16304b964adaSGeorge Wilson 	 * diagnostic information about missing devices in this config.
16314b964adaSGeorge Wilson 	 * We'll pass this up to the user for further processing.
163288ecc943SGeorge Wilson 	 */
16334b964adaSGeorge Wilson 	if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
16344b964adaSGeorge Wilson 		nvlist_t **child, *nv;
16354b964adaSGeorge Wilson 		uint64_t idx = 0;
16364b964adaSGeorge Wilson 
16374b964adaSGeorge Wilson 		child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
16384b964adaSGeorge Wilson 		    KM_SLEEP);
16394b964adaSGeorge Wilson 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1640e6ca193dSGeorge Wilson 
16414b964adaSGeorge Wilson 		for (int c = 0; c < rvd->vdev_children; c++) {
16424b964adaSGeorge Wilson 			vdev_t *tvd = rvd->vdev_child[c];
16434b964adaSGeorge Wilson 			vdev_t *mtvd  = mrvd->vdev_child[c];
16444b964adaSGeorge Wilson 
16454b964adaSGeorge Wilson 			if (tvd->vdev_ops == &vdev_missing_ops &&
16464b964adaSGeorge Wilson 			    mtvd->vdev_ops != &vdev_missing_ops &&
16474b964adaSGeorge Wilson 			    mtvd->vdev_islog)
16484b964adaSGeorge Wilson 				child[idx++] = vdev_config_generate(spa, mtvd,
16494b964adaSGeorge Wilson 				    B_FALSE, 0);
16504b964adaSGeorge Wilson 		}
16514b964adaSGeorge Wilson 
16524b964adaSGeorge Wilson 		if (idx) {
16534b964adaSGeorge Wilson 			VERIFY(nvlist_add_nvlist_array(nv,
16544b964adaSGeorge Wilson 			    ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
16554b964adaSGeorge Wilson 			VERIFY(nvlist_add_nvlist(spa->spa_load_info,
16564b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
16574b964adaSGeorge Wilson 
16584b964adaSGeorge Wilson 			for (int i = 0; i < idx; i++)
16594b964adaSGeorge Wilson 				nvlist_free(child[i]);
16604b964adaSGeorge Wilson 		}
16614b964adaSGeorge Wilson 		nvlist_free(nv);
16624b964adaSGeorge Wilson 		kmem_free(child, rvd->vdev_children * sizeof (char **));
16634b964adaSGeorge Wilson 	}
16644b964adaSGeorge Wilson 
16654b964adaSGeorge Wilson 	/*
16664b964adaSGeorge Wilson 	 * Compare the root vdev tree with the information we have
16674b964adaSGeorge Wilson 	 * from the MOS config (mrvd). Check each top-level vdev
16684b964adaSGeorge Wilson 	 * with the corresponding MOS config top-level (mtvd).
16694b964adaSGeorge Wilson 	 */
167088ecc943SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
16714b964adaSGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
16724b964adaSGeorge Wilson 		vdev_t *mtvd  = mrvd->vdev_child[c];
16734b964adaSGeorge Wilson 
16744b964adaSGeorge Wilson 		/*
16754b964adaSGeorge Wilson 		 * Resolve any "missing" vdevs in the current configuration.
16764b964adaSGeorge Wilson 		 * If we find that the MOS config has more accurate information
16774b964adaSGeorge Wilson 		 * about the top-level vdev then use that vdev instead.
16784b964adaSGeorge Wilson 		 */
16794b964adaSGeorge Wilson 		if (tvd->vdev_ops == &vdev_missing_ops &&
16804b964adaSGeorge Wilson 		    mtvd->vdev_ops != &vdev_missing_ops) {
16814b964adaSGeorge Wilson 
16824b964adaSGeorge Wilson 			if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
16834b964adaSGeorge Wilson 				continue;
16844b964adaSGeorge Wilson 
16854b964adaSGeorge Wilson 			/*
16864b964adaSGeorge Wilson 			 * Device specific actions.
16874b964adaSGeorge Wilson 			 */
16884b964adaSGeorge Wilson 			if (mtvd->vdev_islog) {
16894b964adaSGeorge Wilson 				spa_set_log_state(spa, SPA_LOG_CLEAR);
16904b964adaSGeorge Wilson 			} else {
16914b964adaSGeorge Wilson 				/*
16924b964adaSGeorge Wilson 				 * XXX - once we have 'readonly' pool
16934b964adaSGeorge Wilson 				 * support we should be able to handle
16944b964adaSGeorge Wilson 				 * missing data devices by transitioning
16954b964adaSGeorge Wilson 				 * the pool to readonly.
16964b964adaSGeorge Wilson 				 */
16974b964adaSGeorge Wilson 				continue;
16984b964adaSGeorge Wilson 			}
16994b964adaSGeorge Wilson 
17004b964adaSGeorge Wilson 			/*
17014b964adaSGeorge Wilson 			 * Swap the missing vdev with the data we were
17024b964adaSGeorge Wilson 			 * able to obtain from the MOS config.
17034b964adaSGeorge Wilson 			 */
17044b964adaSGeorge Wilson 			vdev_remove_child(rvd, tvd);
17054b964adaSGeorge Wilson 			vdev_remove_child(mrvd, mtvd);
17064b964adaSGeorge Wilson 
17074b964adaSGeorge Wilson 			vdev_add_child(rvd, mtvd);
17084b964adaSGeorge Wilson 			vdev_add_child(mrvd, tvd);
17094b964adaSGeorge Wilson 
17104b964adaSGeorge Wilson 			spa_config_exit(spa, SCL_ALL, FTAG);
17114b964adaSGeorge Wilson 			vdev_load(mtvd);
17124b964adaSGeorge Wilson 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17134b964adaSGeorge Wilson 
17144b964adaSGeorge Wilson 			vdev_reopen(rvd);
17154b964adaSGeorge Wilson 		} else if (mtvd->vdev_islog) {
17164b964adaSGeorge Wilson 			/*
17174b964adaSGeorge Wilson 			 * Load the slog device's state from the MOS config
17184b964adaSGeorge Wilson 			 * since it's possible that the label does not
17194b964adaSGeorge Wilson 			 * contain the most up-to-date information.
17204b964adaSGeorge Wilson 			 */
17214b964adaSGeorge Wilson 			vdev_load_log_state(tvd, mtvd);
17224b964adaSGeorge Wilson 			vdev_reopen(tvd);
17234b964adaSGeorge Wilson 		}
1724e6ca193dSGeorge Wilson 	}
17254b964adaSGeorge Wilson 	vdev_free(mrvd);
172688ecc943SGeorge Wilson 	spa_config_exit(spa, SCL_ALL, FTAG);
17274b964adaSGeorge Wilson 
17284b964adaSGeorge Wilson 	/*
17294b964adaSGeorge Wilson 	 * Ensure we were able to validate the config.
17304b964adaSGeorge Wilson 	 */
17314b964adaSGeorge Wilson 	return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1732e6ca193dSGeorge Wilson }
1733e6ca193dSGeorge Wilson 
1734b87f3af3Sperrin /*
1735b87f3af3Sperrin  * Check for missing log devices
1736b87f3af3Sperrin  */
17373b2aab18SMatthew Ahrens static boolean_t
1738b87f3af3Sperrin spa_check_logs(spa_t *spa)
1739b87f3af3Sperrin {
17403b2aab18SMatthew Ahrens 	boolean_t rv = B_FALSE;
174112380e1eSArne Jansen 	dsl_pool_t *dp = spa_get_dsl(spa);
17423b2aab18SMatthew Ahrens 
1743b87f3af3Sperrin 	switch (spa->spa_log_state) {
1744b87f3af3Sperrin 	case SPA_LOG_MISSING:
1745b87f3af3Sperrin 		/* need to recheck in case slog has been restored */
1746b87f3af3Sperrin 	case SPA_LOG_UNKNOWN:
174712380e1eSArne Jansen 		rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
174812380e1eSArne Jansen 		    zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
17493b2aab18SMatthew Ahrens 		if (rv)
17501195e687SMark J Musante 			spa_set_log_state(spa, SPA_LOG_MISSING);
1751b87f3af3Sperrin 		break;
1752b87f3af3Sperrin 	}
17533b2aab18SMatthew Ahrens 	return (rv);
1754b87f3af3Sperrin }
1755b87f3af3Sperrin 
17561195e687SMark J Musante static boolean_t
17571195e687SMark J Musante spa_passivate_log(spa_t *spa)
17581195e687SMark J Musante {
17591195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
17601195e687SMark J Musante 	boolean_t slog_found = B_FALSE;
17611195e687SMark J Musante 
17621195e687SMark J Musante 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
17631195e687SMark J Musante 
17641195e687SMark J Musante 	if (!spa_has_slogs(spa))
17651195e687SMark J Musante 		return (B_FALSE);
17661195e687SMark J Musante 
17671195e687SMark J Musante 	for (int c = 0; c < rvd->vdev_children; c++) {
17681195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
17691195e687SMark J Musante 		metaslab_group_t *mg = tvd->vdev_mg;
17701195e687SMark J Musante 
17711195e687SMark J Musante 		if (tvd->vdev_islog) {
17721195e687SMark J Musante 			metaslab_group_passivate(mg);
17731195e687SMark J Musante 			slog_found = B_TRUE;
17741195e687SMark J Musante 		}
17751195e687SMark J Musante 	}
17761195e687SMark J Musante 
17771195e687SMark J Musante 	return (slog_found);
17781195e687SMark J Musante }
17791195e687SMark J Musante 
17801195e687SMark J Musante static void
17811195e687SMark J Musante spa_activate_log(spa_t *spa)
17821195e687SMark J Musante {
17831195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
17841195e687SMark J Musante 
17851195e687SMark J Musante 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
17861195e687SMark J Musante 
17871195e687SMark J Musante 	for (int c = 0; c < rvd->vdev_children; c++) {
17881195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
17891195e687SMark J Musante 		metaslab_group_t *mg = tvd->vdev_mg;
17901195e687SMark J Musante 
17911195e687SMark J Musante 		if (tvd->vdev_islog)
17921195e687SMark J Musante 			metaslab_group_activate(mg);
17931195e687SMark J Musante 	}
17941195e687SMark J Musante }
17951195e687SMark J Musante 
17961195e687SMark J Musante int
17971195e687SMark J Musante spa_offline_log(spa_t *spa)
17981195e687SMark J Musante {
17993b2aab18SMatthew Ahrens 	int error;
18001195e687SMark J Musante 
18013b2aab18SMatthew Ahrens 	error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
18023b2aab18SMatthew Ahrens 	    NULL, DS_FIND_CHILDREN);
18033b2aab18SMatthew Ahrens 	if (error == 0) {
18041195e687SMark J Musante 		/*
18051195e687SMark J Musante 		 * We successfully offlined the log device, sync out the
18061195e687SMark J Musante 		 * current txg so that the "stubby" block can be removed
18071195e687SMark J Musante 		 * by zil_sync().
18081195e687SMark J Musante 		 */
18091195e687SMark J Musante 		txg_wait_synced(spa->spa_dsl_pool, 0);
18101195e687SMark J Musante 	}
18111195e687SMark J Musante 	return (error);
18121195e687SMark J Musante }
18131195e687SMark J Musante 
1814b693757aSEric Schrock static void
1815b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav)
1816b693757aSEric Schrock {
1817b24ab676SJeff Bonwick 	for (int i = 0; i < sav->sav_count; i++)
1818b693757aSEric Schrock 		spa_check_removed(sav->sav_vdevs[i]);
1819b693757aSEric Schrock }
1820b693757aSEric Schrock 
1821b24ab676SJeff Bonwick void
1822b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio)
1823b24ab676SJeff Bonwick {
1824b24ab676SJeff Bonwick 	spa_t *spa = zio->io_spa;
1825b24ab676SJeff Bonwick 
1826b24ab676SJeff Bonwick 	if (zio->io_error)
1827b24ab676SJeff Bonwick 		return;
1828b24ab676SJeff Bonwick 
1829b24ab676SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
1830b24ab676SJeff Bonwick 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1831b24ab676SJeff Bonwick 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1832b24ab676SJeff Bonwick 	mutex_exit(&spa->spa_props_lock);
1833b24ab676SJeff Bonwick }
1834b24ab676SJeff Bonwick 
1835468c413aSTim Haley typedef struct spa_load_error {
1836c8ee1847SVictor Latushkin 	uint64_t	sle_meta_count;
1837468c413aSTim Haley 	uint64_t	sle_data_count;
1838468c413aSTim Haley } spa_load_error_t;
1839468c413aSTim Haley 
1840468c413aSTim Haley static void
1841468c413aSTim Haley spa_load_verify_done(zio_t *zio)
1842468c413aSTim Haley {
1843468c413aSTim Haley 	blkptr_t *bp = zio->io_bp;
1844468c413aSTim Haley 	spa_load_error_t *sle = zio->io_private;
1845468c413aSTim Haley 	dmu_object_type_t type = BP_GET_TYPE(bp);
1846468c413aSTim Haley 	int error = zio->io_error;
1847e42d2059SMatthew Ahrens 	spa_t *spa = zio->io_spa;
1848468c413aSTim Haley 
1849468c413aSTim Haley 	if (error) {
1850ad135b5dSChristopher Siden 		if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1851468c413aSTim Haley 		    type != DMU_OT_INTENT_LOG)
18521a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(&sle->sle_meta_count);
1853468c413aSTim Haley 		else
18541a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(&sle->sle_data_count);
1855468c413aSTim Haley 	}
1856468c413aSTim Haley 	zio_data_buf_free(zio->io_data, zio->io_size);
1857e42d2059SMatthew Ahrens 
1858e42d2059SMatthew Ahrens 	mutex_enter(&spa->spa_scrub_lock);
1859e42d2059SMatthew Ahrens 	spa->spa_scrub_inflight--;
1860e42d2059SMatthew Ahrens 	cv_broadcast(&spa->spa_scrub_io_cv);
1861e42d2059SMatthew Ahrens 	mutex_exit(&spa->spa_scrub_lock);
1862468c413aSTim Haley }
1863468c413aSTim Haley 
1864e42d2059SMatthew Ahrens /*
1865e42d2059SMatthew Ahrens  * Maximum number of concurrent scrub i/os to create while verifying
1866e42d2059SMatthew Ahrens  * a pool while importing it.
1867e42d2059SMatthew Ahrens  */
1868e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000;
1869e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE;
1870e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE;
1871e42d2059SMatthew Ahrens 
1872468c413aSTim Haley /*ARGSUSED*/
1873468c413aSTim Haley static int
1874b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
18757802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1876468c413aSTim Haley {
1877a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1878e42d2059SMatthew Ahrens 		return (0);
1879e42d2059SMatthew Ahrens 	/*
1880e42d2059SMatthew Ahrens 	 * Note: normally this routine will not be called if
1881e42d2059SMatthew Ahrens 	 * spa_load_verify_metadata is not set.  However, it may be useful
1882e42d2059SMatthew Ahrens 	 * to manually set the flag after the traversal has begun.
1883e42d2059SMatthew Ahrens 	 */
1884e42d2059SMatthew Ahrens 	if (!spa_load_verify_metadata)
1885e42d2059SMatthew Ahrens 		return (0);
1886e42d2059SMatthew Ahrens 	if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1887e42d2059SMatthew Ahrens 		return (0);
1888468c413aSTim Haley 
1889e42d2059SMatthew Ahrens 	zio_t *rio = arg;
1890e42d2059SMatthew Ahrens 	size_t size = BP_GET_PSIZE(bp);
1891e42d2059SMatthew Ahrens 	void *data = zio_data_buf_alloc(size);
1892e42d2059SMatthew Ahrens 
1893e42d2059SMatthew Ahrens 	mutex_enter(&spa->spa_scrub_lock);
1894e42d2059SMatthew Ahrens 	while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1895e42d2059SMatthew Ahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1896e42d2059SMatthew Ahrens 	spa->spa_scrub_inflight++;
1897e42d2059SMatthew Ahrens 	mutex_exit(&spa->spa_scrub_lock);
1898e42d2059SMatthew Ahrens 
1899e42d2059SMatthew Ahrens 	zio_nowait(zio_read(rio, spa, bp, data, size,
1900e42d2059SMatthew Ahrens 	    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1901e42d2059SMatthew Ahrens 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1902e42d2059SMatthew Ahrens 	    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1903468c413aSTim Haley 	return (0);
1904468c413aSTim Haley }
1905468c413aSTim Haley 
1906468c413aSTim Haley static int
1907468c413aSTim Haley spa_load_verify(spa_t *spa)
1908468c413aSTim Haley {
1909468c413aSTim Haley 	zio_t *rio;
1910468c413aSTim Haley 	spa_load_error_t sle = { 0 };
1911468c413aSTim Haley 	zpool_rewind_policy_t policy;
1912468c413aSTim Haley 	boolean_t verify_ok = B_FALSE;
1913e42d2059SMatthew Ahrens 	int error = 0;
1914468c413aSTim Haley 
1915c8ee1847SVictor Latushkin 	zpool_get_rewind_policy(spa->spa_config, &policy);
1916c8ee1847SVictor Latushkin 
1917c8ee1847SVictor Latushkin 	if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1918c8ee1847SVictor Latushkin 		return (0);
1919c8ee1847SVictor Latushkin 
1920468c413aSTim Haley 	rio = zio_root(spa, NULL, &sle,
1921468c413aSTim Haley 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1922468c413aSTim Haley 
1923e42d2059SMatthew Ahrens 	if (spa_load_verify_metadata) {
1924e42d2059SMatthew Ahrens 		error = traverse_pool(spa, spa->spa_verify_min_txg,
1925e42d2059SMatthew Ahrens 		    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1926e42d2059SMatthew Ahrens 		    spa_load_verify_cb, rio);
1927e42d2059SMatthew Ahrens 	}
1928468c413aSTim Haley 
1929468c413aSTim Haley 	(void) zio_wait(rio);
1930468c413aSTim Haley 
1931c8ee1847SVictor Latushkin 	spa->spa_load_meta_errors = sle.sle_meta_count;
1932468c413aSTim Haley 	spa->spa_load_data_errors = sle.sle_data_count;
1933468c413aSTim Haley 
1934c8ee1847SVictor Latushkin 	if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1935468c413aSTim Haley 	    sle.sle_data_count <= policy.zrp_maxdata) {
19364b964adaSGeorge Wilson 		int64_t loss = 0;
19374b964adaSGeorge Wilson 
1938468c413aSTim Haley 		verify_ok = B_TRUE;
1939468c413aSTim Haley 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1940468c413aSTim Haley 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
19414b964adaSGeorge Wilson 
19424b964adaSGeorge Wilson 		loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
19434b964adaSGeorge Wilson 		VERIFY(nvlist_add_uint64(spa->spa_load_info,
19444b964adaSGeorge Wilson 		    ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
19454b964adaSGeorge Wilson 		VERIFY(nvlist_add_int64(spa->spa_load_info,
19464b964adaSGeorge Wilson 		    ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
19474b964adaSGeorge Wilson 		VERIFY(nvlist_add_uint64(spa->spa_load_info,
19484b964adaSGeorge Wilson 		    ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1949a33cae98STim Haley 	} else {
1950a33cae98STim Haley 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1951468c413aSTim Haley 	}
1952468c413aSTim Haley 
1953468c413aSTim Haley 	if (error) {
1954468c413aSTim Haley 		if (error != ENXIO && error != EIO)
1955be6fd75aSMatthew Ahrens 			error = SET_ERROR(EIO);
1956468c413aSTim Haley 		return (error);
1957468c413aSTim Haley 	}
1958468c413aSTim Haley 
1959468c413aSTim Haley 	return (verify_ok ? 0 : EIO);
1960468c413aSTim Haley }
1961468c413aSTim Haley 
19621195e687SMark J Musante /*
19631195e687SMark J Musante  * Find a value in the pool props object.
19641195e687SMark J Musante  */
19651195e687SMark J Musante static void
19661195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
19671195e687SMark J Musante {
19681195e687SMark J Musante 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
19691195e687SMark J Musante 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
19701195e687SMark J Musante }
19711195e687SMark J Musante 
19721195e687SMark J Musante /*
19731195e687SMark J Musante  * Find a value in the pool directory object.
19741195e687SMark J Musante  */
19751195e687SMark J Musante static int
19761195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
19771195e687SMark J Musante {
19781195e687SMark J Musante 	return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
19791195e687SMark J Musante 	    name, sizeof (uint64_t), 1, val));
19801195e687SMark J Musante }
19811195e687SMark J Musante 
19821195e687SMark J Musante static int
19831195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
19841195e687SMark J Musante {
19851195e687SMark J Musante 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
19861195e687SMark J Musante 	return (err);
19871195e687SMark J Musante }
19881195e687SMark J Musante 
19891195e687SMark J Musante /*
19901195e687SMark J Musante  * Fix up config after a partly-completed split.  This is done with the
19911195e687SMark J Musante  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
19921195e687SMark J Musante  * pool have that entry in their config, but only the splitting one contains
19931195e687SMark J Musante  * a list of all the guids of the vdevs that are being split off.
19941195e687SMark J Musante  *
19951195e687SMark J Musante  * This function determines what to do with that list: either rejoin
19961195e687SMark J Musante  * all the disks to the pool, or complete the splitting process.  To attempt
19971195e687SMark J Musante  * the rejoin, each disk that is offlined is marked online again, and
19981195e687SMark J Musante  * we do a reopen() call.  If the vdev label for every disk that was
19991195e687SMark J Musante  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
20001195e687SMark J Musante  * then we call vdev_split() on each disk, and complete the split.
20011195e687SMark J Musante  *
2002d41c4376SMark J Musante  * Otherwise we leave the config alone, with all the vdevs in place in
2003d41c4376SMark J Musante  * the original pool.
20041195e687SMark J Musante  */
20051195e687SMark J Musante static void
20061195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config)
20071195e687SMark J Musante {
20081195e687SMark J Musante 	uint_t extracted;
20091195e687SMark J Musante 	uint64_t *glist;
20101195e687SMark J Musante 	uint_t i, gcount;
20111195e687SMark J Musante 	nvlist_t *nvl;
20121195e687SMark J Musante 	vdev_t **vd;
20131195e687SMark J Musante 	boolean_t attempt_reopen;
20141195e687SMark J Musante 
20151195e687SMark J Musante 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
20161195e687SMark J Musante 		return;
20171195e687SMark J Musante 
20181195e687SMark J Musante 	/* check that the config is complete */
20191195e687SMark J Musante 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
20201195e687SMark J Musante 	    &glist, &gcount) != 0)
20211195e687SMark J Musante 		return;
20221195e687SMark J Musante 
20231195e687SMark J Musante 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
20241195e687SMark J Musante 
20251195e687SMark J Musante 	/* attempt to online all the vdevs & validate */
20261195e687SMark J Musante 	attempt_reopen = B_TRUE;
20271195e687SMark J Musante 	for (i = 0; i < gcount; i++) {
20281195e687SMark J Musante 		if (glist[i] == 0)	/* vdev is hole */
20291195e687SMark J Musante 			continue;
20301195e687SMark J Musante 
20311195e687SMark J Musante 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
20321195e687SMark J Musante 		if (vd[i] == NULL) {
20331195e687SMark J Musante 			/*
20341195e687SMark J Musante 			 * Don't bother attempting to reopen the disks;
20351195e687SMark J Musante 			 * just do the split.
20361195e687SMark J Musante 			 */
20371195e687SMark J Musante 			attempt_reopen = B_FALSE;
20381195e687SMark J Musante 		} else {
20391195e687SMark J Musante 			/* attempt to re-online it */
20401195e687SMark J Musante 			vd[i]->vdev_offline = B_FALSE;
20411195e687SMark J Musante 		}
20421195e687SMark J Musante 	}
20431195e687SMark J Musante 
20441195e687SMark J Musante 	if (attempt_reopen) {
20451195e687SMark J Musante 		vdev_reopen(spa->spa_root_vdev);
20461195e687SMark J Musante 
20471195e687SMark J Musante 		/* check each device to see what state it's in */
20481195e687SMark J Musante 		for (extracted = 0, i = 0; i < gcount; i++) {
20491195e687SMark J Musante 			if (vd[i] != NULL &&
20501195e687SMark J Musante 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
20511195e687SMark J Musante 				break;
20521195e687SMark J Musante 			++extracted;
20531195e687SMark J Musante 		}
20541195e687SMark J Musante 	}
20551195e687SMark J Musante 
20561195e687SMark J Musante 	/*
20571195e687SMark J Musante 	 * If every disk has been moved to the new pool, or if we never
20581195e687SMark J Musante 	 * even attempted to look at them, then we split them off for
20591195e687SMark J Musante 	 * good.
20601195e687SMark J Musante 	 */
20611195e687SMark J Musante 	if (!attempt_reopen || gcount == extracted) {
20621195e687SMark J Musante 		for (i = 0; i < gcount; i++)
20631195e687SMark J Musante 			if (vd[i] != NULL)
20641195e687SMark J Musante 				vdev_split(vd[i]);
20651195e687SMark J Musante 		vdev_reopen(spa->spa_root_vdev);
20661195e687SMark J Musante 	}
20671195e687SMark J Musante 
20681195e687SMark J Musante 	kmem_free(vd, gcount * sizeof (vdev_t *));
20691195e687SMark J Musante }
20701195e687SMark J Musante 
20711195e687SMark J Musante static int
20721195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
20731195e687SMark J Musante     boolean_t mosconfig)
20741195e687SMark J Musante {
20751195e687SMark J Musante 	nvlist_t *config = spa->spa_config;
20761195e687SMark J Musante 	char *ereport = FM_EREPORT_ZFS_POOL;
20778704186eSDan McDonald 	char *comment;
20781195e687SMark J Musante 	int error;
20791195e687SMark J Musante 	uint64_t pool_guid;
20801195e687SMark J Musante 	nvlist_t *nvl;
20811195e687SMark J Musante 
20821195e687SMark J Musante 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2083be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
20841195e687SMark J Musante 
20858704186eSDan McDonald 	ASSERT(spa->spa_comment == NULL);
20868704186eSDan McDonald 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
20878704186eSDan McDonald 		spa->spa_comment = spa_strdup(comment);
20888704186eSDan McDonald 
20891195e687SMark J Musante 	/*
20901195e687SMark J Musante 	 * Versioning wasn't explicitly added to the label until later, so if
20911195e687SMark J Musante 	 * it's not present treat it as the initial version.
20921195e687SMark J Musante 	 */
20931195e687SMark J Musante 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
20941195e687SMark J Musante 	    &spa->spa_ubsync.ub_version) != 0)
20951195e687SMark J Musante 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
20961195e687SMark J Musante 
20971195e687SMark J Musante 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
20981195e687SMark J Musante 	    &spa->spa_config_txg);
20991195e687SMark J Musante 
21001195e687SMark J Musante 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
21011195e687SMark J Musante 	    spa_guid_exists(pool_guid, 0)) {
2102be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
21031195e687SMark J Musante 	} else {
2104e9103aaeSGarrett D'Amore 		spa->spa_config_guid = pool_guid;
21051195e687SMark J Musante 
21061195e687SMark J Musante 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
21071195e687SMark J Musante 		    &nvl) == 0) {
21081195e687SMark J Musante 			VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
21091195e687SMark J Musante 			    KM_SLEEP) == 0);
21101195e687SMark J Musante 		}
21111195e687SMark J Musante 
2112ad135b5dSChristopher Siden 		nvlist_free(spa->spa_load_info);
2113ad135b5dSChristopher Siden 		spa->spa_load_info = fnvlist_alloc();
2114ad135b5dSChristopher Siden 
211511027bc7STim Haley 		gethrestime(&spa->spa_loaded_ts);
21161195e687SMark J Musante 		error = spa_load_impl(spa, pool_guid, config, state, type,
21171195e687SMark J Musante 		    mosconfig, &ereport);
21181195e687SMark J Musante 	}
21191195e687SMark J Musante 
2120bc9014e6SJustin Gibbs 	/*
2121bc9014e6SJustin Gibbs 	 * Don't count references from objsets that are already closed
2122bc9014e6SJustin Gibbs 	 * and are making their way through the eviction process.
2123bc9014e6SJustin Gibbs 	 */
2124bc9014e6SJustin Gibbs 	spa_evicting_os_wait(spa);
21251195e687SMark J Musante 	spa->spa_minref = refcount_count(&spa->spa_refcount);
212611027bc7STim Haley 	if (error) {
212711027bc7STim Haley 		if (error != EEXIST) {
212811027bc7STim Haley 			spa->spa_loaded_ts.tv_sec = 0;
212911027bc7STim Haley 			spa->spa_loaded_ts.tv_nsec = 0;
213011027bc7STim Haley 		}
213111027bc7STim Haley 		if (error != EBADF) {
213211027bc7STim Haley 			zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
213311027bc7STim Haley 		}
213411027bc7STim Haley 	}
21351195e687SMark J Musante 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
21361195e687SMark J Musante 	spa->spa_ena = 0;
21371195e687SMark J Musante 
21381195e687SMark J Musante 	return (error);
21391195e687SMark J Musante }
21401195e687SMark J Musante 
2141fa9e4066Sahrens /*
2142fa9e4066Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
2143ea8dc4b6Seschrock  * source of configuration information.
2144fa9e4066Sahrens  */
2145fa9e4066Sahrens static int
21461195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
21471195e687SMark J Musante     spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
21481195e687SMark J Musante     char **ereport)
2149fa9e4066Sahrens {
2150fa9e4066Sahrens 	int error = 0;
2151871a9500SMark J Musante 	nvlist_t *nvroot = NULL;
2152ad135b5dSChristopher Siden 	nvlist_t *label;
2153fa9e4066Sahrens 	vdev_t *rvd;
2154fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
21554b964adaSGeorge Wilson 	uint64_t children, config_cache_txg = spa->spa_config_txg;
21568ad4d6ddSJeff Bonwick 	int orig_mode = spa->spa_mode;
21571195e687SMark J Musante 	int parse;
2158cde58dbcSMatthew Ahrens 	uint64_t obj;
2159ad135b5dSChristopher Siden 	boolean_t missing_feat_write = B_FALSE;
2160fa9e4066Sahrens 
21618ad4d6ddSJeff Bonwick 	/*
21628ad4d6ddSJeff Bonwick 	 * If this is an untrusted config, access the pool in read-only mode.
21638ad4d6ddSJeff Bonwick 	 * This prevents things like resilvering recently removed devices.
21648ad4d6ddSJeff Bonwick 	 */
21658ad4d6ddSJeff Bonwick 	if (!mosconfig)
21668ad4d6ddSJeff Bonwick 		spa->spa_mode = FREAD;
21678ad4d6ddSJeff Bonwick 
2168e14bb325SJeff Bonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2169e14bb325SJeff Bonwick 
2170ea8dc4b6Seschrock 	spa->spa_load_state = state;
21710373e76bSbonwick 
21721195e687SMark J Musante 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2173be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2174fa9e4066Sahrens 
21751195e687SMark J Musante 	parse = (type == SPA_IMPORT_EXISTING ?
21761195e687SMark J Musante 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2177b5989ec7Seschrock 
217854d692b7SGeorge Wilson 	/*
217954d692b7SGeorge Wilson 	 * Create "The Godfather" zio to hold all async IOs
218054d692b7SGeorge Wilson 	 */
21816f834bc1SMatthew Ahrens 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
21826f834bc1SMatthew Ahrens 	    KM_SLEEP);
21836f834bc1SMatthew Ahrens 	for (int i = 0; i < max_ncpus; i++) {
21846f834bc1SMatthew Ahrens 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
21856f834bc1SMatthew Ahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
21866f834bc1SMatthew Ahrens 		    ZIO_FLAG_GODFATHER);
21876f834bc1SMatthew Ahrens 	}
218854d692b7SGeorge Wilson 
2189fa9e4066Sahrens 	/*
219099653d4eSeschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
219199653d4eSeschrock 	 * value that will be returned by spa_version() since parsing the
219299653d4eSeschrock 	 * configuration requires knowing the version number.
2193fa9e4066Sahrens 	 */
2194e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
21951195e687SMark J Musante 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2196e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
2197fa9e4066Sahrens 
219899653d4eSeschrock 	if (error != 0)
21991195e687SMark J Musante 		return (error);
2200fa9e4066Sahrens 
22010e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
220281cd5c55SMatthew Ahrens 	ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
220381cd5c55SMatthew Ahrens 	ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
22041195e687SMark J Musante 
22051195e687SMark J Musante 	if (type != SPA_IMPORT_ASSEMBLE) {
22061195e687SMark J Musante 		ASSERT(spa_guid(spa) == pool_guid);
22071195e687SMark J Musante 	}
2208fa9e4066Sahrens 
2209fa9e4066Sahrens 	/*
2210fa9e4066Sahrens 	 * Try to open all vdevs, loading each label in the process.
2211fa9e4066Sahrens 	 */
2212e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22130bf246f5Smc 	error = vdev_open(rvd);
2214e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
22150bf246f5Smc 	if (error != 0)
22161195e687SMark J Musante 		return (error);
2217fa9e4066Sahrens 
2218560e6e96Seschrock 	/*
221977e3a39cSMark J Musante 	 * We need to validate the vdev labels against the configuration that
222077e3a39cSMark J Musante 	 * we have in hand, which is dependent on the setting of mosconfig. If
222177e3a39cSMark J Musante 	 * mosconfig is true then we're validating the vdev labels based on
22221195e687SMark J Musante 	 * that config.  Otherwise, we're validating against the cached config
222377e3a39cSMark J Musante 	 * (zpool.cache) that was read when we loaded the zfs module, and then
222477e3a39cSMark J Musante 	 * later we will recursively call spa_load() and validate against
222577e3a39cSMark J Musante 	 * the vdev config.
22261195e687SMark J Musante 	 *
22271195e687SMark J Musante 	 * If we're assembling a new pool that's been split off from an
22281195e687SMark J Musante 	 * existing pool, the labels haven't yet been updated so we skip
22291195e687SMark J Musante 	 * validation for now.
2230560e6e96Seschrock 	 */
22311195e687SMark J Musante 	if (type != SPA_IMPORT_ASSEMBLE) {
22321195e687SMark J Musante 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2233d7f601efSGeorge Wilson 		error = vdev_validate(rvd, mosconfig);
22341195e687SMark J Musante 		spa_config_exit(spa, SCL_ALL, FTAG);
2235560e6e96Seschrock 
22361195e687SMark J Musante 		if (error != 0)
22371195e687SMark J Musante 			return (error);
22381195e687SMark J Musante 
22391195e687SMark J Musante 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2240be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENXIO));
2241560e6e96Seschrock 	}
2242560e6e96Seschrock 
2243fa9e4066Sahrens 	/*
2244fa9e4066Sahrens 	 * Find the best uberblock.
2245fa9e4066Sahrens 	 */
2246ad135b5dSChristopher Siden 	vdev_uberblock_load(rvd, ub, &label);
2247fa9e4066Sahrens 
2248fa9e4066Sahrens 	/*
2249fa9e4066Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
2250fa9e4066Sahrens 	 */
2251ad135b5dSChristopher Siden 	if (ub->ub_txg == 0) {
2252ad135b5dSChristopher Siden 		nvlist_free(label);
22531195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2254ad135b5dSChristopher Siden 	}
2255ea8dc4b6Seschrock 
2256ea8dc4b6Seschrock 	/*
2257ad135b5dSChristopher Siden 	 * If the pool has an unsupported version we can't open it.
2258ea8dc4b6Seschrock 	 */
2259ad135b5dSChristopher Siden 	if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2260ad135b5dSChristopher Siden 		nvlist_free(label);
22611195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2262ad135b5dSChristopher Siden 	}
2263ad135b5dSChristopher Siden 
2264ad135b5dSChristopher Siden 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
2265ad135b5dSChristopher Siden 		nvlist_t *features;
2266ad135b5dSChristopher Siden 
2267ad135b5dSChristopher Siden 		/*
2268ad135b5dSChristopher Siden 		 * If we weren't able to find what's necessary for reading the
2269ad135b5dSChristopher Siden 		 * MOS in the label, return failure.
2270ad135b5dSChristopher Siden 		 */
2271ad135b5dSChristopher Siden 		if (label == NULL || nvlist_lookup_nvlist(label,
2272ad135b5dSChristopher Siden 		    ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2273ad135b5dSChristopher Siden 			nvlist_free(label);
2274ad135b5dSChristopher Siden 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2275ad135b5dSChristopher Siden 			    ENXIO));
2276ad135b5dSChristopher Siden 		}
2277ad135b5dSChristopher Siden 
2278ad135b5dSChristopher Siden 		/*
2279ad135b5dSChristopher Siden 		 * Update our in-core representation with the definitive values
2280ad135b5dSChristopher Siden 		 * from the label.
2281ad135b5dSChristopher Siden 		 */
2282ad135b5dSChristopher Siden 		nvlist_free(spa->spa_label_features);
2283ad135b5dSChristopher Siden 		VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2284ad135b5dSChristopher Siden 	}
2285ad135b5dSChristopher Siden 
2286ad135b5dSChristopher Siden 	nvlist_free(label);
2287ad135b5dSChristopher Siden 
2288ad135b5dSChristopher Siden 	/*
2289ad135b5dSChristopher Siden 	 * Look through entries in the label nvlist's features_for_read. If
2290ad135b5dSChristopher Siden 	 * there is a feature listed there which we don't understand then we
2291ad135b5dSChristopher Siden 	 * cannot open a pool.
2292ad135b5dSChristopher Siden 	 */
2293ad135b5dSChristopher Siden 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
2294ad135b5dSChristopher Siden 		nvlist_t *unsup_feat;
2295ad135b5dSChristopher Siden 
2296ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2297ad135b5dSChristopher Siden 		    0);
2298ad135b5dSChristopher Siden 
2299ad135b5dSChristopher Siden 		for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2300ad135b5dSChristopher Siden 		    NULL); nvp != NULL;
2301ad135b5dSChristopher Siden 		    nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2302ad135b5dSChristopher Siden 			if (!zfeature_is_supported(nvpair_name(nvp))) {
2303ad135b5dSChristopher Siden 				VERIFY(nvlist_add_string(unsup_feat,
2304ad135b5dSChristopher Siden 				    nvpair_name(nvp), "") == 0);
2305ad135b5dSChristopher Siden 			}
2306ad135b5dSChristopher Siden 		}
2307ad135b5dSChristopher Siden 
2308ad135b5dSChristopher Siden 		if (!nvlist_empty(unsup_feat)) {
2309ad135b5dSChristopher Siden 			VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2310ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2311ad135b5dSChristopher Siden 			nvlist_free(unsup_feat);
2312ad135b5dSChristopher Siden 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2313ad135b5dSChristopher Siden 			    ENOTSUP));
2314ad135b5dSChristopher Siden 		}
2315ad135b5dSChristopher Siden 
2316ad135b5dSChristopher Siden 		nvlist_free(unsup_feat);
2317ad135b5dSChristopher Siden 	}
2318fa9e4066Sahrens 
2319fa9e4066Sahrens 	/*
2320fa9e4066Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
23214b964adaSGeorge Wilson 	 * incomplete configuration.  We first check to see if the pool
23224b964adaSGeorge Wilson 	 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
23234b964adaSGeorge Wilson 	 * If it is, defer the vdev_guid_sum check till later so we
23244b964adaSGeorge Wilson 	 * can handle missing vdevs.
2325fa9e4066Sahrens 	 */
23264b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
23274b964adaSGeorge Wilson 	    &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
23281195e687SMark J Musante 	    rvd->vdev_guid_sum != ub->ub_guid_sum)
23291195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
23301195e687SMark J Musante 
23311195e687SMark J Musante 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
23321195e687SMark J Musante 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
23331195e687SMark J Musante 		spa_try_repair(spa, config);
23341195e687SMark J Musante 		spa_config_exit(spa, SCL_ALL, FTAG);
23351195e687SMark J Musante 		nvlist_free(spa->spa_config_splitting);
23361195e687SMark J Musante 		spa->spa_config_splitting = NULL;
2337fa9e4066Sahrens 	}
2338fa9e4066Sahrens 
2339fa9e4066Sahrens 	/*
2340fa9e4066Sahrens 	 * Initialize internal SPA structures.
2341fa9e4066Sahrens 	 */
2342fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
2343fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2344468c413aSTim Haley 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2345c8ee1847SVictor Latushkin 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2346468c413aSTim Haley 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2347468c413aSTim Haley 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2348b24ab676SJeff Bonwick 	spa->spa_claim_max_txg = spa->spa_first_txg;
23493f9d6ad7SLin Ling 	spa->spa_prev_software_version = ub->ub_software_version;
2350b24ab676SJeff Bonwick 
2351ad135b5dSChristopher Siden 	error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
23521195e687SMark J Musante 	if (error)
23531195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2354fa9e4066Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2355fa9e4066Sahrens 
23561195e687SMark J Musante 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
23571195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2358fa9e4066Sahrens 
2359ad135b5dSChristopher Siden 	if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2360ad135b5dSChristopher Siden 		boolean_t missing_feat_read = B_FALSE;
236157221772SChristopher Siden 		nvlist_t *unsup_feat, *enabled_feat;
2362ad135b5dSChristopher Siden 
2363ad135b5dSChristopher Siden 		if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2364ad135b5dSChristopher Siden 		    &spa->spa_feat_for_read_obj) != 0) {
2365ad135b5dSChristopher Siden 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2366ad135b5dSChristopher Siden 		}
2367ad135b5dSChristopher Siden 
2368ad135b5dSChristopher Siden 		if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2369ad135b5dSChristopher Siden 		    &spa->spa_feat_for_write_obj) != 0) {
2370ad135b5dSChristopher Siden 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2371ad135b5dSChristopher Siden 		}
2372ad135b5dSChristopher Siden 
2373ad135b5dSChristopher Siden 		if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2374ad135b5dSChristopher Siden 		    &spa->spa_feat_desc_obj) != 0) {
2375ad135b5dSChristopher Siden 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2376ad135b5dSChristopher Siden 		}
2377ad135b5dSChristopher Siden 
237857221772SChristopher Siden 		enabled_feat = fnvlist_alloc();
237957221772SChristopher Siden 		unsup_feat = fnvlist_alloc();
2380ad135b5dSChristopher Siden 
23812acef22dSMatthew Ahrens 		if (!spa_features_check(spa, B_FALSE,
238257221772SChristopher Siden 		    unsup_feat, enabled_feat))
2383ad135b5dSChristopher Siden 			missing_feat_read = B_TRUE;
2384ad135b5dSChristopher Siden 
2385ad135b5dSChristopher Siden 		if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
23862acef22dSMatthew Ahrens 			if (!spa_features_check(spa, B_TRUE,
238757221772SChristopher Siden 			    unsup_feat, enabled_feat)) {
2388ad135b5dSChristopher Siden 				missing_feat_write = B_TRUE;
238957221772SChristopher Siden 			}
2390ad135b5dSChristopher Siden 		}
2391ad135b5dSChristopher Siden 
239257221772SChristopher Siden 		fnvlist_add_nvlist(spa->spa_load_info,
239357221772SChristopher Siden 		    ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
239457221772SChristopher Siden 
2395ad135b5dSChristopher Siden 		if (!nvlist_empty(unsup_feat)) {
239657221772SChristopher Siden 			fnvlist_add_nvlist(spa->spa_load_info,
239757221772SChristopher Siden 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2398ad135b5dSChristopher Siden 		}
2399ad135b5dSChristopher Siden 
240057221772SChristopher Siden 		fnvlist_free(enabled_feat);
240157221772SChristopher Siden 		fnvlist_free(unsup_feat);
2402ad135b5dSChristopher Siden 
2403ad135b5dSChristopher Siden 		if (!missing_feat_read) {
2404ad135b5dSChristopher Siden 			fnvlist_add_boolean(spa->spa_load_info,
2405ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_CAN_RDONLY);
2406ad135b5dSChristopher Siden 		}
2407ad135b5dSChristopher Siden 
2408ad135b5dSChristopher Siden 		/*
2409ad135b5dSChristopher Siden 		 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2410ad135b5dSChristopher Siden 		 * twofold: to determine whether the pool is available for
2411ad135b5dSChristopher Siden 		 * import in read-write mode and (if it is not) whether the
2412ad135b5dSChristopher Siden 		 * pool is available for import in read-only mode. If the pool
2413ad135b5dSChristopher Siden 		 * is available for import in read-write mode, it is displayed
2414ad135b5dSChristopher Siden 		 * as available in userland; if it is not available for import
2415ad135b5dSChristopher Siden 		 * in read-only mode, it is displayed as unavailable in
2416ad135b5dSChristopher Siden 		 * userland. If the pool is available for import in read-only
2417ad135b5dSChristopher Siden 		 * mode but not read-write mode, it is displayed as unavailable
2418ad135b5dSChristopher Siden 		 * in userland with a special note that the pool is actually
2419ad135b5dSChristopher Siden 		 * available for open in read-only mode.
2420ad135b5dSChristopher Siden 		 *
2421ad135b5dSChristopher Siden 		 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2422ad135b5dSChristopher Siden 		 * missing a feature for write, we must first determine whether
2423ad135b5dSChristopher Siden 		 * the pool can be opened read-only before returning to
2424ad135b5dSChristopher Siden 		 * userland in order to know whether to display the
2425ad135b5dSChristopher Siden 		 * abovementioned note.
2426ad135b5dSChristopher Siden 		 */
2427ad135b5dSChristopher Siden 		if (missing_feat_read || (missing_feat_write &&
2428ad135b5dSChristopher Siden 		    spa_writeable(spa))) {
2429ad135b5dSChristopher Siden 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2430ad135b5dSChristopher Siden 			    ENOTSUP));
2431ad135b5dSChristopher Siden 		}
243243466aaeSMax Grossman 
243343466aaeSMax Grossman 		/*
243443466aaeSMax Grossman 		 * Load refcounts for ZFS features from disk into an in-memory
243543466aaeSMax Grossman 		 * cache during SPA initialization.
243643466aaeSMax Grossman 		 */
243743466aaeSMax Grossman 		for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
243843466aaeSMax Grossman 			uint64_t refcount;
243943466aaeSMax Grossman 
244043466aaeSMax Grossman 			error = feature_get_refcount_from_disk(spa,
244143466aaeSMax Grossman 			    &spa_feature_table[i], &refcount);
244243466aaeSMax Grossman 			if (error == 0) {
244343466aaeSMax Grossman 				spa->spa_feat_refcount_cache[i] = refcount;
244443466aaeSMax Grossman 			} else if (error == ENOTSUP) {
244543466aaeSMax Grossman 				spa->spa_feat_refcount_cache[i] =
244643466aaeSMax Grossman 				    SPA_FEATURE_DISABLED;
244743466aaeSMax Grossman 			} else {
244843466aaeSMax Grossman 				return (spa_vdev_err(rvd,
244943466aaeSMax Grossman 				    VDEV_AUX_CORRUPT_DATA, EIO));
245043466aaeSMax Grossman 			}
245143466aaeSMax Grossman 		}
245243466aaeSMax Grossman 	}
245343466aaeSMax Grossman 
245443466aaeSMax Grossman 	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
245543466aaeSMax Grossman 		if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
24565d7b4d43SMatthew Ahrens 		    &spa->spa_feat_enabled_txg_obj) != 0)
245743466aaeSMax Grossman 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2458ad135b5dSChristopher Siden 	}
2459ad135b5dSChristopher Siden 
2460ad135b5dSChristopher Siden 	spa->spa_is_initializing = B_TRUE;
2461ad135b5dSChristopher Siden 	error = dsl_pool_open(spa->spa_dsl_pool);
2462ad135b5dSChristopher Siden 	spa->spa_is_initializing = B_FALSE;
2463ad135b5dSChristopher Siden 	if (error != 0)
2464ad135b5dSChristopher Siden 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2465ad135b5dSChristopher Siden 
2466fa9e4066Sahrens 	if (!mosconfig) {
246795173954Sek 		uint64_t hostid;
2468871a9500SMark J Musante 		nvlist_t *policy = NULL, *nvconfig;
2469871a9500SMark J Musante 
2470871a9500SMark J Musante 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2471871a9500SMark J Musante 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2472fa9e4066Sahrens 
247388ecc943SGeorge Wilson 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
247477650510SLin Ling 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
247595173954Sek 			char *hostname;
247695173954Sek 			unsigned long myhostid = 0;
247795173954Sek 
247888ecc943SGeorge Wilson 			VERIFY(nvlist_lookup_string(nvconfig,
247995173954Sek 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
248095173954Sek 
24815679c89fSjv #ifdef	_KERNEL
24825679c89fSjv 			myhostid = zone_get_hostid(NULL);
24835679c89fSjv #else	/* _KERNEL */
24845679c89fSjv 			/*
24855679c89fSjv 			 * We're emulating the system's hostid in userland, so
24865679c89fSjv 			 * we can't use zone_get_hostid().
24875679c89fSjv 			 */
248895173954Sek 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
24895679c89fSjv #endif	/* _KERNEL */
249017194a52Slling 			if (hostid != 0 && myhostid != 0 &&
24915679c89fSjv 			    hostid != myhostid) {
2492871a9500SMark J Musante 				nvlist_free(nvconfig);
249395173954Sek 				cmn_err(CE_WARN, "pool '%s' could not be "
249495173954Sek 				    "loaded as it was last accessed by "
249577650510SLin Ling 				    "another system (host: %s hostid: 0x%lx). "
2496654b400cSJoshua M. Clulow 				    "See: http://illumos.org/msg/ZFS-8000-EY",
2497e14bb325SJeff Bonwick 				    spa_name(spa), hostname,
249895173954Sek 				    (unsigned long)hostid);
2499be6fd75aSMatthew Ahrens 				return (SET_ERROR(EBADF));
250095173954Sek 			}
250195173954Sek 		}
2502c8ee1847SVictor Latushkin 		if (nvlist_lookup_nvlist(spa->spa_config,
2503c8ee1847SVictor Latushkin 		    ZPOOL_REWIND_POLICY, &policy) == 0)
2504c8ee1847SVictor Latushkin 			VERIFY(nvlist_add_nvlist(nvconfig,
2505c8ee1847SVictor Latushkin 			    ZPOOL_REWIND_POLICY, policy) == 0);
250695173954Sek 
250788ecc943SGeorge Wilson 		spa_config_set(spa, nvconfig);
2508fa9e4066Sahrens 		spa_unload(spa);
2509fa9e4066Sahrens 		spa_deactivate(spa);
25108ad4d6ddSJeff Bonwick 		spa_activate(spa, orig_mode);
2511fa9e4066Sahrens 
25121195e687SMark J Musante 		return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2513fa9e4066Sahrens 	}
2514fa9e4066Sahrens 
2515cde58dbcSMatthew Ahrens 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2516cde58dbcSMatthew Ahrens 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2517cde58dbcSMatthew Ahrens 	error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2518cde58dbcSMatthew Ahrens 	if (error != 0)
25191195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2520fa9e4066Sahrens 
252199653d4eSeschrock 	/*
252299653d4eSeschrock 	 * Load the bit that tells us to use the new accounting function
252399653d4eSeschrock 	 * (raid-z deflation).  If we have an older pool, this will not
252499653d4eSeschrock 	 * be present.
252599653d4eSeschrock 	 */
25261195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
25271195e687SMark J Musante 	if (error != 0 && error != ENOENT)
25281195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
252999653d4eSeschrock 
25303f9d6ad7SLin Ling 	error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
25313f9d6ad7SLin Ling 	    &spa->spa_creation_version);
25323f9d6ad7SLin Ling 	if (error != 0 && error != ENOENT)
25333f9d6ad7SLin Ling 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25343f9d6ad7SLin Ling 
2535fa9e4066Sahrens 	/*
2536ea8dc4b6Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
2537ea8dc4b6Seschrock 	 * not be present.
2538fa9e4066Sahrens 	 */
25391195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
25401195e687SMark J Musante 	if (error != 0 && error != ENOENT)
25411195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2542ea8dc4b6Seschrock 
25431195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
25441195e687SMark J Musante 	    &spa->spa_errlog_scrub);
25451195e687SMark J Musante 	if (error != 0 && error != ENOENT)
25461195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2547ea8dc4b6Seschrock 
254806eeb2adSek 	/*
254906eeb2adSek 	 * Load the history object.  If we have an older pool, this
255006eeb2adSek 	 * will not be present.
255106eeb2adSek 	 */
25521195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
25531195e687SMark J Musante 	if (error != 0 && error != ENOENT)
25541195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25551195e687SMark J Musante 
25561195e687SMark J Musante 	/*
25571195e687SMark J Musante 	 * If we're assembling the pool from the split-off vdevs of
25581195e687SMark J Musante 	 * an existing pool, we don't want to attach the spares & cache
25591195e687SMark J Musante 	 * devices.
25601195e687SMark J Musante 	 */
256106eeb2adSek 
256299653d4eSeschrock 	/*
256399653d4eSeschrock 	 * Load any hot spares for this pool.
256499653d4eSeschrock 	 */
25651195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
25661195e687SMark J Musante 	if (error != 0 && error != ENOENT)
25671195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25681195e687SMark J Musante 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2569e7437265Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2570fa94a07fSbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
25711195e687SMark J Musante 		    &spa->spa_spares.sav_config) != 0)
25721195e687SMark J Musante 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
257399653d4eSeschrock 
2574e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
257599653d4eSeschrock 		spa_load_spares(spa);
2576e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
25771195e687SMark J Musante 	} else if (error == 0) {
25781195e687SMark J Musante 		spa->spa_spares.sav_sync = B_TRUE;
257999653d4eSeschrock 	}
258099653d4eSeschrock 
2581fa94a07fSbrendan 	/*
2582fa94a07fSbrendan 	 * Load any level 2 ARC devices for this pool.
2583fa94a07fSbrendan 	 */
25841195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2585fa94a07fSbrendan 	    &spa->spa_l2cache.sav_object);
25861195e687SMark J Musante 	if (error != 0 && error != ENOENT)
25871195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25881195e687SMark J Musante 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2589fa94a07fSbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2590fa94a07fSbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
25911195e687SMark J Musante 		    &spa->spa_l2cache.sav_config) != 0)
25921195e687SMark J Musante 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2593fa94a07fSbrendan 
2594e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2595fa94a07fSbrendan 		spa_load_l2cache(spa);
2596e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
25971195e687SMark J Musante 	} else if (error == 0) {
25981195e687SMark J Musante 		spa->spa_l2cache.sav_sync = B_TRUE;
2599fa94a07fSbrendan 	}
2600fa94a07fSbrendan 
2601990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2602ecd6cf80Smarks 
26031195e687SMark J Musante 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
26041195e687SMark J Musante 	if (error && error != ENOENT)
26051195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2606b1b8ab34Slling 
2607b1b8ab34Slling 	if (error == 0) {
26081195e687SMark J Musante 		uint64_t autoreplace;
26091195e687SMark J Musante 
26101195e687SMark J Musante 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
26111195e687SMark J Musante 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
26121195e687SMark J Musante 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
26131195e687SMark J Musante 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
26141195e687SMark J Musante 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
26151195e687SMark J Musante 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
26161195e687SMark J Musante 		    &spa->spa_dedup_ditto);
26171195e687SMark J Musante 
2618b693757aSEric Schrock 		spa->spa_autoreplace = (autoreplace != 0);
2619b1b8ab34Slling 	}
2620b1b8ab34Slling 
26213d7072f8Seschrock 	/*
26223d7072f8Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
26233d7072f8Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
26243d7072f8Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
26253d7072f8Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
26263d7072f8Seschrock 	 * over.
26273d7072f8Seschrock 	 */
2628b693757aSEric Schrock 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
26293d7072f8Seschrock 		spa_check_removed(spa->spa_root_vdev);
2630b693757aSEric Schrock 		/*
2631b693757aSEric Schrock 		 * For the import case, this is done in spa_import(), because
2632b693757aSEric Schrock 		 * at this point we're using the spare definitions from
2633b693757aSEric Schrock 		 * the MOS config, not necessarily from the userland config.
2634b693757aSEric Schrock 		 */
2635b693757aSEric Schrock 		if (state != SPA_LOAD_IMPORT) {
2636b693757aSEric Schrock 			spa_aux_check_removed(&spa->spa_spares);
2637b693757aSEric Schrock 			spa_aux_check_removed(&spa->spa_l2cache);
2638b693757aSEric Schrock 		}
2639b693757aSEric Schrock 	}
26403d7072f8Seschrock 
2641ea8dc4b6Seschrock 	/*
2642560e6e96Seschrock 	 * Load the vdev state for all toplevel vdevs.
2643ea8dc4b6Seschrock 	 */
2644560e6e96Seschrock 	vdev_load(rvd);
26450373e76bSbonwick 
2646fa9e4066Sahrens 	/*
2647fa9e4066Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
2648fa9e4066Sahrens 	 */
2649e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2650fa9e4066Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2651e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
2652fa9e4066Sahrens 
2653b24ab676SJeff Bonwick 	/*
2654b24ab676SJeff Bonwick 	 * Load the DDTs (dedup tables).
2655b24ab676SJeff Bonwick 	 */
2656b24ab676SJeff Bonwick 	error = ddt_load(spa);
26571195e687SMark J Musante 	if (error != 0)
26581195e687SMark J Musante 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2659b24ab676SJeff Bonwick 
2660485bbbf5SGeorge Wilson 	spa_update_dspace(spa);
2661485bbbf5SGeorge Wilson 
2662b24ab676SJeff Bonwick 	/*
26634b964adaSGeorge Wilson 	 * Validate the config, using the MOS config to fill in any
26644b964adaSGeorge Wilson 	 * information which might be missing.  If we fail to validate
26654b964adaSGeorge Wilson 	 * the config then declare the pool unfit for use. If we're
26664b964adaSGeorge Wilson 	 * assembling a pool from a split, the log is not transferred
26674b964adaSGeorge Wilson 	 * over.
2668b24ab676SJeff Bonwick 	 */
26691195e687SMark J Musante 	if (type != SPA_IMPORT_ASSEMBLE) {
2670871a9500SMark J Musante 		nvlist_t *nvconfig;
2671871a9500SMark J Musante 
2672871a9500SMark J Musante 		if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2673871a9500SMark J Musante 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2674871a9500SMark J Musante 
26754b964adaSGeorge Wilson 		if (!spa_config_valid(spa, nvconfig)) {
26764b964adaSGeorge Wilson 			nvlist_free(nvconfig);
26774b964adaSGeorge Wilson 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
26784b964adaSGeorge Wilson 			    ENXIO));
26794b964adaSGeorge Wilson 		}
26801195e687SMark J Musante 		nvlist_free(nvconfig);
26811195e687SMark J Musante 
26824b964adaSGeorge Wilson 		/*
2683ad135b5dSChristopher Siden 		 * Now that we've validated the config, check the state of the
26844b964adaSGeorge Wilson 		 * root vdev.  If it can't be opened, it indicates one or
26854b964adaSGeorge Wilson 		 * more toplevel vdevs are faulted.
26864b964adaSGeorge Wilson 		 */
26874b964adaSGeorge Wilson 		if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2688be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENXIO));
26894b964adaSGeorge Wilson 
269023367a2fSMatthew Ahrens 		if (spa_writeable(spa) && spa_check_logs(spa)) {
26911195e687SMark J Musante 			*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
26921195e687SMark J Musante 			return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
26931195e687SMark J Musante 		}
2694b24ab676SJeff Bonwick 	}
2695b24ab676SJeff Bonwick 
2696ad135b5dSChristopher Siden 	if (missing_feat_write) {
2697ad135b5dSChristopher Siden 		ASSERT(state == SPA_LOAD_TRYIMPORT);
2698ad135b5dSChristopher Siden 
2699ad135b5dSChristopher Siden 		/*
2700ad135b5dSChristopher Siden 		 * At this point, we know that we can open the pool in
2701ad135b5dSChristopher Siden 		 * read-only mode but not read-write mode. We now have enough
2702ad135b5dSChristopher Siden 		 * information and can return to userland.
2703ad135b5dSChristopher Siden 		 */
2704ad135b5dSChristopher Siden 		return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2705ad135b5dSChristopher Siden 	}
2706ad135b5dSChristopher Siden 
27074b964adaSGeorge Wilson 	/*
27084b964adaSGeorge Wilson 	 * We've successfully opened the pool, verify that we're ready
27094b964adaSGeorge Wilson 	 * to start pushing transactions.
27104b964adaSGeorge Wilson 	 */
27114b964adaSGeorge Wilson 	if (state != SPA_LOAD_TRYIMPORT) {
27124b964adaSGeorge Wilson 		if (error = spa_load_verify(spa))
27134b964adaSGeorge Wilson 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
27144b964adaSGeorge Wilson 			    error));
27154b964adaSGeorge Wilson 	}
27164b964adaSGeorge Wilson 
2717468c413aSTim Haley 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2718468c413aSTim Haley 	    spa->spa_load_max_txg == UINT64_MAX)) {
27195dabedeeSbonwick 		dmu_tx_t *tx;
27200373e76bSbonwick 		int need_update = B_FALSE;
272112380e1eSArne Jansen 		dsl_pool_t *dp = spa_get_dsl(spa);
27228ad4d6ddSJeff Bonwick 
27238ad4d6ddSJeff Bonwick 		ASSERT(state != SPA_LOAD_TRYIMPORT);
27245dabedeeSbonwick 
27250373e76bSbonwick 		/*
27260373e76bSbonwick 		 * Claim log blocks that haven't been committed yet.
27270373e76bSbonwick 		 * This must all happen in a single txg.
2728b24ab676SJeff Bonwick 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2729b24ab676SJeff Bonwick 		 * invoked from zil_claim_log_block()'s i/o done callback.
2730468c413aSTim Haley 		 * Price of rollback is that we abandon the log.
27310373e76bSbonwick 		 */
2732b24ab676SJeff Bonwick 		spa->spa_claiming = B_TRUE;
2733b24ab676SJeff Bonwick 
273412380e1eSArne Jansen 		tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
273512380e1eSArne Jansen 		(void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
27360b69c2f0Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
2737fa9e4066Sahrens 		dmu_tx_commit(tx);
2738fa9e4066Sahrens 
2739b24ab676SJeff Bonwick 		spa->spa_claiming = B_FALSE;
2740b24ab676SJeff Bonwick 
27411195e687SMark J Musante 		spa_set_log_state(spa, SPA_LOG_GOOD);
2742fa9e4066Sahrens 		spa->spa_sync_on = B_TRUE;
2743fa9e4066Sahrens 		txg_sync_start(spa->spa_dsl_pool);
2744fa9e4066Sahrens 
2745fa9e4066Sahrens 		/*
2746b24ab676SJeff Bonwick 		 * Wait for all claims to sync.  We sync up to the highest
2747b24ab676SJeff Bonwick 		 * claimed log block birth time so that claimed log blocks
2748b24ab676SJeff Bonwick 		 * don't appear to be from the future.  spa_claim_max_txg
2749b24ab676SJeff Bonwick 		 * will have been set for us by either zil_check_log_chain()
2750b24ab676SJeff Bonwick 		 * (invoked from spa_check_logs()) or zil_claim() above.
2751fa9e4066Sahrens 		 */
2752b24ab676SJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
27530e34b6a7Sbonwick 
27540e34b6a7Sbonwick 		/*
27550373e76bSbonwick 		 * If the config cache is stale, or we have uninitialized
27560373e76bSbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
2757bc758434SLin Ling 		 *
27584b964adaSGeorge Wilson 		 * If this is a verbatim import, trust the current
2759bc758434SLin Ling 		 * in-core spa_config and update the disk labels.
27600e34b6a7Sbonwick 		 */
27610373e76bSbonwick 		if (config_cache_txg != spa->spa_config_txg ||
27624b964adaSGeorge Wilson 		    state == SPA_LOAD_IMPORT ||
27634b964adaSGeorge Wilson 		    state == SPA_LOAD_RECOVER ||
27644b964adaSGeorge Wilson 		    (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
27650373e76bSbonwick 			need_update = B_TRUE;
27660373e76bSbonwick 
27678ad4d6ddSJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++)
27680373e76bSbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
27690373e76bSbonwick 				need_update = B_TRUE;
27700e34b6a7Sbonwick 
27710e34b6a7Sbonwick 		/*
27720373e76bSbonwick 		 * Update the config cache asychronously in case we're the
27730373e76bSbonwick 		 * root pool, in which case the config cache isn't writable yet.
27740e34b6a7Sbonwick 		 */
27750373e76bSbonwick 		if (need_update)
27760373e76bSbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
27778ad4d6ddSJeff Bonwick 
27788ad4d6ddSJeff Bonwick 		/*
27798ad4d6ddSJeff Bonwick 		 * Check all DTLs to see if anything needs resilvering.
27808ad4d6ddSJeff Bonwick 		 */
27813f9d6ad7SLin Ling 		if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
27823f9d6ad7SLin Ling 		    vdev_resilver_needed(rvd, NULL, NULL))
27838ad4d6ddSJeff Bonwick 			spa_async_request(spa, SPA_ASYNC_RESILVER);
2784503ad85cSMatthew Ahrens 
27854445fffbSMatthew Ahrens 		/*
27864445fffbSMatthew Ahrens 		 * Log the fact that we booted up (so that we can detect if
27874445fffbSMatthew Ahrens 		 * we rebooted in the middle of an operation).
27884445fffbSMatthew Ahrens 		 */
27894445fffbSMatthew Ahrens 		spa_history_log_version(spa, "open");
27904445fffbSMatthew Ahrens 
2791503ad85cSMatthew Ahrens 		/*
2792503ad85cSMatthew Ahrens 		 * Delete any inconsistent datasets.
2793503ad85cSMatthew Ahrens 		 */
2794503ad85cSMatthew Ahrens 		(void) dmu_objset_find(spa_name(spa),
2795503ad85cSMatthew Ahrens 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2796ca45db41SChris Kirby 
2797ca45db41SChris Kirby 		/*
2798ca45db41SChris Kirby 		 * Clean up any stale temporary dataset userrefs.
2799ca45db41SChris Kirby 		 */
2800ca45db41SChris Kirby 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2801fa9e4066Sahrens 	}
2802fa9e4066Sahrens 
28031195e687SMark J Musante 	return (0);
2804fa9e4066Sahrens }
2805fa9e4066Sahrens 
2806468c413aSTim Haley static int
2807468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2808468c413aSTim Haley {
2809f9af39baSGeorge Wilson 	int mode = spa->spa_mode;
2810f9af39baSGeorge Wilson 
2811468c413aSTim Haley 	spa_unload(spa);
2812468c413aSTim Haley 	spa_deactivate(spa);
2813468c413aSTim Haley 
2814e42d2059SMatthew Ahrens 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2815468c413aSTim Haley 
2816f9af39baSGeorge Wilson 	spa_activate(spa, mode);
2817468c413aSTim Haley 	spa_async_suspend(spa);
2818468c413aSTim Haley 
28191195e687SMark J Musante 	return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2820468c413aSTim Haley }
2821468c413aSTim Haley 
2822ad135b5dSChristopher Siden /*
2823ad135b5dSChristopher Siden  * If spa_load() fails this function will try loading prior txg's. If
2824ad135b5dSChristopher Siden  * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2825ad135b5dSChristopher Siden  * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2826ad135b5dSChristopher Siden  * function will not rewind the pool and will return the same error as
2827ad135b5dSChristopher Siden  * spa_load().
2828ad135b5dSChristopher Siden  */
2829468c413aSTim Haley static int
2830468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2831c8ee1847SVictor Latushkin     uint64_t max_request, int rewind_flags)
2832468c413aSTim Haley {
2833ad135b5dSChristopher Siden 	nvlist_t *loadinfo = NULL;
2834468c413aSTim Haley 	nvlist_t *config = NULL;
2835468c413aSTim Haley 	int load_error, rewind_error;
2836c8ee1847SVictor Latushkin 	uint64_t safe_rewind_txg;
2837468c413aSTim Haley 	uint64_t min_txg;
2838468c413aSTim Haley 
2839a33cae98STim Haley 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2840468c413aSTim Haley 		spa->spa_load_max_txg = spa->spa_load_txg;
28411195e687SMark J Musante 		spa_set_log_state(spa, SPA_LOG_CLEAR);
2842a33cae98STim Haley 	} else {
2843468c413aSTim Haley 		spa->spa_load_max_txg = max_request;
2844e42d2059SMatthew Ahrens 		if (max_request != UINT64_MAX)
2845e42d2059SMatthew Ahrens 			spa->spa_extreme_rewind = B_TRUE;
2846a33cae98STim Haley 	}
2847468c413aSTim Haley 
28481195e687SMark J Musante 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
28491195e687SMark J Musante 	    mosconfig);
2850468c413aSTim Haley 	if (load_error == 0)
2851468c413aSTim Haley 		return (0);
2852468c413aSTim Haley 
2853468c413aSTim Haley 	if (spa->spa_root_vdev != NULL)
2854468c413aSTim Haley 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2855468c413aSTim Haley 
2856468c413aSTim Haley 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2857468c413aSTim Haley 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2858468c413aSTim Haley 
2859c8ee1847SVictor Latushkin 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
2860468c413aSTim Haley 		nvlist_free(config);
2861468c413aSTim Haley 		return (load_error);
2862468c413aSTim Haley 	}
2863468c413aSTim Haley 
2864ad135b5dSChristopher Siden 	if (state == SPA_LOAD_RECOVER) {
2865ad135b5dSChristopher Siden 		/* Price of rolling back is discarding txgs, including log */
28661195e687SMark J Musante 		spa_set_log_state(spa, SPA_LOG_CLEAR);
2867ad135b5dSChristopher Siden 	} else {
2868ad135b5dSChristopher Siden 		/*
2869ad135b5dSChristopher Siden 		 * If we aren't rolling back save the load info from our first
2870ad135b5dSChristopher Siden 		 * import attempt so that we can restore it after attempting
2871ad135b5dSChristopher Siden 		 * to rewind.
2872ad135b5dSChristopher Siden 		 */
2873ad135b5dSChristopher Siden 		loadinfo = spa->spa_load_info;
2874ad135b5dSChristopher Siden 		spa->spa_load_info = fnvlist_alloc();
2875ad135b5dSChristopher Siden 	}
2876468c413aSTim Haley 
2877c8ee1847SVictor Latushkin 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2878c8ee1847SVictor Latushkin 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2879c8ee1847SVictor Latushkin 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2880c8ee1847SVictor Latushkin 	    TXG_INITIAL : safe_rewind_txg;
2881468c413aSTim Haley 
2882c8ee1847SVictor Latushkin 	/*
2883c8ee1847SVictor Latushkin 	 * Continue as long as we're finding errors, we're still within
2884c8ee1847SVictor Latushkin 	 * the acceptable rewind range, and we're still finding uberblocks
2885c8ee1847SVictor Latushkin 	 */
2886c8ee1847SVictor Latushkin 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2887c8ee1847SVictor Latushkin 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2888c8ee1847SVictor Latushkin 		if (spa->spa_load_max_txg < safe_rewind_txg)
2889468c413aSTim Haley 			spa->spa_extreme_rewind = B_TRUE;
2890468c413aSTim Haley 		rewind_error = spa_load_retry(spa, state, mosconfig);
2891468c413aSTim Haley 	}
2892468c413aSTim Haley 
2893468c413aSTim Haley 	spa->spa_extreme_rewind = B_FALSE;
2894468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
2895468c413aSTim Haley 
2896468c413aSTim Haley 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2897468c413aSTim Haley 		spa_config_set(spa, config);
2898468c413aSTim Haley 
2899ad135b5dSChristopher Siden 	if (state == SPA_LOAD_RECOVER) {
2900ad135b5dSChristopher Siden 		ASSERT3P(loadinfo, ==, NULL);
2901ad135b5dSChristopher Siden 		return (rewind_error);
2902ad135b5dSChristopher Siden 	} else {
2903ad135b5dSChristopher Siden 		/* Store the rewind info as part of the initial load info */
2904ad135b5dSChristopher Siden 		fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2905ad135b5dSChristopher Siden 		    spa->spa_load_info);
2906ad135b5dSChristopher Siden 
2907ad135b5dSChristopher Siden 		/* Restore the initial load info */
2908ad135b5dSChristopher Siden 		fnvlist_free(spa->spa_load_info);
2909ad135b5dSChristopher Siden 		spa->spa_load_info = loadinfo;
2910ad135b5dSChristopher Siden 
2911ad135b5dSChristopher Siden 		return (load_error);
2912ad135b5dSChristopher Siden 	}
2913468c413aSTim Haley }
2914468c413aSTim Haley 
2915fa9e4066Sahrens /*
2916fa9e4066Sahrens  * Pool Open/Import
2917fa9e4066Sahrens  *
2918fa9e4066Sahrens  * The import case is identical to an open except that the configuration is sent
2919fa9e4066Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
2920fa9e4066Sahrens  * case of an open, the pool configuration will exist in the
29213d7072f8Seschrock  * POOL_STATE_UNINITIALIZED state.
2922fa9e4066Sahrens  *
2923fa9e4066Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
2924fa9e4066Sahrens  * the same time open the pool, without having to keep around the spa_t in some
2925fa9e4066Sahrens  * ambiguous state.
2926fa9e4066Sahrens  */
2927fa9e4066Sahrens static int
2928468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2929468c413aSTim Haley     nvlist_t **config)
2930fa9e4066Sahrens {
2931fa9e4066Sahrens 	spa_t *spa;
29324b964adaSGeorge Wilson 	spa_load_state_t state = SPA_LOAD_OPEN;
2933fa9e4066Sahrens 	int error;
2934fa9e4066Sahrens 	int locked = B_FALSE;
2935fa9e4066Sahrens 
2936fa9e4066Sahrens 	*spapp = NULL;
2937fa9e4066Sahrens 
2938fa9e4066Sahrens 	/*
2939fa9e4066Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
2940fa9e4066Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
2941fa9e4066Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
2942fa9e4066Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
2943fa9e4066Sahrens 	 */
2944fa9e4066Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
2945fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
2946fa9e4066Sahrens 		locked = B_TRUE;
2947fa9e4066Sahrens 	}
2948fa9e4066Sahrens 
2949fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2950fa9e4066Sahrens 		if (locked)
2951fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
2952be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
2953fa9e4066Sahrens 	}
2954468c413aSTim Haley 
2955fa9e4066Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
29564b44c88cSTim Haley 		zpool_rewind_policy_t policy;
29574b44c88cSTim Haley 
29584b44c88cSTim Haley 		zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
29594b44c88cSTim Haley 		    &policy);
29604b44c88cSTim Haley 		if (policy.zrp_request & ZPOOL_DO_REWIND)
29614b44c88cSTim Haley 			state = SPA_LOAD_RECOVER;
2962fa9e4066Sahrens 
29638ad4d6ddSJeff Bonwick 		spa_activate(spa, spa_mode_global);
2964fa9e4066Sahrens 
2965468c413aSTim Haley 		if (state != SPA_LOAD_RECOVER)
2966468c413aSTim Haley 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2967468c413aSTim Haley 
2968468c413aSTim Haley 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2969c8ee1847SVictor Latushkin 		    policy.zrp_request);
2970fa9e4066Sahrens 
2971fa9e4066Sahrens 		if (error == EBADF) {
2972fa9e4066Sahrens 			/*
2973560e6e96Seschrock 			 * If vdev_validate() returns failure (indicated by
2974560e6e96Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
2975560e6e96Seschrock 			 * that the pool has been exported or destroyed.  If
2976560e6e96Seschrock 			 * this is the case, the config cache is out of sync and
2977560e6e96Seschrock 			 * we should remove the pool from the namespace.
2978fa9e4066Sahrens 			 */
2979fa9e4066Sahrens 			spa_unload(spa);
2980fa9e4066Sahrens 			spa_deactivate(spa);
2981c5904d13Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
2982fa9e4066Sahrens 			spa_remove(spa);
2983fa9e4066Sahrens 			if (locked)
2984fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
2985be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
2986ea8dc4b6Seschrock 		}
2987ea8dc4b6Seschrock 
2988ea8dc4b6Seschrock 		if (error) {
2989fa9e4066Sahrens 			/*
2990fa9e4066Sahrens 			 * We can't open the pool, but we still have useful
2991fa9e4066Sahrens 			 * information: the state of each vdev after the
2992fa9e4066Sahrens 			 * attempted vdev_open().  Return this to the user.
2993fa9e4066Sahrens 			 */
29944b964adaSGeorge Wilson 			if (config != NULL && spa->spa_config) {
2995468c413aSTim Haley 				VERIFY(nvlist_dup(spa->spa_config, config,
2996468c413aSTim Haley 				    KM_SLEEP) == 0);
29974b964adaSGeorge Wilson 				VERIFY(nvlist_add_nvlist(*config,
29984b964adaSGeorge Wilson 				    ZPOOL_CONFIG_LOAD_INFO,
29994b964adaSGeorge Wilson 				    spa->spa_load_info) == 0);
30004b964adaSGeorge Wilson 			}
3001fa9e4066Sahrens 			spa_unload(spa);
3002fa9e4066Sahrens 			spa_deactivate(spa);
3003468c413aSTim Haley 			spa->spa_last_open_failed = error;
3004fa9e4066Sahrens 			if (locked)
3005fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
3006fa9e4066Sahrens 			*spapp = NULL;
3007fa9e4066Sahrens 			return (error);
3008fa9e4066Sahrens 		}
3009fa9e4066Sahrens 	}
3010fa9e4066Sahrens 
3011fa9e4066Sahrens 	spa_open_ref(spa, tag);
30123d7072f8Seschrock 
3013468c413aSTim Haley 	if (config != NULL)
3014468c413aSTim Haley 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3015468c413aSTim Haley 
30164b964adaSGeorge Wilson 	/*
30174b964adaSGeorge Wilson 	 * If we've recovered the pool, pass back any information we
30184b964adaSGeorge Wilson 	 * gathered while doing the load.
30194b964adaSGeorge Wilson 	 */
30204b964adaSGeorge Wilson 	if (state == SPA_LOAD_RECOVER) {
30214b964adaSGeorge Wilson 		VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
30224b964adaSGeorge Wilson 		    spa->spa_load_info) == 0);
30234b964adaSGeorge Wilson 	}
30244b964adaSGeorge Wilson 
3025a33cae98STim Haley 	if (locked) {
3026a33cae98STim Haley 		spa->spa_last_open_failed = 0;
3027a33cae98STim Haley 		spa->spa_last_ubsync_txg = 0;
3028a33cae98STim Haley 		spa->spa_load_txg = 0;
3029fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
3030a33cae98STim Haley 	}
3031fa9e4066Sahrens 
3032fa9e4066Sahrens 	*spapp = spa;
3033fa9e4066Sahrens 
3034fa9e4066Sahrens 	return (0);
3035fa9e4066Sahrens }
3036fa9e4066Sahrens 
3037468c413aSTim Haley int
3038468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3039468c413aSTim Haley     nvlist_t **config)
3040468c413aSTim Haley {
3041468c413aSTim Haley 	return (spa_open_common(name, spapp, tag, policy, config));
3042468c413aSTim Haley }
3043468c413aSTim Haley 
3044fa9e4066Sahrens int
3045fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
3046fa9e4066Sahrens {
3047468c413aSTim Haley 	return (spa_open_common(name, spapp, tag, NULL, NULL));
3048fa9e4066Sahrens }
3049fa9e4066Sahrens 
3050ea8dc4b6Seschrock /*
3051ea8dc4b6Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
3052ea8dc4b6Seschrock  * preventing it from being exported or destroyed.
3053ea8dc4b6Seschrock  */
3054ea8dc4b6Seschrock spa_t *
3055ea8dc4b6Seschrock spa_inject_addref(char *name)
3056ea8dc4b6Seschrock {
3057ea8dc4b6Seschrock 	spa_t *spa;
3058ea8dc4b6Seschrock 
3059ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
3060ea8dc4b6Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
3061ea8dc4b6Seschrock 		mutex_exit(&spa_namespace_lock);
3062ea8dc4b6Seschrock 		return (NULL);
3063ea8dc4b6Seschrock 	}
3064ea8dc4b6Seschrock 	spa->spa_inject_ref++;
3065ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
3066ea8dc4b6Seschrock 
3067ea8dc4b6Seschrock 	return (spa);
3068ea8dc4b6Seschrock }
3069ea8dc4b6Seschrock 
3070ea8dc4b6Seschrock void
3071ea8dc4b6Seschrock spa_inject_delref(spa_t *spa)
3072ea8dc4b6Seschrock {
3073ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
3074ea8dc4b6Seschrock 	spa->spa_inject_ref--;
3075ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
3076ea8dc4b6Seschrock }
3077ea8dc4b6Seschrock 
3078fa94a07fSbrendan /*
3079fa94a07fSbrendan  * Add spares device information to the nvlist.
3080fa94a07fSbrendan  */
308199653d4eSeschrock static void
308299653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config)
308399653d4eSeschrock {
308499653d4eSeschrock 	nvlist_t **spares;
308599653d4eSeschrock 	uint_t i, nspares;
308699653d4eSeschrock 	nvlist_t *nvroot;
308799653d4eSeschrock 	uint64_t guid;
308899653d4eSeschrock 	vdev_stat_t *vs;
308999653d4eSeschrock 	uint_t vsc;
309039c23413Seschrock 	uint64_t pool;
309199653d4eSeschrock 
30926809eb4eSEric Schrock 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
30936809eb4eSEric Schrock 
3094fa94a07fSbrendan 	if (spa->spa_spares.sav_count == 0)
309599653d4eSeschrock 		return;
309699653d4eSeschrock 
309799653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config,
309899653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3099fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
310099653d4eSeschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
310199653d4eSeschrock 	if (nspares != 0) {
310299653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
310399653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
310499653d4eSeschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
310599653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
310699653d4eSeschrock 
310799653d4eSeschrock 		/*
310899653d4eSeschrock 		 * Go through and find any spares which have since been
310999653d4eSeschrock 		 * repurposed as an active spare.  If this is the case, update
311099653d4eSeschrock 		 * their status appropriately.
311199653d4eSeschrock 		 */
311299653d4eSeschrock 		for (i = 0; i < nspares; i++) {
311399653d4eSeschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
311499653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
311589a89ebfSlling 			if (spa_spare_exists(guid, &pool, NULL) &&
311689a89ebfSlling 			    pool != 0ULL) {
311799653d4eSeschrock 				VERIFY(nvlist_lookup_uint64_array(
31183f9d6ad7SLin Ling 				    spares[i], ZPOOL_CONFIG_VDEV_STATS,
311999653d4eSeschrock 				    (uint64_t **)&vs, &vsc) == 0);
312099653d4eSeschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
312199653d4eSeschrock 				vs->vs_aux = VDEV_AUX_SPARED;
312299653d4eSeschrock 			}
312399653d4eSeschrock 		}
312499653d4eSeschrock 	}
312599653d4eSeschrock }
312699653d4eSeschrock 
3127fa94a07fSbrendan /*
3128fa94a07fSbrendan  * Add l2cache device information to the nvlist, including vdev stats.
3129fa94a07fSbrendan  */
3130fa94a07fSbrendan static void
3131fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
3132fa94a07fSbrendan {
3133fa94a07fSbrendan 	nvlist_t **l2cache;
3134fa94a07fSbrendan 	uint_t i, j, nl2cache;
3135fa94a07fSbrendan 	nvlist_t *nvroot;
3136fa94a07fSbrendan 	uint64_t guid;
3137fa94a07fSbrendan 	vdev_t *vd;
3138fa94a07fSbrendan 	vdev_stat_t *vs;
3139fa94a07fSbrendan 	uint_t vsc;
3140fa94a07fSbrendan 
31416809eb4eSEric Schrock 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
31426809eb4eSEric Schrock 
3143fa94a07fSbrendan 	if (spa->spa_l2cache.sav_count == 0)
3144fa94a07fSbrendan 		return;
3145fa94a07fSbrendan 
3146fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist(config,
3147fa94a07fSbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3148fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3149fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3150fa94a07fSbrendan 	if (nl2cache != 0) {
3151fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
3152fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3153fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
3154fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3155fa94a07fSbrendan 
3156fa94a07fSbrendan 		/*
3157fa94a07fSbrendan 		 * Update level 2 cache device stats.
3158fa94a07fSbrendan 		 */
3159fa94a07fSbrendan 
3160fa94a07fSbrendan 		for (i = 0; i < nl2cache; i++) {
3161fa94a07fSbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
3162fa94a07fSbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
3163fa94a07fSbrendan 
3164fa94a07fSbrendan 			vd = NULL;
3165fa94a07fSbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3166fa94a07fSbrendan 				if (guid ==
3167fa94a07fSbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3168fa94a07fSbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
3169fa94a07fSbrendan 					break;
3170fa94a07fSbrendan 				}
3171fa94a07fSbrendan 			}
3172fa94a07fSbrendan 			ASSERT(vd != NULL);
3173fa94a07fSbrendan 
3174fa94a07fSbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
31753f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
31763f9d6ad7SLin Ling 			    == 0);
3177fa94a07fSbrendan 			vdev_get_stats(vd, vs);
3178fa94a07fSbrendan 		}
3179fa94a07fSbrendan 	}
3180fa94a07fSbrendan }
3181fa94a07fSbrendan 
3182ad135b5dSChristopher Siden static void
3183ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3184ad135b5dSChristopher Siden {
3185ad135b5dSChristopher Siden 	nvlist_t *features;
3186ad135b5dSChristopher Siden 	zap_cursor_t zc;
3187ad135b5dSChristopher Siden 	zap_attribute_t za;
3188ad135b5dSChristopher Siden 
3189ad135b5dSChristopher Siden 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3190ad135b5dSChristopher Siden 	VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3191ad135b5dSChristopher Siden 
3192ad135b5dSChristopher Siden 	if (spa->spa_feat_for_read_obj != 0) {
3193ad135b5dSChristopher Siden 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
3194ad135b5dSChristopher Siden 		    spa->spa_feat_for_read_obj);
3195ad135b5dSChristopher Siden 		    zap_cursor_retrieve(&zc, &za) == 0;
3196ad135b5dSChristopher Siden 		    zap_cursor_advance(&zc)) {
3197ad135b5dSChristopher Siden 			ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3198ad135b5dSChristopher Siden 			    za.za_num_integers == 1);
3199b420f3adSRichard Lowe 			VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3200ad135b5dSChristopher Siden 			    za.za_first_integer));
3201ad135b5dSChristopher Siden 		}
3202ad135b5dSChristopher Siden 		zap_cursor_fini(&zc);
3203ad135b5dSChristopher Siden 	}
3204ad135b5dSChristopher Siden 
3205ad135b5dSChristopher Siden 	if (spa->spa_feat_for_write_obj != 0) {
3206ad135b5dSChristopher Siden 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
3207ad135b5dSChristopher Siden 		    spa->spa_feat_for_write_obj);
3208ad135b5dSChristopher Siden 		    zap_cursor_retrieve(&zc, &za) == 0;
3209ad135b5dSChristopher Siden 		    zap_cursor_advance(&zc)) {
3210ad135b5dSChristopher Siden 			ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3211ad135b5dSChristopher Siden 			    za.za_num_integers == 1);
3212b420f3adSRichard Lowe 			VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3213ad135b5dSChristopher Siden 			    za.za_first_integer));
3214ad135b5dSChristopher Siden 		}
3215ad135b5dSChristopher Siden 		zap_cursor_fini(&zc);
3216ad135b5dSChristopher Siden 	}
3217ad135b5dSChristopher Siden 
3218ad135b5dSChristopher Siden 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3219ad135b5dSChristopher Siden 	    features) == 0);
3220ad135b5dSChristopher Siden 	nvlist_free(features);
3221ad135b5dSChristopher Siden }
3222ad135b5dSChristopher Siden 
3223fa9e4066Sahrens int
3224ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config,
3225ad135b5dSChristopher Siden     char *altroot, size_t buflen)
3226fa9e4066Sahrens {
3227fa9e4066Sahrens 	int error;
3228fa9e4066Sahrens 	spa_t *spa;
3229fa9e4066Sahrens 
3230fa9e4066Sahrens 	*config = NULL;
3231468c413aSTim Haley 	error = spa_open_common(name, &spa, FTAG, NULL, config);
3232fa9e4066Sahrens 
32336809eb4eSEric Schrock 	if (spa != NULL) {
32346809eb4eSEric Schrock 		/*
32356809eb4eSEric Schrock 		 * This still leaves a window of inconsistency where the spares
32366809eb4eSEric Schrock 		 * or l2cache devices could change and the config would be
32376809eb4eSEric Schrock 		 * self-inconsistent.
32386809eb4eSEric Schrock 		 */
32396809eb4eSEric Schrock 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3240ea8dc4b6Seschrock 
32416809eb4eSEric Schrock 		if (*config != NULL) {
324211027bc7STim Haley 			uint64_t loadtimes[2];
324311027bc7STim Haley 
324411027bc7STim Haley 			loadtimes[0] = spa->spa_loaded_ts.tv_sec;
324511027bc7STim Haley 			loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
324611027bc7STim Haley 			VERIFY(nvlist_add_uint64_array(*config,
324711027bc7STim Haley 			    ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
324811027bc7STim Haley 
3249e14bb325SJeff Bonwick 			VERIFY(nvlist_add_uint64(*config,
32506809eb4eSEric Schrock 			    ZPOOL_CONFIG_ERRCOUNT,
32516809eb4eSEric Schrock 			    spa_get_errlog_size(spa)) == 0);
3252e14bb325SJeff Bonwick 
32536809eb4eSEric Schrock 			if (spa_suspended(spa))
32546809eb4eSEric Schrock 				VERIFY(nvlist_add_uint64(*config,
32556809eb4eSEric Schrock 				    ZPOOL_CONFIG_SUSPENDED,
32566809eb4eSEric Schrock 				    spa->spa_failmode) == 0);
32576809eb4eSEric Schrock 
32586809eb4eSEric Schrock 			spa_add_spares(spa, *config);
32596809eb4eSEric Schrock 			spa_add_l2cache(spa, *config);
3260ad135b5dSChristopher Siden 			spa_add_feature_stats(spa, *config);
32616809eb4eSEric Schrock 		}
326299653d4eSeschrock 	}
326399653d4eSeschrock 
3264ea8dc4b6Seschrock 	/*
3265ea8dc4b6Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
3266ea8dc4b6Seschrock 	 * and call spa_lookup() directly.
3267ea8dc4b6Seschrock 	 */
3268ea8dc4b6Seschrock 	if (altroot) {
3269ea8dc4b6Seschrock 		if (spa == NULL) {
3270ea8dc4b6Seschrock 			mutex_enter(&spa_namespace_lock);
3271ea8dc4b6Seschrock 			spa = spa_lookup(name);
3272ea8dc4b6Seschrock 			if (spa)
3273ea8dc4b6Seschrock 				spa_altroot(spa, altroot, buflen);
3274ea8dc4b6Seschrock 			else
3275ea8dc4b6Seschrock 				altroot[0] = '\0';
3276ea8dc4b6Seschrock 			spa = NULL;
3277ea8dc4b6Seschrock 			mutex_exit(&spa_namespace_lock);
3278ea8dc4b6Seschrock 		} else {
3279ea8dc4b6Seschrock 			spa_altroot(spa, altroot, buflen);
3280ea8dc4b6Seschrock 		}
3281ea8dc4b6Seschrock 	}
3282ea8dc4b6Seschrock 
32836809eb4eSEric Schrock 	if (spa != NULL) {
32846809eb4eSEric Schrock 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3285fa9e4066Sahrens 		spa_close(spa, FTAG);
32866809eb4eSEric Schrock 	}
3287fa9e4066Sahrens 
3288fa9e4066Sahrens 	return (error);
3289fa9e4066Sahrens }
3290fa9e4066Sahrens 
329199653d4eSeschrock /*
3292fa94a07fSbrendan  * Validate that the auxiliary device array is well formed.  We must have an
3293fa94a07fSbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
3294fa94a07fSbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3295fa94a07fSbrendan  * specified, as long as they are well-formed.
329699653d4eSeschrock  */
329799653d4eSeschrock static int
3298fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3299fa94a07fSbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
3300fa94a07fSbrendan     vdev_labeltype_t label)
330199653d4eSeschrock {
3302fa94a07fSbrendan 	nvlist_t **dev;
3303fa94a07fSbrendan 	uint_t i, ndev;
330499653d4eSeschrock 	vdev_t *vd;
330599653d4eSeschrock 	int error;
330699653d4eSeschrock 
3307e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3308e14bb325SJeff Bonwick 
330999653d4eSeschrock 	/*
3310fa94a07fSbrendan 	 * It's acceptable to have no devs specified.
331199653d4eSeschrock 	 */
3312fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
331399653d4eSeschrock 		return (0);
331499653d4eSeschrock 
3315fa94a07fSbrendan 	if (ndev == 0)
3316be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
331799653d4eSeschrock 
331899653d4eSeschrock 	/*
3319fa94a07fSbrendan 	 * Make sure the pool is formatted with a version that supports this
3320fa94a07fSbrendan 	 * device type.
332199653d4eSeschrock 	 */
3322fa94a07fSbrendan 	if (spa_version(spa) < version)
3323be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
332499653d4eSeschrock 
332539c23413Seschrock 	/*
3326fa94a07fSbrendan 	 * Set the pending device list so we correctly handle device in-use
332739c23413Seschrock 	 * checking.
332839c23413Seschrock 	 */
3329fa94a07fSbrendan 	sav->sav_pending = dev;
3330fa94a07fSbrendan 	sav->sav_npending = ndev;
333139c23413Seschrock 
3332fa94a07fSbrendan 	for (i = 0; i < ndev; i++) {
3333fa94a07fSbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
333499653d4eSeschrock 		    mode)) != 0)
333539c23413Seschrock 			goto out;
333699653d4eSeschrock 
333799653d4eSeschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
333899653d4eSeschrock 			vdev_free(vd);
3339be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
334039c23413Seschrock 			goto out;
334199653d4eSeschrock 		}
334299653d4eSeschrock 
3343fa94a07fSbrendan 		/*
3344e14bb325SJeff Bonwick 		 * The L2ARC currently only supports disk devices in
3345e14bb325SJeff Bonwick 		 * kernel context.  For user-level testing, we allow it.
3346fa94a07fSbrendan 		 */
3347e14bb325SJeff Bonwick #ifdef _KERNEL
3348fa94a07fSbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3349fa94a07fSbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3350be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOTBLK);
3351cd0837ccSGeorge Wilson 			vdev_free(vd);
3352fa94a07fSbrendan 			goto out;
3353fa94a07fSbrendan 		}
3354e14bb325SJeff Bonwick #endif
335599653d4eSeschrock 		vd->vdev_top = vd;
335699653d4eSeschrock 
335739c23413Seschrock 		if ((error = vdev_open(vd)) == 0 &&
3358fa94a07fSbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
3359fa94a07fSbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
336039c23413Seschrock 			    vd->vdev_guid) == 0);
336139c23413Seschrock 		}
336299653d4eSeschrock 
336399653d4eSeschrock 		vdev_free(vd);
336439c23413Seschrock 
3365fa94a07fSbrendan 		if (error &&
3366fa94a07fSbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
336739c23413Seschrock 			goto out;
336839c23413Seschrock 		else
336939c23413Seschrock 			error = 0;
337099653d4eSeschrock 	}
337199653d4eSeschrock 
337239c23413Seschrock out:
3373fa94a07fSbrendan 	sav->sav_pending = NULL;
3374fa94a07fSbrendan 	sav->sav_npending = 0;
337539c23413Seschrock 	return (error);
337699653d4eSeschrock }
337799653d4eSeschrock 
3378fa94a07fSbrendan static int
3379fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3380fa94a07fSbrendan {
3381fa94a07fSbrendan 	int error;
3382fa94a07fSbrendan 
3383e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3384e14bb325SJeff Bonwick 
3385fa94a07fSbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3386fa94a07fSbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3387fa94a07fSbrendan 	    VDEV_LABEL_SPARE)) != 0) {
3388fa94a07fSbrendan 		return (error);
3389fa94a07fSbrendan 	}
3390fa94a07fSbrendan 
3391fa94a07fSbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3392fa94a07fSbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3393fa94a07fSbrendan 	    VDEV_LABEL_L2CACHE));
3394fa94a07fSbrendan }
3395fa94a07fSbrendan 
3396fa94a07fSbrendan static void
3397fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3398fa94a07fSbrendan     const char *config)
3399fa94a07fSbrendan {
3400fa94a07fSbrendan 	int i;
3401fa94a07fSbrendan 
3402fa94a07fSbrendan 	if (sav->sav_config != NULL) {
3403fa94a07fSbrendan 		nvlist_t **olddevs;
3404fa94a07fSbrendan 		uint_t oldndevs;
3405fa94a07fSbrendan 		nvlist_t **newdevs;
3406fa94a07fSbrendan 
3407fa94a07fSbrendan 		/*
3408fa94a07fSbrendan 		 * Generate new dev list by concatentating with the
3409fa94a07fSbrendan 		 * current dev list.
3410fa94a07fSbrendan 		 */
3411fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3412fa94a07fSbrendan 		    &olddevs, &oldndevs) == 0);
3413fa94a07fSbrendan 
3414fa94a07fSbrendan 		newdevs = kmem_alloc(sizeof (void *) *
3415fa94a07fSbrendan 		    (ndevs + oldndevs), KM_SLEEP);
3416fa94a07fSbrendan 		for (i = 0; i < oldndevs; i++)
3417fa94a07fSbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3418fa94a07fSbrendan 			    KM_SLEEP) == 0);
3419fa94a07fSbrendan 		for (i = 0; i < ndevs; i++)
3420fa94a07fSbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3421fa94a07fSbrendan 			    KM_SLEEP) == 0);
3422fa94a07fSbrendan 
3423fa94a07fSbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
3424fa94a07fSbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
3425fa94a07fSbrendan 
3426fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3427fa94a07fSbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
3428fa94a07fSbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
3429fa94a07fSbrendan 			nvlist_free(newdevs[i]);
3430fa94a07fSbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3431fa94a07fSbrendan 	} else {
3432fa94a07fSbrendan 		/*
3433fa94a07fSbrendan 		 * Generate a new dev list.
3434fa94a07fSbrendan 		 */
3435fa94a07fSbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3436fa94a07fSbrendan 		    KM_SLEEP) == 0);
3437fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3438fa94a07fSbrendan 		    devs, ndevs) == 0);
3439fa94a07fSbrendan 	}
3440fa94a07fSbrendan }
3441fa94a07fSbrendan 
3442fa94a07fSbrendan /*
3443fa94a07fSbrendan  * Stop and drop level 2 ARC devices
3444fa94a07fSbrendan  */
3445fa94a07fSbrendan void
3446fa94a07fSbrendan spa_l2cache_drop(spa_t *spa)
3447fa94a07fSbrendan {
3448fa94a07fSbrendan 	vdev_t *vd;
3449fa94a07fSbrendan 	int i;
3450fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
3451fa94a07fSbrendan 
3452fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++) {
3453fa94a07fSbrendan 		uint64_t pool;
3454fa94a07fSbrendan 
3455fa94a07fSbrendan 		vd = sav->sav_vdevs[i];
3456fa94a07fSbrendan 		ASSERT(vd != NULL);
3457fa94a07fSbrendan 
34588ad4d6ddSJeff Bonwick 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
34598ad4d6ddSJeff Bonwick 		    pool != 0ULL && l2arc_vdev_present(vd))
3460fa94a07fSbrendan 			l2arc_remove_vdev(vd);
3461fa94a07fSbrendan 	}
3462fa94a07fSbrendan }
3463fa94a07fSbrendan 
3464fa9e4066Sahrens /*
3465fa9e4066Sahrens  * Pool Creation
3466fa9e4066Sahrens  */
3467fa9e4066Sahrens int
3468990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
34694445fffbSMatthew Ahrens     nvlist_t *zplprops)
3470fa9e4066Sahrens {
3471fa9e4066Sahrens 	spa_t *spa;
3472990b4856Slling 	char *altroot = NULL;
34730373e76bSbonwick 	vdev_t *rvd;
3474fa9e4066Sahrens 	dsl_pool_t *dp;
3475fa9e4066Sahrens 	dmu_tx_t *tx;
3476573ca77eSGeorge Wilson 	int error = 0;
3477fa9e4066Sahrens 	uint64_t txg = TXG_INITIAL;
3478fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
3479fa94a07fSbrendan 	uint_t nspares, nl2cache;
3480cde58dbcSMatthew Ahrens 	uint64_t version, obj;
3481ad135b5dSChristopher Siden 	boolean_t has_features;
3482fa9e4066Sahrens 
3483fa9e4066Sahrens 	/*
3484fa9e4066Sahrens 	 * If this pool already exists, return failure.
3485fa9e4066Sahrens 	 */
3486fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
3487fa9e4066Sahrens 	if (spa_lookup(pool) != NULL) {
3488fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
3489be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
3490fa9e4066Sahrens 	}
3491fa9e4066Sahrens 
3492fa9e4066Sahrens 	/*
3493fa9e4066Sahrens 	 * Allocate a new spa_t structure.
3494fa9e4066Sahrens 	 */
3495990b4856Slling 	(void) nvlist_lookup_string(props,
3496990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3497468c413aSTim Haley 	spa = spa_add(pool, NULL, altroot);
34988ad4d6ddSJeff Bonwick 	spa_activate(spa, spa_mode_global);
3499fa9e4066Sahrens 
3500990b4856Slling 	if (props && (error = spa_prop_validate(spa, props))) {
3501990b4856Slling 		spa_deactivate(spa);
3502990b4856Slling 		spa_remove(spa);
3503c5904d13Seschrock 		mutex_exit(&spa_namespace_lock);
3504990b4856Slling 		return (error);
3505990b4856Slling 	}
3506990b4856Slling 
3507ad135b5dSChristopher Siden 	has_features = B_FALSE;
3508ad135b5dSChristopher Siden 	for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3509ad135b5dSChristopher Siden 	    elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3510ad135b5dSChristopher Siden 		if (zpool_prop_feature(nvpair_name(elem)))
3511ad135b5dSChristopher Siden 			has_features = B_TRUE;
3512ad135b5dSChristopher Siden 	}
3513ad135b5dSChristopher Siden 
3514ad135b5dSChristopher Siden 	if (has_features || nvlist_lookup_uint64(props,
3515ad135b5dSChristopher Siden 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3516990b4856Slling 		version = SPA_VERSION;
3517ad135b5dSChristopher Siden 	}
3518ad135b5dSChristopher Siden 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3519b24ab676SJeff Bonwick 
3520b24ab676SJeff Bonwick 	spa->spa_first_txg = txg;
3521b24ab676SJeff Bonwick 	spa->spa_uberblock.ub_txg = txg - 1;
3522990b4856Slling 	spa->spa_uberblock.ub_version = version;
3523fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
3524fa9e4066Sahrens 
352554d692b7SGeorge Wilson 	/*
352654d692b7SGeorge Wilson 	 * Create "The Godfather" zio to hold all async IOs
352754d692b7SGeorge Wilson 	 */
35286f834bc1SMatthew Ahrens 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
35296f834bc1SMatthew Ahrens 	    KM_SLEEP);
35306f834bc1SMatthew Ahrens 	for (int i = 0; i < max_ncpus; i++) {
35316f834bc1SMatthew Ahrens 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
35326f834bc1SMatthew Ahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
35336f834bc1SMatthew Ahrens 		    ZIO_FLAG_GODFATHER);
35346f834bc1SMatthew Ahrens 	}
353554d692b7SGeorge Wilson 
35360373e76bSbonwick 	/*
35370373e76bSbonwick 	 * Create the root vdev.
35380373e76bSbonwick 	 */
3539e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
35400373e76bSbonwick 
354199653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
35420373e76bSbonwick 
354399653d4eSeschrock 	ASSERT(error != 0 || rvd != NULL);
354499653d4eSeschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
35450373e76bSbonwick 
3546b7b97454Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
3547be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
354899653d4eSeschrock 
354999653d4eSeschrock 	if (error == 0 &&
355099653d4eSeschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3551fa94a07fSbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
355299653d4eSeschrock 	    VDEV_ALLOC_ADD)) == 0) {
3553573ca77eSGeorge Wilson 		for (int c = 0; c < rvd->vdev_children; c++) {
3554573ca77eSGeorge Wilson 			vdev_metaslab_set_size(rvd->vdev_child[c]);
3555573ca77eSGeorge Wilson 			vdev_expand(rvd->vdev_child[c], txg);
3556573ca77eSGeorge Wilson 		}
35570373e76bSbonwick 	}
35580373e76bSbonwick 
3559e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
3560fa9e4066Sahrens 
356199653d4eSeschrock 	if (error != 0) {
3562fa9e4066Sahrens 		spa_unload(spa);
3563fa9e4066Sahrens 		spa_deactivate(spa);
3564fa9e4066Sahrens 		spa_remove(spa);
3565fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
3566fa9e4066Sahrens 		return (error);
3567fa9e4066Sahrens 	}
3568fa9e4066Sahrens 
356999653d4eSeschrock 	/*
357099653d4eSeschrock 	 * Get the list of spares, if specified.
357199653d4eSeschrock 	 */
357299653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
357399653d4eSeschrock 	    &spares, &nspares) == 0) {
3574fa94a07fSbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
357599653d4eSeschrock 		    KM_SLEEP) == 0);
3576fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
357799653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3578e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
357999653d4eSeschrock 		spa_load_spares(spa);
3580e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
3581fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
3582fa94a07fSbrendan 	}
3583fa94a07fSbrendan 
3584fa94a07fSbrendan 	/*
3585fa94a07fSbrendan 	 * Get the list of level 2 cache devices, if specified.
3586fa94a07fSbrendan 	 */
3587fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3588fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
3589fa94a07fSbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3590fa94a07fSbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
3591fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3592fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3593e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3594fa94a07fSbrendan 		spa_load_l2cache(spa);
3595e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
3596fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
359799653d4eSeschrock 	}
359899653d4eSeschrock 
3599ad135b5dSChristopher Siden 	spa->spa_is_initializing = B_TRUE;
36000a48a24eStimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3601fa9e4066Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
3602ad135b5dSChristopher Siden 	spa->spa_is_initializing = B_FALSE;
3603fa9e4066Sahrens 
3604485bbbf5SGeorge Wilson 	/*
3605485bbbf5SGeorge Wilson 	 * Create DDTs (dedup tables).
3606485bbbf5SGeorge Wilson 	 */
3607485bbbf5SGeorge Wilson 	ddt_create(spa);
3608485bbbf5SGeorge Wilson 
3609485bbbf5SGeorge Wilson 	spa_update_dspace(spa);
3610485bbbf5SGeorge Wilson 
3611fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
3612fa9e4066Sahrens 
3613fa9e4066Sahrens 	/*
3614fa9e4066Sahrens 	 * Create the pool config object.
3615fa9e4066Sahrens 	 */
3616fa9e4066Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3617f7991ba4STim Haley 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3618fa9e4066Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3619fa9e4066Sahrens 
3620ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
3621fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3622ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3623ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
3624ea8dc4b6Seschrock 	}
3625fa9e4066Sahrens 
3626ad135b5dSChristopher Siden 	if (spa_version(spa) >= SPA_VERSION_FEATURES)
3627ad135b5dSChristopher Siden 		spa_feature_create_zap_objects(spa, tx);
3628ad135b5dSChristopher Siden 
36293f9d6ad7SLin Ling 	if (zap_add(spa->spa_meta_objset,
36303f9d6ad7SLin Ling 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
36313f9d6ad7SLin Ling 	    sizeof (uint64_t), 1, &version, tx) != 0) {
36323f9d6ad7SLin Ling 		cmn_err(CE_PANIC, "failed to add pool version");
36333f9d6ad7SLin Ling 	}
36343f9d6ad7SLin Ling 
3635990b4856Slling 	/* Newly created pools with the right version are always deflated. */
3636990b4856Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3637990b4856Slling 		spa->spa_deflate = TRUE;
3638990b4856Slling 		if (zap_add(spa->spa_meta_objset,
3639990b4856Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3640990b4856Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3641990b4856Slling 			cmn_err(CE_PANIC, "failed to add deflate");
3642990b4856Slling 		}
364399653d4eSeschrock 	}
364499653d4eSeschrock 
3645fa9e4066Sahrens 	/*
3646cde58dbcSMatthew Ahrens 	 * Create the deferred-free bpobj.  Turn off compression
3647fa9e4066Sahrens 	 * because sync-to-convergence takes longer if the blocksize
3648fa9e4066Sahrens 	 * keeps changing.
3649fa9e4066Sahrens 	 */
3650cde58dbcSMatthew Ahrens 	obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3651cde58dbcSMatthew Ahrens 	dmu_object_set_compress(spa->spa_meta_objset, obj,
3652cde58dbcSMatthew Ahrens 	    ZIO_COMPRESS_OFF, tx);
3653ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
3654cde58dbcSMatthew Ahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3655cde58dbcSMatthew Ahrens 	    sizeof (uint64_t), 1, &obj, tx) != 0) {
3656cde58dbcSMatthew Ahrens 		cmn_err(CE_PANIC, "failed to add bpobj");
3657ea8dc4b6Seschrock 	}
3658b420f3adSRichard Lowe 	VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3659cde58dbcSMatthew Ahrens 	    spa->spa_meta_objset, obj));
3660fa9e4066Sahrens 
366106eeb2adSek 	/*
366206eeb2adSek 	 * Create the pool's history object.
366306eeb2adSek 	 */
3664990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
3665990b4856Slling 		spa_history_create_obj(spa, tx);
3666990b4856Slling 
3667990b4856Slling 	/*
3668990b4856Slling 	 * Set pool properties.
3669990b4856Slling 	 */
3670990b4856Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3671990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
36720a4e9518Sgw 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3673573ca77eSGeorge Wilson 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3674b24ab676SJeff Bonwick 
3675379c004dSEric Schrock 	if (props != NULL) {
3676379c004dSEric Schrock 		spa_configfile_set(spa, props, B_FALSE);
36773b2aab18SMatthew Ahrens 		spa_sync_props(props, tx);
3678379c004dSEric Schrock 	}
367906eeb2adSek 
3680fa9e4066Sahrens 	dmu_tx_commit(tx);
3681fa9e4066Sahrens 
3682fa9e4066Sahrens 	spa->spa_sync_on = B_TRUE;
3683fa9e4066Sahrens 	txg_sync_start(spa->spa_dsl_pool);
3684fa9e4066Sahrens 
3685fa9e4066Sahrens 	/*
3686fa9e4066Sahrens 	 * We explicitly wait for the first transaction to complete so that our
3687fa9e4066Sahrens 	 * bean counters are appropriately updated.
3688fa9e4066Sahrens 	 */
3689fa9e4066Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
3690fa9e4066Sahrens 
3691c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
3692*14372834SHans Rosenfeld 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3693fa9e4066Sahrens 
36944445fffbSMatthew Ahrens 	spa_history_log_version(spa, "create");
3695228975ccSek 
3696bc9014e6SJustin Gibbs 	/*
3697bc9014e6SJustin Gibbs 	 * Don't count references from objsets that are already closed
3698bc9014e6SJustin Gibbs 	 * and are making their way through the eviction process.
3699bc9014e6SJustin Gibbs 	 */
3700bc9014e6SJustin Gibbs 	spa_evicting_os_wait(spa);
3701088f3894Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
3702088f3894Sahrens 
3703daaa36a7SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
3704daaa36a7SGeorge Wilson 
3705fa9e4066Sahrens 	return (0);
3706fa9e4066Sahrens }
3707fa9e4066Sahrens 
3708e7cbe64fSgw #ifdef _KERNEL
3709e7cbe64fSgw /*
371021ecdf64SLin Ling  * Get the root pool information from the root disk, then import the root pool
371121ecdf64SLin Ling  * during the system boot up time.
3712e7cbe64fSgw  */
371321ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
371421ecdf64SLin Ling 
371521ecdf64SLin Ling static nvlist_t *
371621ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3717e7cbe64fSgw {
371821ecdf64SLin Ling 	nvlist_t *config;
3719e7cbe64fSgw 	nvlist_t *nvtop, *nvroot;
3720e7cbe64fSgw 	uint64_t pgid;
3721e7cbe64fSgw 
372221ecdf64SLin Ling 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
372321ecdf64SLin Ling 		return (NULL);
372421ecdf64SLin Ling 
3725e7cbe64fSgw 	/*
3726e7cbe64fSgw 	 * Add this top-level vdev to the child array.
3727e7cbe64fSgw 	 */
372821ecdf64SLin Ling 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
372921ecdf64SLin Ling 	    &nvtop) == 0);
373021ecdf64SLin Ling 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
373121ecdf64SLin Ling 	    &pgid) == 0);
373221ecdf64SLin Ling 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3733e7cbe64fSgw 
3734e7cbe64fSgw 	/*
3735e7cbe64fSgw 	 * Put this pool's top-level vdevs into a root vdev.
3736e7cbe64fSgw 	 */
3737e7cbe64fSgw 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
373821ecdf64SLin Ling 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
373921ecdf64SLin Ling 	    VDEV_TYPE_ROOT) == 0);
3740e7cbe64fSgw 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3741e7cbe64fSgw 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3742e7cbe64fSgw 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3743e7cbe64fSgw 	    &nvtop, 1) == 0);
3744e7cbe64fSgw 
3745e7cbe64fSgw 	/*
3746e7cbe64fSgw 	 * Replace the existing vdev_tree with the new root vdev in
3747e7cbe64fSgw 	 * this pool's configuration (remove the old, add the new).
3748e7cbe64fSgw 	 */
3749e7cbe64fSgw 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3750e7cbe64fSgw 	nvlist_free(nvroot);
375121ecdf64SLin Ling 	return (config);
3752e7cbe64fSgw }
3753e7cbe64fSgw 
3754e7cbe64fSgw /*
375521ecdf64SLin Ling  * Walk the vdev tree and see if we can find a device with "better"
375621ecdf64SLin Ling  * configuration. A configuration is "better" if the label on that
375721ecdf64SLin Ling  * device has a more recent txg.
3758051aabe6Staylor  */
375921ecdf64SLin Ling static void
376021ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3761051aabe6Staylor {
3762573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
376321ecdf64SLin Ling 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3764051aabe6Staylor 
376521ecdf64SLin Ling 	if (vd->vdev_ops->vdev_op_leaf) {
376621ecdf64SLin Ling 		nvlist_t *label;
376721ecdf64SLin Ling 		uint64_t label_txg;
3768051aabe6Staylor 
376921ecdf64SLin Ling 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
377021ecdf64SLin Ling 		    &label) != 0)
377121ecdf64SLin Ling 			return;
3772051aabe6Staylor 
377321ecdf64SLin Ling 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
377421ecdf64SLin Ling 		    &label_txg) == 0);
3775051aabe6Staylor 
377621ecdf64SLin Ling 		/*
377721ecdf64SLin Ling 		 * Do we have a better boot device?
377821ecdf64SLin Ling 		 */
377921ecdf64SLin Ling 		if (label_txg > *txg) {
378021ecdf64SLin Ling 			*txg = label_txg;
378121ecdf64SLin Ling 			*avd = vd;
3782051aabe6Staylor 		}
378321ecdf64SLin Ling 		nvlist_free(label);
3784051aabe6Staylor 	}
3785051aabe6Staylor }
3786051aabe6Staylor 
3787e7cbe64fSgw /*
3788e7cbe64fSgw  * Import a root pool.
3789e7cbe64fSgw  *
3790051aabe6Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
3791051aabe6Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3792051aabe6Staylor  * The GRUB "findroot" command will return the vdev we should boot.
3793e7cbe64fSgw  *
3794e7cbe64fSgw  * For Sparc, devpath_list consists the physpath name of the booting device
3795e7cbe64fSgw  * no matter the rootpool is a single device pool or a mirrored pool.
3796e7cbe64fSgw  * e.g.
3797e7cbe64fSgw  *	"/pci@1f,0/ide@d/disk@0,0:a"
3798e7cbe64fSgw  */
3799e7cbe64fSgw int
3800051aabe6Staylor spa_import_rootpool(char *devpath, char *devid)
3801e7cbe64fSgw {
380221ecdf64SLin Ling 	spa_t *spa;
380321ecdf64SLin Ling 	vdev_t *rvd, *bvd, *avd = NULL;
380421ecdf64SLin Ling 	nvlist_t *config, *nvtop;
380521ecdf64SLin Ling 	uint64_t guid, txg;
3806e7cbe64fSgw 	char *pname;
3807e7cbe64fSgw 	int error;
3808e7cbe64fSgw 
3809e7cbe64fSgw 	/*
381021ecdf64SLin Ling 	 * Read the label from the boot device and generate a configuration.
3811e7cbe64fSgw 	 */
3812dedec472SJack Meng 	config = spa_generate_rootconf(devpath, devid, &guid);
3813dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL)
3814dedec472SJack Meng 	if (config == NULL) {
3815dedec472SJack Meng 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
3816dedec472SJack Meng 			/* iscsi boot */
3817dedec472SJack Meng 			get_iscsi_bootpath_phy(devpath);
3818dedec472SJack Meng 			config = spa_generate_rootconf(devpath, devid, &guid);
3819dedec472SJack Meng 		}
3820dedec472SJack Meng 	}
3821dedec472SJack Meng #endif
3822dedec472SJack Meng 	if (config == NULL) {
3823ad135b5dSChristopher Siden 		cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
382421ecdf64SLin Ling 		    devpath);
3825be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
382621ecdf64SLin Ling 	}
3827e7cbe64fSgw 
382821ecdf64SLin Ling 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
382921ecdf64SLin Ling 	    &pname) == 0);
383021ecdf64SLin Ling 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3831e7cbe64fSgw 
38326809eb4eSEric Schrock 	mutex_enter(&spa_namespace_lock);
38336809eb4eSEric Schrock 	if ((spa = spa_lookup(pname)) != NULL) {
38346809eb4eSEric Schrock 		/*
38356809eb4eSEric Schrock 		 * Remove the existing root pool from the namespace so that we
38366809eb4eSEric Schrock 		 * can replace it with the correct config we just read in.
38376809eb4eSEric Schrock 		 */
38386809eb4eSEric Schrock 		spa_remove(spa);
38396809eb4eSEric Schrock 	}
38406809eb4eSEric Schrock 
3841468c413aSTim Haley 	spa = spa_add(pname, config, NULL);
38426809eb4eSEric Schrock 	spa->spa_is_root = B_TRUE;
38434b964adaSGeorge Wilson 	spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3844e7cbe64fSgw 
384521ecdf64SLin Ling 	/*
384621ecdf64SLin Ling 	 * Build up a vdev tree based on the boot device's label config.
384721ecdf64SLin Ling 	 */
384821ecdf64SLin Ling 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
384921ecdf64SLin Ling 	    &nvtop) == 0);
385021ecdf64SLin Ling 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
385121ecdf64SLin Ling 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
385221ecdf64SLin Ling 	    VDEV_ALLOC_ROOTPOOL);
385321ecdf64SLin Ling 	spa_config_exit(spa, SCL_ALL, FTAG);
385421ecdf64SLin Ling 	if (error) {
385521ecdf64SLin Ling 		mutex_exit(&spa_namespace_lock);
385621ecdf64SLin Ling 		nvlist_free(config);
385721ecdf64SLin Ling 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
385821ecdf64SLin Ling 		    pname);
385921ecdf64SLin Ling 		return (error);
386021ecdf64SLin Ling 	}
386121ecdf64SLin Ling 
386221ecdf64SLin Ling 	/*
386321ecdf64SLin Ling 	 * Get the boot vdev.
386421ecdf64SLin Ling 	 */
386521ecdf64SLin Ling 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
386621ecdf64SLin Ling 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
386721ecdf64SLin Ling 		    (u_longlong_t)guid);
3868be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
386921ecdf64SLin Ling 		goto out;
387021ecdf64SLin Ling 	}
3871e7cbe64fSgw 
387221ecdf64SLin Ling 	/*
387321ecdf64SLin Ling 	 * Determine if there is a better boot device.
387421ecdf64SLin Ling 	 */
387521ecdf64SLin Ling 	avd = bvd;
387621ecdf64SLin Ling 	spa_alt_rootvdev(rvd, &avd, &txg);
387721ecdf64SLin Ling 	if (avd != bvd) {
387821ecdf64SLin Ling 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
387921ecdf64SLin Ling 		    "try booting from '%s'", avd->vdev_path);
3880be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
388121ecdf64SLin Ling 		goto out;
388221ecdf64SLin Ling 	}
3883e7cbe64fSgw 
388421ecdf64SLin Ling 	/*
388521ecdf64SLin Ling 	 * If the boot device is part of a spare vdev then ensure that
388621ecdf64SLin Ling 	 * we're booting off the active spare.
388721ecdf64SLin Ling 	 */
388821ecdf64SLin Ling 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
388921ecdf64SLin Ling 	    !bvd->vdev_isspare) {
389021ecdf64SLin Ling 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
389121ecdf64SLin Ling 		    "try booting from '%s'",
3892cb04b873SMark J Musante 		    bvd->vdev_parent->
3893cb04b873SMark J Musante 		    vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3894be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
389521ecdf64SLin Ling 		goto out;
389621ecdf64SLin Ling 	}
389721ecdf64SLin Ling 
389821ecdf64SLin Ling 	error = 0;
389921ecdf64SLin Ling out:
390021ecdf64SLin Ling 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
390121ecdf64SLin Ling 	vdev_free(rvd);
390221ecdf64SLin Ling 	spa_config_exit(spa, SCL_ALL, FTAG);
390321ecdf64SLin Ling 	mutex_exit(&spa_namespace_lock);
390421ecdf64SLin Ling 
390521ecdf64SLin Ling 	nvlist_free(config);
3906e7cbe64fSgw 	return (error);
3907e7cbe64fSgw }
390821ecdf64SLin Ling 
3909e7cbe64fSgw #endif
3910e7cbe64fSgw 
39116809eb4eSEric Schrock /*
39126809eb4eSEric Schrock  * Import a non-root pool into the system.
39136809eb4eSEric Schrock  */
3914c5904d13Seschrock int
39154b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3916c5904d13Seschrock {
39176809eb4eSEric Schrock 	spa_t *spa;
39186809eb4eSEric Schrock 	char *altroot = NULL;
3919468c413aSTim Haley 	spa_load_state_t state = SPA_LOAD_IMPORT;
3920468c413aSTim Haley 	zpool_rewind_policy_t policy;
3921f9af39baSGeorge Wilson 	uint64_t mode = spa_mode_global;
3922f9af39baSGeorge Wilson 	uint64_t readonly = B_FALSE;
39236809eb4eSEric Schrock 	int error;
39246809eb4eSEric Schrock 	nvlist_t *nvroot;
39256809eb4eSEric Schrock 	nvlist_t **spares, **l2cache;
39266809eb4eSEric Schrock 	uint_t nspares, nl2cache;
39276809eb4eSEric Schrock 
39286809eb4eSEric Schrock 	/*
39296809eb4eSEric Schrock 	 * If a pool with this name exists, return failure.
39306809eb4eSEric Schrock 	 */
39316809eb4eSEric Schrock 	mutex_enter(&spa_namespace_lock);
39321195e687SMark J Musante 	if (spa_lookup(pool) != NULL) {
39336809eb4eSEric Schrock 		mutex_exit(&spa_namespace_lock);
3934be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
39356809eb4eSEric Schrock 	}
39366809eb4eSEric Schrock 
39376809eb4eSEric Schrock 	/*
39386809eb4eSEric Schrock 	 * Create and initialize the spa structure.
39396809eb4eSEric Schrock 	 */
39406809eb4eSEric Schrock 	(void) nvlist_lookup_string(props,
39416809eb4eSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3942f9af39baSGeorge Wilson 	(void) nvlist_lookup_uint64(props,
3943f9af39baSGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3944f9af39baSGeorge Wilson 	if (readonly)
3945f9af39baSGeorge Wilson 		mode = FREAD;
3946468c413aSTim Haley 	spa = spa_add(pool, config, altroot);
39474b964adaSGeorge Wilson 	spa->spa_import_flags = flags;
39484b964adaSGeorge Wilson 
39494b964adaSGeorge Wilson 	/*
39504b964adaSGeorge Wilson 	 * Verbatim import - Take a pool and insert it into the namespace
39514b964adaSGeorge Wilson 	 * as if it had been loaded at boot.
39524b964adaSGeorge Wilson 	 */
39534b964adaSGeorge Wilson 	if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
39544b964adaSGeorge Wilson 		if (props != NULL)
39554b964adaSGeorge Wilson 			spa_configfile_set(spa, props, B_FALSE);
39564b964adaSGeorge Wilson 
39574b964adaSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
3958*14372834SHans Rosenfeld 		spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
39594b964adaSGeorge Wilson 
39604b964adaSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
39614b964adaSGeorge Wilson 		return (0);
39624b964adaSGeorge Wilson 	}
39634b964adaSGeorge Wilson 
3964f9af39baSGeorge Wilson 	spa_activate(spa, mode);
39656809eb4eSEric Schrock 
396625f89ee2SJeff Bonwick 	/*
396725f89ee2SJeff Bonwick 	 * Don't start async tasks until we know everything is healthy.
396825f89ee2SJeff Bonwick 	 */
396925f89ee2SJeff Bonwick 	spa_async_suspend(spa);
397025f89ee2SJeff Bonwick 
39714b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
39724b964adaSGeorge Wilson 	if (policy.zrp_request & ZPOOL_DO_REWIND)
39734b964adaSGeorge Wilson 		state = SPA_LOAD_RECOVER;
39744b964adaSGeorge Wilson 
39756809eb4eSEric Schrock 	/*
39766809eb4eSEric Schrock 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
39776809eb4eSEric Schrock 	 * because the user-supplied config is actually the one to trust when
39786809eb4eSEric Schrock 	 * doing an import.
39796809eb4eSEric Schrock 	 */
3980468c413aSTim Haley 	if (state != SPA_LOAD_RECOVER)
3981468c413aSTim Haley 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
39824b964adaSGeorge Wilson 
3983468c413aSTim Haley 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3984c8ee1847SVictor Latushkin 	    policy.zrp_request);
3985468c413aSTim Haley 
3986468c413aSTim Haley 	/*
39874b964adaSGeorge Wilson 	 * Propagate anything learned while loading the pool and pass it
39884b964adaSGeorge Wilson 	 * back to caller (i.e. rewind info, missing devices, etc).
3989468c413aSTim Haley 	 */
39904b964adaSGeorge Wilson 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
39914b964adaSGeorge Wilson 	    spa->spa_load_info) == 0);
39926809eb4eSEric Schrock 
39936809eb4eSEric Schrock 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
39946809eb4eSEric Schrock 	/*
39956809eb4eSEric Schrock 	 * Toss any existing sparelist, as it doesn't have any validity
39966809eb4eSEric Schrock 	 * anymore, and conflicts with spa_has_spare().
39976809eb4eSEric Schrock 	 */
39986809eb4eSEric Schrock 	if (spa->spa_spares.sav_config) {
39996809eb4eSEric Schrock 		nvlist_free(spa->spa_spares.sav_config);
40006809eb4eSEric Schrock 		spa->spa_spares.sav_config = NULL;
40016809eb4eSEric Schrock 		spa_load_spares(spa);
40026809eb4eSEric Schrock 	}
40036809eb4eSEric Schrock 	if (spa->spa_l2cache.sav_config) {
40046809eb4eSEric Schrock 		nvlist_free(spa->spa_l2cache.sav_config);
40056809eb4eSEric Schrock 		spa->spa_l2cache.sav_config = NULL;
40066809eb4eSEric Schrock 		spa_load_l2cache(spa);
40076809eb4eSEric Schrock 	}
40086809eb4eSEric Schrock 
40096809eb4eSEric Schrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
40106809eb4eSEric Schrock 	    &nvroot) == 0);
40116809eb4eSEric Schrock 	if (error == 0)
40126809eb4eSEric Schrock 		error = spa_validate_aux(spa, nvroot, -1ULL,
40136809eb4eSEric Schrock 		    VDEV_ALLOC_SPARE);
40146809eb4eSEric Schrock 	if (error == 0)
40156809eb4eSEric Schrock 		error = spa_validate_aux(spa, nvroot, -1ULL,
40166809eb4eSEric Schrock 		    VDEV_ALLOC_L2CACHE);
40176809eb4eSEric Schrock 	spa_config_exit(spa, SCL_ALL, FTAG);
40186809eb4eSEric Schrock 
40196809eb4eSEric Schrock 	if (props != NULL)
40206809eb4eSEric Schrock 		spa_configfile_set(spa, props, B_FALSE);
40216809eb4eSEric Schrock 
40226809eb4eSEric Schrock 	if (error != 0 || (props && spa_writeable(spa) &&
40236809eb4eSEric Schrock 	    (error = spa_prop_set(spa, props)))) {
40246809eb4eSEric Schrock 		spa_unload(spa);
40256809eb4eSEric Schrock 		spa_deactivate(spa);
40266809eb4eSEric Schrock 		spa_remove(spa);
40276809eb4eSEric Schrock 		mutex_exit(&spa_namespace_lock);
40286809eb4eSEric Schrock 		return (error);
40296809eb4eSEric Schrock 	}
40306809eb4eSEric Schrock 
4031955ef359SLin Ling 	spa_async_resume(spa);
4032955ef359SLin Ling 
40336809eb4eSEric Schrock 	/*
40346809eb4eSEric Schrock 	 * Override any spares and level 2 cache devices as specified by
40356809eb4eSEric Schrock 	 * the user, as these may have correct device names/devids, etc.
40366809eb4eSEric Schrock 	 */
40376809eb4eSEric Schrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
40386809eb4eSEric Schrock 	    &spares, &nspares) == 0) {
40396809eb4eSEric Schrock 		if (spa->spa_spares.sav_config)
40406809eb4eSEric Schrock 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
40416809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
40426809eb4eSEric Schrock 		else
40436809eb4eSEric Schrock 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
40446809eb4eSEric Schrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
40456809eb4eSEric Schrock 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
40466809eb4eSEric Schrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
40476809eb4eSEric Schrock 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
40486809eb4eSEric Schrock 		spa_load_spares(spa);
40496809eb4eSEric Schrock 		spa_config_exit(spa, SCL_ALL, FTAG);
40506809eb4eSEric Schrock 		spa->spa_spares.sav_sync = B_TRUE;
40516809eb4eSEric Schrock 	}
40526809eb4eSEric Schrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
40536809eb4eSEric Schrock 	    &l2cache, &nl2cache) == 0) {
40546809eb4eSEric Schrock 		if (spa->spa_l2cache.sav_config)
40556809eb4eSEric Schrock 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
40566809eb4eSEric Schrock 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
40576809eb4eSEric Schrock 		else
40586809eb4eSEric Schrock 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
40596809eb4eSEric Schrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
40606809eb4eSEric Schrock 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
40616809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
40626809eb4eSEric Schrock 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
40636809eb4eSEric Schrock 		spa_load_l2cache(spa);
40646809eb4eSEric Schrock 		spa_config_exit(spa, SCL_ALL, FTAG);
40656809eb4eSEric Schrock 		spa->spa_l2cache.sav_sync = B_TRUE;
40666809eb4eSEric Schrock 	}
40676809eb4eSEric Schrock 
4068b693757aSEric Schrock 	/*
4069b693757aSEric Schrock 	 * Check for any removed devices.
4070b693757aSEric Schrock 	 */
4071b693757aSEric Schrock 	if (spa->spa_autoreplace) {
4072b693757aSEric Schrock 		spa_aux_check_removed(&spa->spa_spares);
4073b693757aSEric Schrock 		spa_aux_check_removed(&spa->spa_l2cache);
4074b693757aSEric Schrock 	}
4075b693757aSEric Schrock 
40766809eb4eSEric Schrock 	if (spa_writeable(spa)) {
40776809eb4eSEric Schrock 		/*
40786809eb4eSEric Schrock 		 * Update the config cache to include the newly-imported pool.
40796809eb4eSEric Schrock 		 */
4080bc758434SLin Ling 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
40816809eb4eSEric Schrock 	}
40826809eb4eSEric Schrock 
4083573ca77eSGeorge Wilson 	/*
4084573ca77eSGeorge Wilson 	 * It's possible that the pool was expanded while it was exported.
4085573ca77eSGeorge Wilson 	 * We kick off an async task to handle this for us.
4086573ca77eSGeorge Wilson 	 */
4087573ca77eSGeorge Wilson 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4088573ca77eSGeorge Wilson 
40894445fffbSMatthew Ahrens 	spa_history_log_version(spa, "import");
40906809eb4eSEric Schrock 
4091*14372834SHans Rosenfeld 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4092*14372834SHans Rosenfeld 
4093*14372834SHans Rosenfeld 	mutex_exit(&spa_namespace_lock);
4094*14372834SHans Rosenfeld 
40956809eb4eSEric Schrock 	return (0);
4096c5904d13Seschrock }
4097c5904d13Seschrock 
4098fa9e4066Sahrens nvlist_t *
4099fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig)
4100fa9e4066Sahrens {
4101fa9e4066Sahrens 	nvlist_t *config = NULL;
4102fa9e4066Sahrens 	char *poolname;
4103fa9e4066Sahrens 	spa_t *spa;
4104fa9e4066Sahrens 	uint64_t state;
41057b7154beSLin Ling 	int error;
4106fa9e4066Sahrens 
4107fa9e4066Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4108fa9e4066Sahrens 		return (NULL);
4109fa9e4066Sahrens 
4110fa9e4066Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4111fa9e4066Sahrens 		return (NULL);
4112fa9e4066Sahrens 
4113fa9e4066Sahrens 	/*
41140373e76bSbonwick 	 * Create and initialize the spa structure.
4115fa9e4066Sahrens 	 */
41160373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
4117468c413aSTim Haley 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
41188ad4d6ddSJeff Bonwick 	spa_activate(spa, FREAD);
4119fa9e4066Sahrens 
4120fa9e4066Sahrens 	/*
41210373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
4122ecc2d604Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
4123ecc2d604Sbonwick 	 * is actually the one to trust when doing an import.
4124fa9e4066Sahrens 	 */
41251195e687SMark J Musante 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4126fa9e4066Sahrens 
4127fa9e4066Sahrens 	/*
4128fa9e4066Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
4129fa9e4066Sahrens 	 */
4130fa9e4066Sahrens 	if (spa->spa_root_vdev != NULL) {
4131fa9e4066Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4132fa9e4066Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4133fa9e4066Sahrens 		    poolname) == 0);
4134fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4135fa9e4066Sahrens 		    state) == 0);
413695173954Sek 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
413795173954Sek 		    spa->spa_uberblock.ub_timestamp) == 0);
4138ad135b5dSChristopher Siden 		VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4139ad135b5dSChristopher Siden 		    spa->spa_load_info) == 0);
414099653d4eSeschrock 
4141e7cbe64fSgw 		/*
4142e7cbe64fSgw 		 * If the bootfs property exists on this pool then we
4143e7cbe64fSgw 		 * copy it out so that external consumers can tell which
4144e7cbe64fSgw 		 * pools are bootable.
4145e7cbe64fSgw 		 */
41467b7154beSLin Ling 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
4147e7cbe64fSgw 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4148e7cbe64fSgw 
4149e7cbe64fSgw 			/*
4150e7cbe64fSgw 			 * We have to play games with the name since the
4151e7cbe64fSgw 			 * pool was opened as TRYIMPORT_NAME.
4152e7cbe64fSgw 			 */
4153e14bb325SJeff Bonwick 			if (dsl_dsobj_to_dsname(spa_name(spa),
4154e7cbe64fSgw 			    spa->spa_bootfs, tmpname) == 0) {
4155e7cbe64fSgw 				char *cp;
4156e7cbe64fSgw 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4157e7cbe64fSgw 
4158e7cbe64fSgw 				cp = strchr(tmpname, '/');
4159e7cbe64fSgw 				if (cp == NULL) {
4160e7cbe64fSgw 					(void) strlcpy(dsname, tmpname,
4161e7cbe64fSgw 					    MAXPATHLEN);
4162e7cbe64fSgw 				} else {
4163e7cbe64fSgw 					(void) snprintf(dsname, MAXPATHLEN,
4164e7cbe64fSgw 					    "%s/%s", poolname, ++cp);
4165e7cbe64fSgw 				}
4166e7cbe64fSgw 				VERIFY(nvlist_add_string(config,
4167e7cbe64fSgw 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4168e7cbe64fSgw 				kmem_free(dsname, MAXPATHLEN);
4169e7cbe64fSgw 			}
4170e7cbe64fSgw 			kmem_free(tmpname, MAXPATHLEN);
4171e7cbe64fSgw 		}
4172e7cbe64fSgw 
417399653d4eSeschrock 		/*
4174fa94a07fSbrendan 		 * Add the list of hot spares and level 2 cache devices.
417599653d4eSeschrock 		 */
41766809eb4eSEric Schrock 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
417799653d4eSeschrock 		spa_add_spares(spa, config);
4178fa94a07fSbrendan 		spa_add_l2cache(spa, config);
41796809eb4eSEric Schrock 		spa_config_exit(spa, SCL_CONFIG, FTAG);
4180fa9e4066Sahrens 	}
4181fa9e4066Sahrens 
4182fa9e4066Sahrens 	spa_unload(spa);
4183fa9e4066Sahrens 	spa_deactivate(spa);
4184fa9e4066Sahrens 	spa_remove(spa);
4185fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
4186fa9e4066Sahrens 
4187fa9e4066Sahrens 	return (config);
4188fa9e4066Sahrens }
4189fa9e4066Sahrens 
4190fa9e4066Sahrens /*
4191fa9e4066Sahrens  * Pool export/destroy
4192fa9e4066Sahrens  *
4193fa9e4066Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
4194fa9e4066Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
4195fa9e4066Sahrens  * update the pool state and sync all the labels to disk, removing the
4196394ab0cbSGeorge Wilson  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4197394ab0cbSGeorge Wilson  * we don't sync the labels or remove the configuration cache.
4198fa9e4066Sahrens  */
4199fa9e4066Sahrens static int
420089a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4201394ab0cbSGeorge Wilson     boolean_t force, boolean_t hardforce)
4202fa9e4066Sahrens {
4203fa9e4066Sahrens 	spa_t *spa;
4204fa9e4066Sahrens 
420544cd46caSbillm 	if (oldconfig)
420644cd46caSbillm 		*oldconfig = NULL;
420744cd46caSbillm 
42088ad4d6ddSJeff Bonwick 	if (!(spa_mode_global & FWRITE))
4209be6fd75aSMatthew Ahrens 		return (SET_ERROR(EROFS));
4210fa9e4066Sahrens 
4211fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
4212fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
4213fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
4214be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
4215fa9e4066Sahrens 	}
4216fa9e4066Sahrens 
4217ea8dc4b6Seschrock 	/*
4218ea8dc4b6Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4219ea8dc4b6Seschrock 	 * reacquire the namespace lock, and see if we can export.
4220ea8dc4b6Seschrock 	 */
4221ea8dc4b6Seschrock 	spa_open_ref(spa, FTAG);
4222ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
4223ea8dc4b6Seschrock 	spa_async_suspend(spa);
4224ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
4225ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4226ea8dc4b6Seschrock 
4227fa9e4066Sahrens 	/*
4228fa9e4066Sahrens 	 * The pool will be in core if it's openable,
4229fa9e4066Sahrens 	 * in which case we can modify its state.
4230fa9e4066Sahrens 	 */
4231fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4232fa9e4066Sahrens 		/*
4233fa9e4066Sahrens 		 * Objsets may be open only because they're dirty, so we
4234fa9e4066Sahrens 		 * have to force it to sync before checking spa_refcnt.
4235fa9e4066Sahrens 		 */
4236fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
4237bc9014e6SJustin Gibbs 		spa_evicting_os_wait(spa);
4238fa9e4066Sahrens 
4239ea8dc4b6Seschrock 		/*
4240ea8dc4b6Seschrock 		 * A pool cannot be exported or destroyed if there are active
4241ea8dc4b6Seschrock 		 * references.  If we are resetting a pool, allow references by
4242ea8dc4b6Seschrock 		 * fault injection handlers.
4243ea8dc4b6Seschrock 		 */
4244ea8dc4b6Seschrock 		if (!spa_refcount_zero(spa) ||
4245ea8dc4b6Seschrock 		    (spa->spa_inject_ref != 0 &&
4246ea8dc4b6Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
4247ea8dc4b6Seschrock 			spa_async_resume(spa);
4248fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
4249be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
4250fa9e4066Sahrens 		}
4251fa9e4066Sahrens 
425289a89ebfSlling 		/*
425389a89ebfSlling 		 * A pool cannot be exported if it has an active shared spare.
425489a89ebfSlling 		 * This is to prevent other pools stealing the active spare
425589a89ebfSlling 		 * from an exported pool. At user's own will, such pool can
425689a89ebfSlling 		 * be forcedly exported.
425789a89ebfSlling 		 */
425889a89ebfSlling 		if (!force && new_state == POOL_STATE_EXPORTED &&
425989a89ebfSlling 		    spa_has_active_shared_spare(spa)) {
426089a89ebfSlling 			spa_async_resume(spa);
426189a89ebfSlling 			mutex_exit(&spa_namespace_lock);
4262be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
426389a89ebfSlling 		}
426489a89ebfSlling 
4265fa9e4066Sahrens 		/*
4266fa9e4066Sahrens 		 * We want this to be reflected on every label,
4267fa9e4066Sahrens 		 * so mark them all dirty.  spa_unload() will do the
4268fa9e4066Sahrens 		 * final sync that pushes these changes out.
4269fa9e4066Sahrens 		 */
4270394ab0cbSGeorge Wilson 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4271e14bb325SJeff Bonwick 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4272ea8dc4b6Seschrock 			spa->spa_state = new_state;
42733f9d6ad7SLin Ling 			spa->spa_final_txg = spa_last_synced_txg(spa) +
42743f9d6ad7SLin Ling 			    TXG_DEFER_SIZE + 1;
4275ea8dc4b6Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
4276e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_ALL, FTAG);
4277ea8dc4b6Seschrock 		}
4278fa9e4066Sahrens 	}
4279fa9e4066Sahrens 
42803d7072f8Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
42813d7072f8Seschrock 
4282fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4283fa9e4066Sahrens 		spa_unload(spa);
4284fa9e4066Sahrens 		spa_deactivate(spa);
4285fa9e4066Sahrens 	}
4286fa9e4066Sahrens 
428744cd46caSbillm 	if (oldconfig && spa->spa_config)
428844cd46caSbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
428944cd46caSbillm 
4290ea8dc4b6Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
4291394ab0cbSGeorge Wilson 		if (!hardforce)
4292394ab0cbSGeorge Wilson 			spa_config_sync(spa, B_TRUE, B_TRUE);
4293ea8dc4b6Seschrock 		spa_remove(spa);
4294ea8dc4b6Seschrock 	}
4295fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
4296fa9e4066Sahrens 
4297fa9e4066Sahrens 	return (0);
4298fa9e4066Sahrens }
4299fa9e4066Sahrens 
4300fa9e4066Sahrens /*
4301fa9e4066Sahrens  * Destroy a storage pool.
4302fa9e4066Sahrens  */
4303fa9e4066Sahrens int
4304fa9e4066Sahrens spa_destroy(char *pool)
4305fa9e4066Sahrens {
4306394ab0cbSGeorge Wilson 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4307394ab0cbSGeorge Wilson 	    B_FALSE, B_FALSE));
4308fa9e4066Sahrens }
4309fa9e4066Sahrens 
4310fa9e4066Sahrens /*
4311fa9e4066Sahrens  * Export a storage pool.
4312fa9e4066Sahrens  */
4313fa9e4066Sahrens int
4314394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4315394ab0cbSGeorge Wilson     boolean_t hardforce)
4316fa9e4066Sahrens {
4317394ab0cbSGeorge Wilson 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4318394ab0cbSGeorge Wilson 	    force, hardforce));
4319fa9e4066Sahrens }
4320fa9e4066Sahrens 
4321ea8dc4b6Seschrock /*
4322ea8dc4b6Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
4323ea8dc4b6Seschrock  * from the namespace in any way.
4324ea8dc4b6Seschrock  */
4325ea8dc4b6Seschrock int
4326ea8dc4b6Seschrock spa_reset(char *pool)
4327ea8dc4b6Seschrock {
432889a89ebfSlling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4329394ab0cbSGeorge Wilson 	    B_FALSE, B_FALSE));
4330ea8dc4b6Seschrock }
4331ea8dc4b6Seschrock 
4332fa9e4066Sahrens /*
4333fa9e4066Sahrens  * ==========================================================================
4334fa9e4066Sahrens  * Device manipulation
4335fa9e4066Sahrens  * ==========================================================================
4336fa9e4066Sahrens  */
4337fa9e4066Sahrens 
4338fa9e4066Sahrens /*
43398654d025Sperrin  * Add a device to a storage pool.
4340fa9e4066Sahrens  */
4341fa9e4066Sahrens int
4342fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4343fa9e4066Sahrens {
434488ecc943SGeorge Wilson 	uint64_t txg, id;
43458ad4d6ddSJeff Bonwick 	int error;
4346fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
43470e34b6a7Sbonwick 	vdev_t *vd, *tvd;
4348fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
4349fa94a07fSbrendan 	uint_t nspares, nl2cache;
4350fa9e4066Sahrens 
4351f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
4352f9af39baSGeorge Wilson 
4353fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
4354fa9e4066Sahrens 
435599653d4eSeschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
435699653d4eSeschrock 	    VDEV_ALLOC_ADD)) != 0)
435799653d4eSeschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
4358fa9e4066Sahrens 
4359e14bb325SJeff Bonwick 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
436099653d4eSeschrock 
4361fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4362fa94a07fSbrendan 	    &nspares) != 0)
436399653d4eSeschrock 		nspares = 0;
436499653d4eSeschrock 
4365fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4366fa94a07fSbrendan 	    &nl2cache) != 0)
4367fa94a07fSbrendan 		nl2cache = 0;
4368fa94a07fSbrendan 
4369e14bb325SJeff Bonwick 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4370fa9e4066Sahrens 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
4371fa9e4066Sahrens 
4372e14bb325SJeff Bonwick 	if (vd->vdev_children != 0 &&
4373e14bb325SJeff Bonwick 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
4374e14bb325SJeff Bonwick 		return (spa_vdev_exit(spa, vd, txg, error));
437599653d4eSeschrock 
437639c23413Seschrock 	/*
4377fa94a07fSbrendan 	 * We must validate the spares and l2cache devices after checking the
4378fa94a07fSbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
437939c23413Seschrock 	 */
4380e14bb325SJeff Bonwick 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
438139c23413Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
438239c23413Seschrock 
438339c23413Seschrock 	/*
438439c23413Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
438539c23413Seschrock 	 */
43868ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++) {
438788ecc943SGeorge Wilson 
438888ecc943SGeorge Wilson 		/*
438988ecc943SGeorge Wilson 		 * Set the vdev id to the first hole, if one exists.
439088ecc943SGeorge Wilson 		 */
439188ecc943SGeorge Wilson 		for (id = 0; id < rvd->vdev_children; id++) {
439288ecc943SGeorge Wilson 			if (rvd->vdev_child[id]->vdev_ishole) {
439388ecc943SGeorge Wilson 				vdev_free(rvd->vdev_child[id]);
439488ecc943SGeorge Wilson 				break;
439588ecc943SGeorge Wilson 			}
439688ecc943SGeorge Wilson 		}
439739c23413Seschrock 		tvd = vd->vdev_child[c];
439839c23413Seschrock 		vdev_remove_child(vd, tvd);
439988ecc943SGeorge Wilson 		tvd->vdev_id = id;
440039c23413Seschrock 		vdev_add_child(rvd, tvd);
440139c23413Seschrock 		vdev_config_dirty(tvd);
440239c23413Seschrock 	}
440339c23413Seschrock 
440499653d4eSeschrock 	if (nspares != 0) {
4405fa94a07fSbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4406fa94a07fSbrendan 		    ZPOOL_CONFIG_SPARES);
440799653d4eSeschrock 		spa_load_spares(spa);
4408fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
4409fa94a07fSbrendan 	}
4410fa94a07fSbrendan 
4411fa94a07fSbrendan 	if (nl2cache != 0) {
4412fa94a07fSbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4413fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE);
4414fa94a07fSbrendan 		spa_load_l2cache(spa);
4415fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
4416fa9e4066Sahrens 	}
4417fa9e4066Sahrens 
4418fa9e4066Sahrens 	/*
44190e34b6a7Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
44200e34b6a7Sbonwick 	 * If other threads start allocating from these vdevs before we
44210e34b6a7Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
44220e34b6a7Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
44230e34b6a7Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
44240e34b6a7Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
44250373e76bSbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
44260e34b6a7Sbonwick 	 *
44270e34b6a7Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
44280e34b6a7Sbonwick 	 * if we lose power at any point in this sequence, the remaining
44290e34b6a7Sbonwick 	 * steps will be completed the next time we load the pool.
44300e34b6a7Sbonwick 	 */
44310373e76bSbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
44320e34b6a7Sbonwick 
44330373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
44340373e76bSbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4435*14372834SHans Rosenfeld 	spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
44360373e76bSbonwick 	mutex_exit(&spa_namespace_lock);
4437fa9e4066Sahrens 
44380373e76bSbonwick 	return (0);
4439fa9e4066Sahrens }
4440fa9e4066Sahrens 
4441fa9e4066Sahrens /*
4442fa9e4066Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
4443fa9e4066Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
4444fa9e4066Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
4445fa9e4066Sahrens  *
4446fa9e4066Sahrens  * If 'replacing' is specified, the new device is intended to replace the
4447fa9e4066Sahrens  * existing device; in this case the two devices are made into their own
44483d7072f8Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
4449fa9e4066Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
4450fa9e4066Sahrens  * extra rules: you can't attach to it after it's been created, and upon
4451fa9e4066Sahrens  * completion of resilvering, the first disk (the one being replaced)
4452fa9e4066Sahrens  * is automatically detached.
4453fa9e4066Sahrens  */
4454fa9e4066Sahrens int
4455ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4456fa9e4066Sahrens {
44573f9d6ad7SLin Ling 	uint64_t txg, dtl_max_txg;
4458fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
4459fa9e4066Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
446099653d4eSeschrock 	vdev_ops_t *pvops;
44619b3f6b42SEric Kustarz 	char *oldvdpath, *newvdpath;
44629b3f6b42SEric Kustarz 	int newvd_isspare;
44639b3f6b42SEric Kustarz 	int error;
4464fa9e4066Sahrens 
4465f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
4466f9af39baSGeorge Wilson 
4467fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
4468fa9e4066Sahrens 
4469c5904d13Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4470fa9e4066Sahrens 
4471fa9e4066Sahrens 	if (oldvd == NULL)
4472fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4473fa9e4066Sahrens 
44740e34b6a7Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
44750e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
44760e34b6a7Sbonwick 
4477fa9e4066Sahrens 	pvd = oldvd->vdev_parent;
4478fa9e4066Sahrens 
447999653d4eSeschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4480cd0837ccSGeorge Wilson 	    VDEV_ALLOC_ATTACH)) != 0)
44813d7072f8Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
44823d7072f8Seschrock 
44833d7072f8Seschrock 	if (newrootvd->vdev_children != 1)
4484fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4485fa9e4066Sahrens 
4486fa9e4066Sahrens 	newvd = newrootvd->vdev_child[0];
4487fa9e4066Sahrens 
4488fa9e4066Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
4489fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4490fa9e4066Sahrens 
449199653d4eSeschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4492fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
4493fa9e4066Sahrens 
44948654d025Sperrin 	/*
44958654d025Sperrin 	 * Spares can't replace logs
44968654d025Sperrin 	 */
4497ee0eb9f2SEric Schrock 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
44988654d025Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
44998654d025Sperrin 
450099653d4eSeschrock 	if (!replacing) {
450199653d4eSeschrock 		/*
450299653d4eSeschrock 		 * For attach, the only allowable parent is a mirror or the root
450399653d4eSeschrock 		 * vdev.
450499653d4eSeschrock 		 */
450599653d4eSeschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
450699653d4eSeschrock 		    pvd->vdev_ops != &vdev_root_ops)
450799653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
450899653d4eSeschrock 
450999653d4eSeschrock 		pvops = &vdev_mirror_ops;
451099653d4eSeschrock 	} else {
451199653d4eSeschrock 		/*
451299653d4eSeschrock 		 * Active hot spares can only be replaced by inactive hot
451399653d4eSeschrock 		 * spares.
451499653d4eSeschrock 		 */
451599653d4eSeschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
4516cb04b873SMark J Musante 		    oldvd->vdev_isspare &&
451799653d4eSeschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
451899653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
451999653d4eSeschrock 
452099653d4eSeschrock 		/*
452199653d4eSeschrock 		 * If the source is a hot spare, and the parent isn't already a
452299653d4eSeschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
452339c23413Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
452439c23413Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
452539c23413Seschrock 		 * the same (spare replaces spare, non-spare replaces
452639c23413Seschrock 		 * non-spare).
452799653d4eSeschrock 		 */
4528cb04b873SMark J Musante 		if (pvd->vdev_ops == &vdev_replacing_ops &&
4529cb04b873SMark J Musante 		    spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
453099653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4531cb04b873SMark J Musante 		} else if (pvd->vdev_ops == &vdev_spare_ops &&
4532cb04b873SMark J Musante 		    newvd->vdev_isspare != oldvd->vdev_isspare) {
453339c23413Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4534cb04b873SMark J Musante 		}
4535cb04b873SMark J Musante 
4536cb04b873SMark J Musante 		if (newvd->vdev_isspare)
453799653d4eSeschrock 			pvops = &vdev_spare_ops;
453899653d4eSeschrock 		else
453999653d4eSeschrock 			pvops = &vdev_replacing_ops;
454099653d4eSeschrock 	}
454199653d4eSeschrock 
45422a79c5feSlling 	/*
4543573ca77eSGeorge Wilson 	 * Make sure the new device is big enough.
45442a79c5feSlling 	 */
4545573ca77eSGeorge Wilson 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4546fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4547fa9e4066Sahrens 
4548ecc2d604Sbonwick 	/*
4549ecc2d604Sbonwick 	 * The new device cannot have a higher alignment requirement
4550ecc2d604Sbonwick 	 * than the top-level vdev.
4551ecc2d604Sbonwick 	 */
4552ecc2d604Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4553fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4554fa9e4066Sahrens 
4555fa9e4066Sahrens 	/*
4556fa9e4066Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
4557fa9e4066Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
4558fa9e4066Sahrens 	 */
4559fa9e4066Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4560fa9e4066Sahrens 		spa_strfree(oldvd->vdev_path);
4561fa9e4066Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4562fa9e4066Sahrens 		    KM_SLEEP);
4563fa9e4066Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
4564fa9e4066Sahrens 		    newvd->vdev_path, "old");
4565fa9e4066Sahrens 		if (oldvd->vdev_devid != NULL) {
4566fa9e4066Sahrens 			spa_strfree(oldvd->vdev_devid);
4567fa9e4066Sahrens 			oldvd->vdev_devid = NULL;
4568fa9e4066Sahrens 		}
4569fa9e4066Sahrens 	}
4570fa9e4066Sahrens 
4571cb04b873SMark J Musante 	/* mark the device being resilvered */
4572b4952e17SGeorge Wilson 	newvd->vdev_resilver_txg = txg;
4573cb04b873SMark J Musante 
4574fa9e4066Sahrens 	/*
457599653d4eSeschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
457699653d4eSeschrock 	 * mirror/replacing/spare vdev above oldvd.
4577fa9e4066Sahrens 	 */
4578fa9e4066Sahrens 	if (pvd->vdev_ops != pvops)
4579fa9e4066Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
4580fa9e4066Sahrens 
4581fa9e4066Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
4582fa9e4066Sahrens 	ASSERT(pvd->vdev_ops == pvops);
4583fa9e4066Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
4584fa9e4066Sahrens 
4585fa9e4066Sahrens 	/*
4586fa9e4066Sahrens 	 * Extract the new device from its root and add it to pvd.
4587fa9e4066Sahrens 	 */
4588fa9e4066Sahrens 	vdev_remove_child(newrootvd, newvd);
4589fa9e4066Sahrens 	newvd->vdev_id = pvd->vdev_children;
459088ecc943SGeorge Wilson 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
4591fa9e4066Sahrens 	vdev_add_child(pvd, newvd);
4592fa9e4066Sahrens 
4593fa9e4066Sahrens 	tvd = newvd->vdev_top;
4594fa9e4066Sahrens 	ASSERT(pvd->vdev_top == tvd);
4595fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
4596fa9e4066Sahrens 
4597fa9e4066Sahrens 	vdev_config_dirty(tvd);
4598fa9e4066Sahrens 
4599fa9e4066Sahrens 	/*
46003f9d6ad7SLin Ling 	 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
46013f9d6ad7SLin Ling 	 * for any dmu_sync-ed blocks.  It will propagate upward when
46023f9d6ad7SLin Ling 	 * spa_vdev_exit() calls vdev_dtl_reassess().
4603fa9e4066Sahrens 	 */
46043f9d6ad7SLin Ling 	dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4605fa9e4066Sahrens 
46063f9d6ad7SLin Ling 	vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
46073f9d6ad7SLin Ling 	    dtl_max_txg - TXG_INITIAL);
4608fa9e4066Sahrens 
46096809eb4eSEric Schrock 	if (newvd->vdev_isspare) {
461039c23413Seschrock 		spa_spare_activate(newvd);
46116809eb4eSEric Schrock 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
46126809eb4eSEric Schrock 	}
46136809eb4eSEric Schrock 
4614e14bb325SJeff Bonwick 	oldvdpath = spa_strdup(oldvd->vdev_path);
4615e14bb325SJeff Bonwick 	newvdpath = spa_strdup(newvd->vdev_path);
46169b3f6b42SEric Kustarz 	newvd_isspare = newvd->vdev_isspare;
4617ea8dc4b6Seschrock 
4618fa9e4066Sahrens 	/*
4619fa9e4066Sahrens 	 * Mark newvd's DTL dirty in this txg.
4620fa9e4066Sahrens 	 */
4621ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
4622fa9e4066Sahrens 
46233f9d6ad7SLin Ling 	/*
46240713e232SGeorge Wilson 	 * Schedule the resilver to restart in the future. We do this to
46250713e232SGeorge Wilson 	 * ensure that dmu_sync-ed blocks have been stitched into the
46260713e232SGeorge Wilson 	 * respective datasets.
46273f9d6ad7SLin Ling 	 */
46283f9d6ad7SLin Ling 	dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
46293f9d6ad7SLin Ling 
4630*14372834SHans Rosenfeld 	if (spa->spa_bootfs)
4631*14372834SHans Rosenfeld 		spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4632*14372834SHans Rosenfeld 
4633*14372834SHans Rosenfeld 	spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
4634*14372834SHans Rosenfeld 
46353f9d6ad7SLin Ling 	/*
46363f9d6ad7SLin Ling 	 * Commit the config
46373f9d6ad7SLin Ling 	 */
46383f9d6ad7SLin Ling 	(void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4639fa9e4066Sahrens 
46404445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "vdev attach", NULL,
46413f9d6ad7SLin Ling 	    "%s vdev=%s %s vdev=%s",
4642c8e1f6d2SMark J Musante 	    replacing && newvd_isspare ? "spare in" :
4643c8e1f6d2SMark J Musante 	    replacing ? "replace" : "attach", newvdpath,
4644c8e1f6d2SMark J Musante 	    replacing ? "for" : "to", oldvdpath);
46459b3f6b42SEric Kustarz 
46469b3f6b42SEric Kustarz 	spa_strfree(oldvdpath);
46479b3f6b42SEric Kustarz 	spa_strfree(newvdpath);
46489b3f6b42SEric Kustarz 
4649fa9e4066Sahrens 	return (0);
4650fa9e4066Sahrens }
4651fa9e4066Sahrens 
4652fa9e4066Sahrens /*
4653fa9e4066Sahrens  * Detach a device from a mirror or replacing vdev.
4654f7170741SWill Andrews  *
4655fa9e4066Sahrens  * If 'replace_done' is specified, only detach if the parent
4656fa9e4066Sahrens  * is a replacing vdev.
4657fa9e4066Sahrens  */
4658fa9e4066Sahrens int
46598ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4660fa9e4066Sahrens {
4661fa9e4066Sahrens 	uint64_t txg;
46628ad4d6ddSJeff Bonwick 	int error;
4663fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
4664fa9e4066Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
466599653d4eSeschrock 	boolean_t unspare = B_FALSE;
4666d5285caeSGeorge Wilson 	uint64_t unspare_guid = 0;
46671195e687SMark J Musante 	char *vdpath;
4668fa9e4066Sahrens 
4669f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
4670f9af39baSGeorge Wilson 
4671fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
4672fa9e4066Sahrens 
4673c5904d13Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4674fa9e4066Sahrens 
4675fa9e4066Sahrens 	if (vd == NULL)
4676fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4677fa9e4066Sahrens 
46780e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
46790e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
46800e34b6a7Sbonwick 
4681fa9e4066Sahrens 	pvd = vd->vdev_parent;
4682fa9e4066Sahrens 
46838ad4d6ddSJeff Bonwick 	/*
46848ad4d6ddSJeff Bonwick 	 * If the parent/child relationship is not as expected, don't do it.
46858ad4d6ddSJeff Bonwick 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
46868ad4d6ddSJeff Bonwick 	 * vdev that's replacing B with C.  The user's intent in replacing
46878ad4d6ddSJeff Bonwick 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
46888ad4d6ddSJeff Bonwick 	 * the replace by detaching C, the expected behavior is to end up
46898ad4d6ddSJeff Bonwick 	 * M(A,B).  But suppose that right after deciding to detach C,
46908ad4d6ddSJeff Bonwick 	 * the replacement of B completes.  We would have M(A,C), and then
46918ad4d6ddSJeff Bonwick 	 * ask to detach C, which would leave us with just A -- not what
46928ad4d6ddSJeff Bonwick 	 * the user wanted.  To prevent this, we make sure that the
46938ad4d6ddSJeff Bonwick 	 * parent/child relationship hasn't changed -- in this example,
46948ad4d6ddSJeff Bonwick 	 * that C's parent is still the replacing vdev R.
46958ad4d6ddSJeff Bonwick 	 */
46968ad4d6ddSJeff Bonwick 	if (pvd->vdev_guid != pguid && pguid != 0)
46978ad4d6ddSJeff Bonwick 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
46988ad4d6ddSJeff Bonwick 
4699fa9e4066Sahrens 	/*
4700cb04b873SMark J Musante 	 * Only 'replacing' or 'spare' vdevs can be replaced.
470199653d4eSeschrock 	 */
4702cb04b873SMark J Musante 	if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4703cb04b873SMark J Musante 	    pvd->vdev_ops != &vdev_spare_ops)
4704cb04b873SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
470599653d4eSeschrock 
470699653d4eSeschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4707e7437265Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
4708fa9e4066Sahrens 
4709fa9e4066Sahrens 	/*
471099653d4eSeschrock 	 * Only mirror, replacing, and spare vdevs support detach.
4711fa9e4066Sahrens 	 */
4712fa9e4066Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
471399653d4eSeschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
471499653d4eSeschrock 	    pvd->vdev_ops != &vdev_spare_ops)
4715fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4716fa9e4066Sahrens 
4717fa9e4066Sahrens 	/*
47188ad4d6ddSJeff Bonwick 	 * If this device has the only valid copy of some data,
47198ad4d6ddSJeff Bonwick 	 * we cannot safely detach it.
4720fa9e4066Sahrens 	 */
47218ad4d6ddSJeff Bonwick 	if (vdev_dtl_required(vd))
4722fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4723fa9e4066Sahrens 
47248ad4d6ddSJeff Bonwick 	ASSERT(pvd->vdev_children >= 2);
4725fa9e4066Sahrens 
4726bf82a41bSeschrock 	/*
4727bf82a41bSeschrock 	 * If we are detaching the second disk from a replacing vdev, then
4728bf82a41bSeschrock 	 * check to see if we changed the original vdev's path to have "/old"
4729bf82a41bSeschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
4730bf82a41bSeschrock 	 */
4731cb04b873SMark J Musante 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4732cb04b873SMark J Musante 	    vd->vdev_path != NULL) {
4733cb04b873SMark J Musante 		size_t len = strlen(vd->vdev_path);
4734cb04b873SMark J Musante 
4735cb04b873SMark J Musante 		for (int c = 0; c < pvd->vdev_children; c++) {
4736cb04b873SMark J Musante 			cvd = pvd->vdev_child[c];
4737cb04b873SMark J Musante 
4738cb04b873SMark J Musante 			if (cvd == vd || cvd->vdev_path == NULL)
4739cb04b873SMark J Musante 				continue;
4740cb04b873SMark J Musante 
4741cb04b873SMark J Musante 			if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4742cb04b873SMark J Musante 			    strcmp(cvd->vdev_path + len, "/old") == 0) {
4743cb04b873SMark J Musante 				spa_strfree(cvd->vdev_path);
4744cb04b873SMark J Musante 				cvd->vdev_path = spa_strdup(vd->vdev_path);
4745cb04b873SMark J Musante 				break;
4746cb04b873SMark J Musante 			}
4747bf82a41bSeschrock 		}
4748bf82a41bSeschrock 	}
4749bf82a41bSeschrock 
475099653d4eSeschrock 	/*
475199653d4eSeschrock 	 * If we are detaching the original disk from a spare, then it implies
475299653d4eSeschrock 	 * that the spare should become a real disk, and be removed from the
475399653d4eSeschrock 	 * active spare list for the pool.
475499653d4eSeschrock 	 */
475599653d4eSeschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
4756cb04b873SMark J Musante 	    vd->vdev_id == 0 &&
4757cb04b873SMark J Musante 	    pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
475899653d4eSeschrock 		unspare = B_TRUE;
475999653d4eSeschrock 
4760fa9e4066Sahrens 	/*
4761fa9e4066Sahrens 	 * Erase the disk labels so the disk can be used for other things.
4762fa9e4066Sahrens 	 * This must be done after all other error cases are handled,
4763fa9e4066Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
4764fa9e4066Sahrens 	 * But if we can't do it, don't treat the error as fatal --
4765fa9e4066Sahrens 	 * it may be that the unwritability of the disk is the reason
4766fa9e4066Sahrens 	 * it's being detached!
4767fa9e4066Sahrens 	 */
476839c23413Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4769fa9e4066Sahrens 
4770fa9e4066Sahrens 	/*
4771fa9e4066Sahrens 	 * Remove vd from its parent and compact the parent's children.
4772fa9e4066Sahrens 	 */
4773fa9e4066Sahrens 	vdev_remove_child(pvd, vd);
4774fa9e4066Sahrens 	vdev_compact_children(pvd);
4775fa9e4066Sahrens 
4776fa9e4066Sahrens 	/*
4777fa9e4066Sahrens 	 * Remember one of the remaining children so we can get tvd below.
4778fa9e4066Sahrens 	 */
4779cb04b873SMark J Musante 	cvd = pvd->vdev_child[pvd->vdev_children - 1];
4780fa9e4066Sahrens 
478199653d4eSeschrock 	/*
478299653d4eSeschrock 	 * If we need to remove the remaining child from the list of hot spares,
47838ad4d6ddSJeff Bonwick 	 * do it now, marking the vdev as no longer a spare in the process.
47848ad4d6ddSJeff Bonwick 	 * We must do this before vdev_remove_parent(), because that can
47858ad4d6ddSJeff Bonwick 	 * change the GUID if it creates a new toplevel GUID.  For a similar
47868ad4d6ddSJeff Bonwick 	 * reason, we must remove the spare now, in the same txg as the detach;
47878ad4d6ddSJeff Bonwick 	 * otherwise someone could attach a new sibling, change the GUID, and
47888ad4d6ddSJeff Bonwick 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
478999653d4eSeschrock 	 */
479099653d4eSeschrock 	if (unspare) {
479199653d4eSeschrock 		ASSERT(cvd->vdev_isspare);
479239c23413Seschrock 		spa_spare_remove(cvd);
479399653d4eSeschrock 		unspare_guid = cvd->vdev_guid;
47948ad4d6ddSJeff Bonwick 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4795cb04b873SMark J Musante 		cvd->vdev_unspare = B_TRUE;
479699653d4eSeschrock 	}
479799653d4eSeschrock 
4798fa9e4066Sahrens 	/*
4799fa9e4066Sahrens 	 * If the parent mirror/replacing vdev only has one child,
4800fa9e4066Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
4801fa9e4066Sahrens 	 */
4802cb04b873SMark J Musante 	if (pvd->vdev_children == 1) {
4803cb04b873SMark J Musante 		if (pvd->vdev_ops == &vdev_spare_ops)
4804cb04b873SMark J Musante 			cvd->vdev_unspare = B_FALSE;
4805fa9e4066Sahrens 		vdev_remove_parent(cvd);
4806cb04b873SMark J Musante 	}
4807cb04b873SMark J Musante 
4808fa9e4066Sahrens 
4809fa9e4066Sahrens 	/*
4810fa9e4066Sahrens 	 * We don't set tvd until now because the parent we just removed
4811fa9e4066Sahrens 	 * may have been the previous top-level vdev.
4812fa9e4066Sahrens 	 */
4813fa9e4066Sahrens 	tvd = cvd->vdev_top;
4814fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
4815fa9e4066Sahrens 
4816fa9e4066Sahrens 	/*
481739c23413Seschrock 	 * Reevaluate the parent vdev state.
4818fa9e4066Sahrens 	 */
48193d7072f8Seschrock 	vdev_propagate_state(cvd);
4820fa9e4066Sahrens 
4821fa9e4066Sahrens 	/*
4822573ca77eSGeorge Wilson 	 * If the 'autoexpand' property is set on the pool then automatically
4823573ca77eSGeorge Wilson 	 * try to expand the size of the pool. For example if the device we
4824573ca77eSGeorge Wilson 	 * just detached was smaller than the others, it may be possible to
4825573ca77eSGeorge Wilson 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4826573ca77eSGeorge Wilson 	 * first so that we can obtain the updated sizes of the leaf vdevs.
4827fa9e4066Sahrens 	 */
4828573ca77eSGeorge Wilson 	if (spa->spa_autoexpand) {
4829573ca77eSGeorge Wilson 		vdev_reopen(tvd);
4830573ca77eSGeorge Wilson 		vdev_expand(tvd, txg);
4831573ca77eSGeorge Wilson 	}
4832fa9e4066Sahrens 
4833fa9e4066Sahrens 	vdev_config_dirty(tvd);
4834fa9e4066Sahrens 
4835fa9e4066Sahrens 	/*
483639c23413Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
483739c23413Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
483839c23413Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
483939c23413Seschrock 	 * prevent vd from being accessed after it's freed.
4840fa9e4066Sahrens 	 */
48411195e687SMark J Musante 	vdpath = spa_strdup(vd->vdev_path);
48428ad4d6ddSJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
4843fa9e4066Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4844ecc2d604Sbonwick 	vd->vdev_detached = B_TRUE;
4845ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
4846fa9e4066Sahrens 
48473d7072f8Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
48483d7072f8Seschrock 
4849cb04b873SMark J Musante 	/* hang on to the spa before we release the lock */
4850cb04b873SMark J Musante 	spa_open_ref(spa, FTAG);
4851cb04b873SMark J Musante 
485299653d4eSeschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
485399653d4eSeschrock 
48544445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "detach", NULL,
48551195e687SMark J Musante 	    "vdev=%s", vdpath);
48561195e687SMark J Musante 	spa_strfree(vdpath);
48571195e687SMark J Musante 
485899653d4eSeschrock 	/*
485939c23413Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
486039c23413Seschrock 	 * then we want to go through and remove the device from the hot spare
486139c23413Seschrock 	 * list of every other pool.
486299653d4eSeschrock 	 */
486399653d4eSeschrock 	if (unspare) {
4864cb04b873SMark J Musante 		spa_t *altspa = NULL;
4865cb04b873SMark J Musante 
486699653d4eSeschrock 		mutex_enter(&spa_namespace_lock);
4867cb04b873SMark J Musante 		while ((altspa = spa_next(altspa)) != NULL) {
4868cb04b873SMark J Musante 			if (altspa->spa_state != POOL_STATE_ACTIVE ||
4869cb04b873SMark J Musante 			    altspa == spa)
487099653d4eSeschrock 				continue;
4871cb04b873SMark J Musante 
4872cb04b873SMark J Musante 			spa_open_ref(altspa, FTAG);
48739af0a4dfSJeff Bonwick 			mutex_exit(&spa_namespace_lock);
4874cb04b873SMark J Musante 			(void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
48759af0a4dfSJeff Bonwick 			mutex_enter(&spa_namespace_lock);
4876cb04b873SMark J Musante 			spa_close(altspa, FTAG);
487799653d4eSeschrock 		}
487899653d4eSeschrock 		mutex_exit(&spa_namespace_lock);
4879cb04b873SMark J Musante 
4880cb04b873SMark J Musante 		/* search the rest of the vdevs for spares to remove */
4881cb04b873SMark J Musante 		spa_vdev_resilver_done(spa);
488299653d4eSeschrock 	}
488399653d4eSeschrock 
4884cb04b873SMark J Musante 	/* all done with the spa; OK to release */
4885cb04b873SMark J Musante 	mutex_enter(&spa_namespace_lock);
4886cb04b873SMark J Musante 	spa_close(spa, FTAG);
4887cb04b873SMark J Musante 	mutex_exit(&spa_namespace_lock);
4888cb04b873SMark J Musante 
488999653d4eSeschrock 	return (error);
489099653d4eSeschrock }
489199653d4eSeschrock 
48921195e687SMark J Musante /*
48931195e687SMark J Musante  * Split a set of devices from their mirrors, and create a new pool from them.
48941195e687SMark J Musante  */
48951195e687SMark J Musante int
48961195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
48971195e687SMark J Musante     nvlist_t *props, boolean_t exp)
48981195e687SMark J Musante {
48991195e687SMark J Musante 	int error = 0;
49001195e687SMark J Musante 	uint64_t txg, *glist;
49011195e687SMark J Musante 	spa_t *newspa;
49021195e687SMark J Musante 	uint_t c, children, lastlog;
49031195e687SMark J Musante 	nvlist_t **child, *nvl, *tmp;
49041195e687SMark J Musante 	dmu_tx_t *tx;
49051195e687SMark J Musante 	char *altroot = NULL;
49061195e687SMark J Musante 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
49071195e687SMark J Musante 	boolean_t activate_slog;
49081195e687SMark J Musante 
4909f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
49101195e687SMark J Musante 
49111195e687SMark J Musante 	txg = spa_vdev_enter(spa);
49121195e687SMark J Musante 
49131195e687SMark J Musante 	/* clear the log and flush everything up to now */
49141195e687SMark J Musante 	activate_slog = spa_passivate_log(spa);
49151195e687SMark J Musante 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
49161195e687SMark J Musante 	error = spa_offline_log(spa);
49171195e687SMark J Musante 	txg = spa_vdev_config_enter(spa);
49181195e687SMark J Musante 
49191195e687SMark J Musante 	if (activate_slog)
49201195e687SMark J Musante 		spa_activate_log(spa);
49211195e687SMark J Musante 
49221195e687SMark J Musante 	if (error != 0)
49231195e687SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, error));
49241195e687SMark J Musante 
49251195e687SMark J Musante 	/* check new spa name before going any further */
49261195e687SMark J Musante 	if (spa_lookup(newname) != NULL)
49271195e687SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
49281195e687SMark J Musante 
49291195e687SMark J Musante 	/*
49301195e687SMark J Musante 	 * scan through all the children to ensure they're all mirrors
49311195e687SMark J Musante 	 */
49321195e687SMark J Musante 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
49331195e687SMark J Musante 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
49341195e687SMark J Musante 	    &children) != 0)
49351195e687SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
49361195e687SMark J Musante 
49371195e687SMark J Musante 	/* first, check to ensure we've got the right child count */
49381195e687SMark J Musante 	rvd = spa->spa_root_vdev;
49391195e687SMark J Musante 	lastlog = 0;
49401195e687SMark J Musante 	for (c = 0; c < rvd->vdev_children; c++) {
49411195e687SMark J Musante 		vdev_t *vd = rvd->vdev_child[c];
49421195e687SMark J Musante 
49431195e687SMark J Musante 		/* don't count the holes & logs as children */
49441195e687SMark J Musante 		if (vd->vdev_islog || vd->vdev_ishole) {
49451195e687SMark J Musante 			if (lastlog == 0)
49461195e687SMark J Musante 				lastlog = c;
49471195e687SMark J Musante 			continue;
49481195e687SMark J Musante 		}
49491195e687SMark J Musante 
49501195e687SMark J Musante 		lastlog = 0;
49511195e687SMark J Musante 	}
49521195e687SMark J Musante 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
49531195e687SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
49541195e687SMark J Musante 
49551195e687SMark J Musante 	/* next, ensure no spare or cache devices are part of the split */
49561195e687SMark J Musante 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
49571195e687SMark J Musante 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
49581195e687SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
49591195e687SMark J Musante 
49601195e687SMark J Musante 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
49611195e687SMark J Musante 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
49621195e687SMark J Musante 
49631195e687SMark J Musante 	/* then, loop over each vdev and validate it */
49641195e687SMark J Musante 	for (c = 0; c < children; c++) {
49651195e687SMark J Musante 		uint64_t is_hole = 0;
49661195e687SMark J Musante 
49671195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
49681195e687SMark J Musante 		    &is_hole);
49691195e687SMark J Musante 
49701195e687SMark J Musante 		if (is_hole != 0) {
49711195e687SMark J Musante 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
49721195e687SMark J Musante 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
49731195e687SMark J Musante 				continue;
49741195e687SMark J Musante 			} else {
4975be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
49761195e687SMark J Musante 				break;
49771195e687SMark J Musante 			}
49781195e687SMark J Musante 		}
49791195e687SMark J Musante 
49801195e687SMark J Musante 		/* which disk is going to be split? */
49811195e687SMark J Musante 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
49821195e687SMark J Musante 		    &glist[c]) != 0) {
4983be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
49841195e687SMark J Musante 			break;
49851195e687SMark J Musante 		}
49861195e687SMark J Musante 
49871195e687SMark J Musante 		/* look it up in the spa */
49881195e687SMark J Musante 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
49891195e687SMark J Musante 		if (vml[c] == NULL) {
4990be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENODEV);
49911195e687SMark J Musante 			break;
49921195e687SMark J Musante 		}
49931195e687SMark J Musante 
49941195e687SMark J Musante 		/* make sure there's nothing stopping the split */
49951195e687SMark J Musante 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
49961195e687SMark J Musante 		    vml[c]->vdev_islog ||
49971195e687SMark J Musante 		    vml[c]->vdev_ishole ||
49981195e687SMark J Musante 		    vml[c]->vdev_isspare ||
49991195e687SMark J Musante 		    vml[c]->vdev_isl2cache ||
50001195e687SMark J Musante 		    !vdev_writeable(vml[c]) ||
5001d41c4376SMark J Musante 		    vml[c]->vdev_children != 0 ||
50021195e687SMark J Musante 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
50031195e687SMark J Musante 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5004be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
50051195e687SMark J Musante 			break;
50061195e687SMark J Musante 		}
50071195e687SMark J Musante 
50081195e687SMark J Musante 		if (vdev_dtl_required(vml[c])) {
5009be6fd75aSMatthew Ahrens 			error = SET_ERROR(EBUSY);
50101195e687SMark J Musante 			break;
50111195e687SMark J Musante 		}
50121195e687SMark J Musante 
50131195e687SMark J Musante 		/* we need certain info from the top level */
50141195e687SMark J Musante 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
50151195e687SMark J Musante 		    vml[c]->vdev_top->vdev_ms_array) == 0);
50161195e687SMark J Musante 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
50171195e687SMark J Musante 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
50181195e687SMark J Musante 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
50191195e687SMark J Musante 		    vml[c]->vdev_top->vdev_asize) == 0);
50201195e687SMark J Musante 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
50211195e687SMark J Musante 		    vml[c]->vdev_top->vdev_ashift) == 0);
50221195e687SMark J Musante 	}
50231195e687SMark J Musante 
50241195e687SMark J Musante 	if (error != 0) {
50251195e687SMark J Musante 		kmem_free(vml, children * sizeof (vdev_t *));
50261195e687SMark J Musante 		kmem_free(glist, children * sizeof (uint64_t));
50271195e687SMark J Musante 		return (spa_vdev_exit(spa, NULL, txg, error));
50281195e687SMark J Musante 	}
50291195e687SMark J Musante 
50301195e687SMark J Musante 	/* stop writers from using the disks */
50311195e687SMark J Musante 	for (c = 0; c < children; c++) {
50321195e687SMark J Musante 		if (vml[c] != NULL)
50331195e687SMark J Musante 			vml[c]->vdev_offline = B_TRUE;
50341195e687SMark J Musante 	}
50351195e687SMark J Musante 	vdev_reopen(spa->spa_root_vdev);
50361195e687SMark J Musante 
50371195e687SMark J Musante 	/*
50381195e687SMark J Musante 	 * Temporarily record the splitting vdevs in the spa config.  This
50391195e687SMark J Musante 	 * will disappear once the config is regenerated.
50401195e687SMark J Musante 	 */
50411195e687SMark J Musante 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
50421195e687SMark J Musante 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
50431195e687SMark J Musante 	    glist, children) == 0);
50441195e687SMark J Musante 	kmem_free(glist, children * sizeof (uint64_t));
50451195e687SMark J Musante 
504698295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
50471195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
50481195e687SMark J Musante 	    nvl) == 0);
504998295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
50501195e687SMark J Musante 	spa->spa_config_splitting = nvl;
50511195e687SMark J Musante 	vdev_config_dirty(spa->spa_root_vdev);
50521195e687SMark J Musante 
50531195e687SMark J Musante 	/* configure and create the new pool */
50541195e687SMark J Musante 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
50551195e687SMark J Musante 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
50561195e687SMark J Musante 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
50571195e687SMark J Musante 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
50581195e687SMark J Musante 	    spa_version(spa)) == 0);
50591195e687SMark J Musante 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
50601195e687SMark J Musante 	    spa->spa_config_txg) == 0);
50611195e687SMark J Musante 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
50621195e687SMark J Musante 	    spa_generate_guid(NULL)) == 0);
50631195e687SMark J Musante 	(void) nvlist_lookup_string(props,
50641195e687SMark J Musante 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
50651195e687SMark J Musante 
5066d41c4376SMark J Musante 	/* add the new pool to the namespace */
50671195e687SMark J Musante 	newspa = spa_add(newname, config, altroot);
50681195e687SMark J Musante 	newspa->spa_config_txg = spa->spa_config_txg;
50691195e687SMark J Musante 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
50701195e687SMark J Musante 
50711195e687SMark J Musante 	/* release the spa config lock, retaining the namespace lock */
50721195e687SMark J Musante 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
50731195e687SMark J Musante 
50741195e687SMark J Musante 	if (zio_injection_enabled)
50751195e687SMark J Musante 		zio_handle_panic_injection(spa, FTAG, 1);
50761195e687SMark J Musante 
50771195e687SMark J Musante 	spa_activate(newspa, spa_mode_global);
50781195e687SMark J Musante 	spa_async_suspend(newspa);
50791195e687SMark J Musante 
50801195e687SMark J Musante 	/* create the new pool from the disks of the original pool */
50811195e687SMark J Musante 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
50821195e687SMark J Musante 	if (error)
50831195e687SMark J Musante 		goto out;
50841195e687SMark J Musante 
50851195e687SMark J Musante 	/* if that worked, generate a real config for the new pool */
50861195e687SMark J Musante 	if (newspa->spa_root_vdev != NULL) {
50871195e687SMark J Musante 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
50881195e687SMark J Musante 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
50891195e687SMark J Musante 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
50901195e687SMark J Musante 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
50911195e687SMark J Musante 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
50921195e687SMark J Musante 		    B_TRUE));
50931195e687SMark J Musante 	}
50941195e687SMark J Musante 
50951195e687SMark J Musante 	/* set the props */
50961195e687SMark J Musante 	if (props != NULL) {
50971195e687SMark J Musante 		spa_configfile_set(newspa, props, B_FALSE);
50981195e687SMark J Musante 		error = spa_prop_set(newspa, props);
50991195e687SMark J Musante 		if (error)
51001195e687SMark J Musante 			goto out;
51011195e687SMark J Musante 	}
51021195e687SMark J Musante 
51031195e687SMark J Musante 	/* flush everything */
51041195e687SMark J Musante 	txg = spa_vdev_config_enter(newspa);
51051195e687SMark J Musante 	vdev_config_dirty(newspa->spa_root_vdev);
51061195e687SMark J Musante 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
51071195e687SMark J Musante 
51081195e687SMark J Musante 	if (zio_injection_enabled)
51091195e687SMark J Musante 		zio_handle_panic_injection(spa, FTAG, 2);
51101195e687SMark J Musante 
51111195e687SMark J Musante 	spa_async_resume(newspa);
51121195e687SMark J Musante 
51131195e687SMark J Musante 	/* finally, update the original pool's config */
51141195e687SMark J Musante 	txg = spa_vdev_config_enter(spa);
51151195e687SMark J Musante 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
51161195e687SMark J Musante 	error = dmu_tx_assign(tx, TXG_WAIT);
51171195e687SMark J Musante 	if (error != 0)
51181195e687SMark J Musante 		dmu_tx_abort(tx);
51191195e687SMark J Musante 	for (c = 0; c < children; c++) {
51201195e687SMark J Musante 		if (vml[c] != NULL) {
51211195e687SMark J Musante 			vdev_split(vml[c]);
51221195e687SMark J Musante 			if (error == 0)
51234445fffbSMatthew Ahrens 				spa_history_log_internal(spa, "detach", tx,
51244445fffbSMatthew Ahrens 				    "vdev=%s", vml[c]->vdev_path);
51251195e687SMark J Musante 			vdev_free(vml[c]);
51261195e687SMark J Musante 		}
51271195e687SMark J Musante 	}
51281195e687SMark J Musante 	vdev_config_dirty(spa->spa_root_vdev);
51291195e687SMark J Musante 	spa->spa_config_splitting = NULL;
51301195e687SMark J Musante 	nvlist_free(nvl);
51311195e687SMark J Musante 	if (error == 0)
51321195e687SMark J Musante 		dmu_tx_commit(tx);
51331195e687SMark J Musante 	(void) spa_vdev_exit(spa, NULL, txg, 0);
51341195e687SMark J Musante 
51351195e687SMark J Musante 	if (zio_injection_enabled)
51361195e687SMark J Musante 		zio_handle_panic_injection(spa, FTAG, 3);
51371195e687SMark J Musante 
51381195e687SMark J Musante 	/* split is complete; log a history record */
51394445fffbSMatthew Ahrens 	spa_history_log_internal(newspa, "split", NULL,
51404445fffbSMatthew Ahrens 	    "from pool %s", spa_name(spa));
51411195e687SMark J Musante 
51421195e687SMark J Musante 	kmem_free(vml, children * sizeof (vdev_t *));
51431195e687SMark J Musante 
51441195e687SMark J Musante 	/* if we're not going to mount the filesystems in userland, export */
51451195e687SMark J Musante 	if (exp)
51461195e687SMark J Musante 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
51471195e687SMark J Musante 		    B_FALSE, B_FALSE);
51481195e687SMark J Musante 
51491195e687SMark J Musante 	return (error);
51501195e687SMark J Musante 
51511195e687SMark J Musante out:
51521195e687SMark J Musante 	spa_unload(newspa);
51531195e687SMark J Musante 	spa_deactivate(newspa);
51541195e687SMark J Musante 	spa_remove(newspa);
51551195e687SMark J Musante 
51561195e687SMark J Musante 	txg = spa_vdev_config_enter(spa);
515798295d61SMark J Musante 
515898295d61SMark J Musante 	/* re-online all offlined disks */
515998295d61SMark J Musante 	for (c = 0; c < children; c++) {
516098295d61SMark J Musante 		if (vml[c] != NULL)
516198295d61SMark J Musante 			vml[c]->vdev_offline = B_FALSE;
516298295d61SMark J Musante 	}
516398295d61SMark J Musante 	vdev_reopen(spa->spa_root_vdev);
516498295d61SMark J Musante 
51651195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
51661195e687SMark J Musante 	spa->spa_config_splitting = NULL;
5167d41c4376SMark J Musante 	(void) spa_vdev_exit(spa, NULL, txg, error);
51681195e687SMark J Musante 
51691195e687SMark J Musante 	kmem_free(vml, children * sizeof (vdev_t *));
51701195e687SMark J Musante 	return (error);
51711195e687SMark J Musante }
51721195e687SMark J Musante 
5173e14bb325SJeff Bonwick static nvlist_t *
5174e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
517599653d4eSeschrock {
5176e14bb325SJeff Bonwick 	for (int i = 0; i < count; i++) {
5177e14bb325SJeff Bonwick 		uint64_t guid;
517899653d4eSeschrock 
5179e14bb325SJeff Bonwick 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5180e14bb325SJeff Bonwick 		    &guid) == 0);
518199653d4eSeschrock 
5182e14bb325SJeff Bonwick 		if (guid == target_guid)
5183e14bb325SJeff Bonwick 			return (nvpp[i]);
518499653d4eSeschrock 	}
518599653d4eSeschrock 
5186e14bb325SJeff Bonwick 	return (NULL);
5187fa94a07fSbrendan }
5188fa94a07fSbrendan 
5189e14bb325SJeff Bonwick static void
5190e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5191e14bb325SJeff Bonwick 	nvlist_t *dev_to_remove)
5192fa94a07fSbrendan {
5193e14bb325SJeff Bonwick 	nvlist_t **newdev = NULL;
5194fa94a07fSbrendan 
5195e14bb325SJeff Bonwick 	if (count > 1)
5196e14bb325SJeff Bonwick 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5197fa94a07fSbrendan 
5198e14bb325SJeff Bonwick 	for (int i = 0, j = 0; i < count; i++) {
5199e14bb325SJeff Bonwick 		if (dev[i] == dev_to_remove)
5200e14bb325SJeff Bonwick 			continue;
5201e14bb325SJeff Bonwick 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5202fa94a07fSbrendan 	}
5203fa94a07fSbrendan 
5204e14bb325SJeff Bonwick 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5205e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5206fa94a07fSbrendan 
5207e14bb325SJeff Bonwick 	for (int i = 0; i < count - 1; i++)
5208e14bb325SJeff Bonwick 		nvlist_free(newdev[i]);
5209fa94a07fSbrendan 
5210e14bb325SJeff Bonwick 	if (count > 1)
5211e14bb325SJeff Bonwick 		kmem_free(newdev, (count - 1) * sizeof (void *));
5212fa94a07fSbrendan }
5213fa94a07fSbrendan 
521488ecc943SGeorge Wilson /*
521588ecc943SGeorge Wilson  * Evacuate the device.
521688ecc943SGeorge Wilson  */
52173f9d6ad7SLin Ling static int
521888ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
521988ecc943SGeorge Wilson {
522088ecc943SGeorge Wilson 	uint64_t txg;
52213f9d6ad7SLin Ling 	int error = 0;
522288ecc943SGeorge Wilson 
522388ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
522488ecc943SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5225b24ab676SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
522688ecc943SGeorge Wilson 
522788ecc943SGeorge Wilson 	/*
522888ecc943SGeorge Wilson 	 * Evacuate the device.  We don't hold the config lock as writer
522988ecc943SGeorge Wilson 	 * since we need to do I/O but we do keep the
523088ecc943SGeorge Wilson 	 * spa_namespace_lock held.  Once this completes the device
523188ecc943SGeorge Wilson 	 * should no longer have any blocks allocated on it.
523288ecc943SGeorge Wilson 	 */
523388ecc943SGeorge Wilson 	if (vd->vdev_islog) {
52343f9d6ad7SLin Ling 		if (vd->vdev_stat.vs_alloc != 0)
52353f9d6ad7SLin Ling 			error = spa_offline_log(spa);
5236a1521560SJeff Bonwick 	} else {
5237be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOTSUP);
523888ecc943SGeorge Wilson 	}
523988ecc943SGeorge Wilson 
5240a1521560SJeff Bonwick 	if (error)
5241a1521560SJeff Bonwick 		return (error);
5242a1521560SJeff Bonwick 
524388ecc943SGeorge Wilson 	/*
5244a1521560SJeff Bonwick 	 * The evacuation succeeded.  Remove any remaining MOS metadata
5245a1521560SJeff Bonwick 	 * associated with this vdev, and wait for these changes to sync.
524688ecc943SGeorge Wilson 	 */
5247fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
524888ecc943SGeorge Wilson 	txg = spa_vdev_config_enter(spa);
524988ecc943SGeorge Wilson 	vd->vdev_removing = B_TRUE;
52500713e232SGeorge Wilson 	vdev_dirty_leaves(vd, VDD_DTL, txg);
525188ecc943SGeorge Wilson 	vdev_config_dirty(vd);
525288ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
525388ecc943SGeorge Wilson 
525488ecc943SGeorge Wilson 	return (0);
525588ecc943SGeorge Wilson }
525688ecc943SGeorge Wilson 
525788ecc943SGeorge Wilson /*
525888ecc943SGeorge Wilson  * Complete the removal by cleaning up the namespace.
525988ecc943SGeorge Wilson  */
52603f9d6ad7SLin Ling static void
5261a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
526288ecc943SGeorge Wilson {
526388ecc943SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
526488ecc943SGeorge Wilson 	uint64_t id = vd->vdev_id;
526588ecc943SGeorge Wilson 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
526688ecc943SGeorge Wilson 
526788ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
526888ecc943SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5269b24ab676SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
527088ecc943SGeorge Wilson 
52713f9d6ad7SLin Ling 	/*
52723f9d6ad7SLin Ling 	 * Only remove any devices which are empty.
52733f9d6ad7SLin Ling 	 */
52743f9d6ad7SLin Ling 	if (vd->vdev_stat.vs_alloc != 0)
52753f9d6ad7SLin Ling 		return;
52763f9d6ad7SLin Ling 
527788ecc943SGeorge Wilson 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5278b24ab676SJeff Bonwick 
5279b24ab676SJeff Bonwick 	if (list_link_active(&vd->vdev_state_dirty_node))
5280b24ab676SJeff Bonwick 		vdev_state_clean(vd);
5281b24ab676SJeff Bonwick 	if (list_link_active(&vd->vdev_config_dirty_node))
5282b24ab676SJeff Bonwick 		vdev_config_clean(vd);
5283b24ab676SJeff Bonwick 
528488ecc943SGeorge Wilson 	vdev_free(vd);
528588ecc943SGeorge Wilson 
528688ecc943SGeorge Wilson 	if (last_vdev) {
528788ecc943SGeorge Wilson 		vdev_compact_children(rvd);
528888ecc943SGeorge Wilson 	} else {
528988ecc943SGeorge Wilson 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
529088ecc943SGeorge Wilson 		vdev_add_child(rvd, vd);
529188ecc943SGeorge Wilson 	}
5292fcbfa62bSLin Ling 	vdev_config_dirty(rvd);
5293fcbfa62bSLin Ling 
5294fcbfa62bSLin Ling 	/*
5295fcbfa62bSLin Ling 	 * Reassess the health of our root vdev.
5296fcbfa62bSLin Ling 	 */
5297fcbfa62bSLin Ling 	vdev_reopen(rvd);
529888ecc943SGeorge Wilson }
529988ecc943SGeorge Wilson 
53003f9d6ad7SLin Ling /*
53013f9d6ad7SLin Ling  * Remove a device from the pool -
53023f9d6ad7SLin Ling  *
53033f9d6ad7SLin Ling  * Removing a device from the vdev namespace requires several steps
53043f9d6ad7SLin Ling  * and can take a significant amount of time.  As a result we use
53053f9d6ad7SLin Ling  * the spa_vdev_config_[enter/exit] functions which allow us to
53063f9d6ad7SLin Ling  * grab and release the spa_config_lock while still holding the namespace
53073f9d6ad7SLin Ling  * lock.  During each step the configuration is synced out.
5308f7170741SWill Andrews  *
5309f7170741SWill Andrews  * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5310f7170741SWill Andrews  * devices.
5311fa94a07fSbrendan  */
5312fa94a07fSbrendan int
5313fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5314fa94a07fSbrendan {
5315fa94a07fSbrendan 	vdev_t *vd;
5316a1521560SJeff Bonwick 	metaslab_group_t *mg;
5317e14bb325SJeff Bonwick 	nvlist_t **spares, **l2cache, *nv;
53188ad4d6ddSJeff Bonwick 	uint64_t txg = 0;
531988ecc943SGeorge Wilson 	uint_t nspares, nl2cache;
5320fa94a07fSbrendan 	int error = 0;
53218ad4d6ddSJeff Bonwick 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5322fa94a07fSbrendan 
5323f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
5324f9af39baSGeorge Wilson 
53258ad4d6ddSJeff Bonwick 	if (!locked)
53268ad4d6ddSJeff Bonwick 		txg = spa_vdev_enter(spa);
5327fa94a07fSbrendan 
5328c5904d13Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5329fa94a07fSbrendan 
5330fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
5331fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5332e14bb325SJeff Bonwick 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5333e14bb325SJeff Bonwick 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5334e14bb325SJeff Bonwick 		/*
5335e14bb325SJeff Bonwick 		 * Only remove the hot spare if it's not currently in use
5336e14bb325SJeff Bonwick 		 * in this pool.
5337e14bb325SJeff Bonwick 		 */
5338e14bb325SJeff Bonwick 		if (vd == NULL || unspare) {
5339e14bb325SJeff Bonwick 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
5340e14bb325SJeff Bonwick 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5341e14bb325SJeff Bonwick 			spa_load_spares(spa);
5342e14bb325SJeff Bonwick 			spa->spa_spares.sav_sync = B_TRUE;
5343e14bb325SJeff Bonwick 		} else {
5344be6fd75aSMatthew Ahrens 			error = SET_ERROR(EBUSY);
5345e14bb325SJeff Bonwick 		}
5346e14bb325SJeff Bonwick 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
5347fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5348e14bb325SJeff Bonwick 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5349e14bb325SJeff Bonwick 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5350e14bb325SJeff Bonwick 		/*
5351e14bb325SJeff Bonwick 		 * Cache devices can always be removed.
5352e14bb325SJeff Bonwick 		 */
5353e14bb325SJeff Bonwick 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5354e14bb325SJeff Bonwick 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5355fa94a07fSbrendan 		spa_load_l2cache(spa);
5356fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
535788ecc943SGeorge Wilson 	} else if (vd != NULL && vd->vdev_islog) {
535888ecc943SGeorge Wilson 		ASSERT(!locked);
5359b24ab676SJeff Bonwick 		ASSERT(vd == vd->vdev_top);
536088ecc943SGeorge Wilson 
5361a1521560SJeff Bonwick 		mg = vd->vdev_mg;
5362a1521560SJeff Bonwick 
536388ecc943SGeorge Wilson 		/*
5364a1521560SJeff Bonwick 		 * Stop allocating from this vdev.
536588ecc943SGeorge Wilson 		 */
5366a1521560SJeff Bonwick 		metaslab_group_passivate(mg);
536788ecc943SGeorge Wilson 
5368b24ab676SJeff Bonwick 		/*
5369b24ab676SJeff Bonwick 		 * Wait for the youngest allocations and frees to sync,
5370b24ab676SJeff Bonwick 		 * and then wait for the deferral of those frees to finish.
5371b24ab676SJeff Bonwick 		 */
5372b24ab676SJeff Bonwick 		spa_vdev_config_exit(spa, NULL,
5373b24ab676SJeff Bonwick 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5374b24ab676SJeff Bonwick 
5375a1521560SJeff Bonwick 		/*
5376a1521560SJeff Bonwick 		 * Attempt to evacuate the vdev.
5377a1521560SJeff Bonwick 		 */
5378a1521560SJeff Bonwick 		error = spa_vdev_remove_evacuate(spa, vd);
5379a1521560SJeff Bonwick 
538088ecc943SGeorge Wilson 		txg = spa_vdev_config_enter(spa);
538188ecc943SGeorge Wilson 
5382a1521560SJeff Bonwick 		/*
5383a1521560SJeff Bonwick 		 * If we couldn't evacuate the vdev, unwind.
5384a1521560SJeff Bonwick 		 */
5385a1521560SJeff Bonwick 		if (error) {
5386a1521560SJeff Bonwick 			metaslab_group_activate(mg);
5387a1521560SJeff Bonwick 			return (spa_vdev_exit(spa, NULL, txg, error));
5388a1521560SJeff Bonwick 		}
5389a1521560SJeff Bonwick 
5390a1521560SJeff Bonwick 		/*
5391a1521560SJeff Bonwick 		 * Clean up the vdev namespace.
5392a1521560SJeff Bonwick 		 */
5393a1521560SJeff Bonwick 		spa_vdev_remove_from_namespace(spa, vd);
539488ecc943SGeorge Wilson 
5395e14bb325SJeff Bonwick 	} else if (vd != NULL) {
5396e14bb325SJeff Bonwick 		/*
5397e14bb325SJeff Bonwick 		 * Normal vdevs cannot be removed (yet).
5398e14bb325SJeff Bonwick 		 */
5399be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOTSUP);
5400e14bb325SJeff Bonwick 	} else {
5401e14bb325SJeff Bonwick 		/*
5402e14bb325SJeff Bonwick 		 * There is no vdev of any kind with the specified guid.
5403e14bb325SJeff Bonwick 		 */
5404be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
5405fa94a07fSbrendan 	}
540699653d4eSeschrock 
54078ad4d6ddSJeff Bonwick 	if (!locked)
54088ad4d6ddSJeff Bonwick 		return (spa_vdev_exit(spa, NULL, txg, error));
54098ad4d6ddSJeff Bonwick 
54108ad4d6ddSJeff Bonwick 	return (error);
5411fa9e4066Sahrens }
5412fa9e4066Sahrens 
5413fa9e4066Sahrens /*
54143d7072f8Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
5415f7170741SWill Andrews  * currently spared, so we can detach it.
5416fa9e4066Sahrens  */
5417ea8dc4b6Seschrock static vdev_t *
54183d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
5419fa9e4066Sahrens {
5420ea8dc4b6Seschrock 	vdev_t *newvd, *oldvd;
5421fa9e4066Sahrens 
5422573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
54233d7072f8Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5424ea8dc4b6Seschrock 		if (oldvd != NULL)
5425ea8dc4b6Seschrock 			return (oldvd);
5426ea8dc4b6Seschrock 	}
5427fa9e4066Sahrens 
54283d7072f8Seschrock 	/*
5429cb04b873SMark J Musante 	 * Check for a completed replacement.  We always consider the first
5430cb04b873SMark J Musante 	 * vdev in the list to be the oldest vdev, and the last one to be
5431cb04b873SMark J Musante 	 * the newest (see spa_vdev_attach() for how that works).  In
5432cb04b873SMark J Musante 	 * the case where the newest vdev is faulted, we will not automatically
5433cb04b873SMark J Musante 	 * remove it after a resilver completes.  This is OK as it will require
5434cb04b873SMark J Musante 	 * user intervention to determine which disk the admin wishes to keep.
54353d7072f8Seschrock 	 */
5436cb04b873SMark J Musante 	if (vd->vdev_ops == &vdev_replacing_ops) {
5437cb04b873SMark J Musante 		ASSERT(vd->vdev_children > 1);
5438cb04b873SMark J Musante 
5439cb04b873SMark J Musante 		newvd = vd->vdev_child[vd->vdev_children - 1];
5440ea8dc4b6Seschrock 		oldvd = vd->vdev_child[0];
5441ea8dc4b6Seschrock 
54428ad4d6ddSJeff Bonwick 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5443e69acc92SVictor Latushkin 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
54448ad4d6ddSJeff Bonwick 		    !vdev_dtl_required(oldvd))
5445ea8dc4b6Seschrock 			return (oldvd);
5446fa9e4066Sahrens 	}
5447ea8dc4b6Seschrock 
54483d7072f8Seschrock 	/*
54493d7072f8Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
54503d7072f8Seschrock 	 */
5451cb04b873SMark J Musante 	if (vd->vdev_ops == &vdev_spare_ops) {
5452cb04b873SMark J Musante 		vdev_t *first = vd->vdev_child[0];
5453cb04b873SMark J Musante 		vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5454cb04b873SMark J Musante 
5455cb04b873SMark J Musante 		if (last->vdev_unspare) {
5456cb04b873SMark J Musante 			oldvd = first;
5457cb04b873SMark J Musante 			newvd = last;
5458cb04b873SMark J Musante 		} else if (first->vdev_unspare) {
5459cb04b873SMark J Musante 			oldvd = last;
5460cb04b873SMark J Musante 			newvd = first;
5461cb04b873SMark J Musante 		} else {
5462cb04b873SMark J Musante 			oldvd = NULL;
5463cb04b873SMark J Musante 		}
54643d7072f8Seschrock 
5465cb04b873SMark J Musante 		if (oldvd != NULL &&
54668ad4d6ddSJeff Bonwick 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
5467e69acc92SVictor Latushkin 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5468cb04b873SMark J Musante 		    !vdev_dtl_required(oldvd))
54693d7072f8Seschrock 			return (oldvd);
5470cb04b873SMark J Musante 
5471cb04b873SMark J Musante 		/*
5472cb04b873SMark J Musante 		 * If there are more than two spares attached to a disk,
5473cb04b873SMark J Musante 		 * and those spares are not required, then we want to
5474cb04b873SMark J Musante 		 * attempt to free them up now so that they can be used
5475cb04b873SMark J Musante 		 * by other pools.  Once we're back down to a single
5476cb04b873SMark J Musante 		 * disk+spare, we stop removing them.
5477cb04b873SMark J Musante 		 */
5478cb04b873SMark J Musante 		if (vd->vdev_children > 2) {
5479cb04b873SMark J Musante 			newvd = vd->vdev_child[1];
5480cb04b873SMark J Musante 
5481cb04b873SMark J Musante 			if (newvd->vdev_isspare && last->vdev_isspare &&
5482cb04b873SMark J Musante 			    vdev_dtl_empty(last, DTL_MISSING) &&
5483cb04b873SMark J Musante 			    vdev_dtl_empty(last, DTL_OUTAGE) &&
5484cb04b873SMark J Musante 			    !vdev_dtl_required(newvd))
5485cb04b873SMark J Musante 				return (newvd);
54863d7072f8Seschrock 		}
54873d7072f8Seschrock 	}
54883d7072f8Seschrock 
5489ea8dc4b6Seschrock 	return (NULL);
5490fa9e4066Sahrens }
5491fa9e4066Sahrens 
5492ea8dc4b6Seschrock static void
54933d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa)
5494fa9e4066Sahrens {
54958ad4d6ddSJeff Bonwick 	vdev_t *vd, *pvd, *ppvd;
54968ad4d6ddSJeff Bonwick 	uint64_t guid, sguid, pguid, ppguid;
5497ea8dc4b6Seschrock 
54988ad4d6ddSJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5499ea8dc4b6Seschrock 
55003d7072f8Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
55018ad4d6ddSJeff Bonwick 		pvd = vd->vdev_parent;
55028ad4d6ddSJeff Bonwick 		ppvd = pvd->vdev_parent;
5503ea8dc4b6Seschrock 		guid = vd->vdev_guid;
55048ad4d6ddSJeff Bonwick 		pguid = pvd->vdev_guid;
55058ad4d6ddSJeff Bonwick 		ppguid = ppvd->vdev_guid;
55068ad4d6ddSJeff Bonwick 		sguid = 0;
550799653d4eSeschrock 		/*
550899653d4eSeschrock 		 * If we have just finished replacing a hot spared device, then
550999653d4eSeschrock 		 * we need to detach the parent's first child (the original hot
551099653d4eSeschrock 		 * spare) as well.
551199653d4eSeschrock 		 */
5512cb04b873SMark J Musante 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5513cb04b873SMark J Musante 		    ppvd->vdev_children == 2) {
551499653d4eSeschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
55158ad4d6ddSJeff Bonwick 			sguid = ppvd->vdev_child[1]->vdev_guid;
551699653d4eSeschrock 		}
5517b4952e17SGeorge Wilson 		ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5518b4952e17SGeorge Wilson 
55198ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
55208ad4d6ddSJeff Bonwick 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5521ea8dc4b6Seschrock 			return;
55228ad4d6ddSJeff Bonwick 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
552399653d4eSeschrock 			return;
55248ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5525fa9e4066Sahrens 	}
5526fa9e4066Sahrens 
55278ad4d6ddSJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
5528fa9e4066Sahrens }
5529fa9e4066Sahrens 
5530c67d9675Seschrock /*
5531b3388e4fSEric Taylor  * Update the stored path or FRU for this vdev.
5532c67d9675Seschrock  */
5533c67d9675Seschrock int
55346809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
55356809eb4eSEric Schrock     boolean_t ispath)
5536c67d9675Seschrock {
5537c5904d13Seschrock 	vdev_t *vd;
5538208044b8SGeorge Wilson 	boolean_t sync = B_FALSE;
5539c67d9675Seschrock 
5540f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
5541f9af39baSGeorge Wilson 
5542b3388e4fSEric Taylor 	spa_vdev_state_enter(spa, SCL_ALL);
5543c67d9675Seschrock 
55446809eb4eSEric Schrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5545b3388e4fSEric Taylor 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
5546c67d9675Seschrock 
55470e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
5548b3388e4fSEric Taylor 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
55490e34b6a7Sbonwick 
55506809eb4eSEric Schrock 	if (ispath) {
5551208044b8SGeorge Wilson 		if (strcmp(value, vd->vdev_path) != 0) {
5552208044b8SGeorge Wilson 			spa_strfree(vd->vdev_path);
5553208044b8SGeorge Wilson 			vd->vdev_path = spa_strdup(value);
5554208044b8SGeorge Wilson 			sync = B_TRUE;
5555208044b8SGeorge Wilson 		}
55566809eb4eSEric Schrock 	} else {
5557208044b8SGeorge Wilson 		if (vd->vdev_fru == NULL) {
5558208044b8SGeorge Wilson 			vd->vdev_fru = spa_strdup(value);
5559208044b8SGeorge Wilson 			sync = B_TRUE;
5560208044b8SGeorge Wilson 		} else if (strcmp(value, vd->vdev_fru) != 0) {
55616809eb4eSEric Schrock 			spa_strfree(vd->vdev_fru);
5562208044b8SGeorge Wilson 			vd->vdev_fru = spa_strdup(value);
5563208044b8SGeorge Wilson 			sync = B_TRUE;
5564208044b8SGeorge Wilson 		}
55656809eb4eSEric Schrock 	}
5566c67d9675Seschrock 
5567208044b8SGeorge Wilson 	return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5568c67d9675Seschrock }
5569c67d9675Seschrock 
55706809eb4eSEric Schrock int
55716809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
55726809eb4eSEric Schrock {
55736809eb4eSEric Schrock 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
55746809eb4eSEric Schrock }
55756809eb4eSEric Schrock 
55766809eb4eSEric Schrock int
55776809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
55786809eb4eSEric Schrock {
55796809eb4eSEric Schrock 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
55806809eb4eSEric Schrock }
55816809eb4eSEric Schrock 
5582fa9e4066Sahrens /*
5583fa9e4066Sahrens  * ==========================================================================
55843f9d6ad7SLin Ling  * SPA Scanning
5585fa9e4066Sahrens  * ==========================================================================
5586fa9e4066Sahrens  */
5587fa9e4066Sahrens 
5588ea8dc4b6Seschrock int
55893f9d6ad7SLin Ling spa_scan_stop(spa_t *spa)
5590fa9e4066Sahrens {
5591e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
55923f9d6ad7SLin Ling 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
5593be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
55943f9d6ad7SLin Ling 	return (dsl_scan_cancel(spa->spa_dsl_pool));
55953f9d6ad7SLin Ling }
5596bb8b5132Sek 
55973f9d6ad7SLin Ling int
55983f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func)
55993f9d6ad7SLin Ling {
56003f9d6ad7SLin Ling 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
56013f9d6ad7SLin Ling 
56023f9d6ad7SLin Ling 	if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5603be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
5604fa9e4066Sahrens 
5605fa9e4066Sahrens 	/*
5606088f3894Sahrens 	 * If a resilver was requested, but there is no DTL on a
5607088f3894Sahrens 	 * writeable leaf device, we have nothing to do.
5608fa9e4066Sahrens 	 */
56093f9d6ad7SLin Ling 	if (func == POOL_SCAN_RESILVER &&
5610088f3894Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5611088f3894Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5612ea8dc4b6Seschrock 		return (0);
5613ea8dc4b6Seschrock 	}
5614fa9e4066Sahrens 
56153f9d6ad7SLin Ling 	return (dsl_scan(spa->spa_dsl_pool, func));
5616fa9e4066Sahrens }
5617fa9e4066Sahrens 
5618ea8dc4b6Seschrock /*
5619ea8dc4b6Seschrock  * ==========================================================================
5620ea8dc4b6Seschrock  * SPA async task processing
5621ea8dc4b6Seschrock  * ==========================================================================
5622ea8dc4b6Seschrock  */
5623ea8dc4b6Seschrock 
5624ea8dc4b6Seschrock static void
56253d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
5626fa9e4066Sahrens {
562749cf58c0SBrendan Gregg - Sun Microsystems 	if (vd->vdev_remove_wanted) {
562898d1cbfeSGeorge Wilson 		vd->vdev_remove_wanted = B_FALSE;
562998d1cbfeSGeorge Wilson 		vd->vdev_delayed_close = B_FALSE;
563049cf58c0SBrendan Gregg - Sun Microsystems 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
56311d713200SEric Schrock 
56321d713200SEric Schrock 		/*
56331d713200SEric Schrock 		 * We want to clear the stats, but we don't want to do a full
56341d713200SEric Schrock 		 * vdev_clear() as that will cause us to throw away
56351d713200SEric Schrock 		 * degraded/faulted state as well as attempt to reopen the
56361d713200SEric Schrock 		 * device, all of which is a waste.
56371d713200SEric Schrock 		 */
56381d713200SEric Schrock 		vd->vdev_stat.vs_read_errors = 0;
56391d713200SEric Schrock 		vd->vdev_stat.vs_write_errors = 0;
56401d713200SEric Schrock 		vd->vdev_stat.vs_checksum_errors = 0;
56411d713200SEric Schrock 
5642e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
5643ea8dc4b6Seschrock 	}
564449cf58c0SBrendan Gregg - Sun Microsystems 
5645e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
564649cf58c0SBrendan Gregg - Sun Microsystems 		spa_async_remove(spa, vd->vdev_child[c]);
5647ea8dc4b6Seschrock }
5648fa9e4066Sahrens 
5649e14bb325SJeff Bonwick static void
5650e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd)
5651e14bb325SJeff Bonwick {
5652e14bb325SJeff Bonwick 	if (vd->vdev_probe_wanted) {
565398d1cbfeSGeorge Wilson 		vd->vdev_probe_wanted = B_FALSE;
5654e14bb325SJeff Bonwick 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
5655e14bb325SJeff Bonwick 	}
5656e14bb325SJeff Bonwick 
5657e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
5658e14bb325SJeff Bonwick 		spa_async_probe(spa, vd->vdev_child[c]);
5659e14bb325SJeff Bonwick }
5660e14bb325SJeff Bonwick 
5661573ca77eSGeorge Wilson static void
5662573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5663573ca77eSGeorge Wilson {
5664573ca77eSGeorge Wilson 	sysevent_id_t eid;
5665573ca77eSGeorge Wilson 	nvlist_t *attr;
5666573ca77eSGeorge Wilson 	char *physpath;
5667573ca77eSGeorge Wilson 
5668573ca77eSGeorge Wilson 	if (!spa->spa_autoexpand)
5669573ca77eSGeorge Wilson 		return;
5670573ca77eSGeorge Wilson 
5671573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
5672573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
5673573ca77eSGeorge Wilson 		spa_async_autoexpand(spa, cvd);
5674573ca77eSGeorge Wilson 	}
5675573ca77eSGeorge Wilson 
5676573ca77eSGeorge Wilson 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5677573ca77eSGeorge Wilson 		return;
5678573ca77eSGeorge Wilson 
5679573ca77eSGeorge Wilson 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5680573ca77eSGeorge Wilson 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5681573ca77eSGeorge Wilson 
5682573ca77eSGeorge Wilson 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5683573ca77eSGeorge Wilson 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5684573ca77eSGeorge Wilson 
5685573ca77eSGeorge Wilson 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5686573ca77eSGeorge Wilson 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
5687573ca77eSGeorge Wilson 
5688573ca77eSGeorge Wilson 	nvlist_free(attr);
5689573ca77eSGeorge Wilson 	kmem_free(physpath, MAXPATHLEN);
5690573ca77eSGeorge Wilson }
5691573ca77eSGeorge Wilson 
5692ea8dc4b6Seschrock static void
5693ea8dc4b6Seschrock spa_async_thread(spa_t *spa)
5694ea8dc4b6Seschrock {
5695e14bb325SJeff Bonwick 	int tasks;
5696ea8dc4b6Seschrock 
5697ea8dc4b6Seschrock 	ASSERT(spa->spa_sync_on);
5698ea8dc4b6Seschrock 
5699ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
5700ea8dc4b6Seschrock 	tasks = spa->spa_async_tasks;
5701ea8dc4b6Seschrock 	spa->spa_async_tasks = 0;
5702ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
5703ea8dc4b6Seschrock 
57040373e76bSbonwick 	/*
57050373e76bSbonwick 	 * See if the config needs to be updated.
57060373e76bSbonwick 	 */
57070373e76bSbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5708b24ab676SJeff Bonwick 		uint64_t old_space, new_space;
5709573ca77eSGeorge Wilson 
57100373e76bSbonwick 		mutex_enter(&spa_namespace_lock);
5711b24ab676SJeff Bonwick 		old_space = metaslab_class_get_space(spa_normal_class(spa));
57120373e76bSbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5713b24ab676SJeff Bonwick 		new_space = metaslab_class_get_space(spa_normal_class(spa));
57140373e76bSbonwick 		mutex_exit(&spa_namespace_lock);
5715573ca77eSGeorge Wilson 
5716573ca77eSGeorge Wilson 		/*
5717573ca77eSGeorge Wilson 		 * If the pool grew as a result of the config update,
5718573ca77eSGeorge Wilson 		 * then log an internal history event.
5719573ca77eSGeorge Wilson 		 */
5720b24ab676SJeff Bonwick 		if (new_space != old_space) {
57214445fffbSMatthew Ahrens 			spa_history_log_internal(spa, "vdev online", NULL,
5722c8e1f6d2SMark J Musante 			    "pool '%s' size: %llu(+%llu)",
5723b24ab676SJeff Bonwick 			    spa_name(spa), new_space, new_space - old_space);
5724573ca77eSGeorge Wilson 		}
57250373e76bSbonwick 	}
57260373e76bSbonwick 
5727ea8dc4b6Seschrock 	/*
57283d7072f8Seschrock 	 * See if any devices need to be marked REMOVED.
5729ea8dc4b6Seschrock 	 */
5730e14bb325SJeff Bonwick 	if (tasks & SPA_ASYNC_REMOVE) {
57318f18d1faSGeorge Wilson 		spa_vdev_state_enter(spa, SCL_NONE);
57323d7072f8Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
5733e14bb325SJeff Bonwick 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
573449cf58c0SBrendan Gregg - Sun Microsystems 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5735e14bb325SJeff Bonwick 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
573649cf58c0SBrendan Gregg - Sun Microsystems 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5737e14bb325SJeff Bonwick 		(void) spa_vdev_state_exit(spa, NULL, 0);
5738e14bb325SJeff Bonwick 	}
5739e14bb325SJeff Bonwick 
5740573ca77eSGeorge Wilson 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5741573ca77eSGeorge Wilson 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5742573ca77eSGeorge Wilson 		spa_async_autoexpand(spa, spa->spa_root_vdev);
5743573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_CONFIG, FTAG);
5744573ca77eSGeorge Wilson 	}
5745573ca77eSGeorge Wilson 
5746e14bb325SJeff Bonwick 	/*
5747e14bb325SJeff Bonwick 	 * See if any devices need to be probed.
5748e14bb325SJeff Bonwick 	 */
5749e14bb325SJeff Bonwick 	if (tasks & SPA_ASYNC_PROBE) {
57508f18d1faSGeorge Wilson 		spa_vdev_state_enter(spa, SCL_NONE);
5751e14bb325SJeff Bonwick 		spa_async_probe(spa, spa->spa_root_vdev);
5752e14bb325SJeff Bonwick 		(void) spa_vdev_state_exit(spa, NULL, 0);
57533d7072f8Seschrock 	}
5754ea8dc4b6Seschrock 
5755ea8dc4b6Seschrock 	/*
5756ea8dc4b6Seschrock 	 * If any devices are done replacing, detach them.
5757ea8dc4b6Seschrock 	 */
57583d7072f8Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
57593d7072f8Seschrock 		spa_vdev_resilver_done(spa);
5760fa9e4066Sahrens 
5761ea8dc4b6Seschrock 	/*
5762ea8dc4b6Seschrock 	 * Kick off a resilver.
5763ea8dc4b6Seschrock 	 */
5764088f3894Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
57653f9d6ad7SLin Ling 		dsl_resilver_restart(spa->spa_dsl_pool, 0);
5766ea8dc4b6Seschrock 
5767ea8dc4b6Seschrock 	/*
5768ea8dc4b6Seschrock 	 * Let the world know that we're done.
5769ea8dc4b6Seschrock 	 */
5770ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
5771ea8dc4b6Seschrock 	spa->spa_async_thread = NULL;
5772ea8dc4b6Seschrock 	cv_broadcast(&spa->spa_async_cv);
5773ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
5774ea8dc4b6Seschrock 	thread_exit();
5775ea8dc4b6Seschrock }
5776ea8dc4b6Seschrock 
5777ea8dc4b6Seschrock void
5778ea8dc4b6Seschrock spa_async_suspend(spa_t *spa)
5779ea8dc4b6Seschrock {
5780ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
5781ea8dc4b6Seschrock 	spa->spa_async_suspended++;
5782ea8dc4b6Seschrock 	while (spa->spa_async_thread != NULL)
5783ea8dc4b6Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5784ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
5785ea8dc4b6Seschrock }
5786ea8dc4b6Seschrock 
5787ea8dc4b6Seschrock void
5788ea8dc4b6Seschrock spa_async_resume(spa_t *spa)
5789ea8dc4b6Seschrock {
5790ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
5791ea8dc4b6Seschrock 	ASSERT(spa->spa_async_suspended != 0);
5792ea8dc4b6Seschrock 	spa->spa_async_suspended--;
5793ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
5794ea8dc4b6Seschrock }
5795ea8dc4b6Seschrock 
57963cb69f73SWill Andrews static boolean_t
57973cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa)
57983cb69f73SWill Andrews {
57993cb69f73SWill Andrews 	uint_t non_config_tasks;
58003cb69f73SWill Andrews 	uint_t config_task;
58013cb69f73SWill Andrews 	boolean_t config_task_suspended;
58023cb69f73SWill Andrews 
58033cb69f73SWill Andrews 	non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
58043cb69f73SWill Andrews 	config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
58053cb69f73SWill Andrews 	if (spa->spa_ccw_fail_time == 0) {
58063cb69f73SWill Andrews 		config_task_suspended = B_FALSE;
58073cb69f73SWill Andrews 	} else {
58083cb69f73SWill Andrews 		config_task_suspended =
58093cb69f73SWill Andrews 		    (gethrtime() - spa->spa_ccw_fail_time) <
58103cb69f73SWill Andrews 		    (zfs_ccw_retry_interval * NANOSEC);
58113cb69f73SWill Andrews 	}
58123cb69f73SWill Andrews 
58133cb69f73SWill Andrews 	return (non_config_tasks || (config_task && !config_task_suspended));
58143cb69f73SWill Andrews }
58153cb69f73SWill Andrews 
5816ea8dc4b6Seschrock static void
5817ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa)
5818ea8dc4b6Seschrock {
5819ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
58203cb69f73SWill Andrews 	if (spa_async_tasks_pending(spa) &&
58213cb69f73SWill Andrews 	    !spa->spa_async_suspended &&
58220373e76bSbonwick 	    spa->spa_async_thread == NULL &&
58233cb69f73SWill Andrews 	    rootdir != NULL)
5824ea8dc4b6Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
5825ea8dc4b6Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5826ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
5827ea8dc4b6Seschrock }
5828ea8dc4b6Seschrock 
5829ea8dc4b6Seschrock void
5830ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task)
5831ea8dc4b6Seschrock {
58323f9d6ad7SLin Ling 	zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5833ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
5834ea8dc4b6Seschrock 	spa->spa_async_tasks |= task;
5835ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
5836fa9e4066Sahrens }
5837fa9e4066Sahrens 
5838fa9e4066Sahrens /*
5839fa9e4066Sahrens  * ==========================================================================
5840fa9e4066Sahrens  * SPA syncing routines
5841fa9e4066Sahrens  * ==========================================================================
5842fa9e4066Sahrens  */
5843fa9e4066Sahrens 
5844cde58dbcSMatthew Ahrens static int
5845cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5846cde58dbcSMatthew Ahrens {
5847cde58dbcSMatthew Ahrens 	bpobj_t *bpo = arg;
5848cde58dbcSMatthew Ahrens 	bpobj_enqueue(bpo, bp, tx);
5849cde58dbcSMatthew Ahrens 	return (0);
5850b24ab676SJeff Bonwick }
5851b24ab676SJeff Bonwick 
5852cde58dbcSMatthew Ahrens static int
5853cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5854b24ab676SJeff Bonwick {
5855b24ab676SJeff Bonwick 	zio_t *zio = arg;
5856b24ab676SJeff Bonwick 
5857b24ab676SJeff Bonwick 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5858b24ab676SJeff Bonwick 	    zio->io_flags));
5859cde58dbcSMatthew Ahrens 	return (0);
5860fa9e4066Sahrens }
5861fa9e4066Sahrens 
586269962b56SMatthew Ahrens /*
586369962b56SMatthew Ahrens  * Note: this simple function is not inlined to make it easier to dtrace the
586469962b56SMatthew Ahrens  * amount of time spent syncing frees.
586569962b56SMatthew Ahrens  */
586669962b56SMatthew Ahrens static void
586769962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
586869962b56SMatthew Ahrens {
586969962b56SMatthew Ahrens 	zio_t *zio = zio_root(spa, NULL, NULL, 0);
587069962b56SMatthew Ahrens 	bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
587169962b56SMatthew Ahrens 	VERIFY(zio_wait(zio) == 0);
587269962b56SMatthew Ahrens }
587369962b56SMatthew Ahrens 
587469962b56SMatthew Ahrens /*
587569962b56SMatthew Ahrens  * Note: this simple function is not inlined to make it easier to dtrace the
587669962b56SMatthew Ahrens  * amount of time spent syncing deferred frees.
587769962b56SMatthew Ahrens  */
587869962b56SMatthew Ahrens static void
587969962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
588069962b56SMatthew Ahrens {
588169962b56SMatthew Ahrens 	zio_t *zio = zio_root(spa, NULL, NULL, 0);
588269962b56SMatthew Ahrens 	VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
588369962b56SMatthew Ahrens 	    spa_free_sync_cb, zio, tx), ==, 0);
588469962b56SMatthew Ahrens 	VERIFY0(zio_wait(zio));
588569962b56SMatthew Ahrens }
588669962b56SMatthew Ahrens 
588769962b56SMatthew Ahrens 
5888fa9e4066Sahrens static void
588999653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5890fa9e4066Sahrens {
5891fa9e4066Sahrens 	char *packed = NULL;
5892f7991ba4STim Haley 	size_t bufsize;
5893fa9e4066Sahrens 	size_t nvsize = 0;
5894fa9e4066Sahrens 	dmu_buf_t *db;
5895fa9e4066Sahrens 
589699653d4eSeschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5897fa9e4066Sahrens 
5898f7991ba4STim Haley 	/*
5899f7991ba4STim Haley 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
590043466aaeSMax Grossman 	 * information.  This avoids the dmu_buf_will_dirty() path and
5901f7991ba4STim Haley 	 * saves us a pre-read to get data we don't actually care about.
5902f7991ba4STim Haley 	 */
5903ad135b5dSChristopher Siden 	bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5904f7991ba4STim Haley 	packed = kmem_alloc(bufsize, KM_SLEEP);
5905fa9e4066Sahrens 
590699653d4eSeschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5907ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
5908f7991ba4STim Haley 	bzero(packed + nvsize, bufsize - nvsize);
5909fa9e4066Sahrens 
5910f7991ba4STim Haley 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5911fa9e4066Sahrens 
5912f7991ba4STim Haley 	kmem_free(packed, bufsize);
5913fa9e4066Sahrens 
591499653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5915fa9e4066Sahrens 	dmu_buf_will_dirty(db, tx);
5916fa9e4066Sahrens 	*(uint64_t *)db->db_data = nvsize;
5917ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
5918fa9e4066Sahrens }
5919fa9e4066Sahrens 
592099653d4eSeschrock static void
5921fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5922fa94a07fSbrendan     const char *config, const char *entry)
592399653d4eSeschrock {
592499653d4eSeschrock 	nvlist_t *nvroot;
5925fa94a07fSbrendan 	nvlist_t **list;
592699653d4eSeschrock 	int i;
592799653d4eSeschrock 
5928fa94a07fSbrendan 	if (!sav->sav_sync)
592999653d4eSeschrock 		return;
593099653d4eSeschrock 
593199653d4eSeschrock 	/*
5932fa94a07fSbrendan 	 * Update the MOS nvlist describing the list of available devices.
5933fa94a07fSbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
59343d7072f8Seschrock 	 * valid and the vdevs are labeled appropriately.
593599653d4eSeschrock 	 */
5936fa94a07fSbrendan 	if (sav->sav_object == 0) {
5937fa94a07fSbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5938fa94a07fSbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5939fa94a07fSbrendan 		    sizeof (uint64_t), tx);
594099653d4eSeschrock 		VERIFY(zap_update(spa->spa_meta_objset,
5941fa94a07fSbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5942fa94a07fSbrendan 		    &sav->sav_object, tx) == 0);
594399653d4eSeschrock 	}
594499653d4eSeschrock 
594599653d4eSeschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5946fa94a07fSbrendan 	if (sav->sav_count == 0) {
5947fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
594899653d4eSeschrock 	} else {
5949fa94a07fSbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5950fa94a07fSbrendan 		for (i = 0; i < sav->sav_count; i++)
5951fa94a07fSbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
59523f9d6ad7SLin Ling 			    B_FALSE, VDEV_CONFIG_L2CACHE);
5953fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5954fa94a07fSbrendan 		    sav->sav_count) == 0);
5955fa94a07fSbrendan 		for (i = 0; i < sav->sav_count; i++)
5956fa94a07fSbrendan 			nvlist_free(list[i]);
5957fa94a07fSbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
595899653d4eSeschrock 	}
595999653d4eSeschrock 
5960fa94a07fSbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
596106eeb2adSek 	nvlist_free(nvroot);
596299653d4eSeschrock 
5963fa94a07fSbrendan 	sav->sav_sync = B_FALSE;
596499653d4eSeschrock }
596599653d4eSeschrock 
596699653d4eSeschrock static void
596799653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
596899653d4eSeschrock {
596999653d4eSeschrock 	nvlist_t *config;
597099653d4eSeschrock 
5971e14bb325SJeff Bonwick 	if (list_is_empty(&spa->spa_config_dirty_list))
597299653d4eSeschrock 		return;
597399653d4eSeschrock 
5974e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5975e14bb325SJeff Bonwick 
5976e14bb325SJeff Bonwick 	config = spa_config_generate(spa, spa->spa_root_vdev,
5977e14bb325SJeff Bonwick 	    dmu_tx_get_txg(tx), B_FALSE);
5978e14bb325SJeff Bonwick 
597925345e46SGeorge Wilson 	/*
598025345e46SGeorge Wilson 	 * If we're upgrading the spa version then make sure that
598125345e46SGeorge Wilson 	 * the config object gets updated with the correct version.
598225345e46SGeorge Wilson 	 */
598325345e46SGeorge Wilson 	if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
598425345e46SGeorge Wilson 		fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
598525345e46SGeorge Wilson 		    spa->spa_uberblock.ub_version);
598625345e46SGeorge Wilson 
5987e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
598899653d4eSeschrock 
598999653d4eSeschrock 	if (spa->spa_config_syncing)
599099653d4eSeschrock 		nvlist_free(spa->spa_config_syncing);
599199653d4eSeschrock 	spa->spa_config_syncing = config;
599299653d4eSeschrock 
599399653d4eSeschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
599499653d4eSeschrock }
599599653d4eSeschrock 
5996ad135b5dSChristopher Siden static void
59973b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx)
5998ad135b5dSChristopher Siden {
59993b2aab18SMatthew Ahrens 	uint64_t *versionp = arg;
60003b2aab18SMatthew Ahrens 	uint64_t version = *versionp;
60013b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6002ad135b5dSChristopher Siden 
6003ad135b5dSChristopher Siden 	/*
6004ad135b5dSChristopher Siden 	 * Setting the version is special cased when first creating the pool.
6005ad135b5dSChristopher Siden 	 */
6006ad135b5dSChristopher Siden 	ASSERT(tx->tx_txg != TXG_INITIAL);
6007ad135b5dSChristopher Siden 
600862eae887SRichard Yao 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6009ad135b5dSChristopher Siden 	ASSERT(version >= spa_version(spa));
6010ad135b5dSChristopher Siden 
6011ad135b5dSChristopher Siden 	spa->spa_uberblock.ub_version = version;
6012ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
60134445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6014ad135b5dSChristopher Siden }
6015ad135b5dSChristopher Siden 
6016990b4856Slling /*
6017990b4856Slling  * Set zpool properties.
6018990b4856Slling  */
6019b1b8ab34Slling static void
60203b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx)
6021b1b8ab34Slling {
60223b2aab18SMatthew Ahrens 	nvlist_t *nvp = arg;
60233b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6024b1b8ab34Slling 	objset_t *mos = spa->spa_meta_objset;
6025ad135b5dSChristopher Siden 	nvpair_t *elem = NULL;
6026b1b8ab34Slling 
6027e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
6028e14bb325SJeff Bonwick 
6029990b4856Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
6030ad135b5dSChristopher Siden 		uint64_t intval;
6031ad135b5dSChristopher Siden 		char *strval, *fname;
6032ad135b5dSChristopher Siden 		zpool_prop_t prop;
6033ad135b5dSChristopher Siden 		const char *propname;
6034ad135b5dSChristopher Siden 		zprop_type_t proptype;
60352acef22dSMatthew Ahrens 		spa_feature_t fid;
6036ad135b5dSChristopher Siden 
6037990b4856Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6038ad135b5dSChristopher Siden 		case ZPROP_INVAL:
6039ad135b5dSChristopher Siden 			/*
6040ad135b5dSChristopher Siden 			 * We checked this earlier in spa_prop_validate().
6041ad135b5dSChristopher Siden 			 */
6042ad135b5dSChristopher Siden 			ASSERT(zpool_prop_feature(nvpair_name(elem)));
6043ad135b5dSChristopher Siden 
6044ad135b5dSChristopher Siden 			fname = strchr(nvpair_name(elem), '@') + 1;
60452acef22dSMatthew Ahrens 			VERIFY0(zfeature_lookup_name(fname, &fid));
6046ad135b5dSChristopher Siden 
60472acef22dSMatthew Ahrens 			spa_feature_enable(spa, fid, tx);
60484445fffbSMatthew Ahrens 			spa_history_log_internal(spa, "set", tx,
60494445fffbSMatthew Ahrens 			    "%s=enabled", nvpair_name(elem));
6050ad135b5dSChristopher Siden 			break;
6051ad135b5dSChristopher Siden 
6052990b4856Slling 		case ZPOOL_PROP_VERSION:
60530713e232SGeorge Wilson 			intval = fnvpair_value_uint64(elem);
6054990b4856Slling 			/*
6055ad135b5dSChristopher Siden 			 * The version is synced seperatly before other
6056ad135b5dSChristopher Siden 			 * properties and should be correct by now.
6057990b4856Slling 			 */
6058ad135b5dSChristopher Siden 			ASSERT3U(spa_version(spa), >=, intval);
6059ecd6cf80Smarks 			break;
6060990b4856Slling 
6061990b4856Slling 		case ZPOOL_PROP_ALTROOT:
6062990b4856Slling 			/*
6063990b4856Slling 			 * 'altroot' is a non-persistent property. It should
6064990b4856Slling 			 * have been set temporarily at creation or import time.
6065990b4856Slling 			 */
6066990b4856Slling 			ASSERT(spa->spa_root != NULL);
6067b1b8ab34Slling 			break;
60683d7072f8Seschrock 
6069f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
60702f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
6071990b4856Slling 			/*
6072f9af39baSGeorge Wilson 			 * 'readonly' and 'cachefile' are also non-persisitent
6073f9af39baSGeorge Wilson 			 * properties.
6074990b4856Slling 			 */
60753d7072f8Seschrock 			break;
60768704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
60770713e232SGeorge Wilson 			strval = fnvpair_value_string(elem);
60788704186eSDan McDonald 			if (spa->spa_comment != NULL)
60798704186eSDan McDonald 				spa_strfree(spa->spa_comment);
60808704186eSDan McDonald 			spa->spa_comment = spa_strdup(strval);
60818704186eSDan McDonald 			/*
60828704186eSDan McDonald 			 * We need to dirty the configuration on all the vdevs
60838704186eSDan McDonald 			 * so that their labels get updated.  It's unnecessary
60848704186eSDan McDonald 			 * to do this for pool creation since the vdev's
60858704186eSDan McDonald 			 * configuratoin has already been dirtied.
60868704186eSDan McDonald 			 */
60878704186eSDan McDonald 			if (tx->tx_txg != TXG_INITIAL)
60888704186eSDan McDonald 				vdev_config_dirty(spa->spa_root_vdev);
60894445fffbSMatthew Ahrens 			spa_history_log_internal(spa, "set", tx,
60904445fffbSMatthew Ahrens 			    "%s=%s", nvpair_name(elem), strval);
60918704186eSDan McDonald 			break;
6092990b4856Slling 		default:
6093990b4856Slling 			/*
6094990b4856Slling 			 * Set pool property values in the poolprops mos object.
6095990b4856Slling 			 */
6096990b4856Slling 			if (spa->spa_pool_props_object == 0) {
6097ad135b5dSChristopher Siden 				spa->spa_pool_props_object =
6098ad135b5dSChristopher Siden 				    zap_create_link(mos, DMU_OT_POOL_PROPS,
6099990b4856Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6100ad135b5dSChristopher Siden 				    tx);
6101990b4856Slling 			}
6102990b4856Slling 
6103990b4856Slling 			/* normalize the property name */
6104990b4856Slling 			propname = zpool_prop_to_name(prop);
6105990b4856Slling 			proptype = zpool_prop_get_type(prop);
6106990b4856Slling 
6107990b4856Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
6108990b4856Slling 				ASSERT(proptype == PROP_TYPE_STRING);
61090713e232SGeorge Wilson 				strval = fnvpair_value_string(elem);
61100713e232SGeorge Wilson 				VERIFY0(zap_update(mos,
6111990b4856Slling 				    spa->spa_pool_props_object, propname,
61120713e232SGeorge Wilson 				    1, strlen(strval) + 1, strval, tx));
61134445fffbSMatthew Ahrens 				spa_history_log_internal(spa, "set", tx,
61144445fffbSMatthew Ahrens 				    "%s=%s", nvpair_name(elem), strval);
6115990b4856Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
61160713e232SGeorge Wilson 				intval = fnvpair_value_uint64(elem);
6117990b4856Slling 
6118990b4856Slling 				if (proptype == PROP_TYPE_INDEX) {
6119990b4856Slling 					const char *unused;
61200713e232SGeorge Wilson 					VERIFY0(zpool_prop_index_to_string(
61210713e232SGeorge Wilson 					    prop, intval, &unused));
6122990b4856Slling 				}
61230713e232SGeorge Wilson 				VERIFY0(zap_update(mos,
6124990b4856Slling 				    spa->spa_pool_props_object, propname,
61250713e232SGeorge Wilson 				    8, 1, &intval, tx));
61264445fffbSMatthew Ahrens 				spa_history_log_internal(spa, "set", tx,
61274445fffbSMatthew Ahrens 				    "%s=%lld", nvpair_name(elem), intval);
6128990b4856Slling 			} else {
6129990b4856Slling 				ASSERT(0); /* not allowed */
6130990b4856Slling 			}
6131990b4856Slling 
61320a4e9518Sgw 			switch (prop) {
61330a4e9518Sgw 			case ZPOOL_PROP_DELEGATION:
6134990b4856Slling 				spa->spa_delegation = intval;
61350a4e9518Sgw 				break;
61360a4e9518Sgw 			case ZPOOL_PROP_BOOTFS:
6137990b4856Slling 				spa->spa_bootfs = intval;
61380a4e9518Sgw 				break;
61390a4e9518Sgw 			case ZPOOL_PROP_FAILUREMODE:
61400a4e9518Sgw 				spa->spa_failmode = intval;
61410a4e9518Sgw 				break;
6142573ca77eSGeorge Wilson 			case ZPOOL_PROP_AUTOEXPAND:
6143573ca77eSGeorge Wilson 				spa->spa_autoexpand = intval;
6144b98131cfSEric Taylor 				if (tx->tx_txg != TXG_INITIAL)
6145b98131cfSEric Taylor 					spa_async_request(spa,
6146b98131cfSEric Taylor 					    SPA_ASYNC_AUTOEXPAND);
6147573ca77eSGeorge Wilson 				break;
6148b24ab676SJeff Bonwick 			case ZPOOL_PROP_DEDUPDITTO:
6149b24ab676SJeff Bonwick 				spa->spa_dedup_ditto = intval;
6150b24ab676SJeff Bonwick 				break;
61510a4e9518Sgw 			default:
61520a4e9518Sgw 				break;
61530a4e9518Sgw 			}
6154990b4856Slling 		}
6155990b4856Slling 
6156b1b8ab34Slling 	}
6157e14bb325SJeff Bonwick 
6158e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_props_lock);
6159b1b8ab34Slling }
6160b1b8ab34Slling 
6161cde58dbcSMatthew Ahrens /*
6162cde58dbcSMatthew Ahrens  * Perform one-time upgrade on-disk changes.  spa_version() does not
6163cde58dbcSMatthew Ahrens  * reflect the new version this txg, so there must be no changes this
6164cde58dbcSMatthew Ahrens  * txg to anything that the upgrade code depends on after it executes.
6165cde58dbcSMatthew Ahrens  * Therefore this must be called after dsl_pool_sync() does the sync
6166cde58dbcSMatthew Ahrens  * tasks.
6167cde58dbcSMatthew Ahrens  */
6168cde58dbcSMatthew Ahrens static void
6169cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6170cde58dbcSMatthew Ahrens {
6171cde58dbcSMatthew Ahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
6172cde58dbcSMatthew Ahrens 
6173cde58dbcSMatthew Ahrens 	ASSERT(spa->spa_sync_pass == 1);
6174cde58dbcSMatthew Ahrens 
61753b2aab18SMatthew Ahrens 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
61763b2aab18SMatthew Ahrens 
6177cde58dbcSMatthew Ahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6178cde58dbcSMatthew Ahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6179cde58dbcSMatthew Ahrens 		dsl_pool_create_origin(dp, tx);
6180cde58dbcSMatthew Ahrens 
6181cde58dbcSMatthew Ahrens 		/* Keeping the origin open increases spa_minref */
6182cde58dbcSMatthew Ahrens 		spa->spa_minref += 3;
6183cde58dbcSMatthew Ahrens 	}
6184cde58dbcSMatthew Ahrens 
6185cde58dbcSMatthew Ahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6186cde58dbcSMatthew Ahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6187cde58dbcSMatthew Ahrens 		dsl_pool_upgrade_clones(dp, tx);
6188cde58dbcSMatthew Ahrens 	}
6189cde58dbcSMatthew Ahrens 
6190cde58dbcSMatthew Ahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6191cde58dbcSMatthew Ahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6192cde58dbcSMatthew Ahrens 		dsl_pool_upgrade_dir_clones(dp, tx);
6193cde58dbcSMatthew Ahrens 
6194cde58dbcSMatthew Ahrens 		/* Keeping the freedir open increases spa_minref */
6195cde58dbcSMatthew Ahrens 		spa->spa_minref += 3;
6196cde58dbcSMatthew Ahrens 	}
6197ad135b5dSChristopher Siden 
6198ad135b5dSChristopher Siden 	if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6199ad135b5dSChristopher Siden 	    spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6200ad135b5dSChristopher Siden 		spa_feature_create_zap_objects(spa, tx);
6201ad135b5dSChristopher Siden 	}
6202b8289d24SDaniil Lunev 
6203b8289d24SDaniil Lunev 	/*
6204b8289d24SDaniil Lunev 	 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6205b8289d24SDaniil Lunev 	 * when possibility to use lz4 compression for metadata was added
6206b8289d24SDaniil Lunev 	 * Old pools that have this feature enabled must be upgraded to have
6207b8289d24SDaniil Lunev 	 * this feature active
6208b8289d24SDaniil Lunev 	 */
6209b8289d24SDaniil Lunev 	if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6210b8289d24SDaniil Lunev 		boolean_t lz4_en = spa_feature_is_enabled(spa,
6211b8289d24SDaniil Lunev 		    SPA_FEATURE_LZ4_COMPRESS);
6212b8289d24SDaniil Lunev 		boolean_t lz4_ac = spa_feature_is_active(spa,
6213b8289d24SDaniil Lunev 		    SPA_FEATURE_LZ4_COMPRESS);
6214b8289d24SDaniil Lunev 
6215b8289d24SDaniil Lunev 		if (lz4_en && !lz4_ac)
6216b8289d24SDaniil Lunev 			spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6217b8289d24SDaniil Lunev 	}
62183b2aab18SMatthew Ahrens 	rrw_exit(&dp->dp_config_rwlock, FTAG);
6219cde58dbcSMatthew Ahrens }
6220cde58dbcSMatthew Ahrens 
6221fa9e4066Sahrens /*
6222fa9e4066Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
6223fa9e4066Sahrens  * part of the process, so we iterate until it converges.
6224fa9e4066Sahrens  */
6225fa9e4066Sahrens void
6226fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg)
6227fa9e4066Sahrens {
6228fa9e4066Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
6229fa9e4066Sahrens 	objset_t *mos = spa->spa_meta_objset;
6230b24ab676SJeff Bonwick 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
62310373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
6232fa9e4066Sahrens 	vdev_t *vd;
6233fa9e4066Sahrens 	dmu_tx_t *tx;
6234e14bb325SJeff Bonwick 	int error;
6235fa9e4066Sahrens 
6236f9af39baSGeorge Wilson 	VERIFY(spa_writeable(spa));
6237f9af39baSGeorge Wilson 
6238fa9e4066Sahrens 	/*
6239fa9e4066Sahrens 	 * Lock out configuration changes.
6240fa9e4066Sahrens 	 */
6241e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6242fa9e4066Sahrens 
6243fa9e4066Sahrens 	spa->spa_syncing_txg = txg;
6244fa9e4066Sahrens 	spa->spa_sync_pass = 0;
6245fa9e4066Sahrens 
6246e14bb325SJeff Bonwick 	/*
6247e14bb325SJeff Bonwick 	 * If there are any pending vdev state changes, convert them
6248e14bb325SJeff Bonwick 	 * into config changes that go out with this transaction group.
6249e14bb325SJeff Bonwick 	 */
6250e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
62518ad4d6ddSJeff Bonwick 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
62528ad4d6ddSJeff Bonwick 		/*
62538ad4d6ddSJeff Bonwick 		 * We need the write lock here because, for aux vdevs,
62548ad4d6ddSJeff Bonwick 		 * calling vdev_config_dirty() modifies sav_config.
62558ad4d6ddSJeff Bonwick 		 * This is ugly and will become unnecessary when we
62568ad4d6ddSJeff Bonwick 		 * eliminate the aux vdev wart by integrating all vdevs
62578ad4d6ddSJeff Bonwick 		 * into the root vdev tree.
62588ad4d6ddSJeff Bonwick 		 */
62598ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
62608ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
62618ad4d6ddSJeff Bonwick 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
62628ad4d6ddSJeff Bonwick 			vdev_state_clean(vd);
62638ad4d6ddSJeff Bonwick 			vdev_config_dirty(vd);
62648ad4d6ddSJeff Bonwick 		}
62658ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
62668ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6267e14bb325SJeff Bonwick 	}
6268e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
6269e14bb325SJeff Bonwick 
627099653d4eSeschrock 	tx = dmu_tx_create_assigned(dp, txg);
627199653d4eSeschrock 
6272283b8460SGeorge.Wilson 	spa->spa_sync_starttime = gethrtime();
6273283b8460SGeorge.Wilson 	VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6274283b8460SGeorge.Wilson 	    spa->spa_sync_starttime + spa->spa_deadman_synctime));
6275283b8460SGeorge.Wilson 
627699653d4eSeschrock 	/*
6277e7437265Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
627899653d4eSeschrock 	 * set spa_deflate if we have no raid-z vdevs.
627999653d4eSeschrock 	 */
6280e7437265Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6281e7437265Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
628299653d4eSeschrock 		int i;
628399653d4eSeschrock 
628499653d4eSeschrock 		for (i = 0; i < rvd->vdev_children; i++) {
628599653d4eSeschrock 			vd = rvd->vdev_child[i];
628699653d4eSeschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
628799653d4eSeschrock 				break;
628899653d4eSeschrock 		}
628999653d4eSeschrock 		if (i == rvd->vdev_children) {
629099653d4eSeschrock 			spa->spa_deflate = TRUE;
629199653d4eSeschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
629299653d4eSeschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
629399653d4eSeschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
629499653d4eSeschrock 		}
629599653d4eSeschrock 	}
629699653d4eSeschrock 
6297fa9e4066Sahrens 	/*
6298fa9e4066Sahrens 	 * Iterate to convergence.
6299fa9e4066Sahrens 	 */
6300fa9e4066Sahrens 	do {
6301b24ab676SJeff Bonwick 		int pass = ++spa->spa_sync_pass;
6302fa9e4066Sahrens 
6303fa9e4066Sahrens 		spa_sync_config_object(spa, tx);
6304fa94a07fSbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6305fa94a07fSbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6306fa94a07fSbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6307fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6308ea8dc4b6Seschrock 		spa_errlog_sync(spa, txg);
6309fa9e4066Sahrens 		dsl_pool_sync(dp, txg);
6310fa9e4066Sahrens 
631101f55e48SGeorge Wilson 		if (pass < zfs_sync_pass_deferred_free) {
631269962b56SMatthew Ahrens 			spa_sync_frees(spa, free_bpl, tx);
6313b24ab676SJeff Bonwick 		} else {
6314231aab85SMatthew Ahrens 			/*
6315231aab85SMatthew Ahrens 			 * We can not defer frees in pass 1, because
6316231aab85SMatthew Ahrens 			 * we sync the deferred frees later in pass 1.
6317231aab85SMatthew Ahrens 			 */
6318231aab85SMatthew Ahrens 			ASSERT3U(pass, >, 1);
6319cde58dbcSMatthew Ahrens 			bplist_iterate(free_bpl, bpobj_enqueue_cb,
632069962b56SMatthew Ahrens 			    &spa->spa_deferred_bpobj, tx);
6321fa9e4066Sahrens 		}
6322fa9e4066Sahrens 
6323b24ab676SJeff Bonwick 		ddt_sync(spa, txg);
63243f9d6ad7SLin Ling 		dsl_scan_sync(dp, tx);
6325afee20e4SGeorge Wilson 
6326b24ab676SJeff Bonwick 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6327b24ab676SJeff Bonwick 			vdev_sync(vd, txg);
6328b24ab676SJeff Bonwick 
6329231aab85SMatthew Ahrens 		if (pass == 1) {
6330cde58dbcSMatthew Ahrens 			spa_sync_upgrades(spa, tx);
6331231aab85SMatthew Ahrens 			ASSERT3U(txg, >=,
6332231aab85SMatthew Ahrens 			    spa->spa_uberblock.ub_rootbp.blk_birth);
6333231aab85SMatthew Ahrens 			/*
6334231aab85SMatthew Ahrens 			 * Note: We need to check if the MOS is dirty
6335231aab85SMatthew Ahrens 			 * because we could have marked the MOS dirty
6336231aab85SMatthew Ahrens 			 * without updating the uberblock (e.g. if we
6337231aab85SMatthew Ahrens 			 * have sync tasks but no dirty user data).  We
6338231aab85SMatthew Ahrens 			 * need to check the uberblock's rootbp because
6339231aab85SMatthew Ahrens 			 * it is updated if we have synced out dirty
6340231aab85SMatthew Ahrens 			 * data (though in this case the MOS will most
6341231aab85SMatthew Ahrens 			 * likely also be dirty due to second order
6342231aab85SMatthew Ahrens 			 * effects, we don't want to rely on that here).
6343231aab85SMatthew Ahrens 			 */
6344231aab85SMatthew Ahrens 			if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6345231aab85SMatthew Ahrens 			    !dmu_objset_is_dirty(mos, txg)) {
6346231aab85SMatthew Ahrens 				/*
6347231aab85SMatthew Ahrens 				 * Nothing changed on the first pass,
6348231aab85SMatthew Ahrens 				 * therefore this TXG is a no-op.  Avoid
6349231aab85SMatthew Ahrens 				 * syncing deferred frees, so that we
6350231aab85SMatthew Ahrens 				 * can keep this TXG as a no-op.
6351231aab85SMatthew Ahrens 				 */
6352231aab85SMatthew Ahrens 				ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6353231aab85SMatthew Ahrens 				    txg));
6354231aab85SMatthew Ahrens 				ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6355231aab85SMatthew Ahrens 				ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6356231aab85SMatthew Ahrens 				break;
6357231aab85SMatthew Ahrens 			}
6358231aab85SMatthew Ahrens 			spa_sync_deferred_frees(spa, tx);
6359231aab85SMatthew Ahrens 		}
6360fa9e4066Sahrens 
6361cde58dbcSMatthew Ahrens 	} while (dmu_objset_is_dirty(mos, txg));
6362fa9e4066Sahrens 
6363fa9e4066Sahrens 	/*
6364fa9e4066Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
6365fa9e4066Sahrens 	 * to commit the transaction group.
63660373e76bSbonwick 	 *
636717f17c2dSbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
636817f17c2dSbonwick 	 * random top-level vdevs that are known to be visible in the
6369e14bb325SJeff Bonwick 	 * config cache (see spa_vdev_add() for a complete description).
6370e14bb325SJeff Bonwick 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
63710373e76bSbonwick 	 */
6372e14bb325SJeff Bonwick 	for (;;) {
6373e14bb325SJeff Bonwick 		/*
6374e14bb325SJeff Bonwick 		 * We hold SCL_STATE to prevent vdev open/close/etc.
6375e14bb325SJeff Bonwick 		 * while we're attempting to write the vdev labels.
6376e14bb325SJeff Bonwick 		 */
6377e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6378e14bb325SJeff Bonwick 
6379e14bb325SJeff Bonwick 		if (list_is_empty(&spa->spa_config_dirty_list)) {
6380e14bb325SJeff Bonwick 			vdev_t *svd[SPA_DVAS_PER_BP];
6381e14bb325SJeff Bonwick 			int svdcount = 0;
6382e14bb325SJeff Bonwick 			int children = rvd->vdev_children;
6383e14bb325SJeff Bonwick 			int c0 = spa_get_random(children);
6384e14bb325SJeff Bonwick 
6385573ca77eSGeorge Wilson 			for (int c = 0; c < children; c++) {
6386e14bb325SJeff Bonwick 				vd = rvd->vdev_child[(c0 + c) % children];
6387e14bb325SJeff Bonwick 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6388e14bb325SJeff Bonwick 					continue;
6389e14bb325SJeff Bonwick 				svd[svdcount++] = vd;
6390e14bb325SJeff Bonwick 				if (svdcount == SPA_DVAS_PER_BP)
6391e14bb325SJeff Bonwick 					break;
6392e14bb325SJeff Bonwick 			}
63938956713aSEric Schrock 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
63948956713aSEric Schrock 			if (error != 0)
63958956713aSEric Schrock 				error = vdev_config_sync(svd, svdcount, txg,
63968956713aSEric Schrock 				    B_TRUE);
6397e14bb325SJeff Bonwick 		} else {
6398e14bb325SJeff Bonwick 			error = vdev_config_sync(rvd->vdev_child,
63998956713aSEric Schrock 			    rvd->vdev_children, txg, B_FALSE);
64008956713aSEric Schrock 			if (error != 0)
64018956713aSEric Schrock 				error = vdev_config_sync(rvd->vdev_child,
64028956713aSEric Schrock 				    rvd->vdev_children, txg, B_TRUE);
64030373e76bSbonwick 		}
6404e14bb325SJeff Bonwick 
6405dfbb9432SGeorge Wilson 		if (error == 0)
6406dfbb9432SGeorge Wilson 			spa->spa_last_synced_guid = rvd->vdev_guid;
6407dfbb9432SGeorge Wilson 
6408e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, FTAG);
6409e14bb325SJeff Bonwick 
6410e14bb325SJeff Bonwick 		if (error == 0)
6411e14bb325SJeff Bonwick 			break;
6412e14bb325SJeff Bonwick 		zio_suspend(spa, NULL);
6413e14bb325SJeff Bonwick 		zio_resume_wait(spa);
64140373e76bSbonwick 	}
641599653d4eSeschrock 	dmu_tx_commit(tx);
641699653d4eSeschrock 
6417283b8460SGeorge.Wilson 	VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6418283b8460SGeorge.Wilson 
64190373e76bSbonwick 	/*
64200373e76bSbonwick 	 * Clear the dirty config list.
6421fa9e4066Sahrens 	 */
6422e14bb325SJeff Bonwick 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
64230373e76bSbonwick 		vdev_config_clean(vd);
64240373e76bSbonwick 
64250373e76bSbonwick 	/*
64260373e76bSbonwick 	 * Now that the new config has synced transactionally,
64270373e76bSbonwick 	 * let it become visible to the config cache.
64280373e76bSbonwick 	 */
64290373e76bSbonwick 	if (spa->spa_config_syncing != NULL) {
64300373e76bSbonwick 		spa_config_set(spa, spa->spa_config_syncing);
64310373e76bSbonwick 		spa->spa_config_txg = txg;
64320373e76bSbonwick 		spa->spa_config_syncing = NULL;
64330373e76bSbonwick 	}
6434fa9e4066Sahrens 
6435fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
6436fa9e4066Sahrens 
6437b24ab676SJeff Bonwick 	dsl_pool_sync_done(dp, txg);
6438fa9e4066Sahrens 
6439fa9e4066Sahrens 	/*
6440fa9e4066Sahrens 	 * Update usable space statistics.
6441fa9e4066Sahrens 	 */
6442fa9e4066Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6443fa9e4066Sahrens 		vdev_sync_done(vd, txg);
6444fa9e4066Sahrens 
6445485bbbf5SGeorge Wilson 	spa_update_dspace(spa);
6446485bbbf5SGeorge Wilson 
6447fa9e4066Sahrens 	/*
6448fa9e4066Sahrens 	 * It had better be the case that we didn't dirty anything
644999653d4eSeschrock 	 * since vdev_config_sync().
6450fa9e4066Sahrens 	 */
6451fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6452fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6453fa9e4066Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6454b24ab676SJeff Bonwick 
6455b24ab676SJeff Bonwick 	spa->spa_sync_pass = 0;
6456fa9e4066Sahrens 
6457e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
6458ea8dc4b6Seschrock 
6459468c413aSTim Haley 	spa_handle_ignored_writes(spa);
6460468c413aSTim Haley 
6461ea8dc4b6Seschrock 	/*
6462ea8dc4b6Seschrock 	 * If any async tasks have been requested, kick them off.
6463ea8dc4b6Seschrock 	 */
6464ea8dc4b6Seschrock 	spa_async_dispatch(spa);
6465fa9e4066Sahrens }
6466fa9e4066Sahrens 
6467fa9e4066Sahrens /*
6468fa9e4066Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
6469fa9e4066Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
6470fa9e4066Sahrens  * sync.
6471fa9e4066Sahrens  */
6472fa9e4066Sahrens void
6473fa9e4066Sahrens spa_sync_allpools(void)
6474fa9e4066Sahrens {
6475fa9e4066Sahrens 	spa_t *spa = NULL;
6476fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
6477fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
6478f9af39baSGeorge Wilson 		if (spa_state(spa) != POOL_STATE_ACTIVE ||
6479f9af39baSGeorge Wilson 		    !spa_writeable(spa) || spa_suspended(spa))
6480fa9e4066Sahrens 			continue;
6481fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
6482fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
6483fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
6484fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
6485fa9e4066Sahrens 		spa_close(spa, FTAG);
6486fa9e4066Sahrens 	}
6487fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
6488fa9e4066Sahrens }
6489fa9e4066Sahrens 
6490fa9e4066Sahrens /*
6491fa9e4066Sahrens  * ==========================================================================
6492fa9e4066Sahrens  * Miscellaneous routines
6493fa9e4066Sahrens  * ==========================================================================
6494fa9e4066Sahrens  */
6495fa9e4066Sahrens 
6496fa9e4066Sahrens /*
6497fa9e4066Sahrens  * Remove all pools in the system.
6498fa9e4066Sahrens  */
6499fa9e4066Sahrens void
6500fa9e4066Sahrens spa_evict_all(void)
6501fa9e4066Sahrens {
6502fa9e4066Sahrens 	spa_t *spa;
6503fa9e4066Sahrens 
6504fa9e4066Sahrens 	/*
6505fa9e4066Sahrens 	 * Remove all cached state.  All pools should be closed now,
6506fa9e4066Sahrens 	 * so every spa in the AVL tree should be unreferenced.
6507fa9e4066Sahrens 	 */
6508fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
6509fa9e4066Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
6510fa9e4066Sahrens 		/*
6511ea8dc4b6Seschrock 		 * Stop async tasks.  The async thread may need to detach
6512ea8dc4b6Seschrock 		 * a device that's been replaced, which requires grabbing
6513ea8dc4b6Seschrock 		 * spa_namespace_lock, so we must drop it here.
6514fa9e4066Sahrens 		 */
6515fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
6516fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
6517ea8dc4b6Seschrock 		spa_async_suspend(spa);
6518fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
6519fa9e4066Sahrens 		spa_close(spa, FTAG);
6520fa9e4066Sahrens 
6521fa9e4066Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6522fa9e4066Sahrens 			spa_unload(spa);
6523fa9e4066Sahrens 			spa_deactivate(spa);
6524fa9e4066Sahrens 		}
6525fa9e4066Sahrens 		spa_remove(spa);
6526fa9e4066Sahrens 	}
6527fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
6528fa9e4066Sahrens }
6529ea8dc4b6Seschrock 
6530ea8dc4b6Seschrock vdev_t *
65316809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6532ea8dc4b6Seschrock {
6533c5904d13Seschrock 	vdev_t *vd;
6534c5904d13Seschrock 	int i;
6535c5904d13Seschrock 
6536c5904d13Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6537c5904d13Seschrock 		return (vd);
6538c5904d13Seschrock 
65396809eb4eSEric Schrock 	if (aux) {
6540c5904d13Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6541c5904d13Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
65426809eb4eSEric Schrock 			if (vd->vdev_guid == guid)
65436809eb4eSEric Schrock 				return (vd);
65446809eb4eSEric Schrock 		}
65456809eb4eSEric Schrock 
65466809eb4eSEric Schrock 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
65476809eb4eSEric Schrock 			vd = spa->spa_spares.sav_vdevs[i];
6548c5904d13Seschrock 			if (vd->vdev_guid == guid)
6549c5904d13Seschrock 				return (vd);
6550c5904d13Seschrock 		}
6551c5904d13Seschrock 	}
6552c5904d13Seschrock 
6553c5904d13Seschrock 	return (NULL);
6554ea8dc4b6Seschrock }
6555eaca9bbdSeschrock 
6556eaca9bbdSeschrock void
6557990b4856Slling spa_upgrade(spa_t *spa, uint64_t version)
6558eaca9bbdSeschrock {
6559f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
6560f9af39baSGeorge Wilson 
6561e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6562eaca9bbdSeschrock 
6563eaca9bbdSeschrock 	/*
6564eaca9bbdSeschrock 	 * This should only be called for a non-faulted pool, and since a
6565eaca9bbdSeschrock 	 * future version would result in an unopenable pool, this shouldn't be
6566eaca9bbdSeschrock 	 * possible.
6567eaca9bbdSeschrock 	 */
656862eae887SRichard Yao 	ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
65695d7b4d43SMatthew Ahrens 	ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6570eaca9bbdSeschrock 
6571990b4856Slling 	spa->spa_uberblock.ub_version = version;
6572eaca9bbdSeschrock 	vdev_config_dirty(spa->spa_root_vdev);
6573eaca9bbdSeschrock 
6574e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
657599653d4eSeschrock 
657699653d4eSeschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
657799653d4eSeschrock }
657899653d4eSeschrock 
657999653d4eSeschrock boolean_t
658099653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid)
658199653d4eSeschrock {
658299653d4eSeschrock 	int i;
658339c23413Seschrock 	uint64_t spareguid;
6584fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
658599653d4eSeschrock 
6586fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
6587fa94a07fSbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
658899653d4eSeschrock 			return (B_TRUE);
658999653d4eSeschrock 
6590fa94a07fSbrendan 	for (i = 0; i < sav->sav_npending; i++) {
6591fa94a07fSbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6592fa94a07fSbrendan 		    &spareguid) == 0 && spareguid == guid)
659339c23413Seschrock 			return (B_TRUE);
659439c23413Seschrock 	}
659539c23413Seschrock 
659699653d4eSeschrock 	return (B_FALSE);
6597eaca9bbdSeschrock }
6598b1b8ab34Slling 
659989a89ebfSlling /*
660089a89ebfSlling  * Check if a pool has an active shared spare device.
660189a89ebfSlling  * Note: reference count of an active spare is 2, as a spare and as a replace
660289a89ebfSlling  */
660389a89ebfSlling static boolean_t
660489a89ebfSlling spa_has_active_shared_spare(spa_t *spa)
660589a89ebfSlling {
660689a89ebfSlling 	int i, refcnt;
660789a89ebfSlling 	uint64_t pool;
660889a89ebfSlling 	spa_aux_vdev_t *sav = &spa->spa_spares;
660989a89ebfSlling 
661089a89ebfSlling 	for (i = 0; i < sav->sav_count; i++) {
661189a89ebfSlling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
661289a89ebfSlling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
661389a89ebfSlling 		    refcnt > 2)
661489a89ebfSlling 			return (B_TRUE);
661589a89ebfSlling 	}
661689a89ebfSlling 
661789a89ebfSlling 	return (B_FALSE);
661889a89ebfSlling }
661989a89ebfSlling 
66203d7072f8Seschrock /*
66213d7072f8Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
66223d7072f8Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
66233d7072f8Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
66243d7072f8Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
66253d7072f8Seschrock  * or zdb as real changes.
66263d7072f8Seschrock  */
66273d7072f8Seschrock void
66283d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
66293d7072f8Seschrock {
66303d7072f8Seschrock #ifdef _KERNEL
66313d7072f8Seschrock 	sysevent_t		*ev;
66323d7072f8Seschrock 	sysevent_attr_list_t	*attr = NULL;
66333d7072f8Seschrock 	sysevent_value_t	value;
66343d7072f8Seschrock 	sysevent_id_t		eid;
66353d7072f8Seschrock 
66363d7072f8Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
66373d7072f8Seschrock 	    SE_SLEEP);
66383d7072f8Seschrock 
66393d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
66403d7072f8Seschrock 	value.value.sv_string = spa_name(spa);
66413d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
66423d7072f8Seschrock 		goto done;
66433d7072f8Seschrock 
66443d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
66453d7072f8Seschrock 	value.value.sv_uint64 = spa_guid(spa);
66463d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
66473d7072f8Seschrock 		goto done;
66483d7072f8Seschrock 
66493d7072f8Seschrock 	if (vd) {
66503d7072f8Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
66513d7072f8Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
66523d7072f8Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
66533d7072f8Seschrock 		    SE_SLEEP) != 0)
66543d7072f8Seschrock 			goto done;
66553d7072f8Seschrock 
66563d7072f8Seschrock 		if (vd->vdev_path) {
66573d7072f8Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
66583d7072f8Seschrock 			value.value.sv_string = vd->vdev_path;
66593d7072f8Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
66603d7072f8Seschrock 			    &value, SE_SLEEP) != 0)
66613d7072f8Seschrock 				goto done;
66623d7072f8Seschrock 		}
66633d7072f8Seschrock 	}
66643d7072f8Seschrock 
6665b01c3b58Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
6666b01c3b58Seschrock 		goto done;
6667b01c3b58Seschrock 	attr = NULL;
6668b01c3b58Seschrock 
66693d7072f8Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
66703d7072f8Seschrock 
66713d7072f8Seschrock done:
66723d7072f8Seschrock 	if (attr)
66733d7072f8Seschrock 		sysevent_free_attr(attr);
66743d7072f8Seschrock 	sysevent_free(ev);
66753d7072f8Seschrock #endif
66763d7072f8Seschrock }
6677