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