xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 3460:e7801868c7bc)
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 /*
223377Seschrock  * Copyright 2007 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 *
1902082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
1912082Seschrock     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);
1992082Seschrock 	if (!isspare)
2002082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
2012082Seschrock 		    == 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 
2122082Seschrock 	if (vd->vdev_nparity != 0) {
2132082Seschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
2142082Seschrock 		    VDEV_TYPE_RAIDZ) == 0);
2152082Seschrock 
2162082Seschrock 		/*
2172082Seschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
2182082Seschrock 		 * into a crufty old storage pool.
2192082Seschrock 		 */
2202082Seschrock 		ASSERT(vd->vdev_nparity == 1 ||
2212082Seschrock 		    (vd->vdev_nparity == 2 &&
2222082Seschrock 		    spa_version(spa) >= ZFS_VERSION_RAID6));
2232082Seschrock 
2242082Seschrock 		/*
2252082Seschrock 		 * Note that we'll add the nparity tag even on storage pools
2262082Seschrock 		 * that only support a single parity device -- older software
2272082Seschrock 		 * will just ignore it.
2282082Seschrock 		 */
2292082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
2302082Seschrock 		    vd->vdev_nparity) == 0);
2312082Seschrock 	}
2322082Seschrock 
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 
2402082Seschrock 	if (vd->vdev_isspare)
2412082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
2422082Seschrock 
2432082Seschrock 	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++)
2732082Seschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
2742082Seschrock 			    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 
3373377Seschrock /*
3383377Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
3393377Seschrock  * in with the device guid if this spare is active elsewhere on the system.
3403377Seschrock  */
3413377Seschrock static boolean_t
3423377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
3433377Seschrock     uint64_t *spare_guid)
3443377Seschrock {
3453377Seschrock 	spa_t *spa = vd->vdev_spa;
3463377Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
3473377Seschrock 	uint64_t vdtxg = 0;
3483377Seschrock 	nvlist_t *label;
3493377Seschrock 
3503377Seschrock 	if (spare_guid)
3513377Seschrock 		*spare_guid = 0ULL;
3523377Seschrock 
3533377Seschrock 	/*
3543377Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
3553377Seschrock 	 */
3563377Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
3573377Seschrock 		return (B_FALSE);
3583377Seschrock 
3593377Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
3603377Seschrock 	    &vdtxg);
3613377Seschrock 
3623377Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
3633377Seschrock 	    &state) != 0 ||
3643377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
3653377Seschrock 	    &device_guid) != 0) {
3663377Seschrock 		nvlist_free(label);
3673377Seschrock 		return (B_FALSE);
3683377Seschrock 	}
3693377Seschrock 
3703377Seschrock 	if (state != POOL_STATE_SPARE &&
3713377Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
3723377Seschrock 	    &pool_guid) != 0 ||
3733377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3743377Seschrock 	    &txg) != 0)) {
3753377Seschrock 		nvlist_free(label);
3763377Seschrock 		return (B_FALSE);
3773377Seschrock 	}
3783377Seschrock 
3793377Seschrock 	nvlist_free(label);
3803377Seschrock 
3813377Seschrock 	/*
3823377Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
3833377Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
3843377Seschrock 	 * spare (which we check for later on).
3853377Seschrock 	 */
3863377Seschrock 	if (state != POOL_STATE_SPARE &&
3873377Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
3883377Seschrock 	    !spa_spare_exists(device_guid, NULL))
3893377Seschrock 		return (B_FALSE);
3903377Seschrock 
3913377Seschrock 	/*
3923377Seschrock 	 * If the transaction group is zero, then this an initialized (but
3933377Seschrock 	 * unused) label.  This is only an error if the create transaction
3943377Seschrock 	 * on-disk is the same as the one we're using now, in which case the
3953377Seschrock 	 * user has attempted to add the same vdev multiple times in the same
3963377Seschrock 	 * transaction.
3973377Seschrock 	 */
3983377Seschrock 	if (state != POOL_STATE_SPARE && txg == 0 && vdtxg == crtxg)
3993377Seschrock 		return (B_TRUE);
4003377Seschrock 
4013377Seschrock 	/*
4023377Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
4033377Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
4043377Seschrock 	 * to add.
4053377Seschrock 	 */
4063377Seschrock 	if (spa_spare_exists(device_guid, &spare_pool) ||
4073377Seschrock 	    spa_has_spare(spa, device_guid)) {
4083377Seschrock 		if (spare_guid)
4093377Seschrock 			*spare_guid = device_guid;
4103377Seschrock 
4113377Seschrock 		switch (reason) {
4123377Seschrock 		case VDEV_LABEL_CREATE:
4133377Seschrock 			return (B_TRUE);
4143377Seschrock 
4153377Seschrock 		case VDEV_LABEL_REPLACE:
4163377Seschrock 			return (!spa_has_spare(spa, device_guid) ||
4173377Seschrock 			    spare_pool != 0ULL);
4183377Seschrock 
4193377Seschrock 		case VDEV_LABEL_SPARE:
4203377Seschrock 			return (spa_has_spare(spa, device_guid));
4213377Seschrock 		}
4223377Seschrock 	}
4233377Seschrock 
4243377Seschrock 	/*
4253377Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
4263377Seschrock 	 * pool on the system.
4273377Seschrock 	 */
4283377Seschrock 	return (state == POOL_STATE_ACTIVE);
4293377Seschrock }
4303377Seschrock 
4313377Seschrock /*
4323377Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
4333377Seschrock  * use, and writable.  We put down an initial label which we will later
4343377Seschrock  * overwrite with a complete label.  Note that it's important to do this
4353377Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
4363377Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
4373377Seschrock  * itself.
4383377Seschrock  */
4393377Seschrock int
4403377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
441789Sahrens {
442789Sahrens 	spa_t *spa = vd->vdev_spa;
443789Sahrens 	nvlist_t *label;
444789Sahrens 	vdev_phys_t *vp;
445789Sahrens 	vdev_boot_header_t *vb;
4461732Sbonwick 	uberblock_t *ub;
447789Sahrens 	zio_t *zio;
448789Sahrens 	int l, c, n;
449789Sahrens 	char *buf;
450789Sahrens 	size_t buflen;
451789Sahrens 	int error;
4523377Seschrock 	uint64_t spare_guid;
453789Sahrens 
4541635Sbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
4551635Sbonwick 
456789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
4573377Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
4583377Seschrock 		    crtxg, reason)) != 0)
459789Sahrens 			return (error);
460789Sahrens 
461789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
462789Sahrens 		return (0);
463789Sahrens 
464789Sahrens 	/*
4653377Seschrock 	 * Dead vdevs cannot be initialized.
466789Sahrens 	 */
467789Sahrens 	if (vdev_is_dead(vd))
468789Sahrens 		return (EIO);
469789Sahrens 
470789Sahrens 	/*
4713377Seschrock 	 * Determine if the vdev is in use.
472789Sahrens 	 */
4733377Seschrock 	if (reason != VDEV_LABEL_REMOVE &&
4743377Seschrock 	    vdev_inuse(vd, crtxg, reason, &spare_guid))
4753377Seschrock 		return (EBUSY);
476789Sahrens 
4773377Seschrock 	ASSERT(reason != VDEV_LABEL_REMOVE ||
4783377Seschrock 	    vdev_inuse(vd, crtxg, reason, NULL));
479789Sahrens 
4803377Seschrock 	/*
4813377Seschrock 	 * If this is a request to add or replace a spare that is in use
4823377Seschrock 	 * elsewhere on the system, then we must update the guid (which was
4833377Seschrock 	 * initialized to a random value) to reflect the actual GUID (which is
4843377Seschrock 	 * shared between multiple pools).
4853377Seschrock 	 */
4863377Seschrock 	if (reason != VDEV_LABEL_REMOVE && spare_guid != 0ULL) {
4873377Seschrock 		vdev_t *pvd = vd->vdev_parent;
4882082Seschrock 
4893377Seschrock 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
4903377Seschrock 			pvd->vdev_guid_sum -= vd->vdev_guid;
4913377Seschrock 			pvd->vdev_guid_sum += spare_guid;
492789Sahrens 		}
4932082Seschrock 
4943377Seschrock 		vd->vdev_guid = vd->vdev_guid_sum = spare_guid;
4952082Seschrock 
4963377Seschrock 		/*
4973377Seschrock 		 * If this is a replacement, then we want to fallthrough to the
4983377Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
4993377Seschrock 		 * labelled appropriately and we can just return.
5003377Seschrock 		 */
5013377Seschrock 		if (reason == VDEV_LABEL_SPARE)
5023377Seschrock 			return (0);
5033377Seschrock 		ASSERT(reason == VDEV_LABEL_REPLACE);
504789Sahrens 	}
505789Sahrens 
506789Sahrens 	/*
5073377Seschrock 	 * Initialize its label.
508789Sahrens 	 */
509789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
510789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
511789Sahrens 
512789Sahrens 	/*
513789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
514789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
515789Sahrens 	 * really part of an active pool just yet.  The labels will
516789Sahrens 	 * be written again with a meaningful txg by spa_sync().
517789Sahrens 	 */
5183377Seschrock 	if (reason == VDEV_LABEL_SPARE ||
5193377Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
5203377Seschrock 		/*
5213377Seschrock 		 * For inactive hot spares, we generate a special label that
5223377Seschrock 		 * identifies as a mutually shared hot spare.  We write the
5233377Seschrock 		 * label if we are adding a hot spare, or if we are removing an
5243377Seschrock 		 * active hot spare (in which case we want to revert the
5253377Seschrock 		 * labels).
5263377Seschrock 		 */
5272082Seschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
528789Sahrens 
5292082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
5302082Seschrock 		    spa_version(spa)) == 0);
5312082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
5322082Seschrock 		    POOL_STATE_SPARE) == 0);
5332082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
5342082Seschrock 		    vd->vdev_guid) == 0);
5352082Seschrock 	} else {
5362082Seschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
5372082Seschrock 
5382082Seschrock 		/*
5392082Seschrock 		 * Add our creation time.  This allows us to detect multiple
5402082Seschrock 		 * vdev uses as described above, and automatically expires if we
5412082Seschrock 		 * fail.
5422082Seschrock 		 */
5432082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
5442082Seschrock 		    crtxg) == 0);
5452082Seschrock 	}
546789Sahrens 
547789Sahrens 	buf = vp->vp_nvlist;
548789Sahrens 	buflen = sizeof (vp->vp_nvlist);
549789Sahrens 
550*3460Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
551*3460Smmusante 	if (error != 0) {
552789Sahrens 		nvlist_free(label);
553789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
554*3460Smmusante 		/* EFAULT means nvlist_pack ran out of room */
555*3460Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
556789Sahrens 	}
557789Sahrens 
558789Sahrens 	/*
559789Sahrens 	 * Initialize boot block header.
560789Sahrens 	 */
561789Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
562789Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
563789Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
564789Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
565789Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
566789Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
567789Sahrens 
568789Sahrens 	/*
569789Sahrens 	 * Initialize uberblock template.
570789Sahrens 	 */
5711732Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
5721732Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
5731732Sbonwick 	*ub = spa->spa_uberblock;
5741732Sbonwick 	ub->ub_txg = 0;
575789Sahrens 
576789Sahrens 	/*
577789Sahrens 	 * Write everything in parallel.
578789Sahrens 	 */
579789Sahrens 	zio = zio_root(spa, NULL, NULL,
580789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
581789Sahrens 
582789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
583789Sahrens 
584789Sahrens 		vdev_label_write(zio, vd, l, vp,
585789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
586789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
587789Sahrens 
588789Sahrens 		vdev_label_write(zio, vd, l, vb,
589789Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
590789Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
591789Sahrens 
5921732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
5931732Sbonwick 			vdev_label_write(zio, vd, l, ub,
5941732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
5951732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL);
596789Sahrens 		}
597789Sahrens 	}
598789Sahrens 
599789Sahrens 	error = zio_wait(zio);
600789Sahrens 
601789Sahrens 	nvlist_free(label);
6021732Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
603789Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
604789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
605789Sahrens 
6063377Seschrock 	/*
6073377Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
6083377Seschrock 	 * mark it as such only if a) we are labelling it as a spare, or b) it
6093377Seschrock 	 * exists as a spare elsewhere in the system.
6103377Seschrock 	 */
6113377Seschrock 	if (error == 0 && !vd->vdev_isspare &&
6123377Seschrock 	    (reason == VDEV_LABEL_SPARE ||
6133377Seschrock 	    spa_spare_exists(vd->vdev_guid, NULL)))
6143377Seschrock 		spa_spare_add(vd);
6152082Seschrock 
6163377Seschrock 	return (error);
6172082Seschrock }
6182082Seschrock 
619789Sahrens /*
620789Sahrens  * ==========================================================================
621789Sahrens  * uberblock load/sync
622789Sahrens  * ==========================================================================
623789Sahrens  */
624789Sahrens 
625789Sahrens /*
626789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
627789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
628789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
629789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
630789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
631789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
632789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
633789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
634789Sahrens  */
635789Sahrens static int
636789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
637789Sahrens {
638789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
639789Sahrens 		return (-1);
640789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
641789Sahrens 		return (1);
642789Sahrens 
643789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
644789Sahrens 		return (-1);
645789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
646789Sahrens 		return (1);
647789Sahrens 
648789Sahrens 	return (0);
649789Sahrens }
650789Sahrens 
651789Sahrens static void
652789Sahrens vdev_uberblock_load_done(zio_t *zio)
653789Sahrens {
6541732Sbonwick 	uberblock_t *ub = zio->io_data;
655789Sahrens 	uberblock_t *ubbest = zio->io_private;
656789Sahrens 	spa_t *spa = zio->io_spa;
657789Sahrens 
6581732Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
659789Sahrens 
6601544Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
661789Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
662789Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
663789Sahrens 			*ubbest = *ub;
664789Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
665789Sahrens 	}
666789Sahrens 
667789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
668789Sahrens }
669789Sahrens 
670789Sahrens void
671789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
672789Sahrens {
673789Sahrens 	int l, c, n;
674789Sahrens 
675789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
676789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
677789Sahrens 
678789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
679789Sahrens 		return;
680789Sahrens 
681789Sahrens 	if (vdev_is_dead(vd))
682789Sahrens 		return;
683789Sahrens 
684789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
6851732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
686789Sahrens 			vdev_label_read(zio, vd, l,
6871732Sbonwick 			    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
6881732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
6891732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd),
690789Sahrens 			    vdev_uberblock_load_done, ubbest);
691789Sahrens 		}
692789Sahrens 	}
693789Sahrens }
694789Sahrens 
695789Sahrens /*
696789Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
6971635Sbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
698789Sahrens  */
699789Sahrens static void
700789Sahrens vdev_uberblock_sync_done(zio_t *zio)
701789Sahrens {
702789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
703789Sahrens 
7041635Sbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
705789Sahrens 		atomic_add_64(good_writes, 1);
706789Sahrens }
707789Sahrens 
708789Sahrens static void
7091732Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, uint64_t txg)
710789Sahrens {
711789Sahrens 	int l, c, n;
712789Sahrens 
713789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
7141732Sbonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], txg);
715789Sahrens 
716789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
717789Sahrens 		return;
718789Sahrens 
719789Sahrens 	if (vdev_is_dead(vd))
720789Sahrens 		return;
721789Sahrens 
7221732Sbonwick 	n = txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
723789Sahrens 
7241732Sbonwick 	ASSERT(ub->ub_txg == txg);
725789Sahrens 
726789Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
7271732Sbonwick 		vdev_label_write(zio, vd, l, ub,
7281732Sbonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n),
7291732Sbonwick 		    VDEV_UBERBLOCK_SIZE(vd),
7301732Sbonwick 		    vdev_uberblock_sync_done, NULL);
731789Sahrens 
732789Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
733789Sahrens }
734789Sahrens 
735789Sahrens static int
7361732Sbonwick vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *vd, uint64_t txg)
737789Sahrens {
7381732Sbonwick 	uberblock_t *ubbuf;
7391732Sbonwick 	size_t size = vd->vdev_top ? VDEV_UBERBLOCK_SIZE(vd) : SPA_MAXBLOCKSIZE;
740789Sahrens 	uint64_t *good_writes;
741789Sahrens 	zio_t *zio;
742789Sahrens 	int error;
743789Sahrens 
7441732Sbonwick 	ubbuf = zio_buf_alloc(size);
7451732Sbonwick 	bzero(ubbuf, size);
7461732Sbonwick 	*ubbuf = *ub;
747789Sahrens 
748789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
749789Sahrens 
750789Sahrens 	zio = zio_root(spa, NULL, good_writes,
751789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
752789Sahrens 
7531732Sbonwick 	vdev_uberblock_sync(zio, ubbuf, vd, txg);
754789Sahrens 
755789Sahrens 	error = zio_wait(zio);
756789Sahrens 
757789Sahrens 	if (error && *good_writes != 0) {
758789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
759789Sahrens 		error = 0;
760789Sahrens 	}
761789Sahrens 
762789Sahrens 	/*
763789Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
764789Sahrens 	 * the CANT_OPEN state.
765789Sahrens 	 */
766789Sahrens 	if (*good_writes == 0 && error == 0)
767789Sahrens 		error = EIO;
768789Sahrens 
769789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
7701732Sbonwick 	zio_buf_free(ubbuf, size);
771789Sahrens 
772789Sahrens 	return (error);
773789Sahrens }
774789Sahrens 
775789Sahrens /*
776789Sahrens  * Sync out an individual vdev.
777789Sahrens  */
778789Sahrens static void
779789Sahrens vdev_sync_label_done(zio_t *zio)
780789Sahrens {
781789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
782789Sahrens 
783789Sahrens 	if (zio->io_error == 0)
784789Sahrens 		atomic_add_64(good_writes, 1);
785789Sahrens }
786789Sahrens 
787789Sahrens static void
788789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
789789Sahrens {
790789Sahrens 	nvlist_t *label;
791789Sahrens 	vdev_phys_t *vp;
792789Sahrens 	char *buf;
793789Sahrens 	size_t buflen;
794789Sahrens 	int c;
795789Sahrens 
796789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
797789Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
798789Sahrens 
799789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
800789Sahrens 		return;
801789Sahrens 
802789Sahrens 	if (vdev_is_dead(vd))
803789Sahrens 		return;
804789Sahrens 
805789Sahrens 	/*
806789Sahrens 	 * Generate a label describing the top-level config to which we belong.
807789Sahrens 	 */
8081635Sbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
809789Sahrens 
810789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
811789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
812789Sahrens 
813789Sahrens 	buf = vp->vp_nvlist;
814789Sahrens 	buflen = sizeof (vp->vp_nvlist);
815789Sahrens 
8161544Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
817789Sahrens 		vdev_label_write(zio, vd, l, vp,
818789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
819789Sahrens 		    vdev_sync_label_done, NULL);
820789Sahrens 
821789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
822789Sahrens 	nvlist_free(label);
823789Sahrens 
824789Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
825789Sahrens }
826789Sahrens 
827789Sahrens static int
828789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
829789Sahrens {
830789Sahrens 	uint64_t *good_writes;
831789Sahrens 	zio_t *zio;
832789Sahrens 	int error;
833789Sahrens 
834789Sahrens 	ASSERT(vd == vd->vdev_top);
835789Sahrens 
836789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
837789Sahrens 
838789Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
839789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
840789Sahrens 
841789Sahrens 	/*
842789Sahrens 	 * Recursively kick off writes to all labels.
843789Sahrens 	 */
844789Sahrens 	vdev_sync_label(zio, vd, l, txg);
845789Sahrens 
846789Sahrens 	error = zio_wait(zio);
847789Sahrens 
848789Sahrens 	if (error && *good_writes != 0) {
849789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
850789Sahrens 		error = 0;
851789Sahrens 	}
852789Sahrens 
853789Sahrens 	if (*good_writes == 0 && error == 0)
854789Sahrens 		error = ENODEV;
855789Sahrens 
856789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
857789Sahrens 
858789Sahrens 	return (error);
859789Sahrens }
860789Sahrens 
861789Sahrens /*
862789Sahrens  * Sync the entire vdev configuration.
863789Sahrens  *
864789Sahrens  * The order of operations is carefully crafted to ensure that
865789Sahrens  * if the system panics or loses power at any time, the state on disk
866789Sahrens  * is still transactionally consistent.  The in-line comments below
867789Sahrens  * describe the failure semantics at each stage.
868789Sahrens  *
869789Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
870789Sahrens  * at any time, you can just call it again, and it will resume its work.
871789Sahrens  */
872789Sahrens int
8731635Sbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg)
874789Sahrens {
8751635Sbonwick 	spa_t *spa = uvd->vdev_spa;
876789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
877789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
8781635Sbonwick 	vdev_t *vd;
879789Sahrens 	zio_t *zio;
8801637Sbonwick 	int l, error;
881789Sahrens 
882789Sahrens 	ASSERT(ub->ub_txg <= txg);
883789Sahrens 
884789Sahrens 	/*
885789Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
886789Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
8871635Sbonwick 	 * then there's nothing to do.
888789Sahrens 	 */
889789Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
890789Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
891789Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
892789Sahrens 		    spa_name(spa), txg);
893789Sahrens 		return (0);
894789Sahrens 	}
895789Sahrens 
896789Sahrens 	if (txg > spa_freeze_txg(spa))
897789Sahrens 		return (0);
898789Sahrens 
8991635Sbonwick 	ASSERT(txg <= spa->spa_final_txg);
9001635Sbonwick 
901789Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
902789Sahrens 
903789Sahrens 	/*
904789Sahrens 	 * Flush the write cache of every disk that's been written to
905789Sahrens 	 * in this transaction group.  This ensures that all blocks
906789Sahrens 	 * written in this txg will be committed to stable storage
907789Sahrens 	 * before any uberblock that references them.
908789Sahrens 	 */
909789Sahrens 	zio = zio_root(spa, NULL, NULL,
910789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
911789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
912789Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
913789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
914789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
915789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
916789Sahrens 	}
917789Sahrens 	(void) zio_wait(zio);
918789Sahrens 
919789Sahrens 	/*
920789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
921789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
922789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
923789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
924789Sahrens 	 * which have not yet been touched, will still be valid.
925789Sahrens 	 */
926789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
927789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
928789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
929789Sahrens 			if (l & 1)
930789Sahrens 				continue;
931789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
932789Sahrens 				return (error);
933789Sahrens 		}
934789Sahrens 	}
935789Sahrens 
936789Sahrens 	/*
937789Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
938789Sahrens 	 * updates are committed to stable storage before the uberblock update.
939789Sahrens 	 */
940789Sahrens 	zio = zio_root(spa, NULL, NULL,
941789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
942789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
943789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
944789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
945789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
946789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
947789Sahrens 	}
948789Sahrens 	(void) zio_wait(zio);
949789Sahrens 
950789Sahrens 	/*
9511635Sbonwick 	 * Sync the uberblocks to all vdevs in the tree specified by uvd.
9521635Sbonwick 	 * If the system dies in the middle of this step, there are two cases
9531635Sbonwick 	 * to consider, and the on-disk state is consistent either way:
954789Sahrens 	 *
955789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
956789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
957789Sahrens 	 *	(which had not yet been touched) will be valid with respect
958789Sahrens 	 *	to that uberblock.
959789Sahrens 	 *
960789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
961789Sahrens 	 *	will be the newest, and the even labels (which had all
962789Sahrens 	 *	been successfully committed) will be valid with respect
963789Sahrens 	 *	to the new uberblocks.
964789Sahrens 	 */
965789Sahrens 	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
966789Sahrens 		return (error);
967789Sahrens 
968789Sahrens 	/*
969789Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
970789Sahrens 	 * are no longer needed (because the new uberblocks and the even
971789Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
972789Sahrens 	 */
973789Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
974789Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
975789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
976789Sahrens 
977789Sahrens 	/*
978789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
979789Sahrens 	 * in the middle of this process, the even labels and the new
980789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
981789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
982789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
983789Sahrens 	 * all labels will be brought up to date.
984789Sahrens 	 */
985789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
986789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
987789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
988789Sahrens 			if ((l & 1) == 0)
989789Sahrens 				continue;
990789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
991789Sahrens 				return (error);
992789Sahrens 		}
993789Sahrens 	}
994789Sahrens 
995789Sahrens 	/*
996789Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
997789Sahrens 	 * updates are committed to stable storage before the next
998789Sahrens 	 * transaction group begins.
999789Sahrens 	 */
1000789Sahrens 	zio = zio_root(spa, NULL, NULL,
1001789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
1002789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1003789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1004789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
1005789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
1006789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1007789Sahrens 	}
1008789Sahrens 	(void) zio_wait(zio);
1009789Sahrens 
1010789Sahrens 	return (0);
1011789Sahrens }
1012