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 * If config lock is not held do not check for the txg. spa_sync could 1577 * be updating the vdev's label before updating spa_last_synced_txg. 1578 */ 1579 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1580 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 1581 txg = UINT64_MAX; 1582 else 1583 txg = spa_last_synced_txg(spa); 1584 1585 if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1586 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1587 VDEV_AUX_BAD_LABEL); 1588 vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1589 "txg %llu", (u_longlong_t)txg); 1590 return (0); 1591 } 1592 1593 /* 1594 * Determine if this vdev has been split off into another 1595 * pool. If so, then refuse to open it. 1596 */ 1597 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1598 &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1599 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1600 VDEV_AUX_SPLIT_POOL); 1601 nvlist_free(label); 1602 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 1603 return (0); 1604 } 1605 1606 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 1607 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1608 VDEV_AUX_CORRUPT_DATA); 1609 nvlist_free(label); 1610 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1611 ZPOOL_CONFIG_POOL_GUID); 1612 return (0); 1613 } 1614 1615 /* 1616 * If config is not trusted then ignore the spa guid check. This is 1617 * necessary because if the machine crashed during a re-guid the new 1618 * guid might have been written to all of the vdev labels, but not the 1619 * cached config. The check will be performed again once we have the 1620 * trusted config from the MOS. 1621 */ 1622 if (spa->spa_trust_config && guid != spa_guid(spa)) { 1623 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1624 VDEV_AUX_CORRUPT_DATA); 1625 nvlist_free(label); 1626 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 1627 "match config (%llu != %llu)", (u_longlong_t)guid, 1628 (u_longlong_t)spa_guid(spa)); 1629 return (0); 1630 } 1631 1632 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1633 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1634 &aux_guid) != 0) 1635 aux_guid = 0; 1636 1637 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 1638 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1639 VDEV_AUX_CORRUPT_DATA); 1640 nvlist_free(label); 1641 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1642 ZPOOL_CONFIG_GUID); 1643 return (0); 1644 } 1645 1646 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 1647 != 0) { 1648 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1649 VDEV_AUX_CORRUPT_DATA); 1650 nvlist_free(label); 1651 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1652 ZPOOL_CONFIG_TOP_GUID); 1653 return (0); 1654 } 1655 1656 /* 1657 * If this vdev just became a top-level vdev because its sibling was 1658 * detached, it will have adopted the parent's vdev guid -- but the 1659 * label may or may not be on disk yet. Fortunately, either version 1660 * of the label will have the same top guid, so if we're a top-level 1661 * vdev, we can safely compare to that instead. 1662 * However, if the config comes from a cachefile that failed to update 1663 * after the detach, a top-level vdev will appear as a non top-level 1664 * vdev in the config. Also relax the constraints if we perform an 1665 * extreme rewind. 1666 * 1667 * If we split this vdev off instead, then we also check the 1668 * original pool's guid. We don't want to consider the vdev 1669 * corrupt if it is partway through a split operation. 1670 */ 1671 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 1672 boolean_t mismatch = B_FALSE; 1673 if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 1674 if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 1675 mismatch = B_TRUE; 1676 } else { 1677 if (vd->vdev_guid != top_guid && 1678 vd->vdev_top->vdev_guid != guid) 1679 mismatch = B_TRUE; 1680 } 1681 1682 if (mismatch) { 1683 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1684 VDEV_AUX_CORRUPT_DATA); 1685 nvlist_free(label); 1686 vdev_dbgmsg(vd, "vdev_validate: config guid " 1687 "doesn't match label guid"); 1688 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 1689 (u_longlong_t)vd->vdev_guid, 1690 (u_longlong_t)vd->vdev_top->vdev_guid); 1691 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 1692 "aux_guid %llu", (u_longlong_t)guid, 1693 (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1694 return (0); 1695 } 1696 } 1697 1698 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1699 &state) != 0) { 1700 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1701 VDEV_AUX_CORRUPT_DATA); 1702 nvlist_free(label); 1703 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1704 ZPOOL_CONFIG_POOL_STATE); 1705 return (0); 1706 } 1707 1708 nvlist_free(label); 1709 1710 /* 1711 * If this is a verbatim import, no need to check the 1712 * state of the pool. 1713 */ 1714 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1715 spa_load_state(spa) == SPA_LOAD_OPEN && 1716 state != POOL_STATE_ACTIVE) { 1717 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 1718 "for spa %s", (u_longlong_t)state, spa->spa_name); 1719 return (SET_ERROR(EBADF)); 1720 } 1721 1722 /* 1723 * If we were able to open and validate a vdev that was 1724 * previously marked permanently unavailable, clear that state 1725 * now. 1726 */ 1727 if (vd->vdev_not_present) 1728 vd->vdev_not_present = 0; 1729 1730 return (0); 1731 } 1732 1733 static void 1734 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 1735 { 1736 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 1737 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 1738 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 1739 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 1740 dvd->vdev_path, svd->vdev_path); 1741 spa_strfree(dvd->vdev_path); 1742 dvd->vdev_path = spa_strdup(svd->vdev_path); 1743 } 1744 } else if (svd->vdev_path != NULL) { 1745 dvd->vdev_path = spa_strdup(svd->vdev_path); 1746 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 1747 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 1748 } 1749 } 1750 1751 /* 1752 * Recursively copy vdev paths from one vdev to another. Source and destination 1753 * vdev trees must have same geometry otherwise return error. Intended to copy 1754 * paths from userland config into MOS config. 1755 */ 1756 int 1757 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 1758 { 1759 if ((svd->vdev_ops == &vdev_missing_ops) || 1760 (svd->vdev_ishole && dvd->vdev_ishole) || 1761 (dvd->vdev_ops == &vdev_indirect_ops)) 1762 return (0); 1763 1764 if (svd->vdev_ops != dvd->vdev_ops) { 1765 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 1766 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 1767 return (SET_ERROR(EINVAL)); 1768 } 1769 1770 if (svd->vdev_guid != dvd->vdev_guid) { 1771 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 1772 "%llu)", (u_longlong_t)svd->vdev_guid, 1773 (u_longlong_t)dvd->vdev_guid); 1774 return (SET_ERROR(EINVAL)); 1775 } 1776 1777 if (svd->vdev_children != dvd->vdev_children) { 1778 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 1779 "%llu != %llu", (u_longlong_t)svd->vdev_children, 1780 (u_longlong_t)dvd->vdev_children); 1781 return (SET_ERROR(EINVAL)); 1782 } 1783 1784 for (uint64_t i = 0; i < svd->vdev_children; i++) { 1785 int error = vdev_copy_path_strict(svd->vdev_child[i], 1786 dvd->vdev_child[i]); 1787 if (error != 0) 1788 return (error); 1789 } 1790 1791 if (svd->vdev_ops->vdev_op_leaf) 1792 vdev_copy_path_impl(svd, dvd); 1793 1794 return (0); 1795 } 1796 1797 static void 1798 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 1799 { 1800 ASSERT(stvd->vdev_top == stvd); 1801 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 1802 1803 for (uint64_t i = 0; i < dvd->vdev_children; i++) { 1804 vdev_copy_path_search(stvd, dvd->vdev_child[i]); 1805 } 1806 1807 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 1808 return; 1809 1810 /* 1811 * The idea here is that while a vdev can shift positions within 1812 * a top vdev (when replacing, attaching mirror, etc.) it cannot 1813 * step outside of it. 1814 */ 1815 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 1816 1817 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 1818 return; 1819 1820 ASSERT(vd->vdev_ops->vdev_op_leaf); 1821 1822 vdev_copy_path_impl(vd, dvd); 1823 } 1824 1825 /* 1826 * Recursively copy vdev paths from one root vdev to another. Source and 1827 * destination vdev trees may differ in geometry. For each destination leaf 1828 * vdev, search a vdev with the same guid and top vdev id in the source. 1829 * Intended to copy paths from userland config into MOS config. 1830 */ 1831 void 1832 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 1833 { 1834 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 1835 ASSERT(srvd->vdev_ops == &vdev_root_ops); 1836 ASSERT(drvd->vdev_ops == &vdev_root_ops); 1837 1838 for (uint64_t i = 0; i < children; i++) { 1839 vdev_copy_path_search(srvd->vdev_child[i], 1840 drvd->vdev_child[i]); 1841 } 1842 } 1843 1844 /* 1845 * Close a virtual device. 1846 */ 1847 void 1848 vdev_close(vdev_t *vd) 1849 { 1850 spa_t *spa = vd->vdev_spa; 1851 vdev_t *pvd = vd->vdev_parent; 1852 1853 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1854 1855 /* 1856 * If our parent is reopening, then we are as well, unless we are 1857 * going offline. 1858 */ 1859 if (pvd != NULL && pvd->vdev_reopening) 1860 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1861 1862 vd->vdev_ops->vdev_op_close(vd); 1863 1864 vdev_cache_purge(vd); 1865 1866 /* 1867 * We record the previous state before we close it, so that if we are 1868 * doing a reopen(), we don't generate FMA ereports if we notice that 1869 * it's still faulted. 1870 */ 1871 vd->vdev_prevstate = vd->vdev_state; 1872 1873 if (vd->vdev_offline) 1874 vd->vdev_state = VDEV_STATE_OFFLINE; 1875 else 1876 vd->vdev_state = VDEV_STATE_CLOSED; 1877 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1878 } 1879 1880 void 1881 vdev_hold(vdev_t *vd) 1882 { 1883 spa_t *spa = vd->vdev_spa; 1884 1885 ASSERT(spa_is_root(spa)); 1886 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1887 return; 1888 1889 for (int c = 0; c < vd->vdev_children; c++) 1890 vdev_hold(vd->vdev_child[c]); 1891 1892 if (vd->vdev_ops->vdev_op_leaf) 1893 vd->vdev_ops->vdev_op_hold(vd); 1894 } 1895 1896 void 1897 vdev_rele(vdev_t *vd) 1898 { 1899 spa_t *spa = vd->vdev_spa; 1900 1901 ASSERT(spa_is_root(spa)); 1902 for (int c = 0; c < vd->vdev_children; c++) 1903 vdev_rele(vd->vdev_child[c]); 1904 1905 if (vd->vdev_ops->vdev_op_leaf) 1906 vd->vdev_ops->vdev_op_rele(vd); 1907 } 1908 1909 /* 1910 * Reopen all interior vdevs and any unopened leaves. We don't actually 1911 * reopen leaf vdevs which had previously been opened as they might deadlock 1912 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1913 * If the leaf has never been opened then open it, as usual. 1914 */ 1915 void 1916 vdev_reopen(vdev_t *vd) 1917 { 1918 spa_t *spa = vd->vdev_spa; 1919 1920 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1921 1922 /* set the reopening flag unless we're taking the vdev offline */ 1923 vd->vdev_reopening = !vd->vdev_offline; 1924 vdev_close(vd); 1925 (void) vdev_open(vd); 1926 1927 /* 1928 * Call vdev_validate() here to make sure we have the same device. 1929 * Otherwise, a device with an invalid label could be successfully 1930 * opened in response to vdev_reopen(). 1931 */ 1932 if (vd->vdev_aux) { 1933 (void) vdev_validate_aux(vd); 1934 if (vdev_readable(vd) && vdev_writeable(vd) && 1935 vd->vdev_aux == &spa->spa_l2cache && 1936 !l2arc_vdev_present(vd)) 1937 l2arc_add_vdev(spa, vd); 1938 } else { 1939 (void) vdev_validate(vd); 1940 } 1941 1942 /* 1943 * Reassess parent vdev's health. 1944 */ 1945 vdev_propagate_state(vd); 1946 } 1947 1948 int 1949 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1950 { 1951 int error; 1952 1953 /* 1954 * Normally, partial opens (e.g. of a mirror) are allowed. 1955 * For a create, however, we want to fail the request if 1956 * there are any components we can't open. 1957 */ 1958 error = vdev_open(vd); 1959 1960 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1961 vdev_close(vd); 1962 return (error ? error : ENXIO); 1963 } 1964 1965 /* 1966 * Recursively load DTLs and initialize all labels. 1967 */ 1968 if ((error = vdev_dtl_load(vd)) != 0 || 1969 (error = vdev_label_init(vd, txg, isreplacing ? 1970 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1971 vdev_close(vd); 1972 return (error); 1973 } 1974 1975 return (0); 1976 } 1977 1978 void 1979 vdev_metaslab_set_size(vdev_t *vd) 1980 { 1981 uint64_t asize = vd->vdev_asize; 1982 uint64_t ms_shift = 0; 1983 1984 /* 1985 * For vdevs that are bigger than 8G the metaslab size varies in 1986 * a way that the number of metaslabs increases in powers of two, 1987 * linearly in terms of vdev_asize, starting from 16 metaslabs. 1988 * So for vdev_asize of 8G we get 16 metaslabs, for 16G, we get 32, 1989 * and so on, until we hit the maximum metaslab count limit 1990 * [vdev_max_ms_count] from which point the metaslab count stays 1991 * the same. 1992 */ 1993 ms_shift = vdev_default_ms_shift; 1994 1995 if ((asize >> ms_shift) < vdev_min_ms_count) { 1996 /* 1997 * For devices that are less than 8G we want to have 1998 * exactly 16 metaslabs. We don't want less as integer 1999 * division rounds down, so less metaslabs mean more 2000 * wasted space. We don't want more as these vdevs are 2001 * small and in the likely event that we are running 2002 * out of space, the SPA will have a hard time finding 2003 * space due to fragmentation. 2004 */ 2005 ms_shift = highbit64(asize / vdev_min_ms_count); 2006 ms_shift = MAX(ms_shift, SPA_MAXBLOCKSHIFT); 2007 2008 } else if ((asize >> ms_shift) > vdev_max_ms_count) { 2009 ms_shift = highbit64(asize / vdev_max_ms_count); 2010 } 2011 2012 vd->vdev_ms_shift = ms_shift; 2013 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2014 } 2015 2016 void 2017 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2018 { 2019 ASSERT(vd == vd->vdev_top); 2020 /* indirect vdevs don't have metaslabs or dtls */ 2021 ASSERT(vdev_is_concrete(vd) || flags == 0); 2022 ASSERT(ISP2(flags)); 2023 ASSERT(spa_writeable(vd->vdev_spa)); 2024 2025 if (flags & VDD_METASLAB) 2026 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2027 2028 if (flags & VDD_DTL) 2029 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2030 2031 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2032 } 2033 2034 void 2035 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 2036 { 2037 for (int c = 0; c < vd->vdev_children; c++) 2038 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 2039 2040 if (vd->vdev_ops->vdev_op_leaf) 2041 vdev_dirty(vd->vdev_top, flags, vd, txg); 2042 } 2043 2044 /* 2045 * DTLs. 2046 * 2047 * A vdev's DTL (dirty time log) is the set of transaction groups for which 2048 * the vdev has less than perfect replication. There are four kinds of DTL: 2049 * 2050 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 2051 * 2052 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 2053 * 2054 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 2055 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 2056 * txgs that was scrubbed. 2057 * 2058 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 2059 * persistent errors or just some device being offline. 2060 * Unlike the other three, the DTL_OUTAGE map is not generally 2061 * maintained; it's only computed when needed, typically to 2062 * determine whether a device can be detached. 2063 * 2064 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 2065 * either has the data or it doesn't. 2066 * 2067 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 2068 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 2069 * if any child is less than fully replicated, then so is its parent. 2070 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 2071 * comprising only those txgs which appear in 'maxfaults' or more children; 2072 * those are the txgs we don't have enough replication to read. For example, 2073 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 2074 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 2075 * two child DTL_MISSING maps. 2076 * 2077 * It should be clear from the above that to compute the DTLs and outage maps 2078 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 2079 * Therefore, that is all we keep on disk. When loading the pool, or after 2080 * a configuration change, we generate all other DTLs from first principles. 2081 */ 2082 void 2083 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2084 { 2085 range_tree_t *rt = vd->vdev_dtl[t]; 2086 2087 ASSERT(t < DTL_TYPES); 2088 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2089 ASSERT(spa_writeable(vd->vdev_spa)); 2090 2091 mutex_enter(&vd->vdev_dtl_lock); 2092 if (!range_tree_contains(rt, txg, size)) 2093 range_tree_add(rt, txg, size); 2094 mutex_exit(&vd->vdev_dtl_lock); 2095 } 2096 2097 boolean_t 2098 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2099 { 2100 range_tree_t *rt = vd->vdev_dtl[t]; 2101 boolean_t dirty = B_FALSE; 2102 2103 ASSERT(t < DTL_TYPES); 2104 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2105 2106 /* 2107 * While we are loading the pool, the DTLs have not been loaded yet. 2108 * Ignore the DTLs and try all devices. This avoids a recursive 2109 * mutex enter on the vdev_dtl_lock, and also makes us try hard 2110 * when loading the pool (relying on the checksum to ensure that 2111 * we get the right data -- note that we while loading, we are 2112 * only reading the MOS, which is always checksummed). 2113 */ 2114 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 2115 return (B_FALSE); 2116 2117 mutex_enter(&vd->vdev_dtl_lock); 2118 if (!range_tree_is_empty(rt)) 2119 dirty = range_tree_contains(rt, txg, size); 2120 mutex_exit(&vd->vdev_dtl_lock); 2121 2122 return (dirty); 2123 } 2124 2125 boolean_t 2126 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 2127 { 2128 range_tree_t *rt = vd->vdev_dtl[t]; 2129 boolean_t empty; 2130 2131 mutex_enter(&vd->vdev_dtl_lock); 2132 empty = range_tree_is_empty(rt); 2133 mutex_exit(&vd->vdev_dtl_lock); 2134 2135 return (empty); 2136 } 2137 2138 /* 2139 * Returns the lowest txg in the DTL range. 2140 */ 2141 static uint64_t 2142 vdev_dtl_min(vdev_t *vd) 2143 { 2144 range_seg_t *rs; 2145 2146 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2147 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2148 ASSERT0(vd->vdev_children); 2149 2150 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 2151 return (rs->rs_start - 1); 2152 } 2153 2154 /* 2155 * Returns the highest txg in the DTL. 2156 */ 2157 static uint64_t 2158 vdev_dtl_max(vdev_t *vd) 2159 { 2160 range_seg_t *rs; 2161 2162 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2163 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2164 ASSERT0(vd->vdev_children); 2165 2166 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 2167 return (rs->rs_end); 2168 } 2169 2170 /* 2171 * Determine if a resilvering vdev should remove any DTL entries from 2172 * its range. If the vdev was resilvering for the entire duration of the 2173 * scan then it should excise that range from its DTLs. Otherwise, this 2174 * vdev is considered partially resilvered and should leave its DTL 2175 * entries intact. The comment in vdev_dtl_reassess() describes how we 2176 * excise the DTLs. 2177 */ 2178 static boolean_t 2179 vdev_dtl_should_excise(vdev_t *vd) 2180 { 2181 spa_t *spa = vd->vdev_spa; 2182 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2183 2184 ASSERT0(scn->scn_phys.scn_errors); 2185 ASSERT0(vd->vdev_children); 2186 2187 if (vd->vdev_state < VDEV_STATE_DEGRADED) 2188 return (B_FALSE); 2189 2190 if (vd->vdev_resilver_txg == 0 || 2191 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2192 return (B_TRUE); 2193 2194 /* 2195 * When a resilver is initiated the scan will assign the scn_max_txg 2196 * value to the highest txg value that exists in all DTLs. If this 2197 * device's max DTL is not part of this scan (i.e. it is not in 2198 * the range (scn_min_txg, scn_max_txg] then it is not eligible 2199 * for excision. 2200 */ 2201 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2202 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2203 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2204 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2205 return (B_TRUE); 2206 } 2207 return (B_FALSE); 2208 } 2209 2210 /* 2211 * Reassess DTLs after a config change or scrub completion. 2212 */ 2213 void 2214 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2215 { 2216 spa_t *spa = vd->vdev_spa; 2217 avl_tree_t reftree; 2218 int minref; 2219 2220 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2221 2222 for (int c = 0; c < vd->vdev_children; c++) 2223 vdev_dtl_reassess(vd->vdev_child[c], txg, 2224 scrub_txg, scrub_done); 2225 2226 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 2227 return; 2228 2229 if (vd->vdev_ops->vdev_op_leaf) { 2230 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2231 2232 mutex_enter(&vd->vdev_dtl_lock); 2233 2234 /* 2235 * If we've completed a scan cleanly then determine 2236 * if this vdev should remove any DTLs. We only want to 2237 * excise regions on vdevs that were available during 2238 * the entire duration of this scan. 2239 */ 2240 if (scrub_txg != 0 && 2241 (spa->spa_scrub_started || 2242 (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2243 vdev_dtl_should_excise(vd)) { 2244 /* 2245 * We completed a scrub up to scrub_txg. If we 2246 * did it without rebooting, then the scrub dtl 2247 * will be valid, so excise the old region and 2248 * fold in the scrub dtl. Otherwise, leave the 2249 * dtl as-is if there was an error. 2250 * 2251 * There's little trick here: to excise the beginning 2252 * of the DTL_MISSING map, we put it into a reference 2253 * tree and then add a segment with refcnt -1 that 2254 * covers the range [0, scrub_txg). This means 2255 * that each txg in that range has refcnt -1 or 0. 2256 * We then add DTL_SCRUB with a refcnt of 2, so that 2257 * entries in the range [0, scrub_txg) will have a 2258 * positive refcnt -- either 1 or 2. We then convert 2259 * the reference tree into the new DTL_MISSING map. 2260 */ 2261 space_reftree_create(&reftree); 2262 space_reftree_add_map(&reftree, 2263 vd->vdev_dtl[DTL_MISSING], 1); 2264 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 2265 space_reftree_add_map(&reftree, 2266 vd->vdev_dtl[DTL_SCRUB], 2); 2267 space_reftree_generate_map(&reftree, 2268 vd->vdev_dtl[DTL_MISSING], 1); 2269 space_reftree_destroy(&reftree); 2270 } 2271 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 2272 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2273 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2274 if (scrub_done) 2275 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 2276 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 2277 if (!vdev_readable(vd)) 2278 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 2279 else 2280 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2281 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2282 2283 /* 2284 * If the vdev was resilvering and no longer has any 2285 * DTLs then reset its resilvering flag. 2286 */ 2287 if (vd->vdev_resilver_txg != 0 && 2288 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 2289 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2290 vd->vdev_resilver_txg = 0; 2291 2292 mutex_exit(&vd->vdev_dtl_lock); 2293 2294 if (txg != 0) 2295 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2296 return; 2297 } 2298 2299 mutex_enter(&vd->vdev_dtl_lock); 2300 for (int t = 0; t < DTL_TYPES; t++) { 2301 /* account for child's outage in parent's missing map */ 2302 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 2303 if (t == DTL_SCRUB) 2304 continue; /* leaf vdevs only */ 2305 if (t == DTL_PARTIAL) 2306 minref = 1; /* i.e. non-zero */ 2307 else if (vd->vdev_nparity != 0) 2308 minref = vd->vdev_nparity + 1; /* RAID-Z */ 2309 else 2310 minref = vd->vdev_children; /* any kind of mirror */ 2311 space_reftree_create(&reftree); 2312 for (int c = 0; c < vd->vdev_children; c++) { 2313 vdev_t *cvd = vd->vdev_child[c]; 2314 mutex_enter(&cvd->vdev_dtl_lock); 2315 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 2316 mutex_exit(&cvd->vdev_dtl_lock); 2317 } 2318 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 2319 space_reftree_destroy(&reftree); 2320 } 2321 mutex_exit(&vd->vdev_dtl_lock); 2322 } 2323 2324 int 2325 vdev_dtl_load(vdev_t *vd) 2326 { 2327 spa_t *spa = vd->vdev_spa; 2328 objset_t *mos = spa->spa_meta_objset; 2329 int error = 0; 2330 2331 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 2332 ASSERT(vdev_is_concrete(vd)); 2333 2334 error = space_map_open(&vd->vdev_dtl_sm, mos, 2335 vd->vdev_dtl_object, 0, -1ULL, 0); 2336 if (error) 2337 return (error); 2338 ASSERT(vd->vdev_dtl_sm != NULL); 2339 2340 mutex_enter(&vd->vdev_dtl_lock); 2341 2342 /* 2343 * Now that we've opened the space_map we need to update 2344 * the in-core DTL. 2345 */ 2346 space_map_update(vd->vdev_dtl_sm); 2347 2348 error = space_map_load(vd->vdev_dtl_sm, 2349 vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 2350 mutex_exit(&vd->vdev_dtl_lock); 2351 2352 return (error); 2353 } 2354 2355 for (int c = 0; c < vd->vdev_children; c++) { 2356 error = vdev_dtl_load(vd->vdev_child[c]); 2357 if (error != 0) 2358 break; 2359 } 2360 2361 return (error); 2362 } 2363 2364 void 2365 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2366 { 2367 spa_t *spa = vd->vdev_spa; 2368 2369 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2370 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2371 zapobj, tx)); 2372 } 2373 2374 uint64_t 2375 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2376 { 2377 spa_t *spa = vd->vdev_spa; 2378 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2379 DMU_OT_NONE, 0, tx); 2380 2381 ASSERT(zap != 0); 2382 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2383 zap, tx)); 2384 2385 return (zap); 2386 } 2387 2388 void 2389 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2390 { 2391 if (vd->vdev_ops != &vdev_hole_ops && 2392 vd->vdev_ops != &vdev_missing_ops && 2393 vd->vdev_ops != &vdev_root_ops && 2394 !vd->vdev_top->vdev_removing) { 2395 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2396 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2397 } 2398 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2399 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2400 } 2401 } 2402 for (uint64_t i = 0; i < vd->vdev_children; i++) { 2403 vdev_construct_zaps(vd->vdev_child[i], tx); 2404 } 2405 } 2406 2407 void 2408 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2409 { 2410 spa_t *spa = vd->vdev_spa; 2411 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2412 objset_t *mos = spa->spa_meta_objset; 2413 range_tree_t *rtsync; 2414 dmu_tx_t *tx; 2415 uint64_t object = space_map_object(vd->vdev_dtl_sm); 2416 2417 ASSERT(vdev_is_concrete(vd)); 2418 ASSERT(vd->vdev_ops->vdev_op_leaf); 2419 2420 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2421 2422 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2423 mutex_enter(&vd->vdev_dtl_lock); 2424 space_map_free(vd->vdev_dtl_sm, tx); 2425 space_map_close(vd->vdev_dtl_sm); 2426 vd->vdev_dtl_sm = NULL; 2427 mutex_exit(&vd->vdev_dtl_lock); 2428 2429 /* 2430 * We only destroy the leaf ZAP for detached leaves or for 2431 * removed log devices. Removed data devices handle leaf ZAP 2432 * cleanup later, once cancellation is no longer possible. 2433 */ 2434 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2435 vd->vdev_top->vdev_islog)) { 2436 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2437 vd->vdev_leaf_zap = 0; 2438 } 2439 2440 dmu_tx_commit(tx); 2441 return; 2442 } 2443 2444 if (vd->vdev_dtl_sm == NULL) { 2445 uint64_t new_object; 2446 2447 new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 2448 VERIFY3U(new_object, !=, 0); 2449 2450 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2451 0, -1ULL, 0)); 2452 ASSERT(vd->vdev_dtl_sm != NULL); 2453 } 2454 2455 rtsync = range_tree_create(NULL, NULL); 2456 2457 mutex_enter(&vd->vdev_dtl_lock); 2458 range_tree_walk(rt, range_tree_add, rtsync); 2459 mutex_exit(&vd->vdev_dtl_lock); 2460 2461 space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 2462 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 2463 range_tree_vacate(rtsync, NULL, NULL); 2464 2465 range_tree_destroy(rtsync); 2466 2467 /* 2468 * If the object for the space map has changed then dirty 2469 * the top level so that we update the config. 2470 */ 2471 if (object != space_map_object(vd->vdev_dtl_sm)) { 2472 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 2473 "new object %llu", (u_longlong_t)txg, spa_name(spa), 2474 (u_longlong_t)object, 2475 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 2476 vdev_config_dirty(vd->vdev_top); 2477 } 2478 2479 dmu_tx_commit(tx); 2480 2481 mutex_enter(&vd->vdev_dtl_lock); 2482 space_map_update(vd->vdev_dtl_sm); 2483 mutex_exit(&vd->vdev_dtl_lock); 2484 } 2485 2486 /* 2487 * Determine whether the specified vdev can be offlined/detached/removed 2488 * without losing data. 2489 */ 2490 boolean_t 2491 vdev_dtl_required(vdev_t *vd) 2492 { 2493 spa_t *spa = vd->vdev_spa; 2494 vdev_t *tvd = vd->vdev_top; 2495 uint8_t cant_read = vd->vdev_cant_read; 2496 boolean_t required; 2497 2498 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2499 2500 if (vd == spa->spa_root_vdev || vd == tvd) 2501 return (B_TRUE); 2502 2503 /* 2504 * Temporarily mark the device as unreadable, and then determine 2505 * whether this results in any DTL outages in the top-level vdev. 2506 * If not, we can safely offline/detach/remove the device. 2507 */ 2508 vd->vdev_cant_read = B_TRUE; 2509 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2510 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2511 vd->vdev_cant_read = cant_read; 2512 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2513 2514 if (!required && zio_injection_enabled) 2515 required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2516 2517 return (required); 2518 } 2519 2520 /* 2521 * Determine if resilver is needed, and if so the txg range. 2522 */ 2523 boolean_t 2524 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2525 { 2526 boolean_t needed = B_FALSE; 2527 uint64_t thismin = UINT64_MAX; 2528 uint64_t thismax = 0; 2529 2530 if (vd->vdev_children == 0) { 2531 mutex_enter(&vd->vdev_dtl_lock); 2532 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 2533 vdev_writeable(vd)) { 2534 2535 thismin = vdev_dtl_min(vd); 2536 thismax = vdev_dtl_max(vd); 2537 needed = B_TRUE; 2538 } 2539 mutex_exit(&vd->vdev_dtl_lock); 2540 } else { 2541 for (int c = 0; c < vd->vdev_children; c++) { 2542 vdev_t *cvd = vd->vdev_child[c]; 2543 uint64_t cmin, cmax; 2544 2545 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2546 thismin = MIN(thismin, cmin); 2547 thismax = MAX(thismax, cmax); 2548 needed = B_TRUE; 2549 } 2550 } 2551 } 2552 2553 if (needed && minp) { 2554 *minp = thismin; 2555 *maxp = thismax; 2556 } 2557 return (needed); 2558 } 2559 2560 /* 2561 * Gets the checkpoint space map object from the vdev's ZAP. 2562 * Returns the spacemap object, or 0 if it wasn't in the ZAP 2563 * or the ZAP doesn't exist yet. 2564 */ 2565 int 2566 vdev_checkpoint_sm_object(vdev_t *vd) 2567 { 2568 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 2569 if (vd->vdev_top_zap == 0) { 2570 return (0); 2571 } 2572 2573 uint64_t sm_obj = 0; 2574 int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 2575 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 2576 2577 ASSERT(err == 0 || err == ENOENT); 2578 2579 return (sm_obj); 2580 } 2581 2582 int 2583 vdev_load(vdev_t *vd) 2584 { 2585 int error = 0; 2586 /* 2587 * Recursively load all children. 2588 */ 2589 for (int c = 0; c < vd->vdev_children; c++) { 2590 error = vdev_load(vd->vdev_child[c]); 2591 if (error != 0) { 2592 return (error); 2593 } 2594 } 2595 2596 vdev_set_deflate_ratio(vd); 2597 2598 /* 2599 * If this is a top-level vdev, initialize its metaslabs. 2600 */ 2601 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2602 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 2603 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2604 VDEV_AUX_CORRUPT_DATA); 2605 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 2606 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 2607 (u_longlong_t)vd->vdev_asize); 2608 return (SET_ERROR(ENXIO)); 2609 } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 2610 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 2611 "[error=%d]", error); 2612 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2613 VDEV_AUX_CORRUPT_DATA); 2614 return (error); 2615 } 2616 2617 uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 2618 if (checkpoint_sm_obj != 0) { 2619 objset_t *mos = spa_meta_objset(vd->vdev_spa); 2620 ASSERT(vd->vdev_asize != 0); 2621 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 2622 2623 if ((error = space_map_open(&vd->vdev_checkpoint_sm, 2624 mos, checkpoint_sm_obj, 0, vd->vdev_asize, 2625 vd->vdev_ashift))) { 2626 vdev_dbgmsg(vd, "vdev_load: space_map_open " 2627 "failed for checkpoint spacemap (obj %llu) " 2628 "[error=%d]", 2629 (u_longlong_t)checkpoint_sm_obj, error); 2630 return (error); 2631 } 2632 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 2633 space_map_update(vd->vdev_checkpoint_sm); 2634 2635 /* 2636 * Since the checkpoint_sm contains free entries 2637 * exclusively we can use sm_alloc to indicate the 2638 * culmulative checkpointed space that has been freed. 2639 */ 2640 vd->vdev_stat.vs_checkpoint_space = 2641 -vd->vdev_checkpoint_sm->sm_alloc; 2642 vd->vdev_spa->spa_checkpoint_info.sci_dspace += 2643 vd->vdev_stat.vs_checkpoint_space; 2644 } 2645 } 2646 2647 /* 2648 * If this is a leaf vdev, load its DTL. 2649 */ 2650 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2651 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2652 VDEV_AUX_CORRUPT_DATA); 2653 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 2654 "[error=%d]", error); 2655 return (error); 2656 } 2657 2658 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 2659 if (obsolete_sm_object != 0) { 2660 objset_t *mos = vd->vdev_spa->spa_meta_objset; 2661 ASSERT(vd->vdev_asize != 0); 2662 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 2663 2664 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 2665 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 2666 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2667 VDEV_AUX_CORRUPT_DATA); 2668 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 2669 "obsolete spacemap (obj %llu) [error=%d]", 2670 (u_longlong_t)obsolete_sm_object, error); 2671 return (error); 2672 } 2673 space_map_update(vd->vdev_obsolete_sm); 2674 } 2675 2676 return (0); 2677 } 2678 2679 /* 2680 * The special vdev case is used for hot spares and l2cache devices. Its 2681 * sole purpose it to set the vdev state for the associated vdev. To do this, 2682 * we make sure that we can open the underlying device, then try to read the 2683 * label, and make sure that the label is sane and that it hasn't been 2684 * repurposed to another pool. 2685 */ 2686 int 2687 vdev_validate_aux(vdev_t *vd) 2688 { 2689 nvlist_t *label; 2690 uint64_t guid, version; 2691 uint64_t state; 2692 2693 if (!vdev_readable(vd)) 2694 return (0); 2695 2696 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 2697 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2698 VDEV_AUX_CORRUPT_DATA); 2699 return (-1); 2700 } 2701 2702 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2703 !SPA_VERSION_IS_SUPPORTED(version) || 2704 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 2705 guid != vd->vdev_guid || 2706 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 2707 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2708 VDEV_AUX_CORRUPT_DATA); 2709 nvlist_free(label); 2710 return (-1); 2711 } 2712 2713 /* 2714 * We don't actually check the pool state here. If it's in fact in 2715 * use by another pool, we update this fact on the fly when requested. 2716 */ 2717 nvlist_free(label); 2718 return (0); 2719 } 2720 2721 /* 2722 * Free the objects used to store this vdev's spacemaps, and the array 2723 * that points to them. 2724 */ 2725 void 2726 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 2727 { 2728 if (vd->vdev_ms_array == 0) 2729 return; 2730 2731 objset_t *mos = vd->vdev_spa->spa_meta_objset; 2732 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 2733 size_t array_bytes = array_count * sizeof (uint64_t); 2734 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 2735 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 2736 array_bytes, smobj_array, 0)); 2737 2738 for (uint64_t i = 0; i < array_count; i++) { 2739 uint64_t smobj = smobj_array[i]; 2740 if (smobj == 0) 2741 continue; 2742 2743 space_map_free_obj(mos, smobj, tx); 2744 } 2745 2746 kmem_free(smobj_array, array_bytes); 2747 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 2748 vd->vdev_ms_array = 0; 2749 } 2750 2751 static void 2752 vdev_remove_empty(vdev_t *vd, uint64_t txg) 2753 { 2754 spa_t *spa = vd->vdev_spa; 2755 dmu_tx_t *tx; 2756 2757 ASSERT(vd == vd->vdev_top); 2758 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 2759 2760 if (vd->vdev_ms != NULL) { 2761 metaslab_group_t *mg = vd->vdev_mg; 2762 2763 metaslab_group_histogram_verify(mg); 2764 metaslab_class_histogram_verify(mg->mg_class); 2765 2766 for (int m = 0; m < vd->vdev_ms_count; m++) { 2767 metaslab_t *msp = vd->vdev_ms[m]; 2768 2769 if (msp == NULL || msp->ms_sm == NULL) 2770 continue; 2771 2772 mutex_enter(&msp->ms_lock); 2773 /* 2774 * If the metaslab was not loaded when the vdev 2775 * was removed then the histogram accounting may 2776 * not be accurate. Update the histogram information 2777 * here so that we ensure that the metaslab group 2778 * and metaslab class are up-to-date. 2779 */ 2780 metaslab_group_histogram_remove(mg, msp); 2781 2782 VERIFY0(space_map_allocated(msp->ms_sm)); 2783 space_map_close(msp->ms_sm); 2784 msp->ms_sm = NULL; 2785 mutex_exit(&msp->ms_lock); 2786 } 2787 2788 if (vd->vdev_checkpoint_sm != NULL) { 2789 ASSERT(spa_has_checkpoint(spa)); 2790 space_map_close(vd->vdev_checkpoint_sm); 2791 vd->vdev_checkpoint_sm = NULL; 2792 } 2793 2794 metaslab_group_histogram_verify(mg); 2795 metaslab_class_histogram_verify(mg->mg_class); 2796 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 2797 ASSERT0(mg->mg_histogram[i]); 2798 } 2799 2800 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2801 vdev_destroy_spacemaps(vd, tx); 2802 2803 if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2804 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2805 vd->vdev_top_zap = 0; 2806 } 2807 dmu_tx_commit(tx); 2808 } 2809 2810 void 2811 vdev_sync_done(vdev_t *vd, uint64_t txg) 2812 { 2813 metaslab_t *msp; 2814 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2815 2816 ASSERT(vdev_is_concrete(vd)); 2817 2818 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2819 metaslab_sync_done(msp, txg); 2820 2821 if (reassess) 2822 metaslab_sync_reassess(vd->vdev_mg); 2823 } 2824 2825 void 2826 vdev_sync(vdev_t *vd, uint64_t txg) 2827 { 2828 spa_t *spa = vd->vdev_spa; 2829 vdev_t *lvd; 2830 metaslab_t *msp; 2831 dmu_tx_t *tx; 2832 2833 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 2834 dmu_tx_t *tx; 2835 2836 ASSERT(vd->vdev_removing || 2837 vd->vdev_ops == &vdev_indirect_ops); 2838 2839 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2840 vdev_indirect_sync_obsolete(vd, tx); 2841 dmu_tx_commit(tx); 2842 2843 /* 2844 * If the vdev is indirect, it can't have dirty 2845 * metaslabs or DTLs. 2846 */ 2847 if (vd->vdev_ops == &vdev_indirect_ops) { 2848 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 2849 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 2850 return; 2851 } 2852 } 2853 2854 ASSERT(vdev_is_concrete(vd)); 2855 2856 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 2857 !vd->vdev_removing) { 2858 ASSERT(vd == vd->vdev_top); 2859 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 2860 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2861 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2862 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2863 ASSERT(vd->vdev_ms_array != 0); 2864 vdev_config_dirty(vd); 2865 dmu_tx_commit(tx); 2866 } 2867 2868 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2869 metaslab_sync(msp, txg); 2870 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2871 } 2872 2873 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2874 vdev_dtl_sync(lvd, txg); 2875 2876 /* 2877 * Remove the metadata associated with this vdev once it's empty. 2878 * Note that this is typically used for log/cache device removal; 2879 * we don't empty toplevel vdevs when removing them. But if 2880 * a toplevel happens to be emptied, this is not harmful. 2881 */ 2882 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) { 2883 vdev_remove_empty(vd, txg); 2884 } 2885 2886 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2887 } 2888 2889 uint64_t 2890 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2891 { 2892 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2893 } 2894 2895 /* 2896 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 2897 * not be opened, and no I/O is attempted. 2898 */ 2899 int 2900 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2901 { 2902 vdev_t *vd, *tvd; 2903 2904 spa_vdev_state_enter(spa, SCL_NONE); 2905 2906 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2907 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2908 2909 if (!vd->vdev_ops->vdev_op_leaf) 2910 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2911 2912 tvd = vd->vdev_top; 2913 2914 /* 2915 * We don't directly use the aux state here, but if we do a 2916 * vdev_reopen(), we need this value to be present to remember why we 2917 * were faulted. 2918 */ 2919 vd->vdev_label_aux = aux; 2920 2921 /* 2922 * Faulted state takes precedence over degraded. 2923 */ 2924 vd->vdev_delayed_close = B_FALSE; 2925 vd->vdev_faulted = 1ULL; 2926 vd->vdev_degraded = 0ULL; 2927 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 2928 2929 /* 2930 * If this device has the only valid copy of the data, then 2931 * back off and simply mark the vdev as degraded instead. 2932 */ 2933 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 2934 vd->vdev_degraded = 1ULL; 2935 vd->vdev_faulted = 0ULL; 2936 2937 /* 2938 * If we reopen the device and it's not dead, only then do we 2939 * mark it degraded. 2940 */ 2941 vdev_reopen(tvd); 2942 2943 if (vdev_readable(vd)) 2944 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 2945 } 2946 2947 return (spa_vdev_state_exit(spa, vd, 0)); 2948 } 2949 2950 /* 2951 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 2952 * user that something is wrong. The vdev continues to operate as normal as far 2953 * as I/O is concerned. 2954 */ 2955 int 2956 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2957 { 2958 vdev_t *vd; 2959 2960 spa_vdev_state_enter(spa, SCL_NONE); 2961 2962 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2963 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2964 2965 if (!vd->vdev_ops->vdev_op_leaf) 2966 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2967 2968 /* 2969 * If the vdev is already faulted, then don't do anything. 2970 */ 2971 if (vd->vdev_faulted || vd->vdev_degraded) 2972 return (spa_vdev_state_exit(spa, NULL, 0)); 2973 2974 vd->vdev_degraded = 1ULL; 2975 if (!vdev_is_dead(vd)) 2976 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2977 aux); 2978 2979 return (spa_vdev_state_exit(spa, vd, 0)); 2980 } 2981 2982 /* 2983 * Online the given vdev. 2984 * 2985 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2986 * spare device should be detached when the device finishes resilvering. 2987 * Second, the online should be treated like a 'test' online case, so no FMA 2988 * events are generated if the device fails to open. 2989 */ 2990 int 2991 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2992 { 2993 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2994 boolean_t wasoffline; 2995 vdev_state_t oldstate; 2996 2997 spa_vdev_state_enter(spa, SCL_NONE); 2998 2999 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3000 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3001 3002 if (!vd->vdev_ops->vdev_op_leaf) 3003 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3004 3005 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 3006 oldstate = vd->vdev_state; 3007 3008 tvd = vd->vdev_top; 3009 vd->vdev_offline = B_FALSE; 3010 vd->vdev_tmpoffline = B_FALSE; 3011 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3012 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3013 3014 /* XXX - L2ARC 1.0 does not support expansion */ 3015 if (!vd->vdev_aux) { 3016 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3017 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3018 } 3019 3020 vdev_reopen(tvd); 3021 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 3022 3023 if (!vd->vdev_aux) { 3024 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3025 pvd->vdev_expanding = B_FALSE; 3026 } 3027 3028 if (newstate) 3029 *newstate = vd->vdev_state; 3030 if ((flags & ZFS_ONLINE_UNSPARE) && 3031 !vdev_is_dead(vd) && vd->vdev_parent && 3032 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3033 vd->vdev_parent->vdev_child[0] == vd) 3034 vd->vdev_unspare = B_TRUE; 3035 3036 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3037 3038 /* XXX - L2ARC 1.0 does not support expansion */ 3039 if (vd->vdev_aux) 3040 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3041 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3042 } 3043 3044 if (wasoffline || 3045 (oldstate < VDEV_STATE_DEGRADED && 3046 vd->vdev_state >= VDEV_STATE_DEGRADED)) 3047 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 3048 3049 return (spa_vdev_state_exit(spa, vd, 0)); 3050 } 3051 3052 static int 3053 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3054 { 3055 vdev_t *vd, *tvd; 3056 int error = 0; 3057 uint64_t generation; 3058 metaslab_group_t *mg; 3059 3060 top: 3061 spa_vdev_state_enter(spa, SCL_ALLOC); 3062 3063 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3064 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3065 3066 if (!vd->vdev_ops->vdev_op_leaf) 3067 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3068 3069 tvd = vd->vdev_top; 3070 mg = tvd->vdev_mg; 3071 generation = spa->spa_config_generation + 1; 3072 3073 /* 3074 * If the device isn't already offline, try to offline it. 3075 */ 3076 if (!vd->vdev_offline) { 3077 /* 3078 * If this device has the only valid copy of some data, 3079 * don't allow it to be offlined. Log devices are always 3080 * expendable. 3081 */ 3082 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3083 vdev_dtl_required(vd)) 3084 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3085 3086 /* 3087 * If the top-level is a slog and it has had allocations 3088 * then proceed. We check that the vdev's metaslab group 3089 * is not NULL since it's possible that we may have just 3090 * added this vdev but not yet initialized its metaslabs. 3091 */ 3092 if (tvd->vdev_islog && mg != NULL) { 3093 /* 3094 * Prevent any future allocations. 3095 */ 3096 metaslab_group_passivate(mg); 3097 (void) spa_vdev_state_exit(spa, vd, 0); 3098 3099 error = spa_reset_logs(spa); 3100 3101 /* 3102 * If the log device was successfully reset but has 3103 * checkpointed data, do not offline it. 3104 */ 3105 if (error == 0 && 3106 tvd->vdev_checkpoint_sm != NULL) { 3107 ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, 3108 !=, 0); 3109 error = ZFS_ERR_CHECKPOINT_EXISTS; 3110 } 3111 3112 spa_vdev_state_enter(spa, SCL_ALLOC); 3113 3114 /* 3115 * Check to see if the config has changed. 3116 */ 3117 if (error || generation != spa->spa_config_generation) { 3118 metaslab_group_activate(mg); 3119 if (error) 3120 return (spa_vdev_state_exit(spa, 3121 vd, error)); 3122 (void) spa_vdev_state_exit(spa, vd, 0); 3123 goto top; 3124 } 3125 ASSERT0(tvd->vdev_stat.vs_alloc); 3126 } 3127 3128 /* 3129 * Offline this device and reopen its top-level vdev. 3130 * If the top-level vdev is a log device then just offline 3131 * it. Otherwise, if this action results in the top-level 3132 * vdev becoming unusable, undo it and fail the request. 3133 */ 3134 vd->vdev_offline = B_TRUE; 3135 vdev_reopen(tvd); 3136 3137 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3138 vdev_is_dead(tvd)) { 3139 vd->vdev_offline = B_FALSE; 3140 vdev_reopen(tvd); 3141 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3142 } 3143 3144 /* 3145 * Add the device back into the metaslab rotor so that 3146 * once we online the device it's open for business. 3147 */ 3148 if (tvd->vdev_islog && mg != NULL) 3149 metaslab_group_activate(mg); 3150 } 3151 3152 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3153 3154 return (spa_vdev_state_exit(spa, vd, 0)); 3155 } 3156 3157 int 3158 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3159 { 3160 int error; 3161 3162 mutex_enter(&spa->spa_vdev_top_lock); 3163 error = vdev_offline_locked(spa, guid, flags); 3164 mutex_exit(&spa->spa_vdev_top_lock); 3165 3166 return (error); 3167 } 3168 3169 /* 3170 * Clear the error counts associated with this vdev. Unlike vdev_online() and 3171 * vdev_offline(), we assume the spa config is locked. We also clear all 3172 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3173 */ 3174 void 3175 vdev_clear(spa_t *spa, vdev_t *vd) 3176 { 3177 vdev_t *rvd = spa->spa_root_vdev; 3178 3179 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3180 3181 if (vd == NULL) 3182 vd = rvd; 3183 3184 vd->vdev_stat.vs_read_errors = 0; 3185 vd->vdev_stat.vs_write_errors = 0; 3186 vd->vdev_stat.vs_checksum_errors = 0; 3187 3188 for (int c = 0; c < vd->vdev_children; c++) 3189 vdev_clear(spa, vd->vdev_child[c]); 3190 3191 /* 3192 * It makes no sense to "clear" an indirect vdev. 3193 */ 3194 if (!vdev_is_concrete(vd)) 3195 return; 3196 3197 /* 3198 * If we're in the FAULTED state or have experienced failed I/O, then 3199 * clear the persistent state and attempt to reopen the device. We 3200 * also mark the vdev config dirty, so that the new faulted state is 3201 * written out to disk. 3202 */ 3203 if (vd->vdev_faulted || vd->vdev_degraded || 3204 !vdev_readable(vd) || !vdev_writeable(vd)) { 3205 3206 /* 3207 * When reopening in reponse to a clear event, it may be due to 3208 * a fmadm repair request. In this case, if the device is 3209 * still broken, we want to still post the ereport again. 3210 */ 3211 vd->vdev_forcefault = B_TRUE; 3212 3213 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3214 vd->vdev_cant_read = B_FALSE; 3215 vd->vdev_cant_write = B_FALSE; 3216 3217 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 3218 3219 vd->vdev_forcefault = B_FALSE; 3220 3221 if (vd != rvd && vdev_writeable(vd->vdev_top)) 3222 vdev_state_dirty(vd->vdev_top); 3223 3224 if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3225 spa_async_request(spa, SPA_ASYNC_RESILVER); 3226 3227 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 3228 } 3229 3230 /* 3231 * When clearing a FMA-diagnosed fault, we always want to 3232 * unspare the device, as we assume that the original spare was 3233 * done in response to the FMA fault. 3234 */ 3235 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3236 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3237 vd->vdev_parent->vdev_child[0] == vd) 3238 vd->vdev_unspare = B_TRUE; 3239 } 3240 3241 boolean_t 3242 vdev_is_dead(vdev_t *vd) 3243 { 3244 /* 3245 * Holes and missing devices are always considered "dead". 3246 * This simplifies the code since we don't have to check for 3247 * these types of devices in the various code paths. 3248 * Instead we rely on the fact that we skip over dead devices 3249 * before issuing I/O to them. 3250 */ 3251 return (vd->vdev_state < VDEV_STATE_DEGRADED || 3252 vd->vdev_ops == &vdev_hole_ops || 3253 vd->vdev_ops == &vdev_missing_ops); 3254 } 3255 3256 boolean_t 3257 vdev_readable(vdev_t *vd) 3258 { 3259 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 3260 } 3261 3262 boolean_t 3263 vdev_writeable(vdev_t *vd) 3264 { 3265 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 3266 vdev_is_concrete(vd)); 3267 } 3268 3269 boolean_t 3270 vdev_allocatable(vdev_t *vd) 3271 { 3272 uint64_t state = vd->vdev_state; 3273 3274 /* 3275 * We currently allow allocations from vdevs which may be in the 3276 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3277 * fails to reopen then we'll catch it later when we're holding 3278 * the proper locks. Note that we have to get the vdev state 3279 * in a local variable because although it changes atomically, 3280 * we're asking two separate questions about it. 3281 */ 3282 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 3283 !vd->vdev_cant_write && vdev_is_concrete(vd) && 3284 vd->vdev_mg->mg_initialized); 3285 } 3286 3287 boolean_t 3288 vdev_accessible(vdev_t *vd, zio_t *zio) 3289 { 3290 ASSERT(zio->io_vd == vd); 3291 3292 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3293 return (B_FALSE); 3294 3295 if (zio->io_type == ZIO_TYPE_READ) 3296 return (!vd->vdev_cant_read); 3297 3298 if (zio->io_type == ZIO_TYPE_WRITE) 3299 return (!vd->vdev_cant_write); 3300 3301 return (B_TRUE); 3302 } 3303 3304 boolean_t 3305 vdev_is_spacemap_addressable(vdev_t *vd) 3306 { 3307 /* 3308 * Assuming 47 bits of the space map entry dedicated for the entry's 3309 * offset (see description in space_map.h), we calculate the maximum 3310 * address that can be described by a space map entry for the given 3311 * device. 3312 */ 3313 uint64_t shift = vd->vdev_ashift + 47; 3314 3315 if (shift >= 63) /* detect potential overflow */ 3316 return (B_TRUE); 3317 3318 return (vd->vdev_asize < (1ULL << shift)); 3319 } 3320 3321 /* 3322 * Get statistics for the given vdev. 3323 */ 3324 void 3325 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3326 { 3327 spa_t *spa = vd->vdev_spa; 3328 vdev_t *rvd = spa->spa_root_vdev; 3329 vdev_t *tvd = vd->vdev_top; 3330 3331 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3332 3333 mutex_enter(&vd->vdev_stat_lock); 3334 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3335 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3336 vs->vs_state = vd->vdev_state; 3337 vs->vs_rsize = vdev_get_min_asize(vd); 3338 if (vd->vdev_ops->vdev_op_leaf) 3339 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3340 /* 3341 * Report expandable space on top-level, non-auxillary devices only. 3342 * The expandable space is reported in terms of metaslab sized units 3343 * since that determines how much space the pool can expand. 3344 */ 3345 if (vd->vdev_aux == NULL && tvd != NULL) { 3346 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 3347 spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3348 } 3349 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 3350 vdev_is_concrete(vd)) { 3351 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 3352 } 3353 3354 /* 3355 * If we're getting stats on the root vdev, aggregate the I/O counts 3356 * over all top-level vdevs (i.e. the direct children of the root). 3357 */ 3358 if (vd == rvd) { 3359 for (int c = 0; c < rvd->vdev_children; c++) { 3360 vdev_t *cvd = rvd->vdev_child[c]; 3361 vdev_stat_t *cvs = &cvd->vdev_stat; 3362 3363 for (int t = 0; t < ZIO_TYPES; t++) { 3364 vs->vs_ops[t] += cvs->vs_ops[t]; 3365 vs->vs_bytes[t] += cvs->vs_bytes[t]; 3366 } 3367 cvs->vs_scan_removing = cvd->vdev_removing; 3368 } 3369 } 3370 mutex_exit(&vd->vdev_stat_lock); 3371 } 3372 3373 void 3374 vdev_clear_stats(vdev_t *vd) 3375 { 3376 mutex_enter(&vd->vdev_stat_lock); 3377 vd->vdev_stat.vs_space = 0; 3378 vd->vdev_stat.vs_dspace = 0; 3379 vd->vdev_stat.vs_alloc = 0; 3380 mutex_exit(&vd->vdev_stat_lock); 3381 } 3382 3383 void 3384 vdev_scan_stat_init(vdev_t *vd) 3385 { 3386 vdev_stat_t *vs = &vd->vdev_stat; 3387 3388 for (int c = 0; c < vd->vdev_children; c++) 3389 vdev_scan_stat_init(vd->vdev_child[c]); 3390 3391 mutex_enter(&vd->vdev_stat_lock); 3392 vs->vs_scan_processed = 0; 3393 mutex_exit(&vd->vdev_stat_lock); 3394 } 3395 3396 void 3397 vdev_stat_update(zio_t *zio, uint64_t psize) 3398 { 3399 spa_t *spa = zio->io_spa; 3400 vdev_t *rvd = spa->spa_root_vdev; 3401 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3402 vdev_t *pvd; 3403 uint64_t txg = zio->io_txg; 3404 vdev_stat_t *vs = &vd->vdev_stat; 3405 zio_type_t type = zio->io_type; 3406 int flags = zio->io_flags; 3407 3408 /* 3409 * If this i/o is a gang leader, it didn't do any actual work. 3410 */ 3411 if (zio->io_gang_tree) 3412 return; 3413 3414 if (zio->io_error == 0) { 3415 /* 3416 * If this is a root i/o, don't count it -- we've already 3417 * counted the top-level vdevs, and vdev_get_stats() will 3418 * aggregate them when asked. This reduces contention on 3419 * the root vdev_stat_lock and implicitly handles blocks 3420 * that compress away to holes, for which there is no i/o. 3421 * (Holes never create vdev children, so all the counters 3422 * remain zero, which is what we want.) 3423 * 3424 * Note: this only applies to successful i/o (io_error == 0) 3425 * because unlike i/o counts, errors are not additive. 3426 * When reading a ditto block, for example, failure of 3427 * one top-level vdev does not imply a root-level error. 3428 */ 3429 if (vd == rvd) 3430 return; 3431 3432 ASSERT(vd == zio->io_vd); 3433 3434 if (flags & ZIO_FLAG_IO_BYPASS) 3435 return; 3436 3437 mutex_enter(&vd->vdev_stat_lock); 3438 3439 if (flags & ZIO_FLAG_IO_REPAIR) { 3440 if (flags & ZIO_FLAG_SCAN_THREAD) { 3441 dsl_scan_phys_t *scn_phys = 3442 &spa->spa_dsl_pool->dp_scan->scn_phys; 3443 uint64_t *processed = &scn_phys->scn_processed; 3444 3445 /* XXX cleanup? */ 3446 if (vd->vdev_ops->vdev_op_leaf) 3447 atomic_add_64(processed, psize); 3448 vs->vs_scan_processed += psize; 3449 } 3450 3451 if (flags & ZIO_FLAG_SELF_HEAL) 3452 vs->vs_self_healed += psize; 3453 } 3454 3455 vs->vs_ops[type]++; 3456 vs->vs_bytes[type] += psize; 3457 3458 mutex_exit(&vd->vdev_stat_lock); 3459 return; 3460 } 3461 3462 if (flags & ZIO_FLAG_SPECULATIVE) 3463 return; 3464 3465 /* 3466 * If this is an I/O error that is going to be retried, then ignore the 3467 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 3468 * hard errors, when in reality they can happen for any number of 3469 * innocuous reasons (bus resets, MPxIO link failure, etc). 3470 */ 3471 if (zio->io_error == EIO && 3472 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 3473 return; 3474 3475 /* 3476 * Intent logs writes won't propagate their error to the root 3477 * I/O so don't mark these types of failures as pool-level 3478 * errors. 3479 */ 3480 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 3481 return; 3482 3483 mutex_enter(&vd->vdev_stat_lock); 3484 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3485 if (zio->io_error == ECKSUM) 3486 vs->vs_checksum_errors++; 3487 else 3488 vs->vs_read_errors++; 3489 } 3490 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3491 vs->vs_write_errors++; 3492 mutex_exit(&vd->vdev_stat_lock); 3493 3494 if (spa->spa_load_state == SPA_LOAD_NONE && 3495 type == ZIO_TYPE_WRITE && txg != 0 && 3496 (!(flags & ZIO_FLAG_IO_REPAIR) || 3497 (flags & ZIO_FLAG_SCAN_THREAD) || 3498 spa->spa_claiming)) { 3499 /* 3500 * This is either a normal write (not a repair), or it's 3501 * a repair induced by the scrub thread, or it's a repair 3502 * made by zil_claim() during spa_load() in the first txg. 3503 * In the normal case, we commit the DTL change in the same 3504 * txg as the block was born. In the scrub-induced repair 3505 * case, we know that scrubs run in first-pass syncing context, 3506 * so we commit the DTL change in spa_syncing_txg(spa). 3507 * In the zil_claim() case, we commit in spa_first_txg(spa). 3508 * 3509 * We currently do not make DTL entries for failed spontaneous 3510 * self-healing writes triggered by normal (non-scrubbing) 3511 * reads, because we have no transactional context in which to 3512 * do so -- and it's not clear that it'd be desirable anyway. 3513 */ 3514 if (vd->vdev_ops->vdev_op_leaf) { 3515 uint64_t commit_txg = txg; 3516 if (flags & ZIO_FLAG_SCAN_THREAD) { 3517 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3518 ASSERT(spa_sync_pass(spa) == 1); 3519 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3520 commit_txg = spa_syncing_txg(spa); 3521 } else if (spa->spa_claiming) { 3522 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3523 commit_txg = spa_first_txg(spa); 3524 } 3525 ASSERT(commit_txg >= spa_syncing_txg(spa)); 3526 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3527 return; 3528 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3529 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 3530 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3531 } 3532 if (vd != rvd) 3533 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3534 } 3535 } 3536 3537 /* 3538 * Update the in-core space usage stats for this vdev, its metaslab class, 3539 * and the root vdev. 3540 */ 3541 void 3542 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3543 int64_t space_delta) 3544 { 3545 int64_t dspace_delta = space_delta; 3546 spa_t *spa = vd->vdev_spa; 3547 vdev_t *rvd = spa->spa_root_vdev; 3548 metaslab_group_t *mg = vd->vdev_mg; 3549 metaslab_class_t *mc = mg ? mg->mg_class : NULL; 3550 3551 ASSERT(vd == vd->vdev_top); 3552 3553 /* 3554 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 3555 * factor. We must calculate this here and not at the root vdev 3556 * because the root vdev's psize-to-asize is simply the max of its 3557 * childrens', thus not accurate enough for us. 3558 */ 3559 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 3560 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 3561 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 3562 vd->vdev_deflate_ratio; 3563 3564 mutex_enter(&vd->vdev_stat_lock); 3565 vd->vdev_stat.vs_alloc += alloc_delta; 3566 vd->vdev_stat.vs_space += space_delta; 3567 vd->vdev_stat.vs_dspace += dspace_delta; 3568 mutex_exit(&vd->vdev_stat_lock); 3569 3570 if (mc == spa_normal_class(spa)) { 3571 mutex_enter(&rvd->vdev_stat_lock); 3572 rvd->vdev_stat.vs_alloc += alloc_delta; 3573 rvd->vdev_stat.vs_space += space_delta; 3574 rvd->vdev_stat.vs_dspace += dspace_delta; 3575 mutex_exit(&rvd->vdev_stat_lock); 3576 } 3577 3578 if (mc != NULL) { 3579 ASSERT(rvd == vd->vdev_parent); 3580 ASSERT(vd->vdev_ms_count != 0); 3581 3582 metaslab_class_space_update(mc, 3583 alloc_delta, defer_delta, space_delta, dspace_delta); 3584 } 3585 } 3586 3587 /* 3588 * Mark a top-level vdev's config as dirty, placing it on the dirty list 3589 * so that it will be written out next time the vdev configuration is synced. 3590 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3591 */ 3592 void 3593 vdev_config_dirty(vdev_t *vd) 3594 { 3595 spa_t *spa = vd->vdev_spa; 3596 vdev_t *rvd = spa->spa_root_vdev; 3597 int c; 3598 3599 ASSERT(spa_writeable(spa)); 3600 3601 /* 3602 * If this is an aux vdev (as with l2cache and spare devices), then we 3603 * update the vdev config manually and set the sync flag. 3604 */ 3605 if (vd->vdev_aux != NULL) { 3606 spa_aux_vdev_t *sav = vd->vdev_aux; 3607 nvlist_t **aux; 3608 uint_t naux; 3609 3610 for (c = 0; c < sav->sav_count; c++) { 3611 if (sav->sav_vdevs[c] == vd) 3612 break; 3613 } 3614 3615 if (c == sav->sav_count) { 3616 /* 3617 * We're being removed. There's nothing more to do. 3618 */ 3619 ASSERT(sav->sav_sync == B_TRUE); 3620 return; 3621 } 3622 3623 sav->sav_sync = B_TRUE; 3624 3625 if (nvlist_lookup_nvlist_array(sav->sav_config, 3626 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 3627 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 3628 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 3629 } 3630 3631 ASSERT(c < naux); 3632 3633 /* 3634 * Setting the nvlist in the middle if the array is a little 3635 * sketchy, but it will work. 3636 */ 3637 nvlist_free(aux[c]); 3638 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3639 3640 return; 3641 } 3642 3643 /* 3644 * The dirty list is protected by the SCL_CONFIG lock. The caller 3645 * must either hold SCL_CONFIG as writer, or must be the sync thread 3646 * (which holds SCL_CONFIG as reader). There's only one sync thread, 3647 * so this is sufficient to ensure mutual exclusion. 3648 */ 3649 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3650 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3651 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3652 3653 if (vd == rvd) { 3654 for (c = 0; c < rvd->vdev_children; c++) 3655 vdev_config_dirty(rvd->vdev_child[c]); 3656 } else { 3657 ASSERT(vd == vd->vdev_top); 3658 3659 if (!list_link_active(&vd->vdev_config_dirty_node) && 3660 vdev_is_concrete(vd)) { 3661 list_insert_head(&spa->spa_config_dirty_list, vd); 3662 } 3663 } 3664 } 3665 3666 void 3667 vdev_config_clean(vdev_t *vd) 3668 { 3669 spa_t *spa = vd->vdev_spa; 3670 3671 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3672 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3673 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3674 3675 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3676 list_remove(&spa->spa_config_dirty_list, vd); 3677 } 3678 3679 /* 3680 * Mark a top-level vdev's state as dirty, so that the next pass of 3681 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3682 * the state changes from larger config changes because they require 3683 * much less locking, and are often needed for administrative actions. 3684 */ 3685 void 3686 vdev_state_dirty(vdev_t *vd) 3687 { 3688 spa_t *spa = vd->vdev_spa; 3689 3690 ASSERT(spa_writeable(spa)); 3691 ASSERT(vd == vd->vdev_top); 3692 3693 /* 3694 * The state list is protected by the SCL_STATE lock. The caller 3695 * must either hold SCL_STATE as writer, or must be the sync thread 3696 * (which holds SCL_STATE as reader). There's only one sync thread, 3697 * so this is sufficient to ensure mutual exclusion. 3698 */ 3699 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3700 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3701 spa_config_held(spa, SCL_STATE, RW_READER))); 3702 3703 if (!list_link_active(&vd->vdev_state_dirty_node) && 3704 vdev_is_concrete(vd)) 3705 list_insert_head(&spa->spa_state_dirty_list, vd); 3706 } 3707 3708 void 3709 vdev_state_clean(vdev_t *vd) 3710 { 3711 spa_t *spa = vd->vdev_spa; 3712 3713 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3714 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3715 spa_config_held(spa, SCL_STATE, RW_READER))); 3716 3717 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3718 list_remove(&spa->spa_state_dirty_list, vd); 3719 } 3720 3721 /* 3722 * Propagate vdev state up from children to parent. 3723 */ 3724 void 3725 vdev_propagate_state(vdev_t *vd) 3726 { 3727 spa_t *spa = vd->vdev_spa; 3728 vdev_t *rvd = spa->spa_root_vdev; 3729 int degraded = 0, faulted = 0; 3730 int corrupted = 0; 3731 vdev_t *child; 3732 3733 if (vd->vdev_children > 0) { 3734 for (int c = 0; c < vd->vdev_children; c++) { 3735 child = vd->vdev_child[c]; 3736 3737 /* 3738 * Don't factor holes or indirect vdevs into the 3739 * decision. 3740 */ 3741 if (!vdev_is_concrete(child)) 3742 continue; 3743 3744 if (!vdev_readable(child) || 3745 (!vdev_writeable(child) && spa_writeable(spa))) { 3746 /* 3747 * Root special: if there is a top-level log 3748 * device, treat the root vdev as if it were 3749 * degraded. 3750 */ 3751 if (child->vdev_islog && vd == rvd) 3752 degraded++; 3753 else 3754 faulted++; 3755 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 3756 degraded++; 3757 } 3758 3759 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 3760 corrupted++; 3761 } 3762 3763 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 3764 3765 /* 3766 * Root special: if there is a top-level vdev that cannot be 3767 * opened due to corrupted metadata, then propagate the root 3768 * vdev's aux state as 'corrupt' rather than 'insufficient 3769 * replicas'. 3770 */ 3771 if (corrupted && vd == rvd && 3772 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 3773 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 3774 VDEV_AUX_CORRUPT_DATA); 3775 } 3776 3777 if (vd->vdev_parent) 3778 vdev_propagate_state(vd->vdev_parent); 3779 } 3780 3781 /* 3782 * Set a vdev's state. If this is during an open, we don't update the parent 3783 * state, because we're in the process of opening children depth-first. 3784 * Otherwise, we propagate the change to the parent. 3785 * 3786 * If this routine places a device in a faulted state, an appropriate ereport is 3787 * generated. 3788 */ 3789 void 3790 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3791 { 3792 uint64_t save_state; 3793 spa_t *spa = vd->vdev_spa; 3794 3795 if (state == vd->vdev_state) { 3796 vd->vdev_stat.vs_aux = aux; 3797 return; 3798 } 3799 3800 save_state = vd->vdev_state; 3801 3802 vd->vdev_state = state; 3803 vd->vdev_stat.vs_aux = aux; 3804 3805 /* 3806 * If we are setting the vdev state to anything but an open state, then 3807 * always close the underlying device unless the device has requested 3808 * a delayed close (i.e. we're about to remove or fault the device). 3809 * Otherwise, we keep accessible but invalid devices open forever. 3810 * We don't call vdev_close() itself, because that implies some extra 3811 * checks (offline, etc) that we don't want here. This is limited to 3812 * leaf devices, because otherwise closing the device will affect other 3813 * children. 3814 */ 3815 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 3816 vd->vdev_ops->vdev_op_leaf) 3817 vd->vdev_ops->vdev_op_close(vd); 3818 3819 /* 3820 * If we have brought this vdev back into service, we need 3821 * to notify fmd so that it can gracefully repair any outstanding 3822 * cases due to a missing device. We do this in all cases, even those 3823 * that probably don't correlate to a repaired fault. This is sure to 3824 * catch all cases, and we let the zfs-retire agent sort it out. If 3825 * this is a transient state it's OK, as the retire agent will 3826 * double-check the state of the vdev before repairing it. 3827 */ 3828 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3829 vd->vdev_prevstate != state) 3830 zfs_post_state_change(spa, vd); 3831 3832 if (vd->vdev_removed && 3833 state == VDEV_STATE_CANT_OPEN && 3834 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 3835 /* 3836 * If the previous state is set to VDEV_STATE_REMOVED, then this 3837 * device was previously marked removed and someone attempted to 3838 * reopen it. If this failed due to a nonexistent device, then 3839 * keep the device in the REMOVED state. We also let this be if 3840 * it is one of our special test online cases, which is only 3841 * attempting to online the device and shouldn't generate an FMA 3842 * fault. 3843 */ 3844 vd->vdev_state = VDEV_STATE_REMOVED; 3845 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 3846 } else if (state == VDEV_STATE_REMOVED) { 3847 vd->vdev_removed = B_TRUE; 3848 } else if (state == VDEV_STATE_CANT_OPEN) { 3849 /* 3850 * If we fail to open a vdev during an import or recovery, we 3851 * mark it as "not available", which signifies that it was 3852 * never there to begin with. Failure to open such a device 3853 * is not considered an error. 3854 */ 3855 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3856 spa_load_state(spa) == SPA_LOAD_RECOVER) && 3857 vd->vdev_ops->vdev_op_leaf) 3858 vd->vdev_not_present = 1; 3859 3860 /* 3861 * Post the appropriate ereport. If the 'prevstate' field is 3862 * set to something other than VDEV_STATE_UNKNOWN, it indicates 3863 * that this is part of a vdev_reopen(). In this case, we don't 3864 * want to post the ereport if the device was already in the 3865 * CANT_OPEN state beforehand. 3866 * 3867 * If the 'checkremove' flag is set, then this is an attempt to 3868 * online the device in response to an insertion event. If we 3869 * hit this case, then we have detected an insertion event for a 3870 * faulted or offline device that wasn't in the removed state. 3871 * In this scenario, we don't post an ereport because we are 3872 * about to replace the device, or attempt an online with 3873 * vdev_forcefault, which will generate the fault for us. 3874 */ 3875 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 3876 !vd->vdev_not_present && !vd->vdev_checkremove && 3877 vd != spa->spa_root_vdev) { 3878 const char *class; 3879 3880 switch (aux) { 3881 case VDEV_AUX_OPEN_FAILED: 3882 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3883 break; 3884 case VDEV_AUX_CORRUPT_DATA: 3885 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3886 break; 3887 case VDEV_AUX_NO_REPLICAS: 3888 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3889 break; 3890 case VDEV_AUX_BAD_GUID_SUM: 3891 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3892 break; 3893 case VDEV_AUX_TOO_SMALL: 3894 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3895 break; 3896 case VDEV_AUX_BAD_LABEL: 3897 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3898 break; 3899 default: 3900 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3901 } 3902 3903 zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3904 } 3905 3906 /* Erase any notion of persistent removed state */ 3907 vd->vdev_removed = B_FALSE; 3908 } else { 3909 vd->vdev_removed = B_FALSE; 3910 } 3911 3912 if (!isopen && vd->vdev_parent) 3913 vdev_propagate_state(vd->vdev_parent); 3914 } 3915 3916 boolean_t 3917 vdev_children_are_offline(vdev_t *vd) 3918 { 3919 ASSERT(!vd->vdev_ops->vdev_op_leaf); 3920 3921 for (uint64_t i = 0; i < vd->vdev_children; i++) { 3922 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 3923 return (B_FALSE); 3924 } 3925 3926 return (B_TRUE); 3927 } 3928 3929 /* 3930 * Check the vdev configuration to ensure that it's capable of supporting 3931 * a root pool. We do not support partial configuration. 3932 * In addition, only a single top-level vdev is allowed. 3933 */ 3934 boolean_t 3935 vdev_is_bootable(vdev_t *vd) 3936 { 3937 if (!vd->vdev_ops->vdev_op_leaf) { 3938 char *vdev_type = vd->vdev_ops->vdev_op_type; 3939 3940 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 3941 vd->vdev_children > 1) { 3942 return (B_FALSE); 3943 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 3944 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 3945 return (B_FALSE); 3946 } 3947 } 3948 3949 for (int c = 0; c < vd->vdev_children; c++) { 3950 if (!vdev_is_bootable(vd->vdev_child[c])) 3951 return (B_FALSE); 3952 } 3953 return (B_TRUE); 3954 } 3955 3956 boolean_t 3957 vdev_is_concrete(vdev_t *vd) 3958 { 3959 vdev_ops_t *ops = vd->vdev_ops; 3960 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 3961 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 3962 return (B_FALSE); 3963 } else { 3964 return (B_TRUE); 3965 } 3966 } 3967 3968 /* 3969 * Determine if a log device has valid content. If the vdev was 3970 * removed or faulted in the MOS config then we know that 3971 * the content on the log device has already been written to the pool. 3972 */ 3973 boolean_t 3974 vdev_log_state_valid(vdev_t *vd) 3975 { 3976 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 3977 !vd->vdev_removed) 3978 return (B_TRUE); 3979 3980 for (int c = 0; c < vd->vdev_children; c++) 3981 if (vdev_log_state_valid(vd->vdev_child[c])) 3982 return (B_TRUE); 3983 3984 return (B_FALSE); 3985 } 3986 3987 /* 3988 * Expand a vdev if possible. 3989 */ 3990 void 3991 vdev_expand(vdev_t *vd, uint64_t txg) 3992 { 3993 ASSERT(vd->vdev_top == vd); 3994 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3995 3996 vdev_set_deflate_ratio(vd); 3997 3998 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 3999 vdev_is_concrete(vd)) { 4000 VERIFY(vdev_metaslab_init(vd, txg) == 0); 4001 vdev_config_dirty(vd); 4002 } 4003 } 4004 4005 /* 4006 * Split a vdev. 4007 */ 4008 void 4009 vdev_split(vdev_t *vd) 4010 { 4011 vdev_t *cvd, *pvd = vd->vdev_parent; 4012 4013 vdev_remove_child(pvd, vd); 4014 vdev_compact_children(pvd); 4015 4016 cvd = pvd->vdev_child[0]; 4017 if (pvd->vdev_children == 1) { 4018 vdev_remove_parent(cvd); 4019 cvd->vdev_splitting = B_TRUE; 4020 } 4021 vdev_propagate_state(cvd); 4022 } 4023 4024 void 4025 vdev_deadman(vdev_t *vd) 4026 { 4027 for (int c = 0; c < vd->vdev_children; c++) { 4028 vdev_t *cvd = vd->vdev_child[c]; 4029 4030 vdev_deadman(cvd); 4031 } 4032 4033 if (vd->vdev_ops->vdev_op_leaf) { 4034 vdev_queue_t *vq = &vd->vdev_queue; 4035 4036 mutex_enter(&vq->vq_lock); 4037 if (avl_numnodes(&vq->vq_active_tree) > 0) { 4038 spa_t *spa = vd->vdev_spa; 4039 zio_t *fio; 4040 uint64_t delta; 4041 4042 /* 4043 * Look at the head of all the pending queues, 4044 * if any I/O has been outstanding for longer than 4045 * the spa_deadman_synctime we panic the system. 4046 */ 4047 fio = avl_first(&vq->vq_active_tree); 4048 delta = gethrtime() - fio->io_timestamp; 4049 if (delta > spa_deadman_synctime(spa)) { 4050 vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 4051 "%lluns, delta %lluns, last io %lluns", 4052 fio->io_timestamp, (u_longlong_t)delta, 4053 vq->vq_io_complete_ts); 4054 fm_panic("I/O to pool '%s' appears to be " 4055 "hung.", spa_name(spa)); 4056 } 4057 } 4058 mutex_exit(&vq->vq_lock); 4059 } 4060 } 4061