1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 238632SBill.Moore@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #include <sys/zfs_context.h> 281544Seschrock #include <sys/fm/fs/zfs.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/spa_impl.h> 31789Sahrens #include <sys/dmu.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/vdev_impl.h> 34789Sahrens #include <sys/uberblock_impl.h> 35789Sahrens #include <sys/metaslab.h> 36789Sahrens #include <sys/metaslab_impl.h> 37789Sahrens #include <sys/space_map.h> 38789Sahrens #include <sys/zio.h> 39789Sahrens #include <sys/zap.h> 40789Sahrens #include <sys/fs/zfs.h> 416643Seschrock #include <sys/arc.h> 42789Sahrens 43789Sahrens /* 44789Sahrens * Virtual device management. 45789Sahrens */ 46789Sahrens 47789Sahrens static vdev_ops_t *vdev_ops_table[] = { 48789Sahrens &vdev_root_ops, 49789Sahrens &vdev_raidz_ops, 50789Sahrens &vdev_mirror_ops, 51789Sahrens &vdev_replacing_ops, 522082Seschrock &vdev_spare_ops, 53789Sahrens &vdev_disk_ops, 54789Sahrens &vdev_file_ops, 55789Sahrens &vdev_missing_ops, 56789Sahrens NULL 57789Sahrens }; 58789Sahrens 597046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */ 607046Sahrens int zfs_scrub_limit = 10; 613697Smishra 62789Sahrens /* 63789Sahrens * Given a vdev type, return the appropriate ops vector. 64789Sahrens */ 65789Sahrens static vdev_ops_t * 66789Sahrens vdev_getops(const char *type) 67789Sahrens { 68789Sahrens vdev_ops_t *ops, **opspp; 69789Sahrens 70789Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 71789Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 72789Sahrens break; 73789Sahrens 74789Sahrens return (ops); 75789Sahrens } 76789Sahrens 77789Sahrens /* 78789Sahrens * Default asize function: return the MAX of psize with the asize of 79789Sahrens * all children. This is what's used by anything other than RAID-Z. 80789Sahrens */ 81789Sahrens uint64_t 82789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 83789Sahrens { 841732Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 85789Sahrens uint64_t csize; 86789Sahrens uint64_t c; 87789Sahrens 88789Sahrens for (c = 0; c < vd->vdev_children; c++) { 89789Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 90789Sahrens asize = MAX(asize, csize); 91789Sahrens } 92789Sahrens 93789Sahrens return (asize); 94789Sahrens } 95789Sahrens 961175Slling /* 971175Slling * Get the replaceable or attachable device size. 981175Slling * If the parent is a mirror or raidz, the replaceable size is the minimum 991175Slling * psize of all its children. For the rest, just return our own psize. 1001175Slling * 1011175Slling * e.g. 1021175Slling * psize rsize 1031175Slling * root - - 1041175Slling * mirror/raidz - - 1051175Slling * disk1 20g 20g 1061175Slling * disk2 40g 20g 1071175Slling * disk3 80g 80g 1081175Slling */ 1091175Slling uint64_t 1101175Slling vdev_get_rsize(vdev_t *vd) 1111175Slling { 1121175Slling vdev_t *pvd, *cvd; 1131175Slling uint64_t c, rsize; 1141175Slling 1151175Slling pvd = vd->vdev_parent; 1161175Slling 1171175Slling /* 1181175Slling * If our parent is NULL or the root, just return our own psize. 1191175Slling */ 1201175Slling if (pvd == NULL || pvd->vdev_parent == NULL) 1211175Slling return (vd->vdev_psize); 1221175Slling 1231175Slling rsize = 0; 1241175Slling 1251175Slling for (c = 0; c < pvd->vdev_children; c++) { 1261175Slling cvd = pvd->vdev_child[c]; 1271175Slling rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1; 1281175Slling } 1291175Slling 1301175Slling return (rsize); 1311175Slling } 1321175Slling 133789Sahrens vdev_t * 134789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 135789Sahrens { 136789Sahrens vdev_t *rvd = spa->spa_root_vdev; 137789Sahrens 1387754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1395530Sbonwick 1407046Sahrens if (vdev < rvd->vdev_children) { 1417046Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 142789Sahrens return (rvd->vdev_child[vdev]); 1437046Sahrens } 144789Sahrens 145789Sahrens return (NULL); 146789Sahrens } 147789Sahrens 148789Sahrens vdev_t * 149789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 150789Sahrens { 151789Sahrens int c; 152789Sahrens vdev_t *mvd; 153789Sahrens 1541585Sbonwick if (vd->vdev_guid == guid) 155789Sahrens return (vd); 156789Sahrens 157789Sahrens for (c = 0; c < vd->vdev_children; c++) 158789Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 159789Sahrens NULL) 160789Sahrens return (mvd); 161789Sahrens 162789Sahrens return (NULL); 163789Sahrens } 164789Sahrens 165789Sahrens void 166789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 167789Sahrens { 168789Sahrens size_t oldsize, newsize; 169789Sahrens uint64_t id = cvd->vdev_id; 170789Sahrens vdev_t **newchild; 171789Sahrens 1727754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 173789Sahrens ASSERT(cvd->vdev_parent == NULL); 174789Sahrens 175789Sahrens cvd->vdev_parent = pvd; 176789Sahrens 177789Sahrens if (pvd == NULL) 178789Sahrens return; 179789Sahrens 180789Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 181789Sahrens 182789Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 183789Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 184789Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 185789Sahrens 186789Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 187789Sahrens if (pvd->vdev_child != NULL) { 188789Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 189789Sahrens kmem_free(pvd->vdev_child, oldsize); 190789Sahrens } 191789Sahrens 192789Sahrens pvd->vdev_child = newchild; 193789Sahrens pvd->vdev_child[id] = cvd; 194789Sahrens 195789Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 196789Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 197789Sahrens 198789Sahrens /* 199789Sahrens * Walk up all ancestors to update guid sum. 200789Sahrens */ 201789Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 202789Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 2033697Smishra 2043697Smishra if (cvd->vdev_ops->vdev_op_leaf) 2053697Smishra cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit; 206789Sahrens } 207789Sahrens 208789Sahrens void 209789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 210789Sahrens { 211789Sahrens int c; 212789Sahrens uint_t id = cvd->vdev_id; 213789Sahrens 214789Sahrens ASSERT(cvd->vdev_parent == pvd); 215789Sahrens 216789Sahrens if (pvd == NULL) 217789Sahrens return; 218789Sahrens 219789Sahrens ASSERT(id < pvd->vdev_children); 220789Sahrens ASSERT(pvd->vdev_child[id] == cvd); 221789Sahrens 222789Sahrens pvd->vdev_child[id] = NULL; 223789Sahrens cvd->vdev_parent = NULL; 224789Sahrens 225789Sahrens for (c = 0; c < pvd->vdev_children; c++) 226789Sahrens if (pvd->vdev_child[c]) 227789Sahrens break; 228789Sahrens 229789Sahrens if (c == pvd->vdev_children) { 230789Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 231789Sahrens pvd->vdev_child = NULL; 232789Sahrens pvd->vdev_children = 0; 233789Sahrens } 234789Sahrens 235789Sahrens /* 236789Sahrens * Walk up all ancestors to update guid sum. 237789Sahrens */ 238789Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 239789Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 2403697Smishra 2413697Smishra if (cvd->vdev_ops->vdev_op_leaf) 2423697Smishra cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit; 243789Sahrens } 244789Sahrens 245789Sahrens /* 246789Sahrens * Remove any holes in the child array. 247789Sahrens */ 248789Sahrens void 249789Sahrens vdev_compact_children(vdev_t *pvd) 250789Sahrens { 251789Sahrens vdev_t **newchild, *cvd; 252789Sahrens int oldc = pvd->vdev_children; 253789Sahrens int newc, c; 254789Sahrens 2557754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 256789Sahrens 257789Sahrens for (c = newc = 0; c < oldc; c++) 258789Sahrens if (pvd->vdev_child[c]) 259789Sahrens newc++; 260789Sahrens 261789Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 262789Sahrens 263789Sahrens for (c = newc = 0; c < oldc; c++) { 264789Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 265789Sahrens newchild[newc] = cvd; 266789Sahrens cvd->vdev_id = newc++; 267789Sahrens } 268789Sahrens } 269789Sahrens 270789Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 271789Sahrens pvd->vdev_child = newchild; 272789Sahrens pvd->vdev_children = newc; 273789Sahrens } 274789Sahrens 275789Sahrens /* 276789Sahrens * Allocate and minimally initialize a vdev_t. 277789Sahrens */ 278789Sahrens static vdev_t * 279789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 280789Sahrens { 281789Sahrens vdev_t *vd; 282789Sahrens 2831585Sbonwick vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 2841585Sbonwick 2851585Sbonwick if (spa->spa_root_vdev == NULL) { 2861585Sbonwick ASSERT(ops == &vdev_root_ops); 2871585Sbonwick spa->spa_root_vdev = vd; 2881585Sbonwick } 289789Sahrens 2901585Sbonwick if (guid == 0) { 2911585Sbonwick if (spa->spa_root_vdev == vd) { 2921585Sbonwick /* 2931585Sbonwick * The root vdev's guid will also be the pool guid, 2941585Sbonwick * which must be unique among all pools. 2951585Sbonwick */ 2961585Sbonwick while (guid == 0 || spa_guid_exists(guid, 0)) 2971585Sbonwick guid = spa_get_random(-1ULL); 2981585Sbonwick } else { 2991585Sbonwick /* 3001585Sbonwick * Any other vdev's guid must be unique within the pool. 3011585Sbonwick */ 3021585Sbonwick while (guid == 0 || 3031585Sbonwick spa_guid_exists(spa_guid(spa), guid)) 3041585Sbonwick guid = spa_get_random(-1ULL); 3051585Sbonwick } 3061585Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 3071585Sbonwick } 308789Sahrens 309789Sahrens vd->vdev_spa = spa; 310789Sahrens vd->vdev_id = id; 311789Sahrens vd->vdev_guid = guid; 312789Sahrens vd->vdev_guid_sum = guid; 313789Sahrens vd->vdev_ops = ops; 314789Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 315789Sahrens 316789Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 3172856Snd150628 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 3187754SJeff.Bonwick@Sun.COM mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 3198241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 3208241SJeff.Bonwick@Sun.COM space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0, 3218241SJeff.Bonwick@Sun.COM &vd->vdev_dtl_lock); 3228241SJeff.Bonwick@Sun.COM } 323789Sahrens txg_list_create(&vd->vdev_ms_list, 324789Sahrens offsetof(struct metaslab, ms_txg_node)); 325789Sahrens txg_list_create(&vd->vdev_dtl_list, 326789Sahrens offsetof(struct vdev, vdev_dtl_node)); 327789Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 3284451Seschrock vdev_queue_init(vd); 3294451Seschrock vdev_cache_init(vd); 330789Sahrens 331789Sahrens return (vd); 332789Sahrens } 333789Sahrens 334789Sahrens /* 335789Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 336789Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 337789Sahrens * different for each case. 338789Sahrens */ 3392082Seschrock int 3402082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 3412082Seschrock int alloctype) 342789Sahrens { 343789Sahrens vdev_ops_t *ops; 344789Sahrens char *type; 3454527Sperrin uint64_t guid = 0, islog, nparity; 346789Sahrens vdev_t *vd; 347789Sahrens 3487754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 349789Sahrens 350789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 3512082Seschrock return (EINVAL); 352789Sahrens 353789Sahrens if ((ops = vdev_getops(type)) == NULL) 3542082Seschrock return (EINVAL); 355789Sahrens 356789Sahrens /* 357789Sahrens * If this is a load, get the vdev guid from the nvlist. 358789Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 359789Sahrens */ 360789Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 361789Sahrens uint64_t label_id; 362789Sahrens 363789Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 364789Sahrens label_id != id) 3652082Seschrock return (EINVAL); 366789Sahrens 367789Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3682082Seschrock return (EINVAL); 3692082Seschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 3702082Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3712082Seschrock return (EINVAL); 3725450Sbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 3735450Sbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3745450Sbrendan return (EINVAL); 375789Sahrens } 376789Sahrens 3772082Seschrock /* 3782082Seschrock * The first allocated vdev must be of type 'root'. 3792082Seschrock */ 3802082Seschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 3812082Seschrock return (EINVAL); 3822082Seschrock 3834527Sperrin /* 3844527Sperrin * Determine whether we're a log vdev. 3854527Sperrin */ 3864527Sperrin islog = 0; 3874527Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 3885094Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 3894527Sperrin return (ENOTSUP); 3904527Sperrin 3914527Sperrin /* 3924527Sperrin * Set the nparity property for RAID-Z vdevs. 3934527Sperrin */ 3944527Sperrin nparity = -1ULL; 3954527Sperrin if (ops == &vdev_raidz_ops) { 3964527Sperrin if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 3974527Sperrin &nparity) == 0) { 3984527Sperrin /* 3994527Sperrin * Currently, we can only support 2 parity devices. 4004527Sperrin */ 4014527Sperrin if (nparity == 0 || nparity > 2) 4024527Sperrin return (EINVAL); 4034527Sperrin /* 4044527Sperrin * Older versions can only support 1 parity device. 4054527Sperrin */ 4064527Sperrin if (nparity == 2 && 4074577Sahrens spa_version(spa) < SPA_VERSION_RAID6) 4084527Sperrin return (ENOTSUP); 4094527Sperrin } else { 4104527Sperrin /* 4114527Sperrin * We require the parity to be specified for SPAs that 4124527Sperrin * support multiple parity levels. 4134527Sperrin */ 4144577Sahrens if (spa_version(spa) >= SPA_VERSION_RAID6) 4154527Sperrin return (EINVAL); 4164527Sperrin /* 4174527Sperrin * Otherwise, we default to 1 parity device for RAID-Z. 4184527Sperrin */ 4194527Sperrin nparity = 1; 4204527Sperrin } 4214527Sperrin } else { 4224527Sperrin nparity = 0; 4234527Sperrin } 4244527Sperrin ASSERT(nparity != -1ULL); 4254527Sperrin 426789Sahrens vd = vdev_alloc_common(spa, id, guid, ops); 427789Sahrens 4284527Sperrin vd->vdev_islog = islog; 4294527Sperrin vd->vdev_nparity = nparity; 4304527Sperrin 431789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 432789Sahrens vd->vdev_path = spa_strdup(vd->vdev_path); 433789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 434789Sahrens vd->vdev_devid = spa_strdup(vd->vdev_devid); 4354451Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 4364451Seschrock &vd->vdev_physpath) == 0) 4374451Seschrock vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 438*9425SEric.Schrock@Sun.COM if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 439*9425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(vd->vdev_fru); 440789Sahrens 441789Sahrens /* 4421171Seschrock * Set the whole_disk property. If it's not specified, leave the value 4431171Seschrock * as -1. 4441171Seschrock */ 4451171Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 4461171Seschrock &vd->vdev_wholedisk) != 0) 4471171Seschrock vd->vdev_wholedisk = -1ULL; 4481171Seschrock 4491171Seschrock /* 4501544Seschrock * Look for the 'not present' flag. This will only be set if the device 4511544Seschrock * was not present at the time of import. 4521544Seschrock */ 453*9425SEric.Schrock@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 454*9425SEric.Schrock@Sun.COM &vd->vdev_not_present); 4551544Seschrock 4561544Seschrock /* 4571732Sbonwick * Get the alignment requirement. 4581732Sbonwick */ 4591732Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 4601732Sbonwick 4611732Sbonwick /* 462789Sahrens * If we're a top-level vdev, try to load the allocation parameters. 463789Sahrens */ 464789Sahrens if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) { 465789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 466789Sahrens &vd->vdev_ms_array); 467789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 468789Sahrens &vd->vdev_ms_shift); 469789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 470789Sahrens &vd->vdev_asize); 471789Sahrens } 472789Sahrens 473789Sahrens /* 4744451Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 475789Sahrens */ 4766643Seschrock if (vd->vdev_ops->vdev_op_leaf && 4776643Seschrock (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) { 4786643Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 4796643Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 4808241SJeff.Bonwick@Sun.COM &vd->vdev_dtl_smo.smo_object); 4816643Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 4826643Seschrock &vd->vdev_unspare); 4836643Seschrock } 4841732Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 4851732Sbonwick &vd->vdev_offline); 4866643Seschrock 4874451Seschrock /* 4884451Seschrock * When importing a pool, we want to ignore the persistent fault 4894451Seschrock * state, as the diagnosis made on another system may not be 4904451Seschrock * valid in the current context. 4914451Seschrock */ 4924451Seschrock if (spa->spa_load_state == SPA_LOAD_OPEN) { 4934451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 4944451Seschrock &vd->vdev_faulted); 4954451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 4964451Seschrock &vd->vdev_degraded); 4974451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 4984451Seschrock &vd->vdev_removed); 4994451Seschrock } 500789Sahrens } 501789Sahrens 502789Sahrens /* 503789Sahrens * Add ourselves to the parent's list of children. 504789Sahrens */ 505789Sahrens vdev_add_child(parent, vd); 506789Sahrens 5072082Seschrock *vdp = vd; 5082082Seschrock 5092082Seschrock return (0); 510789Sahrens } 511789Sahrens 512789Sahrens void 513789Sahrens vdev_free(vdev_t *vd) 514789Sahrens { 515789Sahrens int c; 5164451Seschrock spa_t *spa = vd->vdev_spa; 517789Sahrens 518789Sahrens /* 519789Sahrens * vdev_free() implies closing the vdev first. This is simpler than 520789Sahrens * trying to ensure complicated semantics for all callers. 521789Sahrens */ 522789Sahrens vdev_close(vd); 523789Sahrens 5247754SJeff.Bonwick@Sun.COM ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 525789Sahrens 526789Sahrens /* 527789Sahrens * Free all children. 528789Sahrens */ 529789Sahrens for (c = 0; c < vd->vdev_children; c++) 530789Sahrens vdev_free(vd->vdev_child[c]); 531789Sahrens 532789Sahrens ASSERT(vd->vdev_child == NULL); 533789Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 534789Sahrens 535789Sahrens /* 536789Sahrens * Discard allocation state. 537789Sahrens */ 538789Sahrens if (vd == vd->vdev_top) 539789Sahrens vdev_metaslab_fini(vd); 540789Sahrens 541789Sahrens ASSERT3U(vd->vdev_stat.vs_space, ==, 0); 5422082Seschrock ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0); 543789Sahrens ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 544789Sahrens 545789Sahrens /* 546789Sahrens * Remove this vdev from its parent's child list. 547789Sahrens */ 548789Sahrens vdev_remove_child(vd->vdev_parent, vd); 549789Sahrens 550789Sahrens ASSERT(vd->vdev_parent == NULL); 551789Sahrens 5524451Seschrock /* 5534451Seschrock * Clean up vdev structure. 5544451Seschrock */ 5554451Seschrock vdev_queue_fini(vd); 5564451Seschrock vdev_cache_fini(vd); 5574451Seschrock 5584451Seschrock if (vd->vdev_path) 5594451Seschrock spa_strfree(vd->vdev_path); 5604451Seschrock if (vd->vdev_devid) 5614451Seschrock spa_strfree(vd->vdev_devid); 5624451Seschrock if (vd->vdev_physpath) 5634451Seschrock spa_strfree(vd->vdev_physpath); 564*9425SEric.Schrock@Sun.COM if (vd->vdev_fru) 565*9425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 5664451Seschrock 5674451Seschrock if (vd->vdev_isspare) 5684451Seschrock spa_spare_remove(vd); 5695450Sbrendan if (vd->vdev_isl2cache) 5705450Sbrendan spa_l2cache_remove(vd); 5714451Seschrock 5724451Seschrock txg_list_destroy(&vd->vdev_ms_list); 5734451Seschrock txg_list_destroy(&vd->vdev_dtl_list); 5748241SJeff.Bonwick@Sun.COM 5754451Seschrock mutex_enter(&vd->vdev_dtl_lock); 5768241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 5778241SJeff.Bonwick@Sun.COM space_map_unload(&vd->vdev_dtl[t]); 5788241SJeff.Bonwick@Sun.COM space_map_destroy(&vd->vdev_dtl[t]); 5798241SJeff.Bonwick@Sun.COM } 5804451Seschrock mutex_exit(&vd->vdev_dtl_lock); 5818241SJeff.Bonwick@Sun.COM 5824451Seschrock mutex_destroy(&vd->vdev_dtl_lock); 5834451Seschrock mutex_destroy(&vd->vdev_stat_lock); 5847754SJeff.Bonwick@Sun.COM mutex_destroy(&vd->vdev_probe_lock); 5854451Seschrock 5864451Seschrock if (vd == spa->spa_root_vdev) 5874451Seschrock spa->spa_root_vdev = NULL; 5884451Seschrock 5894451Seschrock kmem_free(vd, sizeof (vdev_t)); 590789Sahrens } 591789Sahrens 592789Sahrens /* 593789Sahrens * Transfer top-level vdev state from svd to tvd. 594789Sahrens */ 595789Sahrens static void 596789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 597789Sahrens { 598789Sahrens spa_t *spa = svd->vdev_spa; 599789Sahrens metaslab_t *msp; 600789Sahrens vdev_t *vd; 601789Sahrens int t; 602789Sahrens 603789Sahrens ASSERT(tvd == tvd->vdev_top); 604789Sahrens 605789Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 606789Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 607789Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 608789Sahrens 609789Sahrens svd->vdev_ms_array = 0; 610789Sahrens svd->vdev_ms_shift = 0; 611789Sahrens svd->vdev_ms_count = 0; 612789Sahrens 613789Sahrens tvd->vdev_mg = svd->vdev_mg; 614789Sahrens tvd->vdev_ms = svd->vdev_ms; 615789Sahrens 616789Sahrens svd->vdev_mg = NULL; 617789Sahrens svd->vdev_ms = NULL; 6181732Sbonwick 6191732Sbonwick if (tvd->vdev_mg != NULL) 6201732Sbonwick tvd->vdev_mg->mg_vd = tvd; 621789Sahrens 622789Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 623789Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 6242082Seschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 625789Sahrens 626789Sahrens svd->vdev_stat.vs_alloc = 0; 627789Sahrens svd->vdev_stat.vs_space = 0; 6282082Seschrock svd->vdev_stat.vs_dspace = 0; 629789Sahrens 630789Sahrens for (t = 0; t < TXG_SIZE; t++) { 631789Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 632789Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 633789Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 634789Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 635789Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 636789Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 637789Sahrens } 638789Sahrens 6397754SJeff.Bonwick@Sun.COM if (list_link_active(&svd->vdev_config_dirty_node)) { 640789Sahrens vdev_config_clean(svd); 641789Sahrens vdev_config_dirty(tvd); 642789Sahrens } 643789Sahrens 6447754SJeff.Bonwick@Sun.COM if (list_link_active(&svd->vdev_state_dirty_node)) { 6457754SJeff.Bonwick@Sun.COM vdev_state_clean(svd); 6467754SJeff.Bonwick@Sun.COM vdev_state_dirty(tvd); 6477754SJeff.Bonwick@Sun.COM } 6487754SJeff.Bonwick@Sun.COM 6492082Seschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 6502082Seschrock svd->vdev_deflate_ratio = 0; 6514527Sperrin 6524527Sperrin tvd->vdev_islog = svd->vdev_islog; 6534527Sperrin svd->vdev_islog = 0; 654789Sahrens } 655789Sahrens 656789Sahrens static void 657789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 658789Sahrens { 659789Sahrens int c; 660789Sahrens 661789Sahrens if (vd == NULL) 662789Sahrens return; 663789Sahrens 664789Sahrens vd->vdev_top = tvd; 665789Sahrens 666789Sahrens for (c = 0; c < vd->vdev_children; c++) 667789Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 668789Sahrens } 669789Sahrens 670789Sahrens /* 671789Sahrens * Add a mirror/replacing vdev above an existing vdev. 672789Sahrens */ 673789Sahrens vdev_t * 674789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 675789Sahrens { 676789Sahrens spa_t *spa = cvd->vdev_spa; 677789Sahrens vdev_t *pvd = cvd->vdev_parent; 678789Sahrens vdev_t *mvd; 679789Sahrens 6807754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 681789Sahrens 682789Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 6831732Sbonwick 6841732Sbonwick mvd->vdev_asize = cvd->vdev_asize; 6851732Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 6861732Sbonwick mvd->vdev_state = cvd->vdev_state; 6871732Sbonwick 688789Sahrens vdev_remove_child(pvd, cvd); 689789Sahrens vdev_add_child(pvd, mvd); 690789Sahrens cvd->vdev_id = mvd->vdev_children; 691789Sahrens vdev_add_child(mvd, cvd); 692789Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 693789Sahrens 694789Sahrens if (mvd == mvd->vdev_top) 695789Sahrens vdev_top_transfer(cvd, mvd); 696789Sahrens 697789Sahrens return (mvd); 698789Sahrens } 699789Sahrens 700789Sahrens /* 701789Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 702789Sahrens */ 703789Sahrens void 704789Sahrens vdev_remove_parent(vdev_t *cvd) 705789Sahrens { 706789Sahrens vdev_t *mvd = cvd->vdev_parent; 707789Sahrens vdev_t *pvd = mvd->vdev_parent; 708789Sahrens 7097754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 710789Sahrens 711789Sahrens ASSERT(mvd->vdev_children == 1); 712789Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 7132082Seschrock mvd->vdev_ops == &vdev_replacing_ops || 7142082Seschrock mvd->vdev_ops == &vdev_spare_ops); 7151732Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 716789Sahrens 717789Sahrens vdev_remove_child(mvd, cvd); 718789Sahrens vdev_remove_child(pvd, mvd); 7198241SJeff.Bonwick@Sun.COM 7207754SJeff.Bonwick@Sun.COM /* 7217754SJeff.Bonwick@Sun.COM * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 7227754SJeff.Bonwick@Sun.COM * Otherwise, we could have detached an offline device, and when we 7237754SJeff.Bonwick@Sun.COM * go to import the pool we'll think we have two top-level vdevs, 7247754SJeff.Bonwick@Sun.COM * instead of a different version of the same top-level vdev. 7257754SJeff.Bonwick@Sun.COM */ 7268241SJeff.Bonwick@Sun.COM if (mvd->vdev_top == mvd) { 7278241SJeff.Bonwick@Sun.COM uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 7288241SJeff.Bonwick@Sun.COM cvd->vdev_guid += guid_delta; 7298241SJeff.Bonwick@Sun.COM cvd->vdev_guid_sum += guid_delta; 7308241SJeff.Bonwick@Sun.COM } 731789Sahrens cvd->vdev_id = mvd->vdev_id; 732789Sahrens vdev_add_child(pvd, cvd); 733789Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 734789Sahrens 735789Sahrens if (cvd == cvd->vdev_top) 736789Sahrens vdev_top_transfer(mvd, cvd); 737789Sahrens 738789Sahrens ASSERT(mvd->vdev_children == 0); 739789Sahrens vdev_free(mvd); 740789Sahrens } 741789Sahrens 7421544Seschrock int 743789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 744789Sahrens { 745789Sahrens spa_t *spa = vd->vdev_spa; 7461732Sbonwick objset_t *mos = spa->spa_meta_objset; 7474527Sperrin metaslab_class_t *mc; 7481732Sbonwick uint64_t m; 749789Sahrens uint64_t oldc = vd->vdev_ms_count; 750789Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 7511732Sbonwick metaslab_t **mspp; 7521732Sbonwick int error; 753789Sahrens 7541585Sbonwick if (vd->vdev_ms_shift == 0) /* not being allocated from yet */ 7551585Sbonwick return (0); 7561585Sbonwick 757789Sahrens ASSERT(oldc <= newc); 758789Sahrens 7594527Sperrin if (vd->vdev_islog) 7604527Sperrin mc = spa->spa_log_class; 7614527Sperrin else 7624527Sperrin mc = spa->spa_normal_class; 7634527Sperrin 7641732Sbonwick if (vd->vdev_mg == NULL) 7651732Sbonwick vd->vdev_mg = metaslab_group_create(mc, vd); 7661732Sbonwick 7671732Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 7681732Sbonwick 7691732Sbonwick if (oldc != 0) { 7701732Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 7711732Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 7721732Sbonwick } 7731732Sbonwick 7741732Sbonwick vd->vdev_ms = mspp; 775789Sahrens vd->vdev_ms_count = newc; 776789Sahrens 7771732Sbonwick for (m = oldc; m < newc; m++) { 7781732Sbonwick space_map_obj_t smo = { 0, 0, 0 }; 779789Sahrens if (txg == 0) { 7801732Sbonwick uint64_t object = 0; 7811732Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 7821732Sbonwick m * sizeof (uint64_t), sizeof (uint64_t), &object); 7831732Sbonwick if (error) 7841732Sbonwick return (error); 7851732Sbonwick if (object != 0) { 7861732Sbonwick dmu_buf_t *db; 7871732Sbonwick error = dmu_bonus_hold(mos, object, FTAG, &db); 7881732Sbonwick if (error) 7891732Sbonwick return (error); 7904944Smaybee ASSERT3U(db->db_size, >=, sizeof (smo)); 7914944Smaybee bcopy(db->db_data, &smo, sizeof (smo)); 7921732Sbonwick ASSERT3U(smo.smo_object, ==, object); 7931544Seschrock dmu_buf_rele(db, FTAG); 794789Sahrens } 795789Sahrens } 7961732Sbonwick vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo, 7971732Sbonwick m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg); 798789Sahrens } 799789Sahrens 8001544Seschrock return (0); 801789Sahrens } 802789Sahrens 803789Sahrens void 804789Sahrens vdev_metaslab_fini(vdev_t *vd) 805789Sahrens { 806789Sahrens uint64_t m; 807789Sahrens uint64_t count = vd->vdev_ms_count; 808789Sahrens 809789Sahrens if (vd->vdev_ms != NULL) { 810789Sahrens for (m = 0; m < count; m++) 8111732Sbonwick if (vd->vdev_ms[m] != NULL) 8121732Sbonwick metaslab_fini(vd->vdev_ms[m]); 813789Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 814789Sahrens vd->vdev_ms = NULL; 815789Sahrens } 816789Sahrens } 817789Sahrens 8187754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats { 8197754SJeff.Bonwick@Sun.COM boolean_t vps_readable; 8207754SJeff.Bonwick@Sun.COM boolean_t vps_writeable; 8217754SJeff.Bonwick@Sun.COM int vps_flags; 8227754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t; 8237754SJeff.Bonwick@Sun.COM 8247754SJeff.Bonwick@Sun.COM static void 8257754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio) 8265329Sgw25295 { 8278241SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 8288632SBill.Moore@Sun.COM vdev_t *vd = zio->io_vd; 8297754SJeff.Bonwick@Sun.COM vdev_probe_stats_t *vps = zio->io_private; 8308632SBill.Moore@Sun.COM 8318632SBill.Moore@Sun.COM ASSERT(vd->vdev_probe_zio != NULL); 8327754SJeff.Bonwick@Sun.COM 8337754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_READ) { 8347754SJeff.Bonwick@Sun.COM if (zio->io_error == 0) 8357754SJeff.Bonwick@Sun.COM vps->vps_readable = 1; 8368241SJeff.Bonwick@Sun.COM if (zio->io_error == 0 && spa_writeable(spa)) { 8378632SBill.Moore@Sun.COM zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 8387754SJeff.Bonwick@Sun.COM zio->io_offset, zio->io_size, zio->io_data, 8397754SJeff.Bonwick@Sun.COM ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 8407754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 8417754SJeff.Bonwick@Sun.COM } else { 8427754SJeff.Bonwick@Sun.COM zio_buf_free(zio->io_data, zio->io_size); 8437754SJeff.Bonwick@Sun.COM } 8447754SJeff.Bonwick@Sun.COM } else if (zio->io_type == ZIO_TYPE_WRITE) { 8457754SJeff.Bonwick@Sun.COM if (zio->io_error == 0) 8467754SJeff.Bonwick@Sun.COM vps->vps_writeable = 1; 8477754SJeff.Bonwick@Sun.COM zio_buf_free(zio->io_data, zio->io_size); 8487754SJeff.Bonwick@Sun.COM } else if (zio->io_type == ZIO_TYPE_NULL) { 8498632SBill.Moore@Sun.COM zio_t *pio; 8507754SJeff.Bonwick@Sun.COM 8517754SJeff.Bonwick@Sun.COM vd->vdev_cant_read |= !vps->vps_readable; 8527754SJeff.Bonwick@Sun.COM vd->vdev_cant_write |= !vps->vps_writeable; 8537754SJeff.Bonwick@Sun.COM 8547754SJeff.Bonwick@Sun.COM if (vdev_readable(vd) && 8558241SJeff.Bonwick@Sun.COM (vdev_writeable(vd) || !spa_writeable(spa))) { 8567754SJeff.Bonwick@Sun.COM zio->io_error = 0; 8577754SJeff.Bonwick@Sun.COM } else { 8587754SJeff.Bonwick@Sun.COM ASSERT(zio->io_error != 0); 8597754SJeff.Bonwick@Sun.COM zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 8608241SJeff.Bonwick@Sun.COM spa, vd, NULL, 0, 0); 8617754SJeff.Bonwick@Sun.COM zio->io_error = ENXIO; 8627754SJeff.Bonwick@Sun.COM } 8638632SBill.Moore@Sun.COM 8648632SBill.Moore@Sun.COM mutex_enter(&vd->vdev_probe_lock); 8658632SBill.Moore@Sun.COM ASSERT(vd->vdev_probe_zio == zio); 8668632SBill.Moore@Sun.COM vd->vdev_probe_zio = NULL; 8678632SBill.Moore@Sun.COM mutex_exit(&vd->vdev_probe_lock); 8688632SBill.Moore@Sun.COM 8698632SBill.Moore@Sun.COM while ((pio = zio_walk_parents(zio)) != NULL) 8708632SBill.Moore@Sun.COM if (!vdev_accessible(vd, pio)) 8718632SBill.Moore@Sun.COM pio->io_error = ENXIO; 8728632SBill.Moore@Sun.COM 8737754SJeff.Bonwick@Sun.COM kmem_free(vps, sizeof (*vps)); 8747754SJeff.Bonwick@Sun.COM } 8757754SJeff.Bonwick@Sun.COM } 8765329Sgw25295 8777754SJeff.Bonwick@Sun.COM /* 8787754SJeff.Bonwick@Sun.COM * Determine whether this device is accessible by reading and writing 8797754SJeff.Bonwick@Sun.COM * to several known locations: the pad regions of each vdev label 8807754SJeff.Bonwick@Sun.COM * but the first (which we leave alone in case it contains a VTOC). 8817754SJeff.Bonwick@Sun.COM */ 8827754SJeff.Bonwick@Sun.COM zio_t * 8838632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio) 8847754SJeff.Bonwick@Sun.COM { 8857754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 8868632SBill.Moore@Sun.COM vdev_probe_stats_t *vps = NULL; 8878632SBill.Moore@Sun.COM zio_t *pio; 8887754SJeff.Bonwick@Sun.COM 8897754SJeff.Bonwick@Sun.COM ASSERT(vd->vdev_ops->vdev_op_leaf); 8907754SJeff.Bonwick@Sun.COM 8918632SBill.Moore@Sun.COM /* 8928632SBill.Moore@Sun.COM * Don't probe the probe. 8938632SBill.Moore@Sun.COM */ 8948632SBill.Moore@Sun.COM if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 8958632SBill.Moore@Sun.COM return (NULL); 8968632SBill.Moore@Sun.COM 8978632SBill.Moore@Sun.COM /* 8988632SBill.Moore@Sun.COM * To prevent 'probe storms' when a device fails, we create 8998632SBill.Moore@Sun.COM * just one probe i/o at a time. All zios that want to probe 9008632SBill.Moore@Sun.COM * this vdev will become parents of the probe io. 9018632SBill.Moore@Sun.COM */ 9028632SBill.Moore@Sun.COM mutex_enter(&vd->vdev_probe_lock); 9038632SBill.Moore@Sun.COM 9048632SBill.Moore@Sun.COM if ((pio = vd->vdev_probe_zio) == NULL) { 9058632SBill.Moore@Sun.COM vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 9068632SBill.Moore@Sun.COM 9078632SBill.Moore@Sun.COM vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 9088632SBill.Moore@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 9098632SBill.Moore@Sun.COM ZIO_FLAG_DONT_RETRY; 9108632SBill.Moore@Sun.COM 9118632SBill.Moore@Sun.COM if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 9128632SBill.Moore@Sun.COM /* 9138632SBill.Moore@Sun.COM * vdev_cant_read and vdev_cant_write can only 9148632SBill.Moore@Sun.COM * transition from TRUE to FALSE when we have the 9158632SBill.Moore@Sun.COM * SCL_ZIO lock as writer; otherwise they can only 9168632SBill.Moore@Sun.COM * transition from FALSE to TRUE. This ensures that 9178632SBill.Moore@Sun.COM * any zio looking at these values can assume that 9188632SBill.Moore@Sun.COM * failures persist for the life of the I/O. That's 9198632SBill.Moore@Sun.COM * important because when a device has intermittent 9208632SBill.Moore@Sun.COM * connectivity problems, we want to ensure that 9218632SBill.Moore@Sun.COM * they're ascribed to the device (ENXIO) and not 9228632SBill.Moore@Sun.COM * the zio (EIO). 9238632SBill.Moore@Sun.COM * 9248632SBill.Moore@Sun.COM * Since we hold SCL_ZIO as writer here, clear both 9258632SBill.Moore@Sun.COM * values so the probe can reevaluate from first 9268632SBill.Moore@Sun.COM * principles. 9278632SBill.Moore@Sun.COM */ 9288632SBill.Moore@Sun.COM vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 9298632SBill.Moore@Sun.COM vd->vdev_cant_read = B_FALSE; 9308632SBill.Moore@Sun.COM vd->vdev_cant_write = B_FALSE; 9318632SBill.Moore@Sun.COM } 9328632SBill.Moore@Sun.COM 9338632SBill.Moore@Sun.COM vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 9348632SBill.Moore@Sun.COM vdev_probe_done, vps, 9358632SBill.Moore@Sun.COM vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 9368632SBill.Moore@Sun.COM 9378632SBill.Moore@Sun.COM if (zio != NULL) { 9388632SBill.Moore@Sun.COM vd->vdev_probe_wanted = B_TRUE; 9398632SBill.Moore@Sun.COM spa_async_request(spa, SPA_ASYNC_PROBE); 9408632SBill.Moore@Sun.COM } 9418632SBill.Moore@Sun.COM } 9428632SBill.Moore@Sun.COM 9438632SBill.Moore@Sun.COM if (zio != NULL) 9448632SBill.Moore@Sun.COM zio_add_child(zio, pio); 9458632SBill.Moore@Sun.COM 9468632SBill.Moore@Sun.COM mutex_exit(&vd->vdev_probe_lock); 9478632SBill.Moore@Sun.COM 9488632SBill.Moore@Sun.COM if (vps == NULL) { 9498632SBill.Moore@Sun.COM ASSERT(zio != NULL); 9508632SBill.Moore@Sun.COM return (NULL); 9518632SBill.Moore@Sun.COM } 9527754SJeff.Bonwick@Sun.COM 9537754SJeff.Bonwick@Sun.COM for (int l = 1; l < VDEV_LABELS; l++) { 9548632SBill.Moore@Sun.COM zio_nowait(zio_read_phys(pio, vd, 9557754SJeff.Bonwick@Sun.COM vdev_label_offset(vd->vdev_psize, l, 9569056SLin.Ling@Sun.COM offsetof(vdev_label_t, vl_pad2)), 9579056SLin.Ling@Sun.COM VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 9587754SJeff.Bonwick@Sun.COM ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 9597754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 9607754SJeff.Bonwick@Sun.COM } 9617754SJeff.Bonwick@Sun.COM 9628632SBill.Moore@Sun.COM if (zio == NULL) 9638632SBill.Moore@Sun.COM return (pio); 9648632SBill.Moore@Sun.COM 9658632SBill.Moore@Sun.COM zio_nowait(pio); 9668632SBill.Moore@Sun.COM return (NULL); 9675329Sgw25295 } 9685329Sgw25295 969789Sahrens /* 970789Sahrens * Prepare a virtual device for access. 971789Sahrens */ 972789Sahrens int 973789Sahrens vdev_open(vdev_t *vd) 974789Sahrens { 9758241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 976789Sahrens int error; 977789Sahrens int c; 978789Sahrens uint64_t osize = 0; 979789Sahrens uint64_t asize, psize; 9801732Sbonwick uint64_t ashift = 0; 981789Sahrens 9828241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 9838241SJeff.Bonwick@Sun.COM 984789Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 985789Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 986789Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 987789Sahrens 988789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 989789Sahrens 9904451Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 9914451Seschrock ASSERT(vd->vdev_children == 0); 9924451Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 9934451Seschrock VDEV_AUX_ERR_EXCEEDED); 9944451Seschrock return (ENXIO); 9954451Seschrock } else if (vd->vdev_offline) { 996789Sahrens ASSERT(vd->vdev_children == 0); 9971544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 998789Sahrens return (ENXIO); 999789Sahrens } 1000789Sahrens 1001789Sahrens error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 1002789Sahrens 10031544Seschrock if (zio_injection_enabled && error == 0) 10041544Seschrock error = zio_handle_device_injection(vd, ENXIO); 10051544Seschrock 10064451Seschrock if (error) { 10074451Seschrock if (vd->vdev_removed && 10084451Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 10094451Seschrock vd->vdev_removed = B_FALSE; 1010789Sahrens 10111544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1012789Sahrens vd->vdev_stat.vs_aux); 1013789Sahrens return (error); 1014789Sahrens } 1015789Sahrens 10164451Seschrock vd->vdev_removed = B_FALSE; 10174451Seschrock 10184451Seschrock if (vd->vdev_degraded) { 10194451Seschrock ASSERT(vd->vdev_children == 0); 10204451Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 10214451Seschrock VDEV_AUX_ERR_EXCEEDED); 10224451Seschrock } else { 10234451Seschrock vd->vdev_state = VDEV_STATE_HEALTHY; 10244451Seschrock } 1025789Sahrens 1026789Sahrens for (c = 0; c < vd->vdev_children; c++) 10271544Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 10281544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 10291544Seschrock VDEV_AUX_NONE); 10301544Seschrock break; 10311544Seschrock } 1032789Sahrens 1033789Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1034789Sahrens 1035789Sahrens if (vd->vdev_children == 0) { 1036789Sahrens if (osize < SPA_MINDEVSIZE) { 10371544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 10381544Seschrock VDEV_AUX_TOO_SMALL); 1039789Sahrens return (EOVERFLOW); 1040789Sahrens } 1041789Sahrens psize = osize; 1042789Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1043789Sahrens } else { 10441732Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1045789Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 10461544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 10471544Seschrock VDEV_AUX_TOO_SMALL); 1048789Sahrens return (EOVERFLOW); 1049789Sahrens } 1050789Sahrens psize = 0; 1051789Sahrens asize = osize; 1052789Sahrens } 1053789Sahrens 1054789Sahrens vd->vdev_psize = psize; 1055789Sahrens 1056789Sahrens if (vd->vdev_asize == 0) { 1057789Sahrens /* 1058789Sahrens * This is the first-ever open, so use the computed values. 10591732Sbonwick * For testing purposes, a higher ashift can be requested. 1060789Sahrens */ 1061789Sahrens vd->vdev_asize = asize; 10621732Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1063789Sahrens } else { 1064789Sahrens /* 1065789Sahrens * Make sure the alignment requirement hasn't increased. 1066789Sahrens */ 10671732Sbonwick if (ashift > vd->vdev_top->vdev_ashift) { 10681544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 10691544Seschrock VDEV_AUX_BAD_LABEL); 1070789Sahrens return (EINVAL); 1071789Sahrens } 1072789Sahrens 1073789Sahrens /* 1074789Sahrens * Make sure the device hasn't shrunk. 1075789Sahrens */ 1076789Sahrens if (asize < vd->vdev_asize) { 10771544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 10781544Seschrock VDEV_AUX_BAD_LABEL); 1079789Sahrens return (EINVAL); 1080789Sahrens } 1081789Sahrens 1082789Sahrens /* 1083789Sahrens * If all children are healthy and the asize has increased, 1084789Sahrens * then we've experienced dynamic LUN growth. 1085789Sahrens */ 1086789Sahrens if (vd->vdev_state == VDEV_STATE_HEALTHY && 1087789Sahrens asize > vd->vdev_asize) { 1088789Sahrens vd->vdev_asize = asize; 1089789Sahrens } 1090789Sahrens } 1091789Sahrens 10921544Seschrock /* 10935329Sgw25295 * Ensure we can issue some IO before declaring the 10945329Sgw25295 * vdev open for business. 10955329Sgw25295 */ 10967754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && 10977754SJeff.Bonwick@Sun.COM (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 10985329Sgw25295 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 10997754SJeff.Bonwick@Sun.COM VDEV_AUX_IO_FAILURE); 11005329Sgw25295 return (error); 11015329Sgw25295 } 11025329Sgw25295 11035329Sgw25295 /* 11042082Seschrock * If this is a top-level vdev, compute the raidz-deflation 11052082Seschrock * ratio. Note, we hard-code in 128k (1<<17) because it is the 11062082Seschrock * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE 11072082Seschrock * changes, this algorithm must never change, or we will 11082082Seschrock * inconsistently account for existing bp's. 11092082Seschrock */ 11102082Seschrock if (vd->vdev_top == vd) { 11112082Seschrock vd->vdev_deflate_ratio = (1<<17) / 11122082Seschrock (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT); 11132082Seschrock } 11142082Seschrock 11157046Sahrens /* 11167046Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 11178241SJeff.Bonwick@Sun.COM * resilver. But don't do this if we are doing a reopen for a scrub, 11188241SJeff.Bonwick@Sun.COM * since this would just restart the scrub we are already doing. 11197046Sahrens */ 11208241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 11218241SJeff.Bonwick@Sun.COM vdev_resilver_needed(vd, NULL, NULL)) 11228241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 11237046Sahrens 1124789Sahrens return (0); 1125789Sahrens } 1126789Sahrens 1127789Sahrens /* 11281986Seschrock * Called once the vdevs are all opened, this routine validates the label 11291986Seschrock * contents. This needs to be done before vdev_load() so that we don't 11304451Seschrock * inadvertently do repair I/Os to the wrong device. 11311986Seschrock * 11321986Seschrock * This function will only return failure if one of the vdevs indicates that it 11331986Seschrock * has since been destroyed or exported. This is only possible if 11341986Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 11351986Seschrock * will be updated but the function will return 0. 11361986Seschrock */ 11371986Seschrock int 11381986Seschrock vdev_validate(vdev_t *vd) 11391986Seschrock { 11401986Seschrock spa_t *spa = vd->vdev_spa; 11411986Seschrock int c; 11421986Seschrock nvlist_t *label; 11437754SJeff.Bonwick@Sun.COM uint64_t guid, top_guid; 11441986Seschrock uint64_t state; 11451986Seschrock 11461986Seschrock for (c = 0; c < vd->vdev_children; c++) 11471986Seschrock if (vdev_validate(vd->vdev_child[c]) != 0) 11484070Smc142369 return (EBADF); 11491986Seschrock 11502174Seschrock /* 11512174Seschrock * If the device has already failed, or was marked offline, don't do 11522174Seschrock * any further validation. Otherwise, label I/O will fail and we will 11532174Seschrock * overwrite the previous state. 11542174Seschrock */ 11557754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 11561986Seschrock 11571986Seschrock if ((label = vdev_label_read_config(vd)) == NULL) { 11581986Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 11591986Seschrock VDEV_AUX_BAD_LABEL); 11601986Seschrock return (0); 11611986Seschrock } 11621986Seschrock 11631986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 11641986Seschrock &guid) != 0 || guid != spa_guid(spa)) { 11651986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 11661986Seschrock VDEV_AUX_CORRUPT_DATA); 11671986Seschrock nvlist_free(label); 11681986Seschrock return (0); 11691986Seschrock } 11701986Seschrock 11717754SJeff.Bonwick@Sun.COM /* 11727754SJeff.Bonwick@Sun.COM * If this vdev just became a top-level vdev because its 11737754SJeff.Bonwick@Sun.COM * sibling was detached, it will have adopted the parent's 11747754SJeff.Bonwick@Sun.COM * vdev guid -- but the label may or may not be on disk yet. 11757754SJeff.Bonwick@Sun.COM * Fortunately, either version of the label will have the 11767754SJeff.Bonwick@Sun.COM * same top guid, so if we're a top-level vdev, we can 11777754SJeff.Bonwick@Sun.COM * safely compare to that instead. 11787754SJeff.Bonwick@Sun.COM */ 11791986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 11807754SJeff.Bonwick@Sun.COM &guid) != 0 || 11817754SJeff.Bonwick@Sun.COM nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 11827754SJeff.Bonwick@Sun.COM &top_guid) != 0 || 11837754SJeff.Bonwick@Sun.COM (vd->vdev_guid != guid && 11847754SJeff.Bonwick@Sun.COM (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 11851986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 11861986Seschrock VDEV_AUX_CORRUPT_DATA); 11871986Seschrock nvlist_free(label); 11881986Seschrock return (0); 11891986Seschrock } 11901986Seschrock 11911986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 11921986Seschrock &state) != 0) { 11931986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 11941986Seschrock VDEV_AUX_CORRUPT_DATA); 11951986Seschrock nvlist_free(label); 11961986Seschrock return (0); 11971986Seschrock } 11981986Seschrock 11991986Seschrock nvlist_free(label); 12001986Seschrock 12011986Seschrock if (spa->spa_load_state == SPA_LOAD_OPEN && 12021986Seschrock state != POOL_STATE_ACTIVE) 12034070Smc142369 return (EBADF); 12046976Seschrock 12056976Seschrock /* 12066976Seschrock * If we were able to open and validate a vdev that was 12076976Seschrock * previously marked permanently unavailable, clear that state 12086976Seschrock * now. 12096976Seschrock */ 12106976Seschrock if (vd->vdev_not_present) 12116976Seschrock vd->vdev_not_present = 0; 12121986Seschrock } 12131986Seschrock 12141986Seschrock return (0); 12151986Seschrock } 12161986Seschrock 12171986Seschrock /* 1218789Sahrens * Close a virtual device. 1219789Sahrens */ 1220789Sahrens void 1221789Sahrens vdev_close(vdev_t *vd) 1222789Sahrens { 12238241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 12248241SJeff.Bonwick@Sun.COM 12258241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 12268241SJeff.Bonwick@Sun.COM 1227789Sahrens vd->vdev_ops->vdev_op_close(vd); 1228789Sahrens 12294451Seschrock vdev_cache_purge(vd); 1230789Sahrens 12311986Seschrock /* 12321986Seschrock * We record the previous state before we close it, so that if we are 12331986Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 12341986Seschrock * it's still faulted. 12351986Seschrock */ 12361986Seschrock vd->vdev_prevstate = vd->vdev_state; 12371986Seschrock 1238789Sahrens if (vd->vdev_offline) 1239789Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 1240789Sahrens else 1241789Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 12421544Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1243789Sahrens } 1244789Sahrens 1245789Sahrens void 12461544Seschrock vdev_reopen(vdev_t *vd) 1247789Sahrens { 12481544Seschrock spa_t *spa = vd->vdev_spa; 1249789Sahrens 12507754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 12511544Seschrock 1252789Sahrens vdev_close(vd); 1253789Sahrens (void) vdev_open(vd); 1254789Sahrens 1255789Sahrens /* 12563377Seschrock * Call vdev_validate() here to make sure we have the same device. 12573377Seschrock * Otherwise, a device with an invalid label could be successfully 12583377Seschrock * opened in response to vdev_reopen(). 12593377Seschrock */ 12606643Seschrock if (vd->vdev_aux) { 12616643Seschrock (void) vdev_validate_aux(vd); 12627754SJeff.Bonwick@Sun.COM if (vdev_readable(vd) && vdev_writeable(vd) && 1263*9425SEric.Schrock@Sun.COM vd->vdev_aux == &spa->spa_l2cache && 12646643Seschrock !l2arc_vdev_present(vd)) { 12656643Seschrock uint64_t size = vdev_get_rsize(vd); 12666643Seschrock l2arc_add_vdev(spa, vd, 12676643Seschrock VDEV_LABEL_START_SIZE, 12686643Seschrock size - VDEV_LABEL_START_SIZE); 12696643Seschrock } 12706643Seschrock } else { 12716643Seschrock (void) vdev_validate(vd); 12726643Seschrock } 12733377Seschrock 12743377Seschrock /* 12754451Seschrock * Reassess parent vdev's health. 1276789Sahrens */ 12774451Seschrock vdev_propagate_state(vd); 1278789Sahrens } 1279789Sahrens 1280789Sahrens int 12812082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1282789Sahrens { 1283789Sahrens int error; 1284789Sahrens 1285789Sahrens /* 1286789Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 1287789Sahrens * For a create, however, we want to fail the request if 1288789Sahrens * there are any components we can't open. 1289789Sahrens */ 1290789Sahrens error = vdev_open(vd); 1291789Sahrens 1292789Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1293789Sahrens vdev_close(vd); 1294789Sahrens return (error ? error : ENXIO); 1295789Sahrens } 1296789Sahrens 1297789Sahrens /* 1298789Sahrens * Recursively initialize all labels. 1299789Sahrens */ 13003377Seschrock if ((error = vdev_label_init(vd, txg, isreplacing ? 13013377Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1302789Sahrens vdev_close(vd); 1303789Sahrens return (error); 1304789Sahrens } 1305789Sahrens 1306789Sahrens return (0); 1307789Sahrens } 1308789Sahrens 1309789Sahrens /* 1310789Sahrens * The is the latter half of vdev_create(). It is distinct because it 1311789Sahrens * involves initiating transactions in order to do metaslab creation. 1312789Sahrens * For creation, we want to try to create all vdevs at once and then undo it 1313789Sahrens * if anything fails; this is much harder if we have pending transactions. 1314789Sahrens */ 13151585Sbonwick void 1316789Sahrens vdev_init(vdev_t *vd, uint64_t txg) 1317789Sahrens { 1318789Sahrens /* 1319789Sahrens * Aim for roughly 200 metaslabs per vdev. 1320789Sahrens */ 1321789Sahrens vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 1322789Sahrens vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1323789Sahrens 1324789Sahrens /* 13251585Sbonwick * Initialize the vdev's metaslabs. This can't fail because 13261585Sbonwick * there's nothing to read when creating all new metaslabs. 1327789Sahrens */ 13281585Sbonwick VERIFY(vdev_metaslab_init(vd, txg) == 0); 1329789Sahrens } 1330789Sahrens 1331789Sahrens void 13321732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1333789Sahrens { 13341732Sbonwick ASSERT(vd == vd->vdev_top); 13351732Sbonwick ASSERT(ISP2(flags)); 1336789Sahrens 13371732Sbonwick if (flags & VDD_METASLAB) 13381732Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 13391732Sbonwick 13401732Sbonwick if (flags & VDD_DTL) 13411732Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 13421732Sbonwick 13431732Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1344789Sahrens } 1345789Sahrens 13468241SJeff.Bonwick@Sun.COM /* 13478241SJeff.Bonwick@Sun.COM * DTLs. 13488241SJeff.Bonwick@Sun.COM * 13498241SJeff.Bonwick@Sun.COM * A vdev's DTL (dirty time log) is the set of transaction groups for which 13508241SJeff.Bonwick@Sun.COM * the vdev has less than perfect replication. There are three kinds of DTL: 13518241SJeff.Bonwick@Sun.COM * 13528241SJeff.Bonwick@Sun.COM * DTL_MISSING: txgs for which the vdev has no valid copies of the data 13538241SJeff.Bonwick@Sun.COM * 13548241SJeff.Bonwick@Sun.COM * DTL_PARTIAL: txgs for which data is available, but not fully replicated 13558241SJeff.Bonwick@Sun.COM * 13568241SJeff.Bonwick@Sun.COM * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 13578241SJeff.Bonwick@Sun.COM * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 13588241SJeff.Bonwick@Sun.COM * txgs that was scrubbed. 13598241SJeff.Bonwick@Sun.COM * 13608241SJeff.Bonwick@Sun.COM * DTL_OUTAGE: txgs which cannot currently be read, whether due to 13618241SJeff.Bonwick@Sun.COM * persistent errors or just some device being offline. 13628241SJeff.Bonwick@Sun.COM * Unlike the other three, the DTL_OUTAGE map is not generally 13638241SJeff.Bonwick@Sun.COM * maintained; it's only computed when needed, typically to 13648241SJeff.Bonwick@Sun.COM * determine whether a device can be detached. 13658241SJeff.Bonwick@Sun.COM * 13668241SJeff.Bonwick@Sun.COM * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 13678241SJeff.Bonwick@Sun.COM * either has the data or it doesn't. 13688241SJeff.Bonwick@Sun.COM * 13698241SJeff.Bonwick@Sun.COM * For interior vdevs such as mirror and RAID-Z the picture is more complex. 13708241SJeff.Bonwick@Sun.COM * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 13718241SJeff.Bonwick@Sun.COM * if any child is less than fully replicated, then so is its parent. 13728241SJeff.Bonwick@Sun.COM * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 13738241SJeff.Bonwick@Sun.COM * comprising only those txgs which appear in 'maxfaults' or more children; 13748241SJeff.Bonwick@Sun.COM * those are the txgs we don't have enough replication to read. For example, 13758241SJeff.Bonwick@Sun.COM * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 13768241SJeff.Bonwick@Sun.COM * thus, its DTL_MISSING consists of the set of txgs that appear in more than 13778241SJeff.Bonwick@Sun.COM * two child DTL_MISSING maps. 13788241SJeff.Bonwick@Sun.COM * 13798241SJeff.Bonwick@Sun.COM * It should be clear from the above that to compute the DTLs and outage maps 13808241SJeff.Bonwick@Sun.COM * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 13818241SJeff.Bonwick@Sun.COM * Therefore, that is all we keep on disk. When loading the pool, or after 13828241SJeff.Bonwick@Sun.COM * a configuration change, we generate all other DTLs from first principles. 13838241SJeff.Bonwick@Sun.COM */ 1384789Sahrens void 13858241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1386789Sahrens { 13878241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 13888241SJeff.Bonwick@Sun.COM 13898241SJeff.Bonwick@Sun.COM ASSERT(t < DTL_TYPES); 13908241SJeff.Bonwick@Sun.COM ASSERT(vd != vd->vdev_spa->spa_root_vdev); 13918241SJeff.Bonwick@Sun.COM 1392789Sahrens mutex_enter(sm->sm_lock); 1393789Sahrens if (!space_map_contains(sm, txg, size)) 1394789Sahrens space_map_add(sm, txg, size); 1395789Sahrens mutex_exit(sm->sm_lock); 1396789Sahrens } 1397789Sahrens 13988241SJeff.Bonwick@Sun.COM boolean_t 13998241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1400789Sahrens { 14018241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 14028241SJeff.Bonwick@Sun.COM boolean_t dirty = B_FALSE; 14038241SJeff.Bonwick@Sun.COM 14048241SJeff.Bonwick@Sun.COM ASSERT(t < DTL_TYPES); 14058241SJeff.Bonwick@Sun.COM ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1406789Sahrens 1407789Sahrens mutex_enter(sm->sm_lock); 14088241SJeff.Bonwick@Sun.COM if (sm->sm_space != 0) 14098241SJeff.Bonwick@Sun.COM dirty = space_map_contains(sm, txg, size); 1410789Sahrens mutex_exit(sm->sm_lock); 1411789Sahrens 1412789Sahrens return (dirty); 1413789Sahrens } 1414789Sahrens 14158241SJeff.Bonwick@Sun.COM boolean_t 14168241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 14178241SJeff.Bonwick@Sun.COM { 14188241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 14198241SJeff.Bonwick@Sun.COM boolean_t empty; 14208241SJeff.Bonwick@Sun.COM 14218241SJeff.Bonwick@Sun.COM mutex_enter(sm->sm_lock); 14228241SJeff.Bonwick@Sun.COM empty = (sm->sm_space == 0); 14238241SJeff.Bonwick@Sun.COM mutex_exit(sm->sm_lock); 14248241SJeff.Bonwick@Sun.COM 14258241SJeff.Bonwick@Sun.COM return (empty); 14268241SJeff.Bonwick@Sun.COM } 14278241SJeff.Bonwick@Sun.COM 1428789Sahrens /* 1429789Sahrens * Reassess DTLs after a config change or scrub completion. 1430789Sahrens */ 1431789Sahrens void 1432789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1433789Sahrens { 14341544Seschrock spa_t *spa = vd->vdev_spa; 14358241SJeff.Bonwick@Sun.COM avl_tree_t reftree; 14368241SJeff.Bonwick@Sun.COM int minref; 14378241SJeff.Bonwick@Sun.COM 14388241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 14398241SJeff.Bonwick@Sun.COM 14408241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 14418241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(vd->vdev_child[c], txg, 14428241SJeff.Bonwick@Sun.COM scrub_txg, scrub_done); 14438241SJeff.Bonwick@Sun.COM 14448241SJeff.Bonwick@Sun.COM if (vd == spa->spa_root_vdev) 14458241SJeff.Bonwick@Sun.COM return; 14468241SJeff.Bonwick@Sun.COM 14478241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 1448789Sahrens mutex_enter(&vd->vdev_dtl_lock); 14497046Sahrens if (scrub_txg != 0 && 14507046Sahrens (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) { 14517046Sahrens /* XXX should check scrub_done? */ 14527046Sahrens /* 14537046Sahrens * We completed a scrub up to scrub_txg. If we 14547046Sahrens * did it without rebooting, then the scrub dtl 14557046Sahrens * will be valid, so excise the old region and 14567046Sahrens * fold in the scrub dtl. Otherwise, leave the 14577046Sahrens * dtl as-is if there was an error. 14588241SJeff.Bonwick@Sun.COM * 14598241SJeff.Bonwick@Sun.COM * There's little trick here: to excise the beginning 14608241SJeff.Bonwick@Sun.COM * of the DTL_MISSING map, we put it into a reference 14618241SJeff.Bonwick@Sun.COM * tree and then add a segment with refcnt -1 that 14628241SJeff.Bonwick@Sun.COM * covers the range [0, scrub_txg). This means 14638241SJeff.Bonwick@Sun.COM * that each txg in that range has refcnt -1 or 0. 14648241SJeff.Bonwick@Sun.COM * We then add DTL_SCRUB with a refcnt of 2, so that 14658241SJeff.Bonwick@Sun.COM * entries in the range [0, scrub_txg) will have a 14668241SJeff.Bonwick@Sun.COM * positive refcnt -- either 1 or 2. We then convert 14678241SJeff.Bonwick@Sun.COM * the reference tree into the new DTL_MISSING map. 14687046Sahrens */ 14698241SJeff.Bonwick@Sun.COM space_map_ref_create(&reftree); 14708241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, 14718241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_MISSING], 1); 14728241SJeff.Bonwick@Sun.COM space_map_ref_add_seg(&reftree, 0, scrub_txg, -1); 14738241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, 14748241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_SCRUB], 2); 14758241SJeff.Bonwick@Sun.COM space_map_ref_generate_map(&reftree, 14768241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_MISSING], 1); 14778241SJeff.Bonwick@Sun.COM space_map_ref_destroy(&reftree); 1478789Sahrens } 14798241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 14808241SJeff.Bonwick@Sun.COM space_map_walk(&vd->vdev_dtl[DTL_MISSING], 14818241SJeff.Bonwick@Sun.COM space_map_add, &vd->vdev_dtl[DTL_PARTIAL]); 1482789Sahrens if (scrub_done) 14838241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 14848241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 14858241SJeff.Bonwick@Sun.COM if (!vdev_readable(vd)) 14868241SJeff.Bonwick@Sun.COM space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 14878241SJeff.Bonwick@Sun.COM else 14888241SJeff.Bonwick@Sun.COM space_map_walk(&vd->vdev_dtl[DTL_MISSING], 14898241SJeff.Bonwick@Sun.COM space_map_add, &vd->vdev_dtl[DTL_OUTAGE]); 1490789Sahrens mutex_exit(&vd->vdev_dtl_lock); 14917046Sahrens 14921732Sbonwick if (txg != 0) 14931732Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1494789Sahrens return; 1495789Sahrens } 1496789Sahrens 1497789Sahrens mutex_enter(&vd->vdev_dtl_lock); 14988241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 14998241SJeff.Bonwick@Sun.COM if (t == DTL_SCRUB) 15008241SJeff.Bonwick@Sun.COM continue; /* leaf vdevs only */ 15018241SJeff.Bonwick@Sun.COM if (t == DTL_PARTIAL) 15028241SJeff.Bonwick@Sun.COM minref = 1; /* i.e. non-zero */ 15038241SJeff.Bonwick@Sun.COM else if (vd->vdev_nparity != 0) 15048241SJeff.Bonwick@Sun.COM minref = vd->vdev_nparity + 1; /* RAID-Z */ 15058241SJeff.Bonwick@Sun.COM else 15068241SJeff.Bonwick@Sun.COM minref = vd->vdev_children; /* any kind of mirror */ 15078241SJeff.Bonwick@Sun.COM space_map_ref_create(&reftree); 15088241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 15098241SJeff.Bonwick@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 15108241SJeff.Bonwick@Sun.COM mutex_enter(&cvd->vdev_dtl_lock); 15118241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1); 15128241SJeff.Bonwick@Sun.COM mutex_exit(&cvd->vdev_dtl_lock); 15138241SJeff.Bonwick@Sun.COM } 15148241SJeff.Bonwick@Sun.COM space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref); 15158241SJeff.Bonwick@Sun.COM space_map_ref_destroy(&reftree); 15168241SJeff.Bonwick@Sun.COM } 1517789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1518789Sahrens } 1519789Sahrens 1520789Sahrens static int 1521789Sahrens vdev_dtl_load(vdev_t *vd) 1522789Sahrens { 1523789Sahrens spa_t *spa = vd->vdev_spa; 15248241SJeff.Bonwick@Sun.COM space_map_obj_t *smo = &vd->vdev_dtl_smo; 15251732Sbonwick objset_t *mos = spa->spa_meta_objset; 1526789Sahrens dmu_buf_t *db; 1527789Sahrens int error; 1528789Sahrens 1529789Sahrens ASSERT(vd->vdev_children == 0); 1530789Sahrens 1531789Sahrens if (smo->smo_object == 0) 1532789Sahrens return (0); 1533789Sahrens 15341732Sbonwick if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 15351544Seschrock return (error); 15361732Sbonwick 15374944Smaybee ASSERT3U(db->db_size, >=, sizeof (*smo)); 15384944Smaybee bcopy(db->db_data, smo, sizeof (*smo)); 15391544Seschrock dmu_buf_rele(db, FTAG); 1540789Sahrens 1541789Sahrens mutex_enter(&vd->vdev_dtl_lock); 15428241SJeff.Bonwick@Sun.COM error = space_map_load(&vd->vdev_dtl[DTL_MISSING], 15438241SJeff.Bonwick@Sun.COM NULL, SM_ALLOC, smo, mos); 1544789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1545789Sahrens 1546789Sahrens return (error); 1547789Sahrens } 1548789Sahrens 1549789Sahrens void 1550789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1551789Sahrens { 1552789Sahrens spa_t *spa = vd->vdev_spa; 15538241SJeff.Bonwick@Sun.COM space_map_obj_t *smo = &vd->vdev_dtl_smo; 15548241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[DTL_MISSING]; 15551732Sbonwick objset_t *mos = spa->spa_meta_objset; 1556789Sahrens space_map_t smsync; 1557789Sahrens kmutex_t smlock; 1558789Sahrens dmu_buf_t *db; 1559789Sahrens dmu_tx_t *tx; 1560789Sahrens 1561789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1562789Sahrens 1563789Sahrens if (vd->vdev_detached) { 1564789Sahrens if (smo->smo_object != 0) { 15651732Sbonwick int err = dmu_object_free(mos, smo->smo_object, tx); 1566789Sahrens ASSERT3U(err, ==, 0); 1567789Sahrens smo->smo_object = 0; 1568789Sahrens } 1569789Sahrens dmu_tx_commit(tx); 1570789Sahrens return; 1571789Sahrens } 1572789Sahrens 1573789Sahrens if (smo->smo_object == 0) { 1574789Sahrens ASSERT(smo->smo_objsize == 0); 1575789Sahrens ASSERT(smo->smo_alloc == 0); 15761732Sbonwick smo->smo_object = dmu_object_alloc(mos, 1577789Sahrens DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1578789Sahrens DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1579789Sahrens ASSERT(smo->smo_object != 0); 1580789Sahrens vdev_config_dirty(vd->vdev_top); 1581789Sahrens } 1582789Sahrens 1583789Sahrens mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1584789Sahrens 1585789Sahrens space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1586789Sahrens &smlock); 1587789Sahrens 1588789Sahrens mutex_enter(&smlock); 1589789Sahrens 1590789Sahrens mutex_enter(&vd->vdev_dtl_lock); 15911732Sbonwick space_map_walk(sm, space_map_add, &smsync); 1592789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1593789Sahrens 15941732Sbonwick space_map_truncate(smo, mos, tx); 15951732Sbonwick space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1596789Sahrens 1597789Sahrens space_map_destroy(&smsync); 1598789Sahrens 1599789Sahrens mutex_exit(&smlock); 1600789Sahrens mutex_destroy(&smlock); 1601789Sahrens 16021732Sbonwick VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1603789Sahrens dmu_buf_will_dirty(db, tx); 16044944Smaybee ASSERT3U(db->db_size, >=, sizeof (*smo)); 16054944Smaybee bcopy(smo, db->db_data, sizeof (*smo)); 16061544Seschrock dmu_buf_rele(db, FTAG); 1607789Sahrens 1608789Sahrens dmu_tx_commit(tx); 1609789Sahrens } 1610789Sahrens 16117046Sahrens /* 16128241SJeff.Bonwick@Sun.COM * Determine whether the specified vdev can be offlined/detached/removed 16138241SJeff.Bonwick@Sun.COM * without losing data. 16148241SJeff.Bonwick@Sun.COM */ 16158241SJeff.Bonwick@Sun.COM boolean_t 16168241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd) 16178241SJeff.Bonwick@Sun.COM { 16188241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 16198241SJeff.Bonwick@Sun.COM vdev_t *tvd = vd->vdev_top; 16208241SJeff.Bonwick@Sun.COM uint8_t cant_read = vd->vdev_cant_read; 16218241SJeff.Bonwick@Sun.COM boolean_t required; 16228241SJeff.Bonwick@Sun.COM 16238241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 16248241SJeff.Bonwick@Sun.COM 16258241SJeff.Bonwick@Sun.COM if (vd == spa->spa_root_vdev || vd == tvd) 16268241SJeff.Bonwick@Sun.COM return (B_TRUE); 16278241SJeff.Bonwick@Sun.COM 16288241SJeff.Bonwick@Sun.COM /* 16298241SJeff.Bonwick@Sun.COM * Temporarily mark the device as unreadable, and then determine 16308241SJeff.Bonwick@Sun.COM * whether this results in any DTL outages in the top-level vdev. 16318241SJeff.Bonwick@Sun.COM * If not, we can safely offline/detach/remove the device. 16328241SJeff.Bonwick@Sun.COM */ 16338241SJeff.Bonwick@Sun.COM vd->vdev_cant_read = B_TRUE; 16348241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 16358241SJeff.Bonwick@Sun.COM required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 16368241SJeff.Bonwick@Sun.COM vd->vdev_cant_read = cant_read; 16378241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 16388241SJeff.Bonwick@Sun.COM 16398241SJeff.Bonwick@Sun.COM return (required); 16408241SJeff.Bonwick@Sun.COM } 16418241SJeff.Bonwick@Sun.COM 16428241SJeff.Bonwick@Sun.COM /* 16437046Sahrens * Determine if resilver is needed, and if so the txg range. 16447046Sahrens */ 16457046Sahrens boolean_t 16467046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 16477046Sahrens { 16487046Sahrens boolean_t needed = B_FALSE; 16497046Sahrens uint64_t thismin = UINT64_MAX; 16507046Sahrens uint64_t thismax = 0; 16517046Sahrens 16527046Sahrens if (vd->vdev_children == 0) { 16537046Sahrens mutex_enter(&vd->vdev_dtl_lock); 16548241SJeff.Bonwick@Sun.COM if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 && 16558241SJeff.Bonwick@Sun.COM vdev_writeable(vd)) { 16567046Sahrens space_seg_t *ss; 16577046Sahrens 16588241SJeff.Bonwick@Sun.COM ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root); 16597046Sahrens thismin = ss->ss_start - 1; 16608241SJeff.Bonwick@Sun.COM ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root); 16617046Sahrens thismax = ss->ss_end; 16627046Sahrens needed = B_TRUE; 16637046Sahrens } 16647046Sahrens mutex_exit(&vd->vdev_dtl_lock); 16657046Sahrens } else { 16668241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 16677046Sahrens vdev_t *cvd = vd->vdev_child[c]; 16687046Sahrens uint64_t cmin, cmax; 16697046Sahrens 16707046Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 16717046Sahrens thismin = MIN(thismin, cmin); 16727046Sahrens thismax = MAX(thismax, cmax); 16737046Sahrens needed = B_TRUE; 16747046Sahrens } 16757046Sahrens } 16767046Sahrens } 16777046Sahrens 16787046Sahrens if (needed && minp) { 16797046Sahrens *minp = thismin; 16807046Sahrens *maxp = thismax; 16817046Sahrens } 16827046Sahrens return (needed); 16837046Sahrens } 16847046Sahrens 16851986Seschrock void 16861544Seschrock vdev_load(vdev_t *vd) 1687789Sahrens { 1688789Sahrens /* 1689789Sahrens * Recursively load all children. 1690789Sahrens */ 16918241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 16921986Seschrock vdev_load(vd->vdev_child[c]); 1693789Sahrens 1694789Sahrens /* 16951585Sbonwick * If this is a top-level vdev, initialize its metaslabs. 1696789Sahrens */ 16971986Seschrock if (vd == vd->vdev_top && 16981986Seschrock (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 16991986Seschrock vdev_metaslab_init(vd, 0) != 0)) 17001986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17011986Seschrock VDEV_AUX_CORRUPT_DATA); 1702789Sahrens 1703789Sahrens /* 1704789Sahrens * If this is a leaf vdev, load its DTL. 1705789Sahrens */ 17061986Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 17071986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17081986Seschrock VDEV_AUX_CORRUPT_DATA); 1709789Sahrens } 1710789Sahrens 17112082Seschrock /* 17125450Sbrendan * The special vdev case is used for hot spares and l2cache devices. Its 17135450Sbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 17145450Sbrendan * we make sure that we can open the underlying device, then try to read the 17155450Sbrendan * label, and make sure that the label is sane and that it hasn't been 17165450Sbrendan * repurposed to another pool. 17172082Seschrock */ 17182082Seschrock int 17195450Sbrendan vdev_validate_aux(vdev_t *vd) 17202082Seschrock { 17212082Seschrock nvlist_t *label; 17222082Seschrock uint64_t guid, version; 17232082Seschrock uint64_t state; 17242082Seschrock 17257754SJeff.Bonwick@Sun.COM if (!vdev_readable(vd)) 17266643Seschrock return (0); 17276643Seschrock 17282082Seschrock if ((label = vdev_label_read_config(vd)) == NULL) { 17292082Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 17302082Seschrock VDEV_AUX_CORRUPT_DATA); 17312082Seschrock return (-1); 17322082Seschrock } 17332082Seschrock 17342082Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 17354577Sahrens version > SPA_VERSION || 17362082Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 17372082Seschrock guid != vd->vdev_guid || 17382082Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 17392082Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 17402082Seschrock VDEV_AUX_CORRUPT_DATA); 17412082Seschrock nvlist_free(label); 17422082Seschrock return (-1); 17432082Seschrock } 17442082Seschrock 17452082Seschrock /* 17462082Seschrock * We don't actually check the pool state here. If it's in fact in 17472082Seschrock * use by another pool, we update this fact on the fly when requested. 17482082Seschrock */ 17492082Seschrock nvlist_free(label); 17502082Seschrock return (0); 17512082Seschrock } 17522082Seschrock 1753789Sahrens void 1754789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 1755789Sahrens { 1756789Sahrens metaslab_t *msp; 1757789Sahrens 1758789Sahrens while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1759789Sahrens metaslab_sync_done(msp, txg); 1760789Sahrens } 1761789Sahrens 1762789Sahrens void 1763789Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 1764789Sahrens { 1765789Sahrens spa_t *spa = vd->vdev_spa; 1766789Sahrens vdev_t *lvd; 1767789Sahrens metaslab_t *msp; 17681732Sbonwick dmu_tx_t *tx; 1769789Sahrens 17701732Sbonwick if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 17711732Sbonwick ASSERT(vd == vd->vdev_top); 17721732Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 17731732Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 17741732Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 17751732Sbonwick ASSERT(vd->vdev_ms_array != 0); 17761732Sbonwick vdev_config_dirty(vd); 17771732Sbonwick dmu_tx_commit(tx); 17781732Sbonwick } 1779789Sahrens 17801732Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 1781789Sahrens metaslab_sync(msp, txg); 17821732Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 17831732Sbonwick } 1784789Sahrens 1785789Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 1786789Sahrens vdev_dtl_sync(lvd, txg); 1787789Sahrens 1788789Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 1789789Sahrens } 1790789Sahrens 1791789Sahrens uint64_t 1792789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 1793789Sahrens { 1794789Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 1795789Sahrens } 1796789Sahrens 17974451Seschrock /* 17984451Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 17994451Seschrock * not be opened, and no I/O is attempted. 18004451Seschrock */ 1801789Sahrens int 18024451Seschrock vdev_fault(spa_t *spa, uint64_t guid) 18034451Seschrock { 18046643Seschrock vdev_t *vd; 18054451Seschrock 18067754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 18074451Seschrock 18086643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 18097754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 18107754SJeff.Bonwick@Sun.COM 18114451Seschrock if (!vd->vdev_ops->vdev_op_leaf) 18127754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 18134451Seschrock 18144451Seschrock /* 18154451Seschrock * Faulted state takes precedence over degraded. 18164451Seschrock */ 18174451Seschrock vd->vdev_faulted = 1ULL; 18184451Seschrock vd->vdev_degraded = 0ULL; 18197754SJeff.Bonwick@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED); 18204451Seschrock 18214451Seschrock /* 18228123SDavid.Marker@sun.com * If marking the vdev as faulted cause the top-level vdev to become 18234451Seschrock * unavailable, then back off and simply mark the vdev as degraded 18244451Seschrock * instead. 18254451Seschrock */ 18266643Seschrock if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) { 18274451Seschrock vd->vdev_degraded = 1ULL; 18284451Seschrock vd->vdev_faulted = 0ULL; 18294451Seschrock 18304451Seschrock /* 18314451Seschrock * If we reopen the device and it's not dead, only then do we 18324451Seschrock * mark it degraded. 18334451Seschrock */ 18344451Seschrock vdev_reopen(vd); 18354451Seschrock 18365329Sgw25295 if (vdev_readable(vd)) { 18374451Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 18384451Seschrock VDEV_AUX_ERR_EXCEEDED); 18394451Seschrock } 18404451Seschrock } 18414451Seschrock 18427754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 18434451Seschrock } 18444451Seschrock 18454451Seschrock /* 18464451Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 18474451Seschrock * user that something is wrong. The vdev continues to operate as normal as far 18484451Seschrock * as I/O is concerned. 18494451Seschrock */ 18504451Seschrock int 18514451Seschrock vdev_degrade(spa_t *spa, uint64_t guid) 18524451Seschrock { 18536643Seschrock vdev_t *vd; 18544451Seschrock 18557754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 18564451Seschrock 18576643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 18587754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 18597754SJeff.Bonwick@Sun.COM 18604451Seschrock if (!vd->vdev_ops->vdev_op_leaf) 18617754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 18624451Seschrock 18634451Seschrock /* 18644451Seschrock * If the vdev is already faulted, then don't do anything. 18654451Seschrock */ 18667754SJeff.Bonwick@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded) 18677754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, 0)); 18684451Seschrock 18694451Seschrock vd->vdev_degraded = 1ULL; 18704451Seschrock if (!vdev_is_dead(vd)) 18714451Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 18724451Seschrock VDEV_AUX_ERR_EXCEEDED); 18734451Seschrock 18747754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 18754451Seschrock } 18764451Seschrock 18774451Seschrock /* 18784451Seschrock * Online the given vdev. If 'unspare' is set, it implies two things. First, 18794451Seschrock * any attached spare device should be detached when the device finishes 18804451Seschrock * resilvering. Second, the online should be treated like a 'test' online case, 18814451Seschrock * so no FMA events are generated if the device fails to open. 18824451Seschrock */ 18834451Seschrock int 18847754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 1885789Sahrens { 18866643Seschrock vdev_t *vd; 1887789Sahrens 18887754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 18891485Slling 18906643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 18917754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 1892789Sahrens 18931585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 18947754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 18951585Sbonwick 1896789Sahrens vd->vdev_offline = B_FALSE; 18971485Slling vd->vdev_tmpoffline = B_FALSE; 18987754SJeff.Bonwick@Sun.COM vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 18997754SJeff.Bonwick@Sun.COM vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 19001544Seschrock vdev_reopen(vd->vdev_top); 19014451Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 19024451Seschrock 19034451Seschrock if (newstate) 19044451Seschrock *newstate = vd->vdev_state; 19054451Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 19064451Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 19074451Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 19084451Seschrock vd->vdev_parent->vdev_child[0] == vd) 19094451Seschrock vd->vdev_unspare = B_TRUE; 1910789Sahrens 19118241SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 1912789Sahrens } 1913789Sahrens 1914789Sahrens int 19154451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 1916789Sahrens { 19176643Seschrock vdev_t *vd; 1918789Sahrens 19197754SJeff.Bonwick@Sun.COM spa_vdev_state_enter(spa); 1920789Sahrens 19216643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 19227754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 1923789Sahrens 19241585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 19257754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 19261585Sbonwick 1927789Sahrens /* 19281732Sbonwick * If the device isn't already offline, try to offline it. 1929789Sahrens */ 19301732Sbonwick if (!vd->vdev_offline) { 19311732Sbonwick /* 19328241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 19338241SJeff.Bonwick@Sun.COM * don't allow it to be offlined. 19341732Sbonwick */ 19358241SJeff.Bonwick@Sun.COM if (vd->vdev_aux == NULL && vdev_dtl_required(vd)) 19367754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, EBUSY)); 1937789Sahrens 19381732Sbonwick /* 19391732Sbonwick * Offline this device and reopen its top-level vdev. 19401732Sbonwick * If this action results in the top-level vdev becoming 19411732Sbonwick * unusable, undo it and fail the request. 19421732Sbonwick */ 19431732Sbonwick vd->vdev_offline = B_TRUE; 19441544Seschrock vdev_reopen(vd->vdev_top); 19458241SJeff.Bonwick@Sun.COM if (vd->vdev_aux == NULL && vdev_is_dead(vd->vdev_top)) { 19461732Sbonwick vd->vdev_offline = B_FALSE; 19471732Sbonwick vdev_reopen(vd->vdev_top); 19487754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, EBUSY)); 19491732Sbonwick } 1950789Sahrens } 1951789Sahrens 19527754SJeff.Bonwick@Sun.COM vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 19531732Sbonwick 19547754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 1955789Sahrens } 1956789Sahrens 19571544Seschrock /* 19581544Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 19591544Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 19601544Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 19611544Seschrock */ 19621544Seschrock void 19637754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd) 1964789Sahrens { 19657754SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 19667754SJeff.Bonwick@Sun.COM 19677754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1968789Sahrens 19691544Seschrock if (vd == NULL) 19707754SJeff.Bonwick@Sun.COM vd = rvd; 1971789Sahrens 19721544Seschrock vd->vdev_stat.vs_read_errors = 0; 19731544Seschrock vd->vdev_stat.vs_write_errors = 0; 19741544Seschrock vd->vdev_stat.vs_checksum_errors = 0; 1975789Sahrens 19767754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 19777754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd->vdev_child[c]); 19784451Seschrock 19794451Seschrock /* 19806959Sek110237 * If we're in the FAULTED state or have experienced failed I/O, then 19816959Sek110237 * clear the persistent state and attempt to reopen the device. We 19826959Sek110237 * also mark the vdev config dirty, so that the new faulted state is 19836959Sek110237 * written out to disk. 19844451Seschrock */ 19857754SJeff.Bonwick@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded || 19867754SJeff.Bonwick@Sun.COM !vdev_readable(vd) || !vdev_writeable(vd)) { 19876959Sek110237 19884451Seschrock vd->vdev_faulted = vd->vdev_degraded = 0; 19897754SJeff.Bonwick@Sun.COM vd->vdev_cant_read = B_FALSE; 19907754SJeff.Bonwick@Sun.COM vd->vdev_cant_write = B_FALSE; 19917754SJeff.Bonwick@Sun.COM 19924451Seschrock vdev_reopen(vd); 19934451Seschrock 19947754SJeff.Bonwick@Sun.COM if (vd != rvd) 19957754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 19967754SJeff.Bonwick@Sun.COM 19977754SJeff.Bonwick@Sun.COM if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 19984808Sek110237 spa_async_request(spa, SPA_ASYNC_RESILVER); 19994451Seschrock 20004451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 20014451Seschrock } 2002789Sahrens } 2003789Sahrens 20047754SJeff.Bonwick@Sun.COM boolean_t 20057754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd) 20065329Sgw25295 { 20077754SJeff.Bonwick@Sun.COM return (vd->vdev_state < VDEV_STATE_DEGRADED); 20085329Sgw25295 } 20095329Sgw25295 20107754SJeff.Bonwick@Sun.COM boolean_t 20117754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd) 2012789Sahrens { 20137754SJeff.Bonwick@Sun.COM return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2014789Sahrens } 2015789Sahrens 20167754SJeff.Bonwick@Sun.COM boolean_t 20177754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd) 2018789Sahrens { 20197754SJeff.Bonwick@Sun.COM return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 20207754SJeff.Bonwick@Sun.COM } 2021789Sahrens 20227754SJeff.Bonwick@Sun.COM boolean_t 20237980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd) 20247980SGeorge.Wilson@Sun.COM { 20258241SJeff.Bonwick@Sun.COM uint64_t state = vd->vdev_state; 20268241SJeff.Bonwick@Sun.COM 20277980SGeorge.Wilson@Sun.COM /* 20288241SJeff.Bonwick@Sun.COM * We currently allow allocations from vdevs which may be in the 20297980SGeorge.Wilson@Sun.COM * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 20307980SGeorge.Wilson@Sun.COM * fails to reopen then we'll catch it later when we're holding 20318241SJeff.Bonwick@Sun.COM * the proper locks. Note that we have to get the vdev state 20328241SJeff.Bonwick@Sun.COM * in a local variable because although it changes atomically, 20338241SJeff.Bonwick@Sun.COM * we're asking two separate questions about it. 20347980SGeorge.Wilson@Sun.COM */ 20358241SJeff.Bonwick@Sun.COM return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 20367980SGeorge.Wilson@Sun.COM !vd->vdev_cant_write); 20377980SGeorge.Wilson@Sun.COM } 20387980SGeorge.Wilson@Sun.COM 20397980SGeorge.Wilson@Sun.COM boolean_t 20407754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio) 20417754SJeff.Bonwick@Sun.COM { 20427754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd == vd); 2043789Sahrens 20447754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 20457754SJeff.Bonwick@Sun.COM return (B_FALSE); 2046789Sahrens 20477754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_READ) 20487754SJeff.Bonwick@Sun.COM return (!vd->vdev_cant_read); 2049789Sahrens 20507754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_WRITE) 20517754SJeff.Bonwick@Sun.COM return (!vd->vdev_cant_write); 20527754SJeff.Bonwick@Sun.COM 20537754SJeff.Bonwick@Sun.COM return (B_TRUE); 2054789Sahrens } 2055789Sahrens 2056789Sahrens /* 2057789Sahrens * Get statistics for the given vdev. 2058789Sahrens */ 2059789Sahrens void 2060789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2061789Sahrens { 2062789Sahrens vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 2063789Sahrens 2064789Sahrens mutex_enter(&vd->vdev_stat_lock); 2065789Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 20667046Sahrens vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors; 2067789Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2068789Sahrens vs->vs_state = vd->vdev_state; 20691175Slling vs->vs_rsize = vdev_get_rsize(vd); 2070789Sahrens mutex_exit(&vd->vdev_stat_lock); 2071789Sahrens 2072789Sahrens /* 2073789Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 2074789Sahrens * over all top-level vdevs (i.e. the direct children of the root). 2075789Sahrens */ 2076789Sahrens if (vd == rvd) { 20777754SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 2078789Sahrens vdev_t *cvd = rvd->vdev_child[c]; 2079789Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 2080789Sahrens 2081789Sahrens mutex_enter(&vd->vdev_stat_lock); 20827754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 2083789Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 2084789Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 2085789Sahrens } 2086789Sahrens vs->vs_scrub_examined += cvs->vs_scrub_examined; 2087789Sahrens mutex_exit(&vd->vdev_stat_lock); 2088789Sahrens } 2089789Sahrens } 2090789Sahrens } 2091789Sahrens 2092789Sahrens void 20935450Sbrendan vdev_clear_stats(vdev_t *vd) 20945450Sbrendan { 20955450Sbrendan mutex_enter(&vd->vdev_stat_lock); 20965450Sbrendan vd->vdev_stat.vs_space = 0; 20975450Sbrendan vd->vdev_stat.vs_dspace = 0; 20985450Sbrendan vd->vdev_stat.vs_alloc = 0; 20995450Sbrendan mutex_exit(&vd->vdev_stat_lock); 21005450Sbrendan } 21015450Sbrendan 21025450Sbrendan void 21037754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize) 2104789Sahrens { 21058241SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 21068241SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 21077754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2108789Sahrens vdev_t *pvd; 2109789Sahrens uint64_t txg = zio->io_txg; 2110789Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2111789Sahrens zio_type_t type = zio->io_type; 2112789Sahrens int flags = zio->io_flags; 2113789Sahrens 21147754SJeff.Bonwick@Sun.COM /* 21157754SJeff.Bonwick@Sun.COM * If this i/o is a gang leader, it didn't do any actual work. 21167754SJeff.Bonwick@Sun.COM */ 21177754SJeff.Bonwick@Sun.COM if (zio->io_gang_tree) 21187754SJeff.Bonwick@Sun.COM return; 21197754SJeff.Bonwick@Sun.COM 2120789Sahrens if (zio->io_error == 0) { 21217754SJeff.Bonwick@Sun.COM /* 21227754SJeff.Bonwick@Sun.COM * If this is a root i/o, don't count it -- we've already 21237754SJeff.Bonwick@Sun.COM * counted the top-level vdevs, and vdev_get_stats() will 21247754SJeff.Bonwick@Sun.COM * aggregate them when asked. This reduces contention on 21257754SJeff.Bonwick@Sun.COM * the root vdev_stat_lock and implicitly handles blocks 21267754SJeff.Bonwick@Sun.COM * that compress away to holes, for which there is no i/o. 21277754SJeff.Bonwick@Sun.COM * (Holes never create vdev children, so all the counters 21287754SJeff.Bonwick@Sun.COM * remain zero, which is what we want.) 21297754SJeff.Bonwick@Sun.COM * 21307754SJeff.Bonwick@Sun.COM * Note: this only applies to successful i/o (io_error == 0) 21317754SJeff.Bonwick@Sun.COM * because unlike i/o counts, errors are not additive. 21327754SJeff.Bonwick@Sun.COM * When reading a ditto block, for example, failure of 21337754SJeff.Bonwick@Sun.COM * one top-level vdev does not imply a root-level error. 21347754SJeff.Bonwick@Sun.COM */ 21357754SJeff.Bonwick@Sun.COM if (vd == rvd) 21367754SJeff.Bonwick@Sun.COM return; 21377754SJeff.Bonwick@Sun.COM 21387754SJeff.Bonwick@Sun.COM ASSERT(vd == zio->io_vd); 21398241SJeff.Bonwick@Sun.COM 21408241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_IO_BYPASS) 21418241SJeff.Bonwick@Sun.COM return; 21428241SJeff.Bonwick@Sun.COM 21438241SJeff.Bonwick@Sun.COM mutex_enter(&vd->vdev_stat_lock); 21448241SJeff.Bonwick@Sun.COM 21457754SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_IO_REPAIR) { 21461807Sbonwick if (flags & ZIO_FLAG_SCRUB_THREAD) 21477754SJeff.Bonwick@Sun.COM vs->vs_scrub_repaired += psize; 21488241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_SELF_HEAL) 21497754SJeff.Bonwick@Sun.COM vs->vs_self_healed += psize; 2150789Sahrens } 21518241SJeff.Bonwick@Sun.COM 21528241SJeff.Bonwick@Sun.COM vs->vs_ops[type]++; 21538241SJeff.Bonwick@Sun.COM vs->vs_bytes[type] += psize; 21548241SJeff.Bonwick@Sun.COM 21558241SJeff.Bonwick@Sun.COM mutex_exit(&vd->vdev_stat_lock); 2156789Sahrens return; 2157789Sahrens } 2158789Sahrens 2159789Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 2160789Sahrens return; 2161789Sahrens 21627754SJeff.Bonwick@Sun.COM mutex_enter(&vd->vdev_stat_lock); 21639230SGeorge.Wilson@Sun.COM if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 21647754SJeff.Bonwick@Sun.COM if (zio->io_error == ECKSUM) 21657754SJeff.Bonwick@Sun.COM vs->vs_checksum_errors++; 21667754SJeff.Bonwick@Sun.COM else 21677754SJeff.Bonwick@Sun.COM vs->vs_read_errors++; 2168789Sahrens } 21699230SGeorge.Wilson@Sun.COM if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 21707754SJeff.Bonwick@Sun.COM vs->vs_write_errors++; 21717754SJeff.Bonwick@Sun.COM mutex_exit(&vd->vdev_stat_lock); 2172789Sahrens 21738241SJeff.Bonwick@Sun.COM if (type == ZIO_TYPE_WRITE && txg != 0 && 21748241SJeff.Bonwick@Sun.COM (!(flags & ZIO_FLAG_IO_REPAIR) || 21758241SJeff.Bonwick@Sun.COM (flags & ZIO_FLAG_SCRUB_THREAD))) { 21768241SJeff.Bonwick@Sun.COM /* 21778241SJeff.Bonwick@Sun.COM * This is either a normal write (not a repair), or it's a 21788241SJeff.Bonwick@Sun.COM * repair induced by the scrub thread. In the normal case, 21798241SJeff.Bonwick@Sun.COM * we commit the DTL change in the same txg as the block 21808241SJeff.Bonwick@Sun.COM * was born. In the scrub-induced repair case, we know that 21818241SJeff.Bonwick@Sun.COM * scrubs run in first-pass syncing context, so we commit 21828241SJeff.Bonwick@Sun.COM * the DTL change in spa->spa_syncing_txg. 21838241SJeff.Bonwick@Sun.COM * 21848241SJeff.Bonwick@Sun.COM * We currently do not make DTL entries for failed spontaneous 21858241SJeff.Bonwick@Sun.COM * self-healing writes triggered by normal (non-scrubbing) 21868241SJeff.Bonwick@Sun.COM * reads, because we have no transactional context in which to 21878241SJeff.Bonwick@Sun.COM * do so -- and it's not clear that it'd be desirable anyway. 21888241SJeff.Bonwick@Sun.COM */ 21898241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 21908241SJeff.Bonwick@Sun.COM uint64_t commit_txg = txg; 21918241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_SCRUB_THREAD) { 21928241SJeff.Bonwick@Sun.COM ASSERT(flags & ZIO_FLAG_IO_REPAIR); 21938241SJeff.Bonwick@Sun.COM ASSERT(spa_sync_pass(spa) == 1); 21948241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 21958241SJeff.Bonwick@Sun.COM commit_txg = spa->spa_syncing_txg; 21968241SJeff.Bonwick@Sun.COM } 21978241SJeff.Bonwick@Sun.COM ASSERT(commit_txg >= spa->spa_syncing_txg); 21988241SJeff.Bonwick@Sun.COM if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 21998241SJeff.Bonwick@Sun.COM return; 22008241SJeff.Bonwick@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 22018241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 22028241SJeff.Bonwick@Sun.COM vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2203789Sahrens } 22048241SJeff.Bonwick@Sun.COM if (vd != rvd) 22058241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2206789Sahrens } 2207789Sahrens } 2208789Sahrens 2209789Sahrens void 2210789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 2211789Sahrens { 2212789Sahrens int c; 2213789Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2214789Sahrens 2215789Sahrens for (c = 0; c < vd->vdev_children; c++) 2216789Sahrens vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 2217789Sahrens 2218789Sahrens mutex_enter(&vd->vdev_stat_lock); 2219789Sahrens 2220789Sahrens if (type == POOL_SCRUB_NONE) { 2221789Sahrens /* 2222789Sahrens * Update completion and end time. Leave everything else alone 2223789Sahrens * so we can report what happened during the previous scrub. 2224789Sahrens */ 2225789Sahrens vs->vs_scrub_complete = complete; 2226789Sahrens vs->vs_scrub_end = gethrestime_sec(); 2227789Sahrens } else { 2228789Sahrens vs->vs_scrub_type = type; 2229789Sahrens vs->vs_scrub_complete = 0; 2230789Sahrens vs->vs_scrub_examined = 0; 2231789Sahrens vs->vs_scrub_repaired = 0; 2232789Sahrens vs->vs_scrub_start = gethrestime_sec(); 2233789Sahrens vs->vs_scrub_end = 0; 2234789Sahrens } 2235789Sahrens 2236789Sahrens mutex_exit(&vd->vdev_stat_lock); 2237789Sahrens } 2238789Sahrens 2239789Sahrens /* 2240789Sahrens * Update the in-core space usage stats for this vdev and the root vdev. 2241789Sahrens */ 2242789Sahrens void 22435450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta, 22445450Sbrendan boolean_t update_root) 2245789Sahrens { 22464527Sperrin int64_t dspace_delta = space_delta; 22474527Sperrin spa_t *spa = vd->vdev_spa; 22484527Sperrin vdev_t *rvd = spa->spa_root_vdev; 22494527Sperrin 2250789Sahrens ASSERT(vd == vd->vdev_top); 22514527Sperrin 22524527Sperrin /* 22534527Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 22544527Sperrin * factor. We must calculate this here and not at the root vdev 22554527Sperrin * because the root vdev's psize-to-asize is simply the max of its 22564527Sperrin * childrens', thus not accurate enough for us. 22574527Sperrin */ 22584527Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 22594527Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 22604527Sperrin vd->vdev_deflate_ratio; 2261789Sahrens 22624527Sperrin mutex_enter(&vd->vdev_stat_lock); 22634527Sperrin vd->vdev_stat.vs_space += space_delta; 22644527Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 22654527Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 22664527Sperrin mutex_exit(&vd->vdev_stat_lock); 22672082Seschrock 22685450Sbrendan if (update_root) { 22695450Sbrendan ASSERT(rvd == vd->vdev_parent); 22705450Sbrendan ASSERT(vd->vdev_ms_count != 0); 22714527Sperrin 22725450Sbrendan /* 22735450Sbrendan * Don't count non-normal (e.g. intent log) space as part of 22745450Sbrendan * the pool's capacity. 22755450Sbrendan */ 22765450Sbrendan if (vd->vdev_mg->mg_class != spa->spa_normal_class) 22775450Sbrendan return; 22785450Sbrendan 22795450Sbrendan mutex_enter(&rvd->vdev_stat_lock); 22805450Sbrendan rvd->vdev_stat.vs_space += space_delta; 22815450Sbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 22825450Sbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 22835450Sbrendan mutex_exit(&rvd->vdev_stat_lock); 22845450Sbrendan } 2285789Sahrens } 2286789Sahrens 2287789Sahrens /* 2288789Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 2289789Sahrens * so that it will be written out next time the vdev configuration is synced. 2290789Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 2291789Sahrens */ 2292789Sahrens void 2293789Sahrens vdev_config_dirty(vdev_t *vd) 2294789Sahrens { 2295789Sahrens spa_t *spa = vd->vdev_spa; 2296789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2297789Sahrens int c; 2298789Sahrens 22991601Sbonwick /* 2300*9425SEric.Schrock@Sun.COM * If this is an aux vdev (as with l2cache and spare devices), then we 2301*9425SEric.Schrock@Sun.COM * update the vdev config manually and set the sync flag. 23026643Seschrock */ 23036643Seschrock if (vd->vdev_aux != NULL) { 23046643Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 23056643Seschrock nvlist_t **aux; 23066643Seschrock uint_t naux; 23076643Seschrock 23086643Seschrock for (c = 0; c < sav->sav_count; c++) { 23096643Seschrock if (sav->sav_vdevs[c] == vd) 23106643Seschrock break; 23116643Seschrock } 23126643Seschrock 23137754SJeff.Bonwick@Sun.COM if (c == sav->sav_count) { 23147754SJeff.Bonwick@Sun.COM /* 23157754SJeff.Bonwick@Sun.COM * We're being removed. There's nothing more to do. 23167754SJeff.Bonwick@Sun.COM */ 23177754SJeff.Bonwick@Sun.COM ASSERT(sav->sav_sync == B_TRUE); 23187754SJeff.Bonwick@Sun.COM return; 23197754SJeff.Bonwick@Sun.COM } 23207754SJeff.Bonwick@Sun.COM 23216643Seschrock sav->sav_sync = B_TRUE; 23226643Seschrock 2323*9425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(sav->sav_config, 2324*9425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 2325*9425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 2326*9425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 2327*9425SEric.Schrock@Sun.COM } 23286643Seschrock 23296643Seschrock ASSERT(c < naux); 23306643Seschrock 23316643Seschrock /* 23326643Seschrock * Setting the nvlist in the middle if the array is a little 23336643Seschrock * sketchy, but it will work. 23346643Seschrock */ 23356643Seschrock nvlist_free(aux[c]); 23366643Seschrock aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE); 23376643Seschrock 23386643Seschrock return; 23396643Seschrock } 23406643Seschrock 23416643Seschrock /* 23427754SJeff.Bonwick@Sun.COM * The dirty list is protected by the SCL_CONFIG lock. The caller 23437754SJeff.Bonwick@Sun.COM * must either hold SCL_CONFIG as writer, or must be the sync thread 23447754SJeff.Bonwick@Sun.COM * (which holds SCL_CONFIG as reader). There's only one sync thread, 23451601Sbonwick * so this is sufficient to ensure mutual exclusion. 23461601Sbonwick */ 23477754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 23487754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 23497754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_CONFIG, RW_READER))); 23501601Sbonwick 2351789Sahrens if (vd == rvd) { 2352789Sahrens for (c = 0; c < rvd->vdev_children; c++) 2353789Sahrens vdev_config_dirty(rvd->vdev_child[c]); 2354789Sahrens } else { 2355789Sahrens ASSERT(vd == vd->vdev_top); 2356789Sahrens 23577754SJeff.Bonwick@Sun.COM if (!list_link_active(&vd->vdev_config_dirty_node)) 23587754SJeff.Bonwick@Sun.COM list_insert_head(&spa->spa_config_dirty_list, vd); 2359789Sahrens } 2360789Sahrens } 2361789Sahrens 2362789Sahrens void 2363789Sahrens vdev_config_clean(vdev_t *vd) 2364789Sahrens { 23651601Sbonwick spa_t *spa = vd->vdev_spa; 23661601Sbonwick 23677754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 23687754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 23697754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_CONFIG, RW_READER))); 23707754SJeff.Bonwick@Sun.COM 23717754SJeff.Bonwick@Sun.COM ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 23727754SJeff.Bonwick@Sun.COM list_remove(&spa->spa_config_dirty_list, vd); 23737754SJeff.Bonwick@Sun.COM } 23747754SJeff.Bonwick@Sun.COM 23757754SJeff.Bonwick@Sun.COM /* 23767754SJeff.Bonwick@Sun.COM * Mark a top-level vdev's state as dirty, so that the next pass of 23777754SJeff.Bonwick@Sun.COM * spa_sync() can convert this into vdev_config_dirty(). We distinguish 23787754SJeff.Bonwick@Sun.COM * the state changes from larger config changes because they require 23797754SJeff.Bonwick@Sun.COM * much less locking, and are often needed for administrative actions. 23807754SJeff.Bonwick@Sun.COM */ 23817754SJeff.Bonwick@Sun.COM void 23827754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd) 23837754SJeff.Bonwick@Sun.COM { 23847754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 23857754SJeff.Bonwick@Sun.COM 23867754SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 23871601Sbonwick 23887754SJeff.Bonwick@Sun.COM /* 23897754SJeff.Bonwick@Sun.COM * The state list is protected by the SCL_STATE lock. The caller 23907754SJeff.Bonwick@Sun.COM * must either hold SCL_STATE as writer, or must be the sync thread 23917754SJeff.Bonwick@Sun.COM * (which holds SCL_STATE as reader). There's only one sync thread, 23927754SJeff.Bonwick@Sun.COM * so this is sufficient to ensure mutual exclusion. 23937754SJeff.Bonwick@Sun.COM */ 23947754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 23957754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 23967754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_STATE, RW_READER))); 23977754SJeff.Bonwick@Sun.COM 23987754SJeff.Bonwick@Sun.COM if (!list_link_active(&vd->vdev_state_dirty_node)) 23997754SJeff.Bonwick@Sun.COM list_insert_head(&spa->spa_state_dirty_list, vd); 24007754SJeff.Bonwick@Sun.COM } 24017754SJeff.Bonwick@Sun.COM 24027754SJeff.Bonwick@Sun.COM void 24037754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd) 24047754SJeff.Bonwick@Sun.COM { 24057754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 24067754SJeff.Bonwick@Sun.COM 24077754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 24087754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 24097754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_STATE, RW_READER))); 24107754SJeff.Bonwick@Sun.COM 24117754SJeff.Bonwick@Sun.COM ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 24127754SJeff.Bonwick@Sun.COM list_remove(&spa->spa_state_dirty_list, vd); 2413789Sahrens } 2414789Sahrens 24156523Sek110237 /* 24166523Sek110237 * Propagate vdev state up from children to parent. 24176523Sek110237 */ 24181775Sbillm void 24191775Sbillm vdev_propagate_state(vdev_t *vd) 24201775Sbillm { 24218241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 24228241SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 24231775Sbillm int degraded = 0, faulted = 0; 24241775Sbillm int corrupted = 0; 24251775Sbillm int c; 24261775Sbillm vdev_t *child; 24271775Sbillm 24284451Seschrock if (vd->vdev_children > 0) { 24294451Seschrock for (c = 0; c < vd->vdev_children; c++) { 24304451Seschrock child = vd->vdev_child[c]; 24316976Seschrock 24327754SJeff.Bonwick@Sun.COM if (!vdev_readable(child) || 24338241SJeff.Bonwick@Sun.COM (!vdev_writeable(child) && spa_writeable(spa))) { 24346976Seschrock /* 24356976Seschrock * Root special: if there is a top-level log 24366976Seschrock * device, treat the root vdev as if it were 24376976Seschrock * degraded. 24386976Seschrock */ 24396976Seschrock if (child->vdev_islog && vd == rvd) 24406976Seschrock degraded++; 24416976Seschrock else 24426976Seschrock faulted++; 24436976Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 24444451Seschrock degraded++; 24456976Seschrock } 24464451Seschrock 24474451Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 24484451Seschrock corrupted++; 24494451Seschrock } 24501775Sbillm 24514451Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 24524451Seschrock 24534451Seschrock /* 24547754SJeff.Bonwick@Sun.COM * Root special: if there is a top-level vdev that cannot be 24554451Seschrock * opened due to corrupted metadata, then propagate the root 24564451Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 24574451Seschrock * replicas'. 24584451Seschrock */ 24594451Seschrock if (corrupted && vd == rvd && 24604451Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 24614451Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 24624451Seschrock VDEV_AUX_CORRUPT_DATA); 24631775Sbillm } 24641775Sbillm 24656976Seschrock if (vd->vdev_parent) 24664451Seschrock vdev_propagate_state(vd->vdev_parent); 24671775Sbillm } 24681775Sbillm 2469789Sahrens /* 24701544Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 24711544Seschrock * state, because we're in the process of opening children depth-first. 24721544Seschrock * Otherwise, we propagate the change to the parent. 24731544Seschrock * 24741544Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 24751544Seschrock * generated. 2476789Sahrens */ 2477789Sahrens void 24781544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 2479789Sahrens { 24801986Seschrock uint64_t save_state; 24816643Seschrock spa_t *spa = vd->vdev_spa; 24821544Seschrock 24831544Seschrock if (state == vd->vdev_state) { 24841544Seschrock vd->vdev_stat.vs_aux = aux; 2485789Sahrens return; 24861544Seschrock } 24871544Seschrock 24881986Seschrock save_state = vd->vdev_state; 2489789Sahrens 2490789Sahrens vd->vdev_state = state; 2491789Sahrens vd->vdev_stat.vs_aux = aux; 2492789Sahrens 24934451Seschrock /* 24944451Seschrock * If we are setting the vdev state to anything but an open state, then 24954451Seschrock * always close the underlying device. Otherwise, we keep accessible 24964451Seschrock * but invalid devices open forever. We don't call vdev_close() itself, 24974451Seschrock * because that implies some extra checks (offline, etc) that we don't 24984451Seschrock * want here. This is limited to leaf devices, because otherwise 24994451Seschrock * closing the device will affect other children. 25004451Seschrock */ 25017780SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf) 25024451Seschrock vd->vdev_ops->vdev_op_close(vd); 25034451Seschrock 25044451Seschrock if (vd->vdev_removed && 25054451Seschrock state == VDEV_STATE_CANT_OPEN && 25064451Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 25074451Seschrock /* 25084451Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 25094451Seschrock * device was previously marked removed and someone attempted to 25104451Seschrock * reopen it. If this failed due to a nonexistent device, then 25114451Seschrock * keep the device in the REMOVED state. We also let this be if 25124451Seschrock * it is one of our special test online cases, which is only 25134451Seschrock * attempting to online the device and shouldn't generate an FMA 25144451Seschrock * fault. 25154451Seschrock */ 25164451Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 25174451Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 25184451Seschrock } else if (state == VDEV_STATE_REMOVED) { 25194451Seschrock /* 25204451Seschrock * Indicate to the ZFS DE that this device has been removed, and 25214451Seschrock * any recent errors should be ignored. 25224451Seschrock */ 25236643Seschrock zfs_post_remove(spa, vd); 25244451Seschrock vd->vdev_removed = B_TRUE; 25254451Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 25261544Seschrock /* 25271544Seschrock * If we fail to open a vdev during an import, we mark it as 25281544Seschrock * "not available", which signifies that it was never there to 25291544Seschrock * begin with. Failure to open such a device is not considered 25301544Seschrock * an error. 25311544Seschrock */ 25326643Seschrock if (spa->spa_load_state == SPA_LOAD_IMPORT && 25331986Seschrock vd->vdev_ops->vdev_op_leaf) 25341986Seschrock vd->vdev_not_present = 1; 25351986Seschrock 25361986Seschrock /* 25371986Seschrock * Post the appropriate ereport. If the 'prevstate' field is 25381986Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 25391986Seschrock * that this is part of a vdev_reopen(). In this case, we don't 25401986Seschrock * want to post the ereport if the device was already in the 25411986Seschrock * CANT_OPEN state beforehand. 25424451Seschrock * 25434451Seschrock * If the 'checkremove' flag is set, then this is an attempt to 25444451Seschrock * online the device in response to an insertion event. If we 25454451Seschrock * hit this case, then we have detected an insertion event for a 25464451Seschrock * faulted or offline device that wasn't in the removed state. 25474451Seschrock * In this scenario, we don't post an ereport because we are 25484451Seschrock * about to replace the device, or attempt an online with 25494451Seschrock * vdev_forcefault, which will generate the fault for us. 25501986Seschrock */ 25514451Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 25524451Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 25536643Seschrock vd != spa->spa_root_vdev) { 25541544Seschrock const char *class; 25551544Seschrock 25561544Seschrock switch (aux) { 25571544Seschrock case VDEV_AUX_OPEN_FAILED: 25581544Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 25591544Seschrock break; 25601544Seschrock case VDEV_AUX_CORRUPT_DATA: 25611544Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 25621544Seschrock break; 25631544Seschrock case VDEV_AUX_NO_REPLICAS: 25641544Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 25651544Seschrock break; 25661544Seschrock case VDEV_AUX_BAD_GUID_SUM: 25671544Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 25681544Seschrock break; 25691544Seschrock case VDEV_AUX_TOO_SMALL: 25701544Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 25711544Seschrock break; 25721544Seschrock case VDEV_AUX_BAD_LABEL: 25731544Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 25741544Seschrock break; 25757754SJeff.Bonwick@Sun.COM case VDEV_AUX_IO_FAILURE: 25767754SJeff.Bonwick@Sun.COM class = FM_EREPORT_ZFS_IO_FAILURE; 25777754SJeff.Bonwick@Sun.COM break; 25781544Seschrock default: 25791544Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 25801544Seschrock } 25811544Seschrock 25826643Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 25831544Seschrock } 25844451Seschrock 25854451Seschrock /* Erase any notion of persistent removed state */ 25864451Seschrock vd->vdev_removed = B_FALSE; 25874451Seschrock } else { 25884451Seschrock vd->vdev_removed = B_FALSE; 25891544Seschrock } 25901544Seschrock 25914451Seschrock if (!isopen) 25924451Seschrock vdev_propagate_state(vd); 2593789Sahrens } 25947042Sgw25295 25957042Sgw25295 /* 25967042Sgw25295 * Check the vdev configuration to ensure that it's capable of supporting 25977042Sgw25295 * a root pool. Currently, we do not support RAID-Z or partial configuration. 25987042Sgw25295 * In addition, only a single top-level vdev is allowed and none of the leaves 25997042Sgw25295 * can be wholedisks. 26007042Sgw25295 */ 26017042Sgw25295 boolean_t 26027042Sgw25295 vdev_is_bootable(vdev_t *vd) 26037042Sgw25295 { 26047042Sgw25295 int c; 26057042Sgw25295 26067042Sgw25295 if (!vd->vdev_ops->vdev_op_leaf) { 26077042Sgw25295 char *vdev_type = vd->vdev_ops->vdev_op_type; 26087042Sgw25295 26097042Sgw25295 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 26107042Sgw25295 vd->vdev_children > 1) { 26117042Sgw25295 return (B_FALSE); 26127042Sgw25295 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 26137042Sgw25295 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 26147042Sgw25295 return (B_FALSE); 26157042Sgw25295 } 26167042Sgw25295 } else if (vd->vdev_wholedisk == 1) { 26177042Sgw25295 return (B_FALSE); 26187042Sgw25295 } 26197042Sgw25295 26207042Sgw25295 for (c = 0; c < vd->vdev_children; c++) { 26217042Sgw25295 if (!vdev_is_bootable(vd->vdev_child[c])) 26227042Sgw25295 return (B_FALSE); 26237042Sgw25295 } 26247042Sgw25295 return (B_TRUE); 26257042Sgw25295 } 2626