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 /* 23*11422SMark.Musante@Sun.COM * Copyright 2010 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> 429701SGeorge.Wilson@Sun.COM #include <sys/zil.h> 43789Sahrens 44789Sahrens /* 45789Sahrens * Virtual device management. 46789Sahrens */ 47789Sahrens 48789Sahrens static vdev_ops_t *vdev_ops_table[] = { 49789Sahrens &vdev_root_ops, 50789Sahrens &vdev_raidz_ops, 51789Sahrens &vdev_mirror_ops, 52789Sahrens &vdev_replacing_ops, 532082Seschrock &vdev_spare_ops, 54789Sahrens &vdev_disk_ops, 55789Sahrens &vdev_file_ops, 56789Sahrens &vdev_missing_ops, 5710594SGeorge.Wilson@Sun.COM &vdev_hole_ops, 58789Sahrens NULL 59789Sahrens }; 60789Sahrens 617046Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */ 627046Sahrens int zfs_scrub_limit = 10; 633697Smishra 64789Sahrens /* 65789Sahrens * Given a vdev type, return the appropriate ops vector. 66789Sahrens */ 67789Sahrens static vdev_ops_t * 68789Sahrens vdev_getops(const char *type) 69789Sahrens { 70789Sahrens vdev_ops_t *ops, **opspp; 71789Sahrens 72789Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 73789Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 74789Sahrens break; 75789Sahrens 76789Sahrens return (ops); 77789Sahrens } 78789Sahrens 79789Sahrens /* 80789Sahrens * Default asize function: return the MAX of psize with the asize of 81789Sahrens * all children. This is what's used by anything other than RAID-Z. 82789Sahrens */ 83789Sahrens uint64_t 84789Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 85789Sahrens { 861732Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 87789Sahrens uint64_t csize; 889816SGeorge.Wilson@Sun.COM 899816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 90789Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 91789Sahrens asize = MAX(asize, csize); 92789Sahrens } 93789Sahrens 94789Sahrens return (asize); 95789Sahrens } 96789Sahrens 971175Slling /* 989816SGeorge.Wilson@Sun.COM * Get the minimum allocatable size. We define the allocatable size as 999816SGeorge.Wilson@Sun.COM * the vdev's asize rounded to the nearest metaslab. This allows us to 1009816SGeorge.Wilson@Sun.COM * replace or attach devices which don't have the same physical size but 1019816SGeorge.Wilson@Sun.COM * can still satisfy the same number of allocations. 1021175Slling */ 1031175Slling uint64_t 1049816SGeorge.Wilson@Sun.COM vdev_get_min_asize(vdev_t *vd) 1051175Slling { 1069816SGeorge.Wilson@Sun.COM vdev_t *pvd = vd->vdev_parent; 1079816SGeorge.Wilson@Sun.COM 1089816SGeorge.Wilson@Sun.COM /* 1099816SGeorge.Wilson@Sun.COM * The our parent is NULL (inactive spare or cache) or is the root, 1109816SGeorge.Wilson@Sun.COM * just return our own asize. 1119816SGeorge.Wilson@Sun.COM */ 1129816SGeorge.Wilson@Sun.COM if (pvd == NULL) 1139816SGeorge.Wilson@Sun.COM return (vd->vdev_asize); 1141175Slling 1151175Slling /* 1169816SGeorge.Wilson@Sun.COM * The top-level vdev just returns the allocatable size rounded 1179816SGeorge.Wilson@Sun.COM * to the nearest metaslab. 1189816SGeorge.Wilson@Sun.COM */ 1199816SGeorge.Wilson@Sun.COM if (vd == vd->vdev_top) 1209816SGeorge.Wilson@Sun.COM return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 1219816SGeorge.Wilson@Sun.COM 1229816SGeorge.Wilson@Sun.COM /* 1239816SGeorge.Wilson@Sun.COM * The allocatable space for a raidz vdev is N * sizeof(smallest child), 1249816SGeorge.Wilson@Sun.COM * so each child must provide at least 1/Nth of its asize. 1251175Slling */ 1269816SGeorge.Wilson@Sun.COM if (pvd->vdev_ops == &vdev_raidz_ops) 1279816SGeorge.Wilson@Sun.COM return (pvd->vdev_min_asize / pvd->vdev_children); 1289816SGeorge.Wilson@Sun.COM 1299816SGeorge.Wilson@Sun.COM return (pvd->vdev_min_asize); 1309816SGeorge.Wilson@Sun.COM } 1319816SGeorge.Wilson@Sun.COM 1329816SGeorge.Wilson@Sun.COM void 1339816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vdev_t *vd) 1349816SGeorge.Wilson@Sun.COM { 1359816SGeorge.Wilson@Sun.COM vd->vdev_min_asize = vdev_get_min_asize(vd); 1369816SGeorge.Wilson@Sun.COM 1379816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 1389816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vd->vdev_child[c]); 1391175Slling } 1401175Slling 141789Sahrens vdev_t * 142789Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 143789Sahrens { 144789Sahrens vdev_t *rvd = spa->spa_root_vdev; 145789Sahrens 1467754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1475530Sbonwick 1487046Sahrens if (vdev < rvd->vdev_children) { 1497046Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 150789Sahrens return (rvd->vdev_child[vdev]); 1517046Sahrens } 152789Sahrens 153789Sahrens return (NULL); 154789Sahrens } 155789Sahrens 156789Sahrens vdev_t * 157789Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 158789Sahrens { 159789Sahrens vdev_t *mvd; 160789Sahrens 1611585Sbonwick if (vd->vdev_guid == guid) 162789Sahrens return (vd); 163789Sahrens 1649816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 165789Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 166789Sahrens NULL) 167789Sahrens return (mvd); 168789Sahrens 169789Sahrens return (NULL); 170789Sahrens } 171789Sahrens 172789Sahrens void 173789Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 174789Sahrens { 175789Sahrens size_t oldsize, newsize; 176789Sahrens uint64_t id = cvd->vdev_id; 177789Sahrens vdev_t **newchild; 178789Sahrens 1797754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 180789Sahrens ASSERT(cvd->vdev_parent == NULL); 181789Sahrens 182789Sahrens cvd->vdev_parent = pvd; 183789Sahrens 184789Sahrens if (pvd == NULL) 185789Sahrens return; 186789Sahrens 187789Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 188789Sahrens 189789Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 190789Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 191789Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 192789Sahrens 193789Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 194789Sahrens if (pvd->vdev_child != NULL) { 195789Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 196789Sahrens kmem_free(pvd->vdev_child, oldsize); 197789Sahrens } 198789Sahrens 199789Sahrens pvd->vdev_child = newchild; 200789Sahrens pvd->vdev_child[id] = cvd; 201789Sahrens 202789Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 203789Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 204789Sahrens 205789Sahrens /* 206789Sahrens * Walk up all ancestors to update guid sum. 207789Sahrens */ 208789Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 209789Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 2103697Smishra 2113697Smishra if (cvd->vdev_ops->vdev_op_leaf) 2123697Smishra cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit; 213789Sahrens } 214789Sahrens 215789Sahrens void 216789Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 217789Sahrens { 218789Sahrens int c; 219789Sahrens uint_t id = cvd->vdev_id; 220789Sahrens 221789Sahrens ASSERT(cvd->vdev_parent == pvd); 222789Sahrens 223789Sahrens if (pvd == NULL) 224789Sahrens return; 225789Sahrens 226789Sahrens ASSERT(id < pvd->vdev_children); 227789Sahrens ASSERT(pvd->vdev_child[id] == cvd); 228789Sahrens 229789Sahrens pvd->vdev_child[id] = NULL; 230789Sahrens cvd->vdev_parent = NULL; 231789Sahrens 232789Sahrens for (c = 0; c < pvd->vdev_children; c++) 233789Sahrens if (pvd->vdev_child[c]) 234789Sahrens break; 235789Sahrens 236789Sahrens if (c == pvd->vdev_children) { 237789Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 238789Sahrens pvd->vdev_child = NULL; 239789Sahrens pvd->vdev_children = 0; 240789Sahrens } 241789Sahrens 242789Sahrens /* 243789Sahrens * Walk up all ancestors to update guid sum. 244789Sahrens */ 245789Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 246789Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 2473697Smishra 2483697Smishra if (cvd->vdev_ops->vdev_op_leaf) 2493697Smishra cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit; 250789Sahrens } 251789Sahrens 252789Sahrens /* 253789Sahrens * Remove any holes in the child array. 254789Sahrens */ 255789Sahrens void 256789Sahrens vdev_compact_children(vdev_t *pvd) 257789Sahrens { 258789Sahrens vdev_t **newchild, *cvd; 259789Sahrens int oldc = pvd->vdev_children; 2609816SGeorge.Wilson@Sun.COM int newc; 261789Sahrens 2627754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 263789Sahrens 2649816SGeorge.Wilson@Sun.COM for (int c = newc = 0; c < oldc; c++) 265789Sahrens if (pvd->vdev_child[c]) 266789Sahrens newc++; 267789Sahrens 268789Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 269789Sahrens 2709816SGeorge.Wilson@Sun.COM for (int c = newc = 0; c < oldc; c++) { 271789Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 272789Sahrens newchild[newc] = cvd; 273789Sahrens cvd->vdev_id = newc++; 274789Sahrens } 275789Sahrens } 276789Sahrens 277789Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 278789Sahrens pvd->vdev_child = newchild; 279789Sahrens pvd->vdev_children = newc; 280789Sahrens } 281789Sahrens 282789Sahrens /* 283789Sahrens * Allocate and minimally initialize a vdev_t. 284789Sahrens */ 28510594SGeorge.Wilson@Sun.COM vdev_t * 286789Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 287789Sahrens { 288789Sahrens vdev_t *vd; 289789Sahrens 2901585Sbonwick vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 2911585Sbonwick 2921585Sbonwick if (spa->spa_root_vdev == NULL) { 2931585Sbonwick ASSERT(ops == &vdev_root_ops); 2941585Sbonwick spa->spa_root_vdev = vd; 2951585Sbonwick } 296789Sahrens 29710594SGeorge.Wilson@Sun.COM if (guid == 0 && ops != &vdev_hole_ops) { 2981585Sbonwick if (spa->spa_root_vdev == vd) { 2991585Sbonwick /* 3001585Sbonwick * The root vdev's guid will also be the pool guid, 3011585Sbonwick * which must be unique among all pools. 3021585Sbonwick */ 303*11422SMark.Musante@Sun.COM guid = spa_generate_guid(NULL); 3041585Sbonwick } else { 3051585Sbonwick /* 3061585Sbonwick * Any other vdev's guid must be unique within the pool. 3071585Sbonwick */ 308*11422SMark.Musante@Sun.COM guid = spa_generate_guid(spa); 3091585Sbonwick } 3101585Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 3111585Sbonwick } 312789Sahrens 313789Sahrens vd->vdev_spa = spa; 314789Sahrens vd->vdev_id = id; 315789Sahrens vd->vdev_guid = guid; 316789Sahrens vd->vdev_guid_sum = guid; 317789Sahrens vd->vdev_ops = ops; 318789Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 31910594SGeorge.Wilson@Sun.COM vd->vdev_ishole = (ops == &vdev_hole_ops); 320789Sahrens 321789Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 3222856Snd150628 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 3237754SJeff.Bonwick@Sun.COM mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 3248241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 3258241SJeff.Bonwick@Sun.COM space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0, 3268241SJeff.Bonwick@Sun.COM &vd->vdev_dtl_lock); 3278241SJeff.Bonwick@Sun.COM } 328789Sahrens txg_list_create(&vd->vdev_ms_list, 329789Sahrens offsetof(struct metaslab, ms_txg_node)); 330789Sahrens txg_list_create(&vd->vdev_dtl_list, 331789Sahrens offsetof(struct vdev, vdev_dtl_node)); 332789Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 3334451Seschrock vdev_queue_init(vd); 3344451Seschrock vdev_cache_init(vd); 335789Sahrens 336789Sahrens return (vd); 337789Sahrens } 338789Sahrens 339789Sahrens /* 340789Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 341789Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 342789Sahrens * different for each case. 343789Sahrens */ 3442082Seschrock int 3452082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 3462082Seschrock int alloctype) 347789Sahrens { 348789Sahrens vdev_ops_t *ops; 349789Sahrens char *type; 3504527Sperrin uint64_t guid = 0, islog, nparity; 351789Sahrens vdev_t *vd; 352789Sahrens 3537754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 354789Sahrens 355789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 3562082Seschrock return (EINVAL); 357789Sahrens 358789Sahrens if ((ops = vdev_getops(type)) == NULL) 3592082Seschrock return (EINVAL); 360789Sahrens 361789Sahrens /* 362789Sahrens * If this is a load, get the vdev guid from the nvlist. 363789Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 364789Sahrens */ 365789Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 366789Sahrens uint64_t label_id; 367789Sahrens 368789Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 369789Sahrens label_id != id) 3702082Seschrock return (EINVAL); 371789Sahrens 372789Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3732082Seschrock return (EINVAL); 3742082Seschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 3752082Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3762082Seschrock return (EINVAL); 3775450Sbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 3785450Sbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3795450Sbrendan return (EINVAL); 3809790SLin.Ling@Sun.COM } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 3819790SLin.Ling@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3829790SLin.Ling@Sun.COM return (EINVAL); 383789Sahrens } 384789Sahrens 3852082Seschrock /* 3862082Seschrock * The first allocated vdev must be of type 'root'. 3872082Seschrock */ 3882082Seschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 3892082Seschrock return (EINVAL); 3902082Seschrock 3914527Sperrin /* 3924527Sperrin * Determine whether we're a log vdev. 3934527Sperrin */ 3944527Sperrin islog = 0; 3954527Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 3965094Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 3974527Sperrin return (ENOTSUP); 3984527Sperrin 39910594SGeorge.Wilson@Sun.COM if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 40010594SGeorge.Wilson@Sun.COM return (ENOTSUP); 40110594SGeorge.Wilson@Sun.COM 4024527Sperrin /* 4034527Sperrin * Set the nparity property for RAID-Z vdevs. 4044527Sperrin */ 4054527Sperrin nparity = -1ULL; 4064527Sperrin if (ops == &vdev_raidz_ops) { 4074527Sperrin if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 4084527Sperrin &nparity) == 0) { 40910922SJeff.Bonwick@Sun.COM if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 4104527Sperrin return (EINVAL); 4114527Sperrin /* 41210105Sadam.leventhal@sun.com * Previous versions could only support 1 or 2 parity 41310105Sadam.leventhal@sun.com * device. 4144527Sperrin */ 41510105Sadam.leventhal@sun.com if (nparity > 1 && 41610105Sadam.leventhal@sun.com spa_version(spa) < SPA_VERSION_RAIDZ2) 41710105Sadam.leventhal@sun.com return (ENOTSUP); 41810105Sadam.leventhal@sun.com if (nparity > 2 && 41910105Sadam.leventhal@sun.com spa_version(spa) < SPA_VERSION_RAIDZ3) 4204527Sperrin return (ENOTSUP); 4214527Sperrin } else { 4224527Sperrin /* 4234527Sperrin * We require the parity to be specified for SPAs that 4244527Sperrin * support multiple parity levels. 4254527Sperrin */ 42610105Sadam.leventhal@sun.com if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 4274527Sperrin return (EINVAL); 4284527Sperrin /* 4294527Sperrin * Otherwise, we default to 1 parity device for RAID-Z. 4304527Sperrin */ 4314527Sperrin nparity = 1; 4324527Sperrin } 4334527Sperrin } else { 4344527Sperrin nparity = 0; 4354527Sperrin } 4364527Sperrin ASSERT(nparity != -1ULL); 4374527Sperrin 438789Sahrens vd = vdev_alloc_common(spa, id, guid, ops); 439789Sahrens 4404527Sperrin vd->vdev_islog = islog; 4414527Sperrin vd->vdev_nparity = nparity; 4424527Sperrin 443789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 444789Sahrens vd->vdev_path = spa_strdup(vd->vdev_path); 445789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 446789Sahrens vd->vdev_devid = spa_strdup(vd->vdev_devid); 4474451Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 4484451Seschrock &vd->vdev_physpath) == 0) 4494451Seschrock vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 4509425SEric.Schrock@Sun.COM if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 4519425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(vd->vdev_fru); 452789Sahrens 453789Sahrens /* 4541171Seschrock * Set the whole_disk property. If it's not specified, leave the value 4551171Seschrock * as -1. 4561171Seschrock */ 4571171Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 4581171Seschrock &vd->vdev_wholedisk) != 0) 4591171Seschrock vd->vdev_wholedisk = -1ULL; 4601171Seschrock 4611171Seschrock /* 4621544Seschrock * Look for the 'not present' flag. This will only be set if the device 4631544Seschrock * was not present at the time of import. 4641544Seschrock */ 4659425SEric.Schrock@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 4669425SEric.Schrock@Sun.COM &vd->vdev_not_present); 4671544Seschrock 4681544Seschrock /* 4691732Sbonwick * Get the alignment requirement. 4701732Sbonwick */ 4711732Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 4721732Sbonwick 4731732Sbonwick /* 47410594SGeorge.Wilson@Sun.COM * Retrieve the vdev creation time. 47510594SGeorge.Wilson@Sun.COM */ 47610594SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 47710594SGeorge.Wilson@Sun.COM &vd->vdev_crtxg); 47810594SGeorge.Wilson@Sun.COM 47910594SGeorge.Wilson@Sun.COM /* 480789Sahrens * If we're a top-level vdev, try to load the allocation parameters. 481789Sahrens */ 482*11422SMark.Musante@Sun.COM if (parent && !parent->vdev_parent && 483*11422SMark.Musante@Sun.COM (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 484789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 485789Sahrens &vd->vdev_ms_array); 486789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 487789Sahrens &vd->vdev_ms_shift); 488789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 489789Sahrens &vd->vdev_asize); 490789Sahrens } 491789Sahrens 49210974SJeff.Bonwick@Sun.COM if (parent && !parent->vdev_parent) { 49310974SJeff.Bonwick@Sun.COM ASSERT(alloctype == VDEV_ALLOC_LOAD || 49410982SGeorge.Wilson@Sun.COM alloctype == VDEV_ALLOC_ADD || 495*11422SMark.Musante@Sun.COM alloctype == VDEV_ALLOC_SPLIT || 49610982SGeorge.Wilson@Sun.COM alloctype == VDEV_ALLOC_ROOTPOOL); 49710974SJeff.Bonwick@Sun.COM vd->vdev_mg = metaslab_group_create(islog ? 49810974SJeff.Bonwick@Sun.COM spa_log_class(spa) : spa_normal_class(spa), vd); 49910974SJeff.Bonwick@Sun.COM } 50010974SJeff.Bonwick@Sun.COM 501789Sahrens /* 5024451Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 503789Sahrens */ 5046643Seschrock if (vd->vdev_ops->vdev_op_leaf && 5059790SLin.Ling@Sun.COM (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 5069790SLin.Ling@Sun.COM alloctype == VDEV_ALLOC_ROOTPOOL)) { 5076643Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 5086643Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 5098241SJeff.Bonwick@Sun.COM &vd->vdev_dtl_smo.smo_object); 5106643Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 5116643Seschrock &vd->vdev_unspare); 5126643Seschrock } 5139790SLin.Ling@Sun.COM 5149790SLin.Ling@Sun.COM if (alloctype == VDEV_ALLOC_ROOTPOOL) { 5159790SLin.Ling@Sun.COM uint64_t spare = 0; 5169790SLin.Ling@Sun.COM 5179790SLin.Ling@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 5189790SLin.Ling@Sun.COM &spare) == 0 && spare) 5199790SLin.Ling@Sun.COM spa_spare_add(vd); 5209790SLin.Ling@Sun.COM } 5219790SLin.Ling@Sun.COM 5221732Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 5231732Sbonwick &vd->vdev_offline); 5246643Seschrock 5254451Seschrock /* 5264451Seschrock * When importing a pool, we want to ignore the persistent fault 5274451Seschrock * state, as the diagnosis made on another system may not be 52810817SEric.Schrock@Sun.COM * valid in the current context. Local vdevs will 52910817SEric.Schrock@Sun.COM * remain in the faulted state. 5304451Seschrock */ 53111147SGeorge.Wilson@Sun.COM if (spa_load_state(spa) == SPA_LOAD_OPEN) { 5324451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 5334451Seschrock &vd->vdev_faulted); 5344451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 5354451Seschrock &vd->vdev_degraded); 5364451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 5374451Seschrock &vd->vdev_removed); 53810817SEric.Schrock@Sun.COM 53910817SEric.Schrock@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded) { 54010817SEric.Schrock@Sun.COM char *aux; 54110817SEric.Schrock@Sun.COM 54210817SEric.Schrock@Sun.COM vd->vdev_label_aux = 54310817SEric.Schrock@Sun.COM VDEV_AUX_ERR_EXCEEDED; 54410817SEric.Schrock@Sun.COM if (nvlist_lookup_string(nv, 54510817SEric.Schrock@Sun.COM ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 54610817SEric.Schrock@Sun.COM strcmp(aux, "external") == 0) 54710817SEric.Schrock@Sun.COM vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 54810817SEric.Schrock@Sun.COM } 5494451Seschrock } 550789Sahrens } 551789Sahrens 552789Sahrens /* 553789Sahrens * Add ourselves to the parent's list of children. 554789Sahrens */ 555789Sahrens vdev_add_child(parent, vd); 556789Sahrens 5572082Seschrock *vdp = vd; 5582082Seschrock 5592082Seschrock return (0); 560789Sahrens } 561789Sahrens 562789Sahrens void 563789Sahrens vdev_free(vdev_t *vd) 564789Sahrens { 5654451Seschrock spa_t *spa = vd->vdev_spa; 566789Sahrens 567789Sahrens /* 568789Sahrens * vdev_free() implies closing the vdev first. This is simpler than 569789Sahrens * trying to ensure complicated semantics for all callers. 570789Sahrens */ 571789Sahrens vdev_close(vd); 572789Sahrens 5737754SJeff.Bonwick@Sun.COM ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 57410922SJeff.Bonwick@Sun.COM ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 575789Sahrens 576789Sahrens /* 577789Sahrens * Free all children. 578789Sahrens */ 5799816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 580789Sahrens vdev_free(vd->vdev_child[c]); 581789Sahrens 582789Sahrens ASSERT(vd->vdev_child == NULL); 583789Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 584789Sahrens 585789Sahrens /* 586789Sahrens * Discard allocation state. 587789Sahrens */ 58810974SJeff.Bonwick@Sun.COM if (vd->vdev_mg != NULL) { 589789Sahrens vdev_metaslab_fini(vd); 59010974SJeff.Bonwick@Sun.COM metaslab_group_destroy(vd->vdev_mg); 59110974SJeff.Bonwick@Sun.COM } 592789Sahrens 593789Sahrens ASSERT3U(vd->vdev_stat.vs_space, ==, 0); 5942082Seschrock ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0); 595789Sahrens ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 596789Sahrens 597789Sahrens /* 598789Sahrens * Remove this vdev from its parent's child list. 599789Sahrens */ 600789Sahrens vdev_remove_child(vd->vdev_parent, vd); 601789Sahrens 602789Sahrens ASSERT(vd->vdev_parent == NULL); 603789Sahrens 6044451Seschrock /* 6054451Seschrock * Clean up vdev structure. 6064451Seschrock */ 6074451Seschrock vdev_queue_fini(vd); 6084451Seschrock vdev_cache_fini(vd); 6094451Seschrock 6104451Seschrock if (vd->vdev_path) 6114451Seschrock spa_strfree(vd->vdev_path); 6124451Seschrock if (vd->vdev_devid) 6134451Seschrock spa_strfree(vd->vdev_devid); 6144451Seschrock if (vd->vdev_physpath) 6154451Seschrock spa_strfree(vd->vdev_physpath); 6169425SEric.Schrock@Sun.COM if (vd->vdev_fru) 6179425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 6184451Seschrock 6194451Seschrock if (vd->vdev_isspare) 6204451Seschrock spa_spare_remove(vd); 6215450Sbrendan if (vd->vdev_isl2cache) 6225450Sbrendan spa_l2cache_remove(vd); 6234451Seschrock 6244451Seschrock txg_list_destroy(&vd->vdev_ms_list); 6254451Seschrock txg_list_destroy(&vd->vdev_dtl_list); 6268241SJeff.Bonwick@Sun.COM 6274451Seschrock mutex_enter(&vd->vdev_dtl_lock); 6288241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 6298241SJeff.Bonwick@Sun.COM space_map_unload(&vd->vdev_dtl[t]); 6308241SJeff.Bonwick@Sun.COM space_map_destroy(&vd->vdev_dtl[t]); 6318241SJeff.Bonwick@Sun.COM } 6324451Seschrock mutex_exit(&vd->vdev_dtl_lock); 6338241SJeff.Bonwick@Sun.COM 6344451Seschrock mutex_destroy(&vd->vdev_dtl_lock); 6354451Seschrock mutex_destroy(&vd->vdev_stat_lock); 6367754SJeff.Bonwick@Sun.COM mutex_destroy(&vd->vdev_probe_lock); 6374451Seschrock 6384451Seschrock if (vd == spa->spa_root_vdev) 6394451Seschrock spa->spa_root_vdev = NULL; 6404451Seschrock 6414451Seschrock kmem_free(vd, sizeof (vdev_t)); 642789Sahrens } 643789Sahrens 644789Sahrens /* 645789Sahrens * Transfer top-level vdev state from svd to tvd. 646789Sahrens */ 647789Sahrens static void 648789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 649789Sahrens { 650789Sahrens spa_t *spa = svd->vdev_spa; 651789Sahrens metaslab_t *msp; 652789Sahrens vdev_t *vd; 653789Sahrens int t; 654789Sahrens 655789Sahrens ASSERT(tvd == tvd->vdev_top); 656789Sahrens 657789Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 658789Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 659789Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 660789Sahrens 661789Sahrens svd->vdev_ms_array = 0; 662789Sahrens svd->vdev_ms_shift = 0; 663789Sahrens svd->vdev_ms_count = 0; 664789Sahrens 665789Sahrens tvd->vdev_mg = svd->vdev_mg; 666789Sahrens tvd->vdev_ms = svd->vdev_ms; 667789Sahrens 668789Sahrens svd->vdev_mg = NULL; 669789Sahrens svd->vdev_ms = NULL; 6701732Sbonwick 6711732Sbonwick if (tvd->vdev_mg != NULL) 6721732Sbonwick tvd->vdev_mg->mg_vd = tvd; 673789Sahrens 674789Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 675789Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 6762082Seschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 677789Sahrens 678789Sahrens svd->vdev_stat.vs_alloc = 0; 679789Sahrens svd->vdev_stat.vs_space = 0; 6802082Seschrock svd->vdev_stat.vs_dspace = 0; 681789Sahrens 682789Sahrens for (t = 0; t < TXG_SIZE; t++) { 683789Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 684789Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 685789Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 686789Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 687789Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 688789Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 689789Sahrens } 690789Sahrens 6917754SJeff.Bonwick@Sun.COM if (list_link_active(&svd->vdev_config_dirty_node)) { 692789Sahrens vdev_config_clean(svd); 693789Sahrens vdev_config_dirty(tvd); 694789Sahrens } 695789Sahrens 6967754SJeff.Bonwick@Sun.COM if (list_link_active(&svd->vdev_state_dirty_node)) { 6977754SJeff.Bonwick@Sun.COM vdev_state_clean(svd); 6987754SJeff.Bonwick@Sun.COM vdev_state_dirty(tvd); 6997754SJeff.Bonwick@Sun.COM } 7007754SJeff.Bonwick@Sun.COM 7012082Seschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 7022082Seschrock svd->vdev_deflate_ratio = 0; 7034527Sperrin 7044527Sperrin tvd->vdev_islog = svd->vdev_islog; 7054527Sperrin svd->vdev_islog = 0; 706789Sahrens } 707789Sahrens 708789Sahrens static void 709789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 710789Sahrens { 711789Sahrens if (vd == NULL) 712789Sahrens return; 713789Sahrens 714789Sahrens vd->vdev_top = tvd; 715789Sahrens 7169816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 717789Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 718789Sahrens } 719789Sahrens 720789Sahrens /* 721789Sahrens * Add a mirror/replacing vdev above an existing vdev. 722789Sahrens */ 723789Sahrens vdev_t * 724789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 725789Sahrens { 726789Sahrens spa_t *spa = cvd->vdev_spa; 727789Sahrens vdev_t *pvd = cvd->vdev_parent; 728789Sahrens vdev_t *mvd; 729789Sahrens 7307754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 731789Sahrens 732789Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 7331732Sbonwick 7341732Sbonwick mvd->vdev_asize = cvd->vdev_asize; 7359816SGeorge.Wilson@Sun.COM mvd->vdev_min_asize = cvd->vdev_min_asize; 7361732Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 7371732Sbonwick mvd->vdev_state = cvd->vdev_state; 73810594SGeorge.Wilson@Sun.COM mvd->vdev_crtxg = cvd->vdev_crtxg; 7391732Sbonwick 740789Sahrens vdev_remove_child(pvd, cvd); 741789Sahrens vdev_add_child(pvd, mvd); 742789Sahrens cvd->vdev_id = mvd->vdev_children; 743789Sahrens vdev_add_child(mvd, cvd); 744789Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 745789Sahrens 746789Sahrens if (mvd == mvd->vdev_top) 747789Sahrens vdev_top_transfer(cvd, mvd); 748789Sahrens 749789Sahrens return (mvd); 750789Sahrens } 751789Sahrens 752789Sahrens /* 753789Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 754789Sahrens */ 755789Sahrens void 756789Sahrens vdev_remove_parent(vdev_t *cvd) 757789Sahrens { 758789Sahrens vdev_t *mvd = cvd->vdev_parent; 759789Sahrens vdev_t *pvd = mvd->vdev_parent; 760789Sahrens 7617754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 762789Sahrens 763789Sahrens ASSERT(mvd->vdev_children == 1); 764789Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 7652082Seschrock mvd->vdev_ops == &vdev_replacing_ops || 7662082Seschrock mvd->vdev_ops == &vdev_spare_ops); 7671732Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 768789Sahrens 769789Sahrens vdev_remove_child(mvd, cvd); 770789Sahrens vdev_remove_child(pvd, mvd); 7718241SJeff.Bonwick@Sun.COM 7727754SJeff.Bonwick@Sun.COM /* 7737754SJeff.Bonwick@Sun.COM * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 7747754SJeff.Bonwick@Sun.COM * Otherwise, we could have detached an offline device, and when we 7757754SJeff.Bonwick@Sun.COM * go to import the pool we'll think we have two top-level vdevs, 7767754SJeff.Bonwick@Sun.COM * instead of a different version of the same top-level vdev. 7777754SJeff.Bonwick@Sun.COM */ 7788241SJeff.Bonwick@Sun.COM if (mvd->vdev_top == mvd) { 7798241SJeff.Bonwick@Sun.COM uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 780*11422SMark.Musante@Sun.COM cvd->vdev_orig_guid = cvd->vdev_guid; 7818241SJeff.Bonwick@Sun.COM cvd->vdev_guid += guid_delta; 7828241SJeff.Bonwick@Sun.COM cvd->vdev_guid_sum += guid_delta; 7838241SJeff.Bonwick@Sun.COM } 784789Sahrens cvd->vdev_id = mvd->vdev_id; 785789Sahrens vdev_add_child(pvd, cvd); 786789Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 787789Sahrens 788789Sahrens if (cvd == cvd->vdev_top) 789789Sahrens vdev_top_transfer(mvd, cvd); 790789Sahrens 791789Sahrens ASSERT(mvd->vdev_children == 0); 792789Sahrens vdev_free(mvd); 793789Sahrens } 794789Sahrens 7951544Seschrock int 796789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 797789Sahrens { 798789Sahrens spa_t *spa = vd->vdev_spa; 7991732Sbonwick objset_t *mos = spa->spa_meta_objset; 8001732Sbonwick uint64_t m; 801789Sahrens uint64_t oldc = vd->vdev_ms_count; 802789Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 8031732Sbonwick metaslab_t **mspp; 8041732Sbonwick int error; 805789Sahrens 80610974SJeff.Bonwick@Sun.COM ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 80710974SJeff.Bonwick@Sun.COM 80810594SGeorge.Wilson@Sun.COM /* 80910594SGeorge.Wilson@Sun.COM * This vdev is not being allocated from yet or is a hole. 81010594SGeorge.Wilson@Sun.COM */ 81110594SGeorge.Wilson@Sun.COM if (vd->vdev_ms_shift == 0) 8121585Sbonwick return (0); 8131585Sbonwick 81410594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 81510594SGeorge.Wilson@Sun.COM 8169701SGeorge.Wilson@Sun.COM /* 8179701SGeorge.Wilson@Sun.COM * Compute the raidz-deflation ratio. Note, we hard-code 8189701SGeorge.Wilson@Sun.COM * in 128k (1 << 17) because it is the current "typical" blocksize. 8199701SGeorge.Wilson@Sun.COM * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change, 8209701SGeorge.Wilson@Sun.COM * or we will inconsistently account for existing bp's. 8219701SGeorge.Wilson@Sun.COM */ 8229701SGeorge.Wilson@Sun.COM vd->vdev_deflate_ratio = (1 << 17) / 8239701SGeorge.Wilson@Sun.COM (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 8249701SGeorge.Wilson@Sun.COM 825789Sahrens ASSERT(oldc <= newc); 826789Sahrens 8271732Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 8281732Sbonwick 8291732Sbonwick if (oldc != 0) { 8301732Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 8311732Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 8321732Sbonwick } 8331732Sbonwick 8341732Sbonwick vd->vdev_ms = mspp; 835789Sahrens vd->vdev_ms_count = newc; 836789Sahrens 8371732Sbonwick for (m = oldc; m < newc; m++) { 8381732Sbonwick space_map_obj_t smo = { 0, 0, 0 }; 839789Sahrens if (txg == 0) { 8401732Sbonwick uint64_t object = 0; 8411732Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 8429512SNeil.Perrin@Sun.COM m * sizeof (uint64_t), sizeof (uint64_t), &object, 8439512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 8441732Sbonwick if (error) 8451732Sbonwick return (error); 8461732Sbonwick if (object != 0) { 8471732Sbonwick dmu_buf_t *db; 8481732Sbonwick error = dmu_bonus_hold(mos, object, FTAG, &db); 8491732Sbonwick if (error) 8501732Sbonwick return (error); 8514944Smaybee ASSERT3U(db->db_size, >=, sizeof (smo)); 8524944Smaybee bcopy(db->db_data, &smo, sizeof (smo)); 8531732Sbonwick ASSERT3U(smo.smo_object, ==, object); 8541544Seschrock dmu_buf_rele(db, FTAG); 855789Sahrens } 856789Sahrens } 8571732Sbonwick vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo, 8581732Sbonwick m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg); 859789Sahrens } 860789Sahrens 86110974SJeff.Bonwick@Sun.COM if (txg == 0) 86210974SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 86310974SJeff.Bonwick@Sun.COM 86410974SJeff.Bonwick@Sun.COM if (oldc == 0) 86510974SJeff.Bonwick@Sun.COM metaslab_group_activate(vd->vdev_mg); 86610974SJeff.Bonwick@Sun.COM 86710974SJeff.Bonwick@Sun.COM if (txg == 0) 86810974SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALLOC, FTAG); 86910974SJeff.Bonwick@Sun.COM 8701544Seschrock return (0); 871789Sahrens } 872789Sahrens 873789Sahrens void 874789Sahrens vdev_metaslab_fini(vdev_t *vd) 875789Sahrens { 876789Sahrens uint64_t m; 877789Sahrens uint64_t count = vd->vdev_ms_count; 878789Sahrens 879789Sahrens if (vd->vdev_ms != NULL) { 88010974SJeff.Bonwick@Sun.COM metaslab_group_passivate(vd->vdev_mg); 881789Sahrens for (m = 0; m < count; m++) 8821732Sbonwick if (vd->vdev_ms[m] != NULL) 8831732Sbonwick metaslab_fini(vd->vdev_ms[m]); 884789Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 885789Sahrens vd->vdev_ms = NULL; 886789Sahrens } 887789Sahrens } 888789Sahrens 8897754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats { 8907754SJeff.Bonwick@Sun.COM boolean_t vps_readable; 8917754SJeff.Bonwick@Sun.COM boolean_t vps_writeable; 8927754SJeff.Bonwick@Sun.COM int vps_flags; 8937754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t; 8947754SJeff.Bonwick@Sun.COM 8957754SJeff.Bonwick@Sun.COM static void 8967754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio) 8975329Sgw25295 { 8988241SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 8998632SBill.Moore@Sun.COM vdev_t *vd = zio->io_vd; 9007754SJeff.Bonwick@Sun.COM vdev_probe_stats_t *vps = zio->io_private; 9018632SBill.Moore@Sun.COM 9028632SBill.Moore@Sun.COM ASSERT(vd->vdev_probe_zio != NULL); 9037754SJeff.Bonwick@Sun.COM 9047754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_READ) { 9057754SJeff.Bonwick@Sun.COM if (zio->io_error == 0) 9067754SJeff.Bonwick@Sun.COM vps->vps_readable = 1; 9078241SJeff.Bonwick@Sun.COM if (zio->io_error == 0 && spa_writeable(spa)) { 9088632SBill.Moore@Sun.COM zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 9097754SJeff.Bonwick@Sun.COM zio->io_offset, zio->io_size, zio->io_data, 9107754SJeff.Bonwick@Sun.COM ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 9117754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 9127754SJeff.Bonwick@Sun.COM } else { 9137754SJeff.Bonwick@Sun.COM zio_buf_free(zio->io_data, zio->io_size); 9147754SJeff.Bonwick@Sun.COM } 9157754SJeff.Bonwick@Sun.COM } else if (zio->io_type == ZIO_TYPE_WRITE) { 9167754SJeff.Bonwick@Sun.COM if (zio->io_error == 0) 9177754SJeff.Bonwick@Sun.COM vps->vps_writeable = 1; 9187754SJeff.Bonwick@Sun.COM zio_buf_free(zio->io_data, zio->io_size); 9197754SJeff.Bonwick@Sun.COM } else if (zio->io_type == ZIO_TYPE_NULL) { 9208632SBill.Moore@Sun.COM zio_t *pio; 9217754SJeff.Bonwick@Sun.COM 9227754SJeff.Bonwick@Sun.COM vd->vdev_cant_read |= !vps->vps_readable; 9237754SJeff.Bonwick@Sun.COM vd->vdev_cant_write |= !vps->vps_writeable; 9247754SJeff.Bonwick@Sun.COM 9257754SJeff.Bonwick@Sun.COM if (vdev_readable(vd) && 9268241SJeff.Bonwick@Sun.COM (vdev_writeable(vd) || !spa_writeable(spa))) { 9277754SJeff.Bonwick@Sun.COM zio->io_error = 0; 9287754SJeff.Bonwick@Sun.COM } else { 9297754SJeff.Bonwick@Sun.COM ASSERT(zio->io_error != 0); 9307754SJeff.Bonwick@Sun.COM zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 9318241SJeff.Bonwick@Sun.COM spa, vd, NULL, 0, 0); 9327754SJeff.Bonwick@Sun.COM zio->io_error = ENXIO; 9337754SJeff.Bonwick@Sun.COM } 9348632SBill.Moore@Sun.COM 9358632SBill.Moore@Sun.COM mutex_enter(&vd->vdev_probe_lock); 9368632SBill.Moore@Sun.COM ASSERT(vd->vdev_probe_zio == zio); 9378632SBill.Moore@Sun.COM vd->vdev_probe_zio = NULL; 9388632SBill.Moore@Sun.COM mutex_exit(&vd->vdev_probe_lock); 9398632SBill.Moore@Sun.COM 9408632SBill.Moore@Sun.COM while ((pio = zio_walk_parents(zio)) != NULL) 9418632SBill.Moore@Sun.COM if (!vdev_accessible(vd, pio)) 9428632SBill.Moore@Sun.COM pio->io_error = ENXIO; 9438632SBill.Moore@Sun.COM 9447754SJeff.Bonwick@Sun.COM kmem_free(vps, sizeof (*vps)); 9457754SJeff.Bonwick@Sun.COM } 9467754SJeff.Bonwick@Sun.COM } 9475329Sgw25295 9487754SJeff.Bonwick@Sun.COM /* 9497754SJeff.Bonwick@Sun.COM * Determine whether this device is accessible by reading and writing 9507754SJeff.Bonwick@Sun.COM * to several known locations: the pad regions of each vdev label 9517754SJeff.Bonwick@Sun.COM * but the first (which we leave alone in case it contains a VTOC). 9527754SJeff.Bonwick@Sun.COM */ 9537754SJeff.Bonwick@Sun.COM zio_t * 9548632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio) 9557754SJeff.Bonwick@Sun.COM { 9567754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 9578632SBill.Moore@Sun.COM vdev_probe_stats_t *vps = NULL; 9588632SBill.Moore@Sun.COM zio_t *pio; 9597754SJeff.Bonwick@Sun.COM 9607754SJeff.Bonwick@Sun.COM ASSERT(vd->vdev_ops->vdev_op_leaf); 9617754SJeff.Bonwick@Sun.COM 9628632SBill.Moore@Sun.COM /* 9638632SBill.Moore@Sun.COM * Don't probe the probe. 9648632SBill.Moore@Sun.COM */ 9658632SBill.Moore@Sun.COM if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 9668632SBill.Moore@Sun.COM return (NULL); 9678632SBill.Moore@Sun.COM 9688632SBill.Moore@Sun.COM /* 9698632SBill.Moore@Sun.COM * To prevent 'probe storms' when a device fails, we create 9708632SBill.Moore@Sun.COM * just one probe i/o at a time. All zios that want to probe 9718632SBill.Moore@Sun.COM * this vdev will become parents of the probe io. 9728632SBill.Moore@Sun.COM */ 9738632SBill.Moore@Sun.COM mutex_enter(&vd->vdev_probe_lock); 9748632SBill.Moore@Sun.COM 9758632SBill.Moore@Sun.COM if ((pio = vd->vdev_probe_zio) == NULL) { 9768632SBill.Moore@Sun.COM vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 9778632SBill.Moore@Sun.COM 9788632SBill.Moore@Sun.COM vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 9798632SBill.Moore@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 9809725SEric.Schrock@Sun.COM ZIO_FLAG_TRYHARD; 9818632SBill.Moore@Sun.COM 9828632SBill.Moore@Sun.COM if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 9838632SBill.Moore@Sun.COM /* 9848632SBill.Moore@Sun.COM * vdev_cant_read and vdev_cant_write can only 9858632SBill.Moore@Sun.COM * transition from TRUE to FALSE when we have the 9868632SBill.Moore@Sun.COM * SCL_ZIO lock as writer; otherwise they can only 9878632SBill.Moore@Sun.COM * transition from FALSE to TRUE. This ensures that 9888632SBill.Moore@Sun.COM * any zio looking at these values can assume that 9898632SBill.Moore@Sun.COM * failures persist for the life of the I/O. That's 9908632SBill.Moore@Sun.COM * important because when a device has intermittent 9918632SBill.Moore@Sun.COM * connectivity problems, we want to ensure that 9928632SBill.Moore@Sun.COM * they're ascribed to the device (ENXIO) and not 9938632SBill.Moore@Sun.COM * the zio (EIO). 9948632SBill.Moore@Sun.COM * 9958632SBill.Moore@Sun.COM * Since we hold SCL_ZIO as writer here, clear both 9968632SBill.Moore@Sun.COM * values so the probe can reevaluate from first 9978632SBill.Moore@Sun.COM * principles. 9988632SBill.Moore@Sun.COM */ 9998632SBill.Moore@Sun.COM vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 10008632SBill.Moore@Sun.COM vd->vdev_cant_read = B_FALSE; 10018632SBill.Moore@Sun.COM vd->vdev_cant_write = B_FALSE; 10028632SBill.Moore@Sun.COM } 10038632SBill.Moore@Sun.COM 10048632SBill.Moore@Sun.COM vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 10058632SBill.Moore@Sun.COM vdev_probe_done, vps, 10068632SBill.Moore@Sun.COM vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 10078632SBill.Moore@Sun.COM 10088632SBill.Moore@Sun.COM if (zio != NULL) { 10098632SBill.Moore@Sun.COM vd->vdev_probe_wanted = B_TRUE; 10108632SBill.Moore@Sun.COM spa_async_request(spa, SPA_ASYNC_PROBE); 10118632SBill.Moore@Sun.COM } 10128632SBill.Moore@Sun.COM } 10138632SBill.Moore@Sun.COM 10148632SBill.Moore@Sun.COM if (zio != NULL) 10158632SBill.Moore@Sun.COM zio_add_child(zio, pio); 10168632SBill.Moore@Sun.COM 10178632SBill.Moore@Sun.COM mutex_exit(&vd->vdev_probe_lock); 10188632SBill.Moore@Sun.COM 10198632SBill.Moore@Sun.COM if (vps == NULL) { 10208632SBill.Moore@Sun.COM ASSERT(zio != NULL); 10218632SBill.Moore@Sun.COM return (NULL); 10228632SBill.Moore@Sun.COM } 10237754SJeff.Bonwick@Sun.COM 10247754SJeff.Bonwick@Sun.COM for (int l = 1; l < VDEV_LABELS; l++) { 10258632SBill.Moore@Sun.COM zio_nowait(zio_read_phys(pio, vd, 10267754SJeff.Bonwick@Sun.COM vdev_label_offset(vd->vdev_psize, l, 10279056SLin.Ling@Sun.COM offsetof(vdev_label_t, vl_pad2)), 10289056SLin.Ling@Sun.COM VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 10297754SJeff.Bonwick@Sun.COM ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 10307754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 10317754SJeff.Bonwick@Sun.COM } 10327754SJeff.Bonwick@Sun.COM 10338632SBill.Moore@Sun.COM if (zio == NULL) 10348632SBill.Moore@Sun.COM return (pio); 10358632SBill.Moore@Sun.COM 10368632SBill.Moore@Sun.COM zio_nowait(pio); 10378632SBill.Moore@Sun.COM return (NULL); 10385329Sgw25295 } 10395329Sgw25295 10409846SEric.Taylor@Sun.COM static void 10419846SEric.Taylor@Sun.COM vdev_open_child(void *arg) 10429846SEric.Taylor@Sun.COM { 10439846SEric.Taylor@Sun.COM vdev_t *vd = arg; 10449846SEric.Taylor@Sun.COM 10459846SEric.Taylor@Sun.COM vd->vdev_open_thread = curthread; 10469846SEric.Taylor@Sun.COM vd->vdev_open_error = vdev_open(vd); 10479846SEric.Taylor@Sun.COM vd->vdev_open_thread = NULL; 10489846SEric.Taylor@Sun.COM } 10499846SEric.Taylor@Sun.COM 105010588SEric.Taylor@Sun.COM boolean_t 105110588SEric.Taylor@Sun.COM vdev_uses_zvols(vdev_t *vd) 105210588SEric.Taylor@Sun.COM { 105310588SEric.Taylor@Sun.COM if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 105410588SEric.Taylor@Sun.COM strlen(ZVOL_DIR)) == 0) 105510588SEric.Taylor@Sun.COM return (B_TRUE); 105610588SEric.Taylor@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 105710588SEric.Taylor@Sun.COM if (vdev_uses_zvols(vd->vdev_child[c])) 105810588SEric.Taylor@Sun.COM return (B_TRUE); 105910588SEric.Taylor@Sun.COM return (B_FALSE); 106010588SEric.Taylor@Sun.COM } 106110588SEric.Taylor@Sun.COM 10629846SEric.Taylor@Sun.COM void 10639846SEric.Taylor@Sun.COM vdev_open_children(vdev_t *vd) 10649846SEric.Taylor@Sun.COM { 10659846SEric.Taylor@Sun.COM taskq_t *tq; 10669846SEric.Taylor@Sun.COM int children = vd->vdev_children; 10679846SEric.Taylor@Sun.COM 106810588SEric.Taylor@Sun.COM /* 106910588SEric.Taylor@Sun.COM * in order to handle pools on top of zvols, do the opens 107010588SEric.Taylor@Sun.COM * in a single thread so that the same thread holds the 107110588SEric.Taylor@Sun.COM * spa_namespace_lock 107210588SEric.Taylor@Sun.COM */ 107310588SEric.Taylor@Sun.COM if (vdev_uses_zvols(vd)) { 107410588SEric.Taylor@Sun.COM for (int c = 0; c < children; c++) 107510588SEric.Taylor@Sun.COM vd->vdev_child[c]->vdev_open_error = 107610588SEric.Taylor@Sun.COM vdev_open(vd->vdev_child[c]); 107710588SEric.Taylor@Sun.COM return; 107810588SEric.Taylor@Sun.COM } 10799846SEric.Taylor@Sun.COM tq = taskq_create("vdev_open", children, minclsyspri, 10809846SEric.Taylor@Sun.COM children, children, TASKQ_PREPOPULATE); 10819846SEric.Taylor@Sun.COM 10829846SEric.Taylor@Sun.COM for (int c = 0; c < children; c++) 10839846SEric.Taylor@Sun.COM VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 10849846SEric.Taylor@Sun.COM TQ_SLEEP) != NULL); 10859846SEric.Taylor@Sun.COM 10869846SEric.Taylor@Sun.COM taskq_destroy(tq); 10879846SEric.Taylor@Sun.COM } 10889846SEric.Taylor@Sun.COM 1089789Sahrens /* 1090789Sahrens * Prepare a virtual device for access. 1091789Sahrens */ 1092789Sahrens int 1093789Sahrens vdev_open(vdev_t *vd) 1094789Sahrens { 10958241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 1096789Sahrens int error; 1097789Sahrens uint64_t osize = 0; 1098789Sahrens uint64_t asize, psize; 10991732Sbonwick uint64_t ashift = 0; 1100789Sahrens 11019846SEric.Taylor@Sun.COM ASSERT(vd->vdev_open_thread == curthread || 11029846SEric.Taylor@Sun.COM spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1103789Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1104789Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1105789Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1106789Sahrens 1107789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 11089701SGeorge.Wilson@Sun.COM vd->vdev_cant_read = B_FALSE; 11099701SGeorge.Wilson@Sun.COM vd->vdev_cant_write = B_FALSE; 11109816SGeorge.Wilson@Sun.COM vd->vdev_min_asize = vdev_get_min_asize(vd); 1111789Sahrens 111210817SEric.Schrock@Sun.COM /* 111310817SEric.Schrock@Sun.COM * If this vdev is not removed, check its fault status. If it's 111410817SEric.Schrock@Sun.COM * faulted, bail out of the open. 111510817SEric.Schrock@Sun.COM */ 11164451Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 11174451Seschrock ASSERT(vd->vdev_children == 0); 111810817SEric.Schrock@Sun.COM ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 111910817SEric.Schrock@Sun.COM vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 11204451Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 112110817SEric.Schrock@Sun.COM vd->vdev_label_aux); 11224451Seschrock return (ENXIO); 11234451Seschrock } else if (vd->vdev_offline) { 1124789Sahrens ASSERT(vd->vdev_children == 0); 11251544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1126789Sahrens return (ENXIO); 1127789Sahrens } 1128789Sahrens 1129789Sahrens error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 1130789Sahrens 113110850SGeorge.Wilson@Sun.COM /* 113210850SGeorge.Wilson@Sun.COM * Reset the vdev_reopening flag so that we actually close 113310850SGeorge.Wilson@Sun.COM * the vdev on error. 113410850SGeorge.Wilson@Sun.COM */ 113510850SGeorge.Wilson@Sun.COM vd->vdev_reopening = B_FALSE; 11361544Seschrock if (zio_injection_enabled && error == 0) 11379725SEric.Schrock@Sun.COM error = zio_handle_device_injection(vd, NULL, ENXIO); 11381544Seschrock 11394451Seschrock if (error) { 11404451Seschrock if (vd->vdev_removed && 11414451Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 11424451Seschrock vd->vdev_removed = B_FALSE; 1143789Sahrens 11441544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1145789Sahrens vd->vdev_stat.vs_aux); 1146789Sahrens return (error); 1147789Sahrens } 1148789Sahrens 11494451Seschrock vd->vdev_removed = B_FALSE; 11504451Seschrock 115110830SEric.Schrock@Sun.COM /* 115210830SEric.Schrock@Sun.COM * Recheck the faulted flag now that we have confirmed that 115310830SEric.Schrock@Sun.COM * the vdev is accessible. If we're faulted, bail. 115410830SEric.Schrock@Sun.COM */ 115510830SEric.Schrock@Sun.COM if (vd->vdev_faulted) { 115610830SEric.Schrock@Sun.COM ASSERT(vd->vdev_children == 0); 115710830SEric.Schrock@Sun.COM ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 115810830SEric.Schrock@Sun.COM vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 115910830SEric.Schrock@Sun.COM vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 116010830SEric.Schrock@Sun.COM vd->vdev_label_aux); 116110830SEric.Schrock@Sun.COM return (ENXIO); 116210830SEric.Schrock@Sun.COM } 116310830SEric.Schrock@Sun.COM 11644451Seschrock if (vd->vdev_degraded) { 11654451Seschrock ASSERT(vd->vdev_children == 0); 11664451Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 11674451Seschrock VDEV_AUX_ERR_EXCEEDED); 11684451Seschrock } else { 116910817SEric.Schrock@Sun.COM vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 11704451Seschrock } 1171789Sahrens 117210594SGeorge.Wilson@Sun.COM /* 117310594SGeorge.Wilson@Sun.COM * For hole or missing vdevs we just return success. 117410594SGeorge.Wilson@Sun.COM */ 117510594SGeorge.Wilson@Sun.COM if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 117610594SGeorge.Wilson@Sun.COM return (0); 117710594SGeorge.Wilson@Sun.COM 11789816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 11791544Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 11801544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 11811544Seschrock VDEV_AUX_NONE); 11821544Seschrock break; 11831544Seschrock } 11849816SGeorge.Wilson@Sun.COM } 1185789Sahrens 1186789Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1187789Sahrens 1188789Sahrens if (vd->vdev_children == 0) { 1189789Sahrens if (osize < SPA_MINDEVSIZE) { 11901544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 11911544Seschrock VDEV_AUX_TOO_SMALL); 1192789Sahrens return (EOVERFLOW); 1193789Sahrens } 1194789Sahrens psize = osize; 1195789Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1196789Sahrens } else { 11971732Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1198789Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 11991544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12001544Seschrock VDEV_AUX_TOO_SMALL); 1201789Sahrens return (EOVERFLOW); 1202789Sahrens } 1203789Sahrens psize = 0; 1204789Sahrens asize = osize; 1205789Sahrens } 1206789Sahrens 1207789Sahrens vd->vdev_psize = psize; 1208789Sahrens 12099816SGeorge.Wilson@Sun.COM /* 12109816SGeorge.Wilson@Sun.COM * Make sure the allocatable size hasn't shrunk. 12119816SGeorge.Wilson@Sun.COM */ 12129816SGeorge.Wilson@Sun.COM if (asize < vd->vdev_min_asize) { 12139816SGeorge.Wilson@Sun.COM vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12149816SGeorge.Wilson@Sun.COM VDEV_AUX_BAD_LABEL); 12159816SGeorge.Wilson@Sun.COM return (EINVAL); 12169816SGeorge.Wilson@Sun.COM } 12179816SGeorge.Wilson@Sun.COM 1218789Sahrens if (vd->vdev_asize == 0) { 1219789Sahrens /* 1220789Sahrens * This is the first-ever open, so use the computed values. 12211732Sbonwick * For testing purposes, a higher ashift can be requested. 1222789Sahrens */ 1223789Sahrens vd->vdev_asize = asize; 12241732Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1225789Sahrens } else { 1226789Sahrens /* 1227789Sahrens * Make sure the alignment requirement hasn't increased. 1228789Sahrens */ 12291732Sbonwick if (ashift > vd->vdev_top->vdev_ashift) { 12301544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12311544Seschrock VDEV_AUX_BAD_LABEL); 1232789Sahrens return (EINVAL); 1233789Sahrens } 1234789Sahrens } 1235789Sahrens 12361544Seschrock /* 12379816SGeorge.Wilson@Sun.COM * If all children are healthy and the asize has increased, 12389816SGeorge.Wilson@Sun.COM * then we've experienced dynamic LUN growth. If automatic 12399816SGeorge.Wilson@Sun.COM * expansion is enabled then use the additional space. 12409816SGeorge.Wilson@Sun.COM */ 12419816SGeorge.Wilson@Sun.COM if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize && 12429816SGeorge.Wilson@Sun.COM (vd->vdev_expanding || spa->spa_autoexpand)) 12439816SGeorge.Wilson@Sun.COM vd->vdev_asize = asize; 12449816SGeorge.Wilson@Sun.COM 12459816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vd); 12469816SGeorge.Wilson@Sun.COM 12479816SGeorge.Wilson@Sun.COM /* 12485329Sgw25295 * Ensure we can issue some IO before declaring the 12495329Sgw25295 * vdev open for business. 12505329Sgw25295 */ 12517754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && 12527754SJeff.Bonwick@Sun.COM (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 12535329Sgw25295 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12547754SJeff.Bonwick@Sun.COM VDEV_AUX_IO_FAILURE); 12555329Sgw25295 return (error); 12565329Sgw25295 } 12575329Sgw25295 12585329Sgw25295 /* 12597046Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 12608241SJeff.Bonwick@Sun.COM * resilver. But don't do this if we are doing a reopen for a scrub, 12618241SJeff.Bonwick@Sun.COM * since this would just restart the scrub we are already doing. 12627046Sahrens */ 12638241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 12648241SJeff.Bonwick@Sun.COM vdev_resilver_needed(vd, NULL, NULL)) 12658241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 12667046Sahrens 1267789Sahrens return (0); 1268789Sahrens } 1269789Sahrens 1270789Sahrens /* 12711986Seschrock * Called once the vdevs are all opened, this routine validates the label 12721986Seschrock * contents. This needs to be done before vdev_load() so that we don't 12734451Seschrock * inadvertently do repair I/Os to the wrong device. 12741986Seschrock * 12751986Seschrock * This function will only return failure if one of the vdevs indicates that it 12761986Seschrock * has since been destroyed or exported. This is only possible if 12771986Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 12781986Seschrock * will be updated but the function will return 0. 12791986Seschrock */ 12801986Seschrock int 12811986Seschrock vdev_validate(vdev_t *vd) 12821986Seschrock { 12831986Seschrock spa_t *spa = vd->vdev_spa; 12841986Seschrock nvlist_t *label; 1285*11422SMark.Musante@Sun.COM uint64_t guid = 0, top_guid; 12861986Seschrock uint64_t state; 12871986Seschrock 12889816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 12891986Seschrock if (vdev_validate(vd->vdev_child[c]) != 0) 12904070Smc142369 return (EBADF); 12911986Seschrock 12922174Seschrock /* 12932174Seschrock * If the device has already failed, or was marked offline, don't do 12942174Seschrock * any further validation. Otherwise, label I/O will fail and we will 12952174Seschrock * overwrite the previous state. 12962174Seschrock */ 12977754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1298*11422SMark.Musante@Sun.COM uint64_t aux_guid = 0; 1299*11422SMark.Musante@Sun.COM nvlist_t *nvl; 13001986Seschrock 13011986Seschrock if ((label = vdev_label_read_config(vd)) == NULL) { 13021986Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 13031986Seschrock VDEV_AUX_BAD_LABEL); 13041986Seschrock return (0); 13051986Seschrock } 13061986Seschrock 1307*11422SMark.Musante@Sun.COM /* 1308*11422SMark.Musante@Sun.COM * Determine if this vdev has been split off into another 1309*11422SMark.Musante@Sun.COM * pool. If so, then refuse to open it. 1310*11422SMark.Musante@Sun.COM */ 1311*11422SMark.Musante@Sun.COM if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1312*11422SMark.Musante@Sun.COM &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1313*11422SMark.Musante@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1314*11422SMark.Musante@Sun.COM VDEV_AUX_SPLIT_POOL); 1315*11422SMark.Musante@Sun.COM nvlist_free(label); 1316*11422SMark.Musante@Sun.COM return (0); 1317*11422SMark.Musante@Sun.COM } 1318*11422SMark.Musante@Sun.COM 13191986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 13201986Seschrock &guid) != 0 || guid != spa_guid(spa)) { 13211986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 13221986Seschrock VDEV_AUX_CORRUPT_DATA); 13231986Seschrock nvlist_free(label); 13241986Seschrock return (0); 13251986Seschrock } 13261986Seschrock 1327*11422SMark.Musante@Sun.COM if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1328*11422SMark.Musante@Sun.COM != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1329*11422SMark.Musante@Sun.COM &aux_guid) != 0) 1330*11422SMark.Musante@Sun.COM aux_guid = 0; 1331*11422SMark.Musante@Sun.COM 13327754SJeff.Bonwick@Sun.COM /* 13337754SJeff.Bonwick@Sun.COM * If this vdev just became a top-level vdev because its 13347754SJeff.Bonwick@Sun.COM * sibling was detached, it will have adopted the parent's 13357754SJeff.Bonwick@Sun.COM * vdev guid -- but the label may or may not be on disk yet. 13367754SJeff.Bonwick@Sun.COM * Fortunately, either version of the label will have the 13377754SJeff.Bonwick@Sun.COM * same top guid, so if we're a top-level vdev, we can 13387754SJeff.Bonwick@Sun.COM * safely compare to that instead. 1339*11422SMark.Musante@Sun.COM * 1340*11422SMark.Musante@Sun.COM * If we split this vdev off instead, then we also check the 1341*11422SMark.Musante@Sun.COM * original pool's guid. We don't want to consider the vdev 1342*11422SMark.Musante@Sun.COM * corrupt if it is partway through a split operation. 13437754SJeff.Bonwick@Sun.COM */ 13441986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 13457754SJeff.Bonwick@Sun.COM &guid) != 0 || 13467754SJeff.Bonwick@Sun.COM nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 13477754SJeff.Bonwick@Sun.COM &top_guid) != 0 || 1348*11422SMark.Musante@Sun.COM ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) && 13497754SJeff.Bonwick@Sun.COM (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 13501986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 13511986Seschrock VDEV_AUX_CORRUPT_DATA); 13521986Seschrock nvlist_free(label); 13531986Seschrock return (0); 13541986Seschrock } 13551986Seschrock 13561986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 13571986Seschrock &state) != 0) { 13581986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 13591986Seschrock VDEV_AUX_CORRUPT_DATA); 13601986Seschrock nvlist_free(label); 13611986Seschrock return (0); 13621986Seschrock } 13631986Seschrock 13641986Seschrock nvlist_free(label); 13651986Seschrock 136610100SLin.Ling@Sun.COM /* 136710100SLin.Ling@Sun.COM * If spa->spa_load_verbatim is true, no need to check the 136810100SLin.Ling@Sun.COM * state of the pool. 136910100SLin.Ling@Sun.COM */ 137010100SLin.Ling@Sun.COM if (!spa->spa_load_verbatim && 137111147SGeorge.Wilson@Sun.COM spa_load_state(spa) == SPA_LOAD_OPEN && 137210100SLin.Ling@Sun.COM state != POOL_STATE_ACTIVE) 13734070Smc142369 return (EBADF); 13746976Seschrock 13756976Seschrock /* 13766976Seschrock * If we were able to open and validate a vdev that was 13776976Seschrock * previously marked permanently unavailable, clear that state 13786976Seschrock * now. 13796976Seschrock */ 13806976Seschrock if (vd->vdev_not_present) 13816976Seschrock vd->vdev_not_present = 0; 13821986Seschrock } 13831986Seschrock 13841986Seschrock return (0); 13851986Seschrock } 13861986Seschrock 13871986Seschrock /* 1388789Sahrens * Close a virtual device. 1389789Sahrens */ 1390789Sahrens void 1391789Sahrens vdev_close(vdev_t *vd) 1392789Sahrens { 13938241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 139410850SGeorge.Wilson@Sun.COM vdev_t *pvd = vd->vdev_parent; 13958241SJeff.Bonwick@Sun.COM 13968241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 13978241SJeff.Bonwick@Sun.COM 1398*11422SMark.Musante@Sun.COM /* 1399*11422SMark.Musante@Sun.COM * If our parent is reopening, then we are as well, unless we are 1400*11422SMark.Musante@Sun.COM * going offline. 1401*11422SMark.Musante@Sun.COM */ 140210850SGeorge.Wilson@Sun.COM if (pvd != NULL && pvd->vdev_reopening) 1403*11422SMark.Musante@Sun.COM vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 140410850SGeorge.Wilson@Sun.COM 1405789Sahrens vd->vdev_ops->vdev_op_close(vd); 1406789Sahrens 14074451Seschrock vdev_cache_purge(vd); 1408789Sahrens 14091986Seschrock /* 14109816SGeorge.Wilson@Sun.COM * We record the previous state before we close it, so that if we are 14111986Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 14121986Seschrock * it's still faulted. 14131986Seschrock */ 14141986Seschrock vd->vdev_prevstate = vd->vdev_state; 14151986Seschrock 1416789Sahrens if (vd->vdev_offline) 1417789Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 1418789Sahrens else 1419789Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 14201544Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1421789Sahrens } 1422789Sahrens 142310850SGeorge.Wilson@Sun.COM /* 142410850SGeorge.Wilson@Sun.COM * Reopen all interior vdevs and any unopened leaves. We don't actually 142510850SGeorge.Wilson@Sun.COM * reopen leaf vdevs which had previously been opened as they might deadlock 142610850SGeorge.Wilson@Sun.COM * on the spa_config_lock. Instead we only obtain the leaf's physical size. 142710850SGeorge.Wilson@Sun.COM * If the leaf has never been opened then open it, as usual. 142810850SGeorge.Wilson@Sun.COM */ 1429789Sahrens void 14301544Seschrock vdev_reopen(vdev_t *vd) 1431789Sahrens { 14321544Seschrock spa_t *spa = vd->vdev_spa; 1433789Sahrens 14347754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 14351544Seschrock 1436*11422SMark.Musante@Sun.COM /* set the reopening flag unless we're taking the vdev offline */ 1437*11422SMark.Musante@Sun.COM vd->vdev_reopening = !vd->vdev_offline; 1438789Sahrens vdev_close(vd); 1439789Sahrens (void) vdev_open(vd); 1440789Sahrens 1441789Sahrens /* 14423377Seschrock * Call vdev_validate() here to make sure we have the same device. 14433377Seschrock * Otherwise, a device with an invalid label could be successfully 14443377Seschrock * opened in response to vdev_reopen(). 14453377Seschrock */ 14466643Seschrock if (vd->vdev_aux) { 14476643Seschrock (void) vdev_validate_aux(vd); 14487754SJeff.Bonwick@Sun.COM if (vdev_readable(vd) && vdev_writeable(vd) && 14499425SEric.Schrock@Sun.COM vd->vdev_aux == &spa->spa_l2cache && 14509816SGeorge.Wilson@Sun.COM !l2arc_vdev_present(vd)) 14519816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 14526643Seschrock } else { 14536643Seschrock (void) vdev_validate(vd); 14546643Seschrock } 14553377Seschrock 14563377Seschrock /* 14574451Seschrock * Reassess parent vdev's health. 1458789Sahrens */ 14594451Seschrock vdev_propagate_state(vd); 1460789Sahrens } 1461789Sahrens 1462789Sahrens int 14632082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1464789Sahrens { 1465789Sahrens int error; 1466789Sahrens 1467789Sahrens /* 1468789Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 1469789Sahrens * For a create, however, we want to fail the request if 1470789Sahrens * there are any components we can't open. 1471789Sahrens */ 1472789Sahrens error = vdev_open(vd); 1473789Sahrens 1474789Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1475789Sahrens vdev_close(vd); 1476789Sahrens return (error ? error : ENXIO); 1477789Sahrens } 1478789Sahrens 1479789Sahrens /* 1480789Sahrens * Recursively initialize all labels. 1481789Sahrens */ 14823377Seschrock if ((error = vdev_label_init(vd, txg, isreplacing ? 14833377Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1484789Sahrens vdev_close(vd); 1485789Sahrens return (error); 1486789Sahrens } 1487789Sahrens 1488789Sahrens return (0); 1489789Sahrens } 1490789Sahrens 14911585Sbonwick void 14929816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd) 1493789Sahrens { 1494789Sahrens /* 1495789Sahrens * Aim for roughly 200 metaslabs per vdev. 1496789Sahrens */ 1497789Sahrens vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 1498789Sahrens vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1499789Sahrens } 1500789Sahrens 1501789Sahrens void 15021732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1503789Sahrens { 15041732Sbonwick ASSERT(vd == vd->vdev_top); 150510594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 15061732Sbonwick ASSERT(ISP2(flags)); 1507789Sahrens 15081732Sbonwick if (flags & VDD_METASLAB) 15091732Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 15101732Sbonwick 15111732Sbonwick if (flags & VDD_DTL) 15121732Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 15131732Sbonwick 15141732Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1515789Sahrens } 1516789Sahrens 15178241SJeff.Bonwick@Sun.COM /* 15188241SJeff.Bonwick@Sun.COM * DTLs. 15198241SJeff.Bonwick@Sun.COM * 15208241SJeff.Bonwick@Sun.COM * A vdev's DTL (dirty time log) is the set of transaction groups for which 15218241SJeff.Bonwick@Sun.COM * the vdev has less than perfect replication. There are three kinds of DTL: 15228241SJeff.Bonwick@Sun.COM * 15238241SJeff.Bonwick@Sun.COM * DTL_MISSING: txgs for which the vdev has no valid copies of the data 15248241SJeff.Bonwick@Sun.COM * 15258241SJeff.Bonwick@Sun.COM * DTL_PARTIAL: txgs for which data is available, but not fully replicated 15268241SJeff.Bonwick@Sun.COM * 15278241SJeff.Bonwick@Sun.COM * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 15288241SJeff.Bonwick@Sun.COM * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 15298241SJeff.Bonwick@Sun.COM * txgs that was scrubbed. 15308241SJeff.Bonwick@Sun.COM * 15318241SJeff.Bonwick@Sun.COM * DTL_OUTAGE: txgs which cannot currently be read, whether due to 15328241SJeff.Bonwick@Sun.COM * persistent errors or just some device being offline. 15338241SJeff.Bonwick@Sun.COM * Unlike the other three, the DTL_OUTAGE map is not generally 15348241SJeff.Bonwick@Sun.COM * maintained; it's only computed when needed, typically to 15358241SJeff.Bonwick@Sun.COM * determine whether a device can be detached. 15368241SJeff.Bonwick@Sun.COM * 15378241SJeff.Bonwick@Sun.COM * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 15388241SJeff.Bonwick@Sun.COM * either has the data or it doesn't. 15398241SJeff.Bonwick@Sun.COM * 15408241SJeff.Bonwick@Sun.COM * For interior vdevs such as mirror and RAID-Z the picture is more complex. 15418241SJeff.Bonwick@Sun.COM * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 15428241SJeff.Bonwick@Sun.COM * if any child is less than fully replicated, then so is its parent. 15438241SJeff.Bonwick@Sun.COM * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 15448241SJeff.Bonwick@Sun.COM * comprising only those txgs which appear in 'maxfaults' or more children; 15458241SJeff.Bonwick@Sun.COM * those are the txgs we don't have enough replication to read. For example, 15468241SJeff.Bonwick@Sun.COM * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 15478241SJeff.Bonwick@Sun.COM * thus, its DTL_MISSING consists of the set of txgs that appear in more than 15488241SJeff.Bonwick@Sun.COM * two child DTL_MISSING maps. 15498241SJeff.Bonwick@Sun.COM * 15508241SJeff.Bonwick@Sun.COM * It should be clear from the above that to compute the DTLs and outage maps 15518241SJeff.Bonwick@Sun.COM * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 15528241SJeff.Bonwick@Sun.COM * Therefore, that is all we keep on disk. When loading the pool, or after 15538241SJeff.Bonwick@Sun.COM * a configuration change, we generate all other DTLs from first principles. 15548241SJeff.Bonwick@Sun.COM */ 1555789Sahrens void 15568241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1557789Sahrens { 15588241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 15598241SJeff.Bonwick@Sun.COM 15608241SJeff.Bonwick@Sun.COM ASSERT(t < DTL_TYPES); 15618241SJeff.Bonwick@Sun.COM ASSERT(vd != vd->vdev_spa->spa_root_vdev); 15628241SJeff.Bonwick@Sun.COM 1563789Sahrens mutex_enter(sm->sm_lock); 1564789Sahrens if (!space_map_contains(sm, txg, size)) 1565789Sahrens space_map_add(sm, txg, size); 1566789Sahrens mutex_exit(sm->sm_lock); 1567789Sahrens } 1568789Sahrens 15698241SJeff.Bonwick@Sun.COM boolean_t 15708241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1571789Sahrens { 15728241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 15738241SJeff.Bonwick@Sun.COM boolean_t dirty = B_FALSE; 15748241SJeff.Bonwick@Sun.COM 15758241SJeff.Bonwick@Sun.COM ASSERT(t < DTL_TYPES); 15768241SJeff.Bonwick@Sun.COM ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1577789Sahrens 1578789Sahrens mutex_enter(sm->sm_lock); 15798241SJeff.Bonwick@Sun.COM if (sm->sm_space != 0) 15808241SJeff.Bonwick@Sun.COM dirty = space_map_contains(sm, txg, size); 1581789Sahrens mutex_exit(sm->sm_lock); 1582789Sahrens 1583789Sahrens return (dirty); 1584789Sahrens } 1585789Sahrens 15868241SJeff.Bonwick@Sun.COM boolean_t 15878241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 15888241SJeff.Bonwick@Sun.COM { 15898241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 15908241SJeff.Bonwick@Sun.COM boolean_t empty; 15918241SJeff.Bonwick@Sun.COM 15928241SJeff.Bonwick@Sun.COM mutex_enter(sm->sm_lock); 15938241SJeff.Bonwick@Sun.COM empty = (sm->sm_space == 0); 15948241SJeff.Bonwick@Sun.COM mutex_exit(sm->sm_lock); 15958241SJeff.Bonwick@Sun.COM 15968241SJeff.Bonwick@Sun.COM return (empty); 15978241SJeff.Bonwick@Sun.COM } 15988241SJeff.Bonwick@Sun.COM 1599789Sahrens /* 1600789Sahrens * Reassess DTLs after a config change or scrub completion. 1601789Sahrens */ 1602789Sahrens void 1603789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1604789Sahrens { 16051544Seschrock spa_t *spa = vd->vdev_spa; 16068241SJeff.Bonwick@Sun.COM avl_tree_t reftree; 16078241SJeff.Bonwick@Sun.COM int minref; 16088241SJeff.Bonwick@Sun.COM 16098241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 16108241SJeff.Bonwick@Sun.COM 16118241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 16128241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(vd->vdev_child[c], txg, 16138241SJeff.Bonwick@Sun.COM scrub_txg, scrub_done); 16148241SJeff.Bonwick@Sun.COM 161510922SJeff.Bonwick@Sun.COM if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux) 16168241SJeff.Bonwick@Sun.COM return; 16178241SJeff.Bonwick@Sun.COM 16188241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 1619789Sahrens mutex_enter(&vd->vdev_dtl_lock); 16207046Sahrens if (scrub_txg != 0 && 16217046Sahrens (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) { 16227046Sahrens /* XXX should check scrub_done? */ 16237046Sahrens /* 16247046Sahrens * We completed a scrub up to scrub_txg. If we 16257046Sahrens * did it without rebooting, then the scrub dtl 16267046Sahrens * will be valid, so excise the old region and 16277046Sahrens * fold in the scrub dtl. Otherwise, leave the 16287046Sahrens * dtl as-is if there was an error. 16298241SJeff.Bonwick@Sun.COM * 16308241SJeff.Bonwick@Sun.COM * There's little trick here: to excise the beginning 16318241SJeff.Bonwick@Sun.COM * of the DTL_MISSING map, we put it into a reference 16328241SJeff.Bonwick@Sun.COM * tree and then add a segment with refcnt -1 that 16338241SJeff.Bonwick@Sun.COM * covers the range [0, scrub_txg). This means 16348241SJeff.Bonwick@Sun.COM * that each txg in that range has refcnt -1 or 0. 16358241SJeff.Bonwick@Sun.COM * We then add DTL_SCRUB with a refcnt of 2, so that 16368241SJeff.Bonwick@Sun.COM * entries in the range [0, scrub_txg) will have a 16378241SJeff.Bonwick@Sun.COM * positive refcnt -- either 1 or 2. We then convert 16388241SJeff.Bonwick@Sun.COM * the reference tree into the new DTL_MISSING map. 16397046Sahrens */ 16408241SJeff.Bonwick@Sun.COM space_map_ref_create(&reftree); 16418241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, 16428241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_MISSING], 1); 16438241SJeff.Bonwick@Sun.COM space_map_ref_add_seg(&reftree, 0, scrub_txg, -1); 16448241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, 16458241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_SCRUB], 2); 16468241SJeff.Bonwick@Sun.COM space_map_ref_generate_map(&reftree, 16478241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_MISSING], 1); 16488241SJeff.Bonwick@Sun.COM space_map_ref_destroy(&reftree); 1649789Sahrens } 16508241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 16518241SJeff.Bonwick@Sun.COM space_map_walk(&vd->vdev_dtl[DTL_MISSING], 16528241SJeff.Bonwick@Sun.COM space_map_add, &vd->vdev_dtl[DTL_PARTIAL]); 1653789Sahrens if (scrub_done) 16548241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 16558241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 16568241SJeff.Bonwick@Sun.COM if (!vdev_readable(vd)) 16578241SJeff.Bonwick@Sun.COM space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 16588241SJeff.Bonwick@Sun.COM else 16598241SJeff.Bonwick@Sun.COM space_map_walk(&vd->vdev_dtl[DTL_MISSING], 16608241SJeff.Bonwick@Sun.COM space_map_add, &vd->vdev_dtl[DTL_OUTAGE]); 1661789Sahrens mutex_exit(&vd->vdev_dtl_lock); 16627046Sahrens 16631732Sbonwick if (txg != 0) 16641732Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1665789Sahrens return; 1666789Sahrens } 1667789Sahrens 1668789Sahrens mutex_enter(&vd->vdev_dtl_lock); 16698241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 167010890SEric.Taylor@Sun.COM /* account for child's outage in parent's missing map */ 167110890SEric.Taylor@Sun.COM int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 16728241SJeff.Bonwick@Sun.COM if (t == DTL_SCRUB) 16738241SJeff.Bonwick@Sun.COM continue; /* leaf vdevs only */ 16748241SJeff.Bonwick@Sun.COM if (t == DTL_PARTIAL) 16758241SJeff.Bonwick@Sun.COM minref = 1; /* i.e. non-zero */ 16768241SJeff.Bonwick@Sun.COM else if (vd->vdev_nparity != 0) 16778241SJeff.Bonwick@Sun.COM minref = vd->vdev_nparity + 1; /* RAID-Z */ 16788241SJeff.Bonwick@Sun.COM else 16798241SJeff.Bonwick@Sun.COM minref = vd->vdev_children; /* any kind of mirror */ 16808241SJeff.Bonwick@Sun.COM space_map_ref_create(&reftree); 16818241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 16828241SJeff.Bonwick@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 16838241SJeff.Bonwick@Sun.COM mutex_enter(&cvd->vdev_dtl_lock); 168410890SEric.Taylor@Sun.COM space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1); 16858241SJeff.Bonwick@Sun.COM mutex_exit(&cvd->vdev_dtl_lock); 16868241SJeff.Bonwick@Sun.COM } 16878241SJeff.Bonwick@Sun.COM space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref); 16888241SJeff.Bonwick@Sun.COM space_map_ref_destroy(&reftree); 16898241SJeff.Bonwick@Sun.COM } 1690789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1691789Sahrens } 1692789Sahrens 1693789Sahrens static int 1694789Sahrens vdev_dtl_load(vdev_t *vd) 1695789Sahrens { 1696789Sahrens spa_t *spa = vd->vdev_spa; 16978241SJeff.Bonwick@Sun.COM space_map_obj_t *smo = &vd->vdev_dtl_smo; 16981732Sbonwick objset_t *mos = spa->spa_meta_objset; 1699789Sahrens dmu_buf_t *db; 1700789Sahrens int error; 1701789Sahrens 1702789Sahrens ASSERT(vd->vdev_children == 0); 1703789Sahrens 1704789Sahrens if (smo->smo_object == 0) 1705789Sahrens return (0); 1706789Sahrens 170710594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 170810594SGeorge.Wilson@Sun.COM 17091732Sbonwick if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 17101544Seschrock return (error); 17111732Sbonwick 17124944Smaybee ASSERT3U(db->db_size, >=, sizeof (*smo)); 17134944Smaybee bcopy(db->db_data, smo, sizeof (*smo)); 17141544Seschrock dmu_buf_rele(db, FTAG); 1715789Sahrens 1716789Sahrens mutex_enter(&vd->vdev_dtl_lock); 17178241SJeff.Bonwick@Sun.COM error = space_map_load(&vd->vdev_dtl[DTL_MISSING], 17188241SJeff.Bonwick@Sun.COM NULL, SM_ALLOC, smo, mos); 1719789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1720789Sahrens 1721789Sahrens return (error); 1722789Sahrens } 1723789Sahrens 1724789Sahrens void 1725789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1726789Sahrens { 1727789Sahrens spa_t *spa = vd->vdev_spa; 17288241SJeff.Bonwick@Sun.COM space_map_obj_t *smo = &vd->vdev_dtl_smo; 17298241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[DTL_MISSING]; 17301732Sbonwick objset_t *mos = spa->spa_meta_objset; 1731789Sahrens space_map_t smsync; 1732789Sahrens kmutex_t smlock; 1733789Sahrens dmu_buf_t *db; 1734789Sahrens dmu_tx_t *tx; 1735789Sahrens 173610594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 173710594SGeorge.Wilson@Sun.COM 1738789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1739789Sahrens 1740789Sahrens if (vd->vdev_detached) { 1741789Sahrens if (smo->smo_object != 0) { 17421732Sbonwick int err = dmu_object_free(mos, smo->smo_object, tx); 1743789Sahrens ASSERT3U(err, ==, 0); 1744789Sahrens smo->smo_object = 0; 1745789Sahrens } 1746789Sahrens dmu_tx_commit(tx); 1747789Sahrens return; 1748789Sahrens } 1749789Sahrens 1750789Sahrens if (smo->smo_object == 0) { 1751789Sahrens ASSERT(smo->smo_objsize == 0); 1752789Sahrens ASSERT(smo->smo_alloc == 0); 17531732Sbonwick smo->smo_object = dmu_object_alloc(mos, 1754789Sahrens DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1755789Sahrens DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1756789Sahrens ASSERT(smo->smo_object != 0); 1757789Sahrens vdev_config_dirty(vd->vdev_top); 1758789Sahrens } 1759789Sahrens 1760789Sahrens mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1761789Sahrens 1762789Sahrens space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1763789Sahrens &smlock); 1764789Sahrens 1765789Sahrens mutex_enter(&smlock); 1766789Sahrens 1767789Sahrens mutex_enter(&vd->vdev_dtl_lock); 17681732Sbonwick space_map_walk(sm, space_map_add, &smsync); 1769789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1770789Sahrens 17711732Sbonwick space_map_truncate(smo, mos, tx); 17721732Sbonwick space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1773789Sahrens 1774789Sahrens space_map_destroy(&smsync); 1775789Sahrens 1776789Sahrens mutex_exit(&smlock); 1777789Sahrens mutex_destroy(&smlock); 1778789Sahrens 17791732Sbonwick VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1780789Sahrens dmu_buf_will_dirty(db, tx); 17814944Smaybee ASSERT3U(db->db_size, >=, sizeof (*smo)); 17824944Smaybee bcopy(smo, db->db_data, sizeof (*smo)); 17831544Seschrock dmu_buf_rele(db, FTAG); 1784789Sahrens 1785789Sahrens dmu_tx_commit(tx); 1786789Sahrens } 1787789Sahrens 17887046Sahrens /* 17898241SJeff.Bonwick@Sun.COM * Determine whether the specified vdev can be offlined/detached/removed 17908241SJeff.Bonwick@Sun.COM * without losing data. 17918241SJeff.Bonwick@Sun.COM */ 17928241SJeff.Bonwick@Sun.COM boolean_t 17938241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd) 17948241SJeff.Bonwick@Sun.COM { 17958241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 17968241SJeff.Bonwick@Sun.COM vdev_t *tvd = vd->vdev_top; 17978241SJeff.Bonwick@Sun.COM uint8_t cant_read = vd->vdev_cant_read; 17988241SJeff.Bonwick@Sun.COM boolean_t required; 17998241SJeff.Bonwick@Sun.COM 18008241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 18018241SJeff.Bonwick@Sun.COM 18028241SJeff.Bonwick@Sun.COM if (vd == spa->spa_root_vdev || vd == tvd) 18038241SJeff.Bonwick@Sun.COM return (B_TRUE); 18048241SJeff.Bonwick@Sun.COM 18058241SJeff.Bonwick@Sun.COM /* 18068241SJeff.Bonwick@Sun.COM * Temporarily mark the device as unreadable, and then determine 18078241SJeff.Bonwick@Sun.COM * whether this results in any DTL outages in the top-level vdev. 18088241SJeff.Bonwick@Sun.COM * If not, we can safely offline/detach/remove the device. 18098241SJeff.Bonwick@Sun.COM */ 18108241SJeff.Bonwick@Sun.COM vd->vdev_cant_read = B_TRUE; 18118241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 18128241SJeff.Bonwick@Sun.COM required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 18138241SJeff.Bonwick@Sun.COM vd->vdev_cant_read = cant_read; 18148241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 18158241SJeff.Bonwick@Sun.COM 18168241SJeff.Bonwick@Sun.COM return (required); 18178241SJeff.Bonwick@Sun.COM } 18188241SJeff.Bonwick@Sun.COM 18198241SJeff.Bonwick@Sun.COM /* 18207046Sahrens * Determine if resilver is needed, and if so the txg range. 18217046Sahrens */ 18227046Sahrens boolean_t 18237046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 18247046Sahrens { 18257046Sahrens boolean_t needed = B_FALSE; 18267046Sahrens uint64_t thismin = UINT64_MAX; 18277046Sahrens uint64_t thismax = 0; 18287046Sahrens 18297046Sahrens if (vd->vdev_children == 0) { 18307046Sahrens mutex_enter(&vd->vdev_dtl_lock); 18318241SJeff.Bonwick@Sun.COM if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 && 18328241SJeff.Bonwick@Sun.COM vdev_writeable(vd)) { 18337046Sahrens space_seg_t *ss; 18347046Sahrens 18358241SJeff.Bonwick@Sun.COM ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root); 18367046Sahrens thismin = ss->ss_start - 1; 18378241SJeff.Bonwick@Sun.COM ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root); 18387046Sahrens thismax = ss->ss_end; 18397046Sahrens needed = B_TRUE; 18407046Sahrens } 18417046Sahrens mutex_exit(&vd->vdev_dtl_lock); 18427046Sahrens } else { 18438241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 18447046Sahrens vdev_t *cvd = vd->vdev_child[c]; 18457046Sahrens uint64_t cmin, cmax; 18467046Sahrens 18477046Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 18487046Sahrens thismin = MIN(thismin, cmin); 18497046Sahrens thismax = MAX(thismax, cmax); 18507046Sahrens needed = B_TRUE; 18517046Sahrens } 18527046Sahrens } 18537046Sahrens } 18547046Sahrens 18557046Sahrens if (needed && minp) { 18567046Sahrens *minp = thismin; 18577046Sahrens *maxp = thismax; 18587046Sahrens } 18597046Sahrens return (needed); 18607046Sahrens } 18617046Sahrens 18621986Seschrock void 18631544Seschrock vdev_load(vdev_t *vd) 1864789Sahrens { 1865789Sahrens /* 1866789Sahrens * Recursively load all children. 1867789Sahrens */ 18688241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 18691986Seschrock vdev_load(vd->vdev_child[c]); 1870789Sahrens 1871789Sahrens /* 18721585Sbonwick * If this is a top-level vdev, initialize its metaslabs. 1873789Sahrens */ 187410594SGeorge.Wilson@Sun.COM if (vd == vd->vdev_top && !vd->vdev_ishole && 18751986Seschrock (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 18761986Seschrock vdev_metaslab_init(vd, 0) != 0)) 18771986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18781986Seschrock VDEV_AUX_CORRUPT_DATA); 1879789Sahrens 1880789Sahrens /* 1881789Sahrens * If this is a leaf vdev, load its DTL. 1882789Sahrens */ 18831986Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 18841986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18851986Seschrock VDEV_AUX_CORRUPT_DATA); 1886789Sahrens } 1887789Sahrens 18882082Seschrock /* 18895450Sbrendan * The special vdev case is used for hot spares and l2cache devices. Its 18905450Sbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 18915450Sbrendan * we make sure that we can open the underlying device, then try to read the 18925450Sbrendan * label, and make sure that the label is sane and that it hasn't been 18935450Sbrendan * repurposed to another pool. 18942082Seschrock */ 18952082Seschrock int 18965450Sbrendan vdev_validate_aux(vdev_t *vd) 18972082Seschrock { 18982082Seschrock nvlist_t *label; 18992082Seschrock uint64_t guid, version; 19002082Seschrock uint64_t state; 19012082Seschrock 19027754SJeff.Bonwick@Sun.COM if (!vdev_readable(vd)) 19036643Seschrock return (0); 19046643Seschrock 19052082Seschrock if ((label = vdev_label_read_config(vd)) == NULL) { 19062082Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 19072082Seschrock VDEV_AUX_CORRUPT_DATA); 19082082Seschrock return (-1); 19092082Seschrock } 19102082Seschrock 19112082Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 19124577Sahrens version > SPA_VERSION || 19132082Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 19142082Seschrock guid != vd->vdev_guid || 19152082Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 19162082Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 19172082Seschrock VDEV_AUX_CORRUPT_DATA); 19182082Seschrock nvlist_free(label); 19192082Seschrock return (-1); 19202082Seschrock } 19212082Seschrock 19222082Seschrock /* 19232082Seschrock * We don't actually check the pool state here. If it's in fact in 19242082Seschrock * use by another pool, we update this fact on the fly when requested. 19252082Seschrock */ 19262082Seschrock nvlist_free(label); 19272082Seschrock return (0); 19282082Seschrock } 19292082Seschrock 1930789Sahrens void 193110594SGeorge.Wilson@Sun.COM vdev_remove(vdev_t *vd, uint64_t txg) 193210594SGeorge.Wilson@Sun.COM { 193310594SGeorge.Wilson@Sun.COM spa_t *spa = vd->vdev_spa; 193410594SGeorge.Wilson@Sun.COM objset_t *mos = spa->spa_meta_objset; 193510594SGeorge.Wilson@Sun.COM dmu_tx_t *tx; 193610594SGeorge.Wilson@Sun.COM 193710594SGeorge.Wilson@Sun.COM tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 193810594SGeorge.Wilson@Sun.COM 193910594SGeorge.Wilson@Sun.COM if (vd->vdev_dtl_smo.smo_object) { 194010594SGeorge.Wilson@Sun.COM ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0); 194110594SGeorge.Wilson@Sun.COM (void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx); 194210594SGeorge.Wilson@Sun.COM vd->vdev_dtl_smo.smo_object = 0; 194310594SGeorge.Wilson@Sun.COM } 194410594SGeorge.Wilson@Sun.COM 194510594SGeorge.Wilson@Sun.COM if (vd->vdev_ms != NULL) { 194610594SGeorge.Wilson@Sun.COM for (int m = 0; m < vd->vdev_ms_count; m++) { 194710594SGeorge.Wilson@Sun.COM metaslab_t *msp = vd->vdev_ms[m]; 194810594SGeorge.Wilson@Sun.COM 194910594SGeorge.Wilson@Sun.COM if (msp == NULL || msp->ms_smo.smo_object == 0) 195010594SGeorge.Wilson@Sun.COM continue; 195110594SGeorge.Wilson@Sun.COM 195210594SGeorge.Wilson@Sun.COM ASSERT3U(msp->ms_smo.smo_alloc, ==, 0); 195310594SGeorge.Wilson@Sun.COM (void) dmu_object_free(mos, msp->ms_smo.smo_object, tx); 195410594SGeorge.Wilson@Sun.COM msp->ms_smo.smo_object = 0; 195510594SGeorge.Wilson@Sun.COM } 195610594SGeorge.Wilson@Sun.COM } 195710594SGeorge.Wilson@Sun.COM 195810594SGeorge.Wilson@Sun.COM if (vd->vdev_ms_array) { 195910594SGeorge.Wilson@Sun.COM (void) dmu_object_free(mos, vd->vdev_ms_array, tx); 196010594SGeorge.Wilson@Sun.COM vd->vdev_ms_array = 0; 196110594SGeorge.Wilson@Sun.COM vd->vdev_ms_shift = 0; 196210594SGeorge.Wilson@Sun.COM } 196310594SGeorge.Wilson@Sun.COM dmu_tx_commit(tx); 196410594SGeorge.Wilson@Sun.COM } 196510594SGeorge.Wilson@Sun.COM 196610594SGeorge.Wilson@Sun.COM void 1967789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 1968789Sahrens { 1969789Sahrens metaslab_t *msp; 197011146SGeorge.Wilson@Sun.COM boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 1971789Sahrens 197210594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 197310594SGeorge.Wilson@Sun.COM 1974789Sahrens while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1975789Sahrens metaslab_sync_done(msp, txg); 197611146SGeorge.Wilson@Sun.COM 197711146SGeorge.Wilson@Sun.COM if (reassess) 197811146SGeorge.Wilson@Sun.COM metaslab_sync_reassess(vd->vdev_mg); 1979789Sahrens } 1980789Sahrens 1981789Sahrens void 1982789Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 1983789Sahrens { 1984789Sahrens spa_t *spa = vd->vdev_spa; 1985789Sahrens vdev_t *lvd; 1986789Sahrens metaslab_t *msp; 19871732Sbonwick dmu_tx_t *tx; 1988789Sahrens 198910594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 199010594SGeorge.Wilson@Sun.COM 19911732Sbonwick if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 19921732Sbonwick ASSERT(vd == vd->vdev_top); 19931732Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 19941732Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 19951732Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 19961732Sbonwick ASSERT(vd->vdev_ms_array != 0); 19971732Sbonwick vdev_config_dirty(vd); 19981732Sbonwick dmu_tx_commit(tx); 19991732Sbonwick } 2000789Sahrens 200110594SGeorge.Wilson@Sun.COM if (vd->vdev_removing) 200210594SGeorge.Wilson@Sun.COM vdev_remove(vd, txg); 200310594SGeorge.Wilson@Sun.COM 20041732Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2005789Sahrens metaslab_sync(msp, txg); 20061732Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 20071732Sbonwick } 2008789Sahrens 2009789Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2010789Sahrens vdev_dtl_sync(lvd, txg); 2011789Sahrens 2012789Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2013789Sahrens } 2014789Sahrens 2015789Sahrens uint64_t 2016789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2017789Sahrens { 2018789Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2019789Sahrens } 2020789Sahrens 20214451Seschrock /* 20224451Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 20234451Seschrock * not be opened, and no I/O is attempted. 20244451Seschrock */ 2025789Sahrens int 202610817SEric.Schrock@Sun.COM vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 20274451Seschrock { 20286643Seschrock vdev_t *vd; 20294451Seschrock 203010685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 20314451Seschrock 20326643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 20337754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 20347754SJeff.Bonwick@Sun.COM 20354451Seschrock if (!vd->vdev_ops->vdev_op_leaf) 20367754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 20374451Seschrock 20384451Seschrock /* 203910817SEric.Schrock@Sun.COM * We don't directly use the aux state here, but if we do a 204010817SEric.Schrock@Sun.COM * vdev_reopen(), we need this value to be present to remember why we 204110817SEric.Schrock@Sun.COM * were faulted. 204210817SEric.Schrock@Sun.COM */ 204310817SEric.Schrock@Sun.COM vd->vdev_label_aux = aux; 204410817SEric.Schrock@Sun.COM 204510817SEric.Schrock@Sun.COM /* 20464451Seschrock * Faulted state takes precedence over degraded. 20474451Seschrock */ 20484451Seschrock vd->vdev_faulted = 1ULL; 20494451Seschrock vd->vdev_degraded = 0ULL; 205010817SEric.Schrock@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 20514451Seschrock 20524451Seschrock /* 20538123SDavid.Marker@sun.com * If marking the vdev as faulted cause the top-level vdev to become 20544451Seschrock * unavailable, then back off and simply mark the vdev as degraded 20554451Seschrock * instead. 20564451Seschrock */ 205710685SGeorge.Wilson@Sun.COM if (vdev_is_dead(vd->vdev_top) && !vd->vdev_islog && 205810685SGeorge.Wilson@Sun.COM vd->vdev_aux == NULL) { 20594451Seschrock vd->vdev_degraded = 1ULL; 20604451Seschrock vd->vdev_faulted = 0ULL; 20614451Seschrock 20624451Seschrock /* 20634451Seschrock * If we reopen the device and it's not dead, only then do we 20644451Seschrock * mark it degraded. 20654451Seschrock */ 20664451Seschrock vdev_reopen(vd); 20674451Seschrock 206810817SEric.Schrock@Sun.COM if (vdev_readable(vd)) 206910817SEric.Schrock@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 20704451Seschrock } 20714451Seschrock 20727754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 20734451Seschrock } 20744451Seschrock 20754451Seschrock /* 20764451Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 20774451Seschrock * user that something is wrong. The vdev continues to operate as normal as far 20784451Seschrock * as I/O is concerned. 20794451Seschrock */ 20804451Seschrock int 208110817SEric.Schrock@Sun.COM vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 20824451Seschrock { 20836643Seschrock vdev_t *vd; 20844451Seschrock 208510685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 20864451Seschrock 20876643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 20887754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 20897754SJeff.Bonwick@Sun.COM 20904451Seschrock if (!vd->vdev_ops->vdev_op_leaf) 20917754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 20924451Seschrock 20934451Seschrock /* 20944451Seschrock * If the vdev is already faulted, then don't do anything. 20954451Seschrock */ 20967754SJeff.Bonwick@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded) 20977754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, 0)); 20984451Seschrock 20994451Seschrock vd->vdev_degraded = 1ULL; 21004451Seschrock if (!vdev_is_dead(vd)) 21014451Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 210210817SEric.Schrock@Sun.COM aux); 21034451Seschrock 21047754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 21054451Seschrock } 21064451Seschrock 21074451Seschrock /* 21084451Seschrock * Online the given vdev. If 'unspare' is set, it implies two things. First, 21094451Seschrock * any attached spare device should be detached when the device finishes 21104451Seschrock * resilvering. Second, the online should be treated like a 'test' online case, 21114451Seschrock * so no FMA events are generated if the device fails to open. 21124451Seschrock */ 21134451Seschrock int 21147754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2115789Sahrens { 21169816SGeorge.Wilson@Sun.COM vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2117789Sahrens 211810685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 21191485Slling 21206643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 21217754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2122789Sahrens 21231585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 21247754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 21251585Sbonwick 21269816SGeorge.Wilson@Sun.COM tvd = vd->vdev_top; 2127789Sahrens vd->vdev_offline = B_FALSE; 21281485Slling vd->vdev_tmpoffline = B_FALSE; 21297754SJeff.Bonwick@Sun.COM vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 21307754SJeff.Bonwick@Sun.COM vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 21319816SGeorge.Wilson@Sun.COM 21329816SGeorge.Wilson@Sun.COM /* XXX - L2ARC 1.0 does not support expansion */ 21339816SGeorge.Wilson@Sun.COM if (!vd->vdev_aux) { 21349816SGeorge.Wilson@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 21359816SGeorge.Wilson@Sun.COM pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 21369816SGeorge.Wilson@Sun.COM } 21379816SGeorge.Wilson@Sun.COM 21389816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 21394451Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 21404451Seschrock 21419816SGeorge.Wilson@Sun.COM if (!vd->vdev_aux) { 21429816SGeorge.Wilson@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 21439816SGeorge.Wilson@Sun.COM pvd->vdev_expanding = B_FALSE; 21449816SGeorge.Wilson@Sun.COM } 21459816SGeorge.Wilson@Sun.COM 21464451Seschrock if (newstate) 21474451Seschrock *newstate = vd->vdev_state; 21484451Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 21494451Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 21504451Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 21514451Seschrock vd->vdev_parent->vdev_child[0] == vd) 21524451Seschrock vd->vdev_unspare = B_TRUE; 2153789Sahrens 21549816SGeorge.Wilson@Sun.COM if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 21559816SGeorge.Wilson@Sun.COM 21569816SGeorge.Wilson@Sun.COM /* XXX - L2ARC 1.0 does not support expansion */ 21579816SGeorge.Wilson@Sun.COM if (vd->vdev_aux) 21589816SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 21599816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 21609816SGeorge.Wilson@Sun.COM } 21618241SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 2162789Sahrens } 2163789Sahrens 216410974SJeff.Bonwick@Sun.COM static int 216510974SJeff.Bonwick@Sun.COM vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 2166789Sahrens { 21679701SGeorge.Wilson@Sun.COM vdev_t *vd, *tvd; 216810685SGeorge.Wilson@Sun.COM int error = 0; 216910685SGeorge.Wilson@Sun.COM uint64_t generation; 217010685SGeorge.Wilson@Sun.COM metaslab_group_t *mg; 217110685SGeorge.Wilson@Sun.COM 217210685SGeorge.Wilson@Sun.COM top: 217310685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_ALLOC); 2174789Sahrens 21756643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 21767754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2177789Sahrens 21781585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 21797754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 21801585Sbonwick 21819701SGeorge.Wilson@Sun.COM tvd = vd->vdev_top; 218210685SGeorge.Wilson@Sun.COM mg = tvd->vdev_mg; 218310685SGeorge.Wilson@Sun.COM generation = spa->spa_config_generation + 1; 21849701SGeorge.Wilson@Sun.COM 2185789Sahrens /* 21861732Sbonwick * If the device isn't already offline, try to offline it. 2187789Sahrens */ 21881732Sbonwick if (!vd->vdev_offline) { 21891732Sbonwick /* 21908241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 21919701SGeorge.Wilson@Sun.COM * don't allow it to be offlined. Log devices are always 21929701SGeorge.Wilson@Sun.COM * expendable. 21931732Sbonwick */ 21949701SGeorge.Wilson@Sun.COM if (!tvd->vdev_islog && vd->vdev_aux == NULL && 21959701SGeorge.Wilson@Sun.COM vdev_dtl_required(vd)) 21967754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2197789Sahrens 21981732Sbonwick /* 219910922SJeff.Bonwick@Sun.COM * If the top-level is a slog and it has had allocations 220010922SJeff.Bonwick@Sun.COM * then proceed. We check that the vdev's metaslab group 220110922SJeff.Bonwick@Sun.COM * is not NULL since it's possible that we may have just 220210922SJeff.Bonwick@Sun.COM * added this vdev but not yet initialized its metaslabs. 220310685SGeorge.Wilson@Sun.COM */ 220410685SGeorge.Wilson@Sun.COM if (tvd->vdev_islog && mg != NULL) { 220510685SGeorge.Wilson@Sun.COM /* 220610685SGeorge.Wilson@Sun.COM * Prevent any future allocations. 220710685SGeorge.Wilson@Sun.COM */ 220810974SJeff.Bonwick@Sun.COM metaslab_group_passivate(mg); 220910685SGeorge.Wilson@Sun.COM (void) spa_vdev_state_exit(spa, vd, 0); 221010685SGeorge.Wilson@Sun.COM 2211*11422SMark.Musante@Sun.COM error = spa_offline_log(spa); 221210685SGeorge.Wilson@Sun.COM 221310685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_ALLOC); 221410685SGeorge.Wilson@Sun.COM 221510685SGeorge.Wilson@Sun.COM /* 221610685SGeorge.Wilson@Sun.COM * Check to see if the config has changed. 221710685SGeorge.Wilson@Sun.COM */ 221810685SGeorge.Wilson@Sun.COM if (error || generation != spa->spa_config_generation) { 221910974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 222010685SGeorge.Wilson@Sun.COM if (error) 222110685SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, 222210685SGeorge.Wilson@Sun.COM vd, error)); 222310685SGeorge.Wilson@Sun.COM (void) spa_vdev_state_exit(spa, vd, 0); 222410685SGeorge.Wilson@Sun.COM goto top; 222510685SGeorge.Wilson@Sun.COM } 222610685SGeorge.Wilson@Sun.COM ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0); 222710685SGeorge.Wilson@Sun.COM } 222810685SGeorge.Wilson@Sun.COM 222910685SGeorge.Wilson@Sun.COM /* 22301732Sbonwick * Offline this device and reopen its top-level vdev. 22319701SGeorge.Wilson@Sun.COM * If the top-level vdev is a log device then just offline 22329701SGeorge.Wilson@Sun.COM * it. Otherwise, if this action results in the top-level 22339701SGeorge.Wilson@Sun.COM * vdev becoming unusable, undo it and fail the request. 22341732Sbonwick */ 22351732Sbonwick vd->vdev_offline = B_TRUE; 22369701SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 22379701SGeorge.Wilson@Sun.COM 22389701SGeorge.Wilson@Sun.COM if (!tvd->vdev_islog && vd->vdev_aux == NULL && 22399701SGeorge.Wilson@Sun.COM vdev_is_dead(tvd)) { 22401732Sbonwick vd->vdev_offline = B_FALSE; 22419701SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 22427754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, EBUSY)); 22431732Sbonwick } 224410685SGeorge.Wilson@Sun.COM 224510685SGeorge.Wilson@Sun.COM /* 224610685SGeorge.Wilson@Sun.COM * Add the device back into the metaslab rotor so that 224710685SGeorge.Wilson@Sun.COM * once we online the device it's open for business. 224810685SGeorge.Wilson@Sun.COM */ 224910685SGeorge.Wilson@Sun.COM if (tvd->vdev_islog && mg != NULL) 225010974SJeff.Bonwick@Sun.COM metaslab_group_activate(mg); 2251789Sahrens } 2252789Sahrens 22537754SJeff.Bonwick@Sun.COM vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 22541732Sbonwick 225510685SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 2256789Sahrens } 2257789Sahrens 225810974SJeff.Bonwick@Sun.COM int 225910974SJeff.Bonwick@Sun.COM vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 226010974SJeff.Bonwick@Sun.COM { 226110974SJeff.Bonwick@Sun.COM int error; 226210974SJeff.Bonwick@Sun.COM 226310974SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_vdev_top_lock); 226410974SJeff.Bonwick@Sun.COM error = vdev_offline_locked(spa, guid, flags); 226510974SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_vdev_top_lock); 226610974SJeff.Bonwick@Sun.COM 226710974SJeff.Bonwick@Sun.COM return (error); 226810974SJeff.Bonwick@Sun.COM } 226910974SJeff.Bonwick@Sun.COM 22701544Seschrock /* 22711544Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 22721544Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 22731544Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 22741544Seschrock */ 22751544Seschrock void 22767754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd) 2277789Sahrens { 22787754SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 22797754SJeff.Bonwick@Sun.COM 22807754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2281789Sahrens 22821544Seschrock if (vd == NULL) 22837754SJeff.Bonwick@Sun.COM vd = rvd; 2284789Sahrens 22851544Seschrock vd->vdev_stat.vs_read_errors = 0; 22861544Seschrock vd->vdev_stat.vs_write_errors = 0; 22871544Seschrock vd->vdev_stat.vs_checksum_errors = 0; 2288789Sahrens 22897754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 22907754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd->vdev_child[c]); 22914451Seschrock 22924451Seschrock /* 22936959Sek110237 * If we're in the FAULTED state or have experienced failed I/O, then 22946959Sek110237 * clear the persistent state and attempt to reopen the device. We 22956959Sek110237 * also mark the vdev config dirty, so that the new faulted state is 22966959Sek110237 * written out to disk. 22974451Seschrock */ 22987754SJeff.Bonwick@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded || 22997754SJeff.Bonwick@Sun.COM !vdev_readable(vd) || !vdev_writeable(vd)) { 23006959Sek110237 230110830SEric.Schrock@Sun.COM /* 230210830SEric.Schrock@Sun.COM * When reopening in reponse to a clear event, it may be due to 230310830SEric.Schrock@Sun.COM * a fmadm repair request. In this case, if the device is 230410830SEric.Schrock@Sun.COM * still broken, we want to still post the ereport again. 230510830SEric.Schrock@Sun.COM */ 230610830SEric.Schrock@Sun.COM vd->vdev_forcefault = B_TRUE; 230710830SEric.Schrock@Sun.COM 23084451Seschrock vd->vdev_faulted = vd->vdev_degraded = 0; 23097754SJeff.Bonwick@Sun.COM vd->vdev_cant_read = B_FALSE; 23107754SJeff.Bonwick@Sun.COM vd->vdev_cant_write = B_FALSE; 23117754SJeff.Bonwick@Sun.COM 23124451Seschrock vdev_reopen(vd); 23134451Seschrock 231410830SEric.Schrock@Sun.COM vd->vdev_forcefault = B_FALSE; 231510830SEric.Schrock@Sun.COM 23167754SJeff.Bonwick@Sun.COM if (vd != rvd) 23177754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 23187754SJeff.Bonwick@Sun.COM 23197754SJeff.Bonwick@Sun.COM if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 23204808Sek110237 spa_async_request(spa, SPA_ASYNC_RESILVER); 23214451Seschrock 23224451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 23234451Seschrock } 232410830SEric.Schrock@Sun.COM 232510830SEric.Schrock@Sun.COM /* 232610830SEric.Schrock@Sun.COM * When clearing a FMA-diagnosed fault, we always want to 232710830SEric.Schrock@Sun.COM * unspare the device, as we assume that the original spare was 232810830SEric.Schrock@Sun.COM * done in response to the FMA fault. 232910830SEric.Schrock@Sun.COM */ 233010830SEric.Schrock@Sun.COM if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 233110830SEric.Schrock@Sun.COM vd->vdev_parent->vdev_ops == &vdev_spare_ops && 233210830SEric.Schrock@Sun.COM vd->vdev_parent->vdev_child[0] == vd) 233310830SEric.Schrock@Sun.COM vd->vdev_unspare = B_TRUE; 2334789Sahrens } 2335789Sahrens 23367754SJeff.Bonwick@Sun.COM boolean_t 23377754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd) 23385329Sgw25295 { 233910594SGeorge.Wilson@Sun.COM /* 234010594SGeorge.Wilson@Sun.COM * Holes and missing devices are always considered "dead". 234110594SGeorge.Wilson@Sun.COM * This simplifies the code since we don't have to check for 234210594SGeorge.Wilson@Sun.COM * these types of devices in the various code paths. 234310594SGeorge.Wilson@Sun.COM * Instead we rely on the fact that we skip over dead devices 234410594SGeorge.Wilson@Sun.COM * before issuing I/O to them. 234510594SGeorge.Wilson@Sun.COM */ 234610594SGeorge.Wilson@Sun.COM return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole || 234710594SGeorge.Wilson@Sun.COM vd->vdev_ops == &vdev_missing_ops); 23485329Sgw25295 } 23495329Sgw25295 23507754SJeff.Bonwick@Sun.COM boolean_t 23517754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd) 2352789Sahrens { 23537754SJeff.Bonwick@Sun.COM return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2354789Sahrens } 2355789Sahrens 23567754SJeff.Bonwick@Sun.COM boolean_t 23577754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd) 2358789Sahrens { 23597754SJeff.Bonwick@Sun.COM return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 23607754SJeff.Bonwick@Sun.COM } 2361789Sahrens 23627754SJeff.Bonwick@Sun.COM boolean_t 23637980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd) 23647980SGeorge.Wilson@Sun.COM { 23658241SJeff.Bonwick@Sun.COM uint64_t state = vd->vdev_state; 23668241SJeff.Bonwick@Sun.COM 23677980SGeorge.Wilson@Sun.COM /* 23688241SJeff.Bonwick@Sun.COM * We currently allow allocations from vdevs which may be in the 23697980SGeorge.Wilson@Sun.COM * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 23707980SGeorge.Wilson@Sun.COM * fails to reopen then we'll catch it later when we're holding 23718241SJeff.Bonwick@Sun.COM * the proper locks. Note that we have to get the vdev state 23728241SJeff.Bonwick@Sun.COM * in a local variable because although it changes atomically, 23738241SJeff.Bonwick@Sun.COM * we're asking two separate questions about it. 23747980SGeorge.Wilson@Sun.COM */ 23758241SJeff.Bonwick@Sun.COM return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 237610594SGeorge.Wilson@Sun.COM !vd->vdev_cant_write && !vd->vdev_ishole && !vd->vdev_removing); 23777980SGeorge.Wilson@Sun.COM } 23787980SGeorge.Wilson@Sun.COM 23797980SGeorge.Wilson@Sun.COM boolean_t 23807754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio) 23817754SJeff.Bonwick@Sun.COM { 23827754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd == vd); 2383789Sahrens 23847754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 23857754SJeff.Bonwick@Sun.COM return (B_FALSE); 2386789Sahrens 23877754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_READ) 23887754SJeff.Bonwick@Sun.COM return (!vd->vdev_cant_read); 2389789Sahrens 23907754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_WRITE) 23917754SJeff.Bonwick@Sun.COM return (!vd->vdev_cant_write); 23927754SJeff.Bonwick@Sun.COM 23937754SJeff.Bonwick@Sun.COM return (B_TRUE); 2394789Sahrens } 2395789Sahrens 2396789Sahrens /* 2397789Sahrens * Get statistics for the given vdev. 2398789Sahrens */ 2399789Sahrens void 2400789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2401789Sahrens { 2402789Sahrens vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 2403789Sahrens 2404789Sahrens mutex_enter(&vd->vdev_stat_lock); 2405789Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 24067046Sahrens vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors; 2407789Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2408789Sahrens vs->vs_state = vd->vdev_state; 24099816SGeorge.Wilson@Sun.COM vs->vs_rsize = vdev_get_min_asize(vd); 24109816SGeorge.Wilson@Sun.COM if (vd->vdev_ops->vdev_op_leaf) 24119816SGeorge.Wilson@Sun.COM vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 2412789Sahrens mutex_exit(&vd->vdev_stat_lock); 2413789Sahrens 2414789Sahrens /* 2415789Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 2416789Sahrens * over all top-level vdevs (i.e. the direct children of the root). 2417789Sahrens */ 2418789Sahrens if (vd == rvd) { 24197754SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 2420789Sahrens vdev_t *cvd = rvd->vdev_child[c]; 2421789Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 2422789Sahrens 2423789Sahrens mutex_enter(&vd->vdev_stat_lock); 24247754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 2425789Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 2426789Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 2427789Sahrens } 2428789Sahrens vs->vs_scrub_examined += cvs->vs_scrub_examined; 2429789Sahrens mutex_exit(&vd->vdev_stat_lock); 2430789Sahrens } 2431789Sahrens } 2432789Sahrens } 2433789Sahrens 2434789Sahrens void 24355450Sbrendan vdev_clear_stats(vdev_t *vd) 24365450Sbrendan { 24375450Sbrendan mutex_enter(&vd->vdev_stat_lock); 24385450Sbrendan vd->vdev_stat.vs_space = 0; 24395450Sbrendan vd->vdev_stat.vs_dspace = 0; 24405450Sbrendan vd->vdev_stat.vs_alloc = 0; 24415450Sbrendan mutex_exit(&vd->vdev_stat_lock); 24425450Sbrendan } 24435450Sbrendan 24445450Sbrendan void 24457754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize) 2446789Sahrens { 24478241SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 24488241SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 24497754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2450789Sahrens vdev_t *pvd; 2451789Sahrens uint64_t txg = zio->io_txg; 2452789Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2453789Sahrens zio_type_t type = zio->io_type; 2454789Sahrens int flags = zio->io_flags; 2455789Sahrens 24567754SJeff.Bonwick@Sun.COM /* 24577754SJeff.Bonwick@Sun.COM * If this i/o is a gang leader, it didn't do any actual work. 24587754SJeff.Bonwick@Sun.COM */ 24597754SJeff.Bonwick@Sun.COM if (zio->io_gang_tree) 24607754SJeff.Bonwick@Sun.COM return; 24617754SJeff.Bonwick@Sun.COM 2462789Sahrens if (zio->io_error == 0) { 24637754SJeff.Bonwick@Sun.COM /* 24647754SJeff.Bonwick@Sun.COM * If this is a root i/o, don't count it -- we've already 24657754SJeff.Bonwick@Sun.COM * counted the top-level vdevs, and vdev_get_stats() will 24667754SJeff.Bonwick@Sun.COM * aggregate them when asked. This reduces contention on 24677754SJeff.Bonwick@Sun.COM * the root vdev_stat_lock and implicitly handles blocks 24687754SJeff.Bonwick@Sun.COM * that compress away to holes, for which there is no i/o. 24697754SJeff.Bonwick@Sun.COM * (Holes never create vdev children, so all the counters 24707754SJeff.Bonwick@Sun.COM * remain zero, which is what we want.) 24717754SJeff.Bonwick@Sun.COM * 24727754SJeff.Bonwick@Sun.COM * Note: this only applies to successful i/o (io_error == 0) 24737754SJeff.Bonwick@Sun.COM * because unlike i/o counts, errors are not additive. 24747754SJeff.Bonwick@Sun.COM * When reading a ditto block, for example, failure of 24757754SJeff.Bonwick@Sun.COM * one top-level vdev does not imply a root-level error. 24767754SJeff.Bonwick@Sun.COM */ 24777754SJeff.Bonwick@Sun.COM if (vd == rvd) 24787754SJeff.Bonwick@Sun.COM return; 24797754SJeff.Bonwick@Sun.COM 24807754SJeff.Bonwick@Sun.COM ASSERT(vd == zio->io_vd); 24818241SJeff.Bonwick@Sun.COM 24828241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_IO_BYPASS) 24838241SJeff.Bonwick@Sun.COM return; 24848241SJeff.Bonwick@Sun.COM 24858241SJeff.Bonwick@Sun.COM mutex_enter(&vd->vdev_stat_lock); 24868241SJeff.Bonwick@Sun.COM 24877754SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_IO_REPAIR) { 24881807Sbonwick if (flags & ZIO_FLAG_SCRUB_THREAD) 24897754SJeff.Bonwick@Sun.COM vs->vs_scrub_repaired += psize; 24908241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_SELF_HEAL) 24917754SJeff.Bonwick@Sun.COM vs->vs_self_healed += psize; 2492789Sahrens } 24938241SJeff.Bonwick@Sun.COM 24948241SJeff.Bonwick@Sun.COM vs->vs_ops[type]++; 24958241SJeff.Bonwick@Sun.COM vs->vs_bytes[type] += psize; 24968241SJeff.Bonwick@Sun.COM 24978241SJeff.Bonwick@Sun.COM mutex_exit(&vd->vdev_stat_lock); 2498789Sahrens return; 2499789Sahrens } 2500789Sahrens 2501789Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 2502789Sahrens return; 2503789Sahrens 25049725SEric.Schrock@Sun.COM /* 25059725SEric.Schrock@Sun.COM * If this is an I/O error that is going to be retried, then ignore the 25069725SEric.Schrock@Sun.COM * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 25079725SEric.Schrock@Sun.COM * hard errors, when in reality they can happen for any number of 25089725SEric.Schrock@Sun.COM * innocuous reasons (bus resets, MPxIO link failure, etc). 25099725SEric.Schrock@Sun.COM */ 25109725SEric.Schrock@Sun.COM if (zio->io_error == EIO && 25119725SEric.Schrock@Sun.COM !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 25129725SEric.Schrock@Sun.COM return; 25139725SEric.Schrock@Sun.COM 251410685SGeorge.Wilson@Sun.COM /* 251510685SGeorge.Wilson@Sun.COM * Intent logs writes won't propagate their error to the root 251610685SGeorge.Wilson@Sun.COM * I/O so don't mark these types of failures as pool-level 251710685SGeorge.Wilson@Sun.COM * errors. 251810685SGeorge.Wilson@Sun.COM */ 251910685SGeorge.Wilson@Sun.COM if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 252010685SGeorge.Wilson@Sun.COM return; 252110685SGeorge.Wilson@Sun.COM 25227754SJeff.Bonwick@Sun.COM mutex_enter(&vd->vdev_stat_lock); 25239230SGeorge.Wilson@Sun.COM if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 25247754SJeff.Bonwick@Sun.COM if (zio->io_error == ECKSUM) 25257754SJeff.Bonwick@Sun.COM vs->vs_checksum_errors++; 25267754SJeff.Bonwick@Sun.COM else 25277754SJeff.Bonwick@Sun.COM vs->vs_read_errors++; 2528789Sahrens } 25299230SGeorge.Wilson@Sun.COM if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 25307754SJeff.Bonwick@Sun.COM vs->vs_write_errors++; 25317754SJeff.Bonwick@Sun.COM mutex_exit(&vd->vdev_stat_lock); 2532789Sahrens 25338241SJeff.Bonwick@Sun.COM if (type == ZIO_TYPE_WRITE && txg != 0 && 25348241SJeff.Bonwick@Sun.COM (!(flags & ZIO_FLAG_IO_REPAIR) || 253510922SJeff.Bonwick@Sun.COM (flags & ZIO_FLAG_SCRUB_THREAD) || 253610922SJeff.Bonwick@Sun.COM spa->spa_claiming)) { 25378241SJeff.Bonwick@Sun.COM /* 253810922SJeff.Bonwick@Sun.COM * This is either a normal write (not a repair), or it's 253910922SJeff.Bonwick@Sun.COM * a repair induced by the scrub thread, or it's a repair 254010922SJeff.Bonwick@Sun.COM * made by zil_claim() during spa_load() in the first txg. 254110922SJeff.Bonwick@Sun.COM * In the normal case, we commit the DTL change in the same 254210922SJeff.Bonwick@Sun.COM * txg as the block was born. In the scrub-induced repair 254310922SJeff.Bonwick@Sun.COM * case, we know that scrubs run in first-pass syncing context, 254410922SJeff.Bonwick@Sun.COM * so we commit the DTL change in spa_syncing_txg(spa). 254510922SJeff.Bonwick@Sun.COM * In the zil_claim() case, we commit in spa_first_txg(spa). 25468241SJeff.Bonwick@Sun.COM * 25478241SJeff.Bonwick@Sun.COM * We currently do not make DTL entries for failed spontaneous 25488241SJeff.Bonwick@Sun.COM * self-healing writes triggered by normal (non-scrubbing) 25498241SJeff.Bonwick@Sun.COM * reads, because we have no transactional context in which to 25508241SJeff.Bonwick@Sun.COM * do so -- and it's not clear that it'd be desirable anyway. 25518241SJeff.Bonwick@Sun.COM */ 25528241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 25538241SJeff.Bonwick@Sun.COM uint64_t commit_txg = txg; 25548241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_SCRUB_THREAD) { 25558241SJeff.Bonwick@Sun.COM ASSERT(flags & ZIO_FLAG_IO_REPAIR); 25568241SJeff.Bonwick@Sun.COM ASSERT(spa_sync_pass(spa) == 1); 25578241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 255810922SJeff.Bonwick@Sun.COM commit_txg = spa_syncing_txg(spa); 255910922SJeff.Bonwick@Sun.COM } else if (spa->spa_claiming) { 256010922SJeff.Bonwick@Sun.COM ASSERT(flags & ZIO_FLAG_IO_REPAIR); 256110922SJeff.Bonwick@Sun.COM commit_txg = spa_first_txg(spa); 25628241SJeff.Bonwick@Sun.COM } 256310922SJeff.Bonwick@Sun.COM ASSERT(commit_txg >= spa_syncing_txg(spa)); 25648241SJeff.Bonwick@Sun.COM if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 25658241SJeff.Bonwick@Sun.COM return; 25668241SJeff.Bonwick@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 25678241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 25688241SJeff.Bonwick@Sun.COM vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2569789Sahrens } 25708241SJeff.Bonwick@Sun.COM if (vd != rvd) 25718241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2572789Sahrens } 2573789Sahrens } 2574789Sahrens 2575789Sahrens void 2576789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 2577789Sahrens { 2578789Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2579789Sahrens 25809816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 2581789Sahrens vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 2582789Sahrens 2583789Sahrens mutex_enter(&vd->vdev_stat_lock); 2584789Sahrens 2585789Sahrens if (type == POOL_SCRUB_NONE) { 2586789Sahrens /* 2587789Sahrens * Update completion and end time. Leave everything else alone 2588789Sahrens * so we can report what happened during the previous scrub. 2589789Sahrens */ 2590789Sahrens vs->vs_scrub_complete = complete; 2591789Sahrens vs->vs_scrub_end = gethrestime_sec(); 2592789Sahrens } else { 2593789Sahrens vs->vs_scrub_type = type; 2594789Sahrens vs->vs_scrub_complete = 0; 2595789Sahrens vs->vs_scrub_examined = 0; 2596789Sahrens vs->vs_scrub_repaired = 0; 2597789Sahrens vs->vs_scrub_start = gethrestime_sec(); 2598789Sahrens vs->vs_scrub_end = 0; 2599789Sahrens } 2600789Sahrens 2601789Sahrens mutex_exit(&vd->vdev_stat_lock); 2602789Sahrens } 2603789Sahrens 2604789Sahrens /* 260510922SJeff.Bonwick@Sun.COM * Update the in-core space usage stats for this vdev, its metaslab class, 260610922SJeff.Bonwick@Sun.COM * and the root vdev. 2607789Sahrens */ 2608789Sahrens void 260910922SJeff.Bonwick@Sun.COM vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 261010922SJeff.Bonwick@Sun.COM int64_t space_delta) 2611789Sahrens { 26124527Sperrin int64_t dspace_delta = space_delta; 26134527Sperrin spa_t *spa = vd->vdev_spa; 26144527Sperrin vdev_t *rvd = spa->spa_root_vdev; 261510922SJeff.Bonwick@Sun.COM metaslab_group_t *mg = vd->vdev_mg; 261610922SJeff.Bonwick@Sun.COM metaslab_class_t *mc = mg ? mg->mg_class : NULL; 26174527Sperrin 2618789Sahrens ASSERT(vd == vd->vdev_top); 26194527Sperrin 26204527Sperrin /* 26214527Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 26224527Sperrin * factor. We must calculate this here and not at the root vdev 26234527Sperrin * because the root vdev's psize-to-asize is simply the max of its 26244527Sperrin * childrens', thus not accurate enough for us. 26254527Sperrin */ 26264527Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 26279701SGeorge.Wilson@Sun.COM ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 26284527Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 26294527Sperrin vd->vdev_deflate_ratio; 2630789Sahrens 26314527Sperrin mutex_enter(&vd->vdev_stat_lock); 263210922SJeff.Bonwick@Sun.COM vd->vdev_stat.vs_alloc += alloc_delta; 26334527Sperrin vd->vdev_stat.vs_space += space_delta; 26344527Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 26354527Sperrin mutex_exit(&vd->vdev_stat_lock); 26362082Seschrock 263710922SJeff.Bonwick@Sun.COM if (mc == spa_normal_class(spa)) { 263810922SJeff.Bonwick@Sun.COM mutex_enter(&rvd->vdev_stat_lock); 263910922SJeff.Bonwick@Sun.COM rvd->vdev_stat.vs_alloc += alloc_delta; 264010922SJeff.Bonwick@Sun.COM rvd->vdev_stat.vs_space += space_delta; 264110922SJeff.Bonwick@Sun.COM rvd->vdev_stat.vs_dspace += dspace_delta; 264210922SJeff.Bonwick@Sun.COM mutex_exit(&rvd->vdev_stat_lock); 264310922SJeff.Bonwick@Sun.COM } 264410922SJeff.Bonwick@Sun.COM 264510922SJeff.Bonwick@Sun.COM if (mc != NULL) { 26465450Sbrendan ASSERT(rvd == vd->vdev_parent); 26475450Sbrendan ASSERT(vd->vdev_ms_count != 0); 26484527Sperrin 264910922SJeff.Bonwick@Sun.COM metaslab_class_space_update(mc, 265010922SJeff.Bonwick@Sun.COM alloc_delta, defer_delta, space_delta, dspace_delta); 26515450Sbrendan } 2652789Sahrens } 2653789Sahrens 2654789Sahrens /* 2655789Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 2656789Sahrens * so that it will be written out next time the vdev configuration is synced. 2657789Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 2658789Sahrens */ 2659789Sahrens void 2660789Sahrens vdev_config_dirty(vdev_t *vd) 2661789Sahrens { 2662789Sahrens spa_t *spa = vd->vdev_spa; 2663789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2664789Sahrens int c; 2665789Sahrens 26661601Sbonwick /* 26679425SEric.Schrock@Sun.COM * If this is an aux vdev (as with l2cache and spare devices), then we 26689425SEric.Schrock@Sun.COM * update the vdev config manually and set the sync flag. 26696643Seschrock */ 26706643Seschrock if (vd->vdev_aux != NULL) { 26716643Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 26726643Seschrock nvlist_t **aux; 26736643Seschrock uint_t naux; 26746643Seschrock 26756643Seschrock for (c = 0; c < sav->sav_count; c++) { 26766643Seschrock if (sav->sav_vdevs[c] == vd) 26776643Seschrock break; 26786643Seschrock } 26796643Seschrock 26807754SJeff.Bonwick@Sun.COM if (c == sav->sav_count) { 26817754SJeff.Bonwick@Sun.COM /* 26827754SJeff.Bonwick@Sun.COM * We're being removed. There's nothing more to do. 26837754SJeff.Bonwick@Sun.COM */ 26847754SJeff.Bonwick@Sun.COM ASSERT(sav->sav_sync == B_TRUE); 26857754SJeff.Bonwick@Sun.COM return; 26867754SJeff.Bonwick@Sun.COM } 26877754SJeff.Bonwick@Sun.COM 26886643Seschrock sav->sav_sync = B_TRUE; 26896643Seschrock 26909425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(sav->sav_config, 26919425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 26929425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 26939425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 26949425SEric.Schrock@Sun.COM } 26956643Seschrock 26966643Seschrock ASSERT(c < naux); 26976643Seschrock 26986643Seschrock /* 26996643Seschrock * Setting the nvlist in the middle if the array is a little 27006643Seschrock * sketchy, but it will work. 27016643Seschrock */ 27026643Seschrock nvlist_free(aux[c]); 27036643Seschrock aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE); 27046643Seschrock 27056643Seschrock return; 27066643Seschrock } 27076643Seschrock 27086643Seschrock /* 27097754SJeff.Bonwick@Sun.COM * The dirty list is protected by the SCL_CONFIG lock. The caller 27107754SJeff.Bonwick@Sun.COM * must either hold SCL_CONFIG as writer, or must be the sync thread 27117754SJeff.Bonwick@Sun.COM * (which holds SCL_CONFIG as reader). There's only one sync thread, 27121601Sbonwick * so this is sufficient to ensure mutual exclusion. 27131601Sbonwick */ 27147754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 27157754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 27167754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_CONFIG, RW_READER))); 27171601Sbonwick 2718789Sahrens if (vd == rvd) { 2719789Sahrens for (c = 0; c < rvd->vdev_children; c++) 2720789Sahrens vdev_config_dirty(rvd->vdev_child[c]); 2721789Sahrens } else { 2722789Sahrens ASSERT(vd == vd->vdev_top); 2723789Sahrens 272410594SGeorge.Wilson@Sun.COM if (!list_link_active(&vd->vdev_config_dirty_node) && 272510594SGeorge.Wilson@Sun.COM !vd->vdev_ishole) 27267754SJeff.Bonwick@Sun.COM list_insert_head(&spa->spa_config_dirty_list, vd); 2727789Sahrens } 2728789Sahrens } 2729789Sahrens 2730789Sahrens void 2731789Sahrens vdev_config_clean(vdev_t *vd) 2732789Sahrens { 27331601Sbonwick spa_t *spa = vd->vdev_spa; 27341601Sbonwick 27357754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 27367754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 27377754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_CONFIG, RW_READER))); 27387754SJeff.Bonwick@Sun.COM 27397754SJeff.Bonwick@Sun.COM ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 27407754SJeff.Bonwick@Sun.COM list_remove(&spa->spa_config_dirty_list, vd); 27417754SJeff.Bonwick@Sun.COM } 27427754SJeff.Bonwick@Sun.COM 27437754SJeff.Bonwick@Sun.COM /* 27447754SJeff.Bonwick@Sun.COM * Mark a top-level vdev's state as dirty, so that the next pass of 27457754SJeff.Bonwick@Sun.COM * spa_sync() can convert this into vdev_config_dirty(). We distinguish 27467754SJeff.Bonwick@Sun.COM * the state changes from larger config changes because they require 27477754SJeff.Bonwick@Sun.COM * much less locking, and are often needed for administrative actions. 27487754SJeff.Bonwick@Sun.COM */ 27497754SJeff.Bonwick@Sun.COM void 27507754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd) 27517754SJeff.Bonwick@Sun.COM { 27527754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 27537754SJeff.Bonwick@Sun.COM 27547754SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 27551601Sbonwick 27567754SJeff.Bonwick@Sun.COM /* 27577754SJeff.Bonwick@Sun.COM * The state list is protected by the SCL_STATE lock. The caller 27587754SJeff.Bonwick@Sun.COM * must either hold SCL_STATE as writer, or must be the sync thread 27597754SJeff.Bonwick@Sun.COM * (which holds SCL_STATE as reader). There's only one sync thread, 27607754SJeff.Bonwick@Sun.COM * so this is sufficient to ensure mutual exclusion. 27617754SJeff.Bonwick@Sun.COM */ 27627754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 27637754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 27647754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_STATE, RW_READER))); 27657754SJeff.Bonwick@Sun.COM 276610922SJeff.Bonwick@Sun.COM if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole) 27677754SJeff.Bonwick@Sun.COM list_insert_head(&spa->spa_state_dirty_list, vd); 27687754SJeff.Bonwick@Sun.COM } 27697754SJeff.Bonwick@Sun.COM 27707754SJeff.Bonwick@Sun.COM void 27717754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd) 27727754SJeff.Bonwick@Sun.COM { 27737754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 27747754SJeff.Bonwick@Sun.COM 27757754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 27767754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 27777754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_STATE, RW_READER))); 27787754SJeff.Bonwick@Sun.COM 27797754SJeff.Bonwick@Sun.COM ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 27807754SJeff.Bonwick@Sun.COM list_remove(&spa->spa_state_dirty_list, vd); 2781789Sahrens } 2782789Sahrens 27836523Sek110237 /* 27846523Sek110237 * Propagate vdev state up from children to parent. 27856523Sek110237 */ 27861775Sbillm void 27871775Sbillm vdev_propagate_state(vdev_t *vd) 27881775Sbillm { 27898241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 27908241SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 27911775Sbillm int degraded = 0, faulted = 0; 27921775Sbillm int corrupted = 0; 27931775Sbillm vdev_t *child; 27941775Sbillm 27954451Seschrock if (vd->vdev_children > 0) { 27969816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 27974451Seschrock child = vd->vdev_child[c]; 27986976Seschrock 279910594SGeorge.Wilson@Sun.COM /* 280010594SGeorge.Wilson@Sun.COM * Don't factor holes into the decision. 280110594SGeorge.Wilson@Sun.COM */ 280210594SGeorge.Wilson@Sun.COM if (child->vdev_ishole) 280310594SGeorge.Wilson@Sun.COM continue; 280410594SGeorge.Wilson@Sun.COM 28057754SJeff.Bonwick@Sun.COM if (!vdev_readable(child) || 28068241SJeff.Bonwick@Sun.COM (!vdev_writeable(child) && spa_writeable(spa))) { 28076976Seschrock /* 28086976Seschrock * Root special: if there is a top-level log 28096976Seschrock * device, treat the root vdev as if it were 28106976Seschrock * degraded. 28116976Seschrock */ 28126976Seschrock if (child->vdev_islog && vd == rvd) 28136976Seschrock degraded++; 28146976Seschrock else 28156976Seschrock faulted++; 28166976Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 28174451Seschrock degraded++; 28186976Seschrock } 28194451Seschrock 28204451Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 28214451Seschrock corrupted++; 28224451Seschrock } 28231775Sbillm 28244451Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 28254451Seschrock 28264451Seschrock /* 28277754SJeff.Bonwick@Sun.COM * Root special: if there is a top-level vdev that cannot be 28284451Seschrock * opened due to corrupted metadata, then propagate the root 28294451Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 28304451Seschrock * replicas'. 28314451Seschrock */ 28324451Seschrock if (corrupted && vd == rvd && 28334451Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 28344451Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 28354451Seschrock VDEV_AUX_CORRUPT_DATA); 28361775Sbillm } 28371775Sbillm 28386976Seschrock if (vd->vdev_parent) 28394451Seschrock vdev_propagate_state(vd->vdev_parent); 28401775Sbillm } 28411775Sbillm 2842789Sahrens /* 28431544Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 28441544Seschrock * state, because we're in the process of opening children depth-first. 28451544Seschrock * Otherwise, we propagate the change to the parent. 28461544Seschrock * 28471544Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 28481544Seschrock * generated. 2849789Sahrens */ 2850789Sahrens void 28511544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 2852789Sahrens { 28531986Seschrock uint64_t save_state; 28546643Seschrock spa_t *spa = vd->vdev_spa; 28551544Seschrock 28561544Seschrock if (state == vd->vdev_state) { 28571544Seschrock vd->vdev_stat.vs_aux = aux; 2858789Sahrens return; 28591544Seschrock } 28601544Seschrock 28611986Seschrock save_state = vd->vdev_state; 2862789Sahrens 2863789Sahrens vd->vdev_state = state; 2864789Sahrens vd->vdev_stat.vs_aux = aux; 2865789Sahrens 28664451Seschrock /* 28674451Seschrock * If we are setting the vdev state to anything but an open state, then 28684451Seschrock * always close the underlying device. Otherwise, we keep accessible 28694451Seschrock * but invalid devices open forever. We don't call vdev_close() itself, 28704451Seschrock * because that implies some extra checks (offline, etc) that we don't 28714451Seschrock * want here. This is limited to leaf devices, because otherwise 28724451Seschrock * closing the device will affect other children. 28734451Seschrock */ 28747780SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf) 28754451Seschrock vd->vdev_ops->vdev_op_close(vd); 28764451Seschrock 287710817SEric.Schrock@Sun.COM /* 287810817SEric.Schrock@Sun.COM * If we have brought this vdev back into service, we need 287910817SEric.Schrock@Sun.COM * to notify fmd so that it can gracefully repair any outstanding 288010817SEric.Schrock@Sun.COM * cases due to a missing device. We do this in all cases, even those 288110817SEric.Schrock@Sun.COM * that probably don't correlate to a repaired fault. This is sure to 288210817SEric.Schrock@Sun.COM * catch all cases, and we let the zfs-retire agent sort it out. If 288310817SEric.Schrock@Sun.COM * this is a transient state it's OK, as the retire agent will 288410817SEric.Schrock@Sun.COM * double-check the state of the vdev before repairing it. 288510817SEric.Schrock@Sun.COM */ 288610817SEric.Schrock@Sun.COM if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 288710817SEric.Schrock@Sun.COM vd->vdev_prevstate != state) 288810817SEric.Schrock@Sun.COM zfs_post_state_change(spa, vd); 288910817SEric.Schrock@Sun.COM 28904451Seschrock if (vd->vdev_removed && 28914451Seschrock state == VDEV_STATE_CANT_OPEN && 28924451Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 28934451Seschrock /* 28944451Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 28954451Seschrock * device was previously marked removed and someone attempted to 28964451Seschrock * reopen it. If this failed due to a nonexistent device, then 28974451Seschrock * keep the device in the REMOVED state. We also let this be if 28984451Seschrock * it is one of our special test online cases, which is only 28994451Seschrock * attempting to online the device and shouldn't generate an FMA 29004451Seschrock * fault. 29014451Seschrock */ 29024451Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 29034451Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 29044451Seschrock } else if (state == VDEV_STATE_REMOVED) { 29054451Seschrock vd->vdev_removed = B_TRUE; 29064451Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 29071544Seschrock /* 29081544Seschrock * If we fail to open a vdev during an import, we mark it as 29091544Seschrock * "not available", which signifies that it was never there to 29101544Seschrock * begin with. Failure to open such a device is not considered 29111544Seschrock * an error. 29121544Seschrock */ 291311147SGeorge.Wilson@Sun.COM if (spa_load_state(spa) == SPA_LOAD_IMPORT && 29141986Seschrock vd->vdev_ops->vdev_op_leaf) 29151986Seschrock vd->vdev_not_present = 1; 29161986Seschrock 29171986Seschrock /* 29181986Seschrock * Post the appropriate ereport. If the 'prevstate' field is 29191986Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 29201986Seschrock * that this is part of a vdev_reopen(). In this case, we don't 29211986Seschrock * want to post the ereport if the device was already in the 29221986Seschrock * CANT_OPEN state beforehand. 29234451Seschrock * 29244451Seschrock * If the 'checkremove' flag is set, then this is an attempt to 29254451Seschrock * online the device in response to an insertion event. If we 29264451Seschrock * hit this case, then we have detected an insertion event for a 29274451Seschrock * faulted or offline device that wasn't in the removed state. 29284451Seschrock * In this scenario, we don't post an ereport because we are 29294451Seschrock * about to replace the device, or attempt an online with 29304451Seschrock * vdev_forcefault, which will generate the fault for us. 29311986Seschrock */ 29324451Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 29334451Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 29346643Seschrock vd != spa->spa_root_vdev) { 29351544Seschrock const char *class; 29361544Seschrock 29371544Seschrock switch (aux) { 29381544Seschrock case VDEV_AUX_OPEN_FAILED: 29391544Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 29401544Seschrock break; 29411544Seschrock case VDEV_AUX_CORRUPT_DATA: 29421544Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 29431544Seschrock break; 29441544Seschrock case VDEV_AUX_NO_REPLICAS: 29451544Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 29461544Seschrock break; 29471544Seschrock case VDEV_AUX_BAD_GUID_SUM: 29481544Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 29491544Seschrock break; 29501544Seschrock case VDEV_AUX_TOO_SMALL: 29511544Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 29521544Seschrock break; 29531544Seschrock case VDEV_AUX_BAD_LABEL: 29541544Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 29551544Seschrock break; 29567754SJeff.Bonwick@Sun.COM case VDEV_AUX_IO_FAILURE: 29577754SJeff.Bonwick@Sun.COM class = FM_EREPORT_ZFS_IO_FAILURE; 29587754SJeff.Bonwick@Sun.COM break; 29591544Seschrock default: 29601544Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 29611544Seschrock } 29621544Seschrock 29636643Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 29641544Seschrock } 29654451Seschrock 29664451Seschrock /* Erase any notion of persistent removed state */ 29674451Seschrock vd->vdev_removed = B_FALSE; 29684451Seschrock } else { 29694451Seschrock vd->vdev_removed = B_FALSE; 29701544Seschrock } 29711544Seschrock 29729583STim.Haley@Sun.COM if (!isopen && vd->vdev_parent) 29739583STim.Haley@Sun.COM vdev_propagate_state(vd->vdev_parent); 2974789Sahrens } 29757042Sgw25295 29767042Sgw25295 /* 29777042Sgw25295 * Check the vdev configuration to ensure that it's capable of supporting 29787042Sgw25295 * a root pool. Currently, we do not support RAID-Z or partial configuration. 29797042Sgw25295 * In addition, only a single top-level vdev is allowed and none of the leaves 29807042Sgw25295 * can be wholedisks. 29817042Sgw25295 */ 29827042Sgw25295 boolean_t 29837042Sgw25295 vdev_is_bootable(vdev_t *vd) 29847042Sgw25295 { 29857042Sgw25295 if (!vd->vdev_ops->vdev_op_leaf) { 29867042Sgw25295 char *vdev_type = vd->vdev_ops->vdev_op_type; 29877042Sgw25295 29887042Sgw25295 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 29897042Sgw25295 vd->vdev_children > 1) { 29907042Sgw25295 return (B_FALSE); 29917042Sgw25295 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 29927042Sgw25295 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 29937042Sgw25295 return (B_FALSE); 29947042Sgw25295 } 29957042Sgw25295 } else if (vd->vdev_wholedisk == 1) { 29967042Sgw25295 return (B_FALSE); 29977042Sgw25295 } 29987042Sgw25295 29999816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 30007042Sgw25295 if (!vdev_is_bootable(vd->vdev_child[c])) 30017042Sgw25295 return (B_FALSE); 30027042Sgw25295 } 30037042Sgw25295 return (B_TRUE); 30047042Sgw25295 } 30059701SGeorge.Wilson@Sun.COM 300610594SGeorge.Wilson@Sun.COM /* 300710594SGeorge.Wilson@Sun.COM * Load the state from the original vdev tree (ovd) which 300810594SGeorge.Wilson@Sun.COM * we've retrieved from the MOS config object. If the original 300910594SGeorge.Wilson@Sun.COM * vdev was offline then we transfer that state to the device 301010594SGeorge.Wilson@Sun.COM * in the current vdev tree (nvd). 301110594SGeorge.Wilson@Sun.COM */ 30129701SGeorge.Wilson@Sun.COM void 301310594SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *nvd, vdev_t *ovd) 30149701SGeorge.Wilson@Sun.COM { 301510594SGeorge.Wilson@Sun.COM spa_t *spa = nvd->vdev_spa; 301610594SGeorge.Wilson@Sun.COM 301710594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 301810594SGeorge.Wilson@Sun.COM ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid); 301910594SGeorge.Wilson@Sun.COM 302010594SGeorge.Wilson@Sun.COM for (int c = 0; c < nvd->vdev_children; c++) 302110594SGeorge.Wilson@Sun.COM vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]); 302210594SGeorge.Wilson@Sun.COM 302310594SGeorge.Wilson@Sun.COM if (nvd->vdev_ops->vdev_op_leaf && ovd->vdev_offline) { 30249701SGeorge.Wilson@Sun.COM /* 30259701SGeorge.Wilson@Sun.COM * It would be nice to call vdev_offline() 30269701SGeorge.Wilson@Sun.COM * directly but the pool isn't fully loaded and 30279701SGeorge.Wilson@Sun.COM * the txg threads have not been started yet. 30289701SGeorge.Wilson@Sun.COM */ 302910594SGeorge.Wilson@Sun.COM nvd->vdev_offline = ovd->vdev_offline; 303010594SGeorge.Wilson@Sun.COM vdev_reopen(nvd->vdev_top); 30319701SGeorge.Wilson@Sun.COM } 30329701SGeorge.Wilson@Sun.COM } 30339816SGeorge.Wilson@Sun.COM 30349816SGeorge.Wilson@Sun.COM /* 30359816SGeorge.Wilson@Sun.COM * Expand a vdev if possible. 30369816SGeorge.Wilson@Sun.COM */ 30379816SGeorge.Wilson@Sun.COM void 30389816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg) 30399816SGeorge.Wilson@Sun.COM { 30409816SGeorge.Wilson@Sun.COM ASSERT(vd->vdev_top == vd); 30419816SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 30429816SGeorge.Wilson@Sun.COM 30439816SGeorge.Wilson@Sun.COM if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 30449816SGeorge.Wilson@Sun.COM VERIFY(vdev_metaslab_init(vd, txg) == 0); 30459816SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 30469816SGeorge.Wilson@Sun.COM } 30479816SGeorge.Wilson@Sun.COM } 3048*11422SMark.Musante@Sun.COM 3049*11422SMark.Musante@Sun.COM /* 3050*11422SMark.Musante@Sun.COM * Split a vdev. 3051*11422SMark.Musante@Sun.COM */ 3052*11422SMark.Musante@Sun.COM void 3053*11422SMark.Musante@Sun.COM vdev_split(vdev_t *vd) 3054*11422SMark.Musante@Sun.COM { 3055*11422SMark.Musante@Sun.COM vdev_t *cvd, *pvd = vd->vdev_parent; 3056*11422SMark.Musante@Sun.COM 3057*11422SMark.Musante@Sun.COM vdev_remove_child(pvd, vd); 3058*11422SMark.Musante@Sun.COM vdev_compact_children(pvd); 3059*11422SMark.Musante@Sun.COM 3060*11422SMark.Musante@Sun.COM cvd = pvd->vdev_child[0]; 3061*11422SMark.Musante@Sun.COM if (pvd->vdev_children == 1) { 3062*11422SMark.Musante@Sun.COM vdev_remove_parent(cvd); 3063*11422SMark.Musante@Sun.COM cvd->vdev_splitting = B_TRUE; 3064*11422SMark.Musante@Sun.COM } 3065*11422SMark.Musante@Sun.COM vdev_propagate_state(cvd); 3066*11422SMark.Musante@Sun.COM } 3067