1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 2398d1cbfeSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2481cd5c55SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 251a902ef8SHans Rosenfeld * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 26c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 27c8811bd3SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com> 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens #include <sys/zfs_context.h> 31ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 32fa9e4066Sahrens #include <sys/spa.h> 33fa9e4066Sahrens #include <sys/spa_impl.h> 34fa9e4066Sahrens #include <sys/dmu.h> 35fa9e4066Sahrens #include <sys/dmu_tx.h> 36fa9e4066Sahrens #include <sys/vdev_impl.h> 37fa9e4066Sahrens #include <sys/uberblock_impl.h> 38fa9e4066Sahrens #include <sys/metaslab.h> 39fa9e4066Sahrens #include <sys/metaslab_impl.h> 40fa9e4066Sahrens #include <sys/space_map.h> 410713e232SGeorge Wilson #include <sys/space_reftree.h> 42fa9e4066Sahrens #include <sys/zio.h> 43fa9e4066Sahrens #include <sys/zap.h> 44fa9e4066Sahrens #include <sys/fs/zfs.h> 45c5904d13Seschrock #include <sys/arc.h> 46e6ca193dSGeorge Wilson #include <sys/zil.h> 473f9d6ad7SLin Ling #include <sys/dsl_scan.h> 48fa9e4066Sahrens 49fa9e4066Sahrens /* 50fa9e4066Sahrens * Virtual device management. 51fa9e4066Sahrens */ 52fa9e4066Sahrens 53fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = { 54fa9e4066Sahrens &vdev_root_ops, 55fa9e4066Sahrens &vdev_raidz_ops, 56fa9e4066Sahrens &vdev_mirror_ops, 57fa9e4066Sahrens &vdev_replacing_ops, 5899653d4eSeschrock &vdev_spare_ops, 59fa9e4066Sahrens &vdev_disk_ops, 60fa9e4066Sahrens &vdev_file_ops, 61fa9e4066Sahrens &vdev_missing_ops, 6288ecc943SGeorge Wilson &vdev_hole_ops, 63fa9e4066Sahrens NULL 64fa9e4066Sahrens }; 65fa9e4066Sahrens 66088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */ 67088f3894Sahrens int zfs_scrub_limit = 10; 6805b2b3b8Smishra 69bf3e216cSMatthew Ahrens /* 70bf3e216cSMatthew Ahrens * When a vdev is added, it will be divided into approximately (but no 71bf3e216cSMatthew Ahrens * more than) this number of metaslabs. 72bf3e216cSMatthew Ahrens */ 73bf3e216cSMatthew Ahrens int metaslabs_per_vdev = 200; 74bf3e216cSMatthew Ahrens 75fa9e4066Sahrens /* 76fa9e4066Sahrens * Given a vdev type, return the appropriate ops vector. 77fa9e4066Sahrens */ 78fa9e4066Sahrens static vdev_ops_t * 79fa9e4066Sahrens vdev_getops(const char *type) 80fa9e4066Sahrens { 81fa9e4066Sahrens vdev_ops_t *ops, **opspp; 82fa9e4066Sahrens 83fa9e4066Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 84fa9e4066Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 85fa9e4066Sahrens break; 86fa9e4066Sahrens 87fa9e4066Sahrens return (ops); 88fa9e4066Sahrens } 89fa9e4066Sahrens 90fa9e4066Sahrens /* 91fa9e4066Sahrens * Default asize function: return the MAX of psize with the asize of 92fa9e4066Sahrens * all children. This is what's used by anything other than RAID-Z. 93fa9e4066Sahrens */ 94fa9e4066Sahrens uint64_t 95fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 96fa9e4066Sahrens { 97ecc2d604Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 98fa9e4066Sahrens uint64_t csize; 99fa9e4066Sahrens 100573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 101fa9e4066Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 102fa9e4066Sahrens asize = MAX(asize, csize); 103fa9e4066Sahrens } 104fa9e4066Sahrens 105fa9e4066Sahrens return (asize); 106fa9e4066Sahrens } 107fa9e4066Sahrens 1082a79c5feSlling /* 109573ca77eSGeorge Wilson * Get the minimum allocatable size. We define the allocatable size as 110573ca77eSGeorge Wilson * the vdev's asize rounded to the nearest metaslab. This allows us to 111573ca77eSGeorge Wilson * replace or attach devices which don't have the same physical size but 112573ca77eSGeorge Wilson * can still satisfy the same number of allocations. 1132a79c5feSlling */ 1142a79c5feSlling uint64_t 115573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd) 1162a79c5feSlling { 117573ca77eSGeorge Wilson vdev_t *pvd = vd->vdev_parent; 1182a79c5feSlling 119573ca77eSGeorge Wilson /* 1204263d13fSGeorge Wilson * If our parent is NULL (inactive spare or cache) or is the root, 121573ca77eSGeorge Wilson * just return our own asize. 122573ca77eSGeorge Wilson */ 123573ca77eSGeorge Wilson if (pvd == NULL) 124573ca77eSGeorge Wilson return (vd->vdev_asize); 1252a79c5feSlling 1262a79c5feSlling /* 127573ca77eSGeorge Wilson * The top-level vdev just returns the allocatable size rounded 128573ca77eSGeorge Wilson * to the nearest metaslab. 1292a79c5feSlling */ 130573ca77eSGeorge Wilson if (vd == vd->vdev_top) 131573ca77eSGeorge Wilson return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 1322a79c5feSlling 133573ca77eSGeorge Wilson /* 134573ca77eSGeorge Wilson * The allocatable space for a raidz vdev is N * sizeof(smallest child), 135573ca77eSGeorge Wilson * so each child must provide at least 1/Nth of its asize. 136573ca77eSGeorge Wilson */ 137573ca77eSGeorge Wilson if (pvd->vdev_ops == &vdev_raidz_ops) 138*c040c10cSSteven Hartland return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 139*c040c10cSSteven Hartland pvd->vdev_children); 1402a79c5feSlling 141573ca77eSGeorge Wilson return (pvd->vdev_min_asize); 142573ca77eSGeorge Wilson } 1432a79c5feSlling 144573ca77eSGeorge Wilson void 145573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd) 146573ca77eSGeorge Wilson { 147573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 148573ca77eSGeorge Wilson 149573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 150573ca77eSGeorge Wilson vdev_set_min_asize(vd->vdev_child[c]); 1512a79c5feSlling } 1522a79c5feSlling 153fa9e4066Sahrens vdev_t * 154fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 155fa9e4066Sahrens { 156fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 157fa9e4066Sahrens 158e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 159e05725b1Sbonwick 160088f3894Sahrens if (vdev < rvd->vdev_children) { 161088f3894Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 162fa9e4066Sahrens return (rvd->vdev_child[vdev]); 163088f3894Sahrens } 164fa9e4066Sahrens 165fa9e4066Sahrens return (NULL); 166fa9e4066Sahrens } 167fa9e4066Sahrens 168fa9e4066Sahrens vdev_t * 169fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 170fa9e4066Sahrens { 171fa9e4066Sahrens vdev_t *mvd; 172fa9e4066Sahrens 1730e34b6a7Sbonwick if (vd->vdev_guid == guid) 174fa9e4066Sahrens return (vd); 175fa9e4066Sahrens 176573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 177fa9e4066Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 178fa9e4066Sahrens NULL) 179fa9e4066Sahrens return (mvd); 180fa9e4066Sahrens 181fa9e4066Sahrens return (NULL); 182fa9e4066Sahrens } 183fa9e4066Sahrens 18412380e1eSArne Jansen static int 18512380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd) 18612380e1eSArne Jansen { 18712380e1eSArne Jansen int n = 0; 18812380e1eSArne Jansen 18912380e1eSArne Jansen if (vd->vdev_ops->vdev_op_leaf) 19012380e1eSArne Jansen return (1); 19112380e1eSArne Jansen 19212380e1eSArne Jansen for (int c = 0; c < vd->vdev_children; c++) 19312380e1eSArne Jansen n += vdev_count_leaves_impl(vd->vdev_child[c]); 19412380e1eSArne Jansen 19512380e1eSArne Jansen return (n); 19612380e1eSArne Jansen } 19712380e1eSArne Jansen 19812380e1eSArne Jansen int 19912380e1eSArne Jansen vdev_count_leaves(spa_t *spa) 20012380e1eSArne Jansen { 20112380e1eSArne Jansen return (vdev_count_leaves_impl(spa->spa_root_vdev)); 20212380e1eSArne Jansen } 20312380e1eSArne Jansen 204fa9e4066Sahrens void 205fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 206fa9e4066Sahrens { 207fa9e4066Sahrens size_t oldsize, newsize; 208fa9e4066Sahrens uint64_t id = cvd->vdev_id; 209fa9e4066Sahrens vdev_t **newchild; 21081cd5c55SMatthew Ahrens spa_t *spa = cvd->vdev_spa; 211fa9e4066Sahrens 21281cd5c55SMatthew Ahrens ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 213fa9e4066Sahrens ASSERT(cvd->vdev_parent == NULL); 214fa9e4066Sahrens 215fa9e4066Sahrens cvd->vdev_parent = pvd; 216fa9e4066Sahrens 217fa9e4066Sahrens if (pvd == NULL) 218fa9e4066Sahrens return; 219fa9e4066Sahrens 220fa9e4066Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 221fa9e4066Sahrens 222fa9e4066Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 223fa9e4066Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 224fa9e4066Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 225fa9e4066Sahrens 226fa9e4066Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 227fa9e4066Sahrens if (pvd->vdev_child != NULL) { 228fa9e4066Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 229fa9e4066Sahrens kmem_free(pvd->vdev_child, oldsize); 230fa9e4066Sahrens } 231fa9e4066Sahrens 232fa9e4066Sahrens pvd->vdev_child = newchild; 233fa9e4066Sahrens pvd->vdev_child[id] = cvd; 234fa9e4066Sahrens 235fa9e4066Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 236fa9e4066Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 237fa9e4066Sahrens 238fa9e4066Sahrens /* 239fa9e4066Sahrens * Walk up all ancestors to update guid sum. 240fa9e4066Sahrens */ 241fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 242fa9e4066Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 243fa9e4066Sahrens } 244fa9e4066Sahrens 245fa9e4066Sahrens void 246fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 247fa9e4066Sahrens { 248fa9e4066Sahrens int c; 249fa9e4066Sahrens uint_t id = cvd->vdev_id; 250fa9e4066Sahrens 251fa9e4066Sahrens ASSERT(cvd->vdev_parent == pvd); 252fa9e4066Sahrens 253fa9e4066Sahrens if (pvd == NULL) 254fa9e4066Sahrens return; 255fa9e4066Sahrens 256fa9e4066Sahrens ASSERT(id < pvd->vdev_children); 257fa9e4066Sahrens ASSERT(pvd->vdev_child[id] == cvd); 258fa9e4066Sahrens 259fa9e4066Sahrens pvd->vdev_child[id] = NULL; 260fa9e4066Sahrens cvd->vdev_parent = NULL; 261fa9e4066Sahrens 262fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) 263fa9e4066Sahrens if (pvd->vdev_child[c]) 264fa9e4066Sahrens break; 265fa9e4066Sahrens 266fa9e4066Sahrens if (c == pvd->vdev_children) { 267fa9e4066Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 268fa9e4066Sahrens pvd->vdev_child = NULL; 269fa9e4066Sahrens pvd->vdev_children = 0; 270fa9e4066Sahrens } 271fa9e4066Sahrens 272fa9e4066Sahrens /* 273fa9e4066Sahrens * Walk up all ancestors to update guid sum. 274fa9e4066Sahrens */ 275fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 276fa9e4066Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 277fa9e4066Sahrens } 278fa9e4066Sahrens 279fa9e4066Sahrens /* 280fa9e4066Sahrens * Remove any holes in the child array. 281fa9e4066Sahrens */ 282fa9e4066Sahrens void 283fa9e4066Sahrens vdev_compact_children(vdev_t *pvd) 284fa9e4066Sahrens { 285fa9e4066Sahrens vdev_t **newchild, *cvd; 286fa9e4066Sahrens int oldc = pvd->vdev_children; 287573ca77eSGeorge Wilson int newc; 288fa9e4066Sahrens 289e14bb325SJeff Bonwick ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 290fa9e4066Sahrens 291573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) 292fa9e4066Sahrens if (pvd->vdev_child[c]) 293fa9e4066Sahrens newc++; 294fa9e4066Sahrens 295fa9e4066Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 296fa9e4066Sahrens 297573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) { 298fa9e4066Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 299fa9e4066Sahrens newchild[newc] = cvd; 300fa9e4066Sahrens cvd->vdev_id = newc++; 301fa9e4066Sahrens } 302fa9e4066Sahrens } 303fa9e4066Sahrens 304fa9e4066Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 305fa9e4066Sahrens pvd->vdev_child = newchild; 306fa9e4066Sahrens pvd->vdev_children = newc; 307fa9e4066Sahrens } 308fa9e4066Sahrens 309fa9e4066Sahrens /* 310fa9e4066Sahrens * Allocate and minimally initialize a vdev_t. 311fa9e4066Sahrens */ 31288ecc943SGeorge Wilson vdev_t * 313fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 314fa9e4066Sahrens { 315fa9e4066Sahrens vdev_t *vd; 316fa9e4066Sahrens 317fa9e4066Sahrens vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 318fa9e4066Sahrens 3190e34b6a7Sbonwick if (spa->spa_root_vdev == NULL) { 3200e34b6a7Sbonwick ASSERT(ops == &vdev_root_ops); 3210e34b6a7Sbonwick spa->spa_root_vdev = vd; 322e9103aaeSGarrett D'Amore spa->spa_load_guid = spa_generate_guid(NULL); 3230e34b6a7Sbonwick } 3240e34b6a7Sbonwick 32588ecc943SGeorge Wilson if (guid == 0 && ops != &vdev_hole_ops) { 3260e34b6a7Sbonwick if (spa->spa_root_vdev == vd) { 3270e34b6a7Sbonwick /* 3280e34b6a7Sbonwick * The root vdev's guid will also be the pool guid, 3290e34b6a7Sbonwick * which must be unique among all pools. 3300e34b6a7Sbonwick */ 3311195e687SMark J Musante guid = spa_generate_guid(NULL); 3320e34b6a7Sbonwick } else { 3330e34b6a7Sbonwick /* 3340e34b6a7Sbonwick * Any other vdev's guid must be unique within the pool. 3350e34b6a7Sbonwick */ 3361195e687SMark J Musante guid = spa_generate_guid(spa); 3370e34b6a7Sbonwick } 3380e34b6a7Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 3390e34b6a7Sbonwick } 3400e34b6a7Sbonwick 341fa9e4066Sahrens vd->vdev_spa = spa; 342fa9e4066Sahrens vd->vdev_id = id; 343fa9e4066Sahrens vd->vdev_guid = guid; 344fa9e4066Sahrens vd->vdev_guid_sum = guid; 345fa9e4066Sahrens vd->vdev_ops = ops; 346fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 34788ecc943SGeorge Wilson vd->vdev_ishole = (ops == &vdev_hole_ops); 348fa9e4066Sahrens 349fa9e4066Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 3505ad82045Snd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 351e14bb325SJeff Bonwick mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 3520f7643c7SGeorge Wilson mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 3538ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 3540713e232SGeorge Wilson vd->vdev_dtl[t] = range_tree_create(NULL, NULL, 3558ad4d6ddSJeff Bonwick &vd->vdev_dtl_lock); 3568ad4d6ddSJeff Bonwick } 357fa9e4066Sahrens txg_list_create(&vd->vdev_ms_list, 358fa9e4066Sahrens offsetof(struct metaslab, ms_txg_node)); 359fa9e4066Sahrens txg_list_create(&vd->vdev_dtl_list, 360fa9e4066Sahrens offsetof(struct vdev, vdev_dtl_node)); 361fa9e4066Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 3623d7072f8Seschrock vdev_queue_init(vd); 3633d7072f8Seschrock vdev_cache_init(vd); 364fa9e4066Sahrens 365fa9e4066Sahrens return (vd); 366fa9e4066Sahrens } 367fa9e4066Sahrens 368fa9e4066Sahrens /* 369fa9e4066Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 370fa9e4066Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 371fa9e4066Sahrens * different for each case. 372fa9e4066Sahrens */ 37399653d4eSeschrock int 37499653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 37599653d4eSeschrock int alloctype) 376fa9e4066Sahrens { 377fa9e4066Sahrens vdev_ops_t *ops; 378fa9e4066Sahrens char *type; 3798654d025Sperrin uint64_t guid = 0, islog, nparity; 380fa9e4066Sahrens vdev_t *vd; 381fa9e4066Sahrens 382e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 383fa9e4066Sahrens 384fa9e4066Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 385be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 386fa9e4066Sahrens 387fa9e4066Sahrens if ((ops = vdev_getops(type)) == NULL) 388be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 389fa9e4066Sahrens 390fa9e4066Sahrens /* 391fa9e4066Sahrens * If this is a load, get the vdev guid from the nvlist. 392fa9e4066Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 393fa9e4066Sahrens */ 394fa9e4066Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 395fa9e4066Sahrens uint64_t label_id; 396fa9e4066Sahrens 397fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 398fa9e4066Sahrens label_id != id) 399be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 400fa9e4066Sahrens 401fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 402be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 40399653d4eSeschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 40499653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 405be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 406fa94a07fSbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 407fa94a07fSbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 408be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 40921ecdf64SLin Ling } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 41021ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 411be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 412fa9e4066Sahrens } 413fa9e4066Sahrens 41499653d4eSeschrock /* 41599653d4eSeschrock * The first allocated vdev must be of type 'root'. 41699653d4eSeschrock */ 41799653d4eSeschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 418be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 41999653d4eSeschrock 4208654d025Sperrin /* 4218654d025Sperrin * Determine whether we're a log vdev. 4228654d025Sperrin */ 4238654d025Sperrin islog = 0; 4248654d025Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 425990b4856Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 426be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 427fa9e4066Sahrens 42888ecc943SGeorge Wilson if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 429be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 43088ecc943SGeorge Wilson 43199653d4eSeschrock /* 4328654d025Sperrin * Set the nparity property for RAID-Z vdevs. 43399653d4eSeschrock */ 4348654d025Sperrin nparity = -1ULL; 43599653d4eSeschrock if (ops == &vdev_raidz_ops) { 43699653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 4378654d025Sperrin &nparity) == 0) { 438b24ab676SJeff Bonwick if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 439be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 44099653d4eSeschrock /* 441f94275ceSAdam Leventhal * Previous versions could only support 1 or 2 parity 442f94275ceSAdam Leventhal * device. 44399653d4eSeschrock */ 444f94275ceSAdam Leventhal if (nparity > 1 && 445f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ2) 446be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 447f94275ceSAdam Leventhal if (nparity > 2 && 448f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ3) 449be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 45099653d4eSeschrock } else { 45199653d4eSeschrock /* 45299653d4eSeschrock * We require the parity to be specified for SPAs that 45399653d4eSeschrock * support multiple parity levels. 45499653d4eSeschrock */ 455f94275ceSAdam Leventhal if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 456be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 45799653d4eSeschrock /* 45899653d4eSeschrock * Otherwise, we default to 1 parity device for RAID-Z. 45999653d4eSeschrock */ 4608654d025Sperrin nparity = 1; 46199653d4eSeschrock } 46299653d4eSeschrock } else { 4638654d025Sperrin nparity = 0; 46499653d4eSeschrock } 4658654d025Sperrin ASSERT(nparity != -1ULL); 4668654d025Sperrin 4678654d025Sperrin vd = vdev_alloc_common(spa, id, guid, ops); 4688654d025Sperrin 4698654d025Sperrin vd->vdev_islog = islog; 4708654d025Sperrin vd->vdev_nparity = nparity; 4718654d025Sperrin 4728654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 4738654d025Sperrin vd->vdev_path = spa_strdup(vd->vdev_path); 4748654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 4758654d025Sperrin vd->vdev_devid = spa_strdup(vd->vdev_devid); 4768654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 4778654d025Sperrin &vd->vdev_physpath) == 0) 4788654d025Sperrin vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 4796809eb4eSEric Schrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 4806809eb4eSEric Schrock vd->vdev_fru = spa_strdup(vd->vdev_fru); 48199653d4eSeschrock 482afefbcddSeschrock /* 483afefbcddSeschrock * Set the whole_disk property. If it's not specified, leave the value 484afefbcddSeschrock * as -1. 485afefbcddSeschrock */ 486afefbcddSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 487afefbcddSeschrock &vd->vdev_wholedisk) != 0) 488afefbcddSeschrock vd->vdev_wholedisk = -1ULL; 489afefbcddSeschrock 490ea8dc4b6Seschrock /* 491ea8dc4b6Seschrock * Look for the 'not present' flag. This will only be set if the device 492ea8dc4b6Seschrock * was not present at the time of import. 493ea8dc4b6Seschrock */ 4946809eb4eSEric Schrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 4956809eb4eSEric Schrock &vd->vdev_not_present); 496ea8dc4b6Seschrock 497ecc2d604Sbonwick /* 498ecc2d604Sbonwick * Get the alignment requirement. 499ecc2d604Sbonwick */ 500ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 501ecc2d604Sbonwick 50288ecc943SGeorge Wilson /* 50388ecc943SGeorge Wilson * Retrieve the vdev creation time. 50488ecc943SGeorge Wilson */ 50588ecc943SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 50688ecc943SGeorge Wilson &vd->vdev_crtxg); 50788ecc943SGeorge Wilson 508fa9e4066Sahrens /* 509fa9e4066Sahrens * If we're a top-level vdev, try to load the allocation parameters. 510fa9e4066Sahrens */ 5111195e687SMark J Musante if (parent && !parent->vdev_parent && 5121195e687SMark J Musante (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 513fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 514fa9e4066Sahrens &vd->vdev_ms_array); 515fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 516fa9e4066Sahrens &vd->vdev_ms_shift); 517fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 518fa9e4066Sahrens &vd->vdev_asize); 5193f9d6ad7SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 5203f9d6ad7SLin Ling &vd->vdev_removing); 521215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 522215198a6SJoe Stein &vd->vdev_top_zap); 523215198a6SJoe Stein } else { 524215198a6SJoe Stein ASSERT0(vd->vdev_top_zap); 525fa9e4066Sahrens } 526fa9e4066Sahrens 527cd0837ccSGeorge Wilson if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 528a1521560SJeff Bonwick ASSERT(alloctype == VDEV_ALLOC_LOAD || 5299f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ADD || 5301195e687SMark J Musante alloctype == VDEV_ALLOC_SPLIT || 5319f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ROOTPOOL); 532a1521560SJeff Bonwick vd->vdev_mg = metaslab_group_create(islog ? 533a1521560SJeff Bonwick spa_log_class(spa) : spa_normal_class(spa), vd); 534a1521560SJeff Bonwick } 535a1521560SJeff Bonwick 536215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && 537215198a6SJoe Stein (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 538215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, 539215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 540215198a6SJoe Stein } else { 541215198a6SJoe Stein ASSERT0(vd->vdev_leaf_zap); 542215198a6SJoe Stein } 543215198a6SJoe Stein 544fa9e4066Sahrens /* 5453d7072f8Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 546fa9e4066Sahrens */ 547215198a6SJoe Stein 548c5904d13Seschrock if (vd->vdev_ops->vdev_op_leaf && 54921ecdf64SLin Ling (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 55021ecdf64SLin Ling alloctype == VDEV_ALLOC_ROOTPOOL)) { 551c5904d13Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 552c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 5530713e232SGeorge Wilson &vd->vdev_dtl_object); 554c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 555c5904d13Seschrock &vd->vdev_unspare); 556c5904d13Seschrock } 55721ecdf64SLin Ling 55821ecdf64SLin Ling if (alloctype == VDEV_ALLOC_ROOTPOOL) { 55921ecdf64SLin Ling uint64_t spare = 0; 56021ecdf64SLin Ling 56121ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 56221ecdf64SLin Ling &spare) == 0 && spare) 56321ecdf64SLin Ling spa_spare_add(vd); 56421ecdf64SLin Ling } 56521ecdf64SLin Ling 566ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 567ecc2d604Sbonwick &vd->vdev_offline); 568c5904d13Seschrock 569b4952e17SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 570b4952e17SGeorge Wilson &vd->vdev_resilver_txg); 571cb04b873SMark J Musante 5723d7072f8Seschrock /* 5733d7072f8Seschrock * When importing a pool, we want to ignore the persistent fault 5743d7072f8Seschrock * state, as the diagnosis made on another system may not be 575069f55e2SEric Schrock * valid in the current context. Local vdevs will 576069f55e2SEric Schrock * remain in the faulted state. 5773d7072f8Seschrock */ 578b16da2e2SGeorge Wilson if (spa_load_state(spa) == SPA_LOAD_OPEN) { 5793d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 5803d7072f8Seschrock &vd->vdev_faulted); 5813d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 5823d7072f8Seschrock &vd->vdev_degraded); 5833d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 5843d7072f8Seschrock &vd->vdev_removed); 585069f55e2SEric Schrock 586069f55e2SEric Schrock if (vd->vdev_faulted || vd->vdev_degraded) { 587069f55e2SEric Schrock char *aux; 588069f55e2SEric Schrock 589069f55e2SEric Schrock vd->vdev_label_aux = 590069f55e2SEric Schrock VDEV_AUX_ERR_EXCEEDED; 591069f55e2SEric Schrock if (nvlist_lookup_string(nv, 592069f55e2SEric Schrock ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 593069f55e2SEric Schrock strcmp(aux, "external") == 0) 594069f55e2SEric Schrock vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 595069f55e2SEric Schrock } 5963d7072f8Seschrock } 597fa9e4066Sahrens } 598fa9e4066Sahrens 599fa9e4066Sahrens /* 600fa9e4066Sahrens * Add ourselves to the parent's list of children. 601fa9e4066Sahrens */ 602fa9e4066Sahrens vdev_add_child(parent, vd); 603fa9e4066Sahrens 60499653d4eSeschrock *vdp = vd; 60599653d4eSeschrock 60699653d4eSeschrock return (0); 607fa9e4066Sahrens } 608fa9e4066Sahrens 609fa9e4066Sahrens void 610fa9e4066Sahrens vdev_free(vdev_t *vd) 611fa9e4066Sahrens { 6123d7072f8Seschrock spa_t *spa = vd->vdev_spa; 613fa9e4066Sahrens 614fa9e4066Sahrens /* 615fa9e4066Sahrens * vdev_free() implies closing the vdev first. This is simpler than 616fa9e4066Sahrens * trying to ensure complicated semantics for all callers. 617fa9e4066Sahrens */ 618fa9e4066Sahrens vdev_close(vd); 619fa9e4066Sahrens 620e14bb325SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 621b24ab676SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 622fa9e4066Sahrens 623fa9e4066Sahrens /* 624fa9e4066Sahrens * Free all children. 625fa9e4066Sahrens */ 626573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 627fa9e4066Sahrens vdev_free(vd->vdev_child[c]); 628fa9e4066Sahrens 629fa9e4066Sahrens ASSERT(vd->vdev_child == NULL); 630fa9e4066Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 631fa9e4066Sahrens 632fa9e4066Sahrens /* 633fa9e4066Sahrens * Discard allocation state. 634fa9e4066Sahrens */ 635a1521560SJeff Bonwick if (vd->vdev_mg != NULL) { 636fa9e4066Sahrens vdev_metaslab_fini(vd); 637a1521560SJeff Bonwick metaslab_group_destroy(vd->vdev_mg); 638a1521560SJeff Bonwick } 639fa9e4066Sahrens 640fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_space); 641fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_dspace); 642fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 643fa9e4066Sahrens 644fa9e4066Sahrens /* 645fa9e4066Sahrens * Remove this vdev from its parent's child list. 646fa9e4066Sahrens */ 647fa9e4066Sahrens vdev_remove_child(vd->vdev_parent, vd); 648fa9e4066Sahrens 649fa9e4066Sahrens ASSERT(vd->vdev_parent == NULL); 650fa9e4066Sahrens 6513d7072f8Seschrock /* 6523d7072f8Seschrock * Clean up vdev structure. 6533d7072f8Seschrock */ 6543d7072f8Seschrock vdev_queue_fini(vd); 6553d7072f8Seschrock vdev_cache_fini(vd); 6563d7072f8Seschrock 6573d7072f8Seschrock if (vd->vdev_path) 6583d7072f8Seschrock spa_strfree(vd->vdev_path); 6593d7072f8Seschrock if (vd->vdev_devid) 6603d7072f8Seschrock spa_strfree(vd->vdev_devid); 6613d7072f8Seschrock if (vd->vdev_physpath) 6623d7072f8Seschrock spa_strfree(vd->vdev_physpath); 6636809eb4eSEric Schrock if (vd->vdev_fru) 6646809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 6653d7072f8Seschrock 6663d7072f8Seschrock if (vd->vdev_isspare) 6673d7072f8Seschrock spa_spare_remove(vd); 668fa94a07fSbrendan if (vd->vdev_isl2cache) 669fa94a07fSbrendan spa_l2cache_remove(vd); 6703d7072f8Seschrock 6713d7072f8Seschrock txg_list_destroy(&vd->vdev_ms_list); 6723d7072f8Seschrock txg_list_destroy(&vd->vdev_dtl_list); 6738ad4d6ddSJeff Bonwick 6743d7072f8Seschrock mutex_enter(&vd->vdev_dtl_lock); 6750713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 6768ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 6770713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 6780713e232SGeorge Wilson range_tree_destroy(vd->vdev_dtl[t]); 6798ad4d6ddSJeff Bonwick } 6803d7072f8Seschrock mutex_exit(&vd->vdev_dtl_lock); 6818ad4d6ddSJeff Bonwick 6820f7643c7SGeorge Wilson mutex_destroy(&vd->vdev_queue_lock); 6833d7072f8Seschrock mutex_destroy(&vd->vdev_dtl_lock); 6843d7072f8Seschrock mutex_destroy(&vd->vdev_stat_lock); 685e14bb325SJeff Bonwick mutex_destroy(&vd->vdev_probe_lock); 6863d7072f8Seschrock 6873d7072f8Seschrock if (vd == spa->spa_root_vdev) 6883d7072f8Seschrock spa->spa_root_vdev = NULL; 6893d7072f8Seschrock 6903d7072f8Seschrock kmem_free(vd, sizeof (vdev_t)); 691fa9e4066Sahrens } 692fa9e4066Sahrens 693fa9e4066Sahrens /* 694fa9e4066Sahrens * Transfer top-level vdev state from svd to tvd. 695fa9e4066Sahrens */ 696fa9e4066Sahrens static void 697fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 698fa9e4066Sahrens { 699fa9e4066Sahrens spa_t *spa = svd->vdev_spa; 700fa9e4066Sahrens metaslab_t *msp; 701fa9e4066Sahrens vdev_t *vd; 702fa9e4066Sahrens int t; 703fa9e4066Sahrens 704fa9e4066Sahrens ASSERT(tvd == tvd->vdev_top); 705fa9e4066Sahrens 706fa9e4066Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 707fa9e4066Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 708fa9e4066Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 709215198a6SJoe Stein tvd->vdev_top_zap = svd->vdev_top_zap; 710fa9e4066Sahrens 711fa9e4066Sahrens svd->vdev_ms_array = 0; 712fa9e4066Sahrens svd->vdev_ms_shift = 0; 713fa9e4066Sahrens svd->vdev_ms_count = 0; 714215198a6SJoe Stein svd->vdev_top_zap = 0; 715fa9e4066Sahrens 716cd0837ccSGeorge Wilson if (tvd->vdev_mg) 717cd0837ccSGeorge Wilson ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 718fa9e4066Sahrens tvd->vdev_mg = svd->vdev_mg; 719fa9e4066Sahrens tvd->vdev_ms = svd->vdev_ms; 720fa9e4066Sahrens 721fa9e4066Sahrens svd->vdev_mg = NULL; 722fa9e4066Sahrens svd->vdev_ms = NULL; 723ecc2d604Sbonwick 724ecc2d604Sbonwick if (tvd->vdev_mg != NULL) 725ecc2d604Sbonwick tvd->vdev_mg->mg_vd = tvd; 726fa9e4066Sahrens 727fa9e4066Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 728fa9e4066Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 72999653d4eSeschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 730fa9e4066Sahrens 731fa9e4066Sahrens svd->vdev_stat.vs_alloc = 0; 732fa9e4066Sahrens svd->vdev_stat.vs_space = 0; 73399653d4eSeschrock svd->vdev_stat.vs_dspace = 0; 734fa9e4066Sahrens 735fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 736fa9e4066Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 737fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 738fa9e4066Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 739fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 740fa9e4066Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 741fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 742fa9e4066Sahrens } 743fa9e4066Sahrens 744e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_config_dirty_node)) { 745fa9e4066Sahrens vdev_config_clean(svd); 746fa9e4066Sahrens vdev_config_dirty(tvd); 747fa9e4066Sahrens } 748fa9e4066Sahrens 749e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_state_dirty_node)) { 750e14bb325SJeff Bonwick vdev_state_clean(svd); 751e14bb325SJeff Bonwick vdev_state_dirty(tvd); 752e14bb325SJeff Bonwick } 753e14bb325SJeff Bonwick 75499653d4eSeschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 75599653d4eSeschrock svd->vdev_deflate_ratio = 0; 7568654d025Sperrin 7578654d025Sperrin tvd->vdev_islog = svd->vdev_islog; 7588654d025Sperrin svd->vdev_islog = 0; 759fa9e4066Sahrens } 760fa9e4066Sahrens 761fa9e4066Sahrens static void 762fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 763fa9e4066Sahrens { 764fa9e4066Sahrens if (vd == NULL) 765fa9e4066Sahrens return; 766fa9e4066Sahrens 767fa9e4066Sahrens vd->vdev_top = tvd; 768fa9e4066Sahrens 769573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 770fa9e4066Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 771fa9e4066Sahrens } 772fa9e4066Sahrens 773fa9e4066Sahrens /* 774fa9e4066Sahrens * Add a mirror/replacing vdev above an existing vdev. 775fa9e4066Sahrens */ 776fa9e4066Sahrens vdev_t * 777fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 778fa9e4066Sahrens { 779fa9e4066Sahrens spa_t *spa = cvd->vdev_spa; 780fa9e4066Sahrens vdev_t *pvd = cvd->vdev_parent; 781fa9e4066Sahrens vdev_t *mvd; 782fa9e4066Sahrens 783e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 784fa9e4066Sahrens 785fa9e4066Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 786ecc2d604Sbonwick 787ecc2d604Sbonwick mvd->vdev_asize = cvd->vdev_asize; 788573ca77eSGeorge Wilson mvd->vdev_min_asize = cvd->vdev_min_asize; 7894263d13fSGeorge Wilson mvd->vdev_max_asize = cvd->vdev_max_asize; 790ecc2d604Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 791ecc2d604Sbonwick mvd->vdev_state = cvd->vdev_state; 79288ecc943SGeorge Wilson mvd->vdev_crtxg = cvd->vdev_crtxg; 793ecc2d604Sbonwick 794fa9e4066Sahrens vdev_remove_child(pvd, cvd); 795fa9e4066Sahrens vdev_add_child(pvd, mvd); 796fa9e4066Sahrens cvd->vdev_id = mvd->vdev_children; 797fa9e4066Sahrens vdev_add_child(mvd, cvd); 798fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 799fa9e4066Sahrens 800fa9e4066Sahrens if (mvd == mvd->vdev_top) 801fa9e4066Sahrens vdev_top_transfer(cvd, mvd); 802fa9e4066Sahrens 803fa9e4066Sahrens return (mvd); 804fa9e4066Sahrens } 805fa9e4066Sahrens 806fa9e4066Sahrens /* 807fa9e4066Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 808fa9e4066Sahrens */ 809fa9e4066Sahrens void 810fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd) 811fa9e4066Sahrens { 812fa9e4066Sahrens vdev_t *mvd = cvd->vdev_parent; 813fa9e4066Sahrens vdev_t *pvd = mvd->vdev_parent; 814fa9e4066Sahrens 815e14bb325SJeff Bonwick ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 816fa9e4066Sahrens 817fa9e4066Sahrens ASSERT(mvd->vdev_children == 1); 818fa9e4066Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 81999653d4eSeschrock mvd->vdev_ops == &vdev_replacing_ops || 82099653d4eSeschrock mvd->vdev_ops == &vdev_spare_ops); 821ecc2d604Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 822fa9e4066Sahrens 823fa9e4066Sahrens vdev_remove_child(mvd, cvd); 824fa9e4066Sahrens vdev_remove_child(pvd, mvd); 8258ad4d6ddSJeff Bonwick 82699653d4eSeschrock /* 827e14bb325SJeff Bonwick * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 828e14bb325SJeff Bonwick * Otherwise, we could have detached an offline device, and when we 829e14bb325SJeff Bonwick * go to import the pool we'll think we have two top-level vdevs, 830e14bb325SJeff Bonwick * instead of a different version of the same top-level vdev. 83199653d4eSeschrock */ 8328ad4d6ddSJeff Bonwick if (mvd->vdev_top == mvd) { 8338ad4d6ddSJeff Bonwick uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 8341195e687SMark J Musante cvd->vdev_orig_guid = cvd->vdev_guid; 8358ad4d6ddSJeff Bonwick cvd->vdev_guid += guid_delta; 8368ad4d6ddSJeff Bonwick cvd->vdev_guid_sum += guid_delta; 8378ad4d6ddSJeff Bonwick } 838e14bb325SJeff Bonwick cvd->vdev_id = mvd->vdev_id; 839e14bb325SJeff Bonwick vdev_add_child(pvd, cvd); 840fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 841fa9e4066Sahrens 842fa9e4066Sahrens if (cvd == cvd->vdev_top) 843fa9e4066Sahrens vdev_top_transfer(mvd, cvd); 844fa9e4066Sahrens 845fa9e4066Sahrens ASSERT(mvd->vdev_children == 0); 846fa9e4066Sahrens vdev_free(mvd); 847fa9e4066Sahrens } 848fa9e4066Sahrens 849ea8dc4b6Seschrock int 850fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 851fa9e4066Sahrens { 852fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 853ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 854ecc2d604Sbonwick uint64_t m; 855fa9e4066Sahrens uint64_t oldc = vd->vdev_ms_count; 856fa9e4066Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 857ecc2d604Sbonwick metaslab_t **mspp; 858ecc2d604Sbonwick int error; 859fa9e4066Sahrens 860a1521560SJeff Bonwick ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 861a1521560SJeff Bonwick 86288ecc943SGeorge Wilson /* 86388ecc943SGeorge Wilson * This vdev is not being allocated from yet or is a hole. 86488ecc943SGeorge Wilson */ 86588ecc943SGeorge Wilson if (vd->vdev_ms_shift == 0) 8660e34b6a7Sbonwick return (0); 8670e34b6a7Sbonwick 86888ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 86988ecc943SGeorge Wilson 870e6ca193dSGeorge Wilson /* 871e6ca193dSGeorge Wilson * Compute the raidz-deflation ratio. Note, we hard-code 872b5152584SMatthew Ahrens * in 128k (1 << 17) because it is the "typical" blocksize. 873b5152584SMatthew Ahrens * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 874b5152584SMatthew Ahrens * otherwise it would inconsistently account for existing bp's. 875e6ca193dSGeorge Wilson */ 876e6ca193dSGeorge Wilson vd->vdev_deflate_ratio = (1 << 17) / 877e6ca193dSGeorge Wilson (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 878e6ca193dSGeorge Wilson 879fa9e4066Sahrens ASSERT(oldc <= newc); 880fa9e4066Sahrens 881ecc2d604Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 882fa9e4066Sahrens 883ecc2d604Sbonwick if (oldc != 0) { 884ecc2d604Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 885ecc2d604Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 886ecc2d604Sbonwick } 887fa9e4066Sahrens 888ecc2d604Sbonwick vd->vdev_ms = mspp; 889ecc2d604Sbonwick vd->vdev_ms_count = newc; 890fa9e4066Sahrens 891ecc2d604Sbonwick for (m = oldc; m < newc; m++) { 8920713e232SGeorge Wilson uint64_t object = 0; 8930713e232SGeorge Wilson 894ecc2d604Sbonwick if (txg == 0) { 895ecc2d604Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 8967bfdf011SNeil Perrin m * sizeof (uint64_t), sizeof (uint64_t), &object, 8977bfdf011SNeil Perrin DMU_READ_PREFETCH); 898ecc2d604Sbonwick if (error) 899ecc2d604Sbonwick return (error); 900fa9e4066Sahrens } 9011e9bd7ecSPrakash Surya 9021e9bd7ecSPrakash Surya error = metaslab_init(vd->vdev_mg, m, object, txg, 9031e9bd7ecSPrakash Surya &(vd->vdev_ms[m])); 9041e9bd7ecSPrakash Surya if (error) 9051e9bd7ecSPrakash Surya return (error); 906fa9e4066Sahrens } 907fa9e4066Sahrens 908a1521560SJeff Bonwick if (txg == 0) 909a1521560SJeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 910a1521560SJeff Bonwick 9113f9d6ad7SLin Ling /* 9123f9d6ad7SLin Ling * If the vdev is being removed we don't activate 9133f9d6ad7SLin Ling * the metaslabs since we want to ensure that no new 9143f9d6ad7SLin Ling * allocations are performed on this device. 9153f9d6ad7SLin Ling */ 9163f9d6ad7SLin Ling if (oldc == 0 && !vd->vdev_removing) 917a1521560SJeff Bonwick metaslab_group_activate(vd->vdev_mg); 918a1521560SJeff Bonwick 919a1521560SJeff Bonwick if (txg == 0) 920a1521560SJeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG); 921a1521560SJeff Bonwick 922ea8dc4b6Seschrock return (0); 923fa9e4066Sahrens } 924fa9e4066Sahrens 925fa9e4066Sahrens void 926fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd) 927fa9e4066Sahrens { 928fa9e4066Sahrens uint64_t m; 929fa9e4066Sahrens uint64_t count = vd->vdev_ms_count; 930fa9e4066Sahrens 931fa9e4066Sahrens if (vd->vdev_ms != NULL) { 932a1521560SJeff Bonwick metaslab_group_passivate(vd->vdev_mg); 9330713e232SGeorge Wilson for (m = 0; m < count; m++) { 9340713e232SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 9350713e232SGeorge Wilson 9360713e232SGeorge Wilson if (msp != NULL) 9370713e232SGeorge Wilson metaslab_fini(msp); 9380713e232SGeorge Wilson } 939fa9e4066Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 940fa9e4066Sahrens vd->vdev_ms = NULL; 941fa9e4066Sahrens } 942fa9e4066Sahrens } 943fa9e4066Sahrens 944e14bb325SJeff Bonwick typedef struct vdev_probe_stats { 945e14bb325SJeff Bonwick boolean_t vps_readable; 946e14bb325SJeff Bonwick boolean_t vps_writeable; 947e14bb325SJeff Bonwick int vps_flags; 948e14bb325SJeff Bonwick } vdev_probe_stats_t; 949e14bb325SJeff Bonwick 950e14bb325SJeff Bonwick static void 951e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio) 9520a4e9518Sgw { 9538ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 954a3f829aeSBill Moore vdev_t *vd = zio->io_vd; 955e14bb325SJeff Bonwick vdev_probe_stats_t *vps = zio->io_private; 956a3f829aeSBill Moore 957a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio != NULL); 958e14bb325SJeff Bonwick 959e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) { 960e14bb325SJeff Bonwick if (zio->io_error == 0) 961e14bb325SJeff Bonwick vps->vps_readable = 1; 9628ad4d6ddSJeff Bonwick if (zio->io_error == 0 && spa_writeable(spa)) { 963a3f829aeSBill Moore zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 964e14bb325SJeff Bonwick zio->io_offset, zio->io_size, zio->io_data, 965e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 966e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 967e14bb325SJeff Bonwick } else { 968e14bb325SJeff Bonwick zio_buf_free(zio->io_data, zio->io_size); 969e14bb325SJeff Bonwick } 970e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_WRITE) { 971e14bb325SJeff Bonwick if (zio->io_error == 0) 972e14bb325SJeff Bonwick vps->vps_writeable = 1; 973e14bb325SJeff Bonwick zio_buf_free(zio->io_data, zio->io_size); 974e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_NULL) { 975a3f829aeSBill Moore zio_t *pio; 976e14bb325SJeff Bonwick 977e14bb325SJeff Bonwick vd->vdev_cant_read |= !vps->vps_readable; 978e14bb325SJeff Bonwick vd->vdev_cant_write |= !vps->vps_writeable; 979e14bb325SJeff Bonwick 980e14bb325SJeff Bonwick if (vdev_readable(vd) && 9818ad4d6ddSJeff Bonwick (vdev_writeable(vd) || !spa_writeable(spa))) { 982e14bb325SJeff Bonwick zio->io_error = 0; 983e14bb325SJeff Bonwick } else { 984e14bb325SJeff Bonwick ASSERT(zio->io_error != 0); 985e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 9868ad4d6ddSJeff Bonwick spa, vd, NULL, 0, 0); 987be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 988e14bb325SJeff Bonwick } 989a3f829aeSBill Moore 990a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 991a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio == zio); 992a3f829aeSBill Moore vd->vdev_probe_zio = NULL; 993a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 994a3f829aeSBill Moore 9950f7643c7SGeorge Wilson zio_link_t *zl = NULL; 9960f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 997a3f829aeSBill Moore if (!vdev_accessible(vd, pio)) 998be6fd75aSMatthew Ahrens pio->io_error = SET_ERROR(ENXIO); 999a3f829aeSBill Moore 1000e14bb325SJeff Bonwick kmem_free(vps, sizeof (*vps)); 1001e14bb325SJeff Bonwick } 1002e14bb325SJeff Bonwick } 10030a4e9518Sgw 1004e14bb325SJeff Bonwick /* 1005f7170741SWill Andrews * Determine whether this device is accessible. 1006f7170741SWill Andrews * 1007f7170741SWill Andrews * Read and write to several known locations: the pad regions of each 1008f7170741SWill Andrews * vdev label but the first, which we leave alone in case it contains 1009f7170741SWill Andrews * a VTOC. 1010e14bb325SJeff Bonwick */ 1011e14bb325SJeff Bonwick zio_t * 1012a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio) 1013e14bb325SJeff Bonwick { 1014e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 1015a3f829aeSBill Moore vdev_probe_stats_t *vps = NULL; 1016a3f829aeSBill Moore zio_t *pio; 1017a3f829aeSBill Moore 1018a3f829aeSBill Moore ASSERT(vd->vdev_ops->vdev_op_leaf); 10190a4e9518Sgw 1020a3f829aeSBill Moore /* 1021a3f829aeSBill Moore * Don't probe the probe. 1022a3f829aeSBill Moore */ 1023a3f829aeSBill Moore if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1024a3f829aeSBill Moore return (NULL); 1025e14bb325SJeff Bonwick 1026a3f829aeSBill Moore /* 1027a3f829aeSBill Moore * To prevent 'probe storms' when a device fails, we create 1028a3f829aeSBill Moore * just one probe i/o at a time. All zios that want to probe 1029a3f829aeSBill Moore * this vdev will become parents of the probe io. 1030a3f829aeSBill Moore */ 1031a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1032e14bb325SJeff Bonwick 1033a3f829aeSBill Moore if ((pio = vd->vdev_probe_zio) == NULL) { 1034a3f829aeSBill Moore vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1035a3f829aeSBill Moore 1036a3f829aeSBill Moore vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1037a3f829aeSBill Moore ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 10388956713aSEric Schrock ZIO_FLAG_TRYHARD; 1039a3f829aeSBill Moore 1040a3f829aeSBill Moore if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1041a3f829aeSBill Moore /* 1042a3f829aeSBill Moore * vdev_cant_read and vdev_cant_write can only 1043a3f829aeSBill Moore * transition from TRUE to FALSE when we have the 1044a3f829aeSBill Moore * SCL_ZIO lock as writer; otherwise they can only 1045a3f829aeSBill Moore * transition from FALSE to TRUE. This ensures that 1046a3f829aeSBill Moore * any zio looking at these values can assume that 1047a3f829aeSBill Moore * failures persist for the life of the I/O. That's 1048a3f829aeSBill Moore * important because when a device has intermittent 1049a3f829aeSBill Moore * connectivity problems, we want to ensure that 1050a3f829aeSBill Moore * they're ascribed to the device (ENXIO) and not 1051a3f829aeSBill Moore * the zio (EIO). 1052a3f829aeSBill Moore * 1053a3f829aeSBill Moore * Since we hold SCL_ZIO as writer here, clear both 1054a3f829aeSBill Moore * values so the probe can reevaluate from first 1055a3f829aeSBill Moore * principles. 1056a3f829aeSBill Moore */ 1057a3f829aeSBill Moore vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1058a3f829aeSBill Moore vd->vdev_cant_read = B_FALSE; 1059a3f829aeSBill Moore vd->vdev_cant_write = B_FALSE; 1060a3f829aeSBill Moore } 1061a3f829aeSBill Moore 1062a3f829aeSBill Moore vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1063a3f829aeSBill Moore vdev_probe_done, vps, 1064a3f829aeSBill Moore vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1065a3f829aeSBill Moore 106698d1cbfeSGeorge Wilson /* 106798d1cbfeSGeorge Wilson * We can't change the vdev state in this context, so we 106898d1cbfeSGeorge Wilson * kick off an async task to do it on our behalf. 106998d1cbfeSGeorge Wilson */ 1070a3f829aeSBill Moore if (zio != NULL) { 1071a3f829aeSBill Moore vd->vdev_probe_wanted = B_TRUE; 1072a3f829aeSBill Moore spa_async_request(spa, SPA_ASYNC_PROBE); 1073a3f829aeSBill Moore } 1074e14bb325SJeff Bonwick } 1075e14bb325SJeff Bonwick 1076a3f829aeSBill Moore if (zio != NULL) 1077a3f829aeSBill Moore zio_add_child(zio, pio); 1078e14bb325SJeff Bonwick 1079a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1080e14bb325SJeff Bonwick 1081a3f829aeSBill Moore if (vps == NULL) { 1082a3f829aeSBill Moore ASSERT(zio != NULL); 1083a3f829aeSBill Moore return (NULL); 1084a3f829aeSBill Moore } 1085e14bb325SJeff Bonwick 1086e14bb325SJeff Bonwick for (int l = 1; l < VDEV_LABELS; l++) { 1087a3f829aeSBill Moore zio_nowait(zio_read_phys(pio, vd, 1088e14bb325SJeff Bonwick vdev_label_offset(vd->vdev_psize, l, 1089f83ffe1aSLin Ling offsetof(vdev_label_t, vl_pad2)), 1090f83ffe1aSLin Ling VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 1091e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1092e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1093e14bb325SJeff Bonwick } 1094e14bb325SJeff Bonwick 1095a3f829aeSBill Moore if (zio == NULL) 1096a3f829aeSBill Moore return (pio); 1097a3f829aeSBill Moore 1098a3f829aeSBill Moore zio_nowait(pio); 1099a3f829aeSBill Moore return (NULL); 11000a4e9518Sgw } 11010a4e9518Sgw 1102f64c0e34SEric Taylor static void 1103f64c0e34SEric Taylor vdev_open_child(void *arg) 1104f64c0e34SEric Taylor { 1105f64c0e34SEric Taylor vdev_t *vd = arg; 1106f64c0e34SEric Taylor 1107f64c0e34SEric Taylor vd->vdev_open_thread = curthread; 1108f64c0e34SEric Taylor vd->vdev_open_error = vdev_open(vd); 1109f64c0e34SEric Taylor vd->vdev_open_thread = NULL; 1110f64c0e34SEric Taylor } 1111f64c0e34SEric Taylor 1112681d9761SEric Taylor boolean_t 1113681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd) 1114681d9761SEric Taylor { 1115681d9761SEric Taylor if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1116681d9761SEric Taylor strlen(ZVOL_DIR)) == 0) 1117681d9761SEric Taylor return (B_TRUE); 1118681d9761SEric Taylor for (int c = 0; c < vd->vdev_children; c++) 1119681d9761SEric Taylor if (vdev_uses_zvols(vd->vdev_child[c])) 1120681d9761SEric Taylor return (B_TRUE); 1121681d9761SEric Taylor return (B_FALSE); 1122681d9761SEric Taylor } 1123681d9761SEric Taylor 1124f64c0e34SEric Taylor void 1125f64c0e34SEric Taylor vdev_open_children(vdev_t *vd) 1126f64c0e34SEric Taylor { 1127f64c0e34SEric Taylor taskq_t *tq; 1128f64c0e34SEric Taylor int children = vd->vdev_children; 1129f64c0e34SEric Taylor 1130681d9761SEric Taylor /* 1131681d9761SEric Taylor * in order to handle pools on top of zvols, do the opens 1132681d9761SEric Taylor * in a single thread so that the same thread holds the 1133681d9761SEric Taylor * spa_namespace_lock 1134681d9761SEric Taylor */ 1135681d9761SEric Taylor if (vdev_uses_zvols(vd)) { 1136681d9761SEric Taylor for (int c = 0; c < children; c++) 1137681d9761SEric Taylor vd->vdev_child[c]->vdev_open_error = 1138681d9761SEric Taylor vdev_open(vd->vdev_child[c]); 1139681d9761SEric Taylor return; 1140681d9761SEric Taylor } 1141f64c0e34SEric Taylor tq = taskq_create("vdev_open", children, minclsyspri, 1142f64c0e34SEric Taylor children, children, TASKQ_PREPOPULATE); 1143f64c0e34SEric Taylor 1144f64c0e34SEric Taylor for (int c = 0; c < children; c++) 1145f64c0e34SEric Taylor VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1146f64c0e34SEric Taylor TQ_SLEEP) != NULL); 1147f64c0e34SEric Taylor 1148f64c0e34SEric Taylor taskq_destroy(tq); 1149f64c0e34SEric Taylor } 1150f64c0e34SEric Taylor 1151fa9e4066Sahrens /* 1152fa9e4066Sahrens * Prepare a virtual device for access. 1153fa9e4066Sahrens */ 1154fa9e4066Sahrens int 1155fa9e4066Sahrens vdev_open(vdev_t *vd) 1156fa9e4066Sahrens { 11578ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1158fa9e4066Sahrens int error; 1159fa9e4066Sahrens uint64_t osize = 0; 11604263d13fSGeorge Wilson uint64_t max_osize = 0; 11614263d13fSGeorge Wilson uint64_t asize, max_asize, psize; 1162ecc2d604Sbonwick uint64_t ashift = 0; 1163fa9e4066Sahrens 1164f64c0e34SEric Taylor ASSERT(vd->vdev_open_thread == curthread || 1165f64c0e34SEric Taylor spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1166fa9e4066Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1167fa9e4066Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1168fa9e4066Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1169fa9e4066Sahrens 1170fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1171e6ca193dSGeorge Wilson vd->vdev_cant_read = B_FALSE; 1172e6ca193dSGeorge Wilson vd->vdev_cant_write = B_FALSE; 1173573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 1174fa9e4066Sahrens 1175069f55e2SEric Schrock /* 1176069f55e2SEric Schrock * If this vdev is not removed, check its fault status. If it's 1177069f55e2SEric Schrock * faulted, bail out of the open. 1178069f55e2SEric Schrock */ 11793d7072f8Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 11803d7072f8Seschrock ASSERT(vd->vdev_children == 0); 1181069f55e2SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1182069f55e2SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 11833d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1184069f55e2SEric Schrock vd->vdev_label_aux); 1185be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 11863d7072f8Seschrock } else if (vd->vdev_offline) { 1187fa9e4066Sahrens ASSERT(vd->vdev_children == 0); 1188ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1189be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1190fa9e4066Sahrens } 1191fa9e4066Sahrens 11924263d13fSGeorge Wilson error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1193fa9e4066Sahrens 1194095bcd66SGeorge Wilson /* 1195095bcd66SGeorge Wilson * Reset the vdev_reopening flag so that we actually close 1196095bcd66SGeorge Wilson * the vdev on error. 1197095bcd66SGeorge Wilson */ 1198095bcd66SGeorge Wilson vd->vdev_reopening = B_FALSE; 1199ea8dc4b6Seschrock if (zio_injection_enabled && error == 0) 12008956713aSEric Schrock error = zio_handle_device_injection(vd, NULL, ENXIO); 1201ea8dc4b6Seschrock 1202fa9e4066Sahrens if (error) { 12033d7072f8Seschrock if (vd->vdev_removed && 12043d7072f8Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 12053d7072f8Seschrock vd->vdev_removed = B_FALSE; 12063d7072f8Seschrock 1207ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1208fa9e4066Sahrens vd->vdev_stat.vs_aux); 1209fa9e4066Sahrens return (error); 1210fa9e4066Sahrens } 1211fa9e4066Sahrens 12123d7072f8Seschrock vd->vdev_removed = B_FALSE; 12133d7072f8Seschrock 1214096d22d4SEric Schrock /* 1215096d22d4SEric Schrock * Recheck the faulted flag now that we have confirmed that 1216096d22d4SEric Schrock * the vdev is accessible. If we're faulted, bail. 1217096d22d4SEric Schrock */ 1218096d22d4SEric Schrock if (vd->vdev_faulted) { 1219096d22d4SEric Schrock ASSERT(vd->vdev_children == 0); 1220096d22d4SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1221096d22d4SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1222096d22d4SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1223096d22d4SEric Schrock vd->vdev_label_aux); 1224be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1225096d22d4SEric Schrock } 1226096d22d4SEric Schrock 12273d7072f8Seschrock if (vd->vdev_degraded) { 12283d7072f8Seschrock ASSERT(vd->vdev_children == 0); 12293d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 12303d7072f8Seschrock VDEV_AUX_ERR_EXCEEDED); 12313d7072f8Seschrock } else { 1232069f55e2SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 12333d7072f8Seschrock } 1234fa9e4066Sahrens 123588ecc943SGeorge Wilson /* 123688ecc943SGeorge Wilson * For hole or missing vdevs we just return success. 123788ecc943SGeorge Wilson */ 123888ecc943SGeorge Wilson if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 123988ecc943SGeorge Wilson return (0); 124088ecc943SGeorge Wilson 1241573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 1242ea8dc4b6Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1243ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1244ea8dc4b6Seschrock VDEV_AUX_NONE); 1245ea8dc4b6Seschrock break; 1246ea8dc4b6Seschrock } 1247573ca77eSGeorge Wilson } 1248fa9e4066Sahrens 1249fa9e4066Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 12504263d13fSGeorge Wilson max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1251fa9e4066Sahrens 1252fa9e4066Sahrens if (vd->vdev_children == 0) { 1253fa9e4066Sahrens if (osize < SPA_MINDEVSIZE) { 1254ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1255ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1256be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1257fa9e4066Sahrens } 1258fa9e4066Sahrens psize = osize; 1259fa9e4066Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 12604263d13fSGeorge Wilson max_asize = max_osize - (VDEV_LABEL_START_SIZE + 12614263d13fSGeorge Wilson VDEV_LABEL_END_SIZE); 1262fa9e4066Sahrens } else { 1263ecc2d604Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1264fa9e4066Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1265ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1266ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1267be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1268fa9e4066Sahrens } 1269fa9e4066Sahrens psize = 0; 1270fa9e4066Sahrens asize = osize; 12714263d13fSGeorge Wilson max_asize = max_osize; 1272fa9e4066Sahrens } 1273fa9e4066Sahrens 1274fa9e4066Sahrens vd->vdev_psize = psize; 1275fa9e4066Sahrens 1276573ca77eSGeorge Wilson /* 1277*c040c10cSSteven Hartland * Make sure the allocatable size hasn't shrunk too much. 1278573ca77eSGeorge Wilson */ 1279573ca77eSGeorge Wilson if (asize < vd->vdev_min_asize) { 1280573ca77eSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1281573ca77eSGeorge Wilson VDEV_AUX_BAD_LABEL); 1282be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1283573ca77eSGeorge Wilson } 1284573ca77eSGeorge Wilson 1285fa9e4066Sahrens if (vd->vdev_asize == 0) { 1286fa9e4066Sahrens /* 1287fa9e4066Sahrens * This is the first-ever open, so use the computed values. 1288ecc2d604Sbonwick * For testing purposes, a higher ashift can be requested. 1289fa9e4066Sahrens */ 1290fa9e4066Sahrens vd->vdev_asize = asize; 12914263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1292ecc2d604Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1293fa9e4066Sahrens } else { 1294fa9e4066Sahrens /* 12952384d9f8SGeorge Wilson * Detect if the alignment requirement has increased. 12962384d9f8SGeorge Wilson * We don't want to make the pool unavailable, just 12972384d9f8SGeorge Wilson * issue a warning instead. 1298fa9e4066Sahrens */ 12992384d9f8SGeorge Wilson if (ashift > vd->vdev_top->vdev_ashift && 13002384d9f8SGeorge Wilson vd->vdev_ops->vdev_op_leaf) { 13012384d9f8SGeorge Wilson cmn_err(CE_WARN, 13022384d9f8SGeorge Wilson "Disk, '%s', has a block alignment that is " 13032384d9f8SGeorge Wilson "larger than the pool's alignment\n", 13042384d9f8SGeorge Wilson vd->vdev_path); 1305fa9e4066Sahrens } 13064263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1307573ca77eSGeorge Wilson } 1308fa9e4066Sahrens 1309573ca77eSGeorge Wilson /* 1310*c040c10cSSteven Hartland * If all children are healthy we update asize if either: 1311*c040c10cSSteven Hartland * The asize has increased, due to a device expansion caused by dynamic 1312*c040c10cSSteven Hartland * LUN growth or vdev replacement, and automatic expansion is enabled; 1313*c040c10cSSteven Hartland * making the additional space available. 1314*c040c10cSSteven Hartland * 1315*c040c10cSSteven Hartland * The asize has decreased, due to a device shrink usually caused by a 1316*c040c10cSSteven Hartland * vdev replace with a smaller device. This ensures that calculations 1317*c040c10cSSteven Hartland * based of max_asize and asize e.g. esize are always valid. It's safe 1318*c040c10cSSteven Hartland * to do this as we've already validated that asize is greater than 1319*c040c10cSSteven Hartland * vdev_min_asize. 1320573ca77eSGeorge Wilson */ 1321*c040c10cSSteven Hartland if (vd->vdev_state == VDEV_STATE_HEALTHY && 1322*c040c10cSSteven Hartland ((asize > vd->vdev_asize && 1323*c040c10cSSteven Hartland (vd->vdev_expanding || spa->spa_autoexpand)) || 1324*c040c10cSSteven Hartland (asize < vd->vdev_asize))) 1325573ca77eSGeorge Wilson vd->vdev_asize = asize; 1326fa9e4066Sahrens 1327573ca77eSGeorge Wilson vdev_set_min_asize(vd); 1328fa9e4066Sahrens 13290a4e9518Sgw /* 13300a4e9518Sgw * Ensure we can issue some IO before declaring the 13310a4e9518Sgw * vdev open for business. 13320a4e9518Sgw */ 1333e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 1334e14bb325SJeff Bonwick (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 133598d1cbfeSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 133698d1cbfeSGeorge Wilson VDEV_AUX_ERR_EXCEEDED); 13370a4e9518Sgw return (error); 13380a4e9518Sgw } 13390a4e9518Sgw 134081cd5c55SMatthew Ahrens /* 134181cd5c55SMatthew Ahrens * Track the min and max ashift values for normal data devices. 134281cd5c55SMatthew Ahrens */ 134381cd5c55SMatthew Ahrens if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 134481cd5c55SMatthew Ahrens !vd->vdev_islog && vd->vdev_aux == NULL) { 134581cd5c55SMatthew Ahrens if (vd->vdev_ashift > spa->spa_max_ashift) 134681cd5c55SMatthew Ahrens spa->spa_max_ashift = vd->vdev_ashift; 134781cd5c55SMatthew Ahrens if (vd->vdev_ashift < spa->spa_min_ashift) 134881cd5c55SMatthew Ahrens spa->spa_min_ashift = vd->vdev_ashift; 134981cd5c55SMatthew Ahrens } 135081cd5c55SMatthew Ahrens 1351088f3894Sahrens /* 1352088f3894Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 13538ad4d6ddSJeff Bonwick * resilver. But don't do this if we are doing a reopen for a scrub, 13548ad4d6ddSJeff Bonwick * since this would just restart the scrub we are already doing. 1355088f3894Sahrens */ 13568ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 13578ad4d6ddSJeff Bonwick vdev_resilver_needed(vd, NULL, NULL)) 13588ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1359088f3894Sahrens 1360fa9e4066Sahrens return (0); 1361fa9e4066Sahrens } 1362fa9e4066Sahrens 1363560e6e96Seschrock /* 1364560e6e96Seschrock * Called once the vdevs are all opened, this routine validates the label 1365560e6e96Seschrock * contents. This needs to be done before vdev_load() so that we don't 13663d7072f8Seschrock * inadvertently do repair I/Os to the wrong device. 1367560e6e96Seschrock * 1368d7f601efSGeorge Wilson * If 'strict' is false ignore the spa guid check. This is necessary because 1369d7f601efSGeorge Wilson * if the machine crashed during a re-guid the new guid might have been written 1370d7f601efSGeorge Wilson * to all of the vdev labels, but not the cached config. The strict check 1371d7f601efSGeorge Wilson * will be performed when the pool is opened again using the mos config. 1372d7f601efSGeorge Wilson * 1373560e6e96Seschrock * This function will only return failure if one of the vdevs indicates that it 1374560e6e96Seschrock * has since been destroyed or exported. This is only possible if 1375560e6e96Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1376560e6e96Seschrock * will be updated but the function will return 0. 1377560e6e96Seschrock */ 1378560e6e96Seschrock int 1379d7f601efSGeorge Wilson vdev_validate(vdev_t *vd, boolean_t strict) 1380560e6e96Seschrock { 1381560e6e96Seschrock spa_t *spa = vd->vdev_spa; 1382560e6e96Seschrock nvlist_t *label; 13831195e687SMark J Musante uint64_t guid = 0, top_guid; 1384560e6e96Seschrock uint64_t state; 1385560e6e96Seschrock 1386573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1387d7f601efSGeorge Wilson if (vdev_validate(vd->vdev_child[c], strict) != 0) 1388be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1389560e6e96Seschrock 1390b5989ec7Seschrock /* 1391b5989ec7Seschrock * If the device has already failed, or was marked offline, don't do 1392b5989ec7Seschrock * any further validation. Otherwise, label I/O will fail and we will 1393b5989ec7Seschrock * overwrite the previous state. 1394b5989ec7Seschrock */ 1395e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 13961195e687SMark J Musante uint64_t aux_guid = 0; 13971195e687SMark J Musante nvlist_t *nvl; 1398bda88194SGeorge Wilson uint64_t txg = spa_last_synced_txg(spa) != 0 ? 1399bda88194SGeorge Wilson spa_last_synced_txg(spa) : -1ULL; 1400560e6e96Seschrock 1401dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1402560e6e96Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1403560e6e96Seschrock VDEV_AUX_BAD_LABEL); 1404560e6e96Seschrock return (0); 1405560e6e96Seschrock } 1406560e6e96Seschrock 14071195e687SMark J Musante /* 14081195e687SMark J Musante * Determine if this vdev has been split off into another 14091195e687SMark J Musante * pool. If so, then refuse to open it. 14101195e687SMark J Musante */ 14111195e687SMark J Musante if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 14121195e687SMark J Musante &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 14131195e687SMark J Musante vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 14141195e687SMark J Musante VDEV_AUX_SPLIT_POOL); 14151195e687SMark J Musante nvlist_free(label); 14161195e687SMark J Musante return (0); 14171195e687SMark J Musante } 14181195e687SMark J Musante 1419d7f601efSGeorge Wilson if (strict && (nvlist_lookup_uint64(label, 1420d7f601efSGeorge Wilson ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1421d7f601efSGeorge Wilson guid != spa_guid(spa))) { 1422560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1423560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1424560e6e96Seschrock nvlist_free(label); 1425560e6e96Seschrock return (0); 1426560e6e96Seschrock } 1427560e6e96Seschrock 14281195e687SMark J Musante if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 14291195e687SMark J Musante != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 14301195e687SMark J Musante &aux_guid) != 0) 14311195e687SMark J Musante aux_guid = 0; 14321195e687SMark J Musante 1433e14bb325SJeff Bonwick /* 1434e14bb325SJeff Bonwick * If this vdev just became a top-level vdev because its 1435e14bb325SJeff Bonwick * sibling was detached, it will have adopted the parent's 1436e14bb325SJeff Bonwick * vdev guid -- but the label may or may not be on disk yet. 1437e14bb325SJeff Bonwick * Fortunately, either version of the label will have the 1438e14bb325SJeff Bonwick * same top guid, so if we're a top-level vdev, we can 1439e14bb325SJeff Bonwick * safely compare to that instead. 14401195e687SMark J Musante * 14411195e687SMark J Musante * If we split this vdev off instead, then we also check the 14421195e687SMark J Musante * original pool's guid. We don't want to consider the vdev 14431195e687SMark J Musante * corrupt if it is partway through a split operation. 1444e14bb325SJeff Bonwick */ 1445560e6e96Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1446e14bb325SJeff Bonwick &guid) != 0 || 1447e14bb325SJeff Bonwick nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 1448e14bb325SJeff Bonwick &top_guid) != 0 || 14491195e687SMark J Musante ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) && 1450e14bb325SJeff Bonwick (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 1451560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1452560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1453560e6e96Seschrock nvlist_free(label); 1454560e6e96Seschrock return (0); 1455560e6e96Seschrock } 1456560e6e96Seschrock 1457560e6e96Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1458560e6e96Seschrock &state) != 0) { 1459560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1460560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1461560e6e96Seschrock nvlist_free(label); 1462560e6e96Seschrock return (0); 1463560e6e96Seschrock } 1464560e6e96Seschrock 1465560e6e96Seschrock nvlist_free(label); 1466560e6e96Seschrock 1467bc758434SLin Ling /* 14684b964adaSGeorge Wilson * If this is a verbatim import, no need to check the 1469bc758434SLin Ling * state of the pool. 1470bc758434SLin Ling */ 14714b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1472b16da2e2SGeorge Wilson spa_load_state(spa) == SPA_LOAD_OPEN && 1473bc758434SLin Ling state != POOL_STATE_ACTIVE) 1474be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1475560e6e96Seschrock 147651ece835Seschrock /* 147751ece835Seschrock * If we were able to open and validate a vdev that was 147851ece835Seschrock * previously marked permanently unavailable, clear that state 147951ece835Seschrock * now. 148051ece835Seschrock */ 148151ece835Seschrock if (vd->vdev_not_present) 148251ece835Seschrock vd->vdev_not_present = 0; 148351ece835Seschrock } 1484560e6e96Seschrock 1485560e6e96Seschrock return (0); 1486560e6e96Seschrock } 1487560e6e96Seschrock 1488fa9e4066Sahrens /* 1489fa9e4066Sahrens * Close a virtual device. 1490fa9e4066Sahrens */ 1491fa9e4066Sahrens void 1492fa9e4066Sahrens vdev_close(vdev_t *vd) 1493fa9e4066Sahrens { 14948ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1495095bcd66SGeorge Wilson vdev_t *pvd = vd->vdev_parent; 14968ad4d6ddSJeff Bonwick 14978ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 14988ad4d6ddSJeff Bonwick 14991195e687SMark J Musante /* 15001195e687SMark J Musante * If our parent is reopening, then we are as well, unless we are 15011195e687SMark J Musante * going offline. 15021195e687SMark J Musante */ 1503095bcd66SGeorge Wilson if (pvd != NULL && pvd->vdev_reopening) 15041195e687SMark J Musante vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1505095bcd66SGeorge Wilson 1506fa9e4066Sahrens vd->vdev_ops->vdev_op_close(vd); 1507fa9e4066Sahrens 15083d7072f8Seschrock vdev_cache_purge(vd); 1509fa9e4066Sahrens 1510560e6e96Seschrock /* 1511573ca77eSGeorge Wilson * We record the previous state before we close it, so that if we are 1512560e6e96Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 1513560e6e96Seschrock * it's still faulted. 1514560e6e96Seschrock */ 1515560e6e96Seschrock vd->vdev_prevstate = vd->vdev_state; 1516560e6e96Seschrock 1517fa9e4066Sahrens if (vd->vdev_offline) 1518fa9e4066Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 1519fa9e4066Sahrens else 1520fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 1521ea8dc4b6Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1522fa9e4066Sahrens } 1523fa9e4066Sahrens 1524dcba9f3fSGeorge Wilson void 1525dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd) 1526dcba9f3fSGeorge Wilson { 1527dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 1528dcba9f3fSGeorge Wilson 1529dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 1530dcba9f3fSGeorge Wilson if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1531dcba9f3fSGeorge Wilson return; 1532dcba9f3fSGeorge Wilson 1533dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1534dcba9f3fSGeorge Wilson vdev_hold(vd->vdev_child[c]); 1535dcba9f3fSGeorge Wilson 1536dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 1537dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_hold(vd); 1538dcba9f3fSGeorge Wilson } 1539dcba9f3fSGeorge Wilson 1540dcba9f3fSGeorge Wilson void 1541dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd) 1542dcba9f3fSGeorge Wilson { 1543dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 1544dcba9f3fSGeorge Wilson 1545dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 1546dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1547dcba9f3fSGeorge Wilson vdev_rele(vd->vdev_child[c]); 1548dcba9f3fSGeorge Wilson 1549dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 1550dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_rele(vd); 1551dcba9f3fSGeorge Wilson } 1552dcba9f3fSGeorge Wilson 1553095bcd66SGeorge Wilson /* 1554095bcd66SGeorge Wilson * Reopen all interior vdevs and any unopened leaves. We don't actually 1555095bcd66SGeorge Wilson * reopen leaf vdevs which had previously been opened as they might deadlock 1556095bcd66SGeorge Wilson * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1557095bcd66SGeorge Wilson * If the leaf has never been opened then open it, as usual. 1558095bcd66SGeorge Wilson */ 1559fa9e4066Sahrens void 1560ea8dc4b6Seschrock vdev_reopen(vdev_t *vd) 1561fa9e4066Sahrens { 1562ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 1563fa9e4066Sahrens 1564e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1565ea8dc4b6Seschrock 15661195e687SMark J Musante /* set the reopening flag unless we're taking the vdev offline */ 15671195e687SMark J Musante vd->vdev_reopening = !vd->vdev_offline; 1568fa9e4066Sahrens vdev_close(vd); 1569fa9e4066Sahrens (void) vdev_open(vd); 1570fa9e4066Sahrens 157139c23413Seschrock /* 157239c23413Seschrock * Call vdev_validate() here to make sure we have the same device. 157339c23413Seschrock * Otherwise, a device with an invalid label could be successfully 157439c23413Seschrock * opened in response to vdev_reopen(). 157539c23413Seschrock */ 1576c5904d13Seschrock if (vd->vdev_aux) { 1577c5904d13Seschrock (void) vdev_validate_aux(vd); 1578e14bb325SJeff Bonwick if (vdev_readable(vd) && vdev_writeable(vd) && 15796809eb4eSEric Schrock vd->vdev_aux == &spa->spa_l2cache && 1580573ca77eSGeorge Wilson !l2arc_vdev_present(vd)) 1581573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1582c5904d13Seschrock } else { 1583bda88194SGeorge Wilson (void) vdev_validate(vd, B_TRUE); 1584c5904d13Seschrock } 158539c23413Seschrock 1586fa9e4066Sahrens /* 15873d7072f8Seschrock * Reassess parent vdev's health. 1588fa9e4066Sahrens */ 15893d7072f8Seschrock vdev_propagate_state(vd); 1590fa9e4066Sahrens } 1591fa9e4066Sahrens 1592fa9e4066Sahrens int 159399653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1594fa9e4066Sahrens { 1595fa9e4066Sahrens int error; 1596fa9e4066Sahrens 1597fa9e4066Sahrens /* 1598fa9e4066Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 1599fa9e4066Sahrens * For a create, however, we want to fail the request if 1600fa9e4066Sahrens * there are any components we can't open. 1601fa9e4066Sahrens */ 1602fa9e4066Sahrens error = vdev_open(vd); 1603fa9e4066Sahrens 1604fa9e4066Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1605fa9e4066Sahrens vdev_close(vd); 1606fa9e4066Sahrens return (error ? error : ENXIO); 1607fa9e4066Sahrens } 1608fa9e4066Sahrens 1609fa9e4066Sahrens /* 16100713e232SGeorge Wilson * Recursively load DTLs and initialize all labels. 1611fa9e4066Sahrens */ 16120713e232SGeorge Wilson if ((error = vdev_dtl_load(vd)) != 0 || 16130713e232SGeorge Wilson (error = vdev_label_init(vd, txg, isreplacing ? 161439c23413Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1615fa9e4066Sahrens vdev_close(vd); 1616fa9e4066Sahrens return (error); 1617fa9e4066Sahrens } 1618fa9e4066Sahrens 1619fa9e4066Sahrens return (0); 1620fa9e4066Sahrens } 1621fa9e4066Sahrens 16220e34b6a7Sbonwick void 1623573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd) 1624fa9e4066Sahrens { 1625fa9e4066Sahrens /* 1626bf3e216cSMatthew Ahrens * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev. 1627fa9e4066Sahrens */ 1628bf3e216cSMatthew Ahrens vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev); 1629fa9e4066Sahrens vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1630fa9e4066Sahrens } 1631fa9e4066Sahrens 1632fa9e4066Sahrens void 1633ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1634fa9e4066Sahrens { 1635ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 163688ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 1637ecc2d604Sbonwick ASSERT(ISP2(flags)); 1638f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 1639fa9e4066Sahrens 1640ecc2d604Sbonwick if (flags & VDD_METASLAB) 1641ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1642ecc2d604Sbonwick 1643ecc2d604Sbonwick if (flags & VDD_DTL) 1644ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1645ecc2d604Sbonwick 1646ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1647fa9e4066Sahrens } 1648fa9e4066Sahrens 16490713e232SGeorge Wilson void 16500713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 16510713e232SGeorge Wilson { 16520713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 16530713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 16540713e232SGeorge Wilson 16550713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 16560713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 16570713e232SGeorge Wilson } 16580713e232SGeorge Wilson 16598ad4d6ddSJeff Bonwick /* 16608ad4d6ddSJeff Bonwick * DTLs. 16618ad4d6ddSJeff Bonwick * 16628ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 16639fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 16648ad4d6ddSJeff Bonwick * 16658ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 16668ad4d6ddSJeff Bonwick * 16678ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 16688ad4d6ddSJeff Bonwick * 16698ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 16708ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 16718ad4d6ddSJeff Bonwick * txgs that was scrubbed. 16728ad4d6ddSJeff Bonwick * 16738ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 16748ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 16758ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 16768ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 16778ad4d6ddSJeff Bonwick * determine whether a device can be detached. 16788ad4d6ddSJeff Bonwick * 16798ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 16808ad4d6ddSJeff Bonwick * either has the data or it doesn't. 16818ad4d6ddSJeff Bonwick * 16828ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 16838ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 16848ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 16858ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 16868ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 16878ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 16888ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 16898ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 16908ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 16918ad4d6ddSJeff Bonwick * 16928ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 16938ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 16948ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 16958ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 16968ad4d6ddSJeff Bonwick */ 1697fa9e4066Sahrens void 16988ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1699fa9e4066Sahrens { 17000713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 17018ad4d6ddSJeff Bonwick 17028ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 17038ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1704f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 17058ad4d6ddSJeff Bonwick 17060713e232SGeorge Wilson mutex_enter(rt->rt_lock); 17070713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 17080713e232SGeorge Wilson range_tree_add(rt, txg, size); 17090713e232SGeorge Wilson mutex_exit(rt->rt_lock); 1710fa9e4066Sahrens } 1711fa9e4066Sahrens 17128ad4d6ddSJeff Bonwick boolean_t 17138ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1714fa9e4066Sahrens { 17150713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 17168ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 1717fa9e4066Sahrens 17188ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 17198ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1720fa9e4066Sahrens 17210713e232SGeorge Wilson mutex_enter(rt->rt_lock); 17220713e232SGeorge Wilson if (range_tree_space(rt) != 0) 17230713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 17240713e232SGeorge Wilson mutex_exit(rt->rt_lock); 1725fa9e4066Sahrens 1726fa9e4066Sahrens return (dirty); 1727fa9e4066Sahrens } 1728fa9e4066Sahrens 17298ad4d6ddSJeff Bonwick boolean_t 17308ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 17318ad4d6ddSJeff Bonwick { 17320713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 17338ad4d6ddSJeff Bonwick boolean_t empty; 17348ad4d6ddSJeff Bonwick 17350713e232SGeorge Wilson mutex_enter(rt->rt_lock); 17360713e232SGeorge Wilson empty = (range_tree_space(rt) == 0); 17370713e232SGeorge Wilson mutex_exit(rt->rt_lock); 17388ad4d6ddSJeff Bonwick 17398ad4d6ddSJeff Bonwick return (empty); 17408ad4d6ddSJeff Bonwick } 17418ad4d6ddSJeff Bonwick 1742b4952e17SGeorge Wilson /* 1743b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 1744b4952e17SGeorge Wilson */ 1745b4952e17SGeorge Wilson static uint64_t 1746b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 1747b4952e17SGeorge Wilson { 17480713e232SGeorge Wilson range_seg_t *rs; 1749b4952e17SGeorge Wilson 1750b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 17510713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1752b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 1753b4952e17SGeorge Wilson 17540713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 17550713e232SGeorge Wilson return (rs->rs_start - 1); 1756b4952e17SGeorge Wilson } 1757b4952e17SGeorge Wilson 1758b4952e17SGeorge Wilson /* 1759b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 1760b4952e17SGeorge Wilson */ 1761b4952e17SGeorge Wilson static uint64_t 1762b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 1763b4952e17SGeorge Wilson { 17640713e232SGeorge Wilson range_seg_t *rs; 1765b4952e17SGeorge Wilson 1766b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 17670713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1768b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 1769b4952e17SGeorge Wilson 17700713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 17710713e232SGeorge Wilson return (rs->rs_end); 1772b4952e17SGeorge Wilson } 1773b4952e17SGeorge Wilson 1774b4952e17SGeorge Wilson /* 1775b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 1776b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 1777b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 1778b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 1779b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 1780b4952e17SGeorge Wilson * excise the DTLs. 1781b4952e17SGeorge Wilson */ 1782b4952e17SGeorge Wilson static boolean_t 1783b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 1784b4952e17SGeorge Wilson { 1785b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 1786b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1787b4952e17SGeorge Wilson 1788b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 1789b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 1790b4952e17SGeorge Wilson 1791b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 17920713e232SGeorge Wilson range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0) 1793b4952e17SGeorge Wilson return (B_TRUE); 1794b4952e17SGeorge Wilson 1795b4952e17SGeorge Wilson /* 1796b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 1797b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 1798b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 1799b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 1800b4952e17SGeorge Wilson * for excision. 1801b4952e17SGeorge Wilson */ 1802b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 1803b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 1804b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 1805b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 1806b4952e17SGeorge Wilson return (B_TRUE); 1807b4952e17SGeorge Wilson } 1808b4952e17SGeorge Wilson return (B_FALSE); 1809b4952e17SGeorge Wilson } 1810b4952e17SGeorge Wilson 1811fa9e4066Sahrens /* 1812fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 1813fa9e4066Sahrens */ 1814fa9e4066Sahrens void 1815fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1816fa9e4066Sahrens { 1817ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 18188ad4d6ddSJeff Bonwick avl_tree_t reftree; 18198ad4d6ddSJeff Bonwick int minref; 1820fa9e4066Sahrens 18218ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1822fa9e4066Sahrens 18238ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 18248ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 18258ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 18268ad4d6ddSJeff Bonwick 1827b24ab676SJeff Bonwick if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux) 18288ad4d6ddSJeff Bonwick return; 18298ad4d6ddSJeff Bonwick 18308ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 18313f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 18323f9d6ad7SLin Ling 1833fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 1834b4952e17SGeorge Wilson 1835b4952e17SGeorge Wilson /* 1836b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 1837b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 1838b4952e17SGeorge Wilson * excise regions on vdevs that were available during 1839b4952e17SGeorge Wilson * the entire duration of this scan. 1840b4952e17SGeorge Wilson */ 1841088f3894Sahrens if (scrub_txg != 0 && 18423f9d6ad7SLin Ling (spa->spa_scrub_started || 1843b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 1844b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 1845088f3894Sahrens /* 1846088f3894Sahrens * We completed a scrub up to scrub_txg. If we 1847088f3894Sahrens * did it without rebooting, then the scrub dtl 1848088f3894Sahrens * will be valid, so excise the old region and 1849088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 1850088f3894Sahrens * dtl as-is if there was an error. 18518ad4d6ddSJeff Bonwick * 18528ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 18538ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 18548ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 18558ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 18568ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 18578ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 18588ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 18598ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 18608ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 1861088f3894Sahrens */ 18620713e232SGeorge Wilson space_reftree_create(&reftree); 18630713e232SGeorge Wilson space_reftree_add_map(&reftree, 18640713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 18650713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 18660713e232SGeorge Wilson space_reftree_add_map(&reftree, 18670713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 18680713e232SGeorge Wilson space_reftree_generate_map(&reftree, 18690713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 18700713e232SGeorge Wilson space_reftree_destroy(&reftree); 1871fa9e4066Sahrens } 18720713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 18730713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 18740713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 1875fa9e4066Sahrens if (scrub_done) 18760713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 18770713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 18788ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 18790713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 18808ad4d6ddSJeff Bonwick else 18810713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 18820713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 1883b4952e17SGeorge Wilson 1884b4952e17SGeorge Wilson /* 1885b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 1886b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 1887b4952e17SGeorge Wilson */ 1888b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 18890713e232SGeorge Wilson range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 && 18900713e232SGeorge Wilson range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) 1891b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 1892b4952e17SGeorge Wilson 1893fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 1894088f3894Sahrens 1895ecc2d604Sbonwick if (txg != 0) 1896ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1897fa9e4066Sahrens return; 1898fa9e4066Sahrens } 1899fa9e4066Sahrens 1900fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 19018ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 190299bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 190399bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 19048ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 19058ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 19068ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 19078ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 19088ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 19098ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 19108ad4d6ddSJeff Bonwick else 19118ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 19120713e232SGeorge Wilson space_reftree_create(&reftree); 19138ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 19148ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 19158ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 19160713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 19178ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 19188ad4d6ddSJeff Bonwick } 19190713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 19200713e232SGeorge Wilson space_reftree_destroy(&reftree); 1921fa9e4066Sahrens } 19228ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 1923fa9e4066Sahrens } 1924fa9e4066Sahrens 19250713e232SGeorge Wilson int 1926fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 1927fa9e4066Sahrens { 1928fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 1929ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 19300713e232SGeorge Wilson int error = 0; 1931fa9e4066Sahrens 19320713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 19330713e232SGeorge Wilson ASSERT(!vd->vdev_ishole); 1934fa9e4066Sahrens 19350713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 19360713e232SGeorge Wilson vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock); 19370713e232SGeorge Wilson if (error) 19380713e232SGeorge Wilson return (error); 19390713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 1940fa9e4066Sahrens 19410713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 194288ecc943SGeorge Wilson 19430713e232SGeorge Wilson /* 19440713e232SGeorge Wilson * Now that we've opened the space_map we need to update 19450713e232SGeorge Wilson * the in-core DTL. 19460713e232SGeorge Wilson */ 19470713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 1948ecc2d604Sbonwick 19490713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 19500713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 19510713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 1952fa9e4066Sahrens 19530713e232SGeorge Wilson return (error); 19540713e232SGeorge Wilson } 19550713e232SGeorge Wilson 19560713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 19570713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 19580713e232SGeorge Wilson if (error != 0) 19590713e232SGeorge Wilson break; 19600713e232SGeorge Wilson } 1961fa9e4066Sahrens 1962fa9e4066Sahrens return (error); 1963fa9e4066Sahrens } 1964fa9e4066Sahrens 1965215198a6SJoe Stein void 1966215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 1967215198a6SJoe Stein { 1968215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 1969215198a6SJoe Stein 1970215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 1971215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 1972215198a6SJoe Stein zapobj, tx)); 1973215198a6SJoe Stein } 1974215198a6SJoe Stein 1975215198a6SJoe Stein uint64_t 1976215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 1977215198a6SJoe Stein { 1978215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 1979215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 1980215198a6SJoe Stein DMU_OT_NONE, 0, tx); 1981215198a6SJoe Stein 1982215198a6SJoe Stein ASSERT(zap != 0); 1983215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 1984215198a6SJoe Stein zap, tx)); 1985215198a6SJoe Stein 1986215198a6SJoe Stein return (zap); 1987215198a6SJoe Stein } 1988215198a6SJoe Stein 1989215198a6SJoe Stein void 1990215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 1991215198a6SJoe Stein { 1992215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 1993215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 1994215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 1995215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 1996215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 1997215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 1998215198a6SJoe Stein } 1999215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2000215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2001215198a6SJoe Stein } 2002215198a6SJoe Stein } 2003215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2004215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2005215198a6SJoe Stein } 2006215198a6SJoe Stein } 2007215198a6SJoe Stein 2008fa9e4066Sahrens void 2009fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2010fa9e4066Sahrens { 2011fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 20120713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2013ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 20140713e232SGeorge Wilson range_tree_t *rtsync; 20150713e232SGeorge Wilson kmutex_t rtlock; 2016fa9e4066Sahrens dmu_tx_t *tx; 20170713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2018fa9e4066Sahrens 201988ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 20200713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 202188ecc943SGeorge Wilson 2022fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2023fa9e4066Sahrens 20240713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 20250713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 20260713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 20270713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 20280713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 20290713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2030215198a6SJoe Stein 2031215198a6SJoe Stein /* 2032215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2033215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2034215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2035215198a6SJoe Stein */ 2036215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2037215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2038215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2039215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2040215198a6SJoe Stein } 2041215198a6SJoe Stein 2042fa9e4066Sahrens dmu_tx_commit(tx); 2043fa9e4066Sahrens return; 2044fa9e4066Sahrens } 2045fa9e4066Sahrens 20460713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 20470713e232SGeorge Wilson uint64_t new_object; 20480713e232SGeorge Wilson 20490713e232SGeorge Wilson new_object = space_map_alloc(mos, tx); 20500713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 20510713e232SGeorge Wilson 20520713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 20530713e232SGeorge Wilson 0, -1ULL, 0, &vd->vdev_dtl_lock)); 20540713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2055fa9e4066Sahrens } 2056fa9e4066Sahrens 20570713e232SGeorge Wilson mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL); 2058fa9e4066Sahrens 20590713e232SGeorge Wilson rtsync = range_tree_create(NULL, NULL, &rtlock); 2060fa9e4066Sahrens 20610713e232SGeorge Wilson mutex_enter(&rtlock); 2062fa9e4066Sahrens 2063fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 20640713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2065fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2066fa9e4066Sahrens 20670713e232SGeorge Wilson space_map_truncate(vd->vdev_dtl_sm, tx); 20680713e232SGeorge Wilson space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx); 20690713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2070fa9e4066Sahrens 20710713e232SGeorge Wilson range_tree_destroy(rtsync); 2072fa9e4066Sahrens 20730713e232SGeorge Wilson mutex_exit(&rtlock); 20740713e232SGeorge Wilson mutex_destroy(&rtlock); 2075fa9e4066Sahrens 20760713e232SGeorge Wilson /* 20770713e232SGeorge Wilson * If the object for the space map has changed then dirty 20780713e232SGeorge Wilson * the top level so that we update the config. 20790713e232SGeorge Wilson */ 20800713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 20810713e232SGeorge Wilson zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, " 20820713e232SGeorge Wilson "new object %llu", txg, spa_name(spa), object, 20830713e232SGeorge Wilson space_map_object(vd->vdev_dtl_sm)); 20840713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 20850713e232SGeorge Wilson } 2086fa9e4066Sahrens 2087fa9e4066Sahrens dmu_tx_commit(tx); 20880713e232SGeorge Wilson 20890713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 20900713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 20910713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2092fa9e4066Sahrens } 2093fa9e4066Sahrens 20948ad4d6ddSJeff Bonwick /* 20958ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 20968ad4d6ddSJeff Bonwick * without losing data. 20978ad4d6ddSJeff Bonwick */ 20988ad4d6ddSJeff Bonwick boolean_t 20998ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 21008ad4d6ddSJeff Bonwick { 21018ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 21028ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 21038ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 21048ad4d6ddSJeff Bonwick boolean_t required; 21058ad4d6ddSJeff Bonwick 21068ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 21078ad4d6ddSJeff Bonwick 21088ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 21098ad4d6ddSJeff Bonwick return (B_TRUE); 21108ad4d6ddSJeff Bonwick 21118ad4d6ddSJeff Bonwick /* 21128ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 21138ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 21148ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 21158ad4d6ddSJeff Bonwick */ 21168ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 21178ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 21188ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 21198ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 21208ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 21218ad4d6ddSJeff Bonwick 2122cb04b873SMark J Musante if (!required && zio_injection_enabled) 2123cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2124cb04b873SMark J Musante 21258ad4d6ddSJeff Bonwick return (required); 21268ad4d6ddSJeff Bonwick } 21278ad4d6ddSJeff Bonwick 2128088f3894Sahrens /* 2129088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2130088f3894Sahrens */ 2131088f3894Sahrens boolean_t 2132088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2133088f3894Sahrens { 2134088f3894Sahrens boolean_t needed = B_FALSE; 2135088f3894Sahrens uint64_t thismin = UINT64_MAX; 2136088f3894Sahrens uint64_t thismax = 0; 2137088f3894Sahrens 2138088f3894Sahrens if (vd->vdev_children == 0) { 2139088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 21400713e232SGeorge Wilson if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 && 21418ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2142088f3894Sahrens 2143b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2144b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2145088f3894Sahrens needed = B_TRUE; 2146088f3894Sahrens } 2147088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2148088f3894Sahrens } else { 21498ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2150088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2151088f3894Sahrens uint64_t cmin, cmax; 2152088f3894Sahrens 2153088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2154088f3894Sahrens thismin = MIN(thismin, cmin); 2155088f3894Sahrens thismax = MAX(thismax, cmax); 2156088f3894Sahrens needed = B_TRUE; 2157088f3894Sahrens } 2158088f3894Sahrens } 2159088f3894Sahrens } 2160088f3894Sahrens 2161088f3894Sahrens if (needed && minp) { 2162088f3894Sahrens *minp = thismin; 2163088f3894Sahrens *maxp = thismax; 2164088f3894Sahrens } 2165088f3894Sahrens return (needed); 2166088f3894Sahrens } 2167088f3894Sahrens 2168560e6e96Seschrock void 2169ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2170fa9e4066Sahrens { 2171fa9e4066Sahrens /* 2172fa9e4066Sahrens * Recursively load all children. 2173fa9e4066Sahrens */ 21748ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 2175560e6e96Seschrock vdev_load(vd->vdev_child[c]); 2176fa9e4066Sahrens 2177fa9e4066Sahrens /* 21780e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2179fa9e4066Sahrens */ 218088ecc943SGeorge Wilson if (vd == vd->vdev_top && !vd->vdev_ishole && 2181560e6e96Seschrock (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 2182560e6e96Seschrock vdev_metaslab_init(vd, 0) != 0)) 2183560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2184560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 2185fa9e4066Sahrens 2186fa9e4066Sahrens /* 2187fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2188fa9e4066Sahrens */ 2189560e6e96Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 2190560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2191560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 2192fa9e4066Sahrens } 2193fa9e4066Sahrens 219499653d4eSeschrock /* 2195fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 2196fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 2197fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 2198fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 2199fa94a07fSbrendan * repurposed to another pool. 220099653d4eSeschrock */ 220199653d4eSeschrock int 2202fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 220399653d4eSeschrock { 220499653d4eSeschrock nvlist_t *label; 220599653d4eSeschrock uint64_t guid, version; 220699653d4eSeschrock uint64_t state; 220799653d4eSeschrock 2208e14bb325SJeff Bonwick if (!vdev_readable(vd)) 2209c5904d13Seschrock return (0); 2210c5904d13Seschrock 2211dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 221299653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 221399653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 221499653d4eSeschrock return (-1); 221599653d4eSeschrock } 221699653d4eSeschrock 221799653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2218ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 221999653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 222099653d4eSeschrock guid != vd->vdev_guid || 222199653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 222299653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 222399653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 222499653d4eSeschrock nvlist_free(label); 222599653d4eSeschrock return (-1); 222699653d4eSeschrock } 222799653d4eSeschrock 222899653d4eSeschrock /* 222999653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 223099653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 223199653d4eSeschrock */ 223299653d4eSeschrock nvlist_free(label); 223399653d4eSeschrock return (0); 223499653d4eSeschrock } 223599653d4eSeschrock 223688ecc943SGeorge Wilson void 223788ecc943SGeorge Wilson vdev_remove(vdev_t *vd, uint64_t txg) 223888ecc943SGeorge Wilson { 223988ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 224088ecc943SGeorge Wilson objset_t *mos = spa->spa_meta_objset; 224188ecc943SGeorge Wilson dmu_tx_t *tx; 224288ecc943SGeorge Wilson 224388ecc943SGeorge Wilson tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2244215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 2245215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 224688ecc943SGeorge Wilson 224788ecc943SGeorge Wilson if (vd->vdev_ms != NULL) { 22482e4c9986SGeorge Wilson metaslab_group_t *mg = vd->vdev_mg; 22492e4c9986SGeorge Wilson 22502e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 22512e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 22522e4c9986SGeorge Wilson 225388ecc943SGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) { 225488ecc943SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 225588ecc943SGeorge Wilson 22560713e232SGeorge Wilson if (msp == NULL || msp->ms_sm == NULL) 225788ecc943SGeorge Wilson continue; 225888ecc943SGeorge Wilson 22590713e232SGeorge Wilson mutex_enter(&msp->ms_lock); 22602e4c9986SGeorge Wilson /* 22612e4c9986SGeorge Wilson * If the metaslab was not loaded when the vdev 22622e4c9986SGeorge Wilson * was removed then the histogram accounting may 22632e4c9986SGeorge Wilson * not be accurate. Update the histogram information 22642e4c9986SGeorge Wilson * here so that we ensure that the metaslab group 22652e4c9986SGeorge Wilson * and metaslab class are up-to-date. 22662e4c9986SGeorge Wilson */ 22672e4c9986SGeorge Wilson metaslab_group_histogram_remove(mg, msp); 22682e4c9986SGeorge Wilson 22690713e232SGeorge Wilson VERIFY0(space_map_allocated(msp->ms_sm)); 22700713e232SGeorge Wilson space_map_free(msp->ms_sm, tx); 22710713e232SGeorge Wilson space_map_close(msp->ms_sm); 22720713e232SGeorge Wilson msp->ms_sm = NULL; 22730713e232SGeorge Wilson mutex_exit(&msp->ms_lock); 227488ecc943SGeorge Wilson } 22752e4c9986SGeorge Wilson 22762e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 22772e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 22782e4c9986SGeorge Wilson for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 22792e4c9986SGeorge Wilson ASSERT0(mg->mg_histogram[i]); 22802e4c9986SGeorge Wilson 228188ecc943SGeorge Wilson } 228288ecc943SGeorge Wilson 228388ecc943SGeorge Wilson if (vd->vdev_ms_array) { 228488ecc943SGeorge Wilson (void) dmu_object_free(mos, vd->vdev_ms_array, tx); 228588ecc943SGeorge Wilson vd->vdev_ms_array = 0; 228688ecc943SGeorge Wilson } 2287215198a6SJoe Stein 2288215198a6SJoe Stein if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2289215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2290215198a6SJoe Stein vd->vdev_top_zap = 0; 2291215198a6SJoe Stein } 229288ecc943SGeorge Wilson dmu_tx_commit(tx); 229388ecc943SGeorge Wilson } 229488ecc943SGeorge Wilson 2295fa9e4066Sahrens void 2296fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 2297fa9e4066Sahrens { 2298fa9e4066Sahrens metaslab_t *msp; 229980eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2300fa9e4066Sahrens 230188ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 230288ecc943SGeorge Wilson 2303fa9e4066Sahrens while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2304fa9e4066Sahrens metaslab_sync_done(msp, txg); 230580eb36f2SGeorge Wilson 230680eb36f2SGeorge Wilson if (reassess) 230780eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 2308fa9e4066Sahrens } 2309fa9e4066Sahrens 2310fa9e4066Sahrens void 2311fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 2312fa9e4066Sahrens { 2313fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2314fa9e4066Sahrens vdev_t *lvd; 2315fa9e4066Sahrens metaslab_t *msp; 2316ecc2d604Sbonwick dmu_tx_t *tx; 2317fa9e4066Sahrens 231888ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 231988ecc943SGeorge Wilson 2320ecc2d604Sbonwick if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 2321ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 2322ecc2d604Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2323ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2324ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2325ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 2326ecc2d604Sbonwick vdev_config_dirty(vd); 2327ecc2d604Sbonwick dmu_tx_commit(tx); 2328ecc2d604Sbonwick } 2329fa9e4066Sahrens 23303f9d6ad7SLin Ling /* 23313f9d6ad7SLin Ling * Remove the metadata associated with this vdev once it's empty. 23323f9d6ad7SLin Ling */ 23333f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 233488ecc943SGeorge Wilson vdev_remove(vd, txg); 233588ecc943SGeorge Wilson 2336ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2337fa9e4066Sahrens metaslab_sync(msp, txg); 2338ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2339ecc2d604Sbonwick } 2340fa9e4066Sahrens 2341fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2342fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 2343fa9e4066Sahrens 2344fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2345fa9e4066Sahrens } 2346fa9e4066Sahrens 2347fa9e4066Sahrens uint64_t 2348fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2349fa9e4066Sahrens { 2350fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2351fa9e4066Sahrens } 2352fa9e4066Sahrens 23533d7072f8Seschrock /* 23543d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 23553d7072f8Seschrock * not be opened, and no I/O is attempted. 23563d7072f8Seschrock */ 2357fa9e4066Sahrens int 2358069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2359fa9e4066Sahrens { 23604b964adaSGeorge Wilson vdev_t *vd, *tvd; 2361fa9e4066Sahrens 23628f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 2363fa9e4066Sahrens 2364c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2365e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2366e14bb325SJeff Bonwick 23673d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 2368e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2369fa9e4066Sahrens 23704b964adaSGeorge Wilson tvd = vd->vdev_top; 23714b964adaSGeorge Wilson 2372069f55e2SEric Schrock /* 2373069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 2374069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 2375069f55e2SEric Schrock * were faulted. 2376069f55e2SEric Schrock */ 2377069f55e2SEric Schrock vd->vdev_label_aux = aux; 2378069f55e2SEric Schrock 23793d7072f8Seschrock /* 23803d7072f8Seschrock * Faulted state takes precedence over degraded. 23813d7072f8Seschrock */ 238298d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 23833d7072f8Seschrock vd->vdev_faulted = 1ULL; 23843d7072f8Seschrock vd->vdev_degraded = 0ULL; 2385069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 23863d7072f8Seschrock 23873d7072f8Seschrock /* 2388c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 2389c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 23903d7072f8Seschrock */ 23914b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 23923d7072f8Seschrock vd->vdev_degraded = 1ULL; 23933d7072f8Seschrock vd->vdev_faulted = 0ULL; 23943d7072f8Seschrock 23953d7072f8Seschrock /* 23963d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 23973d7072f8Seschrock * mark it degraded. 23983d7072f8Seschrock */ 23994b964adaSGeorge Wilson vdev_reopen(tvd); 24003d7072f8Seschrock 2401069f55e2SEric Schrock if (vdev_readable(vd)) 2402069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 24033d7072f8Seschrock } 24043d7072f8Seschrock 2405e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 24063d7072f8Seschrock } 24073d7072f8Seschrock 24083d7072f8Seschrock /* 24093d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 24103d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 24113d7072f8Seschrock * as I/O is concerned. 24123d7072f8Seschrock */ 24133d7072f8Seschrock int 2414069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 24153d7072f8Seschrock { 2416c5904d13Seschrock vdev_t *vd; 24170a4e9518Sgw 24188f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 24193d7072f8Seschrock 2420c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2421e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2422e14bb325SJeff Bonwick 24230e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 2424e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 24250e34b6a7Sbonwick 24263d7072f8Seschrock /* 24273d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 24283d7072f8Seschrock */ 2429e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 2430e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 24313d7072f8Seschrock 24323d7072f8Seschrock vd->vdev_degraded = 1ULL; 24333d7072f8Seschrock if (!vdev_is_dead(vd)) 24343d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2435069f55e2SEric Schrock aux); 24363d7072f8Seschrock 2437e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 24383d7072f8Seschrock } 24393d7072f8Seschrock 24403d7072f8Seschrock /* 2441f7170741SWill Andrews * Online the given vdev. 2442f7170741SWill Andrews * 2443f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2444f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 2445f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 2446f7170741SWill Andrews * events are generated if the device fails to open. 24473d7072f8Seschrock */ 24483d7072f8Seschrock int 2449e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 24503d7072f8Seschrock { 2451573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 245214372834SHans Rosenfeld boolean_t postevent = B_FALSE; 24533d7072f8Seschrock 24548f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 24553d7072f8Seschrock 2456c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2457e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 24583d7072f8Seschrock 24593d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 2460e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2461fa9e4066Sahrens 246214372834SHans Rosenfeld postevent = 246314372834SHans Rosenfeld (vd->vdev_offline == B_TRUE || vd->vdev_tmpoffline == B_TRUE) ? 246414372834SHans Rosenfeld B_TRUE : B_FALSE; 246514372834SHans Rosenfeld 2466573ca77eSGeorge Wilson tvd = vd->vdev_top; 2467fa9e4066Sahrens vd->vdev_offline = B_FALSE; 2468441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 2469e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 2470e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 2471573ca77eSGeorge Wilson 2472573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 2473573ca77eSGeorge Wilson if (!vd->vdev_aux) { 2474573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2475573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 2476573ca77eSGeorge Wilson } 2477573ca77eSGeorge Wilson 2478573ca77eSGeorge Wilson vdev_reopen(tvd); 24793d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 24803d7072f8Seschrock 2481573ca77eSGeorge Wilson if (!vd->vdev_aux) { 2482573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2483573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 2484573ca77eSGeorge Wilson } 2485573ca77eSGeorge Wilson 24863d7072f8Seschrock if (newstate) 24873d7072f8Seschrock *newstate = vd->vdev_state; 24883d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 24893d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 24903d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 24913d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 24923d7072f8Seschrock vd->vdev_unspare = B_TRUE; 2493fa9e4066Sahrens 2494573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 2495573ca77eSGeorge Wilson 2496573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 2497573ca77eSGeorge Wilson if (vd->vdev_aux) 2498573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 2499573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 2500573ca77eSGeorge Wilson } 250114372834SHans Rosenfeld 250214372834SHans Rosenfeld if (postevent) 250314372834SHans Rosenfeld spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE); 250414372834SHans Rosenfeld 25058ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 2506fa9e4066Sahrens } 2507fa9e4066Sahrens 2508a1521560SJeff Bonwick static int 2509a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 2510fa9e4066Sahrens { 2511e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 25128f18d1faSGeorge Wilson int error = 0; 25138f18d1faSGeorge Wilson uint64_t generation; 25148f18d1faSGeorge Wilson metaslab_group_t *mg; 25150a4e9518Sgw 25168f18d1faSGeorge Wilson top: 25178f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 2518fa9e4066Sahrens 2519c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2520e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2521fa9e4066Sahrens 25220e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 2523e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 25240e34b6a7Sbonwick 2525e6ca193dSGeorge Wilson tvd = vd->vdev_top; 25268f18d1faSGeorge Wilson mg = tvd->vdev_mg; 25278f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 2528e6ca193dSGeorge Wilson 2529fa9e4066Sahrens /* 2530ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 2531fa9e4066Sahrens */ 2532ecc2d604Sbonwick if (!vd->vdev_offline) { 2533ecc2d604Sbonwick /* 25348ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 2535e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 2536e6ca193dSGeorge Wilson * expendable. 2537ecc2d604Sbonwick */ 2538e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2539e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 2540e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2541fa9e4066Sahrens 25428f18d1faSGeorge Wilson /* 2543b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 2544b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 2545b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 2546b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 25478f18d1faSGeorge Wilson */ 25488f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 25498f18d1faSGeorge Wilson /* 25508f18d1faSGeorge Wilson * Prevent any future allocations. 25518f18d1faSGeorge Wilson */ 2552a1521560SJeff Bonwick metaslab_group_passivate(mg); 25538f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 25548f18d1faSGeorge Wilson 25551195e687SMark J Musante error = spa_offline_log(spa); 25568f18d1faSGeorge Wilson 25578f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 25588f18d1faSGeorge Wilson 25598f18d1faSGeorge Wilson /* 25608f18d1faSGeorge Wilson * Check to see if the config has changed. 25618f18d1faSGeorge Wilson */ 25628f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 2563a1521560SJeff Bonwick metaslab_group_activate(mg); 25648f18d1faSGeorge Wilson if (error) 25658f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 25668f18d1faSGeorge Wilson vd, error)); 25678f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 25688f18d1faSGeorge Wilson goto top; 25698f18d1faSGeorge Wilson } 2570fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 25718f18d1faSGeorge Wilson } 25728f18d1faSGeorge Wilson 2573ecc2d604Sbonwick /* 2574ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 2575e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 2576e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 2577e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 2578ecc2d604Sbonwick */ 2579ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 2580e6ca193dSGeorge Wilson vdev_reopen(tvd); 2581e6ca193dSGeorge Wilson 2582e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2583e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 2584ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 2585e6ca193dSGeorge Wilson vdev_reopen(tvd); 2586e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2587ecc2d604Sbonwick } 25888f18d1faSGeorge Wilson 25898f18d1faSGeorge Wilson /* 25908f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 25918f18d1faSGeorge Wilson * once we online the device it's open for business. 25928f18d1faSGeorge Wilson */ 25938f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 2594a1521560SJeff Bonwick metaslab_group_activate(mg); 2595fa9e4066Sahrens } 2596fa9e4066Sahrens 2597e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 2598ecc2d604Sbonwick 25998f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 2600fa9e4066Sahrens } 2601fa9e4066Sahrens 2602a1521560SJeff Bonwick int 2603a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 2604a1521560SJeff Bonwick { 2605a1521560SJeff Bonwick int error; 2606a1521560SJeff Bonwick 2607a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 2608a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 2609a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 2610a1521560SJeff Bonwick 2611a1521560SJeff Bonwick return (error); 2612a1521560SJeff Bonwick } 2613a1521560SJeff Bonwick 2614ea8dc4b6Seschrock /* 2615ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 2616ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 2617ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 2618ea8dc4b6Seschrock */ 2619ea8dc4b6Seschrock void 2620e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 2621fa9e4066Sahrens { 2622e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 2623e14bb325SJeff Bonwick 2624e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2625fa9e4066Sahrens 2626ea8dc4b6Seschrock if (vd == NULL) 2627e14bb325SJeff Bonwick vd = rvd; 2628fa9e4066Sahrens 2629ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 2630ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 2631ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 2632fa9e4066Sahrens 2633e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 2634e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 26353d7072f8Seschrock 26363d7072f8Seschrock /* 26378a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 26388a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 26398a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 26408a79c1b5Sek * written out to disk. 26413d7072f8Seschrock */ 2642e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 2643e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 26448a79c1b5Sek 2645096d22d4SEric Schrock /* 2646096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 2647096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 2648096d22d4SEric Schrock * still broken, we want to still post the ereport again. 2649096d22d4SEric Schrock */ 2650096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 2651096d22d4SEric Schrock 26524b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 2653e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 2654e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 2655e14bb325SJeff Bonwick 2656f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 26573d7072f8Seschrock 2658096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 2659096d22d4SEric Schrock 2660f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 2661e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 2662e14bb325SJeff Bonwick 2663e14bb325SJeff Bonwick if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 2664bb8b5132Sek spa_async_request(spa, SPA_ASYNC_RESILVER); 26653d7072f8Seschrock 26663d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 26673d7072f8Seschrock } 2668096d22d4SEric Schrock 2669096d22d4SEric Schrock /* 2670096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 2671096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 2672096d22d4SEric Schrock * done in response to the FMA fault. 2673096d22d4SEric Schrock */ 2674096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 2675096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2676096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 2677096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 2678fa9e4066Sahrens } 2679fa9e4066Sahrens 2680e14bb325SJeff Bonwick boolean_t 2681e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 26820a4e9518Sgw { 268388ecc943SGeorge Wilson /* 268488ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 268588ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 268688ecc943SGeorge Wilson * these types of devices in the various code paths. 268788ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 268888ecc943SGeorge Wilson * before issuing I/O to them. 268988ecc943SGeorge Wilson */ 269088ecc943SGeorge Wilson return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole || 269188ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 26920a4e9518Sgw } 26930a4e9518Sgw 2694e14bb325SJeff Bonwick boolean_t 2695e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 26960a4e9518Sgw { 2697e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 26980a4e9518Sgw } 26990a4e9518Sgw 2700e14bb325SJeff Bonwick boolean_t 2701e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 2702fa9e4066Sahrens { 2703e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 2704fa9e4066Sahrens } 2705fa9e4066Sahrens 2706a31e6787SGeorge Wilson boolean_t 2707a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 2708a31e6787SGeorge Wilson { 27098ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 27108ad4d6ddSJeff Bonwick 2711a31e6787SGeorge Wilson /* 27128ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 2713a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 2714a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 27158ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 27168ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 27178ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 2718a31e6787SGeorge Wilson */ 27198ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 27200f7643c7SGeorge Wilson !vd->vdev_cant_write && !vd->vdev_ishole && 27210f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 2722a31e6787SGeorge Wilson } 2723a31e6787SGeorge Wilson 2724e14bb325SJeff Bonwick boolean_t 2725e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 2726fa9e4066Sahrens { 2727e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 2728fa9e4066Sahrens 2729e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 2730e14bb325SJeff Bonwick return (B_FALSE); 2731fa9e4066Sahrens 2732e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 2733e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 2734fa9e4066Sahrens 2735e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 2736e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 2737fa9e4066Sahrens 2738e14bb325SJeff Bonwick return (B_TRUE); 2739fa9e4066Sahrens } 2740fa9e4066Sahrens 2741fa9e4066Sahrens /* 2742fa9e4066Sahrens * Get statistics for the given vdev. 2743fa9e4066Sahrens */ 2744fa9e4066Sahrens void 2745fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2746fa9e4066Sahrens { 27472e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 27482e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 2749c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 27502e4c9986SGeorge Wilson 27512e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2752fa9e4066Sahrens 2753fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 2754fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 2755fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2756fa9e4066Sahrens vs->vs_state = vd->vdev_state; 2757573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 2758573ca77eSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2759573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 2760c39a2aaeSGeorge Wilson /* 2761c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 2762c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 2763c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 2764c39a2aaeSGeorge Wilson */ 2765c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 2766c39a2aaeSGeorge Wilson vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize, 2767c39a2aaeSGeorge Wilson 1ULL << tvd->vdev_ms_shift); 2768c39a2aaeSGeorge Wilson } 27692986efa8SAlex Reece if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) { 27702e4c9986SGeorge Wilson vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 27712986efa8SAlex Reece } 2772fa9e4066Sahrens 2773fa9e4066Sahrens /* 2774fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 2775fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 2776fa9e4066Sahrens */ 2777fa9e4066Sahrens if (vd == rvd) { 2778e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 2779fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 2780fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 2781fa9e4066Sahrens 2782e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 2783fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 2784fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 2785fa9e4066Sahrens } 27863f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 2787fa9e4066Sahrens } 2788fa9e4066Sahrens } 27892e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 2790fa9e4066Sahrens } 2791fa9e4066Sahrens 2792fa94a07fSbrendan void 2793fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 2794fa94a07fSbrendan { 2795fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 2796fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 2797fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 2798fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 2799fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 2800fa94a07fSbrendan } 2801fa94a07fSbrendan 28023f9d6ad7SLin Ling void 28033f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 28043f9d6ad7SLin Ling { 28053f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 28063f9d6ad7SLin Ling 28073f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 28083f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 28093f9d6ad7SLin Ling 28103f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 28113f9d6ad7SLin Ling vs->vs_scan_processed = 0; 28123f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 28133f9d6ad7SLin Ling } 28143f9d6ad7SLin Ling 2815fa9e4066Sahrens void 2816e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 2817fa9e4066Sahrens { 28188ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 28198ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 2820e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2821fa9e4066Sahrens vdev_t *pvd; 2822fa9e4066Sahrens uint64_t txg = zio->io_txg; 2823fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 2824fa9e4066Sahrens zio_type_t type = zio->io_type; 2825fa9e4066Sahrens int flags = zio->io_flags; 2826fa9e4066Sahrens 2827e14bb325SJeff Bonwick /* 2828e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 2829e14bb325SJeff Bonwick */ 2830e14bb325SJeff Bonwick if (zio->io_gang_tree) 2831e14bb325SJeff Bonwick return; 2832e14bb325SJeff Bonwick 2833fa9e4066Sahrens if (zio->io_error == 0) { 2834e14bb325SJeff Bonwick /* 2835e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 2836e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 2837e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 2838e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 2839e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 2840e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 2841e14bb325SJeff Bonwick * remain zero, which is what we want.) 2842e14bb325SJeff Bonwick * 2843e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 2844e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 2845e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 2846e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 2847e14bb325SJeff Bonwick */ 2848e14bb325SJeff Bonwick if (vd == rvd) 2849e14bb325SJeff Bonwick return; 2850e14bb325SJeff Bonwick 2851e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 28528ad4d6ddSJeff Bonwick 28538ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 28548ad4d6ddSJeff Bonwick return; 28558ad4d6ddSJeff Bonwick 28568ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 28578ad4d6ddSJeff Bonwick 2858e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 285944ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 28603f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 28613f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 28623f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 28633f9d6ad7SLin Ling 28643f9d6ad7SLin Ling /* XXX cleanup? */ 28653f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 28663f9d6ad7SLin Ling atomic_add_64(processed, psize); 28673f9d6ad7SLin Ling vs->vs_scan_processed += psize; 28683f9d6ad7SLin Ling } 28693f9d6ad7SLin Ling 28708ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 2871e14bb325SJeff Bonwick vs->vs_self_healed += psize; 2872fa9e4066Sahrens } 28738ad4d6ddSJeff Bonwick 28748ad4d6ddSJeff Bonwick vs->vs_ops[type]++; 28758ad4d6ddSJeff Bonwick vs->vs_bytes[type] += psize; 28768ad4d6ddSJeff Bonwick 28778ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 2878fa9e4066Sahrens return; 2879fa9e4066Sahrens } 2880fa9e4066Sahrens 2881fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 2882fa9e4066Sahrens return; 2883fa9e4066Sahrens 28848956713aSEric Schrock /* 28858956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 28868956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 28878956713aSEric Schrock * hard errors, when in reality they can happen for any number of 28888956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 28898956713aSEric Schrock */ 28908956713aSEric Schrock if (zio->io_error == EIO && 28918956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 28928956713aSEric Schrock return; 28938956713aSEric Schrock 28948f18d1faSGeorge Wilson /* 28958f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 28968f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 28978f18d1faSGeorge Wilson * errors. 28988f18d1faSGeorge Wilson */ 28998f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 29008f18d1faSGeorge Wilson return; 29018f18d1faSGeorge Wilson 2902e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 2903b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 2904e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 2905e14bb325SJeff Bonwick vs->vs_checksum_errors++; 2906e14bb325SJeff Bonwick else 2907e14bb325SJeff Bonwick vs->vs_read_errors++; 2908fa9e4066Sahrens } 2909b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 2910e14bb325SJeff Bonwick vs->vs_write_errors++; 2911e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 2912fa9e4066Sahrens 29138ad4d6ddSJeff Bonwick if (type == ZIO_TYPE_WRITE && txg != 0 && 29148ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 291544ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 2916b24ab676SJeff Bonwick spa->spa_claiming)) { 29178ad4d6ddSJeff Bonwick /* 2918b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 2919b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 2920b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 2921b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 2922b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 2923b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 2924b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 2925b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 29268ad4d6ddSJeff Bonwick * 29278ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 29288ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 29298ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 29308ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 29318ad4d6ddSJeff Bonwick */ 29328ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 29338ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 293444ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 29358ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 29368ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 29378ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 2938b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 2939b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 2940b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2941b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 29428ad4d6ddSJeff Bonwick } 2943b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 29448ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 2945fa9e4066Sahrens return; 29468ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 29478ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 29488ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2949fa9e4066Sahrens } 29508ad4d6ddSJeff Bonwick if (vd != rvd) 29518ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2952fa9e4066Sahrens } 2953fa9e4066Sahrens } 2954fa9e4066Sahrens 2955fa9e4066Sahrens /* 2956b24ab676SJeff Bonwick * Update the in-core space usage stats for this vdev, its metaslab class, 2957b24ab676SJeff Bonwick * and the root vdev. 2958fa9e4066Sahrens */ 2959fa9e4066Sahrens void 2960b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 2961b24ab676SJeff Bonwick int64_t space_delta) 2962fa9e4066Sahrens { 296399653d4eSeschrock int64_t dspace_delta = space_delta; 29648654d025Sperrin spa_t *spa = vd->vdev_spa; 29658654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 2966b24ab676SJeff Bonwick metaslab_group_t *mg = vd->vdev_mg; 2967b24ab676SJeff Bonwick metaslab_class_t *mc = mg ? mg->mg_class : NULL; 2968fa9e4066Sahrens 29698654d025Sperrin ASSERT(vd == vd->vdev_top); 297099653d4eSeschrock 29718654d025Sperrin /* 29728654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 29738654d025Sperrin * factor. We must calculate this here and not at the root vdev 29748654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 29758654d025Sperrin * childrens', thus not accurate enough for us. 29768654d025Sperrin */ 29778654d025Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 2978e6ca193dSGeorge Wilson ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 29798654d025Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 29808654d025Sperrin vd->vdev_deflate_ratio; 29818654d025Sperrin 29828654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 29838654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 2984b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 29858654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 29868654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 29878654d025Sperrin 2988b24ab676SJeff Bonwick if (mc == spa_normal_class(spa)) { 2989fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 2990fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 2991b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 2992fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 2993fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 2994fa94a07fSbrendan } 2995b24ab676SJeff Bonwick 2996b24ab676SJeff Bonwick if (mc != NULL) { 2997b24ab676SJeff Bonwick ASSERT(rvd == vd->vdev_parent); 2998b24ab676SJeff Bonwick ASSERT(vd->vdev_ms_count != 0); 2999b24ab676SJeff Bonwick 3000b24ab676SJeff Bonwick metaslab_class_space_update(mc, 3001b24ab676SJeff Bonwick alloc_delta, defer_delta, space_delta, dspace_delta); 3002b24ab676SJeff Bonwick } 3003fa9e4066Sahrens } 3004fa9e4066Sahrens 3005fa9e4066Sahrens /* 3006fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3007fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3008fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3009fa9e4066Sahrens */ 3010fa9e4066Sahrens void 3011fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3012fa9e4066Sahrens { 3013fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3014fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3015fa9e4066Sahrens int c; 3016fa9e4066Sahrens 3017f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3018f9af39baSGeorge Wilson 3019c5904d13Seschrock /* 30206809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 30216809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3022c5904d13Seschrock */ 3023c5904d13Seschrock if (vd->vdev_aux != NULL) { 3024c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3025c5904d13Seschrock nvlist_t **aux; 3026c5904d13Seschrock uint_t naux; 3027c5904d13Seschrock 3028c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3029c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3030c5904d13Seschrock break; 3031c5904d13Seschrock } 3032c5904d13Seschrock 3033e14bb325SJeff Bonwick if (c == sav->sav_count) { 3034e14bb325SJeff Bonwick /* 3035e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3036e14bb325SJeff Bonwick */ 3037e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3038e14bb325SJeff Bonwick return; 3039e14bb325SJeff Bonwick } 3040e14bb325SJeff Bonwick 3041c5904d13Seschrock sav->sav_sync = B_TRUE; 3042c5904d13Seschrock 30436809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 30446809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 30456809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 30466809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 30476809eb4eSEric Schrock } 3048c5904d13Seschrock 3049c5904d13Seschrock ASSERT(c < naux); 3050c5904d13Seschrock 3051c5904d13Seschrock /* 3052c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 3053c5904d13Seschrock * sketchy, but it will work. 3054c5904d13Seschrock */ 3055c5904d13Seschrock nvlist_free(aux[c]); 30563f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3057c5904d13Seschrock 3058c5904d13Seschrock return; 3059c5904d13Seschrock } 3060c5904d13Seschrock 30615dabedeeSbonwick /* 3062e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 3063e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 3064e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 30655dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 30665dabedeeSbonwick */ 3067e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3068e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3069e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 30705dabedeeSbonwick 3071fa9e4066Sahrens if (vd == rvd) { 3072fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 3073fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 3074fa9e4066Sahrens } else { 3075fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 3076fa9e4066Sahrens 307788ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 307888ecc943SGeorge Wilson !vd->vdev_ishole) 3079e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 3080fa9e4066Sahrens } 3081fa9e4066Sahrens } 3082fa9e4066Sahrens 3083fa9e4066Sahrens void 3084fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 3085fa9e4066Sahrens { 30865dabedeeSbonwick spa_t *spa = vd->vdev_spa; 30875dabedeeSbonwick 3088e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3089e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3090e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 30915dabedeeSbonwick 3092e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3093e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 3094e14bb325SJeff Bonwick } 3095e14bb325SJeff Bonwick 3096e14bb325SJeff Bonwick /* 3097e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 3098e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3099e14bb325SJeff Bonwick * the state changes from larger config changes because they require 3100e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 3101e14bb325SJeff Bonwick */ 3102e14bb325SJeff Bonwick void 3103e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 3104e14bb325SJeff Bonwick { 3105e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3106e14bb325SJeff Bonwick 3107f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3108e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3109e14bb325SJeff Bonwick 3110e14bb325SJeff Bonwick /* 3111e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 3112e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 3113e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 3114e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 3115e14bb325SJeff Bonwick */ 3116e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3117e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3118e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3119e14bb325SJeff Bonwick 3120b24ab676SJeff Bonwick if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole) 3121e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 3122e14bb325SJeff Bonwick } 3123e14bb325SJeff Bonwick 3124e14bb325SJeff Bonwick void 3125e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 3126e14bb325SJeff Bonwick { 3127e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3128e14bb325SJeff Bonwick 3129e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3130e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3131e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3132e14bb325SJeff Bonwick 3133e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3134e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 3135fa9e4066Sahrens } 3136fa9e4066Sahrens 313732b87932Sek /* 313832b87932Sek * Propagate vdev state up from children to parent. 313932b87932Sek */ 314044cd46caSbillm void 314144cd46caSbillm vdev_propagate_state(vdev_t *vd) 314244cd46caSbillm { 31438ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 31448ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 314544cd46caSbillm int degraded = 0, faulted = 0; 314644cd46caSbillm int corrupted = 0; 314744cd46caSbillm vdev_t *child; 314844cd46caSbillm 31493d7072f8Seschrock if (vd->vdev_children > 0) { 3150573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 31513d7072f8Seschrock child = vd->vdev_child[c]; 315251ece835Seschrock 315388ecc943SGeorge Wilson /* 315488ecc943SGeorge Wilson * Don't factor holes into the decision. 315588ecc943SGeorge Wilson */ 315688ecc943SGeorge Wilson if (child->vdev_ishole) 315788ecc943SGeorge Wilson continue; 315888ecc943SGeorge Wilson 3159e14bb325SJeff Bonwick if (!vdev_readable(child) || 31608ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 316151ece835Seschrock /* 316251ece835Seschrock * Root special: if there is a top-level log 316351ece835Seschrock * device, treat the root vdev as if it were 316451ece835Seschrock * degraded. 316551ece835Seschrock */ 316651ece835Seschrock if (child->vdev_islog && vd == rvd) 316751ece835Seschrock degraded++; 316851ece835Seschrock else 316951ece835Seschrock faulted++; 317051ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 31713d7072f8Seschrock degraded++; 317251ece835Seschrock } 317344cd46caSbillm 31743d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 31753d7072f8Seschrock corrupted++; 31763d7072f8Seschrock } 317744cd46caSbillm 31783d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 31793d7072f8Seschrock 31803d7072f8Seschrock /* 3181e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 31823d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 31833d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 31843d7072f8Seschrock * replicas'. 31853d7072f8Seschrock */ 31863d7072f8Seschrock if (corrupted && vd == rvd && 31873d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 31883d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 31893d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 31903d7072f8Seschrock } 31913d7072f8Seschrock 319251ece835Seschrock if (vd->vdev_parent) 31933d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 319444cd46caSbillm } 319544cd46caSbillm 3196fa9e4066Sahrens /* 3197ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 3198ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 3199ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 3200ea8dc4b6Seschrock * 3201ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 3202ea8dc4b6Seschrock * generated. 3203fa9e4066Sahrens */ 3204fa9e4066Sahrens void 3205ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3206fa9e4066Sahrens { 3207560e6e96Seschrock uint64_t save_state; 3208c5904d13Seschrock spa_t *spa = vd->vdev_spa; 3209ea8dc4b6Seschrock 3210ea8dc4b6Seschrock if (state == vd->vdev_state) { 3211ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 3212fa9e4066Sahrens return; 3213ea8dc4b6Seschrock } 3214ea8dc4b6Seschrock 3215560e6e96Seschrock save_state = vd->vdev_state; 3216fa9e4066Sahrens 3217fa9e4066Sahrens vd->vdev_state = state; 3218fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 3219fa9e4066Sahrens 32203d7072f8Seschrock /* 32213d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 322298d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 322398d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 322498d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 322598d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 322698d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 322798d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 322898d1cbfeSGeorge Wilson * children. 322998d1cbfeSGeorge Wilson */ 323098d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 323198d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 32323d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 32333d7072f8Seschrock 3234069f55e2SEric Schrock /* 3235069f55e2SEric Schrock * If we have brought this vdev back into service, we need 3236069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 3237069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 3238069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 3239069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 3240069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 3241069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 3242069f55e2SEric Schrock */ 3243069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3244069f55e2SEric Schrock vd->vdev_prevstate != state) 3245069f55e2SEric Schrock zfs_post_state_change(spa, vd); 3246069f55e2SEric Schrock 32473d7072f8Seschrock if (vd->vdev_removed && 32483d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 32493d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 32503d7072f8Seschrock /* 32513d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 32523d7072f8Seschrock * device was previously marked removed and someone attempted to 32533d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 32543d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 32553d7072f8Seschrock * it is one of our special test online cases, which is only 32563d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 32573d7072f8Seschrock * fault. 32583d7072f8Seschrock */ 32593d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 32603d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 32613d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 32623d7072f8Seschrock vd->vdev_removed = B_TRUE; 32633d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 3264ea8dc4b6Seschrock /* 3265cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 3266cb04b873SMark J Musante * mark it as "not available", which signifies that it was 3267cb04b873SMark J Musante * never there to begin with. Failure to open such a device 3268cb04b873SMark J Musante * is not considered an error. 3269ea8dc4b6Seschrock */ 3270cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3271cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 3272560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 3273560e6e96Seschrock vd->vdev_not_present = 1; 3274560e6e96Seschrock 3275560e6e96Seschrock /* 3276560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 3277560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 3278560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 3279560e6e96Seschrock * want to post the ereport if the device was already in the 3280560e6e96Seschrock * CANT_OPEN state beforehand. 32813d7072f8Seschrock * 32823d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 32833d7072f8Seschrock * online the device in response to an insertion event. If we 32843d7072f8Seschrock * hit this case, then we have detected an insertion event for a 32853d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 32863d7072f8Seschrock * In this scenario, we don't post an ereport because we are 32873d7072f8Seschrock * about to replace the device, or attempt an online with 32883d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 3289560e6e96Seschrock */ 32903d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 32913d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 3292c5904d13Seschrock vd != spa->spa_root_vdev) { 3293ea8dc4b6Seschrock const char *class; 3294ea8dc4b6Seschrock 3295ea8dc4b6Seschrock switch (aux) { 3296ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 3297ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3298ea8dc4b6Seschrock break; 3299ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 3300ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3301ea8dc4b6Seschrock break; 3302ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 3303ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3304ea8dc4b6Seschrock break; 3305ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 3306ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3307ea8dc4b6Seschrock break; 3308ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 3309ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3310ea8dc4b6Seschrock break; 3311ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 3312ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3313ea8dc4b6Seschrock break; 3314ea8dc4b6Seschrock default: 3315ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3316ea8dc4b6Seschrock } 3317ea8dc4b6Seschrock 3318c5904d13Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3319ea8dc4b6Seschrock } 3320ea8dc4b6Seschrock 33213d7072f8Seschrock /* Erase any notion of persistent removed state */ 33223d7072f8Seschrock vd->vdev_removed = B_FALSE; 33233d7072f8Seschrock } else { 33243d7072f8Seschrock vd->vdev_removed = B_FALSE; 33253d7072f8Seschrock } 3326ea8dc4b6Seschrock 33278b33d774STim Haley if (!isopen && vd->vdev_parent) 33288b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 3329fa9e4066Sahrens } 333015e6edf1Sgw 333115e6edf1Sgw /* 333215e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 3333c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 3334c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 333515e6edf1Sgw */ 333615e6edf1Sgw boolean_t 333715e6edf1Sgw vdev_is_bootable(vdev_t *vd) 333815e6edf1Sgw { 333915e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 334015e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 334115e6edf1Sgw 334215e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 334315e6edf1Sgw vd->vdev_children > 1) { 334415e6edf1Sgw return (B_FALSE); 3345c8811bd3SToomas Soome } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 334615e6edf1Sgw return (B_FALSE); 334715e6edf1Sgw } 334815e6edf1Sgw } 334915e6edf1Sgw 3350573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 335115e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 335215e6edf1Sgw return (B_FALSE); 335315e6edf1Sgw } 335415e6edf1Sgw return (B_TRUE); 335515e6edf1Sgw } 3356e6ca193dSGeorge Wilson 335788ecc943SGeorge Wilson /* 335888ecc943SGeorge Wilson * Load the state from the original vdev tree (ovd) which 335988ecc943SGeorge Wilson * we've retrieved from the MOS config object. If the original 33604b964adaSGeorge Wilson * vdev was offline or faulted then we transfer that state to the 33614b964adaSGeorge Wilson * device in the current vdev tree (nvd). 336288ecc943SGeorge Wilson */ 3363e6ca193dSGeorge Wilson void 336488ecc943SGeorge Wilson vdev_load_log_state(vdev_t *nvd, vdev_t *ovd) 3365e6ca193dSGeorge Wilson { 336688ecc943SGeorge Wilson spa_t *spa = nvd->vdev_spa; 3367e6ca193dSGeorge Wilson 33684b964adaSGeorge Wilson ASSERT(nvd->vdev_top->vdev_islog); 336988ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 337088ecc943SGeorge Wilson ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid); 3371e6ca193dSGeorge Wilson 337288ecc943SGeorge Wilson for (int c = 0; c < nvd->vdev_children; c++) 337388ecc943SGeorge Wilson vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]); 3374e6ca193dSGeorge Wilson 33754b964adaSGeorge Wilson if (nvd->vdev_ops->vdev_op_leaf) { 3376e6ca193dSGeorge Wilson /* 33774b964adaSGeorge Wilson * Restore the persistent vdev state 3378e6ca193dSGeorge Wilson */ 337988ecc943SGeorge Wilson nvd->vdev_offline = ovd->vdev_offline; 33804b964adaSGeorge Wilson nvd->vdev_faulted = ovd->vdev_faulted; 33814b964adaSGeorge Wilson nvd->vdev_degraded = ovd->vdev_degraded; 33824b964adaSGeorge Wilson nvd->vdev_removed = ovd->vdev_removed; 3383e6ca193dSGeorge Wilson } 3384e6ca193dSGeorge Wilson } 3385573ca77eSGeorge Wilson 33864b964adaSGeorge Wilson /* 33874b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 33884b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 33894b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 33904b964adaSGeorge Wilson */ 33914b964adaSGeorge Wilson boolean_t 33924b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 33934b964adaSGeorge Wilson { 33944b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 33954b964adaSGeorge Wilson !vd->vdev_removed) 33964b964adaSGeorge Wilson return (B_TRUE); 33974b964adaSGeorge Wilson 33984b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 33994b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 34004b964adaSGeorge Wilson return (B_TRUE); 34014b964adaSGeorge Wilson 34024b964adaSGeorge Wilson return (B_FALSE); 34034b964adaSGeorge Wilson } 34044b964adaSGeorge Wilson 3405573ca77eSGeorge Wilson /* 3406573ca77eSGeorge Wilson * Expand a vdev if possible. 3407573ca77eSGeorge Wilson */ 3408573ca77eSGeorge Wilson void 3409573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 3410573ca77eSGeorge Wilson { 3411573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 3412573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3413573ca77eSGeorge Wilson 3414573ca77eSGeorge Wilson if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 3415573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 3416573ca77eSGeorge Wilson vdev_config_dirty(vd); 3417573ca77eSGeorge Wilson } 3418573ca77eSGeorge Wilson } 34191195e687SMark J Musante 34201195e687SMark J Musante /* 34211195e687SMark J Musante * Split a vdev. 34221195e687SMark J Musante */ 34231195e687SMark J Musante void 34241195e687SMark J Musante vdev_split(vdev_t *vd) 34251195e687SMark J Musante { 34261195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 34271195e687SMark J Musante 34281195e687SMark J Musante vdev_remove_child(pvd, vd); 34291195e687SMark J Musante vdev_compact_children(pvd); 34301195e687SMark J Musante 34311195e687SMark J Musante cvd = pvd->vdev_child[0]; 34321195e687SMark J Musante if (pvd->vdev_children == 1) { 34331195e687SMark J Musante vdev_remove_parent(cvd); 34341195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 34351195e687SMark J Musante } 34361195e687SMark J Musante vdev_propagate_state(cvd); 34371195e687SMark J Musante } 3438283b8460SGeorge.Wilson 3439283b8460SGeorge.Wilson void 3440283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 3441283b8460SGeorge.Wilson { 3442283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 3443283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 3444283b8460SGeorge.Wilson 3445283b8460SGeorge.Wilson vdev_deadman(cvd); 3446283b8460SGeorge.Wilson } 3447283b8460SGeorge.Wilson 3448283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 3449283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 3450283b8460SGeorge.Wilson 3451283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 345269962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 3453283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 3454283b8460SGeorge.Wilson zio_t *fio; 3455283b8460SGeorge.Wilson uint64_t delta; 3456283b8460SGeorge.Wilson 3457283b8460SGeorge.Wilson /* 3458283b8460SGeorge.Wilson * Look at the head of all the pending queues, 3459283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 3460283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 3461283b8460SGeorge.Wilson */ 346269962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 3463c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 3464c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 3465c55e05cbSMatthew Ahrens zfs_dbgmsg("SLOW IO: zio timestamp %lluns, " 3466c55e05cbSMatthew Ahrens "delta %lluns, last io %lluns", 3467283b8460SGeorge.Wilson fio->io_timestamp, delta, 3468283b8460SGeorge.Wilson vq->vq_io_complete_ts); 3469283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 3470283b8460SGeorge.Wilson "hung.", spa_name(spa)); 3471283b8460SGeorge.Wilson } 3472283b8460SGeorge.Wilson } 3473283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 3474283b8460SGeorge.Wilson } 3475283b8460SGeorge.Wilson } 3476