xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision e0f1c0afa46cc84d4b1e40124032a9a87310386e)
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.
25*e0f1c0afSOlaf 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 {
187e14bb325SJeff Bonwick 	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
188e14bb325SJeff Bonwick 	    SCL_STATE_ALL);
189e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
190fa9e4066Sahrens 
191fa9e4066Sahrens 	zio_nowait(zio_read_phys(zio, vd,
192fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
193fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
194e14bb325SJeff Bonwick 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
195fa9e4066Sahrens }
196fa9e4066Sahrens 
197*e0f1c0afSOlaf Faaland void
198770499e1SDan Kimmel vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
1999a686fbcSPaul Dagnelie     uint64_t size, zio_done_func_t *done, void *private, int flags)
200fa9e4066Sahrens {
201*e0f1c0afSOlaf Faaland #ifdef _KERNEL
202*e0f1c0afSOlaf Faaland 	/*
203*e0f1c0afSOlaf Faaland 	 * This assert is invalid in the user-level ztest MMP code because
204*e0f1c0afSOlaf Faaland 	 * the ztest thread is not in dsl_pool_sync_context. ZoL does not
205*e0f1c0afSOlaf Faaland 	 * build the user-level code with DEBUG so this is not an issue there.
206*e0f1c0afSOlaf Faaland 	 */
207e14bb325SJeff Bonwick 	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
208e14bb325SJeff Bonwick 	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
209e14bb325SJeff Bonwick 	    (SCL_CONFIG | SCL_STATE) &&
210e14bb325SJeff Bonwick 	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
211*e0f1c0afSOlaf Faaland #endif
212e14bb325SJeff Bonwick 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
213fa9e4066Sahrens 
214fa9e4066Sahrens 	zio_nowait(zio_write_phys(zio, vd,
215fa9e4066Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
216fa9e4066Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
21717f17c2dSbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
218fa9e4066Sahrens }
219fa9e4066Sahrens 
22086714001SSerapheim Dimitropoulos static void
22186714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
22286714001SSerapheim Dimitropoulos {
22386714001SSerapheim Dimitropoulos 	spa_t *spa = vd->vdev_spa;
22486714001SSerapheim Dimitropoulos 
22586714001SSerapheim Dimitropoulos 	if (vd != spa->spa_root_vdev)
22686714001SSerapheim Dimitropoulos 		return;
22786714001SSerapheim Dimitropoulos 
22886714001SSerapheim Dimitropoulos 	/* provide either current or previous scan information */
22986714001SSerapheim Dimitropoulos 	pool_scan_stat_t ps;
23086714001SSerapheim Dimitropoulos 	if (spa_scan_get_stats(spa, &ps) == 0) {
23186714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
23286714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
23386714001SSerapheim Dimitropoulos 		    sizeof (pool_scan_stat_t) / sizeof (uint64_t));
23486714001SSerapheim Dimitropoulos 	}
23586714001SSerapheim Dimitropoulos 
23686714001SSerapheim Dimitropoulos 	pool_removal_stat_t prs;
23786714001SSerapheim Dimitropoulos 	if (spa_removal_get_stats(spa, &prs) == 0) {
23886714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
23986714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
24086714001SSerapheim Dimitropoulos 		    sizeof (prs) / sizeof (uint64_t));
24186714001SSerapheim Dimitropoulos 	}
24286714001SSerapheim Dimitropoulos 
24386714001SSerapheim Dimitropoulos 	pool_checkpoint_stat_t pcs;
24486714001SSerapheim Dimitropoulos 	if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
24586714001SSerapheim Dimitropoulos 		fnvlist_add_uint64_array(nvl,
24686714001SSerapheim Dimitropoulos 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
24786714001SSerapheim Dimitropoulos 		    sizeof (pcs) / sizeof (uint64_t));
24886714001SSerapheim Dimitropoulos 	}
24986714001SSerapheim Dimitropoulos }
25086714001SSerapheim Dimitropoulos 
251fa9e4066Sahrens /*
252fa9e4066Sahrens  * Generate the nvlist representing this vdev's config.
253fa9e4066Sahrens  */
254fa9e4066Sahrens nvlist_t *
25599653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
2563f9d6ad7SLin Ling     vdev_config_flag_t flags)
257fa9e4066Sahrens {
258fa9e4066Sahrens 	nvlist_t *nv = NULL;
2595cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
260fa9e4066Sahrens 
261b4952e17SGeorge Wilson 	nv = fnvlist_alloc();
262fa9e4066Sahrens 
263b4952e17SGeorge Wilson 	fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
2643f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
265b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
266b4952e17SGeorge Wilson 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
267fa9e4066Sahrens 
268fa9e4066Sahrens 	if (vd->vdev_path != NULL)
269b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
270fa9e4066Sahrens 
271fa9e4066Sahrens 	if (vd->vdev_devid != NULL)
272b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
273fa9e4066Sahrens 
2743d7072f8Seschrock 	if (vd->vdev_physpath != NULL)
275b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
276b4952e17SGeorge Wilson 		    vd->vdev_physpath);
2773d7072f8Seschrock 
2786809eb4eSEric Schrock 	if (vd->vdev_fru != NULL)
279b4952e17SGeorge Wilson 		fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
2806809eb4eSEric Schrock 
28199653d4eSeschrock 	if (vd->vdev_nparity != 0) {
28299653d4eSeschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
28399653d4eSeschrock 		    VDEV_TYPE_RAIDZ) == 0);
28499653d4eSeschrock 
28599653d4eSeschrock 		/*
28699653d4eSeschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
28799653d4eSeschrock 		 * into a crufty old storage pool.
28899653d4eSeschrock 		 */
28999653d4eSeschrock 		ASSERT(vd->vdev_nparity == 1 ||
290f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 2 &&
291f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
292f94275ceSAdam Leventhal 		    (vd->vdev_nparity <= 3 &&
293f94275ceSAdam Leventhal 		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
29499653d4eSeschrock 
29599653d4eSeschrock 		/*
29699653d4eSeschrock 		 * Note that we'll add the nparity tag even on storage pools
29799653d4eSeschrock 		 * that only support a single parity device -- older software
29899653d4eSeschrock 		 * will just ignore it.
29999653d4eSeschrock 		 */
300b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
30199653d4eSeschrock 	}
30299653d4eSeschrock 
303afefbcddSeschrock 	if (vd->vdev_wholedisk != -1ULL)
304b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
305b4952e17SGeorge Wilson 		    vd->vdev_wholedisk);
306afefbcddSeschrock 
3076f793812SPavel Zakharov 	if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
308b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
309ea8dc4b6Seschrock 
31099653d4eSeschrock 	if (vd->vdev_isspare)
311b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
31299653d4eSeschrock 
3133f9d6ad7SLin Ling 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
3143f9d6ad7SLin Ling 	    vd == vd->vdev_top) {
315b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
316b4952e17SGeorge Wilson 		    vd->vdev_ms_array);
317b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
318b4952e17SGeorge Wilson 		    vd->vdev_ms_shift);
319b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
320b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
321b4952e17SGeorge Wilson 		    vd->vdev_asize);
322b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
3235cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_removing) {
324b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
325b4952e17SGeorge Wilson 			    vd->vdev_removing);
3265cabbc6bSPrashanth Sreenivasa 		}
327fa9e4066Sahrens 	}
328fa9e4066Sahrens 
3290713e232SGeorge Wilson 	if (vd->vdev_dtl_sm != NULL) {
330b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
3310713e232SGeorge Wilson 		    space_map_object(vd->vdev_dtl_sm));
3320713e232SGeorge Wilson 	}
333fa9e4066Sahrens 
3345cabbc6bSPrashanth Sreenivasa 	if (vic->vic_mapping_object != 0) {
3355cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
3365cabbc6bSPrashanth Sreenivasa 		    vic->vic_mapping_object);
3375cabbc6bSPrashanth Sreenivasa 	}
3385cabbc6bSPrashanth Sreenivasa 
3395cabbc6bSPrashanth Sreenivasa 	if (vic->vic_births_object != 0) {
3405cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
3415cabbc6bSPrashanth Sreenivasa 		    vic->vic_births_object);
3425cabbc6bSPrashanth Sreenivasa 	}
3435cabbc6bSPrashanth Sreenivasa 
3445cabbc6bSPrashanth Sreenivasa 	if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
3455cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
3465cabbc6bSPrashanth Sreenivasa 		    vic->vic_prev_indirect_vdev);
3475cabbc6bSPrashanth Sreenivasa 	}
3485cabbc6bSPrashanth Sreenivasa 
34988ecc943SGeorge Wilson 	if (vd->vdev_crtxg)
350b4952e17SGeorge Wilson 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
35188ecc943SGeorge Wilson 
352215198a6SJoe Stein 	if (flags & VDEV_CONFIG_MOS) {
353215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0) {
354215198a6SJoe Stein 			ASSERT(vd->vdev_ops->vdev_op_leaf);
355215198a6SJoe Stein 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
356215198a6SJoe Stein 			    vd->vdev_leaf_zap);
357215198a6SJoe Stein 		}
358215198a6SJoe Stein 
359215198a6SJoe Stein 		if (vd->vdev_top_zap != 0) {
360215198a6SJoe Stein 			ASSERT(vd == vd->vdev_top);
361215198a6SJoe Stein 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
362215198a6SJoe Stein 			    vd->vdev_top_zap);
363215198a6SJoe Stein 		}
364215198a6SJoe Stein 	}
365215198a6SJoe Stein 
366fa9e4066Sahrens 	if (getstats) {
367fa9e4066Sahrens 		vdev_stat_t vs;
3683f9d6ad7SLin Ling 
369fa9e4066Sahrens 		vdev_get_stats(vd, &vs);
370b4952e17SGeorge Wilson 		fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
371b4952e17SGeorge Wilson 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t));
3723f9d6ad7SLin Ling 
37386714001SSerapheim Dimitropoulos 		root_vdev_actions_getprogress(vd, nv);
3745cabbc6bSPrashanth Sreenivasa 
3755cabbc6bSPrashanth Sreenivasa 		/*
3765cabbc6bSPrashanth Sreenivasa 		 * Note: this can be called from open context
3775cabbc6bSPrashanth Sreenivasa 		 * (spa_get_stats()), so we need the rwlock to prevent
3785cabbc6bSPrashanth Sreenivasa 		 * the mapping from being changed by condensing.
3795cabbc6bSPrashanth Sreenivasa 		 */
3805cabbc6bSPrashanth Sreenivasa 		rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
3815cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_indirect_mapping != NULL) {
3825cabbc6bSPrashanth Sreenivasa 			ASSERT(vd->vdev_indirect_births != NULL);
3835cabbc6bSPrashanth Sreenivasa 			vdev_indirect_mapping_t *vim =
3845cabbc6bSPrashanth Sreenivasa 			    vd->vdev_indirect_mapping;
3855cabbc6bSPrashanth Sreenivasa 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
3865cabbc6bSPrashanth Sreenivasa 			    vdev_indirect_mapping_size(vim));
3875cabbc6bSPrashanth Sreenivasa 		}
3885cabbc6bSPrashanth Sreenivasa 		rw_exit(&vd->vdev_indirect_rwlock);
3895cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_mg != NULL &&
3905cabbc6bSPrashanth Sreenivasa 		    vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
3915cabbc6bSPrashanth Sreenivasa 			/*
3925cabbc6bSPrashanth Sreenivasa 			 * Compute approximately how much memory would be used
3935cabbc6bSPrashanth Sreenivasa 			 * for the indirect mapping if this device were to
3945cabbc6bSPrashanth Sreenivasa 			 * be removed.
3955cabbc6bSPrashanth Sreenivasa 			 *
3965cabbc6bSPrashanth Sreenivasa 			 * Note: If the frag metric is invalid, then not
3975cabbc6bSPrashanth Sreenivasa 			 * enough metaslabs have been converted to have
3985cabbc6bSPrashanth Sreenivasa 			 * histograms.
3995cabbc6bSPrashanth Sreenivasa 			 */
4005cabbc6bSPrashanth Sreenivasa 			uint64_t seg_count = 0;
401cfd63e1bSMatthew Ahrens 			uint64_t to_alloc = vd->vdev_stat.vs_alloc;
4025cabbc6bSPrashanth Sreenivasa 
4035cabbc6bSPrashanth Sreenivasa 			/*
4045cabbc6bSPrashanth Sreenivasa 			 * There are the same number of allocated segments
4055cabbc6bSPrashanth Sreenivasa 			 * as free segments, so we will have at least one
406cfd63e1bSMatthew Ahrens 			 * entry per free segment.  However, small free
407cfd63e1bSMatthew Ahrens 			 * segments (smaller than vdev_removal_max_span)
408cfd63e1bSMatthew Ahrens 			 * will be combined with adjacent allocated segments
409cfd63e1bSMatthew Ahrens 			 * as a single mapping.
4105cabbc6bSPrashanth Sreenivasa 			 */
4115cabbc6bSPrashanth Sreenivasa 			for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
412cfd63e1bSMatthew Ahrens 				if (1ULL << (i + 1) < vdev_removal_max_span) {
413cfd63e1bSMatthew Ahrens 					to_alloc +=
414cfd63e1bSMatthew Ahrens 					    vd->vdev_mg->mg_histogram[i] <<
415cfd63e1bSMatthew Ahrens 					    i + 1;
416cfd63e1bSMatthew Ahrens 				} else {
417cfd63e1bSMatthew Ahrens 					seg_count +=
418cfd63e1bSMatthew Ahrens 					    vd->vdev_mg->mg_histogram[i];
419cfd63e1bSMatthew Ahrens 				}
4205cabbc6bSPrashanth Sreenivasa 			}
4215cabbc6bSPrashanth Sreenivasa 
4225cabbc6bSPrashanth Sreenivasa 			/*
423cfd63e1bSMatthew Ahrens 			 * The maximum length of a mapping is
424cfd63e1bSMatthew Ahrens 			 * zfs_remove_max_segment, so we need at least one entry
425cfd63e1bSMatthew Ahrens 			 * per zfs_remove_max_segment of allocated data.
4265cabbc6bSPrashanth Sreenivasa 			 */
427cfd63e1bSMatthew Ahrens 			seg_count += to_alloc / zfs_remove_max_segment;
4285cabbc6bSPrashanth Sreenivasa 
4295cabbc6bSPrashanth Sreenivasa 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
4305cabbc6bSPrashanth Sreenivasa 			    seg_count *
4315cabbc6bSPrashanth Sreenivasa 			    sizeof (vdev_indirect_mapping_entry_phys_t));
4325cabbc6bSPrashanth Sreenivasa 		}
433fa9e4066Sahrens 	}
434fa9e4066Sahrens 
435fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
436fa9e4066Sahrens 		nvlist_t **child;
4373f9d6ad7SLin Ling 		int c, idx;
438fa9e4066Sahrens 
43988ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
44088ecc943SGeorge Wilson 
441fa9e4066Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
442fa9e4066Sahrens 		    KM_SLEEP);
443fa9e4066Sahrens 
4443f9d6ad7SLin Ling 		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
4453f9d6ad7SLin Ling 			vdev_t *cvd = vd->vdev_child[c];
4463f9d6ad7SLin Ling 
4473f9d6ad7SLin Ling 			/*
4483f9d6ad7SLin Ling 			 * If we're generating an nvlist of removing
4493f9d6ad7SLin Ling 			 * vdevs then skip over any device which is
4503f9d6ad7SLin Ling 			 * not being removed.
4513f9d6ad7SLin Ling 			 */
4523f9d6ad7SLin Ling 			if ((flags & VDEV_CONFIG_REMOVING) &&
4533f9d6ad7SLin Ling 			    !cvd->vdev_removing)
4543f9d6ad7SLin Ling 				continue;
455fa9e4066Sahrens 
4563f9d6ad7SLin Ling 			child[idx++] = vdev_config_generate(spa, cvd,
4573f9d6ad7SLin Ling 			    getstats, flags);
4583f9d6ad7SLin Ling 		}
4593f9d6ad7SLin Ling 
4603f9d6ad7SLin Ling 		if (idx) {
461b4952e17SGeorge Wilson 			fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
462b4952e17SGeorge Wilson 			    child, idx);
4633f9d6ad7SLin Ling 		}
464fa9e4066Sahrens 
4653f9d6ad7SLin Ling 		for (c = 0; c < idx; c++)
466fa9e4066Sahrens 			nvlist_free(child[c]);
467fa9e4066Sahrens 
468fa9e4066Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
469441d80aaSlling 
470441d80aaSlling 	} else {
471069f55e2SEric Schrock 		const char *aux = NULL;
472069f55e2SEric Schrock 
473ecc2d604Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
474b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
475b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0)
476b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
477b4952e17SGeorge Wilson 			    vd->vdev_resilver_txg);
4783d7072f8Seschrock 		if (vd->vdev_faulted)
479b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
4803d7072f8Seschrock 		if (vd->vdev_degraded)
481b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
4823d7072f8Seschrock 		if (vd->vdev_removed)
483b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
4843d7072f8Seschrock 		if (vd->vdev_unspare)
485b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
48688ecc943SGeorge Wilson 		if (vd->vdev_ishole)
487b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
488069f55e2SEric Schrock 
489069f55e2SEric Schrock 		switch (vd->vdev_stat.vs_aux) {
490069f55e2SEric Schrock 		case VDEV_AUX_ERR_EXCEEDED:
491069f55e2SEric Schrock 			aux = "err_exceeded";
492069f55e2SEric Schrock 			break;
493069f55e2SEric Schrock 
494069f55e2SEric Schrock 		case VDEV_AUX_EXTERNAL:
495069f55e2SEric Schrock 			aux = "external";
496069f55e2SEric Schrock 			break;
497069f55e2SEric Schrock 		}
498069f55e2SEric Schrock 
499069f55e2SEric Schrock 		if (aux != NULL)
500b4952e17SGeorge Wilson 			fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
5011195e687SMark J Musante 
5021195e687SMark J Musante 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
503b4952e17SGeorge Wilson 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
504b4952e17SGeorge Wilson 			    vd->vdev_orig_guid);
5051195e687SMark J Musante 		}
506fa9e4066Sahrens 	}
507fa9e4066Sahrens 
508fa9e4066Sahrens 	return (nv);
509fa9e4066Sahrens }
510fa9e4066Sahrens 
51188ecc943SGeorge Wilson /*
51288ecc943SGeorge Wilson  * Generate a view of the top-level vdevs.  If we currently have holes
51388ecc943SGeorge Wilson  * in the namespace, then generate an array which contains a list of holey
51488ecc943SGeorge Wilson  * vdevs.  Additionally, add the number of top-level children that currently
51588ecc943SGeorge Wilson  * exist.
51688ecc943SGeorge Wilson  */
51788ecc943SGeorge Wilson void
51888ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config)
51988ecc943SGeorge Wilson {
52088ecc943SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
52188ecc943SGeorge Wilson 	uint64_t *array;
5223f9d6ad7SLin Ling 	uint_t c, idx;
52388ecc943SGeorge Wilson 
52488ecc943SGeorge Wilson 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
52588ecc943SGeorge Wilson 
5263f9d6ad7SLin Ling 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
52788ecc943SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
52888ecc943SGeorge Wilson 
5295cabbc6bSPrashanth Sreenivasa 		if (tvd->vdev_ishole) {
53088ecc943SGeorge Wilson 			array[idx++] = c;
5315cabbc6bSPrashanth Sreenivasa 		}
53288ecc943SGeorge Wilson 	}
53388ecc943SGeorge Wilson 
534312c6e15SGeorge Wilson 	if (idx) {
535312c6e15SGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
536312c6e15SGeorge Wilson 		    array, idx) == 0);
537312c6e15SGeorge Wilson 	}
538312c6e15SGeorge Wilson 
53988ecc943SGeorge Wilson 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
54088ecc943SGeorge Wilson 	    rvd->vdev_children) == 0);
54188ecc943SGeorge Wilson 
54288ecc943SGeorge Wilson 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
54388ecc943SGeorge Wilson }
54488ecc943SGeorge Wilson 
545ad135b5dSChristopher Siden /*
546dfbb9432SGeorge Wilson  * Returns the configuration from the label of the given vdev. For vdevs
547dfbb9432SGeorge Wilson  * which don't have a txg value stored on their label (i.e. spares/cache)
548dfbb9432SGeorge Wilson  * or have not been completely initialized (txg = 0) just return
549dfbb9432SGeorge Wilson  * the configuration from the first valid label we find. Otherwise,
550dfbb9432SGeorge Wilson  * find the most up-to-date label that does not exceed the specified
551dfbb9432SGeorge Wilson  * 'txg' value.
552ad135b5dSChristopher Siden  */
553fa9e4066Sahrens nvlist_t *
554dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg)
555fa9e4066Sahrens {
5560373e76bSbonwick 	spa_t *spa = vd->vdev_spa;
557fa9e4066Sahrens 	nvlist_t *config = NULL;
558fa9e4066Sahrens 	vdev_phys_t *vp;
559770499e1SDan Kimmel 	abd_t *vp_abd;
560fa9e4066Sahrens 	zio_t *zio;
561dfbb9432SGeorge Wilson 	uint64_t best_txg = 0;
562b6bf6e15SPavel Zakharov 	uint64_t label_txg = 0;
563dfbb9432SGeorge Wilson 	int error = 0;
5648956713aSEric Schrock 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
5658956713aSEric Schrock 	    ZIO_FLAG_SPECULATIVE;
566fa9e4066Sahrens 
567e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
5680373e76bSbonwick 
5690a4e9518Sgw 	if (!vdev_readable(vd))
570fa9e4066Sahrens 		return (NULL);
571fa9e4066Sahrens 
572770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
573770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
574fa9e4066Sahrens 
5758956713aSEric Schrock retry:
576e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
577dfbb9432SGeorge Wilson 		nvlist_t *label = NULL;
578fa9e4066Sahrens 
579e14bb325SJeff Bonwick 		zio = zio_root(spa, NULL, NULL, flags);
580fa9e4066Sahrens 
581770499e1SDan Kimmel 		vdev_label_read(zio, vd, l, vp_abd,
582fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
583e14bb325SJeff Bonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
584fa9e4066Sahrens 
585fa9e4066Sahrens 		if (zio_wait(zio) == 0 &&
586fa9e4066Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
587dfbb9432SGeorge Wilson 		    &label, 0) == 0) {
588dfbb9432SGeorge Wilson 			/*
589dfbb9432SGeorge Wilson 			 * Auxiliary vdevs won't have txg values in their
590dfbb9432SGeorge Wilson 			 * labels and newly added vdevs may not have been
591dfbb9432SGeorge Wilson 			 * completely initialized so just return the
592dfbb9432SGeorge Wilson 			 * configuration from the first valid label we
593dfbb9432SGeorge Wilson 			 * encounter.
594dfbb9432SGeorge Wilson 			 */
595dfbb9432SGeorge Wilson 			error = nvlist_lookup_uint64(label,
596dfbb9432SGeorge Wilson 			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
597dfbb9432SGeorge Wilson 			if ((error || label_txg == 0) && !config) {
598dfbb9432SGeorge Wilson 				config = label;
599dfbb9432SGeorge Wilson 				break;
600dfbb9432SGeorge Wilson 			} else if (label_txg <= txg && label_txg > best_txg) {
601dfbb9432SGeorge Wilson 				best_txg = label_txg;
602dfbb9432SGeorge Wilson 				nvlist_free(config);
603dfbb9432SGeorge Wilson 				config = fnvlist_dup(label);
604dfbb9432SGeorge Wilson 			}
605dfbb9432SGeorge Wilson 		}
606fa9e4066Sahrens 
607dfbb9432SGeorge Wilson 		if (label != NULL) {
608dfbb9432SGeorge Wilson 			nvlist_free(label);
609dfbb9432SGeorge Wilson 			label = NULL;
610fa9e4066Sahrens 		}
611fa9e4066Sahrens 	}
612fa9e4066Sahrens 
6138956713aSEric Schrock 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
6148956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
6158956713aSEric Schrock 		goto retry;
6168956713aSEric Schrock 	}
6178956713aSEric Schrock 
618b6bf6e15SPavel Zakharov 	/*
619b6bf6e15SPavel Zakharov 	 * We found a valid label but it didn't pass txg restrictions.
620b6bf6e15SPavel Zakharov 	 */
621b6bf6e15SPavel Zakharov 	if (config == NULL && label_txg != 0) {
622b6bf6e15SPavel Zakharov 		vdev_dbgmsg(vd, "label discarded as txg is too large "
623b6bf6e15SPavel Zakharov 		    "(%llu > %llu)", (u_longlong_t)label_txg,
624b6bf6e15SPavel Zakharov 		    (u_longlong_t)txg);
625b6bf6e15SPavel Zakharov 	}
626b6bf6e15SPavel Zakharov 
627770499e1SDan Kimmel 	abd_free(vp_abd);
628fa9e4066Sahrens 
629fa9e4066Sahrens 	return (config);
630fa9e4066Sahrens }
631fa9e4066Sahrens 
63239c23413Seschrock /*
63339c23413Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
63439c23413Seschrock  * in with the device guid if this spare is active elsewhere on the system.
63539c23413Seschrock  */
63639c23413Seschrock static boolean_t
63739c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
638fa94a07fSbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
63939c23413Seschrock {
64039c23413Seschrock 	spa_t *spa = vd->vdev_spa;
64139c23413Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
64239c23413Seschrock 	uint64_t vdtxg = 0;
64339c23413Seschrock 	nvlist_t *label;
64439c23413Seschrock 
64539c23413Seschrock 	if (spare_guid)
64639c23413Seschrock 		*spare_guid = 0ULL;
647fa94a07fSbrendan 	if (l2cache_guid)
648fa94a07fSbrendan 		*l2cache_guid = 0ULL;
64939c23413Seschrock 
65039c23413Seschrock 	/*
65139c23413Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
65239c23413Seschrock 	 */
653dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
65439c23413Seschrock 		return (B_FALSE);
65539c23413Seschrock 
65639c23413Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
65739c23413Seschrock 	    &vdtxg);
65839c23413Seschrock 
65939c23413Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
66039c23413Seschrock 	    &state) != 0 ||
66139c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
66239c23413Seschrock 	    &device_guid) != 0) {
66339c23413Seschrock 		nvlist_free(label);
66439c23413Seschrock 		return (B_FALSE);
66539c23413Seschrock 	}
66639c23413Seschrock 
667fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
66839c23413Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
66939c23413Seschrock 	    &pool_guid) != 0 ||
67039c23413Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
67139c23413Seschrock 	    &txg) != 0)) {
67239c23413Seschrock 		nvlist_free(label);
67339c23413Seschrock 		return (B_FALSE);
67439c23413Seschrock 	}
67539c23413Seschrock 
67639c23413Seschrock 	nvlist_free(label);
67739c23413Seschrock 
67839c23413Seschrock 	/*
67939c23413Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
68039c23413Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
68139c23413Seschrock 	 * spare (which we check for later on).
68239c23413Seschrock 	 */
683fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
68439c23413Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
68589a89ebfSlling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
686fa94a07fSbrendan 	    !spa_l2cache_exists(device_guid, NULL))
68739c23413Seschrock 		return (B_FALSE);
68839c23413Seschrock 
68939c23413Seschrock 	/*
69039c23413Seschrock 	 * If the transaction group is zero, then this an initialized (but
69139c23413Seschrock 	 * unused) label.  This is only an error if the create transaction
69239c23413Seschrock 	 * on-disk is the same as the one we're using now, in which case the
69339c23413Seschrock 	 * user has attempted to add the same vdev multiple times in the same
69439c23413Seschrock 	 * transaction.
69539c23413Seschrock 	 */
696fa94a07fSbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
697fa94a07fSbrendan 	    txg == 0 && vdtxg == crtxg)
69839c23413Seschrock 		return (B_TRUE);
69939c23413Seschrock 
70039c23413Seschrock 	/*
70139c23413Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
70239c23413Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
703fa94a07fSbrendan 	 * to add.  We also check if it is an l2cache device.
70439c23413Seschrock 	 */
70589a89ebfSlling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
70639c23413Seschrock 	    spa_has_spare(spa, device_guid)) {
70739c23413Seschrock 		if (spare_guid)
70839c23413Seschrock 			*spare_guid = device_guid;
70939c23413Seschrock 
71039c23413Seschrock 		switch (reason) {
71139c23413Seschrock 		case VDEV_LABEL_CREATE:
712fa94a07fSbrendan 		case VDEV_LABEL_L2CACHE:
71339c23413Seschrock 			return (B_TRUE);
71439c23413Seschrock 
71539c23413Seschrock 		case VDEV_LABEL_REPLACE:
71639c23413Seschrock 			return (!spa_has_spare(spa, device_guid) ||
71739c23413Seschrock 			    spare_pool != 0ULL);
71839c23413Seschrock 
71939c23413Seschrock 		case VDEV_LABEL_SPARE:
72039c23413Seschrock 			return (spa_has_spare(spa, device_guid));
72139c23413Seschrock 		}
72239c23413Seschrock 	}
72339c23413Seschrock 
724fa94a07fSbrendan 	/*
725fa94a07fSbrendan 	 * Check to see if this is an l2cache device.
726fa94a07fSbrendan 	 */
727fa94a07fSbrendan 	if (spa_l2cache_exists(device_guid, NULL))
728fa94a07fSbrendan 		return (B_TRUE);
729fa94a07fSbrendan 
730f9af39baSGeorge Wilson 	/*
731f9af39baSGeorge Wilson 	 * We can't rely on a pool's state if it's been imported
732f9af39baSGeorge Wilson 	 * read-only.  Instead we look to see if the pools is marked
733f9af39baSGeorge Wilson 	 * read-only in the namespace and set the state to active.
734f9af39baSGeorge Wilson 	 */
7355bdd995dSRichard Yao 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
7365bdd995dSRichard Yao 	    (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
737f9af39baSGeorge Wilson 	    spa_mode(spa) == FREAD)
738f9af39baSGeorge Wilson 		state = POOL_STATE_ACTIVE;
739f9af39baSGeorge Wilson 
74039c23413Seschrock 	/*
74139c23413Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
74239c23413Seschrock 	 * pool on the system.
74339c23413Seschrock 	 */
74439c23413Seschrock 	return (state == POOL_STATE_ACTIVE);
74539c23413Seschrock }
74639c23413Seschrock 
74739c23413Seschrock /*
74839c23413Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
74939c23413Seschrock  * use, and writable.  We put down an initial label which we will later
75039c23413Seschrock  * overwrite with a complete label.  Note that it's important to do this
75139c23413Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
75239c23413Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
75339c23413Seschrock  * itself.
75439c23413Seschrock  */
75539c23413Seschrock int
75639c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
757fa9e4066Sahrens {
758fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
759fa9e4066Sahrens 	nvlist_t *label;
760fa9e4066Sahrens 	vdev_phys_t *vp;
761770499e1SDan Kimmel 	abd_t *vp_abd;
762770499e1SDan Kimmel 	abd_t *pad2;
763ecc2d604Sbonwick 	uberblock_t *ub;
764770499e1SDan Kimmel 	abd_t *ub_abd;
765fa9e4066Sahrens 	zio_t *zio;
766fa9e4066Sahrens 	char *buf;
767fa9e4066Sahrens 	size_t buflen;
768fa9e4066Sahrens 	int error;
769fa94a07fSbrendan 	uint64_t spare_guid, l2cache_guid;
770e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
771fa9e4066Sahrens 
772e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
7730373e76bSbonwick 
774e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
77539c23413Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
77639c23413Seschrock 		    crtxg, reason)) != 0)
777fa9e4066Sahrens 			return (error);
778fa9e4066Sahrens 
77988ecc943SGeorge Wilson 	/* Track the creation time for this vdev */
78088ecc943SGeorge Wilson 	vd->vdev_crtxg = crtxg;
78188ecc943SGeorge Wilson 
782973c78e9SGeorge Wilson 	if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
783fa9e4066Sahrens 		return (0);
784fa9e4066Sahrens 
785fa9e4066Sahrens 	/*
78639c23413Seschrock 	 * Dead vdevs cannot be initialized.
787fa9e4066Sahrens 	 */
788fa9e4066Sahrens 	if (vdev_is_dead(vd))
789be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
790fa9e4066Sahrens 
791fa9e4066Sahrens 	/*
79239c23413Seschrock 	 * Determine if the vdev is in use.
793fa9e4066Sahrens 	 */
7941195e687SMark J Musante 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
795fa94a07fSbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
796be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
79739c23413Seschrock 
79839c23413Seschrock 	/*
799fa94a07fSbrendan 	 * If this is a request to add or replace a spare or l2cache device
800fa94a07fSbrendan 	 * that is in use elsewhere on the system, then we must update the
801fa94a07fSbrendan 	 * guid (which was initialized to a random value) to reflect the
802fa94a07fSbrendan 	 * actual GUID (which is shared between multiple pools).
80339c23413Seschrock 	 */
804fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
805fa94a07fSbrendan 	    spare_guid != 0ULL) {
80631157203SJeff Bonwick 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
80799653d4eSeschrock 
80831157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
80931157203SJeff Bonwick 
81031157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
81131157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
81239c23413Seschrock 
81399653d4eSeschrock 		/*
81439c23413Seschrock 		 * If this is a replacement, then we want to fallthrough to the
81539c23413Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
8163d7072f8Seschrock 		 * labeled appropriately and we can just return.
81799653d4eSeschrock 		 */
81839c23413Seschrock 		if (reason == VDEV_LABEL_SPARE)
81939c23413Seschrock 			return (0);
8201195e687SMark J Musante 		ASSERT(reason == VDEV_LABEL_REPLACE ||
8211195e687SMark J Musante 		    reason == VDEV_LABEL_SPLIT);
822fa9e4066Sahrens 	}
823fa9e4066Sahrens 
824fa94a07fSbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
825fa94a07fSbrendan 	    l2cache_guid != 0ULL) {
82631157203SJeff Bonwick 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
82731157203SJeff Bonwick 
82831157203SJeff Bonwick 		vd->vdev_guid += guid_delta;
829fa94a07fSbrendan 
83031157203SJeff Bonwick 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
83131157203SJeff Bonwick 			pvd->vdev_guid_sum += guid_delta;
832fa94a07fSbrendan 
833fa94a07fSbrendan 		/*
834fa94a07fSbrendan 		 * If this is a replacement, then we want to fallthrough to the
835fa94a07fSbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
836fa94a07fSbrendan 		 * already labeled appropriately and we can just return.
837fa94a07fSbrendan 		 */
838fa94a07fSbrendan 		if (reason == VDEV_LABEL_L2CACHE)
839fa94a07fSbrendan 			return (0);
840fa94a07fSbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
841fa94a07fSbrendan 	}
842fa94a07fSbrendan 
843fa9e4066Sahrens 	/*
84439c23413Seschrock 	 * Initialize its label.
845fa9e4066Sahrens 	 */
846770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
847770499e1SDan Kimmel 	abd_zero(vp_abd, sizeof (vdev_phys_t));
848770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
849fa9e4066Sahrens 
850fa9e4066Sahrens 	/*
851fa9e4066Sahrens 	 * Generate a label describing the pool and our top-level vdev.
852fa9e4066Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
853fa9e4066Sahrens 	 * really part of an active pool just yet.  The labels will
854fa9e4066Sahrens 	 * be written again with a meaningful txg by spa_sync().
855fa9e4066Sahrens 	 */
85639c23413Seschrock 	if (reason == VDEV_LABEL_SPARE ||
85739c23413Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
85839c23413Seschrock 		/*
85939c23413Seschrock 		 * For inactive hot spares, we generate a special label that
86039c23413Seschrock 		 * identifies as a mutually shared hot spare.  We write the
86139c23413Seschrock 		 * label if we are adding a hot spare, or if we are removing an
86239c23413Seschrock 		 * active hot spare (in which case we want to revert the
86339c23413Seschrock 		 * labels).
86439c23413Seschrock 		 */
86599653d4eSeschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
86699653d4eSeschrock 
86799653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
86899653d4eSeschrock 		    spa_version(spa)) == 0);
86999653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
87099653d4eSeschrock 		    POOL_STATE_SPARE) == 0);
87199653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
87299653d4eSeschrock 		    vd->vdev_guid) == 0);
873fa94a07fSbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
874fa94a07fSbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
875fa94a07fSbrendan 		/*
876fa94a07fSbrendan 		 * For level 2 ARC devices, add a special label.
877fa94a07fSbrendan 		 */
878fa94a07fSbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
879fa94a07fSbrendan 
880fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
881fa94a07fSbrendan 		    spa_version(spa)) == 0);
882fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
883fa94a07fSbrendan 		    POOL_STATE_L2CACHE) == 0);
884fa94a07fSbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
885fa94a07fSbrendan 		    vd->vdev_guid) == 0);
88699653d4eSeschrock 	} else {
8871195e687SMark J Musante 		uint64_t txg = 0ULL;
8881195e687SMark J Musante 
8891195e687SMark J Musante 		if (reason == VDEV_LABEL_SPLIT)
8901195e687SMark J Musante 			txg = spa->spa_uberblock.ub_txg;
8911195e687SMark J Musante 		label = spa_config_generate(spa, vd, txg, B_FALSE);
89299653d4eSeschrock 
89399653d4eSeschrock 		/*
89499653d4eSeschrock 		 * Add our creation time.  This allows us to detect multiple
89599653d4eSeschrock 		 * vdev uses as described above, and automatically expires if we
89699653d4eSeschrock 		 * fail.
89799653d4eSeschrock 		 */
89899653d4eSeschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
89999653d4eSeschrock 		    crtxg) == 0);
90099653d4eSeschrock 	}
901fa9e4066Sahrens 
902fa9e4066Sahrens 	buf = vp->vp_nvlist;
903fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
904fa9e4066Sahrens 
905a75573b6Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
906a75573b6Smmusante 	if (error != 0) {
907fa9e4066Sahrens 		nvlist_free(label);
908770499e1SDan Kimmel 		abd_free(vp_abd);
909a75573b6Smmusante 		/* EFAULT means nvlist_pack ran out of room */
910a75573b6Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
911fa9e4066Sahrens 	}
912fa9e4066Sahrens 
913fa9e4066Sahrens 	/*
914fa9e4066Sahrens 	 * Initialize uberblock template.
915fa9e4066Sahrens 	 */
916770499e1SDan Kimmel 	ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
917770499e1SDan Kimmel 	abd_zero(ub_abd, VDEV_UBERBLOCK_RING);
918770499e1SDan Kimmel 	abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
919770499e1SDan Kimmel 	ub = abd_to_buf(ub_abd);
920ecc2d604Sbonwick 	ub->ub_txg = 0;
921fa9e4066Sahrens 
922f83ffe1aSLin Ling 	/* Initialize the 2nd padding area. */
923770499e1SDan Kimmel 	pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
924770499e1SDan Kimmel 	abd_zero(pad2, VDEV_PAD_SIZE);
925f83ffe1aSLin Ling 
926fa9e4066Sahrens 	/*
927fa9e4066Sahrens 	 * Write everything in parallel.
928fa9e4066Sahrens 	 */
9298956713aSEric Schrock retry:
93017f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
931fa9e4066Sahrens 
932e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++) {
933fa9e4066Sahrens 
934770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, vp_abd,
935fa9e4066Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
93617f17c2dSbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
937fa9e4066Sahrens 
938f83ffe1aSLin Ling 		/*
939f83ffe1aSLin Ling 		 * Skip the 1st padding area.
940f83ffe1aSLin Ling 		 * Zero out the 2nd padding area where it might have
941f83ffe1aSLin Ling 		 * left over data from previous filesystem format.
942f83ffe1aSLin Ling 		 */
943f83ffe1aSLin Ling 		vdev_label_write(zio, vd, l, pad2,
944f83ffe1aSLin Ling 		    offsetof(vdev_label_t, vl_pad2),
945f83ffe1aSLin Ling 		    VDEV_PAD_SIZE, NULL, NULL, flags);
946f83ffe1aSLin Ling 
947770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, ub_abd,
948f64c0e34SEric Taylor 		    offsetof(vdev_label_t, vl_uberblock),
949f64c0e34SEric Taylor 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
950fa9e4066Sahrens 	}
951fa9e4066Sahrens 
952fa9e4066Sahrens 	error = zio_wait(zio);
953fa9e4066Sahrens 
9548956713aSEric Schrock 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
9558956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
9568956713aSEric Schrock 		goto retry;
9578956713aSEric Schrock 	}
9588956713aSEric Schrock 
959fa9e4066Sahrens 	nvlist_free(label);
960770499e1SDan Kimmel 	abd_free(pad2);
961770499e1SDan Kimmel 	abd_free(ub_abd);
962770499e1SDan Kimmel 	abd_free(vp_abd);
963fa9e4066Sahrens 
96439c23413Seschrock 	/*
96539c23413Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
9663d7072f8Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
967fa94a07fSbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
968fa94a07fSbrendan 	 * level 2 ARC devices.
96939c23413Seschrock 	 */
97039c23413Seschrock 	if (error == 0 && !vd->vdev_isspare &&
97139c23413Seschrock 	    (reason == VDEV_LABEL_SPARE ||
97289a89ebfSlling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
97339c23413Seschrock 		spa_spare_add(vd);
97499653d4eSeschrock 
975fa94a07fSbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
976fa94a07fSbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
977fa94a07fSbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
978fa94a07fSbrendan 		spa_l2cache_add(vd);
979fa94a07fSbrendan 
98039c23413Seschrock 	return (error);
98199653d4eSeschrock }
98299653d4eSeschrock 
983fa9e4066Sahrens /*
984fa9e4066Sahrens  * ==========================================================================
985fa9e4066Sahrens  * uberblock load/sync
986fa9e4066Sahrens  * ==========================================================================
987fa9e4066Sahrens  */
988fa9e4066Sahrens 
989fa9e4066Sahrens /*
990fa9e4066Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
991fa9e4066Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
992fa9e4066Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
993fa9e4066Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
994fa9e4066Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
995ad135b5dSChristopher Siden  * the missing replica comes back, then for a few seconds we'll have two
996fa9e4066Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
997fa9e4066Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
998fa9e4066Sahrens  */
999fa9e4066Sahrens static int
1000fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
1001fa9e4066Sahrens {
1002fa9e4066Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
1003fa9e4066Sahrens 		return (-1);
1004fa9e4066Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
1005fa9e4066Sahrens 		return (1);
1006fa9e4066Sahrens 
1007fa9e4066Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
1008fa9e4066Sahrens 		return (-1);
1009fa9e4066Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
1010fa9e4066Sahrens 		return (1);
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	return (0);
1013fa9e4066Sahrens }
1014fa9e4066Sahrens 
1015ad135b5dSChristopher Siden struct ubl_cbdata {
1016ad135b5dSChristopher Siden 	uberblock_t	*ubl_ubbest;	/* Best uberblock */
1017ad135b5dSChristopher Siden 	vdev_t		*ubl_vd;	/* vdev associated with the above */
1018ad135b5dSChristopher Siden };
1019ad135b5dSChristopher Siden 
1020fa9e4066Sahrens static void
1021fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
1022fa9e4066Sahrens {
1023ad135b5dSChristopher Siden 	vdev_t *vd = zio->io_vd;
1024468c413aSTim Haley 	spa_t *spa = zio->io_spa;
1025e14bb325SJeff Bonwick 	zio_t *rio = zio->io_private;
1026770499e1SDan Kimmel 	uberblock_t *ub = abd_to_buf(zio->io_abd);
1027ad135b5dSChristopher Siden 	struct ubl_cbdata *cbp = rio->io_private;
1028fa9e4066Sahrens 
1029ad135b5dSChristopher Siden 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1030fa9e4066Sahrens 
1031ea8dc4b6Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1032e14bb325SJeff Bonwick 		mutex_enter(&rio->io_lock);
1033468c413aSTim Haley 		if (ub->ub_txg <= spa->spa_load_max_txg &&
1034ad135b5dSChristopher Siden 		    vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1035ad135b5dSChristopher Siden 			/*
1036dfbb9432SGeorge Wilson 			 * Keep track of the vdev in which this uberblock
1037dfbb9432SGeorge Wilson 			 * was found. We will use this information later
1038dfbb9432SGeorge Wilson 			 * to obtain the config nvlist associated with
1039ad135b5dSChristopher Siden 			 * this uberblock.
1040ad135b5dSChristopher Siden 			 */
1041ad135b5dSChristopher Siden 			*cbp->ubl_ubbest = *ub;
1042ad135b5dSChristopher Siden 			cbp->ubl_vd = vd;
1043ad135b5dSChristopher Siden 		}
1044e14bb325SJeff Bonwick 		mutex_exit(&rio->io_lock);
1045fa9e4066Sahrens 	}
1046fa9e4066Sahrens 
1047770499e1SDan Kimmel 	abd_free(zio->io_abd);
1048fa9e4066Sahrens }
1049fa9e4066Sahrens 
1050ad135b5dSChristopher Siden static void
1051ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1052ad135b5dSChristopher Siden     struct ubl_cbdata *cbp)
1053fa9e4066Sahrens {
1054e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1055ad135b5dSChristopher Siden 		vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1056fa9e4066Sahrens 
1057e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1058e14bb325SJeff Bonwick 		for (int l = 0; l < VDEV_LABELS; l++) {
1059e14bb325SJeff Bonwick 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1060e14bb325SJeff Bonwick 				vdev_label_read(zio, vd, l,
1061770499e1SDan Kimmel 				    abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1062770499e1SDan Kimmel 				    B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1063e14bb325SJeff Bonwick 				    VDEV_UBERBLOCK_SIZE(vd),
1064e14bb325SJeff Bonwick 				    vdev_uberblock_load_done, zio, flags);
1065e14bb325SJeff Bonwick 			}
1066fa9e4066Sahrens 		}
1067fa9e4066Sahrens 	}
1068ad135b5dSChristopher Siden }
1069e14bb325SJeff Bonwick 
1070ad135b5dSChristopher Siden /*
1071ad135b5dSChristopher Siden  * Reads the 'best' uberblock from disk along with its associated
1072ad135b5dSChristopher Siden  * configuration. First, we read the uberblock array of each label of each
1073ad135b5dSChristopher Siden  * vdev, keeping track of the uberblock with the highest txg in each array.
1074dfbb9432SGeorge Wilson  * Then, we read the configuration from the same vdev as the best uberblock.
1075ad135b5dSChristopher Siden  */
1076ad135b5dSChristopher Siden void
1077ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1078ad135b5dSChristopher Siden {
1079ad135b5dSChristopher Siden 	zio_t *zio;
1080ad135b5dSChristopher Siden 	spa_t *spa = rvd->vdev_spa;
1081ad135b5dSChristopher Siden 	struct ubl_cbdata cb;
1082ad135b5dSChristopher Siden 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1083ad135b5dSChristopher Siden 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1084ad135b5dSChristopher Siden 
1085ad135b5dSChristopher Siden 	ASSERT(ub);
1086ad135b5dSChristopher Siden 	ASSERT(config);
1087ad135b5dSChristopher Siden 
1088ad135b5dSChristopher Siden 	bzero(ub, sizeof (uberblock_t));
1089ad135b5dSChristopher Siden 	*config = NULL;
1090ad135b5dSChristopher Siden 
1091ad135b5dSChristopher Siden 	cb.ubl_ubbest = ub;
1092ad135b5dSChristopher Siden 	cb.ubl_vd = NULL;
1093ad135b5dSChristopher Siden 
1094ad135b5dSChristopher Siden 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1095ad135b5dSChristopher Siden 	zio = zio_root(spa, NULL, &cb, flags);
1096ad135b5dSChristopher Siden 	vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1097ad135b5dSChristopher Siden 	(void) zio_wait(zio);
1098dfbb9432SGeorge Wilson 
1099dfbb9432SGeorge Wilson 	/*
1100dfbb9432SGeorge Wilson 	 * It's possible that the best uberblock was discovered on a label
1101dfbb9432SGeorge Wilson 	 * that has a configuration which was written in a future txg.
1102dfbb9432SGeorge Wilson 	 * Search all labels on this vdev to find the configuration that
1103dfbb9432SGeorge Wilson 	 * matches the txg for our uberblock.
1104dfbb9432SGeorge Wilson 	 */
11053ee8c80cSPavel Zakharov 	if (cb.ubl_vd != NULL) {
11063ee8c80cSPavel Zakharov 		vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
11073ee8c80cSPavel Zakharov 		    "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
11083ee8c80cSPavel Zakharov 
1109dfbb9432SGeorge Wilson 		*config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
11106f793812SPavel Zakharov 		if (*config == NULL && spa->spa_extreme_rewind) {
11116f793812SPavel Zakharov 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
11126f793812SPavel Zakharov 			    "Trying again without txg restrictions.");
11136f793812SPavel Zakharov 			*config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
11146f793812SPavel Zakharov 		}
11153ee8c80cSPavel Zakharov 		if (*config == NULL) {
11163ee8c80cSPavel Zakharov 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
11173ee8c80cSPavel Zakharov 		}
11183ee8c80cSPavel Zakharov 	}
1119ad135b5dSChristopher Siden 	spa_config_exit(spa, SCL_ALL, FTAG);
1120fa9e4066Sahrens }
1121fa9e4066Sahrens 
1122fa9e4066Sahrens /*
112317f17c2dSbonwick  * On success, increment root zio's count of good writes.
11240373e76bSbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1125fa9e4066Sahrens  */
1126fa9e4066Sahrens static void
1127fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
1128fa9e4066Sahrens {
112917f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1130fa9e4066Sahrens 
11310373e76bSbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
11321a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(good_writes);
1133fa9e4066Sahrens }
1134fa9e4066Sahrens 
113517f17c2dSbonwick /*
113617f17c2dSbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
113717f17c2dSbonwick  */
1138fa9e4066Sahrens static void
1139a3b55830SMatthew Ahrens vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1140a3b55830SMatthew Ahrens     uberblock_t *ub, vdev_t *vd, int flags)
1141fa9e4066Sahrens {
1142a3b55830SMatthew Ahrens 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
1143a3b55830SMatthew Ahrens 		vdev_uberblock_sync(zio, good_writes,
1144a3b55830SMatthew Ahrens 		    ub, vd->vdev_child[c], flags);
1145a3b55830SMatthew Ahrens 	}
1146fa9e4066Sahrens 
1147fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1148fa9e4066Sahrens 		return;
1149fa9e4066Sahrens 
1150e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1151fa9e4066Sahrens 		return;
1152fa9e4066Sahrens 
1153*e0f1c0afSOlaf Faaland 	int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1154*e0f1c0afSOlaf Faaland 	int n = ub->ub_txg % (VDEV_UBERBLOCK_COUNT(vd) - m);
1155fa9e4066Sahrens 
1156770499e1SDan Kimmel 	/* Copy the uberblock_t into the ABD */
1157770499e1SDan Kimmel 	abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1158770499e1SDan Kimmel 	abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1159770499e1SDan Kimmel 	abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1160fa9e4066Sahrens 
1161e14bb325SJeff Bonwick 	for (int l = 0; l < VDEV_LABELS; l++)
1162770499e1SDan Kimmel 		vdev_label_write(zio, vd, l, ub_abd,
1163e14bb325SJeff Bonwick 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1164a3b55830SMatthew Ahrens 		    vdev_uberblock_sync_done, good_writes,
1165e14bb325SJeff Bonwick 		    flags | ZIO_FLAG_DONT_PROPAGATE);
1166fa9e4066Sahrens 
1167770499e1SDan Kimmel 	abd_free(ub_abd);
1168fa9e4066Sahrens }
1169fa9e4066Sahrens 
11703e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */
117117f17c2dSbonwick int
117217f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1173fa9e4066Sahrens {
117417f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1175e14bb325SJeff Bonwick 	zio_t *zio;
117617f17c2dSbonwick 	uint64_t good_writes = 0;
1177fa9e4066Sahrens 
1178a3b55830SMatthew Ahrens 	zio = zio_root(spa, NULL, NULL, flags);
1179e14bb325SJeff Bonwick 
1180e14bb325SJeff Bonwick 	for (int v = 0; v < svdcount; v++)
1181a3b55830SMatthew Ahrens 		vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1182fa9e4066Sahrens 
118317f17c2dSbonwick 	(void) zio_wait(zio);
1184fa9e4066Sahrens 
1185fa9e4066Sahrens 	/*
118617f17c2dSbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
118717f17c2dSbonwick 	 * are no longer needed (because the new uberblocks and the even
118817f17c2dSbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
1189fa9e4066Sahrens 	 */
119017f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
1191fa9e4066Sahrens 
11925cabbc6bSPrashanth Sreenivasa 	for (int v = 0; v < svdcount; v++) {
11935cabbc6bSPrashanth Sreenivasa 		if (vdev_writeable(svd[v])) {
11945cabbc6bSPrashanth Sreenivasa 			zio_flush(zio, svd[v]);
11955cabbc6bSPrashanth Sreenivasa 		}
11965cabbc6bSPrashanth Sreenivasa 	}
1197fa9e4066Sahrens 
119817f17c2dSbonwick 	(void) zio_wait(zio);
119917f17c2dSbonwick 
120017f17c2dSbonwick 	return (good_writes >= 1 ? 0 : EIO);
1201fa9e4066Sahrens }
1202fa9e4066Sahrens 
1203fa9e4066Sahrens /*
120417f17c2dSbonwick  * On success, increment the count of good writes for our top-level vdev.
1205fa9e4066Sahrens  */
1206fa9e4066Sahrens static void
120717f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
1208fa9e4066Sahrens {
120917f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
1210fa9e4066Sahrens 
1211fa9e4066Sahrens 	if (zio->io_error == 0)
12121a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(good_writes);
1213fa9e4066Sahrens }
1214fa9e4066Sahrens 
121517f17c2dSbonwick /*
121617f17c2dSbonwick  * If there weren't enough good writes, indicate failure to the parent.
121717f17c2dSbonwick  */
1218fa9e4066Sahrens static void
121917f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
122017f17c2dSbonwick {
122117f17c2dSbonwick 	uint64_t *good_writes = zio->io_private;
122217f17c2dSbonwick 
122317f17c2dSbonwick 	if (*good_writes == 0)
1224be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(EIO);
122517f17c2dSbonwick 
122617f17c2dSbonwick 	kmem_free(good_writes, sizeof (uint64_t));
122717f17c2dSbonwick }
122817f17c2dSbonwick 
122951ece835Seschrock /*
12300430f8daSeschrock  * We ignore errors for log and cache devices, simply free the private data.
123151ece835Seschrock  */
123251ece835Seschrock static void
12330430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio)
123451ece835Seschrock {
123551ece835Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
123651ece835Seschrock }
123751ece835Seschrock 
123817f17c2dSbonwick /*
123917f17c2dSbonwick  * Write all even or odd labels to all leaves of the specified vdev.
124017f17c2dSbonwick  */
124117f17c2dSbonwick static void
1242a3b55830SMatthew Ahrens vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1243a3b55830SMatthew Ahrens     vdev_t *vd, int l, uint64_t txg, int flags)
1244fa9e4066Sahrens {
1245fa9e4066Sahrens 	nvlist_t *label;
1246fa9e4066Sahrens 	vdev_phys_t *vp;
1247770499e1SDan Kimmel 	abd_t *vp_abd;
1248fa9e4066Sahrens 	char *buf;
1249fa9e4066Sahrens 	size_t buflen;
1250fa9e4066Sahrens 
1251a3b55830SMatthew Ahrens 	for (int c = 0; c < vd->vdev_children; c++) {
1252a3b55830SMatthew Ahrens 		vdev_label_sync(zio, good_writes,
1253a3b55830SMatthew Ahrens 		    vd->vdev_child[c], l, txg, flags);
1254a3b55830SMatthew Ahrens 	}
1255fa9e4066Sahrens 
1256fa9e4066Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1257fa9e4066Sahrens 		return;
1258fa9e4066Sahrens 
1259e14bb325SJeff Bonwick 	if (!vdev_writeable(vd))
1260fa9e4066Sahrens 		return;
1261fa9e4066Sahrens 
1262fa9e4066Sahrens 	/*
1263fa9e4066Sahrens 	 * Generate a label describing the top-level config to which we belong.
1264fa9e4066Sahrens 	 */
12650373e76bSbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1266fa9e4066Sahrens 
1267770499e1SDan Kimmel 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1268770499e1SDan Kimmel 	abd_zero(vp_abd, sizeof (vdev_phys_t));
1269770499e1SDan Kimmel 	vp = abd_to_buf(vp_abd);
1270fa9e4066Sahrens 
1271fa9e4066Sahrens 	buf = vp->vp_nvlist;
1272fa9e4066Sahrens 	buflen = sizeof (vp->vp_nvlist);
1273fa9e4066Sahrens 
127417f17c2dSbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
127517f17c2dSbonwick 		for (; l < VDEV_LABELS; l += 2) {
1276770499e1SDan Kimmel 			vdev_label_write(zio, vd, l, vp_abd,
127717f17c2dSbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
127817f17c2dSbonwick 			    sizeof (vdev_phys_t),
1279a3b55830SMatthew Ahrens 			    vdev_label_sync_done, good_writes,
1280e14bb325SJeff Bonwick 			    flags | ZIO_FLAG_DONT_PROPAGATE);
128117f17c2dSbonwick 		}
128217f17c2dSbonwick 	}
1283fa9e4066Sahrens 
1284770499e1SDan Kimmel 	abd_free(vp_abd);
1285fa9e4066Sahrens 	nvlist_free(label);
1286fa9e4066Sahrens }
1287fa9e4066Sahrens 
128817f17c2dSbonwick int
1289e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1290fa9e4066Sahrens {
1291e14bb325SJeff Bonwick 	list_t *dl = &spa->spa_config_dirty_list;
129217f17c2dSbonwick 	vdev_t *vd;
1293e14bb325SJeff Bonwick 	zio_t *zio;
1294fa9e4066Sahrens 	int error;
1295fa9e4066Sahrens 
1296fa9e4066Sahrens 	/*
1297e14bb325SJeff Bonwick 	 * Write the new labels to disk.
1298fa9e4066Sahrens 	 */
1299e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, flags);
1300fa9e4066Sahrens 
130117f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
130217f17c2dSbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
130317f17c2dSbonwick 		    KM_SLEEP);
130488ecc943SGeorge Wilson 
130588ecc943SGeorge Wilson 		ASSERT(!vd->vdev_ishole);
130688ecc943SGeorge Wilson 
1307a3f829aeSBill Moore 		zio_t *vio = zio_null(zio, spa, NULL,
13080430f8daSeschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
13090430f8daSeschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
131017f17c2dSbonwick 		    good_writes, flags);
1311a3b55830SMatthew Ahrens 		vdev_label_sync(vio, good_writes, vd, l, txg, flags);
131217f17c2dSbonwick 		zio_nowait(vio);
1313fa9e4066Sahrens 	}
1314e14bb325SJeff Bonwick 
1315e14bb325SJeff Bonwick 	error = zio_wait(zio);
1316fa9e4066Sahrens 
13178654d025Sperrin 	/*
131817f17c2dSbonwick 	 * Flush the new labels to disk.
13198654d025Sperrin 	 */
132017f17c2dSbonwick 	zio = zio_root(spa, NULL, NULL, flags);
13218654d025Sperrin 
132217f17c2dSbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
132317f17c2dSbonwick 		zio_flush(zio, vd);
132417f17c2dSbonwick 
132517f17c2dSbonwick 	(void) zio_wait(zio);
1326fa9e4066Sahrens 
1327fa9e4066Sahrens 	return (error);
1328fa9e4066Sahrens }
1329fa9e4066Sahrens 
1330fa9e4066Sahrens /*
133117f17c2dSbonwick  * Sync the uberblock and any changes to the vdev configuration.
1332fa9e4066Sahrens  *
1333fa9e4066Sahrens  * The order of operations is carefully crafted to ensure that
1334fa9e4066Sahrens  * if the system panics or loses power at any time, the state on disk
1335fa9e4066Sahrens  * is still transactionally consistent.  The in-line comments below
1336fa9e4066Sahrens  * describe the failure semantics at each stage.
1337fa9e4066Sahrens  *
133817f17c2dSbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1339fa9e4066Sahrens  * at any time, you can just call it again, and it will resume its work.
1340fa9e4066Sahrens  */
1341e14bb325SJeff Bonwick int
1342eb5bb584SWill Andrews vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1343fa9e4066Sahrens {
134417f17c2dSbonwick 	spa_t *spa = svd[0]->vdev_spa;
1345fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
1346eb5bb584SWill Andrews 	int error = 0;
1347e14bb325SJeff Bonwick 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1348fa9e4066Sahrens 
134986714001SSerapheim Dimitropoulos 	ASSERT(svdcount != 0);
1350eb5bb584SWill Andrews retry:
13518956713aSEric Schrock 	/*
13528956713aSEric Schrock 	 * Normally, we don't want to try too hard to write every label and
13538956713aSEric Schrock 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
13548956713aSEric Schrock 	 * sync process to block while we retry.  But if we can't write a
13558956713aSEric Schrock 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
13568956713aSEric Schrock 	 * bailing out and declaring the pool faulted.
13578956713aSEric Schrock 	 */
1358eb5bb584SWill Andrews 	if (error != 0) {
1359eb5bb584SWill Andrews 		if ((flags & ZIO_FLAG_TRYHARD) != 0)
1360eb5bb584SWill Andrews 			return (error);
13618956713aSEric Schrock 		flags |= ZIO_FLAG_TRYHARD;
1362eb5bb584SWill Andrews 	}
13638956713aSEric Schrock 
1364fa9e4066Sahrens 	ASSERT(ub->ub_txg <= txg);
1365fa9e4066Sahrens 
1366fa9e4066Sahrens 	/*
136717f17c2dSbonwick 	 * If this isn't a resync due to I/O errors,
136817f17c2dSbonwick 	 * and nothing changed in this transaction group,
136917f17c2dSbonwick 	 * and the vdev configuration hasn't changed,
13700373e76bSbonwick 	 * then there's nothing to do.
1371fa9e4066Sahrens 	 */
1372*e0f1c0afSOlaf Faaland 	if (ub->ub_txg < txg) {
1373*e0f1c0afSOlaf Faaland 		boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
1374*e0f1c0afSOlaf Faaland 		    txg, spa->spa_mmp.mmp_delay);
1375*e0f1c0afSOlaf Faaland 
1376*e0f1c0afSOlaf Faaland 		if (!changed && list_is_empty(&spa->spa_config_dirty_list))
1377*e0f1c0afSOlaf Faaland 			return (0);
1378*e0f1c0afSOlaf Faaland 	}
1379fa9e4066Sahrens 
1380fa9e4066Sahrens 	if (txg > spa_freeze_txg(spa))
1381e14bb325SJeff Bonwick 		return (0);
1382fa9e4066Sahrens 
13830373e76bSbonwick 	ASSERT(txg <= spa->spa_final_txg);
13840373e76bSbonwick 
1385fa9e4066Sahrens 	/*
1386fa9e4066Sahrens 	 * Flush the write cache of every disk that's been written to
1387fa9e4066Sahrens 	 * in this transaction group.  This ensures that all blocks
1388fa9e4066Sahrens 	 * written in this txg will be committed to stable storage
1389fa9e4066Sahrens 	 * before any uberblock that references them.
1390fa9e4066Sahrens 	 */
139186714001SSerapheim Dimitropoulos 	zio_t *zio = zio_root(spa, NULL, NULL, flags);
139217f17c2dSbonwick 
139386714001SSerapheim Dimitropoulos 	for (vdev_t *vd =
139486714001SSerapheim Dimitropoulos 	    txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
139517f17c2dSbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
139617f17c2dSbonwick 		zio_flush(zio, vd);
139717f17c2dSbonwick 
1398fa9e4066Sahrens 	(void) zio_wait(zio);
1399fa9e4066Sahrens 
1400fa9e4066Sahrens 	/*
1401fa9e4066Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1402fa9e4066Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1403fa9e4066Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1404fa9e4066Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
140517f17c2dSbonwick 	 * which have not yet been touched, will still be valid.  We flush
140617f17c2dSbonwick 	 * the new labels to disk to ensure that all even-label updates
140717f17c2dSbonwick 	 * are committed to stable storage before the uberblock update.
1408fa9e4066Sahrens 	 */
140986714001SSerapheim Dimitropoulos 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
141086714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
141186714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
141286714001SSerapheim Dimitropoulos 			    "for pool '%s' when syncing out the even labels "
141386714001SSerapheim Dimitropoulos 			    "of dirty vdevs", error, spa_name(spa));
141486714001SSerapheim Dimitropoulos 		}
1415eb5bb584SWill Andrews 		goto retry;
141686714001SSerapheim Dimitropoulos 	}
1417fa9e4066Sahrens 
1418fa9e4066Sahrens 	/*
1419e14bb325SJeff Bonwick 	 * Sync the uberblocks to all vdevs in svd[].
14200373e76bSbonwick 	 * If the system dies in the middle of this step, there are two cases
14210373e76bSbonwick 	 * to consider, and the on-disk state is consistent either way:
1422fa9e4066Sahrens 	 *
1423fa9e4066Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1424fa9e4066Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1425fa9e4066Sahrens 	 *	(which had not yet been touched) will be valid with respect
1426fa9e4066Sahrens 	 *	to that uberblock.
1427fa9e4066Sahrens 	 *
1428fa9e4066Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1429fa9e4066Sahrens 	 *	will be the newest, and the even labels (which had all
1430fa9e4066Sahrens 	 *	been successfully committed) will be valid with respect
1431fa9e4066Sahrens 	 *	to the new uberblocks.
1432fa9e4066Sahrens 	 */
143386714001SSerapheim Dimitropoulos 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
143486714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
143586714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
143686714001SSerapheim Dimitropoulos 			    "%d for pool '%s'", error, spa_name(spa));
143786714001SSerapheim Dimitropoulos 		}
1438eb5bb584SWill Andrews 		goto retry;
143986714001SSerapheim Dimitropoulos 	}
1440fa9e4066Sahrens 
1441*e0f1c0afSOlaf Faaland 	if (spa_multihost(spa))
1442*e0f1c0afSOlaf Faaland 		mmp_update_uberblock(spa, ub);
1443*e0f1c0afSOlaf Faaland 
1444fa9e4066Sahrens 	/*
1445fa9e4066Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1446fa9e4066Sahrens 	 * in the middle of this process, the even labels and the new
1447fa9e4066Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1448fa9e4066Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1449fa9e4066Sahrens 	 * user data is modified -- is mark every vdev dirty so that
145017f17c2dSbonwick 	 * all labels will be brought up to date.  We flush the new labels
145117f17c2dSbonwick 	 * to disk to ensure that all odd-label updates are committed to
145217f17c2dSbonwick 	 * stable storage before the next transaction group begins.
1453fa9e4066Sahrens 	 */
145486714001SSerapheim Dimitropoulos 	if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
145586714001SSerapheim Dimitropoulos 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
145686714001SSerapheim Dimitropoulos 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
145786714001SSerapheim Dimitropoulos 			    "for pool '%s' when syncing out the odd labels of "
145886714001SSerapheim Dimitropoulos 			    "dirty vdevs", error, spa_name(spa));
145986714001SSerapheim Dimitropoulos 		}
1460eb5bb584SWill Andrews 		goto retry;
146186714001SSerapheim Dimitropoulos 	}
1462eb5bb584SWill Andrews 
1463eb5bb584SWill Andrews 	return (0);
1464fa9e4066Sahrens }
1465