1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 212082Seschrock 22789Sahrens /* 238632SBill.Moore@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #include <sys/zfs_context.h> 281544Seschrock #include <sys/fm/fs/zfs.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/spa_impl.h> 31789Sahrens #include <sys/dmu.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/vdev_impl.h> 34789Sahrens #include <sys/uberblock_impl.h> 35789Sahrens #include <sys/metaslab.h> 36789Sahrens #include <sys/metaslab_impl.h> 37789Sahrens #include <sys/space_map.h> 38789Sahrens #include <sys/zio.h> 39789Sahrens #include <sys/zap.h> 40789Sahrens #include <sys/fs/zfs.h> 416643Seschrock #include <sys/arc.h> 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 */ 3031585Sbonwick while (guid == 0 || spa_guid_exists(guid, 0)) 3041585Sbonwick guid = spa_get_random(-1ULL); 3051585Sbonwick } else { 3061585Sbonwick /* 3071585Sbonwick * Any other vdev's guid must be unique within the pool. 3081585Sbonwick */ 3091585Sbonwick while (guid == 0 || 3101585Sbonwick spa_guid_exists(spa_guid(spa), guid)) 3111585Sbonwick guid = spa_get_random(-1ULL); 3121585Sbonwick } 3131585Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 3141585Sbonwick } 315789Sahrens 316789Sahrens vd->vdev_spa = spa; 317789Sahrens vd->vdev_id = id; 318789Sahrens vd->vdev_guid = guid; 319789Sahrens vd->vdev_guid_sum = guid; 320789Sahrens vd->vdev_ops = ops; 321789Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 32210594SGeorge.Wilson@Sun.COM vd->vdev_ishole = (ops == &vdev_hole_ops); 323789Sahrens 324789Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 3252856Snd150628 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 3267754SJeff.Bonwick@Sun.COM mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 3278241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 3288241SJeff.Bonwick@Sun.COM space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0, 3298241SJeff.Bonwick@Sun.COM &vd->vdev_dtl_lock); 3308241SJeff.Bonwick@Sun.COM } 331789Sahrens txg_list_create(&vd->vdev_ms_list, 332789Sahrens offsetof(struct metaslab, ms_txg_node)); 333789Sahrens txg_list_create(&vd->vdev_dtl_list, 334789Sahrens offsetof(struct vdev, vdev_dtl_node)); 335789Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 3364451Seschrock vdev_queue_init(vd); 3374451Seschrock vdev_cache_init(vd); 338789Sahrens 339789Sahrens return (vd); 340789Sahrens } 341789Sahrens 342789Sahrens /* 343789Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 344789Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 345789Sahrens * different for each case. 346789Sahrens */ 3472082Seschrock int 3482082Seschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 3492082Seschrock int alloctype) 350789Sahrens { 351789Sahrens vdev_ops_t *ops; 352789Sahrens char *type; 3534527Sperrin uint64_t guid = 0, islog, nparity; 354789Sahrens vdev_t *vd; 355789Sahrens 3567754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 357789Sahrens 358789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 3592082Seschrock return (EINVAL); 360789Sahrens 361789Sahrens if ((ops = vdev_getops(type)) == NULL) 3622082Seschrock return (EINVAL); 363789Sahrens 364789Sahrens /* 365789Sahrens * If this is a load, get the vdev guid from the nvlist. 366789Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 367789Sahrens */ 368789Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 369789Sahrens uint64_t label_id; 370789Sahrens 371789Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 372789Sahrens label_id != id) 3732082Seschrock return (EINVAL); 374789Sahrens 375789Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3762082Seschrock return (EINVAL); 3772082Seschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 3782082Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3792082Seschrock return (EINVAL); 3805450Sbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 3815450Sbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3825450Sbrendan return (EINVAL); 3839790SLin.Ling@Sun.COM } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 3849790SLin.Ling@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 3859790SLin.Ling@Sun.COM return (EINVAL); 386789Sahrens } 387789Sahrens 3882082Seschrock /* 3892082Seschrock * The first allocated vdev must be of type 'root'. 3902082Seschrock */ 3912082Seschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 3922082Seschrock return (EINVAL); 3932082Seschrock 3944527Sperrin /* 3954527Sperrin * Determine whether we're a log vdev. 3964527Sperrin */ 3974527Sperrin islog = 0; 3984527Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 3995094Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 4004527Sperrin return (ENOTSUP); 4014527Sperrin 40210594SGeorge.Wilson@Sun.COM if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 40310594SGeorge.Wilson@Sun.COM return (ENOTSUP); 40410594SGeorge.Wilson@Sun.COM 4054527Sperrin /* 4064527Sperrin * Set the nparity property for RAID-Z vdevs. 4074527Sperrin */ 4084527Sperrin nparity = -1ULL; 4094527Sperrin if (ops == &vdev_raidz_ops) { 4104527Sperrin if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 4114527Sperrin &nparity) == 0) { 4124527Sperrin /* 41310105Sadam.leventhal@sun.com * Currently, we can only support 3 parity devices. 4144527Sperrin */ 41510105Sadam.leventhal@sun.com if (nparity == 0 || nparity > 3) 4164527Sperrin return (EINVAL); 4174527Sperrin /* 41810105Sadam.leventhal@sun.com * Previous versions could only support 1 or 2 parity 41910105Sadam.leventhal@sun.com * device. 4204527Sperrin */ 42110105Sadam.leventhal@sun.com if (nparity > 1 && 42210105Sadam.leventhal@sun.com spa_version(spa) < SPA_VERSION_RAIDZ2) 42310105Sadam.leventhal@sun.com return (ENOTSUP); 42410105Sadam.leventhal@sun.com if (nparity > 2 && 42510105Sadam.leventhal@sun.com spa_version(spa) < SPA_VERSION_RAIDZ3) 4264527Sperrin return (ENOTSUP); 4274527Sperrin } else { 4284527Sperrin /* 4294527Sperrin * We require the parity to be specified for SPAs that 4304527Sperrin * support multiple parity levels. 4314527Sperrin */ 43210105Sadam.leventhal@sun.com if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 4334527Sperrin return (EINVAL); 4344527Sperrin /* 4354527Sperrin * Otherwise, we default to 1 parity device for RAID-Z. 4364527Sperrin */ 4374527Sperrin nparity = 1; 4384527Sperrin } 4394527Sperrin } else { 4404527Sperrin nparity = 0; 4414527Sperrin } 4424527Sperrin ASSERT(nparity != -1ULL); 4434527Sperrin 444789Sahrens vd = vdev_alloc_common(spa, id, guid, ops); 445789Sahrens 4464527Sperrin vd->vdev_islog = islog; 4474527Sperrin vd->vdev_nparity = nparity; 4484527Sperrin 449789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 450789Sahrens vd->vdev_path = spa_strdup(vd->vdev_path); 451789Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 452789Sahrens vd->vdev_devid = spa_strdup(vd->vdev_devid); 4534451Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 4544451Seschrock &vd->vdev_physpath) == 0) 4554451Seschrock vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 4569425SEric.Schrock@Sun.COM if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 4579425SEric.Schrock@Sun.COM vd->vdev_fru = spa_strdup(vd->vdev_fru); 458789Sahrens 459789Sahrens /* 4601171Seschrock * Set the whole_disk property. If it's not specified, leave the value 4611171Seschrock * as -1. 4621171Seschrock */ 4631171Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 4641171Seschrock &vd->vdev_wholedisk) != 0) 4651171Seschrock vd->vdev_wholedisk = -1ULL; 4661171Seschrock 4671171Seschrock /* 4681544Seschrock * Look for the 'not present' flag. This will only be set if the device 4691544Seschrock * was not present at the time of import. 4701544Seschrock */ 4719425SEric.Schrock@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 4729425SEric.Schrock@Sun.COM &vd->vdev_not_present); 4731544Seschrock 4741544Seschrock /* 4751732Sbonwick * Get the alignment requirement. 4761732Sbonwick */ 4771732Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 4781732Sbonwick 4791732Sbonwick /* 48010594SGeorge.Wilson@Sun.COM * Retrieve the vdev creation time. 48110594SGeorge.Wilson@Sun.COM */ 48210594SGeorge.Wilson@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 48310594SGeorge.Wilson@Sun.COM &vd->vdev_crtxg); 48410594SGeorge.Wilson@Sun.COM 48510594SGeorge.Wilson@Sun.COM /* 486789Sahrens * If we're a top-level vdev, try to load the allocation parameters. 487789Sahrens */ 488789Sahrens if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) { 489789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 490789Sahrens &vd->vdev_ms_array); 491789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 492789Sahrens &vd->vdev_ms_shift); 493789Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 494789Sahrens &vd->vdev_asize); 495789Sahrens } 496789Sahrens 497789Sahrens /* 4984451Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 499789Sahrens */ 5006643Seschrock if (vd->vdev_ops->vdev_op_leaf && 5019790SLin.Ling@Sun.COM (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 5029790SLin.Ling@Sun.COM alloctype == VDEV_ALLOC_ROOTPOOL)) { 5036643Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 5046643Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 5058241SJeff.Bonwick@Sun.COM &vd->vdev_dtl_smo.smo_object); 5066643Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 5076643Seschrock &vd->vdev_unspare); 5086643Seschrock } 5099790SLin.Ling@Sun.COM 5109790SLin.Ling@Sun.COM if (alloctype == VDEV_ALLOC_ROOTPOOL) { 5119790SLin.Ling@Sun.COM uint64_t spare = 0; 5129790SLin.Ling@Sun.COM 5139790SLin.Ling@Sun.COM if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 5149790SLin.Ling@Sun.COM &spare) == 0 && spare) 5159790SLin.Ling@Sun.COM spa_spare_add(vd); 5169790SLin.Ling@Sun.COM } 5179790SLin.Ling@Sun.COM 5181732Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 5191732Sbonwick &vd->vdev_offline); 5206643Seschrock 5214451Seschrock /* 5224451Seschrock * When importing a pool, we want to ignore the persistent fault 5234451Seschrock * state, as the diagnosis made on another system may not be 52410817SEric.Schrock@Sun.COM * valid in the current context. Local vdevs will 52510817SEric.Schrock@Sun.COM * remain in the faulted state. 5264451Seschrock */ 5274451Seschrock if (spa->spa_load_state == SPA_LOAD_OPEN) { 5284451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 5294451Seschrock &vd->vdev_faulted); 5304451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 5314451Seschrock &vd->vdev_degraded); 5324451Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 5334451Seschrock &vd->vdev_removed); 53410817SEric.Schrock@Sun.COM 53510817SEric.Schrock@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded) { 53610817SEric.Schrock@Sun.COM char *aux; 53710817SEric.Schrock@Sun.COM 53810817SEric.Schrock@Sun.COM vd->vdev_label_aux = 53910817SEric.Schrock@Sun.COM VDEV_AUX_ERR_EXCEEDED; 54010817SEric.Schrock@Sun.COM if (nvlist_lookup_string(nv, 54110817SEric.Schrock@Sun.COM ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 54210817SEric.Schrock@Sun.COM strcmp(aux, "external") == 0) 54310817SEric.Schrock@Sun.COM vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 54410817SEric.Schrock@Sun.COM } 5454451Seschrock } 546789Sahrens } 547789Sahrens 548789Sahrens /* 549789Sahrens * Add ourselves to the parent's list of children. 550789Sahrens */ 551789Sahrens vdev_add_child(parent, vd); 552789Sahrens 5532082Seschrock *vdp = vd; 5542082Seschrock 5552082Seschrock return (0); 556789Sahrens } 557789Sahrens 558789Sahrens void 559789Sahrens vdev_free(vdev_t *vd) 560789Sahrens { 5614451Seschrock spa_t *spa = vd->vdev_spa; 562789Sahrens 563789Sahrens /* 564789Sahrens * vdev_free() implies closing the vdev first. This is simpler than 565789Sahrens * trying to ensure complicated semantics for all callers. 566789Sahrens */ 567789Sahrens vdev_close(vd); 568789Sahrens 5697754SJeff.Bonwick@Sun.COM ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 570789Sahrens 571789Sahrens /* 572789Sahrens * Free all children. 573789Sahrens */ 5749816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 575789Sahrens vdev_free(vd->vdev_child[c]); 576789Sahrens 577789Sahrens ASSERT(vd->vdev_child == NULL); 578789Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 579789Sahrens 580789Sahrens /* 581789Sahrens * Discard allocation state. 582789Sahrens */ 583789Sahrens if (vd == vd->vdev_top) 584789Sahrens vdev_metaslab_fini(vd); 585789Sahrens 586789Sahrens ASSERT3U(vd->vdev_stat.vs_space, ==, 0); 5872082Seschrock ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0); 588789Sahrens ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 589789Sahrens 590789Sahrens /* 591789Sahrens * Remove this vdev from its parent's child list. 592789Sahrens */ 593789Sahrens vdev_remove_child(vd->vdev_parent, vd); 594789Sahrens 595789Sahrens ASSERT(vd->vdev_parent == NULL); 596789Sahrens 5974451Seschrock /* 5984451Seschrock * Clean up vdev structure. 5994451Seschrock */ 6004451Seschrock vdev_queue_fini(vd); 6014451Seschrock vdev_cache_fini(vd); 6024451Seschrock 6034451Seschrock if (vd->vdev_path) 6044451Seschrock spa_strfree(vd->vdev_path); 6054451Seschrock if (vd->vdev_devid) 6064451Seschrock spa_strfree(vd->vdev_devid); 6074451Seschrock if (vd->vdev_physpath) 6084451Seschrock spa_strfree(vd->vdev_physpath); 6099425SEric.Schrock@Sun.COM if (vd->vdev_fru) 6109425SEric.Schrock@Sun.COM spa_strfree(vd->vdev_fru); 6114451Seschrock 6124451Seschrock if (vd->vdev_isspare) 6134451Seschrock spa_spare_remove(vd); 6145450Sbrendan if (vd->vdev_isl2cache) 6155450Sbrendan spa_l2cache_remove(vd); 6164451Seschrock 6174451Seschrock txg_list_destroy(&vd->vdev_ms_list); 6184451Seschrock txg_list_destroy(&vd->vdev_dtl_list); 6198241SJeff.Bonwick@Sun.COM 6204451Seschrock mutex_enter(&vd->vdev_dtl_lock); 6218241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 6228241SJeff.Bonwick@Sun.COM space_map_unload(&vd->vdev_dtl[t]); 6238241SJeff.Bonwick@Sun.COM space_map_destroy(&vd->vdev_dtl[t]); 6248241SJeff.Bonwick@Sun.COM } 6254451Seschrock mutex_exit(&vd->vdev_dtl_lock); 6268241SJeff.Bonwick@Sun.COM 6274451Seschrock mutex_destroy(&vd->vdev_dtl_lock); 6284451Seschrock mutex_destroy(&vd->vdev_stat_lock); 6297754SJeff.Bonwick@Sun.COM mutex_destroy(&vd->vdev_probe_lock); 6304451Seschrock 6314451Seschrock if (vd == spa->spa_root_vdev) 6324451Seschrock spa->spa_root_vdev = NULL; 6334451Seschrock 6344451Seschrock kmem_free(vd, sizeof (vdev_t)); 635789Sahrens } 636789Sahrens 637789Sahrens /* 638789Sahrens * Transfer top-level vdev state from svd to tvd. 639789Sahrens */ 640789Sahrens static void 641789Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 642789Sahrens { 643789Sahrens spa_t *spa = svd->vdev_spa; 644789Sahrens metaslab_t *msp; 645789Sahrens vdev_t *vd; 646789Sahrens int t; 647789Sahrens 648789Sahrens ASSERT(tvd == tvd->vdev_top); 649789Sahrens 650789Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 651789Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 652789Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 653789Sahrens 654789Sahrens svd->vdev_ms_array = 0; 655789Sahrens svd->vdev_ms_shift = 0; 656789Sahrens svd->vdev_ms_count = 0; 657789Sahrens 658789Sahrens tvd->vdev_mg = svd->vdev_mg; 659789Sahrens tvd->vdev_ms = svd->vdev_ms; 660789Sahrens 661789Sahrens svd->vdev_mg = NULL; 662789Sahrens svd->vdev_ms = NULL; 6631732Sbonwick 6641732Sbonwick if (tvd->vdev_mg != NULL) 6651732Sbonwick tvd->vdev_mg->mg_vd = tvd; 666789Sahrens 667789Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 668789Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 6692082Seschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 670789Sahrens 671789Sahrens svd->vdev_stat.vs_alloc = 0; 672789Sahrens svd->vdev_stat.vs_space = 0; 6732082Seschrock svd->vdev_stat.vs_dspace = 0; 674789Sahrens 675789Sahrens for (t = 0; t < TXG_SIZE; t++) { 676789Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 677789Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 678789Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 679789Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 680789Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 681789Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 682789Sahrens } 683789Sahrens 6847754SJeff.Bonwick@Sun.COM if (list_link_active(&svd->vdev_config_dirty_node)) { 685789Sahrens vdev_config_clean(svd); 686789Sahrens vdev_config_dirty(tvd); 687789Sahrens } 688789Sahrens 6897754SJeff.Bonwick@Sun.COM if (list_link_active(&svd->vdev_state_dirty_node)) { 6907754SJeff.Bonwick@Sun.COM vdev_state_clean(svd); 6917754SJeff.Bonwick@Sun.COM vdev_state_dirty(tvd); 6927754SJeff.Bonwick@Sun.COM } 6937754SJeff.Bonwick@Sun.COM 6942082Seschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 6952082Seschrock svd->vdev_deflate_ratio = 0; 6964527Sperrin 6974527Sperrin tvd->vdev_islog = svd->vdev_islog; 6984527Sperrin svd->vdev_islog = 0; 699789Sahrens } 700789Sahrens 701789Sahrens static void 702789Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 703789Sahrens { 704789Sahrens if (vd == NULL) 705789Sahrens return; 706789Sahrens 707789Sahrens vd->vdev_top = tvd; 708789Sahrens 7099816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 710789Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 711789Sahrens } 712789Sahrens 713789Sahrens /* 714789Sahrens * Add a mirror/replacing vdev above an existing vdev. 715789Sahrens */ 716789Sahrens vdev_t * 717789Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 718789Sahrens { 719789Sahrens spa_t *spa = cvd->vdev_spa; 720789Sahrens vdev_t *pvd = cvd->vdev_parent; 721789Sahrens vdev_t *mvd; 722789Sahrens 7237754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 724789Sahrens 725789Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 7261732Sbonwick 7271732Sbonwick mvd->vdev_asize = cvd->vdev_asize; 7289816SGeorge.Wilson@Sun.COM mvd->vdev_min_asize = cvd->vdev_min_asize; 7291732Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 7301732Sbonwick mvd->vdev_state = cvd->vdev_state; 73110594SGeorge.Wilson@Sun.COM mvd->vdev_crtxg = cvd->vdev_crtxg; 7321732Sbonwick 733789Sahrens vdev_remove_child(pvd, cvd); 734789Sahrens vdev_add_child(pvd, mvd); 735789Sahrens cvd->vdev_id = mvd->vdev_children; 736789Sahrens vdev_add_child(mvd, cvd); 737789Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 738789Sahrens 739789Sahrens if (mvd == mvd->vdev_top) 740789Sahrens vdev_top_transfer(cvd, mvd); 741789Sahrens 742789Sahrens return (mvd); 743789Sahrens } 744789Sahrens 745789Sahrens /* 746789Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 747789Sahrens */ 748789Sahrens void 749789Sahrens vdev_remove_parent(vdev_t *cvd) 750789Sahrens { 751789Sahrens vdev_t *mvd = cvd->vdev_parent; 752789Sahrens vdev_t *pvd = mvd->vdev_parent; 753789Sahrens 7547754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 755789Sahrens 756789Sahrens ASSERT(mvd->vdev_children == 1); 757789Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 7582082Seschrock mvd->vdev_ops == &vdev_replacing_ops || 7592082Seschrock mvd->vdev_ops == &vdev_spare_ops); 7601732Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 761789Sahrens 762789Sahrens vdev_remove_child(mvd, cvd); 763789Sahrens vdev_remove_child(pvd, mvd); 7648241SJeff.Bonwick@Sun.COM 7657754SJeff.Bonwick@Sun.COM /* 7667754SJeff.Bonwick@Sun.COM * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 7677754SJeff.Bonwick@Sun.COM * Otherwise, we could have detached an offline device, and when we 7687754SJeff.Bonwick@Sun.COM * go to import the pool we'll think we have two top-level vdevs, 7697754SJeff.Bonwick@Sun.COM * instead of a different version of the same top-level vdev. 7707754SJeff.Bonwick@Sun.COM */ 7718241SJeff.Bonwick@Sun.COM if (mvd->vdev_top == mvd) { 7728241SJeff.Bonwick@Sun.COM uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 7738241SJeff.Bonwick@Sun.COM cvd->vdev_guid += guid_delta; 7748241SJeff.Bonwick@Sun.COM cvd->vdev_guid_sum += guid_delta; 7758241SJeff.Bonwick@Sun.COM } 776789Sahrens cvd->vdev_id = mvd->vdev_id; 777789Sahrens vdev_add_child(pvd, cvd); 778789Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 779789Sahrens 780789Sahrens if (cvd == cvd->vdev_top) 781789Sahrens vdev_top_transfer(mvd, cvd); 782789Sahrens 783789Sahrens ASSERT(mvd->vdev_children == 0); 784789Sahrens vdev_free(mvd); 785789Sahrens } 786789Sahrens 7871544Seschrock int 788789Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 789789Sahrens { 790789Sahrens spa_t *spa = vd->vdev_spa; 7911732Sbonwick objset_t *mos = spa->spa_meta_objset; 7924527Sperrin metaslab_class_t *mc; 7931732Sbonwick uint64_t m; 794789Sahrens uint64_t oldc = vd->vdev_ms_count; 795789Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 7961732Sbonwick metaslab_t **mspp; 7971732Sbonwick int error; 798789Sahrens 79910594SGeorge.Wilson@Sun.COM /* 80010594SGeorge.Wilson@Sun.COM * This vdev is not being allocated from yet or is a hole. 80110594SGeorge.Wilson@Sun.COM */ 80210594SGeorge.Wilson@Sun.COM if (vd->vdev_ms_shift == 0) 8031585Sbonwick return (0); 8041585Sbonwick 80510594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 80610594SGeorge.Wilson@Sun.COM 8079701SGeorge.Wilson@Sun.COM /* 8089701SGeorge.Wilson@Sun.COM * Compute the raidz-deflation ratio. Note, we hard-code 8099701SGeorge.Wilson@Sun.COM * in 128k (1 << 17) because it is the current "typical" blocksize. 8109701SGeorge.Wilson@Sun.COM * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change, 8119701SGeorge.Wilson@Sun.COM * or we will inconsistently account for existing bp's. 8129701SGeorge.Wilson@Sun.COM */ 8139701SGeorge.Wilson@Sun.COM vd->vdev_deflate_ratio = (1 << 17) / 8149701SGeorge.Wilson@Sun.COM (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 8159701SGeorge.Wilson@Sun.COM 816789Sahrens ASSERT(oldc <= newc); 817789Sahrens 8184527Sperrin if (vd->vdev_islog) 8194527Sperrin mc = spa->spa_log_class; 8204527Sperrin else 8214527Sperrin mc = spa->spa_normal_class; 8224527Sperrin 8231732Sbonwick if (vd->vdev_mg == NULL) 8241732Sbonwick vd->vdev_mg = metaslab_group_create(mc, vd); 8251732Sbonwick 8261732Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 8271732Sbonwick 8281732Sbonwick if (oldc != 0) { 8291732Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 8301732Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 8311732Sbonwick } 8321732Sbonwick 8331732Sbonwick vd->vdev_ms = mspp; 834789Sahrens vd->vdev_ms_count = newc; 835789Sahrens 8361732Sbonwick for (m = oldc; m < newc; m++) { 8371732Sbonwick space_map_obj_t smo = { 0, 0, 0 }; 838789Sahrens if (txg == 0) { 8391732Sbonwick uint64_t object = 0; 8401732Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 8419512SNeil.Perrin@Sun.COM m * sizeof (uint64_t), sizeof (uint64_t), &object, 8429512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 8431732Sbonwick if (error) 8441732Sbonwick return (error); 8451732Sbonwick if (object != 0) { 8461732Sbonwick dmu_buf_t *db; 8471732Sbonwick error = dmu_bonus_hold(mos, object, FTAG, &db); 8481732Sbonwick if (error) 8491732Sbonwick return (error); 8504944Smaybee ASSERT3U(db->db_size, >=, sizeof (smo)); 8514944Smaybee bcopy(db->db_data, &smo, sizeof (smo)); 8521732Sbonwick ASSERT3U(smo.smo_object, ==, object); 8531544Seschrock dmu_buf_rele(db, FTAG); 854789Sahrens } 855789Sahrens } 8561732Sbonwick vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo, 8571732Sbonwick m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg); 858789Sahrens } 859789Sahrens 8601544Seschrock return (0); 861789Sahrens } 862789Sahrens 863789Sahrens void 864789Sahrens vdev_metaslab_fini(vdev_t *vd) 865789Sahrens { 866789Sahrens uint64_t m; 867789Sahrens uint64_t count = vd->vdev_ms_count; 868789Sahrens 869789Sahrens if (vd->vdev_ms != NULL) { 870789Sahrens for (m = 0; m < count; m++) 8711732Sbonwick if (vd->vdev_ms[m] != NULL) 8721732Sbonwick metaslab_fini(vd->vdev_ms[m]); 873789Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 874789Sahrens vd->vdev_ms = NULL; 875789Sahrens } 876789Sahrens } 877789Sahrens 8787754SJeff.Bonwick@Sun.COM typedef struct vdev_probe_stats { 8797754SJeff.Bonwick@Sun.COM boolean_t vps_readable; 8807754SJeff.Bonwick@Sun.COM boolean_t vps_writeable; 8817754SJeff.Bonwick@Sun.COM int vps_flags; 8827754SJeff.Bonwick@Sun.COM } vdev_probe_stats_t; 8837754SJeff.Bonwick@Sun.COM 8847754SJeff.Bonwick@Sun.COM static void 8857754SJeff.Bonwick@Sun.COM vdev_probe_done(zio_t *zio) 8865329Sgw25295 { 8878241SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 8888632SBill.Moore@Sun.COM vdev_t *vd = zio->io_vd; 8897754SJeff.Bonwick@Sun.COM vdev_probe_stats_t *vps = zio->io_private; 8908632SBill.Moore@Sun.COM 8918632SBill.Moore@Sun.COM ASSERT(vd->vdev_probe_zio != NULL); 8927754SJeff.Bonwick@Sun.COM 8937754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_READ) { 8947754SJeff.Bonwick@Sun.COM if (zio->io_error == 0) 8957754SJeff.Bonwick@Sun.COM vps->vps_readable = 1; 8968241SJeff.Bonwick@Sun.COM if (zio->io_error == 0 && spa_writeable(spa)) { 8978632SBill.Moore@Sun.COM zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 8987754SJeff.Bonwick@Sun.COM zio->io_offset, zio->io_size, zio->io_data, 8997754SJeff.Bonwick@Sun.COM ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 9007754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 9017754SJeff.Bonwick@Sun.COM } else { 9027754SJeff.Bonwick@Sun.COM zio_buf_free(zio->io_data, zio->io_size); 9037754SJeff.Bonwick@Sun.COM } 9047754SJeff.Bonwick@Sun.COM } else if (zio->io_type == ZIO_TYPE_WRITE) { 9057754SJeff.Bonwick@Sun.COM if (zio->io_error == 0) 9067754SJeff.Bonwick@Sun.COM vps->vps_writeable = 1; 9077754SJeff.Bonwick@Sun.COM zio_buf_free(zio->io_data, zio->io_size); 9087754SJeff.Bonwick@Sun.COM } else if (zio->io_type == ZIO_TYPE_NULL) { 9098632SBill.Moore@Sun.COM zio_t *pio; 9107754SJeff.Bonwick@Sun.COM 9117754SJeff.Bonwick@Sun.COM vd->vdev_cant_read |= !vps->vps_readable; 9127754SJeff.Bonwick@Sun.COM vd->vdev_cant_write |= !vps->vps_writeable; 9137754SJeff.Bonwick@Sun.COM 9147754SJeff.Bonwick@Sun.COM if (vdev_readable(vd) && 9158241SJeff.Bonwick@Sun.COM (vdev_writeable(vd) || !spa_writeable(spa))) { 9167754SJeff.Bonwick@Sun.COM zio->io_error = 0; 9177754SJeff.Bonwick@Sun.COM } else { 9187754SJeff.Bonwick@Sun.COM ASSERT(zio->io_error != 0); 9197754SJeff.Bonwick@Sun.COM zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 9208241SJeff.Bonwick@Sun.COM spa, vd, NULL, 0, 0); 9217754SJeff.Bonwick@Sun.COM zio->io_error = ENXIO; 9227754SJeff.Bonwick@Sun.COM } 9238632SBill.Moore@Sun.COM 9248632SBill.Moore@Sun.COM mutex_enter(&vd->vdev_probe_lock); 9258632SBill.Moore@Sun.COM ASSERT(vd->vdev_probe_zio == zio); 9268632SBill.Moore@Sun.COM vd->vdev_probe_zio = NULL; 9278632SBill.Moore@Sun.COM mutex_exit(&vd->vdev_probe_lock); 9288632SBill.Moore@Sun.COM 9298632SBill.Moore@Sun.COM while ((pio = zio_walk_parents(zio)) != NULL) 9308632SBill.Moore@Sun.COM if (!vdev_accessible(vd, pio)) 9318632SBill.Moore@Sun.COM pio->io_error = ENXIO; 9328632SBill.Moore@Sun.COM 9337754SJeff.Bonwick@Sun.COM kmem_free(vps, sizeof (*vps)); 9347754SJeff.Bonwick@Sun.COM } 9357754SJeff.Bonwick@Sun.COM } 9365329Sgw25295 9377754SJeff.Bonwick@Sun.COM /* 9387754SJeff.Bonwick@Sun.COM * Determine whether this device is accessible by reading and writing 9397754SJeff.Bonwick@Sun.COM * to several known locations: the pad regions of each vdev label 9407754SJeff.Bonwick@Sun.COM * but the first (which we leave alone in case it contains a VTOC). 9417754SJeff.Bonwick@Sun.COM */ 9427754SJeff.Bonwick@Sun.COM zio_t * 9438632SBill.Moore@Sun.COM vdev_probe(vdev_t *vd, zio_t *zio) 9447754SJeff.Bonwick@Sun.COM { 9457754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 9468632SBill.Moore@Sun.COM vdev_probe_stats_t *vps = NULL; 9478632SBill.Moore@Sun.COM zio_t *pio; 9487754SJeff.Bonwick@Sun.COM 9497754SJeff.Bonwick@Sun.COM ASSERT(vd->vdev_ops->vdev_op_leaf); 9507754SJeff.Bonwick@Sun.COM 9518632SBill.Moore@Sun.COM /* 9528632SBill.Moore@Sun.COM * Don't probe the probe. 9538632SBill.Moore@Sun.COM */ 9548632SBill.Moore@Sun.COM if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 9558632SBill.Moore@Sun.COM return (NULL); 9568632SBill.Moore@Sun.COM 9578632SBill.Moore@Sun.COM /* 9588632SBill.Moore@Sun.COM * To prevent 'probe storms' when a device fails, we create 9598632SBill.Moore@Sun.COM * just one probe i/o at a time. All zios that want to probe 9608632SBill.Moore@Sun.COM * this vdev will become parents of the probe io. 9618632SBill.Moore@Sun.COM */ 9628632SBill.Moore@Sun.COM mutex_enter(&vd->vdev_probe_lock); 9638632SBill.Moore@Sun.COM 9648632SBill.Moore@Sun.COM if ((pio = vd->vdev_probe_zio) == NULL) { 9658632SBill.Moore@Sun.COM vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 9668632SBill.Moore@Sun.COM 9678632SBill.Moore@Sun.COM vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 9688632SBill.Moore@Sun.COM ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 9699725SEric.Schrock@Sun.COM ZIO_FLAG_TRYHARD; 9708632SBill.Moore@Sun.COM 9718632SBill.Moore@Sun.COM if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 9728632SBill.Moore@Sun.COM /* 9738632SBill.Moore@Sun.COM * vdev_cant_read and vdev_cant_write can only 9748632SBill.Moore@Sun.COM * transition from TRUE to FALSE when we have the 9758632SBill.Moore@Sun.COM * SCL_ZIO lock as writer; otherwise they can only 9768632SBill.Moore@Sun.COM * transition from FALSE to TRUE. This ensures that 9778632SBill.Moore@Sun.COM * any zio looking at these values can assume that 9788632SBill.Moore@Sun.COM * failures persist for the life of the I/O. That's 9798632SBill.Moore@Sun.COM * important because when a device has intermittent 9808632SBill.Moore@Sun.COM * connectivity problems, we want to ensure that 9818632SBill.Moore@Sun.COM * they're ascribed to the device (ENXIO) and not 9828632SBill.Moore@Sun.COM * the zio (EIO). 9838632SBill.Moore@Sun.COM * 9848632SBill.Moore@Sun.COM * Since we hold SCL_ZIO as writer here, clear both 9858632SBill.Moore@Sun.COM * values so the probe can reevaluate from first 9868632SBill.Moore@Sun.COM * principles. 9878632SBill.Moore@Sun.COM */ 9888632SBill.Moore@Sun.COM vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 9898632SBill.Moore@Sun.COM vd->vdev_cant_read = B_FALSE; 9908632SBill.Moore@Sun.COM vd->vdev_cant_write = B_FALSE; 9918632SBill.Moore@Sun.COM } 9928632SBill.Moore@Sun.COM 9938632SBill.Moore@Sun.COM vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 9948632SBill.Moore@Sun.COM vdev_probe_done, vps, 9958632SBill.Moore@Sun.COM vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 9968632SBill.Moore@Sun.COM 9978632SBill.Moore@Sun.COM if (zio != NULL) { 9988632SBill.Moore@Sun.COM vd->vdev_probe_wanted = B_TRUE; 9998632SBill.Moore@Sun.COM spa_async_request(spa, SPA_ASYNC_PROBE); 10008632SBill.Moore@Sun.COM } 10018632SBill.Moore@Sun.COM } 10028632SBill.Moore@Sun.COM 10038632SBill.Moore@Sun.COM if (zio != NULL) 10048632SBill.Moore@Sun.COM zio_add_child(zio, pio); 10058632SBill.Moore@Sun.COM 10068632SBill.Moore@Sun.COM mutex_exit(&vd->vdev_probe_lock); 10078632SBill.Moore@Sun.COM 10088632SBill.Moore@Sun.COM if (vps == NULL) { 10098632SBill.Moore@Sun.COM ASSERT(zio != NULL); 10108632SBill.Moore@Sun.COM return (NULL); 10118632SBill.Moore@Sun.COM } 10127754SJeff.Bonwick@Sun.COM 10137754SJeff.Bonwick@Sun.COM for (int l = 1; l < VDEV_LABELS; l++) { 10148632SBill.Moore@Sun.COM zio_nowait(zio_read_phys(pio, vd, 10157754SJeff.Bonwick@Sun.COM vdev_label_offset(vd->vdev_psize, l, 10169056SLin.Ling@Sun.COM offsetof(vdev_label_t, vl_pad2)), 10179056SLin.Ling@Sun.COM VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 10187754SJeff.Bonwick@Sun.COM ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 10197754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 10207754SJeff.Bonwick@Sun.COM } 10217754SJeff.Bonwick@Sun.COM 10228632SBill.Moore@Sun.COM if (zio == NULL) 10238632SBill.Moore@Sun.COM return (pio); 10248632SBill.Moore@Sun.COM 10258632SBill.Moore@Sun.COM zio_nowait(pio); 10268632SBill.Moore@Sun.COM return (NULL); 10275329Sgw25295 } 10285329Sgw25295 10299846SEric.Taylor@Sun.COM static void 10309846SEric.Taylor@Sun.COM vdev_open_child(void *arg) 10319846SEric.Taylor@Sun.COM { 10329846SEric.Taylor@Sun.COM vdev_t *vd = arg; 10339846SEric.Taylor@Sun.COM 10349846SEric.Taylor@Sun.COM vd->vdev_open_thread = curthread; 10359846SEric.Taylor@Sun.COM vd->vdev_open_error = vdev_open(vd); 10369846SEric.Taylor@Sun.COM vd->vdev_open_thread = NULL; 10379846SEric.Taylor@Sun.COM } 10389846SEric.Taylor@Sun.COM 103910588SEric.Taylor@Sun.COM boolean_t 104010588SEric.Taylor@Sun.COM vdev_uses_zvols(vdev_t *vd) 104110588SEric.Taylor@Sun.COM { 104210588SEric.Taylor@Sun.COM if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 104310588SEric.Taylor@Sun.COM strlen(ZVOL_DIR)) == 0) 104410588SEric.Taylor@Sun.COM return (B_TRUE); 104510588SEric.Taylor@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 104610588SEric.Taylor@Sun.COM if (vdev_uses_zvols(vd->vdev_child[c])) 104710588SEric.Taylor@Sun.COM return (B_TRUE); 104810588SEric.Taylor@Sun.COM return (B_FALSE); 104910588SEric.Taylor@Sun.COM } 105010588SEric.Taylor@Sun.COM 10519846SEric.Taylor@Sun.COM void 10529846SEric.Taylor@Sun.COM vdev_open_children(vdev_t *vd) 10539846SEric.Taylor@Sun.COM { 10549846SEric.Taylor@Sun.COM taskq_t *tq; 10559846SEric.Taylor@Sun.COM int children = vd->vdev_children; 10569846SEric.Taylor@Sun.COM 105710588SEric.Taylor@Sun.COM /* 105810588SEric.Taylor@Sun.COM * in order to handle pools on top of zvols, do the opens 105910588SEric.Taylor@Sun.COM * in a single thread so that the same thread holds the 106010588SEric.Taylor@Sun.COM * spa_namespace_lock 106110588SEric.Taylor@Sun.COM */ 106210588SEric.Taylor@Sun.COM if (vdev_uses_zvols(vd)) { 106310588SEric.Taylor@Sun.COM for (int c = 0; c < children; c++) 106410588SEric.Taylor@Sun.COM vd->vdev_child[c]->vdev_open_error = 106510588SEric.Taylor@Sun.COM vdev_open(vd->vdev_child[c]); 106610588SEric.Taylor@Sun.COM return; 106710588SEric.Taylor@Sun.COM } 10689846SEric.Taylor@Sun.COM tq = taskq_create("vdev_open", children, minclsyspri, 10699846SEric.Taylor@Sun.COM children, children, TASKQ_PREPOPULATE); 10709846SEric.Taylor@Sun.COM 10719846SEric.Taylor@Sun.COM for (int c = 0; c < children; c++) 10729846SEric.Taylor@Sun.COM VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 10739846SEric.Taylor@Sun.COM TQ_SLEEP) != NULL); 10749846SEric.Taylor@Sun.COM 10759846SEric.Taylor@Sun.COM taskq_destroy(tq); 10769846SEric.Taylor@Sun.COM } 10779846SEric.Taylor@Sun.COM 1078789Sahrens /* 1079789Sahrens * Prepare a virtual device for access. 1080789Sahrens */ 1081789Sahrens int 1082789Sahrens vdev_open(vdev_t *vd) 1083789Sahrens { 10848241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 1085789Sahrens int error; 1086789Sahrens uint64_t osize = 0; 1087789Sahrens uint64_t asize, psize; 10881732Sbonwick uint64_t ashift = 0; 1089789Sahrens 10909846SEric.Taylor@Sun.COM ASSERT(vd->vdev_open_thread == curthread || 10919846SEric.Taylor@Sun.COM spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1092789Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1093789Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1094789Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1095789Sahrens 1096789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 10979701SGeorge.Wilson@Sun.COM vd->vdev_cant_read = B_FALSE; 10989701SGeorge.Wilson@Sun.COM vd->vdev_cant_write = B_FALSE; 10999816SGeorge.Wilson@Sun.COM vd->vdev_min_asize = vdev_get_min_asize(vd); 1100789Sahrens 110110817SEric.Schrock@Sun.COM /* 110210817SEric.Schrock@Sun.COM * If this vdev is not removed, check its fault status. If it's 110310817SEric.Schrock@Sun.COM * faulted, bail out of the open. 110410817SEric.Schrock@Sun.COM */ 11054451Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 11064451Seschrock ASSERT(vd->vdev_children == 0); 110710817SEric.Schrock@Sun.COM ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 110810817SEric.Schrock@Sun.COM vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 11094451Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 111010817SEric.Schrock@Sun.COM vd->vdev_label_aux); 11114451Seschrock return (ENXIO); 11124451Seschrock } else if (vd->vdev_offline) { 1113789Sahrens ASSERT(vd->vdev_children == 0); 11141544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1115789Sahrens return (ENXIO); 1116789Sahrens } 1117789Sahrens 1118789Sahrens error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 1119789Sahrens 11201544Seschrock if (zio_injection_enabled && error == 0) 11219725SEric.Schrock@Sun.COM error = zio_handle_device_injection(vd, NULL, ENXIO); 11221544Seschrock 11234451Seschrock if (error) { 11244451Seschrock if (vd->vdev_removed && 11254451Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 11264451Seschrock vd->vdev_removed = B_FALSE; 1127789Sahrens 11281544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1129789Sahrens vd->vdev_stat.vs_aux); 1130789Sahrens return (error); 1131789Sahrens } 1132789Sahrens 11334451Seschrock vd->vdev_removed = B_FALSE; 11344451Seschrock 1135*10830SEric.Schrock@Sun.COM /* 1136*10830SEric.Schrock@Sun.COM * Recheck the faulted flag now that we have confirmed that 1137*10830SEric.Schrock@Sun.COM * the vdev is accessible. If we're faulted, bail. 1138*10830SEric.Schrock@Sun.COM */ 1139*10830SEric.Schrock@Sun.COM if (vd->vdev_faulted) { 1140*10830SEric.Schrock@Sun.COM ASSERT(vd->vdev_children == 0); 1141*10830SEric.Schrock@Sun.COM ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1142*10830SEric.Schrock@Sun.COM vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1143*10830SEric.Schrock@Sun.COM vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1144*10830SEric.Schrock@Sun.COM vd->vdev_label_aux); 1145*10830SEric.Schrock@Sun.COM return (ENXIO); 1146*10830SEric.Schrock@Sun.COM } 1147*10830SEric.Schrock@Sun.COM 11484451Seschrock if (vd->vdev_degraded) { 11494451Seschrock ASSERT(vd->vdev_children == 0); 11504451Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 11514451Seschrock VDEV_AUX_ERR_EXCEEDED); 11524451Seschrock } else { 115310817SEric.Schrock@Sun.COM vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 11544451Seschrock } 1155789Sahrens 115610594SGeorge.Wilson@Sun.COM /* 115710594SGeorge.Wilson@Sun.COM * For hole or missing vdevs we just return success. 115810594SGeorge.Wilson@Sun.COM */ 115910594SGeorge.Wilson@Sun.COM if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 116010594SGeorge.Wilson@Sun.COM return (0); 116110594SGeorge.Wilson@Sun.COM 11629816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 11631544Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 11641544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 11651544Seschrock VDEV_AUX_NONE); 11661544Seschrock break; 11671544Seschrock } 11689816SGeorge.Wilson@Sun.COM } 1169789Sahrens 1170789Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1171789Sahrens 1172789Sahrens if (vd->vdev_children == 0) { 1173789Sahrens if (osize < SPA_MINDEVSIZE) { 11741544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 11751544Seschrock VDEV_AUX_TOO_SMALL); 1176789Sahrens return (EOVERFLOW); 1177789Sahrens } 1178789Sahrens psize = osize; 1179789Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1180789Sahrens } else { 11811732Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1182789Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 11831544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 11841544Seschrock VDEV_AUX_TOO_SMALL); 1185789Sahrens return (EOVERFLOW); 1186789Sahrens } 1187789Sahrens psize = 0; 1188789Sahrens asize = osize; 1189789Sahrens } 1190789Sahrens 1191789Sahrens vd->vdev_psize = psize; 1192789Sahrens 11939816SGeorge.Wilson@Sun.COM /* 11949816SGeorge.Wilson@Sun.COM * Make sure the allocatable size hasn't shrunk. 11959816SGeorge.Wilson@Sun.COM */ 11969816SGeorge.Wilson@Sun.COM if (asize < vd->vdev_min_asize) { 11979816SGeorge.Wilson@Sun.COM vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 11989816SGeorge.Wilson@Sun.COM VDEV_AUX_BAD_LABEL); 11999816SGeorge.Wilson@Sun.COM return (EINVAL); 12009816SGeorge.Wilson@Sun.COM } 12019816SGeorge.Wilson@Sun.COM 1202789Sahrens if (vd->vdev_asize == 0) { 1203789Sahrens /* 1204789Sahrens * This is the first-ever open, so use the computed values. 12051732Sbonwick * For testing purposes, a higher ashift can be requested. 1206789Sahrens */ 1207789Sahrens vd->vdev_asize = asize; 12081732Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1209789Sahrens } else { 1210789Sahrens /* 1211789Sahrens * Make sure the alignment requirement hasn't increased. 1212789Sahrens */ 12131732Sbonwick if (ashift > vd->vdev_top->vdev_ashift) { 12141544Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12151544Seschrock VDEV_AUX_BAD_LABEL); 1216789Sahrens return (EINVAL); 1217789Sahrens } 1218789Sahrens } 1219789Sahrens 12201544Seschrock /* 12219816SGeorge.Wilson@Sun.COM * If all children are healthy and the asize has increased, 12229816SGeorge.Wilson@Sun.COM * then we've experienced dynamic LUN growth. If automatic 12239816SGeorge.Wilson@Sun.COM * expansion is enabled then use the additional space. 12249816SGeorge.Wilson@Sun.COM */ 12259816SGeorge.Wilson@Sun.COM if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize && 12269816SGeorge.Wilson@Sun.COM (vd->vdev_expanding || spa->spa_autoexpand)) 12279816SGeorge.Wilson@Sun.COM vd->vdev_asize = asize; 12289816SGeorge.Wilson@Sun.COM 12299816SGeorge.Wilson@Sun.COM vdev_set_min_asize(vd); 12309816SGeorge.Wilson@Sun.COM 12319816SGeorge.Wilson@Sun.COM /* 12325329Sgw25295 * Ensure we can issue some IO before declaring the 12335329Sgw25295 * vdev open for business. 12345329Sgw25295 */ 12357754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && 12367754SJeff.Bonwick@Sun.COM (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 12375329Sgw25295 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12387754SJeff.Bonwick@Sun.COM VDEV_AUX_IO_FAILURE); 12395329Sgw25295 return (error); 12405329Sgw25295 } 12415329Sgw25295 12425329Sgw25295 /* 12437046Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 12448241SJeff.Bonwick@Sun.COM * resilver. But don't do this if we are doing a reopen for a scrub, 12458241SJeff.Bonwick@Sun.COM * since this would just restart the scrub we are already doing. 12467046Sahrens */ 12478241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 12488241SJeff.Bonwick@Sun.COM vdev_resilver_needed(vd, NULL, NULL)) 12498241SJeff.Bonwick@Sun.COM spa_async_request(spa, SPA_ASYNC_RESILVER); 12507046Sahrens 1251789Sahrens return (0); 1252789Sahrens } 1253789Sahrens 1254789Sahrens /* 12551986Seschrock * Called once the vdevs are all opened, this routine validates the label 12561986Seschrock * contents. This needs to be done before vdev_load() so that we don't 12574451Seschrock * inadvertently do repair I/Os to the wrong device. 12581986Seschrock * 12591986Seschrock * This function will only return failure if one of the vdevs indicates that it 12601986Seschrock * has since been destroyed or exported. This is only possible if 12611986Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 12621986Seschrock * will be updated but the function will return 0. 12631986Seschrock */ 12641986Seschrock int 12651986Seschrock vdev_validate(vdev_t *vd) 12661986Seschrock { 12671986Seschrock spa_t *spa = vd->vdev_spa; 12681986Seschrock nvlist_t *label; 12697754SJeff.Bonwick@Sun.COM uint64_t guid, top_guid; 12701986Seschrock uint64_t state; 12711986Seschrock 12729816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 12731986Seschrock if (vdev_validate(vd->vdev_child[c]) != 0) 12744070Smc142369 return (EBADF); 12751986Seschrock 12762174Seschrock /* 12772174Seschrock * If the device has already failed, or was marked offline, don't do 12782174Seschrock * any further validation. Otherwise, label I/O will fail and we will 12792174Seschrock * overwrite the previous state. 12802174Seschrock */ 12817754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 12821986Seschrock 12831986Seschrock if ((label = vdev_label_read_config(vd)) == NULL) { 12841986Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 12851986Seschrock VDEV_AUX_BAD_LABEL); 12861986Seschrock return (0); 12871986Seschrock } 12881986Seschrock 12891986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 12901986Seschrock &guid) != 0 || guid != spa_guid(spa)) { 12911986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 12921986Seschrock VDEV_AUX_CORRUPT_DATA); 12931986Seschrock nvlist_free(label); 12941986Seschrock return (0); 12951986Seschrock } 12961986Seschrock 12977754SJeff.Bonwick@Sun.COM /* 12987754SJeff.Bonwick@Sun.COM * If this vdev just became a top-level vdev because its 12997754SJeff.Bonwick@Sun.COM * sibling was detached, it will have adopted the parent's 13007754SJeff.Bonwick@Sun.COM * vdev guid -- but the label may or may not be on disk yet. 13017754SJeff.Bonwick@Sun.COM * Fortunately, either version of the label will have the 13027754SJeff.Bonwick@Sun.COM * same top guid, so if we're a top-level vdev, we can 13037754SJeff.Bonwick@Sun.COM * safely compare to that instead. 13047754SJeff.Bonwick@Sun.COM */ 13051986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 13067754SJeff.Bonwick@Sun.COM &guid) != 0 || 13077754SJeff.Bonwick@Sun.COM nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 13087754SJeff.Bonwick@Sun.COM &top_guid) != 0 || 13097754SJeff.Bonwick@Sun.COM (vd->vdev_guid != guid && 13107754SJeff.Bonwick@Sun.COM (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 13111986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 13121986Seschrock VDEV_AUX_CORRUPT_DATA); 13131986Seschrock nvlist_free(label); 13141986Seschrock return (0); 13151986Seschrock } 13161986Seschrock 13171986Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 13181986Seschrock &state) != 0) { 13191986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 13201986Seschrock VDEV_AUX_CORRUPT_DATA); 13211986Seschrock nvlist_free(label); 13221986Seschrock return (0); 13231986Seschrock } 13241986Seschrock 13251986Seschrock nvlist_free(label); 13261986Seschrock 132710100SLin.Ling@Sun.COM /* 132810100SLin.Ling@Sun.COM * If spa->spa_load_verbatim is true, no need to check the 132910100SLin.Ling@Sun.COM * state of the pool. 133010100SLin.Ling@Sun.COM */ 133110100SLin.Ling@Sun.COM if (!spa->spa_load_verbatim && 133210100SLin.Ling@Sun.COM spa->spa_load_state == SPA_LOAD_OPEN && 133310100SLin.Ling@Sun.COM state != POOL_STATE_ACTIVE) 13344070Smc142369 return (EBADF); 13356976Seschrock 13366976Seschrock /* 13376976Seschrock * If we were able to open and validate a vdev that was 13386976Seschrock * previously marked permanently unavailable, clear that state 13396976Seschrock * now. 13406976Seschrock */ 13416976Seschrock if (vd->vdev_not_present) 13426976Seschrock vd->vdev_not_present = 0; 13431986Seschrock } 13441986Seschrock 13451986Seschrock return (0); 13461986Seschrock } 13471986Seschrock 13481986Seschrock /* 1349789Sahrens * Close a virtual device. 1350789Sahrens */ 1351789Sahrens void 1352789Sahrens vdev_close(vdev_t *vd) 1353789Sahrens { 13548241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 13558241SJeff.Bonwick@Sun.COM 13568241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 13578241SJeff.Bonwick@Sun.COM 1358789Sahrens vd->vdev_ops->vdev_op_close(vd); 1359789Sahrens 13604451Seschrock vdev_cache_purge(vd); 1361789Sahrens 13621986Seschrock /* 13639816SGeorge.Wilson@Sun.COM * We record the previous state before we close it, so that if we are 13641986Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 13651986Seschrock * it's still faulted. 13661986Seschrock */ 13671986Seschrock vd->vdev_prevstate = vd->vdev_state; 13681986Seschrock 1369789Sahrens if (vd->vdev_offline) 1370789Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 1371789Sahrens else 1372789Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 13731544Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1374789Sahrens } 1375789Sahrens 1376789Sahrens void 13771544Seschrock vdev_reopen(vdev_t *vd) 1378789Sahrens { 13791544Seschrock spa_t *spa = vd->vdev_spa; 1380789Sahrens 13817754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 13821544Seschrock 1383789Sahrens vdev_close(vd); 1384789Sahrens (void) vdev_open(vd); 1385789Sahrens 1386789Sahrens /* 13873377Seschrock * Call vdev_validate() here to make sure we have the same device. 13883377Seschrock * Otherwise, a device with an invalid label could be successfully 13893377Seschrock * opened in response to vdev_reopen(). 13903377Seschrock */ 13916643Seschrock if (vd->vdev_aux) { 13926643Seschrock (void) vdev_validate_aux(vd); 13937754SJeff.Bonwick@Sun.COM if (vdev_readable(vd) && vdev_writeable(vd) && 13949425SEric.Schrock@Sun.COM vd->vdev_aux == &spa->spa_l2cache && 13959816SGeorge.Wilson@Sun.COM !l2arc_vdev_present(vd)) 13969816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa, vd); 13976643Seschrock } else { 13986643Seschrock (void) vdev_validate(vd); 13996643Seschrock } 14003377Seschrock 14013377Seschrock /* 14024451Seschrock * Reassess parent vdev's health. 1403789Sahrens */ 14044451Seschrock vdev_propagate_state(vd); 1405789Sahrens } 1406789Sahrens 1407789Sahrens int 14082082Seschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1409789Sahrens { 1410789Sahrens int error; 1411789Sahrens 1412789Sahrens /* 1413789Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 1414789Sahrens * For a create, however, we want to fail the request if 1415789Sahrens * there are any components we can't open. 1416789Sahrens */ 1417789Sahrens error = vdev_open(vd); 1418789Sahrens 1419789Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1420789Sahrens vdev_close(vd); 1421789Sahrens return (error ? error : ENXIO); 1422789Sahrens } 1423789Sahrens 1424789Sahrens /* 1425789Sahrens * Recursively initialize all labels. 1426789Sahrens */ 14273377Seschrock if ((error = vdev_label_init(vd, txg, isreplacing ? 14283377Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1429789Sahrens vdev_close(vd); 1430789Sahrens return (error); 1431789Sahrens } 1432789Sahrens 1433789Sahrens return (0); 1434789Sahrens } 1435789Sahrens 14361585Sbonwick void 14379816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(vdev_t *vd) 1438789Sahrens { 1439789Sahrens /* 1440789Sahrens * Aim for roughly 200 metaslabs per vdev. 1441789Sahrens */ 1442789Sahrens vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 1443789Sahrens vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1444789Sahrens } 1445789Sahrens 1446789Sahrens void 14471732Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1448789Sahrens { 14491732Sbonwick ASSERT(vd == vd->vdev_top); 145010594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 14511732Sbonwick ASSERT(ISP2(flags)); 1452789Sahrens 14531732Sbonwick if (flags & VDD_METASLAB) 14541732Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 14551732Sbonwick 14561732Sbonwick if (flags & VDD_DTL) 14571732Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 14581732Sbonwick 14591732Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1460789Sahrens } 1461789Sahrens 14628241SJeff.Bonwick@Sun.COM /* 14638241SJeff.Bonwick@Sun.COM * DTLs. 14648241SJeff.Bonwick@Sun.COM * 14658241SJeff.Bonwick@Sun.COM * A vdev's DTL (dirty time log) is the set of transaction groups for which 14668241SJeff.Bonwick@Sun.COM * the vdev has less than perfect replication. There are three kinds of DTL: 14678241SJeff.Bonwick@Sun.COM * 14688241SJeff.Bonwick@Sun.COM * DTL_MISSING: txgs for which the vdev has no valid copies of the data 14698241SJeff.Bonwick@Sun.COM * 14708241SJeff.Bonwick@Sun.COM * DTL_PARTIAL: txgs for which data is available, but not fully replicated 14718241SJeff.Bonwick@Sun.COM * 14728241SJeff.Bonwick@Sun.COM * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 14738241SJeff.Bonwick@Sun.COM * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 14748241SJeff.Bonwick@Sun.COM * txgs that was scrubbed. 14758241SJeff.Bonwick@Sun.COM * 14768241SJeff.Bonwick@Sun.COM * DTL_OUTAGE: txgs which cannot currently be read, whether due to 14778241SJeff.Bonwick@Sun.COM * persistent errors or just some device being offline. 14788241SJeff.Bonwick@Sun.COM * Unlike the other three, the DTL_OUTAGE map is not generally 14798241SJeff.Bonwick@Sun.COM * maintained; it's only computed when needed, typically to 14808241SJeff.Bonwick@Sun.COM * determine whether a device can be detached. 14818241SJeff.Bonwick@Sun.COM * 14828241SJeff.Bonwick@Sun.COM * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 14838241SJeff.Bonwick@Sun.COM * either has the data or it doesn't. 14848241SJeff.Bonwick@Sun.COM * 14858241SJeff.Bonwick@Sun.COM * For interior vdevs such as mirror and RAID-Z the picture is more complex. 14868241SJeff.Bonwick@Sun.COM * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 14878241SJeff.Bonwick@Sun.COM * if any child is less than fully replicated, then so is its parent. 14888241SJeff.Bonwick@Sun.COM * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 14898241SJeff.Bonwick@Sun.COM * comprising only those txgs which appear in 'maxfaults' or more children; 14908241SJeff.Bonwick@Sun.COM * those are the txgs we don't have enough replication to read. For example, 14918241SJeff.Bonwick@Sun.COM * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 14928241SJeff.Bonwick@Sun.COM * thus, its DTL_MISSING consists of the set of txgs that appear in more than 14938241SJeff.Bonwick@Sun.COM * two child DTL_MISSING maps. 14948241SJeff.Bonwick@Sun.COM * 14958241SJeff.Bonwick@Sun.COM * It should be clear from the above that to compute the DTLs and outage maps 14968241SJeff.Bonwick@Sun.COM * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 14978241SJeff.Bonwick@Sun.COM * Therefore, that is all we keep on disk. When loading the pool, or after 14988241SJeff.Bonwick@Sun.COM * a configuration change, we generate all other DTLs from first principles. 14998241SJeff.Bonwick@Sun.COM */ 1500789Sahrens void 15018241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1502789Sahrens { 15038241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 15048241SJeff.Bonwick@Sun.COM 15058241SJeff.Bonwick@Sun.COM ASSERT(t < DTL_TYPES); 15068241SJeff.Bonwick@Sun.COM ASSERT(vd != vd->vdev_spa->spa_root_vdev); 15078241SJeff.Bonwick@Sun.COM 1508789Sahrens mutex_enter(sm->sm_lock); 1509789Sahrens if (!space_map_contains(sm, txg, size)) 1510789Sahrens space_map_add(sm, txg, size); 1511789Sahrens mutex_exit(sm->sm_lock); 1512789Sahrens } 1513789Sahrens 15148241SJeff.Bonwick@Sun.COM boolean_t 15158241SJeff.Bonwick@Sun.COM vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1516789Sahrens { 15178241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 15188241SJeff.Bonwick@Sun.COM boolean_t dirty = B_FALSE; 15198241SJeff.Bonwick@Sun.COM 15208241SJeff.Bonwick@Sun.COM ASSERT(t < DTL_TYPES); 15218241SJeff.Bonwick@Sun.COM ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1522789Sahrens 1523789Sahrens mutex_enter(sm->sm_lock); 15248241SJeff.Bonwick@Sun.COM if (sm->sm_space != 0) 15258241SJeff.Bonwick@Sun.COM dirty = space_map_contains(sm, txg, size); 1526789Sahrens mutex_exit(sm->sm_lock); 1527789Sahrens 1528789Sahrens return (dirty); 1529789Sahrens } 1530789Sahrens 15318241SJeff.Bonwick@Sun.COM boolean_t 15328241SJeff.Bonwick@Sun.COM vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 15338241SJeff.Bonwick@Sun.COM { 15348241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[t]; 15358241SJeff.Bonwick@Sun.COM boolean_t empty; 15368241SJeff.Bonwick@Sun.COM 15378241SJeff.Bonwick@Sun.COM mutex_enter(sm->sm_lock); 15388241SJeff.Bonwick@Sun.COM empty = (sm->sm_space == 0); 15398241SJeff.Bonwick@Sun.COM mutex_exit(sm->sm_lock); 15408241SJeff.Bonwick@Sun.COM 15418241SJeff.Bonwick@Sun.COM return (empty); 15428241SJeff.Bonwick@Sun.COM } 15438241SJeff.Bonwick@Sun.COM 1544789Sahrens /* 1545789Sahrens * Reassess DTLs after a config change or scrub completion. 1546789Sahrens */ 1547789Sahrens void 1548789Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1549789Sahrens { 15501544Seschrock spa_t *spa = vd->vdev_spa; 15518241SJeff.Bonwick@Sun.COM avl_tree_t reftree; 15528241SJeff.Bonwick@Sun.COM int minref; 15538241SJeff.Bonwick@Sun.COM 15548241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 15558241SJeff.Bonwick@Sun.COM 15568241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 15578241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(vd->vdev_child[c], txg, 15588241SJeff.Bonwick@Sun.COM scrub_txg, scrub_done); 15598241SJeff.Bonwick@Sun.COM 156010594SGeorge.Wilson@Sun.COM if (vd == spa->spa_root_vdev || vd->vdev_ishole) 15618241SJeff.Bonwick@Sun.COM return; 15628241SJeff.Bonwick@Sun.COM 15638241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 1564789Sahrens mutex_enter(&vd->vdev_dtl_lock); 15657046Sahrens if (scrub_txg != 0 && 15667046Sahrens (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) { 15677046Sahrens /* XXX should check scrub_done? */ 15687046Sahrens /* 15697046Sahrens * We completed a scrub up to scrub_txg. If we 15707046Sahrens * did it without rebooting, then the scrub dtl 15717046Sahrens * will be valid, so excise the old region and 15727046Sahrens * fold in the scrub dtl. Otherwise, leave the 15737046Sahrens * dtl as-is if there was an error. 15748241SJeff.Bonwick@Sun.COM * 15758241SJeff.Bonwick@Sun.COM * There's little trick here: to excise the beginning 15768241SJeff.Bonwick@Sun.COM * of the DTL_MISSING map, we put it into a reference 15778241SJeff.Bonwick@Sun.COM * tree and then add a segment with refcnt -1 that 15788241SJeff.Bonwick@Sun.COM * covers the range [0, scrub_txg). This means 15798241SJeff.Bonwick@Sun.COM * that each txg in that range has refcnt -1 or 0. 15808241SJeff.Bonwick@Sun.COM * We then add DTL_SCRUB with a refcnt of 2, so that 15818241SJeff.Bonwick@Sun.COM * entries in the range [0, scrub_txg) will have a 15828241SJeff.Bonwick@Sun.COM * positive refcnt -- either 1 or 2. We then convert 15838241SJeff.Bonwick@Sun.COM * the reference tree into the new DTL_MISSING map. 15847046Sahrens */ 15858241SJeff.Bonwick@Sun.COM space_map_ref_create(&reftree); 15868241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, 15878241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_MISSING], 1); 15888241SJeff.Bonwick@Sun.COM space_map_ref_add_seg(&reftree, 0, scrub_txg, -1); 15898241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, 15908241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_SCRUB], 2); 15918241SJeff.Bonwick@Sun.COM space_map_ref_generate_map(&reftree, 15928241SJeff.Bonwick@Sun.COM &vd->vdev_dtl[DTL_MISSING], 1); 15938241SJeff.Bonwick@Sun.COM space_map_ref_destroy(&reftree); 1594789Sahrens } 15958241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 15968241SJeff.Bonwick@Sun.COM space_map_walk(&vd->vdev_dtl[DTL_MISSING], 15978241SJeff.Bonwick@Sun.COM space_map_add, &vd->vdev_dtl[DTL_PARTIAL]); 1598789Sahrens if (scrub_done) 15998241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 16008241SJeff.Bonwick@Sun.COM space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 16018241SJeff.Bonwick@Sun.COM if (!vdev_readable(vd)) 16028241SJeff.Bonwick@Sun.COM space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 16038241SJeff.Bonwick@Sun.COM else 16048241SJeff.Bonwick@Sun.COM space_map_walk(&vd->vdev_dtl[DTL_MISSING], 16058241SJeff.Bonwick@Sun.COM space_map_add, &vd->vdev_dtl[DTL_OUTAGE]); 1606789Sahrens mutex_exit(&vd->vdev_dtl_lock); 16077046Sahrens 16081732Sbonwick if (txg != 0) 16091732Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1610789Sahrens return; 1611789Sahrens } 1612789Sahrens 1613789Sahrens mutex_enter(&vd->vdev_dtl_lock); 16148241SJeff.Bonwick@Sun.COM for (int t = 0; t < DTL_TYPES; t++) { 16158241SJeff.Bonwick@Sun.COM if (t == DTL_SCRUB) 16168241SJeff.Bonwick@Sun.COM continue; /* leaf vdevs only */ 16178241SJeff.Bonwick@Sun.COM if (t == DTL_PARTIAL) 16188241SJeff.Bonwick@Sun.COM minref = 1; /* i.e. non-zero */ 16198241SJeff.Bonwick@Sun.COM else if (vd->vdev_nparity != 0) 16208241SJeff.Bonwick@Sun.COM minref = vd->vdev_nparity + 1; /* RAID-Z */ 16218241SJeff.Bonwick@Sun.COM else 16228241SJeff.Bonwick@Sun.COM minref = vd->vdev_children; /* any kind of mirror */ 16238241SJeff.Bonwick@Sun.COM space_map_ref_create(&reftree); 16248241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 16258241SJeff.Bonwick@Sun.COM vdev_t *cvd = vd->vdev_child[c]; 16268241SJeff.Bonwick@Sun.COM mutex_enter(&cvd->vdev_dtl_lock); 16278241SJeff.Bonwick@Sun.COM space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1); 16288241SJeff.Bonwick@Sun.COM mutex_exit(&cvd->vdev_dtl_lock); 16298241SJeff.Bonwick@Sun.COM } 16308241SJeff.Bonwick@Sun.COM space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref); 16318241SJeff.Bonwick@Sun.COM space_map_ref_destroy(&reftree); 16328241SJeff.Bonwick@Sun.COM } 1633789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1634789Sahrens } 1635789Sahrens 1636789Sahrens static int 1637789Sahrens vdev_dtl_load(vdev_t *vd) 1638789Sahrens { 1639789Sahrens spa_t *spa = vd->vdev_spa; 16408241SJeff.Bonwick@Sun.COM space_map_obj_t *smo = &vd->vdev_dtl_smo; 16411732Sbonwick objset_t *mos = spa->spa_meta_objset; 1642789Sahrens dmu_buf_t *db; 1643789Sahrens int error; 1644789Sahrens 1645789Sahrens ASSERT(vd->vdev_children == 0); 1646789Sahrens 1647789Sahrens if (smo->smo_object == 0) 1648789Sahrens return (0); 1649789Sahrens 165010594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 165110594SGeorge.Wilson@Sun.COM 16521732Sbonwick if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 16531544Seschrock return (error); 16541732Sbonwick 16554944Smaybee ASSERT3U(db->db_size, >=, sizeof (*smo)); 16564944Smaybee bcopy(db->db_data, smo, sizeof (*smo)); 16571544Seschrock dmu_buf_rele(db, FTAG); 1658789Sahrens 1659789Sahrens mutex_enter(&vd->vdev_dtl_lock); 16608241SJeff.Bonwick@Sun.COM error = space_map_load(&vd->vdev_dtl[DTL_MISSING], 16618241SJeff.Bonwick@Sun.COM NULL, SM_ALLOC, smo, mos); 1662789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1663789Sahrens 1664789Sahrens return (error); 1665789Sahrens } 1666789Sahrens 1667789Sahrens void 1668789Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1669789Sahrens { 1670789Sahrens spa_t *spa = vd->vdev_spa; 16718241SJeff.Bonwick@Sun.COM space_map_obj_t *smo = &vd->vdev_dtl_smo; 16728241SJeff.Bonwick@Sun.COM space_map_t *sm = &vd->vdev_dtl[DTL_MISSING]; 16731732Sbonwick objset_t *mos = spa->spa_meta_objset; 1674789Sahrens space_map_t smsync; 1675789Sahrens kmutex_t smlock; 1676789Sahrens dmu_buf_t *db; 1677789Sahrens dmu_tx_t *tx; 1678789Sahrens 167910594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 168010594SGeorge.Wilson@Sun.COM 1681789Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1682789Sahrens 1683789Sahrens if (vd->vdev_detached) { 1684789Sahrens if (smo->smo_object != 0) { 16851732Sbonwick int err = dmu_object_free(mos, smo->smo_object, tx); 1686789Sahrens ASSERT3U(err, ==, 0); 1687789Sahrens smo->smo_object = 0; 1688789Sahrens } 1689789Sahrens dmu_tx_commit(tx); 1690789Sahrens return; 1691789Sahrens } 1692789Sahrens 1693789Sahrens if (smo->smo_object == 0) { 1694789Sahrens ASSERT(smo->smo_objsize == 0); 1695789Sahrens ASSERT(smo->smo_alloc == 0); 16961732Sbonwick smo->smo_object = dmu_object_alloc(mos, 1697789Sahrens DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1698789Sahrens DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1699789Sahrens ASSERT(smo->smo_object != 0); 1700789Sahrens vdev_config_dirty(vd->vdev_top); 1701789Sahrens } 1702789Sahrens 1703789Sahrens mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1704789Sahrens 1705789Sahrens space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1706789Sahrens &smlock); 1707789Sahrens 1708789Sahrens mutex_enter(&smlock); 1709789Sahrens 1710789Sahrens mutex_enter(&vd->vdev_dtl_lock); 17111732Sbonwick space_map_walk(sm, space_map_add, &smsync); 1712789Sahrens mutex_exit(&vd->vdev_dtl_lock); 1713789Sahrens 17141732Sbonwick space_map_truncate(smo, mos, tx); 17151732Sbonwick space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1716789Sahrens 1717789Sahrens space_map_destroy(&smsync); 1718789Sahrens 1719789Sahrens mutex_exit(&smlock); 1720789Sahrens mutex_destroy(&smlock); 1721789Sahrens 17221732Sbonwick VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1723789Sahrens dmu_buf_will_dirty(db, tx); 17244944Smaybee ASSERT3U(db->db_size, >=, sizeof (*smo)); 17254944Smaybee bcopy(smo, db->db_data, sizeof (*smo)); 17261544Seschrock dmu_buf_rele(db, FTAG); 1727789Sahrens 1728789Sahrens dmu_tx_commit(tx); 1729789Sahrens } 1730789Sahrens 17317046Sahrens /* 17328241SJeff.Bonwick@Sun.COM * Determine whether the specified vdev can be offlined/detached/removed 17338241SJeff.Bonwick@Sun.COM * without losing data. 17348241SJeff.Bonwick@Sun.COM */ 17358241SJeff.Bonwick@Sun.COM boolean_t 17368241SJeff.Bonwick@Sun.COM vdev_dtl_required(vdev_t *vd) 17378241SJeff.Bonwick@Sun.COM { 17388241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 17398241SJeff.Bonwick@Sun.COM vdev_t *tvd = vd->vdev_top; 17408241SJeff.Bonwick@Sun.COM uint8_t cant_read = vd->vdev_cant_read; 17418241SJeff.Bonwick@Sun.COM boolean_t required; 17428241SJeff.Bonwick@Sun.COM 17438241SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 17448241SJeff.Bonwick@Sun.COM 17458241SJeff.Bonwick@Sun.COM if (vd == spa->spa_root_vdev || vd == tvd) 17468241SJeff.Bonwick@Sun.COM return (B_TRUE); 17478241SJeff.Bonwick@Sun.COM 17488241SJeff.Bonwick@Sun.COM /* 17498241SJeff.Bonwick@Sun.COM * Temporarily mark the device as unreadable, and then determine 17508241SJeff.Bonwick@Sun.COM * whether this results in any DTL outages in the top-level vdev. 17518241SJeff.Bonwick@Sun.COM * If not, we can safely offline/detach/remove the device. 17528241SJeff.Bonwick@Sun.COM */ 17538241SJeff.Bonwick@Sun.COM vd->vdev_cant_read = B_TRUE; 17548241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 17558241SJeff.Bonwick@Sun.COM required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 17568241SJeff.Bonwick@Sun.COM vd->vdev_cant_read = cant_read; 17578241SJeff.Bonwick@Sun.COM vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 17588241SJeff.Bonwick@Sun.COM 17598241SJeff.Bonwick@Sun.COM return (required); 17608241SJeff.Bonwick@Sun.COM } 17618241SJeff.Bonwick@Sun.COM 17628241SJeff.Bonwick@Sun.COM /* 17637046Sahrens * Determine if resilver is needed, and if so the txg range. 17647046Sahrens */ 17657046Sahrens boolean_t 17667046Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 17677046Sahrens { 17687046Sahrens boolean_t needed = B_FALSE; 17697046Sahrens uint64_t thismin = UINT64_MAX; 17707046Sahrens uint64_t thismax = 0; 17717046Sahrens 17727046Sahrens if (vd->vdev_children == 0) { 17737046Sahrens mutex_enter(&vd->vdev_dtl_lock); 17748241SJeff.Bonwick@Sun.COM if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 && 17758241SJeff.Bonwick@Sun.COM vdev_writeable(vd)) { 17767046Sahrens space_seg_t *ss; 17777046Sahrens 17788241SJeff.Bonwick@Sun.COM ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root); 17797046Sahrens thismin = ss->ss_start - 1; 17808241SJeff.Bonwick@Sun.COM ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root); 17817046Sahrens thismax = ss->ss_end; 17827046Sahrens needed = B_TRUE; 17837046Sahrens } 17847046Sahrens mutex_exit(&vd->vdev_dtl_lock); 17857046Sahrens } else { 17868241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 17877046Sahrens vdev_t *cvd = vd->vdev_child[c]; 17887046Sahrens uint64_t cmin, cmax; 17897046Sahrens 17907046Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 17917046Sahrens thismin = MIN(thismin, cmin); 17927046Sahrens thismax = MAX(thismax, cmax); 17937046Sahrens needed = B_TRUE; 17947046Sahrens } 17957046Sahrens } 17967046Sahrens } 17977046Sahrens 17987046Sahrens if (needed && minp) { 17997046Sahrens *minp = thismin; 18007046Sahrens *maxp = thismax; 18017046Sahrens } 18027046Sahrens return (needed); 18037046Sahrens } 18047046Sahrens 18051986Seschrock void 18061544Seschrock vdev_load(vdev_t *vd) 1807789Sahrens { 1808789Sahrens /* 1809789Sahrens * Recursively load all children. 1810789Sahrens */ 18118241SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 18121986Seschrock vdev_load(vd->vdev_child[c]); 1813789Sahrens 1814789Sahrens /* 18151585Sbonwick * If this is a top-level vdev, initialize its metaslabs. 1816789Sahrens */ 181710594SGeorge.Wilson@Sun.COM if (vd == vd->vdev_top && !vd->vdev_ishole && 18181986Seschrock (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 18191986Seschrock vdev_metaslab_init(vd, 0) != 0)) 18201986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18211986Seschrock VDEV_AUX_CORRUPT_DATA); 1822789Sahrens 1823789Sahrens /* 1824789Sahrens * If this is a leaf vdev, load its DTL. 1825789Sahrens */ 18261986Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 18271986Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18281986Seschrock VDEV_AUX_CORRUPT_DATA); 1829789Sahrens } 1830789Sahrens 18312082Seschrock /* 18325450Sbrendan * The special vdev case is used for hot spares and l2cache devices. Its 18335450Sbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 18345450Sbrendan * we make sure that we can open the underlying device, then try to read the 18355450Sbrendan * label, and make sure that the label is sane and that it hasn't been 18365450Sbrendan * repurposed to another pool. 18372082Seschrock */ 18382082Seschrock int 18395450Sbrendan vdev_validate_aux(vdev_t *vd) 18402082Seschrock { 18412082Seschrock nvlist_t *label; 18422082Seschrock uint64_t guid, version; 18432082Seschrock uint64_t state; 18442082Seschrock 18457754SJeff.Bonwick@Sun.COM if (!vdev_readable(vd)) 18466643Seschrock return (0); 18476643Seschrock 18482082Seschrock if ((label = vdev_label_read_config(vd)) == NULL) { 18492082Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 18502082Seschrock VDEV_AUX_CORRUPT_DATA); 18512082Seschrock return (-1); 18522082Seschrock } 18532082Seschrock 18542082Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 18554577Sahrens version > SPA_VERSION || 18562082Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 18572082Seschrock guid != vd->vdev_guid || 18582082Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 18592082Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 18602082Seschrock VDEV_AUX_CORRUPT_DATA); 18612082Seschrock nvlist_free(label); 18622082Seschrock return (-1); 18632082Seschrock } 18642082Seschrock 18652082Seschrock /* 18662082Seschrock * We don't actually check the pool state here. If it's in fact in 18672082Seschrock * use by another pool, we update this fact on the fly when requested. 18682082Seschrock */ 18692082Seschrock nvlist_free(label); 18702082Seschrock return (0); 18712082Seschrock } 18722082Seschrock 1873789Sahrens void 187410594SGeorge.Wilson@Sun.COM vdev_remove(vdev_t *vd, uint64_t txg) 187510594SGeorge.Wilson@Sun.COM { 187610594SGeorge.Wilson@Sun.COM spa_t *spa = vd->vdev_spa; 187710594SGeorge.Wilson@Sun.COM objset_t *mos = spa->spa_meta_objset; 187810594SGeorge.Wilson@Sun.COM dmu_tx_t *tx; 187910594SGeorge.Wilson@Sun.COM 188010594SGeorge.Wilson@Sun.COM tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 188110594SGeorge.Wilson@Sun.COM 188210594SGeorge.Wilson@Sun.COM if (vd->vdev_dtl_smo.smo_object) { 188310594SGeorge.Wilson@Sun.COM ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0); 188410594SGeorge.Wilson@Sun.COM (void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx); 188510594SGeorge.Wilson@Sun.COM vd->vdev_dtl_smo.smo_object = 0; 188610594SGeorge.Wilson@Sun.COM } 188710594SGeorge.Wilson@Sun.COM 188810594SGeorge.Wilson@Sun.COM if (vd->vdev_ms != NULL) { 188910594SGeorge.Wilson@Sun.COM for (int m = 0; m < vd->vdev_ms_count; m++) { 189010594SGeorge.Wilson@Sun.COM metaslab_t *msp = vd->vdev_ms[m]; 189110594SGeorge.Wilson@Sun.COM 189210594SGeorge.Wilson@Sun.COM if (msp == NULL || msp->ms_smo.smo_object == 0) 189310594SGeorge.Wilson@Sun.COM continue; 189410594SGeorge.Wilson@Sun.COM 189510594SGeorge.Wilson@Sun.COM ASSERT3U(msp->ms_smo.smo_alloc, ==, 0); 189610594SGeorge.Wilson@Sun.COM (void) dmu_object_free(mos, msp->ms_smo.smo_object, tx); 189710594SGeorge.Wilson@Sun.COM msp->ms_smo.smo_object = 0; 189810594SGeorge.Wilson@Sun.COM } 189910594SGeorge.Wilson@Sun.COM } 190010594SGeorge.Wilson@Sun.COM 190110594SGeorge.Wilson@Sun.COM if (vd->vdev_ms_array) { 190210594SGeorge.Wilson@Sun.COM (void) dmu_object_free(mos, vd->vdev_ms_array, tx); 190310594SGeorge.Wilson@Sun.COM vd->vdev_ms_array = 0; 190410594SGeorge.Wilson@Sun.COM vd->vdev_ms_shift = 0; 190510594SGeorge.Wilson@Sun.COM } 190610594SGeorge.Wilson@Sun.COM dmu_tx_commit(tx); 190710594SGeorge.Wilson@Sun.COM } 190810594SGeorge.Wilson@Sun.COM 190910594SGeorge.Wilson@Sun.COM void 1910789Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 1911789Sahrens { 1912789Sahrens metaslab_t *msp; 1913789Sahrens 191410594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 191510594SGeorge.Wilson@Sun.COM 1916789Sahrens while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1917789Sahrens metaslab_sync_done(msp, txg); 1918789Sahrens } 1919789Sahrens 1920789Sahrens void 1921789Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 1922789Sahrens { 1923789Sahrens spa_t *spa = vd->vdev_spa; 1924789Sahrens vdev_t *lvd; 1925789Sahrens metaslab_t *msp; 19261732Sbonwick dmu_tx_t *tx; 1927789Sahrens 192810594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 192910594SGeorge.Wilson@Sun.COM 19301732Sbonwick if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 19311732Sbonwick ASSERT(vd == vd->vdev_top); 19321732Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 19331732Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 19341732Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 19351732Sbonwick ASSERT(vd->vdev_ms_array != 0); 19361732Sbonwick vdev_config_dirty(vd); 19371732Sbonwick dmu_tx_commit(tx); 19381732Sbonwick } 1939789Sahrens 194010594SGeorge.Wilson@Sun.COM if (vd->vdev_removing) 194110594SGeorge.Wilson@Sun.COM vdev_remove(vd, txg); 194210594SGeorge.Wilson@Sun.COM 19431732Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 1944789Sahrens metaslab_sync(msp, txg); 19451732Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 19461732Sbonwick } 1947789Sahrens 1948789Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 1949789Sahrens vdev_dtl_sync(lvd, txg); 1950789Sahrens 1951789Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 1952789Sahrens } 1953789Sahrens 1954789Sahrens uint64_t 1955789Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 1956789Sahrens { 1957789Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 1958789Sahrens } 1959789Sahrens 19604451Seschrock /* 19614451Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 19624451Seschrock * not be opened, and no I/O is attempted. 19634451Seschrock */ 1964789Sahrens int 196510817SEric.Schrock@Sun.COM vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 19664451Seschrock { 19676643Seschrock vdev_t *vd; 19684451Seschrock 196910685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 19704451Seschrock 19716643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 19727754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 19737754SJeff.Bonwick@Sun.COM 19744451Seschrock if (!vd->vdev_ops->vdev_op_leaf) 19757754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 19764451Seschrock 19774451Seschrock /* 197810817SEric.Schrock@Sun.COM * We don't directly use the aux state here, but if we do a 197910817SEric.Schrock@Sun.COM * vdev_reopen(), we need this value to be present to remember why we 198010817SEric.Schrock@Sun.COM * were faulted. 198110817SEric.Schrock@Sun.COM */ 198210817SEric.Schrock@Sun.COM vd->vdev_label_aux = aux; 198310817SEric.Schrock@Sun.COM 198410817SEric.Schrock@Sun.COM /* 19854451Seschrock * Faulted state takes precedence over degraded. 19864451Seschrock */ 19874451Seschrock vd->vdev_faulted = 1ULL; 19884451Seschrock vd->vdev_degraded = 0ULL; 198910817SEric.Schrock@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 19904451Seschrock 19914451Seschrock /* 19928123SDavid.Marker@sun.com * If marking the vdev as faulted cause the top-level vdev to become 19934451Seschrock * unavailable, then back off and simply mark the vdev as degraded 19944451Seschrock * instead. 19954451Seschrock */ 199610685SGeorge.Wilson@Sun.COM if (vdev_is_dead(vd->vdev_top) && !vd->vdev_islog && 199710685SGeorge.Wilson@Sun.COM vd->vdev_aux == NULL) { 19984451Seschrock vd->vdev_degraded = 1ULL; 19994451Seschrock vd->vdev_faulted = 0ULL; 20004451Seschrock 20014451Seschrock /* 20024451Seschrock * If we reopen the device and it's not dead, only then do we 20034451Seschrock * mark it degraded. 20044451Seschrock */ 20054451Seschrock vdev_reopen(vd); 20064451Seschrock 200710817SEric.Schrock@Sun.COM if (vdev_readable(vd)) 200810817SEric.Schrock@Sun.COM vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 20094451Seschrock } 20104451Seschrock 20117754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 20124451Seschrock } 20134451Seschrock 20144451Seschrock /* 20154451Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 20164451Seschrock * user that something is wrong. The vdev continues to operate as normal as far 20174451Seschrock * as I/O is concerned. 20184451Seschrock */ 20194451Seschrock int 202010817SEric.Schrock@Sun.COM vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 20214451Seschrock { 20226643Seschrock vdev_t *vd; 20234451Seschrock 202410685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 20254451Seschrock 20266643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 20277754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 20287754SJeff.Bonwick@Sun.COM 20294451Seschrock if (!vd->vdev_ops->vdev_op_leaf) 20307754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 20314451Seschrock 20324451Seschrock /* 20334451Seschrock * If the vdev is already faulted, then don't do anything. 20344451Seschrock */ 20357754SJeff.Bonwick@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded) 20367754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, 0)); 20374451Seschrock 20384451Seschrock vd->vdev_degraded = 1ULL; 20394451Seschrock if (!vdev_is_dead(vd)) 20404451Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 204110817SEric.Schrock@Sun.COM aux); 20424451Seschrock 20437754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 20444451Seschrock } 20454451Seschrock 20464451Seschrock /* 20474451Seschrock * Online the given vdev. If 'unspare' is set, it implies two things. First, 20484451Seschrock * any attached spare device should be detached when the device finishes 20494451Seschrock * resilvering. Second, the online should be treated like a 'test' online case, 20504451Seschrock * so no FMA events are generated if the device fails to open. 20514451Seschrock */ 20524451Seschrock int 20537754SJeff.Bonwick@Sun.COM vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2054789Sahrens { 20559816SGeorge.Wilson@Sun.COM vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2056789Sahrens 205710685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 20581485Slling 20596643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 20607754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2061789Sahrens 20621585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 20637754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 20641585Sbonwick 20659816SGeorge.Wilson@Sun.COM tvd = vd->vdev_top; 2066789Sahrens vd->vdev_offline = B_FALSE; 20671485Slling vd->vdev_tmpoffline = B_FALSE; 20687754SJeff.Bonwick@Sun.COM vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 20697754SJeff.Bonwick@Sun.COM vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 20709816SGeorge.Wilson@Sun.COM 20719816SGeorge.Wilson@Sun.COM /* XXX - L2ARC 1.0 does not support expansion */ 20729816SGeorge.Wilson@Sun.COM if (!vd->vdev_aux) { 20739816SGeorge.Wilson@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 20749816SGeorge.Wilson@Sun.COM pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 20759816SGeorge.Wilson@Sun.COM } 20769816SGeorge.Wilson@Sun.COM 20779816SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 20784451Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 20794451Seschrock 20809816SGeorge.Wilson@Sun.COM if (!vd->vdev_aux) { 20819816SGeorge.Wilson@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 20829816SGeorge.Wilson@Sun.COM pvd->vdev_expanding = B_FALSE; 20839816SGeorge.Wilson@Sun.COM } 20849816SGeorge.Wilson@Sun.COM 20854451Seschrock if (newstate) 20864451Seschrock *newstate = vd->vdev_state; 20874451Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 20884451Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 20894451Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 20904451Seschrock vd->vdev_parent->vdev_child[0] == vd) 20914451Seschrock vd->vdev_unspare = B_TRUE; 2092789Sahrens 20939816SGeorge.Wilson@Sun.COM if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 20949816SGeorge.Wilson@Sun.COM 20959816SGeorge.Wilson@Sun.COM /* XXX - L2ARC 1.0 does not support expansion */ 20969816SGeorge.Wilson@Sun.COM if (vd->vdev_aux) 20979816SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 20989816SGeorge.Wilson@Sun.COM spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 20999816SGeorge.Wilson@Sun.COM } 21008241SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 2101789Sahrens } 2102789Sahrens 2103789Sahrens int 210410685SGeorge.Wilson@Sun.COM vdev_offline_log(spa_t *spa) 210510685SGeorge.Wilson@Sun.COM { 210610685SGeorge.Wilson@Sun.COM int error = 0; 210710685SGeorge.Wilson@Sun.COM 210810685SGeorge.Wilson@Sun.COM if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 210910685SGeorge.Wilson@Sun.COM NULL, DS_FIND_CHILDREN)) == 0) { 211010685SGeorge.Wilson@Sun.COM 211110685SGeorge.Wilson@Sun.COM /* 211210685SGeorge.Wilson@Sun.COM * We successfully offlined the log device, sync out the 211310685SGeorge.Wilson@Sun.COM * current txg so that the "stubby" block can be removed 211410685SGeorge.Wilson@Sun.COM * by zil_sync(). 211510685SGeorge.Wilson@Sun.COM */ 211610685SGeorge.Wilson@Sun.COM txg_wait_synced(spa->spa_dsl_pool, 0); 211710685SGeorge.Wilson@Sun.COM } 211810685SGeorge.Wilson@Sun.COM return (error); 211910685SGeorge.Wilson@Sun.COM } 212010685SGeorge.Wilson@Sun.COM 212110685SGeorge.Wilson@Sun.COM int 21224451Seschrock vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 2123789Sahrens { 21249701SGeorge.Wilson@Sun.COM vdev_t *vd, *tvd; 212510685SGeorge.Wilson@Sun.COM int error = 0; 212610685SGeorge.Wilson@Sun.COM uint64_t generation; 212710685SGeorge.Wilson@Sun.COM metaslab_group_t *mg; 212810685SGeorge.Wilson@Sun.COM 212910685SGeorge.Wilson@Sun.COM top: 213010685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_ALLOC); 2131789Sahrens 21326643Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 21337754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2134789Sahrens 21351585Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 21367754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 21371585Sbonwick 21389701SGeorge.Wilson@Sun.COM tvd = vd->vdev_top; 213910685SGeorge.Wilson@Sun.COM mg = tvd->vdev_mg; 214010685SGeorge.Wilson@Sun.COM generation = spa->spa_config_generation + 1; 21419701SGeorge.Wilson@Sun.COM 2142789Sahrens /* 21431732Sbonwick * If the device isn't already offline, try to offline it. 2144789Sahrens */ 21451732Sbonwick if (!vd->vdev_offline) { 21461732Sbonwick /* 21478241SJeff.Bonwick@Sun.COM * If this device has the only valid copy of some data, 21489701SGeorge.Wilson@Sun.COM * don't allow it to be offlined. Log devices are always 21499701SGeorge.Wilson@Sun.COM * expendable. 21501732Sbonwick */ 21519701SGeorge.Wilson@Sun.COM if (!tvd->vdev_islog && vd->vdev_aux == NULL && 21529701SGeorge.Wilson@Sun.COM vdev_dtl_required(vd)) 21537754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2154789Sahrens 21551732Sbonwick /* 215610685SGeorge.Wilson@Sun.COM * If the top-level is a slog and it's had allocations 215710685SGeorge.Wilson@Sun.COM * then proceed. We check that the vdev's metaslab 215810685SGeorge.Wilson@Sun.COM * grop is not NULL since it's possible that we may 215910685SGeorge.Wilson@Sun.COM * have just added this vdev and have not yet initialized 216010685SGeorge.Wilson@Sun.COM * it's metaslabs. 216110685SGeorge.Wilson@Sun.COM */ 216210685SGeorge.Wilson@Sun.COM if (tvd->vdev_islog && mg != NULL) { 216310685SGeorge.Wilson@Sun.COM /* 216410685SGeorge.Wilson@Sun.COM * Prevent any future allocations. 216510685SGeorge.Wilson@Sun.COM */ 216610685SGeorge.Wilson@Sun.COM metaslab_class_remove(spa->spa_log_class, mg); 216710685SGeorge.Wilson@Sun.COM (void) spa_vdev_state_exit(spa, vd, 0); 216810685SGeorge.Wilson@Sun.COM 216910685SGeorge.Wilson@Sun.COM error = vdev_offline_log(spa); 217010685SGeorge.Wilson@Sun.COM 217110685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_ALLOC); 217210685SGeorge.Wilson@Sun.COM 217310685SGeorge.Wilson@Sun.COM /* 217410685SGeorge.Wilson@Sun.COM * Check to see if the config has changed. 217510685SGeorge.Wilson@Sun.COM */ 217610685SGeorge.Wilson@Sun.COM if (error || generation != spa->spa_config_generation) { 217710685SGeorge.Wilson@Sun.COM metaslab_class_add(spa->spa_log_class, mg); 217810685SGeorge.Wilson@Sun.COM if (error) 217910685SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, 218010685SGeorge.Wilson@Sun.COM vd, error)); 218110685SGeorge.Wilson@Sun.COM (void) spa_vdev_state_exit(spa, vd, 0); 218210685SGeorge.Wilson@Sun.COM goto top; 218310685SGeorge.Wilson@Sun.COM } 218410685SGeorge.Wilson@Sun.COM ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0); 218510685SGeorge.Wilson@Sun.COM } 218610685SGeorge.Wilson@Sun.COM 218710685SGeorge.Wilson@Sun.COM /* 21881732Sbonwick * Offline this device and reopen its top-level vdev. 21899701SGeorge.Wilson@Sun.COM * If the top-level vdev is a log device then just offline 21909701SGeorge.Wilson@Sun.COM * it. Otherwise, if this action results in the top-level 21919701SGeorge.Wilson@Sun.COM * vdev becoming unusable, undo it and fail the request. 21921732Sbonwick */ 21931732Sbonwick vd->vdev_offline = B_TRUE; 21949701SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 21959701SGeorge.Wilson@Sun.COM 21969701SGeorge.Wilson@Sun.COM if (!tvd->vdev_islog && vd->vdev_aux == NULL && 21979701SGeorge.Wilson@Sun.COM vdev_is_dead(tvd)) { 21981732Sbonwick vd->vdev_offline = B_FALSE; 21999701SGeorge.Wilson@Sun.COM vdev_reopen(tvd); 22007754SJeff.Bonwick@Sun.COM return (spa_vdev_state_exit(spa, NULL, EBUSY)); 22011732Sbonwick } 220210685SGeorge.Wilson@Sun.COM 220310685SGeorge.Wilson@Sun.COM /* 220410685SGeorge.Wilson@Sun.COM * Add the device back into the metaslab rotor so that 220510685SGeorge.Wilson@Sun.COM * once we online the device it's open for business. 220610685SGeorge.Wilson@Sun.COM */ 220710685SGeorge.Wilson@Sun.COM if (tvd->vdev_islog && mg != NULL) 220810685SGeorge.Wilson@Sun.COM metaslab_class_add(spa->spa_log_class, mg); 2209789Sahrens } 2210789Sahrens 22117754SJeff.Bonwick@Sun.COM vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 22121732Sbonwick 221310685SGeorge.Wilson@Sun.COM return (spa_vdev_state_exit(spa, vd, 0)); 2214789Sahrens } 2215789Sahrens 22161544Seschrock /* 22171544Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 22181544Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 22191544Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 22201544Seschrock */ 22211544Seschrock void 22227754SJeff.Bonwick@Sun.COM vdev_clear(spa_t *spa, vdev_t *vd) 2223789Sahrens { 22247754SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 22257754SJeff.Bonwick@Sun.COM 22267754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2227789Sahrens 22281544Seschrock if (vd == NULL) 22297754SJeff.Bonwick@Sun.COM vd = rvd; 2230789Sahrens 22311544Seschrock vd->vdev_stat.vs_read_errors = 0; 22321544Seschrock vd->vdev_stat.vs_write_errors = 0; 22331544Seschrock vd->vdev_stat.vs_checksum_errors = 0; 2234789Sahrens 22357754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 22367754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd->vdev_child[c]); 22374451Seschrock 22384451Seschrock /* 22396959Sek110237 * If we're in the FAULTED state or have experienced failed I/O, then 22406959Sek110237 * clear the persistent state and attempt to reopen the device. We 22416959Sek110237 * also mark the vdev config dirty, so that the new faulted state is 22426959Sek110237 * written out to disk. 22434451Seschrock */ 22447754SJeff.Bonwick@Sun.COM if (vd->vdev_faulted || vd->vdev_degraded || 22457754SJeff.Bonwick@Sun.COM !vdev_readable(vd) || !vdev_writeable(vd)) { 22466959Sek110237 2247*10830SEric.Schrock@Sun.COM /* 2248*10830SEric.Schrock@Sun.COM * When reopening in reponse to a clear event, it may be due to 2249*10830SEric.Schrock@Sun.COM * a fmadm repair request. In this case, if the device is 2250*10830SEric.Schrock@Sun.COM * still broken, we want to still post the ereport again. 2251*10830SEric.Schrock@Sun.COM */ 2252*10830SEric.Schrock@Sun.COM vd->vdev_forcefault = B_TRUE; 2253*10830SEric.Schrock@Sun.COM 22544451Seschrock vd->vdev_faulted = vd->vdev_degraded = 0; 22557754SJeff.Bonwick@Sun.COM vd->vdev_cant_read = B_FALSE; 22567754SJeff.Bonwick@Sun.COM vd->vdev_cant_write = B_FALSE; 22577754SJeff.Bonwick@Sun.COM 22584451Seschrock vdev_reopen(vd); 22594451Seschrock 2260*10830SEric.Schrock@Sun.COM vd->vdev_forcefault = B_FALSE; 2261*10830SEric.Schrock@Sun.COM 22627754SJeff.Bonwick@Sun.COM if (vd != rvd) 22637754SJeff.Bonwick@Sun.COM vdev_state_dirty(vd->vdev_top); 22647754SJeff.Bonwick@Sun.COM 22657754SJeff.Bonwick@Sun.COM if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 22664808Sek110237 spa_async_request(spa, SPA_ASYNC_RESILVER); 22674451Seschrock 22684451Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 22694451Seschrock } 2270*10830SEric.Schrock@Sun.COM 2271*10830SEric.Schrock@Sun.COM /* 2272*10830SEric.Schrock@Sun.COM * When clearing a FMA-diagnosed fault, we always want to 2273*10830SEric.Schrock@Sun.COM * unspare the device, as we assume that the original spare was 2274*10830SEric.Schrock@Sun.COM * done in response to the FMA fault. 2275*10830SEric.Schrock@Sun.COM */ 2276*10830SEric.Schrock@Sun.COM if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 2277*10830SEric.Schrock@Sun.COM vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2278*10830SEric.Schrock@Sun.COM vd->vdev_parent->vdev_child[0] == vd) 2279*10830SEric.Schrock@Sun.COM vd->vdev_unspare = B_TRUE; 2280789Sahrens } 2281789Sahrens 22827754SJeff.Bonwick@Sun.COM boolean_t 22837754SJeff.Bonwick@Sun.COM vdev_is_dead(vdev_t *vd) 22845329Sgw25295 { 228510594SGeorge.Wilson@Sun.COM /* 228610594SGeorge.Wilson@Sun.COM * Holes and missing devices are always considered "dead". 228710594SGeorge.Wilson@Sun.COM * This simplifies the code since we don't have to check for 228810594SGeorge.Wilson@Sun.COM * these types of devices in the various code paths. 228910594SGeorge.Wilson@Sun.COM * Instead we rely on the fact that we skip over dead devices 229010594SGeorge.Wilson@Sun.COM * before issuing I/O to them. 229110594SGeorge.Wilson@Sun.COM */ 229210594SGeorge.Wilson@Sun.COM return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole || 229310594SGeorge.Wilson@Sun.COM vd->vdev_ops == &vdev_missing_ops); 22945329Sgw25295 } 22955329Sgw25295 22967754SJeff.Bonwick@Sun.COM boolean_t 22977754SJeff.Bonwick@Sun.COM vdev_readable(vdev_t *vd) 2298789Sahrens { 22997754SJeff.Bonwick@Sun.COM return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2300789Sahrens } 2301789Sahrens 23027754SJeff.Bonwick@Sun.COM boolean_t 23037754SJeff.Bonwick@Sun.COM vdev_writeable(vdev_t *vd) 2304789Sahrens { 23057754SJeff.Bonwick@Sun.COM return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 23067754SJeff.Bonwick@Sun.COM } 2307789Sahrens 23087754SJeff.Bonwick@Sun.COM boolean_t 23097980SGeorge.Wilson@Sun.COM vdev_allocatable(vdev_t *vd) 23107980SGeorge.Wilson@Sun.COM { 23118241SJeff.Bonwick@Sun.COM uint64_t state = vd->vdev_state; 23128241SJeff.Bonwick@Sun.COM 23137980SGeorge.Wilson@Sun.COM /* 23148241SJeff.Bonwick@Sun.COM * We currently allow allocations from vdevs which may be in the 23157980SGeorge.Wilson@Sun.COM * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 23167980SGeorge.Wilson@Sun.COM * fails to reopen then we'll catch it later when we're holding 23178241SJeff.Bonwick@Sun.COM * the proper locks. Note that we have to get the vdev state 23188241SJeff.Bonwick@Sun.COM * in a local variable because although it changes atomically, 23198241SJeff.Bonwick@Sun.COM * we're asking two separate questions about it. 23207980SGeorge.Wilson@Sun.COM */ 23218241SJeff.Bonwick@Sun.COM return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 232210594SGeorge.Wilson@Sun.COM !vd->vdev_cant_write && !vd->vdev_ishole && !vd->vdev_removing); 23237980SGeorge.Wilson@Sun.COM } 23247980SGeorge.Wilson@Sun.COM 23257980SGeorge.Wilson@Sun.COM boolean_t 23267754SJeff.Bonwick@Sun.COM vdev_accessible(vdev_t *vd, zio_t *zio) 23277754SJeff.Bonwick@Sun.COM { 23287754SJeff.Bonwick@Sun.COM ASSERT(zio->io_vd == vd); 2329789Sahrens 23307754SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 23317754SJeff.Bonwick@Sun.COM return (B_FALSE); 2332789Sahrens 23337754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_READ) 23347754SJeff.Bonwick@Sun.COM return (!vd->vdev_cant_read); 2335789Sahrens 23367754SJeff.Bonwick@Sun.COM if (zio->io_type == ZIO_TYPE_WRITE) 23377754SJeff.Bonwick@Sun.COM return (!vd->vdev_cant_write); 23387754SJeff.Bonwick@Sun.COM 23397754SJeff.Bonwick@Sun.COM return (B_TRUE); 2340789Sahrens } 2341789Sahrens 2342789Sahrens /* 2343789Sahrens * Get statistics for the given vdev. 2344789Sahrens */ 2345789Sahrens void 2346789Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2347789Sahrens { 2348789Sahrens vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 2349789Sahrens 2350789Sahrens mutex_enter(&vd->vdev_stat_lock); 2351789Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 23527046Sahrens vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors; 2353789Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2354789Sahrens vs->vs_state = vd->vdev_state; 23559816SGeorge.Wilson@Sun.COM vs->vs_rsize = vdev_get_min_asize(vd); 23569816SGeorge.Wilson@Sun.COM if (vd->vdev_ops->vdev_op_leaf) 23579816SGeorge.Wilson@Sun.COM vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 2358789Sahrens mutex_exit(&vd->vdev_stat_lock); 2359789Sahrens 2360789Sahrens /* 2361789Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 2362789Sahrens * over all top-level vdevs (i.e. the direct children of the root). 2363789Sahrens */ 2364789Sahrens if (vd == rvd) { 23657754SJeff.Bonwick@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 2366789Sahrens vdev_t *cvd = rvd->vdev_child[c]; 2367789Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 2368789Sahrens 2369789Sahrens mutex_enter(&vd->vdev_stat_lock); 23707754SJeff.Bonwick@Sun.COM for (int t = 0; t < ZIO_TYPES; t++) { 2371789Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 2372789Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 2373789Sahrens } 2374789Sahrens vs->vs_scrub_examined += cvs->vs_scrub_examined; 2375789Sahrens mutex_exit(&vd->vdev_stat_lock); 2376789Sahrens } 2377789Sahrens } 2378789Sahrens } 2379789Sahrens 2380789Sahrens void 23815450Sbrendan vdev_clear_stats(vdev_t *vd) 23825450Sbrendan { 23835450Sbrendan mutex_enter(&vd->vdev_stat_lock); 23845450Sbrendan vd->vdev_stat.vs_space = 0; 23855450Sbrendan vd->vdev_stat.vs_dspace = 0; 23865450Sbrendan vd->vdev_stat.vs_alloc = 0; 23875450Sbrendan mutex_exit(&vd->vdev_stat_lock); 23885450Sbrendan } 23895450Sbrendan 23905450Sbrendan void 23917754SJeff.Bonwick@Sun.COM vdev_stat_update(zio_t *zio, uint64_t psize) 2392789Sahrens { 23938241SJeff.Bonwick@Sun.COM spa_t *spa = zio->io_spa; 23948241SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 23957754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2396789Sahrens vdev_t *pvd; 2397789Sahrens uint64_t txg = zio->io_txg; 2398789Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2399789Sahrens zio_type_t type = zio->io_type; 2400789Sahrens int flags = zio->io_flags; 2401789Sahrens 24027754SJeff.Bonwick@Sun.COM /* 24037754SJeff.Bonwick@Sun.COM * If this i/o is a gang leader, it didn't do any actual work. 24047754SJeff.Bonwick@Sun.COM */ 24057754SJeff.Bonwick@Sun.COM if (zio->io_gang_tree) 24067754SJeff.Bonwick@Sun.COM return; 24077754SJeff.Bonwick@Sun.COM 2408789Sahrens if (zio->io_error == 0) { 24097754SJeff.Bonwick@Sun.COM /* 24107754SJeff.Bonwick@Sun.COM * If this is a root i/o, don't count it -- we've already 24117754SJeff.Bonwick@Sun.COM * counted the top-level vdevs, and vdev_get_stats() will 24127754SJeff.Bonwick@Sun.COM * aggregate them when asked. This reduces contention on 24137754SJeff.Bonwick@Sun.COM * the root vdev_stat_lock and implicitly handles blocks 24147754SJeff.Bonwick@Sun.COM * that compress away to holes, for which there is no i/o. 24157754SJeff.Bonwick@Sun.COM * (Holes never create vdev children, so all the counters 24167754SJeff.Bonwick@Sun.COM * remain zero, which is what we want.) 24177754SJeff.Bonwick@Sun.COM * 24187754SJeff.Bonwick@Sun.COM * Note: this only applies to successful i/o (io_error == 0) 24197754SJeff.Bonwick@Sun.COM * because unlike i/o counts, errors are not additive. 24207754SJeff.Bonwick@Sun.COM * When reading a ditto block, for example, failure of 24217754SJeff.Bonwick@Sun.COM * one top-level vdev does not imply a root-level error. 24227754SJeff.Bonwick@Sun.COM */ 24237754SJeff.Bonwick@Sun.COM if (vd == rvd) 24247754SJeff.Bonwick@Sun.COM return; 24257754SJeff.Bonwick@Sun.COM 24267754SJeff.Bonwick@Sun.COM ASSERT(vd == zio->io_vd); 24278241SJeff.Bonwick@Sun.COM 24288241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_IO_BYPASS) 24298241SJeff.Bonwick@Sun.COM return; 24308241SJeff.Bonwick@Sun.COM 24318241SJeff.Bonwick@Sun.COM mutex_enter(&vd->vdev_stat_lock); 24328241SJeff.Bonwick@Sun.COM 24337754SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_IO_REPAIR) { 24341807Sbonwick if (flags & ZIO_FLAG_SCRUB_THREAD) 24357754SJeff.Bonwick@Sun.COM vs->vs_scrub_repaired += psize; 24368241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_SELF_HEAL) 24377754SJeff.Bonwick@Sun.COM vs->vs_self_healed += psize; 2438789Sahrens } 24398241SJeff.Bonwick@Sun.COM 24408241SJeff.Bonwick@Sun.COM vs->vs_ops[type]++; 24418241SJeff.Bonwick@Sun.COM vs->vs_bytes[type] += psize; 24428241SJeff.Bonwick@Sun.COM 24438241SJeff.Bonwick@Sun.COM mutex_exit(&vd->vdev_stat_lock); 2444789Sahrens return; 2445789Sahrens } 2446789Sahrens 2447789Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 2448789Sahrens return; 2449789Sahrens 24509725SEric.Schrock@Sun.COM /* 24519725SEric.Schrock@Sun.COM * If this is an I/O error that is going to be retried, then ignore the 24529725SEric.Schrock@Sun.COM * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 24539725SEric.Schrock@Sun.COM * hard errors, when in reality they can happen for any number of 24549725SEric.Schrock@Sun.COM * innocuous reasons (bus resets, MPxIO link failure, etc). 24559725SEric.Schrock@Sun.COM */ 24569725SEric.Schrock@Sun.COM if (zio->io_error == EIO && 24579725SEric.Schrock@Sun.COM !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 24589725SEric.Schrock@Sun.COM return; 24599725SEric.Schrock@Sun.COM 246010685SGeorge.Wilson@Sun.COM /* 246110685SGeorge.Wilson@Sun.COM * Intent logs writes won't propagate their error to the root 246210685SGeorge.Wilson@Sun.COM * I/O so don't mark these types of failures as pool-level 246310685SGeorge.Wilson@Sun.COM * errors. 246410685SGeorge.Wilson@Sun.COM */ 246510685SGeorge.Wilson@Sun.COM if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 246610685SGeorge.Wilson@Sun.COM return; 246710685SGeorge.Wilson@Sun.COM 24687754SJeff.Bonwick@Sun.COM mutex_enter(&vd->vdev_stat_lock); 24699230SGeorge.Wilson@Sun.COM if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 24707754SJeff.Bonwick@Sun.COM if (zio->io_error == ECKSUM) 24717754SJeff.Bonwick@Sun.COM vs->vs_checksum_errors++; 24727754SJeff.Bonwick@Sun.COM else 24737754SJeff.Bonwick@Sun.COM vs->vs_read_errors++; 2474789Sahrens } 24759230SGeorge.Wilson@Sun.COM if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 24767754SJeff.Bonwick@Sun.COM vs->vs_write_errors++; 24777754SJeff.Bonwick@Sun.COM mutex_exit(&vd->vdev_stat_lock); 2478789Sahrens 24798241SJeff.Bonwick@Sun.COM if (type == ZIO_TYPE_WRITE && txg != 0 && 24808241SJeff.Bonwick@Sun.COM (!(flags & ZIO_FLAG_IO_REPAIR) || 24818241SJeff.Bonwick@Sun.COM (flags & ZIO_FLAG_SCRUB_THREAD))) { 24828241SJeff.Bonwick@Sun.COM /* 24838241SJeff.Bonwick@Sun.COM * This is either a normal write (not a repair), or it's a 24848241SJeff.Bonwick@Sun.COM * repair induced by the scrub thread. In the normal case, 24858241SJeff.Bonwick@Sun.COM * we commit the DTL change in the same txg as the block 24868241SJeff.Bonwick@Sun.COM * was born. In the scrub-induced repair case, we know that 24878241SJeff.Bonwick@Sun.COM * scrubs run in first-pass syncing context, so we commit 24888241SJeff.Bonwick@Sun.COM * the DTL change in spa->spa_syncing_txg. 24898241SJeff.Bonwick@Sun.COM * 24908241SJeff.Bonwick@Sun.COM * We currently do not make DTL entries for failed spontaneous 24918241SJeff.Bonwick@Sun.COM * self-healing writes triggered by normal (non-scrubbing) 24928241SJeff.Bonwick@Sun.COM * reads, because we have no transactional context in which to 24938241SJeff.Bonwick@Sun.COM * do so -- and it's not clear that it'd be desirable anyway. 24948241SJeff.Bonwick@Sun.COM */ 24958241SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf) { 24968241SJeff.Bonwick@Sun.COM uint64_t commit_txg = txg; 24978241SJeff.Bonwick@Sun.COM if (flags & ZIO_FLAG_SCRUB_THREAD) { 24988241SJeff.Bonwick@Sun.COM ASSERT(flags & ZIO_FLAG_IO_REPAIR); 24998241SJeff.Bonwick@Sun.COM ASSERT(spa_sync_pass(spa) == 1); 25008241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 25018241SJeff.Bonwick@Sun.COM commit_txg = spa->spa_syncing_txg; 25028241SJeff.Bonwick@Sun.COM } 25038241SJeff.Bonwick@Sun.COM ASSERT(commit_txg >= spa->spa_syncing_txg); 25048241SJeff.Bonwick@Sun.COM if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 25058241SJeff.Bonwick@Sun.COM return; 25068241SJeff.Bonwick@Sun.COM for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 25078241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 25088241SJeff.Bonwick@Sun.COM vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2509789Sahrens } 25108241SJeff.Bonwick@Sun.COM if (vd != rvd) 25118241SJeff.Bonwick@Sun.COM vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2512789Sahrens } 2513789Sahrens } 2514789Sahrens 2515789Sahrens void 2516789Sahrens vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 2517789Sahrens { 2518789Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2519789Sahrens 25209816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 2521789Sahrens vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 2522789Sahrens 2523789Sahrens mutex_enter(&vd->vdev_stat_lock); 2524789Sahrens 2525789Sahrens if (type == POOL_SCRUB_NONE) { 2526789Sahrens /* 2527789Sahrens * Update completion and end time. Leave everything else alone 2528789Sahrens * so we can report what happened during the previous scrub. 2529789Sahrens */ 2530789Sahrens vs->vs_scrub_complete = complete; 2531789Sahrens vs->vs_scrub_end = gethrestime_sec(); 2532789Sahrens } else { 2533789Sahrens vs->vs_scrub_type = type; 2534789Sahrens vs->vs_scrub_complete = 0; 2535789Sahrens vs->vs_scrub_examined = 0; 2536789Sahrens vs->vs_scrub_repaired = 0; 2537789Sahrens vs->vs_scrub_start = gethrestime_sec(); 2538789Sahrens vs->vs_scrub_end = 0; 2539789Sahrens } 2540789Sahrens 2541789Sahrens mutex_exit(&vd->vdev_stat_lock); 2542789Sahrens } 2543789Sahrens 2544789Sahrens /* 2545789Sahrens * Update the in-core space usage stats for this vdev and the root vdev. 2546789Sahrens */ 2547789Sahrens void 25485450Sbrendan vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta, 25495450Sbrendan boolean_t update_root) 2550789Sahrens { 25514527Sperrin int64_t dspace_delta = space_delta; 25524527Sperrin spa_t *spa = vd->vdev_spa; 25534527Sperrin vdev_t *rvd = spa->spa_root_vdev; 25544527Sperrin 2555789Sahrens ASSERT(vd == vd->vdev_top); 25564527Sperrin 25574527Sperrin /* 25584527Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 25594527Sperrin * factor. We must calculate this here and not at the root vdev 25604527Sperrin * because the root vdev's psize-to-asize is simply the max of its 25614527Sperrin * childrens', thus not accurate enough for us. 25624527Sperrin */ 25634527Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 25649701SGeorge.Wilson@Sun.COM ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 25654527Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 25664527Sperrin vd->vdev_deflate_ratio; 2567789Sahrens 25684527Sperrin mutex_enter(&vd->vdev_stat_lock); 25694527Sperrin vd->vdev_stat.vs_space += space_delta; 25704527Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 25714527Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 25724527Sperrin mutex_exit(&vd->vdev_stat_lock); 25732082Seschrock 25745450Sbrendan if (update_root) { 25755450Sbrendan ASSERT(rvd == vd->vdev_parent); 25765450Sbrendan ASSERT(vd->vdev_ms_count != 0); 25774527Sperrin 25785450Sbrendan /* 25795450Sbrendan * Don't count non-normal (e.g. intent log) space as part of 25805450Sbrendan * the pool's capacity. 25815450Sbrendan */ 258210594SGeorge.Wilson@Sun.COM if (vd->vdev_islog) 25835450Sbrendan return; 25845450Sbrendan 25855450Sbrendan mutex_enter(&rvd->vdev_stat_lock); 25865450Sbrendan rvd->vdev_stat.vs_space += space_delta; 25875450Sbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 25885450Sbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 25895450Sbrendan mutex_exit(&rvd->vdev_stat_lock); 25905450Sbrendan } 2591789Sahrens } 2592789Sahrens 2593789Sahrens /* 2594789Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 2595789Sahrens * so that it will be written out next time the vdev configuration is synced. 2596789Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 2597789Sahrens */ 2598789Sahrens void 2599789Sahrens vdev_config_dirty(vdev_t *vd) 2600789Sahrens { 2601789Sahrens spa_t *spa = vd->vdev_spa; 2602789Sahrens vdev_t *rvd = spa->spa_root_vdev; 2603789Sahrens int c; 2604789Sahrens 26051601Sbonwick /* 26069425SEric.Schrock@Sun.COM * If this is an aux vdev (as with l2cache and spare devices), then we 26079425SEric.Schrock@Sun.COM * update the vdev config manually and set the sync flag. 26086643Seschrock */ 26096643Seschrock if (vd->vdev_aux != NULL) { 26106643Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 26116643Seschrock nvlist_t **aux; 26126643Seschrock uint_t naux; 26136643Seschrock 26146643Seschrock for (c = 0; c < sav->sav_count; c++) { 26156643Seschrock if (sav->sav_vdevs[c] == vd) 26166643Seschrock break; 26176643Seschrock } 26186643Seschrock 26197754SJeff.Bonwick@Sun.COM if (c == sav->sav_count) { 26207754SJeff.Bonwick@Sun.COM /* 26217754SJeff.Bonwick@Sun.COM * We're being removed. There's nothing more to do. 26227754SJeff.Bonwick@Sun.COM */ 26237754SJeff.Bonwick@Sun.COM ASSERT(sav->sav_sync == B_TRUE); 26247754SJeff.Bonwick@Sun.COM return; 26257754SJeff.Bonwick@Sun.COM } 26267754SJeff.Bonwick@Sun.COM 26276643Seschrock sav->sav_sync = B_TRUE; 26286643Seschrock 26299425SEric.Schrock@Sun.COM if (nvlist_lookup_nvlist_array(sav->sav_config, 26309425SEric.Schrock@Sun.COM ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 26319425SEric.Schrock@Sun.COM VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 26329425SEric.Schrock@Sun.COM ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 26339425SEric.Schrock@Sun.COM } 26346643Seschrock 26356643Seschrock ASSERT(c < naux); 26366643Seschrock 26376643Seschrock /* 26386643Seschrock * Setting the nvlist in the middle if the array is a little 26396643Seschrock * sketchy, but it will work. 26406643Seschrock */ 26416643Seschrock nvlist_free(aux[c]); 26426643Seschrock aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE); 26436643Seschrock 26446643Seschrock return; 26456643Seschrock } 26466643Seschrock 26476643Seschrock /* 26487754SJeff.Bonwick@Sun.COM * The dirty list is protected by the SCL_CONFIG lock. The caller 26497754SJeff.Bonwick@Sun.COM * must either hold SCL_CONFIG as writer, or must be the sync thread 26507754SJeff.Bonwick@Sun.COM * (which holds SCL_CONFIG as reader). There's only one sync thread, 26511601Sbonwick * so this is sufficient to ensure mutual exclusion. 26521601Sbonwick */ 26537754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 26547754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 26557754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_CONFIG, RW_READER))); 26561601Sbonwick 2657789Sahrens if (vd == rvd) { 2658789Sahrens for (c = 0; c < rvd->vdev_children; c++) 2659789Sahrens vdev_config_dirty(rvd->vdev_child[c]); 2660789Sahrens } else { 2661789Sahrens ASSERT(vd == vd->vdev_top); 2662789Sahrens 266310594SGeorge.Wilson@Sun.COM if (!list_link_active(&vd->vdev_config_dirty_node) && 266410594SGeorge.Wilson@Sun.COM !vd->vdev_ishole) 26657754SJeff.Bonwick@Sun.COM list_insert_head(&spa->spa_config_dirty_list, vd); 2666789Sahrens } 2667789Sahrens } 2668789Sahrens 2669789Sahrens void 2670789Sahrens vdev_config_clean(vdev_t *vd) 2671789Sahrens { 26721601Sbonwick spa_t *spa = vd->vdev_spa; 26731601Sbonwick 26747754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 26757754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 26767754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_CONFIG, RW_READER))); 26777754SJeff.Bonwick@Sun.COM 26787754SJeff.Bonwick@Sun.COM ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 26797754SJeff.Bonwick@Sun.COM list_remove(&spa->spa_config_dirty_list, vd); 26807754SJeff.Bonwick@Sun.COM } 26817754SJeff.Bonwick@Sun.COM 26827754SJeff.Bonwick@Sun.COM /* 26837754SJeff.Bonwick@Sun.COM * Mark a top-level vdev's state as dirty, so that the next pass of 26847754SJeff.Bonwick@Sun.COM * spa_sync() can convert this into vdev_config_dirty(). We distinguish 26857754SJeff.Bonwick@Sun.COM * the state changes from larger config changes because they require 26867754SJeff.Bonwick@Sun.COM * much less locking, and are often needed for administrative actions. 26877754SJeff.Bonwick@Sun.COM */ 26887754SJeff.Bonwick@Sun.COM void 26897754SJeff.Bonwick@Sun.COM vdev_state_dirty(vdev_t *vd) 26907754SJeff.Bonwick@Sun.COM { 26917754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 26927754SJeff.Bonwick@Sun.COM 26937754SJeff.Bonwick@Sun.COM ASSERT(vd == vd->vdev_top); 26941601Sbonwick 26957754SJeff.Bonwick@Sun.COM /* 26967754SJeff.Bonwick@Sun.COM * The state list is protected by the SCL_STATE lock. The caller 26977754SJeff.Bonwick@Sun.COM * must either hold SCL_STATE as writer, or must be the sync thread 26987754SJeff.Bonwick@Sun.COM * (which holds SCL_STATE as reader). There's only one sync thread, 26997754SJeff.Bonwick@Sun.COM * so this is sufficient to ensure mutual exclusion. 27007754SJeff.Bonwick@Sun.COM */ 27017754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 27027754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 27037754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_STATE, RW_READER))); 27047754SJeff.Bonwick@Sun.COM 27057754SJeff.Bonwick@Sun.COM if (!list_link_active(&vd->vdev_state_dirty_node)) 27067754SJeff.Bonwick@Sun.COM list_insert_head(&spa->spa_state_dirty_list, vd); 27077754SJeff.Bonwick@Sun.COM } 27087754SJeff.Bonwick@Sun.COM 27097754SJeff.Bonwick@Sun.COM void 27107754SJeff.Bonwick@Sun.COM vdev_state_clean(vdev_t *vd) 27117754SJeff.Bonwick@Sun.COM { 27127754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 27137754SJeff.Bonwick@Sun.COM 27147754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 27157754SJeff.Bonwick@Sun.COM (dsl_pool_sync_context(spa_get_dsl(spa)) && 27167754SJeff.Bonwick@Sun.COM spa_config_held(spa, SCL_STATE, RW_READER))); 27177754SJeff.Bonwick@Sun.COM 27187754SJeff.Bonwick@Sun.COM ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 27197754SJeff.Bonwick@Sun.COM list_remove(&spa->spa_state_dirty_list, vd); 2720789Sahrens } 2721789Sahrens 27226523Sek110237 /* 27236523Sek110237 * Propagate vdev state up from children to parent. 27246523Sek110237 */ 27251775Sbillm void 27261775Sbillm vdev_propagate_state(vdev_t *vd) 27271775Sbillm { 27288241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 27298241SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 27301775Sbillm int degraded = 0, faulted = 0; 27311775Sbillm int corrupted = 0; 27321775Sbillm vdev_t *child; 27331775Sbillm 27344451Seschrock if (vd->vdev_children > 0) { 27359816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 27364451Seschrock child = vd->vdev_child[c]; 27376976Seschrock 273810594SGeorge.Wilson@Sun.COM /* 273910594SGeorge.Wilson@Sun.COM * Don't factor holes into the decision. 274010594SGeorge.Wilson@Sun.COM */ 274110594SGeorge.Wilson@Sun.COM if (child->vdev_ishole) 274210594SGeorge.Wilson@Sun.COM continue; 274310594SGeorge.Wilson@Sun.COM 27447754SJeff.Bonwick@Sun.COM if (!vdev_readable(child) || 27458241SJeff.Bonwick@Sun.COM (!vdev_writeable(child) && spa_writeable(spa))) { 27466976Seschrock /* 27476976Seschrock * Root special: if there is a top-level log 27486976Seschrock * device, treat the root vdev as if it were 27496976Seschrock * degraded. 27506976Seschrock */ 27516976Seschrock if (child->vdev_islog && vd == rvd) 27526976Seschrock degraded++; 27536976Seschrock else 27546976Seschrock faulted++; 27556976Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 27564451Seschrock degraded++; 27576976Seschrock } 27584451Seschrock 27594451Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 27604451Seschrock corrupted++; 27614451Seschrock } 27621775Sbillm 27634451Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 27644451Seschrock 27654451Seschrock /* 27667754SJeff.Bonwick@Sun.COM * Root special: if there is a top-level vdev that cannot be 27674451Seschrock * opened due to corrupted metadata, then propagate the root 27684451Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 27694451Seschrock * replicas'. 27704451Seschrock */ 27714451Seschrock if (corrupted && vd == rvd && 27724451Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 27734451Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 27744451Seschrock VDEV_AUX_CORRUPT_DATA); 27751775Sbillm } 27761775Sbillm 27776976Seschrock if (vd->vdev_parent) 27784451Seschrock vdev_propagate_state(vd->vdev_parent); 27791775Sbillm } 27801775Sbillm 2781789Sahrens /* 27821544Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 27831544Seschrock * state, because we're in the process of opening children depth-first. 27841544Seschrock * Otherwise, we propagate the change to the parent. 27851544Seschrock * 27861544Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 27871544Seschrock * generated. 2788789Sahrens */ 2789789Sahrens void 27901544Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 2791789Sahrens { 27921986Seschrock uint64_t save_state; 27936643Seschrock spa_t *spa = vd->vdev_spa; 27941544Seschrock 27951544Seschrock if (state == vd->vdev_state) { 27961544Seschrock vd->vdev_stat.vs_aux = aux; 2797789Sahrens return; 27981544Seschrock } 27991544Seschrock 28001986Seschrock save_state = vd->vdev_state; 2801789Sahrens 2802789Sahrens vd->vdev_state = state; 2803789Sahrens vd->vdev_stat.vs_aux = aux; 2804789Sahrens 28054451Seschrock /* 28064451Seschrock * If we are setting the vdev state to anything but an open state, then 28074451Seschrock * always close the underlying device. Otherwise, we keep accessible 28084451Seschrock * but invalid devices open forever. We don't call vdev_close() itself, 28094451Seschrock * because that implies some extra checks (offline, etc) that we don't 28104451Seschrock * want here. This is limited to leaf devices, because otherwise 28114451Seschrock * closing the device will affect other children. 28124451Seschrock */ 28137780SJeff.Bonwick@Sun.COM if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf) 28144451Seschrock vd->vdev_ops->vdev_op_close(vd); 28154451Seschrock 281610817SEric.Schrock@Sun.COM /* 281710817SEric.Schrock@Sun.COM * If we have brought this vdev back into service, we need 281810817SEric.Schrock@Sun.COM * to notify fmd so that it can gracefully repair any outstanding 281910817SEric.Schrock@Sun.COM * cases due to a missing device. We do this in all cases, even those 282010817SEric.Schrock@Sun.COM * that probably don't correlate to a repaired fault. This is sure to 282110817SEric.Schrock@Sun.COM * catch all cases, and we let the zfs-retire agent sort it out. If 282210817SEric.Schrock@Sun.COM * this is a transient state it's OK, as the retire agent will 282310817SEric.Schrock@Sun.COM * double-check the state of the vdev before repairing it. 282410817SEric.Schrock@Sun.COM */ 282510817SEric.Schrock@Sun.COM if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 282610817SEric.Schrock@Sun.COM vd->vdev_prevstate != state) 282710817SEric.Schrock@Sun.COM zfs_post_state_change(spa, vd); 282810817SEric.Schrock@Sun.COM 28294451Seschrock if (vd->vdev_removed && 28304451Seschrock state == VDEV_STATE_CANT_OPEN && 28314451Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 28324451Seschrock /* 28334451Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 28344451Seschrock * device was previously marked removed and someone attempted to 28354451Seschrock * reopen it. If this failed due to a nonexistent device, then 28364451Seschrock * keep the device in the REMOVED state. We also let this be if 28374451Seschrock * it is one of our special test online cases, which is only 28384451Seschrock * attempting to online the device and shouldn't generate an FMA 28394451Seschrock * fault. 28404451Seschrock */ 28414451Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 28424451Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 28434451Seschrock } else if (state == VDEV_STATE_REMOVED) { 28444451Seschrock vd->vdev_removed = B_TRUE; 28454451Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 28461544Seschrock /* 28471544Seschrock * If we fail to open a vdev during an import, we mark it as 28481544Seschrock * "not available", which signifies that it was never there to 28491544Seschrock * begin with. Failure to open such a device is not considered 28501544Seschrock * an error. 28511544Seschrock */ 28526643Seschrock if (spa->spa_load_state == SPA_LOAD_IMPORT && 28531986Seschrock vd->vdev_ops->vdev_op_leaf) 28541986Seschrock vd->vdev_not_present = 1; 28551986Seschrock 28561986Seschrock /* 28571986Seschrock * Post the appropriate ereport. If the 'prevstate' field is 28581986Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 28591986Seschrock * that this is part of a vdev_reopen(). In this case, we don't 28601986Seschrock * want to post the ereport if the device was already in the 28611986Seschrock * CANT_OPEN state beforehand. 28624451Seschrock * 28634451Seschrock * If the 'checkremove' flag is set, then this is an attempt to 28644451Seschrock * online the device in response to an insertion event. If we 28654451Seschrock * hit this case, then we have detected an insertion event for a 28664451Seschrock * faulted or offline device that wasn't in the removed state. 28674451Seschrock * In this scenario, we don't post an ereport because we are 28684451Seschrock * about to replace the device, or attempt an online with 28694451Seschrock * vdev_forcefault, which will generate the fault for us. 28701986Seschrock */ 28714451Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 28724451Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 28736643Seschrock vd != spa->spa_root_vdev) { 28741544Seschrock const char *class; 28751544Seschrock 28761544Seschrock switch (aux) { 28771544Seschrock case VDEV_AUX_OPEN_FAILED: 28781544Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 28791544Seschrock break; 28801544Seschrock case VDEV_AUX_CORRUPT_DATA: 28811544Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 28821544Seschrock break; 28831544Seschrock case VDEV_AUX_NO_REPLICAS: 28841544Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 28851544Seschrock break; 28861544Seschrock case VDEV_AUX_BAD_GUID_SUM: 28871544Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 28881544Seschrock break; 28891544Seschrock case VDEV_AUX_TOO_SMALL: 28901544Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 28911544Seschrock break; 28921544Seschrock case VDEV_AUX_BAD_LABEL: 28931544Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 28941544Seschrock break; 28957754SJeff.Bonwick@Sun.COM case VDEV_AUX_IO_FAILURE: 28967754SJeff.Bonwick@Sun.COM class = FM_EREPORT_ZFS_IO_FAILURE; 28977754SJeff.Bonwick@Sun.COM break; 28981544Seschrock default: 28991544Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 29001544Seschrock } 29011544Seschrock 29026643Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 29031544Seschrock } 29044451Seschrock 29054451Seschrock /* Erase any notion of persistent removed state */ 29064451Seschrock vd->vdev_removed = B_FALSE; 29074451Seschrock } else { 29084451Seschrock vd->vdev_removed = B_FALSE; 29091544Seschrock } 29101544Seschrock 29119583STim.Haley@Sun.COM if (!isopen && vd->vdev_parent) 29129583STim.Haley@Sun.COM vdev_propagate_state(vd->vdev_parent); 2913789Sahrens } 29147042Sgw25295 29157042Sgw25295 /* 29167042Sgw25295 * Check the vdev configuration to ensure that it's capable of supporting 29177042Sgw25295 * a root pool. Currently, we do not support RAID-Z or partial configuration. 29187042Sgw25295 * In addition, only a single top-level vdev is allowed and none of the leaves 29197042Sgw25295 * can be wholedisks. 29207042Sgw25295 */ 29217042Sgw25295 boolean_t 29227042Sgw25295 vdev_is_bootable(vdev_t *vd) 29237042Sgw25295 { 29247042Sgw25295 if (!vd->vdev_ops->vdev_op_leaf) { 29257042Sgw25295 char *vdev_type = vd->vdev_ops->vdev_op_type; 29267042Sgw25295 29277042Sgw25295 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 29287042Sgw25295 vd->vdev_children > 1) { 29297042Sgw25295 return (B_FALSE); 29307042Sgw25295 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 29317042Sgw25295 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 29327042Sgw25295 return (B_FALSE); 29337042Sgw25295 } 29347042Sgw25295 } else if (vd->vdev_wholedisk == 1) { 29357042Sgw25295 return (B_FALSE); 29367042Sgw25295 } 29377042Sgw25295 29389816SGeorge.Wilson@Sun.COM for (int c = 0; c < vd->vdev_children; c++) { 29397042Sgw25295 if (!vdev_is_bootable(vd->vdev_child[c])) 29407042Sgw25295 return (B_FALSE); 29417042Sgw25295 } 29427042Sgw25295 return (B_TRUE); 29437042Sgw25295 } 29449701SGeorge.Wilson@Sun.COM 294510594SGeorge.Wilson@Sun.COM /* 294610594SGeorge.Wilson@Sun.COM * Load the state from the original vdev tree (ovd) which 294710594SGeorge.Wilson@Sun.COM * we've retrieved from the MOS config object. If the original 294810594SGeorge.Wilson@Sun.COM * vdev was offline then we transfer that state to the device 294910594SGeorge.Wilson@Sun.COM * in the current vdev tree (nvd). 295010594SGeorge.Wilson@Sun.COM */ 29519701SGeorge.Wilson@Sun.COM void 295210594SGeorge.Wilson@Sun.COM vdev_load_log_state(vdev_t *nvd, vdev_t *ovd) 29539701SGeorge.Wilson@Sun.COM { 295410594SGeorge.Wilson@Sun.COM spa_t *spa = nvd->vdev_spa; 295510594SGeorge.Wilson@Sun.COM 295610594SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 295710594SGeorge.Wilson@Sun.COM ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid); 295810594SGeorge.Wilson@Sun.COM 295910594SGeorge.Wilson@Sun.COM for (int c = 0; c < nvd->vdev_children; c++) 296010594SGeorge.Wilson@Sun.COM vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]); 296110594SGeorge.Wilson@Sun.COM 296210594SGeorge.Wilson@Sun.COM if (nvd->vdev_ops->vdev_op_leaf && ovd->vdev_offline) { 29639701SGeorge.Wilson@Sun.COM /* 29649701SGeorge.Wilson@Sun.COM * It would be nice to call vdev_offline() 29659701SGeorge.Wilson@Sun.COM * directly but the pool isn't fully loaded and 29669701SGeorge.Wilson@Sun.COM * the txg threads have not been started yet. 29679701SGeorge.Wilson@Sun.COM */ 296810594SGeorge.Wilson@Sun.COM nvd->vdev_offline = ovd->vdev_offline; 296910594SGeorge.Wilson@Sun.COM vdev_reopen(nvd->vdev_top); 29709701SGeorge.Wilson@Sun.COM } 29719701SGeorge.Wilson@Sun.COM } 29729816SGeorge.Wilson@Sun.COM 29739816SGeorge.Wilson@Sun.COM /* 29749816SGeorge.Wilson@Sun.COM * Expand a vdev if possible. 29759816SGeorge.Wilson@Sun.COM */ 29769816SGeorge.Wilson@Sun.COM void 29779816SGeorge.Wilson@Sun.COM vdev_expand(vdev_t *vd, uint64_t txg) 29789816SGeorge.Wilson@Sun.COM { 29799816SGeorge.Wilson@Sun.COM ASSERT(vd->vdev_top == vd); 29809816SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 29819816SGeorge.Wilson@Sun.COM 29829816SGeorge.Wilson@Sun.COM if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 29839816SGeorge.Wilson@Sun.COM VERIFY(vdev_metaslab_init(vd, txg) == 0); 29849816SGeorge.Wilson@Sun.COM vdev_config_dirty(vd); 29859816SGeorge.Wilson@Sun.COM } 29869816SGeorge.Wilson@Sun.COM } 2987