xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 58447f688d5e308373ab16a3b129bc0ba0fbc154)
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.
2486714001SSerapheim Dimitropoulos  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25e0f1c0afSOlaf Faaland  * Copyright 2019 Joyent, Inc.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Virtual Device Labels
30fa9e4066Sahrens  * ---------------------
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * The vdev label serves several distinct purposes:
33fa9e4066Sahrens  *
34fa9e4066Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
35fa9e4066Sahrens  *	   identity within the pool.
36fa9e4066Sahrens  *
37cfd63e1bSMatthew Ahrens  *	2. Verify that all the devices given in a configuration are present
38fa9e4066Sahrens  *         within the pool.
39fa9e4066Sahrens  *
40cfd63e1bSMatthew Ahrens  *	3. Determine the uberblock for the pool.
41fa9e4066Sahrens  *
42cfd63e1bSMatthew Ahrens  *	4. In case of an import operation, determine the configuration of the
43fa9e4066Sahrens  *         toplevel vdev of which it is a part.
44fa9e4066Sahrens  *
45cfd63e1bSMatthew Ahrens  *	5. If an import operation cannot find all the devices in the pool,
46fa9e4066Sahrens  *         provide enough information to the administrator to determine which
47fa9e4066Sahrens  *         devices are missing.
48fa9e4066Sahrens  *
49fa9e4066Sahrens  * It is important to note that while the kernel is responsible for writing the
50fa9e4066Sahrens  * label, it only consumes the information in the first three cases.  The
51fa9e4066Sahrens  * latter information is only consumed in userland when determining the
52fa9e4066Sahrens  * configuration to import a pool.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * Label Organization
56fa9e4066Sahrens  * ------------------
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * Before describing the contents of the label, it's important to understand how
59fa9e4066Sahrens  * the labels are written and updated with respect to the uberblock.
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * When the pool configuration is altered, either because it was newly created
62fa9e4066Sahrens  * or a device was added, we want to update all the labels such that we can deal
63fa9e4066Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
64fa9e4066Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
653d7072f8Seschrock  * labels and an uberblock with the following transaction groups:
66fa9e4066Sahrens  *
67fa9e4066Sahrens  *              L1          UB          L2
68fa9e4066Sahrens  *           +------+    +------+    +------+
69fa9e4066Sahrens  *           |      |    |      |    |      |
70fa9e4066Sahrens  *           | t10  |    | t10  |    | t10  |
71fa9e4066Sahrens  *           |      |    |      |    |      |
72fa9e4066Sahrens  *           +------+    +------+    +------+
73fa9e4066Sahrens  *
74fa9e4066Sahrens  * In this stable state, the labels and the uberblock were all updated within
75fa9e4066Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
76fa9e4066Sahrens  * that we can detect when we fail partway through writing the label.
77fa9e4066Sahrens  *
78fa9e4066Sahrens  * In order to identify which labels are valid, the labels are written in the
79fa9e4066Sahrens  * following manner:
80fa9e4066Sahrens  *
81cfd63e1bSMatthew Ahrens  *	1. For each vdev, update 'L1' to the new label
82cfd63e1bSMatthew Ahrens  *	2. Update the uberblock
83cfd63e1bSMatthew Ahrens  *	3. For each vdev, update 'L2' to the new label
84fa9e4066Sahrens  *
85fa9e4066Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
86fa9e4066Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
87fa9e4066Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
88fa9e4066Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
89fa9e4066Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
90fa9e4066Sahrens  * therefore L1 is valid.
91fa9e4066Sahrens  *
92fa9e4066Sahrens  * Another added complexity is that not every label is updated when the config
93fa9e4066Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
94fa9e4066Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
95fa9e4066Sahrens  * be older than the pool uberblock, because the necessary information is stored
96fa9e4066Sahrens  * on another vdev.
97fa9e4066Sahrens  *
98fa9e4066Sahrens  *
99fa9e4066Sahrens  * On-disk Format
100fa9e4066Sahrens  * --------------
101fa9e4066Sahrens  *
102fa9e4066Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
103fa9e4066Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
104fa9e4066Sahrens  * VTOC disk labels, but is otherwise ignored.
105fa9e4066Sahrens  *
106fa9e4066Sahrens  * The first half of the label is a packed nvlist which contains pool wide
107fa9e4066Sahrens  * properties, per-vdev properties, and configuration information.  It is
108fa9e4066Sahrens  * described in more detail below.
109fa9e4066Sahrens  *
110fa9e4066Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
111fa9e4066Sahrens  * These uberblocks are updated whenever a transaction group is committed,
112fa9e4066Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
113fa9e4066Sahrens  * vdev for the 'best' uberblock.
114fa9e4066Sahrens  *
115fa9e4066Sahrens  *
116fa9e4066Sahrens  * Configuration Information
117fa9e4066Sahrens  * -------------------------
118fa9e4066Sahrens  *
119fa9e4066Sahrens  * The nvlist describing the pool and vdev contains the following elements:
120fa9e4066Sahrens  *
121cfd63e1bSMatthew Ahrens  *	version		ZFS on-disk version
122cfd63e1bSMatthew Ahrens  *	name		Pool name
123cfd63e1bSMatthew Ahrens  *	state		Pool state
124cfd63e1bSMatthew Ahrens  *	txg		Transaction group in which this label was written
125cfd63e1bSMatthew Ahrens  *	pool_guid	Unique identifier for this pool
126cfd63e1bSMatthew Ahrens  *	vdev_tree	An nvlist describing vdev tree.
127ad135b5dSChristopher Siden  *	features_for_read
128ad135b5dSChristopher Siden  *			An nvlist of the features necessary for reading the MOS.
129fa9e4066Sahrens  *
130fa9e4066Sahrens  * Each leaf device label also contains the following:
131fa9e4066Sahrens  *
132cfd63e1bSMatthew Ahrens  *	top_guid	Unique ID for top-level vdev in which this is contained
133cfd63e1bSMatthew Ahrens  *	guid		Unique ID for the leaf vdev
134fa9e4066Sahrens  *
135fa9e4066Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
136fa9e4066Sahrens  */
137fa9e4066Sahrens 
138fa9e4066Sahrens #include <sys/zfs_context.h>
139fa9e4066Sahrens #include <sys/spa.h>
140fa9e4066Sahrens #include <sys/spa_impl.h>
141fa9e4066Sahrens #include <sys/dmu.h>
142fa9e4066Sahrens #include <sys/zap.h>
143fa9e4066Sahrens #include <sys/vdev.h>
144fa9e4066Sahrens #include <sys/vdev_impl.h>
145fa9e4066Sahrens #include <sys/uberblock_impl.h>
146fa9e4066Sahrens #include <sys/metaslab.h>
1475cabbc6bSPrashanth Sreenivasa #include <sys/metaslab_impl.h>
148fa9e4066Sahrens #include <sys/zio.h>
1493f9d6ad7SLin Ling #include <sys/dsl_scan.h>
150770499e1SDan Kimmel #include <sys/abd.h>
151fa9e4066Sahrens #include <sys/fs/zfs.h>
152fa9e4066Sahrens 
153fa9e4066Sahrens /*
154fa9e4066Sahrens  * Basic routines to read and write from a vdev label.
155fa9e4066Sahrens  * Used throughout the rest of this file.
156fa9e4066Sahrens  */
157fa9e4066Sahrens uint64_t
158fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
159fa9e4066Sahrens {
160ecc2d604Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
161e7437265Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
162ecc2d604Sbonwick 
163fa9e4066Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
164fa9e4066Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
165fa9e4066Sahrens }
166fa9e4066Sahrens 
16721bf64a7Sgw /*
16821bf64a7Sgw  * Returns back the vdev label associated with the passed in offset.
16921bf64a7Sgw  */
17021bf64a7Sgw int
17121bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset)
17221bf64a7Sgw {
17321bf64a7Sgw 	int l;
17421bf64a7Sgw 
17521bf64a7Sgw 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
17621bf64a7Sgw 		offset -= psize - VDEV_LABEL_END_SIZE;
17721bf64a7Sgw 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
17821bf64a7Sgw 	}
17921bf64a7Sgw 	l = offset / sizeof (vdev_label_t);
18021bf64a7Sgw 	return (l < VDEV_LABELS ? l : -1);
18121bf64a7Sgw }
18221bf64a7Sgw 
183fa9e4066Sahrens static void
184770499e1SDan Kimmel vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
1859a686fbcSPaul Dagnelie     uint64_t size, zio_done_func_t *done, void *private, int flags)
186fa9e4066Sahrens {
187*58447f68SOlaf Faaland 	ASSERT(
188*58447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
189*58447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
190e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
191fa9e4066Sahrens 
192fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
193fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
194fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
195e14bb325SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
196fa9e4066Sahrens }
197fa9e4066Sahrens 
198e0f1c0afSOlaf Faaland void
199770499e1SDan Kimmel vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
2009a686fbcSPaul Dagnelie     uint64_t size, zio_done_func_t *done, void *private, int flags)
201fa9e4066Sahrens {
202*58447f68SOlaf Faaland 	ASSERT(
203*58447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
204*58447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
205e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
206fa9e4066Sahrens 
207fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
208fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
209fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
21017f17c2dSbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
211fa9e4066Sahrens }
212fa9e4066Sahrens 
21386714001SSerapheim Dimitropoulos static void
21486714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
21586714001SSerapheim Dimitropoulos {
21686714001SSerapheim Dimitropoulos 	spa_t *spa = vd->vdev_spa;
21786714001SSerapheim Dimitropoulos 
21886714001SSerapheim Dimitropoulos 	if (vd != spa->spa_root_vdev)
21986714001SSerapheim Dimitropoulos 		return;
22086714001SSerapheim Dimitropoulos 
22186714001SSerapheim Dimitropoulos 	/* provide either current or previous scan information */
22286714001SSerapheim Dimitropoulos 	pool_scan_stat_t ps;
22386714001SSerapheim Dimitropoulos 	if (spa_scan_get_stats(spa, &ps) == 0) {
22486714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
22586714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
22686714001SSerapheim Dimitropoulos 		    sizeof (pool_scan_stat_t) / sizeof (uint64_t));
22786714001SSerapheim Dimitropoulos 	}
22886714001SSerapheim Dimitropoulos 
22986714001SSerapheim Dimitropoulos 	pool_removal_stat_t prs;
23086714001SSerapheim Dimitropoulos 	if (spa_removal_get_stats(spa, &prs) == 0) {
23186714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
23286714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
23386714001SSerapheim Dimitropoulos 		    sizeof (prs) / sizeof (uint64_t));
23486714001SSerapheim Dimitropoulos 	}
23586714001SSerapheim Dimitropoulos 
23686714001SSerapheim Dimitropoulos 	pool_checkpoint_stat_t pcs;
23786714001SSerapheim Dimitropoulos 	if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
23886714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
23986714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
24086714001SSerapheim Dimitropoulos 		    sizeof (pcs) / sizeof (uint64_t));
24186714001SSerapheim Dimitropoulos 	}
24286714001SSerapheim Dimitropoulos }
24386714001SSerapheim Dimitropoulos 
244fa9e4066Sahrens /*
245fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
246fa9e4066Sahrens  */
247fa9e4066Sahrens nvlist_t *
24899653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
2493f9d6ad7SLin Ling     vdev_config_flag_t flags)
250fa9e4066Sahrens {
251fa9e4066Sahrens 	nvlist_t *nv = NULL;
2525cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
253fa9e4066Sahrens 
254b4952e17SGeorge Wilson 	nv = fnvlist_alloc();
255fa9e4066Sahrens 
256b4952e17SGeorge Wilson 	fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
2573f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
258b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
259b4952e17SGeorge Wilson 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
260fa9e4066Sahrens 
261fa9e4066Sahrens 	if (vd->vdev_path != NULL)
262b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
263fa9e4066Sahrens 
264fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
265b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
266fa9e4066Sahrens 
2673d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
268b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
269b4952e17SGeorge Wilson 		    vd->vdev_physpath);
2703d7072f8Seschrock 
2716809eb4eSEric Schrock 	if (vd->vdev_fru != NULL)
272b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
2736809eb4eSEric Schrock 
27499653d4eSeschrock 	if (vd->vdev_nparity != 0) {
27599653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
27699653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
27799653d4eSeschrock 
27899653d4eSeschrock 		/*
27999653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
28099653d4eSeschrock 		 * into a crufty old storage pool.
28199653d4eSeschrock 		 */
28299653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
283f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 2 &&
284f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
285f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 3 &&
286f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
28799653d4eSeschrock 
28899653d4eSeschrock 		/*
28999653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
29099653d4eSeschrock 		 * that only support a single parity device -- older software
29199653d4eSeschrock 		 * will just ignore it.
29299653d4eSeschrock 		 */
293b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
29499653d4eSeschrock 	}
29599653d4eSeschrock 
296afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
297b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
298b4952e17SGeorge Wilson 		    vd->vdev_wholedisk);
299afefbcddSeschrock 
3006f793812SPavel Zakharov 	if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
301b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
302ea8dc4b6Seschrock 
30399653d4eSeschrock 	if (vd->vdev_isspare)
304b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
30599653d4eSeschrock 
3063f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
3073f9d6ad7SLin Ling 	    vd == vd->vdev_top) {
308b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
309b4952e17SGeorge Wilson 		    vd->vdev_ms_array);
310b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
311b4952e17SGeorge Wilson 		    vd->vdev_ms_shift);
312b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
313b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
314b4952e17SGeorge Wilson 		    vd->vdev_asize);
315b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
3165cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_removing) {
317b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
318b4952e17SGeorge Wilson 			    vd->vdev_removing);
3195cabbc6bSPrashanth Sreenivasa 		}
320fa9e4066Sahrens 	}
321fa9e4066Sahrens 
3220713e232SGeorge Wilson 	if (vd->vdev_dtl_sm != NULL) {
323b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
3240713e232SGeorge Wilson 		    space_map_object(vd->vdev_dtl_sm));
3250713e232SGeorge Wilson 	}
326fa9e4066Sahrens 
3275cabbc6bSPrashanth Sreenivasa 	if (vic->vic_mapping_object != 0) {
3285cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
3295cabbc6bSPrashanth Sreenivasa 		    vic->vic_mapping_object);
3305cabbc6bSPrashanth Sreenivasa 	}
3315cabbc6bSPrashanth Sreenivasa 
3325cabbc6bSPrashanth Sreenivasa 	if (vic->vic_births_object != 0) {
3335cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
3345cabbc6bSPrashanth Sreenivasa 		    vic->vic_births_object);
3355cabbc6bSPrashanth Sreenivasa 	}
3365cabbc6bSPrashanth Sreenivasa 
3375cabbc6bSPrashanth Sreenivasa 	if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
3385cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
3395cabbc6bSPrashanth Sreenivasa 		    vic->vic_prev_indirect_vdev);
3405cabbc6bSPrashanth Sreenivasa 	}
3415cabbc6bSPrashanth Sreenivasa 
34288ecc943SGeorge Wilson 	if (vd->vdev_crtxg)
343b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
34488ecc943SGeorge Wilson 
345215198a6SJoe Stein 	if (flags & VDEV_CONFIG_MOS) {
346215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0) {
347215198a6SJoe Stein 			ASSERT(vd->vdev_ops->vdev_op_leaf);
348215198a6SJoe Stein 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
349215198a6SJoe Stein 			    vd->vdev_leaf_zap);
350215198a6SJoe Stein 		}
351215198a6SJoe Stein 
352215198a6SJoe Stein 		if (vd->vdev_top_zap != 0) {
353215198a6SJoe Stein 			ASSERT(vd == vd->vdev_top);
354215198a6SJoe Stein 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
355215198a6SJoe Stein 			    vd->vdev_top_zap);
356215198a6SJoe Stein 		}
357215198a6SJoe Stein 	}
358215198a6SJoe Stein 
359fa9e4066Sahrens 	if (getstats) {
360fa9e4066Sahrens 		vdev_stat_t vs;
3613f9d6ad7SLin Ling 
362fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
363b4952e17SGeorge Wilson 		fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
364b4952e17SGeorge Wilson 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t));
3653f9d6ad7SLin Ling 
36686714001SSerapheim Dimitropoulos 		root_vdev_actions_getprogress(vd, nv);
3675cabbc6bSPrashanth Sreenivasa 
3685cabbc6bSPrashanth Sreenivasa 		/*
3695cabbc6bSPrashanth Sreenivasa 		 * Note: this can be called from open context
3705cabbc6bSPrashanth Sreenivasa 		 * (spa_get_stats()), so we need the rwlock to prevent
3715cabbc6bSPrashanth Sreenivasa 		 * the mapping from being changed by condensing.
3725cabbc6bSPrashanth Sreenivasa 		 */
3735cabbc6bSPrashanth Sreenivasa 		rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
3745cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_indirect_mapping != NULL) {
3755cabbc6bSPrashanth Sreenivasa 			ASSERT(vd->vdev_indirect_births != NULL);
3765cabbc6bSPrashanth Sreenivasa 			vdev_indirect_mapping_t *vim =
3775cabbc6bSPrashanth Sreenivasa 			    vd->vdev_indirect_mapping;
3785cabbc6bSPrashanth Sreenivasa 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
3795cabbc6bSPrashanth Sreenivasa 			    vdev_indirect_mapping_size(vim));
3805cabbc6bSPrashanth Sreenivasa 		}
3815cabbc6bSPrashanth Sreenivasa 		rw_exit(&vd->vdev_indirect_rwlock);
3825cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_mg != NULL &&
3835cabbc6bSPrashanth Sreenivasa 		    vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
3845cabbc6bSPrashanth Sreenivasa 			/*
3855cabbc6bSPrashanth Sreenivasa 			 * Compute approximately how much memory would be used
3865cabbc6bSPrashanth Sreenivasa 			 * for the indirect mapping if this device were to
3875cabbc6bSPrashanth Sreenivasa 			 * be removed.
3885cabbc6bSPrashanth Sreenivasa 			 *
3895cabbc6bSPrashanth Sreenivasa 			 * Note: If the frag metric is invalid, then not
3905cabbc6bSPrashanth Sreenivasa 			 * enough metaslabs have been converted to have
3915cabbc6bSPrashanth Sreenivasa 			 * histograms.
3925cabbc6bSPrashanth Sreenivasa 			 */
3935cabbc6bSPrashanth Sreenivasa 			uint64_t seg_count = 0;
394cfd63e1bSMatthew Ahrens 			uint64_t to_alloc = vd->vdev_stat.vs_alloc;
3955cabbc6bSPrashanth Sreenivasa 
3965cabbc6bSPrashanth Sreenivasa 			/*
3975cabbc6bSPrashanth Sreenivasa 			 * There are the same number of allocated segments
3985cabbc6bSPrashanth Sreenivasa 			 * as free segments, so we will have at least one
399cfd63e1bSMatthew Ahrens 			 * entry per free segment.  However, small free
400cfd63e1bSMatthew Ahrens 			 * segments (smaller than vdev_removal_max_span)
401cfd63e1bSMatthew Ahrens 			 * will be combined with adjacent allocated segments
402cfd63e1bSMatthew Ahrens 			 * as a single mapping.
4035cabbc6bSPrashanth Sreenivasa 			 */
4045cabbc6bSPrashanth Sreenivasa 			for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
405cfd63e1bSMatthew Ahrens 				if (1ULL << (i + 1) < vdev_removal_max_span) {
406cfd63e1bSMatthew Ahrens 					to_alloc +=
407cfd63e1bSMatthew Ahrens 					    vd->vdev_mg->mg_histogram[i] <<
408cfd63e1bSMatthew Ahrens 					    i + 1;
409cfd63e1bSMatthew Ahrens 				} else {
410cfd63e1bSMatthew Ahrens 					seg_count +=
411cfd63e1bSMatthew Ahrens 					    vd->vdev_mg->mg_histogram[i];
412cfd63e1bSMatthew Ahrens 				}
4135cabbc6bSPrashanth Sreenivasa 			}
4145cabbc6bSPrashanth Sreenivasa 
4155cabbc6bSPrashanth Sreenivasa 			/*
416cfd63e1bSMatthew Ahrens 			 * The maximum length of a mapping is
417cfd63e1bSMatthew Ahrens 			 * zfs_remove_max_segment, so we need at least one entry
418cfd63e1bSMatthew Ahrens 			 * per zfs_remove_max_segment of allocated data.
4195cabbc6bSPrashanth Sreenivasa 			 */
420cfd63e1bSMatthew Ahrens 			seg_count += to_alloc / zfs_remove_max_segment;
4215cabbc6bSPrashanth Sreenivasa 
4225cabbc6bSPrashanth Sreenivasa 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
4235cabbc6bSPrashanth Sreenivasa 			    seg_count *
4245cabbc6bSPrashanth Sreenivasa 			    sizeof (vdev_indirect_mapping_entry_phys_t));
4255cabbc6bSPrashanth Sreenivasa 		}
426fa9e4066Sahrens 	}
427fa9e4066Sahrens 
428fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
429fa9e4066Sahrens 		nvlist_t **child;
4303f9d6ad7SLin Ling 		int c, idx;
431fa9e4066Sahrens 
43288ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
43388ecc943SGeorge Wilson 
434fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
435fa9e4066Sahrens 		    KM_SLEEP);
436fa9e4066Sahrens 
4373f9d6ad7SLin Ling 		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
4383f9d6ad7SLin Ling 			vdev_t *cvd = vd->vdev_child[c];
4393f9d6ad7SLin Ling 
4403f9d6ad7SLin Ling 			/*
4413f9d6ad7SLin Ling 			 * If we're generating an nvlist of removing
4423f9d6ad7SLin Ling 			 * vdevs then skip over any device which is
4433f9d6ad7SLin Ling 			 * not being removed.
4443f9d6ad7SLin Ling 			 */
4453f9d6ad7SLin Ling 			if ((flags & VDEV_CONFIG_REMOVING) &&
4463f9d6ad7SLin Ling 			    !cvd->vdev_removing)
4473f9d6ad7SLin Ling 				continue;
448fa9e4066Sahrens 
4493f9d6ad7SLin Ling 			child[idx++] = vdev_config_generate(spa, cvd,
4503f9d6ad7SLin Ling 			    getstats, flags);
4513f9d6ad7SLin Ling 		}
4523f9d6ad7SLin Ling 
4533f9d6ad7SLin Ling 		if (idx) {
454b4952e17SGeorge Wilson 			fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
455b4952e17SGeorge Wilson 			    child, idx);
4563f9d6ad7SLin Ling 		}
457fa9e4066Sahrens 
4583f9d6ad7SLin Ling 		for (c = 0; c < idx; c++)
459fa9e4066Sahrens 			nvlist_free(child[c]);
460fa9e4066Sahrens 
461fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
462441d80aaSlling 
463441d80aaSlling 	} else {
464069f55e2SEric Schrock 		const char *aux = NULL;
465069f55e2SEric Schrock 
466ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
467b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
468b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0)
469b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
470b4952e17SGeorge Wilson 			    vd->vdev_resilver_txg);
4713d7072f8Seschrock 		if (vd->vdev_faulted)
472b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
4733d7072f8Seschrock 		if (vd->vdev_degraded)
474b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
4753d7072f8Seschrock 		if (vd->vdev_removed)
476b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
4773d7072f8Seschrock 		if (vd->vdev_unspare)
478b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
47988ecc943SGeorge Wilson 		if (vd->vdev_ishole)
480b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
481069f55e2SEric Schrock 
482069f55e2SEric Schrock 		switch (vd->vdev_stat.vs_aux) {
483069f55e2SEric Schrock 		case VDEV_AUX_ERR_EXCEEDED:
484069f55e2SEric Schrock 			aux = "err_exceeded";
485069f55e2SEric Schrock 			break;
486069f55e2SEric Schrock 
487069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
488069f55e2SEric Schrock 			aux = "external";
489069f55e2SEric Schrock 			break;
490069f55e2SEric Schrock 		}
491069f55e2SEric Schrock 
492069f55e2SEric Schrock 		if (aux != NULL)
493b4952e17SGeorge Wilson 			fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
4941195e687SMark J Musante 
4951195e687SMark J Musante 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
496b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
497b4952e17SGeorge Wilson 			    vd->vdev_orig_guid);
4981195e687SMark J Musante 		}
499fa9e4066Sahrens 	}
500fa9e4066Sahrens 
501fa9e4066Sahrens 	return (nv);
502fa9e4066Sahrens }
503fa9e4066Sahrens 
50488ecc943SGeorge Wilson /*
50588ecc943SGeorge Wilson  * Generate a view of the top-level vdevs.  If we currently have holes
50688ecc943SGeorge Wilson  * in the namespace, then generate an array which contains a list of holey
50788ecc943SGeorge Wilson  * vdevs.  Additionally, add the number of top-level children that currently
50888ecc943SGeorge Wilson  * exist.
50988ecc943SGeorge Wilson  */
51088ecc943SGeorge Wilson void
51188ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config)
51288ecc943SGeorge Wilson {
51388ecc943SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
51488ecc943SGeorge Wilson 	uint64_t *array;
5153f9d6ad7SLin Ling 	uint_t c, idx;
51688ecc943SGeorge Wilson 
51788ecc943SGeorge Wilson 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
51888ecc943SGeorge Wilson 
5193f9d6ad7SLin Ling 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
52088ecc943SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
52188ecc943SGeorge Wilson 
5225cabbc6bSPrashanth Sreenivasa 		if (tvd->vdev_ishole) {
52388ecc943SGeorge Wilson 			array[idx++] = c;
5245cabbc6bSPrashanth Sreenivasa 		}
52588ecc943SGeorge Wilson 	}
52688ecc943SGeorge Wilson 
527312c6e15SGeorge Wilson 	if (idx) {
528312c6e15SGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
529312c6e15SGeorge Wilson 		    array, idx) == 0);
530312c6e15SGeorge Wilson 	}
531312c6e15SGeorge Wilson 
53288ecc943SGeorge Wilson 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
53388ecc943SGeorge Wilson 	    rvd->vdev_children) == 0);
53488ecc943SGeorge Wilson 
53588ecc943SGeorge Wilson 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
53688ecc943SGeorge Wilson }
53788ecc943SGeorge Wilson 
538ad135b5dSChristopher Siden /*
539dfbb9432SGeorge Wilson  * Returns the configuration from the label of the given vdev. For vdevs
540dfbb9432SGeorge Wilson  * which don't have a txg value stored on their label (i.e. spares/cache)
541dfbb9432SGeorge Wilson  * or have not been completely initialized (txg = 0) just return
542dfbb9432SGeorge Wilson  * the configuration from the first valid label we find. Otherwise,
543dfbb9432SGeorge Wilson  * find the most up-to-date label that does not exceed the specified
544dfbb9432SGeorge Wilson  * 'txg' value.
545ad135b5dSChristopher Siden  */
546fa9e4066Sahrens nvlist_t *
547dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg)
548fa9e4066Sahrens {
5490373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
550fa9e4066Sahrens 	nvlist_t *config = NULL;
551fa9e4066Sahrens 	vdev_phys_t *vp;
552770499e1SDan Kimmel 	abd_t *vp_abd;
553fa9e4066Sahrens 	zio_t *zio;
554dfbb9432SGeorge Wilson 	uint64_t best_txg = 0;
555b6bf6e15SPavel Zakharov 	uint64_t label_txg = 0;
556dfbb9432SGeorge Wilson 	int error = 0;
5578956713aSEric Schrock 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
5588956713aSEric Schrock 	    ZIO_FLAG_SPECULATIVE;
559fa9e4066Sahrens 
560e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
5610373e76bSbonwick 
5620a4e9518Sgw 	if (!vdev_readable(vd))
563fa9e4066Sahrens 		return (NULL);
564fa9e4066Sahrens 
565770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
566770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
567fa9e4066Sahrens 
5688956713aSEric Schrock retry:
569e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
570dfbb9432SGeorge Wilson 		nvlist_t *label = NULL;
571fa9e4066Sahrens 
572e14bb325SJeff Bonwick 		zio = zio_root(spa, NULL, NULL, flags);
573fa9e4066Sahrens 
574770499e1SDan Kimmel 		vdev_label_read(zio, vd, l, vp_abd,
575fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
576e14bb325SJeff Bonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
577fa9e4066Sahrens 
578fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
579fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
580dfbb9432SGeorge Wilson 		    &label, 0) == 0) {
581dfbb9432SGeorge Wilson 			/*
582dfbb9432SGeorge Wilson 			 * Auxiliary vdevs won't have txg values in their
583dfbb9432SGeorge Wilson 			 * labels and newly added vdevs may not have been
584dfbb9432SGeorge Wilson 			 * completely initialized so just return the
585dfbb9432SGeorge Wilson 			 * configuration from the first valid label we
586dfbb9432SGeorge Wilson 			 * encounter.
587dfbb9432SGeorge Wilson 			 */
588dfbb9432SGeorge Wilson 			error = nvlist_lookup_uint64(label,
589dfbb9432SGeorge Wilson 			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
590dfbb9432SGeorge Wilson 			if ((error || label_txg == 0) && !config) {
591dfbb9432SGeorge Wilson 				config = label;
592dfbb9432SGeorge Wilson 				break;
593dfbb9432SGeorge Wilson 			} else if (label_txg <= txg && label_txg > best_txg) {
594dfbb9432SGeorge Wilson 				best_txg = label_txg;
595dfbb9432SGeorge Wilson 				nvlist_free(config);
596dfbb9432SGeorge Wilson 				config = fnvlist_dup(label);
597dfbb9432SGeorge Wilson 			}
598dfbb9432SGeorge Wilson 		}
599fa9e4066Sahrens 
600dfbb9432SGeorge Wilson 		if (label != NULL) {
601dfbb9432SGeorge Wilson 			nvlist_free(label);
602dfbb9432SGeorge Wilson 			label = NULL;
603fa9e4066Sahrens 		}
604fa9e4066Sahrens 	}
605fa9e4066Sahrens 
6068956713aSEric Schrock 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
6078956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
6088956713aSEric Schrock 		goto retry;
6098956713aSEric Schrock 	}
6108956713aSEric Schrock 
611b6bf6e15SPavel Zakharov 	/*
612b6bf6e15SPavel Zakharov 	 * We found a valid label but it didn't pass txg restrictions.
613b6bf6e15SPavel Zakharov 	 */
614b6bf6e15SPavel Zakharov 	if (config == NULL && label_txg != 0) {
615b6bf6e15SPavel Zakharov 		vdev_dbgmsg(vd, "label discarded as txg is too large "
616b6bf6e15SPavel Zakharov 		    "(%llu > %llu)", (u_longlong_t)label_txg,
617b6bf6e15SPavel Zakharov 		    (u_longlong_t)txg);
618b6bf6e15SPavel Zakharov 	}
619b6bf6e15SPavel Zakharov 
620770499e1SDan Kimmel 	abd_free(vp_abd);
621fa9e4066Sahrens 
622fa9e4066Sahrens 	return (config);
623fa9e4066Sahrens }
624fa9e4066Sahrens 
62539c23413Seschrock /*
62639c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
62739c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
62839c23413Seschrock  */
62939c23413Seschrock static boolean_t
63039c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
631fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
63239c23413Seschrock {
63339c23413Seschrock 	spa_t *spa = vd->vdev_spa;
63439c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
63539c23413Seschrock 	uint64_t vdtxg = 0;
63639c23413Seschrock 	nvlist_t *label;
63739c23413Seschrock 
63839c23413Seschrock 	if (spare_guid)
63939c23413Seschrock 		*spare_guid = 0ULL;
640fa94a07fSbrendan 	if (l2cache_guid)
641fa94a07fSbrendan 		*l2cache_guid = 0ULL;
64239c23413Seschrock 
64339c23413Seschrock 	/*
64439c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
64539c23413Seschrock 	 */
646dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
64739c23413Seschrock 		return (B_FALSE);
64839c23413Seschrock 
64939c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
65039c23413Seschrock 	    &vdtxg);
65139c23413Seschrock 
65239c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
65339c23413Seschrock 	    &state) != 0 ||
65439c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
65539c23413Seschrock 	    &device_guid) != 0) {
65639c23413Seschrock 		nvlist_free(label);
65739c23413Seschrock 		return (B_FALSE);
65839c23413Seschrock 	}
65939c23413Seschrock 
660fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
66139c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
66239c23413Seschrock 	    &pool_guid) != 0 ||
66339c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
66439c23413Seschrock 	    &txg) != 0)) {
66539c23413Seschrock 		nvlist_free(label);
66639c23413Seschrock 		return (B_FALSE);
66739c23413Seschrock 	}
66839c23413Seschrock 
66939c23413Seschrock 	nvlist_free(label);
67039c23413Seschrock 
67139c23413Seschrock 	/*
67239c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
67339c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
67439c23413Seschrock 	 * spare (which we check for later on).
67539c23413Seschrock 	 */
676fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
67739c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
67889a89ebfSlling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
679fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
68039c23413Seschrock 		return (B_FALSE);
68139c23413Seschrock 
68239c23413Seschrock 	/*
68339c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
68439c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
68539c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
68639c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
68739c23413Seschrock 	 * transaction.
68839c23413Seschrock 	 */
689fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
690fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
69139c23413Seschrock 		return (B_TRUE);
69239c23413Seschrock 
69339c23413Seschrock 	/*
69439c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
69539c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
696fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
69739c23413Seschrock 	 */
69889a89ebfSlling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
69939c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
70039c23413Seschrock 		if (spare_guid)
70139c23413Seschrock 			*spare_guid = device_guid;
70239c23413Seschrock 
70339c23413Seschrock 		switch (reason) {
70439c23413Seschrock 		case VDEV_LABEL_CREATE:
705fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
70639c23413Seschrock 			return (B_TRUE);
70739c23413Seschrock 
70839c23413Seschrock 		case VDEV_LABEL_REPLACE:
70939c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
71039c23413Seschrock 			    spare_pool != 0ULL);
71139c23413Seschrock 
71239c23413Seschrock 		case VDEV_LABEL_SPARE:
71339c23413Seschrock 			return (spa_has_spare(spa, device_guid));
71439c23413Seschrock 		}
71539c23413Seschrock 	}
71639c23413Seschrock 
717fa94a07fSbrendan 	/*
718fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
719fa94a07fSbrendan 	 */
720fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
721fa94a07fSbrendan 		return (B_TRUE);
722fa94a07fSbrendan 
723f9af39baSGeorge Wilson 	/*
724f9af39baSGeorge Wilson 	 * We can't rely on a pool's state if it's been imported
725f9af39baSGeorge Wilson 	 * read-only.  Instead we look to see if the pools is marked
726f9af39baSGeorge Wilson 	 * read-only in the namespace and set the state to active.
727f9af39baSGeorge Wilson 	 */
7285bdd995dSRichard Yao 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
7295bdd995dSRichard Yao 	    (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
730f9af39baSGeorge Wilson 	    spa_mode(spa) == FREAD)
731f9af39baSGeorge Wilson 		state = POOL_STATE_ACTIVE;
732f9af39baSGeorge Wilson 
73339c23413Seschrock 	/*
73439c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
73539c23413Seschrock 	 * pool on the system.
73639c23413Seschrock 	 */
73739c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
73839c23413Seschrock }
73939c23413Seschrock 
74039c23413Seschrock /*
74139c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
74239c23413Seschrock  * use, and writable.  We put down an initial label which we will later
74339c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
74439c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
74539c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
74639c23413Seschrock  * itself.
74739c23413Seschrock  */
74839c23413Seschrock int
74939c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
750fa9e4066Sahrens {
751fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
752fa9e4066Sahrens 	nvlist_t *label;
753fa9e4066Sahrens 	vdev_phys_t *vp;
754770499e1SDan Kimmel 	abd_t *vp_abd;
755770499e1SDan Kimmel 	abd_t *pad2;
756ecc2d604Sbonwick 	uberblock_t *ub;
757770499e1SDan Kimmel 	abd_t *ub_abd;
758fa9e4066Sahrens 	zio_t *zio;
759fa9e4066Sahrens 	char *buf;
760fa9e4066Sahrens 	size_t buflen;
761fa9e4066Sahrens 	int error;
762fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
763e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
764fa9e4066Sahrens 
765e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
7660373e76bSbonwick 
767e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
76839c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
76939c23413Seschrock 		    crtxg, reason)) != 0)
770fa9e4066Sahrens 			return (error);
771fa9e4066Sahrens 
77288ecc943SGeorge Wilson 	/* Track the creation time for this vdev */
77388ecc943SGeorge Wilson 	vd->vdev_crtxg = crtxg;
77488ecc943SGeorge Wilson 
775973c78e9SGeorge Wilson 	if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
776fa9e4066Sahrens 		return (0);
777fa9e4066Sahrens 
778fa9e4066Sahrens 	/*
77939c23413Seschrock 	 * Dead vdevs cannot be initialized.
780fa9e4066Sahrens 	 */
781fa9e4066Sahrens 	if (vdev_is_dead(vd))
782be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
783fa9e4066Sahrens 
784fa9e4066Sahrens 	/*
78539c23413Seschrock 	 * Determine if the vdev is in use.
786fa9e4066Sahrens 	 */
7871195e687SMark J Musante 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
788fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
789be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
79039c23413Seschrock 
79139c23413Seschrock 	/*
792fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
793fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
794fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
795fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
79639c23413Seschrock 	 */
797fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
798fa94a07fSbrendan 	    spare_guid != 0ULL) {
79931157203SJeff Bonwick 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
80099653d4eSeschrock 
80131157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
80231157203SJeff Bonwick 
80331157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
80431157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
80539c23413Seschrock 
80699653d4eSeschrock 		/*
80739c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
80839c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
8093d7072f8Seschrock 		 * labeled appropriately and we can just return.
81099653d4eSeschrock 		 */
81139c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
81239c23413Seschrock 			return (0);
8131195e687SMark J Musante 		ASSERT(reason == VDEV_LABEL_REPLACE ||
8141195e687SMark J Musante 		    reason == VDEV_LABEL_SPLIT);
815fa9e4066Sahrens 	}
816fa9e4066Sahrens 
817fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
818fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
81931157203SJeff Bonwick 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
82031157203SJeff Bonwick 
82131157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
822fa94a07fSbrendan 
82331157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
82431157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
825fa94a07fSbrendan 
826fa94a07fSbrendan 		/*
827fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
828fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
829fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
830fa94a07fSbrendan 		 */
831fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
832fa94a07fSbrendan 			return (0);
833fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
834fa94a07fSbrendan 	}
835fa94a07fSbrendan 
836fa9e4066Sahrens 	/*
83739c23413Seschrock 	 * Initialize its label.
838fa9e4066Sahrens 	 */
839770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
840770499e1SDan Kimmel 	abd_zero(vp_abd, sizeof (vdev_phys_t));
841770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
842fa9e4066Sahrens 
843fa9e4066Sahrens 	/*
844fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
845fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
846fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
847fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
848fa9e4066Sahrens 	 */
84939c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
85039c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
85139c23413Seschrock 		/*
85239c23413Seschrock 		 * For inactive hot spares, we generate a special label that
85339c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
85439c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
85539c23413Seschrock 		 * active hot spare (in which case we want to revert the
85639c23413Seschrock 		 * labels).
85739c23413Seschrock 		 */
85899653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
85999653d4eSeschrock 
86099653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
86199653d4eSeschrock 		    spa_version(spa)) == 0);
86299653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
86399653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
86499653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
86599653d4eSeschrock 		    vd->vdev_guid) == 0);
866fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
867fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
868fa94a07fSbrendan 		/*
869fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
870fa94a07fSbrendan 		 */
871fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
872fa94a07fSbrendan 
873fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
874fa94a07fSbrendan 		    spa_version(spa)) == 0);
875fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
876fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
877fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
878fa94a07fSbrendan 		    vd->vdev_guid) == 0);
87999653d4eSeschrock 	} else {
8801195e687SMark J Musante 		uint64_t txg = 0ULL;
8811195e687SMark J Musante 
8821195e687SMark J Musante 		if (reason == VDEV_LABEL_SPLIT)
8831195e687SMark J Musante 			txg = spa->spa_uberblock.ub_txg;
8841195e687SMark J Musante 		label = spa_config_generate(spa, vd, txg, B_FALSE);
88599653d4eSeschrock 
88699653d4eSeschrock 		/*
88799653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
88899653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
88999653d4eSeschrock 		 * fail.
89099653d4eSeschrock 		 */
89199653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
89299653d4eSeschrock 		    crtxg) == 0);
89399653d4eSeschrock 	}
894fa9e4066Sahrens 
895fa9e4066Sahrens 	buf = vp->vp_nvlist;
896fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
897fa9e4066Sahrens 
898a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
899a75573b6Smmusante 	if (error != 0) {
900fa9e4066Sahrens 		nvlist_free(label);
901770499e1SDan Kimmel 		abd_free(vp_abd);
902a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
903a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
904fa9e4066Sahrens 	}
905fa9e4066Sahrens 
906fa9e4066Sahrens 	/*
907fa9e4066Sahrens 	 * Initialize uberblock template.
908fa9e4066Sahrens 	 */
909770499e1SDan Kimmel 	ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
910770499e1SDan Kimmel 	abd_zero(ub_abd, VDEV_UBERBLOCK_RING);
911770499e1SDan Kimmel 	abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
912770499e1SDan Kimmel 	ub = abd_to_buf(ub_abd);
913ecc2d604Sbonwick 	ub->ub_txg = 0;
914fa9e4066Sahrens 
915f83ffe1aSLin Ling 	/* Initialize the 2nd padding area. */
916770499e1SDan Kimmel 	pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
917770499e1SDan Kimmel 	abd_zero(pad2, VDEV_PAD_SIZE);
918f83ffe1aSLin Ling 
919fa9e4066Sahrens 	/*
920fa9e4066Sahrens 	 * Write everything in parallel.
921fa9e4066Sahrens 	 */
9228956713aSEric Schrock retry:
92317f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
924fa9e4066Sahrens 
925e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
926fa9e4066Sahrens 
927770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, vp_abd,
928fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
92917f17c2dSbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
930fa9e4066Sahrens 
931f83ffe1aSLin Ling 		/*
932f83ffe1aSLin Ling 		 * Skip the 1st padding area.
933f83ffe1aSLin Ling 		 * Zero out the 2nd padding area where it might have
934f83ffe1aSLin Ling 		 * left over data from previous filesystem format.
935f83ffe1aSLin Ling 		 */
936f83ffe1aSLin Ling 		vdev_label_write(zio, vd, l, pad2,
937f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2),
938f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, NULL, NULL, flags);
939f83ffe1aSLin Ling 
940770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, ub_abd,
941f64c0e34SEric Taylor 		    offsetof(vdev_label_t, vl_uberblock),
942f64c0e34SEric Taylor 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
943fa9e4066Sahrens 	}
944fa9e4066Sahrens 
945fa9e4066Sahrens 	error = zio_wait(zio);
946fa9e4066Sahrens 
9478956713aSEric Schrock 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
9488956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
9498956713aSEric Schrock 		goto retry;
9508956713aSEric Schrock 	}
9518956713aSEric Schrock 
952fa9e4066Sahrens 	nvlist_free(label);
953770499e1SDan Kimmel 	abd_free(pad2);
954770499e1SDan Kimmel 	abd_free(ub_abd);
955770499e1SDan Kimmel 	abd_free(vp_abd);
956fa9e4066Sahrens 
95739c23413Seschrock 	/*
95839c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
9593d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
960fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
961fa94a07fSbrendan 	 * level 2 ARC devices.
96239c23413Seschrock 	 */
96339c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
96439c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
96589a89ebfSlling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
96639c23413Seschrock 		spa_spare_add(vd);
96799653d4eSeschrock 
968fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
969fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
970fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
971fa94a07fSbrendan 		spa_l2cache_add(vd);
972fa94a07fSbrendan 
97339c23413Seschrock 	return (error);
97499653d4eSeschrock }
97599653d4eSeschrock 
976fa9e4066Sahrens /*
977fa9e4066Sahrens  * ==========================================================================
978fa9e4066Sahrens  * uberblock load/sync
979fa9e4066Sahrens  * ==========================================================================
980fa9e4066Sahrens  */
981fa9e4066Sahrens 
982fa9e4066Sahrens /*
983fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
984fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
985fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
986fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
987fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
988ad135b5dSChristopher Siden  * the missing replica comes back, then for a few seconds we'll have two
989fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
990fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
991fa9e4066Sahrens  */
992fa9e4066Sahrens static int
993fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
994fa9e4066Sahrens {
995fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
996fa9e4066Sahrens 		return (-1);
997fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
998fa9e4066Sahrens 		return (1);
999fa9e4066Sahrens 
1000fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
1001fa9e4066Sahrens 		return (-1);
1002fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
1003fa9e4066Sahrens 		return (1);
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	return (0);
1006fa9e4066Sahrens }
1007fa9e4066Sahrens 
1008ad135b5dSChristopher Siden struct ubl_cbdata {
1009ad135b5dSChristopher Siden 	uberblock_t	*ubl_ubbest;	/* Best uberblock */
1010ad135b5dSChristopher Siden 	vdev_t		*ubl_vd;	/* vdev associated with the above */
1011ad135b5dSChristopher Siden };
1012ad135b5dSChristopher Siden 
1013fa9e4066Sahrens static void
1014fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
1015fa9e4066Sahrens {
1016ad135b5dSChristopher Siden 	vdev_t *vd = zio->io_vd;
1017468c413aSTim Haley 	spa_t *spa = zio->io_spa;
1018e14bb325SJeff Bonwick 	zio_t *rio = zio->io_private;
1019770499e1SDan Kimmel 	uberblock_t *ub = abd_to_buf(zio->io_abd);
1020ad135b5dSChristopher Siden 	struct ubl_cbdata *cbp = rio->io_private;
1021fa9e4066Sahrens 
1022ad135b5dSChristopher Siden 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1023fa9e4066Sahrens 
1024ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1025e14bb325SJeff Bonwick 		mutex_enter(&rio->io_lock);
1026468c413aSTim Haley 		if (ub->ub_txg <= spa->spa_load_max_txg &&
1027ad135b5dSChristopher Siden 		    vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1028ad135b5dSChristopher Siden 			/*
1029dfbb9432SGeorge Wilson 			 * Keep track of the vdev in which this uberblock
1030dfbb9432SGeorge Wilson 			 * was found. We will use this information later
1031dfbb9432SGeorge Wilson 			 * to obtain the config nvlist associated with
1032ad135b5dSChristopher Siden 			 * this uberblock.
1033ad135b5dSChristopher Siden 			 */
1034ad135b5dSChristopher Siden 			*cbp->ubl_ubbest = *ub;
1035ad135b5dSChristopher Siden 			cbp->ubl_vd = vd;
1036ad135b5dSChristopher Siden 		}
1037e14bb325SJeff Bonwick 		mutex_exit(&rio->io_lock);
1038fa9e4066Sahrens 	}
1039fa9e4066Sahrens 
1040770499e1SDan Kimmel 	abd_free(zio->io_abd);
1041fa9e4066Sahrens }
1042fa9e4066Sahrens 
1043ad135b5dSChristopher Siden static void
1044ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1045ad135b5dSChristopher Siden     struct ubl_cbdata *cbp)
1046fa9e4066Sahrens {
1047e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1048ad135b5dSChristopher Siden 		vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1049fa9e4066Sahrens 
1050e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1051e14bb325SJeff Bonwick 		for (int l = 0; l < VDEV_LABELS; l++) {
1052e14bb325SJeff Bonwick 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1053e14bb325SJeff Bonwick 				vdev_label_read(zio, vd, l,
1054770499e1SDan Kimmel 				    abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1055770499e1SDan Kimmel 				    B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1056e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_SIZE(vd),
1057e14bb325SJeff Bonwick 				    vdev_uberblock_load_done, zio, flags);
1058e14bb325SJeff Bonwick 			}
1059fa9e4066Sahrens 		}
1060fa9e4066Sahrens 	}
1061ad135b5dSChristopher Siden }
1062e14bb325SJeff Bonwick 
1063ad135b5dSChristopher Siden /*
1064ad135b5dSChristopher Siden  * Reads the 'best' uberblock from disk along with its associated
1065ad135b5dSChristopher Siden  * configuration. First, we read the uberblock array of each label of each
1066ad135b5dSChristopher Siden  * vdev, keeping track of the uberblock with the highest txg in each array.
1067dfbb9432SGeorge Wilson  * Then, we read the configuration from the same vdev as the best uberblock.
1068ad135b5dSChristopher Siden  */
1069ad135b5dSChristopher Siden void
1070ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1071ad135b5dSChristopher Siden {
1072ad135b5dSChristopher Siden 	zio_t *zio;
1073ad135b5dSChristopher Siden 	spa_t *spa = rvd->vdev_spa;
1074ad135b5dSChristopher Siden 	struct ubl_cbdata cb;
1075ad135b5dSChristopher Siden 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1076ad135b5dSChristopher Siden 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1077ad135b5dSChristopher Siden 
1078ad135b5dSChristopher Siden 	ASSERT(ub);
1079ad135b5dSChristopher Siden 	ASSERT(config);
1080ad135b5dSChristopher Siden 
1081ad135b5dSChristopher Siden 	bzero(ub, sizeof (uberblock_t));
1082ad135b5dSChristopher Siden 	*config = NULL;
1083ad135b5dSChristopher Siden 
1084ad135b5dSChristopher Siden 	cb.ubl_ubbest = ub;
1085ad135b5dSChristopher Siden 	cb.ubl_vd = NULL;
1086ad135b5dSChristopher Siden 
1087ad135b5dSChristopher Siden 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1088ad135b5dSChristopher Siden 	zio = zio_root(spa, NULL, &cb, flags);
1089ad135b5dSChristopher Siden 	vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1090ad135b5dSChristopher Siden 	(void) zio_wait(zio);
1091dfbb9432SGeorge Wilson 
1092dfbb9432SGeorge Wilson 	/*
1093dfbb9432SGeorge Wilson 	 * It's possible that the best uberblock was discovered on a label
1094dfbb9432SGeorge Wilson 	 * that has a configuration which was written in a future txg.
1095dfbb9432SGeorge Wilson 	 * Search all labels on this vdev to find the configuration that
1096dfbb9432SGeorge Wilson 	 * matches the txg for our uberblock.
1097dfbb9432SGeorge Wilson 	 */
10983ee8c80cSPavel Zakharov 	if (cb.ubl_vd != NULL) {
10993ee8c80cSPavel Zakharov 		vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
11003ee8c80cSPavel Zakharov 		    "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
11013ee8c80cSPavel Zakharov 
1102dfbb9432SGeorge Wilson 		*config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
11036f793812SPavel Zakharov 		if (*config == NULL && spa->spa_extreme_rewind) {
11046f793812SPavel Zakharov 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
11056f793812SPavel Zakharov 			    "Trying again without txg restrictions.");
11066f793812SPavel Zakharov 			*config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
11076f793812SPavel Zakharov 		}
11083ee8c80cSPavel Zakharov 		if (*config == NULL) {
11093ee8c80cSPavel Zakharov 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
11103ee8c80cSPavel Zakharov 		}
11113ee8c80cSPavel Zakharov 	}
1112ad135b5dSChristopher Siden 	spa_config_exit(spa, SCL_ALL, FTAG);
1113fa9e4066Sahrens }
1114fa9e4066Sahrens 
1115fa9e4066Sahrens /*
111617f17c2dSbonwick  * On success, increment root zio's count of good writes.
11170373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1118fa9e4066Sahrens  */
1119fa9e4066Sahrens static void
1120fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
1121fa9e4066Sahrens {
112217f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1123fa9e4066Sahrens 
11240373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
11251a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(good_writes);
1126fa9e4066Sahrens }
1127fa9e4066Sahrens 
112817f17c2dSbonwick /*
112917f17c2dSbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
113017f17c2dSbonwick  */
1131fa9e4066Sahrens static void
1132a3b55830SMatthew Ahrens vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1133a3b55830SMatthew Ahrens     uberblock_t *ub, vdev_t *vd, int flags)
1134fa9e4066Sahrens {
1135a3b55830SMatthew Ahrens 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
1136a3b55830SMatthew Ahrens 		vdev_uberblock_sync(zio, good_writes,
1137a3b55830SMatthew Ahrens 		    ub, vd->vdev_child[c], flags);
1138a3b55830SMatthew Ahrens 	}
1139fa9e4066Sahrens 
1140fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1141fa9e4066Sahrens 		return;
1142fa9e4066Sahrens 
1143e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1144fa9e4066Sahrens 		return;
1145fa9e4066Sahrens 
1146e0f1c0afSOlaf Faaland 	int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1147e0f1c0afSOlaf Faaland 	int n = ub->ub_txg % (VDEV_UBERBLOCK_COUNT(vd) - m);
1148fa9e4066Sahrens 
1149770499e1SDan Kimmel 	/* Copy the uberblock_t into the ABD */
1150770499e1SDan Kimmel 	abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1151770499e1SDan Kimmel 	abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1152770499e1SDan Kimmel 	abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1153fa9e4066Sahrens 
1154e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++)
1155770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, ub_abd,
1156e14bb325SJeff Bonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1157a3b55830SMatthew Ahrens 		    vdev_uberblock_sync_done, good_writes,
1158e14bb325SJeff Bonwick 		    flags | ZIO_FLAG_DONT_PROPAGATE);
1159fa9e4066Sahrens 
1160770499e1SDan Kimmel 	abd_free(ub_abd);
1161fa9e4066Sahrens }
1162fa9e4066Sahrens 
11633e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */
116417f17c2dSbonwick int
116517f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1166fa9e4066Sahrens {
116717f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1168e14bb325SJeff Bonwick 	zio_t *zio;
116917f17c2dSbonwick 	uint64_t good_writes = 0;
1170fa9e4066Sahrens 
1171a3b55830SMatthew Ahrens 	zio = zio_root(spa, NULL, NULL, flags);
1172e14bb325SJeff Bonwick 
1173e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
1174a3b55830SMatthew Ahrens 		vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1175fa9e4066Sahrens 
117617f17c2dSbonwick 	(void) zio_wait(zio);
1177fa9e4066Sahrens 
1178fa9e4066Sahrens 	/*
117917f17c2dSbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
118017f17c2dSbonwick 	 * are no longer needed (because the new uberblocks and the even
118117f17c2dSbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
1182fa9e4066Sahrens 	 */
118317f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
1184fa9e4066Sahrens 
11855cabbc6bSPrashanth Sreenivasa 	for (int v = 0; v < svdcount; v++) {
11865cabbc6bSPrashanth Sreenivasa 		if (vdev_writeable(svd[v])) {
11875cabbc6bSPrashanth Sreenivasa 			zio_flush(zio, svd[v]);
11885cabbc6bSPrashanth Sreenivasa 		}
11895cabbc6bSPrashanth Sreenivasa 	}
1190fa9e4066Sahrens 
119117f17c2dSbonwick 	(void) zio_wait(zio);
119217f17c2dSbonwick 
119317f17c2dSbonwick 	return (good_writes >= 1 ? 0 : EIO);
1194fa9e4066Sahrens }
1195fa9e4066Sahrens 
1196fa9e4066Sahrens /*
119717f17c2dSbonwick  * On success, increment the count of good writes for our top-level vdev.
1198fa9e4066Sahrens  */
1199fa9e4066Sahrens static void
120017f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
1201fa9e4066Sahrens {
120217f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1203fa9e4066Sahrens 
1204fa9e4066Sahrens 	if (zio->io_error == 0)
12051a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(good_writes);
1206fa9e4066Sahrens }
1207fa9e4066Sahrens 
120817f17c2dSbonwick /*
120917f17c2dSbonwick  * If there weren't enough good writes, indicate failure to the parent.
121017f17c2dSbonwick  */
1211fa9e4066Sahrens static void
121217f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
121317f17c2dSbonwick {
121417f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
121517f17c2dSbonwick 
121617f17c2dSbonwick 	if (*good_writes == 0)
1217be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(EIO);
121817f17c2dSbonwick 
121917f17c2dSbonwick 	kmem_free(good_writes, sizeof (uint64_t));
122017f17c2dSbonwick }
122117f17c2dSbonwick 
122251ece835Seschrock /*
12230430f8daSeschrock  * We ignore errors for log and cache devices, simply free the private data.
122451ece835Seschrock  */
122551ece835Seschrock static void
12260430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio)
122751ece835Seschrock {
122851ece835Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
122951ece835Seschrock }
123051ece835Seschrock 
123117f17c2dSbonwick /*
123217f17c2dSbonwick  * Write all even or odd labels to all leaves of the specified vdev.
123317f17c2dSbonwick  */
123417f17c2dSbonwick static void
1235a3b55830SMatthew Ahrens vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1236a3b55830SMatthew Ahrens     vdev_t *vd, int l, uint64_t txg, int flags)
1237fa9e4066Sahrens {
1238fa9e4066Sahrens 	nvlist_t *label;
1239fa9e4066Sahrens 	vdev_phys_t *vp;
1240770499e1SDan Kimmel 	abd_t *vp_abd;
1241fa9e4066Sahrens 	char *buf;
1242fa9e4066Sahrens 	size_t buflen;
1243fa9e4066Sahrens 
1244a3b55830SMatthew Ahrens 	for (int c = 0; c < vd->vdev_children; c++) {
1245a3b55830SMatthew Ahrens 		vdev_label_sync(zio, good_writes,
1246a3b55830SMatthew Ahrens 		    vd->vdev_child[c], l, txg, flags);
1247a3b55830SMatthew Ahrens 	}
1248fa9e4066Sahrens 
1249fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1250fa9e4066Sahrens 		return;
1251fa9e4066Sahrens 
1252e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1253fa9e4066Sahrens 		return;
1254fa9e4066Sahrens 
1255fa9e4066Sahrens 	/*
1256fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
1257fa9e4066Sahrens 	 */
12580373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1259fa9e4066Sahrens 
1260770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1261770499e1SDan Kimmel 	abd_zero(vp_abd, sizeof (vdev_phys_t));
1262770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
1263fa9e4066Sahrens 
1264fa9e4066Sahrens 	buf = vp->vp_nvlist;
1265fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
1266fa9e4066Sahrens 
126717f17c2dSbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
126817f17c2dSbonwick 		for (; l < VDEV_LABELS; l += 2) {
1269770499e1SDan Kimmel 			vdev_label_write(zio, vd, l, vp_abd,
127017f17c2dSbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
127117f17c2dSbonwick 			    sizeof (vdev_phys_t),
1272a3b55830SMatthew Ahrens 			    vdev_label_sync_done, good_writes,
1273e14bb325SJeff Bonwick 			    flags | ZIO_FLAG_DONT_PROPAGATE);
127417f17c2dSbonwick 		}
127517f17c2dSbonwick 	}
1276fa9e4066Sahrens 
1277770499e1SDan Kimmel 	abd_free(vp_abd);
1278fa9e4066Sahrens 	nvlist_free(label);
1279fa9e4066Sahrens }
1280fa9e4066Sahrens 
128117f17c2dSbonwick int
1282e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1283fa9e4066Sahrens {
1284e14bb325SJeff Bonwick 	list_t *dl = &spa->spa_config_dirty_list;
128517f17c2dSbonwick 	vdev_t *vd;
1286e14bb325SJeff Bonwick 	zio_t *zio;
1287fa9e4066Sahrens 	int error;
1288fa9e4066Sahrens 
1289fa9e4066Sahrens 	/*
1290e14bb325SJeff Bonwick 	 * Write the new labels to disk.
1291fa9e4066Sahrens 	 */
1292e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, flags);
1293fa9e4066Sahrens 
129417f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
129517f17c2dSbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
129617f17c2dSbonwick 		    KM_SLEEP);
129788ecc943SGeorge Wilson 
129888ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
129988ecc943SGeorge Wilson 
1300a3f829aeSBill Moore 		zio_t *vio = zio_null(zio, spa, NULL,
13010430f8daSeschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
13020430f8daSeschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
130317f17c2dSbonwick 		    good_writes, flags);
1304a3b55830SMatthew Ahrens 		vdev_label_sync(vio, good_writes, vd, l, txg, flags);
130517f17c2dSbonwick 		zio_nowait(vio);
1306fa9e4066Sahrens 	}
1307e14bb325SJeff Bonwick 
1308e14bb325SJeff Bonwick 	error = zio_wait(zio);
1309fa9e4066Sahrens 
13108654d025Sperrin 	/*
131117f17c2dSbonwick 	 * Flush the new labels to disk.
13128654d025Sperrin 	 */
131317f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
13148654d025Sperrin 
131517f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
131617f17c2dSbonwick 		zio_flush(zio, vd);
131717f17c2dSbonwick 
131817f17c2dSbonwick 	(void) zio_wait(zio);
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 	return (error);
1321fa9e4066Sahrens }
1322fa9e4066Sahrens 
1323fa9e4066Sahrens /*
132417f17c2dSbonwick  * Sync the uberblock and any changes to the vdev configuration.
1325fa9e4066Sahrens  *
1326fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
1327fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
1328fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
1329fa9e4066Sahrens  * describe the failure semantics at each stage.
1330fa9e4066Sahrens  *
133117f17c2dSbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1332fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
1333fa9e4066Sahrens  */
1334e14bb325SJeff Bonwick int
1335eb5bb584SWill Andrews vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1336fa9e4066Sahrens {
133717f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1338fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
1339eb5bb584SWill Andrews 	int error = 0;
1340e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1341fa9e4066Sahrens 
134286714001SSerapheim Dimitropoulos 	ASSERT(svdcount != 0);
1343eb5bb584SWill Andrews retry:
13448956713aSEric Schrock 	/*
13458956713aSEric Schrock 	 * Normally, we don't want to try too hard to write every label and
13468956713aSEric Schrock 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
13478956713aSEric Schrock 	 * sync process to block while we retry.  But if we can't write a
13488956713aSEric Schrock 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
13498956713aSEric Schrock 	 * bailing out and declaring the pool faulted.
13508956713aSEric Schrock 	 */
1351eb5bb584SWill Andrews 	if (error != 0) {
1352eb5bb584SWill Andrews 		if ((flags & ZIO_FLAG_TRYHARD) != 0)
1353eb5bb584SWill Andrews 			return (error);
13548956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
1355eb5bb584SWill Andrews 	}
13568956713aSEric Schrock 
1357fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
1358fa9e4066Sahrens 
1359fa9e4066Sahrens 	/*
136017f17c2dSbonwick 	 * If this isn't a resync due to I/O errors,
136117f17c2dSbonwick 	 * and nothing changed in this transaction group,
136217f17c2dSbonwick 	 * and the vdev configuration hasn't changed,
13630373e76bSbonwick 	 * then there's nothing to do.
1364fa9e4066Sahrens 	 */
1365e0f1c0afSOlaf Faaland 	if (ub->ub_txg < txg) {
1366e0f1c0afSOlaf Faaland 		boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
1367e0f1c0afSOlaf Faaland 		    txg, spa->spa_mmp.mmp_delay);
1368e0f1c0afSOlaf Faaland 
1369e0f1c0afSOlaf Faaland 		if (!changed && list_is_empty(&spa->spa_config_dirty_list))
1370e0f1c0afSOlaf Faaland 			return (0);
1371e0f1c0afSOlaf Faaland 	}
1372fa9e4066Sahrens 
1373fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
1374e14bb325SJeff Bonwick 		return (0);
1375fa9e4066Sahrens 
13760373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
13770373e76bSbonwick 
1378fa9e4066Sahrens 	/*
1379fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
1380fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
1381fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
1382fa9e4066Sahrens 	 * before any uberblock that references them.
1383fa9e4066Sahrens 	 */
138486714001SSerapheim Dimitropoulos 	zio_t *zio = zio_root(spa, NULL, NULL, flags);
138517f17c2dSbonwick 
138686714001SSerapheim Dimitropoulos 	for (vdev_t *vd =
138786714001SSerapheim Dimitropoulos 	    txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
138817f17c2dSbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
138917f17c2dSbonwick 		zio_flush(zio, vd);
139017f17c2dSbonwick 
1391fa9e4066Sahrens 	(void) zio_wait(zio);
1392fa9e4066Sahrens 
1393fa9e4066Sahrens 	/*
1394fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1395fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1396fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1397fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
139817f17c2dSbonwick 	 * which have not yet been touched, will still be valid.  We flush
139917f17c2dSbonwick 	 * the new labels to disk to ensure that all even-label updates
140017f17c2dSbonwick 	 * are committed to stable storage before the uberblock update.
1401fa9e4066Sahrens 	 */
140286714001SSerapheim Dimitropoulos 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
140386714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
140486714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
140586714001SSerapheim Dimitropoulos 			    "for pool '%s' when syncing out the even labels "
140686714001SSerapheim Dimitropoulos 			    "of dirty vdevs", error, spa_name(spa));
140786714001SSerapheim Dimitropoulos 		}
1408eb5bb584SWill Andrews 		goto retry;
140986714001SSerapheim Dimitropoulos 	}
1410fa9e4066Sahrens 
1411fa9e4066Sahrens 	/*
1412e14bb325SJeff Bonwick 	 * Sync the uberblocks to all vdevs in svd[].
14130373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
14140373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1415fa9e4066Sahrens 	 *
1416fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1417fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1418fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1419fa9e4066Sahrens 	 *	to that uberblock.
1420fa9e4066Sahrens 	 *
1421fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1422fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1423fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1424fa9e4066Sahrens 	 *	to the new uberblocks.
1425fa9e4066Sahrens 	 */
142686714001SSerapheim Dimitropoulos 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
142786714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
142886714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
142986714001SSerapheim Dimitropoulos 			    "%d for pool '%s'", error, spa_name(spa));
143086714001SSerapheim Dimitropoulos 		}
1431eb5bb584SWill Andrews 		goto retry;
143286714001SSerapheim Dimitropoulos 	}
1433fa9e4066Sahrens 
1434e0f1c0afSOlaf Faaland 	if (spa_multihost(spa))
1435e0f1c0afSOlaf Faaland 		mmp_update_uberblock(spa, ub);
1436e0f1c0afSOlaf Faaland 
1437fa9e4066Sahrens 	/*
1438fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1439fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1440fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1441fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1442fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
144317f17c2dSbonwick 	 * all labels will be brought up to date.  We flush the new labels
144417f17c2dSbonwick 	 * to disk to ensure that all odd-label updates are committed to
144517f17c2dSbonwick 	 * stable storage before the next transaction group begins.
1446fa9e4066Sahrens 	 */
144786714001SSerapheim Dimitropoulos 	if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
144886714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
144986714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
145086714001SSerapheim Dimitropoulos 			    "for pool '%s' when syncing out the odd labels of "
145186714001SSerapheim Dimitropoulos 			    "dirty vdevs", error, spa_name(spa));
145286714001SSerapheim Dimitropoulos 		}
1453eb5bb584SWill Andrews 		goto retry;
145486714001SSerapheim Dimitropoulos 	}
1455eb5bb584SWill Andrews 
1456eb5bb584SWill Andrews 	return (0);
1457fa9e4066Sahrens }
1458