xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 13049:bda0decf867b)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens /*
26789Sahrens  * Virtual Device Labels
27789Sahrens  * ---------------------
28789Sahrens  *
29789Sahrens  * The vdev label serves several distinct purposes:
30789Sahrens  *
31789Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
32789Sahrens  *	   identity within the pool.
33789Sahrens  *
34789Sahrens  * 	2. Verify that all the devices given in a configuration are present
35789Sahrens  *         within the pool.
36789Sahrens  *
37789Sahrens  * 	3. Determine the uberblock for the pool.
38789Sahrens  *
39789Sahrens  * 	4. In case of an import operation, determine the configuration of the
40789Sahrens  *         toplevel vdev of which it is a part.
41789Sahrens  *
42789Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
43789Sahrens  *         provide enough information to the administrator to determine which
44789Sahrens  *         devices are missing.
45789Sahrens  *
46789Sahrens  * It is important to note that while the kernel is responsible for writing the
47789Sahrens  * label, it only consumes the information in the first three cases.  The
48789Sahrens  * latter information is only consumed in userland when determining the
49789Sahrens  * configuration to import a pool.
50789Sahrens  *
51789Sahrens  *
52789Sahrens  * Label Organization
53789Sahrens  * ------------------
54789Sahrens  *
55789Sahrens  * Before describing the contents of the label, it's important to understand how
56789Sahrens  * the labels are written and updated with respect to the uberblock.
57789Sahrens  *
58789Sahrens  * When the pool configuration is altered, either because it was newly created
59789Sahrens  * or a device was added, we want to update all the labels such that we can deal
60789Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
61789Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
624451Seschrock  * labels and an uberblock with the following transaction groups:
63789Sahrens  *
64789Sahrens  *              L1          UB          L2
65789Sahrens  *           +------+    +------+    +------+
66789Sahrens  *           |      |    |      |    |      |
67789Sahrens  *           | t10  |    | t10  |    | t10  |
68789Sahrens  *           |      |    |      |    |      |
69789Sahrens  *           +------+    +------+    +------+
70789Sahrens  *
71789Sahrens  * In this stable state, the labels and the uberblock were all updated within
72789Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
73789Sahrens  * that we can detect when we fail partway through writing the label.
74789Sahrens  *
75789Sahrens  * In order to identify which labels are valid, the labels are written in the
76789Sahrens  * following manner:
77789Sahrens  *
78789Sahrens  * 	1. For each vdev, update 'L1' to the new label
79789Sahrens  * 	2. Update the uberblock
80789Sahrens  * 	3. For each vdev, update 'L2' to the new label
81789Sahrens  *
82789Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
83789Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
84789Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
85789Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
86789Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
87789Sahrens  * therefore L1 is valid.
88789Sahrens  *
89789Sahrens  * Another added complexity is that not every label is updated when the config
90789Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
91789Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
92789Sahrens  * be older than the pool uberblock, because the necessary information is stored
93789Sahrens  * on another vdev.
94789Sahrens  *
95789Sahrens  *
96789Sahrens  * On-disk Format
97789Sahrens  * --------------
98789Sahrens  *
99789Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
100789Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
101789Sahrens  * VTOC disk labels, but is otherwise ignored.
102789Sahrens  *
103789Sahrens  * The first half of the label is a packed nvlist which contains pool wide
104789Sahrens  * properties, per-vdev properties, and configuration information.  It is
105789Sahrens  * described in more detail below.
106789Sahrens  *
107789Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
108789Sahrens  * These uberblocks are updated whenever a transaction group is committed,
109789Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
110789Sahrens  * vdev for the 'best' uberblock.
111789Sahrens  *
112789Sahrens  *
113789Sahrens  * Configuration Information
114789Sahrens  * -------------------------
115789Sahrens  *
116789Sahrens  * The nvlist describing the pool and vdev contains the following elements:
117789Sahrens  *
118789Sahrens  * 	version		ZFS on-disk version
119789Sahrens  * 	name		Pool name
120789Sahrens  * 	state		Pool state
121789Sahrens  * 	txg		Transaction group in which this label was written
122789Sahrens  * 	pool_guid	Unique identifier for this pool
123789Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
124789Sahrens  *
125789Sahrens  * Each leaf device label also contains the following:
126789Sahrens  *
127789Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
128789Sahrens  * 	guid		Unique ID for the leaf vdev
129789Sahrens  *
130789Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
131789Sahrens  */
132789Sahrens 
133789Sahrens #include <sys/zfs_context.h>
134789Sahrens #include <sys/spa.h>
135789Sahrens #include <sys/spa_impl.h>
136789Sahrens #include <sys/dmu.h>
137789Sahrens #include <sys/zap.h>
138789Sahrens #include <sys/vdev.h>
139789Sahrens #include <sys/vdev_impl.h>
140789Sahrens #include <sys/uberblock_impl.h>
141789Sahrens #include <sys/metaslab.h>
142789Sahrens #include <sys/zio.h>
14312296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
144789Sahrens #include <sys/fs/zfs.h>
145789Sahrens 
146789Sahrens /*
147789Sahrens  * Basic routines to read and write from a vdev label.
148789Sahrens  * Used throughout the rest of this file.
149789Sahrens  */
150789Sahrens uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)151789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
152789Sahrens {
1531732Sbonwick 	ASSERT(offset < sizeof (vdev_label_t));
1544577Sahrens 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
1551732Sbonwick 
156789Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
157789Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
158789Sahrens }
159789Sahrens 
1606615Sgw25295 /*
1616615Sgw25295  * Returns back the vdev label associated with the passed in offset.
1626615Sgw25295  */
1636615Sgw25295 int
vdev_label_number(uint64_t psize,uint64_t offset)1646615Sgw25295 vdev_label_number(uint64_t psize, uint64_t offset)
1656615Sgw25295 {
1666615Sgw25295 	int l;
1676615Sgw25295 
1686615Sgw25295 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
1696615Sgw25295 		offset -= psize - VDEV_LABEL_END_SIZE;
1706615Sgw25295 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
1716615Sgw25295 	}
1726615Sgw25295 	l = offset / sizeof (vdev_label_t);
1736615Sgw25295 	return (l < VDEV_LABELS ? l : -1);
1746615Sgw25295 }
1756615Sgw25295 
176789Sahrens static void
vdev_label_read(zio_t * zio,vdev_t * vd,int l,void * buf,uint64_t offset,uint64_t size,zio_done_func_t * done,void * private,int flags)177789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
1787754SJeff.Bonwick@Sun.COM 	uint64_t size, zio_done_func_t *done, void *private, int flags)
179789Sahrens {
1807754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
1817754SJeff.Bonwick@Sun.COM 	    SCL_STATE_ALL);
1827754SJeff.Bonwick@Sun.COM 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
183789Sahrens 
184789Sahrens 	zio_nowait(zio_read_phys(zio, vd,
185789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
186789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
1877754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
188789Sahrens }
189789Sahrens 
190789Sahrens static void
vdev_label_write(zio_t * zio,vdev_t * vd,int l,void * buf,uint64_t offset,uint64_t size,zio_done_func_t * done,void * private,int flags)191789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
1925688Sbonwick 	uint64_t size, zio_done_func_t *done, void *private, int flags)
193789Sahrens {
1947754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
1957754SJeff.Bonwick@Sun.COM 	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
1967754SJeff.Bonwick@Sun.COM 	    (SCL_CONFIG | SCL_STATE) &&
1977754SJeff.Bonwick@Sun.COM 	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
1987754SJeff.Bonwick@Sun.COM 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
199789Sahrens 
200789Sahrens 	zio_nowait(zio_write_phys(zio, vd,
201789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
202789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
2035688Sbonwick 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
204789Sahrens }
205789Sahrens 
206789Sahrens /*
207789Sahrens  * Generate the nvlist representing this vdev's config.
208789Sahrens  */
209789Sahrens nvlist_t *
vdev_config_generate(spa_t * spa,vdev_t * vd,boolean_t getstats,vdev_config_flag_t flags)2102082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
21112296SLin.Ling@Sun.COM     vdev_config_flag_t flags)
212789Sahrens {
213789Sahrens 	nvlist_t *nv = NULL;
214789Sahrens 
2151544Seschrock 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
216789Sahrens 
217789Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
218789Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
21912296SLin.Ling@Sun.COM 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
2202082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
2212082Seschrock 		    == 0);
222789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
223789Sahrens 
224789Sahrens 	if (vd->vdev_path != NULL)
225789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
226789Sahrens 		    vd->vdev_path) == 0);
227789Sahrens 
228789Sahrens 	if (vd->vdev_devid != NULL)
229789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
230789Sahrens 		    vd->vdev_devid) == 0);
231789Sahrens 
2324451Seschrock 	if (vd->vdev_physpath != NULL)
2334451Seschrock 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
2344451Seschrock 		    vd->vdev_physpath) == 0);
2354451Seschrock 
2369425SEric.Schrock@Sun.COM 	if (vd->vdev_fru != NULL)
2379425SEric.Schrock@Sun.COM 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU,
2389425SEric.Schrock@Sun.COM 		    vd->vdev_fru) == 0);
2399425SEric.Schrock@Sun.COM 
2402082Seschrock 	if (vd->vdev_nparity != 0) {
2412082Seschrock 		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
2422082Seschrock 		    VDEV_TYPE_RAIDZ) == 0);
2432082Seschrock 
2442082Seschrock 		/*
2452082Seschrock 		 * Make sure someone hasn't managed to sneak a fancy new vdev
2462082Seschrock 		 * into a crufty old storage pool.
2472082Seschrock 		 */
2482082Seschrock 		ASSERT(vd->vdev_nparity == 1 ||
24910105Sadam.leventhal@sun.com 		    (vd->vdev_nparity <= 2 &&
25010105Sadam.leventhal@sun.com 		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
25110105Sadam.leventhal@sun.com 		    (vd->vdev_nparity <= 3 &&
25210105Sadam.leventhal@sun.com 		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
2532082Seschrock 
2542082Seschrock 		/*
2552082Seschrock 		 * Note that we'll add the nparity tag even on storage pools
2562082Seschrock 		 * that only support a single parity device -- older software
2572082Seschrock 		 * will just ignore it.
2582082Seschrock 		 */
2592082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
2602082Seschrock 		    vd->vdev_nparity) == 0);
2612082Seschrock 	}
2622082Seschrock 
2631171Seschrock 	if (vd->vdev_wholedisk != -1ULL)
2641171Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2651171Seschrock 		    vd->vdev_wholedisk) == 0);
2661171Seschrock 
2671544Seschrock 	if (vd->vdev_not_present)
2681544Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
2691544Seschrock 
2702082Seschrock 	if (vd->vdev_isspare)
2712082Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
2722082Seschrock 
27312296SLin.Ling@Sun.COM 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
27412296SLin.Ling@Sun.COM 	    vd == vd->vdev_top) {
275789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
276789Sahrens 		    vd->vdev_ms_array) == 0);
277789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
278789Sahrens 		    vd->vdev_ms_shift) == 0);
279789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
280789Sahrens 		    vd->vdev_ashift) == 0);
281789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
282789Sahrens 		    vd->vdev_asize) == 0);
2834527Sperrin 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2844527Sperrin 		    vd->vdev_islog) == 0);
28512296SLin.Ling@Sun.COM 		if (vd->vdev_removing)
28612296SLin.Ling@Sun.COM 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
28712296SLin.Ling@Sun.COM 			    vd->vdev_removing) == 0);
288789Sahrens 	}
289789Sahrens 
2908241SJeff.Bonwick@Sun.COM 	if (vd->vdev_dtl_smo.smo_object != 0)
291789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
2928241SJeff.Bonwick@Sun.COM 		    vd->vdev_dtl_smo.smo_object) == 0);
293789Sahrens 
29410594SGeorge.Wilson@Sun.COM 	if (vd->vdev_crtxg)
29510594SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
29610594SGeorge.Wilson@Sun.COM 		    vd->vdev_crtxg) == 0);
29710594SGeorge.Wilson@Sun.COM 
298789Sahrens 	if (getstats) {
299789Sahrens 		vdev_stat_t vs;
30012296SLin.Ling@Sun.COM 		pool_scan_stat_t ps;
30112296SLin.Ling@Sun.COM 
302789Sahrens 		vdev_get_stats(vd, &vs);
30312296SLin.Ling@Sun.COM 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
304789Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
30512296SLin.Ling@Sun.COM 
30612296SLin.Ling@Sun.COM 		/* provide either current or previous scan information */
30712296SLin.Ling@Sun.COM 		if (spa_scan_get_stats(spa, &ps) == 0) {
30812296SLin.Ling@Sun.COM 			VERIFY(nvlist_add_uint64_array(nv,
30912296SLin.Ling@Sun.COM 			    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
31012296SLin.Ling@Sun.COM 			    sizeof (pool_scan_stat_t) / sizeof (uint64_t))
31112296SLin.Ling@Sun.COM 			    == 0);
31212296SLin.Ling@Sun.COM 		}
313789Sahrens 	}
314789Sahrens 
315789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
316789Sahrens 		nvlist_t **child;
31712296SLin.Ling@Sun.COM 		int c, idx;
318789Sahrens 
31910594SGeorge.Wilson@Sun.COM 		ASSERT(!vd->vdev_ishole);
32010594SGeorge.Wilson@Sun.COM 
321789Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
322789Sahrens 		    KM_SLEEP);
323789Sahrens 
32412296SLin.Ling@Sun.COM 		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
32512296SLin.Ling@Sun.COM 			vdev_t *cvd = vd->vdev_child[c];
326789Sahrens 
32712296SLin.Ling@Sun.COM 			/*
32812296SLin.Ling@Sun.COM 			 * If we're generating an nvlist of removing
32912296SLin.Ling@Sun.COM 			 * vdevs then skip over any device which is
33012296SLin.Ling@Sun.COM 			 * not being removed.
33112296SLin.Ling@Sun.COM 			 */
33212296SLin.Ling@Sun.COM 			if ((flags & VDEV_CONFIG_REMOVING) &&
33312296SLin.Ling@Sun.COM 			    !cvd->vdev_removing)
33412296SLin.Ling@Sun.COM 				continue;
335789Sahrens 
33612296SLin.Ling@Sun.COM 			child[idx++] = vdev_config_generate(spa, cvd,
33712296SLin.Ling@Sun.COM 			    getstats, flags);
33812296SLin.Ling@Sun.COM 		}
33912296SLin.Ling@Sun.COM 
34012296SLin.Ling@Sun.COM 		if (idx) {
34112296SLin.Ling@Sun.COM 			VERIFY(nvlist_add_nvlist_array(nv,
34212296SLin.Ling@Sun.COM 			    ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
34312296SLin.Ling@Sun.COM 		}
34412296SLin.Ling@Sun.COM 
34512296SLin.Ling@Sun.COM 		for (c = 0; c < idx; c++)
346789Sahrens 			nvlist_free(child[c]);
347789Sahrens 
348789Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
3491485Slling 
3501485Slling 	} else {
35110817SEric.Schrock@Sun.COM 		const char *aux = NULL;
35210817SEric.Schrock@Sun.COM 
3531732Sbonwick 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
3541485Slling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
3551732Sbonwick 			    B_TRUE) == 0);
35613037SMark.Musante@Sun.COM 		if (vd->vdev_resilvering)
35713037SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVERING,
35813037SMark.Musante@Sun.COM 			    B_TRUE) == 0);
3594451Seschrock 		if (vd->vdev_faulted)
3604451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
3614451Seschrock 			    B_TRUE) == 0);
3624451Seschrock 		if (vd->vdev_degraded)
3634451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
3644451Seschrock 			    B_TRUE) == 0);
3654451Seschrock 		if (vd->vdev_removed)
3664451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
3674451Seschrock 			    B_TRUE) == 0);
3684451Seschrock 		if (vd->vdev_unspare)
3694451Seschrock 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
3704451Seschrock 			    B_TRUE) == 0);
37110594SGeorge.Wilson@Sun.COM 		if (vd->vdev_ishole)
37210594SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE,
37310594SGeorge.Wilson@Sun.COM 			    B_TRUE) == 0);
37410817SEric.Schrock@Sun.COM 
37510817SEric.Schrock@Sun.COM 		switch (vd->vdev_stat.vs_aux) {
37610817SEric.Schrock@Sun.COM 		case VDEV_AUX_ERR_EXCEEDED:
37710817SEric.Schrock@Sun.COM 			aux = "err_exceeded";
37810817SEric.Schrock@Sun.COM 			break;
37910817SEric.Schrock@Sun.COM 
38010817SEric.Schrock@Sun.COM 		case VDEV_AUX_EXTERNAL:
38110817SEric.Schrock@Sun.COM 			aux = "external";
38210817SEric.Schrock@Sun.COM 			break;
38310817SEric.Schrock@Sun.COM 		}
38410817SEric.Schrock@Sun.COM 
38510817SEric.Schrock@Sun.COM 		if (aux != NULL)
38610817SEric.Schrock@Sun.COM 			VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE,
38710817SEric.Schrock@Sun.COM 			    aux) == 0);
38811422SMark.Musante@Sun.COM 
38911422SMark.Musante@Sun.COM 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
39011422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
39111422SMark.Musante@Sun.COM 			    vd->vdev_orig_guid) == 0);
39211422SMark.Musante@Sun.COM 		}
393789Sahrens 	}
394789Sahrens 
395789Sahrens 	return (nv);
396789Sahrens }
397789Sahrens 
39810594SGeorge.Wilson@Sun.COM /*
39910594SGeorge.Wilson@Sun.COM  * Generate a view of the top-level vdevs.  If we currently have holes
40010594SGeorge.Wilson@Sun.COM  * in the namespace, then generate an array which contains a list of holey
40110594SGeorge.Wilson@Sun.COM  * vdevs.  Additionally, add the number of top-level children that currently
40210594SGeorge.Wilson@Sun.COM  * exist.
40310594SGeorge.Wilson@Sun.COM  */
40410594SGeorge.Wilson@Sun.COM void
vdev_top_config_generate(spa_t * spa,nvlist_t * config)40510594SGeorge.Wilson@Sun.COM vdev_top_config_generate(spa_t *spa, nvlist_t *config)
40610594SGeorge.Wilson@Sun.COM {
40710594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
40810594SGeorge.Wilson@Sun.COM 	uint64_t *array;
40912296SLin.Ling@Sun.COM 	uint_t c, idx;
41010594SGeorge.Wilson@Sun.COM 
41110594SGeorge.Wilson@Sun.COM 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
41210594SGeorge.Wilson@Sun.COM 
41312296SLin.Ling@Sun.COM 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
41410594SGeorge.Wilson@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
41510594SGeorge.Wilson@Sun.COM 
41610594SGeorge.Wilson@Sun.COM 		if (tvd->vdev_ishole)
41710594SGeorge.Wilson@Sun.COM 			array[idx++] = c;
41810594SGeorge.Wilson@Sun.COM 	}
41910594SGeorge.Wilson@Sun.COM 
42010666SGeorge.Wilson@Sun.COM 	if (idx) {
42110666SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
42210666SGeorge.Wilson@Sun.COM 		    array, idx) == 0);
42310666SGeorge.Wilson@Sun.COM 	}
42410666SGeorge.Wilson@Sun.COM 
42510594SGeorge.Wilson@Sun.COM 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
42610594SGeorge.Wilson@Sun.COM 	    rvd->vdev_children) == 0);
42710594SGeorge.Wilson@Sun.COM 
42810594SGeorge.Wilson@Sun.COM 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
42910594SGeorge.Wilson@Sun.COM }
43010594SGeorge.Wilson@Sun.COM 
431789Sahrens nvlist_t *
vdev_label_read_config(vdev_t * vd)432789Sahrens vdev_label_read_config(vdev_t *vd)
433789Sahrens {
4341635Sbonwick 	spa_t *spa = vd->vdev_spa;
435789Sahrens 	nvlist_t *config = NULL;
436789Sahrens 	vdev_phys_t *vp;
437789Sahrens 	zio_t *zio;
4389725SEric.Schrock@Sun.COM 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
4399725SEric.Schrock@Sun.COM 	    ZIO_FLAG_SPECULATIVE;
440789Sahrens 
4417754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4421635Sbonwick 
4435329Sgw25295 	if (!vdev_readable(vd))
444789Sahrens 		return (NULL);
445789Sahrens 
446789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
447789Sahrens 
4489725SEric.Schrock@Sun.COM retry:
4497754SJeff.Bonwick@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++) {
450789Sahrens 
4517754SJeff.Bonwick@Sun.COM 		zio = zio_root(spa, NULL, NULL, flags);
452789Sahrens 
453789Sahrens 		vdev_label_read(zio, vd, l, vp,
454789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
4557754SJeff.Bonwick@Sun.COM 		    sizeof (vdev_phys_t), NULL, NULL, flags);
456789Sahrens 
457789Sahrens 		if (zio_wait(zio) == 0 &&
458789Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
4591544Seschrock 		    &config, 0) == 0)
460789Sahrens 			break;
461789Sahrens 
462789Sahrens 		if (config != NULL) {
463789Sahrens 			nvlist_free(config);
464789Sahrens 			config = NULL;
465789Sahrens 		}
466789Sahrens 	}
467789Sahrens 
4689725SEric.Schrock@Sun.COM 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
4699725SEric.Schrock@Sun.COM 		flags |= ZIO_FLAG_TRYHARD;
4709725SEric.Schrock@Sun.COM 		goto retry;
4719725SEric.Schrock@Sun.COM 	}
4729725SEric.Schrock@Sun.COM 
473789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
474789Sahrens 
475789Sahrens 	return (config);
476789Sahrens }
477789Sahrens 
4783377Seschrock /*
4793377Seschrock  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
4803377Seschrock  * in with the device guid if this spare is active elsewhere on the system.
4813377Seschrock  */
4823377Seschrock static boolean_t
vdev_inuse(vdev_t * vd,uint64_t crtxg,vdev_labeltype_t reason,uint64_t * spare_guid,uint64_t * l2cache_guid)4833377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
4845450Sbrendan     uint64_t *spare_guid, uint64_t *l2cache_guid)
4853377Seschrock {
4863377Seschrock 	spa_t *spa = vd->vdev_spa;
4873377Seschrock 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
4883377Seschrock 	uint64_t vdtxg = 0;
4893377Seschrock 	nvlist_t *label;
4903377Seschrock 
4913377Seschrock 	if (spare_guid)
4923377Seschrock 		*spare_guid = 0ULL;
4935450Sbrendan 	if (l2cache_guid)
4945450Sbrendan 		*l2cache_guid = 0ULL;
4953377Seschrock 
4963377Seschrock 	/*
4973377Seschrock 	 * Read the label, if any, and perform some basic sanity checks.
4983377Seschrock 	 */
4993377Seschrock 	if ((label = vdev_label_read_config(vd)) == NULL)
5003377Seschrock 		return (B_FALSE);
5013377Seschrock 
5023377Seschrock 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
5033377Seschrock 	    &vdtxg);
5043377Seschrock 
5053377Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
5063377Seschrock 	    &state) != 0 ||
5073377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
5083377Seschrock 	    &device_guid) != 0) {
5093377Seschrock 		nvlist_free(label);
5103377Seschrock 		return (B_FALSE);
5113377Seschrock 	}
5123377Seschrock 
5135450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
5143377Seschrock 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
5153377Seschrock 	    &pool_guid) != 0 ||
5163377Seschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
5173377Seschrock 	    &txg) != 0)) {
5183377Seschrock 		nvlist_free(label);
5193377Seschrock 		return (B_FALSE);
5203377Seschrock 	}
5213377Seschrock 
5223377Seschrock 	nvlist_free(label);
5233377Seschrock 
5243377Seschrock 	/*
5253377Seschrock 	 * Check to see if this device indeed belongs to the pool it claims to
5263377Seschrock 	 * be a part of.  The only way this is allowed is if the device is a hot
5273377Seschrock 	 * spare (which we check for later on).
5283377Seschrock 	 */
5295450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
5303377Seschrock 	    !spa_guid_exists(pool_guid, device_guid) &&
5317214Slling 	    !spa_spare_exists(device_guid, NULL, NULL) &&
5325450Sbrendan 	    !spa_l2cache_exists(device_guid, NULL))
5333377Seschrock 		return (B_FALSE);
5343377Seschrock 
5353377Seschrock 	/*
5363377Seschrock 	 * If the transaction group is zero, then this an initialized (but
5373377Seschrock 	 * unused) label.  This is only an error if the create transaction
5383377Seschrock 	 * on-disk is the same as the one we're using now, in which case the
5393377Seschrock 	 * user has attempted to add the same vdev multiple times in the same
5403377Seschrock 	 * transaction.
5413377Seschrock 	 */
5425450Sbrendan 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
5435450Sbrendan 	    txg == 0 && vdtxg == crtxg)
5443377Seschrock 		return (B_TRUE);
5453377Seschrock 
5463377Seschrock 	/*
5473377Seschrock 	 * Check to see if this is a spare device.  We do an explicit check for
5483377Seschrock 	 * spa_has_spare() here because it may be on our pending list of spares
5495450Sbrendan 	 * to add.  We also check if it is an l2cache device.
5503377Seschrock 	 */
5517214Slling 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
5523377Seschrock 	    spa_has_spare(spa, device_guid)) {
5533377Seschrock 		if (spare_guid)
5543377Seschrock 			*spare_guid = device_guid;
5553377Seschrock 
5563377Seschrock 		switch (reason) {
5573377Seschrock 		case VDEV_LABEL_CREATE:
5585450Sbrendan 		case VDEV_LABEL_L2CACHE:
5593377Seschrock 			return (B_TRUE);
5603377Seschrock 
5613377Seschrock 		case VDEV_LABEL_REPLACE:
5623377Seschrock 			return (!spa_has_spare(spa, device_guid) ||
5633377Seschrock 			    spare_pool != 0ULL);
5643377Seschrock 
5653377Seschrock 		case VDEV_LABEL_SPARE:
5663377Seschrock 			return (spa_has_spare(spa, device_guid));
5673377Seschrock 		}
5683377Seschrock 	}
5693377Seschrock 
5703377Seschrock 	/*
5715450Sbrendan 	 * Check to see if this is an l2cache device.
5725450Sbrendan 	 */
5735450Sbrendan 	if (spa_l2cache_exists(device_guid, NULL))
5745450Sbrendan 		return (B_TRUE);
5755450Sbrendan 
5765450Sbrendan 	/*
577*13049SGeorge.Wilson@Sun.COM 	 * We can't rely on a pool's state if it's been imported
578*13049SGeorge.Wilson@Sun.COM 	 * read-only.  Instead we look to see if the pools is marked
579*13049SGeorge.Wilson@Sun.COM 	 * read-only in the namespace and set the state to active.
580*13049SGeorge.Wilson@Sun.COM 	 */
581*13049SGeorge.Wilson@Sun.COM 	if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
582*13049SGeorge.Wilson@Sun.COM 	    spa_mode(spa) == FREAD)
583*13049SGeorge.Wilson@Sun.COM 		state = POOL_STATE_ACTIVE;
584*13049SGeorge.Wilson@Sun.COM 
585*13049SGeorge.Wilson@Sun.COM 	/*
5863377Seschrock 	 * If the device is marked ACTIVE, then this device is in use by another
5873377Seschrock 	 * pool on the system.
5883377Seschrock 	 */
5893377Seschrock 	return (state == POOL_STATE_ACTIVE);
5903377Seschrock }
5913377Seschrock 
5923377Seschrock /*
5933377Seschrock  * Initialize a vdev label.  We check to make sure each leaf device is not in
5943377Seschrock  * use, and writable.  We put down an initial label which we will later
5953377Seschrock  * overwrite with a complete label.  Note that it's important to do this
5963377Seschrock  * sequentially, not in parallel, so that we catch cases of multiple use of the
5973377Seschrock  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
5983377Seschrock  * itself.
5993377Seschrock  */
6003377Seschrock int
vdev_label_init(vdev_t * vd,uint64_t crtxg,vdev_labeltype_t reason)6013377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
602789Sahrens {
603789Sahrens 	spa_t *spa = vd->vdev_spa;
604789Sahrens 	nvlist_t *label;
605789Sahrens 	vdev_phys_t *vp;
6069056SLin.Ling@Sun.COM 	char *pad2;
6071732Sbonwick 	uberblock_t *ub;
608789Sahrens 	zio_t *zio;
609789Sahrens 	char *buf;
610789Sahrens 	size_t buflen;
611789Sahrens 	int error;
6125450Sbrendan 	uint64_t spare_guid, l2cache_guid;
6137754SJeff.Bonwick@Sun.COM 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
614789Sahrens 
6157754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6161635Sbonwick 
6177754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
6183377Seschrock 		if ((error = vdev_label_init(vd->vdev_child[c],
6193377Seschrock 		    crtxg, reason)) != 0)
620789Sahrens 			return (error);
621789Sahrens 
62210594SGeorge.Wilson@Sun.COM 	/* Track the creation time for this vdev */
62310594SGeorge.Wilson@Sun.COM 	vd->vdev_crtxg = crtxg;
62410594SGeorge.Wilson@Sun.COM 
625789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
626789Sahrens 		return (0);
627789Sahrens 
628789Sahrens 	/*
6293377Seschrock 	 * Dead vdevs cannot be initialized.
630789Sahrens 	 */
631789Sahrens 	if (vdev_is_dead(vd))
632789Sahrens 		return (EIO);
633789Sahrens 
634789Sahrens 	/*
6353377Seschrock 	 * Determine if the vdev is in use.
636789Sahrens 	 */
63711422SMark.Musante@Sun.COM 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
6385450Sbrendan 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
6393377Seschrock 		return (EBUSY);
640789Sahrens 
6413377Seschrock 	/*
6425450Sbrendan 	 * If this is a request to add or replace a spare or l2cache device
6435450Sbrendan 	 * that is in use elsewhere on the system, then we must update the
6445450Sbrendan 	 * guid (which was initialized to a random value) to reflect the
6455450Sbrendan 	 * actual GUID (which is shared between multiple pools).
6463377Seschrock 	 */
6475450Sbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
6485450Sbrendan 	    spare_guid != 0ULL) {
6497768SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
6502082Seschrock 
6517768SJeff.Bonwick@Sun.COM 		vd->vdev_guid += guid_delta;
6527768SJeff.Bonwick@Sun.COM 
6537768SJeff.Bonwick@Sun.COM 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
6547768SJeff.Bonwick@Sun.COM 			pvd->vdev_guid_sum += guid_delta;
6552082Seschrock 
6563377Seschrock 		/*
6573377Seschrock 		 * If this is a replacement, then we want to fallthrough to the
6583377Seschrock 		 * rest of the code.  If we're adding a spare, then it's already
6594451Seschrock 		 * labeled appropriately and we can just return.
6603377Seschrock 		 */
6613377Seschrock 		if (reason == VDEV_LABEL_SPARE)
6623377Seschrock 			return (0);
66311422SMark.Musante@Sun.COM 		ASSERT(reason == VDEV_LABEL_REPLACE ||
66411422SMark.Musante@Sun.COM 		    reason == VDEV_LABEL_SPLIT);
665789Sahrens 	}
666789Sahrens 
6675450Sbrendan 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
6685450Sbrendan 	    l2cache_guid != 0ULL) {
6697768SJeff.Bonwick@Sun.COM 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
6705450Sbrendan 
6717768SJeff.Bonwick@Sun.COM 		vd->vdev_guid += guid_delta;
6727768SJeff.Bonwick@Sun.COM 
6737768SJeff.Bonwick@Sun.COM 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
6747768SJeff.Bonwick@Sun.COM 			pvd->vdev_guid_sum += guid_delta;
6755450Sbrendan 
6765450Sbrendan 		/*
6775450Sbrendan 		 * If this is a replacement, then we want to fallthrough to the
6785450Sbrendan 		 * rest of the code.  If we're adding an l2cache, then it's
6795450Sbrendan 		 * already labeled appropriately and we can just return.
6805450Sbrendan 		 */
6815450Sbrendan 		if (reason == VDEV_LABEL_L2CACHE)
6825450Sbrendan 			return (0);
6835450Sbrendan 		ASSERT(reason == VDEV_LABEL_REPLACE);
6845450Sbrendan 	}
6855450Sbrendan 
686789Sahrens 	/*
6873377Seschrock 	 * Initialize its label.
688789Sahrens 	 */
689789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
690789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
691789Sahrens 
692789Sahrens 	/*
693789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
694789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
695789Sahrens 	 * really part of an active pool just yet.  The labels will
696789Sahrens 	 * be written again with a meaningful txg by spa_sync().
697789Sahrens 	 */
6983377Seschrock 	if (reason == VDEV_LABEL_SPARE ||
6993377Seschrock 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
7003377Seschrock 		/*
7013377Seschrock 		 * For inactive hot spares, we generate a special label that
7023377Seschrock 		 * identifies as a mutually shared hot spare.  We write the
7033377Seschrock 		 * label if we are adding a hot spare, or if we are removing an
7043377Seschrock 		 * active hot spare (in which case we want to revert the
7053377Seschrock 		 * labels).
7063377Seschrock 		 */
7072082Seschrock 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
708789Sahrens 
7092082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
7102082Seschrock 		    spa_version(spa)) == 0);
7112082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
7122082Seschrock 		    POOL_STATE_SPARE) == 0);
7132082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
7142082Seschrock 		    vd->vdev_guid) == 0);
7155450Sbrendan 	} else if (reason == VDEV_LABEL_L2CACHE ||
7165450Sbrendan 	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
7175450Sbrendan 		/*
7185450Sbrendan 		 * For level 2 ARC devices, add a special label.
7195450Sbrendan 		 */
7205450Sbrendan 		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7215450Sbrendan 
7225450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
7235450Sbrendan 		    spa_version(spa)) == 0);
7245450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
7255450Sbrendan 		    POOL_STATE_L2CACHE) == 0);
7265450Sbrendan 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
7275450Sbrendan 		    vd->vdev_guid) == 0);
7282082Seschrock 	} else {
72911422SMark.Musante@Sun.COM 		uint64_t txg = 0ULL;
73011422SMark.Musante@Sun.COM 
73111422SMark.Musante@Sun.COM 		if (reason == VDEV_LABEL_SPLIT)
73211422SMark.Musante@Sun.COM 			txg = spa->spa_uberblock.ub_txg;
73311422SMark.Musante@Sun.COM 		label = spa_config_generate(spa, vd, txg, B_FALSE);
7342082Seschrock 
7352082Seschrock 		/*
7362082Seschrock 		 * Add our creation time.  This allows us to detect multiple
7372082Seschrock 		 * vdev uses as described above, and automatically expires if we
7382082Seschrock 		 * fail.
7392082Seschrock 		 */
7402082Seschrock 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
7412082Seschrock 		    crtxg) == 0);
7422082Seschrock 	}
743789Sahrens 
744789Sahrens 	buf = vp->vp_nvlist;
745789Sahrens 	buflen = sizeof (vp->vp_nvlist);
746789Sahrens 
7473460Smmusante 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
7483460Smmusante 	if (error != 0) {
749789Sahrens 		nvlist_free(label);
750789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
7513460Smmusante 		/* EFAULT means nvlist_pack ran out of room */
7523460Smmusante 		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
753789Sahrens 	}
754789Sahrens 
755789Sahrens 	/*
756789Sahrens 	 * Initialize uberblock template.
757789Sahrens 	 */
7589846SEric.Taylor@Sun.COM 	ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
7599846SEric.Taylor@Sun.COM 	bzero(ub, VDEV_UBERBLOCK_RING);
7601732Sbonwick 	*ub = spa->spa_uberblock;
7611732Sbonwick 	ub->ub_txg = 0;
762789Sahrens 
7639056SLin.Ling@Sun.COM 	/* Initialize the 2nd padding area. */
7649056SLin.Ling@Sun.COM 	pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
7659056SLin.Ling@Sun.COM 	bzero(pad2, VDEV_PAD_SIZE);
7669056SLin.Ling@Sun.COM 
767789Sahrens 	/*
768789Sahrens 	 * Write everything in parallel.
769789Sahrens 	 */
7709725SEric.Schrock@Sun.COM retry:
7715688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
772789Sahrens 
7737754SJeff.Bonwick@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++) {
774789Sahrens 
775789Sahrens 		vdev_label_write(zio, vd, l, vp,
776789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
7775688Sbonwick 		    sizeof (vdev_phys_t), NULL, NULL, flags);
778789Sahrens 
7799056SLin.Ling@Sun.COM 		/*
7809056SLin.Ling@Sun.COM 		 * Skip the 1st padding area.
7819056SLin.Ling@Sun.COM 		 * Zero out the 2nd padding area where it might have
7829056SLin.Ling@Sun.COM 		 * left over data from previous filesystem format.
7839056SLin.Ling@Sun.COM 		 */
7849056SLin.Ling@Sun.COM 		vdev_label_write(zio, vd, l, pad2,
7859056SLin.Ling@Sun.COM 		    offsetof(vdev_label_t, vl_pad2),
7869056SLin.Ling@Sun.COM 		    VDEV_PAD_SIZE, NULL, NULL, flags);
7879056SLin.Ling@Sun.COM 
7889846SEric.Taylor@Sun.COM 		vdev_label_write(zio, vd, l, ub,
7899846SEric.Taylor@Sun.COM 		    offsetof(vdev_label_t, vl_uberblock),
7909846SEric.Taylor@Sun.COM 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
791789Sahrens 	}
792789Sahrens 
793789Sahrens 	error = zio_wait(zio);
794789Sahrens 
7959725SEric.Schrock@Sun.COM 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
7969725SEric.Schrock@Sun.COM 		flags |= ZIO_FLAG_TRYHARD;
7979725SEric.Schrock@Sun.COM 		goto retry;
7989725SEric.Schrock@Sun.COM 	}
7999725SEric.Schrock@Sun.COM 
800789Sahrens 	nvlist_free(label);
8019056SLin.Ling@Sun.COM 	zio_buf_free(pad2, VDEV_PAD_SIZE);
8029846SEric.Taylor@Sun.COM 	zio_buf_free(ub, VDEV_UBERBLOCK_RING);
803789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
804789Sahrens 
8053377Seschrock 	/*
8063377Seschrock 	 * If this vdev hasn't been previously identified as a spare, then we
8074451Seschrock 	 * mark it as such only if a) we are labeling it as a spare, or b) it
8085450Sbrendan 	 * exists as a spare elsewhere in the system.  Do the same for
8095450Sbrendan 	 * level 2 ARC devices.
8103377Seschrock 	 */
8113377Seschrock 	if (error == 0 && !vd->vdev_isspare &&
8123377Seschrock 	    (reason == VDEV_LABEL_SPARE ||
8137214Slling 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
8143377Seschrock 		spa_spare_add(vd);
8152082Seschrock 
8165450Sbrendan 	if (error == 0 && !vd->vdev_isl2cache &&
8175450Sbrendan 	    (reason == VDEV_LABEL_L2CACHE ||
8185450Sbrendan 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
8195450Sbrendan 		spa_l2cache_add(vd);
8205450Sbrendan 
8213377Seschrock 	return (error);
8222082Seschrock }
8232082Seschrock 
824789Sahrens /*
825789Sahrens  * ==========================================================================
826789Sahrens  * uberblock load/sync
827789Sahrens  * ==========================================================================
828789Sahrens  */
829789Sahrens 
830789Sahrens /*
831789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
832789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
833789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
834789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
835789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
836789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
837789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
838789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
839789Sahrens  */
840789Sahrens static int
vdev_uberblock_compare(uberblock_t * ub1,uberblock_t * ub2)841789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
842789Sahrens {
843789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
844789Sahrens 		return (-1);
845789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
846789Sahrens 		return (1);
847789Sahrens 
848789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
849789Sahrens 		return (-1);
850789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
851789Sahrens 		return (1);
852789Sahrens 
853789Sahrens 	return (0);
854789Sahrens }
855789Sahrens 
856789Sahrens static void
vdev_uberblock_load_done(zio_t * zio)857789Sahrens vdev_uberblock_load_done(zio_t *zio)
858789Sahrens {
85910921STim.Haley@Sun.COM 	spa_t *spa = zio->io_spa;
8607754SJeff.Bonwick@Sun.COM 	zio_t *rio = zio->io_private;
8611732Sbonwick 	uberblock_t *ub = zio->io_data;
8627754SJeff.Bonwick@Sun.COM 	uberblock_t *ubbest = rio->io_private;
863789Sahrens 
8641732Sbonwick 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
865789Sahrens 
8661544Seschrock 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
8677754SJeff.Bonwick@Sun.COM 		mutex_enter(&rio->io_lock);
86810921STim.Haley@Sun.COM 		if (ub->ub_txg <= spa->spa_load_max_txg &&
8698188SVictor.Latushkin@Sun.COM 		    vdev_uberblock_compare(ub, ubbest) > 0)
870789Sahrens 			*ubbest = *ub;
8717754SJeff.Bonwick@Sun.COM 		mutex_exit(&rio->io_lock);
872789Sahrens 	}
873789Sahrens 
874789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
875789Sahrens }
876789Sahrens 
877789Sahrens void
vdev_uberblock_load(zio_t * zio,vdev_t * vd,uberblock_t * ubbest)878789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
879789Sahrens {
8807754SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
8817754SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
8829725SEric.Schrock@Sun.COM 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
8839725SEric.Schrock@Sun.COM 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
884789Sahrens 
8857754SJeff.Bonwick@Sun.COM 	if (vd == rvd) {
8867754SJeff.Bonwick@Sun.COM 		ASSERT(zio == NULL);
8877754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
8887754SJeff.Bonwick@Sun.COM 		zio = zio_root(spa, NULL, ubbest, flags);
8897754SJeff.Bonwick@Sun.COM 		bzero(ubbest, sizeof (uberblock_t));
8907754SJeff.Bonwick@Sun.COM 	}
8917754SJeff.Bonwick@Sun.COM 
8927754SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
8937754SJeff.Bonwick@Sun.COM 
8947754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
895789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
896789Sahrens 
8977754SJeff.Bonwick@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
8987754SJeff.Bonwick@Sun.COM 		for (int l = 0; l < VDEV_LABELS; l++) {
8997754SJeff.Bonwick@Sun.COM 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
9007754SJeff.Bonwick@Sun.COM 				vdev_label_read(zio, vd, l,
9017754SJeff.Bonwick@Sun.COM 				    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
9027754SJeff.Bonwick@Sun.COM 				    VDEV_UBERBLOCK_OFFSET(vd, n),
9037754SJeff.Bonwick@Sun.COM 				    VDEV_UBERBLOCK_SIZE(vd),
9047754SJeff.Bonwick@Sun.COM 				    vdev_uberblock_load_done, zio, flags);
9057754SJeff.Bonwick@Sun.COM 			}
9067754SJeff.Bonwick@Sun.COM 		}
9077754SJeff.Bonwick@Sun.COM 	}
908789Sahrens 
9097754SJeff.Bonwick@Sun.COM 	if (vd == rvd) {
9107754SJeff.Bonwick@Sun.COM 		(void) zio_wait(zio);
9117754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_ALL, FTAG);
912789Sahrens 	}
913789Sahrens }
914789Sahrens 
915789Sahrens /*
9165688Sbonwick  * On success, increment root zio's count of good writes.
9171635Sbonwick  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
918789Sahrens  */
919789Sahrens static void
vdev_uberblock_sync_done(zio_t * zio)920789Sahrens vdev_uberblock_sync_done(zio_t *zio)
921789Sahrens {
9225688Sbonwick 	uint64_t *good_writes = zio->io_private;
923789Sahrens 
9241635Sbonwick 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
925789Sahrens 		atomic_add_64(good_writes, 1);
926789Sahrens }
927789Sahrens 
9285688Sbonwick /*
9295688Sbonwick  * Write the uberblock to all labels of all leaves of the specified vdev.
9305688Sbonwick  */
931789Sahrens static void
vdev_uberblock_sync(zio_t * zio,uberblock_t * ub,vdev_t * vd,int flags)9327754SJeff.Bonwick@Sun.COM vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
933789Sahrens {
9345688Sbonwick 	uberblock_t *ubbuf;
9357754SJeff.Bonwick@Sun.COM 	int n;
936789Sahrens 
9377754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
9387754SJeff.Bonwick@Sun.COM 		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
939789Sahrens 
940789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
941789Sahrens 		return;
942789Sahrens 
9437754SJeff.Bonwick@Sun.COM 	if (!vdev_writeable(vd))
944789Sahrens 		return;
945789Sahrens 
9465688Sbonwick 	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
947789Sahrens 
9485688Sbonwick 	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
9495688Sbonwick 	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
9501732Sbonwick 	*ubbuf = *ub;
951789Sahrens 
9527754SJeff.Bonwick@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++)
9535688Sbonwick 		vdev_label_write(zio, vd, l, ubbuf,
9547754SJeff.Bonwick@Sun.COM 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
9555688Sbonwick 		    vdev_uberblock_sync_done, zio->io_private,
9567754SJeff.Bonwick@Sun.COM 		    flags | ZIO_FLAG_DONT_PROPAGATE);
957789Sahrens 
9585688Sbonwick 	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
9595688Sbonwick }
960789Sahrens 
9615688Sbonwick int
vdev_uberblock_sync_list(vdev_t ** svd,int svdcount,uberblock_t * ub,int flags)9625688Sbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
9635688Sbonwick {
9645688Sbonwick 	spa_t *spa = svd[0]->vdev_spa;
9657754SJeff.Bonwick@Sun.COM 	zio_t *zio;
9665688Sbonwick 	uint64_t good_writes = 0;
967789Sahrens 
9687754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, &good_writes, flags);
9697754SJeff.Bonwick@Sun.COM 
9707754SJeff.Bonwick@Sun.COM 	for (int v = 0; v < svdcount; v++)
9717754SJeff.Bonwick@Sun.COM 		vdev_uberblock_sync(zio, ub, svd[v], flags);
9727754SJeff.Bonwick@Sun.COM 
9735688Sbonwick 	(void) zio_wait(zio);
974789Sahrens 
975789Sahrens 	/*
9765688Sbonwick 	 * Flush the uberblocks to disk.  This ensures that the odd labels
9775688Sbonwick 	 * are no longer needed (because the new uberblocks and the even
9785688Sbonwick 	 * labels are safely on disk), so it is safe to overwrite them.
979789Sahrens 	 */
9805688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
981789Sahrens 
9827754SJeff.Bonwick@Sun.COM 	for (int v = 0; v < svdcount; v++)
9835688Sbonwick 		zio_flush(zio, svd[v]);
984789Sahrens 
9855688Sbonwick 	(void) zio_wait(zio);
9865688Sbonwick 
9875688Sbonwick 	return (good_writes >= 1 ? 0 : EIO);
988789Sahrens }
989789Sahrens 
990789Sahrens /*
9915688Sbonwick  * On success, increment the count of good writes for our top-level vdev.
992789Sahrens  */
993789Sahrens static void
vdev_label_sync_done(zio_t * zio)9945688Sbonwick vdev_label_sync_done(zio_t *zio)
995789Sahrens {
9965688Sbonwick 	uint64_t *good_writes = zio->io_private;
997789Sahrens 
998789Sahrens 	if (zio->io_error == 0)
999789Sahrens 		atomic_add_64(good_writes, 1);
1000789Sahrens }
1001789Sahrens 
10025688Sbonwick /*
10035688Sbonwick  * If there weren't enough good writes, indicate failure to the parent.
10045688Sbonwick  */
1005789Sahrens static void
vdev_label_sync_top_done(zio_t * zio)10065688Sbonwick vdev_label_sync_top_done(zio_t *zio)
10075688Sbonwick {
10085688Sbonwick 	uint64_t *good_writes = zio->io_private;
10095688Sbonwick 
10105688Sbonwick 	if (*good_writes == 0)
10115688Sbonwick 		zio->io_error = EIO;
10125688Sbonwick 
10135688Sbonwick 	kmem_free(good_writes, sizeof (uint64_t));
10145688Sbonwick }
10155688Sbonwick 
10165688Sbonwick /*
10177041Seschrock  * We ignore errors for log and cache devices, simply free the private data.
10186976Seschrock  */
10196976Seschrock static void
vdev_label_sync_ignore_done(zio_t * zio)10207041Seschrock vdev_label_sync_ignore_done(zio_t *zio)
10216976Seschrock {
10226976Seschrock 	kmem_free(zio->io_private, sizeof (uint64_t));
10236976Seschrock }
10246976Seschrock 
10256976Seschrock /*
10265688Sbonwick  * Write all even or odd labels to all leaves of the specified vdev.
10275688Sbonwick  */
10285688Sbonwick static void
vdev_label_sync(zio_t * zio,vdev_t * vd,int l,uint64_t txg,int flags)10297754SJeff.Bonwick@Sun.COM vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
1030789Sahrens {
1031789Sahrens 	nvlist_t *label;
1032789Sahrens 	vdev_phys_t *vp;
1033789Sahrens 	char *buf;
1034789Sahrens 	size_t buflen;
1035789Sahrens 
10367754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
10377754SJeff.Bonwick@Sun.COM 		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
1038789Sahrens 
1039789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
1040789Sahrens 		return;
1041789Sahrens 
10427754SJeff.Bonwick@Sun.COM 	if (!vdev_writeable(vd))
1043789Sahrens 		return;
1044789Sahrens 
1045789Sahrens 	/*
1046789Sahrens 	 * Generate a label describing the top-level config to which we belong.
1047789Sahrens 	 */
10481635Sbonwick 	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1049789Sahrens 
1050789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
1051789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
1052789Sahrens 
1053789Sahrens 	buf = vp->vp_nvlist;
1054789Sahrens 	buflen = sizeof (vp->vp_nvlist);
1055789Sahrens 
10565688Sbonwick 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
10575688Sbonwick 		for (; l < VDEV_LABELS; l += 2) {
10585688Sbonwick 			vdev_label_write(zio, vd, l, vp,
10595688Sbonwick 			    offsetof(vdev_label_t, vl_vdev_phys),
10605688Sbonwick 			    sizeof (vdev_phys_t),
10615688Sbonwick 			    vdev_label_sync_done, zio->io_private,
10627754SJeff.Bonwick@Sun.COM 			    flags | ZIO_FLAG_DONT_PROPAGATE);
10635688Sbonwick 		}
10645688Sbonwick 	}
1065789Sahrens 
1066789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
1067789Sahrens 	nvlist_free(label);
1068789Sahrens }
1069789Sahrens 
10705688Sbonwick int
vdev_label_sync_list(spa_t * spa,int l,uint64_t txg,int flags)10717754SJeff.Bonwick@Sun.COM vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1072789Sahrens {
10737754SJeff.Bonwick@Sun.COM 	list_t *dl = &spa->spa_config_dirty_list;
10745688Sbonwick 	vdev_t *vd;
10757754SJeff.Bonwick@Sun.COM 	zio_t *zio;
1076789Sahrens 	int error;
1077789Sahrens 
10785688Sbonwick 	/*
10797754SJeff.Bonwick@Sun.COM 	 * Write the new labels to disk.
10805688Sbonwick 	 */
10817754SJeff.Bonwick@Sun.COM 	zio = zio_root(spa, NULL, NULL, flags);
1082789Sahrens 
10835688Sbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
10845688Sbonwick 		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
10855688Sbonwick 		    KM_SLEEP);
108610594SGeorge.Wilson@Sun.COM 
108710594SGeorge.Wilson@Sun.COM 		ASSERT(!vd->vdev_ishole);
108810594SGeorge.Wilson@Sun.COM 
10898632SBill.Moore@Sun.COM 		zio_t *vio = zio_null(zio, spa, NULL,
10907041Seschrock 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
10917041Seschrock 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
10925688Sbonwick 		    good_writes, flags);
10937754SJeff.Bonwick@Sun.COM 		vdev_label_sync(vio, vd, l, txg, flags);
10945688Sbonwick 		zio_nowait(vio);
10955688Sbonwick 	}
10967754SJeff.Bonwick@Sun.COM 
10977754SJeff.Bonwick@Sun.COM 	error = zio_wait(zio);
1098789Sahrens 
10995688Sbonwick 	/*
11005688Sbonwick 	 * Flush the new labels to disk.
11015688Sbonwick 	 */
11025688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
1103789Sahrens 
11045688Sbonwick 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
11055688Sbonwick 		zio_flush(zio, vd);
11064527Sperrin 
11075688Sbonwick 	(void) zio_wait(zio);
1108789Sahrens 
1109789Sahrens 	return (error);
1110789Sahrens }
1111789Sahrens 
1112789Sahrens /*
11135688Sbonwick  * Sync the uberblock and any changes to the vdev configuration.
1114789Sahrens  *
1115789Sahrens  * The order of operations is carefully crafted to ensure that
1116789Sahrens  * if the system panics or loses power at any time, the state on disk
1117789Sahrens  * is still transactionally consistent.  The in-line comments below
1118789Sahrens  * describe the failure semantics at each stage.
1119789Sahrens  *
11205688Sbonwick  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1121789Sahrens  * at any time, you can just call it again, and it will resume its work.
1122789Sahrens  */
11237754SJeff.Bonwick@Sun.COM int
vdev_config_sync(vdev_t ** svd,int svdcount,uint64_t txg,boolean_t tryhard)11249725SEric.Schrock@Sun.COM vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard)
1125789Sahrens {
11265688Sbonwick 	spa_t *spa = svd[0]->vdev_spa;
1127789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
11281635Sbonwick 	vdev_t *vd;
1129789Sahrens 	zio_t *zio;
11307754SJeff.Bonwick@Sun.COM 	int error;
11317754SJeff.Bonwick@Sun.COM 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1132789Sahrens 
11339725SEric.Schrock@Sun.COM 	/*
11349725SEric.Schrock@Sun.COM 	 * Normally, we don't want to try too hard to write every label and
11359725SEric.Schrock@Sun.COM 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
11369725SEric.Schrock@Sun.COM 	 * sync process to block while we retry.  But if we can't write a
11379725SEric.Schrock@Sun.COM 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
11389725SEric.Schrock@Sun.COM 	 * bailing out and declaring the pool faulted.
11399725SEric.Schrock@Sun.COM 	 */
11409725SEric.Schrock@Sun.COM 	if (tryhard)
11419725SEric.Schrock@Sun.COM 		flags |= ZIO_FLAG_TRYHARD;
11429725SEric.Schrock@Sun.COM 
1143789Sahrens 	ASSERT(ub->ub_txg <= txg);
1144789Sahrens 
1145789Sahrens 	/*
11465688Sbonwick 	 * If this isn't a resync due to I/O errors,
11475688Sbonwick 	 * and nothing changed in this transaction group,
11485688Sbonwick 	 * and the vdev configuration hasn't changed,
11491635Sbonwick 	 * then there's nothing to do.
1150789Sahrens 	 */
11515688Sbonwick 	if (ub->ub_txg < txg &&
11525688Sbonwick 	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
11537754SJeff.Bonwick@Sun.COM 	    list_is_empty(&spa->spa_config_dirty_list))
11547754SJeff.Bonwick@Sun.COM 		return (0);
1155789Sahrens 
1156789Sahrens 	if (txg > spa_freeze_txg(spa))
11577754SJeff.Bonwick@Sun.COM 		return (0);
1158789Sahrens 
11591635Sbonwick 	ASSERT(txg <= spa->spa_final_txg);
11601635Sbonwick 
1161789Sahrens 	/*
1162789Sahrens 	 * Flush the write cache of every disk that's been written to
1163789Sahrens 	 * in this transaction group.  This ensures that all blocks
1164789Sahrens 	 * written in this txg will be committed to stable storage
1165789Sahrens 	 * before any uberblock that references them.
1166789Sahrens 	 */
11675688Sbonwick 	zio = zio_root(spa, NULL, NULL, flags);
11685688Sbonwick 
1169789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
11705688Sbonwick 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
11715688Sbonwick 		zio_flush(zio, vd);
11725688Sbonwick 
1173789Sahrens 	(void) zio_wait(zio);
1174789Sahrens 
1175789Sahrens 	/*
1176789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1177789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
1178789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
1179789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
11805688Sbonwick 	 * which have not yet been touched, will still be valid.  We flush
11815688Sbonwick 	 * the new labels to disk to ensure that all even-label updates
11825688Sbonwick 	 * are committed to stable storage before the uberblock update.
1183789Sahrens 	 */
11847754SJeff.Bonwick@Sun.COM 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
11857754SJeff.Bonwick@Sun.COM 		return (error);
1186789Sahrens 
1187789Sahrens 	/*
11887754SJeff.Bonwick@Sun.COM 	 * Sync the uberblocks to all vdevs in svd[].
11891635Sbonwick 	 * If the system dies in the middle of this step, there are two cases
11901635Sbonwick 	 * to consider, and the on-disk state is consistent either way:
1191789Sahrens 	 *
1192789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
1193789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
1194789Sahrens 	 *	(which had not yet been touched) will be valid with respect
1195789Sahrens 	 *	to that uberblock.
1196789Sahrens 	 *
1197789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
1198789Sahrens 	 *	will be the newest, and the even labels (which had all
1199789Sahrens 	 *	been successfully committed) will be valid with respect
1200789Sahrens 	 *	to the new uberblocks.
1201789Sahrens 	 */
12027754SJeff.Bonwick@Sun.COM 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
12037754SJeff.Bonwick@Sun.COM 		return (error);
1204789Sahrens 
1205789Sahrens 	/*
1206789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
1207789Sahrens 	 * in the middle of this process, the even labels and the new
1208789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
1209789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
1210789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
12115688Sbonwick 	 * all labels will be brought up to date.  We flush the new labels
12125688Sbonwick 	 * to disk to ensure that all odd-label updates are committed to
12135688Sbonwick 	 * stable storage before the next transaction group begins.
1214789Sahrens 	 */
12157754SJeff.Bonwick@Sun.COM 	return (vdev_label_sync_list(spa, 1, txg, flags));
1216789Sahrens }
1217