xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 2082:76b439ec3ac1)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221485Slling  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
29789Sahrens  * Virtual Device Labels
30789Sahrens  * ---------------------
31789Sahrens  *
32789Sahrens  * The vdev label serves several distinct purposes:
33789Sahrens  *
34789Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
35789Sahrens  *	   identity within the pool.
36789Sahrens  *
37789Sahrens  * 	2. Verify that all the devices given in a configuration are present
38789Sahrens  *         within the pool.
39789Sahrens  *
40789Sahrens  * 	3. Determine the uberblock for the pool.
41789Sahrens  *
42789Sahrens  * 	4. In case of an import operation, determine the configuration of the
43789Sahrens  *         toplevel vdev of which it is a part.
44789Sahrens  *
45789Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
46789Sahrens  *         provide enough information to the administrator to determine which
47789Sahrens  *         devices are missing.
48789Sahrens  *
49789Sahrens  * It is important to note that while the kernel is responsible for writing the
50789Sahrens  * label, it only consumes the information in the first three cases.  The
51789Sahrens  * latter information is only consumed in userland when determining the
52789Sahrens  * configuration to import a pool.
53789Sahrens  *
54789Sahrens  *
55789Sahrens  * Label Organization
56789Sahrens  * ------------------
57789Sahrens  *
58789Sahrens  * Before describing the contents of the label, it's important to understand how
59789Sahrens  * the labels are written and updated with respect to the uberblock.
60789Sahrens  *
61789Sahrens  * When the pool configuration is altered, either because it was newly created
62789Sahrens  * or a device was added, we want to update all the labels such that we can deal
63789Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
64789Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
65789Sahrens  * labels and an uberblock with the following transacation groups:
66789Sahrens  *
67789Sahrens  *              L1          UB          L2
68789Sahrens  *           +------+    +------+    +------+
69789Sahrens  *           |      |    |      |    |      |
70789Sahrens  *           | t10  |    | t10  |    | t10  |
71789Sahrens  *           |      |    |      |    |      |
72789Sahrens  *           +------+    +------+    +------+
73789Sahrens  *
74789Sahrens  * In this stable state, the labels and the uberblock were all updated within
75789Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
76789Sahrens  * that we can detect when we fail partway through writing the label.
77789Sahrens  *
78789Sahrens  * In order to identify which labels are valid, the labels are written in the
79789Sahrens  * following manner:
80789Sahrens  *
81789Sahrens  * 	1. For each vdev, update 'L1' to the new label
82789Sahrens  * 	2. Update the uberblock
83789Sahrens  * 	3. For each vdev, update 'L2' to the new label
84789Sahrens  *
85789Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
86789Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
87789Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
88789Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
89789Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
90789Sahrens  * therefore L1 is valid.
91789Sahrens  *
92789Sahrens  * Another added complexity is that not every label is updated when the config
93789Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
94789Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
95789Sahrens  * be older than the pool uberblock, because the necessary information is stored
96789Sahrens  * on another vdev.
97789Sahrens  *
98789Sahrens  *
99789Sahrens  * On-disk Format
100789Sahrens  * --------------
101789Sahrens  *
102789Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
103789Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
104789Sahrens  * VTOC disk labels, but is otherwise ignored.
105789Sahrens  *
106789Sahrens  * The first half of the label is a packed nvlist which contains pool wide
107789Sahrens  * properties, per-vdev properties, and configuration information.  It is
108789Sahrens  * described in more detail below.
109789Sahrens  *
110789Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
111789Sahrens  * These uberblocks are updated whenever a transaction group is committed,
112789Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
113789Sahrens  * vdev for the 'best' uberblock.
114789Sahrens  *
115789Sahrens  *
116789Sahrens  * Configuration Information
117789Sahrens  * -------------------------
118789Sahrens  *
119789Sahrens  * The nvlist describing the pool and vdev contains the following elements:
120789Sahrens  *
121789Sahrens  * 	version		ZFS on-disk version
122789Sahrens  * 	name		Pool name
123789Sahrens  * 	state		Pool state
124789Sahrens  * 	txg		Transaction group in which this label was written
125789Sahrens  * 	pool_guid	Unique identifier for this pool
126789Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
127789Sahrens  *
128789Sahrens  * Each leaf device label also contains the following:
129789Sahrens  *
130789Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
131789Sahrens  * 	guid		Unique ID for the leaf vdev
132789Sahrens  *
133789Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
134789Sahrens  */
135789Sahrens 
136789Sahrens #include <sys/zfs_context.h>
137789Sahrens #include <sys/spa.h>
138789Sahrens #include <sys/spa_impl.h>
139789Sahrens #include <sys/dmu.h>
140789Sahrens #include <sys/zap.h>
141789Sahrens #include <sys/vdev.h>
142789Sahrens #include <sys/vdev_impl.h>
143789Sahrens #include <sys/uberblock_impl.h>
144789Sahrens #include <sys/metaslab.h>
145789Sahrens #include <sys/zio.h>
146789Sahrens #include <sys/fs/zfs.h>
147789Sahrens 
148789Sahrens /*
149789Sahrens  * Basic routines to read and write from a vdev label.
150789Sahrens  * Used throughout the rest of this file.
151789Sahrens  */
152789Sahrens uint64_t
153789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
154789Sahrens {
1551732Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
1561732Sbonwick 
157789Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
158789Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
159789Sahrens }
160789Sahrens 
161789Sahrens static void
162789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
163789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
164789Sahrens {
165789Sahrens 	ASSERT(vd->vdev_children == 0);
166789Sahrens 
167789Sahrens 	zio_nowait(zio_read_phys(zio, vd,
168789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
169789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1701544Seschrock 	    ZIO_PRIORITY_SYNC_READ,
1711544Seschrock 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE));
172789Sahrens }
173789Sahrens 
174789Sahrens static void
175789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
176789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
177789Sahrens {
178789Sahrens 	ASSERT(vd->vdev_children == 0);
179789Sahrens 
180789Sahrens 	zio_nowait(zio_write_phys(zio, vd,
181789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
182789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1831544Seschrock 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL));
184789Sahrens }
185789Sahrens 
186789Sahrens /*
187789Sahrens  * Generate the nvlist representing this vdev's config.
188789Sahrens  */
189789Sahrens nvlist_t *
190*2082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
191*2082Seschrock     boolean_t isspare)
192789Sahrens {
193789Sahrens 	nvlist_t *nv = NULL;
194789Sahrens 
1951544Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
196789Sahrens 
197789Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
198789Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
199*2082Seschrock 	if (!isspare)
200*2082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
201*2082Seschrock 		    == 0);
202789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
203789Sahrens 
204789Sahrens 	if (vd->vdev_path != NULL)
205789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
206789Sahrens 		    vd->vdev_path) == 0);
207789Sahrens 
208789Sahrens 	if (vd->vdev_devid != NULL)
209789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
210789Sahrens 		    vd->vdev_devid) == 0);
211789Sahrens 
212*2082Seschrock 	if (vd->vdev_nparity != 0) {
213*2082Seschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
214*2082Seschrock 		    VDEV_TYPE_RAIDZ) == 0);
215*2082Seschrock 
216*2082Seschrock 		/*
217*2082Seschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
218*2082Seschrock 		 * into a crufty old storage pool.
219*2082Seschrock 		 */
220*2082Seschrock 		ASSERT(vd->vdev_nparity == 1 ||
221*2082Seschrock 		    (vd->vdev_nparity == 2 &&
222*2082Seschrock 		    spa_version(spa) >= ZFS_VERSION_RAID6));
223*2082Seschrock 
224*2082Seschrock 		/*
225*2082Seschrock 		 * Note that we'll add the nparity tag even on storage pools
226*2082Seschrock 		 * that only support a single parity device -- older software
227*2082Seschrock 		 * will just ignore it.
228*2082Seschrock 		 */
229*2082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
230*2082Seschrock 		    vd->vdev_nparity) == 0);
231*2082Seschrock 	}
232*2082Seschrock 
2331171Seschrock 	if (vd->vdev_wholedisk != -1ULL)
2341171Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2351171Seschrock 		    vd->vdev_wholedisk) == 0);
2361171Seschrock 
2371544Seschrock 	if (vd->vdev_not_present)
2381544Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
2391544Seschrock 
240*2082Seschrock 	if (vd->vdev_isspare)
241*2082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
242*2082Seschrock 
243*2082Seschrock 	if (!isspare && vd == vd->vdev_top) {
244789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
245789Sahrens 		    vd->vdev_ms_array) == 0);
246789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
247789Sahrens 		    vd->vdev_ms_shift) == 0);
248789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
249789Sahrens 		    vd->vdev_ashift) == 0);
250789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
251789Sahrens 		    vd->vdev_asize) == 0);
252789Sahrens 	}
253789Sahrens 
254789Sahrens 	if (vd->vdev_dtl.smo_object != 0)
255789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
256789Sahrens 		    vd->vdev_dtl.smo_object) == 0);
257789Sahrens 
258789Sahrens 	if (getstats) {
259789Sahrens 		vdev_stat_t vs;
260789Sahrens 		vdev_get_stats(vd, &vs);
261789Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
262789Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
263789Sahrens 	}
264789Sahrens 
265789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
266789Sahrens 		nvlist_t **child;
267789Sahrens 		int c;
268789Sahrens 
269789Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
270789Sahrens 		    KM_SLEEP);
271789Sahrens 
272789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
273*2082Seschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
274*2082Seschrock 			    getstats, isspare);
275789Sahrens 
276789Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
277789Sahrens 		    child, vd->vdev_children) == 0);
278789Sahrens 
279789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
280789Sahrens 			nvlist_free(child[c]);
281789Sahrens 
282789Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
2831485Slling 
2841485Slling 	} else {
2851732Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
2861485Slling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
2871732Sbonwick 			    B_TRUE) == 0);
2881732Sbonwick 		else
2891485Slling 			(void) nvlist_remove(nv, ZPOOL_CONFIG_OFFLINE,
2901732Sbonwick 			    DATA_TYPE_UINT64);
291789Sahrens 	}
292789Sahrens 
293789Sahrens 	return (nv);
294789Sahrens }
295789Sahrens 
296789Sahrens nvlist_t *
297789Sahrens vdev_label_read_config(vdev_t *vd)
298789Sahrens {
2991635Sbonwick 	spa_t *spa = vd->vdev_spa;
300789Sahrens 	nvlist_t *config = NULL;
301789Sahrens 	vdev_phys_t *vp;
302789Sahrens 	zio_t *zio;
303789Sahrens 	int l;
304789Sahrens 
3051635Sbonwick 	ASSERT(spa_config_held(spa, RW_READER));
3061635Sbonwick 
307789Sahrens 	if (vdev_is_dead(vd))
308789Sahrens 		return (NULL);
309789Sahrens 
310789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
311789Sahrens 
312789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
313789Sahrens 
3141635Sbonwick 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL |
3151544Seschrock 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
316789Sahrens 
317789Sahrens 		vdev_label_read(zio, vd, l, vp,
318789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
319789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
320789Sahrens 
321789Sahrens 		if (zio_wait(zio) == 0 &&
322789Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
3231544Seschrock 		    &config, 0) == 0)
324789Sahrens 			break;
325789Sahrens 
326789Sahrens 		if (config != NULL) {
327789Sahrens 			nvlist_free(config);
328789Sahrens 			config = NULL;
329789Sahrens 		}
330789Sahrens 	}
331789Sahrens 
332789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
333789Sahrens 
334789Sahrens 	return (config);
335789Sahrens }
336789Sahrens 
337*2082Seschrock static int
338*2082Seschrock vdev_label_common(vdev_t *vd, uint64_t crtxg, boolean_t isspare,
339*2082Seschrock     boolean_t isreplacing)
340789Sahrens {
341789Sahrens 	spa_t *spa = vd->vdev_spa;
342789Sahrens 	nvlist_t *label;
343789Sahrens 	vdev_phys_t *vp;
344789Sahrens 	vdev_boot_header_t *vb;
3451732Sbonwick 	uberblock_t *ub;
346789Sahrens 	zio_t *zio;
347789Sahrens 	int l, c, n;
348789Sahrens 	char *buf;
349789Sahrens 	size_t buflen;
350789Sahrens 	int error;
351789Sahrens 
3521635Sbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
3531635Sbonwick 
354789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
355*2082Seschrock 		if ((error = vdev_label_common(vd->vdev_child[c],
356*2082Seschrock 		    crtxg, isspare, isreplacing)) != 0)
357789Sahrens 			return (error);
358789Sahrens 
359789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
360789Sahrens 		return (0);
361789Sahrens 
362789Sahrens 	/*
363789Sahrens 	 * Make sure each leaf device is writable, and zero its initial content.
364789Sahrens 	 * Along the way, also make sure that no leaf is already in use.
365789Sahrens 	 * Note that it's important to do this sequentially, not in parallel,
366789Sahrens 	 * so that we catch cases of multiple use of the same leaf vdev in
367789Sahrens 	 * the vdev we're creating -- e.g. mirroring a disk with itself.
368789Sahrens 	 */
369789Sahrens 	if (vdev_is_dead(vd))
370789Sahrens 		return (EIO);
371789Sahrens 
372789Sahrens 	/*
373789Sahrens 	 * Check whether this device is already in use.
374789Sahrens 	 * Ignore the check if crtxg == 0, which we use for device removal.
375789Sahrens 	 */
3761544Seschrock 	if (crtxg != 0 &&
3771544Seschrock 	    (label = vdev_label_read_config(vd)) != NULL) {
378*2082Seschrock 		uint64_t state, pool_guid, device_guid, txg, spare;
379789Sahrens 		uint64_t mycrtxg = 0;
380789Sahrens 
381789Sahrens 		(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
382789Sahrens 		    &mycrtxg);
383789Sahrens 
3841544Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
385789Sahrens 		    &state) == 0 && state == POOL_STATE_ACTIVE &&
386789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
387789Sahrens 		    &pool_guid) == 0 &&
388789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
389789Sahrens 		    &device_guid) == 0 &&
390789Sahrens 		    spa_guid_exists(pool_guid, device_guid) &&
391789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
392789Sahrens 		    &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) {
393*2082Seschrock 			if (isspare && pool_guid != spa_guid(spa) &&
394*2082Seschrock 			    nvlist_lookup_uint64(label,
395*2082Seschrock 			    ZPOOL_CONFIG_IS_SPARE, &spare) == 0 &&
396*2082Seschrock 			    !spa_has_spare(spa, device_guid)) {
397*2082Seschrock 				/*
398*2082Seschrock 				 * If this is a request to add a spare that
399*2082Seschrock 				 * is actively in use in another pool, simply
400*2082Seschrock 				 * return success, after updating the guid.
401*2082Seschrock 				 */
402*2082Seschrock 				vdev_t *pvd = vd->vdev_parent;
403*2082Seschrock 
404*2082Seschrock 				for (; pvd != NULL; pvd = pvd->vdev_parent) {
405*2082Seschrock 					pvd->vdev_guid_sum -= vd->vdev_guid;
406*2082Seschrock 					pvd->vdev_guid_sum += device_guid;
407*2082Seschrock 				}
408*2082Seschrock 
409*2082Seschrock 				vd->vdev_guid = vd->vdev_guid_sum = device_guid;
410*2082Seschrock 				nvlist_free(label);
411*2082Seschrock 				return (0);
412*2082Seschrock 			}
413789Sahrens 			nvlist_free(label);
414789Sahrens 			return (EBUSY);
415789Sahrens 		}
416*2082Seschrock 
417*2082Seschrock 		/*
418*2082Seschrock 		 * If this device is reserved as a hot spare for this pool,
419*2082Seschrock 		 * adopt its GUID, and mark it as such.  This way we preserve
420*2082Seschrock 		 * the fact that it is a hot spare even as it is added and
421*2082Seschrock 		 * removed from the pool.
422*2082Seschrock 		 */
423*2082Seschrock 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
424*2082Seschrock 		    &state) == 0 && state == POOL_STATE_SPARE &&
425*2082Seschrock 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
426*2082Seschrock 		    &device_guid) == 0) {
427*2082Seschrock 			vdev_t *pvd = vd->vdev_parent;
428*2082Seschrock 
429*2082Seschrock 			if ((isspare || !isreplacing) &&
430*2082Seschrock 			    spa_has_spare(spa, device_guid)) {
431*2082Seschrock 				nvlist_free(label);
432*2082Seschrock 				return (EBUSY);
433*2082Seschrock 			}
434*2082Seschrock 
435*2082Seschrock 			for (; pvd != NULL; pvd = pvd->vdev_parent) {
436*2082Seschrock 				pvd->vdev_guid_sum -= vd->vdev_guid;
437*2082Seschrock 				pvd->vdev_guid_sum += device_guid;
438*2082Seschrock 			}
439*2082Seschrock 
440*2082Seschrock 			vd->vdev_guid = vd->vdev_guid_sum = device_guid;
441*2082Seschrock 
442*2082Seschrock 			if (!isspare) {
443*2082Seschrock 				vd->vdev_isspare = B_TRUE;
444*2082Seschrock 				spa_spare_add(vd->vdev_guid);
445*2082Seschrock 			}
446*2082Seschrock 		}
447*2082Seschrock 
448789Sahrens 		nvlist_free(label);
449789Sahrens 	}
450789Sahrens 
451789Sahrens 	/*
452789Sahrens 	 * The device isn't in use, so initialize its label.
453789Sahrens 	 */
454789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
455789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
456789Sahrens 
457789Sahrens 	/*
458789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
459789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
460789Sahrens 	 * really part of an active pool just yet.  The labels will
461789Sahrens 	 * be written again with a meaningful txg by spa_sync().
462*2082Seschrock 	 *
463*2082Seschrock 	 * For hot spares, we generate a special label that identifies as a
464*2082Seschrock 	 * mutually shared hot spare.  If this is being added as a hot spare,
465*2082Seschrock 	 * always write out the spare label.  If this was a hot spare, then
466*2082Seschrock 	 * always label it as such.  If we are adding the vdev, it will remain
467*2082Seschrock 	 * labelled in this state until it's really added to the config.  If we
468*2082Seschrock 	 * are removing the vdev or destroying the pool, then it goes back to
469*2082Seschrock 	 * its original hot spare state.
470789Sahrens 	 */
471*2082Seschrock 	if (isspare || vd->vdev_isspare) {
472*2082Seschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
473789Sahrens 
474*2082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
475*2082Seschrock 		    spa_version(spa)) == 0);
476*2082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
477*2082Seschrock 		    POOL_STATE_SPARE) == 0);
478*2082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
479*2082Seschrock 		    vd->vdev_guid) == 0);
480*2082Seschrock 	} else {
481*2082Seschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
482*2082Seschrock 
483*2082Seschrock 		/*
484*2082Seschrock 		 * Add our creation time.  This allows us to detect multiple
485*2082Seschrock 		 * vdev uses as described above, and automatically expires if we
486*2082Seschrock 		 * fail.
487*2082Seschrock 		 */
488*2082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
489*2082Seschrock 		    crtxg) == 0);
490*2082Seschrock 	}
491789Sahrens 
492789Sahrens 	buf = vp->vp_nvlist;
493789Sahrens 	buflen = sizeof (vp->vp_nvlist);
494789Sahrens 
4951544Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) != 0) {
496789Sahrens 		nvlist_free(label);
497789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
498789Sahrens 		return (EINVAL);
499789Sahrens 	}
500789Sahrens 
501789Sahrens 	/*
502789Sahrens 	 * Initialize boot block header.
503789Sahrens 	 */
504789Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
505789Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
506789Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
507789Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
508789Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
509789Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
510789Sahrens 
511789Sahrens 	/*
512789Sahrens 	 * Initialize uberblock template.
513789Sahrens 	 */
5141732Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
5151732Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
5161732Sbonwick 	*ub = spa->spa_uberblock;
5171732Sbonwick 	ub->ub_txg = 0;
518789Sahrens 
519789Sahrens 	/*
520789Sahrens 	 * Write everything in parallel.
521789Sahrens 	 */
522789Sahrens 	zio = zio_root(spa, NULL, NULL,
523789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
524789Sahrens 
525789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
526789Sahrens 
527789Sahrens 		vdev_label_write(zio, vd, l, vp,
528789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
529789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
530789Sahrens 
531789Sahrens 		vdev_label_write(zio, vd, l, vb,
532789Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
533789Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
534789Sahrens 
5351732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
5361732Sbonwick 			vdev_label_write(zio, vd, l, ub,
5371732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
5381732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL);
539789Sahrens 		}
540789Sahrens 	}
541789Sahrens 
542789Sahrens 	error = zio_wait(zio);
543789Sahrens 
544789Sahrens 	nvlist_free(label);
5451732Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
546789Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
547789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
548789Sahrens 
549789Sahrens 	return (error);
550789Sahrens }
551789Sahrens 
552*2082Seschrock int
553*2082Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, boolean_t isreplacing)
554*2082Seschrock {
555*2082Seschrock 	return (vdev_label_common(vd, crtxg, B_FALSE, isreplacing));
556*2082Seschrock }
557*2082Seschrock 
558*2082Seschrock /*
559*2082Seschrock  * Label a disk as a hot spare.  A hot spare label is a special label with only
560*2082Seschrock  * the following members: version, pool_state, and guid.
561*2082Seschrock  */
562*2082Seschrock int
563*2082Seschrock vdev_label_spare(vdev_t *vd, uint64_t crtxg)
564*2082Seschrock {
565*2082Seschrock 	return (vdev_label_common(vd, crtxg, B_TRUE, B_FALSE));
566*2082Seschrock }
567*2082Seschrock 
568789Sahrens /*
569789Sahrens  * ==========================================================================
570789Sahrens  * uberblock load/sync
571789Sahrens  * ==========================================================================
572789Sahrens  */
573789Sahrens 
574789Sahrens /*
575789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
576789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
577789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
578789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
579789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
580789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
581789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
582789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
583789Sahrens  */
584789Sahrens static int
585789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
586789Sahrens {
587789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
588789Sahrens 		return (-1);
589789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
590789Sahrens 		return (1);
591789Sahrens 
592789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
593789Sahrens 		return (-1);
594789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
595789Sahrens 		return (1);
596789Sahrens 
597789Sahrens 	return (0);
598789Sahrens }
599789Sahrens 
600789Sahrens static void
601789Sahrens vdev_uberblock_load_done(zio_t *zio)
602789Sahrens {
6031732Sbonwick 	uberblock_t *ub = zio->io_data;
604789Sahrens 	uberblock_t *ubbest = zio->io_private;
605789Sahrens 	spa_t *spa = zio->io_spa;
606789Sahrens 
6071732Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
608789Sahrens 
6091544Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
610789Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
611789Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
612789Sahrens 			*ubbest = *ub;
613789Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
614789Sahrens 	}
615789Sahrens 
616789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
617789Sahrens }
618789Sahrens 
619789Sahrens void
620789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
621789Sahrens {
622789Sahrens 	int l, c, n;
623789Sahrens 
624789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
625789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
626789Sahrens 
627789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
628789Sahrens 		return;
629789Sahrens 
630789Sahrens 	if (vdev_is_dead(vd))
631789Sahrens 		return;
632789Sahrens 
633789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
6341732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
635789Sahrens 			vdev_label_read(zio, vd, l,
6361732Sbonwick 			    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
6371732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
6381732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd),
639789Sahrens 			    vdev_uberblock_load_done, ubbest);
640789Sahrens 		}
641789Sahrens 	}
642789Sahrens }
643789Sahrens 
644789Sahrens /*
645789Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
6461635Sbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
647789Sahrens  */
648789Sahrens static void
649789Sahrens vdev_uberblock_sync_done(zio_t *zio)
650789Sahrens {
651789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
652789Sahrens 
6531635Sbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
654789Sahrens 		atomic_add_64(good_writes, 1);
655789Sahrens }
656789Sahrens 
657789Sahrens static void
6581732Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, uint64_t txg)
659789Sahrens {
660789Sahrens 	int l, c, n;
661789Sahrens 
662789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
6631732Sbonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], txg);
664789Sahrens 
665789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
666789Sahrens 		return;
667789Sahrens 
668789Sahrens 	if (vdev_is_dead(vd))
669789Sahrens 		return;
670789Sahrens 
6711732Sbonwick 	n = txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
672789Sahrens 
6731732Sbonwick 	ASSERT(ub->ub_txg == txg);
674789Sahrens 
675789Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
6761732Sbonwick 		vdev_label_write(zio, vd, l, ub,
6771732Sbonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n),
6781732Sbonwick 		    VDEV_UBERBLOCK_SIZE(vd),
6791732Sbonwick 		    vdev_uberblock_sync_done, NULL);
680789Sahrens 
681789Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
682789Sahrens }
683789Sahrens 
684789Sahrens static int
6851732Sbonwick vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *vd, uint64_t txg)
686789Sahrens {
6871732Sbonwick 	uberblock_t *ubbuf;
6881732Sbonwick 	size_t size = vd->vdev_top ? VDEV_UBERBLOCK_SIZE(vd) : SPA_MAXBLOCKSIZE;
689789Sahrens 	uint64_t *good_writes;
690789Sahrens 	zio_t *zio;
691789Sahrens 	int error;
692789Sahrens 
6931732Sbonwick 	ubbuf = zio_buf_alloc(size);
6941732Sbonwick 	bzero(ubbuf, size);
6951732Sbonwick 	*ubbuf = *ub;
696789Sahrens 
697789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
698789Sahrens 
699789Sahrens 	zio = zio_root(spa, NULL, good_writes,
700789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
701789Sahrens 
7021732Sbonwick 	vdev_uberblock_sync(zio, ubbuf, vd, txg);
703789Sahrens 
704789Sahrens 	error = zio_wait(zio);
705789Sahrens 
706789Sahrens 	if (error && *good_writes != 0) {
707789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
708789Sahrens 		error = 0;
709789Sahrens 	}
710789Sahrens 
711789Sahrens 	/*
712789Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
713789Sahrens 	 * the CANT_OPEN state.
714789Sahrens 	 */
715789Sahrens 	if (*good_writes == 0 && error == 0)
716789Sahrens 		error = EIO;
717789Sahrens 
718789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
7191732Sbonwick 	zio_buf_free(ubbuf, size);
720789Sahrens 
721789Sahrens 	return (error);
722789Sahrens }
723789Sahrens 
724789Sahrens /*
725789Sahrens  * Sync out an individual vdev.
726789Sahrens  */
727789Sahrens static void
728789Sahrens vdev_sync_label_done(zio_t *zio)
729789Sahrens {
730789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
731789Sahrens 
732789Sahrens 	if (zio->io_error == 0)
733789Sahrens 		atomic_add_64(good_writes, 1);
734789Sahrens }
735789Sahrens 
736789Sahrens static void
737789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
738789Sahrens {
739789Sahrens 	nvlist_t *label;
740789Sahrens 	vdev_phys_t *vp;
741789Sahrens 	char *buf;
742789Sahrens 	size_t buflen;
743789Sahrens 	int c;
744789Sahrens 
745789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
746789Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
747789Sahrens 
748789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
749789Sahrens 		return;
750789Sahrens 
751789Sahrens 	if (vdev_is_dead(vd))
752789Sahrens 		return;
753789Sahrens 
754789Sahrens 	/*
755789Sahrens 	 * Generate a label describing the top-level config to which we belong.
756789Sahrens 	 */
7571635Sbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
758789Sahrens 
759789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
760789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
761789Sahrens 
762789Sahrens 	buf = vp->vp_nvlist;
763789Sahrens 	buflen = sizeof (vp->vp_nvlist);
764789Sahrens 
7651544Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
766789Sahrens 		vdev_label_write(zio, vd, l, vp,
767789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
768789Sahrens 		    vdev_sync_label_done, NULL);
769789Sahrens 
770789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
771789Sahrens 	nvlist_free(label);
772789Sahrens 
773789Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
774789Sahrens }
775789Sahrens 
776789Sahrens static int
777789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
778789Sahrens {
779789Sahrens 	uint64_t *good_writes;
780789Sahrens 	zio_t *zio;
781789Sahrens 	int error;
782789Sahrens 
783789Sahrens 	ASSERT(vd == vd->vdev_top);
784789Sahrens 
785789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
786789Sahrens 
787789Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
788789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
789789Sahrens 
790789Sahrens 	/*
791789Sahrens 	 * Recursively kick off writes to all labels.
792789Sahrens 	 */
793789Sahrens 	vdev_sync_label(zio, vd, l, txg);
794789Sahrens 
795789Sahrens 	error = zio_wait(zio);
796789Sahrens 
797789Sahrens 	if (error && *good_writes != 0) {
798789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
799789Sahrens 		error = 0;
800789Sahrens 	}
801789Sahrens 
802789Sahrens 	if (*good_writes == 0 && error == 0)
803789Sahrens 		error = ENODEV;
804789Sahrens 
805789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
806789Sahrens 
807789Sahrens 	return (error);
808789Sahrens }
809789Sahrens 
810789Sahrens /*
811789Sahrens  * Sync the entire vdev configuration.
812789Sahrens  *
813789Sahrens  * The order of operations is carefully crafted to ensure that
814789Sahrens  * if the system panics or loses power at any time, the state on disk
815789Sahrens  * is still transactionally consistent.  The in-line comments below
816789Sahrens  * describe the failure semantics at each stage.
817789Sahrens  *
818789Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
819789Sahrens  * at any time, you can just call it again, and it will resume its work.
820789Sahrens  */
821789Sahrens int
8221635Sbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg)
823789Sahrens {
8241635Sbonwick 	spa_t *spa = uvd->vdev_spa;
825789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
826789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
8271635Sbonwick 	vdev_t *vd;
828789Sahrens 	zio_t *zio;
8291637Sbonwick 	int l, error;
830789Sahrens 
831789Sahrens 	ASSERT(ub->ub_txg <= txg);
832789Sahrens 
833789Sahrens 	/*
834789Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
835789Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
8361635Sbonwick 	 * then there's nothing to do.
837789Sahrens 	 */
838789Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
839789Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
840789Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
841789Sahrens 		    spa_name(spa), txg);
842789Sahrens 		return (0);
843789Sahrens 	}
844789Sahrens 
845789Sahrens 	if (txg > spa_freeze_txg(spa))
846789Sahrens 		return (0);
847789Sahrens 
8481635Sbonwick 	ASSERT(txg <= spa->spa_final_txg);
8491635Sbonwick 
850789Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
851789Sahrens 
852789Sahrens 	/*
853789Sahrens 	 * Flush the write cache of every disk that's been written to
854789Sahrens 	 * in this transaction group.  This ensures that all blocks
855789Sahrens 	 * written in this txg will be committed to stable storage
856789Sahrens 	 * before any uberblock that references them.
857789Sahrens 	 */
858789Sahrens 	zio = zio_root(spa, NULL, NULL,
859789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
860789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
861789Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
862789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
863789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
864789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
865789Sahrens 	}
866789Sahrens 	(void) zio_wait(zio);
867789Sahrens 
868789Sahrens 	/*
869789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
870789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
871789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
872789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
873789Sahrens 	 * which have not yet been touched, will still be valid.
874789Sahrens 	 */
875789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
876789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
877789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
878789Sahrens 			if (l & 1)
879789Sahrens 				continue;
880789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
881789Sahrens 				return (error);
882789Sahrens 		}
883789Sahrens 	}
884789Sahrens 
885789Sahrens 	/*
886789Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
887789Sahrens 	 * updates are committed to stable storage before the uberblock update.
888789Sahrens 	 */
889789Sahrens 	zio = zio_root(spa, NULL, NULL,
890789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
891789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
892789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
893789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
894789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
895789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
896789Sahrens 	}
897789Sahrens 	(void) zio_wait(zio);
898789Sahrens 
899789Sahrens 	/*
9001635Sbonwick 	 * Sync the uberblocks to all vdevs in the tree specified by uvd.
9011635Sbonwick 	 * If the system dies in the middle of this step, there are two cases
9021635Sbonwick 	 * to consider, and the on-disk state is consistent either way:
903789Sahrens 	 *
904789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
905789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
906789Sahrens 	 *	(which had not yet been touched) will be valid with respect
907789Sahrens 	 *	to that uberblock.
908789Sahrens 	 *
909789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
910789Sahrens 	 *	will be the newest, and the even labels (which had all
911789Sahrens 	 *	been successfully committed) will be valid with respect
912789Sahrens 	 *	to the new uberblocks.
913789Sahrens 	 */
914789Sahrens 	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
915789Sahrens 		return (error);
916789Sahrens 
917789Sahrens 	/*
918789Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
919789Sahrens 	 * are no longer needed (because the new uberblocks and the even
920789Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
921789Sahrens 	 */
922789Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
923789Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
924789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
925789Sahrens 
926789Sahrens 	/*
927789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
928789Sahrens 	 * in the middle of this process, the even labels and the new
929789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
930789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
931789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
932789Sahrens 	 * all labels will be brought up to date.
933789Sahrens 	 */
934789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
935789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
936789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
937789Sahrens 			if ((l & 1) == 0)
938789Sahrens 				continue;
939789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
940789Sahrens 				return (error);
941789Sahrens 		}
942789Sahrens 	}
943789Sahrens 
944789Sahrens 	/*
945789Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
946789Sahrens 	 * updates are committed to stable storage before the next
947789Sahrens 	 * transaction group begins.
948789Sahrens 	 */
949789Sahrens 	zio = zio_root(spa, NULL, NULL,
950789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
951789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
952789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
953789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
954789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
955789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
956789Sahrens 	}
957789Sahrens 	(void) zio_wait(zio);
958789Sahrens 
959789Sahrens 	return (0);
960789Sahrens }
961