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 */
21236884Smm
22168404Spjd/*
23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24289562Smav * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25168404Spjd */
26168404Spjd
27168404Spjd/*
28168404Spjd * Virtual Device Labels
29168404Spjd * ---------------------
30168404Spjd *
31168404Spjd * The vdev label serves several distinct purposes:
32168404Spjd *
33168404Spjd *	1. Uniquely identify this device as part of a ZFS pool and confirm its
34168404Spjd *	   identity within the pool.
35168404Spjd *
36168404Spjd * 	2. Verify that all the devices given in a configuration are present
37168404Spjd *         within the pool.
38168404Spjd *
39168404Spjd * 	3. Determine the uberblock for the pool.
40168404Spjd *
41168404Spjd * 	4. In case of an import operation, determine the configuration of the
42168404Spjd *         toplevel vdev of which it is a part.
43168404Spjd *
44168404Spjd * 	5. If an import operation cannot find all the devices in the pool,
45168404Spjd *         provide enough information to the administrator to determine which
46168404Spjd *         devices are missing.
47168404Spjd *
48168404Spjd * It is important to note that while the kernel is responsible for writing the
49168404Spjd * label, it only consumes the information in the first three cases.  The
50168404Spjd * latter information is only consumed in userland when determining the
51168404Spjd * configuration to import a pool.
52168404Spjd *
53168404Spjd *
54168404Spjd * Label Organization
55168404Spjd * ------------------
56168404Spjd *
57168404Spjd * Before describing the contents of the label, it's important to understand how
58168404Spjd * the labels are written and updated with respect to the uberblock.
59168404Spjd *
60168404Spjd * When the pool configuration is altered, either because it was newly created
61168404Spjd * or a device was added, we want to update all the labels such that we can deal
62168404Spjd * with fatal failure at any point.  To this end, each disk has two labels which
63168404Spjd * are updated before and after the uberblock is synced.  Assuming we have
64185029Spjd * labels and an uberblock with the following transaction groups:
65168404Spjd *
66168404Spjd *              L1          UB          L2
67168404Spjd *           +------+    +------+    +------+
68168404Spjd *           |      |    |      |    |      |
69168404Spjd *           | t10  |    | t10  |    | t10  |
70168404Spjd *           |      |    |      |    |      |
71168404Spjd *           +------+    +------+    +------+
72168404Spjd *
73168404Spjd * In this stable state, the labels and the uberblock were all updated within
74168404Spjd * the same transaction group (10).  Each label is mirrored and checksummed, so
75168404Spjd * that we can detect when we fail partway through writing the label.
76168404Spjd *
77168404Spjd * In order to identify which labels are valid, the labels are written in the
78168404Spjd * following manner:
79168404Spjd *
80168404Spjd * 	1. For each vdev, update 'L1' to the new label
81168404Spjd * 	2. Update the uberblock
82168404Spjd * 	3. For each vdev, update 'L2' to the new label
83168404Spjd *
84168404Spjd * Given arbitrary failure, we can determine the correct label to use based on
85168404Spjd * the transaction group.  If we fail after updating L1 but before updating the
86168404Spjd * UB, we will notice that L1's transaction group is greater than the uberblock,
87168404Spjd * so L2 must be valid.  If we fail after writing the uberblock but before
88168404Spjd * writing L2, we will notice that L2's transaction group is less than L1, and
89168404Spjd * therefore L1 is valid.
90168404Spjd *
91168404Spjd * Another added complexity is that not every label is updated when the config
92168404Spjd * is synced.  If we add a single device, we do not want to have to re-write
93168404Spjd * every label for every device in the pool.  This means that both L1 and L2 may
94168404Spjd * be older than the pool uberblock, because the necessary information is stored
95168404Spjd * on another vdev.
96168404Spjd *
97168404Spjd *
98168404Spjd * On-disk Format
99168404Spjd * --------------
100168404Spjd *
101168404Spjd * The vdev label consists of two distinct parts, and is wrapped within the
102168404Spjd * vdev_label_t structure.  The label includes 8k of padding to permit legacy
103168404Spjd * VTOC disk labels, but is otherwise ignored.
104168404Spjd *
105168404Spjd * The first half of the label is a packed nvlist which contains pool wide
106168404Spjd * properties, per-vdev properties, and configuration information.  It is
107168404Spjd * described in more detail below.
108168404Spjd *
109168404Spjd * The latter half of the label consists of a redundant array of uberblocks.
110168404Spjd * These uberblocks are updated whenever a transaction group is committed,
111168404Spjd * or when the configuration is updated.  When a pool is loaded, we scan each
112168404Spjd * vdev for the 'best' uberblock.
113168404Spjd *
114168404Spjd *
115168404Spjd * Configuration Information
116168404Spjd * -------------------------
117168404Spjd *
118168404Spjd * The nvlist describing the pool and vdev contains the following elements:
119168404Spjd *
120168404Spjd * 	version		ZFS on-disk version
121168404Spjd * 	name		Pool name
122168404Spjd * 	state		Pool state
123168404Spjd * 	txg		Transaction group in which this label was written
124168404Spjd * 	pool_guid	Unique identifier for this pool
125168404Spjd * 	vdev_tree	An nvlist describing vdev tree.
126236884Smm *	features_for_read
127236884Smm *			An nvlist of the features necessary for reading the MOS.
128168404Spjd *
129168404Spjd * Each leaf device label also contains the following:
130168404Spjd *
131168404Spjd * 	top_guid	Unique ID for top-level vdev in which this is contained
132168404Spjd * 	guid		Unique ID for the leaf vdev
133168404Spjd *
134168404Spjd * The 'vs' configuration follows the format described in 'spa_config.c'.
135168404Spjd */
136168404Spjd
137168404Spjd#include <sys/zfs_context.h>
138168404Spjd#include <sys/spa.h>
139168404Spjd#include <sys/spa_impl.h>
140168404Spjd#include <sys/dmu.h>
141168404Spjd#include <sys/zap.h>
142168404Spjd#include <sys/vdev.h>
143168404Spjd#include <sys/vdev_impl.h>
144168404Spjd#include <sys/uberblock_impl.h>
145168404Spjd#include <sys/metaslab.h>
146168404Spjd#include <sys/zio.h>
147219089Spjd#include <sys/dsl_scan.h>
148240868Spjd#include <sys/trim_map.h>
149168404Spjd#include <sys/fs/zfs.h>
150168404Spjd
151244188Ssmhstatic boolean_t vdev_trim_on_init = B_TRUE;
152244188SsmhSYSCTL_DECL(_vfs_zfs_vdev);
153244188SsmhSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, trim_on_init, CTLFLAG_RW,
154244188Ssmh    &vdev_trim_on_init, 0, "Enable/disable full vdev trim on initialisation");
155244188Ssmh
156168404Spjd/*
157168404Spjd * Basic routines to read and write from a vdev label.
158168404Spjd * Used throughout the rest of this file.
159168404Spjd */
160168404Spjduint64_t
161168404Spjdvdev_label_offset(uint64_t psize, int l, uint64_t offset)
162168404Spjd{
163168404Spjd	ASSERT(offset < sizeof (vdev_label_t));
164185029Spjd	ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
165168404Spjd
166168404Spjd	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
167168404Spjd	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
168168404Spjd}
169168404Spjd
170185029Spjd/*
171185029Spjd * Returns back the vdev label associated with the passed in offset.
172185029Spjd */
173185029Spjdint
174185029Spjdvdev_label_number(uint64_t psize, uint64_t offset)
175185029Spjd{
176185029Spjd	int l;
177185029Spjd
178185029Spjd	if (offset >= psize - VDEV_LABEL_END_SIZE) {
179185029Spjd		offset -= psize - VDEV_LABEL_END_SIZE;
180185029Spjd		offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
181185029Spjd	}
182185029Spjd	l = offset / sizeof (vdev_label_t);
183185029Spjd	return (l < VDEV_LABELS ? l : -1);
184185029Spjd}
185185029Spjd
186168404Spjdstatic void
187168404Spjdvdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
188289562Smav    uint64_t size, zio_done_func_t *done, void *private, int flags)
189168404Spjd{
190185029Spjd	ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
191185029Spjd	    SCL_STATE_ALL);
192185029Spjd	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
193168404Spjd
194168404Spjd	zio_nowait(zio_read_phys(zio, vd,
195168404Spjd	    vdev_label_offset(vd->vdev_psize, l, offset),
196168404Spjd	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
197185029Spjd	    ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
198168404Spjd}
199168404Spjd
200168404Spjdstatic void
201168404Spjdvdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
202289562Smav    uint64_t size, zio_done_func_t *done, void *private, int flags)
203168404Spjd{
204185029Spjd	ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
205185029Spjd	    (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
206185029Spjd	    (SCL_CONFIG | SCL_STATE) &&
207185029Spjd	    dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
208185029Spjd	ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
209168404Spjd
210168404Spjd	zio_nowait(zio_write_phys(zio, vd,
211168404Spjd	    vdev_label_offset(vd->vdev_psize, l, offset),
212168404Spjd	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
213185029Spjd	    ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
214168404Spjd}
215168404Spjd
216168404Spjd/*
217168404Spjd * Generate the nvlist representing this vdev's config.
218168404Spjd */
219168404Spjdnvlist_t *
220168404Spjdvdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
221219089Spjd    vdev_config_flag_t flags)
222168404Spjd{
223168404Spjd	nvlist_t *nv = NULL;
224168404Spjd
225254112Sdelphij	nv = fnvlist_alloc();
226168404Spjd
227254112Sdelphij	fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
228219089Spjd	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
229254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
230254112Sdelphij	fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
231168404Spjd
232168404Spjd	if (vd->vdev_path != NULL)
233254112Sdelphij		fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
234168404Spjd
235168404Spjd	if (vd->vdev_devid != NULL)
236254112Sdelphij		fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
237168404Spjd
238185029Spjd	if (vd->vdev_physpath != NULL)
239254112Sdelphij		fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
240254112Sdelphij		    vd->vdev_physpath);
241185029Spjd
242209962Smm	if (vd->vdev_fru != NULL)
243254112Sdelphij		fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
244209962Smm
245168404Spjd	if (vd->vdev_nparity != 0) {
246168404Spjd		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
247168404Spjd		    VDEV_TYPE_RAIDZ) == 0);
248168404Spjd
249168404Spjd		/*
250168404Spjd		 * Make sure someone hasn't managed to sneak a fancy new vdev
251168404Spjd		 * into a crufty old storage pool.
252168404Spjd		 */
253168404Spjd		ASSERT(vd->vdev_nparity == 1 ||
254219089Spjd		    (vd->vdev_nparity <= 2 &&
255219089Spjd		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
256219089Spjd		    (vd->vdev_nparity <= 3 &&
257219089Spjd		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
258168404Spjd
259168404Spjd		/*
260168404Spjd		 * Note that we'll add the nparity tag even on storage pools
261168404Spjd		 * that only support a single parity device -- older software
262168404Spjd		 * will just ignore it.
263168404Spjd		 */
264254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
265168404Spjd	}
266168404Spjd
267168404Spjd	if (vd->vdev_wholedisk != -1ULL)
268254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
269254112Sdelphij		    vd->vdev_wholedisk);
270168404Spjd
271168404Spjd	if (vd->vdev_not_present)
272254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
273168404Spjd
274168404Spjd	if (vd->vdev_isspare)
275254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
276168404Spjd
277219089Spjd	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
278219089Spjd	    vd == vd->vdev_top) {
279254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
280254112Sdelphij		    vd->vdev_ms_array);
281254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
282254112Sdelphij		    vd->vdev_ms_shift);
283254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
284254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
285254112Sdelphij		    vd->vdev_asize);
286254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
287219089Spjd		if (vd->vdev_removing)
288254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
289254112Sdelphij			    vd->vdev_removing);
290168404Spjd	}
291168404Spjd
292258717Savg	if (vd->vdev_dtl_sm != NULL) {
293254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
294258717Savg		    space_map_object(vd->vdev_dtl_sm));
295258717Savg	}
296168404Spjd
297219089Spjd	if (vd->vdev_crtxg)
298254112Sdelphij		fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
299219089Spjd
300299441Smav	if (flags & VDEV_CONFIG_MOS) {
301299441Smav		if (vd->vdev_leaf_zap != 0) {
302299441Smav			ASSERT(vd->vdev_ops->vdev_op_leaf);
303299441Smav			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
304299441Smav			    vd->vdev_leaf_zap);
305299441Smav		}
306299441Smav
307299441Smav		if (vd->vdev_top_zap != 0) {
308299441Smav			ASSERT(vd == vd->vdev_top);
309299441Smav			fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
310299441Smav			    vd->vdev_top_zap);
311299441Smav		}
312299441Smav	}
313299441Smav
314168404Spjd	if (getstats) {
315168404Spjd		vdev_stat_t vs;
316219089Spjd		pool_scan_stat_t ps;
317219089Spjd
318168404Spjd		vdev_get_stats(vd, &vs);
319254112Sdelphij		fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
320254112Sdelphij		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t));
321219089Spjd
322219089Spjd		/* provide either current or previous scan information */
323219089Spjd		if (spa_scan_get_stats(spa, &ps) == 0) {
324254112Sdelphij			fnvlist_add_uint64_array(nv,
325219089Spjd			    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
326254112Sdelphij			    sizeof (pool_scan_stat_t) / sizeof (uint64_t));
327219089Spjd		}
328168404Spjd	}
329168404Spjd
330168404Spjd	if (!vd->vdev_ops->vdev_op_leaf) {
331168404Spjd		nvlist_t **child;
332219089Spjd		int c, idx;
333168404Spjd
334219089Spjd		ASSERT(!vd->vdev_ishole);
335219089Spjd
336168404Spjd		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
337168404Spjd		    KM_SLEEP);
338168404Spjd
339219089Spjd		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
340219089Spjd			vdev_t *cvd = vd->vdev_child[c];
341168404Spjd
342219089Spjd			/*
343219089Spjd			 * If we're generating an nvlist of removing
344219089Spjd			 * vdevs then skip over any device which is
345219089Spjd			 * not being removed.
346219089Spjd			 */
347219089Spjd			if ((flags & VDEV_CONFIG_REMOVING) &&
348219089Spjd			    !cvd->vdev_removing)
349219089Spjd				continue;
350168404Spjd
351219089Spjd			child[idx++] = vdev_config_generate(spa, cvd,
352219089Spjd			    getstats, flags);
353219089Spjd		}
354219089Spjd
355219089Spjd		if (idx) {
356254112Sdelphij			fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
357254112Sdelphij			    child, idx);
358219089Spjd		}
359219089Spjd
360219089Spjd		for (c = 0; c < idx; c++)
361168404Spjd			nvlist_free(child[c]);
362168404Spjd
363168404Spjd		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
364168404Spjd
365168404Spjd	} else {
366219089Spjd		const char *aux = NULL;
367219089Spjd
368168404Spjd		if (vd->vdev_offline && !vd->vdev_tmpoffline)
369254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
370254112Sdelphij		if (vd->vdev_resilver_txg != 0)
371254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
372254112Sdelphij			    vd->vdev_resilver_txg);
373185029Spjd		if (vd->vdev_faulted)
374254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
375185029Spjd		if (vd->vdev_degraded)
376254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
377185029Spjd		if (vd->vdev_removed)
378254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
379185029Spjd		if (vd->vdev_unspare)
380254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
381219089Spjd		if (vd->vdev_ishole)
382254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
383219089Spjd
384219089Spjd		switch (vd->vdev_stat.vs_aux) {
385219089Spjd		case VDEV_AUX_ERR_EXCEEDED:
386219089Spjd			aux = "err_exceeded";
387219089Spjd			break;
388219089Spjd
389219089Spjd		case VDEV_AUX_EXTERNAL:
390219089Spjd			aux = "external";
391219089Spjd			break;
392219089Spjd		}
393219089Spjd
394219089Spjd		if (aux != NULL)
395254112Sdelphij			fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
396219089Spjd
397219089Spjd		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
398254112Sdelphij			fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
399254112Sdelphij			    vd->vdev_orig_guid);
400219089Spjd		}
401168404Spjd	}
402168404Spjd
403168404Spjd	return (nv);
404168404Spjd}
405168404Spjd
406219089Spjd/*
407219089Spjd * Generate a view of the top-level vdevs.  If we currently have holes
408219089Spjd * in the namespace, then generate an array which contains a list of holey
409219089Spjd * vdevs.  Additionally, add the number of top-level children that currently
410219089Spjd * exist.
411219089Spjd */
412219089Spjdvoid
413219089Spjdvdev_top_config_generate(spa_t *spa, nvlist_t *config)
414219089Spjd{
415219089Spjd	vdev_t *rvd = spa->spa_root_vdev;
416219089Spjd	uint64_t *array;
417219089Spjd	uint_t c, idx;
418219089Spjd
419219089Spjd	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
420219089Spjd
421219089Spjd	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
422219089Spjd		vdev_t *tvd = rvd->vdev_child[c];
423219089Spjd
424219089Spjd		if (tvd->vdev_ishole)
425219089Spjd			array[idx++] = c;
426219089Spjd	}
427219089Spjd
428219089Spjd	if (idx) {
429219089Spjd		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
430219089Spjd		    array, idx) == 0);
431219089Spjd	}
432219089Spjd
433219089Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
434219089Spjd	    rvd->vdev_children) == 0);
435219089Spjd
436219089Spjd	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
437219089Spjd}
438219089Spjd
439236884Smm/*
440239620Smm * Returns the configuration from the label of the given vdev. For vdevs
441239620Smm * which don't have a txg value stored on their label (i.e. spares/cache)
442239620Smm * or have not been completely initialized (txg = 0) just return
443239620Smm * the configuration from the first valid label we find. Otherwise,
444239620Smm * find the most up-to-date label that does not exceed the specified
445239620Smm * 'txg' value.
446236884Smm */
447168404Spjdnvlist_t *
448239620Smmvdev_label_read_config(vdev_t *vd, uint64_t txg)
449168404Spjd{
450168404Spjd	spa_t *spa = vd->vdev_spa;
451168404Spjd	nvlist_t *config = NULL;
452168404Spjd	vdev_phys_t *vp;
453168404Spjd	zio_t *zio;
454239620Smm	uint64_t best_txg = 0;
455239620Smm	int error = 0;
456213198Smm	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
457213198Smm	    ZIO_FLAG_SPECULATIVE;
458168404Spjd
459185029Spjd	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
460168404Spjd
461185029Spjd	if (!vdev_readable(vd))
462168404Spjd		return (NULL);
463168404Spjd
464168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
465168404Spjd
466213198Smmretry:
467185029Spjd	for (int l = 0; l < VDEV_LABELS; l++) {
468239620Smm		nvlist_t *label = NULL;
469168404Spjd
470185029Spjd		zio = zio_root(spa, NULL, NULL, flags);
471168404Spjd
472168404Spjd		vdev_label_read(zio, vd, l, vp,
473168404Spjd		    offsetof(vdev_label_t, vl_vdev_phys),
474185029Spjd		    sizeof (vdev_phys_t), NULL, NULL, flags);
475168404Spjd
476168404Spjd		if (zio_wait(zio) == 0 &&
477168404Spjd		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
478239620Smm		    &label, 0) == 0) {
479239620Smm			uint64_t label_txg = 0;
480168404Spjd
481239620Smm			/*
482239620Smm			 * Auxiliary vdevs won't have txg values in their
483239620Smm			 * labels and newly added vdevs may not have been
484239620Smm			 * completely initialized so just return the
485239620Smm			 * configuration from the first valid label we
486239620Smm			 * encounter.
487239620Smm			 */
488239620Smm			error = nvlist_lookup_uint64(label,
489239620Smm			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
490239620Smm			if ((error || label_txg == 0) && !config) {
491239620Smm				config = label;
492239620Smm				break;
493239620Smm			} else if (label_txg <= txg && label_txg > best_txg) {
494239620Smm				best_txg = label_txg;
495239620Smm				nvlist_free(config);
496239620Smm				config = fnvlist_dup(label);
497239620Smm			}
498168404Spjd		}
499239620Smm
500239620Smm		if (label != NULL) {
501239620Smm			nvlist_free(label);
502239620Smm			label = NULL;
503239620Smm		}
504168404Spjd	}
505168404Spjd
506213198Smm	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
507213198Smm		flags |= ZIO_FLAG_TRYHARD;
508213198Smm		goto retry;
509213198Smm	}
510213198Smm
511168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
512168404Spjd
513168404Spjd	return (config);
514168404Spjd}
515168404Spjd
516168404Spjd/*
517168404Spjd * Determine if a device is in use.  The 'spare_guid' parameter will be filled
518168404Spjd * in with the device guid if this spare is active elsewhere on the system.
519168404Spjd */
520168404Spjdstatic boolean_t
521168404Spjdvdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
522185029Spjd    uint64_t *spare_guid, uint64_t *l2cache_guid)
523168404Spjd{
524168404Spjd	spa_t *spa = vd->vdev_spa;
525168404Spjd	uint64_t state, pool_guid, device_guid, txg, spare_pool;
526168404Spjd	uint64_t vdtxg = 0;
527168404Spjd	nvlist_t *label;
528168404Spjd
529168404Spjd	if (spare_guid)
530168404Spjd		*spare_guid = 0ULL;
531185029Spjd	if (l2cache_guid)
532185029Spjd		*l2cache_guid = 0ULL;
533168404Spjd
534168404Spjd	/*
535168404Spjd	 * Read the label, if any, and perform some basic sanity checks.
536168404Spjd	 */
537239620Smm	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
538168404Spjd		return (B_FALSE);
539168404Spjd
540168404Spjd	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
541168404Spjd	    &vdtxg);
542168404Spjd
543168404Spjd	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
544168404Spjd	    &state) != 0 ||
545168404Spjd	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
546168404Spjd	    &device_guid) != 0) {
547168404Spjd		nvlist_free(label);
548168404Spjd		return (B_FALSE);
549168404Spjd	}
550168404Spjd
551185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
552168404Spjd	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
553168404Spjd	    &pool_guid) != 0 ||
554168404Spjd	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
555168404Spjd	    &txg) != 0)) {
556168404Spjd		nvlist_free(label);
557168404Spjd		return (B_FALSE);
558168404Spjd	}
559168404Spjd
560168404Spjd	nvlist_free(label);
561168404Spjd
562168404Spjd	/*
563168404Spjd	 * Check to see if this device indeed belongs to the pool it claims to
564168404Spjd	 * be a part of.  The only way this is allowed is if the device is a hot
565168404Spjd	 * spare (which we check for later on).
566168404Spjd	 */
567185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
568168404Spjd	    !spa_guid_exists(pool_guid, device_guid) &&
569185029Spjd	    !spa_spare_exists(device_guid, NULL, NULL) &&
570185029Spjd	    !spa_l2cache_exists(device_guid, NULL))
571168404Spjd		return (B_FALSE);
572168404Spjd
573168404Spjd	/*
574168404Spjd	 * If the transaction group is zero, then this an initialized (but
575168404Spjd	 * unused) label.  This is only an error if the create transaction
576168404Spjd	 * on-disk is the same as the one we're using now, in which case the
577168404Spjd	 * user has attempted to add the same vdev multiple times in the same
578168404Spjd	 * transaction.
579168404Spjd	 */
580185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
581185029Spjd	    txg == 0 && vdtxg == crtxg)
582168404Spjd		return (B_TRUE);
583168404Spjd
584168404Spjd	/*
585168404Spjd	 * Check to see if this is a spare device.  We do an explicit check for
586168404Spjd	 * spa_has_spare() here because it may be on our pending list of spares
587185029Spjd	 * to add.  We also check if it is an l2cache device.
588168404Spjd	 */
589185029Spjd	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
590168404Spjd	    spa_has_spare(spa, device_guid)) {
591168404Spjd		if (spare_guid)
592168404Spjd			*spare_guid = device_guid;
593168404Spjd
594168404Spjd		switch (reason) {
595168404Spjd		case VDEV_LABEL_CREATE:
596185029Spjd		case VDEV_LABEL_L2CACHE:
597168404Spjd			return (B_TRUE);
598168404Spjd
599168404Spjd		case VDEV_LABEL_REPLACE:
600168404Spjd			return (!spa_has_spare(spa, device_guid) ||
601168404Spjd			    spare_pool != 0ULL);
602168404Spjd
603168404Spjd		case VDEV_LABEL_SPARE:
604168404Spjd			return (spa_has_spare(spa, device_guid));
605168404Spjd		}
606168404Spjd	}
607168404Spjd
608168404Spjd	/*
609185029Spjd	 * Check to see if this is an l2cache device.
610185029Spjd	 */
611185029Spjd	if (spa_l2cache_exists(device_guid, NULL))
612185029Spjd		return (B_TRUE);
613185029Spjd
614185029Spjd	/*
615219089Spjd	 * We can't rely on a pool's state if it's been imported
616219089Spjd	 * read-only.  Instead we look to see if the pools is marked
617219089Spjd	 * read-only in the namespace and set the state to active.
618219089Spjd	 */
619294805Smav	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
620294805Smav	    (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
621219089Spjd	    spa_mode(spa) == FREAD)
622219089Spjd		state = POOL_STATE_ACTIVE;
623219089Spjd
624219089Spjd	/*
625168404Spjd	 * If the device is marked ACTIVE, then this device is in use by another
626168404Spjd	 * pool on the system.
627168404Spjd	 */
628168404Spjd	return (state == POOL_STATE_ACTIVE);
629168404Spjd}
630168404Spjd
631168404Spjd/*
632168404Spjd * Initialize a vdev label.  We check to make sure each leaf device is not in
633168404Spjd * use, and writable.  We put down an initial label which we will later
634168404Spjd * overwrite with a complete label.  Note that it's important to do this
635168404Spjd * sequentially, not in parallel, so that we catch cases of multiple use of the
636168404Spjd * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
637168404Spjd * itself.
638168404Spjd */
639168404Spjdint
640168404Spjdvdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
641168404Spjd{
642168404Spjd	spa_t *spa = vd->vdev_spa;
643168404Spjd	nvlist_t *label;
644168404Spjd	vdev_phys_t *vp;
645209962Smm	char *pad2;
646168404Spjd	uberblock_t *ub;
647168404Spjd	zio_t *zio;
648168404Spjd	char *buf;
649168404Spjd	size_t buflen;
650168404Spjd	int error;
651185029Spjd	uint64_t spare_guid, l2cache_guid;
652185029Spjd	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
653168404Spjd
654185029Spjd	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
655168404Spjd
656185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
657168404Spjd		if ((error = vdev_label_init(vd->vdev_child[c],
658168404Spjd		    crtxg, reason)) != 0)
659168404Spjd			return (error);
660168404Spjd
661219089Spjd	/* Track the creation time for this vdev */
662219089Spjd	vd->vdev_crtxg = crtxg;
663219089Spjd
664260157Sdelphij	if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
665168404Spjd		return (0);
666168404Spjd
667168404Spjd	/*
668168404Spjd	 * Dead vdevs cannot be initialized.
669168404Spjd	 */
670168404Spjd	if (vdev_is_dead(vd))
671249195Smm		return (SET_ERROR(EIO));
672168404Spjd
673168404Spjd	/*
674168404Spjd	 * Determine if the vdev is in use.
675168404Spjd	 */
676219089Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
677185029Spjd	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
678249195Smm		return (SET_ERROR(EBUSY));
679168404Spjd
680168404Spjd	/*
681185029Spjd	 * If this is a request to add or replace a spare or l2cache device
682185029Spjd	 * that is in use elsewhere on the system, then we must update the
683185029Spjd	 * guid (which was initialized to a random value) to reflect the
684185029Spjd	 * actual GUID (which is shared between multiple pools).
685168404Spjd	 */
686185029Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
687185029Spjd	    spare_guid != 0ULL) {
688185029Spjd		uint64_t guid_delta = spare_guid - vd->vdev_guid;
689168404Spjd
690185029Spjd		vd->vdev_guid += guid_delta;
691168404Spjd
692185029Spjd		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
693185029Spjd			pvd->vdev_guid_sum += guid_delta;
694168404Spjd
695168404Spjd		/*
696168404Spjd		 * If this is a replacement, then we want to fallthrough to the
697168404Spjd		 * rest of the code.  If we're adding a spare, then it's already
698185029Spjd		 * labeled appropriately and we can just return.
699168404Spjd		 */
700168404Spjd		if (reason == VDEV_LABEL_SPARE)
701168404Spjd			return (0);
702219089Spjd		ASSERT(reason == VDEV_LABEL_REPLACE ||
703219089Spjd		    reason == VDEV_LABEL_SPLIT);
704168404Spjd	}
705168404Spjd
706185029Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
707185029Spjd	    l2cache_guid != 0ULL) {
708185029Spjd		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
709185029Spjd
710185029Spjd		vd->vdev_guid += guid_delta;
711185029Spjd
712185029Spjd		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
713185029Spjd			pvd->vdev_guid_sum += guid_delta;
714185029Spjd
715185029Spjd		/*
716185029Spjd		 * If this is a replacement, then we want to fallthrough to the
717185029Spjd		 * rest of the code.  If we're adding an l2cache, then it's
718185029Spjd		 * already labeled appropriately and we can just return.
719185029Spjd		 */
720185029Spjd		if (reason == VDEV_LABEL_L2CACHE)
721185029Spjd			return (0);
722185029Spjd		ASSERT(reason == VDEV_LABEL_REPLACE);
723185029Spjd	}
724185029Spjd
725168404Spjd	/*
726240868Spjd	 * TRIM the whole thing so that we start with a clean slate.
727240868Spjd	 * It's just an optimization, so we don't care if it fails.
728240868Spjd	 * Don't TRIM if removing so that we don't interfere with zpool
729240868Spjd	 * disaster recovery.
730240868Spjd	 */
731274619Ssmh	if (zfs_trim_enabled && vdev_trim_on_init && !vd->vdev_notrim &&
732274619Ssmh	    (reason == VDEV_LABEL_CREATE || reason == VDEV_LABEL_SPARE ||
733274619Ssmh	    reason == VDEV_LABEL_L2CACHE))
734240868Spjd		zio_wait(zio_trim(NULL, spa, vd, 0, vd->vdev_psize));
735240868Spjd
736240868Spjd	/*
737168404Spjd	 * Initialize its label.
738168404Spjd	 */
739168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
740168404Spjd	bzero(vp, sizeof (vdev_phys_t));
741168404Spjd
742168404Spjd	/*
743168404Spjd	 * Generate a label describing the pool and our top-level vdev.
744168404Spjd	 * We mark it as being from txg 0 to indicate that it's not
745168404Spjd	 * really part of an active pool just yet.  The labels will
746168404Spjd	 * be written again with a meaningful txg by spa_sync().
747168404Spjd	 */
748168404Spjd	if (reason == VDEV_LABEL_SPARE ||
749168404Spjd	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
750168404Spjd		/*
751168404Spjd		 * For inactive hot spares, we generate a special label that
752168404Spjd		 * identifies as a mutually shared hot spare.  We write the
753168404Spjd		 * label if we are adding a hot spare, or if we are removing an
754168404Spjd		 * active hot spare (in which case we want to revert the
755168404Spjd		 * labels).
756168404Spjd		 */
757168404Spjd		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
758168404Spjd
759168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
760168404Spjd		    spa_version(spa)) == 0);
761168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
762168404Spjd		    POOL_STATE_SPARE) == 0);
763168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
764168404Spjd		    vd->vdev_guid) == 0);
765185029Spjd	} else if (reason == VDEV_LABEL_L2CACHE ||
766185029Spjd	    (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
767185029Spjd		/*
768185029Spjd		 * For level 2 ARC devices, add a special label.
769185029Spjd		 */
770185029Spjd		VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
771185029Spjd
772185029Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
773185029Spjd		    spa_version(spa)) == 0);
774185029Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
775185029Spjd		    POOL_STATE_L2CACHE) == 0);
776185029Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
777185029Spjd		    vd->vdev_guid) == 0);
778168404Spjd	} else {
779219089Spjd		uint64_t txg = 0ULL;
780168404Spjd
781219089Spjd		if (reason == VDEV_LABEL_SPLIT)
782219089Spjd			txg = spa->spa_uberblock.ub_txg;
783219089Spjd		label = spa_config_generate(spa, vd, txg, B_FALSE);
784219089Spjd
785168404Spjd		/*
786168404Spjd		 * Add our creation time.  This allows us to detect multiple
787168404Spjd		 * vdev uses as described above, and automatically expires if we
788168404Spjd		 * fail.
789168404Spjd		 */
790168404Spjd		VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
791168404Spjd		    crtxg) == 0);
792168404Spjd	}
793168404Spjd
794168404Spjd	buf = vp->vp_nvlist;
795168404Spjd	buflen = sizeof (vp->vp_nvlist);
796168404Spjd
797168404Spjd	error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
798168404Spjd	if (error != 0) {
799168404Spjd		nvlist_free(label);
800168404Spjd		zio_buf_free(vp, sizeof (vdev_phys_t));
801168404Spjd		/* EFAULT means nvlist_pack ran out of room */
802168404Spjd		return (error == EFAULT ? ENAMETOOLONG : EINVAL);
803168404Spjd	}
804168404Spjd
805168404Spjd	/*
806168404Spjd	 * Initialize uberblock template.
807168404Spjd	 */
808219089Spjd	ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
809219089Spjd	bzero(ub, VDEV_UBERBLOCK_RING);
810168404Spjd	*ub = spa->spa_uberblock;
811168404Spjd	ub->ub_txg = 0;
812168404Spjd
813209962Smm	/* Initialize the 2nd padding area. */
814209962Smm	pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
815209962Smm	bzero(pad2, VDEV_PAD_SIZE);
816209962Smm
817168404Spjd	/*
818168404Spjd	 * Write everything in parallel.
819168404Spjd	 */
820213198Smmretry:
821185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
822168404Spjd
823185029Spjd	for (int l = 0; l < VDEV_LABELS; l++) {
824168404Spjd
825168404Spjd		vdev_label_write(zio, vd, l, vp,
826168404Spjd		    offsetof(vdev_label_t, vl_vdev_phys),
827185029Spjd		    sizeof (vdev_phys_t), NULL, NULL, flags);
828168404Spjd
829209962Smm		/*
830209962Smm		 * Skip the 1st padding area.
831209962Smm		 * Zero out the 2nd padding area where it might have
832209962Smm		 * left over data from previous filesystem format.
833209962Smm		 */
834209962Smm		vdev_label_write(zio, vd, l, pad2,
835209962Smm		    offsetof(vdev_label_t, vl_pad2),
836209962Smm		    VDEV_PAD_SIZE, NULL, NULL, flags);
837168404Spjd
838219089Spjd		vdev_label_write(zio, vd, l, ub,
839219089Spjd		    offsetof(vdev_label_t, vl_uberblock),
840219089Spjd		    VDEV_UBERBLOCK_RING, NULL, NULL, flags);
841168404Spjd	}
842168404Spjd
843168404Spjd	error = zio_wait(zio);
844168404Spjd
845213198Smm	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
846213198Smm		flags |= ZIO_FLAG_TRYHARD;
847213198Smm		goto retry;
848213198Smm	}
849213198Smm
850168404Spjd	nvlist_free(label);
851209962Smm	zio_buf_free(pad2, VDEV_PAD_SIZE);
852219089Spjd	zio_buf_free(ub, VDEV_UBERBLOCK_RING);
853168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
854168404Spjd
855168404Spjd	/*
856168404Spjd	 * If this vdev hasn't been previously identified as a spare, then we
857185029Spjd	 * mark it as such only if a) we are labeling it as a spare, or b) it
858185029Spjd	 * exists as a spare elsewhere in the system.  Do the same for
859185029Spjd	 * level 2 ARC devices.
860168404Spjd	 */
861168404Spjd	if (error == 0 && !vd->vdev_isspare &&
862168404Spjd	    (reason == VDEV_LABEL_SPARE ||
863185029Spjd	    spa_spare_exists(vd->vdev_guid, NULL, NULL)))
864168404Spjd		spa_spare_add(vd);
865168404Spjd
866185029Spjd	if (error == 0 && !vd->vdev_isl2cache &&
867185029Spjd	    (reason == VDEV_LABEL_L2CACHE ||
868185029Spjd	    spa_l2cache_exists(vd->vdev_guid, NULL)))
869185029Spjd		spa_l2cache_add(vd);
870185029Spjd
871168404Spjd	return (error);
872168404Spjd}
873168404Spjd
874168404Spjd/*
875168404Spjd * ==========================================================================
876168404Spjd * uberblock load/sync
877168404Spjd * ==========================================================================
878168404Spjd */
879168404Spjd
880168404Spjd/*
881168404Spjd * Consider the following situation: txg is safely synced to disk.  We've
882168404Spjd * written the first uberblock for txg + 1, and then we lose power.  When we
883168404Spjd * come back up, we fail to see the uberblock for txg + 1 because, say,
884168404Spjd * it was on a mirrored device and the replica to which we wrote txg + 1
885168404Spjd * is now offline.  If we then make some changes and sync txg + 1, and then
886236884Smm * the missing replica comes back, then for a few seconds we'll have two
887168404Spjd * conflicting uberblocks on disk with the same txg.  The solution is simple:
888168404Spjd * among uberblocks with equal txg, choose the one with the latest timestamp.
889168404Spjd */
890168404Spjdstatic int
891168404Spjdvdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
892168404Spjd{
893168404Spjd	if (ub1->ub_txg < ub2->ub_txg)
894168404Spjd		return (-1);
895168404Spjd	if (ub1->ub_txg > ub2->ub_txg)
896168404Spjd		return (1);
897168404Spjd
898168404Spjd	if (ub1->ub_timestamp < ub2->ub_timestamp)
899168404Spjd		return (-1);
900168404Spjd	if (ub1->ub_timestamp > ub2->ub_timestamp)
901168404Spjd		return (1);
902168404Spjd
903168404Spjd	return (0);
904168404Spjd}
905168404Spjd
906236884Smmstruct ubl_cbdata {
907236884Smm	uberblock_t	*ubl_ubbest;	/* Best uberblock */
908236884Smm	vdev_t		*ubl_vd;	/* vdev associated with the above */
909236884Smm};
910236884Smm
911168404Spjdstatic void
912168404Spjdvdev_uberblock_load_done(zio_t *zio)
913168404Spjd{
914236884Smm	vdev_t *vd = zio->io_vd;
915219089Spjd	spa_t *spa = zio->io_spa;
916185029Spjd	zio_t *rio = zio->io_private;
917168404Spjd	uberblock_t *ub = zio->io_data;
918236884Smm	struct ubl_cbdata *cbp = rio->io_private;
919168404Spjd
920236884Smm	ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
921168404Spjd
922168404Spjd	if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
923185029Spjd		mutex_enter(&rio->io_lock);
924219089Spjd		if (ub->ub_txg <= spa->spa_load_max_txg &&
925236884Smm		    vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
926236884Smm			/*
927239620Smm			 * Keep track of the vdev in which this uberblock
928239620Smm			 * was found. We will use this information later
929239620Smm			 * to obtain the config nvlist associated with
930236884Smm			 * this uberblock.
931236884Smm			 */
932236884Smm			*cbp->ubl_ubbest = *ub;
933236884Smm			cbp->ubl_vd = vd;
934236884Smm		}
935185029Spjd		mutex_exit(&rio->io_lock);
936168404Spjd	}
937168404Spjd
938168404Spjd	zio_buf_free(zio->io_data, zio->io_size);
939168404Spjd}
940168404Spjd
941236884Smmstatic void
942236884Smmvdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
943236884Smm    struct ubl_cbdata *cbp)
944168404Spjd{
945185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
946236884Smm		vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
947168404Spjd
948185029Spjd	if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
949185029Spjd		for (int l = 0; l < VDEV_LABELS; l++) {
950185029Spjd			for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
951185029Spjd				vdev_label_read(zio, vd, l,
952185029Spjd				    zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
953185029Spjd				    VDEV_UBERBLOCK_OFFSET(vd, n),
954185029Spjd				    VDEV_UBERBLOCK_SIZE(vd),
955185029Spjd				    vdev_uberblock_load_done, zio, flags);
956185029Spjd			}
957168404Spjd		}
958168404Spjd	}
959236884Smm}
960185029Spjd
961236884Smm/*
962236884Smm * Reads the 'best' uberblock from disk along with its associated
963236884Smm * configuration. First, we read the uberblock array of each label of each
964236884Smm * vdev, keeping track of the uberblock with the highest txg in each array.
965239620Smm * Then, we read the configuration from the same vdev as the best uberblock.
966236884Smm */
967236884Smmvoid
968236884Smmvdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
969236884Smm{
970236884Smm	zio_t *zio;
971236884Smm	spa_t *spa = rvd->vdev_spa;
972236884Smm	struct ubl_cbdata cb;
973236884Smm	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
974236884Smm	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
975236884Smm
976236884Smm	ASSERT(ub);
977236884Smm	ASSERT(config);
978236884Smm
979236884Smm	bzero(ub, sizeof (uberblock_t));
980236884Smm	*config = NULL;
981236884Smm
982236884Smm	cb.ubl_ubbest = ub;
983236884Smm	cb.ubl_vd = NULL;
984236884Smm
985236884Smm	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
986236884Smm	zio = zio_root(spa, NULL, &cb, flags);
987236884Smm	vdev_uberblock_load_impl(zio, rvd, flags, &cb);
988236884Smm	(void) zio_wait(zio);
989239620Smm
990239620Smm	/*
991239620Smm	 * It's possible that the best uberblock was discovered on a label
992239620Smm	 * that has a configuration which was written in a future txg.
993239620Smm	 * Search all labels on this vdev to find the configuration that
994239620Smm	 * matches the txg for our uberblock.
995239620Smm	 */
996239620Smm	if (cb.ubl_vd != NULL)
997239620Smm		*config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
998236884Smm	spa_config_exit(spa, SCL_ALL, FTAG);
999168404Spjd}
1000168404Spjd
1001168404Spjd/*
1002185029Spjd * On success, increment root zio's count of good writes.
1003168404Spjd * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1004168404Spjd */
1005168404Spjdstatic void
1006168404Spjdvdev_uberblock_sync_done(zio_t *zio)
1007168404Spjd{
1008185029Spjd	uint64_t *good_writes = zio->io_private;
1009168404Spjd
1010168404Spjd	if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
1011270247Sdelphij		atomic_inc_64(good_writes);
1012168404Spjd}
1013168404Spjd
1014185029Spjd/*
1015185029Spjd * Write the uberblock to all labels of all leaves of the specified vdev.
1016185029Spjd */
1017168404Spjdstatic void
1018185029Spjdvdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
1019168404Spjd{
1020185029Spjd	uberblock_t *ubbuf;
1021185029Spjd	int n;
1022168404Spjd
1023185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
1024185029Spjd		vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
1025168404Spjd
1026168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
1027168404Spjd		return;
1028168404Spjd
1029185029Spjd	if (!vdev_writeable(vd))
1030168404Spjd		return;
1031168404Spjd
1032185029Spjd	n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
1033168404Spjd
1034185029Spjd	ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
1035185029Spjd	bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
1036185029Spjd	*ubbuf = *ub;
1037168404Spjd
1038185029Spjd	for (int l = 0; l < VDEV_LABELS; l++)
1039185029Spjd		vdev_label_write(zio, vd, l, ubbuf,
1040185029Spjd		    VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1041185029Spjd		    vdev_uberblock_sync_done, zio->io_private,
1042185029Spjd		    flags | ZIO_FLAG_DONT_PROPAGATE);
1043168404Spjd
1044185029Spjd	zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
1045168404Spjd}
1046168404Spjd
1047251629Sdelphij/* Sync the uberblocks to all vdevs in svd[] */
1048185029Spjdint
1049185029Spjdvdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1050168404Spjd{
1051185029Spjd	spa_t *spa = svd[0]->vdev_spa;
1052168404Spjd	zio_t *zio;
1053185029Spjd	uint64_t good_writes = 0;
1054168404Spjd
1055185029Spjd	zio = zio_root(spa, NULL, &good_writes, flags);
1056168404Spjd
1057185029Spjd	for (int v = 0; v < svdcount; v++)
1058185029Spjd		vdev_uberblock_sync(zio, ub, svd[v], flags);
1059168404Spjd
1060185029Spjd	(void) zio_wait(zio);
1061168404Spjd
1062168404Spjd	/*
1063185029Spjd	 * Flush the uberblocks to disk.  This ensures that the odd labels
1064185029Spjd	 * are no longer needed (because the new uberblocks and the even
1065185029Spjd	 * labels are safely on disk), so it is safe to overwrite them.
1066168404Spjd	 */
1067185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1068168404Spjd
1069185029Spjd	for (int v = 0; v < svdcount; v++)
1070185029Spjd		zio_flush(zio, svd[v]);
1071168404Spjd
1072185029Spjd	(void) zio_wait(zio);
1073185029Spjd
1074185029Spjd	return (good_writes >= 1 ? 0 : EIO);
1075168404Spjd}
1076168404Spjd
1077168404Spjd/*
1078185029Spjd * On success, increment the count of good writes for our top-level vdev.
1079168404Spjd */
1080168404Spjdstatic void
1081185029Spjdvdev_label_sync_done(zio_t *zio)
1082168404Spjd{
1083185029Spjd	uint64_t *good_writes = zio->io_private;
1084168404Spjd
1085168404Spjd	if (zio->io_error == 0)
1086270247Sdelphij		atomic_inc_64(good_writes);
1087168404Spjd}
1088168404Spjd
1089185029Spjd/*
1090185029Spjd * If there weren't enough good writes, indicate failure to the parent.
1091185029Spjd */
1092168404Spjdstatic void
1093185029Spjdvdev_label_sync_top_done(zio_t *zio)
1094168404Spjd{
1095185029Spjd	uint64_t *good_writes = zio->io_private;
1096185029Spjd
1097185029Spjd	if (*good_writes == 0)
1098249195Smm		zio->io_error = SET_ERROR(EIO);
1099185029Spjd
1100185029Spjd	kmem_free(good_writes, sizeof (uint64_t));
1101185029Spjd}
1102185029Spjd
1103185029Spjd/*
1104185029Spjd * We ignore errors for log and cache devices, simply free the private data.
1105185029Spjd */
1106185029Spjdstatic void
1107185029Spjdvdev_label_sync_ignore_done(zio_t *zio)
1108185029Spjd{
1109185029Spjd	kmem_free(zio->io_private, sizeof (uint64_t));
1110185029Spjd}
1111185029Spjd
1112185029Spjd/*
1113185029Spjd * Write all even or odd labels to all leaves of the specified vdev.
1114185029Spjd */
1115185029Spjdstatic void
1116185029Spjdvdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
1117185029Spjd{
1118168404Spjd	nvlist_t *label;
1119168404Spjd	vdev_phys_t *vp;
1120168404Spjd	char *buf;
1121168404Spjd	size_t buflen;
1122168404Spjd
1123185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
1124185029Spjd		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
1125168404Spjd
1126168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
1127168404Spjd		return;
1128168404Spjd
1129185029Spjd	if (!vdev_writeable(vd))
1130168404Spjd		return;
1131168404Spjd
1132168404Spjd	/*
1133168404Spjd	 * Generate a label describing the top-level config to which we belong.
1134168404Spjd	 */
1135168404Spjd	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1136168404Spjd
1137168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
1138168404Spjd	bzero(vp, sizeof (vdev_phys_t));
1139168404Spjd
1140168404Spjd	buf = vp->vp_nvlist;
1141168404Spjd	buflen = sizeof (vp->vp_nvlist);
1142168404Spjd
1143185029Spjd	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
1144185029Spjd		for (; l < VDEV_LABELS; l += 2) {
1145185029Spjd			vdev_label_write(zio, vd, l, vp,
1146185029Spjd			    offsetof(vdev_label_t, vl_vdev_phys),
1147185029Spjd			    sizeof (vdev_phys_t),
1148185029Spjd			    vdev_label_sync_done, zio->io_private,
1149185029Spjd			    flags | ZIO_FLAG_DONT_PROPAGATE);
1150185029Spjd		}
1151185029Spjd	}
1152168404Spjd
1153168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
1154168404Spjd	nvlist_free(label);
1155168404Spjd}
1156168404Spjd
1157185029Spjdint
1158185029Spjdvdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1159168404Spjd{
1160185029Spjd	list_t *dl = &spa->spa_config_dirty_list;
1161185029Spjd	vdev_t *vd;
1162168404Spjd	zio_t *zio;
1163168404Spjd	int error;
1164168404Spjd
1165185029Spjd	/*
1166185029Spjd	 * Write the new labels to disk.
1167185029Spjd	 */
1168185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1169168404Spjd
1170185029Spjd	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
1171185029Spjd		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
1172185029Spjd		    KM_SLEEP);
1173219089Spjd
1174219089Spjd		ASSERT(!vd->vdev_ishole);
1175219089Spjd
1176209962Smm		zio_t *vio = zio_null(zio, spa, NULL,
1177185029Spjd		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
1178185029Spjd		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
1179185029Spjd		    good_writes, flags);
1180185029Spjd		vdev_label_sync(vio, vd, l, txg, flags);
1181185029Spjd		zio_nowait(vio);
1182185029Spjd	}
1183168404Spjd
1184185029Spjd	error = zio_wait(zio);
1185168404Spjd
1186168404Spjd	/*
1187185029Spjd	 * Flush the new labels to disk.
1188168404Spjd	 */
1189185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1190168404Spjd
1191185029Spjd	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
1192185029Spjd		zio_flush(zio, vd);
1193168404Spjd
1194185029Spjd	(void) zio_wait(zio);
1195168404Spjd
1196168404Spjd	return (error);
1197168404Spjd}
1198168404Spjd
1199168404Spjd/*
1200185029Spjd * Sync the uberblock and any changes to the vdev configuration.
1201168404Spjd *
1202168404Spjd * The order of operations is carefully crafted to ensure that
1203168404Spjd * if the system panics or loses power at any time, the state on disk
1204168404Spjd * is still transactionally consistent.  The in-line comments below
1205168404Spjd * describe the failure semantics at each stage.
1206168404Spjd *
1207185029Spjd * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1208168404Spjd * at any time, you can just call it again, and it will resume its work.
1209168404Spjd */
1210168404Spjdint
1211294811Smavvdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1212168404Spjd{
1213185029Spjd	spa_t *spa = svd[0]->vdev_spa;
1214168404Spjd	uberblock_t *ub = &spa->spa_uberblock;
1215168404Spjd	vdev_t *vd;
1216168404Spjd	zio_t *zio;
1217294811Smav	int error = 0;
1218185029Spjd	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1219168404Spjd
1220294811Smavretry:
1221213198Smm	/*
1222213198Smm	 * Normally, we don't want to try too hard to write every label and
1223213198Smm	 * uberblock.  If there is a flaky disk, we don't want the rest of the
1224213198Smm	 * sync process to block while we retry.  But if we can't write a
1225213198Smm	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
1226213198Smm	 * bailing out and declaring the pool faulted.
1227213198Smm	 */
1228294811Smav	if (error != 0) {
1229294811Smav		if ((flags & ZIO_FLAG_TRYHARD) != 0)
1230294811Smav			return (error);
1231213198Smm		flags |= ZIO_FLAG_TRYHARD;
1232294811Smav	}
1233213198Smm
1234168404Spjd	ASSERT(ub->ub_txg <= txg);
1235168404Spjd
1236168404Spjd	/*
1237185029Spjd	 * If this isn't a resync due to I/O errors,
1238185029Spjd	 * and nothing changed in this transaction group,
1239185029Spjd	 * and the vdev configuration hasn't changed,
1240168404Spjd	 * then there's nothing to do.
1241168404Spjd	 */
1242185029Spjd	if (ub->ub_txg < txg &&
1243185029Spjd	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
1244185029Spjd	    list_is_empty(&spa->spa_config_dirty_list))
1245168404Spjd		return (0);
1246168404Spjd
1247168404Spjd	if (txg > spa_freeze_txg(spa))
1248168404Spjd		return (0);
1249168404Spjd
1250168404Spjd	ASSERT(txg <= spa->spa_final_txg);
1251168404Spjd
1252168404Spjd	/*
1253168404Spjd	 * Flush the write cache of every disk that's been written to
1254168404Spjd	 * in this transaction group.  This ensures that all blocks
1255168404Spjd	 * written in this txg will be committed to stable storage
1256168404Spjd	 * before any uberblock that references them.
1257168404Spjd	 */
1258185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1259185029Spjd
1260168404Spjd	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
1261185029Spjd	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
1262185029Spjd		zio_flush(zio, vd);
1263185029Spjd
1264168404Spjd	(void) zio_wait(zio);
1265168404Spjd
1266168404Spjd	/*
1267168404Spjd	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1268168404Spjd	 * system dies in the middle of this process, that's OK: all of the
1269168404Spjd	 * even labels that made it to disk will be newer than any uberblock,
1270168404Spjd	 * and will therefore be considered invalid.  The odd labels (L1, L3),
1271185029Spjd	 * which have not yet been touched, will still be valid.  We flush
1272185029Spjd	 * the new labels to disk to ensure that all even-label updates
1273185029Spjd	 * are committed to stable storage before the uberblock update.
1274168404Spjd	 */
1275185029Spjd	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
1276294811Smav		goto retry;
1277168404Spjd
1278168404Spjd	/*
1279185029Spjd	 * Sync the uberblocks to all vdevs in svd[].
1280168404Spjd	 * If the system dies in the middle of this step, there are two cases
1281168404Spjd	 * to consider, and the on-disk state is consistent either way:
1282168404Spjd	 *
1283168404Spjd	 * (1)	If none of the new uberblocks made it to disk, then the
1284168404Spjd	 *	previous uberblock will be the newest, and the odd labels
1285168404Spjd	 *	(which had not yet been touched) will be valid with respect
1286168404Spjd	 *	to that uberblock.
1287168404Spjd	 *
1288168404Spjd	 * (2)	If one or more new uberblocks made it to disk, then they
1289168404Spjd	 *	will be the newest, and the even labels (which had all
1290168404Spjd	 *	been successfully committed) will be valid with respect
1291168404Spjd	 *	to the new uberblocks.
1292168404Spjd	 */
1293185029Spjd	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
1294294811Smav		goto retry;
1295168404Spjd
1296168404Spjd	/*
1297168404Spjd	 * Sync out odd labels for every dirty vdev.  If the system dies
1298168404Spjd	 * in the middle of this process, the even labels and the new
1299168404Spjd	 * uberblocks will suffice to open the pool.  The next time
1300168404Spjd	 * the pool is opened, the first thing we'll do -- before any
1301168404Spjd	 * user data is modified -- is mark every vdev dirty so that
1302185029Spjd	 * all labels will be brought up to date.  We flush the new labels
1303185029Spjd	 * to disk to ensure that all odd-label updates are committed to
1304185029Spjd	 * stable storage before the next transaction group begins.
1305168404Spjd	 */
1306240868Spjd	if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0)
1307294811Smav		goto retry;;
1308240868Spjd
1309240868Spjd	trim_thread_wakeup(spa);
1310240868Spjd
1311240868Spjd	return (0);
1312168404Spjd}
1313