xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_label.c (revision 1485:e971e58d18f6)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
5*1485Slling  * Common Development and Distribution License (the "License").
6*1485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*1485Slling  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
29789Sahrens  * Virtual Device Labels
30789Sahrens  * ---------------------
31789Sahrens  *
32789Sahrens  * The vdev label serves several distinct purposes:
33789Sahrens  *
34789Sahrens  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
35789Sahrens  *	   identity within the pool.
36789Sahrens  *
37789Sahrens  * 	2. Verify that all the devices given in a configuration are present
38789Sahrens  *         within the pool.
39789Sahrens  *
40789Sahrens  * 	3. Determine the uberblock for the pool.
41789Sahrens  *
42789Sahrens  * 	4. In case of an import operation, determine the configuration of the
43789Sahrens  *         toplevel vdev of which it is a part.
44789Sahrens  *
45789Sahrens  * 	5. If an import operation cannot find all the devices in the pool,
46789Sahrens  *         provide enough information to the administrator to determine which
47789Sahrens  *         devices are missing.
48789Sahrens  *
49789Sahrens  * It is important to note that while the kernel is responsible for writing the
50789Sahrens  * label, it only consumes the information in the first three cases.  The
51789Sahrens  * latter information is only consumed in userland when determining the
52789Sahrens  * configuration to import a pool.
53789Sahrens  *
54789Sahrens  *
55789Sahrens  * Label Organization
56789Sahrens  * ------------------
57789Sahrens  *
58789Sahrens  * Before describing the contents of the label, it's important to understand how
59789Sahrens  * the labels are written and updated with respect to the uberblock.
60789Sahrens  *
61789Sahrens  * When the pool configuration is altered, either because it was newly created
62789Sahrens  * or a device was added, we want to update all the labels such that we can deal
63789Sahrens  * with fatal failure at any point.  To this end, each disk has two labels which
64789Sahrens  * are updated before and after the uberblock is synced.  Assuming we have
65789Sahrens  * labels and an uberblock with the following transacation groups:
66789Sahrens  *
67789Sahrens  *              L1          UB          L2
68789Sahrens  *           +------+    +------+    +------+
69789Sahrens  *           |      |    |      |    |      |
70789Sahrens  *           | t10  |    | t10  |    | t10  |
71789Sahrens  *           |      |    |      |    |      |
72789Sahrens  *           +------+    +------+    +------+
73789Sahrens  *
74789Sahrens  * In this stable state, the labels and the uberblock were all updated within
75789Sahrens  * the same transaction group (10).  Each label is mirrored and checksummed, so
76789Sahrens  * that we can detect when we fail partway through writing the label.
77789Sahrens  *
78789Sahrens  * In order to identify which labels are valid, the labels are written in the
79789Sahrens  * following manner:
80789Sahrens  *
81789Sahrens  * 	1. For each vdev, update 'L1' to the new label
82789Sahrens  * 	2. Update the uberblock
83789Sahrens  * 	3. For each vdev, update 'L2' to the new label
84789Sahrens  *
85789Sahrens  * Given arbitrary failure, we can determine the correct label to use based on
86789Sahrens  * the transaction group.  If we fail after updating L1 but before updating the
87789Sahrens  * UB, we will notice that L1's transaction group is greater than the uberblock,
88789Sahrens  * so L2 must be valid.  If we fail after writing the uberblock but before
89789Sahrens  * writing L2, we will notice that L2's transaction group is less than L1, and
90789Sahrens  * therefore L1 is valid.
91789Sahrens  *
92789Sahrens  * Another added complexity is that not every label is updated when the config
93789Sahrens  * is synced.  If we add a single device, we do not want to have to re-write
94789Sahrens  * every label for every device in the pool.  This means that both L1 and L2 may
95789Sahrens  * be older than the pool uberblock, because the necessary information is stored
96789Sahrens  * on another vdev.
97789Sahrens  *
98789Sahrens  *
99789Sahrens  * On-disk Format
100789Sahrens  * --------------
101789Sahrens  *
102789Sahrens  * The vdev label consists of two distinct parts, and is wrapped within the
103789Sahrens  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
104789Sahrens  * VTOC disk labels, but is otherwise ignored.
105789Sahrens  *
106789Sahrens  * The first half of the label is a packed nvlist which contains pool wide
107789Sahrens  * properties, per-vdev properties, and configuration information.  It is
108789Sahrens  * described in more detail below.
109789Sahrens  *
110789Sahrens  * The latter half of the label consists of a redundant array of uberblocks.
111789Sahrens  * These uberblocks are updated whenever a transaction group is committed,
112789Sahrens  * or when the configuration is updated.  When a pool is loaded, we scan each
113789Sahrens  * vdev for the 'best' uberblock.
114789Sahrens  *
115789Sahrens  *
116789Sahrens  * Configuration Information
117789Sahrens  * -------------------------
118789Sahrens  *
119789Sahrens  * The nvlist describing the pool and vdev contains the following elements:
120789Sahrens  *
121789Sahrens  * 	version		ZFS on-disk version
122789Sahrens  * 	name		Pool name
123789Sahrens  * 	state		Pool state
124789Sahrens  * 	txg		Transaction group in which this label was written
125789Sahrens  * 	pool_guid	Unique identifier for this pool
126789Sahrens  * 	vdev_tree	An nvlist describing vdev tree.
127789Sahrens  *
128789Sahrens  * Each leaf device label also contains the following:
129789Sahrens  *
130789Sahrens  * 	top_guid	Unique ID for top-level vdev in which this is contained
131789Sahrens  * 	guid		Unique ID for the leaf vdev
132789Sahrens  *
133789Sahrens  * The 'vs' configuration follows the format described in 'spa_config.c'.
134789Sahrens  */
135789Sahrens 
136789Sahrens #include <sys/zfs_context.h>
137789Sahrens #include <sys/spa.h>
138789Sahrens #include <sys/spa_impl.h>
139789Sahrens #include <sys/dmu.h>
140789Sahrens #include <sys/zap.h>
141789Sahrens #include <sys/vdev.h>
142789Sahrens #include <sys/vdev_impl.h>
143789Sahrens #include <sys/uberblock_impl.h>
144789Sahrens #include <sys/metaslab.h>
145789Sahrens #include <sys/zio.h>
146789Sahrens #include <sys/fs/zfs.h>
147789Sahrens 
148789Sahrens /*
149789Sahrens  * Basic routines to read and write from a vdev label.
150789Sahrens  * Used throughout the rest of this file.
151789Sahrens  */
152789Sahrens uint64_t
153789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
154789Sahrens {
155789Sahrens 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
156789Sahrens 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
157789Sahrens }
158789Sahrens 
159789Sahrens static void
160789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
161789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
162789Sahrens {
163789Sahrens 	ASSERT(vd->vdev_children == 0);
164789Sahrens 
165789Sahrens 	zio_nowait(zio_read_phys(zio, vd,
166789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
167789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
168789Sahrens 	    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_SPECULATIVE |
169789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_DONT_RETRY));
170789Sahrens }
171789Sahrens 
172789Sahrens static void
173789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
174789Sahrens 	uint64_t size, zio_done_func_t *done, void *private)
175789Sahrens {
176789Sahrens 	ASSERT(vd->vdev_children == 0);
177789Sahrens 
178789Sahrens 	zio_nowait(zio_write_phys(zio, vd,
179789Sahrens 	    vdev_label_offset(vd->vdev_psize, l, offset),
180789Sahrens 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
181789Sahrens 	    ZIO_PRIORITY_SYNC_WRITE,
182789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_DONT_RETRY));
183789Sahrens }
184789Sahrens 
185789Sahrens /*
186789Sahrens  * Generate the nvlist representing this vdev's config.
187789Sahrens  */
188789Sahrens nvlist_t *
189789Sahrens vdev_config_generate(vdev_t *vd, int getstats)
190789Sahrens {
191789Sahrens 	nvlist_t *nv = NULL;
192789Sahrens 
193789Sahrens 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) == 0);
194789Sahrens 
195789Sahrens 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
196789Sahrens 	    vd->vdev_ops->vdev_op_type) == 0);
197789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0);
198789Sahrens 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
199789Sahrens 
200789Sahrens 	if (vd->vdev_path != NULL)
201789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
202789Sahrens 		    vd->vdev_path) == 0);
203789Sahrens 
204789Sahrens 	if (vd->vdev_devid != NULL)
205789Sahrens 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
206789Sahrens 		    vd->vdev_devid) == 0);
207789Sahrens 
2081171Seschrock 	if (vd->vdev_wholedisk != -1ULL)
2091171Seschrock 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2101171Seschrock 		    vd->vdev_wholedisk) == 0);
2111171Seschrock 
212789Sahrens 	if (vd == vd->vdev_top) {
213789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
214789Sahrens 		    vd->vdev_ms_array) == 0);
215789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
216789Sahrens 		    vd->vdev_ms_shift) == 0);
217789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
218789Sahrens 		    vd->vdev_ashift) == 0);
219789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
220789Sahrens 		    vd->vdev_asize) == 0);
221789Sahrens 	}
222789Sahrens 
223789Sahrens 	if (vd->vdev_dtl.smo_object != 0)
224789Sahrens 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
225789Sahrens 		    vd->vdev_dtl.smo_object) == 0);
226789Sahrens 
227789Sahrens 	if (getstats) {
228789Sahrens 		vdev_stat_t vs;
229789Sahrens 		vdev_get_stats(vd, &vs);
230789Sahrens 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
231789Sahrens 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
232789Sahrens 	}
233789Sahrens 
234789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf) {
235789Sahrens 		nvlist_t **child;
236789Sahrens 		int c;
237789Sahrens 
238789Sahrens 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
239789Sahrens 		    KM_SLEEP);
240789Sahrens 
241789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
242789Sahrens 			child[c] = vdev_config_generate(vd->vdev_child[c],
243789Sahrens 			    getstats);
244789Sahrens 
245789Sahrens 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
246789Sahrens 		    child, vd->vdev_children) == 0);
247789Sahrens 
248789Sahrens 		for (c = 0; c < vd->vdev_children; c++)
249789Sahrens 			nvlist_free(child[c]);
250789Sahrens 
251789Sahrens 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
252*1485Slling 
253*1485Slling 	} else {
254*1485Slling 		if (!vd->vdev_tmpoffline) {
255*1485Slling 		    if (vd->vdev_offline)
256*1485Slling 			VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
257*1485Slling 				B_TRUE) == 0);
258*1485Slling 		    else
259*1485Slling 			(void) nvlist_remove(nv, ZPOOL_CONFIG_OFFLINE,
260*1485Slling 				DATA_TYPE_UINT64);
261*1485Slling 		}
262789Sahrens 	}
263789Sahrens 
264789Sahrens 	return (nv);
265789Sahrens }
266789Sahrens 
267789Sahrens nvlist_t *
268789Sahrens vdev_label_read_config(vdev_t *vd)
269789Sahrens {
270789Sahrens 	nvlist_t *config = NULL;
271789Sahrens 	vdev_phys_t *vp;
272789Sahrens 	uint64_t version;
273789Sahrens 	zio_t *zio;
274789Sahrens 	int l;
275789Sahrens 
276789Sahrens 	if (vdev_is_dead(vd))
277789Sahrens 		return (NULL);
278789Sahrens 
279789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
280789Sahrens 
281789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
282789Sahrens 
283789Sahrens 		zio = zio_root(vd->vdev_spa, NULL, NULL,
284789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD);
285789Sahrens 
286789Sahrens 		vdev_label_read(zio, vd, l, vp,
287789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
288789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
289789Sahrens 
290789Sahrens 		if (zio_wait(zio) == 0 &&
291789Sahrens 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
292789Sahrens 		    &config, 0) == 0 &&
293789Sahrens 		    nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
294789Sahrens 		    &version) == 0 &&
295789Sahrens 		    version == UBERBLOCK_VERSION)
296789Sahrens 			break;
297789Sahrens 
298789Sahrens 		if (config != NULL) {
299789Sahrens 			nvlist_free(config);
300789Sahrens 			config = NULL;
301789Sahrens 		}
302789Sahrens 	}
303789Sahrens 
304789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
305789Sahrens 
306789Sahrens 	return (config);
307789Sahrens }
308789Sahrens 
309789Sahrens int
310789Sahrens vdev_label_init(vdev_t *vd, uint64_t crtxg)
311789Sahrens {
312789Sahrens 	spa_t *spa = vd->vdev_spa;
313789Sahrens 	nvlist_t *label;
314789Sahrens 	vdev_phys_t *vp;
315789Sahrens 	vdev_boot_header_t *vb;
316789Sahrens 	uberblock_phys_t *ubphys;
317789Sahrens 	zio_t *zio;
318789Sahrens 	int l, c, n;
319789Sahrens 	char *buf;
320789Sahrens 	size_t buflen;
321789Sahrens 	int error;
322789Sahrens 
323789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
324789Sahrens 		if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0)
325789Sahrens 			return (error);
326789Sahrens 
327789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
328789Sahrens 		return (0);
329789Sahrens 
330789Sahrens 	/*
331789Sahrens 	 * Make sure each leaf device is writable, and zero its initial content.
332789Sahrens 	 * Along the way, also make sure that no leaf is already in use.
333789Sahrens 	 * Note that it's important to do this sequentially, not in parallel,
334789Sahrens 	 * so that we catch cases of multiple use of the same leaf vdev in
335789Sahrens 	 * the vdev we're creating -- e.g. mirroring a disk with itself.
336789Sahrens 	 */
337789Sahrens 	if (vdev_is_dead(vd))
338789Sahrens 		return (EIO);
339789Sahrens 
340789Sahrens 	/*
341789Sahrens 	 * Check whether this device is already in use.
342789Sahrens 	 * Ignore the check if crtxg == 0, which we use for device removal.
343789Sahrens 	 */
344789Sahrens 	if (crtxg != 0 && (label = vdev_label_read_config(vd)) != NULL) {
345789Sahrens 		uint64_t version, state, pool_guid, device_guid, txg;
346789Sahrens 		uint64_t mycrtxg = 0;
347789Sahrens 
348789Sahrens 		(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
349789Sahrens 		    &mycrtxg);
350789Sahrens 
351789Sahrens 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION,
352789Sahrens 		    &version) == 0 && version == UBERBLOCK_VERSION &&
353789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
354789Sahrens 		    &state) == 0 && state == POOL_STATE_ACTIVE &&
355789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
356789Sahrens 		    &pool_guid) == 0 &&
357789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
358789Sahrens 		    &device_guid) == 0 &&
359789Sahrens 		    spa_guid_exists(pool_guid, device_guid) &&
360789Sahrens 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
361789Sahrens 		    &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) {
362789Sahrens 			dprintf("vdev %s in use, pool_state %d\n",
363789Sahrens 			    vdev_description(vd), state);
364789Sahrens 			nvlist_free(label);
365789Sahrens 			return (EBUSY);
366789Sahrens 		}
367789Sahrens 		nvlist_free(label);
368789Sahrens 	}
369789Sahrens 
370789Sahrens 	/*
371789Sahrens 	 * The device isn't in use, so initialize its label.
372789Sahrens 	 */
373789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
374789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
375789Sahrens 
376789Sahrens 	/*
377789Sahrens 	 * Generate a label describing the pool and our top-level vdev.
378789Sahrens 	 * We mark it as being from txg 0 to indicate that it's not
379789Sahrens 	 * really part of an active pool just yet.  The labels will
380789Sahrens 	 * be written again with a meaningful txg by spa_sync().
381789Sahrens 	 */
382789Sahrens 	label = spa_config_generate(spa, vd, 0ULL, 0);
383789Sahrens 
384789Sahrens 	/*
385789Sahrens 	 * Add our creation time.  This allows us to detect multiple vdev
386789Sahrens 	 * uses as described above, and automatically expires if we fail.
387789Sahrens 	 */
388789Sahrens 	VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0);
389789Sahrens 
390789Sahrens 	buf = vp->vp_nvlist;
391789Sahrens 	buflen = sizeof (vp->vp_nvlist);
392789Sahrens 
393789Sahrens 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, 0) != 0) {
394789Sahrens 		nvlist_free(label);
395789Sahrens 		zio_buf_free(vp, sizeof (vdev_phys_t));
396789Sahrens 		return (EINVAL);
397789Sahrens 	}
398789Sahrens 
399789Sahrens 	/*
400789Sahrens 	 * Initialize boot block header.
401789Sahrens 	 */
402789Sahrens 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
403789Sahrens 	bzero(vb, sizeof (vdev_boot_header_t));
404789Sahrens 	vb->vb_magic = VDEV_BOOT_MAGIC;
405789Sahrens 	vb->vb_version = VDEV_BOOT_VERSION;
406789Sahrens 	vb->vb_offset = VDEV_BOOT_OFFSET;
407789Sahrens 	vb->vb_size = VDEV_BOOT_SIZE;
408789Sahrens 
409789Sahrens 	/*
410789Sahrens 	 * Initialize uberblock template.
411789Sahrens 	 */
412789Sahrens 	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
413789Sahrens 	bzero(ubphys, sizeof (uberblock_phys_t));
414789Sahrens 	ubphys->ubp_uberblock = spa->spa_uberblock;
415789Sahrens 	ubphys->ubp_uberblock.ub_txg = 0;
416789Sahrens 
417789Sahrens 	/*
418789Sahrens 	 * Write everything in parallel.
419789Sahrens 	 */
420789Sahrens 	zio = zio_root(spa, NULL, NULL,
421789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
422789Sahrens 
423789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
424789Sahrens 
425789Sahrens 		vdev_label_write(zio, vd, l, vp,
426789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys),
427789Sahrens 		    sizeof (vdev_phys_t), NULL, NULL);
428789Sahrens 
429789Sahrens 		vdev_label_write(zio, vd, l, vb,
430789Sahrens 		    offsetof(vdev_label_t, vl_boot_header),
431789Sahrens 		    sizeof (vdev_boot_header_t), NULL, NULL);
432789Sahrens 
433789Sahrens 		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
434789Sahrens 
435789Sahrens 			vdev_label_write(zio, vd, l, ubphys,
436789Sahrens 			    offsetof(vdev_label_t, vl_uberblock[n]),
437789Sahrens 			    sizeof (uberblock_phys_t), NULL, NULL);
438789Sahrens 
439789Sahrens 		}
440789Sahrens 	}
441789Sahrens 
442789Sahrens 	error = zio_wait(zio);
443789Sahrens 
444789Sahrens 	nvlist_free(label);
445789Sahrens 	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
446789Sahrens 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
447789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
448789Sahrens 
449789Sahrens 	return (error);
450789Sahrens }
451789Sahrens 
452789Sahrens /*
453789Sahrens  * ==========================================================================
454789Sahrens  * uberblock load/sync
455789Sahrens  * ==========================================================================
456789Sahrens  */
457789Sahrens 
458789Sahrens /*
459789Sahrens  * Consider the following situation: txg is safely synced to disk.  We've
460789Sahrens  * written the first uberblock for txg + 1, and then we lose power.  When we
461789Sahrens  * come back up, we fail to see the uberblock for txg + 1 because, say,
462789Sahrens  * it was on a mirrored device and the replica to which we wrote txg + 1
463789Sahrens  * is now offline.  If we then make some changes and sync txg + 1, and then
464789Sahrens  * the missing replica comes back, then for a new seconds we'll have two
465789Sahrens  * conflicting uberblocks on disk with the same txg.  The solution is simple:
466789Sahrens  * among uberblocks with equal txg, choose the one with the latest timestamp.
467789Sahrens  */
468789Sahrens static int
469789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
470789Sahrens {
471789Sahrens 	if (ub1->ub_txg < ub2->ub_txg)
472789Sahrens 		return (-1);
473789Sahrens 	if (ub1->ub_txg > ub2->ub_txg)
474789Sahrens 		return (1);
475789Sahrens 
476789Sahrens 	if (ub1->ub_timestamp < ub2->ub_timestamp)
477789Sahrens 		return (-1);
478789Sahrens 	if (ub1->ub_timestamp > ub2->ub_timestamp)
479789Sahrens 		return (1);
480789Sahrens 
481789Sahrens 	return (0);
482789Sahrens }
483789Sahrens 
484789Sahrens static void
485789Sahrens vdev_uberblock_load_done(zio_t *zio)
486789Sahrens {
487789Sahrens 	uberblock_phys_t *ubphys = zio->io_data;
488789Sahrens 	uberblock_t *ub = &ubphys->ubp_uberblock;
489789Sahrens 	uberblock_t *ubbest = zio->io_private;
490789Sahrens 	spa_t *spa = zio->io_spa;
491789Sahrens 
492789Sahrens 	ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t));
493789Sahrens 
494789Sahrens 	if (uberblock_verify(ub) == 0) {
495789Sahrens 		mutex_enter(&spa->spa_uberblock_lock);
496789Sahrens 		if (vdev_uberblock_compare(ub, ubbest) > 0)
497789Sahrens 			*ubbest = *ub;
498789Sahrens 		mutex_exit(&spa->spa_uberblock_lock);
499789Sahrens 	}
500789Sahrens 
501789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
502789Sahrens }
503789Sahrens 
504789Sahrens void
505789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
506789Sahrens {
507789Sahrens 	int l, c, n;
508789Sahrens 
509789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
510789Sahrens 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
511789Sahrens 
512789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
513789Sahrens 		return;
514789Sahrens 
515789Sahrens 	if (vdev_is_dead(vd))
516789Sahrens 		return;
517789Sahrens 
518789Sahrens 	for (l = 0; l < VDEV_LABELS; l++) {
519789Sahrens 		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
520789Sahrens 			vdev_label_read(zio, vd, l,
521789Sahrens 			    zio_buf_alloc(sizeof (uberblock_phys_t)),
522789Sahrens 			    offsetof(vdev_label_t, vl_uberblock[n]),
523789Sahrens 			    sizeof (uberblock_phys_t),
524789Sahrens 			    vdev_uberblock_load_done, ubbest);
525789Sahrens 		}
526789Sahrens 	}
527789Sahrens }
528789Sahrens 
529789Sahrens /*
530789Sahrens  * Write the uberblock to both labels of all leaves of the specified vdev.
531789Sahrens  */
532789Sahrens static void
533789Sahrens vdev_uberblock_sync_done(zio_t *zio)
534789Sahrens {
535789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
536789Sahrens 
537789Sahrens 	if (zio->io_error == 0)
538789Sahrens 		atomic_add_64(good_writes, 1);
539789Sahrens }
540789Sahrens 
541789Sahrens static void
542789Sahrens vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd,
543789Sahrens 	uint64_t txg)
544789Sahrens {
545789Sahrens 	int l, c, n;
546789Sahrens 
547789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
548789Sahrens 		vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg);
549789Sahrens 
550789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
551789Sahrens 		return;
552789Sahrens 
553789Sahrens 	if (vdev_is_dead(vd))
554789Sahrens 		return;
555789Sahrens 
556789Sahrens 	n = txg & (VDEV_UBERBLOCKS - 1);
557789Sahrens 
558789Sahrens 	ASSERT(ubphys->ubp_uberblock.ub_txg == txg);
559789Sahrens 
560789Sahrens 	for (l = 0; l < VDEV_LABELS; l++)
561789Sahrens 		vdev_label_write(zio, vd, l, ubphys,
562789Sahrens 		    offsetof(vdev_label_t, vl_uberblock[n]),
563789Sahrens 		    sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL);
564789Sahrens 
565789Sahrens 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
566789Sahrens }
567789Sahrens 
568789Sahrens static int
569789Sahrens vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg)
570789Sahrens {
571789Sahrens 	uberblock_phys_t *ubphys;
572789Sahrens 	uint64_t *good_writes;
573789Sahrens 	zio_t *zio;
574789Sahrens 	int error;
575789Sahrens 
576789Sahrens 	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
577789Sahrens 	bzero(ubphys, sizeof (uberblock_phys_t));
578789Sahrens 	ubphys->ubp_uberblock = *ub;
579789Sahrens 
580789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
581789Sahrens 
582789Sahrens 	zio = zio_root(spa, NULL, good_writes,
583789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
584789Sahrens 
585789Sahrens 	vdev_uberblock_sync(zio, ubphys, uvd, txg);
586789Sahrens 
587789Sahrens 	error = zio_wait(zio);
588789Sahrens 
589789Sahrens 	if (error && *good_writes != 0) {
590789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
591789Sahrens 		error = 0;
592789Sahrens 	}
593789Sahrens 
594789Sahrens 	/*
595789Sahrens 	 * It's possible to have no good writes and no error if every vdev is in
596789Sahrens 	 * the CANT_OPEN state.
597789Sahrens 	 */
598789Sahrens 	if (*good_writes == 0 && error == 0)
599789Sahrens 		error = EIO;
600789Sahrens 
601789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
602789Sahrens 	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
603789Sahrens 
604789Sahrens 	return (error);
605789Sahrens }
606789Sahrens 
607789Sahrens /*
608789Sahrens  * Sync out an individual vdev.
609789Sahrens  */
610789Sahrens static void
611789Sahrens vdev_sync_label_done(zio_t *zio)
612789Sahrens {
613789Sahrens 	uint64_t *good_writes = zio->io_root->io_private;
614789Sahrens 
615789Sahrens 	if (zio->io_error == 0)
616789Sahrens 		atomic_add_64(good_writes, 1);
617789Sahrens }
618789Sahrens 
619789Sahrens static void
620789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
621789Sahrens {
622789Sahrens 	nvlist_t *label;
623789Sahrens 	vdev_phys_t *vp;
624789Sahrens 	char *buf;
625789Sahrens 	size_t buflen;
626789Sahrens 	int c;
627789Sahrens 
628789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
629789Sahrens 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
630789Sahrens 
631789Sahrens 	if (!vd->vdev_ops->vdev_op_leaf)
632789Sahrens 		return;
633789Sahrens 
634789Sahrens 	if (vdev_is_dead(vd))
635789Sahrens 		return;
636789Sahrens 
637789Sahrens 	/*
638789Sahrens 	 * Generate a label describing the top-level config to which we belong.
639789Sahrens 	 */
640789Sahrens 	label = spa_config_generate(vd->vdev_spa, vd, txg, 0);
641789Sahrens 
642789Sahrens 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
643789Sahrens 	bzero(vp, sizeof (vdev_phys_t));
644789Sahrens 
645789Sahrens 	buf = vp->vp_nvlist;
646789Sahrens 	buflen = sizeof (vp->vp_nvlist);
647789Sahrens 
648789Sahrens 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, 0) == 0)
649789Sahrens 		vdev_label_write(zio, vd, l, vp,
650789Sahrens 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
651789Sahrens 		    vdev_sync_label_done, NULL);
652789Sahrens 
653789Sahrens 	zio_buf_free(vp, sizeof (vdev_phys_t));
654789Sahrens 	nvlist_free(label);
655789Sahrens 
656789Sahrens 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
657789Sahrens }
658789Sahrens 
659789Sahrens static int
660789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
661789Sahrens {
662789Sahrens 	uint64_t *good_writes;
663789Sahrens 	zio_t *zio;
664789Sahrens 	int error;
665789Sahrens 
666789Sahrens 	ASSERT(vd == vd->vdev_top);
667789Sahrens 
668789Sahrens 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
669789Sahrens 
670789Sahrens 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
671789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
672789Sahrens 
673789Sahrens 	/*
674789Sahrens 	 * Recursively kick off writes to all labels.
675789Sahrens 	 */
676789Sahrens 	vdev_sync_label(zio, vd, l, txg);
677789Sahrens 
678789Sahrens 	error = zio_wait(zio);
679789Sahrens 
680789Sahrens 	if (error && *good_writes != 0) {
681789Sahrens 		dprintf("partial success: good_writes = %llu\n", *good_writes);
682789Sahrens 		error = 0;
683789Sahrens 	}
684789Sahrens 
685789Sahrens 	if (*good_writes == 0 && error == 0)
686789Sahrens 		error = ENODEV;
687789Sahrens 
688789Sahrens 	kmem_free(good_writes, sizeof (uint64_t));
689789Sahrens 
690789Sahrens 	return (error);
691789Sahrens }
692789Sahrens 
693789Sahrens /*
694789Sahrens  * Sync the entire vdev configuration.
695789Sahrens  *
696789Sahrens  * The order of operations is carefully crafted to ensure that
697789Sahrens  * if the system panics or loses power at any time, the state on disk
698789Sahrens  * is still transactionally consistent.  The in-line comments below
699789Sahrens  * describe the failure semantics at each stage.
700789Sahrens  *
701789Sahrens  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
702789Sahrens  * at any time, you can just call it again, and it will resume its work.
703789Sahrens  */
704789Sahrens int
705789Sahrens spa_sync_labels(spa_t *spa, uint64_t txg)
706789Sahrens {
707789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
708789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
709789Sahrens 	vdev_t *vd, *uvd;
710789Sahrens 	zio_t *zio;
711789Sahrens 	int c, l, error;
712789Sahrens 
713789Sahrens 	ASSERT(ub->ub_txg <= txg);
714789Sahrens 
715789Sahrens 	/*
716789Sahrens 	 * If this isn't a resync due to I/O errors, and nothing changed
717789Sahrens 	 * in this transaction group, and the vdev configuration hasn't changed,
718789Sahrens 	 * and this isn't an explicit sync-all, then there's nothing to do.
719789Sahrens 	 */
720789Sahrens 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
721789Sahrens 	    list_is_empty(&spa->spa_dirty_list)) {
722789Sahrens 		dprintf("nothing to sync in %s in txg %llu\n",
723789Sahrens 		    spa_name(spa), txg);
724789Sahrens 		return (0);
725789Sahrens 	}
726789Sahrens 
727789Sahrens 	if (txg > spa_freeze_txg(spa))
728789Sahrens 		return (0);
729789Sahrens 
730789Sahrens 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
731789Sahrens 
732789Sahrens 	/*
733789Sahrens 	 * Flush the write cache of every disk that's been written to
734789Sahrens 	 * in this transaction group.  This ensures that all blocks
735789Sahrens 	 * written in this txg will be committed to stable storage
736789Sahrens 	 * before any uberblock that references them.
737789Sahrens 	 */
738789Sahrens 	zio = zio_root(spa, NULL, NULL,
739789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
740789Sahrens 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
741789Sahrens 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
742789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
743789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
744789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
745789Sahrens 	}
746789Sahrens 	(void) zio_wait(zio);
747789Sahrens 
748789Sahrens 	/*
749789Sahrens 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
750789Sahrens 	 * system dies in the middle of this process, that's OK: all of the
751789Sahrens 	 * even labels that made it to disk will be newer than any uberblock,
752789Sahrens 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
753789Sahrens 	 * which have not yet been touched, will still be valid.
754789Sahrens 	 */
755789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
756789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
757789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
758789Sahrens 			if (l & 1)
759789Sahrens 				continue;
760789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
761789Sahrens 				return (error);
762789Sahrens 		}
763789Sahrens 	}
764789Sahrens 
765789Sahrens 	/*
766789Sahrens 	 * Flush the new labels to disk.  This ensures that all even-label
767789Sahrens 	 * updates are committed to stable storage before the uberblock update.
768789Sahrens 	 */
769789Sahrens 	zio = zio_root(spa, NULL, NULL,
770789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
771789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
772789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
773789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
774789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
775789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
776789Sahrens 	}
777789Sahrens 	(void) zio_wait(zio);
778789Sahrens 
779789Sahrens 	/*
780789Sahrens 	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
781789Sahrens 	 * Otherwise, pick one top-level vdev at random.
782789Sahrens 	 */
783789Sahrens 	if (!list_is_empty(&spa->spa_dirty_list))
784789Sahrens 		uvd = rvd;
785789Sahrens 	else
786789Sahrens 		uvd = rvd->vdev_child[spa_get_random(rvd->vdev_children)];
787789Sahrens 
788789Sahrens 	/*
789789Sahrens 	 * Sync the uberblocks.  If the system dies in the middle of this
790789Sahrens 	 * step, there are two cases to consider, and the on-disk state
791789Sahrens 	 * is consistent either way:
792789Sahrens 	 *
793789Sahrens 	 * (1)	If none of the new uberblocks made it to disk, then the
794789Sahrens 	 *	previous uberblock will be the newest, and the odd labels
795789Sahrens 	 *	(which had not yet been touched) will be valid with respect
796789Sahrens 	 *	to that uberblock.
797789Sahrens 	 *
798789Sahrens 	 * (2)	If one or more new uberblocks made it to disk, then they
799789Sahrens 	 *	will be the newest, and the even labels (which had all
800789Sahrens 	 *	been successfully committed) will be valid with respect
801789Sahrens 	 *	to the new uberblocks.
802789Sahrens 	 */
803789Sahrens 	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
804789Sahrens 		return (error);
805789Sahrens 
806789Sahrens 	/*
807789Sahrens 	 * Flush the uberblocks to disk.  This ensures that the odd labels
808789Sahrens 	 * are no longer needed (because the new uberblocks and the even
809789Sahrens 	 * labels are safely on disk), so it is safe to overwrite them.
810789Sahrens 	 */
811789Sahrens 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
812789Sahrens 	    NULL, NULL, ZIO_PRIORITY_NOW,
813789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
814789Sahrens 
815789Sahrens 	/*
816789Sahrens 	 * Sync out odd labels for every dirty vdev.  If the system dies
817789Sahrens 	 * in the middle of this process, the even labels and the new
818789Sahrens 	 * uberblocks will suffice to open the pool.  The next time
819789Sahrens 	 * the pool is opened, the first thing we'll do -- before any
820789Sahrens 	 * user data is modified -- is mark every vdev dirty so that
821789Sahrens 	 * all labels will be brought up to date.
822789Sahrens 	 */
823789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
824789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
825789Sahrens 		for (l = 0; l < VDEV_LABELS; l++) {
826789Sahrens 			if ((l & 1) == 0)
827789Sahrens 				continue;
828789Sahrens 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
829789Sahrens 				return (error);
830789Sahrens 		}
831789Sahrens 	}
832789Sahrens 
833789Sahrens 	/*
834789Sahrens 	 * Flush the new labels to disk.  This ensures that all odd-label
835789Sahrens 	 * updates are committed to stable storage before the next
836789Sahrens 	 * transaction group begins.
837789Sahrens 	 */
838789Sahrens 	zio = zio_root(spa, NULL, NULL,
839789Sahrens 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
840789Sahrens 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
841789Sahrens 	    vd = list_next(&spa->spa_dirty_list, vd)) {
842789Sahrens 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
843789Sahrens 		    NULL, NULL, ZIO_PRIORITY_NOW,
844789Sahrens 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
845789Sahrens 	}
846789Sahrens 	(void) zio_wait(zio);
847789Sahrens 
848789Sahrens 	/*
849789Sahrens 	 * Clear the dirty list.
850789Sahrens 	 */
851789Sahrens 	while (!list_is_empty(&spa->spa_dirty_list))
852789Sahrens 		vdev_config_clean(list_head(&spa->spa_dirty_list));
853789Sahrens 
854789Sahrens #ifdef DEBUG
855789Sahrens 	for (c = 0; c < rvd->vdev_children; c++) {
856789Sahrens 		ASSERT(rvd->vdev_child[c]->vdev_is_dirty == 0);
857789Sahrens 	}
858789Sahrens #endif
859789Sahrens 
860789Sahrens 	return (0);
861789Sahrens }
862