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 2007 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 transaction 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 ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0); 157 158 return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 159 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 160 } 161 162 static void 163 vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 164 uint64_t size, zio_done_func_t *done, void *private) 165 { 166 ASSERT(vd->vdev_children == 0); 167 168 zio_nowait(zio_read_phys(zio, vd, 169 vdev_label_offset(vd->vdev_psize, l, offset), 170 size, buf, ZIO_CHECKSUM_LABEL, done, private, 171 ZIO_PRIORITY_SYNC_READ, 172 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE)); 173 } 174 175 static void 176 vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 177 uint64_t size, zio_done_func_t *done, void *private) 178 { 179 ASSERT(vd->vdev_children == 0); 180 181 zio_nowait(zio_write_phys(zio, vd, 182 vdev_label_offset(vd->vdev_psize, l, offset), 183 size, buf, ZIO_CHECKSUM_LABEL, done, private, 184 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL)); 185 } 186 187 /* 188 * Generate the nvlist representing this vdev's config. 189 */ 190 nvlist_t * 191 vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 192 boolean_t isspare) 193 { 194 nvlist_t *nv = NULL; 195 196 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 197 198 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 199 vd->vdev_ops->vdev_op_type) == 0); 200 if (!isspare) 201 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) 202 == 0); 203 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 204 205 if (vd->vdev_path != NULL) 206 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 207 vd->vdev_path) == 0); 208 209 if (vd->vdev_devid != NULL) 210 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 211 vd->vdev_devid) == 0); 212 213 if (vd->vdev_physpath != NULL) 214 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 215 vd->vdev_physpath) == 0); 216 217 if (vd->vdev_nparity != 0) { 218 ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 219 VDEV_TYPE_RAIDZ) == 0); 220 221 /* 222 * Make sure someone hasn't managed to sneak a fancy new vdev 223 * into a crufty old storage pool. 224 */ 225 ASSERT(vd->vdev_nparity == 1 || 226 (vd->vdev_nparity == 2 && 227 spa_version(spa) >= SPA_VERSION_RAID6)); 228 229 /* 230 * Note that we'll add the nparity tag even on storage pools 231 * that only support a single parity device -- older software 232 * will just ignore it. 233 */ 234 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, 235 vd->vdev_nparity) == 0); 236 } 237 238 if (vd->vdev_wholedisk != -1ULL) 239 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 240 vd->vdev_wholedisk) == 0); 241 242 if (vd->vdev_not_present) 243 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 244 245 if (vd->vdev_isspare) 246 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0); 247 248 if (!isspare && vd == vd->vdev_top) { 249 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 250 vd->vdev_ms_array) == 0); 251 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 252 vd->vdev_ms_shift) == 0); 253 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 254 vd->vdev_ashift) == 0); 255 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 256 vd->vdev_asize) == 0); 257 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, 258 vd->vdev_islog) == 0); 259 } 260 261 if (vd->vdev_dtl.smo_object != 0) 262 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 263 vd->vdev_dtl.smo_object) == 0); 264 265 if (getstats) { 266 vdev_stat_t vs; 267 vdev_get_stats(vd, &vs); 268 VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 269 (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 270 } 271 272 if (!vd->vdev_ops->vdev_op_leaf) { 273 nvlist_t **child; 274 int c; 275 276 child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 277 KM_SLEEP); 278 279 for (c = 0; c < vd->vdev_children; c++) 280 child[c] = vdev_config_generate(spa, vd->vdev_child[c], 281 getstats, isspare); 282 283 VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 284 child, vd->vdev_children) == 0); 285 286 for (c = 0; c < vd->vdev_children; c++) 287 nvlist_free(child[c]); 288 289 kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 290 291 } else { 292 if (vd->vdev_offline && !vd->vdev_tmpoffline) 293 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 294 B_TRUE) == 0); 295 if (vd->vdev_faulted) 296 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, 297 B_TRUE) == 0); 298 if (vd->vdev_degraded) 299 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, 300 B_TRUE) == 0); 301 if (vd->vdev_removed) 302 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, 303 B_TRUE) == 0); 304 if (vd->vdev_unspare) 305 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, 306 B_TRUE) == 0); 307 } 308 309 return (nv); 310 } 311 312 nvlist_t * 313 vdev_label_read_config(vdev_t *vd) 314 { 315 spa_t *spa = vd->vdev_spa; 316 nvlist_t *config = NULL; 317 vdev_phys_t *vp; 318 zio_t *zio; 319 int l; 320 321 ASSERT(spa_config_held(spa, RW_READER)); 322 323 if (vdev_is_dead(vd)) 324 return (NULL); 325 326 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 327 328 for (l = 0; l < VDEV_LABELS; l++) { 329 330 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL | 331 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD); 332 333 vdev_label_read(zio, vd, l, vp, 334 offsetof(vdev_label_t, vl_vdev_phys), 335 sizeof (vdev_phys_t), NULL, NULL); 336 337 if (zio_wait(zio) == 0 && 338 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 339 &config, 0) == 0) 340 break; 341 342 if (config != NULL) { 343 nvlist_free(config); 344 config = NULL; 345 } 346 } 347 348 zio_buf_free(vp, sizeof (vdev_phys_t)); 349 350 return (config); 351 } 352 353 /* 354 * Determine if a device is in use. The 'spare_guid' parameter will be filled 355 * in with the device guid if this spare is active elsewhere on the system. 356 */ 357 static boolean_t 358 vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 359 uint64_t *spare_guid) 360 { 361 spa_t *spa = vd->vdev_spa; 362 uint64_t state, pool_guid, device_guid, txg, spare_pool; 363 uint64_t vdtxg = 0; 364 nvlist_t *label; 365 366 if (spare_guid) 367 *spare_guid = 0ULL; 368 369 /* 370 * Read the label, if any, and perform some basic sanity checks. 371 */ 372 if ((label = vdev_label_read_config(vd)) == NULL) 373 return (B_FALSE); 374 375 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 376 &vdtxg); 377 378 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 379 &state) != 0 || 380 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 381 &device_guid) != 0) { 382 nvlist_free(label); 383 return (B_FALSE); 384 } 385 386 if (state != POOL_STATE_SPARE && 387 (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 388 &pool_guid) != 0 || 389 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 390 &txg) != 0)) { 391 nvlist_free(label); 392 return (B_FALSE); 393 } 394 395 nvlist_free(label); 396 397 /* 398 * Check to see if this device indeed belongs to the pool it claims to 399 * be a part of. The only way this is allowed is if the device is a hot 400 * spare (which we check for later on). 401 */ 402 if (state != POOL_STATE_SPARE && 403 !spa_guid_exists(pool_guid, device_guid) && 404 !spa_spare_exists(device_guid, NULL)) 405 return (B_FALSE); 406 407 /* 408 * If the transaction group is zero, then this an initialized (but 409 * unused) label. This is only an error if the create transaction 410 * on-disk is the same as the one we're using now, in which case the 411 * user has attempted to add the same vdev multiple times in the same 412 * transaction. 413 */ 414 if (state != POOL_STATE_SPARE && txg == 0 && vdtxg == crtxg) 415 return (B_TRUE); 416 417 /* 418 * Check to see if this is a spare device. We do an explicit check for 419 * spa_has_spare() here because it may be on our pending list of spares 420 * to add. 421 */ 422 if (spa_spare_exists(device_guid, &spare_pool) || 423 spa_has_spare(spa, device_guid)) { 424 if (spare_guid) 425 *spare_guid = device_guid; 426 427 switch (reason) { 428 case VDEV_LABEL_CREATE: 429 return (B_TRUE); 430 431 case VDEV_LABEL_REPLACE: 432 return (!spa_has_spare(spa, device_guid) || 433 spare_pool != 0ULL); 434 435 case VDEV_LABEL_SPARE: 436 return (spa_has_spare(spa, device_guid)); 437 } 438 } 439 440 /* 441 * If the device is marked ACTIVE, then this device is in use by another 442 * pool on the system. 443 */ 444 return (state == POOL_STATE_ACTIVE); 445 } 446 447 /* 448 * Initialize a vdev label. We check to make sure each leaf device is not in 449 * use, and writable. We put down an initial label which we will later 450 * overwrite with a complete label. Note that it's important to do this 451 * sequentially, not in parallel, so that we catch cases of multiple use of the 452 * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 453 * itself. 454 */ 455 int 456 vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 457 { 458 spa_t *spa = vd->vdev_spa; 459 nvlist_t *label; 460 vdev_phys_t *vp; 461 vdev_boot_header_t *vb; 462 uberblock_t *ub; 463 zio_t *zio; 464 int l, c, n; 465 char *buf; 466 size_t buflen; 467 int error; 468 uint64_t spare_guid; 469 470 ASSERT(spa_config_held(spa, RW_WRITER)); 471 472 for (c = 0; c < vd->vdev_children; c++) 473 if ((error = vdev_label_init(vd->vdev_child[c], 474 crtxg, reason)) != 0) 475 return (error); 476 477 if (!vd->vdev_ops->vdev_op_leaf) 478 return (0); 479 480 /* 481 * Dead vdevs cannot be initialized. 482 */ 483 if (vdev_is_dead(vd)) 484 return (EIO); 485 486 /* 487 * Determine if the vdev is in use. 488 */ 489 if (reason != VDEV_LABEL_REMOVE && 490 vdev_inuse(vd, crtxg, reason, &spare_guid)) 491 return (EBUSY); 492 493 ASSERT(reason != VDEV_LABEL_REMOVE || 494 vdev_inuse(vd, crtxg, reason, NULL)); 495 496 /* 497 * If this is a request to add or replace a spare that is in use 498 * elsewhere on the system, then we must update the guid (which was 499 * initialized to a random value) to reflect the actual GUID (which is 500 * shared between multiple pools). 501 */ 502 if (reason != VDEV_LABEL_REMOVE && spare_guid != 0ULL) { 503 vdev_t *pvd = vd->vdev_parent; 504 505 for (; pvd != NULL; pvd = pvd->vdev_parent) { 506 pvd->vdev_guid_sum -= vd->vdev_guid; 507 pvd->vdev_guid_sum += spare_guid; 508 } 509 510 vd->vdev_guid = vd->vdev_guid_sum = spare_guid; 511 512 /* 513 * If this is a replacement, then we want to fallthrough to the 514 * rest of the code. If we're adding a spare, then it's already 515 * labeled appropriately and we can just return. 516 */ 517 if (reason == VDEV_LABEL_SPARE) 518 return (0); 519 ASSERT(reason == VDEV_LABEL_REPLACE); 520 } 521 522 /* 523 * Initialize its label. 524 */ 525 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 526 bzero(vp, sizeof (vdev_phys_t)); 527 528 /* 529 * Generate a label describing the pool and our top-level vdev. 530 * We mark it as being from txg 0 to indicate that it's not 531 * really part of an active pool just yet. The labels will 532 * be written again with a meaningful txg by spa_sync(). 533 */ 534 if (reason == VDEV_LABEL_SPARE || 535 (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 536 /* 537 * For inactive hot spares, we generate a special label that 538 * identifies as a mutually shared hot spare. We write the 539 * label if we are adding a hot spare, or if we are removing an 540 * active hot spare (in which case we want to revert the 541 * labels). 542 */ 543 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 544 545 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 546 spa_version(spa)) == 0); 547 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 548 POOL_STATE_SPARE) == 0); 549 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 550 vd->vdev_guid) == 0); 551 } else { 552 label = spa_config_generate(spa, vd, 0ULL, B_FALSE); 553 554 /* 555 * Add our creation time. This allows us to detect multiple 556 * vdev uses as described above, and automatically expires if we 557 * fail. 558 */ 559 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 560 crtxg) == 0); 561 } 562 563 buf = vp->vp_nvlist; 564 buflen = sizeof (vp->vp_nvlist); 565 566 error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 567 if (error != 0) { 568 nvlist_free(label); 569 zio_buf_free(vp, sizeof (vdev_phys_t)); 570 /* EFAULT means nvlist_pack ran out of room */ 571 return (error == EFAULT ? ENAMETOOLONG : EINVAL); 572 } 573 574 /* 575 * Initialize boot block header. 576 */ 577 vb = zio_buf_alloc(sizeof (vdev_boot_header_t)); 578 bzero(vb, sizeof (vdev_boot_header_t)); 579 vb->vb_magic = VDEV_BOOT_MAGIC; 580 vb->vb_version = VDEV_BOOT_VERSION; 581 vb->vb_offset = VDEV_BOOT_OFFSET; 582 vb->vb_size = VDEV_BOOT_SIZE; 583 584 /* 585 * Initialize uberblock template. 586 */ 587 ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 588 bzero(ub, VDEV_UBERBLOCK_SIZE(vd)); 589 *ub = spa->spa_uberblock; 590 ub->ub_txg = 0; 591 592 /* 593 * Write everything in parallel. 594 */ 595 zio = zio_root(spa, NULL, NULL, 596 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 597 598 for (l = 0; l < VDEV_LABELS; l++) { 599 600 vdev_label_write(zio, vd, l, vp, 601 offsetof(vdev_label_t, vl_vdev_phys), 602 sizeof (vdev_phys_t), NULL, NULL); 603 604 vdev_label_write(zio, vd, l, vb, 605 offsetof(vdev_label_t, vl_boot_header), 606 sizeof (vdev_boot_header_t), NULL, NULL); 607 608 for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 609 vdev_label_write(zio, vd, l, ub, 610 VDEV_UBERBLOCK_OFFSET(vd, n), 611 VDEV_UBERBLOCK_SIZE(vd), NULL, NULL); 612 } 613 } 614 615 error = zio_wait(zio); 616 617 nvlist_free(label); 618 zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd)); 619 zio_buf_free(vb, sizeof (vdev_boot_header_t)); 620 zio_buf_free(vp, sizeof (vdev_phys_t)); 621 622 /* 623 * If this vdev hasn't been previously identified as a spare, then we 624 * mark it as such only if a) we are labeling it as a spare, or b) it 625 * exists as a spare elsewhere in the system. 626 */ 627 if (error == 0 && !vd->vdev_isspare && 628 (reason == VDEV_LABEL_SPARE || 629 spa_spare_exists(vd->vdev_guid, NULL))) 630 spa_spare_add(vd); 631 632 return (error); 633 } 634 635 /* 636 * ========================================================================== 637 * uberblock load/sync 638 * ========================================================================== 639 */ 640 641 /* 642 * Consider the following situation: txg is safely synced to disk. We've 643 * written the first uberblock for txg + 1, and then we lose power. When we 644 * come back up, we fail to see the uberblock for txg + 1 because, say, 645 * it was on a mirrored device and the replica to which we wrote txg + 1 646 * is now offline. If we then make some changes and sync txg + 1, and then 647 * the missing replica comes back, then for a new seconds we'll have two 648 * conflicting uberblocks on disk with the same txg. The solution is simple: 649 * among uberblocks with equal txg, choose the one with the latest timestamp. 650 */ 651 static int 652 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 653 { 654 if (ub1->ub_txg < ub2->ub_txg) 655 return (-1); 656 if (ub1->ub_txg > ub2->ub_txg) 657 return (1); 658 659 if (ub1->ub_timestamp < ub2->ub_timestamp) 660 return (-1); 661 if (ub1->ub_timestamp > ub2->ub_timestamp) 662 return (1); 663 664 return (0); 665 } 666 667 static void 668 vdev_uberblock_load_done(zio_t *zio) 669 { 670 uberblock_t *ub = zio->io_data; 671 uberblock_t *ubbest = zio->io_private; 672 spa_t *spa = zio->io_spa; 673 674 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd)); 675 676 if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 677 mutex_enter(&spa->spa_uberblock_lock); 678 if (vdev_uberblock_compare(ub, ubbest) > 0) 679 *ubbest = *ub; 680 mutex_exit(&spa->spa_uberblock_lock); 681 } 682 683 zio_buf_free(zio->io_data, zio->io_size); 684 } 685 686 void 687 vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 688 { 689 int l, c, n; 690 691 for (c = 0; c < vd->vdev_children; c++) 692 vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 693 694 if (!vd->vdev_ops->vdev_op_leaf) 695 return; 696 697 if (vdev_is_dead(vd)) 698 return; 699 700 for (l = 0; l < VDEV_LABELS; l++) { 701 for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 702 vdev_label_read(zio, vd, l, 703 zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 704 VDEV_UBERBLOCK_OFFSET(vd, n), 705 VDEV_UBERBLOCK_SIZE(vd), 706 vdev_uberblock_load_done, ubbest); 707 } 708 } 709 } 710 711 /* 712 * Write the uberblock to both labels of all leaves of the specified vdev. 713 * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 714 */ 715 static void 716 vdev_uberblock_sync_done(zio_t *zio) 717 { 718 uint64_t *good_writes = zio->io_root->io_private; 719 720 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 721 atomic_add_64(good_writes, 1); 722 } 723 724 static void 725 vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, uint64_t txg) 726 { 727 int l, c, n; 728 729 for (c = 0; c < vd->vdev_children; c++) 730 vdev_uberblock_sync(zio, ub, vd->vdev_child[c], txg); 731 732 if (!vd->vdev_ops->vdev_op_leaf) 733 return; 734 735 if (vdev_is_dead(vd)) 736 return; 737 738 n = txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 739 740 ASSERT(ub->ub_txg == txg); 741 742 for (l = 0; l < VDEV_LABELS; l++) 743 vdev_label_write(zio, vd, l, ub, 744 VDEV_UBERBLOCK_OFFSET(vd, n), 745 VDEV_UBERBLOCK_SIZE(vd), 746 vdev_uberblock_sync_done, NULL); 747 748 dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg); 749 } 750 751 static int 752 vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *vd, uint64_t txg) 753 { 754 uberblock_t *ubbuf; 755 size_t size = vd->vdev_top ? VDEV_UBERBLOCK_SIZE(vd) : SPA_MAXBLOCKSIZE; 756 uint64_t *good_writes; 757 zio_t *zio; 758 int error; 759 760 ubbuf = zio_buf_alloc(size); 761 bzero(ubbuf, size); 762 *ubbuf = *ub; 763 764 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 765 766 zio = zio_root(spa, NULL, good_writes, 767 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 768 769 vdev_uberblock_sync(zio, ubbuf, vd, txg); 770 771 error = zio_wait(zio); 772 773 if (error && *good_writes != 0) { 774 dprintf("partial success: good_writes = %llu\n", *good_writes); 775 error = 0; 776 } 777 778 /* 779 * It's possible to have no good writes and no error if every vdev is in 780 * the CANT_OPEN state. 781 */ 782 if (*good_writes == 0 && error == 0) 783 error = EIO; 784 785 kmem_free(good_writes, sizeof (uint64_t)); 786 zio_buf_free(ubbuf, size); 787 788 return (error); 789 } 790 791 /* 792 * Sync out an individual vdev. 793 */ 794 static void 795 vdev_sync_label_done(zio_t *zio) 796 { 797 uint64_t *good_writes = zio->io_root->io_private; 798 799 if (zio->io_error == 0) 800 atomic_add_64(good_writes, 1); 801 } 802 803 static void 804 vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg) 805 { 806 nvlist_t *label; 807 vdev_phys_t *vp; 808 char *buf; 809 size_t buflen; 810 int c; 811 812 for (c = 0; c < vd->vdev_children; c++) 813 vdev_sync_label(zio, vd->vdev_child[c], l, txg); 814 815 if (!vd->vdev_ops->vdev_op_leaf) 816 return; 817 818 if (vdev_is_dead(vd)) 819 return; 820 821 /* 822 * Generate a label describing the top-level config to which we belong. 823 */ 824 label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 825 826 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 827 bzero(vp, sizeof (vdev_phys_t)); 828 829 buf = vp->vp_nvlist; 830 buflen = sizeof (vp->vp_nvlist); 831 832 if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) 833 vdev_label_write(zio, vd, l, vp, 834 offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t), 835 vdev_sync_label_done, NULL); 836 837 zio_buf_free(vp, sizeof (vdev_phys_t)); 838 nvlist_free(label); 839 840 dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg); 841 } 842 843 static int 844 vdev_sync_labels(vdev_t *vd, int l, uint64_t txg) 845 { 846 uint64_t *good_writes; 847 zio_t *zio; 848 int error; 849 850 ASSERT(vd == vd->vdev_top); 851 852 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 853 854 zio = zio_root(vd->vdev_spa, NULL, good_writes, 855 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 856 857 /* 858 * Recursively kick off writes to all labels. 859 */ 860 vdev_sync_label(zio, vd, l, txg); 861 862 error = zio_wait(zio); 863 864 if (error && *good_writes != 0) { 865 dprintf("partial success: good_writes = %llu\n", *good_writes); 866 error = 0; 867 } 868 869 if (*good_writes == 0 && error == 0) 870 error = ENODEV; 871 872 /* 873 * Failure to write a label can be fatal for a 874 * top level vdev. We don't want this for slogs 875 * as we use the main pool if they go away. 876 */ 877 if (vd->vdev_islog) 878 error = 0; 879 880 kmem_free(good_writes, sizeof (uint64_t)); 881 882 return (error); 883 } 884 885 /* 886 * Sync the entire vdev configuration. 887 * 888 * The order of operations is carefully crafted to ensure that 889 * if the system panics or loses power at any time, the state on disk 890 * is still transactionally consistent. The in-line comments below 891 * describe the failure semantics at each stage. 892 * 893 * Moreover, it is designed to be idempotent: if spa_sync_labels() fails 894 * at any time, you can just call it again, and it will resume its work. 895 */ 896 int 897 vdev_config_sync(vdev_t *uvd, uint64_t txg) 898 { 899 spa_t *spa = uvd->vdev_spa; 900 uberblock_t *ub = &spa->spa_uberblock; 901 vdev_t *rvd = spa->spa_root_vdev; 902 vdev_t *vd; 903 zio_t *zio; 904 int l, error; 905 906 ASSERT(ub->ub_txg <= txg); 907 908 /* 909 * If this isn't a resync due to I/O errors, and nothing changed 910 * in this transaction group, and the vdev configuration hasn't changed, 911 * then there's nothing to do. 912 */ 913 if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE && 914 list_is_empty(&spa->spa_dirty_list)) { 915 dprintf("nothing to sync in %s in txg %llu\n", 916 spa_name(spa), txg); 917 return (0); 918 } 919 920 if (txg > spa_freeze_txg(spa)) 921 return (0); 922 923 ASSERT(txg <= spa->spa_final_txg); 924 925 dprintf("syncing %s txg %llu\n", spa_name(spa), txg); 926 927 /* 928 * Flush the write cache of every disk that's been written to 929 * in this transaction group. This ensures that all blocks 930 * written in this txg will be committed to stable storage 931 * before any uberblock that references them. 932 */ 933 zio = zio_root(spa, NULL, NULL, 934 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 935 for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 936 vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) { 937 zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 938 NULL, NULL, ZIO_PRIORITY_NOW, 939 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 940 } 941 (void) zio_wait(zio); 942 943 /* 944 * Sync out the even labels (L0, L2) for every dirty vdev. If the 945 * system dies in the middle of this process, that's OK: all of the 946 * even labels that made it to disk will be newer than any uberblock, 947 * and will therefore be considered invalid. The odd labels (L1, L3), 948 * which have not yet been touched, will still be valid. 949 */ 950 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 951 vd = list_next(&spa->spa_dirty_list, vd)) { 952 for (l = 0; l < VDEV_LABELS; l++) { 953 if (l & 1) 954 continue; 955 if ((error = vdev_sync_labels(vd, l, txg)) != 0) 956 return (error); 957 } 958 } 959 960 /* 961 * Flush the new labels to disk. This ensures that all even-label 962 * updates are committed to stable storage before the uberblock update. 963 */ 964 zio = zio_root(spa, NULL, NULL, 965 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 966 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 967 vd = list_next(&spa->spa_dirty_list, vd)) { 968 zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 969 NULL, NULL, ZIO_PRIORITY_NOW, 970 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 971 } 972 (void) zio_wait(zio); 973 974 /* 975 * Sync the uberblocks to all vdevs in the tree specified by uvd. 976 * If the system dies in the middle of this step, there are two cases 977 * to consider, and the on-disk state is consistent either way: 978 * 979 * (1) If none of the new uberblocks made it to disk, then the 980 * previous uberblock will be the newest, and the odd labels 981 * (which had not yet been touched) will be valid with respect 982 * to that uberblock. 983 * 984 * (2) If one or more new uberblocks made it to disk, then they 985 * will be the newest, and the even labels (which had all 986 * been successfully committed) will be valid with respect 987 * to the new uberblocks. 988 */ 989 if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0) 990 return (error); 991 992 /* 993 * Flush the uberblocks to disk. This ensures that the odd labels 994 * are no longer needed (because the new uberblocks and the even 995 * labels are safely on disk), so it is safe to overwrite them. 996 */ 997 (void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE, 998 NULL, NULL, ZIO_PRIORITY_NOW, 999 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 1000 1001 /* 1002 * Sync out odd labels for every dirty vdev. If the system dies 1003 * in the middle of this process, the even labels and the new 1004 * uberblocks will suffice to open the pool. The next time 1005 * the pool is opened, the first thing we'll do -- before any 1006 * user data is modified -- is mark every vdev dirty so that 1007 * all labels will be brought up to date. 1008 */ 1009 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 1010 vd = list_next(&spa->spa_dirty_list, vd)) { 1011 for (l = 0; l < VDEV_LABELS; l++) { 1012 if ((l & 1) == 0) 1013 continue; 1014 if ((error = vdev_sync_labels(vd, l, txg)) != 0) 1015 return (error); 1016 } 1017 } 1018 1019 /* 1020 * Flush the new labels to disk. This ensures that all odd-label 1021 * updates are committed to stable storage before the next 1022 * transaction group begins. 1023 */ 1024 zio = zio_root(spa, NULL, NULL, 1025 ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 1026 for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 1027 vd = list_next(&spa->spa_dirty_list, vd)) { 1028 zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 1029 NULL, NULL, ZIO_PRIORITY_NOW, 1030 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 1031 } 1032 (void) zio_wait(zio); 1033 1034 return (0); 1035 } 1036