xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 0373e76b3c3643df49ef3483e0f293fdea61d8c6)
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 /*
22441d80aaSlling  * Copyright 2006 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
65fa9e4066Sahrens  * labels and an uberblock with the following transacation 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 {
155fa9e4066Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
156fa9e4066Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
157fa9e4066Sahrens }
158fa9e4066Sahrens 
159fa9e4066Sahrens static void
160fa9e4066Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
161fa9e4066Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
162fa9e4066Sahrens {
163fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
164fa9e4066Sahrens 
165fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
166fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
167fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
168ea8dc4b6Seschrock 	    ZIO_PRIORITY_SYNC_READ,
169ea8dc4b6Seschrock 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE));
170fa9e4066Sahrens }
171fa9e4066Sahrens 
172fa9e4066Sahrens static void
173fa9e4066Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
174fa9e4066Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
175fa9e4066Sahrens {
176fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
177fa9e4066Sahrens 
178fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
179fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
180fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
181ea8dc4b6Seschrock 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL));
182fa9e4066Sahrens }
183fa9e4066Sahrens 
184fa9e4066Sahrens /*
185fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
186fa9e4066Sahrens  */
187fa9e4066Sahrens nvlist_t *
188fa9e4066Sahrens vdev_config_generate(vdev_t *vd, int getstats)
189fa9e4066Sahrens {
190fa9e4066Sahrens 	nvlist_t *nv = NULL;
191fa9e4066Sahrens 
192ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
193fa9e4066Sahrens 
194fa9e4066Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
195fa9e4066Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
196fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0);
197fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
198fa9e4066Sahrens 
199fa9e4066Sahrens 	if (vd->vdev_path != NULL)
200fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
201fa9e4066Sahrens 		    vd->vdev_path) == 0);
202fa9e4066Sahrens 
203fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
204fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
205fa9e4066Sahrens 		    vd->vdev_devid) == 0);
206fa9e4066Sahrens 
207afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
208afefbcddSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
209afefbcddSeschrock 		    vd->vdev_wholedisk) == 0);
210afefbcddSeschrock 
211ea8dc4b6Seschrock 	if (vd->vdev_not_present)
212ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
213ea8dc4b6Seschrock 
214fa9e4066Sahrens 	if (vd == vd->vdev_top) {
215fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
216fa9e4066Sahrens 		    vd->vdev_ms_array) == 0);
217fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
218fa9e4066Sahrens 		    vd->vdev_ms_shift) == 0);
219fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
220fa9e4066Sahrens 		    vd->vdev_ashift) == 0);
221fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
222fa9e4066Sahrens 		    vd->vdev_asize) == 0);
223fa9e4066Sahrens 	}
224fa9e4066Sahrens 
225fa9e4066Sahrens 	if (vd->vdev_dtl.smo_object != 0)
226fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
227fa9e4066Sahrens 		    vd->vdev_dtl.smo_object) == 0);
228fa9e4066Sahrens 
229fa9e4066Sahrens 	if (getstats) {
230fa9e4066Sahrens 		vdev_stat_t vs;
231fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
232fa9e4066Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
233fa9e4066Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
234fa9e4066Sahrens 	}
235fa9e4066Sahrens 
236fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
237fa9e4066Sahrens 		nvlist_t **child;
238fa9e4066Sahrens 		int c;
239fa9e4066Sahrens 
240fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
241fa9e4066Sahrens 		    KM_SLEEP);
242fa9e4066Sahrens 
243fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
244fa9e4066Sahrens 			child[c] = vdev_config_generate(vd->vdev_child[c],
245fa9e4066Sahrens 			    getstats);
246fa9e4066Sahrens 
247fa9e4066Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
248fa9e4066Sahrens 		    child, vd->vdev_children) == 0);
249fa9e4066Sahrens 
250fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
251fa9e4066Sahrens 			nvlist_free(child[c]);
252fa9e4066Sahrens 
253fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
254441d80aaSlling 
255441d80aaSlling 	} else {
256441d80aaSlling 		if (!vd->vdev_tmpoffline) {
257441d80aaSlling 		    if (vd->vdev_offline)
258441d80aaSlling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
259441d80aaSlling 				B_TRUE) == 0);
260441d80aaSlling 		    else
261441d80aaSlling 			(void) nvlist_remove(nv, ZPOOL_CONFIG_OFFLINE,
262441d80aaSlling 				DATA_TYPE_UINT64);
263441d80aaSlling 		}
264fa9e4066Sahrens 	}
265fa9e4066Sahrens 
266fa9e4066Sahrens 	return (nv);
267fa9e4066Sahrens }
268fa9e4066Sahrens 
269fa9e4066Sahrens nvlist_t *
270fa9e4066Sahrens vdev_label_read_config(vdev_t *vd)
271fa9e4066Sahrens {
272*0373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
273fa9e4066Sahrens 	nvlist_t *config = NULL;
274fa9e4066Sahrens 	vdev_phys_t *vp;
275fa9e4066Sahrens 	zio_t *zio;
276fa9e4066Sahrens 	int l;
277fa9e4066Sahrens 
278*0373e76bSbonwick 	ASSERT(spa_config_held(spa, RW_READER));
279*0373e76bSbonwick 
280fa9e4066Sahrens 	if (vdev_is_dead(vd))
281fa9e4066Sahrens 		return (NULL);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
284fa9e4066Sahrens 
285fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
286fa9e4066Sahrens 
287*0373e76bSbonwick 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL |
288ea8dc4b6Seschrock 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
289fa9e4066Sahrens 
290fa9e4066Sahrens 		vdev_label_read(zio, vd, l, vp,
291fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
292fa9e4066Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
293fa9e4066Sahrens 
294fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
295fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
296ea8dc4b6Seschrock 		    &config, 0) == 0)
297fa9e4066Sahrens 			break;
298fa9e4066Sahrens 
299fa9e4066Sahrens 		if (config != NULL) {
300fa9e4066Sahrens 			nvlist_free(config);
301fa9e4066Sahrens 			config = NULL;
302fa9e4066Sahrens 		}
303fa9e4066Sahrens 	}
304fa9e4066Sahrens 
305fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
306fa9e4066Sahrens 
307fa9e4066Sahrens 	return (config);
308fa9e4066Sahrens }
309fa9e4066Sahrens 
310fa9e4066Sahrens int
311fa9e4066Sahrens vdev_label_init(vdev_t *vd, uint64_t crtxg)
312fa9e4066Sahrens {
313fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
314fa9e4066Sahrens 	nvlist_t *label;
315fa9e4066Sahrens 	vdev_phys_t *vp;
316fa9e4066Sahrens 	vdev_boot_header_t *vb;
317fa9e4066Sahrens 	uberblock_phys_t *ubphys;
318fa9e4066Sahrens 	zio_t *zio;
319fa9e4066Sahrens 	int l, c, n;
320fa9e4066Sahrens 	char *buf;
321fa9e4066Sahrens 	size_t buflen;
322fa9e4066Sahrens 	int error;
323fa9e4066Sahrens 
324*0373e76bSbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
325*0373e76bSbonwick 
326fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
327fa9e4066Sahrens 		if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0)
328fa9e4066Sahrens 			return (error);
329fa9e4066Sahrens 
330fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
331fa9e4066Sahrens 		return (0);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	/*
334fa9e4066Sahrens 	 * Make sure each leaf device is writable, and zero its initial content.
335fa9e4066Sahrens 	 * Along the way, also make sure that no leaf is already in use.
336fa9e4066Sahrens 	 * Note that it's important to do this sequentially, not in parallel,
337fa9e4066Sahrens 	 * so that we catch cases of multiple use of the same leaf vdev in
338fa9e4066Sahrens 	 * the vdev we're creating -- e.g. mirroring a disk with itself.
339fa9e4066Sahrens 	 */
340fa9e4066Sahrens 	if (vdev_is_dead(vd))
341fa9e4066Sahrens 		return (EIO);
342fa9e4066Sahrens 
343fa9e4066Sahrens 	/*
344fa9e4066Sahrens 	 * Check whether this device is already in use.
345fa9e4066Sahrens 	 * Ignore the check if crtxg == 0, which we use for device removal.
346fa9e4066Sahrens 	 */
347ea8dc4b6Seschrock 	if (crtxg != 0 &&
348ea8dc4b6Seschrock 	    (label = vdev_label_read_config(vd)) != NULL) {
349ea8dc4b6Seschrock 		uint64_t state, pool_guid, device_guid, txg;
350fa9e4066Sahrens 		uint64_t mycrtxg = 0;
351fa9e4066Sahrens 
352fa9e4066Sahrens 		(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
353fa9e4066Sahrens 		    &mycrtxg);
354fa9e4066Sahrens 
355ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
356fa9e4066Sahrens 		    &state) == 0 && state == POOL_STATE_ACTIVE &&
357fa9e4066Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
358fa9e4066Sahrens 		    &pool_guid) == 0 &&
359fa9e4066Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
360fa9e4066Sahrens 		    &device_guid) == 0 &&
361fa9e4066Sahrens 		    spa_guid_exists(pool_guid, device_guid) &&
362fa9e4066Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
363fa9e4066Sahrens 		    &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) {
364fa9e4066Sahrens 			dprintf("vdev %s in use, pool_state %d\n",
365fa9e4066Sahrens 			    vdev_description(vd), state);
366fa9e4066Sahrens 			nvlist_free(label);
367fa9e4066Sahrens 			return (EBUSY);
368fa9e4066Sahrens 		}
369fa9e4066Sahrens 		nvlist_free(label);
370fa9e4066Sahrens 	}
371fa9e4066Sahrens 
372fa9e4066Sahrens 	/*
373fa9e4066Sahrens 	 * The device isn't in use, so initialize its label.
374fa9e4066Sahrens 	 */
375fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
376fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
377fa9e4066Sahrens 
378fa9e4066Sahrens 	/*
379fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
380fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
381fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
382fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
383fa9e4066Sahrens 	 */
384*0373e76bSbonwick 	label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
385fa9e4066Sahrens 
386fa9e4066Sahrens 	/*
387fa9e4066Sahrens 	 * Add our creation time.  This allows us to detect multiple vdev
388fa9e4066Sahrens 	 * uses as described above, and automatically expires if we fail.
389fa9e4066Sahrens 	 */
390fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0);
391fa9e4066Sahrens 
392fa9e4066Sahrens 	buf = vp->vp_nvlist;
393fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
394fa9e4066Sahrens 
395ea8dc4b6Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) != 0) {
396fa9e4066Sahrens 		nvlist_free(label);
397fa9e4066Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
398fa9e4066Sahrens 		return (EINVAL);
399fa9e4066Sahrens 	}
400fa9e4066Sahrens 
401fa9e4066Sahrens 	/*
402fa9e4066Sahrens 	 * Initialize boot block header.
403fa9e4066Sahrens 	 */
404fa9e4066Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
405fa9e4066Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
406fa9e4066Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
407fa9e4066Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
408fa9e4066Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
409fa9e4066Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
410fa9e4066Sahrens 
411fa9e4066Sahrens 	/*
412fa9e4066Sahrens 	 * Initialize uberblock template.
413fa9e4066Sahrens 	 */
414fa9e4066Sahrens 	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
415fa9e4066Sahrens 	bzero(ubphys, sizeof (uberblock_phys_t));
416fa9e4066Sahrens 	ubphys->ubp_uberblock = spa->spa_uberblock;
417fa9e4066Sahrens 	ubphys->ubp_uberblock.ub_txg = 0;
418fa9e4066Sahrens 
419fa9e4066Sahrens 	/*
420fa9e4066Sahrens 	 * Write everything in parallel.
421fa9e4066Sahrens 	 */
422fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
423fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
424fa9e4066Sahrens 
425fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
426fa9e4066Sahrens 
427fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
428fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
429fa9e4066Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
430fa9e4066Sahrens 
431fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vb,
432fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
433fa9e4066Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
434fa9e4066Sahrens 
435fa9e4066Sahrens 		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
436fa9e4066Sahrens 
437fa9e4066Sahrens 			vdev_label_write(zio, vd, l, ubphys,
438fa9e4066Sahrens 			    offsetof(vdev_label_t, vl_uberblock[n]),
439fa9e4066Sahrens 			    sizeof (uberblock_phys_t), NULL, NULL);
440fa9e4066Sahrens 
441fa9e4066Sahrens 		}
442fa9e4066Sahrens 	}
443fa9e4066Sahrens 
444fa9e4066Sahrens 	error = zio_wait(zio);
445fa9e4066Sahrens 
446fa9e4066Sahrens 	nvlist_free(label);
447fa9e4066Sahrens 	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
448fa9e4066Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
449fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
450fa9e4066Sahrens 
451fa9e4066Sahrens 	return (error);
452fa9e4066Sahrens }
453fa9e4066Sahrens 
454fa9e4066Sahrens /*
455fa9e4066Sahrens  * ==========================================================================
456fa9e4066Sahrens  * uberblock load/sync
457fa9e4066Sahrens  * ==========================================================================
458fa9e4066Sahrens  */
459fa9e4066Sahrens 
460fa9e4066Sahrens /*
461fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
462fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
463fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
464fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
465fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
466fa9e4066Sahrens  * the missing replica comes back, then for a new seconds we'll have two
467fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
468fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
469fa9e4066Sahrens  */
470fa9e4066Sahrens static int
471fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
472fa9e4066Sahrens {
473fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
474fa9e4066Sahrens 		return (-1);
475fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
476fa9e4066Sahrens 		return (1);
477fa9e4066Sahrens 
478fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
479fa9e4066Sahrens 		return (-1);
480fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
481fa9e4066Sahrens 		return (1);
482fa9e4066Sahrens 
483fa9e4066Sahrens 	return (0);
484fa9e4066Sahrens }
485fa9e4066Sahrens 
486fa9e4066Sahrens static void
487fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
488fa9e4066Sahrens {
489fa9e4066Sahrens 	uberblock_phys_t *ubphys = zio->io_data;
490fa9e4066Sahrens 	uberblock_t *ub = &ubphys->ubp_uberblock;
491fa9e4066Sahrens 	uberblock_t *ubbest = zio->io_private;
492fa9e4066Sahrens 	spa_t *spa = zio->io_spa;
493fa9e4066Sahrens 
494fa9e4066Sahrens 	ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t));
495fa9e4066Sahrens 
496ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
497fa9e4066Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
498fa9e4066Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
499fa9e4066Sahrens 			*ubbest = *ub;
500fa9e4066Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
501fa9e4066Sahrens 	}
502fa9e4066Sahrens 
503fa9e4066Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
504fa9e4066Sahrens }
505fa9e4066Sahrens 
506fa9e4066Sahrens void
507fa9e4066Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
508fa9e4066Sahrens {
509fa9e4066Sahrens 	int l, c, n;
510fa9e4066Sahrens 
511fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
512fa9e4066Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
513fa9e4066Sahrens 
514fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
515fa9e4066Sahrens 		return;
516fa9e4066Sahrens 
517fa9e4066Sahrens 	if (vdev_is_dead(vd))
518fa9e4066Sahrens 		return;
519fa9e4066Sahrens 
520fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
521fa9e4066Sahrens 		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
522fa9e4066Sahrens 			vdev_label_read(zio, vd, l,
523fa9e4066Sahrens 			    zio_buf_alloc(sizeof (uberblock_phys_t)),
524fa9e4066Sahrens 			    offsetof(vdev_label_t, vl_uberblock[n]),
525fa9e4066Sahrens 			    sizeof (uberblock_phys_t),
526fa9e4066Sahrens 			    vdev_uberblock_load_done, ubbest);
527fa9e4066Sahrens 		}
528fa9e4066Sahrens 	}
529fa9e4066Sahrens }
530fa9e4066Sahrens 
531fa9e4066Sahrens /*
532fa9e4066Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
533*0373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
534fa9e4066Sahrens  */
535fa9e4066Sahrens static void
536fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
537fa9e4066Sahrens {
538fa9e4066Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
539fa9e4066Sahrens 
540*0373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
541fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
542fa9e4066Sahrens }
543fa9e4066Sahrens 
544fa9e4066Sahrens static void
545fa9e4066Sahrens vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd,
546fa9e4066Sahrens 	uint64_t txg)
547fa9e4066Sahrens {
548fa9e4066Sahrens 	int l, c, n;
549fa9e4066Sahrens 
550fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
551fa9e4066Sahrens 		vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg);
552fa9e4066Sahrens 
553fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
554fa9e4066Sahrens 		return;
555fa9e4066Sahrens 
556fa9e4066Sahrens 	if (vdev_is_dead(vd))
557fa9e4066Sahrens 		return;
558fa9e4066Sahrens 
559fa9e4066Sahrens 	n = txg & (VDEV_UBERBLOCKS - 1);
560fa9e4066Sahrens 
561fa9e4066Sahrens 	ASSERT(ubphys->ubp_uberblock.ub_txg == txg);
562fa9e4066Sahrens 
563fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
564fa9e4066Sahrens 		vdev_label_write(zio, vd, l, ubphys,
565fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_uberblock[n]),
566fa9e4066Sahrens 		    sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL);
567fa9e4066Sahrens 
568fa9e4066Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
569fa9e4066Sahrens }
570fa9e4066Sahrens 
571fa9e4066Sahrens static int
572fa9e4066Sahrens vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg)
573fa9e4066Sahrens {
574fa9e4066Sahrens 	uberblock_phys_t *ubphys;
575fa9e4066Sahrens 	uint64_t *good_writes;
576fa9e4066Sahrens 	zio_t *zio;
577fa9e4066Sahrens 	int error;
578fa9e4066Sahrens 
579fa9e4066Sahrens 	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
580fa9e4066Sahrens 	bzero(ubphys, sizeof (uberblock_phys_t));
581fa9e4066Sahrens 	ubphys->ubp_uberblock = *ub;
582fa9e4066Sahrens 
583fa9e4066Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
584fa9e4066Sahrens 
585fa9e4066Sahrens 	zio = zio_root(spa, NULL, good_writes,
586fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
587fa9e4066Sahrens 
588fa9e4066Sahrens 	vdev_uberblock_sync(zio, ubphys, uvd, txg);
589fa9e4066Sahrens 
590fa9e4066Sahrens 	error = zio_wait(zio);
591fa9e4066Sahrens 
592fa9e4066Sahrens 	if (error && *good_writes != 0) {
593fa9e4066Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
594fa9e4066Sahrens 		error = 0;
595fa9e4066Sahrens 	}
596fa9e4066Sahrens 
597fa9e4066Sahrens 	/*
598fa9e4066Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
599fa9e4066Sahrens 	 * the CANT_OPEN state.
600fa9e4066Sahrens 	 */
601fa9e4066Sahrens 	if (*good_writes == 0 && error == 0)
602fa9e4066Sahrens 		error = EIO;
603fa9e4066Sahrens 
604fa9e4066Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
605fa9e4066Sahrens 	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
606fa9e4066Sahrens 
607fa9e4066Sahrens 	return (error);
608fa9e4066Sahrens }
609fa9e4066Sahrens 
610fa9e4066Sahrens /*
611fa9e4066Sahrens  * Sync out an individual vdev.
612fa9e4066Sahrens  */
613fa9e4066Sahrens static void
614fa9e4066Sahrens vdev_sync_label_done(zio_t *zio)
615fa9e4066Sahrens {
616fa9e4066Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
617fa9e4066Sahrens 
618fa9e4066Sahrens 	if (zio->io_error == 0)
619fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622fa9e4066Sahrens static void
623fa9e4066Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
624fa9e4066Sahrens {
625fa9e4066Sahrens 	nvlist_t *label;
626fa9e4066Sahrens 	vdev_phys_t *vp;
627fa9e4066Sahrens 	char *buf;
628fa9e4066Sahrens 	size_t buflen;
629fa9e4066Sahrens 	int c;
630fa9e4066Sahrens 
631fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
632fa9e4066Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
633fa9e4066Sahrens 
634fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
635fa9e4066Sahrens 		return;
636fa9e4066Sahrens 
637fa9e4066Sahrens 	if (vdev_is_dead(vd))
638fa9e4066Sahrens 		return;
639fa9e4066Sahrens 
640fa9e4066Sahrens 	/*
641fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
642fa9e4066Sahrens 	 */
643*0373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
644fa9e4066Sahrens 
645fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
646fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
647fa9e4066Sahrens 
648fa9e4066Sahrens 	buf = vp->vp_nvlist;
649fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
650fa9e4066Sahrens 
651ea8dc4b6Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
652fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
653fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
654fa9e4066Sahrens 		    vdev_sync_label_done, NULL);
655fa9e4066Sahrens 
656fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
657fa9e4066Sahrens 	nvlist_free(label);
658fa9e4066Sahrens 
659fa9e4066Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
660fa9e4066Sahrens }
661fa9e4066Sahrens 
662fa9e4066Sahrens static int
663fa9e4066Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
664fa9e4066Sahrens {
665fa9e4066Sahrens 	uint64_t *good_writes;
666fa9e4066Sahrens 	zio_t *zio;
667fa9e4066Sahrens 	int error;
668fa9e4066Sahrens 
669fa9e4066Sahrens 	ASSERT(vd == vd->vdev_top);
670fa9e4066Sahrens 
671fa9e4066Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
672fa9e4066Sahrens 
673fa9e4066Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
674fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
675fa9e4066Sahrens 
676fa9e4066Sahrens 	/*
677fa9e4066Sahrens 	 * Recursively kick off writes to all labels.
678fa9e4066Sahrens 	 */
679fa9e4066Sahrens 	vdev_sync_label(zio, vd, l, txg);
680fa9e4066Sahrens 
681fa9e4066Sahrens 	error = zio_wait(zio);
682fa9e4066Sahrens 
683fa9e4066Sahrens 	if (error && *good_writes != 0) {
684fa9e4066Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
685fa9e4066Sahrens 		error = 0;
686fa9e4066Sahrens 	}
687fa9e4066Sahrens 
688fa9e4066Sahrens 	if (*good_writes == 0 && error == 0)
689fa9e4066Sahrens 		error = ENODEV;
690fa9e4066Sahrens 
691fa9e4066Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
692fa9e4066Sahrens 
693fa9e4066Sahrens 	return (error);
694fa9e4066Sahrens }
695fa9e4066Sahrens 
696fa9e4066Sahrens /*
697fa9e4066Sahrens  * Sync the entire vdev configuration.
698fa9e4066Sahrens  *
699fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
700fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
701fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
702fa9e4066Sahrens  * describe the failure semantics at each stage.
703fa9e4066Sahrens  *
704fa9e4066Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
705fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
706fa9e4066Sahrens  */
707fa9e4066Sahrens int
708*0373e76bSbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg)
709fa9e4066Sahrens {
710*0373e76bSbonwick 	spa_t *spa = uvd->vdev_spa;
711fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
712fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
713*0373e76bSbonwick 	vdev_t *vd;
714fa9e4066Sahrens 	zio_t *zio;
715fa9e4066Sahrens 	int c, l, error;
716fa9e4066Sahrens 
717fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
718fa9e4066Sahrens 
719fa9e4066Sahrens 	/*
720fa9e4066Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
721fa9e4066Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
722*0373e76bSbonwick 	 * then there's nothing to do.
723fa9e4066Sahrens 	 */
724fa9e4066Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
725fa9e4066Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
726fa9e4066Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
727fa9e4066Sahrens 		    spa_name(spa), txg);
728fa9e4066Sahrens 		return (0);
729fa9e4066Sahrens 	}
730fa9e4066Sahrens 
731fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
732fa9e4066Sahrens 		return (0);
733fa9e4066Sahrens 
734*0373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
735*0373e76bSbonwick 
736fa9e4066Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
737fa9e4066Sahrens 
738fa9e4066Sahrens 	/*
739fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
740fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
741fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
742fa9e4066Sahrens 	 * before any uberblock that references them.
743fa9e4066Sahrens 	 */
744fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
745fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
746fa9e4066Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
747fa9e4066Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
748fa9e4066Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
749fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
750fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 	(void) zio_wait(zio);
753fa9e4066Sahrens 
754fa9e4066Sahrens 	/*
755fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
756fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
757fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
758fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
759fa9e4066Sahrens 	 * which have not yet been touched, will still be valid.
760fa9e4066Sahrens 	 */
761fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
762fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
763fa9e4066Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
764fa9e4066Sahrens 			if (l & 1)
765fa9e4066Sahrens 				continue;
766fa9e4066Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
767fa9e4066Sahrens 				return (error);
768fa9e4066Sahrens 		}
769fa9e4066Sahrens 	}
770fa9e4066Sahrens 
771fa9e4066Sahrens 	/*
772fa9e4066Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
773fa9e4066Sahrens 	 * updates are committed to stable storage before the uberblock update.
774fa9e4066Sahrens 	 */
775fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
776fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
777fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
778fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
779fa9e4066Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
780fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
781fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
782fa9e4066Sahrens 	}
783fa9e4066Sahrens 	(void) zio_wait(zio);
784fa9e4066Sahrens 
785fa9e4066Sahrens 	/*
786*0373e76bSbonwick 	 * Sync the uberblocks to all vdevs in the tree specified by uvd.
787*0373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
788*0373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
789fa9e4066Sahrens 	 *
790fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
791fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
792fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
793fa9e4066Sahrens 	 *	to that uberblock.
794fa9e4066Sahrens 	 *
795fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
796fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
797fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
798fa9e4066Sahrens 	 *	to the new uberblocks.
799fa9e4066Sahrens 	 */
800fa9e4066Sahrens 	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
801fa9e4066Sahrens 		return (error);
802fa9e4066Sahrens 
803fa9e4066Sahrens 	/*
804fa9e4066Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
805fa9e4066Sahrens 	 * are no longer needed (because the new uberblocks and the even
806fa9e4066Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
807fa9e4066Sahrens 	 */
808fa9e4066Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
809fa9e4066Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
810fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
811fa9e4066Sahrens 
812fa9e4066Sahrens 	/*
813fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
814fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
815fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
816fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
817fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
818fa9e4066Sahrens 	 * all labels will be brought up to date.
819fa9e4066Sahrens 	 */
820fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
821fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
822fa9e4066Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
823fa9e4066Sahrens 			if ((l & 1) == 0)
824fa9e4066Sahrens 				continue;
825fa9e4066Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
826fa9e4066Sahrens 				return (error);
827fa9e4066Sahrens 		}
828fa9e4066Sahrens 	}
829fa9e4066Sahrens 
830fa9e4066Sahrens 	/*
831fa9e4066Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
832fa9e4066Sahrens 	 * updates are committed to stable storage before the next
833fa9e4066Sahrens 	 * transaction group begins.
834fa9e4066Sahrens 	 */
835fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
836fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
837fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
838fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
839fa9e4066Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
840fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
841fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
842fa9e4066Sahrens 	}
843fa9e4066Sahrens 	(void) zio_wait(zio);
844fa9e4066Sahrens 
845fa9e4066Sahrens 	return (0);
846fa9e4066Sahrens }
847