xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 21bf64a78855d076f09716ea1c06175d954e934c)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * 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  */
21fa9e4066Sahrens /*
22*21bf64a7Sgw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Virtual Device Labels
30fa9e4066Sahrens  * ---------------------
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * The vdev label serves several distinct purposes:
33fa9e4066Sahrens  *
34fa9e4066Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
35fa9e4066Sahrens  *	   identity within the pool.
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * 	2. Verify that all the devices given in a configuration are present
38fa9e4066Sahrens  *         within the pool.
39fa9e4066Sahrens  *
40fa9e4066Sahrens  * 	3. Determine the uberblock for the pool.
41fa9e4066Sahrens  *
42fa9e4066Sahrens  * 	4. In case of an import operation, determine the configuration of the
43fa9e4066Sahrens  *         toplevel vdev of which it is a part.
44fa9e4066Sahrens  *
45fa9e4066Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
46fa9e4066Sahrens  *         provide enough information to the administrator to determine which
47fa9e4066Sahrens  *         devices are missing.
48fa9e4066Sahrens  *
49fa9e4066Sahrens  * It is important to note that while the kernel is responsible for writing the
50fa9e4066Sahrens  * label, it only consumes the information in the first three cases.  The
51fa9e4066Sahrens  * latter information is only consumed in userland when determining the
52fa9e4066Sahrens  * configuration to import a pool.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * Label Organization
56fa9e4066Sahrens  * ------------------
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * Before describing the contents of the label, it's important to understand how
59fa9e4066Sahrens  * the labels are written and updated with respect to the uberblock.
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * When the pool configuration is altered, either because it was newly created
62fa9e4066Sahrens  * or a device was added, we want to update all the labels such that we can deal
63fa9e4066Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
64fa9e4066Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
653d7072f8Seschrock  * labels and an uberblock with the following transaction groups:
66fa9e4066Sahrens  *
67fa9e4066Sahrens  *              L1          UB          L2
68fa9e4066Sahrens  *           +------+    +------+    +------+
69fa9e4066Sahrens  *           |      |    |      |    |      |
70fa9e4066Sahrens  *           | t10  |    | t10  |    | t10  |
71fa9e4066Sahrens  *           |      |    |      |    |      |
72fa9e4066Sahrens  *           +------+    +------+    +------+
73fa9e4066Sahrens  *
74fa9e4066Sahrens  * In this stable state, the labels and the uberblock were all updated within
75fa9e4066Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
76fa9e4066Sahrens  * that we can detect when we fail partway through writing the label.
77fa9e4066Sahrens  *
78fa9e4066Sahrens  * In order to identify which labels are valid, the labels are written in the
79fa9e4066Sahrens  * following manner:
80fa9e4066Sahrens  *
81fa9e4066Sahrens  * 	1. For each vdev, update 'L1' to the new label
82fa9e4066Sahrens  * 	2. Update the uberblock
83fa9e4066Sahrens  * 	3. For each vdev, update 'L2' to the new label
84fa9e4066Sahrens  *
85fa9e4066Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
86fa9e4066Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
87fa9e4066Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
88fa9e4066Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
89fa9e4066Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
90fa9e4066Sahrens  * therefore L1 is valid.
91fa9e4066Sahrens  *
92fa9e4066Sahrens  * Another added complexity is that not every label is updated when the config
93fa9e4066Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
94fa9e4066Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
95fa9e4066Sahrens  * be older than the pool uberblock, because the necessary information is stored
96fa9e4066Sahrens  * on another vdev.
97fa9e4066Sahrens  *
98fa9e4066Sahrens  *
99fa9e4066Sahrens  * On-disk Format
100fa9e4066Sahrens  * --------------
101fa9e4066Sahrens  *
102fa9e4066Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
103fa9e4066Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
104fa9e4066Sahrens  * VTOC disk labels, but is otherwise ignored.
105fa9e4066Sahrens  *
106fa9e4066Sahrens  * The first half of the label is a packed nvlist which contains pool wide
107fa9e4066Sahrens  * properties, per-vdev properties, and configuration information.  It is
108fa9e4066Sahrens  * described in more detail below.
109fa9e4066Sahrens  *
110fa9e4066Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
111fa9e4066Sahrens  * These uberblocks are updated whenever a transaction group is committed,
112fa9e4066Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
113fa9e4066Sahrens  * vdev for the 'best' uberblock.
114fa9e4066Sahrens  *
115fa9e4066Sahrens  *
116fa9e4066Sahrens  * Configuration Information
117fa9e4066Sahrens  * -------------------------
118fa9e4066Sahrens  *
119fa9e4066Sahrens  * The nvlist describing the pool and vdev contains the following elements:
120fa9e4066Sahrens  *
121fa9e4066Sahrens  * 	version		ZFS on-disk version
122fa9e4066Sahrens  * 	name		Pool name
123fa9e4066Sahrens  * 	state		Pool state
124fa9e4066Sahrens  * 	txg		Transaction group in which this label was written
125fa9e4066Sahrens  * 	pool_guid	Unique identifier for this pool
126fa9e4066Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
127fa9e4066Sahrens  *
128fa9e4066Sahrens  * Each leaf device label also contains the following:
129fa9e4066Sahrens  *
130fa9e4066Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
131fa9e4066Sahrens  * 	guid		Unique ID for the leaf vdev
132fa9e4066Sahrens  *
133fa9e4066Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
134fa9e4066Sahrens  */
135fa9e4066Sahrens 
136fa9e4066Sahrens #include <sys/zfs_context.h>
137fa9e4066Sahrens #include <sys/spa.h>
138fa9e4066Sahrens #include <sys/spa_impl.h>
139fa9e4066Sahrens #include <sys/dmu.h>
140fa9e4066Sahrens #include <sys/zap.h>
141fa9e4066Sahrens #include <sys/vdev.h>
142fa9e4066Sahrens #include <sys/vdev_impl.h>
143fa9e4066Sahrens #include <sys/uberblock_impl.h>
144fa9e4066Sahrens #include <sys/metaslab.h>
145fa9e4066Sahrens #include <sys/zio.h>
146fa9e4066Sahrens #include <sys/fs/zfs.h>
147fa9e4066Sahrens 
148fa9e4066Sahrens /*
149fa9e4066Sahrens  * Basic routines to read and write from a vdev label.
150fa9e4066Sahrens  * Used throughout the rest of this file.
151fa9e4066Sahrens  */
152fa9e4066Sahrens uint64_t
153fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
154fa9e4066Sahrens {
155ecc2d604Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
156e7437265Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
157ecc2d604Sbonwick 
158fa9e4066Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
159fa9e4066Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
160fa9e4066Sahrens }
161fa9e4066Sahrens 
162*21bf64a7Sgw /*
163*21bf64a7Sgw  * Returns back the vdev label associated with the passed in offset.
164*21bf64a7Sgw  */
165*21bf64a7Sgw int
166*21bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset)
167*21bf64a7Sgw {
168*21bf64a7Sgw 	int l;
169*21bf64a7Sgw 
170*21bf64a7Sgw 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
171*21bf64a7Sgw 		offset -= psize - VDEV_LABEL_END_SIZE;
172*21bf64a7Sgw 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
173*21bf64a7Sgw 	}
174*21bf64a7Sgw 	l = offset / sizeof (vdev_label_t);
175*21bf64a7Sgw 	return (l < VDEV_LABELS ? l : -1);
176*21bf64a7Sgw }
177*21bf64a7Sgw 
178fa9e4066Sahrens static void
179fa9e4066Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
180fa9e4066Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
181fa9e4066Sahrens {
182fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
183fa9e4066Sahrens 
184fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
185fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
186fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
187ea8dc4b6Seschrock 	    ZIO_PRIORITY_SYNC_READ,
188fa94a07fSbrendan 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
189fa94a07fSbrendan 	    B_TRUE));
190fa9e4066Sahrens }
191fa9e4066Sahrens 
192fa9e4066Sahrens static void
193fa9e4066Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
19417f17c2dSbonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
195fa9e4066Sahrens {
196fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
197fa9e4066Sahrens 
198fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
199fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
200fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
20117f17c2dSbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
202fa9e4066Sahrens }
203fa9e4066Sahrens 
204fa9e4066Sahrens /*
205fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
206fa9e4066Sahrens  */
207fa9e4066Sahrens nvlist_t *
20899653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
209fa94a07fSbrendan     boolean_t isspare, boolean_t isl2cache)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	nvlist_t *nv = NULL;
212fa9e4066Sahrens 
213ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
214fa9e4066Sahrens 
215fa9e4066Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
216fa9e4066Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
217fa94a07fSbrendan 	if (!isspare && !isl2cache)
21899653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
21999653d4eSeschrock 		    == 0);
220fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
221fa9e4066Sahrens 
222fa9e4066Sahrens 	if (vd->vdev_path != NULL)
223fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
224fa9e4066Sahrens 		    vd->vdev_path) == 0);
225fa9e4066Sahrens 
226fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
227fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
228fa9e4066Sahrens 		    vd->vdev_devid) == 0);
229fa9e4066Sahrens 
2303d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
2313d7072f8Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2323d7072f8Seschrock 		    vd->vdev_physpath) == 0);
2333d7072f8Seschrock 
23499653d4eSeschrock 	if (vd->vdev_nparity != 0) {
23599653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
23699653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
23799653d4eSeschrock 
23899653d4eSeschrock 		/*
23999653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
24099653d4eSeschrock 		 * into a crufty old storage pool.
24199653d4eSeschrock 		 */
24299653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
24399653d4eSeschrock 		    (vd->vdev_nparity == 2 &&
244e7437265Sahrens 		    spa_version(spa) >= SPA_VERSION_RAID6));
24599653d4eSeschrock 
24699653d4eSeschrock 		/*
24799653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
24899653d4eSeschrock 		 * that only support a single parity device -- older software
24999653d4eSeschrock 		 * will just ignore it.
25099653d4eSeschrock 		 */
25199653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
25299653d4eSeschrock 		    vd->vdev_nparity) == 0);
25399653d4eSeschrock 	}
25499653d4eSeschrock 
255afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
256afefbcddSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
257afefbcddSeschrock 		    vd->vdev_wholedisk) == 0);
258afefbcddSeschrock 
259ea8dc4b6Seschrock 	if (vd->vdev_not_present)
260ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
261ea8dc4b6Seschrock 
26299653d4eSeschrock 	if (vd->vdev_isspare)
26399653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
26499653d4eSeschrock 
265fa94a07fSbrendan 	if (!isspare && !isl2cache && vd == vd->vdev_top) {
266fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
267fa9e4066Sahrens 		    vd->vdev_ms_array) == 0);
268fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
269fa9e4066Sahrens 		    vd->vdev_ms_shift) == 0);
270fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
271fa9e4066Sahrens 		    vd->vdev_ashift) == 0);
272fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
273fa9e4066Sahrens 		    vd->vdev_asize) == 0);
2748654d025Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2758654d025Sperrin 		    vd->vdev_islog) == 0);
276fa9e4066Sahrens 	}
277fa9e4066Sahrens 
278fa9e4066Sahrens 	if (vd->vdev_dtl.smo_object != 0)
279fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
280fa9e4066Sahrens 		    vd->vdev_dtl.smo_object) == 0);
281fa9e4066Sahrens 
282fa9e4066Sahrens 	if (getstats) {
283fa9e4066Sahrens 		vdev_stat_t vs;
284fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
285fa9e4066Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
286fa9e4066Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
287fa9e4066Sahrens 	}
288fa9e4066Sahrens 
289fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
290fa9e4066Sahrens 		nvlist_t **child;
291fa9e4066Sahrens 		int c;
292fa9e4066Sahrens 
293fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
294fa9e4066Sahrens 		    KM_SLEEP);
295fa9e4066Sahrens 
296fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
29799653d4eSeschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
298fa94a07fSbrendan 			    getstats, isspare, isl2cache);
299fa9e4066Sahrens 
300fa9e4066Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
301fa9e4066Sahrens 		    child, vd->vdev_children) == 0);
302fa9e4066Sahrens 
303fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
304fa9e4066Sahrens 			nvlist_free(child[c]);
305fa9e4066Sahrens 
306fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
307441d80aaSlling 
308441d80aaSlling 	} else {
309ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
310441d80aaSlling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
311ecc2d604Sbonwick 			    B_TRUE) == 0);
3123d7072f8Seschrock 		if (vd->vdev_faulted)
3133d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
3143d7072f8Seschrock 			    B_TRUE) == 0);
3153d7072f8Seschrock 		if (vd->vdev_degraded)
3163d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3173d7072f8Seschrock 			    B_TRUE) == 0);
3183d7072f8Seschrock 		if (vd->vdev_removed)
3193d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3203d7072f8Seschrock 			    B_TRUE) == 0);
3213d7072f8Seschrock 		if (vd->vdev_unspare)
3223d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3233d7072f8Seschrock 			    B_TRUE) == 0);
324fa9e4066Sahrens 	}
325fa9e4066Sahrens 
326fa9e4066Sahrens 	return (nv);
327fa9e4066Sahrens }
328fa9e4066Sahrens 
329fa9e4066Sahrens nvlist_t *
330fa9e4066Sahrens vdev_label_read_config(vdev_t *vd)
331fa9e4066Sahrens {
3320373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
333fa9e4066Sahrens 	nvlist_t *config = NULL;
334fa9e4066Sahrens 	vdev_phys_t *vp;
335fa9e4066Sahrens 	zio_t *zio;
336fa9e4066Sahrens 	int l;
337fa9e4066Sahrens 
33891ebeef5Sahrens 	ASSERT(spa_config_held(spa, RW_READER) ||
33991ebeef5Sahrens 	    spa_config_held(spa, RW_WRITER));
3400373e76bSbonwick 
3410a4e9518Sgw 	if (!vdev_readable(vd))
342fa9e4066Sahrens 		return (NULL);
343fa9e4066Sahrens 
344fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
345fa9e4066Sahrens 
346fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
347fa9e4066Sahrens 
3480373e76bSbonwick 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL |
349ea8dc4b6Seschrock 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
350fa9e4066Sahrens 
351fa9e4066Sahrens 		vdev_label_read(zio, vd, l, vp,
352fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
353fa9e4066Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
354fa9e4066Sahrens 
355fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
356fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
357ea8dc4b6Seschrock 		    &config, 0) == 0)
358fa9e4066Sahrens 			break;
359fa9e4066Sahrens 
360fa9e4066Sahrens 		if (config != NULL) {
361fa9e4066Sahrens 			nvlist_free(config);
362fa9e4066Sahrens 			config = NULL;
363fa9e4066Sahrens 		}
364fa9e4066Sahrens 	}
365fa9e4066Sahrens 
366fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
367fa9e4066Sahrens 
368fa9e4066Sahrens 	return (config);
369fa9e4066Sahrens }
370fa9e4066Sahrens 
37139c23413Seschrock /*
37239c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
37339c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
37439c23413Seschrock  */
37539c23413Seschrock static boolean_t
37639c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
377fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
37839c23413Seschrock {
37939c23413Seschrock 	spa_t *spa = vd->vdev_spa;
38039c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
38139c23413Seschrock 	uint64_t vdtxg = 0;
38239c23413Seschrock 	nvlist_t *label;
38339c23413Seschrock 
38439c23413Seschrock 	if (spare_guid)
38539c23413Seschrock 		*spare_guid = 0ULL;
386fa94a07fSbrendan 	if (l2cache_guid)
387fa94a07fSbrendan 		*l2cache_guid = 0ULL;
38839c23413Seschrock 
38939c23413Seschrock 	/*
39039c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
39139c23413Seschrock 	 */
39239c23413Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
39339c23413Seschrock 		return (B_FALSE);
39439c23413Seschrock 
39539c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
39639c23413Seschrock 	    &vdtxg);
39739c23413Seschrock 
39839c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
39939c23413Seschrock 	    &state) != 0 ||
40039c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
40139c23413Seschrock 	    &device_guid) != 0) {
40239c23413Seschrock 		nvlist_free(label);
40339c23413Seschrock 		return (B_FALSE);
40439c23413Seschrock 	}
40539c23413Seschrock 
406fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
40739c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
40839c23413Seschrock 	    &pool_guid) != 0 ||
40939c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
41039c23413Seschrock 	    &txg) != 0)) {
41139c23413Seschrock 		nvlist_free(label);
41239c23413Seschrock 		return (B_FALSE);
41339c23413Seschrock 	}
41439c23413Seschrock 
41539c23413Seschrock 	nvlist_free(label);
41639c23413Seschrock 
41739c23413Seschrock 	/*
41839c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
41939c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
42039c23413Seschrock 	 * spare (which we check for later on).
42139c23413Seschrock 	 */
422fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
42339c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
424fa94a07fSbrendan 	    !spa_spare_exists(device_guid, NULL) &&
425fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
42639c23413Seschrock 		return (B_FALSE);
42739c23413Seschrock 
42839c23413Seschrock 	/*
42939c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
43039c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
43139c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
43239c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
43339c23413Seschrock 	 * transaction.
43439c23413Seschrock 	 */
435fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
436fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
43739c23413Seschrock 		return (B_TRUE);
43839c23413Seschrock 
43939c23413Seschrock 	/*
44039c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
44139c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
442fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
44339c23413Seschrock 	 */
44439c23413Seschrock 	if (spa_spare_exists(device_guid, &spare_pool) ||
44539c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
44639c23413Seschrock 		if (spare_guid)
44739c23413Seschrock 			*spare_guid = device_guid;
44839c23413Seschrock 
44939c23413Seschrock 		switch (reason) {
45039c23413Seschrock 		case VDEV_LABEL_CREATE:
451fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
45239c23413Seschrock 			return (B_TRUE);
45339c23413Seschrock 
45439c23413Seschrock 		case VDEV_LABEL_REPLACE:
45539c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
45639c23413Seschrock 			    spare_pool != 0ULL);
45739c23413Seschrock 
45839c23413Seschrock 		case VDEV_LABEL_SPARE:
45939c23413Seschrock 			return (spa_has_spare(spa, device_guid));
46039c23413Seschrock 		}
46139c23413Seschrock 	}
46239c23413Seschrock 
463fa94a07fSbrendan 	/*
464fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
465fa94a07fSbrendan 	 */
466fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
467fa94a07fSbrendan 		return (B_TRUE);
468fa94a07fSbrendan 
46939c23413Seschrock 	/*
47039c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
47139c23413Seschrock 	 * pool on the system.
47239c23413Seschrock 	 */
47339c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
47439c23413Seschrock }
47539c23413Seschrock 
47639c23413Seschrock /*
47739c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
47839c23413Seschrock  * use, and writable.  We put down an initial label which we will later
47939c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
48039c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
48139c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
48239c23413Seschrock  * itself.
48339c23413Seschrock  */
48439c23413Seschrock int
48539c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
486fa9e4066Sahrens {
487fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
488fa9e4066Sahrens 	nvlist_t *label;
489fa9e4066Sahrens 	vdev_phys_t *vp;
490fa9e4066Sahrens 	vdev_boot_header_t *vb;
491ecc2d604Sbonwick 	uberblock_t *ub;
492fa9e4066Sahrens 	zio_t *zio;
493fa9e4066Sahrens 	int l, c, n;
494fa9e4066Sahrens 	char *buf;
495fa9e4066Sahrens 	size_t buflen;
496fa9e4066Sahrens 	int error;
497fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
49817f17c2dSbonwick 	int flags = ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL;
499fa9e4066Sahrens 
5000373e76bSbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
5010373e76bSbonwick 
502fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
50339c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
50439c23413Seschrock 		    crtxg, reason)) != 0)
505fa9e4066Sahrens 			return (error);
506fa9e4066Sahrens 
507fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
508fa9e4066Sahrens 		return (0);
509fa9e4066Sahrens 
510fa9e4066Sahrens 	/*
51139c23413Seschrock 	 * Dead vdevs cannot be initialized.
512fa9e4066Sahrens 	 */
513fa9e4066Sahrens 	if (vdev_is_dead(vd))
514fa9e4066Sahrens 		return (EIO);
515fa9e4066Sahrens 
516fa9e4066Sahrens 	/*
51739c23413Seschrock 	 * Determine if the vdev is in use.
518fa9e4066Sahrens 	 */
51939c23413Seschrock 	if (reason != VDEV_LABEL_REMOVE &&
520fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
52139c23413Seschrock 		return (EBUSY);
52239c23413Seschrock 
52339c23413Seschrock 	ASSERT(reason != VDEV_LABEL_REMOVE ||
524fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, NULL, NULL));
52539c23413Seschrock 
52639c23413Seschrock 	/*
527fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
528fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
529fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
530fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
53139c23413Seschrock 	 */
532fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
533fa94a07fSbrendan 	    spare_guid != 0ULL) {
53439c23413Seschrock 		vdev_t *pvd = vd->vdev_parent;
53539c23413Seschrock 
53639c23413Seschrock 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
53739c23413Seschrock 			pvd->vdev_guid_sum -= vd->vdev_guid;
53839c23413Seschrock 			pvd->vdev_guid_sum += spare_guid;
539fa9e4066Sahrens 		}
54099653d4eSeschrock 
54139c23413Seschrock 		vd->vdev_guid = vd->vdev_guid_sum = spare_guid;
54239c23413Seschrock 
54399653d4eSeschrock 		/*
54439c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
54539c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
5463d7072f8Seschrock 		 * labeled appropriately and we can just return.
54799653d4eSeschrock 		 */
54839c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
54939c23413Seschrock 			return (0);
55039c23413Seschrock 		ASSERT(reason == VDEV_LABEL_REPLACE);
551fa9e4066Sahrens 	}
552fa9e4066Sahrens 
553fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
554fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
555fa94a07fSbrendan 		vdev_t *pvd = vd->vdev_parent;
556fa94a07fSbrendan 
557fa94a07fSbrendan 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
558fa94a07fSbrendan 			pvd->vdev_guid_sum -= vd->vdev_guid;
559fa94a07fSbrendan 			pvd->vdev_guid_sum += l2cache_guid;
560fa94a07fSbrendan 		}
561fa94a07fSbrendan 
562fa94a07fSbrendan 		vd->vdev_guid = vd->vdev_guid_sum = l2cache_guid;
563fa94a07fSbrendan 
564fa94a07fSbrendan 		/*
565fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
566fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
567fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
568fa94a07fSbrendan 		 */
569fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
570fa94a07fSbrendan 			return (0);
571fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
572fa94a07fSbrendan 	}
573fa94a07fSbrendan 
574fa9e4066Sahrens 	/*
57539c23413Seschrock 	 * Initialize its label.
576fa9e4066Sahrens 	 */
577fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
578fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
579fa9e4066Sahrens 
580fa9e4066Sahrens 	/*
581fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
582fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
583fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
584fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
585fa9e4066Sahrens 	 */
58639c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
58739c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
58839c23413Seschrock 		/*
58939c23413Seschrock 		 * For inactive hot spares, we generate a special label that
59039c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
59139c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
59239c23413Seschrock 		 * active hot spare (in which case we want to revert the
59339c23413Seschrock 		 * labels).
59439c23413Seschrock 		 */
59599653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
59699653d4eSeschrock 
59799653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
59899653d4eSeschrock 		    spa_version(spa)) == 0);
59999653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
60099653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
60199653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
60299653d4eSeschrock 		    vd->vdev_guid) == 0);
603fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
604fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
605fa94a07fSbrendan 		/*
606fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
607fa94a07fSbrendan 		 */
608fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
609fa94a07fSbrendan 
610fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
611fa94a07fSbrendan 		    spa_version(spa)) == 0);
612fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
613fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
614fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
615fa94a07fSbrendan 		    vd->vdev_guid) == 0);
61699653d4eSeschrock 	} else {
61799653d4eSeschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
61899653d4eSeschrock 
61999653d4eSeschrock 		/*
62099653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
62199653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
62299653d4eSeschrock 		 * fail.
62399653d4eSeschrock 		 */
62499653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
62599653d4eSeschrock 		    crtxg) == 0);
62699653d4eSeschrock 	}
627fa9e4066Sahrens 
628fa9e4066Sahrens 	buf = vp->vp_nvlist;
629fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
630fa9e4066Sahrens 
631a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
632a75573b6Smmusante 	if (error != 0) {
633fa9e4066Sahrens 		nvlist_free(label);
634fa9e4066Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
635a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
636a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
637fa9e4066Sahrens 	}
638fa9e4066Sahrens 
639fa9e4066Sahrens 	/*
640fa9e4066Sahrens 	 * Initialize boot block header.
641fa9e4066Sahrens 	 */
642fa9e4066Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
643fa9e4066Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
644fa9e4066Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
645fa9e4066Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
646fa9e4066Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
647fa9e4066Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
648fa9e4066Sahrens 
649fa9e4066Sahrens 	/*
650fa9e4066Sahrens 	 * Initialize uberblock template.
651fa9e4066Sahrens 	 */
652ecc2d604Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
653ecc2d604Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
654ecc2d604Sbonwick 	*ub = spa->spa_uberblock;
655ecc2d604Sbonwick 	ub->ub_txg = 0;
656fa9e4066Sahrens 
657fa9e4066Sahrens 	/*
658fa9e4066Sahrens 	 * Write everything in parallel.
659fa9e4066Sahrens 	 */
66017f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
661fa9e4066Sahrens 
662fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
663fa9e4066Sahrens 
664fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
665fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
66617f17c2dSbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
667fa9e4066Sahrens 
668fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vb,
669fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
67017f17c2dSbonwick 		    sizeof (vdev_boot_header_t), NULL, NULL, flags);
671fa9e4066Sahrens 
672ecc2d604Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
673ecc2d604Sbonwick 			vdev_label_write(zio, vd, l, ub,
674ecc2d604Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
67517f17c2dSbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL, flags);
676fa9e4066Sahrens 		}
677fa9e4066Sahrens 	}
678fa9e4066Sahrens 
679fa9e4066Sahrens 	error = zio_wait(zio);
680fa9e4066Sahrens 
681fa9e4066Sahrens 	nvlist_free(label);
682ecc2d604Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
683fa9e4066Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
684fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
685fa9e4066Sahrens 
68639c23413Seschrock 	/*
68739c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
6883d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
689fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
690fa94a07fSbrendan 	 * level 2 ARC devices.
69139c23413Seschrock 	 */
69239c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
69339c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
69439c23413Seschrock 	    spa_spare_exists(vd->vdev_guid, NULL)))
69539c23413Seschrock 		spa_spare_add(vd);
69699653d4eSeschrock 
697fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
698fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
699fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
700fa94a07fSbrendan 		spa_l2cache_add(vd);
701fa94a07fSbrendan 
70239c23413Seschrock 	return (error);
70399653d4eSeschrock }
70499653d4eSeschrock 
705fa9e4066Sahrens /*
706fa9e4066Sahrens  * ==========================================================================
707fa9e4066Sahrens  * uberblock load/sync
708fa9e4066Sahrens  * ==========================================================================
709fa9e4066Sahrens  */
710fa9e4066Sahrens 
711fa9e4066Sahrens /*
712fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
713fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
714fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
715fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
716fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
717fa9e4066Sahrens  * the missing replica comes back, then for a new seconds we'll have two
718fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
719fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
720fa9e4066Sahrens  */
721fa9e4066Sahrens static int
722fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
723fa9e4066Sahrens {
724fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
725fa9e4066Sahrens 		return (-1);
726fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
727fa9e4066Sahrens 		return (1);
728fa9e4066Sahrens 
729fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
730fa9e4066Sahrens 		return (-1);
731fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
732fa9e4066Sahrens 		return (1);
733fa9e4066Sahrens 
734fa9e4066Sahrens 	return (0);
735fa9e4066Sahrens }
736fa9e4066Sahrens 
737fa9e4066Sahrens static void
738fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
739fa9e4066Sahrens {
740ecc2d604Sbonwick 	uberblock_t *ub = zio->io_data;
741fa9e4066Sahrens 	uberblock_t *ubbest = zio->io_private;
742fa9e4066Sahrens 	spa_t *spa = zio->io_spa;
743fa9e4066Sahrens 
744ecc2d604Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
745fa9e4066Sahrens 
746ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
747fa9e4066Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
748fa9e4066Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
749fa9e4066Sahrens 			*ubbest = *ub;
750fa9e4066Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 
753fa9e4066Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
754fa9e4066Sahrens }
755fa9e4066Sahrens 
756fa9e4066Sahrens void
757fa9e4066Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
758fa9e4066Sahrens {
759fa9e4066Sahrens 	int l, c, n;
760fa9e4066Sahrens 
761fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
762fa9e4066Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
763fa9e4066Sahrens 
764fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
765fa9e4066Sahrens 		return;
766fa9e4066Sahrens 
767fa9e4066Sahrens 	if (vdev_is_dead(vd))
768fa9e4066Sahrens 		return;
769fa9e4066Sahrens 
770fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
771ecc2d604Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
772fa9e4066Sahrens 			vdev_label_read(zio, vd, l,
773ecc2d604Sbonwick 			    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
774ecc2d604Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
775ecc2d604Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd),
776fa9e4066Sahrens 			    vdev_uberblock_load_done, ubbest);
777fa9e4066Sahrens 		}
778fa9e4066Sahrens 	}
779fa9e4066Sahrens }
780fa9e4066Sahrens 
781fa9e4066Sahrens /*
78217f17c2dSbonwick  * On success, increment root zio's count of good writes.
7830373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
784fa9e4066Sahrens  */
785fa9e4066Sahrens static void
786fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
787fa9e4066Sahrens {
78817f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
789fa9e4066Sahrens 
7900373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
791fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
792fa9e4066Sahrens }
793fa9e4066Sahrens 
79417f17c2dSbonwick /*
79517f17c2dSbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
79617f17c2dSbonwick  */
797fa9e4066Sahrens static void
79817f17c2dSbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd)
799fa9e4066Sahrens {
800fa9e4066Sahrens 	int l, c, n;
80117f17c2dSbonwick 	uberblock_t *ubbuf;
802fa9e4066Sahrens 
803fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
80417f17c2dSbonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c]);
805fa9e4066Sahrens 
806fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
807fa9e4066Sahrens 		return;
808fa9e4066Sahrens 
809fa9e4066Sahrens 	if (vdev_is_dead(vd))
810fa9e4066Sahrens 		return;
811fa9e4066Sahrens 
81217f17c2dSbonwick 	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
813fa9e4066Sahrens 
81417f17c2dSbonwick 	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
81517f17c2dSbonwick 	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
81617f17c2dSbonwick 	*ubbuf = *ub;
817fa9e4066Sahrens 
818fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
81917f17c2dSbonwick 		vdev_label_write(zio, vd, l, ubbuf,
820ecc2d604Sbonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n),
821ecc2d604Sbonwick 		    VDEV_UBERBLOCK_SIZE(vd),
82217f17c2dSbonwick 		    vdev_uberblock_sync_done, zio->io_private,
82317f17c2dSbonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE);
824fa9e4066Sahrens 
82517f17c2dSbonwick 	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
826fa9e4066Sahrens }
827fa9e4066Sahrens 
828*21bf64a7Sgw static void
829*21bf64a7Sgw vdev_uberblock_sync_list_done(zio_t *zio)
830*21bf64a7Sgw {
831*21bf64a7Sgw 	uint64_t *good_writes = zio->io_private;
832*21bf64a7Sgw 
833*21bf64a7Sgw 	if (*good_writes == 0)
834*21bf64a7Sgw 		zio->io_error = EIO;
835*21bf64a7Sgw }
836*21bf64a7Sgw 
83717f17c2dSbonwick int
83817f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
839fa9e4066Sahrens {
84017f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
84117f17c2dSbonwick 	int v;
842*21bf64a7Sgw 	zio_t *zio, *nio;
84317f17c2dSbonwick 	uint64_t good_writes = 0;
844*21bf64a7Sgw 	int io_flags = flags;
845fa9e4066Sahrens 
846*21bf64a7Sgw 	/*
847*21bf64a7Sgw 	 * If we've been asked to update all the vdevs then we change
848*21bf64a7Sgw 	 * our flags to ZIO_FLAG_MUSTSUCCEED so that the pipeline can
849*21bf64a7Sgw 	 * handle error should all update fail.
850*21bf64a7Sgw 	 */
851*21bf64a7Sgw 	if (svdcount == spa->spa_root_vdev->vdev_children)
852*21bf64a7Sgw 		io_flags &= ~ZIO_FLAG_CANFAIL;
853fa9e4066Sahrens 
854*21bf64a7Sgw 	/*
855*21bf64a7Sgw 	 * We rely on the value of good_writes and the root I/O to determine
856*21bf64a7Sgw 	 * how a complete failure is handled. In the event that the root is a
857*21bf64a7Sgw 	 * ZIO_FLAG_MUSTSUCCED, then the pipeline will block this I/O if we
858*21bf64a7Sgw 	 * were unable to update any uberblock. Once the I/O is blocked the
859*21bf64a7Sgw 	 * pipeline will retry it when the error is cleared. Unfortunately,
860*21bf64a7Sgw 	 * the pipeline does not have the complete I/O tree so it will be
861*21bf64a7Sgw 	 * unable to retry the actual uberblock update. Instead we rely on
862*21bf64a7Sgw 	 * the value of good_writes to return the failed status to the caller
863*21bf64a7Sgw 	 * which will retry on error and thus resubmit the complete I/O
864*21bf64a7Sgw 	 * tree.
865*21bf64a7Sgw 	 */
866*21bf64a7Sgw 	zio = zio_root(spa, NULL, NULL, io_flags);
867*21bf64a7Sgw 	nio = zio_null(zio, spa, vdev_uberblock_sync_list_done, &good_writes,
868*21bf64a7Sgw 	    flags);
86917f17c2dSbonwick 	for (v = 0; v < svdcount; v++)
870*21bf64a7Sgw 		vdev_uberblock_sync(nio, ub, svd[v]);
871*21bf64a7Sgw 	zio_nowait(nio);
87217f17c2dSbonwick 	(void) zio_wait(zio);
873fa9e4066Sahrens 
874fa9e4066Sahrens 	/*
87517f17c2dSbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
87617f17c2dSbonwick 	 * are no longer needed (because the new uberblocks and the even
87717f17c2dSbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
878fa9e4066Sahrens 	 */
87917f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
880fa9e4066Sahrens 
88117f17c2dSbonwick 	for (v = 0; v < svdcount; v++)
88217f17c2dSbonwick 		zio_flush(zio, svd[v]);
883fa9e4066Sahrens 
88417f17c2dSbonwick 	(void) zio_wait(zio);
88517f17c2dSbonwick 
88617f17c2dSbonwick 	return (good_writes >= 1 ? 0 : EIO);
887fa9e4066Sahrens }
888fa9e4066Sahrens 
889fa9e4066Sahrens /*
89017f17c2dSbonwick  * On success, increment the count of good writes for our top-level vdev.
891fa9e4066Sahrens  */
892fa9e4066Sahrens static void
89317f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
894fa9e4066Sahrens {
89517f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
896fa9e4066Sahrens 
897fa9e4066Sahrens 	if (zio->io_error == 0)
898fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
899fa9e4066Sahrens }
900fa9e4066Sahrens 
90117f17c2dSbonwick /*
90217f17c2dSbonwick  * If there weren't enough good writes, indicate failure to the parent.
90317f17c2dSbonwick  */
904fa9e4066Sahrens static void
90517f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
90617f17c2dSbonwick {
90717f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
90817f17c2dSbonwick 
90917f17c2dSbonwick 	if (*good_writes == 0)
91017f17c2dSbonwick 		zio->io_error = EIO;
91117f17c2dSbonwick 
91217f17c2dSbonwick 	kmem_free(good_writes, sizeof (uint64_t));
91317f17c2dSbonwick }
91417f17c2dSbonwick 
91517f17c2dSbonwick /*
91617f17c2dSbonwick  * Write all even or odd labels to all leaves of the specified vdev.
91717f17c2dSbonwick  */
91817f17c2dSbonwick static void
91917f17c2dSbonwick vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
920fa9e4066Sahrens {
921fa9e4066Sahrens 	nvlist_t *label;
922fa9e4066Sahrens 	vdev_phys_t *vp;
923fa9e4066Sahrens 	char *buf;
924fa9e4066Sahrens 	size_t buflen;
925fa9e4066Sahrens 	int c;
926fa9e4066Sahrens 
927fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
92817f17c2dSbonwick 		vdev_label_sync(zio, vd->vdev_child[c], l, txg);
929fa9e4066Sahrens 
930fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
931fa9e4066Sahrens 		return;
932fa9e4066Sahrens 
933fa9e4066Sahrens 	if (vdev_is_dead(vd))
934fa9e4066Sahrens 		return;
935fa9e4066Sahrens 
936fa9e4066Sahrens 	/*
937fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
938fa9e4066Sahrens 	 */
9390373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
940fa9e4066Sahrens 
941fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
942fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
943fa9e4066Sahrens 
944fa9e4066Sahrens 	buf = vp->vp_nvlist;
945fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
946fa9e4066Sahrens 
94717f17c2dSbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
94817f17c2dSbonwick 		for (; l < VDEV_LABELS; l += 2) {
94917f17c2dSbonwick 			vdev_label_write(zio, vd, l, vp,
95017f17c2dSbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
95117f17c2dSbonwick 			    sizeof (vdev_phys_t),
95217f17c2dSbonwick 			    vdev_label_sync_done, zio->io_private,
95317f17c2dSbonwick 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE);
95417f17c2dSbonwick 		}
95517f17c2dSbonwick 	}
956fa9e4066Sahrens 
957fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
958fa9e4066Sahrens 	nvlist_free(label);
959fa9e4066Sahrens }
960fa9e4066Sahrens 
96117f17c2dSbonwick int
96217f17c2dSbonwick vdev_label_sync_list(spa_t *spa, int l, int flags, uint64_t txg)
963fa9e4066Sahrens {
96417f17c2dSbonwick 	list_t *dl = &spa->spa_dirty_list;
96517f17c2dSbonwick 	vdev_t *vd;
966*21bf64a7Sgw 	zio_t *zio, *nio;
967fa9e4066Sahrens 	int error;
968*21bf64a7Sgw 	int io_flags = flags & ~ZIO_FLAG_CANFAIL;
969fa9e4066Sahrens 
970fa9e4066Sahrens 	/*
971*21bf64a7Sgw 	 * The root I/O for all label updates must succeed and we track
972*21bf64a7Sgw 	 * the error returned back from the null I/O to determine if we
973*21bf64a7Sgw 	 * need to reissue the I/O tree from scratch. If we are unable
974*21bf64a7Sgw 	 * to update any leaf vdev associated with a dirty top-level vdev,
975*21bf64a7Sgw 	 * then the pipeline will either suspend or panic when the root I/O
976*21bf64a7Sgw 	 * is issued. If the error is cleared, then the pipleine will retry
977*21bf64a7Sgw 	 * the root I/O. Unfortunately we've lost the entire I/O tree so we
978*21bf64a7Sgw 	 * return back the original error to the caller and allow the caller
979*21bf64a7Sgw 	 * to call use again so that we can build the I/O tree from scratch.
980fa9e4066Sahrens 	 */
981*21bf64a7Sgw 	zio = zio_root(spa, NULL, NULL, io_flags);
982*21bf64a7Sgw 	nio = zio_null(zio, spa, NULL, NULL, flags);
983fa9e4066Sahrens 
98417f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
98517f17c2dSbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
98617f17c2dSbonwick 		    KM_SLEEP);
987*21bf64a7Sgw 		zio_t *vio = zio_null(nio, spa, vdev_label_sync_top_done,
98817f17c2dSbonwick 		    good_writes, flags);
98917f17c2dSbonwick 		vdev_label_sync(vio, vd, l, txg);
99017f17c2dSbonwick 		zio_nowait(vio);
991fa9e4066Sahrens 	}
992*21bf64a7Sgw 	error = zio_wait(nio);
993*21bf64a7Sgw 	(void) zio_wait(zio);
994fa9e4066Sahrens 
9958654d025Sperrin 	/*
99617f17c2dSbonwick 	 * Flush the new labels to disk.
9978654d025Sperrin 	 */
99817f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
9998654d025Sperrin 
100017f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
100117f17c2dSbonwick 		zio_flush(zio, vd);
100217f17c2dSbonwick 
100317f17c2dSbonwick 	(void) zio_wait(zio);
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	return (error);
1006fa9e4066Sahrens }
1007fa9e4066Sahrens 
1008fa9e4066Sahrens /*
100917f17c2dSbonwick  * Sync the uberblock and any changes to the vdev configuration.
1010fa9e4066Sahrens  *
1011fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
1012fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
1013fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
1014fa9e4066Sahrens  * describe the failure semantics at each stage.
1015fa9e4066Sahrens  *
101617f17c2dSbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1017fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
1018fa9e4066Sahrens  */
1019*21bf64a7Sgw void
102017f17c2dSbonwick vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1021fa9e4066Sahrens {
102217f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1023fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
10240373e76bSbonwick 	vdev_t *vd;
1025fa9e4066Sahrens 	zio_t *zio;
102617f17c2dSbonwick 	int flags = ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL;
1027fa9e4066Sahrens 
1028fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	/*
103117f17c2dSbonwick 	 * If this isn't a resync due to I/O errors,
103217f17c2dSbonwick 	 * and nothing changed in this transaction group,
103317f17c2dSbonwick 	 * and the vdev configuration hasn't changed,
10340373e76bSbonwick 	 * then there's nothing to do.
1035fa9e4066Sahrens 	 */
103617f17c2dSbonwick 	if (ub->ub_txg < txg &&
103717f17c2dSbonwick 	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
103817f17c2dSbonwick 	    list_is_empty(&spa->spa_dirty_list))
1039*21bf64a7Sgw 		return;
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
1042*21bf64a7Sgw 		return;
1043fa9e4066Sahrens 
10440373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
10450373e76bSbonwick 
1046fa9e4066Sahrens 	/*
1047fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
1048fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
1049fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
1050fa9e4066Sahrens 	 * before any uberblock that references them.
1051fa9e4066Sahrens 	 */
105217f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
105317f17c2dSbonwick 
1054fa9e4066Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
105517f17c2dSbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
105617f17c2dSbonwick 		zio_flush(zio, vd);
105717f17c2dSbonwick 
1058fa9e4066Sahrens 	(void) zio_wait(zio);
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 	/*
1061fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1062fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1063fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1064fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
106517f17c2dSbonwick 	 * which have not yet been touched, will still be valid.  We flush
106617f17c2dSbonwick 	 * the new labels to disk to ensure that all even-label updates
106717f17c2dSbonwick 	 * are committed to stable storage before the uberblock update.
1068*21bf64a7Sgw 	 * Failure to update any of the labels will invoke the 'failmode'
1069*21bf64a7Sgw 	 * code path. Thus we must retry the entire I/O tree once the error
1070*21bf64a7Sgw 	 * is cleared and we ar resumed.
1071fa9e4066Sahrens 	 */
1072*21bf64a7Sgw 	while (vdev_label_sync_list(spa, 0, flags, txg) != 0)
1073*21bf64a7Sgw 		;
1074fa9e4066Sahrens 
1075fa9e4066Sahrens 	/*
1076*21bf64a7Sgw 	 * Sync the uberblocks to all vdevs in svd[]. If we are unable
1077*21bf64a7Sgw 	 * to do so, then we attempt to sync out to all top-level vdevs.
10780373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
10790373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1080fa9e4066Sahrens 	 *
1081fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1082fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1083fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1084fa9e4066Sahrens 	 *	to that uberblock.
1085fa9e4066Sahrens 	 *
1086fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1087fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1088fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1089fa9e4066Sahrens 	 *	to the new uberblocks.
1090*21bf64a7Sgw 	 *
1091*21bf64a7Sgw 	 * In addition, if we have failed to update all the uberblocks then
1092*21bf64a7Sgw 	 * we will follow the 'failmode' code path. We must retry the entire
1093*21bf64a7Sgw 	 * I/O tree if we are resumed.
1094fa9e4066Sahrens 	 */
1095*21bf64a7Sgw 	if (vdev_uberblock_sync_list(svd, svdcount, ub, flags) != 0) {
1096*21bf64a7Sgw 		vdev_t *rvd = spa->spa_root_vdev;
1097*21bf64a7Sgw 
1098*21bf64a7Sgw 		while (vdev_uberblock_sync_list(rvd->vdev_child,
1099*21bf64a7Sgw 		    rvd->vdev_children, ub, flags))
1100*21bf64a7Sgw 			;
1101*21bf64a7Sgw 	}
1102fa9e4066Sahrens 
1103fa9e4066Sahrens 	/*
1104fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1105fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1106fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1107fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1108fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
110917f17c2dSbonwick 	 * all labels will be brought up to date.  We flush the new labels
111017f17c2dSbonwick 	 * to disk to ensure that all odd-label updates are committed to
111117f17c2dSbonwick 	 * stable storage before the next transaction group begins.
1112*21bf64a7Sgw 	 * Failure to update any of the labels will invoke the 'failmode'
1113*21bf64a7Sgw 	 * code path. Thus we must retry the entire I/O tree once the error
1114*21bf64a7Sgw 	 * is cleared and we are resumed.
1115fa9e4066Sahrens 	 */
1116*21bf64a7Sgw 	while (vdev_label_sync_list(spa, 1, flags, txg) != 0)
1117*21bf64a7Sgw 		;
1118fa9e4066Sahrens }
1119