vdev.c revision 277553
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd 22168404Spjd/* 23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24228103Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25265740Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 26247348Smm * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 27168404Spjd */ 28168404Spjd 29168404Spjd#include <sys/zfs_context.h> 30168404Spjd#include <sys/fm/fs/zfs.h> 31168404Spjd#include <sys/spa.h> 32168404Spjd#include <sys/spa_impl.h> 33168404Spjd#include <sys/dmu.h> 34168404Spjd#include <sys/dmu_tx.h> 35168404Spjd#include <sys/vdev_impl.h> 36168404Spjd#include <sys/uberblock_impl.h> 37168404Spjd#include <sys/metaslab.h> 38168404Spjd#include <sys/metaslab_impl.h> 39168404Spjd#include <sys/space_map.h> 40262093Savg#include <sys/space_reftree.h> 41168404Spjd#include <sys/zio.h> 42168404Spjd#include <sys/zap.h> 43168404Spjd#include <sys/fs/zfs.h> 44185029Spjd#include <sys/arc.h> 45213197Smm#include <sys/zil.h> 46219089Spjd#include <sys/dsl_scan.h> 47240868Spjd#include <sys/trim_map.h> 48168404Spjd 49168404SpjdSYSCTL_DECL(_vfs_zfs); 50168404SpjdSYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV"); 51168404Spjd 52168404Spjd/* 53168404Spjd * Virtual device management. 54168404Spjd */ 55168404Spjd 56266122Ssmh/* 57254591Sgibbs * The limit for ZFS to automatically increase a top-level vdev's ashift 58254591Sgibbs * from logical ashift to physical ashift. 59254591Sgibbs * 60254591Sgibbs * Example: one or more 512B emulation child vdevs 61254591Sgibbs * child->vdev_ashift = 9 (512 bytes) 62254591Sgibbs * child->vdev_physical_ashift = 12 (4096 bytes) 63254591Sgibbs * zfs_max_auto_ashift = 11 (2048 bytes) 64266122Ssmh * zfs_min_auto_ashift = 9 (512 bytes) 65254591Sgibbs * 66266122Ssmh * On pool creation or the addition of a new top-level vdev, ZFS will 67266122Ssmh * increase the ashift of the top-level vdev to 2048 as limited by 68266122Ssmh * zfs_max_auto_ashift. 69254591Sgibbs * 70254591Sgibbs * Example: one or more 512B emulation child vdevs 71254591Sgibbs * child->vdev_ashift = 9 (512 bytes) 72254591Sgibbs * child->vdev_physical_ashift = 12 (4096 bytes) 73254591Sgibbs * zfs_max_auto_ashift = 13 (8192 bytes) 74266122Ssmh * zfs_min_auto_ashift = 9 (512 bytes) 75254591Sgibbs * 76266122Ssmh * On pool creation or the addition of a new top-level vdev, ZFS will 77266122Ssmh * increase the ashift of the top-level vdev to 4096 to match the 78266122Ssmh * max vdev_physical_ashift. 79266122Ssmh * 80266122Ssmh * Example: one or more 512B emulation child vdevs 81266122Ssmh * child->vdev_ashift = 9 (512 bytes) 82266122Ssmh * child->vdev_physical_ashift = 9 (512 bytes) 83266122Ssmh * zfs_max_auto_ashift = 13 (8192 bytes) 84266122Ssmh * zfs_min_auto_ashift = 12 (4096 bytes) 85266122Ssmh * 86266122Ssmh * On pool creation or the addition of a new top-level vdev, ZFS will 87266122Ssmh * increase the ashift of the top-level vdev to 4096 to match the 88266122Ssmh * zfs_min_auto_ashift. 89254591Sgibbs */ 90254591Sgibbsstatic uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT; 91266122Ssmhstatic uint64_t zfs_min_auto_ashift = SPA_MINASHIFT; 92254591Sgibbs 93254591Sgibbsstatic int 94254591Sgibbssysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS) 95254591Sgibbs{ 96254591Sgibbs uint64_t val; 97254591Sgibbs int err; 98254591Sgibbs 99254591Sgibbs val = zfs_max_auto_ashift; 100254591Sgibbs err = sysctl_handle_64(oidp, &val, 0, req); 101254591Sgibbs if (err != 0 || req->newptr == NULL) 102254591Sgibbs return (err); 103254591Sgibbs 104266122Ssmh if (val > SPA_MAXASHIFT || val < zfs_min_auto_ashift) 105266122Ssmh return (EINVAL); 106254591Sgibbs 107254591Sgibbs zfs_max_auto_ashift = val; 108254591Sgibbs 109254591Sgibbs return (0); 110254591Sgibbs} 111254591SgibbsSYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift, 112254591Sgibbs CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 113254591Sgibbs sysctl_vfs_zfs_max_auto_ashift, "QU", 114266122Ssmh "Max ashift used when optimising for logical -> physical sectors size on " 115266122Ssmh "new top-level vdevs."); 116254591Sgibbs 117266122Ssmhstatic int 118266122Ssmhsysctl_vfs_zfs_min_auto_ashift(SYSCTL_HANDLER_ARGS) 119266122Ssmh{ 120266122Ssmh uint64_t val; 121266122Ssmh int err; 122266122Ssmh 123266122Ssmh val = zfs_min_auto_ashift; 124266122Ssmh err = sysctl_handle_64(oidp, &val, 0, req); 125266122Ssmh if (err != 0 || req->newptr == NULL) 126266122Ssmh return (err); 127266122Ssmh 128266122Ssmh if (val < SPA_MINASHIFT || val > zfs_max_auto_ashift) 129266122Ssmh return (EINVAL); 130266122Ssmh 131266122Ssmh zfs_min_auto_ashift = val; 132266122Ssmh 133266122Ssmh return (0); 134266122Ssmh} 135266122SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, min_auto_ashift, 136266122Ssmh CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 137266122Ssmh sysctl_vfs_zfs_min_auto_ashift, "QU", 138266122Ssmh "Min ashift used when creating new top-level vdevs."); 139266122Ssmh 140168404Spjdstatic vdev_ops_t *vdev_ops_table[] = { 141168404Spjd &vdev_root_ops, 142168404Spjd &vdev_raidz_ops, 143168404Spjd &vdev_mirror_ops, 144168404Spjd &vdev_replacing_ops, 145168404Spjd &vdev_spare_ops, 146168404Spjd#ifdef _KERNEL 147168404Spjd &vdev_geom_ops, 148168404Spjd#else 149168404Spjd &vdev_disk_ops, 150185029Spjd#endif 151168404Spjd &vdev_file_ops, 152168404Spjd &vdev_missing_ops, 153219089Spjd &vdev_hole_ops, 154168404Spjd NULL 155168404Spjd}; 156168404Spjd 157168404Spjd 158168404Spjd/* 159273343Sdelphij * When a vdev is added, it will be divided into approximately (but no 160273343Sdelphij * more than) this number of metaslabs. 161273343Sdelphij */ 162273343Sdelphijint metaslabs_per_vdev = 200; 163273343SdelphijSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, metaslabs_per_vdev, CTLFLAG_RDTUN, 164273343Sdelphij &metaslabs_per_vdev, 0, 165273343Sdelphij "When a vdev is added, how many metaslabs the vdev should be divided into"); 166273343Sdelphij 167273343Sdelphij/* 168168404Spjd * Given a vdev type, return the appropriate ops vector. 169168404Spjd */ 170168404Spjdstatic vdev_ops_t * 171168404Spjdvdev_getops(const char *type) 172168404Spjd{ 173168404Spjd vdev_ops_t *ops, **opspp; 174168404Spjd 175168404Spjd for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 176168404Spjd if (strcmp(ops->vdev_op_type, type) == 0) 177168404Spjd break; 178168404Spjd 179168404Spjd return (ops); 180168404Spjd} 181168404Spjd 182168404Spjd/* 183168404Spjd * Default asize function: return the MAX of psize with the asize of 184168404Spjd * all children. This is what's used by anything other than RAID-Z. 185168404Spjd */ 186168404Spjduint64_t 187168404Spjdvdev_default_asize(vdev_t *vd, uint64_t psize) 188168404Spjd{ 189168404Spjd uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 190168404Spjd uint64_t csize; 191168404Spjd 192219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 193168404Spjd csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 194168404Spjd asize = MAX(asize, csize); 195168404Spjd } 196168404Spjd 197168404Spjd return (asize); 198168404Spjd} 199168404Spjd 200168404Spjd/* 201219089Spjd * Get the minimum allocatable size. We define the allocatable size as 202219089Spjd * the vdev's asize rounded to the nearest metaslab. This allows us to 203219089Spjd * replace or attach devices which don't have the same physical size but 204219089Spjd * can still satisfy the same number of allocations. 205168404Spjd */ 206168404Spjduint64_t 207219089Spjdvdev_get_min_asize(vdev_t *vd) 208168404Spjd{ 209219089Spjd vdev_t *pvd = vd->vdev_parent; 210168404Spjd 211219089Spjd /* 212236155Smm * If our parent is NULL (inactive spare or cache) or is the root, 213219089Spjd * just return our own asize. 214219089Spjd */ 215219089Spjd if (pvd == NULL) 216219089Spjd return (vd->vdev_asize); 217168404Spjd 218168404Spjd /* 219219089Spjd * The top-level vdev just returns the allocatable size rounded 220219089Spjd * to the nearest metaslab. 221168404Spjd */ 222219089Spjd if (vd == vd->vdev_top) 223219089Spjd return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 224168404Spjd 225219089Spjd /* 226219089Spjd * The allocatable space for a raidz vdev is N * sizeof(smallest child), 227219089Spjd * so each child must provide at least 1/Nth of its asize. 228219089Spjd */ 229219089Spjd if (pvd->vdev_ops == &vdev_raidz_ops) 230219089Spjd return (pvd->vdev_min_asize / pvd->vdev_children); 231168404Spjd 232219089Spjd return (pvd->vdev_min_asize); 233219089Spjd} 234168404Spjd 235219089Spjdvoid 236219089Spjdvdev_set_min_asize(vdev_t *vd) 237219089Spjd{ 238219089Spjd vd->vdev_min_asize = vdev_get_min_asize(vd); 239219089Spjd 240219089Spjd for (int c = 0; c < vd->vdev_children; c++) 241219089Spjd vdev_set_min_asize(vd->vdev_child[c]); 242168404Spjd} 243168404Spjd 244168404Spjdvdev_t * 245168404Spjdvdev_lookup_top(spa_t *spa, uint64_t vdev) 246168404Spjd{ 247168404Spjd vdev_t *rvd = spa->spa_root_vdev; 248168404Spjd 249185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 250185029Spjd 251185029Spjd if (vdev < rvd->vdev_children) { 252185029Spjd ASSERT(rvd->vdev_child[vdev] != NULL); 253168404Spjd return (rvd->vdev_child[vdev]); 254185029Spjd } 255168404Spjd 256168404Spjd return (NULL); 257168404Spjd} 258168404Spjd 259168404Spjdvdev_t * 260168404Spjdvdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 261168404Spjd{ 262168404Spjd vdev_t *mvd; 263168404Spjd 264168404Spjd if (vd->vdev_guid == guid) 265168404Spjd return (vd); 266168404Spjd 267219089Spjd for (int c = 0; c < vd->vdev_children; c++) 268168404Spjd if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 269168404Spjd NULL) 270168404Spjd return (mvd); 271168404Spjd 272168404Spjd return (NULL); 273168404Spjd} 274168404Spjd 275168404Spjdvoid 276168404Spjdvdev_add_child(vdev_t *pvd, vdev_t *cvd) 277168404Spjd{ 278168404Spjd size_t oldsize, newsize; 279168404Spjd uint64_t id = cvd->vdev_id; 280168404Spjd vdev_t **newchild; 281168404Spjd 282185029Spjd ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 283168404Spjd ASSERT(cvd->vdev_parent == NULL); 284168404Spjd 285168404Spjd cvd->vdev_parent = pvd; 286168404Spjd 287168404Spjd if (pvd == NULL) 288168404Spjd return; 289168404Spjd 290168404Spjd ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 291168404Spjd 292168404Spjd oldsize = pvd->vdev_children * sizeof (vdev_t *); 293168404Spjd pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 294168404Spjd newsize = pvd->vdev_children * sizeof (vdev_t *); 295168404Spjd 296168404Spjd newchild = kmem_zalloc(newsize, KM_SLEEP); 297168404Spjd if (pvd->vdev_child != NULL) { 298168404Spjd bcopy(pvd->vdev_child, newchild, oldsize); 299168404Spjd kmem_free(pvd->vdev_child, oldsize); 300168404Spjd } 301168404Spjd 302168404Spjd pvd->vdev_child = newchild; 303168404Spjd pvd->vdev_child[id] = cvd; 304168404Spjd 305168404Spjd cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 306168404Spjd ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 307168404Spjd 308168404Spjd /* 309168404Spjd * Walk up all ancestors to update guid sum. 310168404Spjd */ 311168404Spjd for (; pvd != NULL; pvd = pvd->vdev_parent) 312168404Spjd pvd->vdev_guid_sum += cvd->vdev_guid_sum; 313168404Spjd} 314168404Spjd 315168404Spjdvoid 316168404Spjdvdev_remove_child(vdev_t *pvd, vdev_t *cvd) 317168404Spjd{ 318168404Spjd int c; 319168404Spjd uint_t id = cvd->vdev_id; 320168404Spjd 321168404Spjd ASSERT(cvd->vdev_parent == pvd); 322168404Spjd 323168404Spjd if (pvd == NULL) 324168404Spjd return; 325168404Spjd 326168404Spjd ASSERT(id < pvd->vdev_children); 327168404Spjd ASSERT(pvd->vdev_child[id] == cvd); 328168404Spjd 329168404Spjd pvd->vdev_child[id] = NULL; 330168404Spjd cvd->vdev_parent = NULL; 331168404Spjd 332168404Spjd for (c = 0; c < pvd->vdev_children; c++) 333168404Spjd if (pvd->vdev_child[c]) 334168404Spjd break; 335168404Spjd 336168404Spjd if (c == pvd->vdev_children) { 337168404Spjd kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 338168404Spjd pvd->vdev_child = NULL; 339168404Spjd pvd->vdev_children = 0; 340168404Spjd } 341168404Spjd 342168404Spjd /* 343168404Spjd * Walk up all ancestors to update guid sum. 344168404Spjd */ 345168404Spjd for (; pvd != NULL; pvd = pvd->vdev_parent) 346168404Spjd pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 347168404Spjd} 348168404Spjd 349168404Spjd/* 350168404Spjd * Remove any holes in the child array. 351168404Spjd */ 352168404Spjdvoid 353168404Spjdvdev_compact_children(vdev_t *pvd) 354168404Spjd{ 355168404Spjd vdev_t **newchild, *cvd; 356168404Spjd int oldc = pvd->vdev_children; 357219089Spjd int newc; 358168404Spjd 359185029Spjd ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 360168404Spjd 361219089Spjd for (int c = newc = 0; c < oldc; c++) 362168404Spjd if (pvd->vdev_child[c]) 363168404Spjd newc++; 364168404Spjd 365168404Spjd newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 366168404Spjd 367219089Spjd for (int c = newc = 0; c < oldc; c++) { 368168404Spjd if ((cvd = pvd->vdev_child[c]) != NULL) { 369168404Spjd newchild[newc] = cvd; 370168404Spjd cvd->vdev_id = newc++; 371168404Spjd } 372168404Spjd } 373168404Spjd 374168404Spjd kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 375168404Spjd pvd->vdev_child = newchild; 376168404Spjd pvd->vdev_children = newc; 377168404Spjd} 378168404Spjd 379168404Spjd/* 380168404Spjd * Allocate and minimally initialize a vdev_t. 381168404Spjd */ 382219089Spjdvdev_t * 383168404Spjdvdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 384168404Spjd{ 385168404Spjd vdev_t *vd; 386168404Spjd 387168404Spjd vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 388168404Spjd 389168404Spjd if (spa->spa_root_vdev == NULL) { 390168404Spjd ASSERT(ops == &vdev_root_ops); 391168404Spjd spa->spa_root_vdev = vd; 392228103Smm spa->spa_load_guid = spa_generate_guid(NULL); 393168404Spjd } 394168404Spjd 395219089Spjd if (guid == 0 && ops != &vdev_hole_ops) { 396168404Spjd if (spa->spa_root_vdev == vd) { 397168404Spjd /* 398168404Spjd * The root vdev's guid will also be the pool guid, 399168404Spjd * which must be unique among all pools. 400168404Spjd */ 401219089Spjd guid = spa_generate_guid(NULL); 402168404Spjd } else { 403168404Spjd /* 404168404Spjd * Any other vdev's guid must be unique within the pool. 405168404Spjd */ 406219089Spjd guid = spa_generate_guid(spa); 407168404Spjd } 408168404Spjd ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 409168404Spjd } 410168404Spjd 411168404Spjd vd->vdev_spa = spa; 412168404Spjd vd->vdev_id = id; 413168404Spjd vd->vdev_guid = guid; 414168404Spjd vd->vdev_guid_sum = guid; 415168404Spjd vd->vdev_ops = ops; 416168404Spjd vd->vdev_state = VDEV_STATE_CLOSED; 417219089Spjd vd->vdev_ishole = (ops == &vdev_hole_ops); 418168404Spjd 419168404Spjd mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 420168404Spjd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 421185029Spjd mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 422209962Smm for (int t = 0; t < DTL_TYPES; t++) { 423262093Savg vd->vdev_dtl[t] = range_tree_create(NULL, NULL, 424209962Smm &vd->vdev_dtl_lock); 425209962Smm } 426168404Spjd txg_list_create(&vd->vdev_ms_list, 427168404Spjd offsetof(struct metaslab, ms_txg_node)); 428168404Spjd txg_list_create(&vd->vdev_dtl_list, 429168404Spjd offsetof(struct vdev, vdev_dtl_node)); 430168404Spjd vd->vdev_stat.vs_timestamp = gethrtime(); 431185029Spjd vdev_queue_init(vd); 432185029Spjd vdev_cache_init(vd); 433168404Spjd 434168404Spjd return (vd); 435168404Spjd} 436168404Spjd 437168404Spjd/* 438168404Spjd * Allocate a new vdev. The 'alloctype' is used to control whether we are 439168404Spjd * creating a new vdev or loading an existing one - the behavior is slightly 440168404Spjd * different for each case. 441168404Spjd */ 442168404Spjdint 443168404Spjdvdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 444168404Spjd int alloctype) 445168404Spjd{ 446168404Spjd vdev_ops_t *ops; 447168404Spjd char *type; 448185029Spjd uint64_t guid = 0, islog, nparity; 449168404Spjd vdev_t *vd; 450168404Spjd 451185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 452168404Spjd 453168404Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 454249195Smm return (SET_ERROR(EINVAL)); 455168404Spjd 456168404Spjd if ((ops = vdev_getops(type)) == NULL) 457249195Smm return (SET_ERROR(EINVAL)); 458168404Spjd 459168404Spjd /* 460168404Spjd * If this is a load, get the vdev guid from the nvlist. 461168404Spjd * Otherwise, vdev_alloc_common() will generate one for us. 462168404Spjd */ 463168404Spjd if (alloctype == VDEV_ALLOC_LOAD) { 464168404Spjd uint64_t label_id; 465168404Spjd 466168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 467168404Spjd label_id != id) 468249195Smm return (SET_ERROR(EINVAL)); 469168404Spjd 470168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 471249195Smm return (SET_ERROR(EINVAL)); 472168404Spjd } else if (alloctype == VDEV_ALLOC_SPARE) { 473168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 474249195Smm return (SET_ERROR(EINVAL)); 475185029Spjd } else if (alloctype == VDEV_ALLOC_L2CACHE) { 476185029Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 477249195Smm return (SET_ERROR(EINVAL)); 478219089Spjd } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 479219089Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 480249195Smm return (SET_ERROR(EINVAL)); 481168404Spjd } 482168404Spjd 483168404Spjd /* 484168404Spjd * The first allocated vdev must be of type 'root'. 485168404Spjd */ 486168404Spjd if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 487249195Smm return (SET_ERROR(EINVAL)); 488168404Spjd 489185029Spjd /* 490185029Spjd * Determine whether we're a log vdev. 491185029Spjd */ 492185029Spjd islog = 0; 493185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 494185029Spjd if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 495249195Smm return (SET_ERROR(ENOTSUP)); 496168404Spjd 497219089Spjd if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 498249195Smm return (SET_ERROR(ENOTSUP)); 499219089Spjd 500168404Spjd /* 501185029Spjd * Set the nparity property for RAID-Z vdevs. 502168404Spjd */ 503185029Spjd nparity = -1ULL; 504168404Spjd if (ops == &vdev_raidz_ops) { 505168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 506185029Spjd &nparity) == 0) { 507219089Spjd if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 508249195Smm return (SET_ERROR(EINVAL)); 509168404Spjd /* 510219089Spjd * Previous versions could only support 1 or 2 parity 511219089Spjd * device. 512168404Spjd */ 513219089Spjd if (nparity > 1 && 514219089Spjd spa_version(spa) < SPA_VERSION_RAIDZ2) 515249195Smm return (SET_ERROR(ENOTSUP)); 516219089Spjd if (nparity > 2 && 517219089Spjd spa_version(spa) < SPA_VERSION_RAIDZ3) 518249195Smm return (SET_ERROR(ENOTSUP)); 519168404Spjd } else { 520168404Spjd /* 521168404Spjd * We require the parity to be specified for SPAs that 522168404Spjd * support multiple parity levels. 523168404Spjd */ 524219089Spjd if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 525249195Smm return (SET_ERROR(EINVAL)); 526168404Spjd /* 527168404Spjd * Otherwise, we default to 1 parity device for RAID-Z. 528168404Spjd */ 529185029Spjd nparity = 1; 530168404Spjd } 531168404Spjd } else { 532185029Spjd nparity = 0; 533168404Spjd } 534185029Spjd ASSERT(nparity != -1ULL); 535168404Spjd 536185029Spjd vd = vdev_alloc_common(spa, id, guid, ops); 537185029Spjd 538185029Spjd vd->vdev_islog = islog; 539185029Spjd vd->vdev_nparity = nparity; 540185029Spjd 541185029Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 542185029Spjd vd->vdev_path = spa_strdup(vd->vdev_path); 543185029Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 544185029Spjd vd->vdev_devid = spa_strdup(vd->vdev_devid); 545185029Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 546185029Spjd &vd->vdev_physpath) == 0) 547185029Spjd vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 548209962Smm if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 549209962Smm vd->vdev_fru = spa_strdup(vd->vdev_fru); 550185029Spjd 551168404Spjd /* 552168404Spjd * Set the whole_disk property. If it's not specified, leave the value 553168404Spjd * as -1. 554168404Spjd */ 555168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 556168404Spjd &vd->vdev_wholedisk) != 0) 557168404Spjd vd->vdev_wholedisk = -1ULL; 558168404Spjd 559168404Spjd /* 560168404Spjd * Look for the 'not present' flag. This will only be set if the device 561168404Spjd * was not present at the time of import. 562168404Spjd */ 563209962Smm (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 564209962Smm &vd->vdev_not_present); 565168404Spjd 566168404Spjd /* 567168404Spjd * Get the alignment requirement. 568168404Spjd */ 569168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 570168404Spjd 571168404Spjd /* 572219089Spjd * Retrieve the vdev creation time. 573219089Spjd */ 574219089Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 575219089Spjd &vd->vdev_crtxg); 576219089Spjd 577219089Spjd /* 578168404Spjd * If we're a top-level vdev, try to load the allocation parameters. 579168404Spjd */ 580219089Spjd if (parent && !parent->vdev_parent && 581219089Spjd (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 582168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 583168404Spjd &vd->vdev_ms_array); 584168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 585168404Spjd &vd->vdev_ms_shift); 586168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 587168404Spjd &vd->vdev_asize); 588219089Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 589219089Spjd &vd->vdev_removing); 590168404Spjd } 591168404Spjd 592230514Smm if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 593219089Spjd ASSERT(alloctype == VDEV_ALLOC_LOAD || 594219089Spjd alloctype == VDEV_ALLOC_ADD || 595219089Spjd alloctype == VDEV_ALLOC_SPLIT || 596219089Spjd alloctype == VDEV_ALLOC_ROOTPOOL); 597219089Spjd vd->vdev_mg = metaslab_group_create(islog ? 598219089Spjd spa_log_class(spa) : spa_normal_class(spa), vd); 599219089Spjd } 600219089Spjd 601168404Spjd /* 602185029Spjd * If we're a leaf vdev, try to load the DTL object and other state. 603168404Spjd */ 604185029Spjd if (vd->vdev_ops->vdev_op_leaf && 605219089Spjd (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 606219089Spjd alloctype == VDEV_ALLOC_ROOTPOOL)) { 607185029Spjd if (alloctype == VDEV_ALLOC_LOAD) { 608185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 609262093Savg &vd->vdev_dtl_object); 610185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 611185029Spjd &vd->vdev_unspare); 612185029Spjd } 613219089Spjd 614219089Spjd if (alloctype == VDEV_ALLOC_ROOTPOOL) { 615219089Spjd uint64_t spare = 0; 616219089Spjd 617219089Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 618219089Spjd &spare) == 0 && spare) 619219089Spjd spa_spare_add(vd); 620219089Spjd } 621219089Spjd 622168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 623168404Spjd &vd->vdev_offline); 624185029Spjd 625254112Sdelphij (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 626254112Sdelphij &vd->vdev_resilver_txg); 627219089Spjd 628185029Spjd /* 629185029Spjd * When importing a pool, we want to ignore the persistent fault 630185029Spjd * state, as the diagnosis made on another system may not be 631219089Spjd * valid in the current context. Local vdevs will 632219089Spjd * remain in the faulted state. 633185029Spjd */ 634219089Spjd if (spa_load_state(spa) == SPA_LOAD_OPEN) { 635185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 636185029Spjd &vd->vdev_faulted); 637185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 638185029Spjd &vd->vdev_degraded); 639185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 640185029Spjd &vd->vdev_removed); 641219089Spjd 642219089Spjd if (vd->vdev_faulted || vd->vdev_degraded) { 643219089Spjd char *aux; 644219089Spjd 645219089Spjd vd->vdev_label_aux = 646219089Spjd VDEV_AUX_ERR_EXCEEDED; 647219089Spjd if (nvlist_lookup_string(nv, 648219089Spjd ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 649219089Spjd strcmp(aux, "external") == 0) 650219089Spjd vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 651219089Spjd } 652185029Spjd } 653168404Spjd } 654168404Spjd 655168404Spjd /* 656168404Spjd * Add ourselves to the parent's list of children. 657168404Spjd */ 658168404Spjd vdev_add_child(parent, vd); 659168404Spjd 660168404Spjd *vdp = vd; 661168404Spjd 662168404Spjd return (0); 663168404Spjd} 664168404Spjd 665168404Spjdvoid 666168404Spjdvdev_free(vdev_t *vd) 667168404Spjd{ 668185029Spjd spa_t *spa = vd->vdev_spa; 669168404Spjd 670168404Spjd /* 671168404Spjd * vdev_free() implies closing the vdev first. This is simpler than 672168404Spjd * trying to ensure complicated semantics for all callers. 673168404Spjd */ 674168404Spjd vdev_close(vd); 675168404Spjd 676185029Spjd ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 677219089Spjd ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 678168404Spjd 679168404Spjd /* 680168404Spjd * Free all children. 681168404Spjd */ 682219089Spjd for (int c = 0; c < vd->vdev_children; c++) 683168404Spjd vdev_free(vd->vdev_child[c]); 684168404Spjd 685168404Spjd ASSERT(vd->vdev_child == NULL); 686168404Spjd ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 687168404Spjd 688168404Spjd /* 689168404Spjd * Discard allocation state. 690168404Spjd */ 691219089Spjd if (vd->vdev_mg != NULL) { 692168404Spjd vdev_metaslab_fini(vd); 693219089Spjd metaslab_group_destroy(vd->vdev_mg); 694219089Spjd } 695168404Spjd 696240415Smm ASSERT0(vd->vdev_stat.vs_space); 697240415Smm ASSERT0(vd->vdev_stat.vs_dspace); 698240415Smm ASSERT0(vd->vdev_stat.vs_alloc); 699168404Spjd 700168404Spjd /* 701168404Spjd * Remove this vdev from its parent's child list. 702168404Spjd */ 703168404Spjd vdev_remove_child(vd->vdev_parent, vd); 704168404Spjd 705168404Spjd ASSERT(vd->vdev_parent == NULL); 706168404Spjd 707185029Spjd /* 708185029Spjd * Clean up vdev structure. 709185029Spjd */ 710185029Spjd vdev_queue_fini(vd); 711185029Spjd vdev_cache_fini(vd); 712185029Spjd 713185029Spjd if (vd->vdev_path) 714185029Spjd spa_strfree(vd->vdev_path); 715185029Spjd if (vd->vdev_devid) 716185029Spjd spa_strfree(vd->vdev_devid); 717185029Spjd if (vd->vdev_physpath) 718185029Spjd spa_strfree(vd->vdev_physpath); 719209962Smm if (vd->vdev_fru) 720209962Smm spa_strfree(vd->vdev_fru); 721185029Spjd 722185029Spjd if (vd->vdev_isspare) 723185029Spjd spa_spare_remove(vd); 724185029Spjd if (vd->vdev_isl2cache) 725185029Spjd spa_l2cache_remove(vd); 726185029Spjd 727185029Spjd txg_list_destroy(&vd->vdev_ms_list); 728185029Spjd txg_list_destroy(&vd->vdev_dtl_list); 729209962Smm 730185029Spjd mutex_enter(&vd->vdev_dtl_lock); 731262093Savg space_map_close(vd->vdev_dtl_sm); 732209962Smm for (int t = 0; t < DTL_TYPES; t++) { 733262093Savg range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 734262093Savg range_tree_destroy(vd->vdev_dtl[t]); 735209962Smm } 736185029Spjd mutex_exit(&vd->vdev_dtl_lock); 737209962Smm 738185029Spjd mutex_destroy(&vd->vdev_dtl_lock); 739185029Spjd mutex_destroy(&vd->vdev_stat_lock); 740185029Spjd mutex_destroy(&vd->vdev_probe_lock); 741185029Spjd 742185029Spjd if (vd == spa->spa_root_vdev) 743185029Spjd spa->spa_root_vdev = NULL; 744185029Spjd 745185029Spjd kmem_free(vd, sizeof (vdev_t)); 746168404Spjd} 747168404Spjd 748168404Spjd/* 749168404Spjd * Transfer top-level vdev state from svd to tvd. 750168404Spjd */ 751168404Spjdstatic void 752168404Spjdvdev_top_transfer(vdev_t *svd, vdev_t *tvd) 753168404Spjd{ 754168404Spjd spa_t *spa = svd->vdev_spa; 755168404Spjd metaslab_t *msp; 756168404Spjd vdev_t *vd; 757168404Spjd int t; 758168404Spjd 759168404Spjd ASSERT(tvd == tvd->vdev_top); 760168404Spjd 761168404Spjd tvd->vdev_ms_array = svd->vdev_ms_array; 762168404Spjd tvd->vdev_ms_shift = svd->vdev_ms_shift; 763168404Spjd tvd->vdev_ms_count = svd->vdev_ms_count; 764168404Spjd 765168404Spjd svd->vdev_ms_array = 0; 766168404Spjd svd->vdev_ms_shift = 0; 767168404Spjd svd->vdev_ms_count = 0; 768168404Spjd 769230514Smm if (tvd->vdev_mg) 770230514Smm ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 771168404Spjd tvd->vdev_mg = svd->vdev_mg; 772168404Spjd tvd->vdev_ms = svd->vdev_ms; 773168404Spjd 774168404Spjd svd->vdev_mg = NULL; 775168404Spjd svd->vdev_ms = NULL; 776168404Spjd 777168404Spjd if (tvd->vdev_mg != NULL) 778168404Spjd tvd->vdev_mg->mg_vd = tvd; 779168404Spjd 780168404Spjd tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 781168404Spjd tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 782168404Spjd tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 783168404Spjd 784168404Spjd svd->vdev_stat.vs_alloc = 0; 785168404Spjd svd->vdev_stat.vs_space = 0; 786168404Spjd svd->vdev_stat.vs_dspace = 0; 787168404Spjd 788168404Spjd for (t = 0; t < TXG_SIZE; t++) { 789168404Spjd while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 790168404Spjd (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 791168404Spjd while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 792168404Spjd (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 793168404Spjd if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 794168404Spjd (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 795168404Spjd } 796168404Spjd 797185029Spjd if (list_link_active(&svd->vdev_config_dirty_node)) { 798168404Spjd vdev_config_clean(svd); 799168404Spjd vdev_config_dirty(tvd); 800168404Spjd } 801168404Spjd 802185029Spjd if (list_link_active(&svd->vdev_state_dirty_node)) { 803185029Spjd vdev_state_clean(svd); 804185029Spjd vdev_state_dirty(tvd); 805185029Spjd } 806168404Spjd 807168404Spjd tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 808168404Spjd svd->vdev_deflate_ratio = 0; 809185029Spjd 810185029Spjd tvd->vdev_islog = svd->vdev_islog; 811185029Spjd svd->vdev_islog = 0; 812168404Spjd} 813168404Spjd 814168404Spjdstatic void 815168404Spjdvdev_top_update(vdev_t *tvd, vdev_t *vd) 816168404Spjd{ 817168404Spjd if (vd == NULL) 818168404Spjd return; 819168404Spjd 820168404Spjd vd->vdev_top = tvd; 821168404Spjd 822219089Spjd for (int c = 0; c < vd->vdev_children; c++) 823168404Spjd vdev_top_update(tvd, vd->vdev_child[c]); 824168404Spjd} 825168404Spjd 826168404Spjd/* 827168404Spjd * Add a mirror/replacing vdev above an existing vdev. 828168404Spjd */ 829168404Spjdvdev_t * 830168404Spjdvdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 831168404Spjd{ 832168404Spjd spa_t *spa = cvd->vdev_spa; 833168404Spjd vdev_t *pvd = cvd->vdev_parent; 834168404Spjd vdev_t *mvd; 835168404Spjd 836185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 837168404Spjd 838168404Spjd mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 839168404Spjd 840168404Spjd mvd->vdev_asize = cvd->vdev_asize; 841219089Spjd mvd->vdev_min_asize = cvd->vdev_min_asize; 842236155Smm mvd->vdev_max_asize = cvd->vdev_max_asize; 843168404Spjd mvd->vdev_ashift = cvd->vdev_ashift; 844254591Sgibbs mvd->vdev_logical_ashift = cvd->vdev_logical_ashift; 845254591Sgibbs mvd->vdev_physical_ashift = cvd->vdev_physical_ashift; 846168404Spjd mvd->vdev_state = cvd->vdev_state; 847219089Spjd mvd->vdev_crtxg = cvd->vdev_crtxg; 848168404Spjd 849168404Spjd vdev_remove_child(pvd, cvd); 850168404Spjd vdev_add_child(pvd, mvd); 851168404Spjd cvd->vdev_id = mvd->vdev_children; 852168404Spjd vdev_add_child(mvd, cvd); 853168404Spjd vdev_top_update(cvd->vdev_top, cvd->vdev_top); 854168404Spjd 855168404Spjd if (mvd == mvd->vdev_top) 856168404Spjd vdev_top_transfer(cvd, mvd); 857168404Spjd 858168404Spjd return (mvd); 859168404Spjd} 860168404Spjd 861168404Spjd/* 862168404Spjd * Remove a 1-way mirror/replacing vdev from the tree. 863168404Spjd */ 864168404Spjdvoid 865168404Spjdvdev_remove_parent(vdev_t *cvd) 866168404Spjd{ 867168404Spjd vdev_t *mvd = cvd->vdev_parent; 868168404Spjd vdev_t *pvd = mvd->vdev_parent; 869168404Spjd 870185029Spjd ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 871168404Spjd 872168404Spjd ASSERT(mvd->vdev_children == 1); 873168404Spjd ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 874168404Spjd mvd->vdev_ops == &vdev_replacing_ops || 875168404Spjd mvd->vdev_ops == &vdev_spare_ops); 876168404Spjd cvd->vdev_ashift = mvd->vdev_ashift; 877254591Sgibbs cvd->vdev_logical_ashift = mvd->vdev_logical_ashift; 878254591Sgibbs cvd->vdev_physical_ashift = mvd->vdev_physical_ashift; 879168404Spjd 880168404Spjd vdev_remove_child(mvd, cvd); 881168404Spjd vdev_remove_child(pvd, mvd); 882209962Smm 883185029Spjd /* 884185029Spjd * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 885185029Spjd * Otherwise, we could have detached an offline device, and when we 886185029Spjd * go to import the pool we'll think we have two top-level vdevs, 887185029Spjd * instead of a different version of the same top-level vdev. 888185029Spjd */ 889209962Smm if (mvd->vdev_top == mvd) { 890209962Smm uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 891219089Spjd cvd->vdev_orig_guid = cvd->vdev_guid; 892209962Smm cvd->vdev_guid += guid_delta; 893209962Smm cvd->vdev_guid_sum += guid_delta; 894209962Smm } 895168404Spjd cvd->vdev_id = mvd->vdev_id; 896168404Spjd vdev_add_child(pvd, cvd); 897168404Spjd vdev_top_update(cvd->vdev_top, cvd->vdev_top); 898168404Spjd 899168404Spjd if (cvd == cvd->vdev_top) 900168404Spjd vdev_top_transfer(mvd, cvd); 901168404Spjd 902168404Spjd ASSERT(mvd->vdev_children == 0); 903168404Spjd vdev_free(mvd); 904168404Spjd} 905168404Spjd 906168404Spjdint 907168404Spjdvdev_metaslab_init(vdev_t *vd, uint64_t txg) 908168404Spjd{ 909168404Spjd spa_t *spa = vd->vdev_spa; 910168404Spjd objset_t *mos = spa->spa_meta_objset; 911168404Spjd uint64_t m; 912168404Spjd uint64_t oldc = vd->vdev_ms_count; 913168404Spjd uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 914168404Spjd metaslab_t **mspp; 915168404Spjd int error; 916168404Spjd 917219089Spjd ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 918219089Spjd 919219089Spjd /* 920219089Spjd * This vdev is not being allocated from yet or is a hole. 921219089Spjd */ 922219089Spjd if (vd->vdev_ms_shift == 0) 923168404Spjd return (0); 924168404Spjd 925219089Spjd ASSERT(!vd->vdev_ishole); 926219089Spjd 927213197Smm /* 928213197Smm * Compute the raidz-deflation ratio. Note, we hard-code 929276081Sdelphij * in 128k (1 << 17) because it is the "typical" blocksize. 930276081Sdelphij * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 931276081Sdelphij * otherwise it would inconsistently account for existing bp's. 932213197Smm */ 933213197Smm vd->vdev_deflate_ratio = (1 << 17) / 934213197Smm (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 935213197Smm 936168404Spjd ASSERT(oldc <= newc); 937168404Spjd 938168404Spjd mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 939168404Spjd 940168404Spjd if (oldc != 0) { 941168404Spjd bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 942168404Spjd kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 943168404Spjd } 944168404Spjd 945168404Spjd vd->vdev_ms = mspp; 946168404Spjd vd->vdev_ms_count = newc; 947168404Spjd 948168404Spjd for (m = oldc; m < newc; m++) { 949262093Savg uint64_t object = 0; 950262093Savg 951168404Spjd if (txg == 0) { 952168404Spjd error = dmu_read(mos, vd->vdev_ms_array, 953209962Smm m * sizeof (uint64_t), sizeof (uint64_t), &object, 954209962Smm DMU_READ_PREFETCH); 955168404Spjd if (error) 956168404Spjd return (error); 957168404Spjd } 958277553Sdelphij 959277553Sdelphij error = metaslab_init(vd->vdev_mg, m, object, txg, 960277553Sdelphij &(vd->vdev_ms[m])); 961277553Sdelphij if (error) 962277553Sdelphij return (error); 963168404Spjd } 964168404Spjd 965219089Spjd if (txg == 0) 966219089Spjd spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 967219089Spjd 968219089Spjd /* 969219089Spjd * If the vdev is being removed we don't activate 970219089Spjd * the metaslabs since we want to ensure that no new 971219089Spjd * allocations are performed on this device. 972219089Spjd */ 973219089Spjd if (oldc == 0 && !vd->vdev_removing) 974219089Spjd metaslab_group_activate(vd->vdev_mg); 975219089Spjd 976219089Spjd if (txg == 0) 977219089Spjd spa_config_exit(spa, SCL_ALLOC, FTAG); 978219089Spjd 979168404Spjd return (0); 980168404Spjd} 981168404Spjd 982168404Spjdvoid 983168404Spjdvdev_metaslab_fini(vdev_t *vd) 984168404Spjd{ 985168404Spjd uint64_t m; 986168404Spjd uint64_t count = vd->vdev_ms_count; 987168404Spjd 988168404Spjd if (vd->vdev_ms != NULL) { 989219089Spjd metaslab_group_passivate(vd->vdev_mg); 990262093Savg for (m = 0; m < count; m++) { 991262093Savg metaslab_t *msp = vd->vdev_ms[m]; 992262093Savg 993262093Savg if (msp != NULL) 994262093Savg metaslab_fini(msp); 995262093Savg } 996168404Spjd kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 997168404Spjd vd->vdev_ms = NULL; 998168404Spjd } 999168404Spjd} 1000168404Spjd 1001185029Spjdtypedef struct vdev_probe_stats { 1002185029Spjd boolean_t vps_readable; 1003185029Spjd boolean_t vps_writeable; 1004185029Spjd int vps_flags; 1005185029Spjd} vdev_probe_stats_t; 1006185029Spjd 1007185029Spjdstatic void 1008185029Spjdvdev_probe_done(zio_t *zio) 1009185029Spjd{ 1010209962Smm spa_t *spa = zio->io_spa; 1011209962Smm vdev_t *vd = zio->io_vd; 1012185029Spjd vdev_probe_stats_t *vps = zio->io_private; 1013185029Spjd 1014209962Smm ASSERT(vd->vdev_probe_zio != NULL); 1015209962Smm 1016185029Spjd if (zio->io_type == ZIO_TYPE_READ) { 1017185029Spjd if (zio->io_error == 0) 1018185029Spjd vps->vps_readable = 1; 1019209962Smm if (zio->io_error == 0 && spa_writeable(spa)) { 1020209962Smm zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1021185029Spjd zio->io_offset, zio->io_size, zio->io_data, 1022185029Spjd ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1023185029Spjd ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1024185029Spjd } else { 1025185029Spjd zio_buf_free(zio->io_data, zio->io_size); 1026185029Spjd } 1027185029Spjd } else if (zio->io_type == ZIO_TYPE_WRITE) { 1028185029Spjd if (zio->io_error == 0) 1029185029Spjd vps->vps_writeable = 1; 1030185029Spjd zio_buf_free(zio->io_data, zio->io_size); 1031185029Spjd } else if (zio->io_type == ZIO_TYPE_NULL) { 1032209962Smm zio_t *pio; 1033185029Spjd 1034185029Spjd vd->vdev_cant_read |= !vps->vps_readable; 1035185029Spjd vd->vdev_cant_write |= !vps->vps_writeable; 1036185029Spjd 1037185029Spjd if (vdev_readable(vd) && 1038209962Smm (vdev_writeable(vd) || !spa_writeable(spa))) { 1039185029Spjd zio->io_error = 0; 1040185029Spjd } else { 1041185029Spjd ASSERT(zio->io_error != 0); 1042185029Spjd zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1043209962Smm spa, vd, NULL, 0, 0); 1044249195Smm zio->io_error = SET_ERROR(ENXIO); 1045185029Spjd } 1046209962Smm 1047209962Smm mutex_enter(&vd->vdev_probe_lock); 1048209962Smm ASSERT(vd->vdev_probe_zio == zio); 1049209962Smm vd->vdev_probe_zio = NULL; 1050209962Smm mutex_exit(&vd->vdev_probe_lock); 1051209962Smm 1052209962Smm while ((pio = zio_walk_parents(zio)) != NULL) 1053209962Smm if (!vdev_accessible(vd, pio)) 1054249195Smm pio->io_error = SET_ERROR(ENXIO); 1055209962Smm 1056185029Spjd kmem_free(vps, sizeof (*vps)); 1057185029Spjd } 1058185029Spjd} 1059185029Spjd 1060168404Spjd/* 1061251631Sdelphij * Determine whether this device is accessible. 1062251631Sdelphij * 1063251631Sdelphij * Read and write to several known locations: the pad regions of each 1064251631Sdelphij * vdev label but the first, which we leave alone in case it contains 1065251631Sdelphij * a VTOC. 1066185029Spjd */ 1067185029Spjdzio_t * 1068209962Smmvdev_probe(vdev_t *vd, zio_t *zio) 1069185029Spjd{ 1070185029Spjd spa_t *spa = vd->vdev_spa; 1071209962Smm vdev_probe_stats_t *vps = NULL; 1072209962Smm zio_t *pio; 1073185029Spjd 1074209962Smm ASSERT(vd->vdev_ops->vdev_op_leaf); 1075185029Spjd 1076209962Smm /* 1077209962Smm * Don't probe the probe. 1078209962Smm */ 1079209962Smm if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1080209962Smm return (NULL); 1081185029Spjd 1082209962Smm /* 1083209962Smm * To prevent 'probe storms' when a device fails, we create 1084209962Smm * just one probe i/o at a time. All zios that want to probe 1085209962Smm * this vdev will become parents of the probe io. 1086209962Smm */ 1087209962Smm mutex_enter(&vd->vdev_probe_lock); 1088209962Smm 1089209962Smm if ((pio = vd->vdev_probe_zio) == NULL) { 1090209962Smm vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1091209962Smm 1092209962Smm vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1093209962Smm ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1094213198Smm ZIO_FLAG_TRYHARD; 1095209962Smm 1096209962Smm if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1097209962Smm /* 1098209962Smm * vdev_cant_read and vdev_cant_write can only 1099209962Smm * transition from TRUE to FALSE when we have the 1100209962Smm * SCL_ZIO lock as writer; otherwise they can only 1101209962Smm * transition from FALSE to TRUE. This ensures that 1102209962Smm * any zio looking at these values can assume that 1103209962Smm * failures persist for the life of the I/O. That's 1104209962Smm * important because when a device has intermittent 1105209962Smm * connectivity problems, we want to ensure that 1106209962Smm * they're ascribed to the device (ENXIO) and not 1107209962Smm * the zio (EIO). 1108209962Smm * 1109209962Smm * Since we hold SCL_ZIO as writer here, clear both 1110209962Smm * values so the probe can reevaluate from first 1111209962Smm * principles. 1112209962Smm */ 1113209962Smm vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1114209962Smm vd->vdev_cant_read = B_FALSE; 1115209962Smm vd->vdev_cant_write = B_FALSE; 1116209962Smm } 1117209962Smm 1118209962Smm vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1119209962Smm vdev_probe_done, vps, 1120209962Smm vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1121209962Smm 1122219089Spjd /* 1123219089Spjd * We can't change the vdev state in this context, so we 1124219089Spjd * kick off an async task to do it on our behalf. 1125219089Spjd */ 1126209962Smm if (zio != NULL) { 1127209962Smm vd->vdev_probe_wanted = B_TRUE; 1128209962Smm spa_async_request(spa, SPA_ASYNC_PROBE); 1129209962Smm } 1130185029Spjd } 1131185029Spjd 1132209962Smm if (zio != NULL) 1133209962Smm zio_add_child(zio, pio); 1134185029Spjd 1135209962Smm mutex_exit(&vd->vdev_probe_lock); 1136185029Spjd 1137209962Smm if (vps == NULL) { 1138209962Smm ASSERT(zio != NULL); 1139209962Smm return (NULL); 1140209962Smm } 1141185029Spjd 1142185029Spjd for (int l = 1; l < VDEV_LABELS; l++) { 1143209962Smm zio_nowait(zio_read_phys(pio, vd, 1144185029Spjd vdev_label_offset(vd->vdev_psize, l, 1145209962Smm offsetof(vdev_label_t, vl_pad2)), 1146209962Smm VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 1147185029Spjd ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1148185029Spjd ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1149185029Spjd } 1150185029Spjd 1151209962Smm if (zio == NULL) 1152209962Smm return (pio); 1153209962Smm 1154209962Smm zio_nowait(pio); 1155209962Smm return (NULL); 1156185029Spjd} 1157185029Spjd 1158219089Spjdstatic void 1159219089Spjdvdev_open_child(void *arg) 1160219089Spjd{ 1161219089Spjd vdev_t *vd = arg; 1162219089Spjd 1163219089Spjd vd->vdev_open_thread = curthread; 1164219089Spjd vd->vdev_open_error = vdev_open(vd); 1165219089Spjd vd->vdev_open_thread = NULL; 1166219089Spjd} 1167219089Spjd 1168219089Spjdboolean_t 1169219089Spjdvdev_uses_zvols(vdev_t *vd) 1170219089Spjd{ 1171219089Spjd if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1172219089Spjd strlen(ZVOL_DIR)) == 0) 1173219089Spjd return (B_TRUE); 1174219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1175219089Spjd if (vdev_uses_zvols(vd->vdev_child[c])) 1176219089Spjd return (B_TRUE); 1177219089Spjd return (B_FALSE); 1178219089Spjd} 1179219089Spjd 1180219089Spjdvoid 1181219089Spjdvdev_open_children(vdev_t *vd) 1182219089Spjd{ 1183219089Spjd taskq_t *tq; 1184219089Spjd int children = vd->vdev_children; 1185219089Spjd 1186219089Spjd /* 1187219089Spjd * in order to handle pools on top of zvols, do the opens 1188219089Spjd * in a single thread so that the same thread holds the 1189219089Spjd * spa_namespace_lock 1190219089Spjd */ 1191219089Spjd if (B_TRUE || vdev_uses_zvols(vd)) { 1192219089Spjd for (int c = 0; c < children; c++) 1193219089Spjd vd->vdev_child[c]->vdev_open_error = 1194219089Spjd vdev_open(vd->vdev_child[c]); 1195219089Spjd return; 1196219089Spjd } 1197219089Spjd tq = taskq_create("vdev_open", children, minclsyspri, 1198219089Spjd children, children, TASKQ_PREPOPULATE); 1199219089Spjd 1200219089Spjd for (int c = 0; c < children; c++) 1201219089Spjd VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1202219089Spjd TQ_SLEEP) != 0); 1203219089Spjd 1204219089Spjd taskq_destroy(tq); 1205219089Spjd} 1206219089Spjd 1207185029Spjd/* 1208168404Spjd * Prepare a virtual device for access. 1209168404Spjd */ 1210168404Spjdint 1211168404Spjdvdev_open(vdev_t *vd) 1212168404Spjd{ 1213209962Smm spa_t *spa = vd->vdev_spa; 1214168404Spjd int error; 1215168404Spjd uint64_t osize = 0; 1216236155Smm uint64_t max_osize = 0; 1217236155Smm uint64_t asize, max_asize, psize; 1218254591Sgibbs uint64_t logical_ashift = 0; 1219254591Sgibbs uint64_t physical_ashift = 0; 1220168404Spjd 1221219089Spjd ASSERT(vd->vdev_open_thread == curthread || 1222219089Spjd spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1223168404Spjd ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1224168404Spjd vd->vdev_state == VDEV_STATE_CANT_OPEN || 1225168404Spjd vd->vdev_state == VDEV_STATE_OFFLINE); 1226168404Spjd 1227168404Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1228213197Smm vd->vdev_cant_read = B_FALSE; 1229213197Smm vd->vdev_cant_write = B_FALSE; 1230274800Ssmh vd->vdev_notrim = B_FALSE; 1231219089Spjd vd->vdev_min_asize = vdev_get_min_asize(vd); 1232168404Spjd 1233219089Spjd /* 1234219089Spjd * If this vdev is not removed, check its fault status. If it's 1235219089Spjd * faulted, bail out of the open. 1236219089Spjd */ 1237185029Spjd if (!vd->vdev_removed && vd->vdev_faulted) { 1238168404Spjd ASSERT(vd->vdev_children == 0); 1239219089Spjd ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1240219089Spjd vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1241185029Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1242219089Spjd vd->vdev_label_aux); 1243249195Smm return (SET_ERROR(ENXIO)); 1244185029Spjd } else if (vd->vdev_offline) { 1245185029Spjd ASSERT(vd->vdev_children == 0); 1246168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1247249195Smm return (SET_ERROR(ENXIO)); 1248168404Spjd } 1249168404Spjd 1250254591Sgibbs error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, 1251254591Sgibbs &logical_ashift, &physical_ashift); 1252168404Spjd 1253219089Spjd /* 1254219089Spjd * Reset the vdev_reopening flag so that we actually close 1255219089Spjd * the vdev on error. 1256219089Spjd */ 1257219089Spjd vd->vdev_reopening = B_FALSE; 1258168404Spjd if (zio_injection_enabled && error == 0) 1259213198Smm error = zio_handle_device_injection(vd, NULL, ENXIO); 1260168404Spjd 1261185029Spjd if (error) { 1262185029Spjd if (vd->vdev_removed && 1263185029Spjd vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1264185029Spjd vd->vdev_removed = B_FALSE; 1265168404Spjd 1266168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1267168404Spjd vd->vdev_stat.vs_aux); 1268168404Spjd return (error); 1269168404Spjd } 1270168404Spjd 1271185029Spjd vd->vdev_removed = B_FALSE; 1272168404Spjd 1273219089Spjd /* 1274219089Spjd * Recheck the faulted flag now that we have confirmed that 1275219089Spjd * the vdev is accessible. If we're faulted, bail. 1276219089Spjd */ 1277219089Spjd if (vd->vdev_faulted) { 1278219089Spjd ASSERT(vd->vdev_children == 0); 1279219089Spjd ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1280219089Spjd vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1281219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1282219089Spjd vd->vdev_label_aux); 1283249195Smm return (SET_ERROR(ENXIO)); 1284219089Spjd } 1285219089Spjd 1286185029Spjd if (vd->vdev_degraded) { 1287185029Spjd ASSERT(vd->vdev_children == 0); 1288185029Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1289185029Spjd VDEV_AUX_ERR_EXCEEDED); 1290185029Spjd } else { 1291219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1292185029Spjd } 1293185029Spjd 1294219089Spjd /* 1295219089Spjd * For hole or missing vdevs we just return success. 1296219089Spjd */ 1297219089Spjd if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1298219089Spjd return (0); 1299219089Spjd 1300274800Ssmh if (zfs_trim_enabled && !vd->vdev_notrim && vd->vdev_ops->vdev_op_leaf) 1301240868Spjd trim_map_create(vd); 1302240868Spjd 1303219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 1304168404Spjd if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1305168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1306168404Spjd VDEV_AUX_NONE); 1307168404Spjd break; 1308168404Spjd } 1309219089Spjd } 1310168404Spjd 1311168404Spjd osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1312236155Smm max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1313168404Spjd 1314168404Spjd if (vd->vdev_children == 0) { 1315168404Spjd if (osize < SPA_MINDEVSIZE) { 1316168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1317168404Spjd VDEV_AUX_TOO_SMALL); 1318249195Smm return (SET_ERROR(EOVERFLOW)); 1319168404Spjd } 1320168404Spjd psize = osize; 1321168404Spjd asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1322236155Smm max_asize = max_osize - (VDEV_LABEL_START_SIZE + 1323236155Smm VDEV_LABEL_END_SIZE); 1324168404Spjd } else { 1325168404Spjd if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1326168404Spjd (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1327168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1328168404Spjd VDEV_AUX_TOO_SMALL); 1329249195Smm return (SET_ERROR(EOVERFLOW)); 1330168404Spjd } 1331168404Spjd psize = 0; 1332168404Spjd asize = osize; 1333236155Smm max_asize = max_osize; 1334168404Spjd } 1335168404Spjd 1336168404Spjd vd->vdev_psize = psize; 1337168404Spjd 1338219089Spjd /* 1339219089Spjd * Make sure the allocatable size hasn't shrunk. 1340219089Spjd */ 1341219089Spjd if (asize < vd->vdev_min_asize) { 1342219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1343219089Spjd VDEV_AUX_BAD_LABEL); 1344249195Smm return (SET_ERROR(EINVAL)); 1345219089Spjd } 1346219089Spjd 1347254591Sgibbs vd->vdev_physical_ashift = 1348254591Sgibbs MAX(physical_ashift, vd->vdev_physical_ashift); 1349254591Sgibbs vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift); 1350254591Sgibbs vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift); 1351254591Sgibbs 1352254591Sgibbs if (vd->vdev_logical_ashift > SPA_MAXASHIFT) { 1353254591Sgibbs vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1354254591Sgibbs VDEV_AUX_ASHIFT_TOO_BIG); 1355254591Sgibbs return (EINVAL); 1356254591Sgibbs } 1357254591Sgibbs 1358168404Spjd if (vd->vdev_asize == 0) { 1359168404Spjd /* 1360168404Spjd * This is the first-ever open, so use the computed values. 1361168404Spjd * For testing purposes, a higher ashift can be requested. 1362168404Spjd */ 1363168404Spjd vd->vdev_asize = asize; 1364236155Smm vd->vdev_max_asize = max_asize; 1365168404Spjd } else { 1366168404Spjd /* 1367254591Sgibbs * Make sure the alignment requirement hasn't increased. 1368168404Spjd */ 1369254591Sgibbs if (vd->vdev_ashift > vd->vdev_top->vdev_ashift && 1370253441Sdelphij vd->vdev_ops->vdev_op_leaf) { 1371254591Sgibbs vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1372254591Sgibbs VDEV_AUX_BAD_LABEL); 1373254591Sgibbs return (EINVAL); 1374168404Spjd } 1375236155Smm vd->vdev_max_asize = max_asize; 1376219089Spjd } 1377168404Spjd 1378219089Spjd /* 1379219089Spjd * If all children are healthy and the asize has increased, 1380219089Spjd * then we've experienced dynamic LUN growth. If automatic 1381219089Spjd * expansion is enabled then use the additional space. 1382219089Spjd */ 1383219089Spjd if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize && 1384219089Spjd (vd->vdev_expanding || spa->spa_autoexpand)) 1385219089Spjd vd->vdev_asize = asize; 1386168404Spjd 1387219089Spjd vdev_set_min_asize(vd); 1388168404Spjd 1389168404Spjd /* 1390185029Spjd * Ensure we can issue some IO before declaring the 1391185029Spjd * vdev open for business. 1392185029Spjd */ 1393185029Spjd if (vd->vdev_ops->vdev_op_leaf && 1394185029Spjd (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1395219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1396219089Spjd VDEV_AUX_ERR_EXCEEDED); 1397185029Spjd return (error); 1398185029Spjd } 1399185029Spjd 1400185029Spjd /* 1401185029Spjd * If a leaf vdev has a DTL, and seems healthy, then kick off a 1402209962Smm * resilver. But don't do this if we are doing a reopen for a scrub, 1403209962Smm * since this would just restart the scrub we are already doing. 1404168404Spjd */ 1405209962Smm if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1406209962Smm vdev_resilver_needed(vd, NULL, NULL)) 1407209962Smm spa_async_request(spa, SPA_ASYNC_RESILVER); 1408168404Spjd 1409168404Spjd return (0); 1410168404Spjd} 1411168404Spjd 1412168404Spjd/* 1413168404Spjd * Called once the vdevs are all opened, this routine validates the label 1414168404Spjd * contents. This needs to be done before vdev_load() so that we don't 1415185029Spjd * inadvertently do repair I/Os to the wrong device. 1416168404Spjd * 1417230514Smm * If 'strict' is false ignore the spa guid check. This is necessary because 1418230514Smm * if the machine crashed during a re-guid the new guid might have been written 1419230514Smm * to all of the vdev labels, but not the cached config. The strict check 1420230514Smm * will be performed when the pool is opened again using the mos config. 1421230514Smm * 1422168404Spjd * This function will only return failure if one of the vdevs indicates that it 1423168404Spjd * has since been destroyed or exported. This is only possible if 1424168404Spjd * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1425168404Spjd * will be updated but the function will return 0. 1426168404Spjd */ 1427168404Spjdint 1428230514Smmvdev_validate(vdev_t *vd, boolean_t strict) 1429168404Spjd{ 1430168404Spjd spa_t *spa = vd->vdev_spa; 1431168404Spjd nvlist_t *label; 1432219089Spjd uint64_t guid = 0, top_guid; 1433168404Spjd uint64_t state; 1434168404Spjd 1435219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1436230514Smm if (vdev_validate(vd->vdev_child[c], strict) != 0) 1437249195Smm return (SET_ERROR(EBADF)); 1438168404Spjd 1439168404Spjd /* 1440168404Spjd * If the device has already failed, or was marked offline, don't do 1441168404Spjd * any further validation. Otherwise, label I/O will fail and we will 1442168404Spjd * overwrite the previous state. 1443168404Spjd */ 1444185029Spjd if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1445219089Spjd uint64_t aux_guid = 0; 1446219089Spjd nvlist_t *nvl; 1447246631Smm uint64_t txg = spa_last_synced_txg(spa) != 0 ? 1448246631Smm spa_last_synced_txg(spa) : -1ULL; 1449168404Spjd 1450239620Smm if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1451168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1452168404Spjd VDEV_AUX_BAD_LABEL); 1453168404Spjd return (0); 1454168404Spjd } 1455168404Spjd 1456219089Spjd /* 1457219089Spjd * Determine if this vdev has been split off into another 1458219089Spjd * pool. If so, then refuse to open it. 1459219089Spjd */ 1460219089Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1461219089Spjd &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1462219089Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1463219089Spjd VDEV_AUX_SPLIT_POOL); 1464219089Spjd nvlist_free(label); 1465219089Spjd return (0); 1466219089Spjd } 1467219089Spjd 1468230514Smm if (strict && (nvlist_lookup_uint64(label, 1469230514Smm ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1470230514Smm guid != spa_guid(spa))) { 1471168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1472168404Spjd VDEV_AUX_CORRUPT_DATA); 1473168404Spjd nvlist_free(label); 1474168404Spjd return (0); 1475168404Spjd } 1476168404Spjd 1477219089Spjd if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1478219089Spjd != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1479219089Spjd &aux_guid) != 0) 1480219089Spjd aux_guid = 0; 1481219089Spjd 1482185029Spjd /* 1483185029Spjd * If this vdev just became a top-level vdev because its 1484185029Spjd * sibling was detached, it will have adopted the parent's 1485185029Spjd * vdev guid -- but the label may or may not be on disk yet. 1486185029Spjd * Fortunately, either version of the label will have the 1487185029Spjd * same top guid, so if we're a top-level vdev, we can 1488185029Spjd * safely compare to that instead. 1489219089Spjd * 1490219089Spjd * If we split this vdev off instead, then we also check the 1491219089Spjd * original pool's guid. We don't want to consider the vdev 1492219089Spjd * corrupt if it is partway through a split operation. 1493185029Spjd */ 1494168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1495185029Spjd &guid) != 0 || 1496185029Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 1497185029Spjd &top_guid) != 0 || 1498219089Spjd ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) && 1499185029Spjd (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 1500168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1501168404Spjd VDEV_AUX_CORRUPT_DATA); 1502168404Spjd nvlist_free(label); 1503168404Spjd return (0); 1504168404Spjd } 1505168404Spjd 1506168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1507168404Spjd &state) != 0) { 1508168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1509168404Spjd VDEV_AUX_CORRUPT_DATA); 1510168404Spjd nvlist_free(label); 1511168404Spjd return (0); 1512168404Spjd } 1513168404Spjd 1514168404Spjd nvlist_free(label); 1515168404Spjd 1516209962Smm /* 1517219089Spjd * If this is a verbatim import, no need to check the 1518209962Smm * state of the pool. 1519209962Smm */ 1520219089Spjd if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1521219089Spjd spa_load_state(spa) == SPA_LOAD_OPEN && 1522168404Spjd state != POOL_STATE_ACTIVE) 1523249195Smm return (SET_ERROR(EBADF)); 1524185029Spjd 1525185029Spjd /* 1526185029Spjd * If we were able to open and validate a vdev that was 1527185029Spjd * previously marked permanently unavailable, clear that state 1528185029Spjd * now. 1529185029Spjd */ 1530185029Spjd if (vd->vdev_not_present) 1531185029Spjd vd->vdev_not_present = 0; 1532168404Spjd } 1533168404Spjd 1534168404Spjd return (0); 1535168404Spjd} 1536168404Spjd 1537168404Spjd/* 1538168404Spjd * Close a virtual device. 1539168404Spjd */ 1540168404Spjdvoid 1541168404Spjdvdev_close(vdev_t *vd) 1542168404Spjd{ 1543209962Smm spa_t *spa = vd->vdev_spa; 1544219089Spjd vdev_t *pvd = vd->vdev_parent; 1545209962Smm 1546209962Smm ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1547209962Smm 1548219089Spjd /* 1549219089Spjd * If our parent is reopening, then we are as well, unless we are 1550219089Spjd * going offline. 1551219089Spjd */ 1552219089Spjd if (pvd != NULL && pvd->vdev_reopening) 1553219089Spjd vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1554219089Spjd 1555168404Spjd vd->vdev_ops->vdev_op_close(vd); 1556168404Spjd 1557185029Spjd vdev_cache_purge(vd); 1558168404Spjd 1559240868Spjd if (vd->vdev_ops->vdev_op_leaf) 1560240868Spjd trim_map_destroy(vd); 1561240868Spjd 1562168404Spjd /* 1563219089Spjd * We record the previous state before we close it, so that if we are 1564168404Spjd * doing a reopen(), we don't generate FMA ereports if we notice that 1565168404Spjd * it's still faulted. 1566168404Spjd */ 1567168404Spjd vd->vdev_prevstate = vd->vdev_state; 1568168404Spjd 1569168404Spjd if (vd->vdev_offline) 1570168404Spjd vd->vdev_state = VDEV_STATE_OFFLINE; 1571168404Spjd else 1572168404Spjd vd->vdev_state = VDEV_STATE_CLOSED; 1573168404Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1574168404Spjd} 1575168404Spjd 1576168404Spjdvoid 1577219089Spjdvdev_hold(vdev_t *vd) 1578219089Spjd{ 1579219089Spjd spa_t *spa = vd->vdev_spa; 1580219089Spjd 1581219089Spjd ASSERT(spa_is_root(spa)); 1582219089Spjd if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1583219089Spjd return; 1584219089Spjd 1585219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1586219089Spjd vdev_hold(vd->vdev_child[c]); 1587219089Spjd 1588219089Spjd if (vd->vdev_ops->vdev_op_leaf) 1589219089Spjd vd->vdev_ops->vdev_op_hold(vd); 1590219089Spjd} 1591219089Spjd 1592219089Spjdvoid 1593219089Spjdvdev_rele(vdev_t *vd) 1594219089Spjd{ 1595219089Spjd spa_t *spa = vd->vdev_spa; 1596219089Spjd 1597219089Spjd ASSERT(spa_is_root(spa)); 1598219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1599219089Spjd vdev_rele(vd->vdev_child[c]); 1600219089Spjd 1601219089Spjd if (vd->vdev_ops->vdev_op_leaf) 1602219089Spjd vd->vdev_ops->vdev_op_rele(vd); 1603219089Spjd} 1604219089Spjd 1605219089Spjd/* 1606219089Spjd * Reopen all interior vdevs and any unopened leaves. We don't actually 1607219089Spjd * reopen leaf vdevs which had previously been opened as they might deadlock 1608219089Spjd * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1609219089Spjd * If the leaf has never been opened then open it, as usual. 1610219089Spjd */ 1611219089Spjdvoid 1612168404Spjdvdev_reopen(vdev_t *vd) 1613168404Spjd{ 1614168404Spjd spa_t *spa = vd->vdev_spa; 1615168404Spjd 1616185029Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1617168404Spjd 1618219089Spjd /* set the reopening flag unless we're taking the vdev offline */ 1619219089Spjd vd->vdev_reopening = !vd->vdev_offline; 1620168404Spjd vdev_close(vd); 1621168404Spjd (void) vdev_open(vd); 1622168404Spjd 1623168404Spjd /* 1624168404Spjd * Call vdev_validate() here to make sure we have the same device. 1625168404Spjd * Otherwise, a device with an invalid label could be successfully 1626168404Spjd * opened in response to vdev_reopen(). 1627168404Spjd */ 1628185029Spjd if (vd->vdev_aux) { 1629185029Spjd (void) vdev_validate_aux(vd); 1630185029Spjd if (vdev_readable(vd) && vdev_writeable(vd) && 1631209962Smm vd->vdev_aux == &spa->spa_l2cache && 1632219089Spjd !l2arc_vdev_present(vd)) 1633219089Spjd l2arc_add_vdev(spa, vd); 1634185029Spjd } else { 1635246631Smm (void) vdev_validate(vd, B_TRUE); 1636185029Spjd } 1637168404Spjd 1638168404Spjd /* 1639185029Spjd * Reassess parent vdev's health. 1640168404Spjd */ 1641185029Spjd vdev_propagate_state(vd); 1642168404Spjd} 1643168404Spjd 1644168404Spjdint 1645168404Spjdvdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1646168404Spjd{ 1647168404Spjd int error; 1648168404Spjd 1649168404Spjd /* 1650168404Spjd * Normally, partial opens (e.g. of a mirror) are allowed. 1651168404Spjd * For a create, however, we want to fail the request if 1652168404Spjd * there are any components we can't open. 1653168404Spjd */ 1654168404Spjd error = vdev_open(vd); 1655168404Spjd 1656168404Spjd if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1657168404Spjd vdev_close(vd); 1658168404Spjd return (error ? error : ENXIO); 1659168404Spjd } 1660168404Spjd 1661168404Spjd /* 1662262093Savg * Recursively load DTLs and initialize all labels. 1663168404Spjd */ 1664262093Savg if ((error = vdev_dtl_load(vd)) != 0 || 1665262093Savg (error = vdev_label_init(vd, txg, isreplacing ? 1666168404Spjd VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1667168404Spjd vdev_close(vd); 1668168404Spjd return (error); 1669168404Spjd } 1670168404Spjd 1671168404Spjd return (0); 1672168404Spjd} 1673168404Spjd 1674168404Spjdvoid 1675219089Spjdvdev_metaslab_set_size(vdev_t *vd) 1676168404Spjd{ 1677168404Spjd /* 1678273343Sdelphij * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev. 1679168404Spjd */ 1680273343Sdelphij vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev); 1681168404Spjd vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1682168404Spjd} 1683168404Spjd 1684254591Sgibbs/* 1685266122Ssmh * Maximize performance by inflating the configured ashift for top level 1686266122Ssmh * vdevs to be as close to the physical ashift as possible while maintaining 1687266122Ssmh * administrator defined limits and ensuring it doesn't go below the 1688266122Ssmh * logical ashift. 1689254591Sgibbs */ 1690168404Spjdvoid 1691254591Sgibbsvdev_ashift_optimize(vdev_t *vd) 1692254591Sgibbs{ 1693266122Ssmh if (vd == vd->vdev_top) { 1694266122Ssmh if (vd->vdev_ashift < vd->vdev_physical_ashift) { 1695266122Ssmh vd->vdev_ashift = MIN( 1696266122Ssmh MAX(zfs_max_auto_ashift, vd->vdev_ashift), 1697266122Ssmh MAX(zfs_min_auto_ashift, vd->vdev_physical_ashift)); 1698266122Ssmh } else { 1699266122Ssmh /* 1700266122Ssmh * Unusual case where logical ashift > physical ashift 1701266122Ssmh * so we can't cap the calculated ashift based on max 1702266122Ssmh * ashift as that would cause failures. 1703266122Ssmh * We still check if we need to increase it to match 1704266122Ssmh * the min ashift. 1705266122Ssmh */ 1706266122Ssmh vd->vdev_ashift = MAX(zfs_min_auto_ashift, 1707266122Ssmh vd->vdev_ashift); 1708266122Ssmh } 1709254591Sgibbs } 1710254591Sgibbs} 1711254591Sgibbs 1712254591Sgibbsvoid 1713168404Spjdvdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1714168404Spjd{ 1715168404Spjd ASSERT(vd == vd->vdev_top); 1716219089Spjd ASSERT(!vd->vdev_ishole); 1717168404Spjd ASSERT(ISP2(flags)); 1718219089Spjd ASSERT(spa_writeable(vd->vdev_spa)); 1719168404Spjd 1720168404Spjd if (flags & VDD_METASLAB) 1721168404Spjd (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1722168404Spjd 1723168404Spjd if (flags & VDD_DTL) 1724168404Spjd (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1725168404Spjd 1726168404Spjd (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1727168404Spjd} 1728168404Spjd 1729262093Savgvoid 1730262093Savgvdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 1731262093Savg{ 1732262093Savg for (int c = 0; c < vd->vdev_children; c++) 1733262093Savg vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 1734262093Savg 1735262093Savg if (vd->vdev_ops->vdev_op_leaf) 1736262093Savg vdev_dirty(vd->vdev_top, flags, vd, txg); 1737262093Savg} 1738262093Savg 1739209962Smm/* 1740209962Smm * DTLs. 1741209962Smm * 1742209962Smm * A vdev's DTL (dirty time log) is the set of transaction groups for which 1743219089Spjd * the vdev has less than perfect replication. There are four kinds of DTL: 1744209962Smm * 1745209962Smm * DTL_MISSING: txgs for which the vdev has no valid copies of the data 1746209962Smm * 1747209962Smm * DTL_PARTIAL: txgs for which data is available, but not fully replicated 1748209962Smm * 1749209962Smm * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 1750209962Smm * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 1751209962Smm * txgs that was scrubbed. 1752209962Smm * 1753209962Smm * DTL_OUTAGE: txgs which cannot currently be read, whether due to 1754209962Smm * persistent errors or just some device being offline. 1755209962Smm * Unlike the other three, the DTL_OUTAGE map is not generally 1756209962Smm * maintained; it's only computed when needed, typically to 1757209962Smm * determine whether a device can be detached. 1758209962Smm * 1759209962Smm * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 1760209962Smm * either has the data or it doesn't. 1761209962Smm * 1762209962Smm * For interior vdevs such as mirror and RAID-Z the picture is more complex. 1763209962Smm * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 1764209962Smm * if any child is less than fully replicated, then so is its parent. 1765209962Smm * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 1766209962Smm * comprising only those txgs which appear in 'maxfaults' or more children; 1767209962Smm * those are the txgs we don't have enough replication to read. For example, 1768209962Smm * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 1769209962Smm * thus, its DTL_MISSING consists of the set of txgs that appear in more than 1770209962Smm * two child DTL_MISSING maps. 1771209962Smm * 1772209962Smm * It should be clear from the above that to compute the DTLs and outage maps 1773209962Smm * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 1774209962Smm * Therefore, that is all we keep on disk. When loading the pool, or after 1775209962Smm * a configuration change, we generate all other DTLs from first principles. 1776209962Smm */ 1777168404Spjdvoid 1778209962Smmvdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1779168404Spjd{ 1780262093Savg range_tree_t *rt = vd->vdev_dtl[t]; 1781209962Smm 1782209962Smm ASSERT(t < DTL_TYPES); 1783209962Smm ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1784219089Spjd ASSERT(spa_writeable(vd->vdev_spa)); 1785209962Smm 1786262093Savg mutex_enter(rt->rt_lock); 1787262093Savg if (!range_tree_contains(rt, txg, size)) 1788262093Savg range_tree_add(rt, txg, size); 1789262093Savg mutex_exit(rt->rt_lock); 1790168404Spjd} 1791168404Spjd 1792209962Smmboolean_t 1793209962Smmvdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1794168404Spjd{ 1795262093Savg range_tree_t *rt = vd->vdev_dtl[t]; 1796209962Smm boolean_t dirty = B_FALSE; 1797168404Spjd 1798209962Smm ASSERT(t < DTL_TYPES); 1799209962Smm ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1800168404Spjd 1801262093Savg mutex_enter(rt->rt_lock); 1802262093Savg if (range_tree_space(rt) != 0) 1803262093Savg dirty = range_tree_contains(rt, txg, size); 1804262093Savg mutex_exit(rt->rt_lock); 1805168404Spjd 1806168404Spjd return (dirty); 1807168404Spjd} 1808168404Spjd 1809209962Smmboolean_t 1810209962Smmvdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 1811209962Smm{ 1812262093Savg range_tree_t *rt = vd->vdev_dtl[t]; 1813209962Smm boolean_t empty; 1814209962Smm 1815262093Savg mutex_enter(rt->rt_lock); 1816262093Savg empty = (range_tree_space(rt) == 0); 1817262093Savg mutex_exit(rt->rt_lock); 1818209962Smm 1819209962Smm return (empty); 1820209962Smm} 1821209962Smm 1822168404Spjd/* 1823254112Sdelphij * Returns the lowest txg in the DTL range. 1824254112Sdelphij */ 1825254112Sdelphijstatic uint64_t 1826254112Sdelphijvdev_dtl_min(vdev_t *vd) 1827254112Sdelphij{ 1828262093Savg range_seg_t *rs; 1829254112Sdelphij 1830254112Sdelphij ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 1831262093Savg ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1832254112Sdelphij ASSERT0(vd->vdev_children); 1833254112Sdelphij 1834262093Savg rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 1835262093Savg return (rs->rs_start - 1); 1836254112Sdelphij} 1837254112Sdelphij 1838254112Sdelphij/* 1839254112Sdelphij * Returns the highest txg in the DTL. 1840254112Sdelphij */ 1841254112Sdelphijstatic uint64_t 1842254112Sdelphijvdev_dtl_max(vdev_t *vd) 1843254112Sdelphij{ 1844262093Savg range_seg_t *rs; 1845254112Sdelphij 1846254112Sdelphij ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 1847262093Savg ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1848254112Sdelphij ASSERT0(vd->vdev_children); 1849254112Sdelphij 1850262093Savg rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 1851262093Savg return (rs->rs_end); 1852254112Sdelphij} 1853254112Sdelphij 1854254112Sdelphij/* 1855254112Sdelphij * Determine if a resilvering vdev should remove any DTL entries from 1856254112Sdelphij * its range. If the vdev was resilvering for the entire duration of the 1857254112Sdelphij * scan then it should excise that range from its DTLs. Otherwise, this 1858254112Sdelphij * vdev is considered partially resilvered and should leave its DTL 1859254112Sdelphij * entries intact. The comment in vdev_dtl_reassess() describes how we 1860254112Sdelphij * excise the DTLs. 1861254112Sdelphij */ 1862254112Sdelphijstatic boolean_t 1863254112Sdelphijvdev_dtl_should_excise(vdev_t *vd) 1864254112Sdelphij{ 1865254112Sdelphij spa_t *spa = vd->vdev_spa; 1866254112Sdelphij dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1867254112Sdelphij 1868254112Sdelphij ASSERT0(scn->scn_phys.scn_errors); 1869254112Sdelphij ASSERT0(vd->vdev_children); 1870254112Sdelphij 1871254112Sdelphij if (vd->vdev_resilver_txg == 0 || 1872262093Savg range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0) 1873254112Sdelphij return (B_TRUE); 1874254112Sdelphij 1875254112Sdelphij /* 1876254112Sdelphij * When a resilver is initiated the scan will assign the scn_max_txg 1877254112Sdelphij * value to the highest txg value that exists in all DTLs. If this 1878254112Sdelphij * device's max DTL is not part of this scan (i.e. it is not in 1879254112Sdelphij * the range (scn_min_txg, scn_max_txg] then it is not eligible 1880254112Sdelphij * for excision. 1881254112Sdelphij */ 1882254112Sdelphij if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 1883254112Sdelphij ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 1884254112Sdelphij ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 1885254112Sdelphij ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 1886254112Sdelphij return (B_TRUE); 1887254112Sdelphij } 1888254112Sdelphij return (B_FALSE); 1889254112Sdelphij} 1890254112Sdelphij 1891254112Sdelphij/* 1892168404Spjd * Reassess DTLs after a config change or scrub completion. 1893168404Spjd */ 1894168404Spjdvoid 1895168404Spjdvdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1896168404Spjd{ 1897168404Spjd spa_t *spa = vd->vdev_spa; 1898209962Smm avl_tree_t reftree; 1899209962Smm int minref; 1900168404Spjd 1901209962Smm ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1902168404Spjd 1903209962Smm for (int c = 0; c < vd->vdev_children; c++) 1904209962Smm vdev_dtl_reassess(vd->vdev_child[c], txg, 1905209962Smm scrub_txg, scrub_done); 1906209962Smm 1907219089Spjd if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux) 1908209962Smm return; 1909209962Smm 1910209962Smm if (vd->vdev_ops->vdev_op_leaf) { 1911219089Spjd dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1912219089Spjd 1913168404Spjd mutex_enter(&vd->vdev_dtl_lock); 1914254112Sdelphij 1915254112Sdelphij /* 1916254112Sdelphij * If we've completed a scan cleanly then determine 1917254112Sdelphij * if this vdev should remove any DTLs. We only want to 1918254112Sdelphij * excise regions on vdevs that were available during 1919254112Sdelphij * the entire duration of this scan. 1920254112Sdelphij */ 1921185029Spjd if (scrub_txg != 0 && 1922219089Spjd (spa->spa_scrub_started || 1923254112Sdelphij (scn != NULL && scn->scn_phys.scn_errors == 0)) && 1924254112Sdelphij vdev_dtl_should_excise(vd)) { 1925185029Spjd /* 1926185029Spjd * We completed a scrub up to scrub_txg. If we 1927185029Spjd * did it without rebooting, then the scrub dtl 1928185029Spjd * will be valid, so excise the old region and 1929185029Spjd * fold in the scrub dtl. Otherwise, leave the 1930185029Spjd * dtl as-is if there was an error. 1931209962Smm * 1932209962Smm * There's little trick here: to excise the beginning 1933209962Smm * of the DTL_MISSING map, we put it into a reference 1934209962Smm * tree and then add a segment with refcnt -1 that 1935209962Smm * covers the range [0, scrub_txg). This means 1936209962Smm * that each txg in that range has refcnt -1 or 0. 1937209962Smm * We then add DTL_SCRUB with a refcnt of 2, so that 1938209962Smm * entries in the range [0, scrub_txg) will have a 1939209962Smm * positive refcnt -- either 1 or 2. We then convert 1940209962Smm * the reference tree into the new DTL_MISSING map. 1941185029Spjd */ 1942262093Savg space_reftree_create(&reftree); 1943262093Savg space_reftree_add_map(&reftree, 1944262093Savg vd->vdev_dtl[DTL_MISSING], 1); 1945262093Savg space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 1946262093Savg space_reftree_add_map(&reftree, 1947262093Savg vd->vdev_dtl[DTL_SCRUB], 2); 1948262093Savg space_reftree_generate_map(&reftree, 1949262093Savg vd->vdev_dtl[DTL_MISSING], 1); 1950262093Savg space_reftree_destroy(&reftree); 1951168404Spjd } 1952262093Savg range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 1953262093Savg range_tree_walk(vd->vdev_dtl[DTL_MISSING], 1954262093Savg range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 1955168404Spjd if (scrub_done) 1956262093Savg range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 1957262093Savg range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 1958209962Smm if (!vdev_readable(vd)) 1959262093Savg range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 1960209962Smm else 1961262093Savg range_tree_walk(vd->vdev_dtl[DTL_MISSING], 1962262093Savg range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 1963254112Sdelphij 1964254112Sdelphij /* 1965254112Sdelphij * If the vdev was resilvering and no longer has any 1966271776Ssmh * DTLs then reset its resilvering flag and dirty 1967271776Ssmh * the top level so that we persist the change. 1968254112Sdelphij */ 1969254112Sdelphij if (vd->vdev_resilver_txg != 0 && 1970262093Savg range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 && 1971271776Ssmh range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) { 1972254112Sdelphij vd->vdev_resilver_txg = 0; 1973271776Ssmh vdev_config_dirty(vd->vdev_top); 1974271776Ssmh } 1975254112Sdelphij 1976168404Spjd mutex_exit(&vd->vdev_dtl_lock); 1977185029Spjd 1978168404Spjd if (txg != 0) 1979168404Spjd vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1980168404Spjd return; 1981168404Spjd } 1982168404Spjd 1983168404Spjd mutex_enter(&vd->vdev_dtl_lock); 1984209962Smm for (int t = 0; t < DTL_TYPES; t++) { 1985209962Smm /* account for child's outage in parent's missing map */ 1986209962Smm int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 1987209962Smm if (t == DTL_SCRUB) 1988209962Smm continue; /* leaf vdevs only */ 1989209962Smm if (t == DTL_PARTIAL) 1990209962Smm minref = 1; /* i.e. non-zero */ 1991209962Smm else if (vd->vdev_nparity != 0) 1992209962Smm minref = vd->vdev_nparity + 1; /* RAID-Z */ 1993209962Smm else 1994209962Smm minref = vd->vdev_children; /* any kind of mirror */ 1995262093Savg space_reftree_create(&reftree); 1996209962Smm for (int c = 0; c < vd->vdev_children; c++) { 1997209962Smm vdev_t *cvd = vd->vdev_child[c]; 1998209962Smm mutex_enter(&cvd->vdev_dtl_lock); 1999262093Savg space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 2000209962Smm mutex_exit(&cvd->vdev_dtl_lock); 2001209962Smm } 2002262093Savg space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 2003262093Savg space_reftree_destroy(&reftree); 2004209962Smm } 2005168404Spjd mutex_exit(&vd->vdev_dtl_lock); 2006168404Spjd} 2007168404Spjd 2008262093Savgint 2009168404Spjdvdev_dtl_load(vdev_t *vd) 2010168404Spjd{ 2011168404Spjd spa_t *spa = vd->vdev_spa; 2012168404Spjd objset_t *mos = spa->spa_meta_objset; 2013262093Savg int error = 0; 2014168404Spjd 2015262093Savg if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 2016262093Savg ASSERT(!vd->vdev_ishole); 2017168404Spjd 2018262093Savg error = space_map_open(&vd->vdev_dtl_sm, mos, 2019262093Savg vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock); 2020262093Savg if (error) 2021262093Savg return (error); 2022262093Savg ASSERT(vd->vdev_dtl_sm != NULL); 2023168404Spjd 2024262093Savg mutex_enter(&vd->vdev_dtl_lock); 2025219089Spjd 2026262093Savg /* 2027262093Savg * Now that we've opened the space_map we need to update 2028262093Savg * the in-core DTL. 2029262093Savg */ 2030262093Savg space_map_update(vd->vdev_dtl_sm); 2031262093Savg 2032262093Savg error = space_map_load(vd->vdev_dtl_sm, 2033262093Savg vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 2034262093Savg mutex_exit(&vd->vdev_dtl_lock); 2035262093Savg 2036168404Spjd return (error); 2037262093Savg } 2038168404Spjd 2039262093Savg for (int c = 0; c < vd->vdev_children; c++) { 2040262093Savg error = vdev_dtl_load(vd->vdev_child[c]); 2041262093Savg if (error != 0) 2042262093Savg break; 2043262093Savg } 2044168404Spjd 2045168404Spjd return (error); 2046168404Spjd} 2047168404Spjd 2048168404Spjdvoid 2049168404Spjdvdev_dtl_sync(vdev_t *vd, uint64_t txg) 2050168404Spjd{ 2051168404Spjd spa_t *spa = vd->vdev_spa; 2052262093Savg range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2053168404Spjd objset_t *mos = spa->spa_meta_objset; 2054262093Savg range_tree_t *rtsync; 2055262093Savg kmutex_t rtlock; 2056168404Spjd dmu_tx_t *tx; 2057262093Savg uint64_t object = space_map_object(vd->vdev_dtl_sm); 2058168404Spjd 2059219089Spjd ASSERT(!vd->vdev_ishole); 2060262093Savg ASSERT(vd->vdev_ops->vdev_op_leaf); 2061219089Spjd 2062168404Spjd tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2063168404Spjd 2064262093Savg if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2065262093Savg mutex_enter(&vd->vdev_dtl_lock); 2066262093Savg space_map_free(vd->vdev_dtl_sm, tx); 2067262093Savg space_map_close(vd->vdev_dtl_sm); 2068262093Savg vd->vdev_dtl_sm = NULL; 2069262093Savg mutex_exit(&vd->vdev_dtl_lock); 2070168404Spjd dmu_tx_commit(tx); 2071168404Spjd return; 2072168404Spjd } 2073168404Spjd 2074262093Savg if (vd->vdev_dtl_sm == NULL) { 2075262093Savg uint64_t new_object; 2076262093Savg 2077262093Savg new_object = space_map_alloc(mos, tx); 2078262093Savg VERIFY3U(new_object, !=, 0); 2079262093Savg 2080262093Savg VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2081262093Savg 0, -1ULL, 0, &vd->vdev_dtl_lock)); 2082262093Savg ASSERT(vd->vdev_dtl_sm != NULL); 2083168404Spjd } 2084168404Spjd 2085262093Savg bzero(&rtlock, sizeof(rtlock)); 2086262093Savg mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL); 2087168404Spjd 2088262093Savg rtsync = range_tree_create(NULL, NULL, &rtlock); 2089168404Spjd 2090262093Savg mutex_enter(&rtlock); 2091168404Spjd 2092168404Spjd mutex_enter(&vd->vdev_dtl_lock); 2093262093Savg range_tree_walk(rt, range_tree_add, rtsync); 2094168404Spjd mutex_exit(&vd->vdev_dtl_lock); 2095168404Spjd 2096262093Savg space_map_truncate(vd->vdev_dtl_sm, tx); 2097262093Savg space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx); 2098262093Savg range_tree_vacate(rtsync, NULL, NULL); 2099168404Spjd 2100262093Savg range_tree_destroy(rtsync); 2101168404Spjd 2102262093Savg mutex_exit(&rtlock); 2103262093Savg mutex_destroy(&rtlock); 2104168404Spjd 2105262093Savg /* 2106262093Savg * If the object for the space map has changed then dirty 2107262093Savg * the top level so that we update the config. 2108262093Savg */ 2109262093Savg if (object != space_map_object(vd->vdev_dtl_sm)) { 2110262093Savg zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, " 2111262093Savg "new object %llu", txg, spa_name(spa), object, 2112262093Savg space_map_object(vd->vdev_dtl_sm)); 2113262093Savg vdev_config_dirty(vd->vdev_top); 2114262093Savg } 2115168404Spjd 2116168404Spjd dmu_tx_commit(tx); 2117262093Savg 2118262093Savg mutex_enter(&vd->vdev_dtl_lock); 2119262093Savg space_map_update(vd->vdev_dtl_sm); 2120262093Savg mutex_exit(&vd->vdev_dtl_lock); 2121168404Spjd} 2122168404Spjd 2123185029Spjd/* 2124209962Smm * Determine whether the specified vdev can be offlined/detached/removed 2125209962Smm * without losing data. 2126209962Smm */ 2127209962Smmboolean_t 2128209962Smmvdev_dtl_required(vdev_t *vd) 2129209962Smm{ 2130209962Smm spa_t *spa = vd->vdev_spa; 2131209962Smm vdev_t *tvd = vd->vdev_top; 2132209962Smm uint8_t cant_read = vd->vdev_cant_read; 2133209962Smm boolean_t required; 2134209962Smm 2135209962Smm ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2136209962Smm 2137209962Smm if (vd == spa->spa_root_vdev || vd == tvd) 2138209962Smm return (B_TRUE); 2139209962Smm 2140209962Smm /* 2141209962Smm * Temporarily mark the device as unreadable, and then determine 2142209962Smm * whether this results in any DTL outages in the top-level vdev. 2143209962Smm * If not, we can safely offline/detach/remove the device. 2144209962Smm */ 2145209962Smm vd->vdev_cant_read = B_TRUE; 2146209962Smm vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2147209962Smm required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2148209962Smm vd->vdev_cant_read = cant_read; 2149209962Smm vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2150209962Smm 2151219089Spjd if (!required && zio_injection_enabled) 2152219089Spjd required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2153219089Spjd 2154209962Smm return (required); 2155209962Smm} 2156209962Smm 2157209962Smm/* 2158185029Spjd * Determine if resilver is needed, and if so the txg range. 2159185029Spjd */ 2160185029Spjdboolean_t 2161185029Spjdvdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2162185029Spjd{ 2163185029Spjd boolean_t needed = B_FALSE; 2164185029Spjd uint64_t thismin = UINT64_MAX; 2165185029Spjd uint64_t thismax = 0; 2166185029Spjd 2167185029Spjd if (vd->vdev_children == 0) { 2168185029Spjd mutex_enter(&vd->vdev_dtl_lock); 2169262093Savg if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 && 2170209962Smm vdev_writeable(vd)) { 2171185029Spjd 2172254112Sdelphij thismin = vdev_dtl_min(vd); 2173254112Sdelphij thismax = vdev_dtl_max(vd); 2174185029Spjd needed = B_TRUE; 2175185029Spjd } 2176185029Spjd mutex_exit(&vd->vdev_dtl_lock); 2177185029Spjd } else { 2178209962Smm for (int c = 0; c < vd->vdev_children; c++) { 2179185029Spjd vdev_t *cvd = vd->vdev_child[c]; 2180185029Spjd uint64_t cmin, cmax; 2181185029Spjd 2182185029Spjd if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2183185029Spjd thismin = MIN(thismin, cmin); 2184185029Spjd thismax = MAX(thismax, cmax); 2185185029Spjd needed = B_TRUE; 2186185029Spjd } 2187185029Spjd } 2188185029Spjd } 2189185029Spjd 2190185029Spjd if (needed && minp) { 2191185029Spjd *minp = thismin; 2192185029Spjd *maxp = thismax; 2193185029Spjd } 2194185029Spjd return (needed); 2195185029Spjd} 2196185029Spjd 2197168404Spjdvoid 2198168404Spjdvdev_load(vdev_t *vd) 2199168404Spjd{ 2200168404Spjd /* 2201168404Spjd * Recursively load all children. 2202168404Spjd */ 2203209962Smm for (int c = 0; c < vd->vdev_children; c++) 2204168404Spjd vdev_load(vd->vdev_child[c]); 2205168404Spjd 2206168404Spjd /* 2207168404Spjd * If this is a top-level vdev, initialize its metaslabs. 2208168404Spjd */ 2209219089Spjd if (vd == vd->vdev_top && !vd->vdev_ishole && 2210168404Spjd (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 2211168404Spjd vdev_metaslab_init(vd, 0) != 0)) 2212168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2213168404Spjd VDEV_AUX_CORRUPT_DATA); 2214168404Spjd 2215168404Spjd /* 2216168404Spjd * If this is a leaf vdev, load its DTL. 2217168404Spjd */ 2218168404Spjd if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 2219168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2220168404Spjd VDEV_AUX_CORRUPT_DATA); 2221168404Spjd} 2222168404Spjd 2223168404Spjd/* 2224185029Spjd * The special vdev case is used for hot spares and l2cache devices. Its 2225185029Spjd * sole purpose it to set the vdev state for the associated vdev. To do this, 2226185029Spjd * we make sure that we can open the underlying device, then try to read the 2227185029Spjd * label, and make sure that the label is sane and that it hasn't been 2228185029Spjd * repurposed to another pool. 2229168404Spjd */ 2230168404Spjdint 2231185029Spjdvdev_validate_aux(vdev_t *vd) 2232168404Spjd{ 2233168404Spjd nvlist_t *label; 2234168404Spjd uint64_t guid, version; 2235168404Spjd uint64_t state; 2236168404Spjd 2237185029Spjd if (!vdev_readable(vd)) 2238185029Spjd return (0); 2239185029Spjd 2240239620Smm if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 2241168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2242168404Spjd VDEV_AUX_CORRUPT_DATA); 2243168404Spjd return (-1); 2244168404Spjd } 2245168404Spjd 2246168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2247236884Smm !SPA_VERSION_IS_SUPPORTED(version) || 2248168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 2249168404Spjd guid != vd->vdev_guid || 2250168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 2251168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2252168404Spjd VDEV_AUX_CORRUPT_DATA); 2253168404Spjd nvlist_free(label); 2254168404Spjd return (-1); 2255168404Spjd } 2256168404Spjd 2257168404Spjd /* 2258168404Spjd * We don't actually check the pool state here. If it's in fact in 2259168404Spjd * use by another pool, we update this fact on the fly when requested. 2260168404Spjd */ 2261168404Spjd nvlist_free(label); 2262168404Spjd return (0); 2263168404Spjd} 2264168404Spjd 2265168404Spjdvoid 2266219089Spjdvdev_remove(vdev_t *vd, uint64_t txg) 2267219089Spjd{ 2268219089Spjd spa_t *spa = vd->vdev_spa; 2269219089Spjd objset_t *mos = spa->spa_meta_objset; 2270219089Spjd dmu_tx_t *tx; 2271219089Spjd 2272219089Spjd tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2273219089Spjd 2274219089Spjd if (vd->vdev_ms != NULL) { 2275269773Sdelphij metaslab_group_t *mg = vd->vdev_mg; 2276269773Sdelphij 2277269773Sdelphij metaslab_group_histogram_verify(mg); 2278269773Sdelphij metaslab_class_histogram_verify(mg->mg_class); 2279269773Sdelphij 2280219089Spjd for (int m = 0; m < vd->vdev_ms_count; m++) { 2281219089Spjd metaslab_t *msp = vd->vdev_ms[m]; 2282219089Spjd 2283262093Savg if (msp == NULL || msp->ms_sm == NULL) 2284219089Spjd continue; 2285219089Spjd 2286262093Savg mutex_enter(&msp->ms_lock); 2287269773Sdelphij /* 2288269773Sdelphij * If the metaslab was not loaded when the vdev 2289269773Sdelphij * was removed then the histogram accounting may 2290269773Sdelphij * not be accurate. Update the histogram information 2291269773Sdelphij * here so that we ensure that the metaslab group 2292269773Sdelphij * and metaslab class are up-to-date. 2293269773Sdelphij */ 2294269773Sdelphij metaslab_group_histogram_remove(mg, msp); 2295269773Sdelphij 2296262093Savg VERIFY0(space_map_allocated(msp->ms_sm)); 2297262093Savg space_map_free(msp->ms_sm, tx); 2298262093Savg space_map_close(msp->ms_sm); 2299262093Savg msp->ms_sm = NULL; 2300262093Savg mutex_exit(&msp->ms_lock); 2301219089Spjd } 2302269773Sdelphij 2303269773Sdelphij metaslab_group_histogram_verify(mg); 2304269773Sdelphij metaslab_class_histogram_verify(mg->mg_class); 2305269773Sdelphij for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 2306269773Sdelphij ASSERT0(mg->mg_histogram[i]); 2307269773Sdelphij 2308219089Spjd } 2309219089Spjd 2310219089Spjd if (vd->vdev_ms_array) { 2311219089Spjd (void) dmu_object_free(mos, vd->vdev_ms_array, tx); 2312219089Spjd vd->vdev_ms_array = 0; 2313219089Spjd } 2314219089Spjd dmu_tx_commit(tx); 2315219089Spjd} 2316219089Spjd 2317219089Spjdvoid 2318168404Spjdvdev_sync_done(vdev_t *vd, uint64_t txg) 2319168404Spjd{ 2320168404Spjd metaslab_t *msp; 2321211931Smm boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2322168404Spjd 2323219089Spjd ASSERT(!vd->vdev_ishole); 2324219089Spjd 2325168404Spjd while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2326168404Spjd metaslab_sync_done(msp, txg); 2327211931Smm 2328211931Smm if (reassess) 2329211931Smm metaslab_sync_reassess(vd->vdev_mg); 2330168404Spjd} 2331168404Spjd 2332168404Spjdvoid 2333168404Spjdvdev_sync(vdev_t *vd, uint64_t txg) 2334168404Spjd{ 2335168404Spjd spa_t *spa = vd->vdev_spa; 2336168404Spjd vdev_t *lvd; 2337168404Spjd metaslab_t *msp; 2338168404Spjd dmu_tx_t *tx; 2339168404Spjd 2340219089Spjd ASSERT(!vd->vdev_ishole); 2341219089Spjd 2342168404Spjd if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 2343168404Spjd ASSERT(vd == vd->vdev_top); 2344168404Spjd tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2345168404Spjd vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2346168404Spjd DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2347168404Spjd ASSERT(vd->vdev_ms_array != 0); 2348168404Spjd vdev_config_dirty(vd); 2349168404Spjd dmu_tx_commit(tx); 2350168404Spjd } 2351168404Spjd 2352219089Spjd /* 2353219089Spjd * Remove the metadata associated with this vdev once it's empty. 2354219089Spjd */ 2355219089Spjd if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 2356219089Spjd vdev_remove(vd, txg); 2357219089Spjd 2358168404Spjd while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2359168404Spjd metaslab_sync(msp, txg); 2360168404Spjd (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2361168404Spjd } 2362168404Spjd 2363168404Spjd while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2364168404Spjd vdev_dtl_sync(lvd, txg); 2365168404Spjd 2366168404Spjd (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2367168404Spjd} 2368168404Spjd 2369168404Spjduint64_t 2370168404Spjdvdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2371168404Spjd{ 2372168404Spjd return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2373168404Spjd} 2374168404Spjd 2375185029Spjd/* 2376185029Spjd * Mark the given vdev faulted. A faulted vdev behaves as if the device could 2377185029Spjd * not be opened, and no I/O is attempted. 2378185029Spjd */ 2379185029Spjdint 2380219089Spjdvdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2381168404Spjd{ 2382219089Spjd vdev_t *vd, *tvd; 2383168404Spjd 2384219089Spjd spa_vdev_state_enter(spa, SCL_NONE); 2385185029Spjd 2386185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2387185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2388185029Spjd 2389185029Spjd if (!vd->vdev_ops->vdev_op_leaf) 2390185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2391185029Spjd 2392219089Spjd tvd = vd->vdev_top; 2393219089Spjd 2394185029Spjd /* 2395219089Spjd * We don't directly use the aux state here, but if we do a 2396219089Spjd * vdev_reopen(), we need this value to be present to remember why we 2397219089Spjd * were faulted. 2398219089Spjd */ 2399219089Spjd vd->vdev_label_aux = aux; 2400219089Spjd 2401219089Spjd /* 2402185029Spjd * Faulted state takes precedence over degraded. 2403185029Spjd */ 2404219089Spjd vd->vdev_delayed_close = B_FALSE; 2405185029Spjd vd->vdev_faulted = 1ULL; 2406185029Spjd vd->vdev_degraded = 0ULL; 2407219089Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 2408185029Spjd 2409185029Spjd /* 2410219089Spjd * If this device has the only valid copy of the data, then 2411219089Spjd * back off and simply mark the vdev as degraded instead. 2412185029Spjd */ 2413219089Spjd if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 2414185029Spjd vd->vdev_degraded = 1ULL; 2415185029Spjd vd->vdev_faulted = 0ULL; 2416185029Spjd 2417185029Spjd /* 2418185029Spjd * If we reopen the device and it's not dead, only then do we 2419185029Spjd * mark it degraded. 2420185029Spjd */ 2421219089Spjd vdev_reopen(tvd); 2422185029Spjd 2423219089Spjd if (vdev_readable(vd)) 2424219089Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 2425185029Spjd } 2426185029Spjd 2427185029Spjd return (spa_vdev_state_exit(spa, vd, 0)); 2428168404Spjd} 2429168404Spjd 2430185029Spjd/* 2431185029Spjd * Mark the given vdev degraded. A degraded vdev is purely an indication to the 2432185029Spjd * user that something is wrong. The vdev continues to operate as normal as far 2433185029Spjd * as I/O is concerned. 2434185029Spjd */ 2435185029Spjdint 2436219089Spjdvdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2437168404Spjd{ 2438185029Spjd vdev_t *vd; 2439168404Spjd 2440219089Spjd spa_vdev_state_enter(spa, SCL_NONE); 2441168404Spjd 2442185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2443185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2444168404Spjd 2445185029Spjd if (!vd->vdev_ops->vdev_op_leaf) 2446185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2447185029Spjd 2448185029Spjd /* 2449185029Spjd * If the vdev is already faulted, then don't do anything. 2450185029Spjd */ 2451185029Spjd if (vd->vdev_faulted || vd->vdev_degraded) 2452185029Spjd return (spa_vdev_state_exit(spa, NULL, 0)); 2453185029Spjd 2454185029Spjd vd->vdev_degraded = 1ULL; 2455185029Spjd if (!vdev_is_dead(vd)) 2456185029Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2457219089Spjd aux); 2458185029Spjd 2459185029Spjd return (spa_vdev_state_exit(spa, vd, 0)); 2460168404Spjd} 2461168404Spjd 2462185029Spjd/* 2463251631Sdelphij * Online the given vdev. 2464251631Sdelphij * 2465251631Sdelphij * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2466251631Sdelphij * spare device should be detached when the device finishes resilvering. 2467251631Sdelphij * Second, the online should be treated like a 'test' online case, so no FMA 2468251631Sdelphij * events are generated if the device fails to open. 2469185029Spjd */ 2470168404Spjdint 2471185029Spjdvdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2472168404Spjd{ 2473219089Spjd vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2474168404Spjd 2475219089Spjd spa_vdev_state_enter(spa, SCL_NONE); 2476168404Spjd 2477185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2478185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2479168404Spjd 2480168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 2481185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2482168404Spjd 2483219089Spjd tvd = vd->vdev_top; 2484168404Spjd vd->vdev_offline = B_FALSE; 2485168404Spjd vd->vdev_tmpoffline = B_FALSE; 2486185029Spjd vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 2487185029Spjd vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 2488219089Spjd 2489219089Spjd /* XXX - L2ARC 1.0 does not support expansion */ 2490219089Spjd if (!vd->vdev_aux) { 2491219089Spjd for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2492219089Spjd pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 2493219089Spjd } 2494219089Spjd 2495219089Spjd vdev_reopen(tvd); 2496185029Spjd vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 2497168404Spjd 2498219089Spjd if (!vd->vdev_aux) { 2499219089Spjd for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2500219089Spjd pvd->vdev_expanding = B_FALSE; 2501219089Spjd } 2502219089Spjd 2503185029Spjd if (newstate) 2504185029Spjd *newstate = vd->vdev_state; 2505185029Spjd if ((flags & ZFS_ONLINE_UNSPARE) && 2506185029Spjd !vdev_is_dead(vd) && vd->vdev_parent && 2507185029Spjd vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2508185029Spjd vd->vdev_parent->vdev_child[0] == vd) 2509185029Spjd vd->vdev_unspare = B_TRUE; 2510168404Spjd 2511219089Spjd if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 2512219089Spjd 2513219089Spjd /* XXX - L2ARC 1.0 does not support expansion */ 2514219089Spjd if (vd->vdev_aux) 2515219089Spjd return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 2516219089Spjd spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 2517219089Spjd } 2518209962Smm return (spa_vdev_state_exit(spa, vd, 0)); 2519168404Spjd} 2520168404Spjd 2521219089Spjdstatic int 2522219089Spjdvdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 2523168404Spjd{ 2524213197Smm vdev_t *vd, *tvd; 2525219089Spjd int error = 0; 2526219089Spjd uint64_t generation; 2527219089Spjd metaslab_group_t *mg; 2528168404Spjd 2529219089Spjdtop: 2530219089Spjd spa_vdev_state_enter(spa, SCL_ALLOC); 2531168404Spjd 2532185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2533185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2534168404Spjd 2535168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 2536185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2537168404Spjd 2538213197Smm tvd = vd->vdev_top; 2539219089Spjd mg = tvd->vdev_mg; 2540219089Spjd generation = spa->spa_config_generation + 1; 2541213197Smm 2542168404Spjd /* 2543168404Spjd * If the device isn't already offline, try to offline it. 2544168404Spjd */ 2545168404Spjd if (!vd->vdev_offline) { 2546168404Spjd /* 2547209962Smm * If this device has the only valid copy of some data, 2548213197Smm * don't allow it to be offlined. Log devices are always 2549213197Smm * expendable. 2550168404Spjd */ 2551213197Smm if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2552213197Smm vdev_dtl_required(vd)) 2553185029Spjd return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2554168404Spjd 2555168404Spjd /* 2556219089Spjd * If the top-level is a slog and it has had allocations 2557219089Spjd * then proceed. We check that the vdev's metaslab group 2558219089Spjd * is not NULL since it's possible that we may have just 2559219089Spjd * added this vdev but not yet initialized its metaslabs. 2560219089Spjd */ 2561219089Spjd if (tvd->vdev_islog && mg != NULL) { 2562219089Spjd /* 2563219089Spjd * Prevent any future allocations. 2564219089Spjd */ 2565219089Spjd metaslab_group_passivate(mg); 2566219089Spjd (void) spa_vdev_state_exit(spa, vd, 0); 2567219089Spjd 2568219089Spjd error = spa_offline_log(spa); 2569219089Spjd 2570219089Spjd spa_vdev_state_enter(spa, SCL_ALLOC); 2571219089Spjd 2572219089Spjd /* 2573219089Spjd * Check to see if the config has changed. 2574219089Spjd */ 2575219089Spjd if (error || generation != spa->spa_config_generation) { 2576219089Spjd metaslab_group_activate(mg); 2577219089Spjd if (error) 2578219089Spjd return (spa_vdev_state_exit(spa, 2579219089Spjd vd, error)); 2580219089Spjd (void) spa_vdev_state_exit(spa, vd, 0); 2581219089Spjd goto top; 2582219089Spjd } 2583240415Smm ASSERT0(tvd->vdev_stat.vs_alloc); 2584219089Spjd } 2585219089Spjd 2586219089Spjd /* 2587168404Spjd * Offline this device and reopen its top-level vdev. 2588213197Smm * If the top-level vdev is a log device then just offline 2589213197Smm * it. Otherwise, if this action results in the top-level 2590213197Smm * vdev becoming unusable, undo it and fail the request. 2591168404Spjd */ 2592168404Spjd vd->vdev_offline = B_TRUE; 2593213197Smm vdev_reopen(tvd); 2594213197Smm 2595213197Smm if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2596213197Smm vdev_is_dead(tvd)) { 2597168404Spjd vd->vdev_offline = B_FALSE; 2598213197Smm vdev_reopen(tvd); 2599185029Spjd return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2600168404Spjd } 2601219089Spjd 2602219089Spjd /* 2603219089Spjd * Add the device back into the metaslab rotor so that 2604219089Spjd * once we online the device it's open for business. 2605219089Spjd */ 2606219089Spjd if (tvd->vdev_islog && mg != NULL) 2607219089Spjd metaslab_group_activate(mg); 2608168404Spjd } 2609168404Spjd 2610185029Spjd vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 2611168404Spjd 2612219089Spjd return (spa_vdev_state_exit(spa, vd, 0)); 2613219089Spjd} 2614213197Smm 2615219089Spjdint 2616219089Spjdvdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 2617219089Spjd{ 2618219089Spjd int error; 2619213197Smm 2620219089Spjd mutex_enter(&spa->spa_vdev_top_lock); 2621219089Spjd error = vdev_offline_locked(spa, guid, flags); 2622219089Spjd mutex_exit(&spa->spa_vdev_top_lock); 2623219089Spjd 2624219089Spjd return (error); 2625168404Spjd} 2626168404Spjd 2627168404Spjd/* 2628168404Spjd * Clear the error counts associated with this vdev. Unlike vdev_online() and 2629168404Spjd * vdev_offline(), we assume the spa config is locked. We also clear all 2630168404Spjd * children. If 'vd' is NULL, then the user wants to clear all vdevs. 2631168404Spjd */ 2632168404Spjdvoid 2633168404Spjdvdev_clear(spa_t *spa, vdev_t *vd) 2634168404Spjd{ 2635185029Spjd vdev_t *rvd = spa->spa_root_vdev; 2636168404Spjd 2637185029Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2638185029Spjd 2639168404Spjd if (vd == NULL) 2640185029Spjd vd = rvd; 2641168404Spjd 2642168404Spjd vd->vdev_stat.vs_read_errors = 0; 2643168404Spjd vd->vdev_stat.vs_write_errors = 0; 2644168404Spjd vd->vdev_stat.vs_checksum_errors = 0; 2645168404Spjd 2646185029Spjd for (int c = 0; c < vd->vdev_children; c++) 2647168404Spjd vdev_clear(spa, vd->vdev_child[c]); 2648185029Spjd 2649253991Smav if (vd == rvd) { 2650253991Smav for (int c = 0; c < spa->spa_l2cache.sav_count; c++) 2651253991Smav vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]); 2652253991Smav 2653253991Smav for (int c = 0; c < spa->spa_spares.sav_count; c++) 2654253991Smav vdev_clear(spa, spa->spa_spares.sav_vdevs[c]); 2655253991Smav } 2656253991Smav 2657185029Spjd /* 2658185029Spjd * If we're in the FAULTED state or have experienced failed I/O, then 2659185029Spjd * clear the persistent state and attempt to reopen the device. We 2660185029Spjd * also mark the vdev config dirty, so that the new faulted state is 2661185029Spjd * written out to disk. 2662185029Spjd */ 2663185029Spjd if (vd->vdev_faulted || vd->vdev_degraded || 2664185029Spjd !vdev_readable(vd) || !vdev_writeable(vd)) { 2665185029Spjd 2666219089Spjd /* 2667219089Spjd * When reopening in reponse to a clear event, it may be due to 2668219089Spjd * a fmadm repair request. In this case, if the device is 2669219089Spjd * still broken, we want to still post the ereport again. 2670219089Spjd */ 2671219089Spjd vd->vdev_forcefault = B_TRUE; 2672219089Spjd 2673219089Spjd vd->vdev_faulted = vd->vdev_degraded = 0ULL; 2674185029Spjd vd->vdev_cant_read = B_FALSE; 2675185029Spjd vd->vdev_cant_write = B_FALSE; 2676185029Spjd 2677219089Spjd vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 2678185029Spjd 2679219089Spjd vd->vdev_forcefault = B_FALSE; 2680219089Spjd 2681219089Spjd if (vd != rvd && vdev_writeable(vd->vdev_top)) 2682185029Spjd vdev_state_dirty(vd->vdev_top); 2683185029Spjd 2684185029Spjd if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 2685185029Spjd spa_async_request(spa, SPA_ASYNC_RESILVER); 2686185029Spjd 2687185029Spjd spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 2688185029Spjd } 2689219089Spjd 2690219089Spjd /* 2691219089Spjd * When clearing a FMA-diagnosed fault, we always want to 2692219089Spjd * unspare the device, as we assume that the original spare was 2693219089Spjd * done in response to the FMA fault. 2694219089Spjd */ 2695219089Spjd if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 2696219089Spjd vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2697219089Spjd vd->vdev_parent->vdev_child[0] == vd) 2698219089Spjd vd->vdev_unspare = B_TRUE; 2699168404Spjd} 2700168404Spjd 2701185029Spjdboolean_t 2702168404Spjdvdev_is_dead(vdev_t *vd) 2703168404Spjd{ 2704219089Spjd /* 2705219089Spjd * Holes and missing devices are always considered "dead". 2706219089Spjd * This simplifies the code since we don't have to check for 2707219089Spjd * these types of devices in the various code paths. 2708219089Spjd * Instead we rely on the fact that we skip over dead devices 2709219089Spjd * before issuing I/O to them. 2710219089Spjd */ 2711219089Spjd return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole || 2712219089Spjd vd->vdev_ops == &vdev_missing_ops); 2713168404Spjd} 2714168404Spjd 2715185029Spjdboolean_t 2716185029Spjdvdev_readable(vdev_t *vd) 2717168404Spjd{ 2718185029Spjd return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2719185029Spjd} 2720168404Spjd 2721185029Spjdboolean_t 2722185029Spjdvdev_writeable(vdev_t *vd) 2723185029Spjd{ 2724185029Spjd return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 2725185029Spjd} 2726168404Spjd 2727185029Spjdboolean_t 2728208370Smmvdev_allocatable(vdev_t *vd) 2729208370Smm{ 2730209962Smm uint64_t state = vd->vdev_state; 2731209962Smm 2732208370Smm /* 2733209962Smm * We currently allow allocations from vdevs which may be in the 2734208370Smm * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 2735208370Smm * fails to reopen then we'll catch it later when we're holding 2736209962Smm * the proper locks. Note that we have to get the vdev state 2737209962Smm * in a local variable because although it changes atomically, 2738209962Smm * we're asking two separate questions about it. 2739208370Smm */ 2740209962Smm return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 2741219089Spjd !vd->vdev_cant_write && !vd->vdev_ishole); 2742208370Smm} 2743208370Smm 2744208370Smmboolean_t 2745185029Spjdvdev_accessible(vdev_t *vd, zio_t *zio) 2746185029Spjd{ 2747185029Spjd ASSERT(zio->io_vd == vd); 2748168404Spjd 2749185029Spjd if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 2750185029Spjd return (B_FALSE); 2751168404Spjd 2752185029Spjd if (zio->io_type == ZIO_TYPE_READ) 2753185029Spjd return (!vd->vdev_cant_read); 2754168404Spjd 2755185029Spjd if (zio->io_type == ZIO_TYPE_WRITE) 2756185029Spjd return (!vd->vdev_cant_write); 2757168404Spjd 2758185029Spjd return (B_TRUE); 2759168404Spjd} 2760168404Spjd 2761168404Spjd/* 2762168404Spjd * Get statistics for the given vdev. 2763168404Spjd */ 2764168404Spjdvoid 2765168404Spjdvdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2766168404Spjd{ 2767269773Sdelphij spa_t *spa = vd->vdev_spa; 2768269773Sdelphij vdev_t *rvd = spa->spa_root_vdev; 2769168404Spjd 2770269773Sdelphij ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2771269773Sdelphij 2772168404Spjd mutex_enter(&vd->vdev_stat_lock); 2773168404Spjd bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 2774168404Spjd vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2775168404Spjd vs->vs_state = vd->vdev_state; 2776219089Spjd vs->vs_rsize = vdev_get_min_asize(vd); 2777219089Spjd if (vd->vdev_ops->vdev_op_leaf) 2778219089Spjd vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 2779236155Smm vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize; 2780254591Sgibbs vs->vs_configured_ashift = vd->vdev_top != NULL 2781254591Sgibbs ? vd->vdev_top->vdev_ashift : vd->vdev_ashift; 2782254591Sgibbs vs->vs_logical_ashift = vd->vdev_logical_ashift; 2783254591Sgibbs vs->vs_physical_ashift = vd->vdev_physical_ashift; 2784270128Sdelphij if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) { 2785269773Sdelphij vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 2786270128Sdelphij } 2787168404Spjd 2788168404Spjd /* 2789168404Spjd * If we're getting stats on the root vdev, aggregate the I/O counts 2790168404Spjd * over all top-level vdevs (i.e. the direct children of the root). 2791168404Spjd */ 2792168404Spjd if (vd == rvd) { 2793185029Spjd for (int c = 0; c < rvd->vdev_children; c++) { 2794168404Spjd vdev_t *cvd = rvd->vdev_child[c]; 2795168404Spjd vdev_stat_t *cvs = &cvd->vdev_stat; 2796168404Spjd 2797185029Spjd for (int t = 0; t < ZIO_TYPES; t++) { 2798168404Spjd vs->vs_ops[t] += cvs->vs_ops[t]; 2799168404Spjd vs->vs_bytes[t] += cvs->vs_bytes[t]; 2800168404Spjd } 2801219089Spjd cvs->vs_scan_removing = cvd->vdev_removing; 2802168404Spjd } 2803168404Spjd } 2804269773Sdelphij mutex_exit(&vd->vdev_stat_lock); 2805168404Spjd} 2806168404Spjd 2807168404Spjdvoid 2808185029Spjdvdev_clear_stats(vdev_t *vd) 2809168404Spjd{ 2810185029Spjd mutex_enter(&vd->vdev_stat_lock); 2811185029Spjd vd->vdev_stat.vs_space = 0; 2812185029Spjd vd->vdev_stat.vs_dspace = 0; 2813185029Spjd vd->vdev_stat.vs_alloc = 0; 2814185029Spjd mutex_exit(&vd->vdev_stat_lock); 2815185029Spjd} 2816185029Spjd 2817185029Spjdvoid 2818219089Spjdvdev_scan_stat_init(vdev_t *vd) 2819219089Spjd{ 2820219089Spjd vdev_stat_t *vs = &vd->vdev_stat; 2821219089Spjd 2822219089Spjd for (int c = 0; c < vd->vdev_children; c++) 2823219089Spjd vdev_scan_stat_init(vd->vdev_child[c]); 2824219089Spjd 2825219089Spjd mutex_enter(&vd->vdev_stat_lock); 2826219089Spjd vs->vs_scan_processed = 0; 2827219089Spjd mutex_exit(&vd->vdev_stat_lock); 2828219089Spjd} 2829219089Spjd 2830219089Spjdvoid 2831185029Spjdvdev_stat_update(zio_t *zio, uint64_t psize) 2832185029Spjd{ 2833209962Smm spa_t *spa = zio->io_spa; 2834209962Smm vdev_t *rvd = spa->spa_root_vdev; 2835185029Spjd vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2836168404Spjd vdev_t *pvd; 2837168404Spjd uint64_t txg = zio->io_txg; 2838168404Spjd vdev_stat_t *vs = &vd->vdev_stat; 2839168404Spjd zio_type_t type = zio->io_type; 2840168404Spjd int flags = zio->io_flags; 2841168404Spjd 2842185029Spjd /* 2843185029Spjd * If this i/o is a gang leader, it didn't do any actual work. 2844185029Spjd */ 2845185029Spjd if (zio->io_gang_tree) 2846185029Spjd return; 2847185029Spjd 2848168404Spjd if (zio->io_error == 0) { 2849185029Spjd /* 2850185029Spjd * If this is a root i/o, don't count it -- we've already 2851185029Spjd * counted the top-level vdevs, and vdev_get_stats() will 2852185029Spjd * aggregate them when asked. This reduces contention on 2853185029Spjd * the root vdev_stat_lock and implicitly handles blocks 2854185029Spjd * that compress away to holes, for which there is no i/o. 2855185029Spjd * (Holes never create vdev children, so all the counters 2856185029Spjd * remain zero, which is what we want.) 2857185029Spjd * 2858185029Spjd * Note: this only applies to successful i/o (io_error == 0) 2859185029Spjd * because unlike i/o counts, errors are not additive. 2860185029Spjd * When reading a ditto block, for example, failure of 2861185029Spjd * one top-level vdev does not imply a root-level error. 2862185029Spjd */ 2863185029Spjd if (vd == rvd) 2864185029Spjd return; 2865185029Spjd 2866185029Spjd ASSERT(vd == zio->io_vd); 2867209962Smm 2868209962Smm if (flags & ZIO_FLAG_IO_BYPASS) 2869209962Smm return; 2870209962Smm 2871209962Smm mutex_enter(&vd->vdev_stat_lock); 2872209962Smm 2873185029Spjd if (flags & ZIO_FLAG_IO_REPAIR) { 2874219089Spjd if (flags & ZIO_FLAG_SCAN_THREAD) { 2875219089Spjd dsl_scan_phys_t *scn_phys = 2876219089Spjd &spa->spa_dsl_pool->dp_scan->scn_phys; 2877219089Spjd uint64_t *processed = &scn_phys->scn_processed; 2878219089Spjd 2879219089Spjd /* XXX cleanup? */ 2880219089Spjd if (vd->vdev_ops->vdev_op_leaf) 2881219089Spjd atomic_add_64(processed, psize); 2882219089Spjd vs->vs_scan_processed += psize; 2883219089Spjd } 2884219089Spjd 2885209962Smm if (flags & ZIO_FLAG_SELF_HEAL) 2886185029Spjd vs->vs_self_healed += psize; 2887168404Spjd } 2888209962Smm 2889209962Smm vs->vs_ops[type]++; 2890209962Smm vs->vs_bytes[type] += psize; 2891209962Smm 2892209962Smm mutex_exit(&vd->vdev_stat_lock); 2893168404Spjd return; 2894168404Spjd } 2895168404Spjd 2896168404Spjd if (flags & ZIO_FLAG_SPECULATIVE) 2897168404Spjd return; 2898168404Spjd 2899213198Smm /* 2900213198Smm * If this is an I/O error that is going to be retried, then ignore the 2901213198Smm * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 2902213198Smm * hard errors, when in reality they can happen for any number of 2903213198Smm * innocuous reasons (bus resets, MPxIO link failure, etc). 2904213198Smm */ 2905213198Smm if (zio->io_error == EIO && 2906213198Smm !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 2907213198Smm return; 2908213198Smm 2909219089Spjd /* 2910219089Spjd * Intent logs writes won't propagate their error to the root 2911219089Spjd * I/O so don't mark these types of failures as pool-level 2912219089Spjd * errors. 2913219089Spjd */ 2914219089Spjd if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 2915219089Spjd return; 2916219089Spjd 2917185029Spjd mutex_enter(&vd->vdev_stat_lock); 2918209962Smm if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 2919185029Spjd if (zio->io_error == ECKSUM) 2920185029Spjd vs->vs_checksum_errors++; 2921185029Spjd else 2922185029Spjd vs->vs_read_errors++; 2923168404Spjd } 2924209962Smm if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 2925185029Spjd vs->vs_write_errors++; 2926185029Spjd mutex_exit(&vd->vdev_stat_lock); 2927168404Spjd 2928209962Smm if (type == ZIO_TYPE_WRITE && txg != 0 && 2929209962Smm (!(flags & ZIO_FLAG_IO_REPAIR) || 2930219089Spjd (flags & ZIO_FLAG_SCAN_THREAD) || 2931219089Spjd spa->spa_claiming)) { 2932209962Smm /* 2933219089Spjd * This is either a normal write (not a repair), or it's 2934219089Spjd * a repair induced by the scrub thread, or it's a repair 2935219089Spjd * made by zil_claim() during spa_load() in the first txg. 2936219089Spjd * In the normal case, we commit the DTL change in the same 2937219089Spjd * txg as the block was born. In the scrub-induced repair 2938219089Spjd * case, we know that scrubs run in first-pass syncing context, 2939219089Spjd * so we commit the DTL change in spa_syncing_txg(spa). 2940219089Spjd * In the zil_claim() case, we commit in spa_first_txg(spa). 2941209962Smm * 2942209962Smm * We currently do not make DTL entries for failed spontaneous 2943209962Smm * self-healing writes triggered by normal (non-scrubbing) 2944209962Smm * reads, because we have no transactional context in which to 2945209962Smm * do so -- and it's not clear that it'd be desirable anyway. 2946209962Smm */ 2947209962Smm if (vd->vdev_ops->vdev_op_leaf) { 2948209962Smm uint64_t commit_txg = txg; 2949219089Spjd if (flags & ZIO_FLAG_SCAN_THREAD) { 2950209962Smm ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2951209962Smm ASSERT(spa_sync_pass(spa) == 1); 2952209962Smm vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 2953219089Spjd commit_txg = spa_syncing_txg(spa); 2954219089Spjd } else if (spa->spa_claiming) { 2955219089Spjd ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2956219089Spjd commit_txg = spa_first_txg(spa); 2957209962Smm } 2958219089Spjd ASSERT(commit_txg >= spa_syncing_txg(spa)); 2959209962Smm if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 2960168404Spjd return; 2961209962Smm for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2962209962Smm vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 2963209962Smm vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2964168404Spjd } 2965209962Smm if (vd != rvd) 2966209962Smm vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2967168404Spjd } 2968168404Spjd} 2969168404Spjd 2970168404Spjd/* 2971219089Spjd * Update the in-core space usage stats for this vdev, its metaslab class, 2972219089Spjd * and the root vdev. 2973168404Spjd */ 2974168404Spjdvoid 2975219089Spjdvdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 2976219089Spjd int64_t space_delta) 2977168404Spjd{ 2978168404Spjd int64_t dspace_delta = space_delta; 2979185029Spjd spa_t *spa = vd->vdev_spa; 2980185029Spjd vdev_t *rvd = spa->spa_root_vdev; 2981219089Spjd metaslab_group_t *mg = vd->vdev_mg; 2982219089Spjd metaslab_class_t *mc = mg ? mg->mg_class : NULL; 2983168404Spjd 2984185029Spjd ASSERT(vd == vd->vdev_top); 2985168404Spjd 2986185029Spjd /* 2987185029Spjd * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 2988185029Spjd * factor. We must calculate this here and not at the root vdev 2989185029Spjd * because the root vdev's psize-to-asize is simply the max of its 2990185029Spjd * childrens', thus not accurate enough for us. 2991185029Spjd */ 2992185029Spjd ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 2993213197Smm ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 2994185029Spjd dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 2995185029Spjd vd->vdev_deflate_ratio; 2996185029Spjd 2997185029Spjd mutex_enter(&vd->vdev_stat_lock); 2998219089Spjd vd->vdev_stat.vs_alloc += alloc_delta; 2999185029Spjd vd->vdev_stat.vs_space += space_delta; 3000185029Spjd vd->vdev_stat.vs_dspace += dspace_delta; 3001185029Spjd mutex_exit(&vd->vdev_stat_lock); 3002185029Spjd 3003219089Spjd if (mc == spa_normal_class(spa)) { 3004185029Spjd mutex_enter(&rvd->vdev_stat_lock); 3005219089Spjd rvd->vdev_stat.vs_alloc += alloc_delta; 3006185029Spjd rvd->vdev_stat.vs_space += space_delta; 3007185029Spjd rvd->vdev_stat.vs_dspace += dspace_delta; 3008185029Spjd mutex_exit(&rvd->vdev_stat_lock); 3009185029Spjd } 3010219089Spjd 3011219089Spjd if (mc != NULL) { 3012219089Spjd ASSERT(rvd == vd->vdev_parent); 3013219089Spjd ASSERT(vd->vdev_ms_count != 0); 3014219089Spjd 3015219089Spjd metaslab_class_space_update(mc, 3016219089Spjd alloc_delta, defer_delta, space_delta, dspace_delta); 3017219089Spjd } 3018168404Spjd} 3019168404Spjd 3020168404Spjd/* 3021168404Spjd * Mark a top-level vdev's config as dirty, placing it on the dirty list 3022168404Spjd * so that it will be written out next time the vdev configuration is synced. 3023168404Spjd * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3024168404Spjd */ 3025168404Spjdvoid 3026168404Spjdvdev_config_dirty(vdev_t *vd) 3027168404Spjd{ 3028168404Spjd spa_t *spa = vd->vdev_spa; 3029168404Spjd vdev_t *rvd = spa->spa_root_vdev; 3030168404Spjd int c; 3031168404Spjd 3032219089Spjd ASSERT(spa_writeable(spa)); 3033219089Spjd 3034168404Spjd /* 3035209962Smm * If this is an aux vdev (as with l2cache and spare devices), then we 3036209962Smm * update the vdev config manually and set the sync flag. 3037185029Spjd */ 3038185029Spjd if (vd->vdev_aux != NULL) { 3039185029Spjd spa_aux_vdev_t *sav = vd->vdev_aux; 3040185029Spjd nvlist_t **aux; 3041185029Spjd uint_t naux; 3042185029Spjd 3043185029Spjd for (c = 0; c < sav->sav_count; c++) { 3044185029Spjd if (sav->sav_vdevs[c] == vd) 3045185029Spjd break; 3046185029Spjd } 3047185029Spjd 3048185029Spjd if (c == sav->sav_count) { 3049185029Spjd /* 3050185029Spjd * We're being removed. There's nothing more to do. 3051185029Spjd */ 3052185029Spjd ASSERT(sav->sav_sync == B_TRUE); 3053185029Spjd return; 3054185029Spjd } 3055185029Spjd 3056185029Spjd sav->sav_sync = B_TRUE; 3057185029Spjd 3058209962Smm if (nvlist_lookup_nvlist_array(sav->sav_config, 3059209962Smm ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 3060209962Smm VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 3061209962Smm ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 3062209962Smm } 3063185029Spjd 3064185029Spjd ASSERT(c < naux); 3065185029Spjd 3066185029Spjd /* 3067185029Spjd * Setting the nvlist in the middle if the array is a little 3068185029Spjd * sketchy, but it will work. 3069185029Spjd */ 3070185029Spjd nvlist_free(aux[c]); 3071219089Spjd aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3072185029Spjd 3073185029Spjd return; 3074185029Spjd } 3075185029Spjd 3076185029Spjd /* 3077185029Spjd * The dirty list is protected by the SCL_CONFIG lock. The caller 3078185029Spjd * must either hold SCL_CONFIG as writer, or must be the sync thread 3079185029Spjd * (which holds SCL_CONFIG as reader). There's only one sync thread, 3080168404Spjd * so this is sufficient to ensure mutual exclusion. 3081168404Spjd */ 3082185029Spjd ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3083185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3084185029Spjd spa_config_held(spa, SCL_CONFIG, RW_READER))); 3085168404Spjd 3086168404Spjd if (vd == rvd) { 3087168404Spjd for (c = 0; c < rvd->vdev_children; c++) 3088168404Spjd vdev_config_dirty(rvd->vdev_child[c]); 3089168404Spjd } else { 3090168404Spjd ASSERT(vd == vd->vdev_top); 3091168404Spjd 3092219089Spjd if (!list_link_active(&vd->vdev_config_dirty_node) && 3093219089Spjd !vd->vdev_ishole) 3094185029Spjd list_insert_head(&spa->spa_config_dirty_list, vd); 3095168404Spjd } 3096168404Spjd} 3097168404Spjd 3098168404Spjdvoid 3099168404Spjdvdev_config_clean(vdev_t *vd) 3100168404Spjd{ 3101168404Spjd spa_t *spa = vd->vdev_spa; 3102168404Spjd 3103185029Spjd ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3104185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3105185029Spjd spa_config_held(spa, SCL_CONFIG, RW_READER))); 3106168404Spjd 3107185029Spjd ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3108185029Spjd list_remove(&spa->spa_config_dirty_list, vd); 3109168404Spjd} 3110168404Spjd 3111185029Spjd/* 3112185029Spjd * Mark a top-level vdev's state as dirty, so that the next pass of 3113185029Spjd * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3114185029Spjd * the state changes from larger config changes because they require 3115185029Spjd * much less locking, and are often needed for administrative actions. 3116185029Spjd */ 3117168404Spjdvoid 3118185029Spjdvdev_state_dirty(vdev_t *vd) 3119185029Spjd{ 3120185029Spjd spa_t *spa = vd->vdev_spa; 3121185029Spjd 3122219089Spjd ASSERT(spa_writeable(spa)); 3123185029Spjd ASSERT(vd == vd->vdev_top); 3124185029Spjd 3125185029Spjd /* 3126185029Spjd * The state list is protected by the SCL_STATE lock. The caller 3127185029Spjd * must either hold SCL_STATE as writer, or must be the sync thread 3128185029Spjd * (which holds SCL_STATE as reader). There's only one sync thread, 3129185029Spjd * so this is sufficient to ensure mutual exclusion. 3130185029Spjd */ 3131185029Spjd ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3132185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3133185029Spjd spa_config_held(spa, SCL_STATE, RW_READER))); 3134185029Spjd 3135219089Spjd if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole) 3136185029Spjd list_insert_head(&spa->spa_state_dirty_list, vd); 3137185029Spjd} 3138185029Spjd 3139185029Spjdvoid 3140185029Spjdvdev_state_clean(vdev_t *vd) 3141185029Spjd{ 3142185029Spjd spa_t *spa = vd->vdev_spa; 3143185029Spjd 3144185029Spjd ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3145185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3146185029Spjd spa_config_held(spa, SCL_STATE, RW_READER))); 3147185029Spjd 3148185029Spjd ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3149185029Spjd list_remove(&spa->spa_state_dirty_list, vd); 3150185029Spjd} 3151185029Spjd 3152185029Spjd/* 3153185029Spjd * Propagate vdev state up from children to parent. 3154185029Spjd */ 3155185029Spjdvoid 3156168404Spjdvdev_propagate_state(vdev_t *vd) 3157168404Spjd{ 3158209962Smm spa_t *spa = vd->vdev_spa; 3159209962Smm vdev_t *rvd = spa->spa_root_vdev; 3160168404Spjd int degraded = 0, faulted = 0; 3161168404Spjd int corrupted = 0; 3162168404Spjd vdev_t *child; 3163168404Spjd 3164185029Spjd if (vd->vdev_children > 0) { 3165219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 3166185029Spjd child = vd->vdev_child[c]; 3167168404Spjd 3168219089Spjd /* 3169219089Spjd * Don't factor holes into the decision. 3170219089Spjd */ 3171219089Spjd if (child->vdev_ishole) 3172219089Spjd continue; 3173219089Spjd 3174185029Spjd if (!vdev_readable(child) || 3175209962Smm (!vdev_writeable(child) && spa_writeable(spa))) { 3176185029Spjd /* 3177185029Spjd * Root special: if there is a top-level log 3178185029Spjd * device, treat the root vdev as if it were 3179185029Spjd * degraded. 3180185029Spjd */ 3181185029Spjd if (child->vdev_islog && vd == rvd) 3182185029Spjd degraded++; 3183185029Spjd else 3184185029Spjd faulted++; 3185185029Spjd } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 3186185029Spjd degraded++; 3187185029Spjd } 3188185029Spjd 3189185029Spjd if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 3190185029Spjd corrupted++; 3191185029Spjd } 3192185029Spjd 3193185029Spjd vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 3194185029Spjd 3195185029Spjd /* 3196185029Spjd * Root special: if there is a top-level vdev that cannot be 3197185029Spjd * opened due to corrupted metadata, then propagate the root 3198185029Spjd * vdev's aux state as 'corrupt' rather than 'insufficient 3199185029Spjd * replicas'. 3200185029Spjd */ 3201185029Spjd if (corrupted && vd == rvd && 3202185029Spjd rvd->vdev_state == VDEV_STATE_CANT_OPEN) 3203185029Spjd vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 3204185029Spjd VDEV_AUX_CORRUPT_DATA); 3205168404Spjd } 3206168404Spjd 3207185029Spjd if (vd->vdev_parent) 3208185029Spjd vdev_propagate_state(vd->vdev_parent); 3209168404Spjd} 3210168404Spjd 3211168404Spjd/* 3212168404Spjd * Set a vdev's state. If this is during an open, we don't update the parent 3213168404Spjd * state, because we're in the process of opening children depth-first. 3214168404Spjd * Otherwise, we propagate the change to the parent. 3215168404Spjd * 3216168404Spjd * If this routine places a device in a faulted state, an appropriate ereport is 3217168404Spjd * generated. 3218168404Spjd */ 3219168404Spjdvoid 3220168404Spjdvdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3221168404Spjd{ 3222168404Spjd uint64_t save_state; 3223185029Spjd spa_t *spa = vd->vdev_spa; 3224168404Spjd 3225168404Spjd if (state == vd->vdev_state) { 3226168404Spjd vd->vdev_stat.vs_aux = aux; 3227168404Spjd return; 3228168404Spjd } 3229168404Spjd 3230168404Spjd save_state = vd->vdev_state; 3231168404Spjd 3232168404Spjd vd->vdev_state = state; 3233168404Spjd vd->vdev_stat.vs_aux = aux; 3234168404Spjd 3235173373Spjd /* 3236173373Spjd * If we are setting the vdev state to anything but an open state, then 3237219089Spjd * always close the underlying device unless the device has requested 3238219089Spjd * a delayed close (i.e. we're about to remove or fault the device). 3239219089Spjd * Otherwise, we keep accessible but invalid devices open forever. 3240219089Spjd * We don't call vdev_close() itself, because that implies some extra 3241219089Spjd * checks (offline, etc) that we don't want here. This is limited to 3242219089Spjd * leaf devices, because otherwise closing the device will affect other 3243219089Spjd * children. 3244173373Spjd */ 3245219089Spjd if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 3246219089Spjd vd->vdev_ops->vdev_op_leaf) 3247173373Spjd vd->vdev_ops->vdev_op_close(vd); 3248173373Spjd 3249219089Spjd /* 3250219089Spjd * If we have brought this vdev back into service, we need 3251219089Spjd * to notify fmd so that it can gracefully repair any outstanding 3252219089Spjd * cases due to a missing device. We do this in all cases, even those 3253219089Spjd * that probably don't correlate to a repaired fault. This is sure to 3254219089Spjd * catch all cases, and we let the zfs-retire agent sort it out. If 3255219089Spjd * this is a transient state it's OK, as the retire agent will 3256219089Spjd * double-check the state of the vdev before repairing it. 3257219089Spjd */ 3258219089Spjd if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3259219089Spjd vd->vdev_prevstate != state) 3260219089Spjd zfs_post_state_change(spa, vd); 3261219089Spjd 3262185029Spjd if (vd->vdev_removed && 3263185029Spjd state == VDEV_STATE_CANT_OPEN && 3264185029Spjd (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 3265168404Spjd /* 3266185029Spjd * If the previous state is set to VDEV_STATE_REMOVED, then this 3267185029Spjd * device was previously marked removed and someone attempted to 3268185029Spjd * reopen it. If this failed due to a nonexistent device, then 3269185029Spjd * keep the device in the REMOVED state. We also let this be if 3270185029Spjd * it is one of our special test online cases, which is only 3271185029Spjd * attempting to online the device and shouldn't generate an FMA 3272185029Spjd * fault. 3273185029Spjd */ 3274185029Spjd vd->vdev_state = VDEV_STATE_REMOVED; 3275185029Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 3276185029Spjd } else if (state == VDEV_STATE_REMOVED) { 3277185029Spjd vd->vdev_removed = B_TRUE; 3278185029Spjd } else if (state == VDEV_STATE_CANT_OPEN) { 3279185029Spjd /* 3280219089Spjd * If we fail to open a vdev during an import or recovery, we 3281219089Spjd * mark it as "not available", which signifies that it was 3282219089Spjd * never there to begin with. Failure to open such a device 3283219089Spjd * is not considered an error. 3284168404Spjd */ 3285219089Spjd if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3286219089Spjd spa_load_state(spa) == SPA_LOAD_RECOVER) && 3287168404Spjd vd->vdev_ops->vdev_op_leaf) 3288168404Spjd vd->vdev_not_present = 1; 3289168404Spjd 3290168404Spjd /* 3291168404Spjd * Post the appropriate ereport. If the 'prevstate' field is 3292168404Spjd * set to something other than VDEV_STATE_UNKNOWN, it indicates 3293168404Spjd * that this is part of a vdev_reopen(). In this case, we don't 3294168404Spjd * want to post the ereport if the device was already in the 3295168404Spjd * CANT_OPEN state beforehand. 3296185029Spjd * 3297185029Spjd * If the 'checkremove' flag is set, then this is an attempt to 3298185029Spjd * online the device in response to an insertion event. If we 3299185029Spjd * hit this case, then we have detected an insertion event for a 3300185029Spjd * faulted or offline device that wasn't in the removed state. 3301185029Spjd * In this scenario, we don't post an ereport because we are 3302185029Spjd * about to replace the device, or attempt an online with 3303185029Spjd * vdev_forcefault, which will generate the fault for us. 3304168404Spjd */ 3305185029Spjd if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 3306185029Spjd !vd->vdev_not_present && !vd->vdev_checkremove && 3307185029Spjd vd != spa->spa_root_vdev) { 3308168404Spjd const char *class; 3309168404Spjd 3310168404Spjd switch (aux) { 3311168404Spjd case VDEV_AUX_OPEN_FAILED: 3312168404Spjd class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3313168404Spjd break; 3314168404Spjd case VDEV_AUX_CORRUPT_DATA: 3315168404Spjd class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3316168404Spjd break; 3317168404Spjd case VDEV_AUX_NO_REPLICAS: 3318168404Spjd class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3319168404Spjd break; 3320168404Spjd case VDEV_AUX_BAD_GUID_SUM: 3321168404Spjd class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3322168404Spjd break; 3323168404Spjd case VDEV_AUX_TOO_SMALL: 3324168404Spjd class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3325168404Spjd break; 3326168404Spjd case VDEV_AUX_BAD_LABEL: 3327168404Spjd class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3328168404Spjd break; 3329168404Spjd default: 3330168404Spjd class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3331168404Spjd } 3332168404Spjd 3333185029Spjd zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3334168404Spjd } 3335185029Spjd 3336185029Spjd /* Erase any notion of persistent removed state */ 3337185029Spjd vd->vdev_removed = B_FALSE; 3338185029Spjd } else { 3339185029Spjd vd->vdev_removed = B_FALSE; 3340168404Spjd } 3341168404Spjd 3342209962Smm if (!isopen && vd->vdev_parent) 3343209962Smm vdev_propagate_state(vd->vdev_parent); 3344185029Spjd} 3345168404Spjd 3346185029Spjd/* 3347185029Spjd * Check the vdev configuration to ensure that it's capable of supporting 3348193163Sdfr * a root pool. 3349193163Sdfr * 3350193163Sdfr * On Solaris, we do not support RAID-Z or partial configuration. In 3351193163Sdfr * addition, only a single top-level vdev is allowed and none of the 3352193163Sdfr * leaves can be wholedisks. 3353193163Sdfr * 3354193163Sdfr * For FreeBSD, we can boot from any configuration. There is a 3355193163Sdfr * limitation that the boot filesystem must be either uncompressed or 3356193163Sdfr * compresses with lzjb compression but I'm not sure how to enforce 3357193163Sdfr * that here. 3358185029Spjd */ 3359185029Spjdboolean_t 3360185029Spjdvdev_is_bootable(vdev_t *vd) 3361185029Spjd{ 3362213197Smm#ifdef sun 3363185029Spjd if (!vd->vdev_ops->vdev_op_leaf) { 3364185029Spjd char *vdev_type = vd->vdev_ops->vdev_op_type; 3365185029Spjd 3366185029Spjd if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 3367185029Spjd vd->vdev_children > 1) { 3368185029Spjd return (B_FALSE); 3369185029Spjd } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 3370185029Spjd strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 3371185029Spjd return (B_FALSE); 3372185029Spjd } 3373185029Spjd } else if (vd->vdev_wholedisk == 1) { 3374185029Spjd return (B_FALSE); 3375185029Spjd } 3376185029Spjd 3377219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 3378185029Spjd if (!vdev_is_bootable(vd->vdev_child[c])) 3379185029Spjd return (B_FALSE); 3380185029Spjd } 3381213197Smm#endif /* sun */ 3382185029Spjd return (B_TRUE); 3383168404Spjd} 3384213197Smm 3385219089Spjd/* 3386219089Spjd * Load the state from the original vdev tree (ovd) which 3387219089Spjd * we've retrieved from the MOS config object. If the original 3388219089Spjd * vdev was offline or faulted then we transfer that state to the 3389219089Spjd * device in the current vdev tree (nvd). 3390219089Spjd */ 3391213197Smmvoid 3392219089Spjdvdev_load_log_state(vdev_t *nvd, vdev_t *ovd) 3393213197Smm{ 3394219089Spjd spa_t *spa = nvd->vdev_spa; 3395213197Smm 3396219089Spjd ASSERT(nvd->vdev_top->vdev_islog); 3397219089Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3398219089Spjd ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid); 3399213197Smm 3400219089Spjd for (int c = 0; c < nvd->vdev_children; c++) 3401219089Spjd vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]); 3402213197Smm 3403219089Spjd if (nvd->vdev_ops->vdev_op_leaf) { 3404213197Smm /* 3405219089Spjd * Restore the persistent vdev state 3406213197Smm */ 3407219089Spjd nvd->vdev_offline = ovd->vdev_offline; 3408219089Spjd nvd->vdev_faulted = ovd->vdev_faulted; 3409219089Spjd nvd->vdev_degraded = ovd->vdev_degraded; 3410219089Spjd nvd->vdev_removed = ovd->vdev_removed; 3411213197Smm } 3412213197Smm} 3413219089Spjd 3414219089Spjd/* 3415219089Spjd * Determine if a log device has valid content. If the vdev was 3416219089Spjd * removed or faulted in the MOS config then we know that 3417219089Spjd * the content on the log device has already been written to the pool. 3418219089Spjd */ 3419219089Spjdboolean_t 3420219089Spjdvdev_log_state_valid(vdev_t *vd) 3421219089Spjd{ 3422219089Spjd if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 3423219089Spjd !vd->vdev_removed) 3424219089Spjd return (B_TRUE); 3425219089Spjd 3426219089Spjd for (int c = 0; c < vd->vdev_children; c++) 3427219089Spjd if (vdev_log_state_valid(vd->vdev_child[c])) 3428219089Spjd return (B_TRUE); 3429219089Spjd 3430219089Spjd return (B_FALSE); 3431219089Spjd} 3432219089Spjd 3433219089Spjd/* 3434219089Spjd * Expand a vdev if possible. 3435219089Spjd */ 3436219089Spjdvoid 3437219089Spjdvdev_expand(vdev_t *vd, uint64_t txg) 3438219089Spjd{ 3439219089Spjd ASSERT(vd->vdev_top == vd); 3440219089Spjd ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3441219089Spjd 3442219089Spjd if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 3443219089Spjd VERIFY(vdev_metaslab_init(vd, txg) == 0); 3444219089Spjd vdev_config_dirty(vd); 3445219089Spjd } 3446219089Spjd} 3447219089Spjd 3448219089Spjd/* 3449219089Spjd * Split a vdev. 3450219089Spjd */ 3451219089Spjdvoid 3452219089Spjdvdev_split(vdev_t *vd) 3453219089Spjd{ 3454219089Spjd vdev_t *cvd, *pvd = vd->vdev_parent; 3455219089Spjd 3456219089Spjd vdev_remove_child(pvd, vd); 3457219089Spjd vdev_compact_children(pvd); 3458219089Spjd 3459219089Spjd cvd = pvd->vdev_child[0]; 3460219089Spjd if (pvd->vdev_children == 1) { 3461219089Spjd vdev_remove_parent(cvd); 3462219089Spjd cvd->vdev_splitting = B_TRUE; 3463219089Spjd } 3464219089Spjd vdev_propagate_state(cvd); 3465219089Spjd} 3466247265Smm 3467247265Smmvoid 3468247265Smmvdev_deadman(vdev_t *vd) 3469247265Smm{ 3470247265Smm for (int c = 0; c < vd->vdev_children; c++) { 3471247265Smm vdev_t *cvd = vd->vdev_child[c]; 3472247265Smm 3473247265Smm vdev_deadman(cvd); 3474247265Smm } 3475247265Smm 3476247265Smm if (vd->vdev_ops->vdev_op_leaf) { 3477247265Smm vdev_queue_t *vq = &vd->vdev_queue; 3478247265Smm 3479247265Smm mutex_enter(&vq->vq_lock); 3480260763Savg if (avl_numnodes(&vq->vq_active_tree) > 0) { 3481247265Smm spa_t *spa = vd->vdev_spa; 3482247265Smm zio_t *fio; 3483247265Smm uint64_t delta; 3484247265Smm 3485247265Smm /* 3486247265Smm * Look at the head of all the pending queues, 3487247265Smm * if any I/O has been outstanding for longer than 3488247265Smm * the spa_deadman_synctime we panic the system. 3489247265Smm */ 3490260763Savg fio = avl_first(&vq->vq_active_tree); 3491249206Smm delta = gethrtime() - fio->io_timestamp; 3492249206Smm if (delta > spa_deadman_synctime(spa)) { 3493249206Smm zfs_dbgmsg("SLOW IO: zio timestamp %lluns, " 3494249206Smm "delta %lluns, last io %lluns", 3495247265Smm fio->io_timestamp, delta, 3496247265Smm vq->vq_io_complete_ts); 3497247265Smm fm_panic("I/O to pool '%s' appears to be " 3498247348Smm "hung on vdev guid %llu at '%s'.", 3499247348Smm spa_name(spa), 3500247348Smm (long long unsigned int) vd->vdev_guid, 3501247348Smm vd->vdev_path); 3502247265Smm } 3503247265Smm } 3504247265Smm mutex_exit(&vq->vq_lock); 3505247265Smm } 3506247265Smm} 3507