xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_label.c (revision b59a0cde6a5253f94494397ce5b18dbfa071e08c)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2017, Intel Corporation.
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy /*
29eda14cbcSMatt Macy  * Virtual Device Labels
30eda14cbcSMatt Macy  * ---------------------
31eda14cbcSMatt Macy  *
32eda14cbcSMatt Macy  * The vdev label serves several distinct purposes:
33eda14cbcSMatt Macy  *
34eda14cbcSMatt Macy  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
35eda14cbcSMatt Macy  *	   identity within the pool.
36eda14cbcSMatt Macy  *
37eda14cbcSMatt Macy  *	2. Verify that all the devices given in a configuration are present
38eda14cbcSMatt Macy  *         within the pool.
39eda14cbcSMatt Macy  *
40eda14cbcSMatt Macy  *	3. Determine the uberblock for the pool.
41eda14cbcSMatt Macy  *
42eda14cbcSMatt Macy  *	4. In case of an import operation, determine the configuration of the
43eda14cbcSMatt Macy  *         toplevel vdev of which it is a part.
44eda14cbcSMatt Macy  *
45eda14cbcSMatt Macy  *	5. If an import operation cannot find all the devices in the pool,
46eda14cbcSMatt Macy  *         provide enough information to the administrator to determine which
47eda14cbcSMatt Macy  *         devices are missing.
48eda14cbcSMatt Macy  *
49eda14cbcSMatt Macy  * It is important to note that while the kernel is responsible for writing the
50eda14cbcSMatt Macy  * label, it only consumes the information in the first three cases.  The
51eda14cbcSMatt Macy  * latter information is only consumed in userland when determining the
52eda14cbcSMatt Macy  * configuration to import a pool.
53eda14cbcSMatt Macy  *
54eda14cbcSMatt Macy  *
55eda14cbcSMatt Macy  * Label Organization
56eda14cbcSMatt Macy  * ------------------
57eda14cbcSMatt Macy  *
58eda14cbcSMatt Macy  * Before describing the contents of the label, it's important to understand how
59eda14cbcSMatt Macy  * the labels are written and updated with respect to the uberblock.
60eda14cbcSMatt Macy  *
61eda14cbcSMatt Macy  * When the pool configuration is altered, either because it was newly created
62eda14cbcSMatt Macy  * or a device was added, we want to update all the labels such that we can deal
63eda14cbcSMatt Macy  * with fatal failure at any point.  To this end, each disk has two labels which
64eda14cbcSMatt Macy  * are updated before and after the uberblock is synced.  Assuming we have
65eda14cbcSMatt Macy  * labels and an uberblock with the following transaction groups:
66eda14cbcSMatt Macy  *
67eda14cbcSMatt Macy  *              L1          UB          L2
68eda14cbcSMatt Macy  *           +------+    +------+    +------+
69eda14cbcSMatt Macy  *           |      |    |      |    |      |
70eda14cbcSMatt Macy  *           | t10  |    | t10  |    | t10  |
71eda14cbcSMatt Macy  *           |      |    |      |    |      |
72eda14cbcSMatt Macy  *           +------+    +------+    +------+
73eda14cbcSMatt Macy  *
74eda14cbcSMatt Macy  * In this stable state, the labels and the uberblock were all updated within
75eda14cbcSMatt Macy  * the same transaction group (10).  Each label is mirrored and checksummed, so
76eda14cbcSMatt Macy  * that we can detect when we fail partway through writing the label.
77eda14cbcSMatt Macy  *
78eda14cbcSMatt Macy  * In order to identify which labels are valid, the labels are written in the
79eda14cbcSMatt Macy  * following manner:
80eda14cbcSMatt Macy  *
81eda14cbcSMatt Macy  *	1. For each vdev, update 'L1' to the new label
82eda14cbcSMatt Macy  *	2. Update the uberblock
83eda14cbcSMatt Macy  *	3. For each vdev, update 'L2' to the new label
84eda14cbcSMatt Macy  *
85eda14cbcSMatt Macy  * Given arbitrary failure, we can determine the correct label to use based on
86eda14cbcSMatt Macy  * the transaction group.  If we fail after updating L1 but before updating the
87eda14cbcSMatt Macy  * UB, we will notice that L1's transaction group is greater than the uberblock,
88eda14cbcSMatt Macy  * so L2 must be valid.  If we fail after writing the uberblock but before
89eda14cbcSMatt Macy  * writing L2, we will notice that L2's transaction group is less than L1, and
90eda14cbcSMatt Macy  * therefore L1 is valid.
91eda14cbcSMatt Macy  *
92eda14cbcSMatt Macy  * Another added complexity is that not every label is updated when the config
93eda14cbcSMatt Macy  * is synced.  If we add a single device, we do not want to have to re-write
94eda14cbcSMatt Macy  * every label for every device in the pool.  This means that both L1 and L2 may
95eda14cbcSMatt Macy  * be older than the pool uberblock, because the necessary information is stored
96eda14cbcSMatt Macy  * on another vdev.
97eda14cbcSMatt Macy  *
98eda14cbcSMatt Macy  *
99eda14cbcSMatt Macy  * On-disk Format
100eda14cbcSMatt Macy  * --------------
101eda14cbcSMatt Macy  *
102eda14cbcSMatt Macy  * The vdev label consists of two distinct parts, and is wrapped within the
103eda14cbcSMatt Macy  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
104eda14cbcSMatt Macy  * VTOC disk labels, but is otherwise ignored.
105eda14cbcSMatt Macy  *
106eda14cbcSMatt Macy  * The first half of the label is a packed nvlist which contains pool wide
107eda14cbcSMatt Macy  * properties, per-vdev properties, and configuration information.  It is
108eda14cbcSMatt Macy  * described in more detail below.
109eda14cbcSMatt Macy  *
110eda14cbcSMatt Macy  * The latter half of the label consists of a redundant array of uberblocks.
111eda14cbcSMatt Macy  * These uberblocks are updated whenever a transaction group is committed,
112eda14cbcSMatt Macy  * or when the configuration is updated.  When a pool is loaded, we scan each
113eda14cbcSMatt Macy  * vdev for the 'best' uberblock.
114eda14cbcSMatt Macy  *
115eda14cbcSMatt Macy  *
116eda14cbcSMatt Macy  * Configuration Information
117eda14cbcSMatt Macy  * -------------------------
118eda14cbcSMatt Macy  *
119eda14cbcSMatt Macy  * The nvlist describing the pool and vdev contains the following elements:
120eda14cbcSMatt Macy  *
121eda14cbcSMatt Macy  *	version		ZFS on-disk version
122eda14cbcSMatt Macy  *	name		Pool name
123eda14cbcSMatt Macy  *	state		Pool state
124eda14cbcSMatt Macy  *	txg		Transaction group in which this label was written
125eda14cbcSMatt Macy  *	pool_guid	Unique identifier for this pool
126eda14cbcSMatt Macy  *	vdev_tree	An nvlist describing vdev tree.
127eda14cbcSMatt Macy  *	features_for_read
128eda14cbcSMatt Macy  *			An nvlist of the features necessary for reading the MOS.
129eda14cbcSMatt Macy  *
130eda14cbcSMatt Macy  * Each leaf device label also contains the following:
131eda14cbcSMatt Macy  *
132eda14cbcSMatt Macy  *	top_guid	Unique ID for top-level vdev in which this is contained
133eda14cbcSMatt Macy  *	guid		Unique ID for the leaf vdev
134eda14cbcSMatt Macy  *
135eda14cbcSMatt Macy  * The 'vs' configuration follows the format described in 'spa_config.c'.
136eda14cbcSMatt Macy  */
137eda14cbcSMatt Macy 
138eda14cbcSMatt Macy #include <sys/zfs_context.h>
139eda14cbcSMatt Macy #include <sys/spa.h>
140eda14cbcSMatt Macy #include <sys/spa_impl.h>
141eda14cbcSMatt Macy #include <sys/dmu.h>
142eda14cbcSMatt Macy #include <sys/zap.h>
143eda14cbcSMatt Macy #include <sys/vdev.h>
144eda14cbcSMatt Macy #include <sys/vdev_impl.h>
145e716630dSMartin Matuska #include <sys/vdev_raidz.h>
1467877fdebSMatt Macy #include <sys/vdev_draid.h>
147eda14cbcSMatt Macy #include <sys/uberblock_impl.h>
148eda14cbcSMatt Macy #include <sys/metaslab.h>
149eda14cbcSMatt Macy #include <sys/metaslab_impl.h>
150eda14cbcSMatt Macy #include <sys/zio.h>
151eda14cbcSMatt Macy #include <sys/dsl_scan.h>
152eda14cbcSMatt Macy #include <sys/abd.h>
153eda14cbcSMatt Macy #include <sys/fs/zfs.h>
1542c48331dSMatt Macy #include <sys/byteorder.h>
1552c48331dSMatt Macy #include <sys/zfs_bootenv.h>
156eda14cbcSMatt Macy 
157eda14cbcSMatt Macy /*
158eda14cbcSMatt Macy  * Basic routines to read and write from a vdev label.
159eda14cbcSMatt Macy  * Used throughout the rest of this file.
160eda14cbcSMatt Macy  */
161eda14cbcSMatt Macy uint64_t
162eda14cbcSMatt Macy vdev_label_offset(uint64_t psize, int l, uint64_t offset)
163eda14cbcSMatt Macy {
164eda14cbcSMatt Macy 	ASSERT(offset < sizeof (vdev_label_t));
165eda14cbcSMatt Macy 	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
168eda14cbcSMatt Macy 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
169eda14cbcSMatt Macy }
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy /*
172eda14cbcSMatt Macy  * Returns back the vdev label associated with the passed in offset.
173eda14cbcSMatt Macy  */
174eda14cbcSMatt Macy int
175eda14cbcSMatt Macy vdev_label_number(uint64_t psize, uint64_t offset)
176eda14cbcSMatt Macy {
177eda14cbcSMatt Macy 	int l;
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 	if (offset >= psize - VDEV_LABEL_END_SIZE) {
180eda14cbcSMatt Macy 		offset -= psize - VDEV_LABEL_END_SIZE;
181eda14cbcSMatt Macy 		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
182eda14cbcSMatt Macy 	}
183eda14cbcSMatt Macy 	l = offset / sizeof (vdev_label_t);
184eda14cbcSMatt Macy 	return (l < VDEV_LABELS ? l : -1);
185eda14cbcSMatt Macy }
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy static void
188eda14cbcSMatt Macy vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
189eda14cbcSMatt Macy     uint64_t size, zio_done_func_t *done, void *private, int flags)
190eda14cbcSMatt Macy {
191eda14cbcSMatt Macy 	ASSERT(
192eda14cbcSMatt Macy 	    spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
193eda14cbcSMatt Macy 	    spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
194eda14cbcSMatt Macy 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy 	zio_nowait(zio_read_phys(zio, vd,
197eda14cbcSMatt Macy 	    vdev_label_offset(vd->vdev_psize, l, offset),
198eda14cbcSMatt Macy 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
199eda14cbcSMatt Macy 	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
200eda14cbcSMatt Macy }
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy void
203eda14cbcSMatt Macy vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
204eda14cbcSMatt Macy     uint64_t size, zio_done_func_t *done, void *private, int flags)
205eda14cbcSMatt Macy {
206eda14cbcSMatt Macy 	ASSERT(
207eda14cbcSMatt Macy 	    spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
208eda14cbcSMatt Macy 	    spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
209eda14cbcSMatt Macy 	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
210eda14cbcSMatt Macy 
211eda14cbcSMatt Macy 	zio_nowait(zio_write_phys(zio, vd,
212eda14cbcSMatt Macy 	    vdev_label_offset(vd->vdev_psize, l, offset),
213eda14cbcSMatt Macy 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
214eda14cbcSMatt Macy 	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
215eda14cbcSMatt Macy }
216eda14cbcSMatt Macy 
217eda14cbcSMatt Macy /*
218eda14cbcSMatt Macy  * Generate the nvlist representing this vdev's stats
219eda14cbcSMatt Macy  */
220eda14cbcSMatt Macy void
221eda14cbcSMatt Macy vdev_config_generate_stats(vdev_t *vd, nvlist_t *nv)
222eda14cbcSMatt Macy {
223eda14cbcSMatt Macy 	nvlist_t *nvx;
224eda14cbcSMatt Macy 	vdev_stat_t *vs;
225eda14cbcSMatt Macy 	vdev_stat_ex_t *vsx;
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy 	vs = kmem_alloc(sizeof (*vs), KM_SLEEP);
228eda14cbcSMatt Macy 	vsx = kmem_alloc(sizeof (*vsx), KM_SLEEP);
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 	vdev_get_stats_ex(vd, vs, vsx);
231eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
232eda14cbcSMatt Macy 	    (uint64_t *)vs, sizeof (*vs) / sizeof (uint64_t));
233eda14cbcSMatt Macy 
234eda14cbcSMatt Macy 	/*
235eda14cbcSMatt Macy 	 * Add extended stats into a special extended stats nvlist.  This keeps
236eda14cbcSMatt Macy 	 * all the extended stats nicely grouped together.  The extended stats
237eda14cbcSMatt Macy 	 * nvlist is then added to the main nvlist.
238eda14cbcSMatt Macy 	 */
239eda14cbcSMatt Macy 	nvx = fnvlist_alloc();
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 	/* ZIOs in flight to disk */
242eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
243eda14cbcSMatt Macy 	    vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_READ]);
244eda14cbcSMatt Macy 
245eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
246eda14cbcSMatt Macy 	    vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_WRITE]);
247eda14cbcSMatt Macy 
248eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
249eda14cbcSMatt Macy 	    vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_READ]);
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
252eda14cbcSMatt Macy 	    vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_WRITE]);
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
255eda14cbcSMatt Macy 	    vsx->vsx_active_queue[ZIO_PRIORITY_SCRUB]);
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
258eda14cbcSMatt Macy 	    vsx->vsx_active_queue[ZIO_PRIORITY_TRIM]);
259eda14cbcSMatt Macy 
26021b492edSMartin Matuska 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
26121b492edSMartin Matuska 	    vsx->vsx_active_queue[ZIO_PRIORITY_REBUILD]);
26221b492edSMartin Matuska 
263eda14cbcSMatt Macy 	/* ZIOs pending */
264eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
265eda14cbcSMatt Macy 	    vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_READ]);
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
268eda14cbcSMatt Macy 	    vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_WRITE]);
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
271eda14cbcSMatt Macy 	    vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_READ]);
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
274eda14cbcSMatt Macy 	    vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_WRITE]);
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
277eda14cbcSMatt Macy 	    vsx->vsx_pend_queue[ZIO_PRIORITY_SCRUB]);
278eda14cbcSMatt Macy 
279eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
280eda14cbcSMatt Macy 	    vsx->vsx_pend_queue[ZIO_PRIORITY_TRIM]);
281eda14cbcSMatt Macy 
28221b492edSMartin Matuska 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
28321b492edSMartin Matuska 	    vsx->vsx_pend_queue[ZIO_PRIORITY_REBUILD]);
28421b492edSMartin Matuska 
285eda14cbcSMatt Macy 	/* Histograms */
286eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
287eda14cbcSMatt Macy 	    vsx->vsx_total_histo[ZIO_TYPE_READ],
288eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_READ]));
289eda14cbcSMatt Macy 
290eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
291eda14cbcSMatt Macy 	    vsx->vsx_total_histo[ZIO_TYPE_WRITE],
292eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_WRITE]));
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
295eda14cbcSMatt Macy 	    vsx->vsx_disk_histo[ZIO_TYPE_READ],
296eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_READ]));
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
299eda14cbcSMatt Macy 	    vsx->vsx_disk_histo[ZIO_TYPE_WRITE],
300eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_WRITE]));
301eda14cbcSMatt Macy 
302eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
303eda14cbcSMatt Macy 	    vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ],
304eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ]));
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
307eda14cbcSMatt Macy 	    vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE],
308eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE]));
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
311eda14cbcSMatt Macy 	    vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ],
312eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ]));
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
315eda14cbcSMatt Macy 	    vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE],
316eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE]));
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
319eda14cbcSMatt Macy 	    vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB],
320eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB]));
321eda14cbcSMatt Macy 
322eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
323eda14cbcSMatt Macy 	    vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM],
324eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM]));
325eda14cbcSMatt Macy 
32621b492edSMartin Matuska 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
32721b492edSMartin Matuska 	    vsx->vsx_queue_histo[ZIO_PRIORITY_REBUILD],
32821b492edSMartin Matuska 	    ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_REBUILD]));
32921b492edSMartin Matuska 
330eda14cbcSMatt Macy 	/* Request sizes */
331eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
332eda14cbcSMatt Macy 	    vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ],
333eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ]));
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
336eda14cbcSMatt Macy 	    vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE],
337eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE]));
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
340eda14cbcSMatt Macy 	    vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ],
341eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ]));
342eda14cbcSMatt Macy 
343eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
344eda14cbcSMatt Macy 	    vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE],
345eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE]));
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
348eda14cbcSMatt Macy 	    vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB],
349eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB]));
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
352eda14cbcSMatt Macy 	    vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM],
353eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM]));
354eda14cbcSMatt Macy 
35521b492edSMartin Matuska 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
35621b492edSMartin Matuska 	    vsx->vsx_ind_histo[ZIO_PRIORITY_REBUILD],
35721b492edSMartin Matuska 	    ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_REBUILD]));
35821b492edSMartin Matuska 
359eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
360eda14cbcSMatt Macy 	    vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ],
361eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ]));
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
364eda14cbcSMatt Macy 	    vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE],
365eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE]));
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
368eda14cbcSMatt Macy 	    vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ],
369eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ]));
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
372eda14cbcSMatt Macy 	    vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE],
373eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE]));
374eda14cbcSMatt Macy 
375eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
376eda14cbcSMatt Macy 	    vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB],
377eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB]));
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
380eda14cbcSMatt Macy 	    vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM],
381eda14cbcSMatt Macy 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM]));
382eda14cbcSMatt Macy 
38321b492edSMartin Matuska 	fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
38421b492edSMartin Matuska 	    vsx->vsx_agg_histo[ZIO_PRIORITY_REBUILD],
38521b492edSMartin Matuska 	    ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_REBUILD]));
38621b492edSMartin Matuska 
387eda14cbcSMatt Macy 	/* IO delays */
388eda14cbcSMatt Macy 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SLOW_IOS, vs->vs_slow_ios);
389eda14cbcSMatt Macy 
3907a7741afSMartin Matuska 	/* Direct I/O write verify errors */
3917a7741afSMartin Matuska 	fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_DIO_VERIFY_ERRORS,
3927a7741afSMartin Matuska 	    vs->vs_dio_verify_errors);
3937a7741afSMartin Matuska 
394eda14cbcSMatt Macy 	/* Add extended stats nvlist to main nvlist */
395eda14cbcSMatt Macy 	fnvlist_add_nvlist(nv, ZPOOL_CONFIG_VDEV_STATS_EX, nvx);
396eda14cbcSMatt Macy 
397eda14cbcSMatt Macy 	fnvlist_free(nvx);
398eda14cbcSMatt Macy 	kmem_free(vs, sizeof (*vs));
399eda14cbcSMatt Macy 	kmem_free(vsx, sizeof (*vsx));
400eda14cbcSMatt Macy }
401eda14cbcSMatt Macy 
402eda14cbcSMatt Macy static void
403eda14cbcSMatt Macy root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
404eda14cbcSMatt Macy {
405eda14cbcSMatt Macy 	spa_t *spa = vd->vdev_spa;
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy 	if (vd != spa->spa_root_vdev)
408eda14cbcSMatt Macy 		return;
409eda14cbcSMatt Macy 
410eda14cbcSMatt Macy 	/* provide either current or previous scan information */
411eda14cbcSMatt Macy 	pool_scan_stat_t ps;
412eda14cbcSMatt Macy 	if (spa_scan_get_stats(spa, &ps) == 0) {
413eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl,
414eda14cbcSMatt Macy 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
415eda14cbcSMatt Macy 		    sizeof (pool_scan_stat_t) / sizeof (uint64_t));
416eda14cbcSMatt Macy 	}
417eda14cbcSMatt Macy 
418eda14cbcSMatt Macy 	pool_removal_stat_t prs;
419eda14cbcSMatt Macy 	if (spa_removal_get_stats(spa, &prs) == 0) {
420eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl,
421eda14cbcSMatt Macy 		    ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
422eda14cbcSMatt Macy 		    sizeof (prs) / sizeof (uint64_t));
423eda14cbcSMatt Macy 	}
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 	pool_checkpoint_stat_t pcs;
426eda14cbcSMatt Macy 	if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
427eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl,
428eda14cbcSMatt Macy 		    ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
429eda14cbcSMatt Macy 		    sizeof (pcs) / sizeof (uint64_t));
430eda14cbcSMatt Macy 	}
431e716630dSMartin Matuska 
432e716630dSMartin Matuska 	pool_raidz_expand_stat_t pres;
433e716630dSMartin Matuska 	if (spa_raidz_expand_get_stats(spa, &pres) == 0) {
434e716630dSMartin Matuska 		fnvlist_add_uint64_array(nvl,
435e716630dSMartin Matuska 		    ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t *)&pres,
436e716630dSMartin Matuska 		    sizeof (pres) / sizeof (uint64_t));
437e716630dSMartin Matuska 	}
438eda14cbcSMatt Macy }
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy static void
441eda14cbcSMatt Macy top_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
442eda14cbcSMatt Macy {
443eda14cbcSMatt Macy 	if (vd == vd->vdev_top) {
444eda14cbcSMatt Macy 		vdev_rebuild_stat_t vrs;
445eda14cbcSMatt Macy 		if (vdev_rebuild_get_stats(vd, &vrs) == 0) {
446eda14cbcSMatt Macy 			fnvlist_add_uint64_array(nvl,
447eda14cbcSMatt Macy 			    ZPOOL_CONFIG_REBUILD_STATS, (uint64_t *)&vrs,
448eda14cbcSMatt Macy 			    sizeof (vrs) / sizeof (uint64_t));
449eda14cbcSMatt Macy 		}
450eda14cbcSMatt Macy 	}
451eda14cbcSMatt Macy }
452eda14cbcSMatt Macy 
453eda14cbcSMatt Macy /*
454eda14cbcSMatt Macy  * Generate the nvlist representing this vdev's config.
455eda14cbcSMatt Macy  */
456eda14cbcSMatt Macy nvlist_t *
457eda14cbcSMatt Macy vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
458eda14cbcSMatt Macy     vdev_config_flag_t flags)
459eda14cbcSMatt Macy {
460eda14cbcSMatt Macy 	nvlist_t *nv = NULL;
461eda14cbcSMatt Macy 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	nv = fnvlist_alloc();
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 	fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
466eda14cbcSMatt Macy 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
467eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
468eda14cbcSMatt Macy 	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
469eda14cbcSMatt Macy 
470eda14cbcSMatt Macy 	if (vd->vdev_path != NULL)
471eda14cbcSMatt Macy 		fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
472eda14cbcSMatt Macy 
473eda14cbcSMatt Macy 	if (vd->vdev_devid != NULL)
474eda14cbcSMatt Macy 		fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	if (vd->vdev_physpath != NULL)
477eda14cbcSMatt Macy 		fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
478eda14cbcSMatt Macy 		    vd->vdev_physpath);
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 	if (vd->vdev_enc_sysfs_path != NULL)
481eda14cbcSMatt Macy 		fnvlist_add_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
482eda14cbcSMatt Macy 		    vd->vdev_enc_sysfs_path);
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	if (vd->vdev_fru != NULL)
485eda14cbcSMatt Macy 		fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
486eda14cbcSMatt Macy 
4877877fdebSMatt Macy 	if (vd->vdev_ops->vdev_op_config_generate != NULL)
4887877fdebSMatt Macy 		vd->vdev_ops->vdev_op_config_generate(vd, nv);
489eda14cbcSMatt Macy 
4907877fdebSMatt Macy 	if (vd->vdev_wholedisk != -1ULL) {
491eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
492eda14cbcSMatt Macy 		    vd->vdev_wholedisk);
4937877fdebSMatt Macy 	}
494eda14cbcSMatt Macy 
495eda14cbcSMatt Macy 	if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
496eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
497eda14cbcSMatt Macy 
498eda14cbcSMatt Macy 	if (vd->vdev_isspare)
499eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
500eda14cbcSMatt Macy 
501b7198dcfSMartin Matuska 	if (flags & VDEV_CONFIG_L2CACHE)
502b7198dcfSMartin Matuska 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
503b7198dcfSMartin Matuska 
504eda14cbcSMatt Macy 	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
505eda14cbcSMatt Macy 	    vd == vd->vdev_top) {
506eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
507eda14cbcSMatt Macy 		    vd->vdev_ms_array);
508eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
509eda14cbcSMatt Macy 		    vd->vdev_ms_shift);
510eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
511eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
512eda14cbcSMatt Macy 		    vd->vdev_asize);
513eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
514681ce946SMartin Matuska 		if (vd->vdev_noalloc) {
515681ce946SMartin Matuska 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_NONALLOCATING,
516681ce946SMartin Matuska 			    vd->vdev_noalloc);
517681ce946SMartin Matuska 		}
5182a58b312SMartin Matuska 
5192a58b312SMartin Matuska 		/*
5202a58b312SMartin Matuska 		 * Slog devices are removed synchronously so don't
5212a58b312SMartin Matuska 		 * persist the vdev_removing flag to the label.
5222a58b312SMartin Matuska 		 */
5232a58b312SMartin Matuska 		if (vd->vdev_removing && !vd->vdev_islog) {
524eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
525eda14cbcSMatt Macy 			    vd->vdev_removing);
526eda14cbcSMatt Macy 		}
527eda14cbcSMatt Macy 
528eda14cbcSMatt Macy 		/* zpool command expects alloc class data */
529eda14cbcSMatt Macy 		if (getstats && vd->vdev_alloc_bias != VDEV_BIAS_NONE) {
530eda14cbcSMatt Macy 			const char *bias = NULL;
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 			switch (vd->vdev_alloc_bias) {
533eda14cbcSMatt Macy 			case VDEV_BIAS_LOG:
534eda14cbcSMatt Macy 				bias = VDEV_ALLOC_BIAS_LOG;
535eda14cbcSMatt Macy 				break;
536eda14cbcSMatt Macy 			case VDEV_BIAS_SPECIAL:
537eda14cbcSMatt Macy 				bias = VDEV_ALLOC_BIAS_SPECIAL;
538eda14cbcSMatt Macy 				break;
539eda14cbcSMatt Macy 			case VDEV_BIAS_DEDUP:
540eda14cbcSMatt Macy 				bias = VDEV_ALLOC_BIAS_DEDUP;
541eda14cbcSMatt Macy 				break;
542eda14cbcSMatt Macy 			default:
543eda14cbcSMatt Macy 				ASSERT3U(vd->vdev_alloc_bias, ==,
544eda14cbcSMatt Macy 				    VDEV_BIAS_NONE);
545eda14cbcSMatt Macy 			}
546eda14cbcSMatt Macy 			fnvlist_add_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
547eda14cbcSMatt Macy 			    bias);
548eda14cbcSMatt Macy 		}
549eda14cbcSMatt Macy 	}
550eda14cbcSMatt Macy 
551eda14cbcSMatt Macy 	if (vd->vdev_dtl_sm != NULL) {
552eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
553eda14cbcSMatt Macy 		    space_map_object(vd->vdev_dtl_sm));
554eda14cbcSMatt Macy 	}
555eda14cbcSMatt Macy 
556eda14cbcSMatt Macy 	if (vic->vic_mapping_object != 0) {
557eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
558eda14cbcSMatt Macy 		    vic->vic_mapping_object);
559eda14cbcSMatt Macy 	}
560eda14cbcSMatt Macy 
561eda14cbcSMatt Macy 	if (vic->vic_births_object != 0) {
562eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
563eda14cbcSMatt Macy 		    vic->vic_births_object);
564eda14cbcSMatt Macy 	}
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
567eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
568eda14cbcSMatt Macy 		    vic->vic_prev_indirect_vdev);
569eda14cbcSMatt Macy 	}
570eda14cbcSMatt Macy 
571eda14cbcSMatt Macy 	if (vd->vdev_crtxg)
572eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
573eda14cbcSMatt Macy 
574eda14cbcSMatt Macy 	if (vd->vdev_expansion_time)
575eda14cbcSMatt Macy 		fnvlist_add_uint64(nv, ZPOOL_CONFIG_EXPANSION_TIME,
576eda14cbcSMatt Macy 		    vd->vdev_expansion_time);
577eda14cbcSMatt Macy 
578eda14cbcSMatt Macy 	if (flags & VDEV_CONFIG_MOS) {
579eda14cbcSMatt Macy 		if (vd->vdev_leaf_zap != 0) {
580eda14cbcSMatt Macy 			ASSERT(vd->vdev_ops->vdev_op_leaf);
581eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
582eda14cbcSMatt Macy 			    vd->vdev_leaf_zap);
583eda14cbcSMatt Macy 		}
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 		if (vd->vdev_top_zap != 0) {
586eda14cbcSMatt Macy 			ASSERT(vd == vd->vdev_top);
587eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
588eda14cbcSMatt Macy 			    vd->vdev_top_zap);
589eda14cbcSMatt Macy 		}
590eda14cbcSMatt Macy 
591d411c1d6SMartin Matuska 		if (vd->vdev_ops == &vdev_root_ops && vd->vdev_root_zap != 0 &&
592d411c1d6SMartin Matuska 		    spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2)) {
593d411c1d6SMartin Matuska 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_ROOT_ZAP,
594d411c1d6SMartin Matuska 			    vd->vdev_root_zap);
595d411c1d6SMartin Matuska 		}
596d411c1d6SMartin Matuska 
597eda14cbcSMatt Macy 		if (vd->vdev_resilver_deferred) {
598eda14cbcSMatt Macy 			ASSERT(vd->vdev_ops->vdev_op_leaf);
599eda14cbcSMatt Macy 			ASSERT(spa->spa_resilver_deferred);
600eda14cbcSMatt Macy 			fnvlist_add_boolean(nv, ZPOOL_CONFIG_RESILVER_DEFER);
601eda14cbcSMatt Macy 		}
602eda14cbcSMatt Macy 	}
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy 	if (getstats) {
605eda14cbcSMatt Macy 		vdev_config_generate_stats(vd, nv);
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 		root_vdev_actions_getprogress(vd, nv);
608eda14cbcSMatt Macy 		top_vdev_actions_getprogress(vd, nv);
609eda14cbcSMatt Macy 
610eda14cbcSMatt Macy 		/*
611eda14cbcSMatt Macy 		 * Note: this can be called from open context
612eda14cbcSMatt Macy 		 * (spa_get_stats()), so we need the rwlock to prevent
613eda14cbcSMatt Macy 		 * the mapping from being changed by condensing.
614eda14cbcSMatt Macy 		 */
615eda14cbcSMatt Macy 		rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
616eda14cbcSMatt Macy 		if (vd->vdev_indirect_mapping != NULL) {
617eda14cbcSMatt Macy 			ASSERT(vd->vdev_indirect_births != NULL);
618eda14cbcSMatt Macy 			vdev_indirect_mapping_t *vim =
619eda14cbcSMatt Macy 			    vd->vdev_indirect_mapping;
620eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
621eda14cbcSMatt Macy 			    vdev_indirect_mapping_size(vim));
622eda14cbcSMatt Macy 		}
623eda14cbcSMatt Macy 		rw_exit(&vd->vdev_indirect_rwlock);
624eda14cbcSMatt Macy 		if (vd->vdev_mg != NULL &&
625eda14cbcSMatt Macy 		    vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
626eda14cbcSMatt Macy 			/*
627eda14cbcSMatt Macy 			 * Compute approximately how much memory would be used
628eda14cbcSMatt Macy 			 * for the indirect mapping if this device were to
629eda14cbcSMatt Macy 			 * be removed.
630eda14cbcSMatt Macy 			 *
631eda14cbcSMatt Macy 			 * Note: If the frag metric is invalid, then not
632eda14cbcSMatt Macy 			 * enough metaslabs have been converted to have
633eda14cbcSMatt Macy 			 * histograms.
634eda14cbcSMatt Macy 			 */
635eda14cbcSMatt Macy 			uint64_t seg_count = 0;
636eda14cbcSMatt Macy 			uint64_t to_alloc = vd->vdev_stat.vs_alloc;
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 			/*
639eda14cbcSMatt Macy 			 * There are the same number of allocated segments
640eda14cbcSMatt Macy 			 * as free segments, so we will have at least one
641eda14cbcSMatt Macy 			 * entry per free segment.  However, small free
642eda14cbcSMatt Macy 			 * segments (smaller than vdev_removal_max_span)
643eda14cbcSMatt Macy 			 * will be combined with adjacent allocated segments
644eda14cbcSMatt Macy 			 * as a single mapping.
645eda14cbcSMatt Macy 			 */
646*b59a0cdeSMartin Matuska 			for (int i = 0; i < ZFS_RANGE_TREE_HISTOGRAM_SIZE;
647*b59a0cdeSMartin Matuska 			    i++) {
648180f8225SMatt Macy 				if (i + 1 < highbit64(vdev_removal_max_span)
649180f8225SMatt Macy 				    - 1) {
650eda14cbcSMatt Macy 					to_alloc +=
651eda14cbcSMatt Macy 					    vd->vdev_mg->mg_histogram[i] <<
652eda14cbcSMatt Macy 					    (i + 1);
653eda14cbcSMatt Macy 				} else {
654eda14cbcSMatt Macy 					seg_count +=
655eda14cbcSMatt Macy 					    vd->vdev_mg->mg_histogram[i];
656eda14cbcSMatt Macy 				}
657eda14cbcSMatt Macy 			}
658eda14cbcSMatt Macy 
659eda14cbcSMatt Macy 			/*
660eda14cbcSMatt Macy 			 * The maximum length of a mapping is
661eda14cbcSMatt Macy 			 * zfs_remove_max_segment, so we need at least one entry
662eda14cbcSMatt Macy 			 * per zfs_remove_max_segment of allocated data.
663eda14cbcSMatt Macy 			 */
664eda14cbcSMatt Macy 			seg_count += to_alloc / spa_remove_max_segment(spa);
665eda14cbcSMatt Macy 
666eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
667eda14cbcSMatt Macy 			    seg_count *
668eda14cbcSMatt Macy 			    sizeof (vdev_indirect_mapping_entry_phys_t));
669eda14cbcSMatt Macy 		}
670eda14cbcSMatt Macy 	}
671eda14cbcSMatt Macy 
672eda14cbcSMatt Macy 	if (!vd->vdev_ops->vdev_op_leaf) {
673eda14cbcSMatt Macy 		nvlist_t **child;
6742a58b312SMartin Matuska 		uint64_t c;
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 		ASSERT(!vd->vdev_ishole);
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
679eda14cbcSMatt Macy 		    KM_SLEEP);
680eda14cbcSMatt Macy 
6812a58b312SMartin Matuska 		for (c = 0; c < vd->vdev_children; c++) {
6822a58b312SMartin Matuska 			child[c] = vdev_config_generate(spa, vd->vdev_child[c],
683eda14cbcSMatt Macy 			    getstats, flags);
684eda14cbcSMatt Macy 		}
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy 		fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
6872a58b312SMartin Matuska 		    (const nvlist_t * const *)child, vd->vdev_children);
688eda14cbcSMatt Macy 
6892a58b312SMartin Matuska 		for (c = 0; c < vd->vdev_children; c++)
690eda14cbcSMatt Macy 			nvlist_free(child[c]);
691eda14cbcSMatt Macy 
692eda14cbcSMatt Macy 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
693eda14cbcSMatt Macy 
694eda14cbcSMatt Macy 	} else {
695eda14cbcSMatt Macy 		const char *aux = NULL;
696eda14cbcSMatt Macy 
697eda14cbcSMatt Macy 		if (vd->vdev_offline && !vd->vdev_tmpoffline)
698eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
699eda14cbcSMatt Macy 		if (vd->vdev_resilver_txg != 0)
700eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
701eda14cbcSMatt Macy 			    vd->vdev_resilver_txg);
702eda14cbcSMatt Macy 		if (vd->vdev_rebuild_txg != 0)
703eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
704eda14cbcSMatt Macy 			    vd->vdev_rebuild_txg);
705eda14cbcSMatt Macy 		if (vd->vdev_faulted)
706eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
707eda14cbcSMatt Macy 		if (vd->vdev_degraded)
708eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
709eda14cbcSMatt Macy 		if (vd->vdev_removed)
710eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
711eda14cbcSMatt Macy 		if (vd->vdev_unspare)
712eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
713eda14cbcSMatt Macy 		if (vd->vdev_ishole)
714eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
715eda14cbcSMatt Macy 
716eda14cbcSMatt Macy 		/* Set the reason why we're FAULTED/DEGRADED. */
717eda14cbcSMatt Macy 		switch (vd->vdev_stat.vs_aux) {
718eda14cbcSMatt Macy 		case VDEV_AUX_ERR_EXCEEDED:
719eda14cbcSMatt Macy 			aux = "err_exceeded";
720eda14cbcSMatt Macy 			break;
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy 		case VDEV_AUX_EXTERNAL:
723eda14cbcSMatt Macy 			aux = "external";
724eda14cbcSMatt Macy 			break;
725eda14cbcSMatt Macy 		}
726eda14cbcSMatt Macy 
727eda14cbcSMatt Macy 		if (aux != NULL && !vd->vdev_tmpoffline) {
728eda14cbcSMatt Macy 			fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
729eda14cbcSMatt Macy 		} else {
730eda14cbcSMatt Macy 			/*
731eda14cbcSMatt Macy 			 * We're healthy - clear any previous AUX_STATE values.
732eda14cbcSMatt Macy 			 */
733eda14cbcSMatt Macy 			if (nvlist_exists(nv, ZPOOL_CONFIG_AUX_STATE))
734eda14cbcSMatt Macy 				nvlist_remove_all(nv, ZPOOL_CONFIG_AUX_STATE);
735eda14cbcSMatt Macy 		}
736eda14cbcSMatt Macy 
737eda14cbcSMatt Macy 		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
738eda14cbcSMatt Macy 			fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
739eda14cbcSMatt Macy 			    vd->vdev_orig_guid);
740eda14cbcSMatt Macy 		}
741eda14cbcSMatt Macy 	}
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy 	return (nv);
744eda14cbcSMatt Macy }
745eda14cbcSMatt Macy 
746eda14cbcSMatt Macy /*
747eda14cbcSMatt Macy  * Generate a view of the top-level vdevs.  If we currently have holes
748eda14cbcSMatt Macy  * in the namespace, then generate an array which contains a list of holey
749eda14cbcSMatt Macy  * vdevs.  Additionally, add the number of top-level children that currently
750eda14cbcSMatt Macy  * exist.
751eda14cbcSMatt Macy  */
752eda14cbcSMatt Macy void
753eda14cbcSMatt Macy vdev_top_config_generate(spa_t *spa, nvlist_t *config)
754eda14cbcSMatt Macy {
755eda14cbcSMatt Macy 	vdev_t *rvd = spa->spa_root_vdev;
756eda14cbcSMatt Macy 	uint64_t *array;
757eda14cbcSMatt Macy 	uint_t c, idx;
758eda14cbcSMatt Macy 
759eda14cbcSMatt Macy 	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
760eda14cbcSMatt Macy 
761eda14cbcSMatt Macy 	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
762eda14cbcSMatt Macy 		vdev_t *tvd = rvd->vdev_child[c];
763eda14cbcSMatt Macy 
764eda14cbcSMatt Macy 		if (tvd->vdev_ishole) {
765eda14cbcSMatt Macy 			array[idx++] = c;
766eda14cbcSMatt Macy 		}
767eda14cbcSMatt Macy 	}
768eda14cbcSMatt Macy 
769eda14cbcSMatt Macy 	if (idx) {
770eda14cbcSMatt Macy 		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
771eda14cbcSMatt Macy 		    array, idx) == 0);
772eda14cbcSMatt Macy 	}
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
775eda14cbcSMatt Macy 	    rvd->vdev_children) == 0);
776eda14cbcSMatt Macy 
777eda14cbcSMatt Macy 	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
778eda14cbcSMatt Macy }
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy /*
781eda14cbcSMatt Macy  * Returns the configuration from the label of the given vdev. For vdevs
782eda14cbcSMatt Macy  * which don't have a txg value stored on their label (i.e. spares/cache)
783eda14cbcSMatt Macy  * or have not been completely initialized (txg = 0) just return
784eda14cbcSMatt Macy  * the configuration from the first valid label we find. Otherwise,
785eda14cbcSMatt Macy  * find the most up-to-date label that does not exceed the specified
786eda14cbcSMatt Macy  * 'txg' value.
787eda14cbcSMatt Macy  */
788eda14cbcSMatt Macy nvlist_t *
789eda14cbcSMatt Macy vdev_label_read_config(vdev_t *vd, uint64_t txg)
790eda14cbcSMatt Macy {
791eda14cbcSMatt Macy 	spa_t *spa = vd->vdev_spa;
792eda14cbcSMatt Macy 	nvlist_t *config = NULL;
793184c1b94SMartin Matuska 	vdev_phys_t *vp[VDEV_LABELS];
794184c1b94SMartin Matuska 	abd_t *vp_abd[VDEV_LABELS];
795184c1b94SMartin Matuska 	zio_t *zio[VDEV_LABELS];
796eda14cbcSMatt Macy 	uint64_t best_txg = 0;
797eda14cbcSMatt Macy 	uint64_t label_txg = 0;
798eda14cbcSMatt Macy 	int error = 0;
799eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
800eda14cbcSMatt Macy 	    ZIO_FLAG_SPECULATIVE;
801eda14cbcSMatt Macy 
802184c1b94SMartin Matuska 	ASSERT(vd->vdev_validate_thread == curthread ||
803184c1b94SMartin Matuska 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
804eda14cbcSMatt Macy 
805eda14cbcSMatt Macy 	if (!vdev_readable(vd))
806eda14cbcSMatt Macy 		return (NULL);
807eda14cbcSMatt Macy 
8087877fdebSMatt Macy 	/*
8097877fdebSMatt Macy 	 * The label for a dRAID distributed spare is not stored on disk.
8107877fdebSMatt Macy 	 * Instead it is generated when needed which allows us to bypass
8117877fdebSMatt Macy 	 * the pipeline when reading the config from the label.
8127877fdebSMatt Macy 	 */
8137877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops)
8147877fdebSMatt Macy 		return (vdev_draid_read_config_spare(vd));
8157877fdebSMatt Macy 
816184c1b94SMartin Matuska 	for (int l = 0; l < VDEV_LABELS; l++) {
817184c1b94SMartin Matuska 		vp_abd[l] = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
818184c1b94SMartin Matuska 		vp[l] = abd_to_buf(vp_abd[l]);
819184c1b94SMartin Matuska 	}
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy retry:
822eda14cbcSMatt Macy 	for (int l = 0; l < VDEV_LABELS; l++) {
823184c1b94SMartin Matuska 		zio[l] = zio_root(spa, NULL, NULL, flags);
824184c1b94SMartin Matuska 
825184c1b94SMartin Matuska 		vdev_label_read(zio[l], vd, l, vp_abd[l],
826184c1b94SMartin Matuska 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
827184c1b94SMartin Matuska 		    NULL, NULL, flags);
828184c1b94SMartin Matuska 	}
829184c1b94SMartin Matuska 	for (int l = 0; l < VDEV_LABELS; l++) {
830eda14cbcSMatt Macy 		nvlist_t *label = NULL;
831eda14cbcSMatt Macy 
832184c1b94SMartin Matuska 		if (zio_wait(zio[l]) == 0 &&
833184c1b94SMartin Matuska 		    nvlist_unpack(vp[l]->vp_nvlist, sizeof (vp[l]->vp_nvlist),
834eda14cbcSMatt Macy 		    &label, 0) == 0) {
835eda14cbcSMatt Macy 			/*
836eda14cbcSMatt Macy 			 * Auxiliary vdevs won't have txg values in their
837eda14cbcSMatt Macy 			 * labels and newly added vdevs may not have been
838eda14cbcSMatt Macy 			 * completely initialized so just return the
839eda14cbcSMatt Macy 			 * configuration from the first valid label we
840eda14cbcSMatt Macy 			 * encounter.
841eda14cbcSMatt Macy 			 */
842eda14cbcSMatt Macy 			error = nvlist_lookup_uint64(label,
843eda14cbcSMatt Macy 			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
844eda14cbcSMatt Macy 			if ((error || label_txg == 0) && !config) {
845eda14cbcSMatt Macy 				config = label;
846184c1b94SMartin Matuska 				for (l++; l < VDEV_LABELS; l++)
847184c1b94SMartin Matuska 					zio_wait(zio[l]);
848eda14cbcSMatt Macy 				break;
849eda14cbcSMatt Macy 			} else if (label_txg <= txg && label_txg > best_txg) {
850eda14cbcSMatt Macy 				best_txg = label_txg;
851eda14cbcSMatt Macy 				nvlist_free(config);
852eda14cbcSMatt Macy 				config = fnvlist_dup(label);
853eda14cbcSMatt Macy 			}
854eda14cbcSMatt Macy 		}
855eda14cbcSMatt Macy 
856eda14cbcSMatt Macy 		if (label != NULL) {
857eda14cbcSMatt Macy 			nvlist_free(label);
858eda14cbcSMatt Macy 			label = NULL;
859eda14cbcSMatt Macy 		}
860eda14cbcSMatt Macy 	}
861eda14cbcSMatt Macy 
862eda14cbcSMatt Macy 	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
863eda14cbcSMatt Macy 		flags |= ZIO_FLAG_TRYHARD;
864eda14cbcSMatt Macy 		goto retry;
865eda14cbcSMatt Macy 	}
866eda14cbcSMatt Macy 
867eda14cbcSMatt Macy 	/*
868eda14cbcSMatt Macy 	 * We found a valid label but it didn't pass txg restrictions.
869eda14cbcSMatt Macy 	 */
870eda14cbcSMatt Macy 	if (config == NULL && label_txg != 0) {
871eda14cbcSMatt Macy 		vdev_dbgmsg(vd, "label discarded as txg is too large "
872eda14cbcSMatt Macy 		    "(%llu > %llu)", (u_longlong_t)label_txg,
873eda14cbcSMatt Macy 		    (u_longlong_t)txg);
874eda14cbcSMatt Macy 	}
875eda14cbcSMatt Macy 
876184c1b94SMartin Matuska 	for (int l = 0; l < VDEV_LABELS; l++) {
877184c1b94SMartin Matuska 		abd_free(vp_abd[l]);
878184c1b94SMartin Matuska 	}
879eda14cbcSMatt Macy 
880eda14cbcSMatt Macy 	return (config);
881eda14cbcSMatt Macy }
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy /*
884eda14cbcSMatt Macy  * Determine if a device is in use.  The 'spare_guid' parameter will be filled
885eda14cbcSMatt Macy  * in with the device guid if this spare is active elsewhere on the system.
886eda14cbcSMatt Macy  */
887eda14cbcSMatt Macy static boolean_t
888eda14cbcSMatt Macy vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
889eda14cbcSMatt Macy     uint64_t *spare_guid, uint64_t *l2cache_guid)
890eda14cbcSMatt Macy {
891eda14cbcSMatt Macy 	spa_t *spa = vd->vdev_spa;
892eda14cbcSMatt Macy 	uint64_t state, pool_guid, device_guid, txg, spare_pool;
893eda14cbcSMatt Macy 	uint64_t vdtxg = 0;
894eda14cbcSMatt Macy 	nvlist_t *label;
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy 	if (spare_guid)
897eda14cbcSMatt Macy 		*spare_guid = 0ULL;
898eda14cbcSMatt Macy 	if (l2cache_guid)
899eda14cbcSMatt Macy 		*l2cache_guid = 0ULL;
900eda14cbcSMatt Macy 
901eda14cbcSMatt Macy 	/*
902eda14cbcSMatt Macy 	 * Read the label, if any, and perform some basic sanity checks.
903eda14cbcSMatt Macy 	 */
904eda14cbcSMatt Macy 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
905eda14cbcSMatt Macy 		return (B_FALSE);
906eda14cbcSMatt Macy 
907eda14cbcSMatt Macy 	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
908eda14cbcSMatt Macy 	    &vdtxg);
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
911eda14cbcSMatt Macy 	    &state) != 0 ||
912eda14cbcSMatt Macy 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
913eda14cbcSMatt Macy 	    &device_guid) != 0) {
914eda14cbcSMatt Macy 		nvlist_free(label);
915eda14cbcSMatt Macy 		return (B_FALSE);
916eda14cbcSMatt Macy 	}
917eda14cbcSMatt Macy 
918eda14cbcSMatt Macy 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
919eda14cbcSMatt Macy 	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
920eda14cbcSMatt Macy 	    &pool_guid) != 0 ||
921eda14cbcSMatt Macy 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
922eda14cbcSMatt Macy 	    &txg) != 0)) {
923eda14cbcSMatt Macy 		nvlist_free(label);
924eda14cbcSMatt Macy 		return (B_FALSE);
925eda14cbcSMatt Macy 	}
926eda14cbcSMatt Macy 
927eda14cbcSMatt Macy 	nvlist_free(label);
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 	/*
930eda14cbcSMatt Macy 	 * Check to see if this device indeed belongs to the pool it claims to
931eda14cbcSMatt Macy 	 * be a part of.  The only way this is allowed is if the device is a hot
932eda14cbcSMatt Macy 	 * spare (which we check for later on).
933eda14cbcSMatt Macy 	 */
934eda14cbcSMatt Macy 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
935eda14cbcSMatt Macy 	    !spa_guid_exists(pool_guid, device_guid) &&
936eda14cbcSMatt Macy 	    !spa_spare_exists(device_guid, NULL, NULL) &&
937eda14cbcSMatt Macy 	    !spa_l2cache_exists(device_guid, NULL))
938eda14cbcSMatt Macy 		return (B_FALSE);
939eda14cbcSMatt Macy 
940eda14cbcSMatt Macy 	/*
941eda14cbcSMatt Macy 	 * If the transaction group is zero, then this an initialized (but
942eda14cbcSMatt Macy 	 * unused) label.  This is only an error if the create transaction
943eda14cbcSMatt Macy 	 * on-disk is the same as the one we're using now, in which case the
944eda14cbcSMatt Macy 	 * user has attempted to add the same vdev multiple times in the same
945eda14cbcSMatt Macy 	 * transaction.
946eda14cbcSMatt Macy 	 */
947eda14cbcSMatt Macy 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
948eda14cbcSMatt Macy 	    txg == 0 && vdtxg == crtxg)
949eda14cbcSMatt Macy 		return (B_TRUE);
950eda14cbcSMatt Macy 
951eda14cbcSMatt Macy 	/*
952eda14cbcSMatt Macy 	 * Check to see if this is a spare device.  We do an explicit check for
953eda14cbcSMatt Macy 	 * spa_has_spare() here because it may be on our pending list of spares
954dae17134SMartin Matuska 	 * to add.
955eda14cbcSMatt Macy 	 */
956eda14cbcSMatt Macy 	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
957eda14cbcSMatt Macy 	    spa_has_spare(spa, device_guid)) {
958eda14cbcSMatt Macy 		if (spare_guid)
959eda14cbcSMatt Macy 			*spare_guid = device_guid;
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy 		switch (reason) {
962eda14cbcSMatt Macy 		case VDEV_LABEL_CREATE:
963eda14cbcSMatt Macy 			return (B_TRUE);
964eda14cbcSMatt Macy 
965eda14cbcSMatt Macy 		case VDEV_LABEL_REPLACE:
966eda14cbcSMatt Macy 			return (!spa_has_spare(spa, device_guid) ||
967eda14cbcSMatt Macy 			    spare_pool != 0ULL);
968eda14cbcSMatt Macy 
969eda14cbcSMatt Macy 		case VDEV_LABEL_SPARE:
970eda14cbcSMatt Macy 			return (spa_has_spare(spa, device_guid));
971eda14cbcSMatt Macy 		default:
972eda14cbcSMatt Macy 			break;
973eda14cbcSMatt Macy 		}
974eda14cbcSMatt Macy 	}
975eda14cbcSMatt Macy 
976eda14cbcSMatt Macy 	/*
977eda14cbcSMatt Macy 	 * Check to see if this is an l2cache device.
978eda14cbcSMatt Macy 	 */
979dae17134SMartin Matuska 	if (spa_l2cache_exists(device_guid, NULL) ||
980dae17134SMartin Matuska 	    spa_has_l2cache(spa, device_guid)) {
981dae17134SMartin Matuska 		if (l2cache_guid)
982dae17134SMartin Matuska 			*l2cache_guid = device_guid;
983dae17134SMartin Matuska 
984dae17134SMartin Matuska 		switch (reason) {
985dae17134SMartin Matuska 		case VDEV_LABEL_CREATE:
986eda14cbcSMatt Macy 			return (B_TRUE);
987eda14cbcSMatt Macy 
988dae17134SMartin Matuska 		case VDEV_LABEL_REPLACE:
989dae17134SMartin Matuska 			return (!spa_has_l2cache(spa, device_guid));
990dae17134SMartin Matuska 
991dae17134SMartin Matuska 		case VDEV_LABEL_L2CACHE:
992dae17134SMartin Matuska 			return (spa_has_l2cache(spa, device_guid));
993dae17134SMartin Matuska 		default:
994dae17134SMartin Matuska 			break;
995dae17134SMartin Matuska 		}
996dae17134SMartin Matuska 	}
997dae17134SMartin Matuska 
998eda14cbcSMatt Macy 	/*
999eda14cbcSMatt Macy 	 * We can't rely on a pool's state if it's been imported
1000eda14cbcSMatt Macy 	 * read-only.  Instead we look to see if the pools is marked
1001eda14cbcSMatt Macy 	 * read-only in the namespace and set the state to active.
1002eda14cbcSMatt Macy 	 */
1003eda14cbcSMatt Macy 	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1004eda14cbcSMatt Macy 	    (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
1005eda14cbcSMatt Macy 	    spa_mode(spa) == SPA_MODE_READ)
1006eda14cbcSMatt Macy 		state = POOL_STATE_ACTIVE;
1007eda14cbcSMatt Macy 
1008eda14cbcSMatt Macy 	/*
1009eda14cbcSMatt Macy 	 * If the device is marked ACTIVE, then this device is in use by another
1010eda14cbcSMatt Macy 	 * pool on the system.
1011eda14cbcSMatt Macy 	 */
1012eda14cbcSMatt Macy 	return (state == POOL_STATE_ACTIVE);
1013eda14cbcSMatt Macy }
1014eda14cbcSMatt Macy 
1015ce4dcb97SMartin Matuska static nvlist_t *
1016ce4dcb97SMartin Matuska vdev_aux_label_generate(vdev_t *vd, boolean_t reason_spare)
1017ce4dcb97SMartin Matuska {
1018ce4dcb97SMartin Matuska 	/*
1019ce4dcb97SMartin Matuska 	 * For inactive hot spares and level 2 ARC devices, we generate
1020ce4dcb97SMartin Matuska 	 * a special label that identifies as a mutually shared hot
1021ce4dcb97SMartin Matuska 	 * spare or l2cache device. We write the label in case of
1022ce4dcb97SMartin Matuska 	 * addition or removal of hot spare or l2cache vdev (in which
1023ce4dcb97SMartin Matuska 	 * case we want to revert the labels).
1024ce4dcb97SMartin Matuska 	 */
1025ce4dcb97SMartin Matuska 	nvlist_t *label = fnvlist_alloc();
1026ce4dcb97SMartin Matuska 	fnvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
1027ce4dcb97SMartin Matuska 	    spa_version(vd->vdev_spa));
1028ce4dcb97SMartin Matuska 	fnvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, reason_spare ?
1029ce4dcb97SMartin Matuska 	    POOL_STATE_SPARE : POOL_STATE_L2CACHE);
1030ce4dcb97SMartin Matuska 	fnvlist_add_uint64(label, ZPOOL_CONFIG_GUID, vd->vdev_guid);
1031ce4dcb97SMartin Matuska 
1032ce4dcb97SMartin Matuska 	/*
1033ce4dcb97SMartin Matuska 	 * This is merely to facilitate reporting the ashift of the
1034ce4dcb97SMartin Matuska 	 * cache device through zdb. The actual retrieval of the
1035ce4dcb97SMartin Matuska 	 * ashift (in vdev_alloc()) uses the nvlist
1036ce4dcb97SMartin Matuska 	 * spa->spa_l2cache->sav_config (populated in
1037ce4dcb97SMartin Matuska 	 * spa_ld_open_aux_vdevs()).
1038ce4dcb97SMartin Matuska 	 */
1039ce4dcb97SMartin Matuska 	if (!reason_spare)
1040ce4dcb97SMartin Matuska 		fnvlist_add_uint64(label, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
1041ce4dcb97SMartin Matuska 
1042ce4dcb97SMartin Matuska 	/*
1043ce4dcb97SMartin Matuska 	 * Add path information to help find it during pool import
1044ce4dcb97SMartin Matuska 	 */
1045ce4dcb97SMartin Matuska 	if (vd->vdev_path != NULL)
1046ce4dcb97SMartin Matuska 		fnvlist_add_string(label, ZPOOL_CONFIG_PATH, vd->vdev_path);
1047ce4dcb97SMartin Matuska 	if (vd->vdev_devid != NULL)
1048ce4dcb97SMartin Matuska 		fnvlist_add_string(label, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
1049ce4dcb97SMartin Matuska 	if (vd->vdev_physpath != NULL) {
1050ce4dcb97SMartin Matuska 		fnvlist_add_string(label, ZPOOL_CONFIG_PHYS_PATH,
1051ce4dcb97SMartin Matuska 		    vd->vdev_physpath);
1052ce4dcb97SMartin Matuska 	}
1053ce4dcb97SMartin Matuska 	return (label);
1054ce4dcb97SMartin Matuska }
1055ce4dcb97SMartin Matuska 
1056eda14cbcSMatt Macy /*
1057eda14cbcSMatt Macy  * Initialize a vdev label.  We check to make sure each leaf device is not in
1058eda14cbcSMatt Macy  * use, and writable.  We put down an initial label which we will later
1059eda14cbcSMatt Macy  * overwrite with a complete label.  Note that it's important to do this
1060eda14cbcSMatt Macy  * sequentially, not in parallel, so that we catch cases of multiple use of the
1061eda14cbcSMatt Macy  * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
1062eda14cbcSMatt Macy  * itself.
1063eda14cbcSMatt Macy  */
1064eda14cbcSMatt Macy int
1065eda14cbcSMatt Macy vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
1066eda14cbcSMatt Macy {
1067eda14cbcSMatt Macy 	spa_t *spa = vd->vdev_spa;
1068eda14cbcSMatt Macy 	nvlist_t *label;
1069eda14cbcSMatt Macy 	vdev_phys_t *vp;
1070eda14cbcSMatt Macy 	abd_t *vp_abd;
1071eda14cbcSMatt Macy 	abd_t *bootenv;
1072eda14cbcSMatt Macy 	uberblock_t *ub;
1073eda14cbcSMatt Macy 	abd_t *ub_abd;
1074eda14cbcSMatt Macy 	zio_t *zio;
1075eda14cbcSMatt Macy 	char *buf;
1076eda14cbcSMatt Macy 	size_t buflen;
1077eda14cbcSMatt Macy 	int error;
1078eda14cbcSMatt Macy 	uint64_t spare_guid = 0, l2cache_guid = 0;
1079eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
108078ae60b4SMartin Matuska 	boolean_t reason_spare = (reason == VDEV_LABEL_SPARE || (reason ==
108178ae60b4SMartin Matuska 	    VDEV_LABEL_REMOVE && vd->vdev_isspare));
108278ae60b4SMartin Matuska 	boolean_t reason_l2cache = (reason == VDEV_LABEL_L2CACHE || (reason ==
108378ae60b4SMartin Matuska 	    VDEV_LABEL_REMOVE && vd->vdev_isl2cache));
1084eda14cbcSMatt Macy 
1085eda14cbcSMatt Macy 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1086eda14cbcSMatt Macy 
1087eda14cbcSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++)
1088eda14cbcSMatt Macy 		if ((error = vdev_label_init(vd->vdev_child[c],
1089eda14cbcSMatt Macy 		    crtxg, reason)) != 0)
1090eda14cbcSMatt Macy 			return (error);
1091eda14cbcSMatt Macy 
1092eda14cbcSMatt Macy 	/* Track the creation time for this vdev */
1093eda14cbcSMatt Macy 	vd->vdev_crtxg = crtxg;
1094eda14cbcSMatt Macy 
1095eda14cbcSMatt Macy 	if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
1096eda14cbcSMatt Macy 		return (0);
1097eda14cbcSMatt Macy 
1098eda14cbcSMatt Macy 	/*
1099eda14cbcSMatt Macy 	 * Dead vdevs cannot be initialized.
1100eda14cbcSMatt Macy 	 */
1101eda14cbcSMatt Macy 	if (vdev_is_dead(vd))
1102eda14cbcSMatt Macy 		return (SET_ERROR(EIO));
1103eda14cbcSMatt Macy 
1104eda14cbcSMatt Macy 	/*
1105eda14cbcSMatt Macy 	 * Determine if the vdev is in use.
1106eda14cbcSMatt Macy 	 */
1107eda14cbcSMatt Macy 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
1108eda14cbcSMatt Macy 	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
1109eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
1110eda14cbcSMatt Macy 
1111eda14cbcSMatt Macy 	/*
1112eda14cbcSMatt Macy 	 * If this is a request to add or replace a spare or l2cache device
1113eda14cbcSMatt Macy 	 * that is in use elsewhere on the system, then we must update the
1114eda14cbcSMatt Macy 	 * guid (which was initialized to a random value) to reflect the
1115eda14cbcSMatt Macy 	 * actual GUID (which is shared between multiple pools).
1116eda14cbcSMatt Macy 	 */
1117eda14cbcSMatt Macy 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
1118eda14cbcSMatt Macy 	    spare_guid != 0ULL) {
1119eda14cbcSMatt Macy 		uint64_t guid_delta = spare_guid - vd->vdev_guid;
1120eda14cbcSMatt Macy 
1121eda14cbcSMatt Macy 		vd->vdev_guid += guid_delta;
1122eda14cbcSMatt Macy 
1123eda14cbcSMatt Macy 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1124eda14cbcSMatt Macy 			pvd->vdev_guid_sum += guid_delta;
1125eda14cbcSMatt Macy 
1126eda14cbcSMatt Macy 		/*
1127eda14cbcSMatt Macy 		 * If this is a replacement, then we want to fallthrough to the
1128eda14cbcSMatt Macy 		 * rest of the code.  If we're adding a spare, then it's already
1129eda14cbcSMatt Macy 		 * labeled appropriately and we can just return.
1130eda14cbcSMatt Macy 		 */
1131eda14cbcSMatt Macy 		if (reason == VDEV_LABEL_SPARE)
1132eda14cbcSMatt Macy 			return (0);
1133eda14cbcSMatt Macy 		ASSERT(reason == VDEV_LABEL_REPLACE ||
1134eda14cbcSMatt Macy 		    reason == VDEV_LABEL_SPLIT);
1135eda14cbcSMatt Macy 	}
1136eda14cbcSMatt Macy 
1137eda14cbcSMatt Macy 	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
1138eda14cbcSMatt Macy 	    l2cache_guid != 0ULL) {
1139eda14cbcSMatt Macy 		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
1140eda14cbcSMatt Macy 
1141eda14cbcSMatt Macy 		vd->vdev_guid += guid_delta;
1142eda14cbcSMatt Macy 
1143eda14cbcSMatt Macy 		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1144eda14cbcSMatt Macy 			pvd->vdev_guid_sum += guid_delta;
1145eda14cbcSMatt Macy 
1146eda14cbcSMatt Macy 		/*
1147eda14cbcSMatt Macy 		 * If this is a replacement, then we want to fallthrough to the
1148eda14cbcSMatt Macy 		 * rest of the code.  If we're adding an l2cache, then it's
1149eda14cbcSMatt Macy 		 * already labeled appropriately and we can just return.
1150eda14cbcSMatt Macy 		 */
1151eda14cbcSMatt Macy 		if (reason == VDEV_LABEL_L2CACHE)
1152eda14cbcSMatt Macy 			return (0);
1153eda14cbcSMatt Macy 		ASSERT(reason == VDEV_LABEL_REPLACE);
1154eda14cbcSMatt Macy 	}
1155eda14cbcSMatt Macy 
1156eda14cbcSMatt Macy 	/*
1157eda14cbcSMatt Macy 	 * Initialize its label.
1158eda14cbcSMatt Macy 	 */
1159eda14cbcSMatt Macy 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1160eda14cbcSMatt Macy 	abd_zero(vp_abd, sizeof (vdev_phys_t));
1161eda14cbcSMatt Macy 	vp = abd_to_buf(vp_abd);
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy 	/*
1164eda14cbcSMatt Macy 	 * Generate a label describing the pool and our top-level vdev.
1165eda14cbcSMatt Macy 	 * We mark it as being from txg 0 to indicate that it's not
1166eda14cbcSMatt Macy 	 * really part of an active pool just yet.  The labels will
1167eda14cbcSMatt Macy 	 * be written again with a meaningful txg by spa_sync().
1168eda14cbcSMatt Macy 	 */
116978ae60b4SMartin Matuska 	if (reason_spare || reason_l2cache) {
1170ce4dcb97SMartin Matuska 		label = vdev_aux_label_generate(vd, reason_spare);
117178ae60b4SMartin Matuska 
117278ae60b4SMartin Matuska 		/*
117378ae60b4SMartin Matuska 		 * When spare or l2cache (aux) vdev is added during pool
117478ae60b4SMartin Matuska 		 * creation, spa->spa_uberblock is not written until this
117578ae60b4SMartin Matuska 		 * point. Write it on next config sync.
117678ae60b4SMartin Matuska 		 */
117778ae60b4SMartin Matuska 		if (uberblock_verify(&spa->spa_uberblock))
117878ae60b4SMartin Matuska 			spa->spa_aux_sync_uber = B_TRUE;
1179eda14cbcSMatt Macy 	} else {
1180eda14cbcSMatt Macy 		uint64_t txg = 0ULL;
1181eda14cbcSMatt Macy 
1182eda14cbcSMatt Macy 		if (reason == VDEV_LABEL_SPLIT)
1183eda14cbcSMatt Macy 			txg = spa->spa_uberblock.ub_txg;
1184eda14cbcSMatt Macy 		label = spa_config_generate(spa, vd, txg, B_FALSE);
1185eda14cbcSMatt Macy 
1186eda14cbcSMatt Macy 		/*
1187eda14cbcSMatt Macy 		 * Add our creation time.  This allows us to detect multiple
1188eda14cbcSMatt Macy 		 * vdev uses as described above, and automatically expires if we
1189eda14cbcSMatt Macy 		 * fail.
1190eda14cbcSMatt Macy 		 */
1191eda14cbcSMatt Macy 		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
1192eda14cbcSMatt Macy 		    crtxg) == 0);
1193eda14cbcSMatt Macy 	}
1194eda14cbcSMatt Macy 
1195eda14cbcSMatt Macy 	buf = vp->vp_nvlist;
1196eda14cbcSMatt Macy 	buflen = sizeof (vp->vp_nvlist);
1197eda14cbcSMatt Macy 
1198eda14cbcSMatt Macy 	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
1199eda14cbcSMatt Macy 	if (error != 0) {
1200eda14cbcSMatt Macy 		nvlist_free(label);
1201eda14cbcSMatt Macy 		abd_free(vp_abd);
1202eda14cbcSMatt Macy 		/* EFAULT means nvlist_pack ran out of room */
1203eda14cbcSMatt Macy 		return (SET_ERROR(error == EFAULT ? ENAMETOOLONG : EINVAL));
1204eda14cbcSMatt Macy 	}
1205eda14cbcSMatt Macy 
1206eda14cbcSMatt Macy 	/*
1207eda14cbcSMatt Macy 	 * Initialize uberblock template.
1208eda14cbcSMatt Macy 	 */
1209eda14cbcSMatt Macy 	ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
1210eda14cbcSMatt Macy 	abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
1211a2b560ccSMartin Matuska 	abd_zero_off(ub_abd, sizeof (uberblock_t),
1212a2b560ccSMartin Matuska 	    VDEV_UBERBLOCK_RING - sizeof (uberblock_t));
1213eda14cbcSMatt Macy 	ub = abd_to_buf(ub_abd);
1214eda14cbcSMatt Macy 	ub->ub_txg = 0;
1215eda14cbcSMatt Macy 
1216eda14cbcSMatt Macy 	/* Initialize the 2nd padding area. */
1217eda14cbcSMatt Macy 	bootenv = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1218eda14cbcSMatt Macy 	abd_zero(bootenv, VDEV_PAD_SIZE);
1219eda14cbcSMatt Macy 
1220eda14cbcSMatt Macy 	/*
1221eda14cbcSMatt Macy 	 * Write everything in parallel.
1222eda14cbcSMatt Macy 	 */
1223eda14cbcSMatt Macy retry:
1224eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, NULL, flags);
1225eda14cbcSMatt Macy 
1226eda14cbcSMatt Macy 	for (int l = 0; l < VDEV_LABELS; l++) {
1227eda14cbcSMatt Macy 
1228eda14cbcSMatt Macy 		vdev_label_write(zio, vd, l, vp_abd,
1229eda14cbcSMatt Macy 		    offsetof(vdev_label_t, vl_vdev_phys),
1230eda14cbcSMatt Macy 		    sizeof (vdev_phys_t), NULL, NULL, flags);
1231eda14cbcSMatt Macy 
1232eda14cbcSMatt Macy 		/*
1233eda14cbcSMatt Macy 		 * Skip the 1st padding area.
1234eda14cbcSMatt Macy 		 * Zero out the 2nd padding area where it might have
1235eda14cbcSMatt Macy 		 * left over data from previous filesystem format.
1236eda14cbcSMatt Macy 		 */
1237eda14cbcSMatt Macy 		vdev_label_write(zio, vd, l, bootenv,
1238eda14cbcSMatt Macy 		    offsetof(vdev_label_t, vl_be),
1239eda14cbcSMatt Macy 		    VDEV_PAD_SIZE, NULL, NULL, flags);
1240eda14cbcSMatt Macy 
1241eda14cbcSMatt Macy 		vdev_label_write(zio, vd, l, ub_abd,
1242eda14cbcSMatt Macy 		    offsetof(vdev_label_t, vl_uberblock),
1243eda14cbcSMatt Macy 		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
1244eda14cbcSMatt Macy 	}
1245eda14cbcSMatt Macy 
1246eda14cbcSMatt Macy 	error = zio_wait(zio);
1247eda14cbcSMatt Macy 
1248eda14cbcSMatt Macy 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
1249eda14cbcSMatt Macy 		flags |= ZIO_FLAG_TRYHARD;
1250eda14cbcSMatt Macy 		goto retry;
1251eda14cbcSMatt Macy 	}
1252eda14cbcSMatt Macy 
1253eda14cbcSMatt Macy 	nvlist_free(label);
1254eda14cbcSMatt Macy 	abd_free(bootenv);
1255eda14cbcSMatt Macy 	abd_free(ub_abd);
1256eda14cbcSMatt Macy 	abd_free(vp_abd);
1257eda14cbcSMatt Macy 
1258eda14cbcSMatt Macy 	/*
1259eda14cbcSMatt Macy 	 * If this vdev hasn't been previously identified as a spare, then we
1260eda14cbcSMatt Macy 	 * mark it as such only if a) we are labeling it as a spare, or b) it
1261eda14cbcSMatt Macy 	 * exists as a spare elsewhere in the system.  Do the same for
1262eda14cbcSMatt Macy 	 * level 2 ARC devices.
1263eda14cbcSMatt Macy 	 */
1264eda14cbcSMatt Macy 	if (error == 0 && !vd->vdev_isspare &&
1265eda14cbcSMatt Macy 	    (reason == VDEV_LABEL_SPARE ||
1266eda14cbcSMatt Macy 	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
1267eda14cbcSMatt Macy 		spa_spare_add(vd);
1268eda14cbcSMatt Macy 
1269eda14cbcSMatt Macy 	if (error == 0 && !vd->vdev_isl2cache &&
1270eda14cbcSMatt Macy 	    (reason == VDEV_LABEL_L2CACHE ||
1271eda14cbcSMatt Macy 	    spa_l2cache_exists(vd->vdev_guid, NULL)))
1272eda14cbcSMatt Macy 		spa_l2cache_add(vd);
1273eda14cbcSMatt Macy 
1274eda14cbcSMatt Macy 	return (error);
1275eda14cbcSMatt Macy }
1276eda14cbcSMatt Macy 
1277eda14cbcSMatt Macy /*
1278eda14cbcSMatt Macy  * Done callback for vdev_label_read_bootenv_impl. If this is the first
1279eda14cbcSMatt Macy  * callback to finish, store our abd in the callback pointer. Otherwise, we
1280eda14cbcSMatt Macy  * just free our abd and return.
1281eda14cbcSMatt Macy  */
1282eda14cbcSMatt Macy static void
1283eda14cbcSMatt Macy vdev_label_read_bootenv_done(zio_t *zio)
1284eda14cbcSMatt Macy {
1285eda14cbcSMatt Macy 	zio_t *rio = zio->io_private;
1286eda14cbcSMatt Macy 	abd_t **cbp = rio->io_private;
1287eda14cbcSMatt Macy 
1288eda14cbcSMatt Macy 	ASSERT3U(zio->io_size, ==, VDEV_PAD_SIZE);
1289eda14cbcSMatt Macy 
1290eda14cbcSMatt Macy 	if (zio->io_error == 0) {
1291eda14cbcSMatt Macy 		mutex_enter(&rio->io_lock);
1292eda14cbcSMatt Macy 		if (*cbp == NULL) {
1293eda14cbcSMatt Macy 			/* Will free this buffer in vdev_label_read_bootenv. */
1294eda14cbcSMatt Macy 			*cbp = zio->io_abd;
1295eda14cbcSMatt Macy 		} else {
1296eda14cbcSMatt Macy 			abd_free(zio->io_abd);
1297eda14cbcSMatt Macy 		}
1298eda14cbcSMatt Macy 		mutex_exit(&rio->io_lock);
1299eda14cbcSMatt Macy 	} else {
1300eda14cbcSMatt Macy 		abd_free(zio->io_abd);
1301eda14cbcSMatt Macy 	}
1302eda14cbcSMatt Macy }
1303eda14cbcSMatt Macy 
1304eda14cbcSMatt Macy static void
1305eda14cbcSMatt Macy vdev_label_read_bootenv_impl(zio_t *zio, vdev_t *vd, int flags)
1306eda14cbcSMatt Macy {
1307eda14cbcSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++)
1308eda14cbcSMatt Macy 		vdev_label_read_bootenv_impl(zio, vd->vdev_child[c], flags);
1309eda14cbcSMatt Macy 
1310eda14cbcSMatt Macy 	/*
1311eda14cbcSMatt Macy 	 * We just use the first label that has a correct checksum; the
1312eda14cbcSMatt Macy 	 * bootloader should have rewritten them all to be the same on boot,
1313eda14cbcSMatt Macy 	 * and any changes we made since boot have been the same across all
1314eda14cbcSMatt Macy 	 * labels.
1315eda14cbcSMatt Macy 	 */
1316eda14cbcSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
13172c48331dSMatt Macy 		for (int l = 0; l < VDEV_LABELS; l++) {
1318eda14cbcSMatt Macy 			vdev_label_read(zio, vd, l,
1319eda14cbcSMatt Macy 			    abd_alloc_linear(VDEV_PAD_SIZE, B_FALSE),
1320eda14cbcSMatt Macy 			    offsetof(vdev_label_t, vl_be), VDEV_PAD_SIZE,
1321eda14cbcSMatt Macy 			    vdev_label_read_bootenv_done, zio, flags);
1322eda14cbcSMatt Macy 		}
1323eda14cbcSMatt Macy 	}
1324eda14cbcSMatt Macy }
1325eda14cbcSMatt Macy 
1326eda14cbcSMatt Macy int
13272c48331dSMatt Macy vdev_label_read_bootenv(vdev_t *rvd, nvlist_t *bootenv)
1328eda14cbcSMatt Macy {
13292c48331dSMatt Macy 	nvlist_t *config;
1330eda14cbcSMatt Macy 	spa_t *spa = rvd->vdev_spa;
1331eda14cbcSMatt Macy 	abd_t *abd = NULL;
1332eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1333eda14cbcSMatt Macy 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1334eda14cbcSMatt Macy 
13352c48331dSMatt Macy 	ASSERT(bootenv);
1336eda14cbcSMatt Macy 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1337eda14cbcSMatt Macy 
1338eda14cbcSMatt Macy 	zio_t *zio = zio_root(spa, NULL, &abd, flags);
1339eda14cbcSMatt Macy 	vdev_label_read_bootenv_impl(zio, rvd, flags);
1340eda14cbcSMatt Macy 	int err = zio_wait(zio);
1341eda14cbcSMatt Macy 
1342eda14cbcSMatt Macy 	if (abd != NULL) {
13432c48331dSMatt Macy 		char *buf;
1344eda14cbcSMatt Macy 		vdev_boot_envblock_t *vbe = abd_to_buf(abd);
13452c48331dSMatt Macy 
13462c48331dSMatt Macy 		vbe->vbe_version = ntohll(vbe->vbe_version);
13472c48331dSMatt Macy 		switch (vbe->vbe_version) {
13482c48331dSMatt Macy 		case VB_RAW:
13492c48331dSMatt Macy 			/*
13502c48331dSMatt Macy 			 * if we have textual data in vbe_bootenv, create nvlist
13512c48331dSMatt Macy 			 * with key "envmap".
13522c48331dSMatt Macy 			 */
13532c48331dSMatt Macy 			fnvlist_add_uint64(bootenv, BOOTENV_VERSION, VB_RAW);
1354eda14cbcSMatt Macy 			vbe->vbe_bootenv[sizeof (vbe->vbe_bootenv) - 1] = '\0';
13552c48331dSMatt Macy 			fnvlist_add_string(bootenv, GRUB_ENVMAP,
13562c48331dSMatt Macy 			    vbe->vbe_bootenv);
13572c48331dSMatt Macy 			break;
13582c48331dSMatt Macy 
13592c48331dSMatt Macy 		case VB_NVLIST:
13602c48331dSMatt Macy 			err = nvlist_unpack(vbe->vbe_bootenv,
13612c48331dSMatt Macy 			    sizeof (vbe->vbe_bootenv), &config, 0);
13622c48331dSMatt Macy 			if (err == 0) {
13632c48331dSMatt Macy 				fnvlist_merge(bootenv, config);
13642c48331dSMatt Macy 				nvlist_free(config);
13652c48331dSMatt Macy 				break;
13662c48331dSMatt Macy 			}
1367c03c5b1cSMartin Matuska 			zfs_fallthrough;
13682c48331dSMatt Macy 		default:
13692c48331dSMatt Macy 			/* Check for FreeBSD zfs bootonce command string */
13702c48331dSMatt Macy 			buf = abd_to_buf(abd);
13712c48331dSMatt Macy 			if (*buf == '\0') {
13722c48331dSMatt Macy 				fnvlist_add_uint64(bootenv, BOOTENV_VERSION,
13732c48331dSMatt Macy 				    VB_NVLIST);
13742c48331dSMatt Macy 				break;
13752c48331dSMatt Macy 			}
13762c48331dSMatt Macy 			fnvlist_add_string(bootenv, FREEBSD_BOOTONCE, buf);
13772c48331dSMatt Macy 		}
13782c48331dSMatt Macy 
13792c48331dSMatt Macy 		/*
13802c48331dSMatt Macy 		 * abd was allocated in vdev_label_read_bootenv_impl()
13812c48331dSMatt Macy 		 */
1382eda14cbcSMatt Macy 		abd_free(abd);
13832c48331dSMatt Macy 		/*
13842c48331dSMatt Macy 		 * If we managed to read any successfully,
13852c48331dSMatt Macy 		 * return success.
13862c48331dSMatt Macy 		 */
1387eda14cbcSMatt Macy 		return (0);
1388eda14cbcSMatt Macy 	}
1389eda14cbcSMatt Macy 	return (err);
1390eda14cbcSMatt Macy }
1391eda14cbcSMatt Macy 
1392eda14cbcSMatt Macy int
13932c48331dSMatt Macy vdev_label_write_bootenv(vdev_t *vd, nvlist_t *env)
1394eda14cbcSMatt Macy {
1395eda14cbcSMatt Macy 	zio_t *zio;
1396eda14cbcSMatt Macy 	spa_t *spa = vd->vdev_spa;
1397eda14cbcSMatt Macy 	vdev_boot_envblock_t *bootenv;
1398eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
13992c48331dSMatt Macy 	int error;
14002c48331dSMatt Macy 	size_t nvsize;
14012c48331dSMatt Macy 	char *nvbuf;
14022a58b312SMartin Matuska 	const char *tmp;
1403eda14cbcSMatt Macy 
14042c48331dSMatt Macy 	error = nvlist_size(env, &nvsize, NV_ENCODE_XDR);
14052c48331dSMatt Macy 	if (error != 0)
14062c48331dSMatt Macy 		return (SET_ERROR(error));
14072c48331dSMatt Macy 
14082c48331dSMatt Macy 	if (nvsize >= sizeof (bootenv->vbe_bootenv)) {
1409eda14cbcSMatt Macy 		return (SET_ERROR(E2BIG));
1410eda14cbcSMatt Macy 	}
1411eda14cbcSMatt Macy 
1412eda14cbcSMatt Macy 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1413eda14cbcSMatt Macy 
14142c48331dSMatt Macy 	error = ENXIO;
1415eda14cbcSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
14162c48331dSMatt Macy 		int child_err;
14172c48331dSMatt Macy 
14182c48331dSMatt Macy 		child_err = vdev_label_write_bootenv(vd->vdev_child[c], env);
1419eda14cbcSMatt Macy 		/*
1420eda14cbcSMatt Macy 		 * As long as any of the disks managed to write all of their
1421eda14cbcSMatt Macy 		 * labels successfully, return success.
1422eda14cbcSMatt Macy 		 */
1423eda14cbcSMatt Macy 		if (child_err == 0)
1424eda14cbcSMatt Macy 			error = child_err;
1425eda14cbcSMatt Macy 	}
1426eda14cbcSMatt Macy 
1427eda14cbcSMatt Macy 	if (!vd->vdev_ops->vdev_op_leaf || vdev_is_dead(vd) ||
1428eda14cbcSMatt Macy 	    !vdev_writeable(vd)) {
1429eda14cbcSMatt Macy 		return (error);
1430eda14cbcSMatt Macy 	}
1431eda14cbcSMatt Macy 	ASSERT3U(sizeof (*bootenv), ==, VDEV_PAD_SIZE);
1432eda14cbcSMatt Macy 	abd_t *abd = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1433eda14cbcSMatt Macy 	abd_zero(abd, VDEV_PAD_SIZE);
1434eda14cbcSMatt Macy 
14352c48331dSMatt Macy 	bootenv = abd_borrow_buf_copy(abd, VDEV_PAD_SIZE);
14362c48331dSMatt Macy 	nvbuf = bootenv->vbe_bootenv;
14372c48331dSMatt Macy 	nvsize = sizeof (bootenv->vbe_bootenv);
14382c48331dSMatt Macy 
14392c48331dSMatt Macy 	bootenv->vbe_version = fnvlist_lookup_uint64(env, BOOTENV_VERSION);
14402c48331dSMatt Macy 	switch (bootenv->vbe_version) {
14412c48331dSMatt Macy 	case VB_RAW:
14422a58b312SMartin Matuska 		if (nvlist_lookup_string(env, GRUB_ENVMAP, &tmp) == 0) {
14432a58b312SMartin Matuska 			(void) strlcpy(bootenv->vbe_bootenv, tmp, nvsize);
14442c48331dSMatt Macy 		}
14452c48331dSMatt Macy 		error = 0;
14462c48331dSMatt Macy 		break;
14472c48331dSMatt Macy 
14482c48331dSMatt Macy 	case VB_NVLIST:
14492c48331dSMatt Macy 		error = nvlist_pack(env, &nvbuf, &nvsize, NV_ENCODE_XDR,
14502c48331dSMatt Macy 		    KM_SLEEP);
14512c48331dSMatt Macy 		break;
14522c48331dSMatt Macy 
14532c48331dSMatt Macy 	default:
14542c48331dSMatt Macy 		error = EINVAL;
14552c48331dSMatt Macy 		break;
14562c48331dSMatt Macy 	}
14572c48331dSMatt Macy 
14582c48331dSMatt Macy 	if (error == 0) {
14592c48331dSMatt Macy 		bootenv->vbe_version = htonll(bootenv->vbe_version);
1460eda14cbcSMatt Macy 		abd_return_buf_copy(abd, bootenv, VDEV_PAD_SIZE);
14612c48331dSMatt Macy 	} else {
14622c48331dSMatt Macy 		abd_free(abd);
14632c48331dSMatt Macy 		return (SET_ERROR(error));
14642c48331dSMatt Macy 	}
1465eda14cbcSMatt Macy 
1466eda14cbcSMatt Macy retry:
1467eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, NULL, flags);
14682c48331dSMatt Macy 	for (int l = 0; l < VDEV_LABELS; l++) {
1469eda14cbcSMatt Macy 		vdev_label_write(zio, vd, l, abd,
1470eda14cbcSMatt Macy 		    offsetof(vdev_label_t, vl_be),
1471eda14cbcSMatt Macy 		    VDEV_PAD_SIZE, NULL, NULL, flags);
1472eda14cbcSMatt Macy 	}
1473eda14cbcSMatt Macy 
1474eda14cbcSMatt Macy 	error = zio_wait(zio);
1475eda14cbcSMatt Macy 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
1476eda14cbcSMatt Macy 		flags |= ZIO_FLAG_TRYHARD;
1477eda14cbcSMatt Macy 		goto retry;
1478eda14cbcSMatt Macy 	}
1479eda14cbcSMatt Macy 
1480eda14cbcSMatt Macy 	abd_free(abd);
1481eda14cbcSMatt Macy 	return (error);
1482eda14cbcSMatt Macy }
1483eda14cbcSMatt Macy 
1484eda14cbcSMatt Macy /*
1485eda14cbcSMatt Macy  * ==========================================================================
1486eda14cbcSMatt Macy  * uberblock load/sync
1487eda14cbcSMatt Macy  * ==========================================================================
1488eda14cbcSMatt Macy  */
1489eda14cbcSMatt Macy 
1490eda14cbcSMatt Macy /*
1491eda14cbcSMatt Macy  * Consider the following situation: txg is safely synced to disk.  We've
1492eda14cbcSMatt Macy  * written the first uberblock for txg + 1, and then we lose power.  When we
1493eda14cbcSMatt Macy  * come back up, we fail to see the uberblock for txg + 1 because, say,
1494eda14cbcSMatt Macy  * it was on a mirrored device and the replica to which we wrote txg + 1
1495eda14cbcSMatt Macy  * is now offline.  If we then make some changes and sync txg + 1, and then
1496eda14cbcSMatt Macy  * the missing replica comes back, then for a few seconds we'll have two
1497eda14cbcSMatt Macy  * conflicting uberblocks on disk with the same txg.  The solution is simple:
1498eda14cbcSMatt Macy  * among uberblocks with equal txg, choose the one with the latest timestamp.
1499eda14cbcSMatt Macy  */
1500eda14cbcSMatt Macy static int
1501eda14cbcSMatt Macy vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1502eda14cbcSMatt Macy {
1503eda14cbcSMatt Macy 	int cmp = TREE_CMP(ub1->ub_txg, ub2->ub_txg);
1504eda14cbcSMatt Macy 
1505eda14cbcSMatt Macy 	if (likely(cmp))
1506eda14cbcSMatt Macy 		return (cmp);
1507eda14cbcSMatt Macy 
1508eda14cbcSMatt Macy 	cmp = TREE_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1509eda14cbcSMatt Macy 	if (likely(cmp))
1510eda14cbcSMatt Macy 		return (cmp);
1511eda14cbcSMatt Macy 
1512eda14cbcSMatt Macy 	/*
1513eda14cbcSMatt Macy 	 * If MMP_VALID(ub) && MMP_SEQ_VALID(ub) then the host has an MMP-aware
1514180f8225SMatt Macy 	 * ZFS, e.g. OpenZFS >= 0.7.
1515eda14cbcSMatt Macy 	 *
1516eda14cbcSMatt Macy 	 * If one ub has MMP and the other does not, they were written by
1517eda14cbcSMatt Macy 	 * different hosts, which matters for MMP.  So we treat no MMP/no SEQ as
1518eda14cbcSMatt Macy 	 * a 0 value.
1519eda14cbcSMatt Macy 	 *
1520eda14cbcSMatt Macy 	 * Since timestamp and txg are the same if we get this far, either is
1521eda14cbcSMatt Macy 	 * acceptable for importing the pool.
1522eda14cbcSMatt Macy 	 */
1523eda14cbcSMatt Macy 	unsigned int seq1 = 0;
1524eda14cbcSMatt Macy 	unsigned int seq2 = 0;
1525eda14cbcSMatt Macy 
1526eda14cbcSMatt Macy 	if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1527eda14cbcSMatt Macy 		seq1 = MMP_SEQ(ub1);
1528eda14cbcSMatt Macy 
1529eda14cbcSMatt Macy 	if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1530eda14cbcSMatt Macy 		seq2 = MMP_SEQ(ub2);
1531eda14cbcSMatt Macy 
1532eda14cbcSMatt Macy 	return (TREE_CMP(seq1, seq2));
1533eda14cbcSMatt Macy }
1534eda14cbcSMatt Macy 
1535eda14cbcSMatt Macy struct ubl_cbdata {
1536e716630dSMartin Matuska 	uberblock_t	ubl_latest;	/* Most recent uberblock */
1537e716630dSMartin Matuska 	uberblock_t	*ubl_ubbest;	/* Best uberblock (w/r/t max_txg) */
1538eda14cbcSMatt Macy 	vdev_t		*ubl_vd;	/* vdev associated with the above */
1539eda14cbcSMatt Macy };
1540eda14cbcSMatt Macy 
1541eda14cbcSMatt Macy static void
1542eda14cbcSMatt Macy vdev_uberblock_load_done(zio_t *zio)
1543eda14cbcSMatt Macy {
1544eda14cbcSMatt Macy 	vdev_t *vd = zio->io_vd;
1545eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
1546eda14cbcSMatt Macy 	zio_t *rio = zio->io_private;
1547eda14cbcSMatt Macy 	uberblock_t *ub = abd_to_buf(zio->io_abd);
1548eda14cbcSMatt Macy 	struct ubl_cbdata *cbp = rio->io_private;
1549eda14cbcSMatt Macy 
1550eda14cbcSMatt Macy 	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1551eda14cbcSMatt Macy 
1552eda14cbcSMatt Macy 	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1553eda14cbcSMatt Macy 		mutex_enter(&rio->io_lock);
1554e716630dSMartin Matuska 		if (vdev_uberblock_compare(ub, &cbp->ubl_latest) > 0) {
1555e716630dSMartin Matuska 			cbp->ubl_latest = *ub;
1556e716630dSMartin Matuska 		}
1557eda14cbcSMatt Macy 		if (ub->ub_txg <= spa->spa_load_max_txg &&
1558eda14cbcSMatt Macy 		    vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1559eda14cbcSMatt Macy 			/*
1560eda14cbcSMatt Macy 			 * Keep track of the vdev in which this uberblock
1561eda14cbcSMatt Macy 			 * was found. We will use this information later
1562eda14cbcSMatt Macy 			 * to obtain the config nvlist associated with
1563eda14cbcSMatt Macy 			 * this uberblock.
1564eda14cbcSMatt Macy 			 */
1565eda14cbcSMatt Macy 			*cbp->ubl_ubbest = *ub;
1566eda14cbcSMatt Macy 			cbp->ubl_vd = vd;
1567eda14cbcSMatt Macy 		}
1568eda14cbcSMatt Macy 		mutex_exit(&rio->io_lock);
1569eda14cbcSMatt Macy 	}
1570eda14cbcSMatt Macy 
1571eda14cbcSMatt Macy 	abd_free(zio->io_abd);
1572eda14cbcSMatt Macy }
1573eda14cbcSMatt Macy 
1574eda14cbcSMatt Macy static void
1575eda14cbcSMatt Macy vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1576eda14cbcSMatt Macy     struct ubl_cbdata *cbp)
1577eda14cbcSMatt Macy {
1578eda14cbcSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++)
1579eda14cbcSMatt Macy 		vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1580eda14cbcSMatt Macy 
15817877fdebSMatt Macy 	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd) &&
15827877fdebSMatt Macy 	    vd->vdev_ops != &vdev_draid_spare_ops) {
1583eda14cbcSMatt Macy 		for (int l = 0; l < VDEV_LABELS; l++) {
1584eda14cbcSMatt Macy 			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1585eda14cbcSMatt Macy 				vdev_label_read(zio, vd, l,
1586eda14cbcSMatt Macy 				    abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1587eda14cbcSMatt Macy 				    B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1588eda14cbcSMatt Macy 				    VDEV_UBERBLOCK_SIZE(vd),
1589eda14cbcSMatt Macy 				    vdev_uberblock_load_done, zio, flags);
1590eda14cbcSMatt Macy 			}
1591eda14cbcSMatt Macy 		}
1592eda14cbcSMatt Macy 	}
1593eda14cbcSMatt Macy }
1594eda14cbcSMatt Macy 
1595eda14cbcSMatt Macy /*
1596eda14cbcSMatt Macy  * Reads the 'best' uberblock from disk along with its associated
1597eda14cbcSMatt Macy  * configuration. First, we read the uberblock array of each label of each
1598eda14cbcSMatt Macy  * vdev, keeping track of the uberblock with the highest txg in each array.
1599eda14cbcSMatt Macy  * Then, we read the configuration from the same vdev as the best uberblock.
1600eda14cbcSMatt Macy  */
1601eda14cbcSMatt Macy void
1602eda14cbcSMatt Macy vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1603eda14cbcSMatt Macy {
1604eda14cbcSMatt Macy 	zio_t *zio;
1605eda14cbcSMatt Macy 	spa_t *spa = rvd->vdev_spa;
1606eda14cbcSMatt Macy 	struct ubl_cbdata cb;
1607eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1608eda14cbcSMatt Macy 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1609eda14cbcSMatt Macy 
1610eda14cbcSMatt Macy 	ASSERT(ub);
1611eda14cbcSMatt Macy 	ASSERT(config);
1612eda14cbcSMatt Macy 
1613da5137abSMartin Matuska 	memset(ub, 0, sizeof (uberblock_t));
1614e716630dSMartin Matuska 	memset(&cb, 0, sizeof (cb));
1615eda14cbcSMatt Macy 	*config = NULL;
1616eda14cbcSMatt Macy 
1617eda14cbcSMatt Macy 	cb.ubl_ubbest = ub;
1618eda14cbcSMatt Macy 
1619eda14cbcSMatt Macy 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1620eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, &cb, flags);
1621eda14cbcSMatt Macy 	vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1622eda14cbcSMatt Macy 	(void) zio_wait(zio);
1623eda14cbcSMatt Macy 
1624eda14cbcSMatt Macy 	/*
1625eda14cbcSMatt Macy 	 * It's possible that the best uberblock was discovered on a label
1626eda14cbcSMatt Macy 	 * that has a configuration which was written in a future txg.
1627eda14cbcSMatt Macy 	 * Search all labels on this vdev to find the configuration that
1628eda14cbcSMatt Macy 	 * matches the txg for our uberblock.
1629eda14cbcSMatt Macy 	 */
1630eda14cbcSMatt Macy 	if (cb.ubl_vd != NULL) {
1631eda14cbcSMatt Macy 		vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
1632eda14cbcSMatt Macy 		    "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
1633eda14cbcSMatt Macy 
1634e716630dSMartin Matuska 		if (ub->ub_raidz_reflow_info !=
1635e716630dSMartin Matuska 		    cb.ubl_latest.ub_raidz_reflow_info) {
1636e716630dSMartin Matuska 			vdev_dbgmsg(cb.ubl_vd,
1637e716630dSMartin Matuska 			    "spa=%s best uberblock (txg=%llu info=0x%llx) "
1638e716630dSMartin Matuska 			    "has different raidz_reflow_info than latest "
1639e716630dSMartin Matuska 			    "uberblock (txg=%llu info=0x%llx)",
1640e716630dSMartin Matuska 			    spa->spa_name,
1641e716630dSMartin Matuska 			    (u_longlong_t)ub->ub_txg,
1642e716630dSMartin Matuska 			    (u_longlong_t)ub->ub_raidz_reflow_info,
1643e716630dSMartin Matuska 			    (u_longlong_t)cb.ubl_latest.ub_txg,
1644e716630dSMartin Matuska 			    (u_longlong_t)cb.ubl_latest.ub_raidz_reflow_info);
1645e716630dSMartin Matuska 			memset(ub, 0, sizeof (uberblock_t));
1646e716630dSMartin Matuska 			spa_config_exit(spa, SCL_ALL, FTAG);
1647e716630dSMartin Matuska 			return;
1648e716630dSMartin Matuska 		}
1649e716630dSMartin Matuska 
1650eda14cbcSMatt Macy 		*config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
1651eda14cbcSMatt Macy 		if (*config == NULL && spa->spa_extreme_rewind) {
1652eda14cbcSMatt Macy 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
1653eda14cbcSMatt Macy 			    "Trying again without txg restrictions.");
1654eda14cbcSMatt Macy 			*config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
1655eda14cbcSMatt Macy 		}
1656eda14cbcSMatt Macy 		if (*config == NULL) {
1657eda14cbcSMatt Macy 			vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
1658eda14cbcSMatt Macy 		}
1659eda14cbcSMatt Macy 	}
1660eda14cbcSMatt Macy 	spa_config_exit(spa, SCL_ALL, FTAG);
1661eda14cbcSMatt Macy }
1662eda14cbcSMatt Macy 
1663eda14cbcSMatt Macy /*
1664eda14cbcSMatt Macy  * For use when a leaf vdev is expanded.
1665eda14cbcSMatt Macy  * The location of labels 2 and 3 changed, and at the new location the
1666eda14cbcSMatt Macy  * uberblock rings are either empty or contain garbage.  The sync will write
1667eda14cbcSMatt Macy  * new configs there because the vdev is dirty, but expansion also needs the
1668eda14cbcSMatt Macy  * uberblock rings copied.  Read them from label 0 which did not move.
1669eda14cbcSMatt Macy  *
1670eda14cbcSMatt Macy  * Since the point is to populate labels {2,3} with valid uberblocks,
1671eda14cbcSMatt Macy  * we zero uberblocks we fail to read or which are not valid.
1672eda14cbcSMatt Macy  */
1673eda14cbcSMatt Macy 
1674eda14cbcSMatt Macy static void
1675eda14cbcSMatt Macy vdev_copy_uberblocks(vdev_t *vd)
1676eda14cbcSMatt Macy {
1677eda14cbcSMatt Macy 	abd_t *ub_abd;
1678eda14cbcSMatt Macy 	zio_t *write_zio;
1679eda14cbcSMatt Macy 	int locks = (SCL_L2ARC | SCL_ZIO);
1680eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1681eda14cbcSMatt Macy 	    ZIO_FLAG_SPECULATIVE;
1682eda14cbcSMatt Macy 
1683eda14cbcSMatt Macy 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_READER) ==
1684eda14cbcSMatt Macy 	    SCL_STATE);
1685eda14cbcSMatt Macy 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1686eda14cbcSMatt Macy 
16877877fdebSMatt Macy 	/*
16887877fdebSMatt Macy 	 * No uberblocks are stored on distributed spares, they may be
16897877fdebSMatt Macy 	 * safely skipped when expanding a leaf vdev.
16907877fdebSMatt Macy 	 */
16917877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops)
16927877fdebSMatt Macy 		return;
16937877fdebSMatt Macy 
1694eda14cbcSMatt Macy 	spa_config_enter(vd->vdev_spa, locks, FTAG, RW_READER);
1695eda14cbcSMatt Macy 
1696eda14cbcSMatt Macy 	ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1697eda14cbcSMatt Macy 
1698eda14cbcSMatt Macy 	write_zio = zio_root(vd->vdev_spa, NULL, NULL, flags);
1699eda14cbcSMatt Macy 	for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1700eda14cbcSMatt Macy 		const int src_label = 0;
1701eda14cbcSMatt Macy 		zio_t *zio;
1702eda14cbcSMatt Macy 
1703eda14cbcSMatt Macy 		zio = zio_root(vd->vdev_spa, NULL, NULL, flags);
1704eda14cbcSMatt Macy 		vdev_label_read(zio, vd, src_label, ub_abd,
1705eda14cbcSMatt Macy 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1706eda14cbcSMatt Macy 		    NULL, NULL, flags);
1707eda14cbcSMatt Macy 
1708eda14cbcSMatt Macy 		if (zio_wait(zio) || uberblock_verify(abd_to_buf(ub_abd)))
1709eda14cbcSMatt Macy 			abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1710eda14cbcSMatt Macy 
1711eda14cbcSMatt Macy 		for (int l = 2; l < VDEV_LABELS; l++)
1712eda14cbcSMatt Macy 			vdev_label_write(write_zio, vd, l, ub_abd,
1713eda14cbcSMatt Macy 			    VDEV_UBERBLOCK_OFFSET(vd, n),
1714eda14cbcSMatt Macy 			    VDEV_UBERBLOCK_SIZE(vd), NULL, NULL,
1715eda14cbcSMatt Macy 			    flags | ZIO_FLAG_DONT_PROPAGATE);
1716eda14cbcSMatt Macy 	}
1717eda14cbcSMatt Macy 	(void) zio_wait(write_zio);
1718eda14cbcSMatt Macy 
1719eda14cbcSMatt Macy 	spa_config_exit(vd->vdev_spa, locks, FTAG);
1720eda14cbcSMatt Macy 
1721eda14cbcSMatt Macy 	abd_free(ub_abd);
1722eda14cbcSMatt Macy }
1723eda14cbcSMatt Macy 
1724eda14cbcSMatt Macy /*
1725eda14cbcSMatt Macy  * On success, increment root zio's count of good writes.
1726eda14cbcSMatt Macy  * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1727eda14cbcSMatt Macy  */
1728eda14cbcSMatt Macy static void
1729eda14cbcSMatt Macy vdev_uberblock_sync_done(zio_t *zio)
1730eda14cbcSMatt Macy {
1731eda14cbcSMatt Macy 	uint64_t *good_writes = zio->io_private;
1732eda14cbcSMatt Macy 
1733eda14cbcSMatt Macy 	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
1734eda14cbcSMatt Macy 		atomic_inc_64(good_writes);
1735eda14cbcSMatt Macy }
1736eda14cbcSMatt Macy 
1737eda14cbcSMatt Macy /*
1738eda14cbcSMatt Macy  * Write the uberblock to all labels of all leaves of the specified vdev.
1739eda14cbcSMatt Macy  */
1740eda14cbcSMatt Macy static void
1741eda14cbcSMatt Macy vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1742eda14cbcSMatt Macy     uberblock_t *ub, vdev_t *vd, int flags)
1743eda14cbcSMatt Macy {
1744eda14cbcSMatt Macy 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
1745eda14cbcSMatt Macy 		vdev_uberblock_sync(zio, good_writes,
1746eda14cbcSMatt Macy 		    ub, vd->vdev_child[c], flags);
1747eda14cbcSMatt Macy 	}
1748eda14cbcSMatt Macy 
1749eda14cbcSMatt Macy 	if (!vd->vdev_ops->vdev_op_leaf)
1750eda14cbcSMatt Macy 		return;
1751eda14cbcSMatt Macy 
1752eda14cbcSMatt Macy 	if (!vdev_writeable(vd))
1753eda14cbcSMatt Macy 		return;
1754eda14cbcSMatt Macy 
17557877fdebSMatt Macy 	/*
17567877fdebSMatt Macy 	 * There's no need to write uberblocks to a distributed spare, they
17577877fdebSMatt Macy 	 * are already stored on all the leaves of the parent dRAID.  For
17587877fdebSMatt Macy 	 * this same reason vdev_uberblock_load_impl() skips distributed
17597877fdebSMatt Macy 	 * spares when reading uberblocks.
17607877fdebSMatt Macy 	 */
17617877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops)
17627877fdebSMatt Macy 		return;
17637877fdebSMatt Macy 
1764eda14cbcSMatt Macy 	/* If the vdev was expanded, need to copy uberblock rings. */
1765eda14cbcSMatt Macy 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1766eda14cbcSMatt Macy 	    vd->vdev_copy_uberblocks == B_TRUE) {
1767eda14cbcSMatt Macy 		vdev_copy_uberblocks(vd);
1768eda14cbcSMatt Macy 		vd->vdev_copy_uberblocks = B_FALSE;
1769eda14cbcSMatt Macy 	}
1770eda14cbcSMatt Macy 
1771e716630dSMartin Matuska 	/*
1772e716630dSMartin Matuska 	 * We chose a slot based on the txg.  If this uberblock has a special
1773e716630dSMartin Matuska 	 * RAIDZ expansion state, then it is essentially an update of the
1774e716630dSMartin Matuska 	 * current uberblock (it has the same txg).  However, the current
1775e716630dSMartin Matuska 	 * state is committed, so we want to write it to a different slot. If
1776e716630dSMartin Matuska 	 * we overwrote the same slot, and we lose power during the uberblock
1777e716630dSMartin Matuska 	 * write, and the disk does not do single-sector overwrites
1778e716630dSMartin Matuska 	 * atomically (even though it is required to - i.e. we should see
1779e716630dSMartin Matuska 	 * either the old or the new uberblock), then we could lose this
1780e716630dSMartin Matuska 	 * txg's uberblock. Rewinding to the previous txg's uberblock may not
1781e716630dSMartin Matuska 	 * be possible because RAIDZ expansion may have already overwritten
1782e716630dSMartin Matuska 	 * some of the data, so we need the progress indicator in the
1783e716630dSMartin Matuska 	 * uberblock.
1784e716630dSMartin Matuska 	 */
1785eda14cbcSMatt Macy 	int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1786e716630dSMartin Matuska 	int n = (ub->ub_txg - (RRSS_GET_STATE(ub) == RRSS_SCRATCH_VALID)) %
1787e716630dSMartin Matuska 	    (VDEV_UBERBLOCK_COUNT(vd) - m);
1788eda14cbcSMatt Macy 
1789eda14cbcSMatt Macy 	/* Copy the uberblock_t into the ABD */
1790eda14cbcSMatt Macy 	abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1791eda14cbcSMatt Macy 	abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1792a2b560ccSMartin Matuska 	abd_zero_off(ub_abd, sizeof (uberblock_t),
1793a2b560ccSMartin Matuska 	    VDEV_UBERBLOCK_SIZE(vd) - sizeof (uberblock_t));
1794eda14cbcSMatt Macy 
1795eda14cbcSMatt Macy 	for (int l = 0; l < VDEV_LABELS; l++)
1796eda14cbcSMatt Macy 		vdev_label_write(zio, vd, l, ub_abd,
1797eda14cbcSMatt Macy 		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1798eda14cbcSMatt Macy 		    vdev_uberblock_sync_done, good_writes,
1799eda14cbcSMatt Macy 		    flags | ZIO_FLAG_DONT_PROPAGATE);
1800eda14cbcSMatt Macy 
1801eda14cbcSMatt Macy 	abd_free(ub_abd);
1802eda14cbcSMatt Macy }
1803eda14cbcSMatt Macy 
1804eda14cbcSMatt Macy /* Sync the uberblocks to all vdevs in svd[] */
1805e716630dSMartin Matuska int
1806eda14cbcSMatt Macy vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1807eda14cbcSMatt Macy {
1808eda14cbcSMatt Macy 	spa_t *spa = svd[0]->vdev_spa;
1809eda14cbcSMatt Macy 	zio_t *zio;
1810eda14cbcSMatt Macy 	uint64_t good_writes = 0;
1811eda14cbcSMatt Macy 
1812eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, NULL, flags);
1813eda14cbcSMatt Macy 
1814eda14cbcSMatt Macy 	for (int v = 0; v < svdcount; v++)
1815eda14cbcSMatt Macy 		vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1816eda14cbcSMatt Macy 
181778ae60b4SMartin Matuska 	if (spa->spa_aux_sync_uber) {
181878ae60b4SMartin Matuska 		for (int v = 0; v < spa->spa_spares.sav_count; v++) {
181978ae60b4SMartin Matuska 			vdev_uberblock_sync(zio, &good_writes, ub,
182078ae60b4SMartin Matuska 			    spa->spa_spares.sav_vdevs[v], flags);
182178ae60b4SMartin Matuska 		}
182278ae60b4SMartin Matuska 		for (int v = 0; v < spa->spa_l2cache.sav_count; v++) {
182378ae60b4SMartin Matuska 			vdev_uberblock_sync(zio, &good_writes, ub,
182478ae60b4SMartin Matuska 			    spa->spa_l2cache.sav_vdevs[v], flags);
182578ae60b4SMartin Matuska 		}
182678ae60b4SMartin Matuska 	}
1827eda14cbcSMatt Macy 	(void) zio_wait(zio);
1828eda14cbcSMatt Macy 
1829eda14cbcSMatt Macy 	/*
1830eda14cbcSMatt Macy 	 * Flush the uberblocks to disk.  This ensures that the odd labels
1831eda14cbcSMatt Macy 	 * are no longer needed (because the new uberblocks and the even
1832eda14cbcSMatt Macy 	 * labels are safely on disk), so it is safe to overwrite them.
1833eda14cbcSMatt Macy 	 */
1834eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, NULL, flags);
1835eda14cbcSMatt Macy 
1836eda14cbcSMatt Macy 	for (int v = 0; v < svdcount; v++) {
1837eda14cbcSMatt Macy 		if (vdev_writeable(svd[v])) {
1838eda14cbcSMatt Macy 			zio_flush(zio, svd[v]);
1839eda14cbcSMatt Macy 		}
1840eda14cbcSMatt Macy 	}
184178ae60b4SMartin Matuska 	if (spa->spa_aux_sync_uber) {
184278ae60b4SMartin Matuska 		spa->spa_aux_sync_uber = B_FALSE;
184378ae60b4SMartin Matuska 		for (int v = 0; v < spa->spa_spares.sav_count; v++) {
184478ae60b4SMartin Matuska 			if (vdev_writeable(spa->spa_spares.sav_vdevs[v])) {
184578ae60b4SMartin Matuska 				zio_flush(zio, spa->spa_spares.sav_vdevs[v]);
184678ae60b4SMartin Matuska 			}
184778ae60b4SMartin Matuska 		}
184878ae60b4SMartin Matuska 		for (int v = 0; v < spa->spa_l2cache.sav_count; v++) {
184978ae60b4SMartin Matuska 			if (vdev_writeable(spa->spa_l2cache.sav_vdevs[v])) {
185078ae60b4SMartin Matuska 				zio_flush(zio, spa->spa_l2cache.sav_vdevs[v]);
185178ae60b4SMartin Matuska 			}
185278ae60b4SMartin Matuska 		}
185378ae60b4SMartin Matuska 	}
1854eda14cbcSMatt Macy 
1855eda14cbcSMatt Macy 	(void) zio_wait(zio);
1856eda14cbcSMatt Macy 
1857eda14cbcSMatt Macy 	return (good_writes >= 1 ? 0 : EIO);
1858eda14cbcSMatt Macy }
1859eda14cbcSMatt Macy 
1860eda14cbcSMatt Macy /*
1861eda14cbcSMatt Macy  * On success, increment the count of good writes for our top-level vdev.
1862eda14cbcSMatt Macy  */
1863eda14cbcSMatt Macy static void
1864eda14cbcSMatt Macy vdev_label_sync_done(zio_t *zio)
1865eda14cbcSMatt Macy {
1866eda14cbcSMatt Macy 	uint64_t *good_writes = zio->io_private;
1867eda14cbcSMatt Macy 
1868eda14cbcSMatt Macy 	if (zio->io_error == 0)
1869eda14cbcSMatt Macy 		atomic_inc_64(good_writes);
1870eda14cbcSMatt Macy }
1871eda14cbcSMatt Macy 
1872eda14cbcSMatt Macy /*
1873eda14cbcSMatt Macy  * If there weren't enough good writes, indicate failure to the parent.
1874eda14cbcSMatt Macy  */
1875eda14cbcSMatt Macy static void
1876eda14cbcSMatt Macy vdev_label_sync_top_done(zio_t *zio)
1877eda14cbcSMatt Macy {
1878eda14cbcSMatt Macy 	uint64_t *good_writes = zio->io_private;
1879eda14cbcSMatt Macy 
1880eda14cbcSMatt Macy 	if (*good_writes == 0)
1881eda14cbcSMatt Macy 		zio->io_error = SET_ERROR(EIO);
1882eda14cbcSMatt Macy 
1883eda14cbcSMatt Macy 	kmem_free(good_writes, sizeof (uint64_t));
1884eda14cbcSMatt Macy }
1885eda14cbcSMatt Macy 
1886eda14cbcSMatt Macy /*
1887eda14cbcSMatt Macy  * We ignore errors for log and cache devices, simply free the private data.
1888eda14cbcSMatt Macy  */
1889eda14cbcSMatt Macy static void
1890eda14cbcSMatt Macy vdev_label_sync_ignore_done(zio_t *zio)
1891eda14cbcSMatt Macy {
1892eda14cbcSMatt Macy 	kmem_free(zio->io_private, sizeof (uint64_t));
1893eda14cbcSMatt Macy }
1894eda14cbcSMatt Macy 
1895eda14cbcSMatt Macy /*
1896eda14cbcSMatt Macy  * Write all even or odd labels to all leaves of the specified vdev.
1897eda14cbcSMatt Macy  */
1898eda14cbcSMatt Macy static void
1899eda14cbcSMatt Macy vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1900eda14cbcSMatt Macy     vdev_t *vd, int l, uint64_t txg, int flags)
1901eda14cbcSMatt Macy {
1902eda14cbcSMatt Macy 	nvlist_t *label;
1903eda14cbcSMatt Macy 	vdev_phys_t *vp;
1904eda14cbcSMatt Macy 	abd_t *vp_abd;
1905eda14cbcSMatt Macy 	char *buf;
1906eda14cbcSMatt Macy 	size_t buflen;
1907ce4dcb97SMartin Matuska 	vdev_t *pvd = vd->vdev_parent;
1908ce4dcb97SMartin Matuska 	boolean_t spare_in_use = B_FALSE;
1909eda14cbcSMatt Macy 
1910eda14cbcSMatt Macy 	for (int c = 0; c < vd->vdev_children; c++) {
1911eda14cbcSMatt Macy 		vdev_label_sync(zio, good_writes,
1912eda14cbcSMatt Macy 		    vd->vdev_child[c], l, txg, flags);
1913eda14cbcSMatt Macy 	}
1914eda14cbcSMatt Macy 
1915eda14cbcSMatt Macy 	if (!vd->vdev_ops->vdev_op_leaf)
1916eda14cbcSMatt Macy 		return;
1917eda14cbcSMatt Macy 
1918eda14cbcSMatt Macy 	if (!vdev_writeable(vd))
1919eda14cbcSMatt Macy 		return;
1920eda14cbcSMatt Macy 
1921eda14cbcSMatt Macy 	/*
19227877fdebSMatt Macy 	 * The top-level config never needs to be written to a distributed
19237877fdebSMatt Macy 	 * spare.  When read vdev_dspare_label_read_config() will generate
19247877fdebSMatt Macy 	 * the config for the vdev_label_read_config().
19257877fdebSMatt Macy 	 */
19267877fdebSMatt Macy 	if (vd->vdev_ops == &vdev_draid_spare_ops)
19277877fdebSMatt Macy 		return;
19287877fdebSMatt Macy 
1929ce4dcb97SMartin Matuska 	if (pvd && pvd->vdev_ops == &vdev_spare_ops)
1930ce4dcb97SMartin Matuska 		spare_in_use = B_TRUE;
1931ce4dcb97SMartin Matuska 
19327877fdebSMatt Macy 	/*
1933eda14cbcSMatt Macy 	 * Generate a label describing the top-level config to which we belong.
1934eda14cbcSMatt Macy 	 */
1935ce4dcb97SMartin Matuska 	if ((vd->vdev_isspare && !spare_in_use) || vd->vdev_isl2cache) {
1936ce4dcb97SMartin Matuska 		label = vdev_aux_label_generate(vd, vd->vdev_isspare);
1937ce4dcb97SMartin Matuska 	} else {
1938eda14cbcSMatt Macy 		label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1939ce4dcb97SMartin Matuska 	}
1940eda14cbcSMatt Macy 
1941eda14cbcSMatt Macy 	vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1942eda14cbcSMatt Macy 	abd_zero(vp_abd, sizeof (vdev_phys_t));
1943eda14cbcSMatt Macy 	vp = abd_to_buf(vp_abd);
1944eda14cbcSMatt Macy 
1945eda14cbcSMatt Macy 	buf = vp->vp_nvlist;
1946eda14cbcSMatt Macy 	buflen = sizeof (vp->vp_nvlist);
1947eda14cbcSMatt Macy 
1948eda14cbcSMatt Macy 	if (!nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP)) {
1949eda14cbcSMatt Macy 		for (; l < VDEV_LABELS; l += 2) {
1950eda14cbcSMatt Macy 			vdev_label_write(zio, vd, l, vp_abd,
1951eda14cbcSMatt Macy 			    offsetof(vdev_label_t, vl_vdev_phys),
1952eda14cbcSMatt Macy 			    sizeof (vdev_phys_t),
1953eda14cbcSMatt Macy 			    vdev_label_sync_done, good_writes,
1954eda14cbcSMatt Macy 			    flags | ZIO_FLAG_DONT_PROPAGATE);
1955eda14cbcSMatt Macy 		}
1956eda14cbcSMatt Macy 	}
1957eda14cbcSMatt Macy 
1958eda14cbcSMatt Macy 	abd_free(vp_abd);
1959eda14cbcSMatt Macy 	nvlist_free(label);
1960eda14cbcSMatt Macy }
1961eda14cbcSMatt Macy 
1962eda14cbcSMatt Macy static int
1963eda14cbcSMatt Macy vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1964eda14cbcSMatt Macy {
1965eda14cbcSMatt Macy 	list_t *dl = &spa->spa_config_dirty_list;
1966eda14cbcSMatt Macy 	vdev_t *vd;
1967eda14cbcSMatt Macy 	zio_t *zio;
1968eda14cbcSMatt Macy 	int error;
1969eda14cbcSMatt Macy 
1970eda14cbcSMatt Macy 	/*
1971eda14cbcSMatt Macy 	 * Write the new labels to disk.
1972eda14cbcSMatt Macy 	 */
1973eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, NULL, flags);
1974eda14cbcSMatt Macy 
1975eda14cbcSMatt Macy 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
1976eda14cbcSMatt Macy 		uint64_t *good_writes;
1977eda14cbcSMatt Macy 
1978eda14cbcSMatt Macy 		ASSERT(!vd->vdev_ishole);
1979eda14cbcSMatt Macy 
1980eda14cbcSMatt Macy 		good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
1981eda14cbcSMatt Macy 		zio_t *vio = zio_null(zio, spa, NULL,
1982eda14cbcSMatt Macy 		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
1983eda14cbcSMatt Macy 		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
1984eda14cbcSMatt Macy 		    good_writes, flags);
1985eda14cbcSMatt Macy 		vdev_label_sync(vio, good_writes, vd, l, txg, flags);
1986eda14cbcSMatt Macy 		zio_nowait(vio);
1987eda14cbcSMatt Macy 	}
1988eda14cbcSMatt Macy 
1989ce4dcb97SMartin Matuska 	/*
1990ce4dcb97SMartin Matuska 	 * AUX path may have changed during import
1991ce4dcb97SMartin Matuska 	 */
1992ce4dcb97SMartin Matuska 	spa_aux_vdev_t *sav[2] = {&spa->spa_spares, &spa->spa_l2cache};
1993ce4dcb97SMartin Matuska 	for (int i = 0; i < 2; i++) {
1994ce4dcb97SMartin Matuska 		for (int v = 0; v < sav[i]->sav_count; v++) {
1995ce4dcb97SMartin Matuska 			uint64_t *good_writes;
1996ce4dcb97SMartin Matuska 			if (!sav[i]->sav_label_sync)
1997ce4dcb97SMartin Matuska 				continue;
1998ce4dcb97SMartin Matuska 			good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
1999ce4dcb97SMartin Matuska 			zio_t *vio = zio_null(zio, spa, NULL,
2000ce4dcb97SMartin Matuska 			    vdev_label_sync_ignore_done, good_writes, flags);
2001ce4dcb97SMartin Matuska 			vdev_label_sync(vio, good_writes, sav[i]->sav_vdevs[v],
2002ce4dcb97SMartin Matuska 			    l, txg, flags);
2003ce4dcb97SMartin Matuska 			zio_nowait(vio);
2004ce4dcb97SMartin Matuska 		}
2005ce4dcb97SMartin Matuska 	}
2006ce4dcb97SMartin Matuska 
2007eda14cbcSMatt Macy 	error = zio_wait(zio);
2008eda14cbcSMatt Macy 
2009eda14cbcSMatt Macy 	/*
2010eda14cbcSMatt Macy 	 * Flush the new labels to disk.
2011eda14cbcSMatt Macy 	 */
2012eda14cbcSMatt Macy 	zio = zio_root(spa, NULL, NULL, flags);
2013eda14cbcSMatt Macy 
2014eda14cbcSMatt Macy 	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
2015eda14cbcSMatt Macy 		zio_flush(zio, vd);
2016eda14cbcSMatt Macy 
2017ce4dcb97SMartin Matuska 	for (int i = 0; i < 2; i++) {
2018ce4dcb97SMartin Matuska 		if (!sav[i]->sav_label_sync)
2019ce4dcb97SMartin Matuska 			continue;
2020ce4dcb97SMartin Matuska 		for (int v = 0; v < sav[i]->sav_count; v++)
2021ce4dcb97SMartin Matuska 			zio_flush(zio, sav[i]->sav_vdevs[v]);
2022ce4dcb97SMartin Matuska 		if (l == 1)
2023ce4dcb97SMartin Matuska 			sav[i]->sav_label_sync = B_FALSE;
2024ce4dcb97SMartin Matuska 	}
2025ce4dcb97SMartin Matuska 
2026eda14cbcSMatt Macy 	(void) zio_wait(zio);
2027eda14cbcSMatt Macy 
2028eda14cbcSMatt Macy 	return (error);
2029eda14cbcSMatt Macy }
2030eda14cbcSMatt Macy 
2031eda14cbcSMatt Macy /*
2032eda14cbcSMatt Macy  * Sync the uberblock and any changes to the vdev configuration.
2033eda14cbcSMatt Macy  *
2034eda14cbcSMatt Macy  * The order of operations is carefully crafted to ensure that
2035eda14cbcSMatt Macy  * if the system panics or loses power at any time, the state on disk
2036eda14cbcSMatt Macy  * is still transactionally consistent.  The in-line comments below
2037eda14cbcSMatt Macy  * describe the failure semantics at each stage.
2038eda14cbcSMatt Macy  *
2039eda14cbcSMatt Macy  * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
2040eda14cbcSMatt Macy  * at any time, you can just call it again, and it will resume its work.
2041eda14cbcSMatt Macy  */
2042eda14cbcSMatt Macy int
2043eda14cbcSMatt Macy vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
2044eda14cbcSMatt Macy {
2045eda14cbcSMatt Macy 	spa_t *spa = svd[0]->vdev_spa;
2046eda14cbcSMatt Macy 	uberblock_t *ub = &spa->spa_uberblock;
2047eda14cbcSMatt Macy 	int error = 0;
2048eda14cbcSMatt Macy 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
2049eda14cbcSMatt Macy 
2050eda14cbcSMatt Macy 	ASSERT(svdcount != 0);
2051eda14cbcSMatt Macy retry:
2052eda14cbcSMatt Macy 	/*
2053eda14cbcSMatt Macy 	 * Normally, we don't want to try too hard to write every label and
2054eda14cbcSMatt Macy 	 * uberblock.  If there is a flaky disk, we don't want the rest of the
2055eda14cbcSMatt Macy 	 * sync process to block while we retry.  But if we can't write a
2056eda14cbcSMatt Macy 	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
2057eda14cbcSMatt Macy 	 * bailing out and declaring the pool faulted.
2058eda14cbcSMatt Macy 	 */
2059eda14cbcSMatt Macy 	if (error != 0) {
2060eda14cbcSMatt Macy 		if ((flags & ZIO_FLAG_TRYHARD) != 0)
2061eda14cbcSMatt Macy 			return (error);
2062eda14cbcSMatt Macy 		flags |= ZIO_FLAG_TRYHARD;
2063eda14cbcSMatt Macy 	}
2064eda14cbcSMatt Macy 
2065eda14cbcSMatt Macy 	ASSERT(ub->ub_txg <= txg);
2066eda14cbcSMatt Macy 
2067eda14cbcSMatt Macy 	/*
2068eda14cbcSMatt Macy 	 * If this isn't a resync due to I/O errors,
2069eda14cbcSMatt Macy 	 * and nothing changed in this transaction group,
2070b985c9caSMartin Matuska 	 * and multihost protection isn't enabled,
2071eda14cbcSMatt Macy 	 * and the vdev configuration hasn't changed,
2072eda14cbcSMatt Macy 	 * then there's nothing to do.
2073eda14cbcSMatt Macy 	 */
2074eda14cbcSMatt Macy 	if (ub->ub_txg < txg) {
2075eda14cbcSMatt Macy 		boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
2076eda14cbcSMatt Macy 		    txg, spa->spa_mmp.mmp_delay);
2077eda14cbcSMatt Macy 
2078b985c9caSMartin Matuska 		if (!changed && list_is_empty(&spa->spa_config_dirty_list) &&
2079b985c9caSMartin Matuska 		    !spa_multihost(spa))
2080eda14cbcSMatt Macy 			return (0);
2081eda14cbcSMatt Macy 	}
2082eda14cbcSMatt Macy 
2083eda14cbcSMatt Macy 	if (txg > spa_freeze_txg(spa))
2084eda14cbcSMatt Macy 		return (0);
2085eda14cbcSMatt Macy 
2086eda14cbcSMatt Macy 	ASSERT(txg <= spa->spa_final_txg);
2087eda14cbcSMatt Macy 
2088eda14cbcSMatt Macy 	/*
2089eda14cbcSMatt Macy 	 * Flush the write cache of every disk that's been written to
2090eda14cbcSMatt Macy 	 * in this transaction group.  This ensures that all blocks
2091eda14cbcSMatt Macy 	 * written in this txg will be committed to stable storage
2092eda14cbcSMatt Macy 	 * before any uberblock that references them.
2093eda14cbcSMatt Macy 	 */
2094eda14cbcSMatt Macy 	zio_t *zio = zio_root(spa, NULL, NULL, flags);
2095eda14cbcSMatt Macy 
2096eda14cbcSMatt Macy 	for (vdev_t *vd =
2097eda14cbcSMatt Macy 	    txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
2098eda14cbcSMatt Macy 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
2099eda14cbcSMatt Macy 		zio_flush(zio, vd);
2100eda14cbcSMatt Macy 
2101eda14cbcSMatt Macy 	(void) zio_wait(zio);
2102eda14cbcSMatt Macy 
2103eda14cbcSMatt Macy 	/*
2104eda14cbcSMatt Macy 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
2105eda14cbcSMatt Macy 	 * system dies in the middle of this process, that's OK: all of the
2106eda14cbcSMatt Macy 	 * even labels that made it to disk will be newer than any uberblock,
2107eda14cbcSMatt Macy 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
2108eda14cbcSMatt Macy 	 * which have not yet been touched, will still be valid.  We flush
2109eda14cbcSMatt Macy 	 * the new labels to disk to ensure that all even-label updates
2110eda14cbcSMatt Macy 	 * are committed to stable storage before the uberblock update.
2111eda14cbcSMatt Macy 	 */
2112eda14cbcSMatt Macy 	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
2113eda14cbcSMatt Macy 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
2114eda14cbcSMatt Macy 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
2115eda14cbcSMatt Macy 			    "for pool '%s' when syncing out the even labels "
2116eda14cbcSMatt Macy 			    "of dirty vdevs", error, spa_name(spa));
2117eda14cbcSMatt Macy 		}
2118eda14cbcSMatt Macy 		goto retry;
2119eda14cbcSMatt Macy 	}
2120eda14cbcSMatt Macy 
2121eda14cbcSMatt Macy 	/*
2122eda14cbcSMatt Macy 	 * Sync the uberblocks to all vdevs in svd[].
2123eda14cbcSMatt Macy 	 * If the system dies in the middle of this step, there are two cases
2124eda14cbcSMatt Macy 	 * to consider, and the on-disk state is consistent either way:
2125eda14cbcSMatt Macy 	 *
2126eda14cbcSMatt Macy 	 * (1)	If none of the new uberblocks made it to disk, then the
2127eda14cbcSMatt Macy 	 *	previous uberblock will be the newest, and the odd labels
2128eda14cbcSMatt Macy 	 *	(which had not yet been touched) will be valid with respect
2129eda14cbcSMatt Macy 	 *	to that uberblock.
2130eda14cbcSMatt Macy 	 *
2131eda14cbcSMatt Macy 	 * (2)	If one or more new uberblocks made it to disk, then they
2132eda14cbcSMatt Macy 	 *	will be the newest, and the even labels (which had all
2133eda14cbcSMatt Macy 	 *	been successfully committed) will be valid with respect
2134eda14cbcSMatt Macy 	 *	to the new uberblocks.
2135eda14cbcSMatt Macy 	 */
2136eda14cbcSMatt Macy 	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
2137eda14cbcSMatt Macy 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
2138eda14cbcSMatt Macy 			zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
2139eda14cbcSMatt Macy 			    "%d for pool '%s'", error, spa_name(spa));
2140eda14cbcSMatt Macy 		}
2141eda14cbcSMatt Macy 		goto retry;
2142eda14cbcSMatt Macy 	}
2143eda14cbcSMatt Macy 
2144eda14cbcSMatt Macy 	if (spa_multihost(spa))
2145eda14cbcSMatt Macy 		mmp_update_uberblock(spa, ub);
2146eda14cbcSMatt Macy 
2147eda14cbcSMatt Macy 	/*
2148eda14cbcSMatt Macy 	 * Sync out odd labels for every dirty vdev.  If the system dies
2149eda14cbcSMatt Macy 	 * in the middle of this process, the even labels and the new
2150eda14cbcSMatt Macy 	 * uberblocks will suffice to open the pool.  The next time
2151eda14cbcSMatt Macy 	 * the pool is opened, the first thing we'll do -- before any
2152eda14cbcSMatt Macy 	 * user data is modified -- is mark every vdev dirty so that
2153eda14cbcSMatt Macy 	 * all labels will be brought up to date.  We flush the new labels
2154eda14cbcSMatt Macy 	 * to disk to ensure that all odd-label updates are committed to
2155eda14cbcSMatt Macy 	 * stable storage before the next transaction group begins.
2156eda14cbcSMatt Macy 	 */
2157eda14cbcSMatt Macy 	if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
2158eda14cbcSMatt Macy 		if ((flags & ZIO_FLAG_TRYHARD) != 0) {
2159eda14cbcSMatt Macy 			zfs_dbgmsg("vdev_label_sync_list() returned error %d "
2160eda14cbcSMatt Macy 			    "for pool '%s' when syncing out the odd labels of "
2161eda14cbcSMatt Macy 			    "dirty vdevs", error, spa_name(spa));
2162eda14cbcSMatt Macy 		}
2163eda14cbcSMatt Macy 		goto retry;
2164eda14cbcSMatt Macy 	}
2165eda14cbcSMatt Macy 
2166eda14cbcSMatt Macy 	return (0);
2167eda14cbcSMatt Macy }
2168