vdev_label.c revision 249195
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.
24249195Smm * Copyright (c) 2013 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,
188185029Spjd	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,
202185029Spjd	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
225168404Spjd	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
226168404Spjd
227168404Spjd	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
228168404Spjd	    vd->vdev_ops->vdev_op_type) == 0);
229219089Spjd	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
230168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
231168404Spjd		    == 0);
232168404Spjd	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
233168404Spjd
234168404Spjd	if (vd->vdev_path != NULL)
235168404Spjd		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
236168404Spjd		    vd->vdev_path) == 0);
237168404Spjd
238168404Spjd	if (vd->vdev_devid != NULL)
239168404Spjd		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
240168404Spjd		    vd->vdev_devid) == 0);
241168404Spjd
242185029Spjd	if (vd->vdev_physpath != NULL)
243185029Spjd		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
244185029Spjd		    vd->vdev_physpath) == 0);
245185029Spjd
246209962Smm	if (vd->vdev_fru != NULL)
247209962Smm		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU,
248209962Smm		    vd->vdev_fru) == 0);
249209962Smm
250168404Spjd	if (vd->vdev_nparity != 0) {
251168404Spjd		ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
252168404Spjd		    VDEV_TYPE_RAIDZ) == 0);
253168404Spjd
254168404Spjd		/*
255168404Spjd		 * Make sure someone hasn't managed to sneak a fancy new vdev
256168404Spjd		 * into a crufty old storage pool.
257168404Spjd		 */
258168404Spjd		ASSERT(vd->vdev_nparity == 1 ||
259219089Spjd		    (vd->vdev_nparity <= 2 &&
260219089Spjd		    spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
261219089Spjd		    (vd->vdev_nparity <= 3 &&
262219089Spjd		    spa_version(spa) >= SPA_VERSION_RAIDZ3));
263168404Spjd
264168404Spjd		/*
265168404Spjd		 * Note that we'll add the nparity tag even on storage pools
266168404Spjd		 * that only support a single parity device -- older software
267168404Spjd		 * will just ignore it.
268168404Spjd		 */
269168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
270168404Spjd		    vd->vdev_nparity) == 0);
271168404Spjd	}
272168404Spjd
273168404Spjd	if (vd->vdev_wholedisk != -1ULL)
274168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
275168404Spjd		    vd->vdev_wholedisk) == 0);
276168404Spjd
277168404Spjd	if (vd->vdev_not_present)
278168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
279168404Spjd
280168404Spjd	if (vd->vdev_isspare)
281168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
282168404Spjd
283219089Spjd	if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
284219089Spjd	    vd == vd->vdev_top) {
285168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
286168404Spjd		    vd->vdev_ms_array) == 0);
287168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
288168404Spjd		    vd->vdev_ms_shift) == 0);
289168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
290168404Spjd		    vd->vdev_ashift) == 0);
291168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
292168404Spjd		    vd->vdev_asize) == 0);
293185029Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
294185029Spjd		    vd->vdev_islog) == 0);
295219089Spjd		if (vd->vdev_removing)
296219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
297219089Spjd			    vd->vdev_removing) == 0);
298168404Spjd	}
299168404Spjd
300209962Smm	if (vd->vdev_dtl_smo.smo_object != 0)
301168404Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
302209962Smm		    vd->vdev_dtl_smo.smo_object) == 0);
303168404Spjd
304219089Spjd	if (vd->vdev_crtxg)
305219089Spjd		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
306219089Spjd		    vd->vdev_crtxg) == 0);
307219089Spjd
308168404Spjd	if (getstats) {
309168404Spjd		vdev_stat_t vs;
310219089Spjd		pool_scan_stat_t ps;
311219089Spjd
312168404Spjd		vdev_get_stats(vd, &vs);
313219089Spjd		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
314168404Spjd		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
315219089Spjd
316219089Spjd		/* provide either current or previous scan information */
317219089Spjd		if (spa_scan_get_stats(spa, &ps) == 0) {
318219089Spjd			VERIFY(nvlist_add_uint64_array(nv,
319219089Spjd			    ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
320219089Spjd			    sizeof (pool_scan_stat_t) / sizeof (uint64_t))
321219089Spjd			    == 0);
322219089Spjd		}
323168404Spjd	}
324168404Spjd
325168404Spjd	if (!vd->vdev_ops->vdev_op_leaf) {
326168404Spjd		nvlist_t **child;
327219089Spjd		int c, idx;
328168404Spjd
329219089Spjd		ASSERT(!vd->vdev_ishole);
330219089Spjd
331168404Spjd		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
332168404Spjd		    KM_SLEEP);
333168404Spjd
334219089Spjd		for (c = 0, idx = 0; c < vd->vdev_children; c++) {
335219089Spjd			vdev_t *cvd = vd->vdev_child[c];
336168404Spjd
337219089Spjd			/*
338219089Spjd			 * If we're generating an nvlist of removing
339219089Spjd			 * vdevs then skip over any device which is
340219089Spjd			 * not being removed.
341219089Spjd			 */
342219089Spjd			if ((flags & VDEV_CONFIG_REMOVING) &&
343219089Spjd			    !cvd->vdev_removing)
344219089Spjd				continue;
345168404Spjd
346219089Spjd			child[idx++] = vdev_config_generate(spa, cvd,
347219089Spjd			    getstats, flags);
348219089Spjd		}
349219089Spjd
350219089Spjd		if (idx) {
351219089Spjd			VERIFY(nvlist_add_nvlist_array(nv,
352219089Spjd			    ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
353219089Spjd		}
354219089Spjd
355219089Spjd		for (c = 0; c < idx; c++)
356168404Spjd			nvlist_free(child[c]);
357168404Spjd
358168404Spjd		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
359168404Spjd
360168404Spjd	} else {
361219089Spjd		const char *aux = NULL;
362219089Spjd
363168404Spjd		if (vd->vdev_offline && !vd->vdev_tmpoffline)
364168404Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
365168404Spjd			    B_TRUE) == 0);
366219089Spjd		if (vd->vdev_resilvering)
367219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVERING,
368219089Spjd			    B_TRUE) == 0);
369185029Spjd		if (vd->vdev_faulted)
370185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
371185029Spjd			    B_TRUE) == 0);
372185029Spjd		if (vd->vdev_degraded)
373185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
374185029Spjd			    B_TRUE) == 0);
375185029Spjd		if (vd->vdev_removed)
376185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
377185029Spjd			    B_TRUE) == 0);
378185029Spjd		if (vd->vdev_unspare)
379185029Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
380185029Spjd			    B_TRUE) == 0);
381219089Spjd		if (vd->vdev_ishole)
382219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE,
383219089Spjd			    B_TRUE) == 0);
384219089Spjd
385219089Spjd		switch (vd->vdev_stat.vs_aux) {
386219089Spjd		case VDEV_AUX_ERR_EXCEEDED:
387219089Spjd			aux = "err_exceeded";
388219089Spjd			break;
389219089Spjd
390219089Spjd		case VDEV_AUX_EXTERNAL:
391219089Spjd			aux = "external";
392219089Spjd			break;
393219089Spjd		}
394219089Spjd
395219089Spjd		if (aux != NULL)
396219089Spjd			VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE,
397219089Spjd			    aux) == 0);
398219089Spjd
399219089Spjd		if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
400219089Spjd			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
401219089Spjd			    vd->vdev_orig_guid) == 0);
402219089Spjd		}
403168404Spjd	}
404168404Spjd
405168404Spjd	return (nv);
406168404Spjd}
407168404Spjd
408219089Spjd/*
409219089Spjd * Generate a view of the top-level vdevs.  If we currently have holes
410219089Spjd * in the namespace, then generate an array which contains a list of holey
411219089Spjd * vdevs.  Additionally, add the number of top-level children that currently
412219089Spjd * exist.
413219089Spjd */
414219089Spjdvoid
415219089Spjdvdev_top_config_generate(spa_t *spa, nvlist_t *config)
416219089Spjd{
417219089Spjd	vdev_t *rvd = spa->spa_root_vdev;
418219089Spjd	uint64_t *array;
419219089Spjd	uint_t c, idx;
420219089Spjd
421219089Spjd	array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
422219089Spjd
423219089Spjd	for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
424219089Spjd		vdev_t *tvd = rvd->vdev_child[c];
425219089Spjd
426219089Spjd		if (tvd->vdev_ishole)
427219089Spjd			array[idx++] = c;
428219089Spjd	}
429219089Spjd
430219089Spjd	if (idx) {
431219089Spjd		VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
432219089Spjd		    array, idx) == 0);
433219089Spjd	}
434219089Spjd
435219089Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
436219089Spjd	    rvd->vdev_children) == 0);
437219089Spjd
438219089Spjd	kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
439219089Spjd}
440219089Spjd
441236884Smm/*
442239620Smm * Returns the configuration from the label of the given vdev. For vdevs
443239620Smm * which don't have a txg value stored on their label (i.e. spares/cache)
444239620Smm * or have not been completely initialized (txg = 0) just return
445239620Smm * the configuration from the first valid label we find. Otherwise,
446239620Smm * find the most up-to-date label that does not exceed the specified
447239620Smm * 'txg' value.
448236884Smm */
449168404Spjdnvlist_t *
450239620Smmvdev_label_read_config(vdev_t *vd, uint64_t txg)
451168404Spjd{
452168404Spjd	spa_t *spa = vd->vdev_spa;
453168404Spjd	nvlist_t *config = NULL;
454168404Spjd	vdev_phys_t *vp;
455168404Spjd	zio_t *zio;
456239620Smm	uint64_t best_txg = 0;
457239620Smm	int error = 0;
458213198Smm	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
459213198Smm	    ZIO_FLAG_SPECULATIVE;
460168404Spjd
461185029Spjd	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
462168404Spjd
463185029Spjd	if (!vdev_readable(vd))
464168404Spjd		return (NULL);
465168404Spjd
466168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
467168404Spjd
468213198Smmretry:
469185029Spjd	for (int l = 0; l < VDEV_LABELS; l++) {
470239620Smm		nvlist_t *label = NULL;
471168404Spjd
472185029Spjd		zio = zio_root(spa, NULL, NULL, flags);
473168404Spjd
474168404Spjd		vdev_label_read(zio, vd, l, vp,
475168404Spjd		    offsetof(vdev_label_t, vl_vdev_phys),
476185029Spjd		    sizeof (vdev_phys_t), NULL, NULL, flags);
477168404Spjd
478168404Spjd		if (zio_wait(zio) == 0 &&
479168404Spjd		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
480239620Smm		    &label, 0) == 0) {
481239620Smm			uint64_t label_txg = 0;
482168404Spjd
483239620Smm			/*
484239620Smm			 * Auxiliary vdevs won't have txg values in their
485239620Smm			 * labels and newly added vdevs may not have been
486239620Smm			 * completely initialized so just return the
487239620Smm			 * configuration from the first valid label we
488239620Smm			 * encounter.
489239620Smm			 */
490239620Smm			error = nvlist_lookup_uint64(label,
491239620Smm			    ZPOOL_CONFIG_POOL_TXG, &label_txg);
492239620Smm			if ((error || label_txg == 0) && !config) {
493239620Smm				config = label;
494239620Smm				break;
495239620Smm			} else if (label_txg <= txg && label_txg > best_txg) {
496239620Smm				best_txg = label_txg;
497239620Smm				nvlist_free(config);
498239620Smm				config = fnvlist_dup(label);
499239620Smm			}
500168404Spjd		}
501239620Smm
502239620Smm		if (label != NULL) {
503239620Smm			nvlist_free(label);
504239620Smm			label = NULL;
505239620Smm		}
506168404Spjd	}
507168404Spjd
508213198Smm	if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
509213198Smm		flags |= ZIO_FLAG_TRYHARD;
510213198Smm		goto retry;
511213198Smm	}
512213198Smm
513168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
514168404Spjd
515168404Spjd	return (config);
516168404Spjd}
517168404Spjd
518168404Spjd/*
519168404Spjd * Determine if a device is in use.  The 'spare_guid' parameter will be filled
520168404Spjd * in with the device guid if this spare is active elsewhere on the system.
521168404Spjd */
522168404Spjdstatic boolean_t
523168404Spjdvdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
524185029Spjd    uint64_t *spare_guid, uint64_t *l2cache_guid)
525168404Spjd{
526168404Spjd	spa_t *spa = vd->vdev_spa;
527168404Spjd	uint64_t state, pool_guid, device_guid, txg, spare_pool;
528168404Spjd	uint64_t vdtxg = 0;
529168404Spjd	nvlist_t *label;
530168404Spjd
531168404Spjd	if (spare_guid)
532168404Spjd		*spare_guid = 0ULL;
533185029Spjd	if (l2cache_guid)
534185029Spjd		*l2cache_guid = 0ULL;
535168404Spjd
536168404Spjd	/*
537168404Spjd	 * Read the label, if any, and perform some basic sanity checks.
538168404Spjd	 */
539239620Smm	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
540168404Spjd		return (B_FALSE);
541168404Spjd
542168404Spjd	(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
543168404Spjd	    &vdtxg);
544168404Spjd
545168404Spjd	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
546168404Spjd	    &state) != 0 ||
547168404Spjd	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
548168404Spjd	    &device_guid) != 0) {
549168404Spjd		nvlist_free(label);
550168404Spjd		return (B_FALSE);
551168404Spjd	}
552168404Spjd
553185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
554168404Spjd	    (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
555168404Spjd	    &pool_guid) != 0 ||
556168404Spjd	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
557168404Spjd	    &txg) != 0)) {
558168404Spjd		nvlist_free(label);
559168404Spjd		return (B_FALSE);
560168404Spjd	}
561168404Spjd
562168404Spjd	nvlist_free(label);
563168404Spjd
564168404Spjd	/*
565168404Spjd	 * Check to see if this device indeed belongs to the pool it claims to
566168404Spjd	 * be a part of.  The only way this is allowed is if the device is a hot
567168404Spjd	 * spare (which we check for later on).
568168404Spjd	 */
569185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
570168404Spjd	    !spa_guid_exists(pool_guid, device_guid) &&
571185029Spjd	    !spa_spare_exists(device_guid, NULL, NULL) &&
572185029Spjd	    !spa_l2cache_exists(device_guid, NULL))
573168404Spjd		return (B_FALSE);
574168404Spjd
575168404Spjd	/*
576168404Spjd	 * If the transaction group is zero, then this an initialized (but
577168404Spjd	 * unused) label.  This is only an error if the create transaction
578168404Spjd	 * on-disk is the same as the one we're using now, in which case the
579168404Spjd	 * user has attempted to add the same vdev multiple times in the same
580168404Spjd	 * transaction.
581168404Spjd	 */
582185029Spjd	if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
583185029Spjd	    txg == 0 && vdtxg == crtxg)
584168404Spjd		return (B_TRUE);
585168404Spjd
586168404Spjd	/*
587168404Spjd	 * Check to see if this is a spare device.  We do an explicit check for
588168404Spjd	 * spa_has_spare() here because it may be on our pending list of spares
589185029Spjd	 * to add.  We also check if it is an l2cache device.
590168404Spjd	 */
591185029Spjd	if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
592168404Spjd	    spa_has_spare(spa, device_guid)) {
593168404Spjd		if (spare_guid)
594168404Spjd			*spare_guid = device_guid;
595168404Spjd
596168404Spjd		switch (reason) {
597168404Spjd		case VDEV_LABEL_CREATE:
598185029Spjd		case VDEV_LABEL_L2CACHE:
599168404Spjd			return (B_TRUE);
600168404Spjd
601168404Spjd		case VDEV_LABEL_REPLACE:
602168404Spjd			return (!spa_has_spare(spa, device_guid) ||
603168404Spjd			    spare_pool != 0ULL);
604168404Spjd
605168404Spjd		case VDEV_LABEL_SPARE:
606168404Spjd			return (spa_has_spare(spa, device_guid));
607168404Spjd		}
608168404Spjd	}
609168404Spjd
610168404Spjd	/*
611185029Spjd	 * Check to see if this is an l2cache device.
612185029Spjd	 */
613185029Spjd	if (spa_l2cache_exists(device_guid, NULL))
614185029Spjd		return (B_TRUE);
615185029Spjd
616185029Spjd	/*
617219089Spjd	 * We can't rely on a pool's state if it's been imported
618219089Spjd	 * read-only.  Instead we look to see if the pools is marked
619219089Spjd	 * read-only in the namespace and set the state to active.
620219089Spjd	 */
621219089Spjd	if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
622219089Spjd	    spa_mode(spa) == FREAD)
623219089Spjd		state = POOL_STATE_ACTIVE;
624219089Spjd
625219089Spjd	/*
626168404Spjd	 * If the device is marked ACTIVE, then this device is in use by another
627168404Spjd	 * pool on the system.
628168404Spjd	 */
629168404Spjd	return (state == POOL_STATE_ACTIVE);
630168404Spjd}
631168404Spjd
632168404Spjd/*
633168404Spjd * Initialize a vdev label.  We check to make sure each leaf device is not in
634168404Spjd * use, and writable.  We put down an initial label which we will later
635168404Spjd * overwrite with a complete label.  Note that it's important to do this
636168404Spjd * sequentially, not in parallel, so that we catch cases of multiple use of the
637168404Spjd * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
638168404Spjd * itself.
639168404Spjd */
640168404Spjdint
641168404Spjdvdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
642168404Spjd{
643168404Spjd	spa_t *spa = vd->vdev_spa;
644168404Spjd	nvlist_t *label;
645168404Spjd	vdev_phys_t *vp;
646209962Smm	char *pad2;
647168404Spjd	uberblock_t *ub;
648168404Spjd	zio_t *zio;
649168404Spjd	char *buf;
650168404Spjd	size_t buflen;
651168404Spjd	int error;
652185029Spjd	uint64_t spare_guid, l2cache_guid;
653185029Spjd	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
654168404Spjd
655185029Spjd	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
656168404Spjd
657185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
658168404Spjd		if ((error = vdev_label_init(vd->vdev_child[c],
659168404Spjd		    crtxg, reason)) != 0)
660168404Spjd			return (error);
661168404Spjd
662219089Spjd	/* Track the creation time for this vdev */
663219089Spjd	vd->vdev_crtxg = crtxg;
664219089Spjd
665168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
666168404Spjd		return (0);
667168404Spjd
668168404Spjd	/*
669168404Spjd	 * Dead vdevs cannot be initialized.
670168404Spjd	 */
671168404Spjd	if (vdev_is_dead(vd))
672249195Smm		return (SET_ERROR(EIO));
673168404Spjd
674168404Spjd	/*
675168404Spjd	 * Determine if the vdev is in use.
676168404Spjd	 */
677219089Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
678185029Spjd	    vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
679249195Smm		return (SET_ERROR(EBUSY));
680168404Spjd
681168404Spjd	/*
682185029Spjd	 * If this is a request to add or replace a spare or l2cache device
683185029Spjd	 * that is in use elsewhere on the system, then we must update the
684185029Spjd	 * guid (which was initialized to a random value) to reflect the
685185029Spjd	 * actual GUID (which is shared between multiple pools).
686168404Spjd	 */
687185029Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
688185029Spjd	    spare_guid != 0ULL) {
689185029Spjd		uint64_t guid_delta = spare_guid - vd->vdev_guid;
690168404Spjd
691185029Spjd		vd->vdev_guid += guid_delta;
692168404Spjd
693185029Spjd		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
694185029Spjd			pvd->vdev_guid_sum += guid_delta;
695168404Spjd
696168404Spjd		/*
697168404Spjd		 * If this is a replacement, then we want to fallthrough to the
698168404Spjd		 * rest of the code.  If we're adding a spare, then it's already
699185029Spjd		 * labeled appropriately and we can just return.
700168404Spjd		 */
701168404Spjd		if (reason == VDEV_LABEL_SPARE)
702168404Spjd			return (0);
703219089Spjd		ASSERT(reason == VDEV_LABEL_REPLACE ||
704219089Spjd		    reason == VDEV_LABEL_SPLIT);
705168404Spjd	}
706168404Spjd
707185029Spjd	if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
708185029Spjd	    l2cache_guid != 0ULL) {
709185029Spjd		uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
710185029Spjd
711185029Spjd		vd->vdev_guid += guid_delta;
712185029Spjd
713185029Spjd		for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
714185029Spjd			pvd->vdev_guid_sum += guid_delta;
715185029Spjd
716185029Spjd		/*
717185029Spjd		 * If this is a replacement, then we want to fallthrough to the
718185029Spjd		 * rest of the code.  If we're adding an l2cache, then it's
719185029Spjd		 * already labeled appropriately and we can just return.
720185029Spjd		 */
721185029Spjd		if (reason == VDEV_LABEL_L2CACHE)
722185029Spjd			return (0);
723185029Spjd		ASSERT(reason == VDEV_LABEL_REPLACE);
724185029Spjd	}
725185029Spjd
726168404Spjd	/*
727240868Spjd	 * TRIM the whole thing so that we start with a clean slate.
728240868Spjd	 * It's just an optimization, so we don't care if it fails.
729240868Spjd	 * Don't TRIM if removing so that we don't interfere with zpool
730240868Spjd	 * disaster recovery.
731240868Spjd	 */
732244188Ssmh	if (!zfs_notrim && vdev_trim_on_init && (reason == VDEV_LABEL_CREATE ||
733240868Spjd	    reason == VDEV_LABEL_SPARE || 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)
1011168404Spjd		atomic_add_64(good_writes, 1);
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
1047185029Spjdint
1048185029Spjdvdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1049168404Spjd{
1050185029Spjd	spa_t *spa = svd[0]->vdev_spa;
1051168404Spjd	zio_t *zio;
1052185029Spjd	uint64_t good_writes = 0;
1053168404Spjd
1054185029Spjd	zio = zio_root(spa, NULL, &good_writes, flags);
1055168404Spjd
1056185029Spjd	for (int v = 0; v < svdcount; v++)
1057185029Spjd		vdev_uberblock_sync(zio, ub, svd[v], flags);
1058168404Spjd
1059185029Spjd	(void) zio_wait(zio);
1060168404Spjd
1061168404Spjd	/*
1062185029Spjd	 * Flush the uberblocks to disk.  This ensures that the odd labels
1063185029Spjd	 * are no longer needed (because the new uberblocks and the even
1064185029Spjd	 * labels are safely on disk), so it is safe to overwrite them.
1065168404Spjd	 */
1066185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1067168404Spjd
1068185029Spjd	for (int v = 0; v < svdcount; v++)
1069185029Spjd		zio_flush(zio, svd[v]);
1070168404Spjd
1071185029Spjd	(void) zio_wait(zio);
1072185029Spjd
1073185029Spjd	return (good_writes >= 1 ? 0 : EIO);
1074168404Spjd}
1075168404Spjd
1076168404Spjd/*
1077185029Spjd * On success, increment the count of good writes for our top-level vdev.
1078168404Spjd */
1079168404Spjdstatic void
1080185029Spjdvdev_label_sync_done(zio_t *zio)
1081168404Spjd{
1082185029Spjd	uint64_t *good_writes = zio->io_private;
1083168404Spjd
1084168404Spjd	if (zio->io_error == 0)
1085168404Spjd		atomic_add_64(good_writes, 1);
1086168404Spjd}
1087168404Spjd
1088185029Spjd/*
1089185029Spjd * If there weren't enough good writes, indicate failure to the parent.
1090185029Spjd */
1091168404Spjdstatic void
1092185029Spjdvdev_label_sync_top_done(zio_t *zio)
1093168404Spjd{
1094185029Spjd	uint64_t *good_writes = zio->io_private;
1095185029Spjd
1096185029Spjd	if (*good_writes == 0)
1097249195Smm		zio->io_error = SET_ERROR(EIO);
1098185029Spjd
1099185029Spjd	kmem_free(good_writes, sizeof (uint64_t));
1100185029Spjd}
1101185029Spjd
1102185029Spjd/*
1103185029Spjd * We ignore errors for log and cache devices, simply free the private data.
1104185029Spjd */
1105185029Spjdstatic void
1106185029Spjdvdev_label_sync_ignore_done(zio_t *zio)
1107185029Spjd{
1108185029Spjd	kmem_free(zio->io_private, sizeof (uint64_t));
1109185029Spjd}
1110185029Spjd
1111185029Spjd/*
1112185029Spjd * Write all even or odd labels to all leaves of the specified vdev.
1113185029Spjd */
1114185029Spjdstatic void
1115185029Spjdvdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
1116185029Spjd{
1117168404Spjd	nvlist_t *label;
1118168404Spjd	vdev_phys_t *vp;
1119168404Spjd	char *buf;
1120168404Spjd	size_t buflen;
1121168404Spjd
1122185029Spjd	for (int c = 0; c < vd->vdev_children; c++)
1123185029Spjd		vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
1124168404Spjd
1125168404Spjd	if (!vd->vdev_ops->vdev_op_leaf)
1126168404Spjd		return;
1127168404Spjd
1128185029Spjd	if (!vdev_writeable(vd))
1129168404Spjd		return;
1130168404Spjd
1131168404Spjd	/*
1132168404Spjd	 * Generate a label describing the top-level config to which we belong.
1133168404Spjd	 */
1134168404Spjd	label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1135168404Spjd
1136168404Spjd	vp = zio_buf_alloc(sizeof (vdev_phys_t));
1137168404Spjd	bzero(vp, sizeof (vdev_phys_t));
1138168404Spjd
1139168404Spjd	buf = vp->vp_nvlist;
1140168404Spjd	buflen = sizeof (vp->vp_nvlist);
1141168404Spjd
1142185029Spjd	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
1143185029Spjd		for (; l < VDEV_LABELS; l += 2) {
1144185029Spjd			vdev_label_write(zio, vd, l, vp,
1145185029Spjd			    offsetof(vdev_label_t, vl_vdev_phys),
1146185029Spjd			    sizeof (vdev_phys_t),
1147185029Spjd			    vdev_label_sync_done, zio->io_private,
1148185029Spjd			    flags | ZIO_FLAG_DONT_PROPAGATE);
1149185029Spjd		}
1150185029Spjd	}
1151168404Spjd
1152168404Spjd	zio_buf_free(vp, sizeof (vdev_phys_t));
1153168404Spjd	nvlist_free(label);
1154168404Spjd}
1155168404Spjd
1156185029Spjdint
1157185029Spjdvdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1158168404Spjd{
1159185029Spjd	list_t *dl = &spa->spa_config_dirty_list;
1160185029Spjd	vdev_t *vd;
1161168404Spjd	zio_t *zio;
1162168404Spjd	int error;
1163168404Spjd
1164185029Spjd	/*
1165185029Spjd	 * Write the new labels to disk.
1166185029Spjd	 */
1167185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1168168404Spjd
1169185029Spjd	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
1170185029Spjd		uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
1171185029Spjd		    KM_SLEEP);
1172219089Spjd
1173219089Spjd		ASSERT(!vd->vdev_ishole);
1174219089Spjd
1175209962Smm		zio_t *vio = zio_null(zio, spa, NULL,
1176185029Spjd		    (vd->vdev_islog || vd->vdev_aux != NULL) ?
1177185029Spjd		    vdev_label_sync_ignore_done : vdev_label_sync_top_done,
1178185029Spjd		    good_writes, flags);
1179185029Spjd		vdev_label_sync(vio, vd, l, txg, flags);
1180185029Spjd		zio_nowait(vio);
1181185029Spjd	}
1182168404Spjd
1183185029Spjd	error = zio_wait(zio);
1184168404Spjd
1185168404Spjd	/*
1186185029Spjd	 * Flush the new labels to disk.
1187168404Spjd	 */
1188185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1189168404Spjd
1190185029Spjd	for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
1191185029Spjd		zio_flush(zio, vd);
1192168404Spjd
1193185029Spjd	(void) zio_wait(zio);
1194168404Spjd
1195168404Spjd	return (error);
1196168404Spjd}
1197168404Spjd
1198168404Spjd/*
1199185029Spjd * Sync the uberblock and any changes to the vdev configuration.
1200168404Spjd *
1201168404Spjd * The order of operations is carefully crafted to ensure that
1202168404Spjd * if the system panics or loses power at any time, the state on disk
1203168404Spjd * is still transactionally consistent.  The in-line comments below
1204168404Spjd * describe the failure semantics at each stage.
1205168404Spjd *
1206185029Spjd * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1207168404Spjd * at any time, you can just call it again, and it will resume its work.
1208168404Spjd */
1209168404Spjdint
1210213198Smmvdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard)
1211168404Spjd{
1212185029Spjd	spa_t *spa = svd[0]->vdev_spa;
1213168404Spjd	uberblock_t *ub = &spa->spa_uberblock;
1214168404Spjd	vdev_t *vd;
1215168404Spjd	zio_t *zio;
1216185029Spjd	int error;
1217185029Spjd	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1218168404Spjd
1219213198Smm	/*
1220213198Smm	 * Normally, we don't want to try too hard to write every label and
1221213198Smm	 * uberblock.  If there is a flaky disk, we don't want the rest of the
1222213198Smm	 * sync process to block while we retry.  But if we can't write a
1223213198Smm	 * single label out, we should retry with ZIO_FLAG_TRYHARD before
1224213198Smm	 * bailing out and declaring the pool faulted.
1225213198Smm	 */
1226213198Smm	if (tryhard)
1227213198Smm		flags |= ZIO_FLAG_TRYHARD;
1228213198Smm
1229168404Spjd	ASSERT(ub->ub_txg <= txg);
1230168404Spjd
1231168404Spjd	/*
1232185029Spjd	 * If this isn't a resync due to I/O errors,
1233185029Spjd	 * and nothing changed in this transaction group,
1234185029Spjd	 * and the vdev configuration hasn't changed,
1235168404Spjd	 * then there's nothing to do.
1236168404Spjd	 */
1237185029Spjd	if (ub->ub_txg < txg &&
1238185029Spjd	    uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
1239185029Spjd	    list_is_empty(&spa->spa_config_dirty_list))
1240168404Spjd		return (0);
1241168404Spjd
1242168404Spjd	if (txg > spa_freeze_txg(spa))
1243168404Spjd		return (0);
1244168404Spjd
1245168404Spjd	ASSERT(txg <= spa->spa_final_txg);
1246168404Spjd
1247168404Spjd	/*
1248168404Spjd	 * Flush the write cache of every disk that's been written to
1249168404Spjd	 * in this transaction group.  This ensures that all blocks
1250168404Spjd	 * written in this txg will be committed to stable storage
1251168404Spjd	 * before any uberblock that references them.
1252168404Spjd	 */
1253185029Spjd	zio = zio_root(spa, NULL, NULL, flags);
1254185029Spjd
1255168404Spjd	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
1256185029Spjd	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
1257185029Spjd		zio_flush(zio, vd);
1258185029Spjd
1259168404Spjd	(void) zio_wait(zio);
1260168404Spjd
1261168404Spjd	/*
1262168404Spjd	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
1263168404Spjd	 * system dies in the middle of this process, that's OK: all of the
1264168404Spjd	 * even labels that made it to disk will be newer than any uberblock,
1265168404Spjd	 * and will therefore be considered invalid.  The odd labels (L1, L3),
1266185029Spjd	 * which have not yet been touched, will still be valid.  We flush
1267185029Spjd	 * the new labels to disk to ensure that all even-label updates
1268185029Spjd	 * are committed to stable storage before the uberblock update.
1269168404Spjd	 */
1270185029Spjd	if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
1271185029Spjd		return (error);
1272168404Spjd
1273168404Spjd	/*
1274185029Spjd	 * Sync the uberblocks to all vdevs in svd[].
1275168404Spjd	 * If the system dies in the middle of this step, there are two cases
1276168404Spjd	 * to consider, and the on-disk state is consistent either way:
1277168404Spjd	 *
1278168404Spjd	 * (1)	If none of the new uberblocks made it to disk, then the
1279168404Spjd	 *	previous uberblock will be the newest, and the odd labels
1280168404Spjd	 *	(which had not yet been touched) will be valid with respect
1281168404Spjd	 *	to that uberblock.
1282168404Spjd	 *
1283168404Spjd	 * (2)	If one or more new uberblocks made it to disk, then they
1284168404Spjd	 *	will be the newest, and the even labels (which had all
1285168404Spjd	 *	been successfully committed) will be valid with respect
1286168404Spjd	 *	to the new uberblocks.
1287168404Spjd	 */
1288185029Spjd	if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
1289168404Spjd		return (error);
1290168404Spjd
1291168404Spjd	/*
1292168404Spjd	 * Sync out odd labels for every dirty vdev.  If the system dies
1293168404Spjd	 * in the middle of this process, the even labels and the new
1294168404Spjd	 * uberblocks will suffice to open the pool.  The next time
1295168404Spjd	 * the pool is opened, the first thing we'll do -- before any
1296168404Spjd	 * user data is modified -- is mark every vdev dirty so that
1297185029Spjd	 * all labels will be brought up to date.  We flush the new labels
1298185029Spjd	 * to disk to ensure that all odd-label updates are committed to
1299185029Spjd	 * stable storage before the next transaction group begins.
1300168404Spjd	 */
1301240868Spjd	if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0)
1302240868Spjd		return (error);
1303240868Spjd
1304240868Spjd	trim_thread_wakeup(spa);
1305240868Spjd
1306240868Spjd	return (0);
1307168404Spjd}
1308