xref: /titanic_51/usr/src/uts/common/fs/zfs/vdev_label.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Virtual Device Labels
31  * ---------------------
32  *
33  * The vdev label serves several distinct purposes:
34  *
35  *	1. Uniquely identify this device as part of a ZFS pool and confirm its
36  *	   identity within the pool.
37  *
38  * 	2. Verify that all the devices given in a configuration are present
39  *         within the pool.
40  *
41  * 	3. Determine the uberblock for the pool.
42  *
43  * 	4. In case of an import operation, determine the configuration of the
44  *         toplevel vdev of which it is a part.
45  *
46  * 	5. If an import operation cannot find all the devices in the pool,
47  *         provide enough information to the administrator to determine which
48  *         devices are missing.
49  *
50  * It is important to note that while the kernel is responsible for writing the
51  * label, it only consumes the information in the first three cases.  The
52  * latter information is only consumed in userland when determining the
53  * configuration to import a pool.
54  *
55  *
56  * Label Organization
57  * ------------------
58  *
59  * Before describing the contents of the label, it's important to understand how
60  * the labels are written and updated with respect to the uberblock.
61  *
62  * When the pool configuration is altered, either because it was newly created
63  * or a device was added, we want to update all the labels such that we can deal
64  * with fatal failure at any point.  To this end, each disk has two labels which
65  * are updated before and after the uberblock is synced.  Assuming we have
66  * labels and an uberblock with the following transacation groups:
67  *
68  *              L1          UB          L2
69  *           +------+    +------+    +------+
70  *           |      |    |      |    |      |
71  *           | t10  |    | t10  |    | t10  |
72  *           |      |    |      |    |      |
73  *           +------+    +------+    +------+
74  *
75  * In this stable state, the labels and the uberblock were all updated within
76  * the same transaction group (10).  Each label is mirrored and checksummed, so
77  * that we can detect when we fail partway through writing the label.
78  *
79  * In order to identify which labels are valid, the labels are written in the
80  * following manner:
81  *
82  * 	1. For each vdev, update 'L1' to the new label
83  * 	2. Update the uberblock
84  * 	3. For each vdev, update 'L2' to the new label
85  *
86  * Given arbitrary failure, we can determine the correct label to use based on
87  * the transaction group.  If we fail after updating L1 but before updating the
88  * UB, we will notice that L1's transaction group is greater than the uberblock,
89  * so L2 must be valid.  If we fail after writing the uberblock but before
90  * writing L2, we will notice that L2's transaction group is less than L1, and
91  * therefore L1 is valid.
92  *
93  * Another added complexity is that not every label is updated when the config
94  * is synced.  If we add a single device, we do not want to have to re-write
95  * every label for every device in the pool.  This means that both L1 and L2 may
96  * be older than the pool uberblock, because the necessary information is stored
97  * on another vdev.
98  *
99  *
100  * On-disk Format
101  * --------------
102  *
103  * The vdev label consists of two distinct parts, and is wrapped within the
104  * vdev_label_t structure.  The label includes 8k of padding to permit legacy
105  * VTOC disk labels, but is otherwise ignored.
106  *
107  * The first half of the label is a packed nvlist which contains pool wide
108  * properties, per-vdev properties, and configuration information.  It is
109  * described in more detail below.
110  *
111  * The latter half of the label consists of a redundant array of uberblocks.
112  * These uberblocks are updated whenever a transaction group is committed,
113  * or when the configuration is updated.  When a pool is loaded, we scan each
114  * vdev for the 'best' uberblock.
115  *
116  *
117  * Configuration Information
118  * -------------------------
119  *
120  * The nvlist describing the pool and vdev contains the following elements:
121  *
122  * 	version		ZFS on-disk version
123  * 	name		Pool name
124  * 	state		Pool state
125  * 	txg		Transaction group in which this label was written
126  * 	pool_guid	Unique identifier for this pool
127  * 	vdev_tree	An nvlist describing vdev tree.
128  *
129  * Each leaf device label also contains the following:
130  *
131  * 	top_guid	Unique ID for top-level vdev in which this is contained
132  * 	guid		Unique ID for the leaf vdev
133  *
134  * The 'vs' configuration follows the format described in 'spa_config.c'.
135  */
136 
137 #include <sys/zfs_context.h>
138 #include <sys/spa.h>
139 #include <sys/spa_impl.h>
140 #include <sys/dmu.h>
141 #include <sys/zap.h>
142 #include <sys/vdev.h>
143 #include <sys/vdev_impl.h>
144 #include <sys/uberblock_impl.h>
145 #include <sys/metaslab.h>
146 #include <sys/zio.h>
147 #include <sys/fs/zfs.h>
148 
149 /*
150  * Basic routines to read and write from a vdev label.
151  * Used throughout the rest of this file.
152  */
153 uint64_t
154 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
155 {
156 	return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
157 	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
158 }
159 
160 static void
161 vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
162 	uint64_t size, zio_done_func_t *done, void *private)
163 {
164 	ASSERT(vd->vdev_children == 0);
165 
166 	zio_nowait(zio_read_phys(zio, vd,
167 	    vdev_label_offset(vd->vdev_psize, l, offset),
168 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
169 	    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_SPECULATIVE |
170 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_DONT_RETRY));
171 }
172 
173 static void
174 vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
175 	uint64_t size, zio_done_func_t *done, void *private)
176 {
177 	ASSERT(vd->vdev_children == 0);
178 
179 	zio_nowait(zio_write_phys(zio, vd,
180 	    vdev_label_offset(vd->vdev_psize, l, offset),
181 	    size, buf, ZIO_CHECKSUM_LABEL, done, private,
182 	    ZIO_PRIORITY_SYNC_WRITE,
183 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_DONT_RETRY));
184 }
185 
186 /*
187  * Generate the nvlist representing this vdev's config.
188  */
189 nvlist_t *
190 vdev_config_generate(vdev_t *vd, int getstats)
191 {
192 	nvlist_t *nv = NULL;
193 
194 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) == 0);
195 
196 	VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
197 	    vd->vdev_ops->vdev_op_type) == 0);
198 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0);
199 	VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
200 
201 	if (vd->vdev_path != NULL)
202 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
203 		    vd->vdev_path) == 0);
204 
205 	if (vd->vdev_devid != NULL)
206 		VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
207 		    vd->vdev_devid) == 0);
208 
209 	if (vd->vdev_wholedisk != -1ULL)
210 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
211 		    vd->vdev_wholedisk) == 0);
212 
213 	if (vd == vd->vdev_top) {
214 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
215 		    vd->vdev_ms_array) == 0);
216 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
217 		    vd->vdev_ms_shift) == 0);
218 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
219 		    vd->vdev_ashift) == 0);
220 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
221 		    vd->vdev_asize) == 0);
222 	}
223 
224 	if (vd->vdev_dtl.smo_object != 0)
225 		VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
226 		    vd->vdev_dtl.smo_object) == 0);
227 
228 	if (getstats) {
229 		vdev_stat_t vs;
230 		vdev_get_stats(vd, &vs);
231 		VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS,
232 		    (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
233 	}
234 
235 	if (!vd->vdev_ops->vdev_op_leaf) {
236 		nvlist_t **child;
237 		int c;
238 
239 		child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
240 		    KM_SLEEP);
241 
242 		for (c = 0; c < vd->vdev_children; c++)
243 			child[c] = vdev_config_generate(vd->vdev_child[c],
244 			    getstats);
245 
246 		VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
247 		    child, vd->vdev_children) == 0);
248 
249 		for (c = 0; c < vd->vdev_children; c++)
250 			nvlist_free(child[c]);
251 
252 		kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
253 	}
254 
255 	return (nv);
256 }
257 
258 nvlist_t *
259 vdev_label_read_config(vdev_t *vd)
260 {
261 	nvlist_t *config = NULL;
262 	vdev_phys_t *vp;
263 	uint64_t version;
264 	zio_t *zio;
265 	int l;
266 
267 	if (vdev_is_dead(vd))
268 		return (NULL);
269 
270 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
271 
272 	for (l = 0; l < VDEV_LABELS; l++) {
273 
274 		zio = zio_root(vd->vdev_spa, NULL, NULL,
275 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD);
276 
277 		vdev_label_read(zio, vd, l, vp,
278 		    offsetof(vdev_label_t, vl_vdev_phys),
279 		    sizeof (vdev_phys_t), NULL, NULL);
280 
281 		if (zio_wait(zio) == 0 &&
282 		    nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
283 		    &config, 0) == 0 &&
284 		    nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
285 		    &version) == 0 &&
286 		    version == UBERBLOCK_VERSION)
287 			break;
288 
289 		if (config != NULL) {
290 			nvlist_free(config);
291 			config = NULL;
292 		}
293 	}
294 
295 	zio_buf_free(vp, sizeof (vdev_phys_t));
296 
297 	return (config);
298 }
299 
300 int
301 vdev_label_init(vdev_t *vd, uint64_t crtxg)
302 {
303 	spa_t *spa = vd->vdev_spa;
304 	nvlist_t *label;
305 	vdev_phys_t *vp;
306 	vdev_boot_header_t *vb;
307 	uberblock_phys_t *ubphys;
308 	zio_t *zio;
309 	int l, c, n;
310 	char *buf;
311 	size_t buflen;
312 	int error;
313 
314 	for (c = 0; c < vd->vdev_children; c++)
315 		if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0)
316 			return (error);
317 
318 	if (!vd->vdev_ops->vdev_op_leaf)
319 		return (0);
320 
321 	/*
322 	 * Make sure each leaf device is writable, and zero its initial content.
323 	 * Along the way, also make sure that no leaf is already in use.
324 	 * Note that it's important to do this sequentially, not in parallel,
325 	 * so that we catch cases of multiple use of the same leaf vdev in
326 	 * the vdev we're creating -- e.g. mirroring a disk with itself.
327 	 */
328 	if (vdev_is_dead(vd))
329 		return (EIO);
330 
331 	/*
332 	 * Check whether this device is already in use.
333 	 * Ignore the check if crtxg == 0, which we use for device removal.
334 	 */
335 	if (crtxg != 0 && (label = vdev_label_read_config(vd)) != NULL) {
336 		uint64_t version, state, pool_guid, device_guid, txg;
337 		uint64_t mycrtxg = 0;
338 
339 		(void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
340 		    &mycrtxg);
341 
342 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION,
343 		    &version) == 0 && version == UBERBLOCK_VERSION &&
344 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
345 		    &state) == 0 && state == POOL_STATE_ACTIVE &&
346 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
347 		    &pool_guid) == 0 &&
348 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
349 		    &device_guid) == 0 &&
350 		    spa_guid_exists(pool_guid, device_guid) &&
351 		    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
352 		    &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) {
353 			dprintf("vdev %s in use, pool_state %d\n",
354 			    vdev_description(vd), state);
355 			nvlist_free(label);
356 			return (EBUSY);
357 		}
358 		nvlist_free(label);
359 	}
360 
361 	/*
362 	 * The device isn't in use, so initialize its label.
363 	 */
364 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
365 	bzero(vp, sizeof (vdev_phys_t));
366 
367 	/*
368 	 * Generate a label describing the pool and our top-level vdev.
369 	 * We mark it as being from txg 0 to indicate that it's not
370 	 * really part of an active pool just yet.  The labels will
371 	 * be written again with a meaningful txg by spa_sync().
372 	 */
373 	label = spa_config_generate(spa, vd, 0ULL, 0);
374 
375 	/*
376 	 * Add our creation time.  This allows us to detect multiple vdev
377 	 * uses as described above, and automatically expires if we fail.
378 	 */
379 	VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0);
380 
381 	buf = vp->vp_nvlist;
382 	buflen = sizeof (vp->vp_nvlist);
383 
384 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, 0) != 0) {
385 		nvlist_free(label);
386 		zio_buf_free(vp, sizeof (vdev_phys_t));
387 		return (EINVAL);
388 	}
389 
390 	/*
391 	 * Initialize boot block header.
392 	 */
393 	vb = zio_buf_alloc(sizeof (vdev_boot_header_t));
394 	bzero(vb, sizeof (vdev_boot_header_t));
395 	vb->vb_magic = VDEV_BOOT_MAGIC;
396 	vb->vb_version = VDEV_BOOT_VERSION;
397 	vb->vb_offset = VDEV_BOOT_OFFSET;
398 	vb->vb_size = VDEV_BOOT_SIZE;
399 
400 	/*
401 	 * Initialize uberblock template.
402 	 */
403 	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
404 	bzero(ubphys, sizeof (uberblock_phys_t));
405 	ubphys->ubp_uberblock = spa->spa_uberblock;
406 	ubphys->ubp_uberblock.ub_txg = 0;
407 
408 	/*
409 	 * Write everything in parallel.
410 	 */
411 	zio = zio_root(spa, NULL, NULL,
412 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
413 
414 	for (l = 0; l < VDEV_LABELS; l++) {
415 
416 		vdev_label_write(zio, vd, l, vp,
417 		    offsetof(vdev_label_t, vl_vdev_phys),
418 		    sizeof (vdev_phys_t), NULL, NULL);
419 
420 		vdev_label_write(zio, vd, l, vb,
421 		    offsetof(vdev_label_t, vl_boot_header),
422 		    sizeof (vdev_boot_header_t), NULL, NULL);
423 
424 		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
425 
426 			vdev_label_write(zio, vd, l, ubphys,
427 			    offsetof(vdev_label_t, vl_uberblock[n]),
428 			    sizeof (uberblock_phys_t), NULL, NULL);
429 
430 		}
431 	}
432 
433 	error = zio_wait(zio);
434 
435 	nvlist_free(label);
436 	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
437 	zio_buf_free(vb, sizeof (vdev_boot_header_t));
438 	zio_buf_free(vp, sizeof (vdev_phys_t));
439 
440 	return (error);
441 }
442 
443 /*
444  * ==========================================================================
445  * uberblock load/sync
446  * ==========================================================================
447  */
448 
449 /*
450  * Consider the following situation: txg is safely synced to disk.  We've
451  * written the first uberblock for txg + 1, and then we lose power.  When we
452  * come back up, we fail to see the uberblock for txg + 1 because, say,
453  * it was on a mirrored device and the replica to which we wrote txg + 1
454  * is now offline.  If we then make some changes and sync txg + 1, and then
455  * the missing replica comes back, then for a new seconds we'll have two
456  * conflicting uberblocks on disk with the same txg.  The solution is simple:
457  * among uberblocks with equal txg, choose the one with the latest timestamp.
458  */
459 static int
460 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
461 {
462 	if (ub1->ub_txg < ub2->ub_txg)
463 		return (-1);
464 	if (ub1->ub_txg > ub2->ub_txg)
465 		return (1);
466 
467 	if (ub1->ub_timestamp < ub2->ub_timestamp)
468 		return (-1);
469 	if (ub1->ub_timestamp > ub2->ub_timestamp)
470 		return (1);
471 
472 	return (0);
473 }
474 
475 static void
476 vdev_uberblock_load_done(zio_t *zio)
477 {
478 	uberblock_phys_t *ubphys = zio->io_data;
479 	uberblock_t *ub = &ubphys->ubp_uberblock;
480 	uberblock_t *ubbest = zio->io_private;
481 	spa_t *spa = zio->io_spa;
482 
483 	ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t));
484 
485 	if (uberblock_verify(ub) == 0) {
486 		mutex_enter(&spa->spa_uberblock_lock);
487 		if (vdev_uberblock_compare(ub, ubbest) > 0)
488 			*ubbest = *ub;
489 		mutex_exit(&spa->spa_uberblock_lock);
490 	}
491 
492 	zio_buf_free(zio->io_data, zio->io_size);
493 }
494 
495 void
496 vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
497 {
498 	int l, c, n;
499 
500 	for (c = 0; c < vd->vdev_children; c++)
501 		vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
502 
503 	if (!vd->vdev_ops->vdev_op_leaf)
504 		return;
505 
506 	if (vdev_is_dead(vd))
507 		return;
508 
509 	for (l = 0; l < VDEV_LABELS; l++) {
510 		for (n = 0; n < VDEV_UBERBLOCKS; n++) {
511 			vdev_label_read(zio, vd, l,
512 			    zio_buf_alloc(sizeof (uberblock_phys_t)),
513 			    offsetof(vdev_label_t, vl_uberblock[n]),
514 			    sizeof (uberblock_phys_t),
515 			    vdev_uberblock_load_done, ubbest);
516 		}
517 	}
518 }
519 
520 /*
521  * Write the uberblock to both labels of all leaves of the specified vdev.
522  */
523 static void
524 vdev_uberblock_sync_done(zio_t *zio)
525 {
526 	uint64_t *good_writes = zio->io_root->io_private;
527 
528 	if (zio->io_error == 0)
529 		atomic_add_64(good_writes, 1);
530 }
531 
532 static void
533 vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd,
534 	uint64_t txg)
535 {
536 	int l, c, n;
537 
538 	for (c = 0; c < vd->vdev_children; c++)
539 		vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg);
540 
541 	if (!vd->vdev_ops->vdev_op_leaf)
542 		return;
543 
544 	if (vdev_is_dead(vd))
545 		return;
546 
547 	n = txg & (VDEV_UBERBLOCKS - 1);
548 
549 	ASSERT(ubphys->ubp_uberblock.ub_txg == txg);
550 
551 	for (l = 0; l < VDEV_LABELS; l++)
552 		vdev_label_write(zio, vd, l, ubphys,
553 		    offsetof(vdev_label_t, vl_uberblock[n]),
554 		    sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL);
555 
556 	dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg);
557 }
558 
559 static int
560 vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg)
561 {
562 	uberblock_phys_t *ubphys;
563 	uint64_t *good_writes;
564 	zio_t *zio;
565 	int error;
566 
567 	ubphys = zio_buf_alloc(sizeof (uberblock_phys_t));
568 	bzero(ubphys, sizeof (uberblock_phys_t));
569 	ubphys->ubp_uberblock = *ub;
570 
571 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
572 
573 	zio = zio_root(spa, NULL, good_writes,
574 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
575 
576 	vdev_uberblock_sync(zio, ubphys, uvd, txg);
577 
578 	error = zio_wait(zio);
579 
580 	if (error && *good_writes != 0) {
581 		dprintf("partial success: good_writes = %llu\n", *good_writes);
582 		error = 0;
583 	}
584 
585 	/*
586 	 * It's possible to have no good writes and no error if every vdev is in
587 	 * the CANT_OPEN state.
588 	 */
589 	if (*good_writes == 0 && error == 0)
590 		error = EIO;
591 
592 	kmem_free(good_writes, sizeof (uint64_t));
593 	zio_buf_free(ubphys, sizeof (uberblock_phys_t));
594 
595 	return (error);
596 }
597 
598 /*
599  * Sync out an individual vdev.
600  */
601 static void
602 vdev_sync_label_done(zio_t *zio)
603 {
604 	uint64_t *good_writes = zio->io_root->io_private;
605 
606 	if (zio->io_error == 0)
607 		atomic_add_64(good_writes, 1);
608 }
609 
610 static void
611 vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg)
612 {
613 	nvlist_t *label;
614 	vdev_phys_t *vp;
615 	char *buf;
616 	size_t buflen;
617 	int c;
618 
619 	for (c = 0; c < vd->vdev_children; c++)
620 		vdev_sync_label(zio, vd->vdev_child[c], l, txg);
621 
622 	if (!vd->vdev_ops->vdev_op_leaf)
623 		return;
624 
625 	if (vdev_is_dead(vd))
626 		return;
627 
628 	/*
629 	 * Generate a label describing the top-level config to which we belong.
630 	 */
631 	label = spa_config_generate(vd->vdev_spa, vd, txg, 0);
632 
633 	vp = zio_buf_alloc(sizeof (vdev_phys_t));
634 	bzero(vp, sizeof (vdev_phys_t));
635 
636 	buf = vp->vp_nvlist;
637 	buflen = sizeof (vp->vp_nvlist);
638 
639 	if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, 0) == 0)
640 		vdev_label_write(zio, vd, l, vp,
641 		    offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
642 		    vdev_sync_label_done, NULL);
643 
644 	zio_buf_free(vp, sizeof (vdev_phys_t));
645 	nvlist_free(label);
646 
647 	dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg);
648 }
649 
650 static int
651 vdev_sync_labels(vdev_t *vd, int l, uint64_t txg)
652 {
653 	uint64_t *good_writes;
654 	zio_t *zio;
655 	int error;
656 
657 	ASSERT(vd == vd->vdev_top);
658 
659 	good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
660 
661 	zio = zio_root(vd->vdev_spa, NULL, good_writes,
662 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
663 
664 	/*
665 	 * Recursively kick off writes to all labels.
666 	 */
667 	vdev_sync_label(zio, vd, l, txg);
668 
669 	error = zio_wait(zio);
670 
671 	if (error && *good_writes != 0) {
672 		dprintf("partial success: good_writes = %llu\n", *good_writes);
673 		error = 0;
674 	}
675 
676 	if (*good_writes == 0 && error == 0)
677 		error = ENODEV;
678 
679 	kmem_free(good_writes, sizeof (uint64_t));
680 
681 	return (error);
682 }
683 
684 /*
685  * Sync the entire vdev configuration.
686  *
687  * The order of operations is carefully crafted to ensure that
688  * if the system panics or loses power at any time, the state on disk
689  * is still transactionally consistent.  The in-line comments below
690  * describe the failure semantics at each stage.
691  *
692  * Moreover, it is designed to be idempotent: if spa_sync_labels() fails
693  * at any time, you can just call it again, and it will resume its work.
694  */
695 int
696 spa_sync_labels(spa_t *spa, uint64_t txg)
697 {
698 	uberblock_t *ub = &spa->spa_uberblock;
699 	vdev_t *rvd = spa->spa_root_vdev;
700 	vdev_t *vd, *uvd;
701 	zio_t *zio;
702 	int c, l, error;
703 
704 	ASSERT(ub->ub_txg <= txg);
705 
706 	/*
707 	 * If this isn't a resync due to I/O errors, and nothing changed
708 	 * in this transaction group, and the vdev configuration hasn't changed,
709 	 * and this isn't an explicit sync-all, then there's nothing to do.
710 	 */
711 	if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE &&
712 	    list_is_empty(&spa->spa_dirty_list)) {
713 		dprintf("nothing to sync in %s in txg %llu\n",
714 		    spa_name(spa), txg);
715 		return (0);
716 	}
717 
718 	if (txg > spa_freeze_txg(spa))
719 		return (0);
720 
721 	dprintf("syncing %s txg %llu\n", spa_name(spa), txg);
722 
723 	/*
724 	 * Flush the write cache of every disk that's been written to
725 	 * in this transaction group.  This ensures that all blocks
726 	 * written in this txg will be committed to stable storage
727 	 * before any uberblock that references them.
728 	 */
729 	zio = zio_root(spa, NULL, NULL,
730 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
731 	for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
732 	    vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) {
733 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
734 		    NULL, NULL, ZIO_PRIORITY_NOW,
735 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
736 	}
737 	(void) zio_wait(zio);
738 
739 	/*
740 	 * Sync out the even labels (L0, L2) for every dirty vdev.  If the
741 	 * system dies in the middle of this process, that's OK: all of the
742 	 * even labels that made it to disk will be newer than any uberblock,
743 	 * and will therefore be considered invalid.  The odd labels (L1, L3),
744 	 * which have not yet been touched, will still be valid.
745 	 */
746 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
747 	    vd = list_next(&spa->spa_dirty_list, vd)) {
748 		for (l = 0; l < VDEV_LABELS; l++) {
749 			if (l & 1)
750 				continue;
751 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
752 				return (error);
753 		}
754 	}
755 
756 	/*
757 	 * Flush the new labels to disk.  This ensures that all even-label
758 	 * updates are committed to stable storage before the uberblock update.
759 	 */
760 	zio = zio_root(spa, NULL, NULL,
761 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
762 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
763 	    vd = list_next(&spa->spa_dirty_list, vd)) {
764 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
765 		    NULL, NULL, ZIO_PRIORITY_NOW,
766 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
767 	}
768 	(void) zio_wait(zio);
769 
770 	/*
771 	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
772 	 * Otherwise, pick one top-level vdev at random.
773 	 */
774 	if (!list_is_empty(&spa->spa_dirty_list))
775 		uvd = rvd;
776 	else
777 		uvd = rvd->vdev_child[spa_get_random(rvd->vdev_children)];
778 
779 	/*
780 	 * Sync the uberblocks.  If the system dies in the middle of this
781 	 * step, there are two cases to consider, and the on-disk state
782 	 * is consistent either way:
783 	 *
784 	 * (1)	If none of the new uberblocks made it to disk, then the
785 	 *	previous uberblock will be the newest, and the odd labels
786 	 *	(which had not yet been touched) will be valid with respect
787 	 *	to that uberblock.
788 	 *
789 	 * (2)	If one or more new uberblocks made it to disk, then they
790 	 *	will be the newest, and the even labels (which had all
791 	 *	been successfully committed) will be valid with respect
792 	 *	to the new uberblocks.
793 	 */
794 	if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0)
795 		return (error);
796 
797 	/*
798 	 * Flush the uberblocks to disk.  This ensures that the odd labels
799 	 * are no longer needed (because the new uberblocks and the even
800 	 * labels are safely on disk), so it is safe to overwrite them.
801 	 */
802 	(void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE,
803 	    NULL, NULL, ZIO_PRIORITY_NOW,
804 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
805 
806 	/*
807 	 * Sync out odd labels for every dirty vdev.  If the system dies
808 	 * in the middle of this process, the even labels and the new
809 	 * uberblocks will suffice to open the pool.  The next time
810 	 * the pool is opened, the first thing we'll do -- before any
811 	 * user data is modified -- is mark every vdev dirty so that
812 	 * all labels will be brought up to date.
813 	 */
814 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
815 	    vd = list_next(&spa->spa_dirty_list, vd)) {
816 		for (l = 0; l < VDEV_LABELS; l++) {
817 			if ((l & 1) == 0)
818 				continue;
819 			if ((error = vdev_sync_labels(vd, l, txg)) != 0)
820 				return (error);
821 		}
822 	}
823 
824 	/*
825 	 * Flush the new labels to disk.  This ensures that all odd-label
826 	 * updates are committed to stable storage before the next
827 	 * transaction group begins.
828 	 */
829 	zio = zio_root(spa, NULL, NULL,
830 	    ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL);
831 	for (vd = list_head(&spa->spa_dirty_list); vd != NULL;
832 	    vd = list_next(&spa->spa_dirty_list, vd)) {
833 		zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE,
834 		    NULL, NULL, ZIO_PRIORITY_NOW,
835 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY));
836 	}
837 	(void) zio_wait(zio);
838 
839 	/*
840 	 * Clear the dirty list.
841 	 */
842 	while (!list_is_empty(&spa->spa_dirty_list))
843 		vdev_config_clean(list_head(&spa->spa_dirty_list));
844 
845 #ifdef DEBUG
846 	for (c = 0; c < rvd->vdev_children; c++) {
847 		ASSERT(rvd->vdev_child[c]->vdev_is_dirty == 0);
848 	}
849 #endif
850 
851 	return (0);
852 }
853