vdev_label.c revision 219089
1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23168404Spjd */
24168404Spjd
25168404Spjd/*
26168404Spjd * Virtual Device Labels
27168404Spjd * ---------------------
28168404Spjd *
29168404Spjd * The vdev label serves several distinct purposes:
30168404Spjd *
31168404Spjd *	1. Uniquely identify this device as part of a ZFS pool and confirm its
32168404Spjd *	   identity within the pool.
33168404Spjd *
34168404Spjd * 	2. Verify that all the devices given in a configuration are present
35168404Spjd *         within the pool.
36168404Spjd *
37168404Spjd * 	3. Determine the uberblock for the pool.
38168404Spjd *
39168404Spjd * 	4. In case of an import operation, determine the configuration of the
40168404Spjd *         toplevel vdev of which it is a part.
41168404Spjd *
42168404Spjd * 	5. If an import operation cannot find all the devices in the pool,
43168404Spjd *         provide enough information to the administrator to determine which
44168404Spjd *         devices are missing.
45168404Spjd *
46168404Spjd * It is important to note that while the kernel is responsible for writing the
47168404Spjd * label, it only consumes the information in the first three cases.  The
48168404Spjd * latter information is only consumed in userland when determining the
49168404Spjd * configuration to import a pool.
50168404Spjd *
51168404Spjd *
52168404Spjd * Label Organization
53168404Spjd * ------------------
54168404Spjd *
55168404Spjd * Before describing the contents of the label, it's important to understand how
56168404Spjd * the labels are written and updated with respect to the uberblock.
57168404Spjd *
58168404Spjd * When the pool configuration is altered, either because it was newly created
59168404Spjd * or a device was added, we want to update all the labels such that we can deal
60168404Spjd * with fatal failure at any point.  To this end, each disk has two labels which
61168404Spjd * are updated before and after the uberblock is synced.  Assuming we have
62185029Spjd * labels and an uberblock with the following transaction groups:
63168404Spjd *
64168404Spjd *              L1          UB          L2
65168404Spjd *           +------+    +------+    +------+
66168404Spjd *           |      |    |      |    |      |
67168404Spjd *           | t10  |    | t10  |    | t10  |
68168404Spjd *           |      |    |      |    |      |
69168404Spjd *           +------+    +------+    +------+
70168404Spjd *
71168404Spjd * In this stable state, the labels and the uberblock were all updated within
72168404Spjd * the same transaction group (10).  Each label is mirrored and checksummed, so
73168404Spjd * that we can detect when we fail partway through writing the label.
74168404Spjd *
75168404Spjd * In order to identify which labels are valid, the labels are written in the
76168404Spjd * following manner:
77168404Spjd *
78168404Spjd * 	1. For each vdev, update 'L1' to the new label
79168404Spjd * 	2. Update the uberblock
80168404Spjd * 	3. For each vdev, update 'L2' to the new label
81168404Spjd *
82168404Spjd * Given arbitrary failure, we can determine the correct label to use based on
83168404Spjd * the transaction group.  If we fail after updating L1 but before updating the
84168404Spjd * UB, we will notice that L1's transaction group is greater than the uberblock,
85168404Spjd * so L2 must be valid.  If we fail after writing the uberblock but before
86168404Spjd * writing L2, we will notice that L2's transaction group is less than L1, and
87168404Spjd * therefore L1 is valid.
88168404Spjd *
89168404Spjd * Another added complexity is that not every label is updated when the config
90168404Spjd * is synced.  If we add a single device, we do not want to have to re-write
91168404Spjd * every label for every device in the pool.  This means that both L1 and L2 may
92168404Spjd * be older than the pool uberblock, because the necessary information is stored
93168404Spjd * on another vdev.
94168404Spjd *
95168404Spjd *
96168404Spjd * On-disk Format
97168404Spjd * --------------
98168404Spjd *
99168404Spjd * The vdev label consists of two distinct parts, and is wrapped within the
100168404Spjd * vdev_label_t structure.  The label includes 8k of padding to permit legacy
101168404Spjd * VTOC disk labels, but is otherwise ignored.
102168404Spjd *
103168404Spjd * The first half of the label is a packed nvlist which contains pool wide
104168404Spjd * properties, per-vdev properties, and configuration information.  It is
105168404Spjd * described in more detail below.
106168404Spjd *
107168404Spjd * The latter half of the label consists of a redundant array of uberblocks.
108168404Spjd * These uberblocks are updated whenever a transaction group is committed,
109168404Spjd * or when the configuration is updated.  When a pool is loaded, we scan each
110168404Spjd * vdev for the 'best' uberblock.
111168404Spjd *
112168404Spjd *
113168404Spjd * Configuration Information
114168404Spjd * -------------------------
115168404Spjd *
116168404Spjd * The nvlist describing the pool and vdev contains the following elements:
117168404Spjd *
118168404Spjd * 	version		ZFS on-disk version
119168404Spjd * 	name		Pool name
120168404Spjd * 	state		Pool state
121168404Spjd * 	txg		Transaction group in which this label was written
122168404Spjd * 	pool_guid	Unique identifier for this pool
123168404Spjd * 	vdev_tree	An nvlist describing vdev tree.
124168404Spjd *
125168404Spjd * Each leaf device label also contains the following:
126168404Spjd *
127168404Spjd * 	top_guid	Unique ID for top-level vdev in which this is contained
128168404Spjd * 	guid		Unique ID for the leaf vdev
129168404Spjd *
130168404Spjd * The 'vs' configuration follows the format described in 'spa_config.c'.
131168404Spjd */
132168404Spjd
133168404Spjd#include <sys/zfs_context.h>
134168404Spjd#include <sys/spa.h>
135168404Spjd#include <sys/spa_impl.h>
136168404Spjd#include <sys/dmu.h>
137168404Spjd#include <sys/zap.h>
138168404Spjd#include <sys/vdev.h>
139168404Spjd#include <sys/vdev_impl.h>
140168404Spjd#include <sys/uberblock_impl.h>
141168404Spjd#include <sys/metaslab.h>
142168404Spjd#include <sys/zio.h>
143219089Spjd#include <sys/dsl_scan.h>
144168404Spjd#include <sys/fs/zfs.h>
145168404Spjd
146168404Spjd/*
147168404Spjd * Basic routines to read and write from a vdev label.
148168404Spjd * Used throughout the rest of this file.
149168404Spjd */
150168404Spjduint64_t
151168404Spjdvdev_label_offset(uint64_t psize, int l, uint64_t offset)
152168404Spjd{
153168404Spjd	ASSERT(offset < sizeof (vdev_label_t));
154185029Spjd	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
155168404Spjd
156168404Spjd	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
157168404Spjd	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
158168404Spjd}
159168404Spjd
160185029Spjd/*
161185029Spjd * Returns back the vdev label associated with the passed in offset.
162185029Spjd */
163185029Spjdint
164185029Spjdvdev_label_number(uint64_t psize, uint64_t offset)
165185029Spjd{
166185029Spjd	int l;
167185029Spjd
168185029Spjd	if (offset >= psize - VDEV_LABEL_END_SIZE) {
169185029Spjd		offset -= psize - VDEV_LABEL_END_SIZE;
170185029Spjd		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
171185029Spjd	}
172185029Spjd	l = offset / sizeof (vdev_label_t);
173185029Spjd	return (l < VDEV_LABELS ? l : -1);
174185029Spjd}
175185029Spjd
176168404Spjdstatic void
177168404Spjdvdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
178185029Spjd	uint64_t size, zio_done_func_t *done, void *private, int flags)
179168404Spjd{
180185029Spjd	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
181185029Spjd	    SCL_STATE_ALL);
182185029Spjd	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
183168404Spjd
184168404Spjd	zio_nowait(zio_read_phys(zio, vd,
185168404Spjd	    vdev_label_offset(vd->vdev_psize, l, offset),
186168404Spjd	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
187185029Spjd	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
188168404Spjd}
189168404Spjd
190168404Spjdstatic void
191168404Spjdvdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
192185029Spjd	uint64_t size, zio_done_func_t *done, void *private, int flags)
193168404Spjd{
194185029Spjd	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
195185029Spjd	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
196185029Spjd	    (SCL_CONFIG | SCL_STATE) &&
197185029Spjd	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
198185029Spjd	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
199168404Spjd
200168404Spjd	zio_nowait(zio_write_phys(zio, vd,
201168404Spjd	    vdev_label_offset(vd->vdev_psize, l, offset),
202168404Spjd	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
203185029Spjd	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
204168404Spjd}
205168404Spjd
206168404Spjd/*
207168404Spjd * Generate the nvlist representing this vdev's config.
208168404Spjd */
209168404Spjdnvlist_t *
210168404Spjdvdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
211219089Spjd    vdev_config_flag_t flags)
212168404Spjd{
213168404Spjd	nvlist_t *nv = NULL;
214168404Spjd
215168404Spjd	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
216168404Spjd
217168404Spjd	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
218168404Spjd	    vd->vdev_ops->vdev_op_type) == 0);
219219089Spjd	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
220168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
221168404Spjd		    == 0);
222168404Spjd	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
223168404Spjd
224168404Spjd	if (vd->vdev_path != NULL)
225168404Spjd		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
226168404Spjd		    vd->vdev_path) == 0);
227168404Spjd
228168404Spjd	if (vd->vdev_devid != NULL)
229168404Spjd		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
230168404Spjd		    vd->vdev_devid) == 0);
231168404Spjd
232185029Spjd	if (vd->vdev_physpath != NULL)
233185029Spjd		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
234185029Spjd		    vd->vdev_physpath) == 0);
235185029Spjd
236209962Smm	if (vd->vdev_fru != NULL)
237209962Smm		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU,
238209962Smm		    vd->vdev_fru) == 0);
239209962Smm
240168404Spjd	if (vd->vdev_nparity != 0) {
241168404Spjd		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
242168404Spjd		    VDEV_TYPE_RAIDZ) == 0);
243168404Spjd
244168404Spjd		/*
245168404Spjd		 * Make sure someone hasn't managed to sneak a fancy new vdev
246168404Spjd		 * into a crufty old storage pool.
247168404Spjd		 */
248168404Spjd		ASSERT(vd->vdev_nparity == 1 ||
249219089Spjd		    (vd->vdev_nparity <= 2 &&
250219089Spjd		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
251219089Spjd		    (vd->vdev_nparity <= 3 &&
252219089Spjd		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
253168404Spjd
254168404Spjd		/*
255168404Spjd		 * Note that we'll add the nparity tag even on storage pools
256168404Spjd		 * that only support a single parity device -- older software
257168404Spjd		 * will just ignore it.
258168404Spjd		 */
259168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
260168404Spjd		    vd->vdev_nparity) == 0);
261168404Spjd	}
262168404Spjd
263168404Spjd	if (vd->vdev_wholedisk != -1ULL)
264168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
265168404Spjd		    vd->vdev_wholedisk) == 0);
266168404Spjd
267168404Spjd	if (vd->vdev_not_present)
268168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
269168404Spjd
270168404Spjd	if (vd->vdev_isspare)
271168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
272168404Spjd
273219089Spjd	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
274219089Spjd	    vd == vd->vdev_top) {
275168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
276168404Spjd		    vd->vdev_ms_array) == 0);
277168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
278168404Spjd		    vd->vdev_ms_shift) == 0);
279168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
280168404Spjd		    vd->vdev_ashift) == 0);
281168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
282168404Spjd		    vd->vdev_asize) == 0);
283185029Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
284185029Spjd		    vd->vdev_islog) == 0);
285219089Spjd		if (vd->vdev_removing)
286219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
287219089Spjd			    vd->vdev_removing) == 0);
288168404Spjd	}
289168404Spjd
290209962Smm	if (vd->vdev_dtl_smo.smo_object != 0)
291168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
292209962Smm		    vd->vdev_dtl_smo.smo_object) == 0);
293168404Spjd
294219089Spjd	if (vd->vdev_crtxg)
295219089Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
296219089Spjd		    vd->vdev_crtxg) == 0);
297219089Spjd
298168404Spjd	if (getstats) {
299168404Spjd		vdev_stat_t vs;
300219089Spjd		pool_scan_stat_t ps;
301219089Spjd
302168404Spjd		vdev_get_stats(vd, &vs);
303219089Spjd		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
304168404Spjd		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
305219089Spjd
306219089Spjd		/* provide either current or previous scan information */
307219089Spjd		if (spa_scan_get_stats(spa, &ps) == 0) {
308219089Spjd			VERIFY(nvlist_add_uint64_array(nv,
309219089Spjd			    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
310219089Spjd			    sizeof (pool_scan_stat_t) / sizeof (uint64_t))
311219089Spjd			    == 0);
312219089Spjd		}
313168404Spjd	}
314168404Spjd
315168404Spjd	if (!vd->vdev_ops->vdev_op_leaf) {
316168404Spjd		nvlist_t **child;
317219089Spjd		int c, idx;
318168404Spjd
319219089Spjd		ASSERT(!vd->vdev_ishole);
320219089Spjd
321168404Spjd		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
322168404Spjd		    KM_SLEEP);
323168404Spjd
324219089Spjd		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
325219089Spjd			vdev_t *cvd = vd->vdev_child[c];
326168404Spjd
327219089Spjd			/*
328219089Spjd			 * If we're generating an nvlist of removing
329219089Spjd			 * vdevs then skip over any device which is
330219089Spjd			 * not being removed.
331219089Spjd			 */
332219089Spjd			if ((flags & VDEV_CONFIG_REMOVING) &&
333219089Spjd			    !cvd->vdev_removing)
334219089Spjd				continue;
335168404Spjd
336219089Spjd			child[idx++] = vdev_config_generate(spa, cvd,
337219089Spjd			    getstats, flags);
338219089Spjd		}
339219089Spjd
340219089Spjd		if (idx) {
341219089Spjd			VERIFY(nvlist_add_nvlist_array(nv,
342219089Spjd			    ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
343219089Spjd		}
344219089Spjd
345219089Spjd		for (c = 0; c < idx; c++)
346168404Spjd			nvlist_free(child[c]);
347168404Spjd
348168404Spjd		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
349168404Spjd
350168404Spjd	} else {
351219089Spjd		const char *aux = NULL;
352219089Spjd
353168404Spjd		if (vd->vdev_offline && !vd->vdev_tmpoffline)
354168404Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
355168404Spjd			    B_TRUE) == 0);
356219089Spjd		if (vd->vdev_resilvering)
357219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVERING,
358219089Spjd			    B_TRUE) == 0);
359185029Spjd		if (vd->vdev_faulted)
360185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
361185029Spjd			    B_TRUE) == 0);
362185029Spjd		if (vd->vdev_degraded)
363185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
364185029Spjd			    B_TRUE) == 0);
365185029Spjd		if (vd->vdev_removed)
366185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
367185029Spjd			    B_TRUE) == 0);
368185029Spjd		if (vd->vdev_unspare)
369185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
370185029Spjd			    B_TRUE) == 0);
371219089Spjd		if (vd->vdev_ishole)
372219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE,
373219089Spjd			    B_TRUE) == 0);
374219089Spjd
375219089Spjd		switch (vd->vdev_stat.vs_aux) {
376219089Spjd		case VDEV_AUX_ERR_EXCEEDED:
377219089Spjd			aux = "err_exceeded";
378219089Spjd			break;
379219089Spjd
380219089Spjd		case VDEV_AUX_EXTERNAL:
381219089Spjd			aux = "external";
382219089Spjd			break;
383219089Spjd		}
384219089Spjd
385219089Spjd		if (aux != NULL)
386219089Spjd			VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE,
387219089Spjd			    aux) == 0);
388219089Spjd
389219089Spjd		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
390219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
391219089Spjd			    vd->vdev_orig_guid) == 0);
392219089Spjd		}
393168404Spjd	}
394168404Spjd
395168404Spjd	return (nv);
396168404Spjd}
397168404Spjd
398219089Spjd/*
399219089Spjd * Generate a view of the top-level vdevs.  If we currently have holes
400219089Spjd * in the namespace, then generate an array which contains a list of holey
401219089Spjd * vdevs.  Additionally, add the number of top-level children that currently
402219089Spjd * exist.
403219089Spjd */
404219089Spjdvoid
405219089Spjdvdev_top_config_generate(spa_t *spa, nvlist_t *config)
406219089Spjd{
407219089Spjd	vdev_t *rvd = spa->spa_root_vdev;
408219089Spjd	uint64_t *array;
409219089Spjd	uint_t c, idx;
410219089Spjd
411219089Spjd	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
412219089Spjd
413219089Spjd	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
414219089Spjd		vdev_t *tvd = rvd->vdev_child[c];
415219089Spjd
416219089Spjd		if (tvd->vdev_ishole)
417219089Spjd			array[idx++] = c;
418219089Spjd	}
419219089Spjd
420219089Spjd	if (idx) {
421219089Spjd		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
422219089Spjd		    array, idx) == 0);
423219089Spjd	}
424219089Spjd
425219089Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
426219089Spjd	    rvd->vdev_children) == 0);
427219089Spjd
428219089Spjd	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
429219089Spjd}
430219089Spjd
431168404Spjdnvlist_t *
432168404Spjdvdev_label_read_config(vdev_t *vd)
433168404Spjd{
434168404Spjd	spa_t *spa = vd->vdev_spa;
435168404Spjd	nvlist_t *config = NULL;
436168404Spjd	vdev_phys_t *vp;
437168404Spjd	zio_t *zio;
438213198Smm	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
439213198Smm	    ZIO_FLAG_SPECULATIVE;
440168404Spjd
441185029Spjd	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
442168404Spjd
443185029Spjd	if (!vdev_readable(vd))
444168404Spjd		return (NULL);
445168404Spjd
446168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
447168404Spjd
448213198Smmretry:
449185029Spjd	for (int l = 0; l < VDEV_LABELS; l++) {
450168404Spjd
451185029Spjd		zio = zio_root(spa, NULL, NULL, flags);
452168404Spjd
453168404Spjd		vdev_label_read(zio, vd, l, vp,
454168404Spjd		    offsetof(vdev_label_t, vl_vdev_phys),
455185029Spjd		    sizeof (vdev_phys_t), NULL, NULL, flags);
456168404Spjd
457168404Spjd		if (zio_wait(zio) == 0 &&
458168404Spjd		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
459168404Spjd		    &config, 0) == 0)
460168404Spjd			break;
461168404Spjd
462168404Spjd		if (config != NULL) {
463168404Spjd			nvlist_free(config);
464168404Spjd			config = NULL;
465168404Spjd		}
466168404Spjd	}
467168404Spjd
468213198Smm	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
469213198Smm		flags |= ZIO_FLAG_TRYHARD;
470213198Smm		goto retry;
471213198Smm	}
472213198Smm
473168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
474168404Spjd
475168404Spjd	return (config);
476168404Spjd}
477168404Spjd
478168404Spjd/*
479168404Spjd * Determine if a device is in use.  The 'spare_guid' parameter will be filled
480168404Spjd * in with the device guid if this spare is active elsewhere on the system.
481168404Spjd */
482168404Spjdstatic boolean_t
483168404Spjdvdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
484185029Spjd    uint64_t *spare_guid, uint64_t *l2cache_guid)
485168404Spjd{
486168404Spjd	spa_t *spa = vd->vdev_spa;
487168404Spjd	uint64_t state, pool_guid, device_guid, txg, spare_pool;
488168404Spjd	uint64_t vdtxg = 0;
489168404Spjd	nvlist_t *label;
490168404Spjd
491168404Spjd	if (spare_guid)
492168404Spjd		*spare_guid = 0ULL;
493185029Spjd	if (l2cache_guid)
494185029Spjd		*l2cache_guid = 0ULL;
495168404Spjd
496168404Spjd	/*
497168404Spjd	 * Read the label, if any, and perform some basic sanity checks.
498168404Spjd	 */
499168404Spjd	if ((label = vdev_label_read_config(vd)) == NULL)
500168404Spjd		return (B_FALSE);
501168404Spjd
502168404Spjd	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
503168404Spjd	    &vdtxg);
504168404Spjd
505168404Spjd	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
506168404Spjd	    &state) != 0 ||
507168404Spjd	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
508168404Spjd	    &device_guid) != 0) {
509168404Spjd		nvlist_free(label);
510168404Spjd		return (B_FALSE);
511168404Spjd	}
512168404Spjd
513185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
514168404Spjd	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
515168404Spjd	    &pool_guid) != 0 ||
516168404Spjd	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
517168404Spjd	    &txg) != 0)) {
518168404Spjd		nvlist_free(label);
519168404Spjd		return (B_FALSE);
520168404Spjd	}
521168404Spjd
522168404Spjd	nvlist_free(label);
523168404Spjd
524168404Spjd	/*
525168404Spjd	 * Check to see if this device indeed belongs to the pool it claims to
526168404Spjd	 * be a part of.  The only way this is allowed is if the device is a hot
527168404Spjd	 * spare (which we check for later on).
528168404Spjd	 */
529185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
530168404Spjd	    !spa_guid_exists(pool_guid, device_guid) &&
531185029Spjd	    !spa_spare_exists(device_guid, NULL, NULL) &&
532185029Spjd	    !spa_l2cache_exists(device_guid, NULL))
533168404Spjd		return (B_FALSE);
534168404Spjd
535168404Spjd	/*
536168404Spjd	 * If the transaction group is zero, then this an initialized (but
537168404Spjd	 * unused) label.  This is only an error if the create transaction
538168404Spjd	 * on-disk is the same as the one we're using now, in which case the
539168404Spjd	 * user has attempted to add the same vdev multiple times in the same
540168404Spjd	 * transaction.
541168404Spjd	 */
542185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
543185029Spjd	    txg == 0 && vdtxg == crtxg)
544168404Spjd		return (B_TRUE);
545168404Spjd
546168404Spjd	/*
547168404Spjd	 * Check to see if this is a spare device.  We do an explicit check for
548168404Spjd	 * spa_has_spare() here because it may be on our pending list of spares
549185029Spjd	 * to add.  We also check if it is an l2cache device.
550168404Spjd	 */
551185029Spjd	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
552168404Spjd	    spa_has_spare(spa, device_guid)) {
553168404Spjd		if (spare_guid)
554168404Spjd			*spare_guid = device_guid;
555168404Spjd
556168404Spjd		switch (reason) {
557168404Spjd		case VDEV_LABEL_CREATE:
558185029Spjd		case VDEV_LABEL_L2CACHE:
559168404Spjd			return (B_TRUE);
560168404Spjd
561168404Spjd		case VDEV_LABEL_REPLACE:
562168404Spjd			return (!spa_has_spare(spa, device_guid) ||
563168404Spjd			    spare_pool != 0ULL);
564168404Spjd
565168404Spjd		case VDEV_LABEL_SPARE:
566168404Spjd			return (spa_has_spare(spa, device_guid));
567168404Spjd		}
568168404Spjd	}
569168404Spjd
570168404Spjd	/*
571185029Spjd	 * Check to see if this is an l2cache device.
572185029Spjd	 */
573185029Spjd	if (spa_l2cache_exists(device_guid, NULL))
574185029Spjd		return (B_TRUE);
575185029Spjd
576185029Spjd	/*
577219089Spjd	 * We can't rely on a pool's state if it's been imported
578219089Spjd	 * read-only.  Instead we look to see if the pools is marked
579219089Spjd	 * read-only in the namespace and set the state to active.
580219089Spjd	 */
581219089Spjd	if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
582219089Spjd	    spa_mode(spa) == FREAD)
583219089Spjd		state = POOL_STATE_ACTIVE;
584219089Spjd
585219089Spjd	/*
586168404Spjd	 * If the device is marked ACTIVE, then this device is in use by another
587168404Spjd	 * pool on the system.
588168404Spjd	 */
589168404Spjd	return (state == POOL_STATE_ACTIVE);
590168404Spjd}
591168404Spjd
592168404Spjd/*
593168404Spjd * Initialize a vdev label.  We check to make sure each leaf device is not in
594168404Spjd * use, and writable.  We put down an initial label which we will later
595168404Spjd * overwrite with a complete label.  Note that it's important to do this
596168404Spjd * sequentially, not in parallel, so that we catch cases of multiple use of the
597168404Spjd * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
598168404Spjd * itself.
599168404Spjd */
600168404Spjdint
601168404Spjdvdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
602168404Spjd{
603168404Spjd	spa_t *spa = vd->vdev_spa;
604168404Spjd	nvlist_t *label;
605168404Spjd	vdev_phys_t *vp;
606209962Smm	char *pad2;
607168404Spjd	uberblock_t *ub;
608168404Spjd	zio_t *zio;
609168404Spjd	char *buf;
610168404Spjd	size_t buflen;
611168404Spjd	int error;
612185029Spjd	uint64_t spare_guid, l2cache_guid;
613185029Spjd	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
614168404Spjd
615185029Spjd	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
616168404Spjd
617185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
618168404Spjd		if ((error = vdev_label_init(vd->vdev_child[c],
619168404Spjd		    crtxg, reason)) != 0)
620168404Spjd			return (error);
621168404Spjd
622219089Spjd	/* Track the creation time for this vdev */
623219089Spjd	vd->vdev_crtxg = crtxg;
624219089Spjd
625168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
626168404Spjd		return (0);
627168404Spjd
628168404Spjd	/*
629168404Spjd	 * Dead vdevs cannot be initialized.
630168404Spjd	 */
631168404Spjd	if (vdev_is_dead(vd))
632168404Spjd		return (EIO);
633168404Spjd
634168404Spjd	/*
635168404Spjd	 * Determine if the vdev is in use.
636168404Spjd	 */
637219089Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
638185029Spjd	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
639168404Spjd		return (EBUSY);
640168404Spjd
641168404Spjd	/*
642185029Spjd	 * If this is a request to add or replace a spare or l2cache device
643185029Spjd	 * that is in use elsewhere on the system, then we must update the
644185029Spjd	 * guid (which was initialized to a random value) to reflect the
645185029Spjd	 * actual GUID (which is shared between multiple pools).
646168404Spjd	 */
647185029Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
648185029Spjd	    spare_guid != 0ULL) {
649185029Spjd		uint64_t guid_delta = spare_guid - vd->vdev_guid;
650168404Spjd
651185029Spjd		vd->vdev_guid += guid_delta;
652168404Spjd
653185029Spjd		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
654185029Spjd			pvd->vdev_guid_sum += guid_delta;
655168404Spjd
656168404Spjd		/*
657168404Spjd		 * If this is a replacement, then we want to fallthrough to the
658168404Spjd		 * rest of the code.  If we're adding a spare, then it's already
659185029Spjd		 * labeled appropriately and we can just return.
660168404Spjd		 */
661168404Spjd		if (reason == VDEV_LABEL_SPARE)
662168404Spjd			return (0);
663219089Spjd		ASSERT(reason == VDEV_LABEL_REPLACE ||
664219089Spjd		    reason == VDEV_LABEL_SPLIT);
665168404Spjd	}
666168404Spjd
667185029Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
668185029Spjd	    l2cache_guid != 0ULL) {
669185029Spjd		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
670185029Spjd
671185029Spjd		vd->vdev_guid += guid_delta;
672185029Spjd
673185029Spjd		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
674185029Spjd			pvd->vdev_guid_sum += guid_delta;
675185029Spjd
676185029Spjd		/*
677185029Spjd		 * If this is a replacement, then we want to fallthrough to the
678185029Spjd		 * rest of the code.  If we're adding an l2cache, then it's
679185029Spjd		 * already labeled appropriately and we can just return.
680185029Spjd		 */
681185029Spjd		if (reason == VDEV_LABEL_L2CACHE)
682185029Spjd			return (0);
683185029Spjd		ASSERT(reason == VDEV_LABEL_REPLACE);
684185029Spjd	}
685185029Spjd
686168404Spjd	/*
687168404Spjd	 * Initialize its label.
688168404Spjd	 */
689168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
690168404Spjd	bzero(vp, sizeof (vdev_phys_t));
691168404Spjd
692168404Spjd	/*
693168404Spjd	 * Generate a label describing the pool and our top-level vdev.
694168404Spjd	 * We mark it as being from txg 0 to indicate that it's not
695168404Spjd	 * really part of an active pool just yet.  The labels will
696168404Spjd	 * be written again with a meaningful txg by spa_sync().
697168404Spjd	 */
698168404Spjd	if (reason == VDEV_LABEL_SPARE ||
699168404Spjd	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
700168404Spjd		/*
701168404Spjd		 * For inactive hot spares, we generate a special label that
702168404Spjd		 * identifies as a mutually shared hot spare.  We write the
703168404Spjd		 * label if we are adding a hot spare, or if we are removing an
704168404Spjd		 * active hot spare (in which case we want to revert the
705168404Spjd		 * labels).
706168404Spjd		 */
707168404Spjd		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
708168404Spjd
709168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
710168404Spjd		    spa_version(spa)) == 0);
711168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
712168404Spjd		    POOL_STATE_SPARE) == 0);
713168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
714168404Spjd		    vd->vdev_guid) == 0);
715185029Spjd	} else if (reason == VDEV_LABEL_L2CACHE ||
716185029Spjd	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
717185029Spjd		/*
718185029Spjd		 * For level 2 ARC devices, add a special label.
719185029Spjd		 */
720185029Spjd		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
721185029Spjd
722185029Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
723185029Spjd		    spa_version(spa)) == 0);
724185029Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
725185029Spjd		    POOL_STATE_L2CACHE) == 0);
726185029Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
727185029Spjd		    vd->vdev_guid) == 0);
728168404Spjd	} else {
729219089Spjd		uint64_t txg = 0ULL;
730168404Spjd
731219089Spjd		if (reason == VDEV_LABEL_SPLIT)
732219089Spjd			txg = spa->spa_uberblock.ub_txg;
733219089Spjd		label = spa_config_generate(spa, vd, txg, B_FALSE);
734219089Spjd
735168404Spjd		/*
736168404Spjd		 * Add our creation time.  This allows us to detect multiple
737168404Spjd		 * vdev uses as described above, and automatically expires if we
738168404Spjd		 * fail.
739168404Spjd		 */
740168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
741168404Spjd		    crtxg) == 0);
742168404Spjd	}
743168404Spjd
744168404Spjd	buf = vp->vp_nvlist;
745168404Spjd	buflen = sizeof (vp->vp_nvlist);
746168404Spjd
747168404Spjd	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
748168404Spjd	if (error != 0) {
749168404Spjd		nvlist_free(label);
750168404Spjd		zio_buf_free(vp, sizeof (vdev_phys_t));
751168404Spjd		/* EFAULT means nvlist_pack ran out of room */
752168404Spjd		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
753168404Spjd	}
754168404Spjd
755168404Spjd	/*
756168404Spjd	 * Initialize uberblock template.
757168404Spjd	 */
758219089Spjd	ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
759219089Spjd	bzero(ub, VDEV_UBERBLOCK_RING);
760168404Spjd	*ub = spa->spa_uberblock;
761168404Spjd	ub->ub_txg = 0;
762168404Spjd
763209962Smm	/* Initialize the 2nd padding area. */
764209962Smm	pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
765209962Smm	bzero(pad2, VDEV_PAD_SIZE);
766209962Smm
767168404Spjd	/*
768168404Spjd	 * Write everything in parallel.
769168404Spjd	 */
770213198Smmretry:
771185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
772168404Spjd
773185029Spjd	for (int l = 0; l < VDEV_LABELS; l++) {
774168404Spjd
775168404Spjd		vdev_label_write(zio, vd, l, vp,
776168404Spjd		    offsetof(vdev_label_t, vl_vdev_phys),
777185029Spjd		    sizeof (vdev_phys_t), NULL, NULL, flags);
778168404Spjd
779209962Smm		/*
780209962Smm		 * Skip the 1st padding area.
781209962Smm		 * Zero out the 2nd padding area where it might have
782209962Smm		 * left over data from previous filesystem format.
783209962Smm		 */
784209962Smm		vdev_label_write(zio, vd, l, pad2,
785209962Smm		    offsetof(vdev_label_t, vl_pad2),
786209962Smm		    VDEV_PAD_SIZE, NULL, NULL, flags);
787168404Spjd
788219089Spjd		vdev_label_write(zio, vd, l, ub,
789219089Spjd		    offsetof(vdev_label_t, vl_uberblock),
790219089Spjd		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
791168404Spjd	}
792168404Spjd
793168404Spjd	error = zio_wait(zio);
794168404Spjd
795213198Smm	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
796213198Smm		flags |= ZIO_FLAG_TRYHARD;
797213198Smm		goto retry;
798213198Smm	}
799213198Smm
800168404Spjd	nvlist_free(label);
801209962Smm	zio_buf_free(pad2, VDEV_PAD_SIZE);
802219089Spjd	zio_buf_free(ub, VDEV_UBERBLOCK_RING);
803168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
804168404Spjd
805168404Spjd	/*
806168404Spjd	 * If this vdev hasn't been previously identified as a spare, then we
807185029Spjd	 * mark it as such only if a) we are labeling it as a spare, or b) it
808185029Spjd	 * exists as a spare elsewhere in the system.  Do the same for
809185029Spjd	 * level 2 ARC devices.
810168404Spjd	 */
811168404Spjd	if (error == 0 && !vd->vdev_isspare &&
812168404Spjd	    (reason == VDEV_LABEL_SPARE ||
813185029Spjd	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
814168404Spjd		spa_spare_add(vd);
815168404Spjd
816185029Spjd	if (error == 0 && !vd->vdev_isl2cache &&
817185029Spjd	    (reason == VDEV_LABEL_L2CACHE ||
818185029Spjd	    spa_l2cache_exists(vd->vdev_guid, NULL)))
819185029Spjd		spa_l2cache_add(vd);
820185029Spjd
821168404Spjd	return (error);
822168404Spjd}
823168404Spjd
824168404Spjd/*
825168404Spjd * ==========================================================================
826168404Spjd * uberblock load/sync
827168404Spjd * ==========================================================================
828168404Spjd */
829168404Spjd
830168404Spjd/*
831168404Spjd * Consider the following situation: txg is safely synced to disk.  We've
832168404Spjd * written the first uberblock for txg + 1, and then we lose power.  When we
833168404Spjd * come back up, we fail to see the uberblock for txg + 1 because, say,
834168404Spjd * it was on a mirrored device and the replica to which we wrote txg + 1
835168404Spjd * is now offline.  If we then make some changes and sync txg + 1, and then
836168404Spjd * the missing replica comes back, then for a new seconds we'll have two
837168404Spjd * conflicting uberblocks on disk with the same txg.  The solution is simple:
838168404Spjd * among uberblocks with equal txg, choose the one with the latest timestamp.
839168404Spjd */
840168404Spjdstatic int
841168404Spjdvdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
842168404Spjd{
843168404Spjd	if (ub1->ub_txg < ub2->ub_txg)
844168404Spjd		return (-1);
845168404Spjd	if (ub1->ub_txg > ub2->ub_txg)
846168404Spjd		return (1);
847168404Spjd
848168404Spjd	if (ub1->ub_timestamp < ub2->ub_timestamp)
849168404Spjd		return (-1);
850168404Spjd	if (ub1->ub_timestamp > ub2->ub_timestamp)
851168404Spjd		return (1);
852168404Spjd
853168404Spjd	return (0);
854168404Spjd}
855168404Spjd
856168404Spjdstatic void
857168404Spjdvdev_uberblock_load_done(zio_t *zio)
858168404Spjd{
859219089Spjd	spa_t *spa = zio->io_spa;
860185029Spjd	zio_t *rio = zio->io_private;
861168404Spjd	uberblock_t *ub = zio->io_data;
862185029Spjd	uberblock_t *ubbest = rio->io_private;
863168404Spjd
864168404Spjd	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
865168404Spjd
866168404Spjd	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
867185029Spjd		mutex_enter(&rio->io_lock);
868219089Spjd		if (ub->ub_txg <= spa->spa_load_max_txg &&
869209962Smm		    vdev_uberblock_compare(ub, ubbest) > 0)
870168404Spjd			*ubbest = *ub;
871185029Spjd		mutex_exit(&rio->io_lock);
872168404Spjd	}
873168404Spjd
874168404Spjd	zio_buf_free(zio->io_data, zio->io_size);
875168404Spjd}
876168404Spjd
877168404Spjdvoid
878168404Spjdvdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
879168404Spjd{
880185029Spjd	spa_t *spa = vd->vdev_spa;
881185029Spjd	vdev_t *rvd = spa->spa_root_vdev;
882213198Smm	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
883213198Smm	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
884168404Spjd
885185029Spjd	if (vd == rvd) {
886185029Spjd		ASSERT(zio == NULL);
887185029Spjd		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
888185029Spjd		zio = zio_root(spa, NULL, ubbest, flags);
889185029Spjd		bzero(ubbest, sizeof (uberblock_t));
890185029Spjd	}
891168404Spjd
892185029Spjd	ASSERT(zio != NULL);
893168404Spjd
894185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
895185029Spjd		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
896168404Spjd
897185029Spjd	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
898185029Spjd		for (int l = 0; l < VDEV_LABELS; l++) {
899185029Spjd			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
900185029Spjd				vdev_label_read(zio, vd, l,
901185029Spjd				    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
902185029Spjd				    VDEV_UBERBLOCK_OFFSET(vd, n),
903185029Spjd				    VDEV_UBERBLOCK_SIZE(vd),
904185029Spjd				    vdev_uberblock_load_done, zio, flags);
905185029Spjd			}
906168404Spjd		}
907168404Spjd	}
908185029Spjd
909185029Spjd	if (vd == rvd) {
910185029Spjd		(void) zio_wait(zio);
911185029Spjd		spa_config_exit(spa, SCL_ALL, FTAG);
912185029Spjd	}
913168404Spjd}
914168404Spjd
915168404Spjd/*
916185029Spjd * On success, increment root zio's count of good writes.
917168404Spjd * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
918168404Spjd */
919168404Spjdstatic void
920168404Spjdvdev_uberblock_sync_done(zio_t *zio)
921168404Spjd{
922185029Spjd	uint64_t *good_writes = zio->io_private;
923168404Spjd
924168404Spjd	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
925168404Spjd		atomic_add_64(good_writes, 1);
926168404Spjd}
927168404Spjd
928185029Spjd/*
929185029Spjd * Write the uberblock to all labels of all leaves of the specified vdev.
930185029Spjd */
931168404Spjdstatic void
932185029Spjdvdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
933168404Spjd{
934185029Spjd	uberblock_t *ubbuf;
935185029Spjd	int n;
936168404Spjd
937185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
938185029Spjd		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
939168404Spjd
940168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
941168404Spjd		return;
942168404Spjd
943185029Spjd	if (!vdev_writeable(vd))
944168404Spjd		return;
945168404Spjd
946185029Spjd	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
947168404Spjd
948185029Spjd	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
949185029Spjd	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
950185029Spjd	*ubbuf = *ub;
951168404Spjd
952185029Spjd	for (int l = 0; l < VDEV_LABELS; l++)
953185029Spjd		vdev_label_write(zio, vd, l, ubbuf,
954185029Spjd		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
955185029Spjd		    vdev_uberblock_sync_done, zio->io_private,
956185029Spjd		    flags | ZIO_FLAG_DONT_PROPAGATE);
957168404Spjd
958185029Spjd	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
959168404Spjd}
960168404Spjd
961185029Spjdint
962185029Spjdvdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
963168404Spjd{
964185029Spjd	spa_t *spa = svd[0]->vdev_spa;
965168404Spjd	zio_t *zio;
966185029Spjd	uint64_t good_writes = 0;
967168404Spjd
968185029Spjd	zio = zio_root(spa, NULL, &good_writes, flags);
969168404Spjd
970185029Spjd	for (int v = 0; v < svdcount; v++)
971185029Spjd		vdev_uberblock_sync(zio, ub, svd[v], flags);
972168404Spjd
973185029Spjd	(void) zio_wait(zio);
974168404Spjd
975168404Spjd	/*
976185029Spjd	 * Flush the uberblocks to disk.  This ensures that the odd labels
977185029Spjd	 * are no longer needed (because the new uberblocks and the even
978185029Spjd	 * labels are safely on disk), so it is safe to overwrite them.
979168404Spjd	 */
980185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
981168404Spjd
982185029Spjd	for (int v = 0; v < svdcount; v++)
983185029Spjd		zio_flush(zio, svd[v]);
984168404Spjd
985185029Spjd	(void) zio_wait(zio);
986185029Spjd
987185029Spjd	return (good_writes >= 1 ? 0 : EIO);
988168404Spjd}
989168404Spjd
990168404Spjd/*
991185029Spjd * On success, increment the count of good writes for our top-level vdev.
992168404Spjd */
993168404Spjdstatic void
994185029Spjdvdev_label_sync_done(zio_t *zio)
995168404Spjd{
996185029Spjd	uint64_t *good_writes = zio->io_private;
997168404Spjd
998168404Spjd	if (zio->io_error == 0)
999168404Spjd		atomic_add_64(good_writes, 1);
1000168404Spjd}
1001168404Spjd
1002185029Spjd/*
1003185029Spjd * If there weren't enough good writes, indicate failure to the parent.
1004185029Spjd */
1005168404Spjdstatic void
1006185029Spjdvdev_label_sync_top_done(zio_t *zio)
1007168404Spjd{
1008185029Spjd	uint64_t *good_writes = zio->io_private;
1009185029Spjd
1010185029Spjd	if (*good_writes == 0)
1011185029Spjd		zio->io_error = EIO;
1012185029Spjd
1013185029Spjd	kmem_free(good_writes, sizeof (uint64_t));
1014185029Spjd}
1015185029Spjd
1016185029Spjd/*
1017185029Spjd * We ignore errors for log and cache devices, simply free the private data.
1018185029Spjd */
1019185029Spjdstatic void
1020185029Spjdvdev_label_sync_ignore_done(zio_t *zio)
1021185029Spjd{
1022185029Spjd	kmem_free(zio->io_private, sizeof (uint64_t));
1023185029Spjd}
1024185029Spjd
1025185029Spjd/*
1026185029Spjd * Write all even or odd labels to all leaves of the specified vdev.
1027185029Spjd */
1028185029Spjdstatic void
1029185029Spjdvdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
1030185029Spjd{
1031168404Spjd	nvlist_t *label;
1032168404Spjd	vdev_phys_t *vp;
1033168404Spjd	char *buf;
1034168404Spjd	size_t buflen;
1035168404Spjd
1036185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
1037185029Spjd		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
1038168404Spjd
1039168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
1040168404Spjd		return;
1041168404Spjd
1042185029Spjd	if (!vdev_writeable(vd))
1043168404Spjd		return;
1044168404Spjd
1045168404Spjd	/*
1046168404Spjd	 * Generate a label describing the top-level config to which we belong.
1047168404Spjd	 */
1048168404Spjd	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1049168404Spjd
1050168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
1051168404Spjd	bzero(vp, sizeof (vdev_phys_t));
1052168404Spjd
1053168404Spjd	buf = vp->vp_nvlist;
1054168404Spjd	buflen = sizeof (vp->vp_nvlist);
1055168404Spjd
1056185029Spjd	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
1057185029Spjd		for (; l < VDEV_LABELS; l += 2) {
1058185029Spjd			vdev_label_write(zio, vd, l, vp,
1059185029Spjd			    offsetof(vdev_label_t, vl_vdev_phys),
1060185029Spjd			    sizeof (vdev_phys_t),
1061185029Spjd			    vdev_label_sync_done, zio->io_private,
1062185029Spjd			    flags | ZIO_FLAG_DONT_PROPAGATE);
1063185029Spjd		}
1064185029Spjd	}
1065168404Spjd
1066168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
1067168404Spjd	nvlist_free(label);
1068168404Spjd}
1069168404Spjd
1070185029Spjdint
1071185029Spjdvdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1072168404Spjd{
1073185029Spjd	list_t *dl = &spa->spa_config_dirty_list;
1074185029Spjd	vdev_t *vd;
1075168404Spjd	zio_t *zio;
1076168404Spjd	int error;
1077168404Spjd
1078185029Spjd	/*
1079185029Spjd	 * Write the new labels to disk.
1080185029Spjd	 */
1081185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1082168404Spjd
1083185029Spjd	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
1084185029Spjd		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
1085185029Spjd		    KM_SLEEP);
1086219089Spjd
1087219089Spjd		ASSERT(!vd->vdev_ishole);
1088219089Spjd
1089209962Smm		zio_t *vio = zio_null(zio, spa, NULL,
1090185029Spjd		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
1091185029Spjd		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
1092185029Spjd		    good_writes, flags);
1093185029Spjd		vdev_label_sync(vio, vd, l, txg, flags);
1094185029Spjd		zio_nowait(vio);
1095185029Spjd	}
1096168404Spjd
1097185029Spjd	error = zio_wait(zio);
1098168404Spjd
1099168404Spjd	/*
1100185029Spjd	 * Flush the new labels to disk.
1101168404Spjd	 */
1102185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1103168404Spjd
1104185029Spjd	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
1105185029Spjd		zio_flush(zio, vd);
1106168404Spjd
1107185029Spjd	(void) zio_wait(zio);
1108168404Spjd
1109168404Spjd	return (error);
1110168404Spjd}
1111168404Spjd
1112168404Spjd/*
1113185029Spjd * Sync the uberblock and any changes to the vdev configuration.
1114168404Spjd *
1115168404Spjd * The order of operations is carefully crafted to ensure that
1116168404Spjd * if the system panics or loses power at any time, the state on disk
1117168404Spjd * is still transactionally consistent.  The in-line comments below
1118168404Spjd * describe the failure semantics at each stage.
1119168404Spjd *
1120185029Spjd * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1121168404Spjd * at any time, you can just call it again, and it will resume its work.
1122168404Spjd */
1123168404Spjdint
1124213198Smmvdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard)
1125168404Spjd{
1126185029Spjd	spa_t *spa = svd[0]->vdev_spa;
1127168404Spjd	uberblock_t *ub = &spa->spa_uberblock;
1128168404Spjd	vdev_t *vd;
1129168404Spjd	zio_t *zio;
1130185029Spjd	int error;
1131185029Spjd	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1132168404Spjd
1133213198Smm	/*
1134213198Smm	 * Normally, we don't want to try too hard to write every label and
1135213198Smm	 * uberblock.  If there is a flaky disk, we don't want the rest of the
1136213198Smm	 * sync process to block while we retry.  But if we can't write a
1137213198Smm	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
1138213198Smm	 * bailing out and declaring the pool faulted.
1139213198Smm	 */
1140213198Smm	if (tryhard)
1141213198Smm		flags |= ZIO_FLAG_TRYHARD;
1142213198Smm
1143168404Spjd	ASSERT(ub->ub_txg <= txg);
1144168404Spjd
1145168404Spjd	/*
1146185029Spjd	 * If this isn't a resync due to I/O errors,
1147185029Spjd	 * and nothing changed in this transaction group,
1148185029Spjd	 * and the vdev configuration hasn't changed,
1149168404Spjd	 * then there's nothing to do.
1150168404Spjd	 */
1151185029Spjd	if (ub->ub_txg < txg &&
1152185029Spjd	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
1153185029Spjd	    list_is_empty(&spa->spa_config_dirty_list))
1154168404Spjd		return (0);
1155168404Spjd
1156168404Spjd	if (txg > spa_freeze_txg(spa))
1157168404Spjd		return (0);
1158168404Spjd
1159168404Spjd	ASSERT(txg <= spa->spa_final_txg);
1160168404Spjd
1161168404Spjd	/*
1162168404Spjd	 * Flush the write cache of every disk that's been written to
1163168404Spjd	 * in this transaction group.  This ensures that all blocks
1164168404Spjd	 * written in this txg will be committed to stable storage
1165168404Spjd	 * before any uberblock that references them.
1166168404Spjd	 */
1167185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1168185029Spjd
1169168404Spjd	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
1170185029Spjd	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
1171185029Spjd		zio_flush(zio, vd);
1172185029Spjd
1173168404Spjd	(void) zio_wait(zio);
1174168404Spjd
1175168404Spjd	/*
1176168404Spjd	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1177168404Spjd	 * system dies in the middle of this process, that's OK: all of the
1178168404Spjd	 * even labels that made it to disk will be newer than any uberblock,
1179168404Spjd	 * and will therefore be considered invalid.  The odd labels (L1, L3),
1180185029Spjd	 * which have not yet been touched, will still be valid.  We flush
1181185029Spjd	 * the new labels to disk to ensure that all even-label updates
1182185029Spjd	 * are committed to stable storage before the uberblock update.
1183168404Spjd	 */
1184185029Spjd	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
1185185029Spjd		return (error);
1186168404Spjd
1187168404Spjd	/*
1188185029Spjd	 * Sync the uberblocks to all vdevs in svd[].
1189168404Spjd	 * If the system dies in the middle of this step, there are two cases
1190168404Spjd	 * to consider, and the on-disk state is consistent either way:
1191168404Spjd	 *
1192168404Spjd	 * (1)	If none of the new uberblocks made it to disk, then the
1193168404Spjd	 *	previous uberblock will be the newest, and the odd labels
1194168404Spjd	 *	(which had not yet been touched) will be valid with respect
1195168404Spjd	 *	to that uberblock.
1196168404Spjd	 *
1197168404Spjd	 * (2)	If one or more new uberblocks made it to disk, then they
1198168404Spjd	 *	will be the newest, and the even labels (which had all
1199168404Spjd	 *	been successfully committed) will be valid with respect
1200168404Spjd	 *	to the new uberblocks.
1201168404Spjd	 */
1202185029Spjd	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
1203168404Spjd		return (error);
1204168404Spjd
1205168404Spjd	/*
1206168404Spjd	 * Sync out odd labels for every dirty vdev.  If the system dies
1207168404Spjd	 * in the middle of this process, the even labels and the new
1208168404Spjd	 * uberblocks will suffice to open the pool.  The next time
1209168404Spjd	 * the pool is opened, the first thing we'll do -- before any
1210168404Spjd	 * user data is modified -- is mark every vdev dirty so that
1211185029Spjd	 * all labels will be brought up to date.  We flush the new labels
1212185029Spjd	 * to disk to ensure that all odd-label updates are committed to
1213185029Spjd	 * stable storage before the next transaction group begins.
1214168404Spjd	 */
1215185029Spjd	return (vdev_label_sync_list(spa, 1, txg, flags));
1216168404Spjd}
1217