spa.c revision 416e0cd82df71e380f21199abde8c5f7a18bac51
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/*
23c67d967eschrock * Copyright 2006 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;
30499653d4eschrock
30599653d4eschrock	/*
30699653d4eschrock	 * First, close and free any existing spare vdevs.
30799653d4eschrock	 */
30899653d4eschrock	for (i = 0; i < spa->spa_nspares; i++) {
30999653d4eschrock		vdev_close(spa->spa_spares[i]);
31099653d4eschrock		vdev_free(spa->spa_spares[i]);
31199653d4eschrock	}
31299653d4eschrock	if (spa->spa_spares)
31399653d4eschrock		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
31499653d4eschrock
31599653d4eschrock	if (spa->spa_sparelist == NULL)
31699653d4eschrock		nspares = 0;
31799653d4eschrock	else
31899653d4eschrock		VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
31999653d4eschrock		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
32099653d4eschrock
32199653d4eschrock	spa->spa_nspares = (int)nspares;
32299653d4eschrock	spa->spa_spares = NULL;
32399653d4eschrock
32499653d4eschrock	if (nspares == 0)
32599653d4eschrock		return;
32699653d4eschrock
32799653d4eschrock	/*
32899653d4eschrock	 * Construct the array of vdevs, opening them to get status in the
32999653d4eschrock	 * process.
33099653d4eschrock	 */
33199653d4eschrock	spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP);
33299653d4eschrock	for (i = 0; i < spa->spa_nspares; i++) {
33399653d4eschrock		vdev_t *vd;
33499653d4eschrock
33599653d4eschrock		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
33699653d4eschrock		    VDEV_ALLOC_SPARE) == 0);
33799653d4eschrock		ASSERT(vd != NULL);
33899653d4eschrock
33999653d4eschrock		spa->spa_spares[i] = vd;
34099653d4eschrock
34199653d4eschrock		if (vdev_open(vd) != 0)
34299653d4eschrock			continue;
34399653d4eschrock
34499653d4eschrock		vd->vdev_top = vd;
34599653d4eschrock		(void) vdev_validate_spare(vd);
34699653d4eschrock	}
34799653d4eschrock
34899653d4eschrock	/*
34999653d4eschrock	 * Recompute the stashed list of spares, with status information
35099653d4eschrock	 * this time.
35199653d4eschrock	 */
35299653d4eschrock	VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
35399653d4eschrock	    DATA_TYPE_NVLIST_ARRAY) == 0);
35499653d4eschrock
35599653d4eschrock	spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP);
35699653d4eschrock	for (i = 0; i < spa->spa_nspares; i++)
35799653d4eschrock		spares[i] = vdev_config_generate(spa, spa->spa_spares[i],
35899653d4eschrock		    B_TRUE, B_TRUE);
35999653d4eschrock	VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
36099653d4eschrock	    spares, spa->spa_nspares) == 0);
36199653d4eschrock	for (i = 0; i < spa->spa_nspares; i++)
36299653d4eschrock		nvlist_free(spares[i]);
36399653d4eschrock	kmem_free(spares, spa->spa_nspares * sizeof (void *));
36499653d4eschrock}
36599653d4eschrock
36699653d4eschrockstatic int
36799653d4eschrockload_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
36899653d4eschrock{
36999653d4eschrock	dmu_buf_t *db;
37099653d4eschrock	char *packed = NULL;
37199653d4eschrock	size_t nvsize = 0;
37299653d4eschrock	int error;
37399653d4eschrock	*value = NULL;
37499653d4eschrock
37599653d4eschrock	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
37699653d4eschrock	nvsize = *(uint64_t *)db->db_data;
37799653d4eschrock	dmu_buf_rele(db, FTAG);
37899653d4eschrock
37999653d4eschrock	packed = kmem_alloc(nvsize, KM_SLEEP);
38099653d4eschrock	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
38199653d4eschrock	if (error == 0)
38299653d4eschrock		error = nvlist_unpack(packed, nvsize, value, 0);
38399653d4eschrock	kmem_free(packed, nvsize);
38499653d4eschrock
38599653d4eschrock	return (error);
38699653d4eschrock}
38799653d4eschrock
38899653d4eschrock/*
389fa9e406ahrens * Load an existing storage pool, using the pool's builtin spa_config as a
390ea8dc4beschrock * source of configuration information.
391fa9e406ahrens */
392fa9e406ahrensstatic int
393ea8dc4beschrockspa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
394fa9e406ahrens{
395fa9e406ahrens	int error = 0;
396fa9e406ahrens	nvlist_t *nvroot = NULL;
397fa9e406ahrens	vdev_t *rvd;
398fa9e406ahrens	uberblock_t *ub = &spa->spa_uberblock;
3990373e76bonwick	uint64_t config_cache_txg = spa->spa_config_txg;
400fa9e406ahrens	uint64_t pool_guid;
40199653d4eschrock	uint64_t version;
402fa9e406ahrens	zio_t *zio;
403fa9e406ahrens
404ea8dc4beschrock	spa->spa_load_state = state;
4050373e76bonwick
406fa9e406ahrens	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
407a9926bfbonwick	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
408ea8dc4beschrock		error = EINVAL;
409ea8dc4beschrock		goto out;
410ea8dc4beschrock	}
411fa9e406ahrens
41299653d4eschrock	/*
41399653d4eschrock	 * Versioning wasn't explicitly added to the label until later, so if
41499653d4eschrock	 * it's not present treat it as the initial version.
41599653d4eschrock	 */
41699653d4eschrock	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
41799653d4eschrock		version = ZFS_VERSION_INITIAL;
41899653d4eschrock
419a9926bfbonwick	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
420a9926bfbonwick	    &spa->spa_config_txg);
421a9926bfbonwick
4220373e76bonwick	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
423ea8dc4beschrock	    spa_guid_exists(pool_guid, 0)) {
424ea8dc4beschrock		error = EEXIST;
425ea8dc4beschrock		goto out;
426ea8dc4beschrock	}
427fa9e406ahrens
428b5989eceschrock	spa->spa_load_guid = pool_guid;
429b5989eceschrock
430fa9e406ahrens	/*
43199653d4eschrock	 * Parse the configuration into a vdev tree.  We explicitly set the
43299653d4eschrock	 * value that will be returned by spa_version() since parsing the
43399653d4eschrock	 * configuration requires knowing the version number.
434fa9e406ahrens	 */
435ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
43699653d4eschrock	spa->spa_ubsync.ub_version = version;
43799653d4eschrock	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
438ea8dc4beschrock	spa_config_exit(spa, FTAG);
439fa9e406ahrens
44099653d4eschrock	if (error != 0)
441ea8dc4beschrock		goto out;
442fa9e406ahrens
4430e34b6abonwick	ASSERT(spa->spa_root_vdev == rvd);
444fa9e406ahrens	ASSERT(spa_guid(spa) == pool_guid);
445fa9e406ahrens
446fa9e406ahrens	/*
447fa9e406ahrens	 * Try to open all vdevs, loading each label in the process.
448fa9e406ahrens	 */
449ea8dc4beschrock	if (vdev_open(rvd) != 0) {
450ea8dc4beschrock		error = ENXIO;
451ea8dc4beschrock		goto out;
452ea8dc4beschrock	}
453fa9e406ahrens
454fa9e406ahrens	/*
455560e6e9eschrock	 * Validate the labels for all leaf vdevs.  We need to grab the config
456560e6e9eschrock	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
457560e6e9eschrock	 * flag.
458560e6e9eschrock	 */
459560e6e9eschrock	spa_config_enter(spa, RW_READER, FTAG);
460560e6e9eschrock	error = vdev_validate(rvd);
461560e6e9eschrock	spa_config_exit(spa, FTAG);
462560e6e9eschrock
463560e6e9eschrock	if (error != 0) {
464560e6e9eschrock		error = EBADF;
465560e6e9eschrock		goto out;
466560e6e9eschrock	}
467560e6e9eschrock
468560e6e9eschrock	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
469560e6e9eschrock		error = ENXIO;
470560e6e9eschrock		goto out;
471560e6e9eschrock	}
472560e6e9eschrock
473560e6e9eschrock	/*
474fa9e406ahrens	 * Find the best uberblock.
475fa9e406ahrens	 */
476fa9e406ahrens	bzero(ub, sizeof (uberblock_t));
477fa9e406ahrens
478fa9e406ahrens	zio = zio_root(spa, NULL, NULL,
479fa9e406ahrens	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
480fa9e406ahrens	vdev_uberblock_load(zio, rvd, ub);
481fa9e406ahrens	error = zio_wait(zio);
482fa9e406ahrens
483fa9e406ahrens	/*
484fa9e406ahrens	 * If we weren't able to find a single valid uberblock, return failure.
485fa9e406ahrens	 */
486fa9e406ahrens	if (ub->ub_txg == 0) {
487eaca9bbeschrock		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
488eaca9bbeschrock		    VDEV_AUX_CORRUPT_DATA);
489ea8dc4beschrock		error = ENXIO;
490ea8dc4beschrock		goto out;
491ea8dc4beschrock	}
492ea8dc4beschrock
493ea8dc4beschrock	/*
494ea8dc4beschrock	 * If the pool is newer than the code, we can't open it.
495ea8dc4beschrock	 */
496