spa.c revision 39c23413b8df94a95f67b34cfd4a4dfc3fd0b48d
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
2199653d4eschrock
22fa9e406ahrens/*
2339c2341eschrock * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24fa9e406ahrens * Use is subject to license terms.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens#pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e406ahrens
29fa9e406ahrens/*
30fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
31fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
32fa9e406ahrens * pool.
33fa9e406ahrens */
34fa9e406ahrens
35fa9e406ahrens#include <sys/zfs_context.h>
36ea8dc4beschrock#include <sys/fm/fs/zfs.h>
37fa9e406ahrens#include <sys/spa_impl.h>
38fa9e406ahrens#include <sys/zio.h>
39fa9e406ahrens#include <sys/zio_checksum.h>
40fa9e406ahrens#include <sys/zio_compress.h>
41fa9e406ahrens#include <sys/dmu.h>
42fa9e406ahrens#include <sys/dmu_tx.h>
43fa9e406ahrens#include <sys/zap.h>
44fa9e406ahrens#include <sys/zil.h>
45fa9e406ahrens#include <sys/vdev_impl.h>
46fa9e406ahrens#include <sys/metaslab.h>
47fa9e406ahrens#include <sys/uberblock_impl.h>
48fa9e406ahrens#include <sys/txg.h>
49fa9e406ahrens#include <sys/avl.h>
50fa9e406ahrens#include <sys/dmu_traverse.h>
51fa9e406ahrens#include <sys/unique.h>
52fa9e406ahrens#include <sys/dsl_pool.h>
53fa9e406ahrens#include <sys/dsl_dir.h>
54fa9e406ahrens#include <sys/dsl_prop.h>
55fa9e406ahrens#include <sys/fs/zfs.h>
56fa9e406ahrens#include <sys/callb.h>
57fa9e406ahrens
58416e0cdekint zio_taskq_threads = 8;
59416e0cdek
60fa9e406ahrens/*
61fa9e406ahrens * ==========================================================================
62fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
63fa9e406ahrens * ==========================================================================
64fa9e406ahrens */
65fa9e406ahrens
66ea8dc4beschrockstatic int
67ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
68ea8dc4beschrock{
69ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
70ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
71ea8dc4beschrock	int ret;
72ea8dc4beschrock
73ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
74ea8dc4beschrock	    sizeof (zbookmark_t));
75ea8dc4beschrock
76ea8dc4beschrock	if (ret < 0)
77ea8dc4beschrock		return (-1);
78ea8dc4beschrock	else if (ret > 0)
79ea8dc4beschrock		return (1);
80ea8dc4beschrock	else
81ea8dc4beschrock		return (0);
82ea8dc4beschrock}
83ea8dc4beschrock
84ea8dc4beschrock/*
85ea8dc4beschrock * Utility function which retrieves copies of the current logs and
86ea8dc4beschrock * re-initializes them in the process.
87ea8dc4beschrock */
88ea8dc4beschrockvoid
89ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
90ea8dc4beschrock{
91ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
92ea8dc4beschrock
93ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
94ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
95ea8dc4beschrock
96ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
97ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
98ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
99ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
100ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
101ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
102ea8dc4beschrock}
103ea8dc4beschrock
104fa9e406ahrens/*
105fa9e406ahrens * Activate an uninitialized pool.
106fa9e406ahrens */
107fa9e406ahrensstatic void
108fa9e406ahrensspa_activate(spa_t *spa)
109fa9e406ahrens{
110fa9e406ahrens	int t;
111fa9e406ahrens
112fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
113fa9e406ahrens
114fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
115fa9e406ahrens
116fa9e406ahrens	spa->spa_normal_class = metaslab_class_create();
117fa9e406ahrens
118fa9e406ahrens	for (t = 0; t < ZIO_TYPES; t++) {
119fa9e406ahrens		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
120416e0cdek		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
121fa9e406ahrens		    TASKQ_PREPOPULATE);
122fa9e406ahrens		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
123416e0cdek		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
124fa9e406ahrens		    TASKQ_PREPOPULATE);
125fa9e406ahrens	}
126fa9e406ahrens
127fa9e406ahrens	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
128fa9e406ahrens
1295ad8204nd	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
1305ad8204nd	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
1315ad8204nd	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
1325ad8204nd	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
1335ad8204nd	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
1345ad8204nd	mutex_init(&spa->spa_config_lock.scl_lock, NULL, MUTEX_DEFAULT, NULL);
1355ad8204nd	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
13606eeb2aek	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
1375ad8204nd
138fa9e406ahrens	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
139fa9e406ahrens	    offsetof(vdev_t, vdev_dirty_node));
140fa9e406ahrens
141fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
142fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
143ea8dc4beschrock
144ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
145ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
146ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
147ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
148ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
149ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
150fa9e406ahrens}
151fa9e406ahrens
152fa9e406ahrens/*
153fa9e406ahrens * Opposite of spa_activate().
154fa9e406ahrens */
155fa9e406ahrensstatic void
156fa9e406ahrensspa_deactivate(spa_t *spa)
157fa9e406ahrens{
158fa9e406ahrens	int t;
159fa9e406ahrens
160fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
161fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
162fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
163fa9e406ahrens
164fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
165fa9e406ahrens
166fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
167fa9e406ahrens
168fa9e406ahrens	list_destroy(&spa->spa_dirty_list);
169fa9e406ahrens
170fa9e406ahrens	rw_destroy(&spa->spa_traverse_lock);
171fa9e406ahrens
172fa9e406ahrens	for (t = 0; t < ZIO_TYPES; t++) {
173fa9e406ahrens		taskq_destroy(spa->spa_zio_issue_taskq[t]);
174fa9e406ahrens		taskq_destroy(spa->spa_zio_intr_taskq[t]);
175fa9e406ahrens		spa->spa_zio_issue_taskq[t] = NULL;
176fa9e406ahrens		spa->spa_zio_intr_taskq[t] = NULL;
177fa9e406ahrens	}
178fa9e406ahrens
179fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
180fa9e406ahrens	spa->spa_normal_class = NULL;
181fa9e406ahrens
182ea8dc4beschrock	/*
183ea8dc4beschrock	 * If this was part of an import or the open otherwise failed, we may
184ea8dc4beschrock	 * still have errors left in the queues.  Empty them just in case.
185ea8dc4beschrock	 */
186ea8dc4beschrock	spa_errlog_drain(spa);
187ea8dc4beschrock
188ea8dc4beschrock	avl_destroy(&spa->spa_errlist_scrub);
189ea8dc4beschrock	avl_destroy(&spa->spa_errlist_last);
190ea8dc4beschrock
191fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
192fa9e406ahrens}
193fa9e406ahrens
194fa9e406ahrens/*
195fa9e406ahrens * Verify a pool configuration, and construct the vdev tree appropriately.  This
196fa9e406ahrens * will create all the necessary vdevs in the appropriate layout, with each vdev
197fa9e406ahrens * in the CLOSED state.  This will prep the pool before open/creation/import.
198fa9e406ahrens * All vdev validation is done by the vdev_alloc() routine.
199fa9e406ahrens */
20099653d4eschrockstatic int
20199653d4eschrockspa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
20299653d4eschrock    uint_t id, int atype)
203fa9e406ahrens{
204fa9e406ahrens	nvlist_t **child;
205fa9e406ahrens	uint_t c, children;
20699653d4eschrock	int error;
207fa9e406ahrens
20899653d4eschrock	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
20999653d4eschrock		return (error);
210fa9e406ahrens
21199653d4eschrock	if ((*vdp)->vdev_ops->vdev_op_leaf)
21299653d4eschrock		return (0);
213fa9e406ahrens
214fa9e406ahrens	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
215fa9e406ahrens	    &child, &children) != 0) {
21699653d4eschrock		vdev_free(*vdp);
21799653d4eschrock		*vdp = NULL;
21899653d4eschrock		return (EINVAL);
219fa9e406ahrens	}
220fa9e406ahrens
221fa9e406ahrens	for (c = 0; c < children; c++) {
22299653d4eschrock		vdev_t *vd;
22399653d4eschrock		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
22499653d4eschrock		    atype)) != 0) {
22599653d4eschrock			vdev_free(*vdp);
22699653d4eschrock			*vdp = NULL;
22799653d4eschrock			return (error);
228fa9e406ahrens		}
229fa9e406ahrens	}
230fa9e406ahrens
23199653d4eschrock	ASSERT(*vdp != NULL);
23299653d4eschrock
23399653d4eschrock	return (0);
234fa9e406ahrens}
235fa9e406ahrens
236fa9e406ahrens/*
237fa9e406ahrens * Opposite of spa_load().
238fa9e406ahrens */
239fa9e406ahrensstatic void
240fa9e406ahrensspa_unload(spa_t *spa)
241fa9e406ahrens{
24299653d4eschrock	int i;
24399653d4eschrock
244fa9e406ahrens	/*
245ea8dc4beschrock	 * Stop async tasks.
246ea8dc4beschrock	 */
247ea8dc4beschrock	spa_async_suspend(spa);
248ea8dc4beschrock
249ea8dc4beschrock	/*
250fa9e406ahrens	 * Stop syncing.
251fa9e406ahrens	 */
252fa9e406ahrens	if (spa->spa_sync_on) {
253fa9e406ahrens		txg_sync_stop(spa->spa_dsl_pool);
254fa9e406ahrens		spa->spa_sync_on = B_FALSE;
255fa9e406ahrens	}
256fa9e406ahrens
257fa9e406ahrens	/*
258fa9e406ahrens	 * Wait for any outstanding prefetch I/O to complete.
259fa9e406ahrens	 */
260ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
261ea8dc4beschrock	spa_config_exit(spa, FTAG);
262fa9e406ahrens
263fa9e406ahrens	/*
264fa9e406ahrens	 * Close the dsl pool.
265fa9e406ahrens	 */
266fa9e406ahrens	if (spa->spa_dsl_pool) {
267fa9e406ahrens		dsl_pool_close(spa->spa_dsl_pool);
268fa9e406ahrens		spa->spa_dsl_pool = NULL;
269fa9e406ahrens	}
270fa9e406ahrens
271fa9e406ahrens	/*
272fa9e406ahrens	 * Close all vdevs.
273fa9e406ahrens	 */
2740e34b6abonwick	if (spa->spa_root_vdev)
275fa9e406ahrens		vdev_free(spa->spa_root_vdev);
2760e34b6abonwick	ASSERT(spa->spa_root_vdev == NULL);
277ea8dc4beschrock
27899653d4eschrock	for (i = 0; i < spa->spa_nspares; i++)
27999653d4eschrock		vdev_free(spa->spa_spares[i]);
28099653d4eschrock	if (spa->spa_spares) {
28199653d4eschrock		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
28299653d4eschrock		spa->spa_spares = NULL;
28399653d4eschrock	}
28499653d4eschrock	if (spa->spa_sparelist) {
28599653d4eschrock		nvlist_free(spa->spa_sparelist);
28699653d4eschrock		spa->spa_sparelist = NULL;
28799653d4eschrock	}
28899653d4eschrock
289ea8dc4beschrock	spa->spa_async_suspended = 0;
290fa9e406ahrens}
291fa9e406ahrens
292fa9e406ahrens/*
29399653d4eschrock * Load (or re-load) the current list of vdevs describing the active spares for
29499653d4eschrock * this pool.  When this is called, we have some form of basic information in
29599653d4eschrock * 'spa_sparelist'.  We parse this into vdevs, try to open them, and then
29699653d4eschrock * re-generate a more complete list including status information.
29799653d4eschrock */
29899653d4eschrockstatic void
29999653d4eschrockspa_load_spares(spa_t *spa)
30099653d4eschrock{
30199653d4eschrock	nvlist_t **spares;
30299653d4eschrock	uint_t nspares;
30399653d4eschrock	int i;
30439c2341eschrock	vdev_t *vd, *tvd;
30599653d4eschrock
30699653d4eschrock	/*
30799653d4eschrock	 * First, close and free any existing spare vdevs.
30899653d4eschrock	 */
30999653d4eschrock	for (i = 0; i < spa->spa_nspares; i++) {
31039c2341eschrock		vd = spa->spa_spares[i];
31139c2341eschrock
31239c2341eschrock		/* Undo the call to spa_activate() below */
31339c2341eschrock		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL &&
31439c2341eschrock		    tvd->vdev_isspare)
31539c2341eschrock			spa_spare_remove(tvd);
31639c2341eschrock		vdev_close(vd);
31739c2341eschrock		vdev_free(vd);
31899653d4eschrock	}
31939c2341eschrock
32099653d4eschrock	if (spa->spa_spares)
32199653d4eschrock		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
32299653d4eschrock
32399653d4eschrock	if (spa->spa_sparelist == NULL)
32499653d4eschrock		nspares = 0;
32599653d4eschrock	else
32699653d4eschrock		VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
32799653d4eschrock		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
32899653d4eschrock
32999653d4eschrock	spa->spa_nspares = (int)nspares;
33099653d4eschrock	spa->spa_spares = NULL;
33199653d4eschrock
33299653d4eschrock	if (nspares == 0)
33399653d4eschrock		return;
33499653d4eschrock
33599653d4eschrock	/*
33699653d4eschrock	 * Construct the array of vdevs, opening them to get status in the
33739c2341eschrock	 * process.   For each spare, there is potentially two different vdev_t
33839c2341eschrock	 * structures associated with it: one in the list of spares (used only
33939c2341eschrock	 * for basic validation purposes) and one in the active vdev
34039c2341eschrock	 * configuration (if it's spared in).  During this phase we open and
34139c2341eschrock	 * validate each vdev on the spare list.  If the vdev also exists in the
34239c2341eschrock	 * active configuration, then we also mark this vdev as an active spare.
34399653d4eschrock	 */
34499653d4eschrock	spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP);
34599653d4eschrock	for (i = 0; i < spa->spa_nspares; i++) {
34699653d4eschrock		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
34799653d4eschrock		    VDEV_ALLOC_SPARE) == 0);
34899653d4eschrock		ASSERT(vd != NULL);
34999653d4eschrock
35099653d4eschrock		spa->spa_spares[i] = vd;
35199653d4eschrock
35239c2341eschrock		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) {
35339c2341eschrock			if (!tvd->vdev_isspare)
35439c2341eschrock				spa_spare_add(tvd);
35539c2341eschrock
35639c2341eschrock			/*
35739c2341eschrock			 * We only mark the spare active if we were successfully
35839c2341eschrock			 * able to load the vdev.  Otherwise, importing a pool
35939c2341eschrock			 * with a bad active spare would result in strange
36039c2341eschrock			 * behavior, because multiple pool would think the spare
36139c2341eschrock			 * is actively in use.
36239c2341eschrock			 *
36339c2341eschrock			 * There is a vulnerability here to an equally bizarre
36439c2341eschrock			 * circumstance, where a dead active spare is later
36539c2341eschrock			 * brought back to life (onlined or otherwise).  Given
36639c2341eschrock			 * the rarity of this scenario, and the extra complexity
36739c2341eschrock			 * it adds, we ignore the possibility.
36839c2341eschrock			 */
36939c2341eschrock			if (!vdev_is_dead(tvd))
37039c2341eschrock				spa_spare_activate(tvd);
37139c2341eschrock		}
37239c2341eschrock
37399653d4eschrock		if (vdev_open(vd) != 0)
37499653d4eschrock			continue;
37599653d4eschrock
37699653d4eschrock		vd->vdev_top = vd;
37799653d4eschrock		(void) vdev_validate_spare(vd);
37899653d4eschrock	}
37999653d4eschrock
38099653d4eschrock	/*
38199653d4eschrock	 * Recompute the stashed list of spares, with status information
38299653d4eschrock	 * this time.
38399653d4eschrock	 */
38499653d4eschrock	VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
38599653d4eschrock	    DATA_TYPE_NVLIST_ARRAY) == 0);
38699653d4eschrock
38799653d4eschrock	spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP);
38899653d4eschrock	for (i = 0; i < spa->spa_nspares; i++)
38999653d4eschrock		spares[i] = vdev_config_generate(spa, spa->spa_spares[i],
39099653d4eschrock		    B_TRUE, B_TRUE);
39199653d4eschrock	VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
39299653d4eschrock	    spares, spa->spa_nspares) == 0);
39399653d4eschrock	for (i = 0; i < spa->spa_nspares; i++)
39499653d4eschrock		nvlist_free(spares[i]);
39599653d4eschrock	kmem_free(spares, spa->spa_nspares * sizeof (void *));
39699653d4eschrock}
39799653d4eschrock
39899653d4eschrockstatic int
39999653d4eschrockload_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
40099653d4eschrock{
40199653d4eschrock	dmu_buf_t *db;
40299653d4eschrock	char *packed = NULL;
40399653d4eschrock	size_t nvsize = 0;
40499653d4eschrock	int error;
40599653d4eschrock	*value = NULL;
40699653d4eschrock
40799653d4eschrock	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
40899653d4eschrock	nvsize = *(uint64_t *)db->db_data;
40999653d4eschrock	dmu_buf_rele(db, FTAG);
41099653d4eschrock
41199653d4eschrock	packed = kmem_alloc(nvsize, KM_SLEEP);
41299653d4eschrock	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
41399653d4eschrock	if (error == 0)
41499653d4eschrock		error = nvlist_unpack(packed, nvsize, value, 0);
41599653d4eschrock	kmem_free(packed, nvsize);
41699653d4eschrock
41799653d4eschrock	return (error);
41899653d4eschrock}
41999653d4eschrock
42099653d4eschrock/*
421fa9e406ahrens * Load an existing storage pool, using the pool's builtin spa_config as a
422ea8dc4beschrock * source of configuration information.
423fa9e406ahrens */
424fa9e406ahrensstatic int
425ea8dc4beschrockspa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
426fa9e406ahrens{
427fa9e406ahrens	int error = 0;
428fa9e406ahrens	nvlist_t *nvroot = NULL;
429fa9e406ahrens	vdev_t *rvd;
430fa9e406ahrens	uberblock_t *ub = &spa->spa_uberblock;
4310373e76bonwick	uint64_t config_cache_txg = spa->spa_config_txg;
432fa9e406ahrens	uint64_t pool_guid;
43399653d4eschrock	uint64_t version;
434fa9e406ahrens	zio_t *zio;
435fa9e406ahrens
436ea8dc4beschrock	spa->spa_load_state = state;
4370373e76bonwick
438fa9e406ahrens	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
439a9926bfbonwick	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
440ea8dc4beschrock		error = EINVAL;
441ea8dc4beschrock		goto out;
442ea8dc4beschrock	}
443fa9e406ahrens
44499653d4eschrock	/*
44599653d4eschrock	 * Versioning wasn't explicitly added to the label until later, so if
44699653d4eschrock	 * it's not present treat it as the initial version.
44799653d4eschrock	 */
44899653d4eschrock	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
44999653d4eschrock		version = ZFS_VERSION_INITIAL;
45099653d4eschrock
451a9926bfbonwick	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
452a9926bfbonwick	    &spa->spa_config_txg);
453a9926bfbonwick
4540373e76bonwick	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
455ea8dc4beschrock	    spa_guid_exists(pool_guid, 0)) {
456ea8dc4beschrock		error = EEXIST;
457ea8dc4beschrock		goto out;
458ea8dc4beschrock	}
459fa9e406ahrens
460b5989eceschrock	spa->spa_load_guid = pool_guid;
461b5989eceschrock
462fa9e406ahrens	/*
46399653d4eschrock	 * Parse the configuration into a vdev tree.  We explicitly set the
46499653d4eschrock	 * value that will be returned by spa_version() since parsing the
46599653d4eschrock	 * configuration requires knowing the version number.
466fa9e406ahrens	 */
467ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
46899653d4eschrock	spa->spa_ubsync.ub_version = version;
46999653d4eschrock	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
470ea8dc4beschrock	spa_config_exit(spa, FTAG);
471fa9e406ahrens
47299653d4eschrock	if (error != 0)
473ea8dc4beschrock		goto out;
474fa9e406ahrens
4750e34b6abonwick	ASSERT(spa->spa_root_vdev == rvd);
476fa9e406ahrens	ASSERT(spa_guid(spa) == pool_guid);
477fa9e406ahrens
478fa9e406ahrens	/*
479fa9e406ahrens	 * Try to open all vdevs, loading each label in the process.
480fa9e406ahrens	 */
481ea8dc4beschrock	if (vdev_open(rvd) != 0) {
482ea8dc4beschrock		error = ENXIO;
483ea8dc4beschrock		goto out;
484ea8dc4beschrock	}
485fa9e406ahrens
486fa9e406ahrens	/*
487560e6e9eschrock	 * Validate the labels for all leaf vdevs.  We need to grab the config
488