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