xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 5450:b25030891c44)
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));
1564577Sahrens 	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,
172*5450Sbrendan 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
173*5450Sbrendan 	    B_TRUE));
174789Sahrens }
175789Sahrens 
176789Sahrens static void
177789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
178789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
179789Sahrens {
180789Sahrens 	ASSERT(vd->vdev_children == 0);
181789Sahrens 
182789Sahrens 	zio_nowait(zio_write_phys(zio, vd,
183789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
184789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
185*5450Sbrendan 	    ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL,
186*5450Sbrendan 	    B_TRUE));
187789Sahrens }
188789Sahrens 
189789Sahrens /*
190789Sahrens  * Generate the nvlist representing this vdev's config.
191789Sahrens  */
192789Sahrens nvlist_t *
1932082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
194*5450Sbrendan     boolean_t isspare, boolean_t isl2cache)
195789Sahrens {
196789Sahrens 	nvlist_t *nv = NULL;
197789Sahrens 
1981544Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
199789Sahrens 
200789Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
201789Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
202*5450Sbrendan 	if (!isspare && !isl2cache)
2032082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
2042082Seschrock 		    == 0);
205789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
206789Sahrens 
207789Sahrens 	if (vd->vdev_path != NULL)
208789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
209789Sahrens 		    vd->vdev_path) == 0);
210789Sahrens 
211789Sahrens 	if (vd->vdev_devid != NULL)
212789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
213789Sahrens 		    vd->vdev_devid) == 0);
214789Sahrens 
2154451Seschrock 	if (vd->vdev_physpath != NULL)
2164451Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2174451Seschrock 		    vd->vdev_physpath) == 0);
2184451Seschrock 
2192082Seschrock 	if (vd->vdev_nparity != 0) {
2202082Seschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
2212082Seschrock 		    VDEV_TYPE_RAIDZ) == 0);
2222082Seschrock 
2232082Seschrock 		/*
2242082Seschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
2252082Seschrock 		 * into a crufty old storage pool.
2262082Seschrock 		 */
2272082Seschrock 		ASSERT(vd->vdev_nparity == 1 ||
2282082Seschrock 		    (vd->vdev_nparity == 2 &&
2294577Sahrens 		    spa_version(spa) >= SPA_VERSION_RAID6));
2302082Seschrock 
2312082Seschrock 		/*
2322082Seschrock 		 * Note that we'll add the nparity tag even on storage pools
2332082Seschrock 		 * that only support a single parity device -- older software
2342082Seschrock 		 * will just ignore it.
2352082Seschrock 		 */
2362082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
2372082Seschrock 		    vd->vdev_nparity) == 0);
2382082Seschrock 	}
2392082Seschrock 
2401171Seschrock 	if (vd->vdev_wholedisk != -1ULL)
2411171Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2421171Seschrock 		    vd->vdev_wholedisk) == 0);
2431171Seschrock 
2441544Seschrock 	if (vd->vdev_not_present)
2451544Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
2461544Seschrock 
2472082Seschrock 	if (vd->vdev_isspare)
2482082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
2492082Seschrock 
250*5450Sbrendan 	if (!isspare && !isl2cache && vd == vd->vdev_top) {
251789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
252789Sahrens 		    vd->vdev_ms_array) == 0);
253789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
254789Sahrens 		    vd->vdev_ms_shift) == 0);
255789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
256789Sahrens 		    vd->vdev_ashift) == 0);
257789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
258789Sahrens 		    vd->vdev_asize) == 0);
2594527Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2604527Sperrin 		    vd->vdev_islog) == 0);
261789Sahrens 	}
262789Sahrens 
263789Sahrens 	if (vd->vdev_dtl.smo_object != 0)
264789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
265789Sahrens 		    vd->vdev_dtl.smo_object) == 0);
266789Sahrens 
267789Sahrens 	if (getstats) {
268789Sahrens 		vdev_stat_t vs;
269789Sahrens 		vdev_get_stats(vd, &vs);
270789Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
271789Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
272789Sahrens 	}
273789Sahrens 
274789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
275789Sahrens 		nvlist_t **child;
276789Sahrens 		int c;
277789Sahrens 
278789Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
279789Sahrens 		    KM_SLEEP);
280789Sahrens 
281789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
2822082Seschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
283*5450Sbrendan 			    getstats, isspare, isl2cache);
284789Sahrens 
285789Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
286789Sahrens 		    child, vd->vdev_children) == 0);
287789Sahrens 
288789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
289789Sahrens 			nvlist_free(child[c]);
290789Sahrens 
291789Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
2921485Slling 
2931485Slling 	} else {
2941732Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
2951485Slling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
2961732Sbonwick 			    B_TRUE) == 0);
2974451Seschrock 		if (vd->vdev_faulted)
2984451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
2994451Seschrock 			    B_TRUE) == 0);
3004451Seschrock 		if (vd->vdev_degraded)
3014451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3024451Seschrock 			    B_TRUE) == 0);
3034451Seschrock 		if (vd->vdev_removed)
3044451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3054451Seschrock 			    B_TRUE) == 0);
3064451Seschrock 		if (vd->vdev_unspare)
3074451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3084451Seschrock 			    B_TRUE) == 0);
309789Sahrens 	}
310789Sahrens 
311789Sahrens 	return (nv);
312789Sahrens }
313789Sahrens 
314789Sahrens nvlist_t *
315789Sahrens vdev_label_read_config(vdev_t *vd)
316789Sahrens {
3171635Sbonwick 	spa_t *spa = vd->vdev_spa;
318789Sahrens 	nvlist_t *config = NULL;
319789Sahrens 	vdev_phys_t *vp;
320789Sahrens 	zio_t *zio;
321789Sahrens 	int l;
322789Sahrens 
3234787Sahrens 	ASSERT(spa_config_held(spa, RW_READER) ||
3244787Sahrens 	    spa_config_held(spa, RW_WRITER));
3251635Sbonwick 
3265329Sgw25295 	if (!vdev_readable(vd))
327789Sahrens 		return (NULL);
328789Sahrens 
329789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
330789Sahrens 
331789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
332789Sahrens 
3331635Sbonwick 		zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL |
3341544Seschrock 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD);
335789Sahrens 
336789Sahrens 		vdev_label_read(zio, vd, l, vp,
337789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
338789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
339789Sahrens 
340789Sahrens 		if (zio_wait(zio) == 0 &&
341789Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
3421544Seschrock 		    &config, 0) == 0)
343789Sahrens 			break;
344789Sahrens 
345789Sahrens 		if (config != NULL) {
346789Sahrens 			nvlist_free(config);
347789Sahrens 			config = NULL;
348789Sahrens 		}
349789Sahrens 	}
350789Sahrens 
351789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
352789Sahrens 
353789Sahrens 	return (config);
354789Sahrens }
355789Sahrens 
3563377Seschrock /*
3573377Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
3583377Seschrock  * in with the device guid if this spare is active elsewhere on the system.
3593377Seschrock  */
3603377Seschrock static boolean_t
3613377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
362*5450Sbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
3633377Seschrock {
3643377Seschrock 	spa_t *spa = vd->vdev_spa;
3653377Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
3663377Seschrock 	uint64_t vdtxg = 0;
3673377Seschrock 	nvlist_t *label;
3683377Seschrock 
3693377Seschrock 	if (spare_guid)
3703377Seschrock 		*spare_guid = 0ULL;
371*5450Sbrendan 	if (l2cache_guid)
372*5450Sbrendan 		*l2cache_guid = 0ULL;
3733377Seschrock 
3743377Seschrock 	/*
3753377Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
3763377Seschrock 	 */
3773377Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
3783377Seschrock 		return (B_FALSE);
3793377Seschrock 
3803377Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
3813377Seschrock 	    &vdtxg);
3823377Seschrock 
3833377Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
3843377Seschrock 	    &state) != 0 ||
3853377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
3863377Seschrock 	    &device_guid) != 0) {
3873377Seschrock 		nvlist_free(label);
3883377Seschrock 		return (B_FALSE);
3893377Seschrock 	}
3903377Seschrock 
391*5450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
3923377Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
3933377Seschrock 	    &pool_guid) != 0 ||
3943377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3953377Seschrock 	    &txg) != 0)) {
3963377Seschrock 		nvlist_free(label);
3973377Seschrock 		return (B_FALSE);
3983377Seschrock 	}
3993377Seschrock 
4003377Seschrock 	nvlist_free(label);
4013377Seschrock 
4023377Seschrock 	/*
4033377Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
4043377Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
4053377Seschrock 	 * spare (which we check for later on).
4063377Seschrock 	 */
407*5450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
4083377Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
409*5450Sbrendan 	    !spa_spare_exists(device_guid, NULL) &&
410*5450Sbrendan 	    !spa_l2cache_exists(device_guid, NULL))
4113377Seschrock 		return (B_FALSE);
4123377Seschrock 
4133377Seschrock 	/*
4143377Seschrock 	 * If the transaction group is zero, then this an initialized (but
4153377Seschrock 	 * unused) label.  This is only an error if the create transaction
4163377Seschrock 	 * on-disk is the same as the one we're using now, in which case the
4173377Seschrock 	 * user has attempted to add the same vdev multiple times in the same
4183377Seschrock 	 * transaction.
4193377Seschrock 	 */
420*5450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
421*5450Sbrendan 	    txg == 0 && vdtxg == crtxg)
4223377Seschrock 		return (B_TRUE);
4233377Seschrock 
4243377Seschrock 	/*
4253377Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
4263377Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
427*5450Sbrendan 	 * to add.  We also check if it is an l2cache device.
4283377Seschrock 	 */
4293377Seschrock 	if (spa_spare_exists(device_guid, &spare_pool) ||
4303377Seschrock 	    spa_has_spare(spa, device_guid)) {
4313377Seschrock 		if (spare_guid)
4323377Seschrock 			*spare_guid = device_guid;
4333377Seschrock 
4343377Seschrock 		switch (reason) {
4353377Seschrock 		case VDEV_LABEL_CREATE:
436*5450Sbrendan 		case VDEV_LABEL_L2CACHE:
4373377Seschrock 			return (B_TRUE);
4383377Seschrock 
4393377Seschrock 		case VDEV_LABEL_REPLACE:
4403377Seschrock 			return (!spa_has_spare(spa, device_guid) ||
4413377Seschrock 			    spare_pool != 0ULL);
4423377Seschrock 
4433377Seschrock 		case VDEV_LABEL_SPARE:
4443377Seschrock 			return (spa_has_spare(spa, device_guid));
4453377Seschrock 		}
4463377Seschrock 	}
4473377Seschrock 
4483377Seschrock 	/*
449*5450Sbrendan 	 * Check to see if this is an l2cache device.
450*5450Sbrendan 	 */
451*5450Sbrendan 	if (spa_l2cache_exists(device_guid, NULL))
452*5450Sbrendan 		return (B_TRUE);
453*5450Sbrendan 
454*5450Sbrendan 	/*
4553377Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
4563377Seschrock 	 * pool on the system.
4573377Seschrock 	 */
4583377Seschrock 	return (state == POOL_STATE_ACTIVE);
4593377Seschrock }
4603377Seschrock 
4613377Seschrock /*
4623377Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
4633377Seschrock  * use, and writable.  We put down an initial label which we will later
4643377Seschrock  * overwrite with a complete label.  Note that it's important to do this
4653377Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
4663377Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
4673377Seschrock  * itself.
4683377Seschrock  */
4693377Seschrock int
4703377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
471789Sahrens {
472789Sahrens 	spa_t *spa = vd->vdev_spa;
473789Sahrens 	nvlist_t *label;
474789Sahrens 	vdev_phys_t *vp;
475789Sahrens 	vdev_boot_header_t *vb;
4761732Sbonwick 	uberblock_t *ub;
477789Sahrens 	zio_t *zio;
478789Sahrens 	int l, c, n;
479789Sahrens 	char *buf;
480789Sahrens 	size_t buflen;
481789Sahrens 	int error;
482*5450Sbrendan 	uint64_t spare_guid, l2cache_guid;
483789Sahrens 
4841635Sbonwick 	ASSERT(spa_config_held(spa, RW_WRITER));
4851635Sbonwick 
486789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
4873377Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
4883377Seschrock 		    crtxg, reason)) != 0)
489789Sahrens 			return (error);
490789Sahrens 
491789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
492789Sahrens 		return (0);
493789Sahrens 
494789Sahrens 	/*
4953377Seschrock 	 * Dead vdevs cannot be initialized.
496789Sahrens 	 */
497789Sahrens 	if (vdev_is_dead(vd))
498789Sahrens 		return (EIO);
499789Sahrens 
500789Sahrens 	/*
5013377Seschrock 	 * Determine if the vdev is in use.
502789Sahrens 	 */
5033377Seschrock 	if (reason != VDEV_LABEL_REMOVE &&
504*5450Sbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
5053377Seschrock 		return (EBUSY);
506789Sahrens 
5073377Seschrock 	ASSERT(reason != VDEV_LABEL_REMOVE ||
508*5450Sbrendan 	    vdev_inuse(vd, crtxg, reason, NULL, NULL));
509789Sahrens 
5103377Seschrock 	/*
511*5450Sbrendan 	 * If this is a request to add or replace a spare or l2cache device
512*5450Sbrendan 	 * that is in use elsewhere on the system, then we must update the
513*5450Sbrendan 	 * guid (which was initialized to a random value) to reflect the
514*5450Sbrendan 	 * actual GUID (which is shared between multiple pools).
5153377Seschrock 	 */
516*5450Sbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
517*5450Sbrendan 	    spare_guid != 0ULL) {
5183377Seschrock 		vdev_t *pvd = vd->vdev_parent;
5192082Seschrock 
5203377Seschrock 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
5213377Seschrock 			pvd->vdev_guid_sum -= vd->vdev_guid;
5223377Seschrock 			pvd->vdev_guid_sum += spare_guid;
523789Sahrens 		}
5242082Seschrock 
5253377Seschrock 		vd->vdev_guid = vd->vdev_guid_sum = spare_guid;
5262082Seschrock 
5273377Seschrock 		/*
5283377Seschrock 		 * If this is a replacement, then we want to fallthrough to the
5293377Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
5304451Seschrock 		 * labeled appropriately and we can just return.
5313377Seschrock 		 */
5323377Seschrock 		if (reason == VDEV_LABEL_SPARE)
5333377Seschrock 			return (0);
5343377Seschrock 		ASSERT(reason == VDEV_LABEL_REPLACE);
535789Sahrens 	}
536789Sahrens 
537*5450Sbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
538*5450Sbrendan 	    l2cache_guid != 0ULL) {
539*5450Sbrendan 		vdev_t *pvd = vd->vdev_parent;
540*5450Sbrendan 
541*5450Sbrendan 		for (; pvd != NULL; pvd = pvd->vdev_parent) {
542*5450Sbrendan 			pvd->vdev_guid_sum -= vd->vdev_guid;
543*5450Sbrendan 			pvd->vdev_guid_sum += l2cache_guid;
544*5450Sbrendan 		}
545*5450Sbrendan 
546*5450Sbrendan 		vd->vdev_guid = vd->vdev_guid_sum = l2cache_guid;
547*5450Sbrendan 
548*5450Sbrendan 		/*
549*5450Sbrendan 		 * If this is a replacement, then we want to fallthrough to the
550*5450Sbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
551*5450Sbrendan 		 * already labeled appropriately and we can just return.
552*5450Sbrendan 		 */
553*5450Sbrendan 		if (reason == VDEV_LABEL_L2CACHE)
554*5450Sbrendan 			return (0);
555*5450Sbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
556*5450Sbrendan 	}
557*5450Sbrendan 
558789Sahrens 	/*
5593377Seschrock 	 * Initialize its label.
560789Sahrens 	 */
561789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
562789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
563789Sahrens 
564789Sahrens 	/*
565789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
566789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
567789Sahrens 	 * really part of an active pool just yet.  The labels will
568789Sahrens 	 * be written again with a meaningful txg by spa_sync().
569789Sahrens 	 */
5703377Seschrock 	if (reason == VDEV_LABEL_SPARE ||
5713377Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
5723377Seschrock 		/*
5733377Seschrock 		 * For inactive hot spares, we generate a special label that
5743377Seschrock 		 * identifies as a mutually shared hot spare.  We write the
5753377Seschrock 		 * label if we are adding a hot spare, or if we are removing an
5763377Seschrock 		 * active hot spare (in which case we want to revert the
5773377Seschrock 		 * labels).
5783377Seschrock 		 */
5792082Seschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
580789Sahrens 
5812082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
5822082Seschrock 		    spa_version(spa)) == 0);
5832082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
5842082Seschrock 		    POOL_STATE_SPARE) == 0);
5852082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
5862082Seschrock 		    vd->vdev_guid) == 0);
587*5450Sbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
588*5450Sbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
589*5450Sbrendan 		/*
590*5450Sbrendan 		 * For level 2 ARC devices, add a special label.
591*5450Sbrendan 		 */
592*5450Sbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
593*5450Sbrendan 
594*5450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
595*5450Sbrendan 		    spa_version(spa)) == 0);
596*5450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
597*5450Sbrendan 		    POOL_STATE_L2CACHE) == 0);
598*5450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
599*5450Sbrendan 		    vd->vdev_guid) == 0);
6002082Seschrock 	} else {
6012082Seschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
6022082Seschrock 
6032082Seschrock 		/*
6042082Seschrock 		 * Add our creation time.  This allows us to detect multiple
6052082Seschrock 		 * vdev uses as described above, and automatically expires if we
6062082Seschrock 		 * fail.
6072082Seschrock 		 */
6082082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
6092082Seschrock 		    crtxg) == 0);
6102082Seschrock 	}
611789Sahrens 
612789Sahrens 	buf = vp->vp_nvlist;
613789Sahrens 	buflen = sizeof (vp->vp_nvlist);
614789Sahrens 
6153460Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
6163460Smmusante 	if (error != 0) {
617789Sahrens 		nvlist_free(label);
618789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
6193460Smmusante 		/* EFAULT means nvlist_pack ran out of room */
6203460Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
621789Sahrens 	}
622789Sahrens 
623789Sahrens 	/*
624789Sahrens 	 * Initialize boot block header.
625789Sahrens 	 */
626789Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
627789Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
628789Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
629789Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
630789Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
631789Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
632789Sahrens 
633789Sahrens 	/*
634789Sahrens 	 * Initialize uberblock template.
635789Sahrens 	 */
6361732Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
6371732Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
6381732Sbonwick 	*ub = spa->spa_uberblock;
6391732Sbonwick 	ub->ub_txg = 0;
640789Sahrens 
641789Sahrens 	/*
642789Sahrens 	 * Write everything in parallel.
643789Sahrens 	 */
644789Sahrens 	zio = zio_root(spa, NULL, NULL,
645789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
646789Sahrens 
647789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
648789Sahrens 
649789Sahrens 		vdev_label_write(zio, vd, l, vp,
650789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
651789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
652789Sahrens 
653789Sahrens 		vdev_label_write(zio, vd, l, vb,
654789Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
655789Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
656789Sahrens 
6571732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
6581732Sbonwick 			vdev_label_write(zio, vd, l, ub,
6591732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
6601732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL);
661789Sahrens 		}
662789Sahrens 	}
663789Sahrens 
664789Sahrens 	error = zio_wait(zio);
665789Sahrens 
666789Sahrens 	nvlist_free(label);
6671732Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
668789Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
669789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
670789Sahrens 
6713377Seschrock 	/*
6723377Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
6734451Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
674*5450Sbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
675*5450Sbrendan 	 * level 2 ARC devices.
6763377Seschrock 	 */
6773377Seschrock 	if (error == 0 && !vd->vdev_isspare &&
6783377Seschrock 	    (reason == VDEV_LABEL_SPARE ||
6793377Seschrock 	    spa_spare_exists(vd->vdev_guid, NULL)))
6803377Seschrock 		spa_spare_add(vd);
6812082Seschrock 
682*5450Sbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
683*5450Sbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
684*5450Sbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
685*5450Sbrendan 		spa_l2cache_add(vd);
686*5450Sbrendan 
6873377Seschrock 	return (error);
6882082Seschrock }
6892082Seschrock 
690789Sahrens /*
691789Sahrens  * ==========================================================================
692789Sahrens  * uberblock load/sync
693789Sahrens  * ==========================================================================
694789Sahrens  */
695789Sahrens 
696789Sahrens /*
697789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
698789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
699789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
700789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
701789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
702789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
703789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
704789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
705789Sahrens  */
706789Sahrens static int
707789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
708789Sahrens {
709789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
710789Sahrens 		return (-1);
711789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
712789Sahrens 		return (1);
713789Sahrens 
714789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
715789Sahrens 		return (-1);
716789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
717789Sahrens 		return (1);
718789Sahrens 
719789Sahrens 	return (0);
720789Sahrens }
721789Sahrens 
722789Sahrens static void
723789Sahrens vdev_uberblock_load_done(zio_t *zio)
724789Sahrens {
7251732Sbonwick 	uberblock_t *ub = zio->io_data;
726789Sahrens 	uberblock_t *ubbest = zio->io_private;
727789Sahrens 	spa_t *spa = zio->io_spa;
728789Sahrens 
7291732Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
730789Sahrens 
7311544Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
732789Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
733789Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
734789Sahrens 			*ubbest = *ub;
735789Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
736789Sahrens 	}
737789Sahrens 
738789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
739789Sahrens }
740789Sahrens 
741789Sahrens void
742789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
743789Sahrens {
744789Sahrens 	int l, c, n;
745789Sahrens 
746789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
747789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
748789Sahrens 
749789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
750789Sahrens 		return;
751789Sahrens 
752789Sahrens 	if (vdev_is_dead(vd))
753789Sahrens 		return;
754789Sahrens 
755789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
7561732Sbonwick 		for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
757789Sahrens 			vdev_label_read(zio, vd, l,
7581732Sbonwick 			    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
7591732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
7601732Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd),
761789Sahrens 			    vdev_uberblock_load_done, ubbest);
762789Sahrens 		}
763789Sahrens 	}
764789Sahrens }
765789Sahrens 
766789Sahrens /*
767789Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
7681635Sbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
769789Sahrens  */
770789Sahrens static void
771789Sahrens vdev_uberblock_sync_done(zio_t *zio)
772789Sahrens {
773789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
774789Sahrens 
7751635Sbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
776789Sahrens 		atomic_add_64(good_writes, 1);
777789Sahrens }
778789Sahrens 
779789Sahrens static void
7801732Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, uint64_t txg)
781789Sahrens {
782789Sahrens 	int l, c, n;
783789Sahrens 
784789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
7851732Sbonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], txg);
786789Sahrens 
787789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
788789Sahrens 		return;
789789Sahrens 
790789Sahrens 	if (vdev_is_dead(vd))
791789Sahrens 		return;
792789Sahrens 
7931732Sbonwick 	n = txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
794789Sahrens 
7951732Sbonwick 	ASSERT(ub->ub_txg == txg);
796789Sahrens 
797789Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
7981732Sbonwick 		vdev_label_write(zio, vd, l, ub,
7991732Sbonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n),
8001732Sbonwick 		    VDEV_UBERBLOCK_SIZE(vd),
8011732Sbonwick 		    vdev_uberblock_sync_done, NULL);
802789Sahrens 
803789Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
804789Sahrens }
805789Sahrens 
806789Sahrens static int
8071732Sbonwick vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *vd, uint64_t txg)
808789Sahrens {
8091732Sbonwick 	uberblock_t *ubbuf;
8101732Sbonwick 	size_t size = vd->vdev_top ? VDEV_UBERBLOCK_SIZE(vd) : SPA_MAXBLOCKSIZE;
811789Sahrens 	uint64_t *good_writes;
812789Sahrens 	zio_t *zio;
813789Sahrens 	int error;
814789Sahrens 
8151732Sbonwick 	ubbuf = zio_buf_alloc(size);
8161732Sbonwick 	bzero(ubbuf, size);
8171732Sbonwick 	*ubbuf = *ub;
818789Sahrens 
819789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
820789Sahrens 
821789Sahrens 	zio = zio_root(spa, NULL, good_writes,
822789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
823789Sahrens 
8241732Sbonwick 	vdev_uberblock_sync(zio, ubbuf, vd, txg);
825789Sahrens 
826789Sahrens 	error = zio_wait(zio);
827789Sahrens 
828789Sahrens 	if (error && *good_writes != 0) {
829789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
830789Sahrens 		error = 0;
831789Sahrens 	}
832789Sahrens 
833789Sahrens 	/*
834789Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
835789Sahrens 	 * the CANT_OPEN state.
836789Sahrens 	 */
837789Sahrens 	if (*good_writes == 0 && error == 0)
838789Sahrens 		error = EIO;
839789Sahrens 
840789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
8411732Sbonwick 	zio_buf_free(ubbuf, size);
842789Sahrens 
843789Sahrens 	return (error);
844789Sahrens }
845789Sahrens 
846789Sahrens /*
847789Sahrens  * Sync out an individual vdev.
848789Sahrens  */
849789Sahrens static void
850789Sahrens vdev_sync_label_done(zio_t *zio)
851789Sahrens {
852789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
853789Sahrens 
854789Sahrens 	if (zio->io_error == 0)
855789Sahrens 		atomic_add_64(good_writes, 1);
856789Sahrens }
857789Sahrens 
858789Sahrens static void
859789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
860789Sahrens {
861789Sahrens 	nvlist_t *label;
862789Sahrens 	vdev_phys_t *vp;
863789Sahrens 	char *buf;
864789Sahrens 	size_t buflen;
865789Sahrens 	int c;
866789Sahrens 
867789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
868789Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
869789Sahrens 
870789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
871789Sahrens 		return;
872789Sahrens 
873789Sahrens 	if (vdev_is_dead(vd))
874789Sahrens 		return;
875789Sahrens 
876789Sahrens 	/*
877789Sahrens 	 * Generate a label describing the top-level config to which we belong.
878789Sahrens 	 */
8791635Sbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
880789Sahrens 
881789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
882789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
883789Sahrens 
884789Sahrens 	buf = vp->vp_nvlist;
885789Sahrens 	buflen = sizeof (vp->vp_nvlist);
886789Sahrens 
8871544Seschrock 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0)
888789Sahrens 		vdev_label_write(zio, vd, l, vp,
889789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
890789Sahrens 		    vdev_sync_label_done, NULL);
891789Sahrens 
892789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
893789Sahrens 	nvlist_free(label);
894789Sahrens 
895789Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
896789Sahrens }
897789Sahrens 
898789Sahrens static int
899789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
900789Sahrens {
901789Sahrens 	uint64_t *good_writes;
902789Sahrens 	zio_t *zio;
903789Sahrens 	int error;
904789Sahrens 
905789Sahrens 	ASSERT(vd == vd->vdev_top);
906789Sahrens 
907789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
908789Sahrens 
909789Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
910789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
911789Sahrens 
912789Sahrens 	/*
913789Sahrens 	 * Recursively kick off writes to all labels.
914789Sahrens 	 */
915789Sahrens 	vdev_sync_label(zio, vd, l, txg);
916789Sahrens 
917789Sahrens 	error = zio_wait(zio);
918789Sahrens 
919789Sahrens 	if (error && *good_writes != 0) {
920789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
921789Sahrens 		error = 0;
922789Sahrens 	}
923789Sahrens 
924789Sahrens 	if (*good_writes == 0 && error == 0)
925789Sahrens 		error = ENODEV;
926789Sahrens 
9274527Sperrin 	/*
9284527Sperrin 	 * Failure to write a label can be fatal for a
9294527Sperrin 	 * top level vdev. We don't want this for slogs
9304527Sperrin 	 * as we use the main pool if they go away.
9314527Sperrin 	 */
9324527Sperrin 	if (vd->vdev_islog)
9334527Sperrin 		error = 0;
9344527Sperrin 
935789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
936789Sahrens 
937789Sahrens 	return (error);
938789Sahrens }
939789Sahrens 
940789Sahrens /*
941789Sahrens  * Sync the entire vdev configuration.
942789Sahrens  *
943789Sahrens  * The order of operations is carefully crafted to ensure that
944789Sahrens  * if the system panics or loses power at any time, the state on disk
945789Sahrens  * is still transactionally consistent.  The in-line comments below
946789Sahrens  * describe the failure semantics at each stage.
947789Sahrens  *
948789Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
949789Sahrens  * at any time, you can just call it again, and it will resume its work.
950789Sahrens  */
951789Sahrens int
9521635Sbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg)
953789Sahrens {
9541635Sbonwick 	spa_t *spa = uvd->vdev_spa;
955789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
956789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
9571635Sbonwick 	vdev_t *vd;
958789Sahrens 	zio_t *zio;
9595329Sgw25295 	int l, last_error = 0, error = 0;
9605329Sgw25295 	uint64_t good_writes = 0;
9615329Sgw25295 	boolean_t retry_avail = B_TRUE;
962789Sahrens 
963789Sahrens 	ASSERT(ub->ub_txg <= txg);
964789Sahrens 
965789Sahrens 	/*
966789Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
967789Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
9681635Sbonwick 	 * then there's nothing to do.
969789Sahrens 	 */
970789Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
971789Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
972789Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
973789Sahrens 		    spa_name(spa), txg);
974789Sahrens 		return (0);
975789Sahrens 	}
976789Sahrens 
977789Sahrens 	if (txg > spa_freeze_txg(spa))
978789Sahrens 		return (0);
979789Sahrens 
9801635Sbonwick 	ASSERT(txg <= spa->spa_final_txg);
9811635Sbonwick 
982789Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
983789Sahrens 
984789Sahrens 	/*
985789Sahrens 	 * Flush the write cache of every disk that's been written to
986789Sahrens 	 * in this transaction group.  This ensures that all blocks
987789Sahrens 	 * written in this txg will be committed to stable storage
988789Sahrens 	 * before any uberblock that references them.
989789Sahrens 	 */
990789Sahrens 	zio = zio_root(spa, NULL, NULL,
991789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
992789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
993789Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
994789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
995789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
996789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
997789Sahrens 	}
998789Sahrens 	(void) zio_wait(zio);
999789Sahrens 
10005329Sgw25295 retry:
1001789Sahrens 	/*
1002789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1003789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1004789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1005789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
1006789Sahrens 	 * which have not yet been touched, will still be valid.
1007789Sahrens 	 */
1008789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1009789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1010789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
1011789Sahrens 			if (l & 1)
1012789Sahrens 				continue;
1013789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
10145329Sgw25295 				last_error = error;
10155329Sgw25295 			else
10165329Sgw25295 				good_writes++;
1017789Sahrens 		}
1018789Sahrens 	}
1019789Sahrens 
1020789Sahrens 	/*
10215329Sgw25295 	 * If all the vdevs that are currently dirty have failed or the
10225329Sgw25295 	 * spa_dirty_list is empty then we dirty all the vdevs and try again.
10235329Sgw25295 	 * This is a last ditch effort to ensure that we get at least one
10245329Sgw25295 	 * update before proceeding to the uberblock.
10255329Sgw25295 	 */
10265329Sgw25295 	if (good_writes == 0 && retry_avail) {
10275329Sgw25295 		vdev_config_dirty(rvd);
10285329Sgw25295 		retry_avail = B_FALSE;
10295329Sgw25295 		last_error = 0;
10305329Sgw25295 		goto retry;
10315329Sgw25295 	}
10325329Sgw25295 
10335329Sgw25295 	if (good_writes == 0)
10345329Sgw25295 		return (last_error);
10355329Sgw25295 
10365329Sgw25295 	/*
1037789Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
1038789Sahrens 	 * updates are committed to stable storage before the uberblock update.
1039789Sahrens 	 */
1040789Sahrens 	zio = zio_root(spa, NULL, NULL,
1041789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
1042789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1043789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1044789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
1045789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
1046789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1047789Sahrens 	}
1048789Sahrens 	(void) zio_wait(zio);
1049789Sahrens 
1050789Sahrens 	/*
10511635Sbonwick 	 * Sync the uberblocks to all vdevs in the tree specified by uvd.
10521635Sbonwick 	 * If the system dies in the middle of this step, there are two cases
10531635Sbonwick 	 * to consider, and the on-disk state is consistent either way:
1054789Sahrens 	 *
1055789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1056789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1057789Sahrens 	 *	(which had not yet been touched) will be valid with respect
1058789Sahrens 	 *	to that uberblock.
1059789Sahrens 	 *
1060789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1061789Sahrens 	 *	will be the newest, and the even labels (which had all
1062789Sahrens 	 *	been successfully committed) will be valid with respect
1063789Sahrens 	 *	to the new uberblocks.
10645329Sgw25295 	 *
10655329Sgw25295 	 * NOTE: We retry to an uberblock update on the root if we were
10665329Sgw25295 	 * failed our initial update attempt.
1067789Sahrens 	 */
10685329Sgw25295 	error = vdev_uberblock_sync_tree(spa, ub, uvd, txg);
10695329Sgw25295 	if (error && uvd != rvd)
10705329Sgw25295 		error = vdev_uberblock_sync_tree(spa, ub, rvd, txg);
10715329Sgw25295 
10725329Sgw25295 	if (error)
1073789Sahrens 		return (error);
1074789Sahrens 
1075789Sahrens 	/*
1076789Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
1077789Sahrens 	 * are no longer needed (because the new uberblocks and the even
1078789Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
1079789Sahrens 	 */
1080789Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
1081789Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
1082789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1083789Sahrens 
10845329Sgw25295 	last_error = 0;
1085789Sahrens 	/*
1086789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1087789Sahrens 	 * in the middle of this process, the even labels and the new
1088789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1089789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1090789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
1091789Sahrens 	 * all labels will be brought up to date.
1092789Sahrens 	 */
1093789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1094789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1095789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
1096789Sahrens 			if ((l & 1) == 0)
1097789Sahrens 				continue;
1098789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
10995329Sgw25295 				last_error = error;
11005329Sgw25295 			else
11015329Sgw25295 				good_writes++;
1102789Sahrens 		}
1103789Sahrens 	}
1104789Sahrens 
11055329Sgw25295 	if (good_writes == 0)
11065329Sgw25295 		return (last_error);
11075329Sgw25295 
1108789Sahrens 	/*
1109789Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
1110789Sahrens 	 * updates are committed to stable storage before the next
1111789Sahrens 	 * transaction group begins.
1112789Sahrens 	 */
1113789Sahrens 	zio = zio_root(spa, NULL, NULL,
1114789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
1115789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
1116789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
1117789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
1118789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
1119789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
1120789Sahrens 	}
1121789Sahrens 	(void) zio_wait(zio);
1122789Sahrens 
1123789Sahrens 	return (0);
1124789Sahrens }
1125