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