xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22*3f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
25fa9e4066Sahrens /*
26fa9e4066Sahrens  * Virtual Device Labels
27fa9e4066Sahrens  * ---------------------
28fa9e4066Sahrens  *
29fa9e4066Sahrens  * The vdev label serves several distinct purposes:
30fa9e4066Sahrens  *
31fa9e4066Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
32fa9e4066Sahrens  *	   identity within the pool.
33fa9e4066Sahrens  *
34fa9e4066Sahrens  * 	2. Verify that all the devices given in a configuration are present
35fa9e4066Sahrens  *         within the pool.
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * 	3. Determine the uberblock for the pool.
38fa9e4066Sahrens  *
39fa9e4066Sahrens  * 	4. In case of an import operation, determine the configuration of the
40fa9e4066Sahrens  *         toplevel vdev of which it is a part.
41fa9e4066Sahrens  *
42fa9e4066Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
43fa9e4066Sahrens  *         provide enough information to the administrator to determine which
44fa9e4066Sahrens  *         devices are missing.
45fa9e4066Sahrens  *
46fa9e4066Sahrens  * It is important to note that while the kernel is responsible for writing the
47fa9e4066Sahrens  * label, it only consumes the information in the first three cases.  The
48fa9e4066Sahrens  * latter information is only consumed in userland when determining the
49fa9e4066Sahrens  * configuration to import a pool.
50fa9e4066Sahrens  *
51fa9e4066Sahrens  *
52fa9e4066Sahrens  * Label Organization
53fa9e4066Sahrens  * ------------------
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * Before describing the contents of the label, it's important to understand how
56fa9e4066Sahrens  * the labels are written and updated with respect to the uberblock.
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * When the pool configuration is altered, either because it was newly created
59fa9e4066Sahrens  * or a device was added, we want to update all the labels such that we can deal
60fa9e4066Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
61fa9e4066Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
623d7072f8Seschrock  * labels and an uberblock with the following transaction groups:
63fa9e4066Sahrens  *
64fa9e4066Sahrens  *              L1          UB          L2
65fa9e4066Sahrens  *           +------+    +------+    +------+
66fa9e4066Sahrens  *           |      |    |      |    |      |
67fa9e4066Sahrens  *           | t10  |    | t10  |    | t10  |
68fa9e4066Sahrens  *           |      |    |      |    |      |
69fa9e4066Sahrens  *           +------+    +------+    +------+
70fa9e4066Sahrens  *
71fa9e4066Sahrens  * In this stable state, the labels and the uberblock were all updated within
72fa9e4066Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
73fa9e4066Sahrens  * that we can detect when we fail partway through writing the label.
74fa9e4066Sahrens  *
75fa9e4066Sahrens  * In order to identify which labels are valid, the labels are written in the
76fa9e4066Sahrens  * following manner:
77fa9e4066Sahrens  *
78fa9e4066Sahrens  * 	1. For each vdev, update 'L1' to the new label
79fa9e4066Sahrens  * 	2. Update the uberblock
80fa9e4066Sahrens  * 	3. For each vdev, update 'L2' to the new label
81fa9e4066Sahrens  *
82fa9e4066Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
83fa9e4066Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
84fa9e4066Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
85fa9e4066Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
86fa9e4066Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
87fa9e4066Sahrens  * therefore L1 is valid.
88fa9e4066Sahrens  *
89fa9e4066Sahrens  * Another added complexity is that not every label is updated when the config
90fa9e4066Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
91fa9e4066Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
92fa9e4066Sahrens  * be older than the pool uberblock, because the necessary information is stored
93fa9e4066Sahrens  * on another vdev.
94fa9e4066Sahrens  *
95fa9e4066Sahrens  *
96fa9e4066Sahrens  * On-disk Format
97fa9e4066Sahrens  * --------------
98fa9e4066Sahrens  *
99fa9e4066Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
100fa9e4066Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
101fa9e4066Sahrens  * VTOC disk labels, but is otherwise ignored.
102fa9e4066Sahrens  *
103fa9e4066Sahrens  * The first half of the label is a packed nvlist which contains pool wide
104fa9e4066Sahrens  * properties, per-vdev properties, and configuration information.  It is
105fa9e4066Sahrens  * described in more detail below.
106fa9e4066Sahrens  *
107fa9e4066Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
108fa9e4066Sahrens  * These uberblocks are updated whenever a transaction group is committed,
109fa9e4066Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
110fa9e4066Sahrens  * vdev for the 'best' uberblock.
111fa9e4066Sahrens  *
112fa9e4066Sahrens  *
113fa9e4066Sahrens  * Configuration Information
114fa9e4066Sahrens  * -------------------------
115fa9e4066Sahrens  *
116fa9e4066Sahrens  * The nvlist describing the pool and vdev contains the following elements:
117fa9e4066Sahrens  *
118fa9e4066Sahrens  * 	version		ZFS on-disk version
119fa9e4066Sahrens  * 	name		Pool name
120fa9e4066Sahrens  * 	state		Pool state
121fa9e4066Sahrens  * 	txg		Transaction group in which this label was written
122fa9e4066Sahrens  * 	pool_guid	Unique identifier for this pool
123fa9e4066Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
124fa9e4066Sahrens  *
125fa9e4066Sahrens  * Each leaf device label also contains the following:
126fa9e4066Sahrens  *
127fa9e4066Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
128fa9e4066Sahrens  * 	guid		Unique ID for the leaf vdev
129fa9e4066Sahrens  *
130fa9e4066Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
131fa9e4066Sahrens  */
132fa9e4066Sahrens 
133fa9e4066Sahrens #include <sys/zfs_context.h>
134fa9e4066Sahrens #include <sys/spa.h>
135fa9e4066Sahrens #include <sys/spa_impl.h>
136fa9e4066Sahrens #include <sys/dmu.h>
137fa9e4066Sahrens #include <sys/zap.h>
138fa9e4066Sahrens #include <sys/vdev.h>
139fa9e4066Sahrens #include <sys/vdev_impl.h>
140fa9e4066Sahrens #include <sys/uberblock_impl.h>
141fa9e4066Sahrens #include <sys/metaslab.h>
142fa9e4066Sahrens #include <sys/zio.h>
143*3f9d6ad7SLin Ling #include <sys/dsl_scan.h>
144fa9e4066Sahrens #include <sys/fs/zfs.h>
145fa9e4066Sahrens 
146fa9e4066Sahrens /*
147fa9e4066Sahrens  * Basic routines to read and write from a vdev label.
148fa9e4066Sahrens  * Used throughout the rest of this file.
149fa9e4066Sahrens  */
150fa9e4066Sahrens uint64_t
151fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
152fa9e4066Sahrens {
153ecc2d604Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
154e7437265Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
155ecc2d604Sbonwick 
156fa9e4066Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
157fa9e4066Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
158fa9e4066Sahrens }
159fa9e4066Sahrens 
16021bf64a7Sgw /*
16121bf64a7Sgw  * Returns back the vdev label associated with the passed in offset.
16221bf64a7Sgw  */
16321bf64a7Sgw int
16421bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset)
16521bf64a7Sgw {
16621bf64a7Sgw 	int l;
16721bf64a7Sgw 
16821bf64a7Sgw 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
16921bf64a7Sgw 		offset -= psize - VDEV_LABEL_END_SIZE;
17021bf64a7Sgw 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
17121bf64a7Sgw 	}
17221bf64a7Sgw 	l = offset / sizeof (vdev_label_t);
17321bf64a7Sgw 	return (l < VDEV_LABELS ? l : -1);
17421bf64a7Sgw }
17521bf64a7Sgw 
176fa9e4066Sahrens static void
177fa9e4066Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
178e14bb325SJeff Bonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
179fa9e4066Sahrens {
180e14bb325SJeff Bonwick 	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
181e14bb325SJeff Bonwick 	    SCL_STATE_ALL);
182e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
183fa9e4066Sahrens 
184fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
185fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
186fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
187e14bb325SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
188fa9e4066Sahrens }
189fa9e4066Sahrens 
190fa9e4066Sahrens static void
191fa9e4066Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
19217f17c2dSbonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
193fa9e4066Sahrens {
194e14bb325SJeff Bonwick 	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
195e14bb325SJeff Bonwick 	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
196e14bb325SJeff Bonwick 	    (SCL_CONFIG | SCL_STATE) &&
197e14bb325SJeff Bonwick 	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
198e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
199fa9e4066Sahrens 
200fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
201fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
202fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
20317f17c2dSbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /*
207fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
208fa9e4066Sahrens  */
209fa9e4066Sahrens nvlist_t *
21099653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
211*3f9d6ad7SLin Ling     vdev_config_flag_t flags)
212fa9e4066Sahrens {
213fa9e4066Sahrens 	nvlist_t *nv = NULL;
214fa9e4066Sahrens 
215ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
216fa9e4066Sahrens 
217fa9e4066Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
218fa9e4066Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
219*3f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
22099653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
22199653d4eSeschrock 		    == 0);
222fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
223fa9e4066Sahrens 
224fa9e4066Sahrens 	if (vd->vdev_path != NULL)
225fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
226fa9e4066Sahrens 		    vd->vdev_path) == 0);
227fa9e4066Sahrens 
228fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
229fa9e4066Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
230fa9e4066Sahrens 		    vd->vdev_devid) == 0);
231fa9e4066Sahrens 
2323d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
2333d7072f8Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2343d7072f8Seschrock 		    vd->vdev_physpath) == 0);
2353d7072f8Seschrock 
2366809eb4eSEric Schrock 	if (vd->vdev_fru != NULL)
2376809eb4eSEric Schrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU,
2386809eb4eSEric Schrock 		    vd->vdev_fru) == 0);
2396809eb4eSEric Schrock 
24099653d4eSeschrock 	if (vd->vdev_nparity != 0) {
24199653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
24299653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
24399653d4eSeschrock 
24499653d4eSeschrock 		/*
24599653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
24699653d4eSeschrock 		 * into a crufty old storage pool.
24799653d4eSeschrock 		 */
24899653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
249f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 2 &&
250f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
251f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 3 &&
252f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
25399653d4eSeschrock 
25499653d4eSeschrock 		/*
25599653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
25699653d4eSeschrock 		 * that only support a single parity device -- older software
25799653d4eSeschrock 		 * will just ignore it.
25899653d4eSeschrock 		 */
25999653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
26099653d4eSeschrock 		    vd->vdev_nparity) == 0);
26199653d4eSeschrock 	}
26299653d4eSeschrock 
263afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
264afefbcddSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
265afefbcddSeschrock 		    vd->vdev_wholedisk) == 0);
266afefbcddSeschrock 
267ea8dc4b6Seschrock 	if (vd->vdev_not_present)
268ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
269ea8dc4b6Seschrock 
27099653d4eSeschrock 	if (vd->vdev_isspare)
27199653d4eSeschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
27299653d4eSeschrock 
273*3f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
274*3f9d6ad7SLin Ling 	    vd == vd->vdev_top) {
275fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
276fa9e4066Sahrens 		    vd->vdev_ms_array) == 0);
277fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
278fa9e4066Sahrens 		    vd->vdev_ms_shift) == 0);
279fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
280fa9e4066Sahrens 		    vd->vdev_ashift) == 0);
281fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
282fa9e4066Sahrens 		    vd->vdev_asize) == 0);
2838654d025Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2848654d025Sperrin 		    vd->vdev_islog) == 0);
285*3f9d6ad7SLin Ling 		if (vd->vdev_removing)
286*3f9d6ad7SLin Ling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
287*3f9d6ad7SLin Ling 			    vd->vdev_removing) == 0);
288fa9e4066Sahrens 	}
289fa9e4066Sahrens 
2908ad4d6ddSJeff Bonwick 	if (vd->vdev_dtl_smo.smo_object != 0)
291fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
2928ad4d6ddSJeff Bonwick 		    vd->vdev_dtl_smo.smo_object) == 0);
293fa9e4066Sahrens 
29488ecc943SGeorge Wilson 	if (vd->vdev_crtxg)
29588ecc943SGeorge Wilson 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
29688ecc943SGeorge Wilson 		    vd->vdev_crtxg) == 0);
29788ecc943SGeorge Wilson 
298fa9e4066Sahrens 	if (getstats) {
299fa9e4066Sahrens 		vdev_stat_t vs;
300*3f9d6ad7SLin Ling 		pool_scan_stat_t ps;
301*3f9d6ad7SLin Ling 
302fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
303*3f9d6ad7SLin Ling 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
304fa9e4066Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
305*3f9d6ad7SLin Ling 
306*3f9d6ad7SLin Ling 		/* provide either current or previous scan information */
307*3f9d6ad7SLin Ling 		if (spa_scan_get_stats(spa, &ps) == 0) {
308*3f9d6ad7SLin Ling 			VERIFY(nvlist_add_uint64_array(nv,
309*3f9d6ad7SLin Ling 			    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
310*3f9d6ad7SLin Ling 			    sizeof (pool_scan_stat_t) / sizeof (uint64_t))
311*3f9d6ad7SLin Ling 			    == 0);
312*3f9d6ad7SLin Ling 		}
313fa9e4066Sahrens 	}
314fa9e4066Sahrens 
315fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
316fa9e4066Sahrens 		nvlist_t **child;
317*3f9d6ad7SLin Ling 		int c, idx;
318fa9e4066Sahrens 
31988ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
32088ecc943SGeorge Wilson 
321fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
322fa9e4066Sahrens 		    KM_SLEEP);
323fa9e4066Sahrens 
324*3f9d6ad7SLin Ling 		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
325*3f9d6ad7SLin Ling 			vdev_t *cvd = vd->vdev_child[c];
326*3f9d6ad7SLin Ling 
327*3f9d6ad7SLin Ling 			/*
328*3f9d6ad7SLin Ling 			 * If we're generating an nvlist of removing
329*3f9d6ad7SLin Ling 			 * vdevs then skip over any device which is
330*3f9d6ad7SLin Ling 			 * not being removed.
331*3f9d6ad7SLin Ling 			 */
332*3f9d6ad7SLin Ling 			if ((flags & VDEV_CONFIG_REMOVING) &&
333*3f9d6ad7SLin Ling 			    !cvd->vdev_removing)
334*3f9d6ad7SLin Ling 				continue;
335fa9e4066Sahrens 
336*3f9d6ad7SLin Ling 			child[idx++] = vdev_config_generate(spa, cvd,
337*3f9d6ad7SLin Ling 			    getstats, flags);
338*3f9d6ad7SLin Ling 		}
339*3f9d6ad7SLin Ling 
340*3f9d6ad7SLin Ling 		if (idx) {
341*3f9d6ad7SLin Ling 			VERIFY(nvlist_add_nvlist_array(nv,
342*3f9d6ad7SLin Ling 			    ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
343*3f9d6ad7SLin Ling 		}
344fa9e4066Sahrens 
345*3f9d6ad7SLin Ling 		for (c = 0; c < idx; c++)
346fa9e4066Sahrens 			nvlist_free(child[c]);
347fa9e4066Sahrens 
348fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
349441d80aaSlling 
350441d80aaSlling 	} else {
351069f55e2SEric Schrock 		const char *aux = NULL;
352069f55e2SEric Schrock 
353ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
354441d80aaSlling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
355ecc2d604Sbonwick 			    B_TRUE) == 0);
3563d7072f8Seschrock 		if (vd->vdev_faulted)
3573d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
3583d7072f8Seschrock 			    B_TRUE) == 0);
3593d7072f8Seschrock 		if (vd->vdev_degraded)
3603d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3613d7072f8Seschrock 			    B_TRUE) == 0);
3623d7072f8Seschrock 		if (vd->vdev_removed)
3633d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3643d7072f8Seschrock 			    B_TRUE) == 0);
3653d7072f8Seschrock 		if (vd->vdev_unspare)
3663d7072f8Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3673d7072f8Seschrock 			    B_TRUE) == 0);
36888ecc943SGeorge Wilson 		if (vd->vdev_ishole)
36988ecc943SGeorge Wilson 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE,
37088ecc943SGeorge Wilson 			    B_TRUE) == 0);
371069f55e2SEric Schrock 
372069f55e2SEric Schrock 		switch (vd->vdev_stat.vs_aux) {
373069f55e2SEric Schrock 		case VDEV_AUX_ERR_EXCEEDED:
374069f55e2SEric Schrock 			aux = "err_exceeded";
375069f55e2SEric Schrock 			break;
376069f55e2SEric Schrock 
377069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
378069f55e2SEric Schrock 			aux = "external";
379069f55e2SEric Schrock 			break;
380069f55e2SEric Schrock 		}
381069f55e2SEric Schrock 
382069f55e2SEric Schrock 		if (aux != NULL)
383069f55e2SEric Schrock 			VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE,
384069f55e2SEric Schrock 			    aux) == 0);
3851195e687SMark J Musante 
3861195e687SMark J Musante 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
3871195e687SMark J Musante 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
3881195e687SMark J Musante 			    vd->vdev_orig_guid) == 0);
3891195e687SMark J Musante 		}
390fa9e4066Sahrens 	}
391fa9e4066Sahrens 
392fa9e4066Sahrens 	return (nv);
393fa9e4066Sahrens }
394fa9e4066Sahrens 
39588ecc943SGeorge Wilson /*
39688ecc943SGeorge Wilson  * Generate a view of the top-level vdevs.  If we currently have holes
39788ecc943SGeorge Wilson  * in the namespace, then generate an array which contains a list of holey
39888ecc943SGeorge Wilson  * vdevs.  Additionally, add the number of top-level children that currently
39988ecc943SGeorge Wilson  * exist.
40088ecc943SGeorge Wilson  */
40188ecc943SGeorge Wilson void
40288ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config)
40388ecc943SGeorge Wilson {
40488ecc943SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
40588ecc943SGeorge Wilson 	uint64_t *array;
406*3f9d6ad7SLin Ling 	uint_t c, idx;
40788ecc943SGeorge Wilson 
40888ecc943SGeorge Wilson 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
40988ecc943SGeorge Wilson 
410*3f9d6ad7SLin Ling 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
41188ecc943SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
41288ecc943SGeorge Wilson 
41388ecc943SGeorge Wilson 		if (tvd->vdev_ishole)
41488ecc943SGeorge Wilson 			array[idx++] = c;
41588ecc943SGeorge Wilson 	}
41688ecc943SGeorge Wilson 
417312c6e15SGeorge Wilson 	if (idx) {
418312c6e15SGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
419312c6e15SGeorge Wilson 		    array, idx) == 0);
420312c6e15SGeorge Wilson 	}
421312c6e15SGeorge Wilson 
42288ecc943SGeorge Wilson 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
42388ecc943SGeorge Wilson 	    rvd->vdev_children) == 0);
42488ecc943SGeorge Wilson 
42588ecc943SGeorge Wilson 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
42688ecc943SGeorge Wilson }
42788ecc943SGeorge Wilson 
428fa9e4066Sahrens nvlist_t *
429fa9e4066Sahrens vdev_label_read_config(vdev_t *vd)
430fa9e4066Sahrens {
4310373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
432fa9e4066Sahrens 	nvlist_t *config = NULL;
433fa9e4066Sahrens 	vdev_phys_t *vp;
434fa9e4066Sahrens 	zio_t *zio;
4358956713aSEric Schrock 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
4368956713aSEric Schrock 	    ZIO_FLAG_SPECULATIVE;
437fa9e4066Sahrens 
438e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4390373e76bSbonwick 
4400a4e9518Sgw 	if (!vdev_readable(vd))
441fa9e4066Sahrens 		return (NULL);
442fa9e4066Sahrens 
443fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
444fa9e4066Sahrens 
4458956713aSEric Schrock retry:
446e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
447fa9e4066Sahrens 
448e14bb325SJeff Bonwick 		zio = zio_root(spa, NULL, NULL, flags);
449fa9e4066Sahrens 
450fa9e4066Sahrens 		vdev_label_read(zio, vd, l, vp,
451fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
452e14bb325SJeff Bonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
453fa9e4066Sahrens 
454fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
455fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
456ea8dc4b6Seschrock 		    &config, 0) == 0)
457fa9e4066Sahrens 			break;
458fa9e4066Sahrens 
459fa9e4066Sahrens 		if (config != NULL) {
460fa9e4066Sahrens 			nvlist_free(config);
461fa9e4066Sahrens 			config = NULL;
462fa9e4066Sahrens 		}
463fa9e4066Sahrens 	}
464fa9e4066Sahrens 
4658956713aSEric Schrock 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
4668956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
4678956713aSEric Schrock 		goto retry;
4688956713aSEric Schrock 	}
4698956713aSEric Schrock 
470fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
471fa9e4066Sahrens 
472fa9e4066Sahrens 	return (config);
473fa9e4066Sahrens }
474fa9e4066Sahrens 
47539c23413Seschrock /*
47639c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
47739c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
47839c23413Seschrock  */
47939c23413Seschrock static boolean_t
48039c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
481fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
48239c23413Seschrock {
48339c23413Seschrock 	spa_t *spa = vd->vdev_spa;
48439c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
48539c23413Seschrock 	uint64_t vdtxg = 0;
48639c23413Seschrock 	nvlist_t *label;
48739c23413Seschrock 
48839c23413Seschrock 	if (spare_guid)
48939c23413Seschrock 		*spare_guid = 0ULL;
490fa94a07fSbrendan 	if (l2cache_guid)
491fa94a07fSbrendan 		*l2cache_guid = 0ULL;
49239c23413Seschrock 
49339c23413Seschrock 	/*
49439c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
49539c23413Seschrock 	 */
49639c23413Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
49739c23413Seschrock 		return (B_FALSE);
49839c23413Seschrock 
49939c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
50039c23413Seschrock 	    &vdtxg);
50139c23413Seschrock 
50239c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
50339c23413Seschrock 	    &state) != 0 ||
50439c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
50539c23413Seschrock 	    &device_guid) != 0) {
50639c23413Seschrock 		nvlist_free(label);
50739c23413Seschrock 		return (B_FALSE);
50839c23413Seschrock 	}
50939c23413Seschrock 
510fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
51139c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
51239c23413Seschrock 	    &pool_guid) != 0 ||
51339c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
51439c23413Seschrock 	    &txg) != 0)) {
51539c23413Seschrock 		nvlist_free(label);
51639c23413Seschrock 		return (B_FALSE);
51739c23413Seschrock 	}
51839c23413Seschrock 
51939c23413Seschrock 	nvlist_free(label);
52039c23413Seschrock 
52139c23413Seschrock 	/*
52239c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
52339c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
52439c23413Seschrock 	 * spare (which we check for later on).
52539c23413Seschrock 	 */
526fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
52739c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
52889a89ebfSlling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
529fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
53039c23413Seschrock 		return (B_FALSE);
53139c23413Seschrock 
53239c23413Seschrock 	/*
53339c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
53439c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
53539c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
53639c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
53739c23413Seschrock 	 * transaction.
53839c23413Seschrock 	 */
539fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
540fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
54139c23413Seschrock 		return (B_TRUE);
54239c23413Seschrock 
54339c23413Seschrock 	/*
54439c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
54539c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
546fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
54739c23413Seschrock 	 */
54889a89ebfSlling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
54939c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
55039c23413Seschrock 		if (spare_guid)
55139c23413Seschrock 			*spare_guid = device_guid;
55239c23413Seschrock 
55339c23413Seschrock 		switch (reason) {
55439c23413Seschrock 		case VDEV_LABEL_CREATE:
555fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
55639c23413Seschrock 			return (B_TRUE);
55739c23413Seschrock 
55839c23413Seschrock 		case VDEV_LABEL_REPLACE:
55939c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
56039c23413Seschrock 			    spare_pool != 0ULL);
56139c23413Seschrock 
56239c23413Seschrock 		case VDEV_LABEL_SPARE:
56339c23413Seschrock 			return (spa_has_spare(spa, device_guid));
56439c23413Seschrock 		}
56539c23413Seschrock 	}
56639c23413Seschrock 
567fa94a07fSbrendan 	/*
568fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
569fa94a07fSbrendan 	 */
570fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
571fa94a07fSbrendan 		return (B_TRUE);
572fa94a07fSbrendan 
57339c23413Seschrock 	/*
57439c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
57539c23413Seschrock 	 * pool on the system.
57639c23413Seschrock 	 */
57739c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
57839c23413Seschrock }
57939c23413Seschrock 
58039c23413Seschrock /*
58139c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
58239c23413Seschrock  * use, and writable.  We put down an initial label which we will later
58339c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
58439c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
58539c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
58639c23413Seschrock  * itself.
58739c23413Seschrock  */
58839c23413Seschrock int
58939c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
590fa9e4066Sahrens {
591fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
592fa9e4066Sahrens 	nvlist_t *label;
593fa9e4066Sahrens 	vdev_phys_t *vp;
594f83ffe1aSLin Ling 	char *pad2;
595ecc2d604Sbonwick 	uberblock_t *ub;
596fa9e4066Sahrens 	zio_t *zio;
597fa9e4066Sahrens 	char *buf;
598fa9e4066Sahrens 	size_t buflen;
599fa9e4066Sahrens 	int error;
600fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
601e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
602fa9e4066Sahrens 
603e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6040373e76bSbonwick 
605e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
60639c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
60739c23413Seschrock 		    crtxg, reason)) != 0)
608fa9e4066Sahrens 			return (error);
609fa9e4066Sahrens 
61088ecc943SGeorge Wilson 	/* Track the creation time for this vdev */
61188ecc943SGeorge Wilson 	vd->vdev_crtxg = crtxg;
61288ecc943SGeorge Wilson 
613fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
614fa9e4066Sahrens 		return (0);
615fa9e4066Sahrens 
616fa9e4066Sahrens 	/*
61739c23413Seschrock 	 * Dead vdevs cannot be initialized.
618fa9e4066Sahrens 	 */
619fa9e4066Sahrens 	if (vdev_is_dead(vd))
620fa9e4066Sahrens 		return (EIO);
621fa9e4066Sahrens 
622fa9e4066Sahrens 	/*
62339c23413Seschrock 	 * Determine if the vdev is in use.
624fa9e4066Sahrens 	 */
6251195e687SMark J Musante 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
626fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
62739c23413Seschrock 		return (EBUSY);
62839c23413Seschrock 
62939c23413Seschrock 	/*
630fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
631fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
632fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
633fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
63439c23413Seschrock 	 */
635fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
636fa94a07fSbrendan 	    spare_guid != 0ULL) {
63731157203SJeff Bonwick 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
63899653d4eSeschrock 
63931157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
64031157203SJeff Bonwick 
64131157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
64231157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
64339c23413Seschrock 
64499653d4eSeschrock 		/*
64539c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
64639c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
6473d7072f8Seschrock 		 * labeled appropriately and we can just return.
64899653d4eSeschrock 		 */
64939c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
65039c23413Seschrock 			return (0);
6511195e687SMark J Musante 		ASSERT(reason == VDEV_LABEL_REPLACE ||
6521195e687SMark J Musante 		    reason == VDEV_LABEL_SPLIT);
653fa9e4066Sahrens 	}
654fa9e4066Sahrens 
655fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
656fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
65731157203SJeff Bonwick 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
65831157203SJeff Bonwick 
65931157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
660fa94a07fSbrendan 
66131157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
66231157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
663fa94a07fSbrendan 
664fa94a07fSbrendan 		/*
665fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
666fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
667fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
668fa94a07fSbrendan 		 */
669fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
670fa94a07fSbrendan 			return (0);
671fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
672fa94a07fSbrendan 	}
673fa94a07fSbrendan 
674fa9e4066Sahrens 	/*
67539c23413Seschrock 	 * Initialize its label.
676fa9e4066Sahrens 	 */
677fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
678fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
679fa9e4066Sahrens 
680fa9e4066Sahrens 	/*
681fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
682fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
683fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
684fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
685fa9e4066Sahrens 	 */
68639c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
68739c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
68839c23413Seschrock 		/*
68939c23413Seschrock 		 * For inactive hot spares, we generate a special label that
69039c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
69139c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
69239c23413Seschrock 		 * active hot spare (in which case we want to revert the
69339c23413Seschrock 		 * labels).
69439c23413Seschrock 		 */
69599653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
69699653d4eSeschrock 
69799653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
69899653d4eSeschrock 		    spa_version(spa)) == 0);
69999653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
70099653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
70199653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
70299653d4eSeschrock 		    vd->vdev_guid) == 0);
703fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
704fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
705fa94a07fSbrendan 		/*
706fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
707fa94a07fSbrendan 		 */
708fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
709fa94a07fSbrendan 
710fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
711fa94a07fSbrendan 		    spa_version(spa)) == 0);
712fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
713fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
714fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
715fa94a07fSbrendan 		    vd->vdev_guid) == 0);
71699653d4eSeschrock 	} else {
7171195e687SMark J Musante 		uint64_t txg = 0ULL;
7181195e687SMark J Musante 
7191195e687SMark J Musante 		if (reason == VDEV_LABEL_SPLIT)
7201195e687SMark J Musante 			txg = spa->spa_uberblock.ub_txg;
7211195e687SMark J Musante 		label = spa_config_generate(spa, vd, txg, B_FALSE);
72299653d4eSeschrock 
72399653d4eSeschrock 		/*
72499653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
72599653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
72699653d4eSeschrock 		 * fail.
72799653d4eSeschrock 		 */
72899653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
72999653d4eSeschrock 		    crtxg) == 0);
73099653d4eSeschrock 	}
731fa9e4066Sahrens 
732fa9e4066Sahrens 	buf = vp->vp_nvlist;
733fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
734fa9e4066Sahrens 
735a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
736a75573b6Smmusante 	if (error != 0) {
737fa9e4066Sahrens 		nvlist_free(label);
738fa9e4066Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
739a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
740a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
741fa9e4066Sahrens 	}
742fa9e4066Sahrens 
743fa9e4066Sahrens 	/*
744fa9e4066Sahrens 	 * Initialize uberblock template.
745fa9e4066Sahrens 	 */
746f64c0e34SEric Taylor 	ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
747f64c0e34SEric Taylor 	bzero(ub, VDEV_UBERBLOCK_RING);
748ecc2d604Sbonwick 	*ub = spa->spa_uberblock;
749ecc2d604Sbonwick 	ub->ub_txg = 0;
750fa9e4066Sahrens 
751f83ffe1aSLin Ling 	/* Initialize the 2nd padding area. */
752f83ffe1aSLin Ling 	pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
753f83ffe1aSLin Ling 	bzero(pad2, VDEV_PAD_SIZE);
754f83ffe1aSLin Ling 
755fa9e4066Sahrens 	/*
756fa9e4066Sahrens 	 * Write everything in parallel.
757fa9e4066Sahrens 	 */
7588956713aSEric Schrock retry:
75917f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
760fa9e4066Sahrens 
761e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
762fa9e4066Sahrens 
763fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
764fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
76517f17c2dSbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
766fa9e4066Sahrens 
767f83ffe1aSLin Ling 		/*
768f83ffe1aSLin Ling 		 * Skip the 1st padding area.
769f83ffe1aSLin Ling 		 * Zero out the 2nd padding area where it might have
770f83ffe1aSLin Ling 		 * left over data from previous filesystem format.
771f83ffe1aSLin Ling 		 */
772f83ffe1aSLin Ling 		vdev_label_write(zio, vd, l, pad2,
773f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2),
774f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, NULL, NULL, flags);
775f83ffe1aSLin Ling 
776f64c0e34SEric Taylor 		vdev_label_write(zio, vd, l, ub,
777f64c0e34SEric Taylor 		    offsetof(vdev_label_t, vl_uberblock),
778f64c0e34SEric Taylor 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
779fa9e4066Sahrens 	}
780fa9e4066Sahrens 
781fa9e4066Sahrens 	error = zio_wait(zio);
782fa9e4066Sahrens 
7838956713aSEric Schrock 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
7848956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
7858956713aSEric Schrock 		goto retry;
7868956713aSEric Schrock 	}
7878956713aSEric Schrock 
788fa9e4066Sahrens 	nvlist_free(label);
789f83ffe1aSLin Ling 	zio_buf_free(pad2, VDEV_PAD_SIZE);
790f64c0e34SEric Taylor 	zio_buf_free(ub, VDEV_UBERBLOCK_RING);
791fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
792fa9e4066Sahrens 
79339c23413Seschrock 	/*
79439c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
7953d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
796fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
797fa94a07fSbrendan 	 * level 2 ARC devices.
79839c23413Seschrock 	 */
79939c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
80039c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
80189a89ebfSlling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
80239c23413Seschrock 		spa_spare_add(vd);
80399653d4eSeschrock 
804fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
805fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
806fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
807fa94a07fSbrendan 		spa_l2cache_add(vd);
808fa94a07fSbrendan 
80939c23413Seschrock 	return (error);
81099653d4eSeschrock }
81199653d4eSeschrock 
812fa9e4066Sahrens /*
813fa9e4066Sahrens  * ==========================================================================
814fa9e4066Sahrens  * uberblock load/sync
815fa9e4066Sahrens  * ==========================================================================
816fa9e4066Sahrens  */
817fa9e4066Sahrens 
818fa9e4066Sahrens /*
819fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
820fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
821fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
822fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
823fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
824fa9e4066Sahrens  * the missing replica comes back, then for a new seconds we'll have two
825fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
826fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
827fa9e4066Sahrens  */
828fa9e4066Sahrens static int
829fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
830fa9e4066Sahrens {
831fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
832fa9e4066Sahrens 		return (-1);
833fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
834fa9e4066Sahrens 		return (1);
835fa9e4066Sahrens 
836fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
837fa9e4066Sahrens 		return (-1);
838fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
839fa9e4066Sahrens 		return (1);
840fa9e4066Sahrens 
841fa9e4066Sahrens 	return (0);
842fa9e4066Sahrens }
843fa9e4066Sahrens 
844fa9e4066Sahrens static void
845fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
846fa9e4066Sahrens {
847468c413aSTim Haley 	spa_t *spa = zio->io_spa;
848e14bb325SJeff Bonwick 	zio_t *rio = zio->io_private;
849ecc2d604Sbonwick 	uberblock_t *ub = zio->io_data;
850e14bb325SJeff Bonwick 	uberblock_t *ubbest = rio->io_private;
851fa9e4066Sahrens 
852ecc2d604Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
853fa9e4066Sahrens 
854ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
855e14bb325SJeff Bonwick 		mutex_enter(&rio->io_lock);
856468c413aSTim Haley 		if (ub->ub_txg <= spa->spa_load_max_txg &&
8572e551927SVictor Latushkin 		    vdev_uberblock_compare(ub, ubbest) > 0)
858fa9e4066Sahrens 			*ubbest = *ub;
859e14bb325SJeff Bonwick 		mutex_exit(&rio->io_lock);
860fa9e4066Sahrens 	}
861fa9e4066Sahrens 
862fa9e4066Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
863fa9e4066Sahrens }
864fa9e4066Sahrens 
865fa9e4066Sahrens void
866fa9e4066Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
867fa9e4066Sahrens {
868e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
869e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
8708956713aSEric Schrock 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
8718956713aSEric Schrock 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
872e14bb325SJeff Bonwick 
873e14bb325SJeff Bonwick 	if (vd == rvd) {
874e14bb325SJeff Bonwick 		ASSERT(zio == NULL);
875e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
876e14bb325SJeff Bonwick 		zio = zio_root(spa, NULL, ubbest, flags);
877e14bb325SJeff Bonwick 		bzero(ubbest, sizeof (uberblock_t));
878e14bb325SJeff Bonwick 	}
879fa9e4066Sahrens 
880e14bb325SJeff Bonwick 	ASSERT(zio != NULL);
881fa9e4066Sahrens 
882e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
883e14bb325SJeff Bonwick 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
884fa9e4066Sahrens 
885e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
886e14bb325SJeff Bonwick 		for (int l = 0; l < VDEV_LABELS; l++) {
887e14bb325SJeff Bonwick 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
888e14bb325SJeff Bonwick 				vdev_label_read(zio, vd, l,
889e14bb325SJeff Bonwick 				    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
890e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_OFFSET(vd, n),
891e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_SIZE(vd),
892e14bb325SJeff Bonwick 				    vdev_uberblock_load_done, zio, flags);
893e14bb325SJeff Bonwick 			}
894fa9e4066Sahrens 		}
895fa9e4066Sahrens 	}
896e14bb325SJeff Bonwick 
897e14bb325SJeff Bonwick 	if (vd == rvd) {
898e14bb325SJeff Bonwick 		(void) zio_wait(zio);
899e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
900e14bb325SJeff Bonwick 	}
901fa9e4066Sahrens }
902fa9e4066Sahrens 
903fa9e4066Sahrens /*
90417f17c2dSbonwick  * On success, increment root zio's count of good writes.
9050373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
906fa9e4066Sahrens  */
907fa9e4066Sahrens static void
908fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
909fa9e4066Sahrens {
91017f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
911fa9e4066Sahrens 
9120373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
913fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
914fa9e4066Sahrens }
915fa9e4066Sahrens 
91617f17c2dSbonwick /*
91717f17c2dSbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
91817f17c2dSbonwick  */
919fa9e4066Sahrens static void
920e14bb325SJeff Bonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
921fa9e4066Sahrens {
92217f17c2dSbonwick 	uberblock_t *ubbuf;
923e14bb325SJeff Bonwick 	int n;
924fa9e4066Sahrens 
925e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
926e14bb325SJeff Bonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
927fa9e4066Sahrens 
928fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
929fa9e4066Sahrens 		return;
930fa9e4066Sahrens 
931e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
932fa9e4066Sahrens 		return;
933fa9e4066Sahrens 
93417f17c2dSbonwick 	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
935fa9e4066Sahrens 
93617f17c2dSbonwick 	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
93717f17c2dSbonwick 	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
93817f17c2dSbonwick 	*ubbuf = *ub;
939fa9e4066Sahrens 
940e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++)
94117f17c2dSbonwick 		vdev_label_write(zio, vd, l, ubbuf,
942e14bb325SJeff Bonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
94317f17c2dSbonwick 		    vdev_uberblock_sync_done, zio->io_private,
944e14bb325SJeff Bonwick 		    flags | ZIO_FLAG_DONT_PROPAGATE);
945fa9e4066Sahrens 
94617f17c2dSbonwick 	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
947fa9e4066Sahrens }
948fa9e4066Sahrens 
94917f17c2dSbonwick int
95017f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
951fa9e4066Sahrens {
95217f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
953e14bb325SJeff Bonwick 	zio_t *zio;
95417f17c2dSbonwick 	uint64_t good_writes = 0;
955fa9e4066Sahrens 
956e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, &good_writes, flags);
957e14bb325SJeff Bonwick 
958e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
959e14bb325SJeff Bonwick 		vdev_uberblock_sync(zio, ub, svd[v], flags);
960fa9e4066Sahrens 
96117f17c2dSbonwick 	(void) zio_wait(zio);
962fa9e4066Sahrens 
963fa9e4066Sahrens 	/*
96417f17c2dSbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
96517f17c2dSbonwick 	 * are no longer needed (because the new uberblocks and the even
96617f17c2dSbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
967fa9e4066Sahrens 	 */
96817f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
969fa9e4066Sahrens 
970e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
97117f17c2dSbonwick 		zio_flush(zio, svd[v]);
972fa9e4066Sahrens 
97317f17c2dSbonwick 	(void) zio_wait(zio);
97417f17c2dSbonwick 
97517f17c2dSbonwick 	return (good_writes >= 1 ? 0 : EIO);
976fa9e4066Sahrens }
977fa9e4066Sahrens 
978fa9e4066Sahrens /*
97917f17c2dSbonwick  * On success, increment the count of good writes for our top-level vdev.
980fa9e4066Sahrens  */
981fa9e4066Sahrens static void
98217f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
983fa9e4066Sahrens {
98417f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
985fa9e4066Sahrens 
986fa9e4066Sahrens 	if (zio->io_error == 0)
987fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
988fa9e4066Sahrens }
989fa9e4066Sahrens 
99017f17c2dSbonwick /*
99117f17c2dSbonwick  * If there weren't enough good writes, indicate failure to the parent.
99217f17c2dSbonwick  */
993fa9e4066Sahrens static void
99417f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
99517f17c2dSbonwick {
99617f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
99717f17c2dSbonwick 
99817f17c2dSbonwick 	if (*good_writes == 0)
99917f17c2dSbonwick 		zio->io_error = EIO;
100017f17c2dSbonwick 
100117f17c2dSbonwick 	kmem_free(good_writes, sizeof (uint64_t));
100217f17c2dSbonwick }
100317f17c2dSbonwick 
100451ece835Seschrock /*
10050430f8daSeschrock  * We ignore errors for log and cache devices, simply free the private data.
100651ece835Seschrock  */
100751ece835Seschrock static void
10080430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio)
100951ece835Seschrock {
101051ece835Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
101151ece835Seschrock }
101251ece835Seschrock 
101317f17c2dSbonwick /*
101417f17c2dSbonwick  * Write all even or odd labels to all leaves of the specified vdev.
101517f17c2dSbonwick  */
101617f17c2dSbonwick static void
1017e14bb325SJeff Bonwick vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
1018fa9e4066Sahrens {
1019fa9e4066Sahrens 	nvlist_t *label;
1020fa9e4066Sahrens 	vdev_phys_t *vp;
1021fa9e4066Sahrens 	char *buf;
1022fa9e4066Sahrens 	size_t buflen;
1023fa9e4066Sahrens 
1024e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1025e14bb325SJeff Bonwick 		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1028fa9e4066Sahrens 		return;
1029fa9e4066Sahrens 
1030e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1031fa9e4066Sahrens 		return;
1032fa9e4066Sahrens 
1033fa9e4066Sahrens 	/*
1034fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
1035fa9e4066Sahrens 	 */
10360373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
1039fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 	buf = vp->vp_nvlist;
1042fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
1043fa9e4066Sahrens 
104417f17c2dSbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
104517f17c2dSbonwick 		for (; l < VDEV_LABELS; l += 2) {
104617f17c2dSbonwick 			vdev_label_write(zio, vd, l, vp,
104717f17c2dSbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
104817f17c2dSbonwick 			    sizeof (vdev_phys_t),
104917f17c2dSbonwick 			    vdev_label_sync_done, zio->io_private,
1050e14bb325SJeff Bonwick 			    flags | ZIO_FLAG_DONT_PROPAGATE);
105117f17c2dSbonwick 		}
105217f17c2dSbonwick 	}
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
1055fa9e4066Sahrens 	nvlist_free(label);
1056fa9e4066Sahrens }
1057fa9e4066Sahrens 
105817f17c2dSbonwick int
1059e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1060fa9e4066Sahrens {
1061e14bb325SJeff Bonwick 	list_t *dl = &spa->spa_config_dirty_list;
106217f17c2dSbonwick 	vdev_t *vd;
1063e14bb325SJeff Bonwick 	zio_t *zio;
1064fa9e4066Sahrens 	int error;
1065fa9e4066Sahrens 
1066fa9e4066Sahrens 	/*
1067e14bb325SJeff Bonwick 	 * Write the new labels to disk.
1068fa9e4066Sahrens 	 */
1069e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, flags);
1070fa9e4066Sahrens 
107117f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
107217f17c2dSbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
107317f17c2dSbonwick 		    KM_SLEEP);
107488ecc943SGeorge Wilson 
107588ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
107688ecc943SGeorge Wilson 
1077a3f829aeSBill Moore 		zio_t *vio = zio_null(zio, spa, NULL,
10780430f8daSeschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
10790430f8daSeschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
108017f17c2dSbonwick 		    good_writes, flags);
1081e14bb325SJeff Bonwick 		vdev_label_sync(vio, vd, l, txg, flags);
108217f17c2dSbonwick 		zio_nowait(vio);
1083fa9e4066Sahrens 	}
1084e14bb325SJeff Bonwick 
1085e14bb325SJeff Bonwick 	error = zio_wait(zio);
1086fa9e4066Sahrens 
10878654d025Sperrin 	/*
108817f17c2dSbonwick 	 * Flush the new labels to disk.
10898654d025Sperrin 	 */
109017f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
10918654d025Sperrin 
109217f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
109317f17c2dSbonwick 		zio_flush(zio, vd);
109417f17c2dSbonwick 
109517f17c2dSbonwick 	(void) zio_wait(zio);
1096fa9e4066Sahrens 
1097fa9e4066Sahrens 	return (error);
1098fa9e4066Sahrens }
1099fa9e4066Sahrens 
1100fa9e4066Sahrens /*
110117f17c2dSbonwick  * Sync the uberblock and any changes to the vdev configuration.
1102fa9e4066Sahrens  *
1103fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
1104fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
1105fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
1106fa9e4066Sahrens  * describe the failure semantics at each stage.
1107fa9e4066Sahrens  *
110817f17c2dSbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1109fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
1110fa9e4066Sahrens  */
1111e14bb325SJeff Bonwick int
11128956713aSEric Schrock vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard)
1113fa9e4066Sahrens {
111417f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1115fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
11160373e76bSbonwick 	vdev_t *vd;
1117fa9e4066Sahrens 	zio_t *zio;
1118e14bb325SJeff Bonwick 	int error;
1119e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1120fa9e4066Sahrens 
11218956713aSEric Schrock 	/*
11228956713aSEric Schrock 	 * Normally, we don't want to try too hard to write every label and
11238956713aSEric Schrock 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
11248956713aSEric Schrock 	 * sync process to block while we retry.  But if we can't write a
11258956713aSEric Schrock 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
11268956713aSEric Schrock 	 * bailing out and declaring the pool faulted.
11278956713aSEric Schrock 	 */
11288956713aSEric Schrock 	if (tryhard)
11298956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
11308956713aSEric Schrock 
1131fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
1132fa9e4066Sahrens 
1133fa9e4066Sahrens 	/*
113417f17c2dSbonwick 	 * If this isn't a resync due to I/O errors,
113517f17c2dSbonwick 	 * and nothing changed in this transaction group,
113617f17c2dSbonwick 	 * and the vdev configuration hasn't changed,
11370373e76bSbonwick 	 * then there's nothing to do.
1138fa9e4066Sahrens 	 */
113917f17c2dSbonwick 	if (ub->ub_txg < txg &&
114017f17c2dSbonwick 	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
1141e14bb325SJeff Bonwick 	    list_is_empty(&spa->spa_config_dirty_list))
1142e14bb325SJeff Bonwick 		return (0);
1143fa9e4066Sahrens 
1144fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
1145e14bb325SJeff Bonwick 		return (0);
1146fa9e4066Sahrens 
11470373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
11480373e76bSbonwick 
1149fa9e4066Sahrens 	/*
1150fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
1151fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
1152fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
1153fa9e4066Sahrens 	 * before any uberblock that references them.
1154fa9e4066Sahrens 	 */
115517f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
115617f17c2dSbonwick 
1157fa9e4066Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
115817f17c2dSbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
115917f17c2dSbonwick 		zio_flush(zio, vd);
116017f17c2dSbonwick 
1161fa9e4066Sahrens 	(void) zio_wait(zio);
1162fa9e4066Sahrens 
1163fa9e4066Sahrens 	/*
1164fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1165fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1166fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1167fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
116817f17c2dSbonwick 	 * which have not yet been touched, will still be valid.  We flush
116917f17c2dSbonwick 	 * the new labels to disk to ensure that all even-label updates
117017f17c2dSbonwick 	 * are committed to stable storage before the uberblock update.
1171fa9e4066Sahrens 	 */
1172e14bb325SJeff Bonwick 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
1173e14bb325SJeff Bonwick 		return (error);
1174fa9e4066Sahrens 
1175fa9e4066Sahrens 	/*
1176e14bb325SJeff Bonwick 	 * Sync the uberblocks to all vdevs in svd[].
11770373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
11780373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1179fa9e4066Sahrens 	 *
1180fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1181fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1182fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1183fa9e4066Sahrens 	 *	to that uberblock.
1184fa9e4066Sahrens 	 *
1185fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1186fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1187fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1188fa9e4066Sahrens 	 *	to the new uberblocks.
1189fa9e4066Sahrens 	 */
1190e14bb325SJeff Bonwick 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
1191e14bb325SJeff Bonwick 		return (error);
1192fa9e4066Sahrens 
1193fa9e4066Sahrens 	/*
1194fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1195fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1196fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1197fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1198fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
119917f17c2dSbonwick 	 * all labels will be brought up to date.  We flush the new labels
120017f17c2dSbonwick 	 * to disk to ensure that all odd-label updates are committed to
120117f17c2dSbonwick 	 * stable storage before the next transaction group begins.
1202fa9e4066Sahrens 	 */
1203e14bb325SJeff Bonwick 	return (vdev_label_sync_list(spa, 1, txg, flags));
1204fa9e4066Sahrens }
1205