xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 7768:7d2fd61355d7)
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 /*
226615Sgw25295  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * Virtual Device Labels
28789Sahrens  * ---------------------
29789Sahrens  *
30789Sahrens  * The vdev label serves several distinct purposes:
31789Sahrens  *
32789Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
33789Sahrens  *	   identity within the pool.
34789Sahrens  *
35789Sahrens  * 	2. Verify that all the devices given in a configuration are present
36789Sahrens  *         within the pool.
37789Sahrens  *
38789Sahrens  * 	3. Determine the uberblock for the pool.
39789Sahrens  *
40789Sahrens  * 	4. In case of an import operation, determine the configuration of the
41789Sahrens  *         toplevel vdev of which it is a part.
42789Sahrens  *
43789Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
44789Sahrens  *         provide enough information to the administrator to determine which
45789Sahrens  *         devices are missing.
46789Sahrens  *
47789Sahrens  * It is important to note that while the kernel is responsible for writing the
48789Sahrens  * label, it only consumes the information in the first three cases.  The
49789Sahrens  * latter information is only consumed in userland when determining the
50789Sahrens  * configuration to import a pool.
51789Sahrens  *
52789Sahrens  *
53789Sahrens  * Label Organization
54789Sahrens  * ------------------
55789Sahrens  *
56789Sahrens  * Before describing the contents of the label, it's important to understand how
57789Sahrens  * the labels are written and updated with respect to the uberblock.
58789Sahrens  *
59789Sahrens  * When the pool configuration is altered, either because it was newly created
60789Sahrens  * or a device was added, we want to update all the labels such that we can deal
61789Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
62789Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
634451Seschrock  * labels and an uberblock with the following transaction groups:
64789Sahrens  *
65789Sahrens  *              L1          UB          L2
66789Sahrens  *           +------+    +------+    +------+
67789Sahrens  *           |      |    |      |    |      |
68789Sahrens  *           | t10  |    | t10  |    | t10  |
69789Sahrens  *           |      |    |      |    |      |
70789Sahrens  *           +------+    +------+    +------+
71789Sahrens  *
72789Sahrens  * In this stable state, the labels and the uberblock were all updated within
73789Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
74789Sahrens  * that we can detect when we fail partway through writing the label.
75789Sahrens  *
76789Sahrens  * In order to identify which labels are valid, the labels are written in the
77789Sahrens  * following manner:
78789Sahrens  *
79789Sahrens  * 	1. For each vdev, update 'L1' to the new label
80789Sahrens  * 	2. Update the uberblock
81789Sahrens  * 	3. For each vdev, update 'L2' to the new label
82789Sahrens  *
83789Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
84789Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
85789Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
86789Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
87789Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
88789Sahrens  * therefore L1 is valid.
89789Sahrens  *
90789Sahrens  * Another added complexity is that not every label is updated when the config
91789Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
92789Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
93789Sahrens  * be older than the pool uberblock, because the necessary information is stored
94789Sahrens  * on another vdev.
95789Sahrens  *
96789Sahrens  *
97789Sahrens  * On-disk Format
98789Sahrens  * --------------
99789Sahrens  *
100789Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
101789Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
102789Sahrens  * VTOC disk labels, but is otherwise ignored.
103789Sahrens  *
104789Sahrens  * The first half of the label is a packed nvlist which contains pool wide
105789Sahrens  * properties, per-vdev properties, and configuration information.  It is
106789Sahrens  * described in more detail below.
107789Sahrens  *
108789Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
109789Sahrens  * These uberblocks are updated whenever a transaction group is committed,
110789Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
111789Sahrens  * vdev for the 'best' uberblock.
112789Sahrens  *
113789Sahrens  *
114789Sahrens  * Configuration Information
115789Sahrens  * -------------------------
116789Sahrens  *
117789Sahrens  * The nvlist describing the pool and vdev contains the following elements:
118789Sahrens  *
119789Sahrens  * 	version		ZFS on-disk version
120789Sahrens  * 	name		Pool name
121789Sahrens  * 	state		Pool state
122789Sahrens  * 	txg		Transaction group in which this label was written
123789Sahrens  * 	pool_guid	Unique identifier for this pool
124789Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
125789Sahrens  *
126789Sahrens  * Each leaf device label also contains the following:
127789Sahrens  *
128789Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
129789Sahrens  * 	guid		Unique ID for the leaf vdev
130789Sahrens  *
131789Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
132789Sahrens  */
133789Sahrens 
134789Sahrens #include <sys/zfs_context.h>
135789Sahrens #include <sys/spa.h>
136789Sahrens #include <sys/spa_impl.h>
137789Sahrens #include <sys/dmu.h>
138789Sahrens #include <sys/zap.h>
139789Sahrens #include <sys/vdev.h>
140789Sahrens #include <sys/vdev_impl.h>
141789Sahrens #include <sys/uberblock_impl.h>
142789Sahrens #include <sys/metaslab.h>
143789Sahrens #include <sys/zio.h>
144789Sahrens #include <sys/fs/zfs.h>
145789Sahrens 
146789Sahrens /*
147789Sahrens  * Basic routines to read and write from a vdev label.
148789Sahrens  * Used throughout the rest of this file.
149789Sahrens  */
150789Sahrens uint64_t
151789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
152789Sahrens {
1531732Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
1544577Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
1551732Sbonwick 
156789Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
157789Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
158789Sahrens }
159789Sahrens 
1606615Sgw25295 /*
1616615Sgw25295  * Returns back the vdev label associated with the passed in offset.
1626615Sgw25295  */
1636615Sgw25295 int
1646615Sgw25295 vdev_label_number(uint64_t psize, uint64_t offset)
1656615Sgw25295 {
1666615Sgw25295 	int l;
1676615Sgw25295 
1686615Sgw25295 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
1696615Sgw25295 		offset -= psize - VDEV_LABEL_END_SIZE;
1706615Sgw25295 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
1716615Sgw25295 	}
1726615Sgw25295 	l = offset / sizeof (vdev_label_t);
1736615Sgw25295 	return (l < VDEV_LABELS ? l : -1);
1746615Sgw25295 }
1756615Sgw25295 
176789Sahrens static void
177789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
1787754SJeff.Bonwick@Sun.COM 	uint64_t size, zio_done_func_t *done, void *private, int flags)
179789Sahrens {
1807754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
1817754SJeff.Bonwick@Sun.COM 	    SCL_STATE_ALL);
1827754SJeff.Bonwick@Sun.COM 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
183789Sahrens 
184789Sahrens 	zio_nowait(zio_read_phys(zio, vd,
185789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
186789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1877754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
188789Sahrens }
189789Sahrens 
190789Sahrens static void
191789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
1925688Sbonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
193789Sahrens {
1947754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
1957754SJeff.Bonwick@Sun.COM 	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
1967754SJeff.Bonwick@Sun.COM 	    (SCL_CONFIG | SCL_STATE) &&
1977754SJeff.Bonwick@Sun.COM 	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
1987754SJeff.Bonwick@Sun.COM 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
199789Sahrens 
200789Sahrens 	zio_nowait(zio_write_phys(zio, vd,
201789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
202789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
2035688Sbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
204789Sahrens }
205789Sahrens 
206789Sahrens /*
207789Sahrens  * Generate the nvlist representing this vdev's config.
208789Sahrens  */
209789Sahrens nvlist_t *
2102082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
2115450Sbrendan     boolean_t isspare, boolean_t isl2cache)
212789Sahrens {
213789Sahrens 	nvlist_t *nv = NULL;
214789Sahrens 
2151544Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
216789Sahrens 
217789Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
218789Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
2195450Sbrendan 	if (!isspare && !isl2cache)
2202082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
2212082Seschrock 		    == 0);
222789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
223789Sahrens 
224789Sahrens 	if (vd->vdev_path != NULL)
225789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
226789Sahrens 		    vd->vdev_path) == 0);
227789Sahrens 
228789Sahrens 	if (vd->vdev_devid != NULL)
229789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
230789Sahrens 		    vd->vdev_devid) == 0);
231789Sahrens 
2324451Seschrock 	if (vd->vdev_physpath != NULL)
2334451Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2344451Seschrock 		    vd->vdev_physpath) == 0);
2354451Seschrock 
2362082Seschrock 	if (vd->vdev_nparity != 0) {
2372082Seschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
2382082Seschrock 		    VDEV_TYPE_RAIDZ) == 0);
2392082Seschrock 
2402082Seschrock 		/*
2412082Seschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
2422082Seschrock 		 * into a crufty old storage pool.
2432082Seschrock 		 */
2442082Seschrock 		ASSERT(vd->vdev_nparity == 1 ||
2452082Seschrock 		    (vd->vdev_nparity == 2 &&
2464577Sahrens 		    spa_version(spa) >= SPA_VERSION_RAID6));
2472082Seschrock 
2482082Seschrock 		/*
2492082Seschrock 		 * Note that we'll add the nparity tag even on storage pools
2502082Seschrock 		 * that only support a single parity device -- older software
2512082Seschrock 		 * will just ignore it.
2522082Seschrock 		 */
2532082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
2542082Seschrock 		    vd->vdev_nparity) == 0);
2552082Seschrock 	}
2562082Seschrock 
2571171Seschrock 	if (vd->vdev_wholedisk != -1ULL)
2581171Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2591171Seschrock 		    vd->vdev_wholedisk) == 0);
2601171Seschrock 
2611544Seschrock 	if (vd->vdev_not_present)
2621544Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
2631544Seschrock 
2642082Seschrock 	if (vd->vdev_isspare)
2652082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
2662082Seschrock 
2675450Sbrendan 	if (!isspare && !isl2cache && vd == vd->vdev_top) {
268789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
269789Sahrens 		    vd->vdev_ms_array) == 0);
270789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
271789Sahrens 		    vd->vdev_ms_shift) == 0);
272789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
273789Sahrens 		    vd->vdev_ashift) == 0);
274789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
275789Sahrens 		    vd->vdev_asize) == 0);
2764527Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2774527Sperrin 		    vd->vdev_islog) == 0);
278789Sahrens 	}
279789Sahrens 
280789Sahrens 	if (vd->vdev_dtl.smo_object != 0)
281789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
282789Sahrens 		    vd->vdev_dtl.smo_object) == 0);
283789Sahrens 
284789Sahrens 	if (getstats) {
285789Sahrens 		vdev_stat_t vs;
286789Sahrens 		vdev_get_stats(vd, &vs);
287789Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
288789Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
289789Sahrens 	}
290789Sahrens 
291789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
292789Sahrens 		nvlist_t **child;
293789Sahrens 		int c;
294789Sahrens 
295789Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
296789Sahrens 		    KM_SLEEP);
297789Sahrens 
298789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
2992082Seschrock 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
3005450Sbrendan 			    getstats, isspare, isl2cache);
301789Sahrens 
302789Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
303789Sahrens 		    child, vd->vdev_children) == 0);
304789Sahrens 
305789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
306789Sahrens 			nvlist_free(child[c]);
307789Sahrens 
308789Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
3091485Slling 
3101485Slling 	} else {
3111732Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
3121485Slling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
3131732Sbonwick 			    B_TRUE) == 0);
3144451Seschrock 		if (vd->vdev_faulted)
3154451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
3164451Seschrock 			    B_TRUE) == 0);
3174451Seschrock 		if (vd->vdev_degraded)
3184451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3194451Seschrock 			    B_TRUE) == 0);
3204451Seschrock 		if (vd->vdev_removed)
3214451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3224451Seschrock 			    B_TRUE) == 0);
3234451Seschrock 		if (vd->vdev_unspare)
3244451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3254451Seschrock 			    B_TRUE) == 0);
326789Sahrens 	}
327789Sahrens 
328789Sahrens 	return (nv);
329789Sahrens }
330789Sahrens 
331789Sahrens nvlist_t *
332789Sahrens vdev_label_read_config(vdev_t *vd)
333789Sahrens {
3341635Sbonwick 	spa_t *spa = vd->vdev_spa;
335789Sahrens 	nvlist_t *config = NULL;
336789Sahrens 	vdev_phys_t *vp;
337789Sahrens 	zio_t *zio;
3387754SJeff.Bonwick@Sun.COM 	int flags =
3397754SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
340789Sahrens 
3417754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3421635Sbonwick 
3435329Sgw25295 	if (!vdev_readable(vd))
344789Sahrens 		return (NULL);
345789Sahrens 
346789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
347789Sahrens 
3487754SJeff.Bonwick@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++) {
349789Sahrens 
3507754SJeff.Bonwick@Sun.COM 		zio = zio_root(spa, NULL, NULL, flags);
351789Sahrens 
352789Sahrens 		vdev_label_read(zio, vd, l, vp,
353789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
3547754SJeff.Bonwick@Sun.COM 		    sizeof (vdev_phys_t), NULL, NULL, flags);
355789Sahrens 
356789Sahrens 		if (zio_wait(zio) == 0 &&
357789Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
3581544Seschrock 		    &config, 0) == 0)
359789Sahrens 			break;
360789Sahrens 
361789Sahrens 		if (config != NULL) {
362789Sahrens 			nvlist_free(config);
363789Sahrens 			config = NULL;
364789Sahrens 		}
365789Sahrens 	}
366789Sahrens 
367789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
368789Sahrens 
369789Sahrens 	return (config);
370789Sahrens }
371789Sahrens 
3723377Seschrock /*
3733377Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
3743377Seschrock  * in with the device guid if this spare is active elsewhere on the system.
3753377Seschrock  */
3763377Seschrock static boolean_t
3773377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
3785450Sbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
3793377Seschrock {
3803377Seschrock 	spa_t *spa = vd->vdev_spa;
3813377Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
3823377Seschrock 	uint64_t vdtxg = 0;
3833377Seschrock 	nvlist_t *label;
3843377Seschrock 
3853377Seschrock 	if (spare_guid)
3863377Seschrock 		*spare_guid = 0ULL;
3875450Sbrendan 	if (l2cache_guid)
3885450Sbrendan 		*l2cache_guid = 0ULL;
3893377Seschrock 
3903377Seschrock 	/*
3913377Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
3923377Seschrock 	 */
3933377Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
3943377Seschrock 		return (B_FALSE);
3953377Seschrock 
3963377Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
3973377Seschrock 	    &vdtxg);
3983377Seschrock 
3993377Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
4003377Seschrock 	    &state) != 0 ||
4013377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
4023377Seschrock 	    &device_guid) != 0) {
4033377Seschrock 		nvlist_free(label);
4043377Seschrock 		return (B_FALSE);
4053377Seschrock 	}
4063377Seschrock 
4075450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
4083377Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
4093377Seschrock 	    &pool_guid) != 0 ||
4103377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
4113377Seschrock 	    &txg) != 0)) {
4123377Seschrock 		nvlist_free(label);
4133377Seschrock 		return (B_FALSE);
4143377Seschrock 	}
4153377Seschrock 
4163377Seschrock 	nvlist_free(label);
4173377Seschrock 
4183377Seschrock 	/*
4193377Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
4203377Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
4213377Seschrock 	 * spare (which we check for later on).
4223377Seschrock 	 */
4235450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
4243377Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
4257214Slling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
4265450Sbrendan 	    !spa_l2cache_exists(device_guid, NULL))
4273377Seschrock 		return (B_FALSE);
4283377Seschrock 
4293377Seschrock 	/*
4303377Seschrock 	 * If the transaction group is zero, then this an initialized (but
4313377Seschrock 	 * unused) label.  This is only an error if the create transaction
4323377Seschrock 	 * on-disk is the same as the one we're using now, in which case the
4333377Seschrock 	 * user has attempted to add the same vdev multiple times in the same
4343377Seschrock 	 * transaction.
4353377Seschrock 	 */
4365450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
4375450Sbrendan 	    txg == 0 && vdtxg == crtxg)
4383377Seschrock 		return (B_TRUE);
4393377Seschrock 
4403377Seschrock 	/*
4413377Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
4423377Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
4435450Sbrendan 	 * to add.  We also check if it is an l2cache device.
4443377Seschrock 	 */
4457214Slling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
4463377Seschrock 	    spa_has_spare(spa, device_guid)) {
4473377Seschrock 		if (spare_guid)
4483377Seschrock 			*spare_guid = device_guid;
4493377Seschrock 
4503377Seschrock 		switch (reason) {
4513377Seschrock 		case VDEV_LABEL_CREATE:
4525450Sbrendan 		case VDEV_LABEL_L2CACHE:
4533377Seschrock 			return (B_TRUE);
4543377Seschrock 
4553377Seschrock 		case VDEV_LABEL_REPLACE:
4563377Seschrock 			return (!spa_has_spare(spa, device_guid) ||
4573377Seschrock 			    spare_pool != 0ULL);
4583377Seschrock 
4593377Seschrock 		case VDEV_LABEL_SPARE:
4603377Seschrock 			return (spa_has_spare(spa, device_guid));
4613377Seschrock 		}
4623377Seschrock 	}
4633377Seschrock 
4643377Seschrock 	/*
4655450Sbrendan 	 * Check to see if this is an l2cache device.
4665450Sbrendan 	 */
4675450Sbrendan 	if (spa_l2cache_exists(device_guid, NULL))
4685450Sbrendan 		return (B_TRUE);
4695450Sbrendan 
4705450Sbrendan 	/*
4713377Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
4723377Seschrock 	 * pool on the system.
4733377Seschrock 	 */
4743377Seschrock 	return (state == POOL_STATE_ACTIVE);
4753377Seschrock }
4763377Seschrock 
4773377Seschrock /*
4783377Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
4793377Seschrock  * use, and writable.  We put down an initial label which we will later
4803377Seschrock  * overwrite with a complete label.  Note that it's important to do this
4813377Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
4823377Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
4833377Seschrock  * itself.
4843377Seschrock  */
4853377Seschrock int
4863377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
487789Sahrens {
488789Sahrens 	spa_t *spa = vd->vdev_spa;
489789Sahrens 	nvlist_t *label;
490789Sahrens 	vdev_phys_t *vp;
491789Sahrens 	vdev_boot_header_t *vb;
4921732Sbonwick 	uberblock_t *ub;
493789Sahrens 	zio_t *zio;
494789Sahrens 	char *buf;
495789Sahrens 	size_t buflen;
496789Sahrens 	int error;
4975450Sbrendan 	uint64_t spare_guid, l2cache_guid;
4987754SJeff.Bonwick@Sun.COM 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
499789Sahrens 
5007754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5011635Sbonwick 
5027754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
5033377Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
5043377Seschrock 		    crtxg, reason)) != 0)
505789Sahrens 			return (error);
506789Sahrens 
507789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
508789Sahrens 		return (0);
509789Sahrens 
510789Sahrens 	/*
5113377Seschrock 	 * Dead vdevs cannot be initialized.
512789Sahrens 	 */
513789Sahrens 	if (vdev_is_dead(vd))
514789Sahrens 		return (EIO);
515789Sahrens 
516789Sahrens 	/*
5173377Seschrock 	 * Determine if the vdev is in use.
518789Sahrens 	 */
5193377Seschrock 	if (reason != VDEV_LABEL_REMOVE &&
5205450Sbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
5213377Seschrock 		return (EBUSY);
522789Sahrens 
5233377Seschrock 	ASSERT(reason != VDEV_LABEL_REMOVE ||
5245450Sbrendan 	    vdev_inuse(vd, crtxg, reason, NULL, NULL));
525789Sahrens 
5263377Seschrock 	/*
5275450Sbrendan 	 * If this is a request to add or replace a spare or l2cache device
5285450Sbrendan 	 * that is in use elsewhere on the system, then we must update the
5295450Sbrendan 	 * guid (which was initialized to a random value) to reflect the
5305450Sbrendan 	 * actual GUID (which is shared between multiple pools).
5313377Seschrock 	 */
5325450Sbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
5335450Sbrendan 	    spare_guid != 0ULL) {
534*7768SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
5352082Seschrock 
536*7768SJeff.Bonwick@Sun.COM 		vd->vdev_guid += guid_delta;
537*7768SJeff.Bonwick@Sun.COM 
538*7768SJeff.Bonwick@Sun.COM 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
539*7768SJeff.Bonwick@Sun.COM 			pvd->vdev_guid_sum += guid_delta;
5402082Seschrock 
5413377Seschrock 		/*
5423377Seschrock 		 * If this is a replacement, then we want to fallthrough to the
5433377Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
5444451Seschrock 		 * labeled appropriately and we can just return.
5453377Seschrock 		 */
5463377Seschrock 		if (reason == VDEV_LABEL_SPARE)
5473377Seschrock 			return (0);
5483377Seschrock 		ASSERT(reason == VDEV_LABEL_REPLACE);
549789Sahrens 	}
550789Sahrens 
5515450Sbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
5525450Sbrendan 	    l2cache_guid != 0ULL) {
553*7768SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
5545450Sbrendan 
555*7768SJeff.Bonwick@Sun.COM 		vd->vdev_guid += guid_delta;
556*7768SJeff.Bonwick@Sun.COM 
557*7768SJeff.Bonwick@Sun.COM 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
558*7768SJeff.Bonwick@Sun.COM 			pvd->vdev_guid_sum += guid_delta;
5595450Sbrendan 
5605450Sbrendan 		/*
5615450Sbrendan 		 * If this is a replacement, then we want to fallthrough to the
5625450Sbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
5635450Sbrendan 		 * already labeled appropriately and we can just return.
5645450Sbrendan 		 */
5655450Sbrendan 		if (reason == VDEV_LABEL_L2CACHE)
5665450Sbrendan 			return (0);
5675450Sbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
5685450Sbrendan 	}
5695450Sbrendan 
570789Sahrens 	/*
5713377Seschrock 	 * Initialize its label.
572789Sahrens 	 */
573789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
574789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
575789Sahrens 
576789Sahrens 	/*
577789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
578789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
579789Sahrens 	 * really part of an active pool just yet.  The labels will
580789Sahrens 	 * be written again with a meaningful txg by spa_sync().
581789Sahrens 	 */
5823377Seschrock 	if (reason == VDEV_LABEL_SPARE ||
5833377Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
5843377Seschrock 		/*
5853377Seschrock 		 * For inactive hot spares, we generate a special label that
5863377Seschrock 		 * identifies as a mutually shared hot spare.  We write the
5873377Seschrock 		 * label if we are adding a hot spare, or if we are removing an
5883377Seschrock 		 * active hot spare (in which case we want to revert the
5893377Seschrock 		 * labels).
5903377Seschrock 		 */
5912082Seschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
592789Sahrens 
5932082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
5942082Seschrock 		    spa_version(spa)) == 0);
5952082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
5962082Seschrock 		    POOL_STATE_SPARE) == 0);
5972082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
5982082Seschrock 		    vd->vdev_guid) == 0);
5995450Sbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
6005450Sbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
6015450Sbrendan 		/*
6025450Sbrendan 		 * For level 2 ARC devices, add a special label.
6035450Sbrendan 		 */
6045450Sbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6055450Sbrendan 
6065450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
6075450Sbrendan 		    spa_version(spa)) == 0);
6085450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
6095450Sbrendan 		    POOL_STATE_L2CACHE) == 0);
6105450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
6115450Sbrendan 		    vd->vdev_guid) == 0);
6122082Seschrock 	} else {
6132082Seschrock 		label = spa_config_generate(spa, vd, 0ULL, B_FALSE);
6142082Seschrock 
6152082Seschrock 		/*
6162082Seschrock 		 * Add our creation time.  This allows us to detect multiple
6172082Seschrock 		 * vdev uses as described above, and automatically expires if we
6182082Seschrock 		 * fail.
6192082Seschrock 		 */
6202082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
6212082Seschrock 		    crtxg) == 0);
6222082Seschrock 	}
623789Sahrens 
624789Sahrens 	buf = vp->vp_nvlist;
625789Sahrens 	buflen = sizeof (vp->vp_nvlist);
626789Sahrens 
6273460Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
6283460Smmusante 	if (error != 0) {
629789Sahrens 		nvlist_free(label);
630789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
6313460Smmusante 		/* EFAULT means nvlist_pack ran out of room */
6323460Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
633789Sahrens 	}
634789Sahrens 
635789Sahrens 	/*
636789Sahrens 	 * Initialize boot block header.
637789Sahrens 	 */
638789Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
639789Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
640789Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
641789Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
642789Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
643789Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
644789Sahrens 
645789Sahrens 	/*
646789Sahrens 	 * Initialize uberblock template.
647789Sahrens 	 */
6481732Sbonwick 	ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
6491732Sbonwick 	bzero(ub, VDEV_UBERBLOCK_SIZE(vd));
6501732Sbonwick 	*ub = spa->spa_uberblock;
6511732Sbonwick 	ub->ub_txg = 0;
652789Sahrens 
653789Sahrens 	/*
654789Sahrens 	 * Write everything in parallel.
655789Sahrens 	 */
6565688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
657789Sahrens 
6587754SJeff.Bonwick@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++) {
659789Sahrens 
660789Sahrens 		vdev_label_write(zio, vd, l, vp,
661789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
6625688Sbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
663789Sahrens 
664789Sahrens 		vdev_label_write(zio, vd, l, vb,
665789Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
6665688Sbonwick 		    sizeof (vdev_boot_header_t), NULL, NULL, flags);
667789Sahrens 
6687754SJeff.Bonwick@Sun.COM 		for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
6691732Sbonwick 			vdev_label_write(zio, vd, l, ub,
6701732Sbonwick 			    VDEV_UBERBLOCK_OFFSET(vd, n),
6715688Sbonwick 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL, flags);
672789Sahrens 		}
673789Sahrens 	}
674789Sahrens 
675789Sahrens 	error = zio_wait(zio);
676789Sahrens 
677789Sahrens 	nvlist_free(label);
6781732Sbonwick 	zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd));
679789Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
680789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
681789Sahrens 
6823377Seschrock 	/*
6833377Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
6844451Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
6855450Sbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
6865450Sbrendan 	 * level 2 ARC devices.
6873377Seschrock 	 */
6883377Seschrock 	if (error == 0 && !vd->vdev_isspare &&
6893377Seschrock 	    (reason == VDEV_LABEL_SPARE ||
6907214Slling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
6913377Seschrock 		spa_spare_add(vd);
6922082Seschrock 
6935450Sbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
6945450Sbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
6955450Sbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
6965450Sbrendan 		spa_l2cache_add(vd);
6975450Sbrendan 
6983377Seschrock 	return (error);
6992082Seschrock }
7002082Seschrock 
701789Sahrens /*
702789Sahrens  * ==========================================================================
703789Sahrens  * uberblock load/sync
704789Sahrens  * ==========================================================================
705789Sahrens  */
706789Sahrens 
707789Sahrens /*
708789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
709789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
710789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
711789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
712789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
713789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
714789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
715789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
716789Sahrens  */
717789Sahrens static int
718789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
719789Sahrens {
720789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
721789Sahrens 		return (-1);
722789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
723789Sahrens 		return (1);
724789Sahrens 
725789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
726789Sahrens 		return (-1);
727789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
728789Sahrens 		return (1);
729789Sahrens 
730789Sahrens 	return (0);
731789Sahrens }
732789Sahrens 
733789Sahrens static void
734789Sahrens vdev_uberblock_load_done(zio_t *zio)
735789Sahrens {
7367754SJeff.Bonwick@Sun.COM 	zio_t *rio = zio->io_private;
7371732Sbonwick 	uberblock_t *ub = zio->io_data;
7387754SJeff.Bonwick@Sun.COM 	uberblock_t *ubbest = rio->io_private;
739789Sahrens 
7401732Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
741789Sahrens 
7421544Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
7437754SJeff.Bonwick@Sun.COM 		mutex_enter(&rio->io_lock);
744789Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
745789Sahrens 			*ubbest = *ub;
7467754SJeff.Bonwick@Sun.COM 		mutex_exit(&rio->io_lock);
747789Sahrens 	}
748789Sahrens 
749789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
750789Sahrens }
751789Sahrens 
752789Sahrens void
753789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
754789Sahrens {
7557754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
7567754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
7577754SJeff.Bonwick@Sun.COM 	int flags =
7587754SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
759789Sahrens 
7607754SJeff.Bonwick@Sun.COM 	if (vd == rvd) {
7617754SJeff.Bonwick@Sun.COM 		ASSERT(zio == NULL);
7627754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7637754SJeff.Bonwick@Sun.COM 		zio = zio_root(spa, NULL, ubbest, flags);
7647754SJeff.Bonwick@Sun.COM 		bzero(ubbest, sizeof (uberblock_t));
7657754SJeff.Bonwick@Sun.COM 	}
7667754SJeff.Bonwick@Sun.COM 
7677754SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
7687754SJeff.Bonwick@Sun.COM 
7697754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
770789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
771789Sahrens 
7727754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
7737754SJeff.Bonwick@Sun.COM 		for (int l = 0; l < VDEV_LABELS; l++) {
7747754SJeff.Bonwick@Sun.COM 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
7757754SJeff.Bonwick@Sun.COM 				vdev_label_read(zio, vd, l,
7767754SJeff.Bonwick@Sun.COM 				    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
7777754SJeff.Bonwick@Sun.COM 				    VDEV_UBERBLOCK_OFFSET(vd, n),
7787754SJeff.Bonwick@Sun.COM 				    VDEV_UBERBLOCK_SIZE(vd),
7797754SJeff.Bonwick@Sun.COM 				    vdev_uberblock_load_done, zio, flags);
7807754SJeff.Bonwick@Sun.COM 			}
7817754SJeff.Bonwick@Sun.COM 		}
7827754SJeff.Bonwick@Sun.COM 	}
783789Sahrens 
7847754SJeff.Bonwick@Sun.COM 	if (vd == rvd) {
7857754SJeff.Bonwick@Sun.COM 		(void) zio_wait(zio);
7867754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
787789Sahrens 	}
788789Sahrens }
789789Sahrens 
790789Sahrens /*
7915688Sbonwick  * On success, increment root zio's count of good writes.
7921635Sbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
793789Sahrens  */
794789Sahrens static void
795789Sahrens vdev_uberblock_sync_done(zio_t *zio)
796789Sahrens {
7975688Sbonwick 	uint64_t *good_writes = zio->io_private;
798789Sahrens 
7991635Sbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
800789Sahrens 		atomic_add_64(good_writes, 1);
801789Sahrens }
802789Sahrens 
8035688Sbonwick /*
8045688Sbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
8055688Sbonwick  */
806789Sahrens static void
8077754SJeff.Bonwick@Sun.COM vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
808789Sahrens {
8095688Sbonwick 	uberblock_t *ubbuf;
8107754SJeff.Bonwick@Sun.COM 	int n;
811789Sahrens 
8127754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
8137754SJeff.Bonwick@Sun.COM 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
814789Sahrens 
815789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
816789Sahrens 		return;
817789Sahrens 
8187754SJeff.Bonwick@Sun.COM 	if (!vdev_writeable(vd))
819789Sahrens 		return;
820789Sahrens 
8215688Sbonwick 	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
822789Sahrens 
8235688Sbonwick 	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
8245688Sbonwick 	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
8251732Sbonwick 	*ubbuf = *ub;
826789Sahrens 
8277754SJeff.Bonwick@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++)
8285688Sbonwick 		vdev_label_write(zio, vd, l, ubbuf,
8297754SJeff.Bonwick@Sun.COM 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
8305688Sbonwick 		    vdev_uberblock_sync_done, zio->io_private,
8317754SJeff.Bonwick@Sun.COM 		    flags | ZIO_FLAG_DONT_PROPAGATE);
832789Sahrens 
8335688Sbonwick 	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
8345688Sbonwick }
835789Sahrens 
8365688Sbonwick int
8375688Sbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
8385688Sbonwick {
8395688Sbonwick 	spa_t *spa = svd[0]->vdev_spa;
8407754SJeff.Bonwick@Sun.COM 	zio_t *zio;
8415688Sbonwick 	uint64_t good_writes = 0;
842789Sahrens 
8437754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, &good_writes, flags);
8447754SJeff.Bonwick@Sun.COM 
8457754SJeff.Bonwick@Sun.COM 	for (int v = 0; v < svdcount; v++)
8467754SJeff.Bonwick@Sun.COM 		vdev_uberblock_sync(zio, ub, svd[v], flags);
8477754SJeff.Bonwick@Sun.COM 
8485688Sbonwick 	(void) zio_wait(zio);
849789Sahrens 
850789Sahrens 	/*
8515688Sbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
8525688Sbonwick 	 * are no longer needed (because the new uberblocks and the even
8535688Sbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
854789Sahrens 	 */
8555688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
856789Sahrens 
8577754SJeff.Bonwick@Sun.COM 	for (int v = 0; v < svdcount; v++)
8585688Sbonwick 		zio_flush(zio, svd[v]);
859789Sahrens 
8605688Sbonwick 	(void) zio_wait(zio);
8615688Sbonwick 
8625688Sbonwick 	return (good_writes >= 1 ? 0 : EIO);
863789Sahrens }
864789Sahrens 
865789Sahrens /*
8665688Sbonwick  * On success, increment the count of good writes for our top-level vdev.
867789Sahrens  */
868789Sahrens static void
8695688Sbonwick vdev_label_sync_done(zio_t *zio)
870789Sahrens {
8715688Sbonwick 	uint64_t *good_writes = zio->io_private;
872789Sahrens 
873789Sahrens 	if (zio->io_error == 0)
874789Sahrens 		atomic_add_64(good_writes, 1);
875789Sahrens }
876789Sahrens 
8775688Sbonwick /*
8785688Sbonwick  * If there weren't enough good writes, indicate failure to the parent.
8795688Sbonwick  */
880789Sahrens static void
8815688Sbonwick vdev_label_sync_top_done(zio_t *zio)
8825688Sbonwick {
8835688Sbonwick 	uint64_t *good_writes = zio->io_private;
8845688Sbonwick 
8855688Sbonwick 	if (*good_writes == 0)
8865688Sbonwick 		zio->io_error = EIO;
8875688Sbonwick 
8885688Sbonwick 	kmem_free(good_writes, sizeof (uint64_t));
8895688Sbonwick }
8905688Sbonwick 
8915688Sbonwick /*
8927041Seschrock  * We ignore errors for log and cache devices, simply free the private data.
8936976Seschrock  */
8946976Seschrock static void
8957041Seschrock vdev_label_sync_ignore_done(zio_t *zio)
8966976Seschrock {
8976976Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
8986976Seschrock }
8996976Seschrock 
9006976Seschrock /*
9015688Sbonwick  * Write all even or odd labels to all leaves of the specified vdev.
9025688Sbonwick  */
9035688Sbonwick static void
9047754SJeff.Bonwick@Sun.COM vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
905789Sahrens {
906789Sahrens 	nvlist_t *label;
907789Sahrens 	vdev_phys_t *vp;
908789Sahrens 	char *buf;
909789Sahrens 	size_t buflen;
910789Sahrens 
9117754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
9127754SJeff.Bonwick@Sun.COM 		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
913789Sahrens 
914789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
915789Sahrens 		return;
916789Sahrens 
9177754SJeff.Bonwick@Sun.COM 	if (!vdev_writeable(vd))
918789Sahrens 		return;
919789Sahrens 
920789Sahrens 	/*
921789Sahrens 	 * Generate a label describing the top-level config to which we belong.
922789Sahrens 	 */
9231635Sbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
924789Sahrens 
925789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
926789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
927789Sahrens 
928789Sahrens 	buf = vp->vp_nvlist;
929789Sahrens 	buflen = sizeof (vp->vp_nvlist);
930789Sahrens 
9315688Sbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
9325688Sbonwick 		for (; l < VDEV_LABELS; l += 2) {
9335688Sbonwick 			vdev_label_write(zio, vd, l, vp,
9345688Sbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
9355688Sbonwick 			    sizeof (vdev_phys_t),
9365688Sbonwick 			    vdev_label_sync_done, zio->io_private,
9377754SJeff.Bonwick@Sun.COM 			    flags | ZIO_FLAG_DONT_PROPAGATE);
9385688Sbonwick 		}
9395688Sbonwick 	}
940789Sahrens 
941789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
942789Sahrens 	nvlist_free(label);
943789Sahrens }
944789Sahrens 
9455688Sbonwick int
9467754SJeff.Bonwick@Sun.COM vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
947789Sahrens {
9487754SJeff.Bonwick@Sun.COM 	list_t *dl = &spa->spa_config_dirty_list;
9495688Sbonwick 	vdev_t *vd;
9507754SJeff.Bonwick@Sun.COM 	zio_t *zio;
951789Sahrens 	int error;
952789Sahrens 
9535688Sbonwick 	/*
9547754SJeff.Bonwick@Sun.COM 	 * Write the new labels to disk.
9555688Sbonwick 	 */
9567754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, flags);
957789Sahrens 
9585688Sbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
9595688Sbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
9605688Sbonwick 		    KM_SLEEP);
9617754SJeff.Bonwick@Sun.COM 		zio_t *vio = zio_null(zio, spa,
9627041Seschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
9637041Seschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
9645688Sbonwick 		    good_writes, flags);
9657754SJeff.Bonwick@Sun.COM 		vdev_label_sync(vio, vd, l, txg, flags);
9665688Sbonwick 		zio_nowait(vio);
9675688Sbonwick 	}
9687754SJeff.Bonwick@Sun.COM 
9697754SJeff.Bonwick@Sun.COM 	error = zio_wait(zio);
970789Sahrens 
9715688Sbonwick 	/*
9725688Sbonwick 	 * Flush the new labels to disk.
9735688Sbonwick 	 */
9745688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
975789Sahrens 
9765688Sbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
9775688Sbonwick 		zio_flush(zio, vd);
9784527Sperrin 
9795688Sbonwick 	(void) zio_wait(zio);
980789Sahrens 
981789Sahrens 	return (error);
982789Sahrens }
983789Sahrens 
984789Sahrens /*
9855688Sbonwick  * Sync the uberblock and any changes to the vdev configuration.
986789Sahrens  *
987789Sahrens  * The order of operations is carefully crafted to ensure that
988789Sahrens  * if the system panics or loses power at any time, the state on disk
989789Sahrens  * is still transactionally consistent.  The in-line comments below
990789Sahrens  * describe the failure semantics at each stage.
991789Sahrens  *
9925688Sbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
993789Sahrens  * at any time, you can just call it again, and it will resume its work.
994789Sahrens  */
9957754SJeff.Bonwick@Sun.COM int
9965688Sbonwick vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
997789Sahrens {
9985688Sbonwick 	spa_t *spa = svd[0]->vdev_spa;
999789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
10001635Sbonwick 	vdev_t *vd;
1001789Sahrens 	zio_t *zio;
10027754SJeff.Bonwick@Sun.COM 	int error;
10037754SJeff.Bonwick@Sun.COM 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1004789Sahrens 
1005789Sahrens 	ASSERT(ub->ub_txg <= txg);
1006789Sahrens 
1007789Sahrens 	/*
10085688Sbonwick 	 * If this isn't a resync due to I/O errors,
10095688Sbonwick 	 * and nothing changed in this transaction group,
10105688Sbonwick 	 * and the vdev configuration hasn't changed,
10111635Sbonwick 	 * then there's nothing to do.
1012789Sahrens 	 */
10135688Sbonwick 	if (ub->ub_txg < txg &&
10145688Sbonwick 	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
10157754SJeff.Bonwick@Sun.COM 	    list_is_empty(&spa->spa_config_dirty_list))
10167754SJeff.Bonwick@Sun.COM 		return (0);
1017789Sahrens 
1018789Sahrens 	if (txg > spa_freeze_txg(spa))
10197754SJeff.Bonwick@Sun.COM 		return (0);
1020789Sahrens 
10211635Sbonwick 	ASSERT(txg <= spa->spa_final_txg);
10221635Sbonwick 
1023789Sahrens 	/*
1024789Sahrens 	 * Flush the write cache of every disk that's been written to
1025789Sahrens 	 * in this transaction group.  This ensures that all blocks
1026789Sahrens 	 * written in this txg will be committed to stable storage
1027789Sahrens 	 * before any uberblock that references them.
1028789Sahrens 	 */
10295688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
10305688Sbonwick 
1031789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
10325688Sbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
10335688Sbonwick 		zio_flush(zio, vd);
10345688Sbonwick 
1035789Sahrens 	(void) zio_wait(zio);
1036789Sahrens 
1037789Sahrens 	/*
1038789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1039789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1040789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1041789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
10425688Sbonwick 	 * which have not yet been touched, will still be valid.  We flush
10435688Sbonwick 	 * the new labels to disk to ensure that all even-label updates
10445688Sbonwick 	 * are committed to stable storage before the uberblock update.
1045789Sahrens 	 */
10467754SJeff.Bonwick@Sun.COM 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
10477754SJeff.Bonwick@Sun.COM 		return (error);
1048789Sahrens 
1049789Sahrens 	/*
10507754SJeff.Bonwick@Sun.COM 	 * Sync the uberblocks to all vdevs in svd[].
10511635Sbonwick 	 * If the system dies in the middle of this step, there are two cases
10521635Sbonwick 	 * to consider, and the on-disk state is consistent either way:
1053789Sahrens 	 *
1054789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1055789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1056789Sahrens 	 *	(which had not yet been touched) will be valid with respect
1057789Sahrens 	 *	to that uberblock.
1058789Sahrens 	 *
1059789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1060789Sahrens 	 *	will be the newest, and the even labels (which had all
1061789Sahrens 	 *	been successfully committed) will be valid with respect
1062789Sahrens 	 *	to the new uberblocks.
1063789Sahrens 	 */
10647754SJeff.Bonwick@Sun.COM 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
10657754SJeff.Bonwick@Sun.COM 		return (error);
1066789Sahrens 
1067789Sahrens 	/*
1068789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1069789Sahrens 	 * in the middle of this process, the even labels and the new
1070789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1071789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1072789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
10735688Sbonwick 	 * all labels will be brought up to date.  We flush the new labels
10745688Sbonwick 	 * to disk to ensure that all odd-label updates are committed to
10755688Sbonwick 	 * stable storage before the next transaction group begins.
1076789Sahrens 	 */
10777754SJeff.Bonwick@Sun.COM 	return (vdev_label_sync_list(spa, 1, txg, flags));
1078789Sahrens }
1079