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