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