xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision e4c795beb33bf59dd4ad2e3f88f493111484b890)
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.
25663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
26e0f1c0afSOlaf Faaland  * Copyright 2019 Joyent, Inc.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * Virtual Device Labels
31fa9e4066Sahrens  * ---------------------
32fa9e4066Sahrens  *
33fa9e4066Sahrens  * The vdev label serves several distinct purposes:
34fa9e4066Sahrens  *
35fa9e4066Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
36fa9e4066Sahrens  *	   identity within the pool.
37fa9e4066Sahrens  *
38cfd63e1bSMatthew Ahrens  *	2. Verify that all the devices given in a configuration are present
39fa9e4066Sahrens  *         within the pool.
40fa9e4066Sahrens  *
41cfd63e1bSMatthew Ahrens  *	3. Determine the uberblock for the pool.
42fa9e4066Sahrens  *
43cfd63e1bSMatthew Ahrens  *	4. In case of an import operation, determine the configuration of the
44fa9e4066Sahrens  *         toplevel vdev of which it is a part.
45fa9e4066Sahrens  *
46cfd63e1bSMatthew Ahrens  *	5. If an import operation cannot find all the devices in the pool,
47fa9e4066Sahrens  *         provide enough information to the administrator to determine which
48fa9e4066Sahrens  *         devices are missing.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * It is important to note that while the kernel is responsible for writing the
51fa9e4066Sahrens  * label, it only consumes the information in the first three cases.  The
52fa9e4066Sahrens  * latter information is only consumed in userland when determining the
53fa9e4066Sahrens  * configuration to import a pool.
54fa9e4066Sahrens  *
55fa9e4066Sahrens  *
56fa9e4066Sahrens  * Label Organization
57fa9e4066Sahrens  * ------------------
58fa9e4066Sahrens  *
59fa9e4066Sahrens  * Before describing the contents of the label, it's important to understand how
60fa9e4066Sahrens  * the labels are written and updated with respect to the uberblock.
61fa9e4066Sahrens  *
62fa9e4066Sahrens  * When the pool configuration is altered, either because it was newly created
63fa9e4066Sahrens  * or a device was added, we want to update all the labels such that we can deal
64fa9e4066Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
65fa9e4066Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
663d7072f8Seschrock  * labels and an uberblock with the following transaction groups:
67fa9e4066Sahrens  *
68fa9e4066Sahrens  *              L1          UB          L2
69fa9e4066Sahrens  *           +------+    +------+    +------+
70fa9e4066Sahrens  *           |      |    |      |    |      |
71fa9e4066Sahrens  *           | t10  |    | t10  |    | t10  |
72fa9e4066Sahrens  *           |      |    |      |    |      |
73fa9e4066Sahrens  *           +------+    +------+    +------+
74fa9e4066Sahrens  *
75fa9e4066Sahrens  * In this stable state, the labels and the uberblock were all updated within
76fa9e4066Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
77fa9e4066Sahrens  * that we can detect when we fail partway through writing the label.
78fa9e4066Sahrens  *
79fa9e4066Sahrens  * In order to identify which labels are valid, the labels are written in the
80fa9e4066Sahrens  * following manner:
81fa9e4066Sahrens  *
82cfd63e1bSMatthew Ahrens  *	1. For each vdev, update 'L1' to the new label
83cfd63e1bSMatthew Ahrens  *	2. Update the uberblock
84cfd63e1bSMatthew Ahrens  *	3. For each vdev, update 'L2' to the new label
85fa9e4066Sahrens  *
86fa9e4066Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
87fa9e4066Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
88fa9e4066Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
89fa9e4066Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
90fa9e4066Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
91fa9e4066Sahrens  * therefore L1 is valid.
92fa9e4066Sahrens  *
93fa9e4066Sahrens  * Another added complexity is that not every label is updated when the config
94fa9e4066Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
95fa9e4066Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
96fa9e4066Sahrens  * be older than the pool uberblock, because the necessary information is stored
97fa9e4066Sahrens  * on another vdev.
98fa9e4066Sahrens  *
99fa9e4066Sahrens  *
100fa9e4066Sahrens  * On-disk Format
101fa9e4066Sahrens  * --------------
102fa9e4066Sahrens  *
103fa9e4066Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
104fa9e4066Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
105fa9e4066Sahrens  * VTOC disk labels, but is otherwise ignored.
106fa9e4066Sahrens  *
107fa9e4066Sahrens  * The first half of the label is a packed nvlist which contains pool wide
108fa9e4066Sahrens  * properties, per-vdev properties, and configuration information.  It is
109fa9e4066Sahrens  * described in more detail below.
110fa9e4066Sahrens  *
111fa9e4066Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
112fa9e4066Sahrens  * These uberblocks are updated whenever a transaction group is committed,
113fa9e4066Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
114fa9e4066Sahrens  * vdev for the 'best' uberblock.
115fa9e4066Sahrens  *
116fa9e4066Sahrens  *
117fa9e4066Sahrens  * Configuration Information
118fa9e4066Sahrens  * -------------------------
119fa9e4066Sahrens  *
120fa9e4066Sahrens  * The nvlist describing the pool and vdev contains the following elements:
121fa9e4066Sahrens  *
122cfd63e1bSMatthew Ahrens  *	version		ZFS on-disk version
123cfd63e1bSMatthew Ahrens  *	name		Pool name
124cfd63e1bSMatthew Ahrens  *	state		Pool state
125cfd63e1bSMatthew Ahrens  *	txg		Transaction group in which this label was written
126cfd63e1bSMatthew Ahrens  *	pool_guid	Unique identifier for this pool
127cfd63e1bSMatthew Ahrens  *	vdev_tree	An nvlist describing vdev tree.
128ad135b5dSChristopher Siden  *	features_for_read
129ad135b5dSChristopher Siden  *			An nvlist of the features necessary for reading the MOS.
130fa9e4066Sahrens  *
131fa9e4066Sahrens  * Each leaf device label also contains the following:
132fa9e4066Sahrens  *
133cfd63e1bSMatthew Ahrens  *	top_guid	Unique ID for top-level vdev in which this is contained
134cfd63e1bSMatthew Ahrens  *	guid		Unique ID for the leaf vdev
135fa9e4066Sahrens  *
136fa9e4066Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
137fa9e4066Sahrens  */
138fa9e4066Sahrens 
139fa9e4066Sahrens #include <sys/zfs_context.h>
140fa9e4066Sahrens #include <sys/spa.h>
141fa9e4066Sahrens #include <sys/spa_impl.h>
142fa9e4066Sahrens #include <sys/dmu.h>
143fa9e4066Sahrens #include <sys/zap.h>
144fa9e4066Sahrens #include <sys/vdev.h>
145fa9e4066Sahrens #include <sys/vdev_impl.h>
146fa9e4066Sahrens #include <sys/uberblock_impl.h>
147fa9e4066Sahrens #include <sys/metaslab.h>
1485cabbc6bSPrashanth Sreenivasa #include <sys/metaslab_impl.h>
149fa9e4066Sahrens #include <sys/zio.h>
1503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
151770499e1SDan Kimmel #include <sys/abd.h>
152fa9e4066Sahrens #include <sys/fs/zfs.h>
153fa9e4066Sahrens 
154fa9e4066Sahrens /*
155fa9e4066Sahrens  * Basic routines to read and write from a vdev label.
156fa9e4066Sahrens  * Used throughout the rest of this file.
157fa9e4066Sahrens  */
158fa9e4066Sahrens uint64_t
159fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
160fa9e4066Sahrens {
161ecc2d604Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
162e7437265Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
163ecc2d604Sbonwick 
164fa9e4066Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
165fa9e4066Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
166fa9e4066Sahrens }
167fa9e4066Sahrens 
16821bf64a7Sgw /*
16921bf64a7Sgw  * Returns back the vdev label associated with the passed in offset.
17021bf64a7Sgw  */
17121bf64a7Sgw int
17221bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset)
17321bf64a7Sgw {
17421bf64a7Sgw 	int l;
17521bf64a7Sgw 
17621bf64a7Sgw 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
17721bf64a7Sgw 		offset -= psize - VDEV_LABEL_END_SIZE;
17821bf64a7Sgw 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
17921bf64a7Sgw 	}
18021bf64a7Sgw 	l = offset / sizeof (vdev_label_t);
18121bf64a7Sgw 	return (l < VDEV_LABELS ? l : -1);
18221bf64a7Sgw }
18321bf64a7Sgw 
184fa9e4066Sahrens static void
185770499e1SDan Kimmel vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
1869a686fbcSPaul Dagnelie     uint64_t size, zio_done_func_t *done, void *private, int flags)
187fa9e4066Sahrens {
18858447f68SOlaf Faaland 	ASSERT(
18958447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
19058447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
191e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
192fa9e4066Sahrens 
193fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
194fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
195fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
196e14bb325SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
197fa9e4066Sahrens }
198fa9e4066Sahrens 
199e0f1c0afSOlaf Faaland void
200770499e1SDan Kimmel vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
2019a686fbcSPaul Dagnelie     uint64_t size, zio_done_func_t *done, void *private, int flags)
202fa9e4066Sahrens {
20358447f68SOlaf Faaland 	ASSERT(
20458447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
20558447f68SOlaf Faaland 	    spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
206e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
207fa9e4066Sahrens 
208fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
209fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
210fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
21117f17c2dSbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
212fa9e4066Sahrens }
213fa9e4066Sahrens 
21486714001SSerapheim Dimitropoulos static void
21586714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
21686714001SSerapheim Dimitropoulos {
21786714001SSerapheim Dimitropoulos 	spa_t *spa = vd->vdev_spa;
21886714001SSerapheim Dimitropoulos 
21986714001SSerapheim Dimitropoulos 	if (vd != spa->spa_root_vdev)
22086714001SSerapheim Dimitropoulos 		return;
22186714001SSerapheim Dimitropoulos 
22286714001SSerapheim Dimitropoulos 	/* provide either current or previous scan information */
22386714001SSerapheim Dimitropoulos 	pool_scan_stat_t ps;
22486714001SSerapheim Dimitropoulos 	if (spa_scan_get_stats(spa, &ps) == 0) {
22586714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
22686714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
22786714001SSerapheim Dimitropoulos 		    sizeof (pool_scan_stat_t) / sizeof (uint64_t));
22886714001SSerapheim Dimitropoulos 	}
22986714001SSerapheim Dimitropoulos 
23086714001SSerapheim Dimitropoulos 	pool_removal_stat_t prs;
23186714001SSerapheim Dimitropoulos 	if (spa_removal_get_stats(spa, &prs) == 0) {
23286714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
23386714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
23486714001SSerapheim Dimitropoulos 		    sizeof (prs) / sizeof (uint64_t));
23586714001SSerapheim Dimitropoulos 	}
23686714001SSerapheim Dimitropoulos 
23786714001SSerapheim Dimitropoulos 	pool_checkpoint_stat_t pcs;
23886714001SSerapheim Dimitropoulos 	if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
23986714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
24086714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
24186714001SSerapheim Dimitropoulos 		    sizeof (pcs) / sizeof (uint64_t));
24286714001SSerapheim Dimitropoulos 	}
24386714001SSerapheim Dimitropoulos }
24486714001SSerapheim Dimitropoulos 
245fa9e4066Sahrens /*
246fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
247fa9e4066Sahrens  */
248fa9e4066Sahrens nvlist_t *
24999653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
2503f9d6ad7SLin Ling     vdev_config_flag_t flags)
251fa9e4066Sahrens {
252fa9e4066Sahrens 	nvlist_t *nv = NULL;
2535cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
254fa9e4066Sahrens 
255b4952e17SGeorge Wilson 	nv = fnvlist_alloc();
256fa9e4066Sahrens 
257b4952e17SGeorge Wilson 	fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
2583f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
259b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
260b4952e17SGeorge Wilson 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
261fa9e4066Sahrens 
262fa9e4066Sahrens 	if (vd->vdev_path != NULL)
263b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
264fa9e4066Sahrens 
265fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
266b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
267fa9e4066Sahrens 
2683d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
269b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
270b4952e17SGeorge Wilson 		    vd->vdev_physpath);
2713d7072f8Seschrock 
2726809eb4eSEric Schrock 	if (vd->vdev_fru != NULL)
273b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
2746809eb4eSEric Schrock 
27599653d4eSeschrock 	if (vd->vdev_nparity != 0) {
27699653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
27799653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
27899653d4eSeschrock 
27999653d4eSeschrock 		/*
28099653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
28199653d4eSeschrock 		 * into a crufty old storage pool.
28299653d4eSeschrock 		 */
28399653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
284f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 2 &&
285f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
286f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 3 &&
287f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
28899653d4eSeschrock 
28999653d4eSeschrock 		/*
29099653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
29199653d4eSeschrock 		 * that only support a single parity device -- older software
29299653d4eSeschrock 		 * will just ignore it.
29399653d4eSeschrock 		 */
294b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
29599653d4eSeschrock 	}
29699653d4eSeschrock 
297afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
298b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
299b4952e17SGeorge Wilson 		    vd->vdev_wholedisk);
300afefbcddSeschrock 
3016f793812SPavel Zakharov 	if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
302b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
303ea8dc4b6Seschrock 
30499653d4eSeschrock 	if (vd->vdev_isspare)
305b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
30699653d4eSeschrock 
3073f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
3083f9d6ad7SLin Ling 	    vd == vd->vdev_top) {
309b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
310b4952e17SGeorge Wilson 		    vd->vdev_ms_array);
311b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
312b4952e17SGeorge Wilson 		    vd->vdev_ms_shift);
313b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
314b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
315b4952e17SGeorge Wilson 		    vd->vdev_asize);
316b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
3175cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_removing) {
318b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
319b4952e17SGeorge Wilson 			    vd->vdev_removing);
3205cabbc6bSPrashanth Sreenivasa 		}
321663207adSDon Brady 
322663207adSDon Brady 		/* zpool command expects alloc class data */
323663207adSDon Brady 		if (getstats && vd->vdev_alloc_bias != VDEV_BIAS_NONE) {
324663207adSDon Brady 			const char *bias = NULL;
325663207adSDon Brady 
326663207adSDon Brady 			switch (vd->vdev_alloc_bias) {
327663207adSDon Brady 			case VDEV_BIAS_LOG:
328663207adSDon Brady 				bias = VDEV_ALLOC_BIAS_LOG;
329663207adSDon Brady 				break;
330663207adSDon Brady 			case VDEV_BIAS_SPECIAL:
331663207adSDon Brady 				bias = VDEV_ALLOC_BIAS_SPECIAL;
332663207adSDon Brady 				break;
333663207adSDon Brady 			case VDEV_BIAS_DEDUP:
334663207adSDon Brady 				bias = VDEV_ALLOC_BIAS_DEDUP;
335663207adSDon Brady 				break;
336663207adSDon Brady 			default:
337663207adSDon Brady 				ASSERT3U(vd->vdev_alloc_bias, ==,
338663207adSDon Brady 				    VDEV_BIAS_NONE);
339663207adSDon Brady 			}
340663207adSDon Brady 			fnvlist_add_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
341663207adSDon Brady 			    bias);
342663207adSDon Brady 		}
343fa9e4066Sahrens 	}
344fa9e4066Sahrens 
3450713e232SGeorge Wilson 	if (vd->vdev_dtl_sm != NULL) {
346b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
3470713e232SGeorge Wilson 		    space_map_object(vd->vdev_dtl_sm));
3480713e232SGeorge Wilson 	}
349fa9e4066Sahrens 
3505cabbc6bSPrashanth Sreenivasa 	if (vic->vic_mapping_object != 0) {
3515cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
3525cabbc6bSPrashanth Sreenivasa 		    vic->vic_mapping_object);
3535cabbc6bSPrashanth Sreenivasa 	}
3545cabbc6bSPrashanth Sreenivasa 
3555cabbc6bSPrashanth Sreenivasa 	if (vic->vic_births_object != 0) {
3565cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
3575cabbc6bSPrashanth Sreenivasa 		    vic->vic_births_object);
3585cabbc6bSPrashanth Sreenivasa 	}
3595cabbc6bSPrashanth Sreenivasa 
3605cabbc6bSPrashanth Sreenivasa 	if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
3615cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
3625cabbc6bSPrashanth Sreenivasa 		    vic->vic_prev_indirect_vdev);
3635cabbc6bSPrashanth Sreenivasa 	}
3645cabbc6bSPrashanth Sreenivasa 
36588ecc943SGeorge Wilson 	if (vd->vdev_crtxg)
366b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
36788ecc943SGeorge Wilson 
368215198a6SJoe Stein 	if (flags & VDEV_CONFIG_MOS) {
369215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0) {
370215198a6SJoe Stein 			ASSERT(vd->vdev_ops->vdev_op_leaf);
371215198a6SJoe Stein 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
372215198a6SJoe Stein 			    vd->vdev_leaf_zap);
373215198a6SJoe Stein 		}
374215198a6SJoe Stein 
375215198a6SJoe Stein 		if (vd->vdev_top_zap != 0) {
376215198a6SJoe Stein 			ASSERT(vd == vd->vdev_top);
377215198a6SJoe Stein 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
378215198a6SJoe Stein 			    vd->vdev_top_zap);
379215198a6SJoe Stein 		}
380*e4c795beSTom Caputi 
381*e4c795beSTom Caputi 		if (vd->vdev_resilver_deferred) {
382*e4c795beSTom Caputi 			ASSERT(vd->vdev_ops->vdev_op_leaf);
383*e4c795beSTom Caputi 			ASSERT(spa->spa_resilver_deferred);
384*e4c795beSTom Caputi 			fnvlist_add_boolean(nv, ZPOOL_CONFIG_RESILVER_DEFER);
385*e4c795beSTom Caputi 		}
386215198a6SJoe Stein 	}
387215198a6SJoe Stein 
388fa9e4066Sahrens 	if (getstats) {
389fa9e4066Sahrens 		vdev_stat_t vs;
3903f9d6ad7SLin Ling 
391fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
392b4952e17SGeorge Wilson 		fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
393b4952e17SGeorge Wilson 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t));
3943f9d6ad7SLin Ling 
39586714001SSerapheim Dimitropoulos 		root_vdev_actions_getprogress(vd, nv);
3965cabbc6bSPrashanth Sreenivasa 
3975cabbc6bSPrashanth Sreenivasa 		/*
3985cabbc6bSPrashanth Sreenivasa 		 * Note: this can be called from open context
3995cabbc6bSPrashanth Sreenivasa 		 * (spa_get_stats()), so we need the rwlock to prevent
4005cabbc6bSPrashanth Sreenivasa 		 * the mapping from being changed by condensing.
4015cabbc6bSPrashanth Sreenivasa 		 */
4025cabbc6bSPrashanth Sreenivasa 		rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
4035cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_indirect_mapping != NULL) {
4045cabbc6bSPrashanth Sreenivasa 			ASSERT(vd->vdev_indirect_births != NULL);
4055cabbc6bSPrashanth Sreenivasa 			vdev_indirect_mapping_t *vim =
4065cabbc6bSPrashanth Sreenivasa 			    vd->vdev_indirect_mapping;
4075cabbc6bSPrashanth Sreenivasa 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
4085cabbc6bSPrashanth Sreenivasa 			    vdev_indirect_mapping_size(vim));
4095cabbc6bSPrashanth Sreenivasa 		}
4105cabbc6bSPrashanth Sreenivasa 		rw_exit(&vd->vdev_indirect_rwlock);
4115cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_mg != NULL &&
4125cabbc6bSPrashanth Sreenivasa 		    vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
4135cabbc6bSPrashanth Sreenivasa 			/*
4145cabbc6bSPrashanth Sreenivasa 			 * Compute approximately how much memory would be used
4155cabbc6bSPrashanth Sreenivasa 			 * for the indirect mapping if this device were to
4165cabbc6bSPrashanth Sreenivasa 			 * be removed.
4175cabbc6bSPrashanth Sreenivasa 			 *
4185cabbc6bSPrashanth Sreenivasa 			 * Note: If the frag metric is invalid, then not
4195cabbc6bSPrashanth Sreenivasa 			 * enough metaslabs have been converted to have
4205cabbc6bSPrashanth Sreenivasa 			 * histograms.
4215cabbc6bSPrashanth Sreenivasa 			 */
4225cabbc6bSPrashanth Sreenivasa 			uint64_t seg_count = 0;
423cfd63e1bSMatthew Ahrens 			uint64_t to_alloc = vd->vdev_stat.vs_alloc;
4245cabbc6bSPrashanth Sreenivasa 
4255cabbc6bSPrashanth Sreenivasa 			/*
4265cabbc6bSPrashanth Sreenivasa 			 * There are the same number of allocated segments
4275cabbc6bSPrashanth Sreenivasa 			 * as free segments, so we will have at least one
428cfd63e1bSMatthew Ahrens 			 * entry per free segment.  However, small free
429cfd63e1bSMatthew Ahrens 			 * segments (smaller than vdev_removal_max_span)
430cfd63e1bSMatthew Ahrens 			 * will be combined with adjacent allocated segments
431cfd63e1bSMatthew Ahrens 			 * as a single mapping.
4325cabbc6bSPrashanth Sreenivasa 			 */
4335cabbc6bSPrashanth Sreenivasa 			for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
434cfd63e1bSMatthew Ahrens 				if (1ULL << (i + 1) < vdev_removal_max_span) {
435cfd63e1bSMatthew Ahrens 					to_alloc +=
436cfd63e1bSMatthew Ahrens 					    vd->vdev_mg->mg_histogram[i] <<
437cfd63e1bSMatthew Ahrens 					    i + 1;
438cfd63e1bSMatthew Ahrens 				} else {
439cfd63e1bSMatthew Ahrens 					seg_count +=
440cfd63e1bSMatthew Ahrens 					    vd->vdev_mg->mg_histogram[i];
441cfd63e1bSMatthew Ahrens 				}
4425cabbc6bSPrashanth Sreenivasa 			}
4435cabbc6bSPrashanth Sreenivasa 
4445cabbc6bSPrashanth Sreenivasa 			/*
445cfd63e1bSMatthew Ahrens 			 * The maximum length of a mapping is
446cfd63e1bSMatthew Ahrens 			 * zfs_remove_max_segment, so we need at least one entry
447cfd63e1bSMatthew Ahrens 			 * per zfs_remove_max_segment of allocated data.
4485cabbc6bSPrashanth Sreenivasa 			 */
449cfd63e1bSMatthew Ahrens 			seg_count += to_alloc / zfs_remove_max_segment;
4505cabbc6bSPrashanth Sreenivasa 
4515cabbc6bSPrashanth Sreenivasa 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
4525cabbc6bSPrashanth Sreenivasa 			    seg_count *
4535cabbc6bSPrashanth Sreenivasa 			    sizeof (vdev_indirect_mapping_entry_phys_t));
4545cabbc6bSPrashanth Sreenivasa 		}
455fa9e4066Sahrens 	}
456fa9e4066Sahrens 
457fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
458fa9e4066Sahrens 		nvlist_t **child;
4593f9d6ad7SLin Ling 		int c, idx;
460fa9e4066Sahrens 
46188ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
46288ecc943SGeorge Wilson 
463fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
464fa9e4066Sahrens 		    KM_SLEEP);
465fa9e4066Sahrens 
4663f9d6ad7SLin Ling 		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
4673f9d6ad7SLin Ling 			vdev_t *cvd = vd->vdev_child[c];
4683f9d6ad7SLin Ling 
4693f9d6ad7SLin Ling 			/*
4703f9d6ad7SLin Ling 			 * If we're generating an nvlist of removing
4713f9d6ad7SLin Ling 			 * vdevs then skip over any device which is
4723f9d6ad7SLin Ling 			 * not being removed.
4733f9d6ad7SLin Ling 			 */
4743f9d6ad7SLin Ling 			if ((flags & VDEV_CONFIG_REMOVING) &&
4753f9d6ad7SLin Ling 			    !cvd->vdev_removing)
4763f9d6ad7SLin Ling 				continue;
477fa9e4066Sahrens 
4783f9d6ad7SLin Ling 			child[idx++] = vdev_config_generate(spa, cvd,
4793f9d6ad7SLin Ling 			    getstats, flags);
4803f9d6ad7SLin Ling 		}
4813f9d6ad7SLin Ling 
4823f9d6ad7SLin Ling 		if (idx) {
483b4952e17SGeorge Wilson 			fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
484b4952e17SGeorge Wilson 			    child, idx);
4853f9d6ad7SLin Ling 		}
486fa9e4066Sahrens 
4873f9d6ad7SLin Ling 		for (c = 0; c < idx; c++)
488fa9e4066Sahrens 			nvlist_free(child[c]);
489fa9e4066Sahrens 
490fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
491441d80aaSlling 
492441d80aaSlling 	} else {
493069f55e2SEric Schrock 		const char *aux = NULL;
494069f55e2SEric Schrock 
495ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
496b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
497b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0)
498b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
499b4952e17SGeorge Wilson 			    vd->vdev_resilver_txg);
5003d7072f8Seschrock 		if (vd->vdev_faulted)
501b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
5023d7072f8Seschrock 		if (vd->vdev_degraded)
503b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
5043d7072f8Seschrock 		if (vd->vdev_removed)
505b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
5063d7072f8Seschrock 		if (vd->vdev_unspare)
507b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
50888ecc943SGeorge Wilson 		if (vd->vdev_ishole)
509b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
510069f55e2SEric Schrock 
511069f55e2SEric Schrock 		switch (vd->vdev_stat.vs_aux) {
512069f55e2SEric Schrock 		case VDEV_AUX_ERR_EXCEEDED:
513069f55e2SEric Schrock 			aux = "err_exceeded";
514069f55e2SEric Schrock 			break;
515069f55e2SEric Schrock 
516069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
517069f55e2SEric Schrock 			aux = "external";
518069f55e2SEric Schrock 			break;
519069f55e2SEric Schrock 		}
520069f55e2SEric Schrock 
521069f55e2SEric Schrock 		if (aux != NULL)
522b4952e17SGeorge Wilson 			fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
5231195e687SMark J Musante 
5241195e687SMark J Musante 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
525b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
526b4952e17SGeorge Wilson 			    vd->vdev_orig_guid);
5271195e687SMark J Musante 		}
528fa9e4066Sahrens 	}
529fa9e4066Sahrens 
530fa9e4066Sahrens 	return (nv);
531fa9e4066Sahrens }
532fa9e4066Sahrens 
53388ecc943SGeorge Wilson /*
53488ecc943SGeorge Wilson  * Generate a view of the top-level vdevs.  If we currently have holes
53588ecc943SGeorge Wilson  * in the namespace, then generate an array which contains a list of holey
53688ecc943SGeorge Wilson  * vdevs.  Additionally, add the number of top-level children that currently
53788ecc943SGeorge Wilson  * exist.
53888ecc943SGeorge Wilson  */
53988ecc943SGeorge Wilson void
54088ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config)
54188ecc943SGeorge Wilson {
54288ecc943SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
54388ecc943SGeorge Wilson 	uint64_t *array;
5443f9d6ad7SLin Ling 	uint_t c, idx;
54588ecc943SGeorge Wilson 
54688ecc943SGeorge Wilson 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
54788ecc943SGeorge Wilson 
5483f9d6ad7SLin Ling 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
54988ecc943SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
55088ecc943SGeorge Wilson 
5515cabbc6bSPrashanth Sreenivasa 		if (tvd->vdev_ishole) {
55288ecc943SGeorge Wilson 			array[idx++] = c;
5535cabbc6bSPrashanth Sreenivasa 		}
55488ecc943SGeorge Wilson 	}
55588ecc943SGeorge Wilson 
556312c6e15SGeorge Wilson 	if (idx) {
557312c6e15SGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
558312c6e15SGeorge Wilson 		    array, idx) == 0);
559312c6e15SGeorge Wilson 	}
560312c6e15SGeorge Wilson 
56188ecc943SGeorge Wilson 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
56288ecc943SGeorge Wilson 	    rvd->vdev_children) == 0);
56388ecc943SGeorge Wilson 
56488ecc943SGeorge Wilson 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
56588ecc943SGeorge Wilson }
56688ecc943SGeorge Wilson 
567ad135b5dSChristopher Siden /*
568dfbb9432SGeorge Wilson  * Returns the configuration from the label of the given vdev. For vdevs
569dfbb9432SGeorge Wilson  * which don't have a txg value stored on their label (i.e. spares/cache)
570dfbb9432SGeorge Wilson  * or have not been completely initialized (txg = 0) just return
571dfbb9432SGeorge Wilson  * the configuration from the first valid label we find. Otherwise,
572dfbb9432SGeorge Wilson  * find the most up-to-date label that does not exceed the specified
573dfbb9432SGeorge Wilson  * 'txg' value.
574ad135b5dSChristopher Siden  */
575fa9e4066Sahrens nvlist_t *
576dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg)
577fa9e4066Sahrens {
5780373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
579fa9e4066Sahrens 	nvlist_t *config = NULL;
580fa9e4066Sahrens 	vdev_phys_t *vp;
581770499e1SDan Kimmel 	abd_t *vp_abd;
582fa9e4066Sahrens 	zio_t *zio;
583dfbb9432SGeorge Wilson 	uint64_t best_txg = 0;
584b6bf6e15SPavel Zakharov 	uint64_t label_txg = 0;
585dfbb9432SGeorge Wilson 	int error = 0;
5868956713aSEric Schrock 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
5878956713aSEric Schrock 	    ZIO_FLAG_SPECULATIVE;
588fa9e4066Sahrens 
589e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
5900373e76bSbonwick 
5910a4e9518Sgw 	if (!vdev_readable(vd))
592fa9e4066Sahrens 		return (NULL);
593fa9e4066Sahrens 
594770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
595770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
596fa9e4066Sahrens 
5978956713aSEric Schrock retry:
598e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
599dfbb9432SGeorge Wilson 		nvlist_t *label = NULL;
600fa9e4066Sahrens 
601e14bb325SJeff Bonwick 		zio = zio_root(spa, NULL, NULL, flags);
602fa9e4066Sahrens 
603770499e1SDan Kimmel 		vdev_label_read(zio, vd, l, vp_abd,
604fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
605e14bb325SJeff Bonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
606fa9e4066Sahrens 
607fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
608fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
609dfbb9432SGeorge Wilson 		    &label, 0) == 0) {
610dfbb9432SGeorge Wilson 			/*
611dfbb9432SGeorge Wilson 			 * Auxiliary vdevs won't have txg values in their
612dfbb9432SGeorge Wilson 			 * labels and newly added vdevs may not have been
613dfbb9432SGeorge Wilson 			 * completely initialized so just return the
614dfbb9432SGeorge Wilson 			 * configuration from the first valid label we
615dfbb9432SGeorge Wilson 			 * encounter.
616dfbb9432SGeorge Wilson 			 */
617dfbb9432SGeorge Wilson 			error = nvlist_lookup_uint64(label,
618dfbb9432SGeorge Wilson 			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
619dfbb9432SGeorge Wilson 			if ((error || label_txg == 0) && !config) {
620dfbb9432SGeorge Wilson 				config = label;
621dfbb9432SGeorge Wilson 				break;
622dfbb9432SGeorge Wilson 			} else if (label_txg <= txg && label_txg > best_txg) {
623dfbb9432SGeorge Wilson 				best_txg = label_txg;
624dfbb9432SGeorge Wilson 				nvlist_free(config);
625dfbb9432SGeorge Wilson 				config = fnvlist_dup(label);
626dfbb9432SGeorge Wilson 			}
627dfbb9432SGeorge Wilson 		}
628fa9e4066Sahrens 
629dfbb9432SGeorge Wilson 		if (label != NULL) {
630dfbb9432SGeorge Wilson 			nvlist_free(label);
631dfbb9432SGeorge Wilson 			label = NULL;
632fa9e4066Sahrens 		}
633fa9e4066Sahrens 	}
634fa9e4066Sahrens 
6358956713aSEric Schrock 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
6368956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
6378956713aSEric Schrock 		goto retry;
6388956713aSEric Schrock 	}
6398956713aSEric Schrock 
640b6bf6e15SPavel Zakharov 	/*
641b6bf6e15SPavel Zakharov 	 * We found a valid label but it didn't pass txg restrictions.
642b6bf6e15SPavel Zakharov 	 */
643b6bf6e15SPavel Zakharov 	if (config == NULL && label_txg != 0) {
644b6bf6e15SPavel Zakharov 		vdev_dbgmsg(vd, "label discarded as txg is too large "
645b6bf6e15SPavel Zakharov 		    "(%llu > %llu)", (u_longlong_t)label_txg,
646b6bf6e15SPavel Zakharov 		    (u_longlong_t)txg);
647b6bf6e15SPavel Zakharov 	}
648b6bf6e15SPavel Zakharov 
649770499e1SDan Kimmel 	abd_free(vp_abd);
650fa9e4066Sahrens 
651fa9e4066Sahrens 	return (config);
652fa9e4066Sahrens }
653fa9e4066Sahrens 
65439c23413Seschrock /*
65539c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
65639c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
65739c23413Seschrock  */
65839c23413Seschrock static boolean_t
65939c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
660fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
66139c23413Seschrock {
66239c23413Seschrock 	spa_t *spa = vd->vdev_spa;
66339c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
66439c23413Seschrock 	uint64_t vdtxg = 0;
66539c23413Seschrock 	nvlist_t *label;
66639c23413Seschrock 
66739c23413Seschrock 	if (spare_guid)
66839c23413Seschrock 		*spare_guid = 0ULL;
669fa94a07fSbrendan 	if (l2cache_guid)
670fa94a07fSbrendan 		*l2cache_guid = 0ULL;
67139c23413Seschrock 
67239c23413Seschrock 	/*
67339c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
67439c23413Seschrock 	 */
675dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
67639c23413Seschrock 		return (B_FALSE);
67739c23413Seschrock 
67839c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
67939c23413Seschrock 	    &vdtxg);
68039c23413Seschrock 
68139c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
68239c23413Seschrock 	    &state) != 0 ||
68339c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
68439c23413Seschrock 	    &device_guid) != 0) {
68539c23413Seschrock 		nvlist_free(label);
68639c23413Seschrock 		return (B_FALSE);
68739c23413Seschrock 	}
68839c23413Seschrock 
689fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
69039c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
69139c23413Seschrock 	    &pool_guid) != 0 ||
69239c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
69339c23413Seschrock 	    &txg) != 0)) {
69439c23413Seschrock 		nvlist_free(label);
69539c23413Seschrock 		return (B_FALSE);
69639c23413Seschrock 	}
69739c23413Seschrock 
69839c23413Seschrock 	nvlist_free(label);
69939c23413Seschrock 
70039c23413Seschrock 	/*
70139c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
70239c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
70339c23413Seschrock 	 * spare (which we check for later on).
70439c23413Seschrock 	 */
705fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
70639c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
70789a89ebfSlling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
708fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
70939c23413Seschrock 		return (B_FALSE);
71039c23413Seschrock 
71139c23413Seschrock 	/*
71239c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
71339c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
71439c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
71539c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
71639c23413Seschrock 	 * transaction.
71739c23413Seschrock 	 */
718fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
719fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
72039c23413Seschrock 		return (B_TRUE);
72139c23413Seschrock 
72239c23413Seschrock 	/*
72339c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
72439c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
725fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
72639c23413Seschrock 	 */
72789a89ebfSlling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
72839c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
72939c23413Seschrock 		if (spare_guid)
73039c23413Seschrock 			*spare_guid = device_guid;
73139c23413Seschrock 
73239c23413Seschrock 		switch (reason) {
73339c23413Seschrock 		case VDEV_LABEL_CREATE:
734fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
73539c23413Seschrock 			return (B_TRUE);
73639c23413Seschrock 
73739c23413Seschrock 		case VDEV_LABEL_REPLACE:
73839c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
73939c23413Seschrock 			    spare_pool != 0ULL);
74039c23413Seschrock 
74139c23413Seschrock 		case VDEV_LABEL_SPARE:
74239c23413Seschrock 			return (spa_has_spare(spa, device_guid));
74339c23413Seschrock 		}
74439c23413Seschrock 	}
74539c23413Seschrock 
746fa94a07fSbrendan 	/*
747fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
748fa94a07fSbrendan 	 */
749fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
750fa94a07fSbrendan 		return (B_TRUE);
751fa94a07fSbrendan 
752f9af39baSGeorge Wilson 	/*
753f9af39baSGeorge Wilson 	 * We can't rely on a pool's state if it's been imported
754f9af39baSGeorge Wilson 	 * read-only.  Instead we look to see if the pools is marked
755f9af39baSGeorge Wilson 	 * read-only in the namespace and set the state to active.
756f9af39baSGeorge Wilson 	 */
7575bdd995dSRichard Yao 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
7585bdd995dSRichard Yao 	    (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
759f9af39baSGeorge Wilson 	    spa_mode(spa) == FREAD)
760f9af39baSGeorge Wilson 		state = POOL_STATE_ACTIVE;
761f9af39baSGeorge Wilson 
76239c23413Seschrock 	/*
76339c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
76439c23413Seschrock 	 * pool on the system.
76539c23413Seschrock 	 */
76639c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
76739c23413Seschrock }
76839c23413Seschrock 
76939c23413Seschrock /*
77039c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
77139c23413Seschrock  * use, and writable.  We put down an initial label which we will later
77239c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
77339c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
77439c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
77539c23413Seschrock  * itself.
77639c23413Seschrock  */
77739c23413Seschrock int
77839c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
779fa9e4066Sahrens {
780fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
781fa9e4066Sahrens 	nvlist_t *label;
782fa9e4066Sahrens 	vdev_phys_t *vp;
783770499e1SDan Kimmel 	abd_t *vp_abd;
784770499e1SDan Kimmel 	abd_t *pad2;
785ecc2d604Sbonwick 	uberblock_t *ub;
786770499e1SDan Kimmel 	abd_t *ub_abd;
787fa9e4066Sahrens 	zio_t *zio;
788fa9e4066Sahrens 	char *buf;
789fa9e4066Sahrens 	size_t buflen;
790fa9e4066Sahrens 	int error;
791fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
792e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
793fa9e4066Sahrens 
794e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
7950373e76bSbonwick 
796e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
79739c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
79839c23413Seschrock 		    crtxg, reason)) != 0)
799fa9e4066Sahrens 			return (error);
800fa9e4066Sahrens 
80188ecc943SGeorge Wilson 	/* Track the creation time for this vdev */
80288ecc943SGeorge Wilson 	vd->vdev_crtxg = crtxg;
80388ecc943SGeorge Wilson 
804973c78e9SGeorge Wilson 	if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
805fa9e4066Sahrens 		return (0);
806fa9e4066Sahrens 
807fa9e4066Sahrens 	/*
80839c23413Seschrock 	 * Dead vdevs cannot be initialized.
809fa9e4066Sahrens 	 */
810fa9e4066Sahrens 	if (vdev_is_dead(vd))
811be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
812fa9e4066Sahrens 
813fa9e4066Sahrens 	/*
81439c23413Seschrock 	 * Determine if the vdev is in use.
815fa9e4066Sahrens 	 */
8161195e687SMark J Musante 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
817fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
818be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
81939c23413Seschrock 
82039c23413Seschrock 	/*
821fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
822fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
823fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
824fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
82539c23413Seschrock 	 */
826fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
827fa94a07fSbrendan 	    spare_guid != 0ULL) {
82831157203SJeff Bonwick 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
82999653d4eSeschrock 
83031157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
83131157203SJeff Bonwick 
83231157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
83331157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
83439c23413Seschrock 
83599653d4eSeschrock 		/*
83639c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
83739c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
8383d7072f8Seschrock 		 * labeled appropriately and we can just return.
83999653d4eSeschrock 		 */
84039c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
84139c23413Seschrock 			return (0);
8421195e687SMark J Musante 		ASSERT(reason == VDEV_LABEL_REPLACE ||
8431195e687SMark J Musante 		    reason == VDEV_LABEL_SPLIT);
844fa9e4066Sahrens 	}
845fa9e4066Sahrens 
846fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
847fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
84831157203SJeff Bonwick 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
84931157203SJeff Bonwick 
85031157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
851fa94a07fSbrendan 
85231157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
85331157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
854fa94a07fSbrendan 
855fa94a07fSbrendan 		/*
856fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
857fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
858fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
859fa94a07fSbrendan 		 */
860fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
861fa94a07fSbrendan 			return (0);
862fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
863fa94a07fSbrendan 	}
864fa94a07fSbrendan 
865fa9e4066Sahrens 	/*
86639c23413Seschrock 	 * Initialize its label.
867fa9e4066Sahrens 	 */
868770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
869770499e1SDan Kimmel 	abd_zero(vp_abd, sizeof (vdev_phys_t));
870770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
871fa9e4066Sahrens 
872fa9e4066Sahrens 	/*
873fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
874fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
875fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
876fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
877fa9e4066Sahrens 	 */
87839c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
87939c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
88039c23413Seschrock 		/*
88139c23413Seschrock 		 * For inactive hot spares, we generate a special label that
88239c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
88339c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
88439c23413Seschrock 		 * active hot spare (in which case we want to revert the
88539c23413Seschrock 		 * labels).
88639c23413Seschrock 		 */
88799653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
88899653d4eSeschrock 
88999653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
89099653d4eSeschrock 		    spa_version(spa)) == 0);
89199653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
89299653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
89399653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
89499653d4eSeschrock 		    vd->vdev_guid) == 0);
895fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
896fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
897fa94a07fSbrendan 		/*
898fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
899fa94a07fSbrendan 		 */
900fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
901fa94a07fSbrendan 
902fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
903fa94a07fSbrendan 		    spa_version(spa)) == 0);
904fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
905fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
906fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
907fa94a07fSbrendan 		    vd->vdev_guid) == 0);
90899653d4eSeschrock 	} else {
9091195e687SMark J Musante 		uint64_t txg = 0ULL;
9101195e687SMark J Musante 
9111195e687SMark J Musante 		if (reason == VDEV_LABEL_SPLIT)
9121195e687SMark J Musante 			txg = spa->spa_uberblock.ub_txg;
9131195e687SMark J Musante 		label = spa_config_generate(spa, vd, txg, B_FALSE);
91499653d4eSeschrock 
91599653d4eSeschrock 		/*
91699653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
91799653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
91899653d4eSeschrock 		 * fail.
91999653d4eSeschrock 		 */
92099653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
92199653d4eSeschrock 		    crtxg) == 0);
92299653d4eSeschrock 	}
923fa9e4066Sahrens 
924fa9e4066Sahrens 	buf = vp->vp_nvlist;
925fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
926fa9e4066Sahrens 
927a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
928a75573b6Smmusante 	if (error != 0) {
929fa9e4066Sahrens 		nvlist_free(label);
930770499e1SDan Kimmel 		abd_free(vp_abd);
931a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
932a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
933fa9e4066Sahrens 	}
934fa9e4066Sahrens 
935fa9e4066Sahrens 	/*
936fa9e4066Sahrens 	 * Initialize uberblock template.
937fa9e4066Sahrens 	 */
938770499e1SDan Kimmel 	ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
939770499e1SDan Kimmel 	abd_zero(ub_abd, VDEV_UBERBLOCK_RING);
940770499e1SDan Kimmel 	abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
941770499e1SDan Kimmel 	ub = abd_to_buf(ub_abd);
942ecc2d604Sbonwick 	ub->ub_txg = 0;
943fa9e4066Sahrens 
944f83ffe1aSLin Ling 	/* Initialize the 2nd padding area. */
945770499e1SDan Kimmel 	pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
946770499e1SDan Kimmel 	abd_zero(pad2, VDEV_PAD_SIZE);
947f83ffe1aSLin Ling 
948fa9e4066Sahrens 	/*
949fa9e4066Sahrens 	 * Write everything in parallel.
950fa9e4066Sahrens 	 */
9518956713aSEric Schrock retry:
95217f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
953fa9e4066Sahrens 
954e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
955fa9e4066Sahrens 
956770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, vp_abd,
957fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
95817f17c2dSbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
959fa9e4066Sahrens 
960f83ffe1aSLin Ling 		/*
961f83ffe1aSLin Ling 		 * Skip the 1st padding area.
962f83ffe1aSLin Ling 		 * Zero out the 2nd padding area where it might have
963f83ffe1aSLin Ling 		 * left over data from previous filesystem format.
964f83ffe1aSLin Ling 		 */
965f83ffe1aSLin Ling 		vdev_label_write(zio, vd, l, pad2,
966f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2),
967f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, NULL, NULL, flags);
968f83ffe1aSLin Ling 
969770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, ub_abd,
970f64c0e34SEric Taylor 		    offsetof(vdev_label_t, vl_uberblock),
971f64c0e34SEric Taylor 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
972fa9e4066Sahrens 	}
973fa9e4066Sahrens 
974fa9e4066Sahrens 	error = zio_wait(zio);
975fa9e4066Sahrens 
9768956713aSEric Schrock 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
9778956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
9788956713aSEric Schrock 		goto retry;
9798956713aSEric Schrock 	}
9808956713aSEric Schrock 
981fa9e4066Sahrens 	nvlist_free(label);
982770499e1SDan Kimmel 	abd_free(pad2);
983770499e1SDan Kimmel 	abd_free(ub_abd);
984770499e1SDan Kimmel 	abd_free(vp_abd);
985fa9e4066Sahrens 
98639c23413Seschrock 	/*
98739c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
9883d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
989fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
990fa94a07fSbrendan 	 * level 2 ARC devices.
99139c23413Seschrock 	 */
99239c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
99339c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
99489a89ebfSlling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
99539c23413Seschrock 		spa_spare_add(vd);
99699653d4eSeschrock 
997fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
998fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
999fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
1000fa94a07fSbrendan 		spa_l2cache_add(vd);
1001fa94a07fSbrendan 
100239c23413Seschrock 	return (error);
100399653d4eSeschrock }
100499653d4eSeschrock 
1005fa9e4066Sahrens /*
1006fa9e4066Sahrens  * ==========================================================================
1007fa9e4066Sahrens  * uberblock load/sync
1008fa9e4066Sahrens  * ==========================================================================
1009fa9e4066Sahrens  */
1010fa9e4066Sahrens 
1011fa9e4066Sahrens /*
1012fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
1013fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
1014fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
1015fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
1016fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
1017ad135b5dSChristopher Siden  * the missing replica comes back, then for a few seconds we'll have two
1018fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
1019fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
1020fa9e4066Sahrens  */
1021fa9e4066Sahrens static int
1022c4ab0d3fSGvozden Neskovic vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1023fa9e4066Sahrens {
1024c4ab0d3fSGvozden Neskovic 	int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1025c4ab0d3fSGvozden Neskovic 	if (likely(cmp))
1026c4ab0d3fSGvozden Neskovic 		return (cmp);
1027fa9e4066Sahrens 
1028c4ab0d3fSGvozden Neskovic 	return (AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp));
1029fa9e4066Sahrens }
1030fa9e4066Sahrens 
1031ad135b5dSChristopher Siden struct ubl_cbdata {
1032ad135b5dSChristopher Siden 	uberblock_t	*ubl_ubbest;	/* Best uberblock */
1033ad135b5dSChristopher Siden 	vdev_t		*ubl_vd;	/* vdev associated with the above */
1034ad135b5dSChristopher Siden };
1035ad135b5dSChristopher Siden 
1036fa9e4066Sahrens static void
1037fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
1038fa9e4066Sahrens {
1039ad135b5dSChristopher Siden 	vdev_t *vd = zio->io_vd;
1040468c413aSTim Haley 	spa_t *spa = zio->io_spa;
1041e14bb325SJeff Bonwick 	zio_t *rio = zio->io_private;
1042770499e1SDan Kimmel 	uberblock_t *ub = abd_to_buf(zio->io_abd);
1043ad135b5dSChristopher Siden 	struct ubl_cbdata *cbp = rio->io_private;
1044fa9e4066Sahrens 
1045ad135b5dSChristopher Siden 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1046fa9e4066Sahrens 
1047ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1048e14bb325SJeff Bonwick 		mutex_enter(&rio->io_lock);
1049468c413aSTim Haley 		if (ub->ub_txg <= spa->spa_load_max_txg &&
1050ad135b5dSChristopher Siden 		    vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1051ad135b5dSChristopher Siden 			/*
1052dfbb9432SGeorge Wilson 			 * Keep track of the vdev in which this uberblock
1053dfbb9432SGeorge Wilson 			 * was found. We will use this information later
1054dfbb9432SGeorge Wilson 			 * to obtain the config nvlist associated with
1055ad135b5dSChristopher Siden 			 * this uberblock.
1056ad135b5dSChristopher Siden 			 */
1057ad135b5dSChristopher Siden 			*cbp->ubl_ubbest = *ub;
1058ad135b5dSChristopher Siden 			cbp->ubl_vd = vd;
1059ad135b5dSChristopher Siden 		}
1060e14bb325SJeff Bonwick 		mutex_exit(&rio->io_lock);
1061fa9e4066Sahrens 	}
1062fa9e4066Sahrens 
1063770499e1SDan Kimmel 	abd_free(zio->io_abd);
1064fa9e4066Sahrens }
1065fa9e4066Sahrens 
1066ad135b5dSChristopher Siden static void
1067ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1068ad135b5dSChristopher Siden     struct ubl_cbdata *cbp)
1069fa9e4066Sahrens {
1070e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1071ad135b5dSChristopher Siden 		vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1072fa9e4066Sahrens 
1073e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1074e14bb325SJeff Bonwick 		for (int l = 0; l < VDEV_LABELS; l++) {
1075e14bb325SJeff Bonwick 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1076e14bb325SJeff Bonwick 				vdev_label_read(zio, vd, l,
1077770499e1SDan Kimmel 				    abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1078770499e1SDan Kimmel 				    B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1079e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_SIZE(vd),
1080e14bb325SJeff Bonwick 				    vdev_uberblock_load_done, zio, flags);
1081e14bb325SJeff Bonwick 			}
1082fa9e4066Sahrens 		}
1083fa9e4066Sahrens 	}
1084ad135b5dSChristopher Siden }
1085e14bb325SJeff Bonwick 
1086ad135b5dSChristopher Siden /*
1087ad135b5dSChristopher Siden  * Reads the 'best' uberblock from disk along with its associated
1088ad135b5dSChristopher Siden  * configuration. First, we read the uberblock array of each label of each
1089ad135b5dSChristopher Siden  * vdev, keeping track of the uberblock with the highest txg in each array.
1090dfbb9432SGeorge Wilson  * Then, we read the configuration from the same vdev as the best uberblock.
1091ad135b5dSChristopher Siden  */
1092ad135b5dSChristopher Siden void
1093ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1094ad135b5dSChristopher Siden {
1095ad135b5dSChristopher Siden 	zio_t *zio;
1096ad135b5dSChristopher Siden 	spa_t *spa = rvd->vdev_spa;
1097ad135b5dSChristopher Siden 	struct ubl_cbdata cb;
1098ad135b5dSChristopher Siden 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1099ad135b5dSChristopher Siden 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1100ad135b5dSChristopher Siden 
1101ad135b5dSChristopher Siden 	ASSERT(ub);
1102ad135b5dSChristopher Siden 	ASSERT(config);
1103ad135b5dSChristopher Siden 
1104ad135b5dSChristopher Siden 	bzero(ub, sizeof (uberblock_t));
1105ad135b5dSChristopher Siden 	*config = NULL;
1106ad135b5dSChristopher Siden 
1107ad135b5dSChristopher Siden 	cb.ubl_ubbest = ub;
1108ad135b5dSChristopher Siden 	cb.ubl_vd = NULL;
1109ad135b5dSChristopher Siden 
1110ad135b5dSChristopher Siden 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1111ad135b5dSChristopher Siden 	zio = zio_root(spa, NULL, &cb, flags);
1112ad135b5dSChristopher Siden 	vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1113ad135b5dSChristopher Siden 	(void) zio_wait(zio);
1114dfbb9432SGeorge Wilson 
1115dfbb9432SGeorge Wilson 	/*
1116dfbb9432SGeorge Wilson 	 * It's possible that the best uberblock was discovered on a label
1117dfbb9432SGeorge Wilson 	 * that has a configuration which was written in a future txg.
1118dfbb9432SGeorge Wilson 	 * Search all labels on this vdev to find the configuration that
1119dfbb9432SGeorge Wilson 	 * matches the txg for our uberblock.
1120dfbb9432SGeorge Wilson 	 */
11213ee8c80cSPavel Zakharov 	if (cb.ubl_vd != NULL) {
11223ee8c80cSPavel Zakharov 		vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
11233ee8c80cSPavel Zakharov 		    "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
11243ee8c80cSPavel Zakharov 
1125dfbb9432SGeorge Wilson 		*config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
11266f793812SPavel Zakharov 		if (*config == NULL && spa->spa_extreme_rewind) {
11276f793812SPavel Zakharov 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
11286f793812SPavel Zakharov 			    "Trying again without txg restrictions.");
11296f793812SPavel Zakharov 			*config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
11306f793812SPavel Zakharov 		}
11313ee8c80cSPavel Zakharov 		if (*config == NULL) {
11323ee8c80cSPavel Zakharov 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
11333ee8c80cSPavel Zakharov 		}
11343ee8c80cSPavel Zakharov 	}
1135ad135b5dSChristopher Siden 	spa_config_exit(spa, SCL_ALL, FTAG);
1136fa9e4066Sahrens }
1137fa9e4066Sahrens 
1138fa9e4066Sahrens /*
113917f17c2dSbonwick  * On success, increment root zio's count of good writes.
11400373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1141fa9e4066Sahrens  */
1142fa9e4066Sahrens static void
1143fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
1144fa9e4066Sahrens {
114517f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1146fa9e4066Sahrens 
11470373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
11481a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(good_writes);
1149fa9e4066Sahrens }
1150fa9e4066Sahrens 
115117f17c2dSbonwick /*
115217f17c2dSbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
115317f17c2dSbonwick  */
1154fa9e4066Sahrens static void
1155a3b55830SMatthew Ahrens vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1156a3b55830SMatthew Ahrens     uberblock_t *ub, vdev_t *vd, int flags)
1157fa9e4066Sahrens {
1158a3b55830SMatthew Ahrens 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
1159a3b55830SMatthew Ahrens 		vdev_uberblock_sync(zio, good_writes,
1160a3b55830SMatthew Ahrens 		    ub, vd->vdev_child[c], flags);
1161a3b55830SMatthew Ahrens 	}
1162fa9e4066Sahrens 
1163fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1164fa9e4066Sahrens 		return;
1165fa9e4066Sahrens 
1166e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1167fa9e4066Sahrens 		return;
1168fa9e4066Sahrens 
1169e0f1c0afSOlaf Faaland 	int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1170e0f1c0afSOlaf Faaland 	int n = ub->ub_txg % (VDEV_UBERBLOCK_COUNT(vd) - m);
1171fa9e4066Sahrens 
1172770499e1SDan Kimmel 	/* Copy the uberblock_t into the ABD */
1173770499e1SDan Kimmel 	abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1174770499e1SDan Kimmel 	abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1175770499e1SDan Kimmel 	abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1176fa9e4066Sahrens 
1177e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++)
1178770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, ub_abd,
1179e14bb325SJeff Bonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1180a3b55830SMatthew Ahrens 		    vdev_uberblock_sync_done, good_writes,
1181e14bb325SJeff Bonwick 		    flags | ZIO_FLAG_DONT_PROPAGATE);
1182fa9e4066Sahrens 
1183770499e1SDan Kimmel 	abd_free(ub_abd);
1184fa9e4066Sahrens }
1185fa9e4066Sahrens 
11863e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */
118717f17c2dSbonwick int
118817f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1189fa9e4066Sahrens {
119017f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1191e14bb325SJeff Bonwick 	zio_t *zio;
119217f17c2dSbonwick 	uint64_t good_writes = 0;
1193fa9e4066Sahrens 
1194a3b55830SMatthew Ahrens 	zio = zio_root(spa, NULL, NULL, flags);
1195e14bb325SJeff Bonwick 
1196e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
1197a3b55830SMatthew Ahrens 		vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1198fa9e4066Sahrens 
119917f17c2dSbonwick 	(void) zio_wait(zio);
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	/*
120217f17c2dSbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
120317f17c2dSbonwick 	 * are no longer needed (because the new uberblocks and the even
120417f17c2dSbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
1205fa9e4066Sahrens 	 */
120617f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
1207fa9e4066Sahrens 
12085cabbc6bSPrashanth Sreenivasa 	for (int v = 0; v < svdcount; v++) {
12095cabbc6bSPrashanth Sreenivasa 		if (vdev_writeable(svd[v])) {
12105cabbc6bSPrashanth Sreenivasa 			zio_flush(zio, svd[v]);
12115cabbc6bSPrashanth Sreenivasa 		}
12125cabbc6bSPrashanth Sreenivasa 	}
1213fa9e4066Sahrens 
121417f17c2dSbonwick 	(void) zio_wait(zio);
121517f17c2dSbonwick 
121617f17c2dSbonwick 	return (good_writes >= 1 ? 0 : EIO);
1217fa9e4066Sahrens }
1218fa9e4066Sahrens 
1219fa9e4066Sahrens /*
122017f17c2dSbonwick  * On success, increment the count of good writes for our top-level vdev.
1221fa9e4066Sahrens  */
1222fa9e4066Sahrens static void
122317f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
1224fa9e4066Sahrens {
122517f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1226fa9e4066Sahrens 
1227fa9e4066Sahrens 	if (zio->io_error == 0)
12281a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(good_writes);
1229fa9e4066Sahrens }
1230fa9e4066Sahrens 
123117f17c2dSbonwick /*
123217f17c2dSbonwick  * If there weren't enough good writes, indicate failure to the parent.
123317f17c2dSbonwick  */
1234fa9e4066Sahrens static void
123517f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
123617f17c2dSbonwick {
123717f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
123817f17c2dSbonwick 
123917f17c2dSbonwick 	if (*good_writes == 0)
1240be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(EIO);
124117f17c2dSbonwick 
124217f17c2dSbonwick 	kmem_free(good_writes, sizeof (uint64_t));
124317f17c2dSbonwick }
124417f17c2dSbonwick 
124551ece835Seschrock /*
12460430f8daSeschrock  * We ignore errors for log and cache devices, simply free the private data.
124751ece835Seschrock  */
124851ece835Seschrock static void
12490430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio)
125051ece835Seschrock {
125151ece835Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
125251ece835Seschrock }
125351ece835Seschrock 
125417f17c2dSbonwick /*
125517f17c2dSbonwick  * Write all even or odd labels to all leaves of the specified vdev.
125617f17c2dSbonwick  */
125717f17c2dSbonwick static void
1258a3b55830SMatthew Ahrens vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1259a3b55830SMatthew Ahrens     vdev_t *vd, int l, uint64_t txg, int flags)
1260fa9e4066Sahrens {
1261fa9e4066Sahrens 	nvlist_t *label;
1262fa9e4066Sahrens 	vdev_phys_t *vp;
1263770499e1SDan Kimmel 	abd_t *vp_abd;
1264fa9e4066Sahrens 	char *buf;
1265fa9e4066Sahrens 	size_t buflen;
1266fa9e4066Sahrens 
1267a3b55830SMatthew Ahrens 	for (int c = 0; c < vd->vdev_children; c++) {
1268a3b55830SMatthew Ahrens 		vdev_label_sync(zio, good_writes,
1269a3b55830SMatthew Ahrens 		    vd->vdev_child[c], l, txg, flags);
1270a3b55830SMatthew Ahrens 	}
1271fa9e4066Sahrens 
1272fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1273fa9e4066Sahrens 		return;
1274fa9e4066Sahrens 
1275e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1276fa9e4066Sahrens 		return;
1277fa9e4066Sahrens 
1278fa9e4066Sahrens 	/*
1279fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
1280fa9e4066Sahrens 	 */
12810373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1282fa9e4066Sahrens 
1283770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1284770499e1SDan Kimmel 	abd_zero(vp_abd, sizeof (vdev_phys_t));
1285770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
1286fa9e4066Sahrens 
1287fa9e4066Sahrens 	buf = vp->vp_nvlist;
1288fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
1289fa9e4066Sahrens 
129017f17c2dSbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
129117f17c2dSbonwick 		for (; l < VDEV_LABELS; l += 2) {
1292770499e1SDan Kimmel 			vdev_label_write(zio, vd, l, vp_abd,
129317f17c2dSbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
129417f17c2dSbonwick 			    sizeof (vdev_phys_t),
1295a3b55830SMatthew Ahrens 			    vdev_label_sync_done, good_writes,
1296e14bb325SJeff Bonwick 			    flags | ZIO_FLAG_DONT_PROPAGATE);
129717f17c2dSbonwick 		}
129817f17c2dSbonwick 	}
1299fa9e4066Sahrens 
1300770499e1SDan Kimmel 	abd_free(vp_abd);
1301fa9e4066Sahrens 	nvlist_free(label);
1302fa9e4066Sahrens }
1303fa9e4066Sahrens 
130417f17c2dSbonwick int
1305e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1306fa9e4066Sahrens {
1307e14bb325SJeff Bonwick 	list_t *dl = &spa->spa_config_dirty_list;
130817f17c2dSbonwick 	vdev_t *vd;
1309e14bb325SJeff Bonwick 	zio_t *zio;
1310fa9e4066Sahrens 	int error;
1311fa9e4066Sahrens 
1312fa9e4066Sahrens 	/*
1313e14bb325SJeff Bonwick 	 * Write the new labels to disk.
1314fa9e4066Sahrens 	 */
1315e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, flags);
1316fa9e4066Sahrens 
131717f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
131817f17c2dSbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
131917f17c2dSbonwick 		    KM_SLEEP);
132088ecc943SGeorge Wilson 
132188ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
132288ecc943SGeorge Wilson 
1323a3f829aeSBill Moore 		zio_t *vio = zio_null(zio, spa, NULL,
13240430f8daSeschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
13250430f8daSeschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
132617f17c2dSbonwick 		    good_writes, flags);
1327a3b55830SMatthew Ahrens 		vdev_label_sync(vio, good_writes, vd, l, txg, flags);
132817f17c2dSbonwick 		zio_nowait(vio);
1329fa9e4066Sahrens 	}
1330e14bb325SJeff Bonwick 
1331e14bb325SJeff Bonwick 	error = zio_wait(zio);
1332fa9e4066Sahrens 
13338654d025Sperrin 	/*
133417f17c2dSbonwick 	 * Flush the new labels to disk.
13358654d025Sperrin 	 */
133617f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
13378654d025Sperrin 
133817f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
133917f17c2dSbonwick 		zio_flush(zio, vd);
134017f17c2dSbonwick 
134117f17c2dSbonwick 	(void) zio_wait(zio);
1342fa9e4066Sahrens 
1343fa9e4066Sahrens 	return (error);
1344fa9e4066Sahrens }
1345fa9e4066Sahrens 
1346fa9e4066Sahrens /*
134717f17c2dSbonwick  * Sync the uberblock and any changes to the vdev configuration.
1348fa9e4066Sahrens  *
1349fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
1350fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
1351fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
1352fa9e4066Sahrens  * describe the failure semantics at each stage.
1353fa9e4066Sahrens  *
135417f17c2dSbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1355fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
1356fa9e4066Sahrens  */
1357e14bb325SJeff Bonwick int
1358eb5bb584SWill Andrews vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1359fa9e4066Sahrens {
136017f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1361fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
1362eb5bb584SWill Andrews 	int error = 0;
1363e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1364fa9e4066Sahrens 
136586714001SSerapheim Dimitropoulos 	ASSERT(svdcount != 0);
1366eb5bb584SWill Andrews retry:
13678956713aSEric Schrock 	/*
13688956713aSEric Schrock 	 * Normally, we don't want to try too hard to write every label and
13698956713aSEric Schrock 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
13708956713aSEric Schrock 	 * sync process to block while we retry.  But if we can't write a
13718956713aSEric Schrock 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
13728956713aSEric Schrock 	 * bailing out and declaring the pool faulted.
13738956713aSEric Schrock 	 */
1374eb5bb584SWill Andrews 	if (error != 0) {
1375eb5bb584SWill Andrews 		if ((flags & ZIO_FLAG_TRYHARD) != 0)
1376eb5bb584SWill Andrews 			return (error);
13778956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
1378eb5bb584SWill Andrews 	}
13798956713aSEric Schrock 
1380fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
1381fa9e4066Sahrens 
1382fa9e4066Sahrens 	/*
138317f17c2dSbonwick 	 * If this isn't a resync due to I/O errors,
138417f17c2dSbonwick 	 * and nothing changed in this transaction group,
138517f17c2dSbonwick 	 * and the vdev configuration hasn't changed,
13860373e76bSbonwick 	 * then there's nothing to do.
1387fa9e4066Sahrens 	 */
1388e0f1c0afSOlaf Faaland 	if (ub->ub_txg < txg) {
1389e0f1c0afSOlaf Faaland 		boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
1390e0f1c0afSOlaf Faaland 		    txg, spa->spa_mmp.mmp_delay);
1391e0f1c0afSOlaf Faaland 
1392e0f1c0afSOlaf Faaland 		if (!changed && list_is_empty(&spa->spa_config_dirty_list))
1393e0f1c0afSOlaf Faaland 			return (0);
1394e0f1c0afSOlaf Faaland 	}
1395fa9e4066Sahrens 
1396fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
1397e14bb325SJeff Bonwick 		return (0);
1398fa9e4066Sahrens 
13990373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
14000373e76bSbonwick 
1401fa9e4066Sahrens 	/*
1402fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
1403fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
1404fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
1405fa9e4066Sahrens 	 * before any uberblock that references them.
1406fa9e4066Sahrens 	 */
140786714001SSerapheim Dimitropoulos 	zio_t *zio = zio_root(spa, NULL, NULL, flags);
140817f17c2dSbonwick 
140986714001SSerapheim Dimitropoulos 	for (vdev_t *vd =
141086714001SSerapheim Dimitropoulos 	    txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
141117f17c2dSbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
141217f17c2dSbonwick 		zio_flush(zio, vd);
141317f17c2dSbonwick 
1414fa9e4066Sahrens 	(void) zio_wait(zio);
1415fa9e4066Sahrens 
1416fa9e4066Sahrens 	/*
1417fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1418fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1419fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1420fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
142117f17c2dSbonwick 	 * which have not yet been touched, will still be valid.  We flush
142217f17c2dSbonwick 	 * the new labels to disk to ensure that all even-label updates
142317f17c2dSbonwick 	 * are committed to stable storage before the uberblock update.
1424fa9e4066Sahrens 	 */
142586714001SSerapheim Dimitropoulos 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
142686714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
142786714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
142886714001SSerapheim Dimitropoulos 			    "for pool '%s' when syncing out the even labels "
142986714001SSerapheim Dimitropoulos 			    "of dirty vdevs", error, spa_name(spa));
143086714001SSerapheim Dimitropoulos 		}
1431eb5bb584SWill Andrews 		goto retry;
143286714001SSerapheim Dimitropoulos 	}
1433fa9e4066Sahrens 
1434fa9e4066Sahrens 	/*
1435e14bb325SJeff Bonwick 	 * Sync the uberblocks to all vdevs in svd[].
14360373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
14370373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1438fa9e4066Sahrens 	 *
1439fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1440fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1441fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1442fa9e4066Sahrens 	 *	to that uberblock.
1443fa9e4066Sahrens 	 *
1444fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1445fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1446fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1447fa9e4066Sahrens 	 *	to the new uberblocks.
1448fa9e4066Sahrens 	 */
144986714001SSerapheim Dimitropoulos 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
145086714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
145186714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
145286714001SSerapheim Dimitropoulos 			    "%d for pool '%s'", error, spa_name(spa));
145386714001SSerapheim Dimitropoulos 		}
1454eb5bb584SWill Andrews 		goto retry;
145586714001SSerapheim Dimitropoulos 	}
1456fa9e4066Sahrens 
1457e0f1c0afSOlaf Faaland 	if (spa_multihost(spa))
1458e0f1c0afSOlaf Faaland 		mmp_update_uberblock(spa, ub);
1459e0f1c0afSOlaf Faaland 
1460fa9e4066Sahrens 	/*
1461fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1462fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1463fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1464fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1465fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
146617f17c2dSbonwick 	 * all labels will be brought up to date.  We flush the new labels
146717f17c2dSbonwick 	 * to disk to ensure that all odd-label updates are committed to
146817f17c2dSbonwick 	 * stable storage before the next transaction group begins.
1469fa9e4066Sahrens 	 */
147086714001SSerapheim Dimitropoulos 	if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
147186714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
147286714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
147386714001SSerapheim Dimitropoulos 			    "for pool '%s' when syncing out the odd labels of "
147486714001SSerapheim Dimitropoulos 			    "dirty vdevs", error, spa_name(spa));
147586714001SSerapheim Dimitropoulos 		}
1476eb5bb584SWill Andrews 		goto retry;
147786714001SSerapheim Dimitropoulos 	}
1478eb5bb584SWill Andrews 
1479eb5bb584SWill Andrews 	return (0);
1480fa9e4066Sahrens }
1481