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 return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 156 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 157 } 158 159 static void 160 vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 161 uint64_t size, zio_done_func_t *done, void *private) 162 { 163 ASSERT(vd->vdev_children == 0); 164 165 zio_nowait(zio_read_phys(zio, vd, 166 vdev_label_offset(vd->vdev_psize, l, offset), 167 size, buf, ZIO_CHECKSUM_LABEL, done, private, 168 ZIO_PRIORITY_SYNC_READ, 169 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE)); 170 } 171 172 static void 173 vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 174 uint64_t size, zio_done_func_t *done, void *private) 175 { 176 ASSERT(vd->vdev_children == 0); 177 178 zio_nowait(zio_write_phys(zio, vd, 179 vdev_label_offset(vd->vdev_psize, l, offset), 180 size, buf, ZIO_CHECKSUM_LABEL, done, private, 181 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL)); 182 } 183 184 /* 185 * Generate the nvlist representing this vdev's config. 186 */ 187 nvlist_t * 188 vdev_config_generate(vdev_t *vd, int getstats) 189 { 190 nvlist_t *nv = NULL; 191 192 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 193 194 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 195 vd->vdev_ops->vdev_op_type) == 0); 196 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0); 197 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 198 199 if (vd->vdev_path != NULL) 200 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 201 vd->vdev_path) == 0); 202 203 if (vd->vdev_devid != NULL) 204 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 205 vd->vdev_devid) == 0); 206 207 if (vd->vdev_wholedisk != -1ULL) 208 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 209 vd->vdev_wholedisk) == 0); 210 211 if (vd->vdev_not_present) 212 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 213 214 if (vd == vd->vdev_top) { 215 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 216 vd->vdev_ms_array) == 0); 217 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 218 vd->vdev_ms_shift) == 0); 219 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 220 vd->vdev_ashift) == 0); 221 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 222 vd->vdev_asize) == 0); 223 } 224 225 if (vd->vdev_dtl.smo_object != 0) 226 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 227 vd->vdev_dtl.smo_object) == 0); 228 229 if (getstats) { 230 vdev_stat_t vs; 231 vdev_get_stats(vd, &vs); 232 VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 233 (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 234 } 235 236 if (!vd->vdev_ops->vdev_op_leaf) { 237 nvlist_t **child; 238 int c; 239 240 child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 241 KM_SLEEP); 242 243 for (c = 0; c < vd->vdev_children; c++) 244 child[c] = vdev_config_generate(vd->vdev_child[c], 245 getstats); 246 247 VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 248 child, vd->vdev_children) == 0); 249 250 for (c = 0; c < vd->vdev_children; c++) 251 nvlist_free(child[c]); 252 253 kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 254 255 } else { 256 if (!vd->vdev_tmpoffline) { 257 if (vd->vdev_offline) 258 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 259 B_TRUE) == 0); 260 else 261 (void) nvlist_remove(nv, ZPOOL_CONFIG_OFFLINE, 262 DATA_TYPE_UINT64); 263 } 264 } 265 266 return (nv); 267 } 268 269 nvlist_t * 270 vdev_label_read_config(vdev_t *vd) 271 { 272 nvlist_t *config = NULL; 273 vdev_phys_t *vp; 274 zio_t *zio; 275 int l; 276 277 if (vdev_is_dead(vd)) 278 return (NULL); 279 280 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 281 282 for (l = 0; l < VDEV_LABELS; l++) { 283 284 zio = zio_root(vd->vdev_spa, NULL, NULL, ZIO_FLAG_CANFAIL | 285 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD); 286 287 vdev_label_read(zio, vd, l, vp, 288 offsetof(vdev_label_t, vl_vdev_phys), 289 sizeof (vdev_phys_t), NULL, NULL); 290 291 if (zio_wait(zio) == 0 && 292 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 293 &config, 0) == 0) 294 break; 295 296 if (config != NULL) { 297 nvlist_free(config); 298 config = NULL; 299 } 300 } 301 302 zio_buf_free(vp, sizeof (vdev_phys_t)); 303 304 return (config); 305 } 306 307 int 308 vdev_label_init(vdev_t *vd, uint64_t crtxg) 309 { 310 spa_t *spa = vd->vdev_spa; 311 nvlist_t *label; 312 vdev_phys_t *vp; 313 vdev_boot_header_t *vb; 314 uberblock_phys_t *ubphys; 315 zio_t *zio; 316 int l, c, n; 317 char *buf; 318 size_t buflen; 319 int error; 320 321 for (c = 0; c < vd->vdev_children; c++) 322 if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0) 323 return (error); 324 325 if (!vd->vdev_ops->vdev_op_leaf) 326 return (0); 327 328 /* 329 * Make sure each leaf device is writable, and zero its initial content. 330 * Along the way, also make sure that no leaf is already in use. 331 * Note that it's important to do this sequentially, not in parallel, 332 * so that we catch cases of multiple use of the same leaf vdev in 333 * the vdev we're creating -- e.g. mirroring a disk with itself. 334 */ 335 if (vdev_is_dead(vd)) 336 return (EIO); 337 338 /* 339 * Check whether this device is already in use. 340 * Ignore the check if crtxg == 0, which we use for device removal. 341 */ 342 if (crtxg != 0 && 343 (label = vdev_label_read_config(vd)) != NULL) { 344 uint64_t state, pool_guid, device_guid, txg; 345 uint64_t mycrtxg = 0; 346 347 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 348 &mycrtxg); 349 350 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 351 &state) == 0 && state == POOL_STATE_ACTIVE && 352 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 353 &pool_guid) == 0 && 354 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 355 &device_guid) == 0 && 356 spa_guid_exists(pool_guid, device_guid) && 357 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 358 &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) { 359 dprintf("vdev %s in use, pool_state %d\n", 360 vdev_description(vd), state); 361 nvlist_free(label); 362 return (EBUSY); 363 } 364 nvlist_free(label); 365 } 366 367 /* 368 * The device isn't in use, so initialize its label. 369 */ 370 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 371 bzero(vp, sizeof (vdev_phys_t)); 372 373 /* 374 * Generate a label describing the pool and our top-level vdev. 375 * We mark it as being from txg 0 to indicate that it's not 376 * really part of an active pool just yet. The labels will 377 * be written again with a meaningful txg by spa_sync(). 378 */ 379 label = spa_config_generate(spa, vd, 0ULL, 0); 380 381 /* 382 * Add our creation time. This allows us to detect multiple vdev 383 * uses as described above, and automatically expires if we fail. 384 */ 385 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0); 386 387 buf = vp->vp_nvlist; 388 buflen = sizeof (vp->vp_nvlist); 389 390 if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) != 0) { 391 nvlist_free(label); 392 zio_buf_free(vp, sizeof (vdev_phys_t)); 393 return (EINVAL); 394 } 395 396 /* 397 * Initialize boot block header. 398 */ 399 vb = zio_buf_alloc(sizeof (vdev_boot_header_t)); 400 bzero(vb, sizeof (vdev_boot_header_t)); 401 vb->vb_magic = VDEV_BOOT_MAGIC; 402 vb->vb_version = VDEV_BOOT_VERSION; 403 vb->vb_offset = VDEV_BOOT_OFFSET; 404 vb->vb_size = VDEV_BOOT_SIZE; 405 406 /* 407 * Initialize uberblock template. 408 */ 409 ubphys = zio_buf_alloc(sizeof (uberblock_phys_t)); 410 bzero(ubphys, sizeof (uberblock_phys_t)); 411 ubphys->ubp_uberblock = spa->spa_uberblock; 412 ubphys->ubp_uberblock.ub_txg = 0; 413 414 /* 415 * Write everything in parallel. 416 */ 417 zio = zio_root(spa, NULL, NULL, 418 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 419 420 for (l = 0; l < VDEV_LABELS; l++) { 421 422 vdev_label_write(zio, vd, l, vp, 423 offsetof(vdev_label_t, vl_vdev_phys), 424 sizeof (vdev_phys_t), NULL, NULL); 425 426 vdev_label_write(zio, vd, l, vb, 427 offsetof(vdev_label_t, vl_boot_header), 428 sizeof (vdev_boot_header_t), NULL, NULL); 429 430 for (n = 0; n < VDEV_UBERBLOCKS; n++) { 431 432 vdev_label_write(zio, vd, l, ubphys, 433 offsetof(vdev_label_t, vl_uberblock[n]), 434 sizeof (uberblock_phys_t), NULL, NULL); 435 436 } 437 } 438 439 error = zio_wait(zio); 440 441 nvlist_free(label); 442 zio_buf_free(ubphys, sizeof (uberblock_phys_t)); 443 zio_buf_free(vb, sizeof (vdev_boot_header_t)); 444 zio_buf_free(vp, sizeof (vdev_phys_t)); 445 446 return (error); 447 } 448 449 /* 450 * ========================================================================== 451 * uberblock load/sync 452 * ========================================================================== 453 */ 454 455 /* 456 * Consider the following situation: txg is safely synced to disk. We've 457 * written the first uberblock for txg + 1, and then we lose power. When we 458 * come back up, we fail to see the uberblock for txg + 1 because, say, 459 * it was on a mirrored device and the replica to which we wrote txg + 1 460 * is now offline. If we then make some changes and sync txg + 1, and then 461 * the missing replica comes back, then for a new seconds we'll have two 462 * conflicting uberblocks on disk with the same txg. The solution is simple: 463 * among uberblocks with equal txg, choose the one with the latest timestamp. 464 */ 465 static int 466 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 467 { 468 if (ub1->ub_txg < ub2->ub_txg) 469 return (-1); 470 if (ub1->ub_txg > ub2->ub_txg) 471 return (1); 472 473 if (ub1->ub_timestamp < ub2->ub_timestamp) 474 return (-1); 475 if (ub1->ub_timestamp > ub2->ub_timestamp) 476 return (1); 477 478 return (0); 479 } 480 481 static void 482 vdev_uberblock_load_done(zio_t *zio) 483 { 484 uberblock_phys_t *ubphys = zio->io_data; 485 uberblock_t *ub = &ubphys->ubp_uberblock; 486 uberblock_t *ubbest = zio->io_private; 487 spa_t *spa = zio->io_spa; 488 489 ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t)); 490 491 if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 492 mutex_enter(&spa->spa_uberblock_lock); 493 if (vdev_uberblock_compare(ub, ubbest) > 0) 494 *ubbest = *ub; 495 mutex_exit(&spa->spa_uberblock_lock); 496 } 497 498 zio_buf_free(zio->io_data, zio->io_size); 499 } 500 501 void 502 vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 503 { 504 int l, c, n; 505 506 for (c = 0; c < vd->vdev_children; c++) 507 vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 508 509 if (!vd->vdev_ops->vdev_op_leaf) 510 return; 511 512 if (vdev_is_dead(vd)) 513 return; 514 515 for (l = 0; l < VDEV_LABELS; l++) { 516 for (n = 0; n < VDEV_UBERBLOCKS; n++) { 517 vdev_label_read(zio, vd, l, 518 zio_buf_alloc(sizeof (uberblock_phys_t)), 519 offsetof(vdev_label_t, vl_uberblock[n]), 520 sizeof (uberblock_phys_t), 521 vdev_uberblock_load_done, ubbest); 522 } 523 } 524 } 525 526 /* 527 * Write the uberblock to both labels of all leaves of the specified vdev. 528 */ 529 static void 530 vdev_uberblock_sync_done(zio_t *zio) 531 { 532 uint64_t *good_writes = zio->io_root->io_private; 533 534 if (zio->io_error == 0) 535 atomic_add_64(good_writes, 1); 536 } 537 538 static void 539 vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd, 540 uint64_t txg) 541 { 542 int l, c, n; 543 544 for (c = 0; c < vd->vdev_children; c++) 545 vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg); 546 547 if (!vd->vdev_ops->vdev_op_leaf) 548 return; 549 550 if (vdev_is_dead(vd)) 551 return; 552 553 n = txg & (VDEV_UBERBLOCKS - 1); 554 555 ASSERT(ubphys->ubp_uberblock.ub_txg == txg); 556 557 for (l = 0; l < VDEV_LABELS; l++) 558 vdev_label_write(zio, vd, l, ubphys, 559 offsetof(vdev_label_t, vl_uberblock[n]), 560 sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL); 561 562 dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg); 563 } 564 565 static int 566 vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg) 567 { 568 uberblock_phys_t *ubphys; 569 uint64_t *good_writes; 570 zio_t *zio; 571 int error; 572 573 ubphys = zio_buf_alloc(sizeof (uberblock_phys_t)); 574 bzero(ubphys, sizeof (uberblock_phys_t)); 575 ubphys->ubp_uberblock = *ub; 576 577 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 578 579 zio = zio_root(spa, NULL, good_writes, 580 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 581 582 vdev_uberblock_sync(zio, ubphys, uvd, txg); 583 584 error = zio_wait(zio); 585 586 if (error && *good_writes != 0) { 587 dprintf("partial success: good_writes = %llu\n", *good_writes); 588 error = 0; 589 } 590 591 /* 592 * It's possible to have no good writes and no error if every vdev is in 593 * the CANT_OPEN state. 594 */ 595 if (*good_writes == 0 && error == 0) 596 error = EIO; 597 598 kmem_free(good_writes, sizeof (uint64_t)); 599 zio_buf_free(ubphys, sizeof (uberblock_phys_t)); 600 601 return (error); 602 } 603 604 /* 605 * Sync out an individual vdev. 606 */ 607 static void 608 vdev_sync_label_done(zio_t *zio) 609 { 610 uint64_t *good_writes = zio->io_root->io_private; 611 612 if (zio->io_error == 0) 613 atomic_add_64(good_writes, 1); 614 } 615 616 static void 617 vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg) 618 { 619 nvlist_t *label; 620 vdev_phys_t *vp; 621 char *buf; 622 size_t buflen; 623 int c; 624 625 for (c = 0; c < vd->vdev_children; c++) 626 vdev_sync_label(zio, vd->vdev_child[c], l, txg); 627 628 if (!vd->vdev_ops->vdev_op_leaf) 629 return; 630 631 if (vdev_is_dead(vd)) 632 return; 633 634 /* 635 * Generate a label describing the top-level config to which we belong. 636 */ 637 label = spa_config_generate(vd->vdev_spa, vd, txg, 0); 638 639 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 640 bzero(vp, sizeof (vdev_phys_t)); 641 642 buf = vp->vp_nvlist; 643 buflen = sizeof (vp->vp_nvlist); 644 645 if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) 646 vdev_label_write(zio, vd, l, vp, 647 offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t), 648 vdev_sync_label_done, NULL); 649 650 zio_buf_free(vp, sizeof (vdev_phys_t)); 651 nvlist_free(label); 652 653 dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg); 654 } 655 656 static int 657 vdev_sync_labels(vdev_t *vd, int l, uint64_t txg) 658 { 659 uint64_t *good_writes; 660 zio_t *zio; 661 int error; 662 663 ASSERT(vd == vd->vdev_top); 664 665 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 666 667 zio = zio_root(vd->vdev_spa, NULL, good_writes, 668 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 669 670 /* 671 * Recursively kick off writes to all labels. 672 */ 673 vdev_sync_label(zio, vd, l, txg); 674 675 error = zio_wait(zio); 676 677 if (error && *good_writes != 0) { 678 dprintf("partial success: good_writes = %llu\n", *good_writes); 679 error = 0; 680 } 681 682 if (*good_writes == 0 && error == 0) 683 error = ENODEV; 684 685 kmem_free(good_writes, sizeof (uint64_t)); 686 687 return (error); 688 } 689 690 /* 691 * Sync the entire vdev configuration. 692 * 693 * The order of operations is carefully crafted to ensure that 694 * if the system panics or loses power at any time, the state on disk 695 * is still transactionally consistent. The in-line comments below 696 * describe the failure semantics at each stage. 697 * 698 * Moreover, it is designed to be idempotent: if spa_sync_labels() fails 699 * at any time, you can just call it again, and it will resume its work. 700 */ 701 int 702 spa_sync_labels(spa_t *spa, uint64_t txg) 703 { 704 uberblock_t *ub = &spa->spa_uberblock; 705 vdev_t *rvd = spa->spa_root_vdev; 706 vdev_t *vd, *uvd; 707 zio_t *zio; 708 int c, l, error; 709 710 ASSERT(ub->ub_txg <= txg); 711 712 /* 713 * If this isn't a resync due to I/O errors, and nothing changed 714 * in this transaction group, and the vdev configuration hasn't changed, 715 * and this isn't an explicit sync-all, then there's nothing to do. 716 */ 717 if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE && 718 list_is_empty(&spa->spa_dirty_list)) { 719 dprintf("nothing to sync in %s in txg %llu\n", 720 spa_name(spa), txg); 721 return (0); 722 } 723 724 if (txg > spa_freeze_txg(spa)) 725 return (0); 726 727 dprintf("syncing %s txg %llu\n", spa_name(spa), txg); 728 729 /* 730 * Flush the write cache of every disk that's been written to 731 * in this transaction group. This ensures that all blocks 732 * written in this txg will be committed to stable storage 733 * before any uberblock that references them. 734 */ 735 zio = zio_root(spa, NULL, NULL, 736 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 737 for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 738 vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) { 739 zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 740 NULL, NULL, ZIO_PRIORITY_NOW, 741 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 742 } 743 (void) zio_wait(zio); 744 745 /* 746 * Sync out the even labels (L0, L2) for every dirty vdev. If the 747 * system dies in the middle of this process, that's OK: all of the 748 * even labels that made it to disk will be newer than any uberblock, 749 * and will therefore be considered invalid. The odd labels (L1, L3), 750 * which have not yet been touched, will still be valid. 751 */ 752 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 753 vd = list_next(&spa->spa_dirty_list, vd)) { 754 for (l = 0; l < VDEV_LABELS; l++) { 755 if (l & 1) 756 continue; 757 if ((error = vdev_sync_labels(vd, l, txg)) != 0) 758 return (error); 759 } 760 } 761 762 /* 763 * Flush the new labels to disk. This ensures that all even-label 764 * updates are committed to stable storage before the uberblock update. 765 */ 766 zio = zio_root(spa, NULL, NULL, 767 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 768 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 769 vd = list_next(&spa->spa_dirty_list, vd)) { 770 zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 771 NULL, NULL, ZIO_PRIORITY_NOW, 772 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 773 } 774 (void) zio_wait(zio); 775 776 /* 777 * If there are any dirty vdevs, sync the uberblock to all vdevs. 778 * Otherwise, pick a random top-level vdev that's known to be 779 * visible in the config cache (see spa_vdev_add() for details). 780 */ 781 if (!list_is_empty(&spa->spa_dirty_list)) { 782 uvd = rvd; 783 } else { 784 do { 785 uvd = 786 rvd->vdev_child[spa_get_random(rvd->vdev_children)]; 787 } while (uvd->vdev_ms_array == 0); 788 } 789 790 /* 791 * Sync the uberblocks. If the system dies in the middle of this 792 * step, there are two cases to consider, and the on-disk state 793 * is consistent either way: 794 * 795 * (1) If none of the new uberblocks made it to disk, then the 796 * previous uberblock will be the newest, and the odd labels 797 * (which had not yet been touched) will be valid with respect 798 * to that uberblock. 799 * 800 * (2) If one or more new uberblocks made it to disk, then they 801 * will be the newest, and the even labels (which had all 802 * been successfully committed) will be valid with respect 803 * to the new uberblocks. 804 */ 805 if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0) 806 return (error); 807 808 /* 809 * Flush the uberblocks to disk. This ensures that the odd labels 810 * are no longer needed (because the new uberblocks and the even 811 * labels are safely on disk), so it is safe to overwrite them. 812 */ 813 (void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE, 814 NULL, NULL, ZIO_PRIORITY_NOW, 815 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 816 817 /* 818 * Sync out odd labels for every dirty vdev. If the system dies 819 * in the middle of this process, the even labels and the new 820 * uberblocks will suffice to open the pool. The next time 821 * the pool is opened, the first thing we'll do -- before any 822 * user data is modified -- is mark every vdev dirty so that 823 * all labels will be brought up to date. 824 */ 825 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 826 vd = list_next(&spa->spa_dirty_list, vd)) { 827 for (l = 0; l < VDEV_LABELS; l++) { 828 if ((l & 1) == 0) 829 continue; 830 if ((error = vdev_sync_labels(vd, l, txg)) != 0) 831 return (error); 832 } 833 } 834 835 /* 836 * Flush the new labels to disk. This ensures that all odd-label 837 * updates are committed to stable storage before the next 838 * transaction group begins. 839 */ 840 zio = zio_root(spa, NULL, NULL, 841 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 842 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 843 vd = list_next(&spa->spa_dirty_list, vd)) { 844 zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 845 NULL, NULL, ZIO_PRIORITY_NOW, 846 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 847 } 848 (void) zio_wait(zio); 849 850 /* 851 * Clear the dirty list. 852 */ 853 while (!list_is_empty(&spa->spa_dirty_list)) 854 vdev_config_clean(list_head(&spa->spa_dirty_list)); 855 856 #ifdef DEBUG 857 for (c = 0; c < rvd->vdev_children; c++) { 858 ASSERT(rvd->vdev_child[c]->vdev_is_dirty == 0); 859 } 860 #endif 861 862 return (0); 863 } 864