xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision fa94a07fd0519b8abfd871ad8fe60e6bebe1e2bb)
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 /*
2239c23413Seschrock  * Copyright 2007 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 
162fa9e4066Sahrens static void
163fa9e4066Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
164fa9e4066Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
165fa9e4066Sahrens {
166fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
167fa9e4066Sahrens 
168fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
169fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
170fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
171ea8dc4b6Seschrock 	    ZIO_PRIORITY_SYNC_READ,
172*fa94a07fSbrendan 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
173*fa94a07fSbrendan 	    B_TRUE));
174fa9e4066Sahrens }
175fa9e4066Sahrens 
176fa9e4066Sahrens static void
177fa9e4066Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
178fa9e4066Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
179fa9e4066Sahrens {
180fa9e4066Sahrens 	ASSERT(vd->vdev_children == 0);
181fa9e4066Sahrens 
182fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
183fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
184fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
185*fa94a07fSbrendan 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL,
186*fa94a07fSbrendan 	    B_TRUE));
187fa9e4066Sahrens }
188fa9e4066Sahrens 
189fa9e4066Sahrens /*
190fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
191fa9e4066Sahrens  */
192fa9e4066Sahrens nvlist_t *
19399653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
194*fa94a07fSbrendan     boolean_t isspare, boolean_t isl2cache)
195fa9e4066Sahrens {
196fa9e4066Sahrens 	nvlist_t *nv = NULL;
197fa9e4066Sahrens 
198ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
199fa9e4066Sahrens 
200fa9e4066Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
201fa9e4066Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
202*fa94a07fSbrendan 	if (!isspare && !isl2cache)
20399653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
20499653d4eSeschrock 		    == 0);
205fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
206fa9e4066Sahrens 
207fa9e4066Sahrens 	if (vd->vdev_path != NULL)
208fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
209fa9e4066Sahrens 		    vd->vdev_path) == 0);
210fa9e4066Sahrens 
211fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
212fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
213fa9e4066Sahrens 		    vd->vdev_devid) == 0);
214fa9e4066Sahrens 
2153d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
2163d7072f8Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2173d7072f8Seschrock 		    vd->vdev_physpath) == 0);
2183d7072f8Seschrock 
21999653d4eSeschrock 	if (vd->vdev_nparity != 0) {
22099653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
22199653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
22299653d4eSeschrock 
22399653d4eSeschrock 		/*
22499653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
22599653d4eSeschrock 		 * into a crufty old storage pool.
22699653d4eSeschrock 		 */
22799653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
22899653d4eSeschrock 		    (vd->vdev_nparity == 2 &&
229e7437265Sahrens 		    spa_version(spa) >= SPA_VERSION_RAID6));
23099653d4eSeschrock 
23199653d4eSeschrock 		/*
23299653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
23399653d4eSeschrock 		 * that only support a single parity device -- older software
23499653d4eSeschrock 		 * will just ignore it.
23599653d4eSeschrock 		 */
23699653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
23799653d4eSeschrock 		    vd->vdev_nparity) == 0);
23899653d4eSeschrock 	}
23999653d4eSeschrock 
240afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
241afefbcddSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
242afefbcddSeschrock 		    vd->vdev_wholedisk) == 0);
243afefbcddSeschrock 
244ea8dc4b6Seschrock 	if (vd->vdev_not_present)
245ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
246ea8dc4b6Seschrock 
24799653d4eSeschrock 	if (vd->vdev_isspare)
24899653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
24999653d4eSeschrock 
250*fa94a07fSbrendan 	if (!isspare && !isl2cache && vd == vd->vdev_top) {
251fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
252fa9e4066Sahrens 		    vd->vdev_ms_array) == 0);
253fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
254fa9e4066Sahrens 		    vd->vdev_ms_shift) == 0);
255fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
256fa9e4066Sahrens 		    vd->vdev_ashift) == 0);
257fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
258fa9e4066Sahrens 		    vd->vdev_asize) == 0);
2598654d025Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2608654d025Sperrin 		    vd->vdev_islog) == 0);
261fa9e4066Sahrens 	}
262fa9e4066Sahrens 
263fa9e4066Sahrens 	if (vd->vdev_dtl.smo_object != 0)
264fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
265fa9e4066Sahrens 		    vd->vdev_dtl.smo_object) == 0);
266fa9e4066Sahrens 
267fa9e4066Sahrens 	if (getstats) {
268fa9e4066Sahrens 		vdev_stat_t vs;
269fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
270fa9e4066Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
271fa9e4066Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
272fa9e4066Sahrens 	}
273fa9e4066Sahrens 
274fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
275fa9e4066Sahrens 		nvlist_t **child;
276fa9e4066Sahrens 		int c;
277fa9e4066Sahrens 
278fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
279fa9e4066Sahrens 		    KM_SLEEP);
280fa9e4066Sahrens 
281fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
28299653d4eSeschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
283*fa94a07fSbrendan 			    getstats, isspare, isl2cache);
284fa9e4066Sahrens 
285fa9e4066Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
286fa9e4066Sahrens 		    child, vd->vdev_children) == 0);
287fa9e4066Sahrens 
288fa9e4066Sahrens 		for (c = 0; c < vd->vdev_children; c++)
289fa9e4066Sahrens 			nvlist_free(child[c]);
290fa9e4066Sahrens 
291fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
292441d80aaSlling 
293441d80aaSlling 	} else {
294ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
295441d80aaSlling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
296ecc2d604Sbonwick 			    B_TRUE) == 0);
2973d7072f8Seschrock 		if (vd->vdev_faulted)
2983d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
2993d7072f8Seschrock 			    B_TRUE) == 0);
3003d7072f8Seschrock 		if (vd->vdev_degraded)
3013d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3023d7072f8Seschrock 			    B_TRUE) == 0);
3033d7072f8Seschrock 		if (vd->vdev_removed)
3043d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3053d7072f8Seschrock 			    B_TRUE) == 0);
3063d7072f8Seschrock 		if (vd->vdev_unspare)
3073d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3083d7072f8Seschrock 			    B_TRUE) == 0);
309fa9e4066Sahrens 	}
310fa9e4066Sahrens 
311fa9e4066Sahrens 	return (nv);
312fa9e4066Sahrens }
313fa9e4066Sahrens 
314fa9e4066Sahrens nvlist_t *
315fa9e4066Sahrens vdev_label_read_config(vdev_t *vd)
316fa9e4066Sahrens {
3170373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
318fa9e4066Sahrens 	nvlist_t *config = NULL;
319fa9e4066Sahrens 	vdev_phys_t *vp;
320fa9e4066Sahrens 	zio_t *zio;
321fa9e4066Sahrens 	int l;
322fa9e4066Sahrens 
32391ebeef5Sahrens 	ASSERT(spa_config_held(spa, RW_READER) ||
32491ebeef5Sahrens 	    spa_config_held(spa, RW_WRITER));
3250373e76bSbonwick 
3260a4e9518Sgw 	if (!vdev_readable(vd))
327fa9e4066Sahrens 		return (NULL);
328fa9e4066Sahrens 
329fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
330fa9e4066Sahrens 
331fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
332fa9e4066Sahrens 
3330373e76bSbonwick 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL |
334ea8dc4b6Seschrock 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
335fa9e4066Sahrens 
336fa9e4066Sahrens 		vdev_label_read(zio, vd, l, vp,
337fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
338fa9e4066Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
339fa9e4066Sahrens 
340fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
341fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
342ea8dc4b6Seschrock 		    &config, 0) == 0)
343fa9e4066Sahrens 			break;
344fa9e4066Sahrens 
345fa9e4066Sahrens 		if (config != NULL) {
346fa9e4066Sahrens 			nvlist_free(config);
347fa9e4066Sahrens 			config = NULL;
348fa9e4066Sahrens 		}
349fa9e4066Sahrens 	}
350fa9e4066Sahrens 
351fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
352fa9e4066Sahrens 
353fa9e4066Sahrens 	return (config);
354fa9e4066Sahrens }
355fa9e4066Sahrens 
35639c23413Seschrock /*
35739c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
35839c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
35939c23413Seschrock  */
36039c23413Seschrock static boolean_t
36139c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
362*fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
36339c23413Seschrock {
36439c23413Seschrock 	spa_t *spa = vd->vdev_spa;
36539c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
36639c23413Seschrock 	uint64_t vdtxg = 0;
36739c23413Seschrock 	nvlist_t *label;
36839c23413Seschrock 
36939c23413Seschrock 	if (spare_guid)
37039c23413Seschrock 		*spare_guid = 0ULL;
371*fa94a07fSbrendan 	if (l2cache_guid)
372*fa94a07fSbrendan 		*l2cache_guid = 0ULL;
37339c23413Seschrock 
37439c23413Seschrock 	/*
37539c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
37639c23413Seschrock 	 */
37739c23413Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
37839c23413Seschrock 		return (B_FALSE);
37939c23413Seschrock 
38039c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
38139c23413Seschrock 	    &vdtxg);
38239c23413Seschrock 
38339c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
38439c23413Seschrock 	    &state) != 0 ||
38539c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
38639c23413Seschrock 	    &device_guid) != 0) {
38739c23413Seschrock 		nvlist_free(label);
38839c23413Seschrock 		return (B_FALSE);
38939c23413Seschrock 	}
39039c23413Seschrock 
391*fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
39239c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
39339c23413Seschrock 	    &pool_guid) != 0 ||
39439c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
39539c23413Seschrock 	    &txg) != 0)) {
39639c23413Seschrock 		nvlist_free(label);
39739c23413Seschrock 		return (B_FALSE);
39839c23413Seschrock 	}
39939c23413Seschrock 
40039c23413Seschrock 	nvlist_free(label);
40139c23413Seschrock 
40239c23413Seschrock 	/*
40339c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
40439c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
40539c23413Seschrock 	 * spare (which we check for later on).
40639c23413Seschrock 	 */
407*fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
40839c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
409*fa94a07fSbrendan 	    !spa_spare_exists(device_guid, NULL) &&
410*fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
41139c23413Seschrock 		return (B_FALSE);
41239c23413Seschrock 
41339c23413Seschrock 	/*
41439c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
41539c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
41639c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
41739c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
41839c23413Seschrock 	 * transaction.
41939c23413Seschrock 	 */
420*fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
421*fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
42239c23413Seschrock 		return (B_TRUE);
42339c23413Seschrock 
42439c23413Seschrock 	/*
42539c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
42639c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
427*fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
42839c23413Seschrock 	 */
42939c23413Seschrock 	if (spa_spare_exists(device_guid, &spare_pool) ||
43039c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
43139c23413Seschrock 		if (spare_guid)
43239c23413Seschrock 			*spare_guid = device_guid;
43339c23413Seschrock 
43439c23413Seschrock 		switch (reason) {
43539c23413Seschrock 		case VDEV_LABEL_CREATE:
436*fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
43739c23413Seschrock 			return (B_TRUE);
43839c23413Seschrock 
43939c23413Seschrock 		case VDEV_LABEL_REPLACE:
44039c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
44139c23413Seschrock 			    spare_pool != 0ULL);
44239c23413Seschrock 
44339c23413Seschrock 		case VDEV_LABEL_SPARE:
44439c23413Seschrock 			return (spa_has_spare(spa, device_guid));
44539c23413Seschrock 		}
44639c23413Seschrock 	}
44739c23413Seschrock 
448*fa94a07fSbrendan 	/*
449*fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
450*fa94a07fSbrendan 	 */
451*fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
452*fa94a07fSbrendan 		return (B_TRUE);
453*fa94a07fSbrendan 
45439c23413Seschrock 	/*
45539c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
45639c23413Seschrock 	 * pool on the system.
45739c23413Seschrock 	 */
45839c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
45939c23413Seschrock }
46039c23413Seschrock 
46139c23413Seschrock /*
46239c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
46339c23413Seschrock  * use, and writable.  We put down an initial label which we will later
46439c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
46539c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
46639c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
46739c23413Seschrock  * itself.
46839c23413Seschrock  */
46939c23413Seschrock int
47039c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
471fa9e4066Sahrens {
472fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
473fa9e4066Sahrens 	nvlist_t *label;
474fa9e4066Sahrens 	vdev_phys_t *vp;
475fa9e4066Sahrens 	vdev_boot_header_t *vb;
476ecc2d604Sbonwick 	uberblock_t *ub;
477fa9e4066Sahrens 	zio_t *zio;
478fa9e4066Sahrens 	int l, c, n;
479fa9e4066Sahrens 	char *buf;
480fa9e4066Sahrens 	size_t buflen;
481fa9e4066Sahrens 	int error;
482*fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
483fa9e4066Sahrens 
4840373e76bSbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
4850373e76bSbonwick 
486fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
48739c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
48839c23413Seschrock 		    crtxg, reason)) != 0)
489fa9e4066Sahrens 			return (error);
490fa9e4066Sahrens 
491fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
492fa9e4066Sahrens 		return (0);
493fa9e4066Sahrens 
494fa9e4066Sahrens 	/*
49539c23413Seschrock 	 * Dead vdevs cannot be initialized.
496fa9e4066Sahrens 	 */
497fa9e4066Sahrens 	if (vdev_is_dead(vd))
498fa9e4066Sahrens 		return (EIO);
499fa9e4066Sahrens 
500fa9e4066Sahrens 	/*
50139c23413Seschrock 	 * Determine if the vdev is in use.
502fa9e4066Sahrens 	 */
50339c23413Seschrock 	if (reason != VDEV_LABEL_REMOVE &&
504*fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
50539c23413Seschrock 		return (EBUSY);
50639c23413Seschrock 
50739c23413Seschrock 	ASSERT(reason != VDEV_LABEL_REMOVE ||
508*fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, NULL, NULL));
50939c23413Seschrock 
51039c23413Seschrock 	/*
511*fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
512*fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
513*fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
514*fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
51539c23413Seschrock 	 */
516*fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
517*fa94a07fSbrendan 	    spare_guid != 0ULL) {
51839c23413Seschrock 		vdev_t *pvd = vd->vdev_parent;
51939c23413Seschrock 
52039c23413Seschrock 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
52139c23413Seschrock 			pvd->vdev_guid_sum -= vd->vdev_guid;
52239c23413Seschrock 			pvd->vdev_guid_sum += spare_guid;
523fa9e4066Sahrens 		}
52499653d4eSeschrock 
52539c23413Seschrock 		vd->vdev_guid = vd->vdev_guid_sum = spare_guid;
52639c23413Seschrock 
52799653d4eSeschrock 		/*
52839c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
52939c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
5303d7072f8Seschrock 		 * labeled appropriately and we can just return.
53199653d4eSeschrock 		 */
53239c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
53339c23413Seschrock 			return (0);
53439c23413Seschrock 		ASSERT(reason == VDEV_LABEL_REPLACE);
535fa9e4066Sahrens 	}
536fa9e4066Sahrens 
537*fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
538*fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
539*fa94a07fSbrendan 		vdev_t *pvd = vd->vdev_parent;
540*fa94a07fSbrendan 
541*fa94a07fSbrendan 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
542*fa94a07fSbrendan 			pvd->vdev_guid_sum -= vd->vdev_guid;
543*fa94a07fSbrendan 			pvd->vdev_guid_sum += l2cache_guid;
544*fa94a07fSbrendan 		}
545*fa94a07fSbrendan 
546*fa94a07fSbrendan 		vd->vdev_guid = vd->vdev_guid_sum = l2cache_guid;
547*fa94a07fSbrendan 
548*fa94a07fSbrendan 		/*
549*fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
550*fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
551*fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
552*fa94a07fSbrendan 		 */
553*fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
554*fa94a07fSbrendan 			return (0);
555*fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
556*fa94a07fSbrendan 	}
557*fa94a07fSbrendan 
558fa9e4066Sahrens 	/*
55939c23413Seschrock 	 * Initialize its label.
560fa9e4066Sahrens 	 */
561fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
562fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
563fa9e4066Sahrens 
564fa9e4066Sahrens 	/*
565fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
566fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
567fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
568fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
569fa9e4066Sahrens 	 */
57039c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
57139c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
57239c23413Seschrock 		/*
57339c23413Seschrock 		 * For inactive hot spares, we generate a special label that
57439c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
57539c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
57639c23413Seschrock 		 * active hot spare (in which case we want to revert the
57739c23413Seschrock 		 * labels).
57839c23413Seschrock 		 */
57999653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
58099653d4eSeschrock 
58199653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
58299653d4eSeschrock 		    spa_version(spa)) == 0);
58399653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
58499653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
58599653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
58699653d4eSeschrock 		    vd->vdev_guid) == 0);
587*fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
588*fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
589*fa94a07fSbrendan 		/*
590*fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
591*fa94a07fSbrendan 		 */
592*fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
593*fa94a07fSbrendan 
594*fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
595*fa94a07fSbrendan 		    spa_version(spa)) == 0);
596*fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
597*fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
598*fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
599*fa94a07fSbrendan 		    vd->vdev_guid) == 0);
60099653d4eSeschrock 	} else {
60199653d4eSeschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
60299653d4eSeschrock 
60399653d4eSeschrock 		/*
60499653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
60599653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
60699653d4eSeschrock 		 * fail.
60799653d4eSeschrock 		 */
60899653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
60999653d4eSeschrock 		    crtxg) == 0);
61099653d4eSeschrock 	}
611fa9e4066Sahrens 
612fa9e4066Sahrens 	buf = vp->vp_nvlist;
613fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
614fa9e4066Sahrens 
615a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
616a75573b6Smmusante 	if (error != 0) {
617fa9e4066Sahrens 		nvlist_free(label);
618fa9e4066Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
619a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
620a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
621fa9e4066Sahrens 	}
622fa9e4066Sahrens 
623fa9e4066Sahrens 	/*
624fa9e4066Sahrens 	 * Initialize boot block header.
625fa9e4066Sahrens 	 */
626fa9e4066Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
627fa9e4066Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
628fa9e4066Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
629fa9e4066Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
630fa9e4066Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
631fa9e4066Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
632fa9e4066Sahrens 
633fa9e4066Sahrens 	/*
634fa9e4066Sahrens 	 * Initialize uberblock template.
635fa9e4066Sahrens 	 */
636ecc2d604Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
637ecc2d604Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
638ecc2d604Sbonwick 	*ub = spa->spa_uberblock;
639ecc2d604Sbonwick 	ub->ub_txg = 0;
640fa9e4066Sahrens 
641fa9e4066Sahrens 	/*
642fa9e4066Sahrens 	 * Write everything in parallel.
643fa9e4066Sahrens 	 */
644fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
645fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
646fa9e4066Sahrens 
647fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
648fa9e4066Sahrens 
649fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
650fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
651fa9e4066Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
652fa9e4066Sahrens 
653fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vb,
654fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
655fa9e4066Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
656fa9e4066Sahrens 
657ecc2d604Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
658ecc2d604Sbonwick 			vdev_label_write(zio, vd, l, ub,
659ecc2d604Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
660ecc2d604Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL);
661fa9e4066Sahrens 		}
662fa9e4066Sahrens 	}
663fa9e4066Sahrens 
664fa9e4066Sahrens 	error = zio_wait(zio);
665fa9e4066Sahrens 
666fa9e4066Sahrens 	nvlist_free(label);
667ecc2d604Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
668fa9e4066Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
669fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
670fa9e4066Sahrens 
67139c23413Seschrock 	/*
67239c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
6733d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
674*fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
675*fa94a07fSbrendan 	 * level 2 ARC devices.
67639c23413Seschrock 	 */
67739c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
67839c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
67939c23413Seschrock 	    spa_spare_exists(vd->vdev_guid, NULL)))
68039c23413Seschrock 		spa_spare_add(vd);
68199653d4eSeschrock 
682*fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
683*fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
684*fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
685*fa94a07fSbrendan 		spa_l2cache_add(vd);
686*fa94a07fSbrendan 
68739c23413Seschrock 	return (error);
68899653d4eSeschrock }
68999653d4eSeschrock 
690fa9e4066Sahrens /*
691fa9e4066Sahrens  * ==========================================================================
692fa9e4066Sahrens  * uberblock load/sync
693fa9e4066Sahrens  * ==========================================================================
694fa9e4066Sahrens  */
695fa9e4066Sahrens 
696fa9e4066Sahrens /*
697fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
698fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
699fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
700fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
701fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
702fa9e4066Sahrens  * the missing replica comes back, then for a new seconds we'll have two
703fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
704fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
705fa9e4066Sahrens  */
706fa9e4066Sahrens static int
707fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
708fa9e4066Sahrens {
709fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
710fa9e4066Sahrens 		return (-1);
711fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
712fa9e4066Sahrens 		return (1);
713fa9e4066Sahrens 
714fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
715fa9e4066Sahrens 		return (-1);
716fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
717fa9e4066Sahrens 		return (1);
718fa9e4066Sahrens 
719fa9e4066Sahrens 	return (0);
720fa9e4066Sahrens }
721fa9e4066Sahrens 
722fa9e4066Sahrens static void
723fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
724fa9e4066Sahrens {
725ecc2d604Sbonwick 	uberblock_t *ub = zio->io_data;
726fa9e4066Sahrens 	uberblock_t *ubbest = zio->io_private;
727fa9e4066Sahrens 	spa_t *spa = zio->io_spa;
728fa9e4066Sahrens 
729ecc2d604Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
730fa9e4066Sahrens 
731ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
732fa9e4066Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
733fa9e4066Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
734fa9e4066Sahrens 			*ubbest = *ub;
735fa9e4066Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
736fa9e4066Sahrens 	}
737fa9e4066Sahrens 
738fa9e4066Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
739fa9e4066Sahrens }
740fa9e4066Sahrens 
741fa9e4066Sahrens void
742fa9e4066Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
743fa9e4066Sahrens {
744fa9e4066Sahrens 	int l, c, n;
745fa9e4066Sahrens 
746fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
747fa9e4066Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
748fa9e4066Sahrens 
749fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
750fa9e4066Sahrens 		return;
751fa9e4066Sahrens 
752fa9e4066Sahrens 	if (vdev_is_dead(vd))
753fa9e4066Sahrens 		return;
754fa9e4066Sahrens 
755fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
756ecc2d604Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
757fa9e4066Sahrens 			vdev_label_read(zio, vd, l,
758ecc2d604Sbonwick 			    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
759ecc2d604Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
760ecc2d604Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd),
761fa9e4066Sahrens 			    vdev_uberblock_load_done, ubbest);
762fa9e4066Sahrens 		}
763fa9e4066Sahrens 	}
764fa9e4066Sahrens }
765fa9e4066Sahrens 
766fa9e4066Sahrens /*
767fa9e4066Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
7680373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
769fa9e4066Sahrens  */
770fa9e4066Sahrens static void
771fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
772fa9e4066Sahrens {
773fa9e4066Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
774fa9e4066Sahrens 
7750373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
776fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
777fa9e4066Sahrens }
778fa9e4066Sahrens 
779fa9e4066Sahrens static void
780ecc2d604Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, uint64_t txg)
781fa9e4066Sahrens {
782fa9e4066Sahrens 	int l, c, n;
783fa9e4066Sahrens 
784fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
785ecc2d604Sbonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], txg);
786fa9e4066Sahrens 
787fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
788fa9e4066Sahrens 		return;
789fa9e4066Sahrens 
790fa9e4066Sahrens 	if (vdev_is_dead(vd))
791fa9e4066Sahrens 		return;
792fa9e4066Sahrens 
793ecc2d604Sbonwick 	n = txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
794fa9e4066Sahrens 
795ecc2d604Sbonwick 	ASSERT(ub->ub_txg == txg);
796fa9e4066Sahrens 
797fa9e4066Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
798ecc2d604Sbonwick 		vdev_label_write(zio, vd, l, ub,
799ecc2d604Sbonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n),
800ecc2d604Sbonwick 		    VDEV_UBERBLOCK_SIZE(vd),
801ecc2d604Sbonwick 		    vdev_uberblock_sync_done, NULL);
802fa9e4066Sahrens 
803fa9e4066Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
804fa9e4066Sahrens }
805fa9e4066Sahrens 
806fa9e4066Sahrens static int
807ecc2d604Sbonwick vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *vd, uint64_t txg)
808fa9e4066Sahrens {
809ecc2d604Sbonwick 	uberblock_t *ubbuf;
810ecc2d604Sbonwick 	size_t size = vd->vdev_top ? VDEV_UBERBLOCK_SIZE(vd) : SPA_MAXBLOCKSIZE;
811fa9e4066Sahrens 	uint64_t *good_writes;
812fa9e4066Sahrens 	zio_t *zio;
813fa9e4066Sahrens 	int error;
814fa9e4066Sahrens 
815ecc2d604Sbonwick 	ubbuf = zio_buf_alloc(size);
816ecc2d604Sbonwick 	bzero(ubbuf, size);
817ecc2d604Sbonwick 	*ubbuf = *ub;
818fa9e4066Sahrens 
819fa9e4066Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
820fa9e4066Sahrens 
821fa9e4066Sahrens 	zio = zio_root(spa, NULL, good_writes,
822fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
823fa9e4066Sahrens 
824ecc2d604Sbonwick 	vdev_uberblock_sync(zio, ubbuf, vd, txg);
825fa9e4066Sahrens 
826fa9e4066Sahrens 	error = zio_wait(zio);
827fa9e4066Sahrens 
828fa9e4066Sahrens 	if (error && *good_writes != 0) {
829fa9e4066Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
830fa9e4066Sahrens 		error = 0;
831fa9e4066Sahrens 	}
832fa9e4066Sahrens 
833fa9e4066Sahrens 	/*
834fa9e4066Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
835fa9e4066Sahrens 	 * the CANT_OPEN state.
836fa9e4066Sahrens 	 */
837fa9e4066Sahrens 	if (*good_writes == 0 && error == 0)
838fa9e4066Sahrens 		error = EIO;
839fa9e4066Sahrens 
840fa9e4066Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
841ecc2d604Sbonwick 	zio_buf_free(ubbuf, size);
842fa9e4066Sahrens 
843fa9e4066Sahrens 	return (error);
844fa9e4066Sahrens }
845fa9e4066Sahrens 
846fa9e4066Sahrens /*
847fa9e4066Sahrens  * Sync out an individual vdev.
848fa9e4066Sahrens  */
849fa9e4066Sahrens static void
850fa9e4066Sahrens vdev_sync_label_done(zio_t *zio)
851fa9e4066Sahrens {
852fa9e4066Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
853fa9e4066Sahrens 
854fa9e4066Sahrens 	if (zio->io_error == 0)
855fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
856fa9e4066Sahrens }
857fa9e4066Sahrens 
858fa9e4066Sahrens static void
859fa9e4066Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
860fa9e4066Sahrens {
861fa9e4066Sahrens 	nvlist_t *label;
862fa9e4066Sahrens 	vdev_phys_t *vp;
863fa9e4066Sahrens 	char *buf;
864fa9e4066Sahrens 	size_t buflen;
865fa9e4066Sahrens 	int c;
866fa9e4066Sahrens 
867fa9e4066Sahrens 	for (c = 0; c < vd->vdev_children; c++)
868fa9e4066Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
869fa9e4066Sahrens 
870fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
871fa9e4066Sahrens 		return;
872fa9e4066Sahrens 
873fa9e4066Sahrens 	if (vdev_is_dead(vd))
874fa9e4066Sahrens 		return;
875fa9e4066Sahrens 
876fa9e4066Sahrens 	/*
877fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
878fa9e4066Sahrens 	 */
8790373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
880fa9e4066Sahrens 
881fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
882fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
883fa9e4066Sahrens 
884fa9e4066Sahrens 	buf = vp->vp_nvlist;
885fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
886fa9e4066Sahrens 
887ea8dc4b6Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
888fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
889fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
890fa9e4066Sahrens 		    vdev_sync_label_done, NULL);
891fa9e4066Sahrens 
892fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
893fa9e4066Sahrens 	nvlist_free(label);
894fa9e4066Sahrens 
895fa9e4066Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
896fa9e4066Sahrens }
897fa9e4066Sahrens 
898fa9e4066Sahrens static int
899fa9e4066Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
900fa9e4066Sahrens {
901fa9e4066Sahrens 	uint64_t *good_writes;
902fa9e4066Sahrens 	zio_t *zio;
903fa9e4066Sahrens 	int error;
904fa9e4066Sahrens 
905fa9e4066Sahrens 	ASSERT(vd == vd->vdev_top);
906fa9e4066Sahrens 
907fa9e4066Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
908fa9e4066Sahrens 
909fa9e4066Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
910fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
911fa9e4066Sahrens 
912fa9e4066Sahrens 	/*
913fa9e4066Sahrens 	 * Recursively kick off writes to all labels.
914fa9e4066Sahrens 	 */
915fa9e4066Sahrens 	vdev_sync_label(zio, vd, l, txg);
916fa9e4066Sahrens 
917fa9e4066Sahrens 	error = zio_wait(zio);
918fa9e4066Sahrens 
919fa9e4066Sahrens 	if (error && *good_writes != 0) {
920fa9e4066Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
921fa9e4066Sahrens 		error = 0;
922fa9e4066Sahrens 	}
923fa9e4066Sahrens 
924fa9e4066Sahrens 	if (*good_writes == 0 && error == 0)
925fa9e4066Sahrens 		error = ENODEV;
926fa9e4066Sahrens 
9278654d025Sperrin 	/*
9288654d025Sperrin 	 * Failure to write a label can be fatal for a
9298654d025Sperrin 	 * top level vdev. We don't want this for slogs
9308654d025Sperrin 	 * as we use the main pool if they go away.
9318654d025Sperrin 	 */
9328654d025Sperrin 	if (vd->vdev_islog)
9338654d025Sperrin 		error = 0;
9348654d025Sperrin 
935fa9e4066Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
936fa9e4066Sahrens 
937fa9e4066Sahrens 	return (error);
938fa9e4066Sahrens }
939fa9e4066Sahrens 
940fa9e4066Sahrens /*
941fa9e4066Sahrens  * Sync the entire vdev configuration.
942fa9e4066Sahrens  *
943fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
944fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
945fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
946fa9e4066Sahrens  * describe the failure semantics at each stage.
947fa9e4066Sahrens  *
948fa9e4066Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
949fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
950fa9e4066Sahrens  */
951fa9e4066Sahrens int
9520373e76bSbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg)
953fa9e4066Sahrens {
9540373e76bSbonwick 	spa_t *spa = uvd->vdev_spa;
955fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
956fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
9570373e76bSbonwick 	vdev_t *vd;
958fa9e4066Sahrens 	zio_t *zio;
9590a4e9518Sgw 	int l, last_error = 0, error = 0;
9600a4e9518Sgw 	uint64_t good_writes = 0;
9610a4e9518Sgw 	boolean_t retry_avail = B_TRUE;
962fa9e4066Sahrens 
963fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
964fa9e4066Sahrens 
965fa9e4066Sahrens 	/*
966fa9e4066Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
967fa9e4066Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
9680373e76bSbonwick 	 * then there's nothing to do.
969fa9e4066Sahrens 	 */
970fa9e4066Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
971fa9e4066Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
972fa9e4066Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
973fa9e4066Sahrens 		    spa_name(spa), txg);
974fa9e4066Sahrens 		return (0);
975fa9e4066Sahrens 	}
976fa9e4066Sahrens 
977fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
978fa9e4066Sahrens 		return (0);
979fa9e4066Sahrens 
9800373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
9810373e76bSbonwick 
982fa9e4066Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
983fa9e4066Sahrens 
984fa9e4066Sahrens 	/*
985fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
986fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
987fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
988fa9e4066Sahrens 	 * before any uberblock that references them.
989fa9e4066Sahrens 	 */
990fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
991fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
992fa9e4066Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
993fa9e4066Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
994fa9e4066Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
995fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
996fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
997fa9e4066Sahrens 	}
998fa9e4066Sahrens 	(void) zio_wait(zio);
999fa9e4066Sahrens 
10000a4e9518Sgw retry:
1001fa9e4066Sahrens 	/*
1002fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1003fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1004fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1005fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
1006fa9e4066Sahrens 	 * which have not yet been touched, will still be valid.
1007fa9e4066Sahrens 	 */
1008fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1009fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1010fa9e4066Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
1011fa9e4066Sahrens 			if (l & 1)
1012fa9e4066Sahrens 				continue;
1013fa9e4066Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
10140a4e9518Sgw 				last_error = error;
10150a4e9518Sgw 			else
10160a4e9518Sgw 				good_writes++;
1017fa9e4066Sahrens 		}
1018fa9e4066Sahrens 	}
1019fa9e4066Sahrens 
10200a4e9518Sgw 	/*
10210a4e9518Sgw 	 * If all the vdevs that are currently dirty have failed or the
10220a4e9518Sgw 	 * spa_dirty_list is empty then we dirty all the vdevs and try again.
10230a4e9518Sgw 	 * This is a last ditch effort to ensure that we get at least one
10240a4e9518Sgw 	 * update before proceeding to the uberblock.
10250a4e9518Sgw 	 */
10260a4e9518Sgw 	if (good_writes == 0 && retry_avail) {
10270a4e9518Sgw 		vdev_config_dirty(rvd);
10280a4e9518Sgw 		retry_avail = B_FALSE;
10290a4e9518Sgw 		last_error = 0;
10300a4e9518Sgw 		goto retry;
10310a4e9518Sgw 	}
10320a4e9518Sgw 
10330a4e9518Sgw 	if (good_writes == 0)
10340a4e9518Sgw 		return (last_error);
10350a4e9518Sgw 
1036fa9e4066Sahrens 	/*
1037fa9e4066Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
1038fa9e4066Sahrens 	 * updates are committed to stable storage before the uberblock update.
1039fa9e4066Sahrens 	 */
1040fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
1041fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
1042fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1043fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1044fa9e4066Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
1045fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
1046fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1047fa9e4066Sahrens 	}
1048fa9e4066Sahrens 	(void) zio_wait(zio);
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 	/*
10510373e76bSbonwick 	 * Sync the uberblocks to all vdevs in the tree specified by uvd.
10520373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
10530373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1054fa9e4066Sahrens 	 *
1055fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1056fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1057fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1058fa9e4066Sahrens 	 *	to that uberblock.
1059fa9e4066Sahrens 	 *
1060fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1061fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1062fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1063fa9e4066Sahrens 	 *	to the new uberblocks.
10640a4e9518Sgw 	 *
10650a4e9518Sgw 	 * NOTE: We retry to an uberblock update on the root if we were
10660a4e9518Sgw 	 * failed our initial update attempt.
1067fa9e4066Sahrens 	 */
10680a4e9518Sgw 	error = vdev_uberblock_sync_tree(spa, ub, uvd, txg);
10690a4e9518Sgw 	if (error && uvd != rvd)
10700a4e9518Sgw 		error = vdev_uberblock_sync_tree(spa, ub, rvd, txg);
10710a4e9518Sgw 
10720a4e9518Sgw 	if (error)
1073fa9e4066Sahrens 		return (error);
1074fa9e4066Sahrens 
1075fa9e4066Sahrens 	/*
1076fa9e4066Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
1077fa9e4066Sahrens 	 * are no longer needed (because the new uberblocks and the even
1078fa9e4066Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
1079fa9e4066Sahrens 	 */
1080fa9e4066Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
1081fa9e4066Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
1082fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1083fa9e4066Sahrens 
10840a4e9518Sgw 	last_error = 0;
1085fa9e4066Sahrens 	/*
1086fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1087fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1088fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1089fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1090fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
1091fa9e4066Sahrens 	 * all labels will be brought up to date.
1092fa9e4066Sahrens 	 */
1093fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1094fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1095fa9e4066Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
1096fa9e4066Sahrens 			if ((l & 1) == 0)
1097fa9e4066Sahrens 				continue;
1098fa9e4066Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
10990a4e9518Sgw 				last_error = error;
11000a4e9518Sgw 			else
11010a4e9518Sgw 				good_writes++;
1102fa9e4066Sahrens 		}
1103fa9e4066Sahrens 	}
1104fa9e4066Sahrens 
11050a4e9518Sgw 	if (good_writes == 0)
11060a4e9518Sgw 		return (last_error);
11070a4e9518Sgw 
1108fa9e4066Sahrens 	/*
1109fa9e4066Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
1110fa9e4066Sahrens 	 * updates are committed to stable storage before the next
1111fa9e4066Sahrens 	 * transaction group begins.
1112fa9e4066Sahrens 	 */
1113fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
1114fa9e4066Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
1115fa9e4066Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1116fa9e4066Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1117fa9e4066Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
1118fa9e4066Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
1119fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1120fa9e4066Sahrens 	}
1121fa9e4066Sahrens 	(void) zio_wait(zio);
1122fa9e4066Sahrens 
1123fa9e4066Sahrens 	return (0);
1124fa9e4066Sahrens }
1125