xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision b4952e17e8858d3225793b28788278de9fe6038d)
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  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens /*
28fa9e4066Sahrens  * Virtual Device Labels
29fa9e4066Sahrens  * ---------------------
30fa9e4066Sahrens  *
31fa9e4066Sahrens  * The vdev label serves several distinct purposes:
32fa9e4066Sahrens  *
33fa9e4066Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
34fa9e4066Sahrens  *	   identity within the pool.
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * 	2. Verify that all the devices given in a configuration are present
37fa9e4066Sahrens  *         within the pool.
38fa9e4066Sahrens  *
39fa9e4066Sahrens  * 	3. Determine the uberblock for the pool.
40fa9e4066Sahrens  *
41fa9e4066Sahrens  * 	4. In case of an import operation, determine the configuration of the
42fa9e4066Sahrens  *         toplevel vdev of which it is a part.
43fa9e4066Sahrens  *
44fa9e4066Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
45fa9e4066Sahrens  *         provide enough information to the administrator to determine which
46fa9e4066Sahrens  *         devices are missing.
47fa9e4066Sahrens  *
48fa9e4066Sahrens  * It is important to note that while the kernel is responsible for writing the
49fa9e4066Sahrens  * label, it only consumes the information in the first three cases.  The
50fa9e4066Sahrens  * latter information is only consumed in userland when determining the
51fa9e4066Sahrens  * configuration to import a pool.
52fa9e4066Sahrens  *
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * Label Organization
55fa9e4066Sahrens  * ------------------
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * Before describing the contents of the label, it's important to understand how
58fa9e4066Sahrens  * the labels are written and updated with respect to the uberblock.
59fa9e4066Sahrens  *
60fa9e4066Sahrens  * When the pool configuration is altered, either because it was newly created
61fa9e4066Sahrens  * or a device was added, we want to update all the labels such that we can deal
62fa9e4066Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
63fa9e4066Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
643d7072f8Seschrock  * labels and an uberblock with the following transaction groups:
65fa9e4066Sahrens  *
66fa9e4066Sahrens  *              L1          UB          L2
67fa9e4066Sahrens  *           +------+    +------+    +------+
68fa9e4066Sahrens  *           |      |    |      |    |      |
69fa9e4066Sahrens  *           | t10  |    | t10  |    | t10  |
70fa9e4066Sahrens  *           |      |    |      |    |      |
71fa9e4066Sahrens  *           +------+    +------+    +------+
72fa9e4066Sahrens  *
73fa9e4066Sahrens  * In this stable state, the labels and the uberblock were all updated within
74fa9e4066Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
75fa9e4066Sahrens  * that we can detect when we fail partway through writing the label.
76fa9e4066Sahrens  *
77fa9e4066Sahrens  * In order to identify which labels are valid, the labels are written in the
78fa9e4066Sahrens  * following manner:
79fa9e4066Sahrens  *
80fa9e4066Sahrens  * 	1. For each vdev, update 'L1' to the new label
81fa9e4066Sahrens  * 	2. Update the uberblock
82fa9e4066Sahrens  * 	3. For each vdev, update 'L2' to the new label
83fa9e4066Sahrens  *
84fa9e4066Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
85fa9e4066Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
86fa9e4066Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
87fa9e4066Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
88fa9e4066Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
89fa9e4066Sahrens  * therefore L1 is valid.
90fa9e4066Sahrens  *
91fa9e4066Sahrens  * Another added complexity is that not every label is updated when the config
92fa9e4066Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
93fa9e4066Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
94fa9e4066Sahrens  * be older than the pool uberblock, because the necessary information is stored
95fa9e4066Sahrens  * on another vdev.
96fa9e4066Sahrens  *
97fa9e4066Sahrens  *
98fa9e4066Sahrens  * On-disk Format
99fa9e4066Sahrens  * --------------
100fa9e4066Sahrens  *
101fa9e4066Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
102fa9e4066Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
103fa9e4066Sahrens  * VTOC disk labels, but is otherwise ignored.
104fa9e4066Sahrens  *
105fa9e4066Sahrens  * The first half of the label is a packed nvlist which contains pool wide
106fa9e4066Sahrens  * properties, per-vdev properties, and configuration information.  It is
107fa9e4066Sahrens  * described in more detail below.
108fa9e4066Sahrens  *
109fa9e4066Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
110fa9e4066Sahrens  * These uberblocks are updated whenever a transaction group is committed,
111fa9e4066Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
112fa9e4066Sahrens  * vdev for the 'best' uberblock.
113fa9e4066Sahrens  *
114fa9e4066Sahrens  *
115fa9e4066Sahrens  * Configuration Information
116fa9e4066Sahrens  * -------------------------
117fa9e4066Sahrens  *
118fa9e4066Sahrens  * The nvlist describing the pool and vdev contains the following elements:
119fa9e4066Sahrens  *
120fa9e4066Sahrens  * 	version		ZFS on-disk version
121fa9e4066Sahrens  * 	name		Pool name
122fa9e4066Sahrens  * 	state		Pool state
123fa9e4066Sahrens  * 	txg		Transaction group in which this label was written
124fa9e4066Sahrens  * 	pool_guid	Unique identifier for this pool
125fa9e4066Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
126ad135b5dSChristopher Siden  *	features_for_read
127ad135b5dSChristopher Siden  *			An nvlist of the features necessary for reading the MOS.
128fa9e4066Sahrens  *
129fa9e4066Sahrens  * Each leaf device label also contains the following:
130fa9e4066Sahrens  *
131fa9e4066Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
132fa9e4066Sahrens  * 	guid		Unique ID for the leaf vdev
133fa9e4066Sahrens  *
134fa9e4066Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
135fa9e4066Sahrens  */
136fa9e4066Sahrens 
137fa9e4066Sahrens #include <sys/zfs_context.h>
138fa9e4066Sahrens #include <sys/spa.h>
139fa9e4066Sahrens #include <sys/spa_impl.h>
140fa9e4066Sahrens #include <sys/dmu.h>
141fa9e4066Sahrens #include <sys/zap.h>
142fa9e4066Sahrens #include <sys/vdev.h>
143fa9e4066Sahrens #include <sys/vdev_impl.h>
144fa9e4066Sahrens #include <sys/uberblock_impl.h>
145fa9e4066Sahrens #include <sys/metaslab.h>
146fa9e4066Sahrens #include <sys/zio.h>
1473f9d6ad7SLin Ling #include <sys/dsl_scan.h>
148fa9e4066Sahrens #include <sys/fs/zfs.h>
149fa9e4066Sahrens 
150fa9e4066Sahrens /*
151fa9e4066Sahrens  * Basic routines to read and write from a vdev label.
152fa9e4066Sahrens  * Used throughout the rest of this file.
153fa9e4066Sahrens  */
154fa9e4066Sahrens uint64_t
155fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
156fa9e4066Sahrens {
157ecc2d604Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
158e7437265Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
159ecc2d604Sbonwick 
160fa9e4066Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
161fa9e4066Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
162fa9e4066Sahrens }
163fa9e4066Sahrens 
16421bf64a7Sgw /*
16521bf64a7Sgw  * Returns back the vdev label associated with the passed in offset.
16621bf64a7Sgw  */
16721bf64a7Sgw int
16821bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset)
16921bf64a7Sgw {
17021bf64a7Sgw 	int l;
17121bf64a7Sgw 
17221bf64a7Sgw 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
17321bf64a7Sgw 		offset -= psize - VDEV_LABEL_END_SIZE;
17421bf64a7Sgw 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
17521bf64a7Sgw 	}
17621bf64a7Sgw 	l = offset / sizeof (vdev_label_t);
17721bf64a7Sgw 	return (l < VDEV_LABELS ? l : -1);
17821bf64a7Sgw }
17921bf64a7Sgw 
180fa9e4066Sahrens static void
181fa9e4066Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
182e14bb325SJeff Bonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
183fa9e4066Sahrens {
184e14bb325SJeff Bonwick 	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
185e14bb325SJeff Bonwick 	    SCL_STATE_ALL);
186e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
187fa9e4066Sahrens 
188fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
189fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
190fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
191e14bb325SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
192fa9e4066Sahrens }
193fa9e4066Sahrens 
194fa9e4066Sahrens static void
195fa9e4066Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
19617f17c2dSbonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
197fa9e4066Sahrens {
198e14bb325SJeff Bonwick 	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
199e14bb325SJeff Bonwick 	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
200e14bb325SJeff Bonwick 	    (SCL_CONFIG | SCL_STATE) &&
201e14bb325SJeff Bonwick 	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
202e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
203fa9e4066Sahrens 
204fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
205fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
206fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
20717f17c2dSbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
208fa9e4066Sahrens }
209fa9e4066Sahrens 
210fa9e4066Sahrens /*
211fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
212fa9e4066Sahrens  */
213fa9e4066Sahrens nvlist_t *
21499653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
2153f9d6ad7SLin Ling     vdev_config_flag_t flags)
216fa9e4066Sahrens {
217fa9e4066Sahrens 	nvlist_t *nv = NULL;
218fa9e4066Sahrens 
219*b4952e17SGeorge Wilson 	nv = fnvlist_alloc();
220fa9e4066Sahrens 
221*b4952e17SGeorge Wilson 	fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
2223f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
223*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
224*b4952e17SGeorge Wilson 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
225fa9e4066Sahrens 
226fa9e4066Sahrens 	if (vd->vdev_path != NULL)
227*b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
228fa9e4066Sahrens 
229fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
230*b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
231fa9e4066Sahrens 
2323d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
233*b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
234*b4952e17SGeorge Wilson 		    vd->vdev_physpath);
2353d7072f8Seschrock 
2366809eb4eSEric Schrock 	if (vd->vdev_fru != NULL)
237*b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
2386809eb4eSEric Schrock 
23999653d4eSeschrock 	if (vd->vdev_nparity != 0) {
24099653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
24199653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
24299653d4eSeschrock 
24399653d4eSeschrock 		/*
24499653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
24599653d4eSeschrock 		 * into a crufty old storage pool.
24699653d4eSeschrock 		 */
24799653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
248f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 2 &&
249f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
250f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 3 &&
251f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
25299653d4eSeschrock 
25399653d4eSeschrock 		/*
25499653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
25599653d4eSeschrock 		 * that only support a single parity device -- older software
25699653d4eSeschrock 		 * will just ignore it.
25799653d4eSeschrock 		 */
258*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
25999653d4eSeschrock 	}
26099653d4eSeschrock 
261afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
262*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
263*b4952e17SGeorge Wilson 		    vd->vdev_wholedisk);
264afefbcddSeschrock 
265ea8dc4b6Seschrock 	if (vd->vdev_not_present)
266*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
267ea8dc4b6Seschrock 
26899653d4eSeschrock 	if (vd->vdev_isspare)
269*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
27099653d4eSeschrock 
2713f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
2723f9d6ad7SLin Ling 	    vd == vd->vdev_top) {
273*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
274*b4952e17SGeorge Wilson 		    vd->vdev_ms_array);
275*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
276*b4952e17SGeorge Wilson 		    vd->vdev_ms_shift);
277*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
278*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
279*b4952e17SGeorge Wilson 		    vd->vdev_asize);
280*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
2813f9d6ad7SLin Ling 		if (vd->vdev_removing)
282*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
283*b4952e17SGeorge Wilson 			    vd->vdev_removing);
284fa9e4066Sahrens 	}
285fa9e4066Sahrens 
2868ad4d6ddSJeff Bonwick 	if (vd->vdev_dtl_smo.smo_object != 0)
287*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
288*b4952e17SGeorge Wilson 		    vd->vdev_dtl_smo.smo_object);
289fa9e4066Sahrens 
29088ecc943SGeorge Wilson 	if (vd->vdev_crtxg)
291*b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
29288ecc943SGeorge Wilson 
293fa9e4066Sahrens 	if (getstats) {
294fa9e4066Sahrens 		vdev_stat_t vs;
2953f9d6ad7SLin Ling 		pool_scan_stat_t ps;
2963f9d6ad7SLin Ling 
297fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
298*b4952e17SGeorge Wilson 		fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
299*b4952e17SGeorge Wilson 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t));
3003f9d6ad7SLin Ling 
3013f9d6ad7SLin Ling 		/* provide either current or previous scan information */
3023f9d6ad7SLin Ling 		if (spa_scan_get_stats(spa, &ps) == 0) {
303*b4952e17SGeorge Wilson 			fnvlist_add_uint64_array(nv,
3043f9d6ad7SLin Ling 			    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
305*b4952e17SGeorge Wilson 			    sizeof (pool_scan_stat_t) / sizeof (uint64_t));
3063f9d6ad7SLin Ling 		}
307fa9e4066Sahrens 	}
308fa9e4066Sahrens 
309fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
310fa9e4066Sahrens 		nvlist_t **child;
3113f9d6ad7SLin Ling 		int c, idx;
312fa9e4066Sahrens 
31388ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
31488ecc943SGeorge Wilson 
315fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
316fa9e4066Sahrens 		    KM_SLEEP);
317fa9e4066Sahrens 
3183f9d6ad7SLin Ling 		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
3193f9d6ad7SLin Ling 			vdev_t *cvd = vd->vdev_child[c];
3203f9d6ad7SLin Ling 
3213f9d6ad7SLin Ling 			/*
3223f9d6ad7SLin Ling 			 * If we're generating an nvlist of removing
3233f9d6ad7SLin Ling 			 * vdevs then skip over any device which is
3243f9d6ad7SLin Ling 			 * not being removed.
3253f9d6ad7SLin Ling 			 */
3263f9d6ad7SLin Ling 			if ((flags & VDEV_CONFIG_REMOVING) &&
3273f9d6ad7SLin Ling 			    !cvd->vdev_removing)
3283f9d6ad7SLin Ling 				continue;
329fa9e4066Sahrens 
3303f9d6ad7SLin Ling 			child[idx++] = vdev_config_generate(spa, cvd,
3313f9d6ad7SLin Ling 			    getstats, flags);
3323f9d6ad7SLin Ling 		}
3333f9d6ad7SLin Ling 
3343f9d6ad7SLin Ling 		if (idx) {
335*b4952e17SGeorge Wilson 			fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
336*b4952e17SGeorge Wilson 			    child, idx);
3373f9d6ad7SLin Ling 		}
338fa9e4066Sahrens 
3393f9d6ad7SLin Ling 		for (c = 0; c < idx; c++)
340fa9e4066Sahrens 			nvlist_free(child[c]);
341fa9e4066Sahrens 
342fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
343441d80aaSlling 
344441d80aaSlling 	} else {
345069f55e2SEric Schrock 		const char *aux = NULL;
346069f55e2SEric Schrock 
347ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
348*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
349*b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0)
350*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
351*b4952e17SGeorge Wilson 			    vd->vdev_resilver_txg);
3523d7072f8Seschrock 		if (vd->vdev_faulted)
353*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
3543d7072f8Seschrock 		if (vd->vdev_degraded)
355*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
3563d7072f8Seschrock 		if (vd->vdev_removed)
357*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
3583d7072f8Seschrock 		if (vd->vdev_unspare)
359*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
36088ecc943SGeorge Wilson 		if (vd->vdev_ishole)
361*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
362069f55e2SEric Schrock 
363069f55e2SEric Schrock 		switch (vd->vdev_stat.vs_aux) {
364069f55e2SEric Schrock 		case VDEV_AUX_ERR_EXCEEDED:
365069f55e2SEric Schrock 			aux = "err_exceeded";
366069f55e2SEric Schrock 			break;
367069f55e2SEric Schrock 
368069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
369069f55e2SEric Schrock 			aux = "external";
370069f55e2SEric Schrock 			break;
371069f55e2SEric Schrock 		}
372069f55e2SEric Schrock 
373069f55e2SEric Schrock 		if (aux != NULL)
374*b4952e17SGeorge Wilson 			fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
3751195e687SMark J Musante 
3761195e687SMark J Musante 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
377*b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
378*b4952e17SGeorge Wilson 			    vd->vdev_orig_guid);
3791195e687SMark J Musante 		}
380fa9e4066Sahrens 	}
381fa9e4066Sahrens 
382fa9e4066Sahrens 	return (nv);
383fa9e4066Sahrens }
384fa9e4066Sahrens 
38588ecc943SGeorge Wilson /*
38688ecc943SGeorge Wilson  * Generate a view of the top-level vdevs.  If we currently have holes
38788ecc943SGeorge Wilson  * in the namespace, then generate an array which contains a list of holey
38888ecc943SGeorge Wilson  * vdevs.  Additionally, add the number of top-level children that currently
38988ecc943SGeorge Wilson  * exist.
39088ecc943SGeorge Wilson  */
39188ecc943SGeorge Wilson void
39288ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config)
39388ecc943SGeorge Wilson {
39488ecc943SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
39588ecc943SGeorge Wilson 	uint64_t *array;
3963f9d6ad7SLin Ling 	uint_t c, idx;
39788ecc943SGeorge Wilson 
39888ecc943SGeorge Wilson 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
39988ecc943SGeorge Wilson 
4003f9d6ad7SLin Ling 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
40188ecc943SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
40288ecc943SGeorge Wilson 
40388ecc943SGeorge Wilson 		if (tvd->vdev_ishole)
40488ecc943SGeorge Wilson 			array[idx++] = c;
40588ecc943SGeorge Wilson 	}
40688ecc943SGeorge Wilson 
407312c6e15SGeorge Wilson 	if (idx) {
408312c6e15SGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
409312c6e15SGeorge Wilson 		    array, idx) == 0);
410312c6e15SGeorge Wilson 	}
411312c6e15SGeorge Wilson 
41288ecc943SGeorge Wilson 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
41388ecc943SGeorge Wilson 	    rvd->vdev_children) == 0);
41488ecc943SGeorge Wilson 
41588ecc943SGeorge Wilson 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
41688ecc943SGeorge Wilson }
41788ecc943SGeorge Wilson 
418ad135b5dSChristopher Siden /*
419dfbb9432SGeorge Wilson  * Returns the configuration from the label of the given vdev. For vdevs
420dfbb9432SGeorge Wilson  * which don't have a txg value stored on their label (i.e. spares/cache)
421dfbb9432SGeorge Wilson  * or have not been completely initialized (txg = 0) just return
422dfbb9432SGeorge Wilson  * the configuration from the first valid label we find. Otherwise,
423dfbb9432SGeorge Wilson  * find the most up-to-date label that does not exceed the specified
424dfbb9432SGeorge Wilson  * 'txg' value.
425ad135b5dSChristopher Siden  */
426fa9e4066Sahrens nvlist_t *
427dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg)
428fa9e4066Sahrens {
4290373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
430fa9e4066Sahrens 	nvlist_t *config = NULL;
431fa9e4066Sahrens 	vdev_phys_t *vp;
432fa9e4066Sahrens 	zio_t *zio;
433dfbb9432SGeorge Wilson 	uint64_t best_txg = 0;
434dfbb9432SGeorge Wilson 	int error = 0;
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++) {
447dfbb9432SGeorge Wilson 		nvlist_t *label = NULL;
448fa9e4066Sahrens 
449e14bb325SJeff Bonwick 		zio = zio_root(spa, NULL, NULL, flags);
450fa9e4066Sahrens 
451fa9e4066Sahrens 		vdev_label_read(zio, vd, l, vp,
452fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
453e14bb325SJeff Bonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
454fa9e4066Sahrens 
455fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
456fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
457dfbb9432SGeorge Wilson 		    &label, 0) == 0) {
458dfbb9432SGeorge Wilson 			uint64_t label_txg = 0;
459dfbb9432SGeorge Wilson 
460dfbb9432SGeorge Wilson 			/*
461dfbb9432SGeorge Wilson 			 * Auxiliary vdevs won't have txg values in their
462dfbb9432SGeorge Wilson 			 * labels and newly added vdevs may not have been
463dfbb9432SGeorge Wilson 			 * completely initialized so just return the
464dfbb9432SGeorge Wilson 			 * configuration from the first valid label we
465dfbb9432SGeorge Wilson 			 * encounter.
466dfbb9432SGeorge Wilson 			 */
467dfbb9432SGeorge Wilson 			error = nvlist_lookup_uint64(label,
468dfbb9432SGeorge Wilson 			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
469dfbb9432SGeorge Wilson 			if ((error || label_txg == 0) && !config) {
470dfbb9432SGeorge Wilson 				config = label;
471dfbb9432SGeorge Wilson 				break;
472dfbb9432SGeorge Wilson 			} else if (label_txg <= txg && label_txg > best_txg) {
473dfbb9432SGeorge Wilson 				best_txg = label_txg;
474dfbb9432SGeorge Wilson 				nvlist_free(config);
475dfbb9432SGeorge Wilson 				config = fnvlist_dup(label);
476dfbb9432SGeorge Wilson 			}
477dfbb9432SGeorge Wilson 		}
478fa9e4066Sahrens 
479dfbb9432SGeorge Wilson 		if (label != NULL) {
480dfbb9432SGeorge Wilson 			nvlist_free(label);
481dfbb9432SGeorge Wilson 			label = NULL;
482fa9e4066Sahrens 		}
483fa9e4066Sahrens 	}
484fa9e4066Sahrens 
4858956713aSEric Schrock 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
4868956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
4878956713aSEric Schrock 		goto retry;
4888956713aSEric Schrock 	}
4898956713aSEric Schrock 
490fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
491fa9e4066Sahrens 
492fa9e4066Sahrens 	return (config);
493fa9e4066Sahrens }
494fa9e4066Sahrens 
49539c23413Seschrock /*
49639c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
49739c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
49839c23413Seschrock  */
49939c23413Seschrock static boolean_t
50039c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
501fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
50239c23413Seschrock {
50339c23413Seschrock 	spa_t *spa = vd->vdev_spa;
50439c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
50539c23413Seschrock 	uint64_t vdtxg = 0;
50639c23413Seschrock 	nvlist_t *label;
50739c23413Seschrock 
50839c23413Seschrock 	if (spare_guid)
50939c23413Seschrock 		*spare_guid = 0ULL;
510fa94a07fSbrendan 	if (l2cache_guid)
511fa94a07fSbrendan 		*l2cache_guid = 0ULL;
51239c23413Seschrock 
51339c23413Seschrock 	/*
51439c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
51539c23413Seschrock 	 */
516dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
51739c23413Seschrock 		return (B_FALSE);
51839c23413Seschrock 
51939c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
52039c23413Seschrock 	    &vdtxg);
52139c23413Seschrock 
52239c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
52339c23413Seschrock 	    &state) != 0 ||
52439c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
52539c23413Seschrock 	    &device_guid) != 0) {
52639c23413Seschrock 		nvlist_free(label);
52739c23413Seschrock 		return (B_FALSE);
52839c23413Seschrock 	}
52939c23413Seschrock 
530fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
53139c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
53239c23413Seschrock 	    &pool_guid) != 0 ||
53339c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
53439c23413Seschrock 	    &txg) != 0)) {
53539c23413Seschrock 		nvlist_free(label);
53639c23413Seschrock 		return (B_FALSE);
53739c23413Seschrock 	}
53839c23413Seschrock 
53939c23413Seschrock 	nvlist_free(label);
54039c23413Seschrock 
54139c23413Seschrock 	/*
54239c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
54339c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
54439c23413Seschrock 	 * spare (which we check for later on).
54539c23413Seschrock 	 */
546fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
54739c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
54889a89ebfSlling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
549fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
55039c23413Seschrock 		return (B_FALSE);
55139c23413Seschrock 
55239c23413Seschrock 	/*
55339c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
55439c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
55539c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
55639c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
55739c23413Seschrock 	 * transaction.
55839c23413Seschrock 	 */
559fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
560fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
56139c23413Seschrock 		return (B_TRUE);
56239c23413Seschrock 
56339c23413Seschrock 	/*
56439c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
56539c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
566fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
56739c23413Seschrock 	 */
56889a89ebfSlling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
56939c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
57039c23413Seschrock 		if (spare_guid)
57139c23413Seschrock 			*spare_guid = device_guid;
57239c23413Seschrock 
57339c23413Seschrock 		switch (reason) {
57439c23413Seschrock 		case VDEV_LABEL_CREATE:
575fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
57639c23413Seschrock 			return (B_TRUE);
57739c23413Seschrock 
57839c23413Seschrock 		case VDEV_LABEL_REPLACE:
57939c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
58039c23413Seschrock 			    spare_pool != 0ULL);
58139c23413Seschrock 
58239c23413Seschrock 		case VDEV_LABEL_SPARE:
58339c23413Seschrock 			return (spa_has_spare(spa, device_guid));
58439c23413Seschrock 		}
58539c23413Seschrock 	}
58639c23413Seschrock 
587fa94a07fSbrendan 	/*
588fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
589fa94a07fSbrendan 	 */
590fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
591fa94a07fSbrendan 		return (B_TRUE);
592fa94a07fSbrendan 
593f9af39baSGeorge Wilson 	/*
594f9af39baSGeorge Wilson 	 * We can't rely on a pool's state if it's been imported
595f9af39baSGeorge Wilson 	 * read-only.  Instead we look to see if the pools is marked
596f9af39baSGeorge Wilson 	 * read-only in the namespace and set the state to active.
597f9af39baSGeorge Wilson 	 */
598f9af39baSGeorge Wilson 	if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
599f9af39baSGeorge Wilson 	    spa_mode(spa) == FREAD)
600f9af39baSGeorge Wilson 		state = POOL_STATE_ACTIVE;
601f9af39baSGeorge Wilson 
60239c23413Seschrock 	/*
60339c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
60439c23413Seschrock 	 * pool on the system.
60539c23413Seschrock 	 */
60639c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
60739c23413Seschrock }
60839c23413Seschrock 
60939c23413Seschrock /*
61039c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
61139c23413Seschrock  * use, and writable.  We put down an initial label which we will later
61239c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
61339c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
61439c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
61539c23413Seschrock  * itself.
61639c23413Seschrock  */
61739c23413Seschrock int
61839c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
619fa9e4066Sahrens {
620fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
621fa9e4066Sahrens 	nvlist_t *label;
622fa9e4066Sahrens 	vdev_phys_t *vp;
623f83ffe1aSLin Ling 	char *pad2;
624ecc2d604Sbonwick 	uberblock_t *ub;
625fa9e4066Sahrens 	zio_t *zio;
626fa9e4066Sahrens 	char *buf;
627fa9e4066Sahrens 	size_t buflen;
628fa9e4066Sahrens 	int error;
629fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
630e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
631fa9e4066Sahrens 
632e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6330373e76bSbonwick 
634e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
63539c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
63639c23413Seschrock 		    crtxg, reason)) != 0)
637fa9e4066Sahrens 			return (error);
638fa9e4066Sahrens 
63988ecc943SGeorge Wilson 	/* Track the creation time for this vdev */
64088ecc943SGeorge Wilson 	vd->vdev_crtxg = crtxg;
64188ecc943SGeorge Wilson 
642fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
643fa9e4066Sahrens 		return (0);
644fa9e4066Sahrens 
645fa9e4066Sahrens 	/*
64639c23413Seschrock 	 * Dead vdevs cannot be initialized.
647fa9e4066Sahrens 	 */
648fa9e4066Sahrens 	if (vdev_is_dead(vd))
649be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
650fa9e4066Sahrens 
651fa9e4066Sahrens 	/*
65239c23413Seschrock 	 * Determine if the vdev is in use.
653fa9e4066Sahrens 	 */
6541195e687SMark J Musante 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
655fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
656be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
65739c23413Seschrock 
65839c23413Seschrock 	/*
659fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
660fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
661fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
662fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
66339c23413Seschrock 	 */
664fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
665fa94a07fSbrendan 	    spare_guid != 0ULL) {
66631157203SJeff Bonwick 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
66799653d4eSeschrock 
66831157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
66931157203SJeff Bonwick 
67031157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
67131157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
67239c23413Seschrock 
67399653d4eSeschrock 		/*
67439c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
67539c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
6763d7072f8Seschrock 		 * labeled appropriately and we can just return.
67799653d4eSeschrock 		 */
67839c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
67939c23413Seschrock 			return (0);
6801195e687SMark J Musante 		ASSERT(reason == VDEV_LABEL_REPLACE ||
6811195e687SMark J Musante 		    reason == VDEV_LABEL_SPLIT);
682fa9e4066Sahrens 	}
683fa9e4066Sahrens 
684fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
685fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
68631157203SJeff Bonwick 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
68731157203SJeff Bonwick 
68831157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
689fa94a07fSbrendan 
69031157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
69131157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
692fa94a07fSbrendan 
693fa94a07fSbrendan 		/*
694fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
695fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
696fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
697fa94a07fSbrendan 		 */
698fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
699fa94a07fSbrendan 			return (0);
700fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
701fa94a07fSbrendan 	}
702fa94a07fSbrendan 
703fa9e4066Sahrens 	/*
70439c23413Seschrock 	 * Initialize its label.
705fa9e4066Sahrens 	 */
706fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
707fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
708fa9e4066Sahrens 
709fa9e4066Sahrens 	/*
710fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
711fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
712fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
713fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
714fa9e4066Sahrens 	 */
71539c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
71639c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
71739c23413Seschrock 		/*
71839c23413Seschrock 		 * For inactive hot spares, we generate a special label that
71939c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
72039c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
72139c23413Seschrock 		 * active hot spare (in which case we want to revert the
72239c23413Seschrock 		 * labels).
72339c23413Seschrock 		 */
72499653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
72599653d4eSeschrock 
72699653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
72799653d4eSeschrock 		    spa_version(spa)) == 0);
72899653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
72999653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
73099653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
73199653d4eSeschrock 		    vd->vdev_guid) == 0);
732fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
733fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
734fa94a07fSbrendan 		/*
735fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
736fa94a07fSbrendan 		 */
737fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
738fa94a07fSbrendan 
739fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
740fa94a07fSbrendan 		    spa_version(spa)) == 0);
741fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
742fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
743fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
744fa94a07fSbrendan 		    vd->vdev_guid) == 0);
74599653d4eSeschrock 	} else {
7461195e687SMark J Musante 		uint64_t txg = 0ULL;
7471195e687SMark J Musante 
7481195e687SMark J Musante 		if (reason == VDEV_LABEL_SPLIT)
7491195e687SMark J Musante 			txg = spa->spa_uberblock.ub_txg;
7501195e687SMark J Musante 		label = spa_config_generate(spa, vd, txg, B_FALSE);
75199653d4eSeschrock 
75299653d4eSeschrock 		/*
75399653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
75499653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
75599653d4eSeschrock 		 * fail.
75699653d4eSeschrock 		 */
75799653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
75899653d4eSeschrock 		    crtxg) == 0);
75999653d4eSeschrock 	}
760fa9e4066Sahrens 
761fa9e4066Sahrens 	buf = vp->vp_nvlist;
762fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
763fa9e4066Sahrens 
764a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
765a75573b6Smmusante 	if (error != 0) {
766fa9e4066Sahrens 		nvlist_free(label);
767fa9e4066Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
768a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
769a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
770fa9e4066Sahrens 	}
771fa9e4066Sahrens 
772fa9e4066Sahrens 	/*
773fa9e4066Sahrens 	 * Initialize uberblock template.
774fa9e4066Sahrens 	 */
775f64c0e34SEric Taylor 	ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
776f64c0e34SEric Taylor 	bzero(ub, VDEV_UBERBLOCK_RING);
777ecc2d604Sbonwick 	*ub = spa->spa_uberblock;
778ecc2d604Sbonwick 	ub->ub_txg = 0;
779fa9e4066Sahrens 
780f83ffe1aSLin Ling 	/* Initialize the 2nd padding area. */
781f83ffe1aSLin Ling 	pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
782f83ffe1aSLin Ling 	bzero(pad2, VDEV_PAD_SIZE);
783f83ffe1aSLin Ling 
784fa9e4066Sahrens 	/*
785fa9e4066Sahrens 	 * Write everything in parallel.
786fa9e4066Sahrens 	 */
7878956713aSEric Schrock retry:
78817f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
789fa9e4066Sahrens 
790e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
791fa9e4066Sahrens 
792fa9e4066Sahrens 		vdev_label_write(zio, vd, l, vp,
793fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
79417f17c2dSbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
795fa9e4066Sahrens 
796f83ffe1aSLin Ling 		/*
797f83ffe1aSLin Ling 		 * Skip the 1st padding area.
798f83ffe1aSLin Ling 		 * Zero out the 2nd padding area where it might have
799f83ffe1aSLin Ling 		 * left over data from previous filesystem format.
800f83ffe1aSLin Ling 		 */
801f83ffe1aSLin Ling 		vdev_label_write(zio, vd, l, pad2,
802f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2),
803f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, NULL, NULL, flags);
804f83ffe1aSLin Ling 
805f64c0e34SEric Taylor 		vdev_label_write(zio, vd, l, ub,
806f64c0e34SEric Taylor 		    offsetof(vdev_label_t, vl_uberblock),
807f64c0e34SEric Taylor 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
808fa9e4066Sahrens 	}
809fa9e4066Sahrens 
810fa9e4066Sahrens 	error = zio_wait(zio);
811fa9e4066Sahrens 
8128956713aSEric Schrock 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
8138956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
8148956713aSEric Schrock 		goto retry;
8158956713aSEric Schrock 	}
8168956713aSEric Schrock 
817fa9e4066Sahrens 	nvlist_free(label);
818f83ffe1aSLin Ling 	zio_buf_free(pad2, VDEV_PAD_SIZE);
819f64c0e34SEric Taylor 	zio_buf_free(ub, VDEV_UBERBLOCK_RING);
820fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
821fa9e4066Sahrens 
82239c23413Seschrock 	/*
82339c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
8243d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
825fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
826fa94a07fSbrendan 	 * level 2 ARC devices.
82739c23413Seschrock 	 */
82839c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
82939c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
83089a89ebfSlling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
83139c23413Seschrock 		spa_spare_add(vd);
83299653d4eSeschrock 
833fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
834fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
835fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
836fa94a07fSbrendan 		spa_l2cache_add(vd);
837fa94a07fSbrendan 
83839c23413Seschrock 	return (error);
83999653d4eSeschrock }
84099653d4eSeschrock 
841fa9e4066Sahrens /*
842fa9e4066Sahrens  * ==========================================================================
843fa9e4066Sahrens  * uberblock load/sync
844fa9e4066Sahrens  * ==========================================================================
845fa9e4066Sahrens  */
846fa9e4066Sahrens 
847fa9e4066Sahrens /*
848fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
849fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
850fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
851fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
852fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
853ad135b5dSChristopher Siden  * the missing replica comes back, then for a few seconds we'll have two
854fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
855fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
856fa9e4066Sahrens  */
857fa9e4066Sahrens static int
858fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
859fa9e4066Sahrens {
860fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
861fa9e4066Sahrens 		return (-1);
862fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
863fa9e4066Sahrens 		return (1);
864fa9e4066Sahrens 
865fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
866fa9e4066Sahrens 		return (-1);
867fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
868fa9e4066Sahrens 		return (1);
869fa9e4066Sahrens 
870fa9e4066Sahrens 	return (0);
871fa9e4066Sahrens }
872fa9e4066Sahrens 
873ad135b5dSChristopher Siden struct ubl_cbdata {
874ad135b5dSChristopher Siden 	uberblock_t	*ubl_ubbest;	/* Best uberblock */
875ad135b5dSChristopher Siden 	vdev_t		*ubl_vd;	/* vdev associated with the above */
876ad135b5dSChristopher Siden };
877ad135b5dSChristopher Siden 
878fa9e4066Sahrens static void
879fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
880fa9e4066Sahrens {
881ad135b5dSChristopher Siden 	vdev_t *vd = zio->io_vd;
882468c413aSTim Haley 	spa_t *spa = zio->io_spa;
883e14bb325SJeff Bonwick 	zio_t *rio = zio->io_private;
884ecc2d604Sbonwick 	uberblock_t *ub = zio->io_data;
885ad135b5dSChristopher Siden 	struct ubl_cbdata *cbp = rio->io_private;
886fa9e4066Sahrens 
887ad135b5dSChristopher Siden 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
888fa9e4066Sahrens 
889ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
890e14bb325SJeff Bonwick 		mutex_enter(&rio->io_lock);
891468c413aSTim Haley 		if (ub->ub_txg <= spa->spa_load_max_txg &&
892ad135b5dSChristopher Siden 		    vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
893ad135b5dSChristopher Siden 			/*
894dfbb9432SGeorge Wilson 			 * Keep track of the vdev in which this uberblock
895dfbb9432SGeorge Wilson 			 * was found. We will use this information later
896dfbb9432SGeorge Wilson 			 * to obtain the config nvlist associated with
897ad135b5dSChristopher Siden 			 * this uberblock.
898ad135b5dSChristopher Siden 			 */
899ad135b5dSChristopher Siden 			*cbp->ubl_ubbest = *ub;
900ad135b5dSChristopher Siden 			cbp->ubl_vd = vd;
901ad135b5dSChristopher Siden 		}
902e14bb325SJeff Bonwick 		mutex_exit(&rio->io_lock);
903fa9e4066Sahrens 	}
904fa9e4066Sahrens 
905fa9e4066Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
906fa9e4066Sahrens }
907fa9e4066Sahrens 
908ad135b5dSChristopher Siden static void
909ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
910ad135b5dSChristopher Siden     struct ubl_cbdata *cbp)
911fa9e4066Sahrens {
912e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
913ad135b5dSChristopher Siden 		vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
914fa9e4066Sahrens 
915e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
916e14bb325SJeff Bonwick 		for (int l = 0; l < VDEV_LABELS; l++) {
917e14bb325SJeff Bonwick 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
918e14bb325SJeff Bonwick 				vdev_label_read(zio, vd, l,
919e14bb325SJeff Bonwick 				    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
920e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_OFFSET(vd, n),
921e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_SIZE(vd),
922e14bb325SJeff Bonwick 				    vdev_uberblock_load_done, zio, flags);
923e14bb325SJeff Bonwick 			}
924fa9e4066Sahrens 		}
925fa9e4066Sahrens 	}
926ad135b5dSChristopher Siden }
927e14bb325SJeff Bonwick 
928ad135b5dSChristopher Siden /*
929ad135b5dSChristopher Siden  * Reads the 'best' uberblock from disk along with its associated
930ad135b5dSChristopher Siden  * configuration. First, we read the uberblock array of each label of each
931ad135b5dSChristopher Siden  * vdev, keeping track of the uberblock with the highest txg in each array.
932dfbb9432SGeorge Wilson  * Then, we read the configuration from the same vdev as the best uberblock.
933ad135b5dSChristopher Siden  */
934ad135b5dSChristopher Siden void
935ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
936ad135b5dSChristopher Siden {
937ad135b5dSChristopher Siden 	zio_t *zio;
938ad135b5dSChristopher Siden 	spa_t *spa = rvd->vdev_spa;
939ad135b5dSChristopher Siden 	struct ubl_cbdata cb;
940ad135b5dSChristopher Siden 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
941ad135b5dSChristopher Siden 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
942ad135b5dSChristopher Siden 
943ad135b5dSChristopher Siden 	ASSERT(ub);
944ad135b5dSChristopher Siden 	ASSERT(config);
945ad135b5dSChristopher Siden 
946ad135b5dSChristopher Siden 	bzero(ub, sizeof (uberblock_t));
947ad135b5dSChristopher Siden 	*config = NULL;
948ad135b5dSChristopher Siden 
949ad135b5dSChristopher Siden 	cb.ubl_ubbest = ub;
950ad135b5dSChristopher Siden 	cb.ubl_vd = NULL;
951ad135b5dSChristopher Siden 
952ad135b5dSChristopher Siden 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
953ad135b5dSChristopher Siden 	zio = zio_root(spa, NULL, &cb, flags);
954ad135b5dSChristopher Siden 	vdev_uberblock_load_impl(zio, rvd, flags, &cb);
955ad135b5dSChristopher Siden 	(void) zio_wait(zio);
956dfbb9432SGeorge Wilson 
957dfbb9432SGeorge Wilson 	/*
958dfbb9432SGeorge Wilson 	 * It's possible that the best uberblock was discovered on a label
959dfbb9432SGeorge Wilson 	 * that has a configuration which was written in a future txg.
960dfbb9432SGeorge Wilson 	 * Search all labels on this vdev to find the configuration that
961dfbb9432SGeorge Wilson 	 * matches the txg for our uberblock.
962dfbb9432SGeorge Wilson 	 */
963dfbb9432SGeorge Wilson 	if (cb.ubl_vd != NULL)
964dfbb9432SGeorge Wilson 		*config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
965ad135b5dSChristopher Siden 	spa_config_exit(spa, SCL_ALL, FTAG);
966fa9e4066Sahrens }
967fa9e4066Sahrens 
968fa9e4066Sahrens /*
96917f17c2dSbonwick  * On success, increment root zio's count of good writes.
9700373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
971fa9e4066Sahrens  */
972fa9e4066Sahrens static void
973fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
974fa9e4066Sahrens {
97517f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
976fa9e4066Sahrens 
9770373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
978fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
979fa9e4066Sahrens }
980fa9e4066Sahrens 
98117f17c2dSbonwick /*
98217f17c2dSbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
98317f17c2dSbonwick  */
984fa9e4066Sahrens static void
985e14bb325SJeff Bonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
986fa9e4066Sahrens {
98717f17c2dSbonwick 	uberblock_t *ubbuf;
988e14bb325SJeff Bonwick 	int n;
989fa9e4066Sahrens 
990e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
991e14bb325SJeff Bonwick 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
992fa9e4066Sahrens 
993fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
994fa9e4066Sahrens 		return;
995fa9e4066Sahrens 
996e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
997fa9e4066Sahrens 		return;
998fa9e4066Sahrens 
99917f17c2dSbonwick 	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
1000fa9e4066Sahrens 
100117f17c2dSbonwick 	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
100217f17c2dSbonwick 	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
100317f17c2dSbonwick 	*ubbuf = *ub;
1004fa9e4066Sahrens 
1005e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++)
100617f17c2dSbonwick 		vdev_label_write(zio, vd, l, ubbuf,
1007e14bb325SJeff Bonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
100817f17c2dSbonwick 		    vdev_uberblock_sync_done, zio->io_private,
1009e14bb325SJeff Bonwick 		    flags | ZIO_FLAG_DONT_PROPAGATE);
1010fa9e4066Sahrens 
101117f17c2dSbonwick 	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
1012fa9e4066Sahrens }
1013fa9e4066Sahrens 
10143e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */
101517f17c2dSbonwick int
101617f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1017fa9e4066Sahrens {
101817f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1019e14bb325SJeff Bonwick 	zio_t *zio;
102017f17c2dSbonwick 	uint64_t good_writes = 0;
1021fa9e4066Sahrens 
1022e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, &good_writes, flags);
1023e14bb325SJeff Bonwick 
1024e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
1025e14bb325SJeff Bonwick 		vdev_uberblock_sync(zio, ub, svd[v], flags);
1026fa9e4066Sahrens 
102717f17c2dSbonwick 	(void) zio_wait(zio);
1028fa9e4066Sahrens 
1029fa9e4066Sahrens 	/*
103017f17c2dSbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
103117f17c2dSbonwick 	 * are no longer needed (because the new uberblocks and the even
103217f17c2dSbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
1033fa9e4066Sahrens 	 */
103417f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
1035fa9e4066Sahrens 
1036e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
103717f17c2dSbonwick 		zio_flush(zio, svd[v]);
1038fa9e4066Sahrens 
103917f17c2dSbonwick 	(void) zio_wait(zio);
104017f17c2dSbonwick 
104117f17c2dSbonwick 	return (good_writes >= 1 ? 0 : EIO);
1042fa9e4066Sahrens }
1043fa9e4066Sahrens 
1044fa9e4066Sahrens /*
104517f17c2dSbonwick  * On success, increment the count of good writes for our top-level vdev.
1046fa9e4066Sahrens  */
1047fa9e4066Sahrens static void
104817f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
1049fa9e4066Sahrens {
105017f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1051fa9e4066Sahrens 
1052fa9e4066Sahrens 	if (zio->io_error == 0)
1053fa9e4066Sahrens 		atomic_add_64(good_writes, 1);
1054fa9e4066Sahrens }
1055fa9e4066Sahrens 
105617f17c2dSbonwick /*
105717f17c2dSbonwick  * If there weren't enough good writes, indicate failure to the parent.
105817f17c2dSbonwick  */
1059fa9e4066Sahrens static void
106017f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
106117f17c2dSbonwick {
106217f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
106317f17c2dSbonwick 
106417f17c2dSbonwick 	if (*good_writes == 0)
1065be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(EIO);
106617f17c2dSbonwick 
106717f17c2dSbonwick 	kmem_free(good_writes, sizeof (uint64_t));
106817f17c2dSbonwick }
106917f17c2dSbonwick 
107051ece835Seschrock /*
10710430f8daSeschrock  * We ignore errors for log and cache devices, simply free the private data.
107251ece835Seschrock  */
107351ece835Seschrock static void
10740430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio)
107551ece835Seschrock {
107651ece835Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
107751ece835Seschrock }
107851ece835Seschrock 
107917f17c2dSbonwick /*
108017f17c2dSbonwick  * Write all even or odd labels to all leaves of the specified vdev.
108117f17c2dSbonwick  */
108217f17c2dSbonwick static void
1083e14bb325SJeff Bonwick vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
1084fa9e4066Sahrens {
1085fa9e4066Sahrens 	nvlist_t *label;
1086fa9e4066Sahrens 	vdev_phys_t *vp;
1087fa9e4066Sahrens 	char *buf;
1088fa9e4066Sahrens 	size_t buflen;
1089fa9e4066Sahrens 
1090e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1091e14bb325SJeff Bonwick 		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
1092fa9e4066Sahrens 
1093fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1094fa9e4066Sahrens 		return;
1095fa9e4066Sahrens 
1096e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1097fa9e4066Sahrens 		return;
1098fa9e4066Sahrens 
1099fa9e4066Sahrens 	/*
1100fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
1101fa9e4066Sahrens 	 */
11020373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1103fa9e4066Sahrens 
1104fa9e4066Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
1105fa9e4066Sahrens 	bzero(vp, sizeof (vdev_phys_t));
1106fa9e4066Sahrens 
1107fa9e4066Sahrens 	buf = vp->vp_nvlist;
1108fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
1109fa9e4066Sahrens 
111017f17c2dSbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
111117f17c2dSbonwick 		for (; l < VDEV_LABELS; l += 2) {
111217f17c2dSbonwick 			vdev_label_write(zio, vd, l, vp,
111317f17c2dSbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
111417f17c2dSbonwick 			    sizeof (vdev_phys_t),
111517f17c2dSbonwick 			    vdev_label_sync_done, zio->io_private,
1116e14bb325SJeff Bonwick 			    flags | ZIO_FLAG_DONT_PROPAGATE);
111717f17c2dSbonwick 		}
111817f17c2dSbonwick 	}
1119fa9e4066Sahrens 
1120fa9e4066Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
1121fa9e4066Sahrens 	nvlist_free(label);
1122fa9e4066Sahrens }
1123fa9e4066Sahrens 
112417f17c2dSbonwick int
1125e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1126fa9e4066Sahrens {
1127e14bb325SJeff Bonwick 	list_t *dl = &spa->spa_config_dirty_list;
112817f17c2dSbonwick 	vdev_t *vd;
1129e14bb325SJeff Bonwick 	zio_t *zio;
1130fa9e4066Sahrens 	int error;
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	/*
1133e14bb325SJeff Bonwick 	 * Write the new labels to disk.
1134fa9e4066Sahrens 	 */
1135e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, flags);
1136fa9e4066Sahrens 
113717f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
113817f17c2dSbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
113917f17c2dSbonwick 		    KM_SLEEP);
114088ecc943SGeorge Wilson 
114188ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
114288ecc943SGeorge Wilson 
1143a3f829aeSBill Moore 		zio_t *vio = zio_null(zio, spa, NULL,
11440430f8daSeschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
11450430f8daSeschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
114617f17c2dSbonwick 		    good_writes, flags);
1147e14bb325SJeff Bonwick 		vdev_label_sync(vio, vd, l, txg, flags);
114817f17c2dSbonwick 		zio_nowait(vio);
1149fa9e4066Sahrens 	}
1150e14bb325SJeff Bonwick 
1151e14bb325SJeff Bonwick 	error = zio_wait(zio);
1152fa9e4066Sahrens 
11538654d025Sperrin 	/*
115417f17c2dSbonwick 	 * Flush the new labels to disk.
11558654d025Sperrin 	 */
115617f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
11578654d025Sperrin 
115817f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
115917f17c2dSbonwick 		zio_flush(zio, vd);
116017f17c2dSbonwick 
116117f17c2dSbonwick 	(void) zio_wait(zio);
1162fa9e4066Sahrens 
1163fa9e4066Sahrens 	return (error);
1164fa9e4066Sahrens }
1165fa9e4066Sahrens 
1166fa9e4066Sahrens /*
116717f17c2dSbonwick  * Sync the uberblock and any changes to the vdev configuration.
1168fa9e4066Sahrens  *
1169fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
1170fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
1171fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
1172fa9e4066Sahrens  * describe the failure semantics at each stage.
1173fa9e4066Sahrens  *
117417f17c2dSbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1175fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
1176fa9e4066Sahrens  */
1177e14bb325SJeff Bonwick int
11788956713aSEric Schrock vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard)
1179fa9e4066Sahrens {
118017f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1181fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
11820373e76bSbonwick 	vdev_t *vd;
1183fa9e4066Sahrens 	zio_t *zio;
1184e14bb325SJeff Bonwick 	int error;
1185e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1186fa9e4066Sahrens 
11878956713aSEric Schrock 	/*
11888956713aSEric Schrock 	 * Normally, we don't want to try too hard to write every label and
11898956713aSEric Schrock 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
11908956713aSEric Schrock 	 * sync process to block while we retry.  But if we can't write a
11918956713aSEric Schrock 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
11928956713aSEric Schrock 	 * bailing out and declaring the pool faulted.
11938956713aSEric Schrock 	 */
11948956713aSEric Schrock 	if (tryhard)
11958956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
11968956713aSEric Schrock 
1197fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
1198fa9e4066Sahrens 
1199fa9e4066Sahrens 	/*
120017f17c2dSbonwick 	 * If this isn't a resync due to I/O errors,
120117f17c2dSbonwick 	 * and nothing changed in this transaction group,
120217f17c2dSbonwick 	 * and the vdev configuration hasn't changed,
12030373e76bSbonwick 	 * then there's nothing to do.
1204fa9e4066Sahrens 	 */
120517f17c2dSbonwick 	if (ub->ub_txg < txg &&
120617f17c2dSbonwick 	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
1207e14bb325SJeff Bonwick 	    list_is_empty(&spa->spa_config_dirty_list))
1208e14bb325SJeff Bonwick 		return (0);
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
1211e14bb325SJeff Bonwick 		return (0);
1212fa9e4066Sahrens 
12130373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
12140373e76bSbonwick 
1215fa9e4066Sahrens 	/*
1216fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
1217fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
1218fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
1219fa9e4066Sahrens 	 * before any uberblock that references them.
1220fa9e4066Sahrens 	 */
122117f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
122217f17c2dSbonwick 
1223fa9e4066Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
122417f17c2dSbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
122517f17c2dSbonwick 		zio_flush(zio, vd);
122617f17c2dSbonwick 
1227fa9e4066Sahrens 	(void) zio_wait(zio);
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	/*
1230fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1231fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1232fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1233fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
123417f17c2dSbonwick 	 * which have not yet been touched, will still be valid.  We flush
123517f17c2dSbonwick 	 * the new labels to disk to ensure that all even-label updates
123617f17c2dSbonwick 	 * are committed to stable storage before the uberblock update.
1237fa9e4066Sahrens 	 */
1238e14bb325SJeff Bonwick 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
1239e14bb325SJeff Bonwick 		return (error);
1240fa9e4066Sahrens 
1241fa9e4066Sahrens 	/*
1242e14bb325SJeff Bonwick 	 * Sync the uberblocks to all vdevs in svd[].
12430373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
12440373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1245fa9e4066Sahrens 	 *
1246fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1247fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1248fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1249fa9e4066Sahrens 	 *	to that uberblock.
1250fa9e4066Sahrens 	 *
1251fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1252fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1253fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1254fa9e4066Sahrens 	 *	to the new uberblocks.
1255fa9e4066Sahrens 	 */
1256e14bb325SJeff Bonwick 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
1257e14bb325SJeff Bonwick 		return (error);
1258fa9e4066Sahrens 
1259fa9e4066Sahrens 	/*
1260fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1261fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1262fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1263fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1264fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
126517f17c2dSbonwick 	 * all labels will be brought up to date.  We flush the new labels
126617f17c2dSbonwick 	 * to disk to ensure that all odd-label updates are committed to
126717f17c2dSbonwick 	 * stable storage before the next transaction group begins.
1268fa9e4066Sahrens 	 */
1269e14bb325SJeff Bonwick 	return (vdev_label_sync_list(spa, 1, txg, flags));
1270fa9e4066Sahrens }
1271