xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 4577:ed36b0e652bc)
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
654451Seschrock  * labels and an uberblock with the following transaction 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));
156*4577Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
1571732Sbonwick 
158789Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
159789Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
160789Sahrens }
161789Sahrens 
162789Sahrens static void
163789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
164789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
165789Sahrens {
166789Sahrens 	ASSERT(vd->vdev_children == 0);
167789Sahrens 
168789Sahrens 	zio_nowait(zio_read_phys(zio, vd,
169789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
170789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1711544Seschrock 	    ZIO_PRIORITY_SYNC_READ,
1721544Seschrock 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE));
173789Sahrens }
174789Sahrens 
175789Sahrens static void
176789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
177789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
178789Sahrens {
179789Sahrens 	ASSERT(vd->vdev_children == 0);
180789Sahrens 
181789Sahrens 	zio_nowait(zio_write_phys(zio, vd,
182789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
183789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1841544Seschrock 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL));
185789Sahrens }
186789Sahrens 
187789Sahrens /*
188789Sahrens  * Generate the nvlist representing this vdev's config.
189789Sahrens  */
190789Sahrens nvlist_t *
1912082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
1922082Seschrock     boolean_t isspare)
193789Sahrens {
194789Sahrens 	nvlist_t *nv = NULL;
195789Sahrens 
1961544Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
197789Sahrens 
198789Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
199789Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
2002082Seschrock 	if (!isspare)
2012082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
2022082Seschrock 		    == 0);
203789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
204789Sahrens 
205789Sahrens 	if (vd->vdev_path != NULL)
206789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
207789Sahrens 		    vd->vdev_path) == 0);
208789Sahrens 
209789Sahrens 	if (vd->vdev_devid != NULL)
210789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
211789Sahrens 		    vd->vdev_devid) == 0);
212789Sahrens 
2134451Seschrock 	if (vd->vdev_physpath != NULL)
2144451Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2154451Seschrock 		    vd->vdev_physpath) == 0);
2164451Seschrock 
2172082Seschrock 	if (vd->vdev_nparity != 0) {
2182082Seschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
2192082Seschrock 		    VDEV_TYPE_RAIDZ) == 0);
2202082Seschrock 
2212082Seschrock 		/*
2222082Seschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
2232082Seschrock 		 * into a crufty old storage pool.
2242082Seschrock 		 */
2252082Seschrock 		ASSERT(vd->vdev_nparity == 1 ||
2262082Seschrock 		    (vd->vdev_nparity == 2 &&
227*4577Sahrens 		    spa_version(spa) >= SPA_VERSION_RAID6));
2282082Seschrock 
2292082Seschrock 		/*
2302082Seschrock 		 * Note that we'll add the nparity tag even on storage pools
2312082Seschrock 		 * that only support a single parity device -- older software
2322082Seschrock 		 * will just ignore it.
2332082Seschrock 		 */
2342082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
2352082Seschrock 		    vd->vdev_nparity) == 0);
2362082Seschrock 	}
2372082Seschrock 
2381171Seschrock 	if (vd->vdev_wholedisk != -1ULL)
2391171Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2401171Seschrock 		    vd->vdev_wholedisk) == 0);
2411171Seschrock 
2421544Seschrock 	if (vd->vdev_not_present)
2431544Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
2441544Seschrock 
2452082Seschrock 	if (vd->vdev_isspare)
2462082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
2472082Seschrock 
2482082Seschrock 	if (!isspare && vd == vd->vdev_top) {
249789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
250789Sahrens 		    vd->vdev_ms_array) == 0);
251789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
252789Sahrens 		    vd->vdev_ms_shift) == 0);
253789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
254789Sahrens 		    vd->vdev_ashift) == 0);
255789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
256789Sahrens 		    vd->vdev_asize) == 0);
2574527Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2584527Sperrin 		    vd->vdev_islog) == 0);
259789Sahrens 	}
260789Sahrens 
261789Sahrens 	if (vd->vdev_dtl.smo_object != 0)
262789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
263789Sahrens 		    vd->vdev_dtl.smo_object) == 0);
264789Sahrens 
265789Sahrens 	if (getstats) {
266789Sahrens 		vdev_stat_t vs;
267789Sahrens 		vdev_get_stats(vd, &vs);
268789Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
269789Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
270789Sahrens 	}
271789Sahrens 
272789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
273789Sahrens 		nvlist_t **child;
274789Sahrens 		int c;
275789Sahrens 
276789Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
277789Sahrens 		    KM_SLEEP);
278789Sahrens 
279789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
2802082Seschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
2812082Seschrock 			    getstats, isspare);
282789Sahrens 
283789Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
284789Sahrens 		    child, vd->vdev_children) == 0);
285789Sahrens 
286789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
287789Sahrens 			nvlist_free(child[c]);
288789Sahrens 
289789Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
2901485Slling 
2911485Slling 	} else {
2921732Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
2931485Slling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
2941732Sbonwick 			    B_TRUE) == 0);
2954451Seschrock 		if (vd->vdev_faulted)
2964451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
2974451Seschrock 			    B_TRUE) == 0);
2984451Seschrock 		if (vd->vdev_degraded)
2994451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3004451Seschrock 			    B_TRUE) == 0);
3014451Seschrock 		if (vd->vdev_removed)
3024451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3034451Seschrock 			    B_TRUE) == 0);
3044451Seschrock 		if (vd->vdev_unspare)
3054451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3064451Seschrock 			    B_TRUE) == 0);
307789Sahrens 	}
308789Sahrens 
309789Sahrens 	return (nv);
310789Sahrens }
311789Sahrens 
312789Sahrens nvlist_t *
313789Sahrens vdev_label_read_config(vdev_t *vd)
314789Sahrens {
3151635Sbonwick 	spa_t *spa = vd->vdev_spa;
316789Sahrens 	nvlist_t *config = NULL;
317789Sahrens 	vdev_phys_t *vp;
318789Sahrens 	zio_t *zio;
319789Sahrens 	int l;
320789Sahrens 
3211635Sbonwick 	ASSERT(spa_config_held(spa, RW_READER));
3221635Sbonwick 
323789Sahrens 	if (vdev_is_dead(vd))
324789Sahrens 		return (NULL);
325789Sahrens 
326789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
327789Sahrens 
328789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
329789Sahrens 
3301635Sbonwick 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL |
3311544Seschrock 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
332789Sahrens 
333789Sahrens 		vdev_label_read(zio, vd, l, vp,
334789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
335789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
336789Sahrens 
337789Sahrens 		if (zio_wait(zio) == 0 &&
338789Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
3391544Seschrock 		    &config, 0) == 0)
340789Sahrens 			break;
341789Sahrens 
342789Sahrens 		if (config != NULL) {
343789Sahrens 			nvlist_free(config);
344789Sahrens 			config = NULL;
345789Sahrens 		}
346789Sahrens 	}
347789Sahrens 
348789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
349789Sahrens 
350789Sahrens 	return (config);
351789Sahrens }
352789Sahrens 
3533377Seschrock /*
3543377Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
3553377Seschrock  * in with the device guid if this spare is active elsewhere on the system.
3563377Seschrock  */
3573377Seschrock static boolean_t
3583377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
3593377Seschrock     uint64_t *spare_guid)
3603377Seschrock {
3613377Seschrock 	spa_t *spa = vd->vdev_spa;
3623377Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
3633377Seschrock 	uint64_t vdtxg = 0;
3643377Seschrock 	nvlist_t *label;
3653377Seschrock 
3663377Seschrock 	if (spare_guid)
3673377Seschrock 		*spare_guid = 0ULL;
3683377Seschrock 
3693377Seschrock 	/*
3703377Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
3713377Seschrock 	 */
3723377Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
3733377Seschrock 		return (B_FALSE);
3743377Seschrock 
3753377Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
3763377Seschrock 	    &vdtxg);
3773377Seschrock 
3783377Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
3793377Seschrock 	    &state) != 0 ||
3803377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
3813377Seschrock 	    &device_guid) != 0) {
3823377Seschrock 		nvlist_free(label);
3833377Seschrock 		return (B_FALSE);
3843377Seschrock 	}
3853377Seschrock 
3863377Seschrock 	if (state != POOL_STATE_SPARE &&
3873377Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
3883377Seschrock 	    &pool_guid) != 0 ||
3893377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3903377Seschrock 	    &txg) != 0)) {
3913377Seschrock 		nvlist_free(label);
3923377Seschrock 		return (B_FALSE);
3933377Seschrock 	}
3943377Seschrock 
3953377Seschrock 	nvlist_free(label);
3963377Seschrock 
3973377Seschrock 	/*
3983377Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
3993377Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
4003377Seschrock 	 * spare (which we check for later on).
4013377Seschrock 	 */
4023377Seschrock 	if (state != POOL_STATE_SPARE &&
4033377Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
4043377Seschrock 	    !spa_spare_exists(device_guid, NULL))
4053377Seschrock 		return (B_FALSE);
4063377Seschrock 
4073377Seschrock 	/*
4083377Seschrock 	 * If the transaction group is zero, then this an initialized (but
4093377Seschrock 	 * unused) label.  This is only an error if the create transaction
4103377Seschrock 	 * on-disk is the same as the one we're using now, in which case the
4113377Seschrock 	 * user has attempted to add the same vdev multiple times in the same
4123377Seschrock 	 * transaction.
4133377Seschrock 	 */
4143377Seschrock 	if (state != POOL_STATE_SPARE && txg == 0 && vdtxg == crtxg)
4153377Seschrock 		return (B_TRUE);
4163377Seschrock 
4173377Seschrock 	/*
4183377Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
4193377Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
4203377Seschrock 	 * to add.
4213377Seschrock 	 */
4223377Seschrock 	if (spa_spare_exists(device_guid, &spare_pool) ||
4233377Seschrock 	    spa_has_spare(spa, device_guid)) {
4243377Seschrock 		if (spare_guid)
4253377Seschrock 			*spare_guid = device_guid;
4263377Seschrock 
4273377Seschrock 		switch (reason) {
4283377Seschrock 		case VDEV_LABEL_CREATE:
4293377Seschrock 			return (B_TRUE);
4303377Seschrock 
4313377Seschrock 		case VDEV_LABEL_REPLACE:
4323377Seschrock 			return (!spa_has_spare(spa, device_guid) ||
4333377Seschrock 			    spare_pool != 0ULL);
4343377Seschrock 
4353377Seschrock 		case VDEV_LABEL_SPARE:
4363377Seschrock 			return (spa_has_spare(spa, device_guid));
4373377Seschrock 		}
4383377Seschrock 	}
4393377Seschrock 
4403377Seschrock 	/*
4413377Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
4423377Seschrock 	 * pool on the system.
4433377Seschrock 	 */
4443377Seschrock 	return (state == POOL_STATE_ACTIVE);
4453377Seschrock }
4463377Seschrock 
4473377Seschrock /*
4483377Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
4493377Seschrock  * use, and writable.  We put down an initial label which we will later
4503377Seschrock  * overwrite with a complete label.  Note that it's important to do this
4513377Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
4523377Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
4533377Seschrock  * itself.
4543377Seschrock  */
4553377Seschrock int
4563377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
457789Sahrens {
458789Sahrens 	spa_t *spa = vd->vdev_spa;
459789Sahrens 	nvlist_t *label;
460789Sahrens 	vdev_phys_t *vp;
461789Sahrens 	vdev_boot_header_t *vb;
4621732Sbonwick 	uberblock_t *ub;
463789Sahrens 	zio_t *zio;
464789Sahrens 	int l, c, n;
465789Sahrens 	char *buf;
466789Sahrens 	size_t buflen;
467789Sahrens 	int error;
4683377Seschrock 	uint64_t spare_guid;
469789Sahrens 
4701635Sbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
4711635Sbonwick 
472789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
4733377Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
4743377Seschrock 		    crtxg, reason)) != 0)
475789Sahrens 			return (error);
476789Sahrens 
477789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
478789Sahrens 		return (0);
479789Sahrens 
480789Sahrens 	/*
4813377Seschrock 	 * Dead vdevs cannot be initialized.
482789Sahrens 	 */
483789Sahrens 	if (vdev_is_dead(vd))
484789Sahrens 		return (EIO);
485789Sahrens 
486789Sahrens 	/*
4873377Seschrock 	 * Determine if the vdev is in use.
488789Sahrens 	 */
4893377Seschrock 	if (reason != VDEV_LABEL_REMOVE &&
4903377Seschrock 	    vdev_inuse(vd, crtxg, reason, &spare_guid))
4913377Seschrock 		return (EBUSY);
492789Sahrens 
4933377Seschrock 	ASSERT(reason != VDEV_LABEL_REMOVE ||
4943377Seschrock 	    vdev_inuse(vd, crtxg, reason, NULL));
495789Sahrens 
4963377Seschrock 	/*
4973377Seschrock 	 * If this is a request to add or replace a spare that is in use
4983377Seschrock 	 * elsewhere on the system, then we must update the guid (which was
4993377Seschrock 	 * initialized to a random value) to reflect the actual GUID (which is
5003377Seschrock 	 * shared between multiple pools).
5013377Seschrock 	 */
5023377Seschrock 	if (reason != VDEV_LABEL_REMOVE && spare_guid != 0ULL) {
5033377Seschrock 		vdev_t *pvd = vd->vdev_parent;
5042082Seschrock 
5053377Seschrock 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
5063377Seschrock 			pvd->vdev_guid_sum -= vd->vdev_guid;
5073377Seschrock 			pvd->vdev_guid_sum += spare_guid;
508789Sahrens 		}
5092082Seschrock 
5103377Seschrock 		vd->vdev_guid = vd->vdev_guid_sum = spare_guid;
5112082Seschrock 
5123377Seschrock 		/*
5133377Seschrock 		 * If this is a replacement, then we want to fallthrough to the
5143377Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
5154451Seschrock 		 * labeled appropriately and we can just return.
5163377Seschrock 		 */
5173377Seschrock 		if (reason == VDEV_LABEL_SPARE)
5183377Seschrock 			return (0);
5193377Seschrock 		ASSERT(reason == VDEV_LABEL_REPLACE);
520789Sahrens 	}
521789Sahrens 
522789Sahrens 	/*
5233377Seschrock 	 * Initialize its label.
524789Sahrens 	 */
525789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
526789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
527789Sahrens 
528789Sahrens 	/*
529789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
530789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
531789Sahrens 	 * really part of an active pool just yet.  The labels will
532789Sahrens 	 * be written again with a meaningful txg by spa_sync().
533789Sahrens 	 */
5343377Seschrock 	if (reason == VDEV_LABEL_SPARE ||
5353377Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
5363377Seschrock 		/*
5373377Seschrock 		 * For inactive hot spares, we generate a special label that
5383377Seschrock 		 * identifies as a mutually shared hot spare.  We write the
5393377Seschrock 		 * label if we are adding a hot spare, or if we are removing an
5403377Seschrock 		 * active hot spare (in which case we want to revert the
5413377Seschrock 		 * labels).
5423377Seschrock 		 */
5432082Seschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
544789Sahrens 
5452082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
5462082Seschrock 		    spa_version(spa)) == 0);
5472082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
5482082Seschrock 		    POOL_STATE_SPARE) == 0);
5492082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
5502082Seschrock 		    vd->vdev_guid) == 0);
5512082Seschrock 	} else {
5522082Seschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
5532082Seschrock 
5542082Seschrock 		/*
5552082Seschrock 		 * Add our creation time.  This allows us to detect multiple
5562082Seschrock 		 * vdev uses as described above, and automatically expires if we
5572082Seschrock 		 * fail.
5582082Seschrock 		 */
5592082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
5602082Seschrock 		    crtxg) == 0);
5612082Seschrock 	}
562789Sahrens 
563789Sahrens 	buf = vp->vp_nvlist;
564789Sahrens 	buflen = sizeof (vp->vp_nvlist);
565789Sahrens 
5663460Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
5673460Smmusante 	if (error != 0) {
568789Sahrens 		nvlist_free(label);
569789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
5703460Smmusante 		/* EFAULT means nvlist_pack ran out of room */
5713460Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
572789Sahrens 	}
573789Sahrens 
574789Sahrens 	/*
575789Sahrens 	 * Initialize boot block header.
576789Sahrens 	 */
577789Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
578789Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
579789Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
580789Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
581789Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
582789Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
583789Sahrens 
584789Sahrens 	/*
585789Sahrens 	 * Initialize uberblock template.
586789Sahrens 	 */
5871732Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
5881732Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
5891732Sbonwick 	*ub = spa->spa_uberblock;
5901732Sbonwick 	ub->ub_txg = 0;
591789Sahrens 
592789Sahrens 	/*
593789Sahrens 	 * Write everything in parallel.
594789Sahrens 	 */
595789Sahrens 	zio = zio_root(spa, NULL, NULL,
596789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
597789Sahrens 
598789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
599789Sahrens 
600789Sahrens 		vdev_label_write(zio, vd, l, vp,
601789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
602789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
603789Sahrens 
604789Sahrens 		vdev_label_write(zio, vd, l, vb,
605789Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
606789Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
607789Sahrens 
6081732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
6091732Sbonwick 			vdev_label_write(zio, vd, l, ub,
6101732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
6111732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL);
612789Sahrens 		}
613789Sahrens 	}
614789Sahrens 
615789Sahrens 	error = zio_wait(zio);
616789Sahrens 
617789Sahrens 	nvlist_free(label);
6181732Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
619789Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
620789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
621789Sahrens 
6223377Seschrock 	/*
6233377Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
6244451Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
6253377Seschrock 	 * exists as a spare elsewhere in the system.
6263377Seschrock 	 */
6273377Seschrock 	if (error == 0 && !vd->vdev_isspare &&
6283377Seschrock 	    (reason == VDEV_LABEL_SPARE ||
6293377Seschrock 	    spa_spare_exists(vd->vdev_guid, NULL)))
6303377Seschrock 		spa_spare_add(vd);
6312082Seschrock 
6323377Seschrock 	return (error);
6332082Seschrock }
6342082Seschrock 
635789Sahrens /*
636789Sahrens  * ==========================================================================
637789Sahrens  * uberblock load/sync
638789Sahrens  * ==========================================================================
639789Sahrens  */
640789Sahrens 
641789Sahrens /*
642789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
643789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
644789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
645789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
646789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
647789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
648789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
649789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
650789Sahrens  */
651789Sahrens static int
652789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
653789Sahrens {
654789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
655789Sahrens 		return (-1);
656789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
657789Sahrens 		return (1);
658789Sahrens 
659789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
660789Sahrens 		return (-1);
661789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
662789Sahrens 		return (1);
663789Sahrens 
664789Sahrens 	return (0);
665789Sahrens }
666789Sahrens 
667789Sahrens static void
668789Sahrens vdev_uberblock_load_done(zio_t *zio)
669789Sahrens {
6701732Sbonwick 	uberblock_t *ub = zio->io_data;
671789Sahrens 	uberblock_t *ubbest = zio->io_private;
672789Sahrens 	spa_t *spa = zio->io_spa;
673789Sahrens 
6741732Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
675789Sahrens 
6761544Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
677789Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
678789Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
679789Sahrens 			*ubbest = *ub;
680789Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
681789Sahrens 	}
682789Sahrens 
683789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
684789Sahrens }
685789Sahrens 
686789Sahrens void
687789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
688789Sahrens {
689789Sahrens 	int l, c, n;
690789Sahrens 
691789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
692789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
693789Sahrens 
694789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
695789Sahrens 		return;
696789Sahrens 
697789Sahrens 	if (vdev_is_dead(vd))
698789Sahrens 		return;
699789Sahrens 
700789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
7011732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
702789Sahrens 			vdev_label_read(zio, vd, l,
7031732Sbonwick 			    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
7041732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
7051732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd),
706789Sahrens 			    vdev_uberblock_load_done, ubbest);
707789Sahrens 		}
708789Sahrens 	}
709789Sahrens }
710789Sahrens 
711789Sahrens /*
712789Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
7131635Sbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
714789Sahrens  */
715789Sahrens static void
716789Sahrens vdev_uberblock_sync_done(zio_t *zio)
717789Sahrens {
718789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
719789Sahrens 
7201635Sbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
721789Sahrens 		atomic_add_64(good_writes, 1);
722789Sahrens }
723789Sahrens 
724789Sahrens static void
7251732Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, uint64_t txg)
726789Sahrens {
727789Sahrens 	int l, c, n;
728789Sahrens 
729789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
7301732Sbonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], txg);
731789Sahrens 
732789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
733789Sahrens 		return;
734789Sahrens 
735789Sahrens 	if (vdev_is_dead(vd))
736789Sahrens 		return;
737789Sahrens 
7381732Sbonwick 	n = txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
739789Sahrens 
7401732Sbonwick 	ASSERT(ub->ub_txg == txg);
741789Sahrens 
742789Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
7431732Sbonwick 		vdev_label_write(zio, vd, l, ub,
7441732Sbonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n),
7451732Sbonwick 		    VDEV_UBERBLOCK_SIZE(vd),
7461732Sbonwick 		    vdev_uberblock_sync_done, NULL);
747789Sahrens 
748789Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
749789Sahrens }
750789Sahrens 
751789Sahrens static int
7521732Sbonwick vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *vd, uint64_t txg)
753789Sahrens {
7541732Sbonwick 	uberblock_t *ubbuf;
7551732Sbonwick 	size_t size = vd->vdev_top ? VDEV_UBERBLOCK_SIZE(vd) : SPA_MAXBLOCKSIZE;
756789Sahrens 	uint64_t *good_writes;
757789Sahrens 	zio_t *zio;
758789Sahrens 	int error;
759789Sahrens 
7601732Sbonwick 	ubbuf = zio_buf_alloc(size);
7611732Sbonwick 	bzero(ubbuf, size);
7621732Sbonwick 	*ubbuf = *ub;
763789Sahrens 
764789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
765789Sahrens 
766789Sahrens 	zio = zio_root(spa, NULL, good_writes,
767789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
768789Sahrens 
7691732Sbonwick 	vdev_uberblock_sync(zio, ubbuf, vd, txg);
770789Sahrens 
771789Sahrens 	error = zio_wait(zio);
772789Sahrens 
773789Sahrens 	if (error && *good_writes != 0) {
774789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
775789Sahrens 		error = 0;
776789Sahrens 	}
777789Sahrens 
778789Sahrens 	/*
779789Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
780789Sahrens 	 * the CANT_OPEN state.
781789Sahrens 	 */
782789Sahrens 	if (*good_writes == 0 && error == 0)
783789Sahrens 		error = EIO;
784789Sahrens 
785789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
7861732Sbonwick 	zio_buf_free(ubbuf, size);
787789Sahrens 
788789Sahrens 	return (error);
789789Sahrens }
790789Sahrens 
791789Sahrens /*
792789Sahrens  * Sync out an individual vdev.
793789Sahrens  */
794789Sahrens static void
795789Sahrens vdev_sync_label_done(zio_t *zio)
796789Sahrens {
797789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
798789Sahrens 
799789Sahrens 	if (zio->io_error == 0)
800789Sahrens 		atomic_add_64(good_writes, 1);
801789Sahrens }
802789Sahrens 
803789Sahrens static void
804789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
805789Sahrens {
806789Sahrens 	nvlist_t *label;
807789Sahrens 	vdev_phys_t *vp;
808789Sahrens 	char *buf;
809789Sahrens 	size_t buflen;
810789Sahrens 	int c;
811789Sahrens 
812789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
813789Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
814789Sahrens 
815789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
816789Sahrens 		return;
817789Sahrens 
818789Sahrens 	if (vdev_is_dead(vd))
819789Sahrens 		return;
820789Sahrens 
821789Sahrens 	/*
822789Sahrens 	 * Generate a label describing the top-level config to which we belong.
823789Sahrens 	 */
8241635Sbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
825789Sahrens 
826789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
827789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
828789Sahrens 
829789Sahrens 	buf = vp->vp_nvlist;
830789Sahrens 	buflen = sizeof (vp->vp_nvlist);
831789Sahrens 
8321544Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
833789Sahrens 		vdev_label_write(zio, vd, l, vp,
834789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
835789Sahrens 		    vdev_sync_label_done, NULL);
836789Sahrens 
837789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
838789Sahrens 	nvlist_free(label);
839789Sahrens 
840789Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
841789Sahrens }
842789Sahrens 
843789Sahrens static int
844789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
845789Sahrens {
846789Sahrens 	uint64_t *good_writes;
847789Sahrens 	zio_t *zio;
848789Sahrens 	int error;
849789Sahrens 
850789Sahrens 	ASSERT(vd == vd->vdev_top);
851789Sahrens 
852789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
853789Sahrens 
854789Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
855789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
856789Sahrens 
857789Sahrens 	/*
858789Sahrens 	 * Recursively kick off writes to all labels.
859789Sahrens 	 */
860789Sahrens 	vdev_sync_label(zio, vd, l, txg);
861789Sahrens 
862789Sahrens 	error = zio_wait(zio);
863789Sahrens 
864789Sahrens 	if (error && *good_writes != 0) {
865789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
866789Sahrens 		error = 0;
867789Sahrens 	}
868789Sahrens 
869789Sahrens 	if (*good_writes == 0 && error == 0)
870789Sahrens 		error = ENODEV;
871789Sahrens 
8724527Sperrin 	/*
8734527Sperrin 	 * Failure to write a label can be fatal for a
8744527Sperrin 	 * top level vdev. We don't want this for slogs
8754527Sperrin 	 * as we use the main pool if they go away.
8764527Sperrin 	 */
8774527Sperrin 	if (vd->vdev_islog)
8784527Sperrin 		error = 0;
8794527Sperrin 
880789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
881789Sahrens 
882789Sahrens 	return (error);
883789Sahrens }
884789Sahrens 
885789Sahrens /*
886789Sahrens  * Sync the entire vdev configuration.
887789Sahrens  *
888789Sahrens  * The order of operations is carefully crafted to ensure that
889789Sahrens  * if the system panics or loses power at any time, the state on disk
890789Sahrens  * is still transactionally consistent.  The in-line comments below
891789Sahrens  * describe the failure semantics at each stage.
892789Sahrens  *
893789Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
894789Sahrens  * at any time, you can just call it again, and it will resume its work.
895789Sahrens  */
896789Sahrens int
8971635Sbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg)
898789Sahrens {
8991635Sbonwick 	spa_t *spa = uvd->vdev_spa;
900789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
901789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
9021635Sbonwick 	vdev_t *vd;
903789Sahrens 	zio_t *zio;
9041637Sbonwick 	int l, error;
905789Sahrens 
906789Sahrens 	ASSERT(ub->ub_txg <= txg);
907789Sahrens 
908789Sahrens 	/*
909789Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
910789Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
9111635Sbonwick 	 * then there's nothing to do.
912789Sahrens 	 */
913789Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
914789Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
915789Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
916789Sahrens 		    spa_name(spa), txg);
917789Sahrens 		return (0);
918789Sahrens 	}
919789Sahrens 
920789Sahrens 	if (txg > spa_freeze_txg(spa))
921789Sahrens 		return (0);
922789Sahrens 
9231635Sbonwick 	ASSERT(txg <= spa->spa_final_txg);
9241635Sbonwick 
925789Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
926789Sahrens 
927789Sahrens 	/*
928789Sahrens 	 * Flush the write cache of every disk that's been written to
929789Sahrens 	 * in this transaction group.  This ensures that all blocks
930789Sahrens 	 * written in this txg will be committed to stable storage
931789Sahrens 	 * before any uberblock that references them.
932789Sahrens 	 */
933789Sahrens 	zio = zio_root(spa, NULL, NULL,
934789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
935789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
936789Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
937789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
938789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
939789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
940789Sahrens 	}
941789Sahrens 	(void) zio_wait(zio);
942789Sahrens 
943789Sahrens 	/*
944789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
945789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
946789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
947789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
948789Sahrens 	 * which have not yet been touched, will still be valid.
949789Sahrens 	 */
950789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
951789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
952789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
953789Sahrens 			if (l & 1)
954789Sahrens 				continue;
955789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
956789Sahrens 				return (error);
957789Sahrens 		}
958789Sahrens 	}
959789Sahrens 
960789Sahrens 	/*
961789Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
962789Sahrens 	 * updates are committed to stable storage before the uberblock update.
963789Sahrens 	 */
964789Sahrens 	zio = zio_root(spa, NULL, NULL,
965789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
966789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
967789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
968789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
969789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
970789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
971789Sahrens 	}
972789Sahrens 	(void) zio_wait(zio);
973789Sahrens 
974789Sahrens 	/*
9751635Sbonwick 	 * Sync the uberblocks to all vdevs in the tree specified by uvd.
9761635Sbonwick 	 * If the system dies in the middle of this step, there are two cases
9771635Sbonwick 	 * to consider, and the on-disk state is consistent either way:
978789Sahrens 	 *
979789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
980789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
981789Sahrens 	 *	(which had not yet been touched) will be valid with respect
982789Sahrens 	 *	to that uberblock.
983789Sahrens 	 *
984789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
985789Sahrens 	 *	will be the newest, and the even labels (which had all
986789Sahrens 	 *	been successfully committed) will be valid with respect
987789Sahrens 	 *	to the new uberblocks.
988789Sahrens 	 */
989789Sahrens 	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
990789Sahrens 		return (error);
991789Sahrens 
992789Sahrens 	/*
993789Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
994789Sahrens 	 * are no longer needed (because the new uberblocks and the even
995789Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
996789Sahrens 	 */
997789Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
998789Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
999789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1000789Sahrens 
1001789Sahrens 	/*
1002789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1003789Sahrens 	 * in the middle of this process, the even labels and the new
1004789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1005789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1006789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
1007789Sahrens 	 * all labels will be brought up to date.
1008789Sahrens 	 */
1009789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1010789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1011789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
1012789Sahrens 			if ((l & 1) == 0)
1013789Sahrens 				continue;
1014789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
1015789Sahrens 				return (error);
1016789Sahrens 		}
1017789Sahrens 	}
1018789Sahrens 
1019789Sahrens 	/*
1020789Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
1021789Sahrens 	 * updates are committed to stable storage before the next
1022789Sahrens 	 * transaction group begins.
1023789Sahrens 	 */
1024789Sahrens 	zio = zio_root(spa, NULL, NULL,
1025789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
1026789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1027789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1028789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
1029789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
1030789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1031789Sahrens 	}
1032789Sahrens 	(void) zio_wait(zio);
1033789Sahrens 
1034789Sahrens 	return (0);
1035789Sahrens }
1036