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