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 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved. 25 * Copyright 2017 Nexenta Systems, Inc. 26 * Copyright (c) 2014 Integros [integros.com] 27 * Copyright 2016 Toomas Soome <tsoome@me.com> 28 * Copyright 2017 Joyent, Inc. 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/fm/fs/zfs.h> 33 #include <sys/spa.h> 34 #include <sys/spa_impl.h> 35 #include <sys/bpobj.h> 36 #include <sys/dmu.h> 37 #include <sys/dmu_tx.h> 38 #include <sys/dsl_dir.h> 39 #include <sys/vdev_impl.h> 40 #include <sys/uberblock_impl.h> 41 #include <sys/metaslab.h> 42 #include <sys/metaslab_impl.h> 43 #include <sys/space_map.h> 44 #include <sys/space_reftree.h> 45 #include <sys/zio.h> 46 #include <sys/zap.h> 47 #include <sys/fs/zfs.h> 48 #include <sys/arc.h> 49 #include <sys/zil.h> 50 #include <sys/dsl_scan.h> 51 #include <sys/abd.h> 52 53 /* 54 * Virtual device management. 55 */ 56 57 static vdev_ops_t *vdev_ops_table[] = { 58 &vdev_root_ops, 59 &vdev_raidz_ops, 60 &vdev_mirror_ops, 61 &vdev_replacing_ops, 62 &vdev_spare_ops, 63 &vdev_disk_ops, 64 &vdev_file_ops, 65 &vdev_missing_ops, 66 &vdev_hole_ops, 67 &vdev_indirect_ops, 68 NULL 69 }; 70 71 /* maximum scrub/resilver I/O queue per leaf vdev */ 72 int zfs_scrub_limit = 10; 73 74 /* maximum number of metaslabs per top-level vdev */ 75 int vdev_max_ms_count = 200; 76 77 /* minimum amount of metaslabs per top-level vdev */ 78 int vdev_min_ms_count = 16; 79 80 /* see comment in vdev_metaslab_set_size() */ 81 int vdev_default_ms_shift = 29; 82 83 boolean_t vdev_validate_skip = B_FALSE; 84 85 /* 86 * Since the DTL space map of a vdev is not expected to have a lot of 87 * entries, we default its block size to 4K. 88 */ 89 int vdev_dtl_sm_blksz = (1 << 12); 90 91 /* 92 * vdev-wide space maps that have lots of entries written to them at 93 * the end of each transaction can benefit from a higher I/O bandwidth 94 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 95 */ 96 int vdev_standard_sm_blksz = (1 << 17); 97 98 /*PRINTFLIKE2*/ 99 void 100 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 101 { 102 va_list adx; 103 char buf[256]; 104 105 va_start(adx, fmt); 106 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 107 va_end(adx); 108 109 if (vd->vdev_path != NULL) { 110 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 111 vd->vdev_path, buf); 112 } else { 113 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 114 vd->vdev_ops->vdev_op_type, 115 (u_longlong_t)vd->vdev_id, 116 (u_longlong_t)vd->vdev_guid, buf); 117 } 118 } 119 120 void 121 vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 122 { 123 char state[20]; 124 125 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 126 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 127 vd->vdev_ops->vdev_op_type); 128 return; 129 } 130 131 switch (vd->vdev_state) { 132 case VDEV_STATE_UNKNOWN: 133 (void) snprintf(state, sizeof (state), "unknown"); 134 break; 135 case VDEV_STATE_CLOSED: 136 (void) snprintf(state, sizeof (state), "closed"); 137 break; 138 case VDEV_STATE_OFFLINE: 139 (void) snprintf(state, sizeof (state), "offline"); 140 break; 141 case VDEV_STATE_REMOVED: 142 (void) snprintf(state, sizeof (state), "removed"); 143 break; 144 case VDEV_STATE_CANT_OPEN: 145 (void) snprintf(state, sizeof (state), "can't open"); 146 break; 147 case VDEV_STATE_FAULTED: 148 (void) snprintf(state, sizeof (state), "faulted"); 149 break; 150 case VDEV_STATE_DEGRADED: 151 (void) snprintf(state, sizeof (state), "degraded"); 152 break; 153 case VDEV_STATE_HEALTHY: 154 (void) snprintf(state, sizeof (state), "healthy"); 155 break; 156 default: 157 (void) snprintf(state, sizeof (state), "<state %u>", 158 (uint_t)vd->vdev_state); 159 } 160 161 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 162 "", vd->vdev_id, vd->vdev_ops->vdev_op_type, 163 vd->vdev_islog ? " (log)" : "", 164 (u_longlong_t)vd->vdev_guid, 165 vd->vdev_path ? vd->vdev_path : "N/A", state); 166 167 for (uint64_t i = 0; i < vd->vdev_children; i++) 168 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 169 } 170 171 /* 172 * Given a vdev type, return the appropriate ops vector. 173 */ 174 static vdev_ops_t * 175 vdev_getops(const char *type) 176 { 177 vdev_ops_t *ops, **opspp; 178 179 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 180 if (strcmp(ops->vdev_op_type, type) == 0) 181 break; 182 183 return (ops); 184 } 185 186 /* 187 * Default asize function: return the MAX of psize with the asize of 188 * all children. This is what's used by anything other than RAID-Z. 189 */ 190 uint64_t 191 vdev_default_asize(vdev_t *vd, uint64_t psize) 192 { 193 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 194 uint64_t csize; 195 196 for (int c = 0; c < vd->vdev_children; c++) { 197 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 198 asize = MAX(asize, csize); 199 } 200 201 return (asize); 202 } 203 204 /* 205 * Get the minimum allocatable size. We define the allocatable size as 206 * the vdev's asize rounded to the nearest metaslab. This allows us to 207 * replace or attach devices which don't have the same physical size but 208 * can still satisfy the same number of allocations. 209 */ 210 uint64_t 211 vdev_get_min_asize(vdev_t *vd) 212 { 213 vdev_t *pvd = vd->vdev_parent; 214 215 /* 216 * If our parent is NULL (inactive spare or cache) or is the root, 217 * just return our own asize. 218 */ 219 if (pvd == NULL) 220 return (vd->vdev_asize); 221 222 /* 223 * The top-level vdev just returns the allocatable size rounded 224 * to the nearest metaslab. 225 */ 226 if (vd == vd->vdev_top) 227 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 228 229 /* 230 * The allocatable space for a raidz vdev is N * sizeof(smallest child), 231 * so each child must provide at least 1/Nth of its asize. 232 */ 233 if (pvd->vdev_ops == &vdev_raidz_ops) 234 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 235 pvd->vdev_children); 236 237 return (pvd->vdev_min_asize); 238 } 239 240 void 241 vdev_set_min_asize(vdev_t *vd) 242 { 243 vd->vdev_min_asize = vdev_get_min_asize(vd); 244 245 for (int c = 0; c < vd->vdev_children; c++) 246 vdev_set_min_asize(vd->vdev_child[c]); 247 } 248 249 vdev_t * 250 vdev_lookup_top(spa_t *spa, uint64_t vdev) 251 { 252 vdev_t *rvd = spa->spa_root_vdev; 253 254 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 255 256 if (vdev < rvd->vdev_children) { 257 ASSERT(rvd->vdev_child[vdev] != NULL); 258 return (rvd->vdev_child[vdev]); 259 } 260 261 return (NULL); 262 } 263 264 vdev_t * 265 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 266 { 267 vdev_t *mvd; 268 269 if (vd->vdev_guid == guid) 270 return (vd); 271 272 for (int c = 0; c < vd->vdev_children; c++) 273 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 274 NULL) 275 return (mvd); 276 277 return (NULL); 278 } 279 280 static int 281 vdev_count_leaves_impl(vdev_t *vd) 282 { 283 int n = 0; 284 285 if (vd->vdev_ops->vdev_op_leaf) 286 return (1); 287 288 for (int c = 0; c < vd->vdev_children; c++) 289 n += vdev_count_leaves_impl(vd->vdev_child[c]); 290 291 return (n); 292 } 293 294 int 295 vdev_count_leaves(spa_t *spa) 296 { 297 return (vdev_count_leaves_impl(spa->spa_root_vdev)); 298 } 299 300 void 301 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 302 { 303 size_t oldsize, newsize; 304 uint64_t id = cvd->vdev_id; 305 vdev_t **newchild; 306 spa_t *spa = cvd->vdev_spa; 307 308 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 309 ASSERT(cvd->vdev_parent == NULL); 310 311 cvd->vdev_parent = pvd; 312 313 if (pvd == NULL) 314 return; 315 316 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 317 318 oldsize = pvd->vdev_children * sizeof (vdev_t *); 319 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 320 newsize = pvd->vdev_children * sizeof (vdev_t *); 321 322 newchild = kmem_zalloc(newsize, KM_SLEEP); 323 if (pvd->vdev_child != NULL) { 324 bcopy(pvd->vdev_child, newchild, oldsize); 325 kmem_free(pvd->vdev_child, oldsize); 326 } 327 328 pvd->vdev_child = newchild; 329 pvd->vdev_child[id] = cvd; 330 331 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 332 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 333 334 /* 335 * Walk up all ancestors to update guid sum. 336 */ 337 for (; pvd != NULL; pvd = pvd->vdev_parent) 338 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 339 } 340 341 void 342 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 343 { 344 int c; 345 uint_t id = cvd->vdev_id; 346 347 ASSERT(cvd->vdev_parent == pvd); 348 349 if (pvd == NULL) 350 return; 351 352 ASSERT(id < pvd->vdev_children); 353 ASSERT(pvd->vdev_child[id] == cvd); 354 355 pvd->vdev_child[id] = NULL; 356 cvd->vdev_parent = NULL; 357 358 for (c = 0; c < pvd->vdev_children; c++) 359 if (pvd->vdev_child[c]) 360 break; 361 362 if (c == pvd->vdev_children) { 363 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 364 pvd->vdev_child = NULL; 365 pvd->vdev_children = 0; 366 } 367 368 /* 369 * Walk up all ancestors to update guid sum. 370 */ 371 for (; pvd != NULL; pvd = pvd->vdev_parent) 372 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 373 } 374 375 /* 376 * Remove any holes in the child array. 377 */ 378 void 379 vdev_compact_children(vdev_t *pvd) 380 { 381 vdev_t **newchild, *cvd; 382 int oldc = pvd->vdev_children; 383 int newc; 384 385 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 386 387 for (int c = newc = 0; c < oldc; c++) 388 if (pvd->vdev_child[c]) 389 newc++; 390 391 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 392 393 for (int c = newc = 0; c < oldc; c++) { 394 if ((cvd = pvd->vdev_child[c]) != NULL) { 395 newchild[newc] = cvd; 396 cvd->vdev_id = newc++; 397 } 398 } 399 400 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 401 pvd->vdev_child = newchild; 402 pvd->vdev_children = newc; 403 } 404 405 /* 406 * Allocate and minimally initialize a vdev_t. 407 */ 408 vdev_t * 409 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 410 { 411 vdev_t *vd; 412 vdev_indirect_config_t *vic; 413 414 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 415 vic = &vd->vdev_indirect_config; 416 417 if (spa->spa_root_vdev == NULL) { 418 ASSERT(ops == &vdev_root_ops); 419 spa->spa_root_vdev = vd; 420 spa->spa_load_guid = spa_generate_guid(NULL); 421 } 422 423 if (guid == 0 && ops != &vdev_hole_ops) { 424 if (spa->spa_root_vdev == vd) { 425 /* 426 * The root vdev's guid will also be the pool guid, 427 * which must be unique among all pools. 428 */ 429 guid = spa_generate_guid(NULL); 430 } else { 431 /* 432 * Any other vdev's guid must be unique within the pool. 433 */ 434 guid = spa_generate_guid(spa); 435 } 436 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 437 } 438 439 vd->vdev_spa = spa; 440 vd->vdev_id = id; 441 vd->vdev_guid = guid; 442 vd->vdev_guid_sum = guid; 443 vd->vdev_ops = ops; 444 vd->vdev_state = VDEV_STATE_CLOSED; 445 vd->vdev_ishole = (ops == &vdev_hole_ops); 446 vic->vic_prev_indirect_vdev = UINT64_MAX; 447 448 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 449 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 450 vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); 451 452 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 453 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 454 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 455 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 456 for (int t = 0; t < DTL_TYPES; t++) { 457 vd->vdev_dtl[t] = range_tree_create(NULL, NULL); 458 } 459 txg_list_create(&vd->vdev_ms_list, spa, 460 offsetof(struct metaslab, ms_txg_node)); 461 txg_list_create(&vd->vdev_dtl_list, spa, 462 offsetof(struct vdev, vdev_dtl_node)); 463 vd->vdev_stat.vs_timestamp = gethrtime(); 464 vdev_queue_init(vd); 465 vdev_cache_init(vd); 466 467 return (vd); 468 } 469 470 /* 471 * Allocate a new vdev. The 'alloctype' is used to control whether we are 472 * creating a new vdev or loading an existing one - the behavior is slightly 473 * different for each case. 474 */ 475 int 476 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 477 int alloctype) 478 { 479 vdev_ops_t *ops; 480 char *type; 481 uint64_t guid = 0, islog, nparity; 482 vdev_t *vd; 483 vdev_indirect_config_t *vic; 484 485 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 486 487 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 488 return (SET_ERROR(EINVAL)); 489 490 if ((ops = vdev_getops(type)) == NULL) 491 return (SET_ERROR(EINVAL)); 492 493 /* 494 * If this is a load, get the vdev guid from the nvlist. 495 * Otherwise, vdev_alloc_common() will generate one for us. 496 */ 497 if (alloctype == VDEV_ALLOC_LOAD) { 498 uint64_t label_id; 499 500 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 501 label_id != id) 502 return (SET_ERROR(EINVAL)); 503 504 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 505 return (SET_ERROR(EINVAL)); 506 } else if (alloctype == VDEV_ALLOC_SPARE) { 507 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 508 return (SET_ERROR(EINVAL)); 509 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 510 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 511 return (SET_ERROR(EINVAL)); 512 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 513 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 514 return (SET_ERROR(EINVAL)); 515 } 516 517 /* 518 * The first allocated vdev must be of type 'root'. 519 */ 520 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 521 return (SET_ERROR(EINVAL)); 522 523 /* 524 * Determine whether we're a log vdev. 525 */ 526 islog = 0; 527 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 528 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 529 return (SET_ERROR(ENOTSUP)); 530 531 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 532 return (SET_ERROR(ENOTSUP)); 533 534 /* 535 * Set the nparity property for RAID-Z vdevs. 536 */ 537 nparity = -1ULL; 538 if (ops == &vdev_raidz_ops) { 539 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 540 &nparity) == 0) { 541 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 542 return (SET_ERROR(EINVAL)); 543 /* 544 * Previous versions could only support 1 or 2 parity 545 * device. 546 */ 547 if (nparity > 1 && 548 spa_version(spa) < SPA_VERSION_RAIDZ2) 549 return (SET_ERROR(ENOTSUP)); 550 if (nparity > 2 && 551 spa_version(spa) < SPA_VERSION_RAIDZ3) 552 return (SET_ERROR(ENOTSUP)); 553 } else { 554 /* 555 * We require the parity to be specified for SPAs that 556 * support multiple parity levels. 557 */ 558 if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 559 return (SET_ERROR(EINVAL)); 560 /* 561 * Otherwise, we default to 1 parity device for RAID-Z. 562 */ 563 nparity = 1; 564 } 565 } else { 566 nparity = 0; 567 } 568 ASSERT(nparity != -1ULL); 569 570 vd = vdev_alloc_common(spa, id, guid, ops); 571 vic = &vd->vdev_indirect_config; 572 573 vd->vdev_islog = islog; 574 vd->vdev_nparity = nparity; 575 576 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 577 vd->vdev_path = spa_strdup(vd->vdev_path); 578 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 579 vd->vdev_devid = spa_strdup(vd->vdev_devid); 580 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 581 &vd->vdev_physpath) == 0) 582 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 583 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 584 vd->vdev_fru = spa_strdup(vd->vdev_fru); 585 586 /* 587 * Set the whole_disk property. If it's not specified, leave the value 588 * as -1. 589 */ 590 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 591 &vd->vdev_wholedisk) != 0) 592 vd->vdev_wholedisk = -1ULL; 593 594 ASSERT0(vic->vic_mapping_object); 595 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 596 &vic->vic_mapping_object); 597 ASSERT0(vic->vic_births_object); 598 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 599 &vic->vic_births_object); 600 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 601 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 602 &vic->vic_prev_indirect_vdev); 603 604 /* 605 * Look for the 'not present' flag. This will only be set if the device 606 * was not present at the time of import. 607 */ 608 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 609 &vd->vdev_not_present); 610 611 /* 612 * Get the alignment requirement. 613 */ 614 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 615 616 /* 617 * Retrieve the vdev creation time. 618 */ 619 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 620 &vd->vdev_crtxg); 621 622 /* 623 * If we're a top-level vdev, try to load the allocation parameters. 624 */ 625 if (parent && !parent->vdev_parent && 626 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 627 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 628 &vd->vdev_ms_array); 629 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 630 &vd->vdev_ms_shift); 631 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 632 &vd->vdev_asize); 633 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 634 &vd->vdev_removing); 635 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 636 &vd->vdev_top_zap); 637 } else { 638 ASSERT0(vd->vdev_top_zap); 639 } 640 641 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 642 ASSERT(alloctype == VDEV_ALLOC_LOAD || 643 alloctype == VDEV_ALLOC_ADD || 644 alloctype == VDEV_ALLOC_SPLIT || 645 alloctype == VDEV_ALLOC_ROOTPOOL); 646 vd->vdev_mg = metaslab_group_create(islog ? 647 spa_log_class(spa) : spa_normal_class(spa), vd); 648 } 649 650 if (vd->vdev_ops->vdev_op_leaf && 651 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 652 (void) nvlist_lookup_uint64(nv, 653 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 654 } else { 655 ASSERT0(vd->vdev_leaf_zap); 656 } 657 658 /* 659 * If we're a leaf vdev, try to load the DTL object and other state. 660 */ 661 662 if (vd->vdev_ops->vdev_op_leaf && 663 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 664 alloctype == VDEV_ALLOC_ROOTPOOL)) { 665 if (alloctype == VDEV_ALLOC_LOAD) { 666 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 667 &vd->vdev_dtl_object); 668 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 669 &vd->vdev_unspare); 670 } 671 672 if (alloctype == VDEV_ALLOC_ROOTPOOL) { 673 uint64_t spare = 0; 674 675 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 676 &spare) == 0 && spare) 677 spa_spare_add(vd); 678 } 679 680 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 681 &vd->vdev_offline); 682 683 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 684 &vd->vdev_resilver_txg); 685 686 /* 687 * When importing a pool, we want to ignore the persistent fault 688 * state, as the diagnosis made on another system may not be 689 * valid in the current context. Local vdevs will 690 * remain in the faulted state. 691 */ 692 if (spa_load_state(spa) == SPA_LOAD_OPEN) { 693 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 694 &vd->vdev_faulted); 695 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 696 &vd->vdev_degraded); 697 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 698 &vd->vdev_removed); 699 700 if (vd->vdev_faulted || vd->vdev_degraded) { 701 char *aux; 702 703 vd->vdev_label_aux = 704 VDEV_AUX_ERR_EXCEEDED; 705 if (nvlist_lookup_string(nv, 706 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 707 strcmp(aux, "external") == 0) 708 vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 709 } 710 } 711 } 712 713 /* 714 * Add ourselves to the parent's list of children. 715 */ 716 vdev_add_child(parent, vd); 717 718 *vdp = vd; 719 720 return (0); 721 } 722 723 void 724 vdev_free(vdev_t *vd) 725 { 726 spa_t *spa = vd->vdev_spa; 727 728 /* 729 * vdev_free() implies closing the vdev first. This is simpler than 730 * trying to ensure complicated semantics for all callers. 731 */ 732 vdev_close(vd); 733 734 ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 735 ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 736 737 /* 738 * Free all children. 739 */ 740 for (int c = 0; c < vd->vdev_children; c++) 741 vdev_free(vd->vdev_child[c]); 742 743 ASSERT(vd->vdev_child == NULL); 744 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 745 746 /* 747 * Discard allocation state. 748 */ 749 if (vd->vdev_mg != NULL) { 750 vdev_metaslab_fini(vd); 751 metaslab_group_destroy(vd->vdev_mg); 752 } 753 754 ASSERT0(vd->vdev_stat.vs_space); 755 ASSERT0(vd->vdev_stat.vs_dspace); 756 ASSERT0(vd->vdev_stat.vs_alloc); 757 758 /* 759 * Remove this vdev from its parent's child list. 760 */ 761 vdev_remove_child(vd->vdev_parent, vd); 762 763 ASSERT(vd->vdev_parent == NULL); 764 765 /* 766 * Clean up vdev structure. 767 */ 768 vdev_queue_fini(vd); 769 vdev_cache_fini(vd); 770 771 if (vd->vdev_path) 772 spa_strfree(vd->vdev_path); 773 if (vd->vdev_devid) 774 spa_strfree(vd->vdev_devid); 775 if (vd->vdev_physpath) 776 spa_strfree(vd->vdev_physpath); 777 if (vd->vdev_fru) 778 spa_strfree(vd->vdev_fru); 779 780 if (vd->vdev_isspare) 781 spa_spare_remove(vd); 782 if (vd->vdev_isl2cache) 783 spa_l2cache_remove(vd); 784 785 txg_list_destroy(&vd->vdev_ms_list); 786 txg_list_destroy(&vd->vdev_dtl_list); 787 788 mutex_enter(&vd->vdev_dtl_lock); 789 space_map_close(vd->vdev_dtl_sm); 790 for (int t = 0; t < DTL_TYPES; t++) { 791 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 792 range_tree_destroy(vd->vdev_dtl[t]); 793 } 794 mutex_exit(&vd->vdev_dtl_lock); 795 796 EQUIV(vd->vdev_indirect_births != NULL, 797 vd->vdev_indirect_mapping != NULL); 798 if (vd->vdev_indirect_births != NULL) { 799 vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 800 vdev_indirect_births_close(vd->vdev_indirect_births); 801 } 802 803 if (vd->vdev_obsolete_sm != NULL) { 804 ASSERT(vd->vdev_removing || 805 vd->vdev_ops == &vdev_indirect_ops); 806 space_map_close(vd->vdev_obsolete_sm); 807 vd->vdev_obsolete_sm = NULL; 808 } 809 range_tree_destroy(vd->vdev_obsolete_segments); 810 rw_destroy(&vd->vdev_indirect_rwlock); 811 mutex_destroy(&vd->vdev_obsolete_lock); 812 813 mutex_destroy(&vd->vdev_queue_lock); 814 mutex_destroy(&vd->vdev_dtl_lock); 815 mutex_destroy(&vd->vdev_stat_lock); 816 mutex_destroy(&vd->vdev_probe_lock); 817 818 if (vd == spa->spa_root_vdev) 819 spa->spa_root_vdev = NULL; 820 821 kmem_free(vd, sizeof (vdev_t)); 822 } 823 824 /* 825 * Transfer top-level vdev state from svd to tvd. 826 */ 827 static void 828 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 829 { 830 spa_t *spa = svd->vdev_spa; 831 metaslab_t *msp; 832 vdev_t *vd; 833 int t; 834 835 ASSERT(tvd == tvd->vdev_top); 836 837 tvd->vdev_ms_array = svd->vdev_ms_array; 838 tvd->vdev_ms_shift = svd->vdev_ms_shift; 839 tvd->vdev_ms_count = svd->vdev_ms_count; 840 tvd->vdev_top_zap = svd->vdev_top_zap; 841 842 svd->vdev_ms_array = 0; 843 svd->vdev_ms_shift = 0; 844 svd->vdev_ms_count = 0; 845 svd->vdev_top_zap = 0; 846 847 if (tvd->vdev_mg) 848 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 849 tvd->vdev_mg = svd->vdev_mg; 850 tvd->vdev_ms = svd->vdev_ms; 851 852 svd->vdev_mg = NULL; 853 svd->vdev_ms = NULL; 854 855 if (tvd->vdev_mg != NULL) 856 tvd->vdev_mg->mg_vd = tvd; 857 858 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 859 svd->vdev_checkpoint_sm = NULL; 860 861 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 862 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 863 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 864 865 svd->vdev_stat.vs_alloc = 0; 866 svd->vdev_stat.vs_space = 0; 867 svd->vdev_stat.vs_dspace = 0; 868 869 for (t = 0; t < TXG_SIZE; t++) { 870 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 871 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 872 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 873 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 874 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 875 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 876 } 877 878 if (list_link_active(&svd->vdev_config_dirty_node)) { 879 vdev_config_clean(svd); 880 vdev_config_dirty(tvd); 881 } 882 883 if (list_link_active(&svd->vdev_state_dirty_node)) { 884 vdev_state_clean(svd); 885 vdev_state_dirty(tvd); 886 } 887 888 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 889 svd->vdev_deflate_ratio = 0; 890 891 tvd->vdev_islog = svd->vdev_islog; 892 svd->vdev_islog = 0; 893 } 894 895 static void 896 vdev_top_update(vdev_t *tvd, vdev_t *vd) 897 { 898 if (vd == NULL) 899 return; 900 901 vd->vdev_top = tvd; 902 903 for (int c = 0; c < vd->vdev_children; c++) 904 vdev_top_update(tvd, vd->vdev_child[c]); 905 } 906 907 /* 908 * Add a mirror/replacing vdev above an existing vdev. 909 */ 910 vdev_t * 911 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 912 { 913 spa_t *spa = cvd->vdev_spa; 914 vdev_t *pvd = cvd->vdev_parent; 915 vdev_t *mvd; 916 917 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 918 919 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 920 921 mvd->vdev_asize = cvd->vdev_asize; 922 mvd->vdev_min_asize = cvd->vdev_min_asize; 923 mvd->vdev_max_asize = cvd->vdev_max_asize; 924 mvd->vdev_psize = cvd->vdev_psize; 925 mvd->vdev_ashift = cvd->vdev_ashift; 926 mvd->vdev_state = cvd->vdev_state; 927 mvd->vdev_crtxg = cvd->vdev_crtxg; 928 929 vdev_remove_child(pvd, cvd); 930 vdev_add_child(pvd, mvd); 931 cvd->vdev_id = mvd->vdev_children; 932 vdev_add_child(mvd, cvd); 933 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 934 935 if (mvd == mvd->vdev_top) 936 vdev_top_transfer(cvd, mvd); 937 938 return (mvd); 939 } 940 941 /* 942 * Remove a 1-way mirror/replacing vdev from the tree. 943 */ 944 void 945 vdev_remove_parent(vdev_t *cvd) 946 { 947 vdev_t *mvd = cvd->vdev_parent; 948 vdev_t *pvd = mvd->vdev_parent; 949 950 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 951 952 ASSERT(mvd->vdev_children == 1); 953 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 954 mvd->vdev_ops == &vdev_replacing_ops || 955 mvd->vdev_ops == &vdev_spare_ops); 956 cvd->vdev_ashift = mvd->vdev_ashift; 957 958 vdev_remove_child(mvd, cvd); 959 vdev_remove_child(pvd, mvd); 960 961 /* 962 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 963 * Otherwise, we could have detached an offline device, and when we 964 * go to import the pool we'll think we have two top-level vdevs, 965 * instead of a different version of the same top-level vdev. 966 */ 967 if (mvd->vdev_top == mvd) { 968 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 969 cvd->vdev_orig_guid = cvd->vdev_guid; 970 cvd->vdev_guid += guid_delta; 971 cvd->vdev_guid_sum += guid_delta; 972 } 973 cvd->vdev_id = mvd->vdev_id; 974 vdev_add_child(pvd, cvd); 975 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 976 977 if (cvd == cvd->vdev_top) 978 vdev_top_transfer(mvd, cvd); 979 980 ASSERT(mvd->vdev_children == 0); 981 vdev_free(mvd); 982 } 983 984 int 985 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 986 { 987 spa_t *spa = vd->vdev_spa; 988 objset_t *mos = spa->spa_meta_objset; 989 uint64_t m; 990 uint64_t oldc = vd->vdev_ms_count; 991 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 992 metaslab_t **mspp; 993 int error; 994 995 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 996 997 /* 998 * This vdev is not being allocated from yet or is a hole. 999 */ 1000 if (vd->vdev_ms_shift == 0) 1001 return (0); 1002 1003 ASSERT(!vd->vdev_ishole); 1004 1005 ASSERT(oldc <= newc); 1006 1007 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1008 1009 if (oldc != 0) { 1010 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1011 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1012 } 1013 1014 vd->vdev_ms = mspp; 1015 vd->vdev_ms_count = newc; 1016 1017 for (m = oldc; m < newc; m++) { 1018 uint64_t object = 0; 1019 1020 /* 1021 * vdev_ms_array may be 0 if we are creating the "fake" 1022 * metaslabs for an indirect vdev for zdb's leak detection. 1023 * See zdb_leak_init(). 1024 */ 1025 if (txg == 0 && vd->vdev_ms_array != 0) { 1026 error = dmu_read(mos, vd->vdev_ms_array, 1027 m * sizeof (uint64_t), sizeof (uint64_t), &object, 1028 DMU_READ_PREFETCH); 1029 if (error != 0) { 1030 vdev_dbgmsg(vd, "unable to read the metaslab " 1031 "array [error=%d]", error); 1032 return (error); 1033 } 1034 } 1035 1036 error = metaslab_init(vd->vdev_mg, m, object, txg, 1037 &(vd->vdev_ms[m])); 1038 if (error != 0) { 1039 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 1040 error); 1041 return (error); 1042 } 1043 } 1044 1045 if (txg == 0) 1046 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1047 1048 /* 1049 * If the vdev is being removed we don't activate 1050 * the metaslabs since we want to ensure that no new 1051 * allocations are performed on this device. 1052 */ 1053 if (oldc == 0 && !vd->vdev_removing) 1054 metaslab_group_activate(vd->vdev_mg); 1055 1056 if (txg == 0) 1057 spa_config_exit(spa, SCL_ALLOC, FTAG); 1058 1059 return (0); 1060 } 1061 1062 void 1063 vdev_metaslab_fini(vdev_t *vd) 1064 { 1065 if (vd->vdev_checkpoint_sm != NULL) { 1066 ASSERT(spa_feature_is_active(vd->vdev_spa, 1067 SPA_FEATURE_POOL_CHECKPOINT)); 1068 space_map_close(vd->vdev_checkpoint_sm); 1069 /* 1070 * Even though we close the space map, we need to set its 1071 * pointer to NULL. The reason is that vdev_metaslab_fini() 1072 * may be called multiple times for certain operations 1073 * (i.e. when destroying a pool) so we need to ensure that 1074 * this clause never executes twice. This logic is similar 1075 * to the one used for the vdev_ms clause below. 1076 */ 1077 vd->vdev_checkpoint_sm = NULL; 1078 } 1079 1080 if (vd->vdev_ms != NULL) { 1081 uint64_t count = vd->vdev_ms_count; 1082 1083 metaslab_group_passivate(vd->vdev_mg); 1084 for (uint64_t m = 0; m < count; m++) { 1085 metaslab_t *msp = vd->vdev_ms[m]; 1086 1087 if (msp != NULL) 1088 metaslab_fini(msp); 1089 } 1090 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1091 vd->vdev_ms = NULL; 1092 1093 vd->vdev_ms_count = 0; 1094 } 1095 ASSERT0(vd->vdev_ms_count); 1096 } 1097 1098 typedef struct vdev_probe_stats { 1099 boolean_t vps_readable; 1100 boolean_t vps_writeable; 1101 int vps_flags; 1102 } vdev_probe_stats_t; 1103 1104 static void 1105 vdev_probe_done(zio_t *zio) 1106 { 1107 spa_t *spa = zio->io_spa; 1108 vdev_t *vd = zio->io_vd; 1109 vdev_probe_stats_t *vps = zio->io_private; 1110 1111 ASSERT(vd->vdev_probe_zio != NULL); 1112 1113 if (zio->io_type == ZIO_TYPE_READ) { 1114 if (zio->io_error == 0) 1115 vps->vps_readable = 1; 1116 if (zio->io_error == 0 && spa_writeable(spa)) { 1117 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1118 zio->io_offset, zio->io_size, zio->io_abd, 1119 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1120 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1121 } else { 1122 abd_free(zio->io_abd); 1123 } 1124 } else if (zio->io_type == ZIO_TYPE_WRITE) { 1125 if (zio->io_error == 0) 1126 vps->vps_writeable = 1; 1127 abd_free(zio->io_abd); 1128 } else if (zio->io_type == ZIO_TYPE_NULL) { 1129 zio_t *pio; 1130 1131 vd->vdev_cant_read |= !vps->vps_readable; 1132 vd->vdev_cant_write |= !vps->vps_writeable; 1133 1134 if (vdev_readable(vd) && 1135 (vdev_writeable(vd) || !spa_writeable(spa))) { 1136 zio->io_error = 0; 1137 } else { 1138 ASSERT(zio->io_error != 0); 1139 vdev_dbgmsg(vd, "failed probe"); 1140 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1141 spa, vd, NULL, 0, 0); 1142 zio->io_error = SET_ERROR(ENXIO); 1143 } 1144 1145 mutex_enter(&vd->vdev_probe_lock); 1146 ASSERT(vd->vdev_probe_zio == zio); 1147 vd->vdev_probe_zio = NULL; 1148 mutex_exit(&vd->vdev_probe_lock); 1149 1150 zio_link_t *zl = NULL; 1151 while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1152 if (!vdev_accessible(vd, pio)) 1153 pio->io_error = SET_ERROR(ENXIO); 1154 1155 kmem_free(vps, sizeof (*vps)); 1156 } 1157 } 1158 1159 /* 1160 * Determine whether this device is accessible. 1161 * 1162 * Read and write to several known locations: the pad regions of each 1163 * vdev label but the first, which we leave alone in case it contains 1164 * a VTOC. 1165 */ 1166 zio_t * 1167 vdev_probe(vdev_t *vd, zio_t *zio) 1168 { 1169 spa_t *spa = vd->vdev_spa; 1170 vdev_probe_stats_t *vps = NULL; 1171 zio_t *pio; 1172 1173 ASSERT(vd->vdev_ops->vdev_op_leaf); 1174 1175 /* 1176 * Don't probe the probe. 1177 */ 1178 if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1179 return (NULL); 1180 1181 /* 1182 * To prevent 'probe storms' when a device fails, we create 1183 * just one probe i/o at a time. All zios that want to probe 1184 * this vdev will become parents of the probe io. 1185 */ 1186 mutex_enter(&vd->vdev_probe_lock); 1187 1188 if ((pio = vd->vdev_probe_zio) == NULL) { 1189 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1190 1191 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1192 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1193 ZIO_FLAG_TRYHARD; 1194 1195 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1196 /* 1197 * vdev_cant_read and vdev_cant_write can only 1198 * transition from TRUE to FALSE when we have the 1199 * SCL_ZIO lock as writer; otherwise they can only 1200 * transition from FALSE to TRUE. This ensures that 1201 * any zio looking at these values can assume that 1202 * failures persist for the life of the I/O. That's 1203 * important because when a device has intermittent 1204 * connectivity problems, we want to ensure that 1205 * they're ascribed to the device (ENXIO) and not 1206 * the zio (EIO). 1207 * 1208 * Since we hold SCL_ZIO as writer here, clear both 1209 * values so the probe can reevaluate from first 1210 * principles. 1211 */ 1212 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1213 vd->vdev_cant_read = B_FALSE; 1214 vd->vdev_cant_write = B_FALSE; 1215 } 1216 1217 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1218 vdev_probe_done, vps, 1219 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1220 1221 /* 1222 * We can't change the vdev state in this context, so we 1223 * kick off an async task to do it on our behalf. 1224 */ 1225 if (zio != NULL) { 1226 vd->vdev_probe_wanted = B_TRUE; 1227 spa_async_request(spa, SPA_ASYNC_PROBE); 1228 } 1229 } 1230 1231 if (zio != NULL) 1232 zio_add_child(zio, pio); 1233 1234 mutex_exit(&vd->vdev_probe_lock); 1235 1236 if (vps == NULL) { 1237 ASSERT(zio != NULL); 1238 return (NULL); 1239 } 1240 1241 for (int l = 1; l < VDEV_LABELS; l++) { 1242 zio_nowait(zio_read_phys(pio, vd, 1243 vdev_label_offset(vd->vdev_psize, l, 1244 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1245 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1246 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1247 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1248 } 1249 1250 if (zio == NULL) 1251 return (pio); 1252 1253 zio_nowait(pio); 1254 return (NULL); 1255 } 1256 1257 static void 1258 vdev_open_child(void *arg) 1259 { 1260 vdev_t *vd = arg; 1261 1262 vd->vdev_open_thread = curthread; 1263 vd->vdev_open_error = vdev_open(vd); 1264 vd->vdev_open_thread = NULL; 1265 } 1266 1267 boolean_t 1268 vdev_uses_zvols(vdev_t *vd) 1269 { 1270 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1271 strlen(ZVOL_DIR)) == 0) 1272 return (B_TRUE); 1273 for (int c = 0; c < vd->vdev_children; c++) 1274 if (vdev_uses_zvols(vd->vdev_child[c])) 1275 return (B_TRUE); 1276 return (B_FALSE); 1277 } 1278 1279 void 1280 vdev_open_children(vdev_t *vd) 1281 { 1282 taskq_t *tq; 1283 int children = vd->vdev_children; 1284 1285 /* 1286 * in order to handle pools on top of zvols, do the opens 1287 * in a single thread so that the same thread holds the 1288 * spa_namespace_lock 1289 */ 1290 if (vdev_uses_zvols(vd)) { 1291 for (int c = 0; c < children; c++) 1292 vd->vdev_child[c]->vdev_open_error = 1293 vdev_open(vd->vdev_child[c]); 1294 return; 1295 } 1296 tq = taskq_create("vdev_open", children, minclsyspri, 1297 children, children, TASKQ_PREPOPULATE); 1298 1299 for (int c = 0; c < children; c++) 1300 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1301 TQ_SLEEP) != NULL); 1302 1303 taskq_destroy(tq); 1304 } 1305 1306 /* 1307 * Compute the raidz-deflation ratio. Note, we hard-code 1308 * in 128k (1 << 17) because it is the "typical" blocksize. 1309 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 1310 * otherwise it would inconsistently account for existing bp's. 1311 */ 1312 static void 1313 vdev_set_deflate_ratio(vdev_t *vd) 1314 { 1315 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 1316 vd->vdev_deflate_ratio = (1 << 17) / 1317 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 1318 } 1319 } 1320 1321 /* 1322 * Prepare a virtual device for access. 1323 */ 1324 int 1325 vdev_open(vdev_t *vd) 1326 { 1327 spa_t *spa = vd->vdev_spa; 1328 int error; 1329 uint64_t osize = 0; 1330 uint64_t max_osize = 0; 1331 uint64_t asize, max_asize, psize; 1332 uint64_t ashift = 0; 1333 1334 ASSERT(vd->vdev_open_thread == curthread || 1335 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1336 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1337 vd->vdev_state == VDEV_STATE_CANT_OPEN || 1338 vd->vdev_state == VDEV_STATE_OFFLINE); 1339 1340 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1341 vd->vdev_cant_read = B_FALSE; 1342 vd->vdev_cant_write = B_FALSE; 1343 vd->vdev_min_asize = vdev_get_min_asize(vd); 1344 1345 /* 1346 * If this vdev is not removed, check its fault status. If it's 1347 * faulted, bail out of the open. 1348 */ 1349 if (!vd->vdev_removed && vd->vdev_faulted) { 1350 ASSERT(vd->vdev_children == 0); 1351 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1352 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1353 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1354 vd->vdev_label_aux); 1355 return (SET_ERROR(ENXIO)); 1356 } else if (vd->vdev_offline) { 1357 ASSERT(vd->vdev_children == 0); 1358 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1359 return (SET_ERROR(ENXIO)); 1360 } 1361 1362 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1363 1364 /* 1365 * Reset the vdev_reopening flag so that we actually close 1366 * the vdev on error. 1367 */ 1368 vd->vdev_reopening = B_FALSE; 1369 if (zio_injection_enabled && error == 0) 1370 error = zio_handle_device_injection(vd, NULL, ENXIO); 1371 1372 if (error) { 1373 if (vd->vdev_removed && 1374 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1375 vd->vdev_removed = B_FALSE; 1376 1377 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 1378 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 1379 vd->vdev_stat.vs_aux); 1380 } else { 1381 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1382 vd->vdev_stat.vs_aux); 1383 } 1384 return (error); 1385 } 1386 1387 vd->vdev_removed = B_FALSE; 1388 1389 /* 1390 * Recheck the faulted flag now that we have confirmed that 1391 * the vdev is accessible. If we're faulted, bail. 1392 */ 1393 if (vd->vdev_faulted) { 1394 ASSERT(vd->vdev_children == 0); 1395 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1396 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1397 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1398 vd->vdev_label_aux); 1399 return (SET_ERROR(ENXIO)); 1400 } 1401 1402 if (vd->vdev_degraded) { 1403 ASSERT(vd->vdev_children == 0); 1404 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1405 VDEV_AUX_ERR_EXCEEDED); 1406 } else { 1407 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1408 } 1409 1410 /* 1411 * For hole or missing vdevs we just return success. 1412 */ 1413 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1414 return (0); 1415 1416 for (int c = 0; c < vd->vdev_children; c++) { 1417 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1418 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1419 VDEV_AUX_NONE); 1420 break; 1421 } 1422 } 1423 1424 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1425 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1426 1427 if (vd->vdev_children == 0) { 1428 if (osize < SPA_MINDEVSIZE) { 1429 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1430 VDEV_AUX_TOO_SMALL); 1431 return (SET_ERROR(EOVERFLOW)); 1432 } 1433 psize = osize; 1434 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1435 max_asize = max_osize - (VDEV_LABEL_START_SIZE + 1436 VDEV_LABEL_END_SIZE); 1437 } else { 1438 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1439 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1440 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1441 VDEV_AUX_TOO_SMALL); 1442 return (SET_ERROR(EOVERFLOW)); 1443 } 1444 psize = 0; 1445 asize = osize; 1446 max_asize = max_osize; 1447 } 1448 1449 vd->vdev_psize = psize; 1450 1451 /* 1452 * Make sure the allocatable size hasn't shrunk too much. 1453 */ 1454 if (asize < vd->vdev_min_asize) { 1455 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1456 VDEV_AUX_BAD_LABEL); 1457 return (SET_ERROR(EINVAL)); 1458 } 1459 1460 if (vd->vdev_asize == 0) { 1461 /* 1462 * This is the first-ever open, so use the computed values. 1463 * For testing purposes, a higher ashift can be requested. 1464 */ 1465 vd->vdev_asize = asize; 1466 vd->vdev_max_asize = max_asize; 1467 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1468 } else { 1469 /* 1470 * Detect if the alignment requirement has increased. 1471 * We don't want to make the pool unavailable, just 1472 * issue a warning instead. 1473 */ 1474 if (ashift > vd->vdev_top->vdev_ashift && 1475 vd->vdev_ops->vdev_op_leaf) { 1476 cmn_err(CE_WARN, 1477 "Disk, '%s', has a block alignment that is " 1478 "larger than the pool's alignment\n", 1479 vd->vdev_path); 1480 } 1481 vd->vdev_max_asize = max_asize; 1482 } 1483 1484 /* 1485 * If all children are healthy we update asize if either: 1486 * The asize has increased, due to a device expansion caused by dynamic 1487 * LUN growth or vdev replacement, and automatic expansion is enabled; 1488 * making the additional space available. 1489 * 1490 * The asize has decreased, due to a device shrink usually caused by a 1491 * vdev replace with a smaller device. This ensures that calculations 1492 * based of max_asize and asize e.g. esize are always valid. It's safe 1493 * to do this as we've already validated that asize is greater than 1494 * vdev_min_asize. 1495 */ 1496 if (vd->vdev_state == VDEV_STATE_HEALTHY && 1497 ((asize > vd->vdev_asize && 1498 (vd->vdev_expanding || spa->spa_autoexpand)) || 1499 (asize < vd->vdev_asize))) 1500 vd->vdev_asize = asize; 1501 1502 vdev_set_min_asize(vd); 1503 1504 /* 1505 * Ensure we can issue some IO before declaring the 1506 * vdev open for business. 1507 */ 1508 if (vd->vdev_ops->vdev_op_leaf && 1509 (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1510 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1511 VDEV_AUX_ERR_EXCEEDED); 1512 return (error); 1513 } 1514 1515 /* 1516 * Track the min and max ashift values for normal data devices. 1517 */ 1518 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1519 !vd->vdev_islog && vd->vdev_aux == NULL) { 1520 if (vd->vdev_ashift > spa->spa_max_ashift) 1521 spa->spa_max_ashift = vd->vdev_ashift; 1522 if (vd->vdev_ashift < spa->spa_min_ashift) 1523 spa->spa_min_ashift = vd->vdev_ashift; 1524 } 1525 1526 /* 1527 * If a leaf vdev has a DTL, and seems healthy, then kick off a 1528 * resilver. But don't do this if we are doing a reopen for a scrub, 1529 * since this would just restart the scrub we are already doing. 1530 */ 1531 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1532 vdev_resilver_needed(vd, NULL, NULL)) 1533 spa_async_request(spa, SPA_ASYNC_RESILVER); 1534 1535 return (0); 1536 } 1537 1538 /* 1539 * Called once the vdevs are all opened, this routine validates the label 1540 * contents. This needs to be done before vdev_load() so that we don't 1541 * inadvertently do repair I/Os to the wrong device. 1542 * 1543 * This function will only return failure if one of the vdevs indicates that it 1544 * has since been destroyed or exported. This is only possible if 1545 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1546 * will be updated but the function will return 0. 1547 */ 1548 int 1549 vdev_validate(vdev_t *vd) 1550 { 1551 spa_t *spa = vd->vdev_spa; 1552 nvlist_t *label; 1553 uint64_t guid = 0, aux_guid = 0, top_guid; 1554 uint64_t state; 1555 nvlist_t *nvl; 1556 uint64_t txg; 1557 1558 if (vdev_validate_skip) 1559 return (0); 1560 1561 for (uint64_t c = 0; c < vd->vdev_children; c++) 1562 if (vdev_validate(vd->vdev_child[c]) != 0) 1563 return (SET_ERROR(EBADF)); 1564 1565 /* 1566 * If the device has already failed, or was marked offline, don't do 1567 * any further validation. Otherwise, label I/O will fail and we will 1568 * overwrite the previous state. 1569 */ 1570 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 1571 return (0); 1572 1573 /* 1574 * If we are performing an extreme rewind, we allow for a label that 1575 * was modified at a point after the current txg. 1576 */ 1577 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0) 1578 txg = UINT64_MAX; 1579 else 1580 txg = spa_last_synced_txg(spa); 1581 1582 if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1583 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1584 VDEV_AUX_BAD_LABEL); 1585 vdev_dbgmsg(vd, "vdev_validate: failed reading config"); 1586 return (0); 1587 } 1588 1589 /* 1590 * Determine if this vdev has been split off into another 1591 * pool. If so, then refuse to open it. 1592 */ 1593 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1594 &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1595 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1596 VDEV_AUX_SPLIT_POOL); 1597 nvlist_free(label); 1598 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 1599 return (0); 1600 } 1601 1602 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 1603 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1604 VDEV_AUX_CORRUPT_DATA); 1605 nvlist_free(label); 1606 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1607 ZPOOL_CONFIG_POOL_GUID); 1608 return (0); 1609 } 1610 1611 /* 1612 * If config is not trusted then ignore the spa guid check. This is 1613 * necessary because if the machine crashed during a re-guid the new 1614 * guid might have been written to all of the vdev labels, but not the 1615 * cached config. The check will be performed again once we have the 1616 * trusted config from the MOS. 1617 */ 1618 if (spa->spa_trust_config && guid != spa_guid(spa)) { 1619 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1620 VDEV_AUX_CORRUPT_DATA); 1621 nvlist_free(label); 1622 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 1623 "match config (%llu != %llu)", (u_longlong_t)guid, 1624 (u_longlong_t)spa_guid(spa)); 1625 return (0); 1626 } 1627 1628 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1629 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1630 &aux_guid) != 0) 1631 aux_guid = 0; 1632 1633 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 1634 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1635 VDEV_AUX_CORRUPT_DATA); 1636 nvlist_free(label); 1637 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1638 ZPOOL_CONFIG_GUID); 1639 return (0); 1640 } 1641 1642 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 1643 != 0) { 1644 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1645 VDEV_AUX_CORRUPT_DATA); 1646 nvlist_free(label); 1647 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1648 ZPOOL_CONFIG_TOP_GUID); 1649 return (0); 1650 } 1651 1652 /* 1653 * If this vdev just became a top-level vdev because its sibling was 1654 * detached, it will have adopted the parent's vdev guid -- but the 1655 * label may or may not be on disk yet. Fortunately, either version 1656 * of the label will have the same top guid, so if we're a top-level 1657 * vdev, we can safely compare to that instead. 1658 * However, if the config comes from a cachefile that failed to update 1659 * after the detach, a top-level vdev will appear as a non top-level 1660 * vdev in the config. Also relax the constraints if we perform an 1661 * extreme rewind. 1662 * 1663 * If we split this vdev off instead, then we also check the 1664 * original pool's guid. We don't want to consider the vdev 1665 * corrupt if it is partway through a split operation. 1666 */ 1667 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 1668 boolean_t mismatch = B_FALSE; 1669 if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 1670 if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 1671 mismatch = B_TRUE; 1672 } else { 1673 if (vd->vdev_guid != top_guid && 1674 vd->vdev_top->vdev_guid != guid) 1675 mismatch = B_TRUE; 1676 } 1677 1678 if (mismatch) { 1679 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1680 VDEV_AUX_CORRUPT_DATA); 1681 nvlist_free(label); 1682 vdev_dbgmsg(vd, "vdev_validate: config guid " 1683 "doesn't match label guid"); 1684 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 1685 (u_longlong_t)vd->vdev_guid, 1686 (u_longlong_t)vd->vdev_top->vdev_guid); 1687 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 1688 "aux_guid %llu", (u_longlong_t)guid, 1689 (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1690 return (0); 1691 } 1692 } 1693 1694 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1695 &state) != 0) { 1696 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1697 VDEV_AUX_CORRUPT_DATA); 1698 nvlist_free(label); 1699 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1700 ZPOOL_CONFIG_POOL_STATE); 1701 return (0); 1702 } 1703 1704 nvlist_free(label); 1705 1706 /* 1707 * If this is a verbatim import, no need to check the 1708 * state of the pool. 1709 */ 1710 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1711 spa_load_state(spa) == SPA_LOAD_OPEN && 1712 state != POOL_STATE_ACTIVE) { 1713 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 1714 "for spa %s", (u_longlong_t)state, spa->spa_name); 1715 return (SET_ERROR(EBADF)); 1716 } 1717 1718 /* 1719 * If we were able to open and validate a vdev that was 1720 * previously marked permanently unavailable, clear that state 1721 * now. 1722 */ 1723 if (vd->vdev_not_present) 1724 vd->vdev_not_present = 0; 1725 1726 return (0); 1727 } 1728 1729 static void 1730 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 1731 { 1732 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 1733 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 1734 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 1735 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 1736 dvd->vdev_path, svd->vdev_path); 1737 spa_strfree(dvd->vdev_path); 1738 dvd->vdev_path = spa_strdup(svd->vdev_path); 1739 } 1740 } else if (svd->vdev_path != NULL) { 1741 dvd->vdev_path = spa_strdup(svd->vdev_path); 1742 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 1743 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 1744 } 1745 } 1746 1747 /* 1748 * Recursively copy vdev paths from one vdev to another. Source and destination 1749 * vdev trees must have same geometry otherwise return error. Intended to copy 1750 * paths from userland config into MOS config. 1751 */ 1752 int 1753 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 1754 { 1755 if ((svd->vdev_ops == &vdev_missing_ops) || 1756 (svd->vdev_ishole && dvd->vdev_ishole) || 1757 (dvd->vdev_ops == &vdev_indirect_ops)) 1758 return (0); 1759 1760 if (svd->vdev_ops != dvd->vdev_ops) { 1761 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 1762 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 1763 return (SET_ERROR(EINVAL)); 1764 } 1765 1766 if (svd->vdev_guid != dvd->vdev_guid) { 1767 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 1768 "%llu)", (u_longlong_t)svd->vdev_guid, 1769 (u_longlong_t)dvd->vdev_guid); 1770 return (SET_ERROR(EINVAL)); 1771 } 1772 1773 if (svd->vdev_children != dvd->vdev_children) { 1774 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 1775 "%llu != %llu", (u_longlong_t)svd->vdev_children, 1776 (u_longlong_t)dvd->vdev_children); 1777 return (SET_ERROR(EINVAL)); 1778 } 1779 1780 for (uint64_t i = 0; i < svd->vdev_children; i++) { 1781 int error = vdev_copy_path_strict(svd->vdev_child[i], 1782 dvd->vdev_child[i]); 1783 if (error != 0) 1784 return (error); 1785 } 1786 1787 if (svd->vdev_ops->vdev_op_leaf) 1788 vdev_copy_path_impl(svd, dvd); 1789 1790 return (0); 1791 } 1792 1793 static void 1794 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 1795 { 1796 ASSERT(stvd->vdev_top == stvd); 1797 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 1798 1799 for (uint64_t i = 0; i < dvd->vdev_children; i++) { 1800 vdev_copy_path_search(stvd, dvd->vdev_child[i]); 1801 } 1802 1803 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 1804 return; 1805 1806 /* 1807 * The idea here is that while a vdev can shift positions within 1808 * a top vdev (when replacing, attaching mirror, etc.) it cannot 1809 * step outside of it. 1810 */ 1811 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 1812 1813 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 1814 return; 1815 1816 ASSERT(vd->vdev_ops->vdev_op_leaf); 1817 1818 vdev_copy_path_impl(vd, dvd); 1819 } 1820 1821 /* 1822 * Recursively copy vdev paths from one root vdev to another. Source and 1823 * destination vdev trees may differ in geometry. For each destination leaf 1824 * vdev, search a vdev with the same guid and top vdev id in the source. 1825 * Intended to copy paths from userland config into MOS config. 1826 */ 1827 void 1828 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 1829 { 1830 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 1831 ASSERT(srvd->vdev_ops == &vdev_root_ops); 1832 ASSERT(drvd->vdev_ops == &vdev_root_ops); 1833 1834 for (uint64_t i = 0; i < children; i++) { 1835 vdev_copy_path_search(srvd->vdev_child[i], 1836 drvd->vdev_child[i]); 1837 } 1838 } 1839 1840 /* 1841 * Close a virtual device. 1842 */ 1843 void 1844 vdev_close(vdev_t *vd) 1845 { 1846 spa_t *spa = vd->vdev_spa; 1847 vdev_t *pvd = vd->vdev_parent; 1848 1849 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1850 1851 /* 1852 * If our parent is reopening, then we are as well, unless we are 1853 * going offline. 1854 */ 1855 if (pvd != NULL && pvd->vdev_reopening) 1856 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1857 1858 vd->vdev_ops->vdev_op_close(vd); 1859 1860 vdev_cache_purge(vd); 1861 1862 /* 1863 * We record the previous state before we close it, so that if we are 1864 * doing a reopen(), we don't generate FMA ereports if we notice that 1865 * it's still faulted. 1866 */ 1867 vd->vdev_prevstate = vd->vdev_state; 1868 1869 if (vd->vdev_offline) 1870 vd->vdev_state = VDEV_STATE_OFFLINE; 1871 else 1872 vd->vdev_state = VDEV_STATE_CLOSED; 1873 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1874 } 1875 1876 void 1877 vdev_hold(vdev_t *vd) 1878 { 1879 spa_t *spa = vd->vdev_spa; 1880 1881 ASSERT(spa_is_root(spa)); 1882 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1883 return; 1884 1885 for (int c = 0; c < vd->vdev_children; c++) 1886 vdev_hold(vd->vdev_child[c]); 1887 1888 if (vd->vdev_ops->vdev_op_leaf) 1889 vd->vdev_ops->vdev_op_hold(vd); 1890 } 1891 1892 void 1893 vdev_rele(vdev_t *vd) 1894 { 1895 spa_t *spa = vd->vdev_spa; 1896 1897 ASSERT(spa_is_root(spa)); 1898 for (int c = 0; c < vd->vdev_children; c++) 1899 vdev_rele(vd->vdev_child[c]); 1900 1901 if (vd->vdev_ops->vdev_op_leaf) 1902 vd->vdev_ops->vdev_op_rele(vd); 1903 } 1904 1905 /* 1906 * Reopen all interior vdevs and any unopened leaves. We don't actually 1907 * reopen leaf vdevs which had previously been opened as they might deadlock 1908 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1909 * If the leaf has never been opened then open it, as usual. 1910 */ 1911 void 1912 vdev_reopen(vdev_t *vd) 1913 { 1914 spa_t *spa = vd->vdev_spa; 1915 1916 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1917 1918 /* set the reopening flag unless we're taking the vdev offline */ 1919 vd->vdev_reopening = !vd->vdev_offline; 1920 vdev_close(vd); 1921 (void) vdev_open(vd); 1922 1923 /* 1924 * Call vdev_validate() here to make sure we have the same device. 1925 * Otherwise, a device with an invalid label could be successfully 1926 * opened in response to vdev_reopen(). 1927 */ 1928 if (vd->vdev_aux) { 1929 (void) vdev_validate_aux(vd); 1930 if (vdev_readable(vd) && vdev_writeable(vd) && 1931 vd->vdev_aux == &spa->spa_l2cache && 1932 !l2arc_vdev_present(vd)) 1933 l2arc_add_vdev(spa, vd); 1934 } else { 1935 (void) vdev_validate(vd); 1936 } 1937 1938 /* 1939 * Reassess parent vdev's health. 1940 */ 1941 vdev_propagate_state(vd); 1942 } 1943 1944 int 1945 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1946 { 1947 int error; 1948 1949 /* 1950 * Normally, partial opens (e.g. of a mirror) are allowed. 1951 * For a create, however, we want to fail the request if 1952 * there are any components we can't open. 1953 */ 1954 error = vdev_open(vd); 1955 1956 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1957 vdev_close(vd); 1958 return (error ? error : ENXIO); 1959 } 1960 1961 /* 1962 * Recursively load DTLs and initialize all labels. 1963 */ 1964 if ((error = vdev_dtl_load(vd)) != 0 || 1965 (error = vdev_label_init(vd, txg, isreplacing ? 1966 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1967 vdev_close(vd); 1968 return (error); 1969 } 1970 1971 return (0); 1972 } 1973 1974 void 1975 vdev_metaslab_set_size(vdev_t *vd) 1976 { 1977 uint64_t asize = vd->vdev_asize; 1978 uint64_t ms_shift = 0; 1979 1980 /* 1981 * For vdevs that are bigger than 8G the metaslab size varies in 1982 * a way that the number of metaslabs increases in powers of two, 1983 * linearly in terms of vdev_asize, starting from 16 metaslabs. 1984 * So for vdev_asize of 8G we get 16 metaslabs, for 16G, we get 32, 1985 * and so on, until we hit the maximum metaslab count limit 1986 * [vdev_max_ms_count] from which point the metaslab count stays 1987 * the same. 1988 */ 1989 ms_shift = vdev_default_ms_shift; 1990 1991 if ((asize >> ms_shift) < vdev_min_ms_count) { 1992 /* 1993 * For devices that are less than 8G we want to have 1994 * exactly 16 metaslabs. We don't want less as integer 1995 * division rounds down, so less metaslabs mean more 1996 * wasted space. We don't want more as these vdevs are 1997 * small and in the likely event that we are running 1998 * out of space, the SPA will have a hard time finding 1999 * space due to fragmentation. 2000 */ 2001 ms_shift = highbit64(asize / vdev_min_ms_count); 2002 ms_shift = MAX(ms_shift, SPA_MAXBLOCKSHIFT); 2003 2004 } else if ((asize >> ms_shift) > vdev_max_ms_count) { 2005 ms_shift = highbit64(asize / vdev_max_ms_count); 2006 } 2007 2008 vd->vdev_ms_shift = ms_shift; 2009 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2010 } 2011 2012 void 2013 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2014 { 2015 ASSERT(vd == vd->vdev_top); 2016 /* indirect vdevs don't have metaslabs or dtls */ 2017 ASSERT(vdev_is_concrete(vd) || flags == 0); 2018 ASSERT(ISP2(flags)); 2019 ASSERT(spa_writeable(vd->vdev_spa)); 2020 2021 if (flags & VDD_METASLAB) 2022 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2023 2024 if (flags & VDD_DTL) 2025 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2026 2027 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2028 } 2029 2030 void 2031 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 2032 { 2033 for (int c = 0; c < vd->vdev_children; c++) 2034 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 2035 2036 if (vd->vdev_ops->vdev_op_leaf) 2037 vdev_dirty(vd->vdev_top, flags, vd, txg); 2038 } 2039 2040 /* 2041 * DTLs. 2042 * 2043 * A vdev's DTL (dirty time log) is the set of transaction groups for which 2044 * the vdev has less than perfect replication. There are four kinds of DTL: 2045 * 2046 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 2047 * 2048 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 2049 * 2050 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 2051 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 2052 * txgs that was scrubbed. 2053 * 2054 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 2055 * persistent errors or just some device being offline. 2056 * Unlike the other three, the DTL_OUTAGE map is not generally 2057 * maintained; it's only computed when needed, typically to 2058 * determine whether a device can be detached. 2059 * 2060 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 2061 * either has the data or it doesn't. 2062 * 2063 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 2064 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 2065 * if any child is less than fully replicated, then so is its parent. 2066 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 2067 * comprising only those txgs which appear in 'maxfaults' or more children; 2068 * those are the txgs we don't have enough replication to read. For example, 2069 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 2070 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 2071 * two child DTL_MISSING maps. 2072 * 2073 * It should be clear from the above that to compute the DTLs and outage maps 2074 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 2075 * Therefore, that is all we keep on disk. When loading the pool, or after 2076 * a configuration change, we generate all other DTLs from first principles. 2077 */ 2078 void 2079 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2080 { 2081 range_tree_t *rt = vd->vdev_dtl[t]; 2082 2083 ASSERT(t < DTL_TYPES); 2084 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2085 ASSERT(spa_writeable(vd->vdev_spa)); 2086 2087 mutex_enter(&vd->vdev_dtl_lock); 2088 if (!range_tree_contains(rt, txg, size)) 2089 range_tree_add(rt, txg, size); 2090 mutex_exit(&vd->vdev_dtl_lock); 2091 } 2092 2093 boolean_t 2094 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2095 { 2096 range_tree_t *rt = vd->vdev_dtl[t]; 2097 boolean_t dirty = B_FALSE; 2098 2099 ASSERT(t < DTL_TYPES); 2100 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2101 2102 /* 2103 * While we are loading the pool, the DTLs have not been loaded yet. 2104 * Ignore the DTLs and try all devices. This avoids a recursive 2105 * mutex enter on the vdev_dtl_lock, and also makes us try hard 2106 * when loading the pool (relying on the checksum to ensure that 2107 * we get the right data -- note that we while loading, we are 2108 * only reading the MOS, which is always checksummed). 2109 */ 2110 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 2111 return (B_FALSE); 2112 2113 mutex_enter(&vd->vdev_dtl_lock); 2114 if (!range_tree_is_empty(rt)) 2115 dirty = range_tree_contains(rt, txg, size); 2116 mutex_exit(&vd->vdev_dtl_lock); 2117 2118 return (dirty); 2119 } 2120 2121 boolean_t 2122 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 2123 { 2124 range_tree_t *rt = vd->vdev_dtl[t]; 2125 boolean_t empty; 2126 2127 mutex_enter(&vd->vdev_dtl_lock); 2128 empty = range_tree_is_empty(rt); 2129 mutex_exit(&vd->vdev_dtl_lock); 2130 2131 return (empty); 2132 } 2133 2134 /* 2135 * Returns the lowest txg in the DTL range. 2136 */ 2137 static uint64_t 2138 vdev_dtl_min(vdev_t *vd) 2139 { 2140 range_seg_t *rs; 2141 2142 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2143 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2144 ASSERT0(vd->vdev_children); 2145 2146 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 2147 return (rs->rs_start - 1); 2148 } 2149 2150 /* 2151 * Returns the highest txg in the DTL. 2152 */ 2153 static uint64_t 2154 vdev_dtl_max(vdev_t *vd) 2155 { 2156 range_seg_t *rs; 2157 2158 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2159 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2160 ASSERT0(vd->vdev_children); 2161 2162 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 2163 return (rs->rs_end); 2164 } 2165 2166 /* 2167 * Determine if a resilvering vdev should remove any DTL entries from 2168 * its range. If the vdev was resilvering for the entire duration of the 2169 * scan then it should excise that range from its DTLs. Otherwise, this 2170 * vdev is considered partially resilvered and should leave its DTL 2171 * entries intact. The comment in vdev_dtl_reassess() describes how we 2172 * excise the DTLs. 2173 */ 2174 static boolean_t 2175 vdev_dtl_should_excise(vdev_t *vd) 2176 { 2177 spa_t *spa = vd->vdev_spa; 2178 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2179 2180 ASSERT0(scn->scn_phys.scn_errors); 2181 ASSERT0(vd->vdev_children); 2182 2183 if (vd->vdev_state < VDEV_STATE_DEGRADED) 2184 return (B_FALSE); 2185 2186 if (vd->vdev_resilver_txg == 0 || 2187 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2188 return (B_TRUE); 2189 2190 /* 2191 * When a resilver is initiated the scan will assign the scn_max_txg 2192 * value to the highest txg value that exists in all DTLs. If this 2193 * device's max DTL is not part of this scan (i.e. it is not in 2194 * the range (scn_min_txg, scn_max_txg] then it is not eligible 2195 * for excision. 2196 */ 2197 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2198 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2199 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2200 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2201 return (B_TRUE); 2202 } 2203 return (B_FALSE); 2204 } 2205 2206 /* 2207 * Reassess DTLs after a config change or scrub completion. 2208 */ 2209 void 2210 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2211 { 2212 spa_t *spa = vd->vdev_spa; 2213 avl_tree_t reftree; 2214 int minref; 2215 2216 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2217 2218 for (int c = 0; c < vd->vdev_children; c++) 2219 vdev_dtl_reassess(vd->vdev_child[c], txg, 2220 scrub_txg, scrub_done); 2221 2222 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 2223 return; 2224 2225 if (vd->vdev_ops->vdev_op_leaf) { 2226 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2227 2228 mutex_enter(&vd->vdev_dtl_lock); 2229 2230 /* 2231 * If we've completed a scan cleanly then determine 2232 * if this vdev should remove any DTLs. We only want to 2233 * excise regions on vdevs that were available during 2234 * the entire duration of this scan. 2235 */ 2236 if (scrub_txg != 0 && 2237 (spa->spa_scrub_started || 2238 (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2239 vdev_dtl_should_excise(vd)) { 2240 /* 2241 * We completed a scrub up to scrub_txg. If we 2242 * did it without rebooting, then the scrub dtl 2243 * will be valid, so excise the old region and 2244 * fold in the scrub dtl. Otherwise, leave the 2245 * dtl as-is if there was an error. 2246 * 2247 * There's little trick here: to excise the beginning 2248 * of the DTL_MISSING map, we put it into a reference 2249 * tree and then add a segment with refcnt -1 that 2250 * covers the range [0, scrub_txg). This means 2251 * that each txg in that range has refcnt -1 or 0. 2252 * We then add DTL_SCRUB with a refcnt of 2, so that 2253 * entries in the range [0, scrub_txg) will have a 2254 * positive refcnt -- either 1 or 2. We then convert 2255 * the reference tree into the new DTL_MISSING map. 2256 */ 2257 space_reftree_create(&reftree); 2258 space_reftree_add_map(&reftree, 2259 vd->vdev_dtl[DTL_MISSING], 1); 2260 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 2261 space_reftree_add_map(&reftree, 2262 vd->vdev_dtl[DTL_SCRUB], 2); 2263 space_reftree_generate_map(&reftree, 2264 vd->vdev_dtl[DTL_MISSING], 1); 2265 space_reftree_destroy(&reftree); 2266 } 2267 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 2268 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2269 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2270 if (scrub_done) 2271 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 2272 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 2273 if (!vdev_readable(vd)) 2274 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 2275 else 2276 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2277 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2278 2279 /* 2280 * If the vdev was resilvering and no longer has any 2281 * DTLs then reset its resilvering flag. 2282 */ 2283 if (vd->vdev_resilver_txg != 0 && 2284 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 2285 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2286 vd->vdev_resilver_txg = 0; 2287 2288 mutex_exit(&vd->vdev_dtl_lock); 2289 2290 if (txg != 0) 2291 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2292 return; 2293 } 2294 2295 mutex_enter(&vd->vdev_dtl_lock); 2296 for (int t = 0; t < DTL_TYPES; t++) { 2297 /* account for child's outage in parent's missing map */ 2298 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 2299 if (t == DTL_SCRUB) 2300 continue; /* leaf vdevs only */ 2301 if (t == DTL_PARTIAL) 2302 minref = 1; /* i.e. non-zero */ 2303 else if (vd->vdev_nparity != 0) 2304 minref = vd->vdev_nparity + 1; /* RAID-Z */ 2305 else 2306 minref = vd->vdev_children; /* any kind of mirror */ 2307 space_reftree_create(&reftree); 2308 for (int c = 0; c < vd->vdev_children; c++) { 2309 vdev_t *cvd = vd->vdev_child[c]; 2310 mutex_enter(&cvd->vdev_dtl_lock); 2311 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 2312 mutex_exit(&cvd->vdev_dtl_lock); 2313 } 2314 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 2315 space_reftree_destroy(&reftree); 2316 } 2317 mutex_exit(&vd->vdev_dtl_lock); 2318 } 2319 2320 int 2321 vdev_dtl_load(vdev_t *vd) 2322 { 2323 spa_t *spa = vd->vdev_spa; 2324 objset_t *mos = spa->spa_meta_objset; 2325 int error = 0; 2326 2327 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 2328 ASSERT(vdev_is_concrete(vd)); 2329 2330 error = space_map_open(&vd->vdev_dtl_sm, mos, 2331 vd->vdev_dtl_object, 0, -1ULL, 0); 2332 if (error) 2333 return (error); 2334 ASSERT(vd->vdev_dtl_sm != NULL); 2335 2336 mutex_enter(&vd->vdev_dtl_lock); 2337 2338 /* 2339 * Now that we've opened the space_map we need to update 2340 * the in-core DTL. 2341 */ 2342 space_map_update(vd->vdev_dtl_sm); 2343 2344 error = space_map_load(vd->vdev_dtl_sm, 2345 vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 2346 mutex_exit(&vd->vdev_dtl_lock); 2347 2348 return (error); 2349 } 2350 2351 for (int c = 0; c < vd->vdev_children; c++) { 2352 error = vdev_dtl_load(vd->vdev_child[c]); 2353 if (error != 0) 2354 break; 2355 } 2356 2357 return (error); 2358 } 2359 2360 void 2361 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2362 { 2363 spa_t *spa = vd->vdev_spa; 2364 2365 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2366 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2367 zapobj, tx)); 2368 } 2369 2370 uint64_t 2371 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2372 { 2373 spa_t *spa = vd->vdev_spa; 2374 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2375 DMU_OT_NONE, 0, tx); 2376 2377 ASSERT(zap != 0); 2378 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2379 zap, tx)); 2380 2381 return (zap); 2382 } 2383 2384 void 2385 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2386 { 2387 if (vd->vdev_ops != &vdev_hole_ops && 2388 vd->vdev_ops != &vdev_missing_ops && 2389 vd->vdev_ops != &vdev_root_ops && 2390 !vd->vdev_top->vdev_removing) { 2391 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2392 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2393 } 2394 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2395 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2396 } 2397 } 2398 for (uint64_t i = 0; i < vd->vdev_children; i++) { 2399 vdev_construct_zaps(vd->vdev_child[i], tx); 2400 } 2401 } 2402 2403 void 2404 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2405 { 2406 spa_t *spa = vd->vdev_spa; 2407 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2408 objset_t *mos = spa->spa_meta_objset; 2409 range_tree_t *rtsync; 2410 dmu_tx_t *tx; 2411 uint64_t object = space_map_object(vd->vdev_dtl_sm); 2412 2413 ASSERT(vdev_is_concrete(vd)); 2414 ASSERT(vd->vdev_ops->vdev_op_leaf); 2415 2416 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2417 2418 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2419 mutex_enter(&vd->vdev_dtl_lock); 2420 space_map_free(vd->vdev_dtl_sm, tx); 2421 space_map_close(vd->vdev_dtl_sm); 2422 vd->vdev_dtl_sm = NULL; 2423 mutex_exit(&vd->vdev_dtl_lock); 2424 2425 /* 2426 * We only destroy the leaf ZAP for detached leaves or for 2427 * removed log devices. Removed data devices handle leaf ZAP 2428 * cleanup later, once cancellation is no longer possible. 2429 */ 2430 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2431 vd->vdev_top->vdev_islog)) { 2432 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2433 vd->vdev_leaf_zap = 0; 2434 } 2435 2436 dmu_tx_commit(tx); 2437 return; 2438 } 2439 2440 if (vd->vdev_dtl_sm == NULL) { 2441 uint64_t new_object; 2442 2443 new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 2444 VERIFY3U(new_object, !=, 0); 2445 2446 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2447 0, -1ULL, 0)); 2448 ASSERT(vd->vdev_dtl_sm != NULL); 2449 } 2450 2451 rtsync = range_tree_create(NULL, NULL); 2452 2453 mutex_enter(&vd->vdev_dtl_lock); 2454 range_tree_walk(rt, range_tree_add, rtsync); 2455 mutex_exit(&vd->vdev_dtl_lock); 2456 2457 space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 2458 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx); 2459 range_tree_vacate(rtsync, NULL, NULL); 2460 2461 range_tree_destroy(rtsync); 2462 2463 /* 2464 * If the object for the space map has changed then dirty 2465 * the top level so that we update the config. 2466 */ 2467 if (object != space_map_object(vd->vdev_dtl_sm)) { 2468 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 2469 "new object %llu", (u_longlong_t)txg, spa_name(spa), 2470 (u_longlong_t)object, 2471 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 2472 vdev_config_dirty(vd->vdev_top); 2473 } 2474 2475 dmu_tx_commit(tx); 2476 2477 mutex_enter(&vd->vdev_dtl_lock); 2478 space_map_update(vd->vdev_dtl_sm); 2479 mutex_exit(&vd->vdev_dtl_lock); 2480 } 2481 2482 /* 2483 * Determine whether the specified vdev can be offlined/detached/removed 2484 * without losing data. 2485 */ 2486 boolean_t 2487 vdev_dtl_required(vdev_t *vd) 2488 { 2489 spa_t *spa = vd->vdev_spa; 2490 vdev_t *tvd = vd->vdev_top; 2491 uint8_t cant_read = vd->vdev_cant_read; 2492 boolean_t required; 2493 2494 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2495 2496 if (vd == spa->spa_root_vdev || vd == tvd) 2497 return (B_TRUE); 2498 2499 /* 2500 * Temporarily mark the device as unreadable, and then determine 2501 * whether this results in any DTL outages in the top-level vdev. 2502 * If not, we can safely offline/detach/remove the device. 2503 */ 2504 vd->vdev_cant_read = B_TRUE; 2505 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2506 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2507 vd->vdev_cant_read = cant_read; 2508 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2509 2510 if (!required && zio_injection_enabled) 2511 required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2512 2513 return (required); 2514 } 2515 2516 /* 2517 * Determine if resilver is needed, and if so the txg range. 2518 */ 2519 boolean_t 2520 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2521 { 2522 boolean_t needed = B_FALSE; 2523 uint64_t thismin = UINT64_MAX; 2524 uint64_t thismax = 0; 2525 2526 if (vd->vdev_children == 0) { 2527 mutex_enter(&vd->vdev_dtl_lock); 2528 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 2529 vdev_writeable(vd)) { 2530 2531 thismin = vdev_dtl_min(vd); 2532 thismax = vdev_dtl_max(vd); 2533 needed = B_TRUE; 2534 } 2535 mutex_exit(&vd->vdev_dtl_lock); 2536 } else { 2537 for (int c = 0; c < vd->vdev_children; c++) { 2538 vdev_t *cvd = vd->vdev_child[c]; 2539 uint64_t cmin, cmax; 2540 2541 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2542 thismin = MIN(thismin, cmin); 2543 thismax = MAX(thismax, cmax); 2544 needed = B_TRUE; 2545 } 2546 } 2547 } 2548 2549 if (needed && minp) { 2550 *minp = thismin; 2551 *maxp = thismax; 2552 } 2553 return (needed); 2554 } 2555 2556 /* 2557 * Gets the checkpoint space map object from the vdev's ZAP. 2558 * Returns the spacemap object, or 0 if it wasn't in the ZAP 2559 * or the ZAP doesn't exist yet. 2560 */ 2561 int 2562 vdev_checkpoint_sm_object(vdev_t *vd) 2563 { 2564 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 2565 if (vd->vdev_top_zap == 0) { 2566 return (0); 2567 } 2568 2569 uint64_t sm_obj = 0; 2570 int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 2571 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 2572 2573 ASSERT(err == 0 || err == ENOENT); 2574 2575 return (sm_obj); 2576 } 2577 2578 int 2579 vdev_load(vdev_t *vd) 2580 { 2581 int error = 0; 2582 /* 2583 * Recursively load all children. 2584 */ 2585 for (int c = 0; c < vd->vdev_children; c++) { 2586 error = vdev_load(vd->vdev_child[c]); 2587 if (error != 0) { 2588 return (error); 2589 } 2590 } 2591 2592 vdev_set_deflate_ratio(vd); 2593 2594 /* 2595 * If this is a top-level vdev, initialize its metaslabs. 2596 */ 2597 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2598 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 2599 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2600 VDEV_AUX_CORRUPT_DATA); 2601 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 2602 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 2603 (u_longlong_t)vd->vdev_asize); 2604 return (SET_ERROR(ENXIO)); 2605 } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 2606 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 2607 "[error=%d]", error); 2608 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2609 VDEV_AUX_CORRUPT_DATA); 2610 return (error); 2611 } 2612 2613 uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 2614 if (checkpoint_sm_obj != 0) { 2615 objset_t *mos = spa_meta_objset(vd->vdev_spa); 2616 ASSERT(vd->vdev_asize != 0); 2617 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 2618 2619 if ((error = space_map_open(&vd->vdev_checkpoint_sm, 2620 mos, checkpoint_sm_obj, 0, vd->vdev_asize, 2621 vd->vdev_ashift))) { 2622 vdev_dbgmsg(vd, "vdev_load: space_map_open " 2623 "failed for checkpoint spacemap (obj %llu) " 2624 "[error=%d]", 2625 (u_longlong_t)checkpoint_sm_obj, error); 2626 return (error); 2627 } 2628 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 2629 space_map_update(vd->vdev_checkpoint_sm); 2630 2631 /* 2632 * Since the checkpoint_sm contains free entries 2633 * exclusively we can use sm_alloc to indicate the 2634 * culmulative checkpointed space that has been freed. 2635 */ 2636 vd->vdev_stat.vs_checkpoint_space = 2637 -vd->vdev_checkpoint_sm->sm_alloc; 2638 vd->vdev_spa->spa_checkpoint_info.sci_dspace += 2639 vd->vdev_stat.vs_checkpoint_space; 2640 } 2641 } 2642 2643 /* 2644 * If this is a leaf vdev, load its DTL. 2645 */ 2646 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2647 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2648 VDEV_AUX_CORRUPT_DATA); 2649 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 2650 "[error=%d]", error); 2651 return (error); 2652 } 2653 2654 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 2655 if (obsolete_sm_object != 0) { 2656 objset_t *mos = vd->vdev_spa->spa_meta_objset; 2657 ASSERT(vd->vdev_asize != 0); 2658 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 2659 2660 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 2661 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 2662 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2663 VDEV_AUX_CORRUPT_DATA); 2664 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 2665 "obsolete spacemap (obj %llu) [error=%d]", 2666 (u_longlong_t)obsolete_sm_object, error); 2667 return (error); 2668 } 2669 space_map_update(vd->vdev_obsolete_sm); 2670 } 2671 2672 return (0); 2673 } 2674 2675 /* 2676 * The special vdev case is used for hot spares and l2cache devices. Its 2677 * sole purpose it to set the vdev state for the associated vdev. To do this, 2678 * we make sure that we can open the underlying device, then try to read the 2679 * label, and make sure that the label is sane and that it hasn't been 2680 * repurposed to another pool. 2681 */ 2682 int 2683 vdev_validate_aux(vdev_t *vd) 2684 { 2685 nvlist_t *label; 2686 uint64_t guid, version; 2687 uint64_t state; 2688 2689 if (!vdev_readable(vd)) 2690 return (0); 2691 2692 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 2693 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2694 VDEV_AUX_CORRUPT_DATA); 2695 return (-1); 2696 } 2697 2698 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2699 !SPA_VERSION_IS_SUPPORTED(version) || 2700 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 2701 guid != vd->vdev_guid || 2702 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 2703 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2704 VDEV_AUX_CORRUPT_DATA); 2705 nvlist_free(label); 2706 return (-1); 2707 } 2708 2709 /* 2710 * We don't actually check the pool state here. If it's in fact in 2711 * use by another pool, we update this fact on the fly when requested. 2712 */ 2713 nvlist_free(label); 2714 return (0); 2715 } 2716 2717 /* 2718 * Free the objects used to store this vdev's spacemaps, and the array 2719 * that points to them. 2720 */ 2721 void 2722 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 2723 { 2724 if (vd->vdev_ms_array == 0) 2725 return; 2726 2727 objset_t *mos = vd->vdev_spa->spa_meta_objset; 2728 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 2729 size_t array_bytes = array_count * sizeof (uint64_t); 2730 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 2731 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 2732 array_bytes, smobj_array, 0)); 2733 2734 for (uint64_t i = 0; i < array_count; i++) { 2735 uint64_t smobj = smobj_array[i]; 2736 if (smobj == 0) 2737 continue; 2738 2739 space_map_free_obj(mos, smobj, tx); 2740 } 2741 2742 kmem_free(smobj_array, array_bytes); 2743 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 2744 vd->vdev_ms_array = 0; 2745 } 2746 2747 static void 2748 vdev_remove_empty(vdev_t *vd, uint64_t txg) 2749 { 2750 spa_t *spa = vd->vdev_spa; 2751 dmu_tx_t *tx; 2752 2753 ASSERT(vd == vd->vdev_top); 2754 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 2755 2756 if (vd->vdev_ms != NULL) { 2757 metaslab_group_t *mg = vd->vdev_mg; 2758 2759 metaslab_group_histogram_verify(mg); 2760 metaslab_class_histogram_verify(mg->mg_class); 2761 2762 for (int m = 0; m < vd->vdev_ms_count; m++) { 2763 metaslab_t *msp = vd->vdev_ms[m]; 2764 2765 if (msp == NULL || msp->ms_sm == NULL) 2766 continue; 2767 2768 mutex_enter(&msp->ms_lock); 2769 /* 2770 * If the metaslab was not loaded when the vdev 2771 * was removed then the histogram accounting may 2772 * not be accurate. Update the histogram information 2773 * here so that we ensure that the metaslab group 2774 * and metaslab class are up-to-date. 2775 */ 2776 metaslab_group_histogram_remove(mg, msp); 2777 2778 VERIFY0(space_map_allocated(msp->ms_sm)); 2779 space_map_close(msp->ms_sm); 2780 msp->ms_sm = NULL; 2781 mutex_exit(&msp->ms_lock); 2782 } 2783 2784 if (vd->vdev_checkpoint_sm != NULL) { 2785 ASSERT(spa_has_checkpoint(spa)); 2786 space_map_close(vd->vdev_checkpoint_sm); 2787 vd->vdev_checkpoint_sm = NULL; 2788 } 2789 2790 metaslab_group_histogram_verify(mg); 2791 metaslab_class_histogram_verify(mg->mg_class); 2792 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 2793 ASSERT0(mg->mg_histogram[i]); 2794 } 2795 2796 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2797 vdev_destroy_spacemaps(vd, tx); 2798 2799 if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2800 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2801 vd->vdev_top_zap = 0; 2802 } 2803 dmu_tx_commit(tx); 2804 } 2805 2806 void 2807 vdev_sync_done(vdev_t *vd, uint64_t txg) 2808 { 2809 metaslab_t *msp; 2810 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2811 2812 ASSERT(vdev_is_concrete(vd)); 2813 2814 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2815 metaslab_sync_done(msp, txg); 2816 2817 if (reassess) 2818 metaslab_sync_reassess(vd->vdev_mg); 2819 } 2820 2821 void 2822 vdev_sync(vdev_t *vd, uint64_t txg) 2823 { 2824 spa_t *spa = vd->vdev_spa; 2825 vdev_t *lvd; 2826 metaslab_t *msp; 2827 dmu_tx_t *tx; 2828 2829 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 2830 dmu_tx_t *tx; 2831 2832 ASSERT(vd->vdev_removing || 2833 vd->vdev_ops == &vdev_indirect_ops); 2834 2835 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2836 vdev_indirect_sync_obsolete(vd, tx); 2837 dmu_tx_commit(tx); 2838 2839 /* 2840 * If the vdev is indirect, it can't have dirty 2841 * metaslabs or DTLs. 2842 */ 2843 if (vd->vdev_ops == &vdev_indirect_ops) { 2844 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 2845 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 2846 return; 2847 } 2848 } 2849 2850 ASSERT(vdev_is_concrete(vd)); 2851 2852 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 2853 !vd->vdev_removing) { 2854 ASSERT(vd == vd->vdev_top); 2855 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 2856 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2857 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2858 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2859 ASSERT(vd->vdev_ms_array != 0); 2860 vdev_config_dirty(vd); 2861 dmu_tx_commit(tx); 2862 } 2863 2864 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2865 metaslab_sync(msp, txg); 2866 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2867 } 2868 2869 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2870 vdev_dtl_sync(lvd, txg); 2871 2872 /* 2873 * Remove the metadata associated with this vdev once it's empty. 2874 * Note that this is typically used for log/cache device removal; 2875 * we don't empty toplevel vdevs when removing them. But if 2876 * a toplevel happens to be emptied, this is not harmful. 2877 */ 2878 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) { 2879 vdev_remove_empty(vd, txg); 2880 } 2881 2882 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2883 } 2884 2885 uint64_t 2886 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2887 { 2888 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2889 } 2890 2891 /* 2892 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 2893 * not be opened, and no I/O is attempted. 2894 */ 2895 int 2896 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2897 { 2898 vdev_t *vd, *tvd; 2899 2900 spa_vdev_state_enter(spa, SCL_NONE); 2901 2902 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2903 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2904 2905 if (!vd->vdev_ops->vdev_op_leaf) 2906 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2907 2908 tvd = vd->vdev_top; 2909 2910 /* 2911 * We don't directly use the aux state here, but if we do a 2912 * vdev_reopen(), we need this value to be present to remember why we 2913 * were faulted. 2914 */ 2915 vd->vdev_label_aux = aux; 2916 2917 /* 2918 * Faulted state takes precedence over degraded. 2919 */ 2920 vd->vdev_delayed_close = B_FALSE; 2921 vd->vdev_faulted = 1ULL; 2922 vd->vdev_degraded = 0ULL; 2923 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 2924 2925 /* 2926 * If this device has the only valid copy of the data, then 2927 * back off and simply mark the vdev as degraded instead. 2928 */ 2929 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 2930 vd->vdev_degraded = 1ULL; 2931 vd->vdev_faulted = 0ULL; 2932 2933 /* 2934 * If we reopen the device and it's not dead, only then do we 2935 * mark it degraded. 2936 */ 2937 vdev_reopen(tvd); 2938 2939 if (vdev_readable(vd)) 2940 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 2941 } 2942 2943 return (spa_vdev_state_exit(spa, vd, 0)); 2944 } 2945 2946 /* 2947 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 2948 * user that something is wrong. The vdev continues to operate as normal as far 2949 * as I/O is concerned. 2950 */ 2951 int 2952 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2953 { 2954 vdev_t *vd; 2955 2956 spa_vdev_state_enter(spa, SCL_NONE); 2957 2958 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2959 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2960 2961 if (!vd->vdev_ops->vdev_op_leaf) 2962 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2963 2964 /* 2965 * If the vdev is already faulted, then don't do anything. 2966 */ 2967 if (vd->vdev_faulted || vd->vdev_degraded) 2968 return (spa_vdev_state_exit(spa, NULL, 0)); 2969 2970 vd->vdev_degraded = 1ULL; 2971 if (!vdev_is_dead(vd)) 2972 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2973 aux); 2974 2975 return (spa_vdev_state_exit(spa, vd, 0)); 2976 } 2977 2978 /* 2979 * Online the given vdev. 2980 * 2981 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2982 * spare device should be detached when the device finishes resilvering. 2983 * Second, the online should be treated like a 'test' online case, so no FMA 2984 * events are generated if the device fails to open. 2985 */ 2986 int 2987 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2988 { 2989 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2990 boolean_t wasoffline; 2991 vdev_state_t oldstate; 2992 2993 spa_vdev_state_enter(spa, SCL_NONE); 2994 2995 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2996 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2997 2998 if (!vd->vdev_ops->vdev_op_leaf) 2999 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3000 3001 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 3002 oldstate = vd->vdev_state; 3003 3004 tvd = vd->vdev_top; 3005 vd->vdev_offline = B_FALSE; 3006 vd->vdev_tmpoffline = B_FALSE; 3007 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3008 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3009 3010 /* XXX - L2ARC 1.0 does not support expansion */ 3011 if (!vd->vdev_aux) { 3012 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3013 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3014 } 3015 3016 vdev_reopen(tvd); 3017 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 3018 3019 if (!vd->vdev_aux) { 3020 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3021 pvd->vdev_expanding = B_FALSE; 3022 } 3023 3024 if (newstate) 3025 *newstate = vd->vdev_state; 3026 if ((flags & ZFS_ONLINE_UNSPARE) && 3027 !vdev_is_dead(vd) && vd->vdev_parent && 3028 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3029 vd->vdev_parent->vdev_child[0] == vd) 3030 vd->vdev_unspare = B_TRUE; 3031 3032 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3033 3034 /* XXX - L2ARC 1.0 does not support expansion */ 3035 if (vd->vdev_aux) 3036 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3037 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3038 } 3039 3040 if (wasoffline || 3041 (oldstate < VDEV_STATE_DEGRADED && 3042 vd->vdev_state >= VDEV_STATE_DEGRADED)) 3043 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 3044 3045 return (spa_vdev_state_exit(spa, vd, 0)); 3046 } 3047 3048 static int 3049 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3050 { 3051 vdev_t *vd, *tvd; 3052 int error = 0; 3053 uint64_t generation; 3054 metaslab_group_t *mg; 3055 3056 top: 3057 spa_vdev_state_enter(spa, SCL_ALLOC); 3058 3059 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3060 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3061 3062 if (!vd->vdev_ops->vdev_op_leaf) 3063 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3064 3065 tvd = vd->vdev_top; 3066 mg = tvd->vdev_mg; 3067 generation = spa->spa_config_generation + 1; 3068 3069 /* 3070 * If the device isn't already offline, try to offline it. 3071 */ 3072 if (!vd->vdev_offline) { 3073 /* 3074 * If this device has the only valid copy of some data, 3075 * don't allow it to be offlined. Log devices are always 3076 * expendable. 3077 */ 3078 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3079 vdev_dtl_required(vd)) 3080 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3081 3082 /* 3083 * If the top-level is a slog and it has had allocations 3084 * then proceed. We check that the vdev's metaslab group 3085 * is not NULL since it's possible that we may have just 3086 * added this vdev but not yet initialized its metaslabs. 3087 */ 3088 if (tvd->vdev_islog && mg != NULL) { 3089 /* 3090 * Prevent any future allocations. 3091 */ 3092 metaslab_group_passivate(mg); 3093 (void) spa_vdev_state_exit(spa, vd, 0); 3094 3095 error = spa_reset_logs(spa); 3096 3097 /* 3098 * If the log device was successfully reset but has 3099 * checkpointed data, do not offline it. 3100 */ 3101 if (error == 0 && 3102 tvd->vdev_checkpoint_sm != NULL) { 3103 ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, 3104 !=, 0); 3105 error = ZFS_ERR_CHECKPOINT_EXISTS; 3106 } 3107 3108 spa_vdev_state_enter(spa, SCL_ALLOC); 3109 3110 /* 3111 * Check to see if the config has changed. 3112 */ 3113 if (error || generation != spa->spa_config_generation) { 3114 metaslab_group_activate(mg); 3115 if (error) 3116 return (spa_vdev_state_exit(spa, 3117 vd, error)); 3118 (void) spa_vdev_state_exit(spa, vd, 0); 3119 goto top; 3120 } 3121 ASSERT0(tvd->vdev_stat.vs_alloc); 3122 } 3123 3124 /* 3125 * Offline this device and reopen its top-level vdev. 3126 * If the top-level vdev is a log device then just offline 3127 * it. Otherwise, if this action results in the top-level 3128 * vdev becoming unusable, undo it and fail the request. 3129 */ 3130 vd->vdev_offline = B_TRUE; 3131 vdev_reopen(tvd); 3132 3133 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3134 vdev_is_dead(tvd)) { 3135 vd->vdev_offline = B_FALSE; 3136 vdev_reopen(tvd); 3137 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3138 } 3139 3140 /* 3141 * Add the device back into the metaslab rotor so that 3142 * once we online the device it's open for business. 3143 */ 3144 if (tvd->vdev_islog && mg != NULL) 3145 metaslab_group_activate(mg); 3146 } 3147 3148 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3149 3150 return (spa_vdev_state_exit(spa, vd, 0)); 3151 } 3152 3153 int 3154 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3155 { 3156 int error; 3157 3158 mutex_enter(&spa->spa_vdev_top_lock); 3159 error = vdev_offline_locked(spa, guid, flags); 3160 mutex_exit(&spa->spa_vdev_top_lock); 3161 3162 return (error); 3163 } 3164 3165 /* 3166 * Clear the error counts associated with this vdev. Unlike vdev_online() and 3167 * vdev_offline(), we assume the spa config is locked. We also clear all 3168 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3169 */ 3170 void 3171 vdev_clear(spa_t *spa, vdev_t *vd) 3172 { 3173 vdev_t *rvd = spa->spa_root_vdev; 3174 3175 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3176 3177 if (vd == NULL) 3178 vd = rvd; 3179 3180 vd->vdev_stat.vs_read_errors = 0; 3181 vd->vdev_stat.vs_write_errors = 0; 3182 vd->vdev_stat.vs_checksum_errors = 0; 3183 3184 for (int c = 0; c < vd->vdev_children; c++) 3185 vdev_clear(spa, vd->vdev_child[c]); 3186 3187 /* 3188 * It makes no sense to "clear" an indirect vdev. 3189 */ 3190 if (!vdev_is_concrete(vd)) 3191 return; 3192 3193 /* 3194 * If we're in the FAULTED state or have experienced failed I/O, then 3195 * clear the persistent state and attempt to reopen the device. We 3196 * also mark the vdev config dirty, so that the new faulted state is 3197 * written out to disk. 3198 */ 3199 if (vd->vdev_faulted || vd->vdev_degraded || 3200 !vdev_readable(vd) || !vdev_writeable(vd)) { 3201 3202 /* 3203 * When reopening in reponse to a clear event, it may be due to 3204 * a fmadm repair request. In this case, if the device is 3205 * still broken, we want to still post the ereport again. 3206 */ 3207 vd->vdev_forcefault = B_TRUE; 3208 3209 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3210 vd->vdev_cant_read = B_FALSE; 3211 vd->vdev_cant_write = B_FALSE; 3212 3213 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 3214 3215 vd->vdev_forcefault = B_FALSE; 3216 3217 if (vd != rvd && vdev_writeable(vd->vdev_top)) 3218 vdev_state_dirty(vd->vdev_top); 3219 3220 if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3221 spa_async_request(spa, SPA_ASYNC_RESILVER); 3222 3223 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 3224 } 3225 3226 /* 3227 * When clearing a FMA-diagnosed fault, we always want to 3228 * unspare the device, as we assume that the original spare was 3229 * done in response to the FMA fault. 3230 */ 3231 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3232 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3233 vd->vdev_parent->vdev_child[0] == vd) 3234 vd->vdev_unspare = B_TRUE; 3235 } 3236 3237 boolean_t 3238 vdev_is_dead(vdev_t *vd) 3239 { 3240 /* 3241 * Holes and missing devices are always considered "dead". 3242 * This simplifies the code since we don't have to check for 3243 * these types of devices in the various code paths. 3244 * Instead we rely on the fact that we skip over dead devices 3245 * before issuing I/O to them. 3246 */ 3247 return (vd->vdev_state < VDEV_STATE_DEGRADED || 3248 vd->vdev_ops == &vdev_hole_ops || 3249 vd->vdev_ops == &vdev_missing_ops); 3250 } 3251 3252 boolean_t 3253 vdev_readable(vdev_t *vd) 3254 { 3255 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 3256 } 3257 3258 boolean_t 3259 vdev_writeable(vdev_t *vd) 3260 { 3261 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 3262 vdev_is_concrete(vd)); 3263 } 3264 3265 boolean_t 3266 vdev_allocatable(vdev_t *vd) 3267 { 3268 uint64_t state = vd->vdev_state; 3269 3270 /* 3271 * We currently allow allocations from vdevs which may be in the 3272 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3273 * fails to reopen then we'll catch it later when we're holding 3274 * the proper locks. Note that we have to get the vdev state 3275 * in a local variable because although it changes atomically, 3276 * we're asking two separate questions about it. 3277 */ 3278 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 3279 !vd->vdev_cant_write && vdev_is_concrete(vd) && 3280 vd->vdev_mg->mg_initialized); 3281 } 3282 3283 boolean_t 3284 vdev_accessible(vdev_t *vd, zio_t *zio) 3285 { 3286 ASSERT(zio->io_vd == vd); 3287 3288 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3289 return (B_FALSE); 3290 3291 if (zio->io_type == ZIO_TYPE_READ) 3292 return (!vd->vdev_cant_read); 3293 3294 if (zio->io_type == ZIO_TYPE_WRITE) 3295 return (!vd->vdev_cant_write); 3296 3297 return (B_TRUE); 3298 } 3299 3300 boolean_t 3301 vdev_is_spacemap_addressable(vdev_t *vd) 3302 { 3303 /* 3304 * Assuming 47 bits of the space map entry dedicated for the entry's 3305 * offset (see description in space_map.h), we calculate the maximum 3306 * address that can be described by a space map entry for the given 3307 * device. 3308 */ 3309 uint64_t shift = vd->vdev_ashift + 47; 3310 3311 if (shift >= 63) /* detect potential overflow */ 3312 return (B_TRUE); 3313 3314 return (vd->vdev_asize < (1ULL << shift)); 3315 } 3316 3317 /* 3318 * Get statistics for the given vdev. 3319 */ 3320 void 3321 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3322 { 3323 spa_t *spa = vd->vdev_spa; 3324 vdev_t *rvd = spa->spa_root_vdev; 3325 vdev_t *tvd = vd->vdev_top; 3326 3327 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3328 3329 mutex_enter(&vd->vdev_stat_lock); 3330 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3331 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3332 vs->vs_state = vd->vdev_state; 3333 vs->vs_rsize = vdev_get_min_asize(vd); 3334 if (vd->vdev_ops->vdev_op_leaf) 3335 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3336 /* 3337 * Report expandable space on top-level, non-auxillary devices only. 3338 * The expandable space is reported in terms of metaslab sized units 3339 * since that determines how much space the pool can expand. 3340 */ 3341 if (vd->vdev_aux == NULL && tvd != NULL) { 3342 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 3343 spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3344 } 3345 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 3346 vdev_is_concrete(vd)) { 3347 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 3348 } 3349 3350 /* 3351 * If we're getting stats on the root vdev, aggregate the I/O counts 3352 * over all top-level vdevs (i.e. the direct children of the root). 3353 */ 3354 if (vd == rvd) { 3355 for (int c = 0; c < rvd->vdev_children; c++) { 3356 vdev_t *cvd = rvd->vdev_child[c]; 3357 vdev_stat_t *cvs = &cvd->vdev_stat; 3358 3359 for (int t = 0; t < ZIO_TYPES; t++) { 3360 vs->vs_ops[t] += cvs->vs_ops[t]; 3361 vs->vs_bytes[t] += cvs->vs_bytes[t]; 3362 } 3363 cvs->vs_scan_removing = cvd->vdev_removing; 3364 } 3365 } 3366 mutex_exit(&vd->vdev_stat_lock); 3367 } 3368 3369 void 3370 vdev_clear_stats(vdev_t *vd) 3371 { 3372 mutex_enter(&vd->vdev_stat_lock); 3373 vd->vdev_stat.vs_space = 0; 3374 vd->vdev_stat.vs_dspace = 0; 3375 vd->vdev_stat.vs_alloc = 0; 3376 mutex_exit(&vd->vdev_stat_lock); 3377 } 3378 3379 void 3380 vdev_scan_stat_init(vdev_t *vd) 3381 { 3382 vdev_stat_t *vs = &vd->vdev_stat; 3383 3384 for (int c = 0; c < vd->vdev_children; c++) 3385 vdev_scan_stat_init(vd->vdev_child[c]); 3386 3387 mutex_enter(&vd->vdev_stat_lock); 3388 vs->vs_scan_processed = 0; 3389 mutex_exit(&vd->vdev_stat_lock); 3390 } 3391 3392 void 3393 vdev_stat_update(zio_t *zio, uint64_t psize) 3394 { 3395 spa_t *spa = zio->io_spa; 3396 vdev_t *rvd = spa->spa_root_vdev; 3397 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3398 vdev_t *pvd; 3399 uint64_t txg = zio->io_txg; 3400 vdev_stat_t *vs = &vd->vdev_stat; 3401 zio_type_t type = zio->io_type; 3402 int flags = zio->io_flags; 3403 3404 /* 3405 * If this i/o is a gang leader, it didn't do any actual work. 3406 */ 3407 if (zio->io_gang_tree) 3408 return; 3409 3410 if (zio->io_error == 0) { 3411 /* 3412 * If this is a root i/o, don't count it -- we've already 3413 * counted the top-level vdevs, and vdev_get_stats() will 3414 * aggregate them when asked. This reduces contention on 3415 * the root vdev_stat_lock and implicitly handles blocks 3416 * that compress away to holes, for which there is no i/o. 3417 * (Holes never create vdev children, so all the counters 3418 * remain zero, which is what we want.) 3419 * 3420 * Note: this only applies to successful i/o (io_error == 0) 3421 * because unlike i/o counts, errors are not additive. 3422 * When reading a ditto block, for example, failure of 3423 * one top-level vdev does not imply a root-level error. 3424 */ 3425 if (vd == rvd) 3426 return; 3427 3428 ASSERT(vd == zio->io_vd); 3429 3430 if (flags & ZIO_FLAG_IO_BYPASS) 3431 return; 3432 3433 mutex_enter(&vd->vdev_stat_lock); 3434 3435 if (flags & ZIO_FLAG_IO_REPAIR) { 3436 if (flags & ZIO_FLAG_SCAN_THREAD) { 3437 dsl_scan_phys_t *scn_phys = 3438 &spa->spa_dsl_pool->dp_scan->scn_phys; 3439 uint64_t *processed = &scn_phys->scn_processed; 3440 3441 /* XXX cleanup? */ 3442 if (vd->vdev_ops->vdev_op_leaf) 3443 atomic_add_64(processed, psize); 3444 vs->vs_scan_processed += psize; 3445 } 3446 3447 if (flags & ZIO_FLAG_SELF_HEAL) 3448 vs->vs_self_healed += psize; 3449 } 3450 3451 vs->vs_ops[type]++; 3452 vs->vs_bytes[type] += psize; 3453 3454 mutex_exit(&vd->vdev_stat_lock); 3455 return; 3456 } 3457 3458 if (flags & ZIO_FLAG_SPECULATIVE) 3459 return; 3460 3461 /* 3462 * If this is an I/O error that is going to be retried, then ignore the 3463 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 3464 * hard errors, when in reality they can happen for any number of 3465 * innocuous reasons (bus resets, MPxIO link failure, etc). 3466 */ 3467 if (zio->io_error == EIO && 3468 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 3469 return; 3470 3471 /* 3472 * Intent logs writes won't propagate their error to the root 3473 * I/O so don't mark these types of failures as pool-level 3474 * errors. 3475 */ 3476 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 3477 return; 3478 3479 mutex_enter(&vd->vdev_stat_lock); 3480 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3481 if (zio->io_error == ECKSUM) 3482 vs->vs_checksum_errors++; 3483 else 3484 vs->vs_read_errors++; 3485 } 3486 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3487 vs->vs_write_errors++; 3488 mutex_exit(&vd->vdev_stat_lock); 3489 3490 if (spa->spa_load_state == SPA_LOAD_NONE && 3491 type == ZIO_TYPE_WRITE && txg != 0 && 3492 (!(flags & ZIO_FLAG_IO_REPAIR) || 3493 (flags & ZIO_FLAG_SCAN_THREAD) || 3494 spa->spa_claiming)) { 3495 /* 3496 * This is either a normal write (not a repair), or it's 3497 * a repair induced by the scrub thread, or it's a repair 3498 * made by zil_claim() during spa_load() in the first txg. 3499 * In the normal case, we commit the DTL change in the same 3500 * txg as the block was born. In the scrub-induced repair 3501 * case, we know that scrubs run in first-pass syncing context, 3502 * so we commit the DTL change in spa_syncing_txg(spa). 3503 * In the zil_claim() case, we commit in spa_first_txg(spa). 3504 * 3505 * We currently do not make DTL entries for failed spontaneous 3506 * self-healing writes triggered by normal (non-scrubbing) 3507 * reads, because we have no transactional context in which to 3508 * do so -- and it's not clear that it'd be desirable anyway. 3509 */ 3510 if (vd->vdev_ops->vdev_op_leaf) { 3511 uint64_t commit_txg = txg; 3512 if (flags & ZIO_FLAG_SCAN_THREAD) { 3513 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3514 ASSERT(spa_sync_pass(spa) == 1); 3515 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3516 commit_txg = spa_syncing_txg(spa); 3517 } else if (spa->spa_claiming) { 3518 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3519 commit_txg = spa_first_txg(spa); 3520 } 3521 ASSERT(commit_txg >= spa_syncing_txg(spa)); 3522 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3523 return; 3524 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3525 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 3526 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3527 } 3528 if (vd != rvd) 3529 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3530 } 3531 } 3532 3533 /* 3534 * Update the in-core space usage stats for this vdev, its metaslab class, 3535 * and the root vdev. 3536 */ 3537 void 3538 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3539 int64_t space_delta) 3540 { 3541 int64_t dspace_delta = space_delta; 3542 spa_t *spa = vd->vdev_spa; 3543 vdev_t *rvd = spa->spa_root_vdev; 3544 metaslab_group_t *mg = vd->vdev_mg; 3545 metaslab_class_t *mc = mg ? mg->mg_class : NULL; 3546 3547 ASSERT(vd == vd->vdev_top); 3548 3549 /* 3550 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 3551 * factor. We must calculate this here and not at the root vdev 3552 * because the root vdev's psize-to-asize is simply the max of its 3553 * childrens', thus not accurate enough for us. 3554 */ 3555 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 3556 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 3557 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 3558 vd->vdev_deflate_ratio; 3559 3560 mutex_enter(&vd->vdev_stat_lock); 3561 vd->vdev_stat.vs_alloc += alloc_delta; 3562 vd->vdev_stat.vs_space += space_delta; 3563 vd->vdev_stat.vs_dspace += dspace_delta; 3564 mutex_exit(&vd->vdev_stat_lock); 3565 3566 if (mc == spa_normal_class(spa)) { 3567 mutex_enter(&rvd->vdev_stat_lock); 3568 rvd->vdev_stat.vs_alloc += alloc_delta; 3569 rvd->vdev_stat.vs_space += space_delta; 3570 rvd->vdev_stat.vs_dspace += dspace_delta; 3571 mutex_exit(&rvd->vdev_stat_lock); 3572 } 3573 3574 if (mc != NULL) { 3575 ASSERT(rvd == vd->vdev_parent); 3576 ASSERT(vd->vdev_ms_count != 0); 3577 3578 metaslab_class_space_update(mc, 3579 alloc_delta, defer_delta, space_delta, dspace_delta); 3580 } 3581 } 3582 3583 /* 3584 * Mark a top-level vdev's config as dirty, placing it on the dirty list 3585 * so that it will be written out next time the vdev configuration is synced. 3586 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3587 */ 3588 void 3589 vdev_config_dirty(vdev_t *vd) 3590 { 3591 spa_t *spa = vd->vdev_spa; 3592 vdev_t *rvd = spa->spa_root_vdev; 3593 int c; 3594 3595 ASSERT(spa_writeable(spa)); 3596 3597 /* 3598 * If this is an aux vdev (as with l2cache and spare devices), then we 3599 * update the vdev config manually and set the sync flag. 3600 */ 3601 if (vd->vdev_aux != NULL) { 3602 spa_aux_vdev_t *sav = vd->vdev_aux; 3603 nvlist_t **aux; 3604 uint_t naux; 3605 3606 for (c = 0; c < sav->sav_count; c++) { 3607 if (sav->sav_vdevs[c] == vd) 3608 break; 3609 } 3610 3611 if (c == sav->sav_count) { 3612 /* 3613 * We're being removed. There's nothing more to do. 3614 */ 3615 ASSERT(sav->sav_sync == B_TRUE); 3616 return; 3617 } 3618 3619 sav->sav_sync = B_TRUE; 3620 3621 if (nvlist_lookup_nvlist_array(sav->sav_config, 3622 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 3623 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 3624 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 3625 } 3626 3627 ASSERT(c < naux); 3628 3629 /* 3630 * Setting the nvlist in the middle if the array is a little 3631 * sketchy, but it will work. 3632 */ 3633 nvlist_free(aux[c]); 3634 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3635 3636 return; 3637 } 3638 3639 /* 3640 * The dirty list is protected by the SCL_CONFIG lock. The caller 3641 * must either hold SCL_CONFIG as writer, or must be the sync thread 3642 * (which holds SCL_CONFIG as reader). There's only one sync thread, 3643 * so this is sufficient to ensure mutual exclusion. 3644 */ 3645 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3646 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3647 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3648 3649 if (vd == rvd) { 3650 for (c = 0; c < rvd->vdev_children; c++) 3651 vdev_config_dirty(rvd->vdev_child[c]); 3652 } else { 3653 ASSERT(vd == vd->vdev_top); 3654 3655 if (!list_link_active(&vd->vdev_config_dirty_node) && 3656 vdev_is_concrete(vd)) { 3657 list_insert_head(&spa->spa_config_dirty_list, vd); 3658 } 3659 } 3660 } 3661 3662 void 3663 vdev_config_clean(vdev_t *vd) 3664 { 3665 spa_t *spa = vd->vdev_spa; 3666 3667 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3668 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3669 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3670 3671 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3672 list_remove(&spa->spa_config_dirty_list, vd); 3673 } 3674 3675 /* 3676 * Mark a top-level vdev's state as dirty, so that the next pass of 3677 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3678 * the state changes from larger config changes because they require 3679 * much less locking, and are often needed for administrative actions. 3680 */ 3681 void 3682 vdev_state_dirty(vdev_t *vd) 3683 { 3684 spa_t *spa = vd->vdev_spa; 3685 3686 ASSERT(spa_writeable(spa)); 3687 ASSERT(vd == vd->vdev_top); 3688 3689 /* 3690 * The state list is protected by the SCL_STATE lock. The caller 3691 * must either hold SCL_STATE as writer, or must be the sync thread 3692 * (which holds SCL_STATE as reader). There's only one sync thread, 3693 * so this is sufficient to ensure mutual exclusion. 3694 */ 3695 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3696 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3697 spa_config_held(spa, SCL_STATE, RW_READER))); 3698 3699 if (!list_link_active(&vd->vdev_state_dirty_node) && 3700 vdev_is_concrete(vd)) 3701 list_insert_head(&spa->spa_state_dirty_list, vd); 3702 } 3703 3704 void 3705 vdev_state_clean(vdev_t *vd) 3706 { 3707 spa_t *spa = vd->vdev_spa; 3708 3709 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3710 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3711 spa_config_held(spa, SCL_STATE, RW_READER))); 3712 3713 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3714 list_remove(&spa->spa_state_dirty_list, vd); 3715 } 3716 3717 /* 3718 * Propagate vdev state up from children to parent. 3719 */ 3720 void 3721 vdev_propagate_state(vdev_t *vd) 3722 { 3723 spa_t *spa = vd->vdev_spa; 3724 vdev_t *rvd = spa->spa_root_vdev; 3725 int degraded = 0, faulted = 0; 3726 int corrupted = 0; 3727 vdev_t *child; 3728 3729 if (vd->vdev_children > 0) { 3730 for (int c = 0; c < vd->vdev_children; c++) { 3731 child = vd->vdev_child[c]; 3732 3733 /* 3734 * Don't factor holes or indirect vdevs into the 3735 * decision. 3736 */ 3737 if (!vdev_is_concrete(child)) 3738 continue; 3739 3740 if (!vdev_readable(child) || 3741 (!vdev_writeable(child) && spa_writeable(spa))) { 3742 /* 3743 * Root special: if there is a top-level log 3744 * device, treat the root vdev as if it were 3745 * degraded. 3746 */ 3747 if (child->vdev_islog && vd == rvd) 3748 degraded++; 3749 else 3750 faulted++; 3751 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 3752 degraded++; 3753 } 3754 3755 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 3756 corrupted++; 3757 } 3758 3759 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 3760 3761 /* 3762 * Root special: if there is a top-level vdev that cannot be 3763 * opened due to corrupted metadata, then propagate the root 3764 * vdev's aux state as 'corrupt' rather than 'insufficient 3765 * replicas'. 3766 */ 3767 if (corrupted && vd == rvd && 3768 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 3769 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 3770 VDEV_AUX_CORRUPT_DATA); 3771 } 3772 3773 if (vd->vdev_parent) 3774 vdev_propagate_state(vd->vdev_parent); 3775 } 3776 3777 /* 3778 * Set a vdev's state. If this is during an open, we don't update the parent 3779 * state, because we're in the process of opening children depth-first. 3780 * Otherwise, we propagate the change to the parent. 3781 * 3782 * If this routine places a device in a faulted state, an appropriate ereport is 3783 * generated. 3784 */ 3785 void 3786 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3787 { 3788 uint64_t save_state; 3789 spa_t *spa = vd->vdev_spa; 3790 3791 if (state == vd->vdev_state) { 3792 vd->vdev_stat.vs_aux = aux; 3793 return; 3794 } 3795 3796 save_state = vd->vdev_state; 3797 3798 vd->vdev_state = state; 3799 vd->vdev_stat.vs_aux = aux; 3800 3801 /* 3802 * If we are setting the vdev state to anything but an open state, then 3803 * always close the underlying device unless the device has requested 3804 * a delayed close (i.e. we're about to remove or fault the device). 3805 * Otherwise, we keep accessible but invalid devices open forever. 3806 * We don't call vdev_close() itself, because that implies some extra 3807 * checks (offline, etc) that we don't want here. This is limited to 3808 * leaf devices, because otherwise closing the device will affect other 3809 * children. 3810 */ 3811 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 3812 vd->vdev_ops->vdev_op_leaf) 3813 vd->vdev_ops->vdev_op_close(vd); 3814 3815 /* 3816 * If we have brought this vdev back into service, we need 3817 * to notify fmd so that it can gracefully repair any outstanding 3818 * cases due to a missing device. We do this in all cases, even those 3819 * that probably don't correlate to a repaired fault. This is sure to 3820 * catch all cases, and we let the zfs-retire agent sort it out. If 3821 * this is a transient state it's OK, as the retire agent will 3822 * double-check the state of the vdev before repairing it. 3823 */ 3824 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3825 vd->vdev_prevstate != state) 3826 zfs_post_state_change(spa, vd); 3827 3828 if (vd->vdev_removed && 3829 state == VDEV_STATE_CANT_OPEN && 3830 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 3831 /* 3832 * If the previous state is set to VDEV_STATE_REMOVED, then this 3833 * device was previously marked removed and someone attempted to 3834 * reopen it. If this failed due to a nonexistent device, then 3835 * keep the device in the REMOVED state. We also let this be if 3836 * it is one of our special test online cases, which is only 3837 * attempting to online the device and shouldn't generate an FMA 3838 * fault. 3839 */ 3840 vd->vdev_state = VDEV_STATE_REMOVED; 3841 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 3842 } else if (state == VDEV_STATE_REMOVED) { 3843 vd->vdev_removed = B_TRUE; 3844 } else if (state == VDEV_STATE_CANT_OPEN) { 3845 /* 3846 * If we fail to open a vdev during an import or recovery, we 3847 * mark it as "not available", which signifies that it was 3848 * never there to begin with. Failure to open such a device 3849 * is not considered an error. 3850 */ 3851 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3852 spa_load_state(spa) == SPA_LOAD_RECOVER) && 3853 vd->vdev_ops->vdev_op_leaf) 3854 vd->vdev_not_present = 1; 3855 3856 /* 3857 * Post the appropriate ereport. If the 'prevstate' field is 3858 * set to something other than VDEV_STATE_UNKNOWN, it indicates 3859 * that this is part of a vdev_reopen(). In this case, we don't 3860 * want to post the ereport if the device was already in the 3861 * CANT_OPEN state beforehand. 3862 * 3863 * If the 'checkremove' flag is set, then this is an attempt to 3864 * online the device in response to an insertion event. If we 3865 * hit this case, then we have detected an insertion event for a 3866 * faulted or offline device that wasn't in the removed state. 3867 * In this scenario, we don't post an ereport because we are 3868 * about to replace the device, or attempt an online with 3869 * vdev_forcefault, which will generate the fault for us. 3870 */ 3871 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 3872 !vd->vdev_not_present && !vd->vdev_checkremove && 3873 vd != spa->spa_root_vdev) { 3874 const char *class; 3875 3876 switch (aux) { 3877 case VDEV_AUX_OPEN_FAILED: 3878 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3879 break; 3880 case VDEV_AUX_CORRUPT_DATA: 3881 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3882 break; 3883 case VDEV_AUX_NO_REPLICAS: 3884 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3885 break; 3886 case VDEV_AUX_BAD_GUID_SUM: 3887 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3888 break; 3889 case VDEV_AUX_TOO_SMALL: 3890 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3891 break; 3892 case VDEV_AUX_BAD_LABEL: 3893 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3894 break; 3895 default: 3896 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3897 } 3898 3899 zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3900 } 3901 3902 /* Erase any notion of persistent removed state */ 3903 vd->vdev_removed = B_FALSE; 3904 } else { 3905 vd->vdev_removed = B_FALSE; 3906 } 3907 3908 if (!isopen && vd->vdev_parent) 3909 vdev_propagate_state(vd->vdev_parent); 3910 } 3911 3912 boolean_t 3913 vdev_children_are_offline(vdev_t *vd) 3914 { 3915 ASSERT(!vd->vdev_ops->vdev_op_leaf); 3916 3917 for (uint64_t i = 0; i < vd->vdev_children; i++) { 3918 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 3919 return (B_FALSE); 3920 } 3921 3922 return (B_TRUE); 3923 } 3924 3925 /* 3926 * Check the vdev configuration to ensure that it's capable of supporting 3927 * a root pool. We do not support partial configuration. 3928 * In addition, only a single top-level vdev is allowed. 3929 */ 3930 boolean_t 3931 vdev_is_bootable(vdev_t *vd) 3932 { 3933 if (!vd->vdev_ops->vdev_op_leaf) { 3934 char *vdev_type = vd->vdev_ops->vdev_op_type; 3935 3936 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 3937 vd->vdev_children > 1) { 3938 return (B_FALSE); 3939 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 3940 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 3941 return (B_FALSE); 3942 } 3943 } 3944 3945 for (int c = 0; c < vd->vdev_children; c++) { 3946 if (!vdev_is_bootable(vd->vdev_child[c])) 3947 return (B_FALSE); 3948 } 3949 return (B_TRUE); 3950 } 3951 3952 boolean_t 3953 vdev_is_concrete(vdev_t *vd) 3954 { 3955 vdev_ops_t *ops = vd->vdev_ops; 3956 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 3957 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 3958 return (B_FALSE); 3959 } else { 3960 return (B_TRUE); 3961 } 3962 } 3963 3964 /* 3965 * Determine if a log device has valid content. If the vdev was 3966 * removed or faulted in the MOS config then we know that 3967 * the content on the log device has already been written to the pool. 3968 */ 3969 boolean_t 3970 vdev_log_state_valid(vdev_t *vd) 3971 { 3972 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 3973 !vd->vdev_removed) 3974 return (B_TRUE); 3975 3976 for (int c = 0; c < vd->vdev_children; c++) 3977 if (vdev_log_state_valid(vd->vdev_child[c])) 3978 return (B_TRUE); 3979 3980 return (B_FALSE); 3981 } 3982 3983 /* 3984 * Expand a vdev if possible. 3985 */ 3986 void 3987 vdev_expand(vdev_t *vd, uint64_t txg) 3988 { 3989 ASSERT(vd->vdev_top == vd); 3990 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3991 3992 vdev_set_deflate_ratio(vd); 3993 3994 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 3995 vdev_is_concrete(vd)) { 3996 VERIFY(vdev_metaslab_init(vd, txg) == 0); 3997 vdev_config_dirty(vd); 3998 } 3999 } 4000 4001 /* 4002 * Split a vdev. 4003 */ 4004 void 4005 vdev_split(vdev_t *vd) 4006 { 4007 vdev_t *cvd, *pvd = vd->vdev_parent; 4008 4009 vdev_remove_child(pvd, vd); 4010 vdev_compact_children(pvd); 4011 4012 cvd = pvd->vdev_child[0]; 4013 if (pvd->vdev_children == 1) { 4014 vdev_remove_parent(cvd); 4015 cvd->vdev_splitting = B_TRUE; 4016 } 4017 vdev_propagate_state(cvd); 4018 } 4019 4020 void 4021 vdev_deadman(vdev_t *vd) 4022 { 4023 for (int c = 0; c < vd->vdev_children; c++) { 4024 vdev_t *cvd = vd->vdev_child[c]; 4025 4026 vdev_deadman(cvd); 4027 } 4028 4029 if (vd->vdev_ops->vdev_op_leaf) { 4030 vdev_queue_t *vq = &vd->vdev_queue; 4031 4032 mutex_enter(&vq->vq_lock); 4033 if (avl_numnodes(&vq->vq_active_tree) > 0) { 4034 spa_t *spa = vd->vdev_spa; 4035 zio_t *fio; 4036 uint64_t delta; 4037 4038 /* 4039 * Look at the head of all the pending queues, 4040 * if any I/O has been outstanding for longer than 4041 * the spa_deadman_synctime we panic the system. 4042 */ 4043 fio = avl_first(&vq->vq_active_tree); 4044 delta = gethrtime() - fio->io_timestamp; 4045 if (delta > spa_deadman_synctime(spa)) { 4046 vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 4047 "%lluns, delta %lluns, last io %lluns", 4048 fio->io_timestamp, (u_longlong_t)delta, 4049 vq->vq_io_complete_ts); 4050 fm_panic("I/O to pool '%s' appears to be " 4051 "hung.", spa_name(spa)); 4052 } 4053 } 4054 mutex_exit(&vq->vq_lock); 4055 } 4056 } 4057