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. 245cabbc6bSPrashanth Sreenivasa * Copyright (c) 2011, 2018 by Delphix. All rights reserved. 255f368aefSYuri Pankov * Copyright 2017 Nexenta Systems, Inc. 26c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 27c8811bd3SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com> 28c1064fd7SJerry Jelinek * Copyright 2019 Joyent, Inc. 29663207adSDon Brady * Copyright (c) 2017, Intel Corporation. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <sys/zfs_context.h> 33ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 34fa9e4066Sahrens #include <sys/spa.h> 35fa9e4066Sahrens #include <sys/spa_impl.h> 365cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h> 37fa9e4066Sahrens #include <sys/dmu.h> 38fa9e4066Sahrens #include <sys/dmu_tx.h> 395cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h> 40fa9e4066Sahrens #include <sys/vdev_impl.h> 41fa9e4066Sahrens #include <sys/uberblock_impl.h> 42fa9e4066Sahrens #include <sys/metaslab.h> 43fa9e4066Sahrens #include <sys/metaslab_impl.h> 44fa9e4066Sahrens #include <sys/space_map.h> 450713e232SGeorge Wilson #include <sys/space_reftree.h> 46fa9e4066Sahrens #include <sys/zio.h> 47fa9e4066Sahrens #include <sys/zap.h> 48fa9e4066Sahrens #include <sys/fs/zfs.h> 49c5904d13Seschrock #include <sys/arc.h> 50e6ca193dSGeorge Wilson #include <sys/zil.h> 513f9d6ad7SLin Ling #include <sys/dsl_scan.h> 52770499e1SDan Kimmel #include <sys/abd.h> 53094e47e9SGeorge Wilson #include <sys/vdev_initialize.h> 54084fd14fSBrian Behlendorf #include <sys/vdev_trim.h> 55fa9e4066Sahrens 56fa9e4066Sahrens /* 57fa9e4066Sahrens * Virtual device management. 58fa9e4066Sahrens */ 59fa9e4066Sahrens 60fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = { 61fa9e4066Sahrens &vdev_root_ops, 62fa9e4066Sahrens &vdev_raidz_ops, 63fa9e4066Sahrens &vdev_mirror_ops, 64fa9e4066Sahrens &vdev_replacing_ops, 6599653d4eSeschrock &vdev_spare_ops, 66fa9e4066Sahrens &vdev_disk_ops, 67fa9e4066Sahrens &vdev_file_ops, 68fa9e4066Sahrens &vdev_missing_ops, 6988ecc943SGeorge Wilson &vdev_hole_ops, 705cabbc6bSPrashanth Sreenivasa &vdev_indirect_ops, 71fa9e4066Sahrens NULL 72fa9e4066Sahrens }; 73fa9e4066Sahrens 74088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */ 75088f3894Sahrens int zfs_scrub_limit = 10; 7605b2b3b8Smishra 77a0b03b16SSerapheim Dimitropoulos /* default target for number of metaslabs per top-level vdev */ 78a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_count = 200; 7986714001SSerapheim Dimitropoulos 80b4bf0cf0SDon Brady /* minimum number of metaslabs per top-level vdev */ 81a0b03b16SSerapheim Dimitropoulos int zfs_vdev_min_ms_count = 16; 8286714001SSerapheim Dimitropoulos 83b4bf0cf0SDon Brady /* practical upper limit of total metaslabs per top-level vdev */ 84a0b03b16SSerapheim Dimitropoulos int zfs_vdev_ms_count_limit = 1ULL << 17; 85b4bf0cf0SDon Brady 86b4bf0cf0SDon Brady /* lower limit for metaslab size (512M) */ 87a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_shift = 29; 8886714001SSerapheim Dimitropoulos 89a0b03b16SSerapheim Dimitropoulos /* upper limit for metaslab size (16G) */ 90a0b03b16SSerapheim Dimitropoulos int zfs_vdev_max_ms_shift = 34; 91b4bf0cf0SDon Brady 9286714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE; 9386714001SSerapheim Dimitropoulos 94bf3e216cSMatthew Ahrens /* 9586714001SSerapheim Dimitropoulos * Since the DTL space map of a vdev is not expected to have a lot of 9686714001SSerapheim Dimitropoulos * entries, we default its block size to 4K. 97bf3e216cSMatthew Ahrens */ 98*814dcd43SSerapheim Dimitropoulos int zfs_vdev_dtl_sm_blksz = (1 << 12); 99bf3e216cSMatthew Ahrens 10086714001SSerapheim Dimitropoulos /* 10186714001SSerapheim Dimitropoulos * vdev-wide space maps that have lots of entries written to them at 10286714001SSerapheim Dimitropoulos * the end of each transaction can benefit from a higher I/O bandwidth 10386714001SSerapheim Dimitropoulos * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 10486714001SSerapheim Dimitropoulos */ 105*814dcd43SSerapheim Dimitropoulos int zfs_vdev_standard_sm_blksz = (1 << 17); 1066f793812SPavel Zakharov 10793a1902eSMatthew Ahrens int zfs_ashift_min; 10893a1902eSMatthew Ahrens 1093ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 1103ee8c80cSPavel Zakharov void 1113ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 1123ee8c80cSPavel Zakharov { 1133ee8c80cSPavel Zakharov va_list adx; 1143ee8c80cSPavel Zakharov char buf[256]; 1153ee8c80cSPavel Zakharov 1163ee8c80cSPavel Zakharov va_start(adx, fmt); 1173ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 1183ee8c80cSPavel Zakharov va_end(adx); 1193ee8c80cSPavel Zakharov 1203ee8c80cSPavel Zakharov if (vd->vdev_path != NULL) { 1213ee8c80cSPavel Zakharov zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 1223ee8c80cSPavel Zakharov vd->vdev_path, buf); 1233ee8c80cSPavel Zakharov } else { 1243ee8c80cSPavel Zakharov zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 1253ee8c80cSPavel Zakharov vd->vdev_ops->vdev_op_type, 1263ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_id, 1273ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_guid, buf); 1283ee8c80cSPavel Zakharov } 1293ee8c80cSPavel Zakharov } 1303ee8c80cSPavel Zakharov 1316f793812SPavel Zakharov void 1326f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 1336f793812SPavel Zakharov { 1346f793812SPavel Zakharov char state[20]; 1356f793812SPavel Zakharov 1366f793812SPavel Zakharov if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 1376f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 1386f793812SPavel Zakharov vd->vdev_ops->vdev_op_type); 1396f793812SPavel Zakharov return; 1406f793812SPavel Zakharov } 1416f793812SPavel Zakharov 1426f793812SPavel Zakharov switch (vd->vdev_state) { 1436f793812SPavel Zakharov case VDEV_STATE_UNKNOWN: 1446f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "unknown"); 1456f793812SPavel Zakharov break; 1466f793812SPavel Zakharov case VDEV_STATE_CLOSED: 1476f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "closed"); 1486f793812SPavel Zakharov break; 1496f793812SPavel Zakharov case VDEV_STATE_OFFLINE: 1506f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "offline"); 1516f793812SPavel Zakharov break; 1526f793812SPavel Zakharov case VDEV_STATE_REMOVED: 1536f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "removed"); 1546f793812SPavel Zakharov break; 1556f793812SPavel Zakharov case VDEV_STATE_CANT_OPEN: 1566f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "can't open"); 1576f793812SPavel Zakharov break; 1586f793812SPavel Zakharov case VDEV_STATE_FAULTED: 1596f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "faulted"); 1606f793812SPavel Zakharov break; 1616f793812SPavel Zakharov case VDEV_STATE_DEGRADED: 1626f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "degraded"); 1636f793812SPavel Zakharov break; 1646f793812SPavel Zakharov case VDEV_STATE_HEALTHY: 1656f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "healthy"); 1666f793812SPavel Zakharov break; 1676f793812SPavel Zakharov default: 1686f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "<state %u>", 1696f793812SPavel Zakharov (uint_t)vd->vdev_state); 1706f793812SPavel Zakharov } 1716f793812SPavel Zakharov 1726f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 173c7a7b2faSAndriy Gapon "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type, 1746f793812SPavel Zakharov vd->vdev_islog ? " (log)" : "", 1756f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 1766f793812SPavel Zakharov vd->vdev_path ? vd->vdev_path : "N/A", state); 1776f793812SPavel Zakharov 1786f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) 1796f793812SPavel Zakharov vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 1806f793812SPavel Zakharov } 1816f793812SPavel Zakharov 182fa9e4066Sahrens /* 183fa9e4066Sahrens * Given a vdev type, return the appropriate ops vector. 184fa9e4066Sahrens */ 185fa9e4066Sahrens static vdev_ops_t * 186fa9e4066Sahrens vdev_getops(const char *type) 187fa9e4066Sahrens { 188fa9e4066Sahrens vdev_ops_t *ops, **opspp; 189fa9e4066Sahrens 190fa9e4066Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 191fa9e4066Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 192fa9e4066Sahrens break; 193fa9e4066Sahrens 194fa9e4066Sahrens return (ops); 195fa9e4066Sahrens } 196fa9e4066Sahrens 197663207adSDon Brady /* 198663207adSDon Brady * Derive the enumerated alloction bias from string input. 199663207adSDon Brady * String origin is either the per-vdev zap or zpool(1M). 200663207adSDon Brady */ 201663207adSDon Brady static vdev_alloc_bias_t 202663207adSDon Brady vdev_derive_alloc_bias(const char *bias) 203663207adSDon Brady { 204663207adSDon Brady vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 205663207adSDon Brady 206663207adSDon Brady if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0) 207663207adSDon Brady alloc_bias = VDEV_BIAS_LOG; 208663207adSDon Brady else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) 209663207adSDon Brady alloc_bias = VDEV_BIAS_SPECIAL; 210663207adSDon Brady else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) 211663207adSDon Brady alloc_bias = VDEV_BIAS_DEDUP; 212663207adSDon Brady 213663207adSDon Brady return (alloc_bias); 214663207adSDon Brady } 215663207adSDon Brady 216094e47e9SGeorge Wilson /* ARGSUSED */ 217094e47e9SGeorge Wilson void 218094e47e9SGeorge Wilson vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res) 219094e47e9SGeorge Wilson { 220094e47e9SGeorge Wilson res->rs_start = in->rs_start; 221094e47e9SGeorge Wilson res->rs_end = in->rs_end; 222094e47e9SGeorge Wilson } 223094e47e9SGeorge Wilson 224fa9e4066Sahrens /* 225fa9e4066Sahrens * Default asize function: return the MAX of psize with the asize of 226fa9e4066Sahrens * all children. This is what's used by anything other than RAID-Z. 227fa9e4066Sahrens */ 228fa9e4066Sahrens uint64_t 229fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 230fa9e4066Sahrens { 231ecc2d604Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 232fa9e4066Sahrens uint64_t csize; 233fa9e4066Sahrens 234573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 235fa9e4066Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 236fa9e4066Sahrens asize = MAX(asize, csize); 237fa9e4066Sahrens } 238fa9e4066Sahrens 239fa9e4066Sahrens return (asize); 240fa9e4066Sahrens } 241fa9e4066Sahrens 2422a79c5feSlling /* 243573ca77eSGeorge Wilson * Get the minimum allocatable size. We define the allocatable size as 244573ca77eSGeorge Wilson * the vdev's asize rounded to the nearest metaslab. This allows us to 245573ca77eSGeorge Wilson * replace or attach devices which don't have the same physical size but 246573ca77eSGeorge Wilson * can still satisfy the same number of allocations. 2472a79c5feSlling */ 2482a79c5feSlling uint64_t 249573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd) 2502a79c5feSlling { 251573ca77eSGeorge Wilson vdev_t *pvd = vd->vdev_parent; 2522a79c5feSlling 253573ca77eSGeorge Wilson /* 2544263d13fSGeorge Wilson * If our parent is NULL (inactive spare or cache) or is the root, 255573ca77eSGeorge Wilson * just return our own asize. 256573ca77eSGeorge Wilson */ 257573ca77eSGeorge Wilson if (pvd == NULL) 258573ca77eSGeorge Wilson return (vd->vdev_asize); 2592a79c5feSlling 2602a79c5feSlling /* 261573ca77eSGeorge Wilson * The top-level vdev just returns the allocatable size rounded 262573ca77eSGeorge Wilson * to the nearest metaslab. 2632a79c5feSlling */ 264573ca77eSGeorge Wilson if (vd == vd->vdev_top) 265573ca77eSGeorge Wilson return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 2662a79c5feSlling 267573ca77eSGeorge Wilson /* 268573ca77eSGeorge Wilson * The allocatable space for a raidz vdev is N * sizeof(smallest child), 269573ca77eSGeorge Wilson * so each child must provide at least 1/Nth of its asize. 270573ca77eSGeorge Wilson */ 271573ca77eSGeorge Wilson if (pvd->vdev_ops == &vdev_raidz_ops) 272c040c10cSSteven Hartland return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 273c040c10cSSteven Hartland pvd->vdev_children); 2742a79c5feSlling 275573ca77eSGeorge Wilson return (pvd->vdev_min_asize); 276573ca77eSGeorge Wilson } 2772a79c5feSlling 278573ca77eSGeorge Wilson void 279573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd) 280573ca77eSGeorge Wilson { 281573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 282573ca77eSGeorge Wilson 283573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 284573ca77eSGeorge Wilson vdev_set_min_asize(vd->vdev_child[c]); 2852a79c5feSlling } 2862a79c5feSlling 287fa9e4066Sahrens vdev_t * 288fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 289fa9e4066Sahrens { 290fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 291fa9e4066Sahrens 292e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 293e05725b1Sbonwick 294088f3894Sahrens if (vdev < rvd->vdev_children) { 295088f3894Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 296fa9e4066Sahrens return (rvd->vdev_child[vdev]); 297088f3894Sahrens } 298fa9e4066Sahrens 299fa9e4066Sahrens return (NULL); 300fa9e4066Sahrens } 301fa9e4066Sahrens 302fa9e4066Sahrens vdev_t * 303fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 304fa9e4066Sahrens { 305fa9e4066Sahrens vdev_t *mvd; 306fa9e4066Sahrens 3070e34b6a7Sbonwick if (vd->vdev_guid == guid) 308fa9e4066Sahrens return (vd); 309fa9e4066Sahrens 310573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 311fa9e4066Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 312fa9e4066Sahrens NULL) 313fa9e4066Sahrens return (mvd); 314fa9e4066Sahrens 315fa9e4066Sahrens return (NULL); 316fa9e4066Sahrens } 317fa9e4066Sahrens 31812380e1eSArne Jansen static int 31912380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd) 32012380e1eSArne Jansen { 32112380e1eSArne Jansen int n = 0; 32212380e1eSArne Jansen 32312380e1eSArne Jansen if (vd->vdev_ops->vdev_op_leaf) 32412380e1eSArne Jansen return (1); 32512380e1eSArne Jansen 32612380e1eSArne Jansen for (int c = 0; c < vd->vdev_children; c++) 32712380e1eSArne Jansen n += vdev_count_leaves_impl(vd->vdev_child[c]); 32812380e1eSArne Jansen 32912380e1eSArne Jansen return (n); 33012380e1eSArne Jansen } 33112380e1eSArne Jansen 33212380e1eSArne Jansen int 33312380e1eSArne Jansen vdev_count_leaves(spa_t *spa) 33412380e1eSArne Jansen { 33512380e1eSArne Jansen return (vdev_count_leaves_impl(spa->spa_root_vdev)); 33612380e1eSArne Jansen } 33712380e1eSArne Jansen 338fa9e4066Sahrens void 339fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 340fa9e4066Sahrens { 341fa9e4066Sahrens size_t oldsize, newsize; 342fa9e4066Sahrens uint64_t id = cvd->vdev_id; 343fa9e4066Sahrens vdev_t **newchild; 34481cd5c55SMatthew Ahrens spa_t *spa = cvd->vdev_spa; 345fa9e4066Sahrens 34681cd5c55SMatthew Ahrens ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 347fa9e4066Sahrens ASSERT(cvd->vdev_parent == NULL); 348fa9e4066Sahrens 349fa9e4066Sahrens cvd->vdev_parent = pvd; 350fa9e4066Sahrens 351fa9e4066Sahrens if (pvd == NULL) 352fa9e4066Sahrens return; 353fa9e4066Sahrens 354fa9e4066Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 355fa9e4066Sahrens 356fa9e4066Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 357fa9e4066Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 358fa9e4066Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 359fa9e4066Sahrens 360fa9e4066Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 361fa9e4066Sahrens if (pvd->vdev_child != NULL) { 362fa9e4066Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 363fa9e4066Sahrens kmem_free(pvd->vdev_child, oldsize); 364fa9e4066Sahrens } 365fa9e4066Sahrens 366fa9e4066Sahrens pvd->vdev_child = newchild; 367fa9e4066Sahrens pvd->vdev_child[id] = cvd; 368fa9e4066Sahrens 369fa9e4066Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 370fa9e4066Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 371fa9e4066Sahrens 372fa9e4066Sahrens /* 373fa9e4066Sahrens * Walk up all ancestors to update guid sum. 374fa9e4066Sahrens */ 375fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 376fa9e4066Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 377e0f1c0afSOlaf Faaland 378e0f1c0afSOlaf Faaland if (cvd->vdev_ops->vdev_op_leaf) { 379e0f1c0afSOlaf Faaland list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd); 380e0f1c0afSOlaf Faaland cvd->vdev_spa->spa_leaf_list_gen++; 381e0f1c0afSOlaf Faaland } 382fa9e4066Sahrens } 383fa9e4066Sahrens 384fa9e4066Sahrens void 385fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 386fa9e4066Sahrens { 387fa9e4066Sahrens int c; 388fa9e4066Sahrens uint_t id = cvd->vdev_id; 389fa9e4066Sahrens 390fa9e4066Sahrens ASSERT(cvd->vdev_parent == pvd); 391fa9e4066Sahrens 392fa9e4066Sahrens if (pvd == NULL) 393fa9e4066Sahrens return; 394fa9e4066Sahrens 395fa9e4066Sahrens ASSERT(id < pvd->vdev_children); 396fa9e4066Sahrens ASSERT(pvd->vdev_child[id] == cvd); 397fa9e4066Sahrens 398fa9e4066Sahrens pvd->vdev_child[id] = NULL; 399fa9e4066Sahrens cvd->vdev_parent = NULL; 400fa9e4066Sahrens 401fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) 402fa9e4066Sahrens if (pvd->vdev_child[c]) 403fa9e4066Sahrens break; 404fa9e4066Sahrens 405fa9e4066Sahrens if (c == pvd->vdev_children) { 406fa9e4066Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 407fa9e4066Sahrens pvd->vdev_child = NULL; 408fa9e4066Sahrens pvd->vdev_children = 0; 409fa9e4066Sahrens } 410fa9e4066Sahrens 411e0f1c0afSOlaf Faaland if (cvd->vdev_ops->vdev_op_leaf) { 412e0f1c0afSOlaf Faaland spa_t *spa = cvd->vdev_spa; 413e0f1c0afSOlaf Faaland list_remove(&spa->spa_leaf_list, cvd); 414e0f1c0afSOlaf Faaland spa->spa_leaf_list_gen++; 415e0f1c0afSOlaf Faaland } 416e0f1c0afSOlaf Faaland 417fa9e4066Sahrens /* 418fa9e4066Sahrens * Walk up all ancestors to update guid sum. 419fa9e4066Sahrens */ 420fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 421fa9e4066Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 422fa9e4066Sahrens } 423fa9e4066Sahrens 424fa9e4066Sahrens /* 425fa9e4066Sahrens * Remove any holes in the child array. 426fa9e4066Sahrens */ 427fa9e4066Sahrens void 428fa9e4066Sahrens vdev_compact_children(vdev_t *pvd) 429fa9e4066Sahrens { 430fa9e4066Sahrens vdev_t **newchild, *cvd; 431fa9e4066Sahrens int oldc = pvd->vdev_children; 432573ca77eSGeorge Wilson int newc; 433fa9e4066Sahrens 434e14bb325SJeff Bonwick ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 435fa9e4066Sahrens 436573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) 437fa9e4066Sahrens if (pvd->vdev_child[c]) 438fa9e4066Sahrens newc++; 439fa9e4066Sahrens 440fa9e4066Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 441fa9e4066Sahrens 442573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) { 443fa9e4066Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 444fa9e4066Sahrens newchild[newc] = cvd; 445fa9e4066Sahrens cvd->vdev_id = newc++; 446fa9e4066Sahrens } 447fa9e4066Sahrens } 448fa9e4066Sahrens 449fa9e4066Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 450fa9e4066Sahrens pvd->vdev_child = newchild; 451fa9e4066Sahrens pvd->vdev_children = newc; 452fa9e4066Sahrens } 453fa9e4066Sahrens 454fa9e4066Sahrens /* 455fa9e4066Sahrens * Allocate and minimally initialize a vdev_t. 456fa9e4066Sahrens */ 45788ecc943SGeorge Wilson vdev_t * 458fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 459fa9e4066Sahrens { 460fa9e4066Sahrens vdev_t *vd; 4615cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 462fa9e4066Sahrens 463fa9e4066Sahrens vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 4645cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 465fa9e4066Sahrens 4660e34b6a7Sbonwick if (spa->spa_root_vdev == NULL) { 4670e34b6a7Sbonwick ASSERT(ops == &vdev_root_ops); 4680e34b6a7Sbonwick spa->spa_root_vdev = vd; 469e9103aaeSGarrett D'Amore spa->spa_load_guid = spa_generate_guid(NULL); 4700e34b6a7Sbonwick } 4710e34b6a7Sbonwick 47288ecc943SGeorge Wilson if (guid == 0 && ops != &vdev_hole_ops) { 4730e34b6a7Sbonwick if (spa->spa_root_vdev == vd) { 4740e34b6a7Sbonwick /* 4750e34b6a7Sbonwick * The root vdev's guid will also be the pool guid, 4760e34b6a7Sbonwick * which must be unique among all pools. 4770e34b6a7Sbonwick */ 4781195e687SMark J Musante guid = spa_generate_guid(NULL); 4790e34b6a7Sbonwick } else { 4800e34b6a7Sbonwick /* 4810e34b6a7Sbonwick * Any other vdev's guid must be unique within the pool. 4820e34b6a7Sbonwick */ 4831195e687SMark J Musante guid = spa_generate_guid(spa); 4840e34b6a7Sbonwick } 4850e34b6a7Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 4860e34b6a7Sbonwick } 4870e34b6a7Sbonwick 488fa9e4066Sahrens vd->vdev_spa = spa; 489fa9e4066Sahrens vd->vdev_id = id; 490fa9e4066Sahrens vd->vdev_guid = guid; 491fa9e4066Sahrens vd->vdev_guid_sum = guid; 492fa9e4066Sahrens vd->vdev_ops = ops; 493fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 49488ecc943SGeorge Wilson vd->vdev_ishole = (ops == &vdev_hole_ops); 4955cabbc6bSPrashanth Sreenivasa vic->vic_prev_indirect_vdev = UINT64_MAX; 4965cabbc6bSPrashanth Sreenivasa 4975cabbc6bSPrashanth Sreenivasa rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 4985cabbc6bSPrashanth Sreenivasa mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 4995cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); 500fa9e4066Sahrens 501084fd14fSBrian Behlendorf list_link_init(&vd->vdev_initialize_node); 502e0f1c0afSOlaf Faaland list_link_init(&vd->vdev_leaf_node); 503084fd14fSBrian Behlendorf list_link_init(&vd->vdev_trim_node); 504fa9e4066Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 5055ad82045Snd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 506e14bb325SJeff Bonwick mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 507a3874b8bSToomas Soome mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL); 508094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); 509094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); 510094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); 511094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); 512084fd14fSBrian Behlendorf mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL); 513084fd14fSBrian Behlendorf mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL); 514084fd14fSBrian Behlendorf mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL); 515084fd14fSBrian Behlendorf cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL); 516084fd14fSBrian Behlendorf cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL); 517084fd14fSBrian Behlendorf cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL); 518094e47e9SGeorge Wilson 5198ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 5205cabbc6bSPrashanth Sreenivasa vd->vdev_dtl[t] = range_tree_create(NULL, NULL); 5218ad4d6ddSJeff Bonwick } 522b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_ms_list, spa, 523fa9e4066Sahrens offsetof(struct metaslab, ms_txg_node)); 524b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_dtl_list, spa, 525fa9e4066Sahrens offsetof(struct vdev, vdev_dtl_node)); 526fa9e4066Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 5273d7072f8Seschrock vdev_queue_init(vd); 5283d7072f8Seschrock vdev_cache_init(vd); 529fa9e4066Sahrens 530fa9e4066Sahrens return (vd); 531fa9e4066Sahrens } 532fa9e4066Sahrens 533fa9e4066Sahrens /* 534fa9e4066Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 535fa9e4066Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 536fa9e4066Sahrens * different for each case. 537fa9e4066Sahrens */ 53899653d4eSeschrock int 53999653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 54099653d4eSeschrock int alloctype) 541fa9e4066Sahrens { 542fa9e4066Sahrens vdev_ops_t *ops; 543fa9e4066Sahrens char *type; 5448654d025Sperrin uint64_t guid = 0, islog, nparity; 545fa9e4066Sahrens vdev_t *vd; 5465cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 547663207adSDon Brady vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 548663207adSDon Brady boolean_t top_level = (parent && !parent->vdev_parent); 549fa9e4066Sahrens 550e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 551fa9e4066Sahrens 552fa9e4066Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 553be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 554fa9e4066Sahrens 555fa9e4066Sahrens if ((ops = vdev_getops(type)) == NULL) 556be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 557fa9e4066Sahrens 558fa9e4066Sahrens /* 559fa9e4066Sahrens * If this is a load, get the vdev guid from the nvlist. 560fa9e4066Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 561fa9e4066Sahrens */ 562fa9e4066Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 563fa9e4066Sahrens uint64_t label_id; 564fa9e4066Sahrens 565fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 566fa9e4066Sahrens label_id != id) 567be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 568fa9e4066Sahrens 569fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 570be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 57199653d4eSeschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 57299653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 573be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 574fa94a07fSbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 575fa94a07fSbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 576be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 57721ecdf64SLin Ling } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 57821ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 579be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 580fa9e4066Sahrens } 581fa9e4066Sahrens 58299653d4eSeschrock /* 58399653d4eSeschrock * The first allocated vdev must be of type 'root'. 58499653d4eSeschrock */ 58599653d4eSeschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 586be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 58799653d4eSeschrock 5888654d025Sperrin /* 5898654d025Sperrin * Determine whether we're a log vdev. 5908654d025Sperrin */ 5918654d025Sperrin islog = 0; 5928654d025Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 593990b4856Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 594be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 595fa9e4066Sahrens 59688ecc943SGeorge Wilson if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 597be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 59888ecc943SGeorge Wilson 59999653d4eSeschrock /* 6008654d025Sperrin * Set the nparity property for RAID-Z vdevs. 60199653d4eSeschrock */ 6028654d025Sperrin nparity = -1ULL; 60399653d4eSeschrock if (ops == &vdev_raidz_ops) { 60499653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 6058654d025Sperrin &nparity) == 0) { 606b24ab676SJeff Bonwick if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 607be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 60899653d4eSeschrock /* 609f94275ceSAdam Leventhal * Previous versions could only support 1 or 2 parity 610f94275ceSAdam Leventhal * device. 61199653d4eSeschrock */ 612f94275ceSAdam Leventhal if (nparity > 1 && 613f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ2) 614be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 615f94275ceSAdam Leventhal if (nparity > 2 && 616f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ3) 617be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 61899653d4eSeschrock } else { 61999653d4eSeschrock /* 62099653d4eSeschrock * We require the parity to be specified for SPAs that 62199653d4eSeschrock * support multiple parity levels. 62299653d4eSeschrock */ 623f94275ceSAdam Leventhal if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 624be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 62599653d4eSeschrock /* 62699653d4eSeschrock * Otherwise, we default to 1 parity device for RAID-Z. 62799653d4eSeschrock */ 6288654d025Sperrin nparity = 1; 62999653d4eSeschrock } 63099653d4eSeschrock } else { 6318654d025Sperrin nparity = 0; 63299653d4eSeschrock } 6338654d025Sperrin ASSERT(nparity != -1ULL); 6348654d025Sperrin 635663207adSDon Brady /* 636663207adSDon Brady * If creating a top-level vdev, check for allocation classes input 637663207adSDon Brady */ 638663207adSDon Brady if (top_level && alloctype == VDEV_ALLOC_ADD) { 639663207adSDon Brady char *bias; 640663207adSDon Brady 641663207adSDon Brady if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 642663207adSDon Brady &bias) == 0) { 643663207adSDon Brady alloc_bias = vdev_derive_alloc_bias(bias); 644663207adSDon Brady 645663207adSDon Brady /* spa_vdev_add() expects feature to be enabled */ 646c1064fd7SJerry Jelinek if (alloc_bias != VDEV_BIAS_LOG && 647c1064fd7SJerry Jelinek spa->spa_load_state != SPA_LOAD_CREATE && 648663207adSDon Brady !spa_feature_is_enabled(spa, 649663207adSDon Brady SPA_FEATURE_ALLOCATION_CLASSES)) { 650663207adSDon Brady return (SET_ERROR(ENOTSUP)); 651663207adSDon Brady } 652663207adSDon Brady } 653663207adSDon Brady } 654663207adSDon Brady 6558654d025Sperrin vd = vdev_alloc_common(spa, id, guid, ops); 6565cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 6578654d025Sperrin 6588654d025Sperrin vd->vdev_islog = islog; 6598654d025Sperrin vd->vdev_nparity = nparity; 660663207adSDon Brady if (top_level && alloc_bias != VDEV_BIAS_NONE) 661663207adSDon Brady vd->vdev_alloc_bias = alloc_bias; 6628654d025Sperrin 6638654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 6648654d025Sperrin vd->vdev_path = spa_strdup(vd->vdev_path); 6658654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 6668654d025Sperrin vd->vdev_devid = spa_strdup(vd->vdev_devid); 6678654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 6688654d025Sperrin &vd->vdev_physpath) == 0) 6698654d025Sperrin vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 6706809eb4eSEric Schrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 6716809eb4eSEric Schrock vd->vdev_fru = spa_strdup(vd->vdev_fru); 67299653d4eSeschrock 673afefbcddSeschrock /* 674afefbcddSeschrock * Set the whole_disk property. If it's not specified, leave the value 675afefbcddSeschrock * as -1. 676afefbcddSeschrock */ 677afefbcddSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 678afefbcddSeschrock &vd->vdev_wholedisk) != 0) 679afefbcddSeschrock vd->vdev_wholedisk = -1ULL; 680afefbcddSeschrock 6815cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_mapping_object); 6825cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 6835cabbc6bSPrashanth Sreenivasa &vic->vic_mapping_object); 6845cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_births_object); 6855cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 6865cabbc6bSPrashanth Sreenivasa &vic->vic_births_object); 6875cabbc6bSPrashanth Sreenivasa ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 6885cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 6895cabbc6bSPrashanth Sreenivasa &vic->vic_prev_indirect_vdev); 6905cabbc6bSPrashanth Sreenivasa 691ea8dc4b6Seschrock /* 692ea8dc4b6Seschrock * Look for the 'not present' flag. This will only be set if the device 693ea8dc4b6Seschrock * was not present at the time of import. 694ea8dc4b6Seschrock */ 6956809eb4eSEric Schrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 6966809eb4eSEric Schrock &vd->vdev_not_present); 697ea8dc4b6Seschrock 698ecc2d604Sbonwick /* 699ecc2d604Sbonwick * Get the alignment requirement. 700ecc2d604Sbonwick */ 701ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 702ecc2d604Sbonwick 70388ecc943SGeorge Wilson /* 70488ecc943SGeorge Wilson * Retrieve the vdev creation time. 70588ecc943SGeorge Wilson */ 70688ecc943SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 70788ecc943SGeorge Wilson &vd->vdev_crtxg); 70888ecc943SGeorge Wilson 709fa9e4066Sahrens /* 710fa9e4066Sahrens * If we're a top-level vdev, try to load the allocation parameters. 711fa9e4066Sahrens */ 712663207adSDon Brady if (top_level && 7131195e687SMark J Musante (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 714fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 715fa9e4066Sahrens &vd->vdev_ms_array); 716fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 717fa9e4066Sahrens &vd->vdev_ms_shift); 718fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 719fa9e4066Sahrens &vd->vdev_asize); 7203f9d6ad7SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 7213f9d6ad7SLin Ling &vd->vdev_removing); 722215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 723215198a6SJoe Stein &vd->vdev_top_zap); 724215198a6SJoe Stein } else { 725215198a6SJoe Stein ASSERT0(vd->vdev_top_zap); 726fa9e4066Sahrens } 727fa9e4066Sahrens 728663207adSDon Brady if (top_level && alloctype != VDEV_ALLOC_ATTACH) { 729a1521560SJeff Bonwick ASSERT(alloctype == VDEV_ALLOC_LOAD || 7309f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ADD || 7311195e687SMark J Musante alloctype == VDEV_ALLOC_SPLIT || 7329f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ROOTPOOL); 733663207adSDon Brady /* Note: metaslab_group_create() is now deferred */ 734a1521560SJeff Bonwick } 735a1521560SJeff Bonwick 736215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && 737215198a6SJoe Stein (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 738215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, 739215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 740215198a6SJoe Stein } else { 741215198a6SJoe Stein ASSERT0(vd->vdev_leaf_zap); 742215198a6SJoe Stein } 743215198a6SJoe Stein 744fa9e4066Sahrens /* 7453d7072f8Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 746fa9e4066Sahrens */ 747215198a6SJoe Stein 748c5904d13Seschrock if (vd->vdev_ops->vdev_op_leaf && 74921ecdf64SLin Ling (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 75021ecdf64SLin Ling alloctype == VDEV_ALLOC_ROOTPOOL)) { 751c5904d13Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 752c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 7530713e232SGeorge Wilson &vd->vdev_dtl_object); 754c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 755c5904d13Seschrock &vd->vdev_unspare); 756c5904d13Seschrock } 75721ecdf64SLin Ling 75821ecdf64SLin Ling if (alloctype == VDEV_ALLOC_ROOTPOOL) { 75921ecdf64SLin Ling uint64_t spare = 0; 76021ecdf64SLin Ling 76121ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 76221ecdf64SLin Ling &spare) == 0 && spare) 76321ecdf64SLin Ling spa_spare_add(vd); 76421ecdf64SLin Ling } 76521ecdf64SLin Ling 766ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 767ecc2d604Sbonwick &vd->vdev_offline); 768c5904d13Seschrock 769b4952e17SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 770b4952e17SGeorge Wilson &vd->vdev_resilver_txg); 771cb04b873SMark J Musante 772e4c795beSTom Caputi if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER)) 773e4c795beSTom Caputi vdev_set_deferred_resilver(spa, vd); 774e4c795beSTom Caputi 7753d7072f8Seschrock /* 7763d7072f8Seschrock * When importing a pool, we want to ignore the persistent fault 7773d7072f8Seschrock * state, as the diagnosis made on another system may not be 778069f55e2SEric Schrock * valid in the current context. Local vdevs will 779069f55e2SEric Schrock * remain in the faulted state. 7803d7072f8Seschrock */ 781b16da2e2SGeorge Wilson if (spa_load_state(spa) == SPA_LOAD_OPEN) { 7823d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 7833d7072f8Seschrock &vd->vdev_faulted); 7843d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 7853d7072f8Seschrock &vd->vdev_degraded); 7863d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 7873d7072f8Seschrock &vd->vdev_removed); 788069f55e2SEric Schrock 789069f55e2SEric Schrock if (vd->vdev_faulted || vd->vdev_degraded) { 790069f55e2SEric Schrock char *aux; 791069f55e2SEric Schrock 792069f55e2SEric Schrock vd->vdev_label_aux = 793069f55e2SEric Schrock VDEV_AUX_ERR_EXCEEDED; 794069f55e2SEric Schrock if (nvlist_lookup_string(nv, 795069f55e2SEric Schrock ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 796069f55e2SEric Schrock strcmp(aux, "external") == 0) 797069f55e2SEric Schrock vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 798069f55e2SEric Schrock } 7993d7072f8Seschrock } 800fa9e4066Sahrens } 801fa9e4066Sahrens 802fa9e4066Sahrens /* 803fa9e4066Sahrens * Add ourselves to the parent's list of children. 804fa9e4066Sahrens */ 805fa9e4066Sahrens vdev_add_child(parent, vd); 806fa9e4066Sahrens 80799653d4eSeschrock *vdp = vd; 80899653d4eSeschrock 80999653d4eSeschrock return (0); 810fa9e4066Sahrens } 811fa9e4066Sahrens 812fa9e4066Sahrens void 813fa9e4066Sahrens vdev_free(vdev_t *vd) 814fa9e4066Sahrens { 8153d7072f8Seschrock spa_t *spa = vd->vdev_spa; 816084fd14fSBrian Behlendorf 817094e47e9SGeorge Wilson ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 818084fd14fSBrian Behlendorf ASSERT3P(vd->vdev_trim_thread, ==, NULL); 819084fd14fSBrian Behlendorf ASSERT3P(vd->vdev_autotrim_thread, ==, NULL); 820fa9e4066Sahrens 821a3874b8bSToomas Soome /* 822a3874b8bSToomas Soome * Scan queues are normally destroyed at the end of a scan. If the 823a3874b8bSToomas Soome * queue exists here, that implies the vdev is being removed while 824a3874b8bSToomas Soome * the scan is still running. 825a3874b8bSToomas Soome */ 826a3874b8bSToomas Soome if (vd->vdev_scan_io_queue != NULL) { 827a3874b8bSToomas Soome mutex_enter(&vd->vdev_scan_io_queue_lock); 828a3874b8bSToomas Soome dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue); 829a3874b8bSToomas Soome vd->vdev_scan_io_queue = NULL; 830a3874b8bSToomas Soome mutex_exit(&vd->vdev_scan_io_queue_lock); 831a3874b8bSToomas Soome } 832a3874b8bSToomas Soome 833fa9e4066Sahrens /* 834fa9e4066Sahrens * vdev_free() implies closing the vdev first. This is simpler than 835fa9e4066Sahrens * trying to ensure complicated semantics for all callers. 836fa9e4066Sahrens */ 837fa9e4066Sahrens vdev_close(vd); 838fa9e4066Sahrens 839e14bb325SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 840b24ab676SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 841fa9e4066Sahrens 842fa9e4066Sahrens /* 843fa9e4066Sahrens * Free all children. 844fa9e4066Sahrens */ 845573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 846fa9e4066Sahrens vdev_free(vd->vdev_child[c]); 847fa9e4066Sahrens 848fa9e4066Sahrens ASSERT(vd->vdev_child == NULL); 849fa9e4066Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 850fa9e4066Sahrens 851fa9e4066Sahrens /* 852fa9e4066Sahrens * Discard allocation state. 853fa9e4066Sahrens */ 854a1521560SJeff Bonwick if (vd->vdev_mg != NULL) { 855fa9e4066Sahrens vdev_metaslab_fini(vd); 856a1521560SJeff Bonwick metaslab_group_destroy(vd->vdev_mg); 857*814dcd43SSerapheim Dimitropoulos vd->vdev_mg = NULL; 858a1521560SJeff Bonwick } 859fa9e4066Sahrens 860fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_space); 861fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_dspace); 862fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 863fa9e4066Sahrens 864fa9e4066Sahrens /* 865fa9e4066Sahrens * Remove this vdev from its parent's child list. 866fa9e4066Sahrens */ 867fa9e4066Sahrens vdev_remove_child(vd->vdev_parent, vd); 868fa9e4066Sahrens 869fa9e4066Sahrens ASSERT(vd->vdev_parent == NULL); 870e0f1c0afSOlaf Faaland ASSERT(!list_link_active(&vd->vdev_leaf_node)); 871fa9e4066Sahrens 8723d7072f8Seschrock /* 8733d7072f8Seschrock * Clean up vdev structure. 8743d7072f8Seschrock */ 8753d7072f8Seschrock vdev_queue_fini(vd); 8763d7072f8Seschrock vdev_cache_fini(vd); 8773d7072f8Seschrock 8783d7072f8Seschrock if (vd->vdev_path) 8793d7072f8Seschrock spa_strfree(vd->vdev_path); 8803d7072f8Seschrock if (vd->vdev_devid) 8813d7072f8Seschrock spa_strfree(vd->vdev_devid); 8823d7072f8Seschrock if (vd->vdev_physpath) 8833d7072f8Seschrock spa_strfree(vd->vdev_physpath); 8846809eb4eSEric Schrock if (vd->vdev_fru) 8856809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 8863d7072f8Seschrock 8873d7072f8Seschrock if (vd->vdev_isspare) 8883d7072f8Seschrock spa_spare_remove(vd); 889fa94a07fSbrendan if (vd->vdev_isl2cache) 890fa94a07fSbrendan spa_l2cache_remove(vd); 8913d7072f8Seschrock 8923d7072f8Seschrock txg_list_destroy(&vd->vdev_ms_list); 8933d7072f8Seschrock txg_list_destroy(&vd->vdev_dtl_list); 8948ad4d6ddSJeff Bonwick 8953d7072f8Seschrock mutex_enter(&vd->vdev_dtl_lock); 8960713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 8978ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 8980713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 8990713e232SGeorge Wilson range_tree_destroy(vd->vdev_dtl[t]); 9008ad4d6ddSJeff Bonwick } 9013d7072f8Seschrock mutex_exit(&vd->vdev_dtl_lock); 9028ad4d6ddSJeff Bonwick 9035cabbc6bSPrashanth Sreenivasa EQUIV(vd->vdev_indirect_births != NULL, 9045cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping != NULL); 9055cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_births != NULL) { 9065cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 9075cabbc6bSPrashanth Sreenivasa vdev_indirect_births_close(vd->vdev_indirect_births); 9085cabbc6bSPrashanth Sreenivasa } 9095cabbc6bSPrashanth Sreenivasa 9105cabbc6bSPrashanth Sreenivasa if (vd->vdev_obsolete_sm != NULL) { 9115cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 9125cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 9135cabbc6bSPrashanth Sreenivasa space_map_close(vd->vdev_obsolete_sm); 9145cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_sm = NULL; 9155cabbc6bSPrashanth Sreenivasa } 9165cabbc6bSPrashanth Sreenivasa range_tree_destroy(vd->vdev_obsolete_segments); 9175cabbc6bSPrashanth Sreenivasa rw_destroy(&vd->vdev_indirect_rwlock); 9185cabbc6bSPrashanth Sreenivasa mutex_destroy(&vd->vdev_obsolete_lock); 9195cabbc6bSPrashanth Sreenivasa 9203d7072f8Seschrock mutex_destroy(&vd->vdev_dtl_lock); 9213d7072f8Seschrock mutex_destroy(&vd->vdev_stat_lock); 922e14bb325SJeff Bonwick mutex_destroy(&vd->vdev_probe_lock); 923a3874b8bSToomas Soome mutex_destroy(&vd->vdev_scan_io_queue_lock); 924094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_lock); 925094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_io_lock); 926094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_io_cv); 927094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_cv); 928084fd14fSBrian Behlendorf mutex_destroy(&vd->vdev_trim_lock); 929084fd14fSBrian Behlendorf mutex_destroy(&vd->vdev_autotrim_lock); 930084fd14fSBrian Behlendorf mutex_destroy(&vd->vdev_trim_io_lock); 931084fd14fSBrian Behlendorf cv_destroy(&vd->vdev_trim_cv); 932084fd14fSBrian Behlendorf cv_destroy(&vd->vdev_autotrim_cv); 933084fd14fSBrian Behlendorf cv_destroy(&vd->vdev_trim_io_cv); 9343d7072f8Seschrock 9353d7072f8Seschrock if (vd == spa->spa_root_vdev) 9363d7072f8Seschrock spa->spa_root_vdev = NULL; 9373d7072f8Seschrock 9383d7072f8Seschrock kmem_free(vd, sizeof (vdev_t)); 939fa9e4066Sahrens } 940fa9e4066Sahrens 941fa9e4066Sahrens /* 942fa9e4066Sahrens * Transfer top-level vdev state from svd to tvd. 943fa9e4066Sahrens */ 944fa9e4066Sahrens static void 945fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 946fa9e4066Sahrens { 947fa9e4066Sahrens spa_t *spa = svd->vdev_spa; 948fa9e4066Sahrens metaslab_t *msp; 949fa9e4066Sahrens vdev_t *vd; 950fa9e4066Sahrens int t; 951fa9e4066Sahrens 952fa9e4066Sahrens ASSERT(tvd == tvd->vdev_top); 953fa9e4066Sahrens 954fa9e4066Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 955fa9e4066Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 956fa9e4066Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 957215198a6SJoe Stein tvd->vdev_top_zap = svd->vdev_top_zap; 958fa9e4066Sahrens 959fa9e4066Sahrens svd->vdev_ms_array = 0; 960fa9e4066Sahrens svd->vdev_ms_shift = 0; 961fa9e4066Sahrens svd->vdev_ms_count = 0; 962215198a6SJoe Stein svd->vdev_top_zap = 0; 963fa9e4066Sahrens 964cd0837ccSGeorge Wilson if (tvd->vdev_mg) 965cd0837ccSGeorge Wilson ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 966fa9e4066Sahrens tvd->vdev_mg = svd->vdev_mg; 967fa9e4066Sahrens tvd->vdev_ms = svd->vdev_ms; 968fa9e4066Sahrens 969fa9e4066Sahrens svd->vdev_mg = NULL; 970fa9e4066Sahrens svd->vdev_ms = NULL; 971ecc2d604Sbonwick 972ecc2d604Sbonwick if (tvd->vdev_mg != NULL) 973ecc2d604Sbonwick tvd->vdev_mg->mg_vd = tvd; 974fa9e4066Sahrens 97586714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 97686714001SSerapheim Dimitropoulos svd->vdev_checkpoint_sm = NULL; 97786714001SSerapheim Dimitropoulos 978663207adSDon Brady tvd->vdev_alloc_bias = svd->vdev_alloc_bias; 979663207adSDon Brady svd->vdev_alloc_bias = VDEV_BIAS_NONE; 980663207adSDon Brady 981fa9e4066Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 982fa9e4066Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 98399653d4eSeschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 984fa9e4066Sahrens 985fa9e4066Sahrens svd->vdev_stat.vs_alloc = 0; 986fa9e4066Sahrens svd->vdev_stat.vs_space = 0; 98799653d4eSeschrock svd->vdev_stat.vs_dspace = 0; 988fa9e4066Sahrens 9893a4b1be9SMatthew Ahrens /* 9903a4b1be9SMatthew Ahrens * State which may be set on a top-level vdev that's in the 9913a4b1be9SMatthew Ahrens * process of being removed. 9923a4b1be9SMatthew Ahrens */ 9933a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_births_object); 9943a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 9953a4b1be9SMatthew Ahrens ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 9963a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 9973a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 9983a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 9993a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_removing); 10003a4b1be9SMatthew Ahrens tvd->vdev_removing = svd->vdev_removing; 10013a4b1be9SMatthew Ahrens tvd->vdev_indirect_config = svd->vdev_indirect_config; 10023a4b1be9SMatthew Ahrens tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 10033a4b1be9SMatthew Ahrens tvd->vdev_indirect_births = svd->vdev_indirect_births; 10043a4b1be9SMatthew Ahrens range_tree_swap(&svd->vdev_obsolete_segments, 10053a4b1be9SMatthew Ahrens &tvd->vdev_obsolete_segments); 10063a4b1be9SMatthew Ahrens tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 10073a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_mapping_object = 0; 10083a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_births_object = 0; 10093a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 10103a4b1be9SMatthew Ahrens svd->vdev_indirect_mapping = NULL; 10113a4b1be9SMatthew Ahrens svd->vdev_indirect_births = NULL; 10123a4b1be9SMatthew Ahrens svd->vdev_obsolete_sm = NULL; 10133a4b1be9SMatthew Ahrens svd->vdev_removing = 0; 10143a4b1be9SMatthew Ahrens 1015fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 1016fa9e4066Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 1017fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 1018fa9e4066Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 1019fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 1020fa9e4066Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 1021fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 1022fa9e4066Sahrens } 1023fa9e4066Sahrens 1024e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_config_dirty_node)) { 1025fa9e4066Sahrens vdev_config_clean(svd); 1026fa9e4066Sahrens vdev_config_dirty(tvd); 1027fa9e4066Sahrens } 1028fa9e4066Sahrens 1029e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_state_dirty_node)) { 1030e14bb325SJeff Bonwick vdev_state_clean(svd); 1031e14bb325SJeff Bonwick vdev_state_dirty(tvd); 1032e14bb325SJeff Bonwick } 1033e14bb325SJeff Bonwick 103499653d4eSeschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 103599653d4eSeschrock svd->vdev_deflate_ratio = 0; 10368654d025Sperrin 10378654d025Sperrin tvd->vdev_islog = svd->vdev_islog; 10388654d025Sperrin svd->vdev_islog = 0; 1039a3874b8bSToomas Soome 1040a3874b8bSToomas Soome dsl_scan_io_queue_vdev_xfer(svd, tvd); 1041fa9e4066Sahrens } 1042fa9e4066Sahrens 1043fa9e4066Sahrens static void 1044fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 1045fa9e4066Sahrens { 1046fa9e4066Sahrens if (vd == NULL) 1047fa9e4066Sahrens return; 1048fa9e4066Sahrens 1049fa9e4066Sahrens vd->vdev_top = tvd; 1050fa9e4066Sahrens 1051573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1052fa9e4066Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 1053fa9e4066Sahrens } 1054fa9e4066Sahrens 1055fa9e4066Sahrens /* 1056fa9e4066Sahrens * Add a mirror/replacing vdev above an existing vdev. 1057fa9e4066Sahrens */ 1058fa9e4066Sahrens vdev_t * 1059fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 1060fa9e4066Sahrens { 1061fa9e4066Sahrens spa_t *spa = cvd->vdev_spa; 1062fa9e4066Sahrens vdev_t *pvd = cvd->vdev_parent; 1063fa9e4066Sahrens vdev_t *mvd; 1064fa9e4066Sahrens 1065e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1066fa9e4066Sahrens 1067fa9e4066Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 1068ecc2d604Sbonwick 1069ecc2d604Sbonwick mvd->vdev_asize = cvd->vdev_asize; 1070573ca77eSGeorge Wilson mvd->vdev_min_asize = cvd->vdev_min_asize; 10714263d13fSGeorge Wilson mvd->vdev_max_asize = cvd->vdev_max_asize; 10725cabbc6bSPrashanth Sreenivasa mvd->vdev_psize = cvd->vdev_psize; 1073ecc2d604Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 1074ecc2d604Sbonwick mvd->vdev_state = cvd->vdev_state; 107588ecc943SGeorge Wilson mvd->vdev_crtxg = cvd->vdev_crtxg; 1076ecc2d604Sbonwick 1077fa9e4066Sahrens vdev_remove_child(pvd, cvd); 1078fa9e4066Sahrens vdev_add_child(pvd, mvd); 1079fa9e4066Sahrens cvd->vdev_id = mvd->vdev_children; 1080fa9e4066Sahrens vdev_add_child(mvd, cvd); 1081fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1082fa9e4066Sahrens 1083fa9e4066Sahrens if (mvd == mvd->vdev_top) 1084fa9e4066Sahrens vdev_top_transfer(cvd, mvd); 1085fa9e4066Sahrens 1086fa9e4066Sahrens return (mvd); 1087fa9e4066Sahrens } 1088fa9e4066Sahrens 1089fa9e4066Sahrens /* 1090fa9e4066Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 1091fa9e4066Sahrens */ 1092fa9e4066Sahrens void 1093fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd) 1094fa9e4066Sahrens { 1095fa9e4066Sahrens vdev_t *mvd = cvd->vdev_parent; 1096fa9e4066Sahrens vdev_t *pvd = mvd->vdev_parent; 1097fa9e4066Sahrens 1098e14bb325SJeff Bonwick ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1099fa9e4066Sahrens 1100fa9e4066Sahrens ASSERT(mvd->vdev_children == 1); 1101fa9e4066Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 110299653d4eSeschrock mvd->vdev_ops == &vdev_replacing_ops || 110399653d4eSeschrock mvd->vdev_ops == &vdev_spare_ops); 1104ecc2d604Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 1105fa9e4066Sahrens 1106fa9e4066Sahrens vdev_remove_child(mvd, cvd); 1107fa9e4066Sahrens vdev_remove_child(pvd, mvd); 11088ad4d6ddSJeff Bonwick 110999653d4eSeschrock /* 1110e14bb325SJeff Bonwick * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1111e14bb325SJeff Bonwick * Otherwise, we could have detached an offline device, and when we 1112e14bb325SJeff Bonwick * go to import the pool we'll think we have two top-level vdevs, 1113e14bb325SJeff Bonwick * instead of a different version of the same top-level vdev. 111499653d4eSeschrock */ 11158ad4d6ddSJeff Bonwick if (mvd->vdev_top == mvd) { 11168ad4d6ddSJeff Bonwick uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 11171195e687SMark J Musante cvd->vdev_orig_guid = cvd->vdev_guid; 11188ad4d6ddSJeff Bonwick cvd->vdev_guid += guid_delta; 11198ad4d6ddSJeff Bonwick cvd->vdev_guid_sum += guid_delta; 11208ad4d6ddSJeff Bonwick } 1121e14bb325SJeff Bonwick cvd->vdev_id = mvd->vdev_id; 1122e14bb325SJeff Bonwick vdev_add_child(pvd, cvd); 1123fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1124fa9e4066Sahrens 1125fa9e4066Sahrens if (cvd == cvd->vdev_top) 1126fa9e4066Sahrens vdev_top_transfer(mvd, cvd); 1127fa9e4066Sahrens 1128fa9e4066Sahrens ASSERT(mvd->vdev_children == 0); 1129fa9e4066Sahrens vdev_free(mvd); 1130fa9e4066Sahrens } 1131fa9e4066Sahrens 1132663207adSDon Brady static void 1133663207adSDon Brady vdev_metaslab_group_create(vdev_t *vd) 1134663207adSDon Brady { 1135663207adSDon Brady spa_t *spa = vd->vdev_spa; 1136663207adSDon Brady 1137663207adSDon Brady /* 1138663207adSDon Brady * metaslab_group_create was delayed until allocation bias was available 1139663207adSDon Brady */ 1140663207adSDon Brady if (vd->vdev_mg == NULL) { 1141663207adSDon Brady metaslab_class_t *mc; 1142663207adSDon Brady 1143663207adSDon Brady if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE) 1144663207adSDon Brady vd->vdev_alloc_bias = VDEV_BIAS_LOG; 1145663207adSDon Brady 1146663207adSDon Brady ASSERT3U(vd->vdev_islog, ==, 1147663207adSDon Brady (vd->vdev_alloc_bias == VDEV_BIAS_LOG)); 1148663207adSDon Brady 1149663207adSDon Brady switch (vd->vdev_alloc_bias) { 1150663207adSDon Brady case VDEV_BIAS_LOG: 1151663207adSDon Brady mc = spa_log_class(spa); 1152663207adSDon Brady break; 1153663207adSDon Brady case VDEV_BIAS_SPECIAL: 1154663207adSDon Brady mc = spa_special_class(spa); 1155663207adSDon Brady break; 1156663207adSDon Brady case VDEV_BIAS_DEDUP: 1157663207adSDon Brady mc = spa_dedup_class(spa); 1158663207adSDon Brady break; 1159663207adSDon Brady default: 1160663207adSDon Brady mc = spa_normal_class(spa); 1161663207adSDon Brady } 1162663207adSDon Brady 1163663207adSDon Brady vd->vdev_mg = metaslab_group_create(mc, vd, 1164663207adSDon Brady spa->spa_alloc_count); 1165663207adSDon Brady 1166663207adSDon Brady /* 1167663207adSDon Brady * The spa ashift values currently only reflect the 1168663207adSDon Brady * general vdev classes. Class destination is late 1169663207adSDon Brady * binding so ashift checking had to wait until now 1170663207adSDon Brady */ 1171663207adSDon Brady if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1172663207adSDon Brady mc == spa_normal_class(spa) && vd->vdev_aux == NULL) { 1173663207adSDon Brady if (vd->vdev_ashift > spa->spa_max_ashift) 1174663207adSDon Brady spa->spa_max_ashift = vd->vdev_ashift; 1175663207adSDon Brady if (vd->vdev_ashift < spa->spa_min_ashift) 1176663207adSDon Brady spa->spa_min_ashift = vd->vdev_ashift; 1177663207adSDon Brady } 1178663207adSDon Brady } 1179663207adSDon Brady } 1180663207adSDon Brady 1181ea8dc4b6Seschrock int 1182fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1183fa9e4066Sahrens { 1184fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 1185ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 1186ecc2d604Sbonwick uint64_t m; 1187fa9e4066Sahrens uint64_t oldc = vd->vdev_ms_count; 1188fa9e4066Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1189ecc2d604Sbonwick metaslab_t **mspp; 1190ecc2d604Sbonwick int error; 1191663207adSDon Brady boolean_t expanding = (oldc != 0); 1192fa9e4066Sahrens 1193a1521560SJeff Bonwick ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1194a1521560SJeff Bonwick 119588ecc943SGeorge Wilson /* 119688ecc943SGeorge Wilson * This vdev is not being allocated from yet or is a hole. 119788ecc943SGeorge Wilson */ 119888ecc943SGeorge Wilson if (vd->vdev_ms_shift == 0) 11990e34b6a7Sbonwick return (0); 12000e34b6a7Sbonwick 120188ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 120288ecc943SGeorge Wilson 1203fa9e4066Sahrens ASSERT(oldc <= newc); 1204fa9e4066Sahrens 1205ecc2d604Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1206fa9e4066Sahrens 1207663207adSDon Brady if (expanding) { 1208ecc2d604Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1209ecc2d604Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1210ecc2d604Sbonwick } 1211fa9e4066Sahrens 1212ecc2d604Sbonwick vd->vdev_ms = mspp; 1213ecc2d604Sbonwick vd->vdev_ms_count = newc; 1214ecc2d604Sbonwick for (m = oldc; m < newc; m++) { 12150713e232SGeorge Wilson uint64_t object = 0; 12160713e232SGeorge Wilson 12175cabbc6bSPrashanth Sreenivasa /* 12185cabbc6bSPrashanth Sreenivasa * vdev_ms_array may be 0 if we are creating the "fake" 12195cabbc6bSPrashanth Sreenivasa * metaslabs for an indirect vdev for zdb's leak detection. 12205cabbc6bSPrashanth Sreenivasa * See zdb_leak_init(). 12215cabbc6bSPrashanth Sreenivasa */ 12225cabbc6bSPrashanth Sreenivasa if (txg == 0 && vd->vdev_ms_array != 0) { 1223ecc2d604Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 12247bfdf011SNeil Perrin m * sizeof (uint64_t), sizeof (uint64_t), &object, 12257bfdf011SNeil Perrin DMU_READ_PREFETCH); 12263ee8c80cSPavel Zakharov if (error != 0) { 12273ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "unable to read the metaslab " 12283ee8c80cSPavel Zakharov "array [error=%d]", error); 1229ecc2d604Sbonwick return (error); 12303ee8c80cSPavel Zakharov } 1231fa9e4066Sahrens } 12321e9bd7ecSPrakash Surya 1233663207adSDon Brady #ifndef _KERNEL 1234663207adSDon Brady /* 1235663207adSDon Brady * To accomodate zdb_leak_init() fake indirect 1236663207adSDon Brady * metaslabs, we allocate a metaslab group for 1237663207adSDon Brady * indirect vdevs which normally don't have one. 1238663207adSDon Brady */ 1239663207adSDon Brady if (vd->vdev_mg == NULL) { 1240663207adSDon Brady ASSERT0(vdev_is_concrete(vd)); 1241663207adSDon Brady vdev_metaslab_group_create(vd); 1242663207adSDon Brady } 1243663207adSDon Brady #endif 12441e9bd7ecSPrakash Surya error = metaslab_init(vd->vdev_mg, m, object, txg, 12451e9bd7ecSPrakash Surya &(vd->vdev_ms[m])); 12463ee8c80cSPavel Zakharov if (error != 0) { 12473ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 12483ee8c80cSPavel Zakharov error); 12491e9bd7ecSPrakash Surya return (error); 12503ee8c80cSPavel Zakharov } 1251fa9e4066Sahrens } 1252fa9e4066Sahrens 1253a1521560SJeff Bonwick if (txg == 0) 1254a1521560SJeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1255a1521560SJeff Bonwick 12563f9d6ad7SLin Ling /* 12573f9d6ad7SLin Ling * If the vdev is being removed we don't activate 12583f9d6ad7SLin Ling * the metaslabs since we want to ensure that no new 12593f9d6ad7SLin Ling * allocations are performed on this device. 12603f9d6ad7SLin Ling */ 1261663207adSDon Brady if (!expanding && !vd->vdev_removing) { 1262a1521560SJeff Bonwick metaslab_group_activate(vd->vdev_mg); 1263663207adSDon Brady } 1264a1521560SJeff Bonwick 1265a1521560SJeff Bonwick if (txg == 0) 1266a1521560SJeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG); 1267a1521560SJeff Bonwick 1268*814dcd43SSerapheim Dimitropoulos /* 1269*814dcd43SSerapheim Dimitropoulos * Regardless whether this vdev was just added or it is being 1270*814dcd43SSerapheim Dimitropoulos * expanded, the metaslab count has changed. Recalculate the 1271*814dcd43SSerapheim Dimitropoulos * block limit. 1272*814dcd43SSerapheim Dimitropoulos */ 1273*814dcd43SSerapheim Dimitropoulos spa_log_sm_set_blocklimit(spa); 1274*814dcd43SSerapheim Dimitropoulos 1275ea8dc4b6Seschrock return (0); 1276fa9e4066Sahrens } 1277fa9e4066Sahrens 1278fa9e4066Sahrens void 1279fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd) 1280fa9e4066Sahrens { 128186714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 128286714001SSerapheim Dimitropoulos ASSERT(spa_feature_is_active(vd->vdev_spa, 128386714001SSerapheim Dimitropoulos SPA_FEATURE_POOL_CHECKPOINT)); 128486714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 128586714001SSerapheim Dimitropoulos /* 128686714001SSerapheim Dimitropoulos * Even though we close the space map, we need to set its 128786714001SSerapheim Dimitropoulos * pointer to NULL. The reason is that vdev_metaslab_fini() 128886714001SSerapheim Dimitropoulos * may be called multiple times for certain operations 128986714001SSerapheim Dimitropoulos * (i.e. when destroying a pool) so we need to ensure that 129086714001SSerapheim Dimitropoulos * this clause never executes twice. This logic is similar 129186714001SSerapheim Dimitropoulos * to the one used for the vdev_ms clause below. 129286714001SSerapheim Dimitropoulos */ 129386714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 129486714001SSerapheim Dimitropoulos } 129586714001SSerapheim Dimitropoulos 1296fa9e4066Sahrens if (vd->vdev_ms != NULL) { 1297555d674dSSerapheim Dimitropoulos metaslab_group_t *mg = vd->vdev_mg; 1298555d674dSSerapheim Dimitropoulos metaslab_group_passivate(mg); 12995cabbc6bSPrashanth Sreenivasa 1300555d674dSSerapheim Dimitropoulos uint64_t count = vd->vdev_ms_count; 13015cabbc6bSPrashanth Sreenivasa for (uint64_t m = 0; m < count; m++) { 13020713e232SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 13030713e232SGeorge Wilson if (msp != NULL) 13040713e232SGeorge Wilson metaslab_fini(msp); 13050713e232SGeorge Wilson } 1306fa9e4066Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1307fa9e4066Sahrens vd->vdev_ms = NULL; 13085cabbc6bSPrashanth Sreenivasa 13095cabbc6bSPrashanth Sreenivasa vd->vdev_ms_count = 0; 1310555d674dSSerapheim Dimitropoulos 1311555d674dSSerapheim Dimitropoulos for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 1312555d674dSSerapheim Dimitropoulos ASSERT0(mg->mg_histogram[i]); 1313fa9e4066Sahrens } 13145cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_ms_count); 1315fa9e4066Sahrens } 1316fa9e4066Sahrens 1317e14bb325SJeff Bonwick typedef struct vdev_probe_stats { 1318e14bb325SJeff Bonwick boolean_t vps_readable; 1319e14bb325SJeff Bonwick boolean_t vps_writeable; 1320e14bb325SJeff Bonwick int vps_flags; 1321e14bb325SJeff Bonwick } vdev_probe_stats_t; 1322e14bb325SJeff Bonwick 1323e14bb325SJeff Bonwick static void 1324e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio) 13250a4e9518Sgw { 13268ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 1327a3f829aeSBill Moore vdev_t *vd = zio->io_vd; 1328e14bb325SJeff Bonwick vdev_probe_stats_t *vps = zio->io_private; 1329a3f829aeSBill Moore 1330a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio != NULL); 1331e14bb325SJeff Bonwick 1332e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) { 1333e14bb325SJeff Bonwick if (zio->io_error == 0) 1334e14bb325SJeff Bonwick vps->vps_readable = 1; 13358ad4d6ddSJeff Bonwick if (zio->io_error == 0 && spa_writeable(spa)) { 1336a3f829aeSBill Moore zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1337770499e1SDan Kimmel zio->io_offset, zio->io_size, zio->io_abd, 1338e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1339e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1340e14bb325SJeff Bonwick } else { 1341770499e1SDan Kimmel abd_free(zio->io_abd); 1342e14bb325SJeff Bonwick } 1343e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_WRITE) { 1344e14bb325SJeff Bonwick if (zio->io_error == 0) 1345e14bb325SJeff Bonwick vps->vps_writeable = 1; 1346770499e1SDan Kimmel abd_free(zio->io_abd); 1347e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_NULL) { 1348a3f829aeSBill Moore zio_t *pio; 1349e14bb325SJeff Bonwick 1350e14bb325SJeff Bonwick vd->vdev_cant_read |= !vps->vps_readable; 1351e14bb325SJeff Bonwick vd->vdev_cant_write |= !vps->vps_writeable; 1352e14bb325SJeff Bonwick 1353e14bb325SJeff Bonwick if (vdev_readable(vd) && 13548ad4d6ddSJeff Bonwick (vdev_writeable(vd) || !spa_writeable(spa))) { 1355e14bb325SJeff Bonwick zio->io_error = 0; 1356e14bb325SJeff Bonwick } else { 1357e14bb325SJeff Bonwick ASSERT(zio->io_error != 0); 13583ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "failed probe"); 1359e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1360eb633035STom Caputi spa, vd, NULL, NULL, 0, 0); 1361be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 1362e14bb325SJeff Bonwick } 1363a3f829aeSBill Moore 1364a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1365a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio == zio); 1366a3f829aeSBill Moore vd->vdev_probe_zio = NULL; 1367a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1368a3f829aeSBill Moore 13690f7643c7SGeorge Wilson zio_link_t *zl = NULL; 13700f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1371a3f829aeSBill Moore if (!vdev_accessible(vd, pio)) 1372be6fd75aSMatthew Ahrens pio->io_error = SET_ERROR(ENXIO); 1373a3f829aeSBill Moore 1374e14bb325SJeff Bonwick kmem_free(vps, sizeof (*vps)); 1375e14bb325SJeff Bonwick } 1376e14bb325SJeff Bonwick } 13770a4e9518Sgw 1378e14bb325SJeff Bonwick /* 1379f7170741SWill Andrews * Determine whether this device is accessible. 1380f7170741SWill Andrews * 1381f7170741SWill Andrews * Read and write to several known locations: the pad regions of each 1382f7170741SWill Andrews * vdev label but the first, which we leave alone in case it contains 1383f7170741SWill Andrews * a VTOC. 1384e14bb325SJeff Bonwick */ 1385e14bb325SJeff Bonwick zio_t * 1386a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio) 1387e14bb325SJeff Bonwick { 1388e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 1389a3f829aeSBill Moore vdev_probe_stats_t *vps = NULL; 1390a3f829aeSBill Moore zio_t *pio; 1391a3f829aeSBill Moore 1392a3f829aeSBill Moore ASSERT(vd->vdev_ops->vdev_op_leaf); 13930a4e9518Sgw 1394a3f829aeSBill Moore /* 1395a3f829aeSBill Moore * Don't probe the probe. 1396a3f829aeSBill Moore */ 1397a3f829aeSBill Moore if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1398a3f829aeSBill Moore return (NULL); 1399e14bb325SJeff Bonwick 1400a3f829aeSBill Moore /* 1401a3f829aeSBill Moore * To prevent 'probe storms' when a device fails, we create 1402a3f829aeSBill Moore * just one probe i/o at a time. All zios that want to probe 1403a3f829aeSBill Moore * this vdev will become parents of the probe io. 1404a3f829aeSBill Moore */ 1405a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1406e14bb325SJeff Bonwick 1407a3f829aeSBill Moore if ((pio = vd->vdev_probe_zio) == NULL) { 1408a3f829aeSBill Moore vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1409a3f829aeSBill Moore 1410a3f829aeSBill Moore vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1411a3f829aeSBill Moore ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 14128956713aSEric Schrock ZIO_FLAG_TRYHARD; 1413a3f829aeSBill Moore 1414a3f829aeSBill Moore if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1415a3f829aeSBill Moore /* 1416a3f829aeSBill Moore * vdev_cant_read and vdev_cant_write can only 1417a3f829aeSBill Moore * transition from TRUE to FALSE when we have the 1418a3f829aeSBill Moore * SCL_ZIO lock as writer; otherwise they can only 1419a3f829aeSBill Moore * transition from FALSE to TRUE. This ensures that 1420a3f829aeSBill Moore * any zio looking at these values can assume that 1421a3f829aeSBill Moore * failures persist for the life of the I/O. That's 1422a3f829aeSBill Moore * important because when a device has intermittent 1423a3f829aeSBill Moore * connectivity problems, we want to ensure that 1424a3f829aeSBill Moore * they're ascribed to the device (ENXIO) and not 1425a3f829aeSBill Moore * the zio (EIO). 1426a3f829aeSBill Moore * 1427a3f829aeSBill Moore * Since we hold SCL_ZIO as writer here, clear both 1428a3f829aeSBill Moore * values so the probe can reevaluate from first 1429a3f829aeSBill Moore * principles. 1430a3f829aeSBill Moore */ 1431a3f829aeSBill Moore vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1432a3f829aeSBill Moore vd->vdev_cant_read = B_FALSE; 1433a3f829aeSBill Moore vd->vdev_cant_write = B_FALSE; 1434a3f829aeSBill Moore } 1435a3f829aeSBill Moore 1436a3f829aeSBill Moore vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1437a3f829aeSBill Moore vdev_probe_done, vps, 1438a3f829aeSBill Moore vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1439a3f829aeSBill Moore 144098d1cbfeSGeorge Wilson /* 144198d1cbfeSGeorge Wilson * We can't change the vdev state in this context, so we 144298d1cbfeSGeorge Wilson * kick off an async task to do it on our behalf. 144398d1cbfeSGeorge Wilson */ 1444a3f829aeSBill Moore if (zio != NULL) { 1445a3f829aeSBill Moore vd->vdev_probe_wanted = B_TRUE; 1446a3f829aeSBill Moore spa_async_request(spa, SPA_ASYNC_PROBE); 1447a3f829aeSBill Moore } 1448e14bb325SJeff Bonwick } 1449e14bb325SJeff Bonwick 1450a3f829aeSBill Moore if (zio != NULL) 1451a3f829aeSBill Moore zio_add_child(zio, pio); 1452e14bb325SJeff Bonwick 1453a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1454e14bb325SJeff Bonwick 1455a3f829aeSBill Moore if (vps == NULL) { 1456a3f829aeSBill Moore ASSERT(zio != NULL); 1457a3f829aeSBill Moore return (NULL); 1458a3f829aeSBill Moore } 1459e14bb325SJeff Bonwick 1460e14bb325SJeff Bonwick for (int l = 1; l < VDEV_LABELS; l++) { 1461a3f829aeSBill Moore zio_nowait(zio_read_phys(pio, vd, 1462e14bb325SJeff Bonwick vdev_label_offset(vd->vdev_psize, l, 1463770499e1SDan Kimmel offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1464770499e1SDan Kimmel abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1465e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1466e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1467e14bb325SJeff Bonwick } 1468e14bb325SJeff Bonwick 1469a3f829aeSBill Moore if (zio == NULL) 1470a3f829aeSBill Moore return (pio); 1471a3f829aeSBill Moore 1472a3f829aeSBill Moore zio_nowait(pio); 1473a3f829aeSBill Moore return (NULL); 14740a4e9518Sgw } 14750a4e9518Sgw 1476f64c0e34SEric Taylor static void 1477f64c0e34SEric Taylor vdev_open_child(void *arg) 1478f64c0e34SEric Taylor { 1479f64c0e34SEric Taylor vdev_t *vd = arg; 1480f64c0e34SEric Taylor 1481f64c0e34SEric Taylor vd->vdev_open_thread = curthread; 1482f64c0e34SEric Taylor vd->vdev_open_error = vdev_open(vd); 1483f64c0e34SEric Taylor vd->vdev_open_thread = NULL; 1484f64c0e34SEric Taylor } 1485f64c0e34SEric Taylor 1486681d9761SEric Taylor boolean_t 1487681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd) 1488681d9761SEric Taylor { 1489681d9761SEric Taylor if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1490681d9761SEric Taylor strlen(ZVOL_DIR)) == 0) 1491681d9761SEric Taylor return (B_TRUE); 1492681d9761SEric Taylor for (int c = 0; c < vd->vdev_children; c++) 1493681d9761SEric Taylor if (vdev_uses_zvols(vd->vdev_child[c])) 1494681d9761SEric Taylor return (B_TRUE); 1495681d9761SEric Taylor return (B_FALSE); 1496681d9761SEric Taylor } 1497681d9761SEric Taylor 1498f64c0e34SEric Taylor void 1499f64c0e34SEric Taylor vdev_open_children(vdev_t *vd) 1500f64c0e34SEric Taylor { 1501f64c0e34SEric Taylor taskq_t *tq; 1502f64c0e34SEric Taylor int children = vd->vdev_children; 1503f64c0e34SEric Taylor 1504681d9761SEric Taylor /* 1505681d9761SEric Taylor * in order to handle pools on top of zvols, do the opens 1506681d9761SEric Taylor * in a single thread so that the same thread holds the 1507681d9761SEric Taylor * spa_namespace_lock 1508681d9761SEric Taylor */ 1509681d9761SEric Taylor if (vdev_uses_zvols(vd)) { 151012a8814cSTom Caputi retry_sync: 1511681d9761SEric Taylor for (int c = 0; c < children; c++) 1512681d9761SEric Taylor vd->vdev_child[c]->vdev_open_error = 1513681d9761SEric Taylor vdev_open(vd->vdev_child[c]); 151412a8814cSTom Caputi } else { 151512a8814cSTom Caputi tq = taskq_create("vdev_open", children, minclsyspri, 151612a8814cSTom Caputi children, children, TASKQ_PREPOPULATE); 151712a8814cSTom Caputi if (tq == NULL) 151812a8814cSTom Caputi goto retry_sync; 151912a8814cSTom Caputi 152012a8814cSTom Caputi for (int c = 0; c < children; c++) 152112a8814cSTom Caputi VERIFY(taskq_dispatch(tq, vdev_open_child, 152212a8814cSTom Caputi vd->vdev_child[c], TQ_SLEEP) != TASKQID_INVALID); 152312a8814cSTom Caputi 152412a8814cSTom Caputi taskq_destroy(tq); 1525681d9761SEric Taylor } 1526f64c0e34SEric Taylor 152712a8814cSTom Caputi vd->vdev_nonrot = B_TRUE; 1528f64c0e34SEric Taylor 152912a8814cSTom Caputi for (int c = 0; c < children; c++) 153012a8814cSTom Caputi vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot; 1531f64c0e34SEric Taylor } 1532f64c0e34SEric Taylor 15335cabbc6bSPrashanth Sreenivasa /* 15345cabbc6bSPrashanth Sreenivasa * Compute the raidz-deflation ratio. Note, we hard-code 15355cabbc6bSPrashanth Sreenivasa * in 128k (1 << 17) because it is the "typical" blocksize. 15365cabbc6bSPrashanth Sreenivasa * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 15375cabbc6bSPrashanth Sreenivasa * otherwise it would inconsistently account for existing bp's. 15385cabbc6bSPrashanth Sreenivasa */ 15395cabbc6bSPrashanth Sreenivasa static void 15405cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd) 15415cabbc6bSPrashanth Sreenivasa { 15425cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 15435cabbc6bSPrashanth Sreenivasa vd->vdev_deflate_ratio = (1 << 17) / 15445cabbc6bSPrashanth Sreenivasa (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 15455cabbc6bSPrashanth Sreenivasa } 15465cabbc6bSPrashanth Sreenivasa } 15475cabbc6bSPrashanth Sreenivasa 1548fa9e4066Sahrens /* 1549fa9e4066Sahrens * Prepare a virtual device for access. 1550fa9e4066Sahrens */ 1551fa9e4066Sahrens int 1552fa9e4066Sahrens vdev_open(vdev_t *vd) 1553fa9e4066Sahrens { 15548ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1555fa9e4066Sahrens int error; 1556fa9e4066Sahrens uint64_t osize = 0; 15574263d13fSGeorge Wilson uint64_t max_osize = 0; 15584263d13fSGeorge Wilson uint64_t asize, max_asize, psize; 1559ecc2d604Sbonwick uint64_t ashift = 0; 1560fa9e4066Sahrens 1561f64c0e34SEric Taylor ASSERT(vd->vdev_open_thread == curthread || 1562f64c0e34SEric Taylor spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1563fa9e4066Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1564fa9e4066Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1565fa9e4066Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1566fa9e4066Sahrens 1567fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1568e6ca193dSGeorge Wilson vd->vdev_cant_read = B_FALSE; 1569e6ca193dSGeorge Wilson vd->vdev_cant_write = B_FALSE; 1570573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 1571fa9e4066Sahrens 1572069f55e2SEric Schrock /* 1573069f55e2SEric Schrock * If this vdev is not removed, check its fault status. If it's 1574069f55e2SEric Schrock * faulted, bail out of the open. 1575069f55e2SEric Schrock */ 15763d7072f8Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 15773d7072f8Seschrock ASSERT(vd->vdev_children == 0); 1578069f55e2SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1579069f55e2SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 15803d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1581069f55e2SEric Schrock vd->vdev_label_aux); 1582be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15833d7072f8Seschrock } else if (vd->vdev_offline) { 1584fa9e4066Sahrens ASSERT(vd->vdev_children == 0); 1585ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1586be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1587fa9e4066Sahrens } 1588fa9e4066Sahrens 15894263d13fSGeorge Wilson error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1590fa9e4066Sahrens 1591095bcd66SGeorge Wilson /* 1592095bcd66SGeorge Wilson * Reset the vdev_reopening flag so that we actually close 1593095bcd66SGeorge Wilson * the vdev on error. 1594095bcd66SGeorge Wilson */ 1595095bcd66SGeorge Wilson vd->vdev_reopening = B_FALSE; 1596ea8dc4b6Seschrock if (zio_injection_enabled && error == 0) 15978956713aSEric Schrock error = zio_handle_device_injection(vd, NULL, ENXIO); 1598ea8dc4b6Seschrock 1599fa9e4066Sahrens if (error) { 16003d7072f8Seschrock if (vd->vdev_removed && 16013d7072f8Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 16023d7072f8Seschrock vd->vdev_removed = B_FALSE; 16033d7072f8Seschrock 16046f793812SPavel Zakharov if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 16056f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 16066f793812SPavel Zakharov vd->vdev_stat.vs_aux); 16076f793812SPavel Zakharov } else { 16086f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 16096f793812SPavel Zakharov vd->vdev_stat.vs_aux); 16106f793812SPavel Zakharov } 1611fa9e4066Sahrens return (error); 1612fa9e4066Sahrens } 1613fa9e4066Sahrens 16143d7072f8Seschrock vd->vdev_removed = B_FALSE; 16153d7072f8Seschrock 1616096d22d4SEric Schrock /* 1617096d22d4SEric Schrock * Recheck the faulted flag now that we have confirmed that 1618096d22d4SEric Schrock * the vdev is accessible. If we're faulted, bail. 1619096d22d4SEric Schrock */ 1620096d22d4SEric Schrock if (vd->vdev_faulted) { 1621096d22d4SEric Schrock ASSERT(vd->vdev_children == 0); 1622096d22d4SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1623096d22d4SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1624096d22d4SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1625096d22d4SEric Schrock vd->vdev_label_aux); 1626be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1627096d22d4SEric Schrock } 1628096d22d4SEric Schrock 16293d7072f8Seschrock if (vd->vdev_degraded) { 16303d7072f8Seschrock ASSERT(vd->vdev_children == 0); 16313d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 16323d7072f8Seschrock VDEV_AUX_ERR_EXCEEDED); 16333d7072f8Seschrock } else { 1634069f55e2SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 16353d7072f8Seschrock } 1636fa9e4066Sahrens 163788ecc943SGeorge Wilson /* 163888ecc943SGeorge Wilson * For hole or missing vdevs we just return success. 163988ecc943SGeorge Wilson */ 164088ecc943SGeorge Wilson if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 164188ecc943SGeorge Wilson return (0); 164288ecc943SGeorge Wilson 1643573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 1644ea8dc4b6Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1645ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1646ea8dc4b6Seschrock VDEV_AUX_NONE); 1647ea8dc4b6Seschrock break; 1648ea8dc4b6Seschrock } 1649573ca77eSGeorge Wilson } 1650fa9e4066Sahrens 1651fa9e4066Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 16524263d13fSGeorge Wilson max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1653fa9e4066Sahrens 1654fa9e4066Sahrens if (vd->vdev_children == 0) { 1655fa9e4066Sahrens if (osize < SPA_MINDEVSIZE) { 1656ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1657ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1658be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1659fa9e4066Sahrens } 1660fa9e4066Sahrens psize = osize; 1661fa9e4066Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 16624263d13fSGeorge Wilson max_asize = max_osize - (VDEV_LABEL_START_SIZE + 16634263d13fSGeorge Wilson VDEV_LABEL_END_SIZE); 1664fa9e4066Sahrens } else { 1665ecc2d604Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1666fa9e4066Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1667ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1668ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1669be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1670fa9e4066Sahrens } 1671fa9e4066Sahrens psize = 0; 1672fa9e4066Sahrens asize = osize; 16734263d13fSGeorge Wilson max_asize = max_osize; 1674fa9e4066Sahrens } 1675fa9e4066Sahrens 1676fa9e4066Sahrens vd->vdev_psize = psize; 1677fa9e4066Sahrens 1678573ca77eSGeorge Wilson /* 1679c040c10cSSteven Hartland * Make sure the allocatable size hasn't shrunk too much. 1680573ca77eSGeorge Wilson */ 1681573ca77eSGeorge Wilson if (asize < vd->vdev_min_asize) { 1682573ca77eSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1683573ca77eSGeorge Wilson VDEV_AUX_BAD_LABEL); 1684be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1685573ca77eSGeorge Wilson } 1686573ca77eSGeorge Wilson 1687fa9e4066Sahrens if (vd->vdev_asize == 0) { 1688fa9e4066Sahrens /* 1689fa9e4066Sahrens * This is the first-ever open, so use the computed values. 16905711d393Sloli * For compatibility, a different ashift can be requested. 1691fa9e4066Sahrens */ 1692fa9e4066Sahrens vd->vdev_asize = asize; 16934263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 16945711d393Sloli if (vd->vdev_ashift == 0) { 16955711d393Sloli vd->vdev_ashift = ashift; /* use detected value */ 16965711d393Sloli } 16975711d393Sloli if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN || 16985711d393Sloli vd->vdev_ashift > ASHIFT_MAX)) { 16995711d393Sloli vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 17005711d393Sloli VDEV_AUX_BAD_ASHIFT); 17015711d393Sloli return (SET_ERROR(EDOM)); 17025711d393Sloli } 1703fa9e4066Sahrens } else { 1704fa9e4066Sahrens /* 17052384d9f8SGeorge Wilson * Detect if the alignment requirement has increased. 17062384d9f8SGeorge Wilson * We don't want to make the pool unavailable, just 17075711d393Sloli * post an event instead. 1708fa9e4066Sahrens */ 17092384d9f8SGeorge Wilson if (ashift > vd->vdev_top->vdev_ashift && 17102384d9f8SGeorge Wilson vd->vdev_ops->vdev_op_leaf) { 17115711d393Sloli zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT, 17125711d393Sloli spa, vd, NULL, NULL, 0, 0); 1713fa9e4066Sahrens } 17145711d393Sloli 17154263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1716573ca77eSGeorge Wilson } 1717fa9e4066Sahrens 1718573ca77eSGeorge Wilson /* 1719c040c10cSSteven Hartland * If all children are healthy we update asize if either: 1720c040c10cSSteven Hartland * The asize has increased, due to a device expansion caused by dynamic 1721c040c10cSSteven Hartland * LUN growth or vdev replacement, and automatic expansion is enabled; 1722c040c10cSSteven Hartland * making the additional space available. 1723c040c10cSSteven Hartland * 1724c040c10cSSteven Hartland * The asize has decreased, due to a device shrink usually caused by a 1725c040c10cSSteven Hartland * vdev replace with a smaller device. This ensures that calculations 1726c040c10cSSteven Hartland * based of max_asize and asize e.g. esize are always valid. It's safe 1727c040c10cSSteven Hartland * to do this as we've already validated that asize is greater than 1728c040c10cSSteven Hartland * vdev_min_asize. 1729573ca77eSGeorge Wilson */ 1730c040c10cSSteven Hartland if (vd->vdev_state == VDEV_STATE_HEALTHY && 1731c040c10cSSteven Hartland ((asize > vd->vdev_asize && 1732c040c10cSSteven Hartland (vd->vdev_expanding || spa->spa_autoexpand)) || 1733c040c10cSSteven Hartland (asize < vd->vdev_asize))) 1734573ca77eSGeorge Wilson vd->vdev_asize = asize; 1735fa9e4066Sahrens 1736573ca77eSGeorge Wilson vdev_set_min_asize(vd); 1737fa9e4066Sahrens 17380a4e9518Sgw /* 17390a4e9518Sgw * Ensure we can issue some IO before declaring the 17400a4e9518Sgw * vdev open for business. 17410a4e9518Sgw */ 1742e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 1743e14bb325SJeff Bonwick (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 174498d1cbfeSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 174598d1cbfeSGeorge Wilson VDEV_AUX_ERR_EXCEEDED); 17460a4e9518Sgw return (error); 17470a4e9518Sgw } 17480a4e9518Sgw 174981cd5c55SMatthew Ahrens /* 175081cd5c55SMatthew Ahrens * Track the min and max ashift values for normal data devices. 1751663207adSDon Brady * 1752663207adSDon Brady * DJB - TBD these should perhaps be tracked per allocation class 1753663207adSDon Brady * (e.g. spa_min_ashift is used to round up post compression buffers) 175481cd5c55SMatthew Ahrens */ 175581cd5c55SMatthew Ahrens if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1756663207adSDon Brady vd->vdev_alloc_bias == VDEV_BIAS_NONE && 1757663207adSDon Brady vd->vdev_aux == NULL) { 175881cd5c55SMatthew Ahrens if (vd->vdev_ashift > spa->spa_max_ashift) 175981cd5c55SMatthew Ahrens spa->spa_max_ashift = vd->vdev_ashift; 176081cd5c55SMatthew Ahrens if (vd->vdev_ashift < spa->spa_min_ashift) 176181cd5c55SMatthew Ahrens spa->spa_min_ashift = vd->vdev_ashift; 176281cd5c55SMatthew Ahrens } 176381cd5c55SMatthew Ahrens 1764088f3894Sahrens /* 1765088f3894Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 17668ad4d6ddSJeff Bonwick * resilver. But don't do this if we are doing a reopen for a scrub, 17678ad4d6ddSJeff Bonwick * since this would just restart the scrub we are already doing. 1768088f3894Sahrens */ 17698ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1770e4c795beSTom Caputi vdev_resilver_needed(vd, NULL, NULL)) { 1771e4c795beSTom Caputi if (dsl_scan_resilvering(spa->spa_dsl_pool) && 1772e4c795beSTom Caputi spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) 1773e4c795beSTom Caputi vdev_set_deferred_resilver(spa, vd); 1774e4c795beSTom Caputi else 1775e4c795beSTom Caputi spa_async_request(spa, SPA_ASYNC_RESILVER); 1776e4c795beSTom Caputi } 1777088f3894Sahrens 1778fa9e4066Sahrens return (0); 1779fa9e4066Sahrens } 1780fa9e4066Sahrens 1781560e6e96Seschrock /* 1782560e6e96Seschrock * Called once the vdevs are all opened, this routine validates the label 17836f793812SPavel Zakharov * contents. This needs to be done before vdev_load() so that we don't 17843d7072f8Seschrock * inadvertently do repair I/Os to the wrong device. 1785560e6e96Seschrock * 1786560e6e96Seschrock * This function will only return failure if one of the vdevs indicates that it 1787560e6e96Seschrock * has since been destroyed or exported. This is only possible if 1788560e6e96Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1789560e6e96Seschrock * will be updated but the function will return 0. 1790560e6e96Seschrock */ 1791560e6e96Seschrock int 17926f793812SPavel Zakharov vdev_validate(vdev_t *vd) 1793560e6e96Seschrock { 1794560e6e96Seschrock spa_t *spa = vd->vdev_spa; 1795560e6e96Seschrock nvlist_t *label; 17966f793812SPavel Zakharov uint64_t guid = 0, aux_guid = 0, top_guid; 1797560e6e96Seschrock uint64_t state; 17986f793812SPavel Zakharov nvlist_t *nvl; 17996f793812SPavel Zakharov uint64_t txg; 1800560e6e96Seschrock 18016f793812SPavel Zakharov if (vdev_validate_skip) 18026f793812SPavel Zakharov return (0); 18036f793812SPavel Zakharov 18046f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 18056f793812SPavel Zakharov if (vdev_validate(vd->vdev_child[c]) != 0) 1806be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1807560e6e96Seschrock 1808b5989ec7Seschrock /* 1809b5989ec7Seschrock * If the device has already failed, or was marked offline, don't do 1810b5989ec7Seschrock * any further validation. Otherwise, label I/O will fail and we will 1811b5989ec7Seschrock * overwrite the previous state. 1812b5989ec7Seschrock */ 18136f793812SPavel Zakharov if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 18146f793812SPavel Zakharov return (0); 1815560e6e96Seschrock 18166f793812SPavel Zakharov /* 18176f793812SPavel Zakharov * If we are performing an extreme rewind, we allow for a label that 18186f793812SPavel Zakharov * was modified at a point after the current txg. 1819d1de72cfSPavel Zakharov * If config lock is not held do not check for the txg. spa_sync could 1820d1de72cfSPavel Zakharov * be updating the vdev's label before updating spa_last_synced_txg. 18216f793812SPavel Zakharov */ 1822d1de72cfSPavel Zakharov if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1823d1de72cfSPavel Zakharov spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 18246f793812SPavel Zakharov txg = UINT64_MAX; 18256f793812SPavel Zakharov else 18266f793812SPavel Zakharov txg = spa_last_synced_txg(spa); 1827560e6e96Seschrock 18286f793812SPavel Zakharov if ((label = vdev_label_read_config(vd, txg)) == NULL) { 18296f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 18306f793812SPavel Zakharov VDEV_AUX_BAD_LABEL); 1831b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1832b6bf6e15SPavel Zakharov "txg %llu", (u_longlong_t)txg); 18336f793812SPavel Zakharov return (0); 18346f793812SPavel Zakharov } 18351195e687SMark J Musante 18366f793812SPavel Zakharov /* 18376f793812SPavel Zakharov * Determine if this vdev has been split off into another 18386f793812SPavel Zakharov * pool. If so, then refuse to open it. 18396f793812SPavel Zakharov */ 18406f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 18416f793812SPavel Zakharov &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 18426f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18436f793812SPavel Zakharov VDEV_AUX_SPLIT_POOL); 18446f793812SPavel Zakharov nvlist_free(label); 18456f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 18466f793812SPavel Zakharov return (0); 18476f793812SPavel Zakharov } 1848560e6e96Seschrock 18496f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 18506f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18516f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18526f793812SPavel Zakharov nvlist_free(label); 18536f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18546f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 18556f793812SPavel Zakharov return (0); 18566f793812SPavel Zakharov } 18571195e687SMark J Musante 18586f793812SPavel Zakharov /* 18596f793812SPavel Zakharov * If config is not trusted then ignore the spa guid check. This is 18606f793812SPavel Zakharov * necessary because if the machine crashed during a re-guid the new 18616f793812SPavel Zakharov * guid might have been written to all of the vdev labels, but not the 18626f793812SPavel Zakharov * cached config. The check will be performed again once we have the 18636f793812SPavel Zakharov * trusted config from the MOS. 18646f793812SPavel Zakharov */ 18656f793812SPavel Zakharov if (spa->spa_trust_config && guid != spa_guid(spa)) { 18666f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18676f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18686f793812SPavel Zakharov nvlist_free(label); 18696f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 18706f793812SPavel Zakharov "match config (%llu != %llu)", (u_longlong_t)guid, 18716f793812SPavel Zakharov (u_longlong_t)spa_guid(spa)); 18726f793812SPavel Zakharov return (0); 18736f793812SPavel Zakharov } 18746f793812SPavel Zakharov 18756f793812SPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 18766f793812SPavel Zakharov != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 18776f793812SPavel Zakharov &aux_guid) != 0) 18786f793812SPavel Zakharov aux_guid = 0; 18796f793812SPavel Zakharov 18806f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 18816f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18826f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18836f793812SPavel Zakharov nvlist_free(label); 18846f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18856f793812SPavel Zakharov ZPOOL_CONFIG_GUID); 18866f793812SPavel Zakharov return (0); 18876f793812SPavel Zakharov } 18886f793812SPavel Zakharov 18896f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 18906f793812SPavel Zakharov != 0) { 18916f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18926f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18936f793812SPavel Zakharov nvlist_free(label); 18946f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18956f793812SPavel Zakharov ZPOOL_CONFIG_TOP_GUID); 18966f793812SPavel Zakharov return (0); 18976f793812SPavel Zakharov } 18986f793812SPavel Zakharov 18996f793812SPavel Zakharov /* 19006f793812SPavel Zakharov * If this vdev just became a top-level vdev because its sibling was 19016f793812SPavel Zakharov * detached, it will have adopted the parent's vdev guid -- but the 19026f793812SPavel Zakharov * label may or may not be on disk yet. Fortunately, either version 19036f793812SPavel Zakharov * of the label will have the same top guid, so if we're a top-level 19046f793812SPavel Zakharov * vdev, we can safely compare to that instead. 19056f793812SPavel Zakharov * However, if the config comes from a cachefile that failed to update 19066f793812SPavel Zakharov * after the detach, a top-level vdev will appear as a non top-level 19076f793812SPavel Zakharov * vdev in the config. Also relax the constraints if we perform an 19086f793812SPavel Zakharov * extreme rewind. 19096f793812SPavel Zakharov * 19106f793812SPavel Zakharov * If we split this vdev off instead, then we also check the 19116f793812SPavel Zakharov * original pool's guid. We don't want to consider the vdev 19126f793812SPavel Zakharov * corrupt if it is partway through a split operation. 19136f793812SPavel Zakharov */ 19146f793812SPavel Zakharov if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 19156f793812SPavel Zakharov boolean_t mismatch = B_FALSE; 19166f793812SPavel Zakharov if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 19176f793812SPavel Zakharov if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 19186f793812SPavel Zakharov mismatch = B_TRUE; 19196f793812SPavel Zakharov } else { 19206f793812SPavel Zakharov if (vd->vdev_guid != top_guid && 19216f793812SPavel Zakharov vd->vdev_top->vdev_guid != guid) 19226f793812SPavel Zakharov mismatch = B_TRUE; 1923560e6e96Seschrock } 1924560e6e96Seschrock 19256f793812SPavel Zakharov if (mismatch) { 1926560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1927560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1928560e6e96Seschrock nvlist_free(label); 19296f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: config guid " 19306f793812SPavel Zakharov "doesn't match label guid"); 19316f793812SPavel Zakharov vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 19326f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 19336f793812SPavel Zakharov (u_longlong_t)vd->vdev_top->vdev_guid); 19346f793812SPavel Zakharov vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 19356f793812SPavel Zakharov "aux_guid %llu", (u_longlong_t)guid, 19366f793812SPavel Zakharov (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1937560e6e96Seschrock return (0); 1938560e6e96Seschrock } 19396f793812SPavel Zakharov } 1940560e6e96Seschrock 19416f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 19426f793812SPavel Zakharov &state) != 0) { 19436f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 19446f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 1945560e6e96Seschrock nvlist_free(label); 19466f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 19476f793812SPavel Zakharov ZPOOL_CONFIG_POOL_STATE); 19486f793812SPavel Zakharov return (0); 19496f793812SPavel Zakharov } 1950560e6e96Seschrock 19516f793812SPavel Zakharov nvlist_free(label); 19526f793812SPavel Zakharov 19536f793812SPavel Zakharov /* 19546f793812SPavel Zakharov * If this is a verbatim import, no need to check the 19556f793812SPavel Zakharov * state of the pool. 19566f793812SPavel Zakharov */ 19576f793812SPavel Zakharov if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 19586f793812SPavel Zakharov spa_load_state(spa) == SPA_LOAD_OPEN && 19596f793812SPavel Zakharov state != POOL_STATE_ACTIVE) { 19606f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 19616f793812SPavel Zakharov "for spa %s", (u_longlong_t)state, spa->spa_name); 19626f793812SPavel Zakharov return (SET_ERROR(EBADF)); 19636f793812SPavel Zakharov } 19646f793812SPavel Zakharov 19656f793812SPavel Zakharov /* 19666f793812SPavel Zakharov * If we were able to open and validate a vdev that was 19676f793812SPavel Zakharov * previously marked permanently unavailable, clear that state 19686f793812SPavel Zakharov * now. 19696f793812SPavel Zakharov */ 19706f793812SPavel Zakharov if (vd->vdev_not_present) 19716f793812SPavel Zakharov vd->vdev_not_present = 0; 19726f793812SPavel Zakharov 19736f793812SPavel Zakharov return (0); 19746f793812SPavel Zakharov } 19756f793812SPavel Zakharov 19766f793812SPavel Zakharov static void 19776f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 19786f793812SPavel Zakharov { 19796f793812SPavel Zakharov if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 19806f793812SPavel Zakharov if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 19816f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 19826f793812SPavel Zakharov "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 19836f793812SPavel Zakharov dvd->vdev_path, svd->vdev_path); 19846f793812SPavel Zakharov spa_strfree(dvd->vdev_path); 19856f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 19863ee8c80cSPavel Zakharov } 19876f793812SPavel Zakharov } else if (svd->vdev_path != NULL) { 19886f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 19896f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 19906f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 19916f793812SPavel Zakharov } 19926f793812SPavel Zakharov } 1993560e6e96Seschrock 19946f793812SPavel Zakharov /* 19956f793812SPavel Zakharov * Recursively copy vdev paths from one vdev to another. Source and destination 19966f793812SPavel Zakharov * vdev trees must have same geometry otherwise return error. Intended to copy 19976f793812SPavel Zakharov * paths from userland config into MOS config. 19986f793812SPavel Zakharov */ 19996f793812SPavel Zakharov int 20006f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 20016f793812SPavel Zakharov { 20026f793812SPavel Zakharov if ((svd->vdev_ops == &vdev_missing_ops) || 20036f793812SPavel Zakharov (svd->vdev_ishole && dvd->vdev_ishole) || 20046f793812SPavel Zakharov (dvd->vdev_ops == &vdev_indirect_ops)) 20056f793812SPavel Zakharov return (0); 20066f793812SPavel Zakharov 20076f793812SPavel Zakharov if (svd->vdev_ops != dvd->vdev_ops) { 20086f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 20096f793812SPavel Zakharov svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 20106f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 20116f793812SPavel Zakharov } 20126f793812SPavel Zakharov 20136f793812SPavel Zakharov if (svd->vdev_guid != dvd->vdev_guid) { 20146f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 20156f793812SPavel Zakharov "%llu)", (u_longlong_t)svd->vdev_guid, 20166f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid); 20176f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 20186f793812SPavel Zakharov } 20196f793812SPavel Zakharov 20206f793812SPavel Zakharov if (svd->vdev_children != dvd->vdev_children) { 20216f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 20226f793812SPavel Zakharov "%llu != %llu", (u_longlong_t)svd->vdev_children, 20236f793812SPavel Zakharov (u_longlong_t)dvd->vdev_children); 20246f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 202551ece835Seschrock } 2026560e6e96Seschrock 20276f793812SPavel Zakharov for (uint64_t i = 0; i < svd->vdev_children; i++) { 20286f793812SPavel Zakharov int error = vdev_copy_path_strict(svd->vdev_child[i], 20296f793812SPavel Zakharov dvd->vdev_child[i]); 20306f793812SPavel Zakharov if (error != 0) 20316f793812SPavel Zakharov return (error); 20326f793812SPavel Zakharov } 20336f793812SPavel Zakharov 20346f793812SPavel Zakharov if (svd->vdev_ops->vdev_op_leaf) 20356f793812SPavel Zakharov vdev_copy_path_impl(svd, dvd); 20366f793812SPavel Zakharov 2037560e6e96Seschrock return (0); 2038560e6e96Seschrock } 2039560e6e96Seschrock 20406f793812SPavel Zakharov static void 20416f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 20426f793812SPavel Zakharov { 20436f793812SPavel Zakharov ASSERT(stvd->vdev_top == stvd); 20446f793812SPavel Zakharov ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 20456f793812SPavel Zakharov 20466f793812SPavel Zakharov for (uint64_t i = 0; i < dvd->vdev_children; i++) { 20476f793812SPavel Zakharov vdev_copy_path_search(stvd, dvd->vdev_child[i]); 20486f793812SPavel Zakharov } 20496f793812SPavel Zakharov 20506f793812SPavel Zakharov if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 20516f793812SPavel Zakharov return; 20526f793812SPavel Zakharov 20536f793812SPavel Zakharov /* 20546f793812SPavel Zakharov * The idea here is that while a vdev can shift positions within 20556f793812SPavel Zakharov * a top vdev (when replacing, attaching mirror, etc.) it cannot 20566f793812SPavel Zakharov * step outside of it. 20576f793812SPavel Zakharov */ 20586f793812SPavel Zakharov vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 20596f793812SPavel Zakharov 20606f793812SPavel Zakharov if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 20616f793812SPavel Zakharov return; 20626f793812SPavel Zakharov 20636f793812SPavel Zakharov ASSERT(vd->vdev_ops->vdev_op_leaf); 20646f793812SPavel Zakharov 20656f793812SPavel Zakharov vdev_copy_path_impl(vd, dvd); 20666f793812SPavel Zakharov } 20676f793812SPavel Zakharov 20686f793812SPavel Zakharov /* 20696f793812SPavel Zakharov * Recursively copy vdev paths from one root vdev to another. Source and 20706f793812SPavel Zakharov * destination vdev trees may differ in geometry. For each destination leaf 20716f793812SPavel Zakharov * vdev, search a vdev with the same guid and top vdev id in the source. 20726f793812SPavel Zakharov * Intended to copy paths from userland config into MOS config. 20736f793812SPavel Zakharov */ 20746f793812SPavel Zakharov void 20756f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 20766f793812SPavel Zakharov { 20776f793812SPavel Zakharov uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 20786f793812SPavel Zakharov ASSERT(srvd->vdev_ops == &vdev_root_ops); 20796f793812SPavel Zakharov ASSERT(drvd->vdev_ops == &vdev_root_ops); 20806f793812SPavel Zakharov 20816f793812SPavel Zakharov for (uint64_t i = 0; i < children; i++) { 20826f793812SPavel Zakharov vdev_copy_path_search(srvd->vdev_child[i], 20836f793812SPavel Zakharov drvd->vdev_child[i]); 20846f793812SPavel Zakharov } 20856f793812SPavel Zakharov } 20866f793812SPavel Zakharov 2087fa9e4066Sahrens /* 2088fa9e4066Sahrens * Close a virtual device. 2089fa9e4066Sahrens */ 2090fa9e4066Sahrens void 2091fa9e4066Sahrens vdev_close(vdev_t *vd) 2092fa9e4066Sahrens { 20938ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 2094095bcd66SGeorge Wilson vdev_t *pvd = vd->vdev_parent; 20958ad4d6ddSJeff Bonwick 20968ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 20978ad4d6ddSJeff Bonwick 20981195e687SMark J Musante /* 20991195e687SMark J Musante * If our parent is reopening, then we are as well, unless we are 21001195e687SMark J Musante * going offline. 21011195e687SMark J Musante */ 2102095bcd66SGeorge Wilson if (pvd != NULL && pvd->vdev_reopening) 21031195e687SMark J Musante vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 2104095bcd66SGeorge Wilson 2105fa9e4066Sahrens vd->vdev_ops->vdev_op_close(vd); 2106fa9e4066Sahrens 21073d7072f8Seschrock vdev_cache_purge(vd); 2108fa9e4066Sahrens 2109560e6e96Seschrock /* 2110573ca77eSGeorge Wilson * We record the previous state before we close it, so that if we are 2111560e6e96Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 2112560e6e96Seschrock * it's still faulted. 2113560e6e96Seschrock */ 2114560e6e96Seschrock vd->vdev_prevstate = vd->vdev_state; 2115560e6e96Seschrock 2116fa9e4066Sahrens if (vd->vdev_offline) 2117fa9e4066Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 2118fa9e4066Sahrens else 2119fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 2120ea8dc4b6Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2121fa9e4066Sahrens } 2122fa9e4066Sahrens 2123dcba9f3fSGeorge Wilson void 2124dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd) 2125dcba9f3fSGeorge Wilson { 2126dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 2127dcba9f3fSGeorge Wilson 2128dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 2129dcba9f3fSGeorge Wilson if (spa->spa_state == POOL_STATE_UNINITIALIZED) 2130dcba9f3fSGeorge Wilson return; 2131dcba9f3fSGeorge Wilson 2132dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 2133dcba9f3fSGeorge Wilson vdev_hold(vd->vdev_child[c]); 2134dcba9f3fSGeorge Wilson 2135dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2136dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_hold(vd); 2137dcba9f3fSGeorge Wilson } 2138dcba9f3fSGeorge Wilson 2139dcba9f3fSGeorge Wilson void 2140dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd) 2141dcba9f3fSGeorge Wilson { 2142dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 2143dcba9f3fSGeorge Wilson 2144dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 2145dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 2146dcba9f3fSGeorge Wilson vdev_rele(vd->vdev_child[c]); 2147dcba9f3fSGeorge Wilson 2148dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2149dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_rele(vd); 2150dcba9f3fSGeorge Wilson } 2151dcba9f3fSGeorge Wilson 2152095bcd66SGeorge Wilson /* 2153095bcd66SGeorge Wilson * Reopen all interior vdevs and any unopened leaves. We don't actually 2154095bcd66SGeorge Wilson * reopen leaf vdevs which had previously been opened as they might deadlock 2155095bcd66SGeorge Wilson * on the spa_config_lock. Instead we only obtain the leaf's physical size. 2156095bcd66SGeorge Wilson * If the leaf has never been opened then open it, as usual. 2157095bcd66SGeorge Wilson */ 2158fa9e4066Sahrens void 2159ea8dc4b6Seschrock vdev_reopen(vdev_t *vd) 2160fa9e4066Sahrens { 2161ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 2162fa9e4066Sahrens 2163e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2164ea8dc4b6Seschrock 21651195e687SMark J Musante /* set the reopening flag unless we're taking the vdev offline */ 21661195e687SMark J Musante vd->vdev_reopening = !vd->vdev_offline; 2167fa9e4066Sahrens vdev_close(vd); 2168fa9e4066Sahrens (void) vdev_open(vd); 2169fa9e4066Sahrens 217039c23413Seschrock /* 217139c23413Seschrock * Call vdev_validate() here to make sure we have the same device. 217239c23413Seschrock * Otherwise, a device with an invalid label could be successfully 217339c23413Seschrock * opened in response to vdev_reopen(). 217439c23413Seschrock */ 2175c5904d13Seschrock if (vd->vdev_aux) { 2176c5904d13Seschrock (void) vdev_validate_aux(vd); 2177e14bb325SJeff Bonwick if (vdev_readable(vd) && vdev_writeable(vd) && 21786809eb4eSEric Schrock vd->vdev_aux == &spa->spa_l2cache && 2179573ca77eSGeorge Wilson !l2arc_vdev_present(vd)) 2180573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 2181c5904d13Seschrock } else { 21826f793812SPavel Zakharov (void) vdev_validate(vd); 2183c5904d13Seschrock } 218439c23413Seschrock 2185fa9e4066Sahrens /* 21863d7072f8Seschrock * Reassess parent vdev's health. 2187fa9e4066Sahrens */ 21883d7072f8Seschrock vdev_propagate_state(vd); 2189fa9e4066Sahrens } 2190fa9e4066Sahrens 2191fa9e4066Sahrens int 219299653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2193fa9e4066Sahrens { 2194fa9e4066Sahrens int error; 2195fa9e4066Sahrens 2196fa9e4066Sahrens /* 2197fa9e4066Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 2198fa9e4066Sahrens * For a create, however, we want to fail the request if 2199fa9e4066Sahrens * there are any components we can't open. 2200fa9e4066Sahrens */ 2201fa9e4066Sahrens error = vdev_open(vd); 2202fa9e4066Sahrens 2203fa9e4066Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2204fa9e4066Sahrens vdev_close(vd); 2205fa9e4066Sahrens return (error ? error : ENXIO); 2206fa9e4066Sahrens } 2207fa9e4066Sahrens 2208fa9e4066Sahrens /* 22090713e232SGeorge Wilson * Recursively load DTLs and initialize all labels. 2210fa9e4066Sahrens */ 22110713e232SGeorge Wilson if ((error = vdev_dtl_load(vd)) != 0 || 22120713e232SGeorge Wilson (error = vdev_label_init(vd, txg, isreplacing ? 221339c23413Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2214fa9e4066Sahrens vdev_close(vd); 2215fa9e4066Sahrens return (error); 2216fa9e4066Sahrens } 2217fa9e4066Sahrens 2218fa9e4066Sahrens return (0); 2219fa9e4066Sahrens } 2220fa9e4066Sahrens 22210e34b6a7Sbonwick void 2222573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd) 2223fa9e4066Sahrens { 222486714001SSerapheim Dimitropoulos uint64_t asize = vd->vdev_asize; 2225a0b03b16SSerapheim Dimitropoulos uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2226b4bf0cf0SDon Brady uint64_t ms_shift; 222786714001SSerapheim Dimitropoulos 2228a3874b8bSToomas Soome /* BEGIN CSTYLED */ 2229fa9e4066Sahrens /* 2230b4bf0cf0SDon Brady * There are two dimensions to the metaslab sizing calculation: 2231b4bf0cf0SDon Brady * the size of the metaslab and the count of metaslabs per vdev. 2232b4bf0cf0SDon Brady * 2233a0b03b16SSerapheim Dimitropoulos * The default values used below are a good balance between memory 2234a0b03b16SSerapheim Dimitropoulos * usage (larger metaslab size means more memory needed for loaded 2235a0b03b16SSerapheim Dimitropoulos * metaslabs; more metaslabs means more memory needed for the 2236a0b03b16SSerapheim Dimitropoulos * metaslab_t structs), metaslab load time (larger metaslabs take 2237a0b03b16SSerapheim Dimitropoulos * longer to load), and metaslab sync time (more metaslabs means 2238a0b03b16SSerapheim Dimitropoulos * more time spent syncing all of them). 2239a0b03b16SSerapheim Dimitropoulos * 2240a0b03b16SSerapheim Dimitropoulos * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2241a0b03b16SSerapheim Dimitropoulos * The range of the dimensions are as follows: 2242a0b03b16SSerapheim Dimitropoulos * 2243a0b03b16SSerapheim Dimitropoulos * 2^29 <= ms_size <= 2^34 2244b4bf0cf0SDon Brady * 16 <= ms_count <= 131,072 2245b4bf0cf0SDon Brady * 2246b4bf0cf0SDon Brady * On the lower end of vdev sizes, we aim for metaslabs sizes of 2247b4bf0cf0SDon Brady * at least 512MB (2^29) to minimize fragmentation effects when 2248b4bf0cf0SDon Brady * testing with smaller devices. However, the count constraint 2249b4bf0cf0SDon Brady * of at least 16 metaslabs will override this minimum size goal. 2250b4bf0cf0SDon Brady * 2251b4bf0cf0SDon Brady * On the upper end of vdev sizes, we aim for a maximum metaslab 2252a0b03b16SSerapheim Dimitropoulos * size of 16GB. However, we will cap the total count to 2^17 2253a0b03b16SSerapheim Dimitropoulos * metaslabs to keep our memory footprint in check and let the 2254a0b03b16SSerapheim Dimitropoulos * metaslab size grow from there if that limit is hit. 2255b4bf0cf0SDon Brady * 2256b4bf0cf0SDon Brady * The net effect of applying above constrains is summarized below. 2257b4bf0cf0SDon Brady * 2258663207adSDon Brady * vdev size metaslab count 2259a0b03b16SSerapheim Dimitropoulos * --------------|----------------- 2260663207adSDon Brady * < 8GB ~16 2261663207adSDon Brady * 8GB - 100GB one per 512MB 2262663207adSDon Brady * 100GB - 3TB ~200 2263663207adSDon Brady * 3TB - 2PB one per 16GB 2264663207adSDon Brady * > 2PB ~131,072 2265a0b03b16SSerapheim Dimitropoulos * -------------------------------- 2266a0b03b16SSerapheim Dimitropoulos * 2267a0b03b16SSerapheim Dimitropoulos * Finally, note that all of the above calculate the initial 2268a0b03b16SSerapheim Dimitropoulos * number of metaslabs. Expanding a top-level vdev will result 2269a0b03b16SSerapheim Dimitropoulos * in additional metaslabs being allocated making it possible 2270a0b03b16SSerapheim Dimitropoulos * to exceed the zfs_vdev_ms_count_limit. 2271a0b03b16SSerapheim Dimitropoulos */ 2272a3874b8bSToomas Soome /* END CSTYLED */ 2273a0b03b16SSerapheim Dimitropoulos 2274a0b03b16SSerapheim Dimitropoulos if (ms_count < zfs_vdev_min_ms_count) 2275a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2276a0b03b16SSerapheim Dimitropoulos else if (ms_count > zfs_vdev_default_ms_count) 2277a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2278b4bf0cf0SDon Brady else 2279a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_default_ms_shift; 2280b4bf0cf0SDon Brady 2281b4bf0cf0SDon Brady if (ms_shift < SPA_MAXBLOCKSHIFT) { 2282b4bf0cf0SDon Brady ms_shift = SPA_MAXBLOCKSHIFT; 2283a0b03b16SSerapheim Dimitropoulos } else if (ms_shift > zfs_vdev_max_ms_shift) { 2284a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_max_ms_shift; 2285b4bf0cf0SDon Brady /* cap the total count to constrain memory footprint */ 2286a0b03b16SSerapheim Dimitropoulos if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2287a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 228886714001SSerapheim Dimitropoulos } 228986714001SSerapheim Dimitropoulos 229086714001SSerapheim Dimitropoulos vd->vdev_ms_shift = ms_shift; 229186714001SSerapheim Dimitropoulos ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2292fa9e4066Sahrens } 2293fa9e4066Sahrens 2294fa9e4066Sahrens void 2295ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2296fa9e4066Sahrens { 2297ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 22985cabbc6bSPrashanth Sreenivasa /* indirect vdevs don't have metaslabs or dtls */ 22995cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd) || flags == 0); 2300ecc2d604Sbonwick ASSERT(ISP2(flags)); 2301f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 2302fa9e4066Sahrens 2303ecc2d604Sbonwick if (flags & VDD_METASLAB) 2304ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2305ecc2d604Sbonwick 2306ecc2d604Sbonwick if (flags & VDD_DTL) 2307ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2308ecc2d604Sbonwick 2309ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2310fa9e4066Sahrens } 2311fa9e4066Sahrens 23120713e232SGeorge Wilson void 23130713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 23140713e232SGeorge Wilson { 23150713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 23160713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 23170713e232SGeorge Wilson 23180713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 23190713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 23200713e232SGeorge Wilson } 23210713e232SGeorge Wilson 23228ad4d6ddSJeff Bonwick /* 23238ad4d6ddSJeff Bonwick * DTLs. 23248ad4d6ddSJeff Bonwick * 23258ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 23269fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 23278ad4d6ddSJeff Bonwick * 23288ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 23298ad4d6ddSJeff Bonwick * 23308ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 23318ad4d6ddSJeff Bonwick * 23328ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 23338ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 23348ad4d6ddSJeff Bonwick * txgs that was scrubbed. 23358ad4d6ddSJeff Bonwick * 23368ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 23378ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 23388ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 23398ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 23408ad4d6ddSJeff Bonwick * determine whether a device can be detached. 23418ad4d6ddSJeff Bonwick * 23428ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 23438ad4d6ddSJeff Bonwick * either has the data or it doesn't. 23448ad4d6ddSJeff Bonwick * 23458ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 23468ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 23478ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 23488ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 23498ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 23508ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 23518ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 23528ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 23538ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 23548ad4d6ddSJeff Bonwick * 23558ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 23568ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 23578ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 23588ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 23598ad4d6ddSJeff Bonwick */ 2360fa9e4066Sahrens void 23618ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2362fa9e4066Sahrens { 23630713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23648ad4d6ddSJeff Bonwick 23658ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 23668ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2367f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 23688ad4d6ddSJeff Bonwick 23695cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 23700713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 23710713e232SGeorge Wilson range_tree_add(rt, txg, size); 23725cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2373fa9e4066Sahrens } 2374fa9e4066Sahrens 23758ad4d6ddSJeff Bonwick boolean_t 23768ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2377fa9e4066Sahrens { 23780713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23798ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 2380fa9e4066Sahrens 23818ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 23828ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2383fa9e4066Sahrens 23845cabbc6bSPrashanth Sreenivasa /* 23855cabbc6bSPrashanth Sreenivasa * While we are loading the pool, the DTLs have not been loaded yet. 23865cabbc6bSPrashanth Sreenivasa * Ignore the DTLs and try all devices. This avoids a recursive 23875cabbc6bSPrashanth Sreenivasa * mutex enter on the vdev_dtl_lock, and also makes us try hard 23885cabbc6bSPrashanth Sreenivasa * when loading the pool (relying on the checksum to ensure that 23895cabbc6bSPrashanth Sreenivasa * we get the right data -- note that we while loading, we are 23905cabbc6bSPrashanth Sreenivasa * only reading the MOS, which is always checksummed). 23915cabbc6bSPrashanth Sreenivasa */ 23925cabbc6bSPrashanth Sreenivasa if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 23935cabbc6bSPrashanth Sreenivasa return (B_FALSE); 23945cabbc6bSPrashanth Sreenivasa 23955cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 239686714001SSerapheim Dimitropoulos if (!range_tree_is_empty(rt)) 23970713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 23985cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2399fa9e4066Sahrens 2400fa9e4066Sahrens return (dirty); 2401fa9e4066Sahrens } 2402fa9e4066Sahrens 24038ad4d6ddSJeff Bonwick boolean_t 24048ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 24058ad4d6ddSJeff Bonwick { 24060713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 24078ad4d6ddSJeff Bonwick boolean_t empty; 24088ad4d6ddSJeff Bonwick 24095cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 241086714001SSerapheim Dimitropoulos empty = range_tree_is_empty(rt); 24115cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 24128ad4d6ddSJeff Bonwick 24138ad4d6ddSJeff Bonwick return (empty); 24148ad4d6ddSJeff Bonwick } 24158ad4d6ddSJeff Bonwick 2416a3874b8bSToomas Soome /* 2417a3874b8bSToomas Soome * Returns B_TRUE if vdev determines offset needs to be resilvered. 2418a3874b8bSToomas Soome */ 2419a3874b8bSToomas Soome boolean_t 2420a3874b8bSToomas Soome vdev_dtl_need_resilver(vdev_t *vd, uint64_t offset, size_t psize) 2421a3874b8bSToomas Soome { 2422a3874b8bSToomas Soome ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2423a3874b8bSToomas Soome 2424a3874b8bSToomas Soome if (vd->vdev_ops->vdev_op_need_resilver == NULL || 2425a3874b8bSToomas Soome vd->vdev_ops->vdev_op_leaf) 2426a3874b8bSToomas Soome return (B_TRUE); 2427a3874b8bSToomas Soome 2428a3874b8bSToomas Soome return (vd->vdev_ops->vdev_op_need_resilver(vd, offset, psize)); 2429a3874b8bSToomas Soome } 2430a3874b8bSToomas Soome 2431b4952e17SGeorge Wilson /* 2432b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 2433b4952e17SGeorge Wilson */ 2434b4952e17SGeorge Wilson static uint64_t 2435b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 2436b4952e17SGeorge Wilson { 24370713e232SGeorge Wilson range_seg_t *rs; 2438b4952e17SGeorge Wilson 2439b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 24400713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2441b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2442b4952e17SGeorge Wilson 24430713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 24440713e232SGeorge Wilson return (rs->rs_start - 1); 2445b4952e17SGeorge Wilson } 2446b4952e17SGeorge Wilson 2447b4952e17SGeorge Wilson /* 2448b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 2449b4952e17SGeorge Wilson */ 2450b4952e17SGeorge Wilson static uint64_t 2451b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 2452b4952e17SGeorge Wilson { 24530713e232SGeorge Wilson range_seg_t *rs; 2454b4952e17SGeorge Wilson 2455b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 24560713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2457b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2458b4952e17SGeorge Wilson 24590713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 24600713e232SGeorge Wilson return (rs->rs_end); 2461b4952e17SGeorge Wilson } 2462b4952e17SGeorge Wilson 2463b4952e17SGeorge Wilson /* 2464b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 2465b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 2466b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 2467b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 2468b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 2469b4952e17SGeorge Wilson * excise the DTLs. 2470b4952e17SGeorge Wilson */ 2471b4952e17SGeorge Wilson static boolean_t 2472b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 2473b4952e17SGeorge Wilson { 2474b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 2475b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2476b4952e17SGeorge Wilson 2477b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 2478b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2479b4952e17SGeorge Wilson 24802d2f193aSMatthew Ahrens if (vd->vdev_state < VDEV_STATE_DEGRADED) 24812d2f193aSMatthew Ahrens return (B_FALSE); 24822d2f193aSMatthew Ahrens 2483e4c795beSTom Caputi if (vd->vdev_resilver_deferred) 2484e4c795beSTom Caputi return (B_FALSE); 2485e4c795beSTom Caputi 2486b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 248786714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2488b4952e17SGeorge Wilson return (B_TRUE); 2489b4952e17SGeorge Wilson 2490b4952e17SGeorge Wilson /* 2491b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 2492b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 2493b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 2494b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 2495b4952e17SGeorge Wilson * for excision. 2496b4952e17SGeorge Wilson */ 2497b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2498b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2499b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2500b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2501b4952e17SGeorge Wilson return (B_TRUE); 2502b4952e17SGeorge Wilson } 2503b4952e17SGeorge Wilson return (B_FALSE); 2504b4952e17SGeorge Wilson } 2505b4952e17SGeorge Wilson 2506fa9e4066Sahrens /* 2507fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 2508fa9e4066Sahrens */ 2509fa9e4066Sahrens void 2510fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2511fa9e4066Sahrens { 2512ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 25138ad4d6ddSJeff Bonwick avl_tree_t reftree; 25148ad4d6ddSJeff Bonwick int minref; 2515fa9e4066Sahrens 25168ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2517fa9e4066Sahrens 25188ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 25198ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 25208ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 25218ad4d6ddSJeff Bonwick 25225cabbc6bSPrashanth Sreenivasa if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 25238ad4d6ddSJeff Bonwick return; 25248ad4d6ddSJeff Bonwick 25258ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 25263f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 25273f9d6ad7SLin Ling 2528fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 2529b4952e17SGeorge Wilson 2530b4952e17SGeorge Wilson /* 2531b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 2532b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 2533b4952e17SGeorge Wilson * excise regions on vdevs that were available during 2534b4952e17SGeorge Wilson * the entire duration of this scan. 2535b4952e17SGeorge Wilson */ 2536088f3894Sahrens if (scrub_txg != 0 && 25373f9d6ad7SLin Ling (spa->spa_scrub_started || 2538b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2539b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 2540088f3894Sahrens /* 2541088f3894Sahrens * We completed a scrub up to scrub_txg. If we 2542088f3894Sahrens * did it without rebooting, then the scrub dtl 2543088f3894Sahrens * will be valid, so excise the old region and 2544088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 2545088f3894Sahrens * dtl as-is if there was an error. 25468ad4d6ddSJeff Bonwick * 25478ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 25488ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 25498ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 25508ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 25518ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 25528ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 25538ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 25548ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 25558ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 2556088f3894Sahrens */ 25570713e232SGeorge Wilson space_reftree_create(&reftree); 25580713e232SGeorge Wilson space_reftree_add_map(&reftree, 25590713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 25600713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 25610713e232SGeorge Wilson space_reftree_add_map(&reftree, 25620713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 25630713e232SGeorge Wilson space_reftree_generate_map(&reftree, 25640713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 25650713e232SGeorge Wilson space_reftree_destroy(&reftree); 2566fa9e4066Sahrens } 25670713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 25680713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 25690713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2570fa9e4066Sahrens if (scrub_done) 25710713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 25720713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 25738ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 25740713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 25758ad4d6ddSJeff Bonwick else 25760713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 25770713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2578b4952e17SGeorge Wilson 2579b4952e17SGeorge Wilson /* 2580b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 2581b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 2582b4952e17SGeorge Wilson */ 2583b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 258486714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 258586714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2586b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 2587b4952e17SGeorge Wilson 2588fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2589088f3894Sahrens 2590ecc2d604Sbonwick if (txg != 0) 2591ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2592fa9e4066Sahrens return; 2593fa9e4066Sahrens } 2594fa9e4066Sahrens 2595fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 25968ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 259799bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 259899bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 25998ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 26008ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 26018ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 26028ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 26038ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 26048ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 26058ad4d6ddSJeff Bonwick else 26068ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 26070713e232SGeorge Wilson space_reftree_create(&reftree); 26088ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 26098ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 26108ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 26110713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 26128ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 26138ad4d6ddSJeff Bonwick } 26140713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 26150713e232SGeorge Wilson space_reftree_destroy(&reftree); 2616fa9e4066Sahrens } 26178ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 2618fa9e4066Sahrens } 2619fa9e4066Sahrens 26200713e232SGeorge Wilson int 2621fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 2622fa9e4066Sahrens { 2623fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2624ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 26250713e232SGeorge Wilson int error = 0; 2626fa9e4066Sahrens 26270713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 26285cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 2629fa9e4066Sahrens 26300713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 26315cabbc6bSPrashanth Sreenivasa vd->vdev_dtl_object, 0, -1ULL, 0); 26320713e232SGeorge Wilson if (error) 26330713e232SGeorge Wilson return (error); 26340713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2635fa9e4066Sahrens 26360713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 26370713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 26380713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 26390713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2640fa9e4066Sahrens 26410713e232SGeorge Wilson return (error); 26420713e232SGeorge Wilson } 26430713e232SGeorge Wilson 26440713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 26450713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 26460713e232SGeorge Wilson if (error != 0) 26470713e232SGeorge Wilson break; 26480713e232SGeorge Wilson } 2649fa9e4066Sahrens 2650fa9e4066Sahrens return (error); 2651fa9e4066Sahrens } 2652fa9e4066Sahrens 2653663207adSDon Brady static void 2654663207adSDon Brady vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 2655663207adSDon Brady { 2656663207adSDon Brady spa_t *spa = vd->vdev_spa; 2657663207adSDon Brady objset_t *mos = spa->spa_meta_objset; 2658663207adSDon Brady vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 2659663207adSDon Brady const char *string; 2660663207adSDon Brady 2661663207adSDon Brady ASSERT(alloc_bias != VDEV_BIAS_NONE); 2662663207adSDon Brady 2663663207adSDon Brady string = 2664663207adSDon Brady (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 2665663207adSDon Brady (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 2666663207adSDon Brady (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 2667663207adSDon Brady 2668663207adSDon Brady ASSERT(string != NULL); 2669663207adSDon Brady VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 2670663207adSDon Brady 1, strlen(string) + 1, string, tx)); 2671663207adSDon Brady 2672663207adSDon Brady if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 2673663207adSDon Brady spa_activate_allocation_classes(spa, tx); 2674663207adSDon Brady } 2675663207adSDon Brady } 2676663207adSDon Brady 2677215198a6SJoe Stein void 2678215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2679215198a6SJoe Stein { 2680215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2681215198a6SJoe Stein 2682215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2683215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2684215198a6SJoe Stein zapobj, tx)); 2685215198a6SJoe Stein } 2686215198a6SJoe Stein 2687215198a6SJoe Stein uint64_t 2688215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2689215198a6SJoe Stein { 2690215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2691215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2692215198a6SJoe Stein DMU_OT_NONE, 0, tx); 2693215198a6SJoe Stein 2694215198a6SJoe Stein ASSERT(zap != 0); 2695215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2696215198a6SJoe Stein zap, tx)); 2697215198a6SJoe Stein 2698215198a6SJoe Stein return (zap); 2699215198a6SJoe Stein } 2700215198a6SJoe Stein 2701215198a6SJoe Stein void 2702215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2703215198a6SJoe Stein { 2704215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 2705215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 2706215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 2707215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 2708215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2709215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2710215198a6SJoe Stein } 2711215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2712215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2713663207adSDon Brady if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 2714663207adSDon Brady vdev_zap_allocation_data(vd, tx); 2715215198a6SJoe Stein } 2716215198a6SJoe Stein } 2717663207adSDon Brady 2718215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2719215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2720215198a6SJoe Stein } 2721215198a6SJoe Stein } 2722215198a6SJoe Stein 2723fa9e4066Sahrens void 2724fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2725fa9e4066Sahrens { 2726fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 27270713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2728ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 27290713e232SGeorge Wilson range_tree_t *rtsync; 2730fa9e4066Sahrens dmu_tx_t *tx; 27310713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2732fa9e4066Sahrens 27335cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 27340713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 273588ecc943SGeorge Wilson 2736fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2737fa9e4066Sahrens 27380713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 27390713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 27400713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 27410713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 27420713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 27430713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2744215198a6SJoe Stein 2745215198a6SJoe Stein /* 2746215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2747215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2748215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2749215198a6SJoe Stein */ 2750215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2751215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2752215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2753215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2754215198a6SJoe Stein } 2755215198a6SJoe Stein 2756fa9e4066Sahrens dmu_tx_commit(tx); 2757fa9e4066Sahrens return; 2758fa9e4066Sahrens } 2759fa9e4066Sahrens 27600713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 27610713e232SGeorge Wilson uint64_t new_object; 27620713e232SGeorge Wilson 2763*814dcd43SSerapheim Dimitropoulos new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx); 27640713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 27650713e232SGeorge Wilson 27660713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 27675cabbc6bSPrashanth Sreenivasa 0, -1ULL, 0)); 27680713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2769fa9e4066Sahrens } 2770fa9e4066Sahrens 27715cabbc6bSPrashanth Sreenivasa rtsync = range_tree_create(NULL, NULL); 2772fa9e4066Sahrens 2773fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 27740713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2775fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2776fa9e4066Sahrens 2777*814dcd43SSerapheim Dimitropoulos space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx); 277817f11284SSerapheim Dimitropoulos space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 27790713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2780fa9e4066Sahrens 27810713e232SGeorge Wilson range_tree_destroy(rtsync); 2782fa9e4066Sahrens 27830713e232SGeorge Wilson /* 27840713e232SGeorge Wilson * If the object for the space map has changed then dirty 27850713e232SGeorge Wilson * the top level so that we update the config. 27860713e232SGeorge Wilson */ 27870713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 27883ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 27893ee8c80cSPavel Zakharov "new object %llu", (u_longlong_t)txg, spa_name(spa), 27903ee8c80cSPavel Zakharov (u_longlong_t)object, 27913ee8c80cSPavel Zakharov (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 27920713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 27930713e232SGeorge Wilson } 2794fa9e4066Sahrens 2795fa9e4066Sahrens dmu_tx_commit(tx); 2796fa9e4066Sahrens } 2797fa9e4066Sahrens 27988ad4d6ddSJeff Bonwick /* 27998ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 28008ad4d6ddSJeff Bonwick * without losing data. 28018ad4d6ddSJeff Bonwick */ 28028ad4d6ddSJeff Bonwick boolean_t 28038ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 28048ad4d6ddSJeff Bonwick { 28058ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 28068ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 28078ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 28088ad4d6ddSJeff Bonwick boolean_t required; 28098ad4d6ddSJeff Bonwick 28108ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 28118ad4d6ddSJeff Bonwick 28128ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 28138ad4d6ddSJeff Bonwick return (B_TRUE); 28148ad4d6ddSJeff Bonwick 28158ad4d6ddSJeff Bonwick /* 28168ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 28178ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 28188ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 28198ad4d6ddSJeff Bonwick */ 28208ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 28218ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 28228ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 28238ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 28248ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 28258ad4d6ddSJeff Bonwick 2826cb04b873SMark J Musante if (!required && zio_injection_enabled) 2827cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2828cb04b873SMark J Musante 28298ad4d6ddSJeff Bonwick return (required); 28308ad4d6ddSJeff Bonwick } 28318ad4d6ddSJeff Bonwick 2832088f3894Sahrens /* 2833088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2834088f3894Sahrens */ 2835088f3894Sahrens boolean_t 2836088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2837088f3894Sahrens { 2838088f3894Sahrens boolean_t needed = B_FALSE; 2839088f3894Sahrens uint64_t thismin = UINT64_MAX; 2840088f3894Sahrens uint64_t thismax = 0; 2841088f3894Sahrens 2842088f3894Sahrens if (vd->vdev_children == 0) { 2843088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 284486714001SSerapheim Dimitropoulos if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 28458ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2846088f3894Sahrens 2847b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2848b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2849088f3894Sahrens needed = B_TRUE; 2850088f3894Sahrens } 2851088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2852088f3894Sahrens } else { 28538ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2854088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2855088f3894Sahrens uint64_t cmin, cmax; 2856088f3894Sahrens 2857088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2858088f3894Sahrens thismin = MIN(thismin, cmin); 2859088f3894Sahrens thismax = MAX(thismax, cmax); 2860088f3894Sahrens needed = B_TRUE; 2861088f3894Sahrens } 2862088f3894Sahrens } 2863088f3894Sahrens } 2864088f3894Sahrens 2865088f3894Sahrens if (needed && minp) { 2866088f3894Sahrens *minp = thismin; 2867088f3894Sahrens *maxp = thismax; 2868088f3894Sahrens } 2869088f3894Sahrens return (needed); 2870088f3894Sahrens } 2871088f3894Sahrens 287286714001SSerapheim Dimitropoulos /* 287386714001SSerapheim Dimitropoulos * Gets the checkpoint space map object from the vdev's ZAP. 287486714001SSerapheim Dimitropoulos * Returns the spacemap object, or 0 if it wasn't in the ZAP 287586714001SSerapheim Dimitropoulos * or the ZAP doesn't exist yet. 287686714001SSerapheim Dimitropoulos */ 287786714001SSerapheim Dimitropoulos int 287886714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd) 287986714001SSerapheim Dimitropoulos { 288086714001SSerapheim Dimitropoulos ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 288186714001SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) { 288286714001SSerapheim Dimitropoulos return (0); 288386714001SSerapheim Dimitropoulos } 288486714001SSerapheim Dimitropoulos 288586714001SSerapheim Dimitropoulos uint64_t sm_obj = 0; 288686714001SSerapheim Dimitropoulos int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 288786714001SSerapheim Dimitropoulos VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 288886714001SSerapheim Dimitropoulos 288986714001SSerapheim Dimitropoulos ASSERT(err == 0 || err == ENOENT); 289086714001SSerapheim Dimitropoulos 289186714001SSerapheim Dimitropoulos return (sm_obj); 289286714001SSerapheim Dimitropoulos } 289386714001SSerapheim Dimitropoulos 28945cabbc6bSPrashanth Sreenivasa int 2895ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2896fa9e4066Sahrens { 28975cabbc6bSPrashanth Sreenivasa int error = 0; 2898fa9e4066Sahrens /* 2899fa9e4066Sahrens * Recursively load all children. 2900fa9e4066Sahrens */ 29015cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 29025cabbc6bSPrashanth Sreenivasa error = vdev_load(vd->vdev_child[c]); 29035cabbc6bSPrashanth Sreenivasa if (error != 0) { 29045cabbc6bSPrashanth Sreenivasa return (error); 29055cabbc6bSPrashanth Sreenivasa } 29065cabbc6bSPrashanth Sreenivasa } 29075cabbc6bSPrashanth Sreenivasa 29085cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 2909fa9e4066Sahrens 2910663207adSDon Brady /* 2911663207adSDon Brady * On spa_load path, grab the allocation bias from our zap 2912663207adSDon Brady */ 2913663207adSDon Brady if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 2914663207adSDon Brady spa_t *spa = vd->vdev_spa; 2915663207adSDon Brady char bias_str[64]; 2916663207adSDon Brady 2917663207adSDon Brady if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 2918663207adSDon Brady VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 2919663207adSDon Brady bias_str) == 0) { 2920663207adSDon Brady ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 2921663207adSDon Brady vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 2922663207adSDon Brady } 2923663207adSDon Brady } 2924663207adSDon Brady 2925fa9e4066Sahrens /* 29260e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2927fa9e4066Sahrens */ 29285cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2929663207adSDon Brady vdev_metaslab_group_create(vd); 2930663207adSDon Brady 29315cabbc6bSPrashanth Sreenivasa if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 29325cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 29335cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 29343ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 29353ee8c80cSPavel Zakharov "asize=%llu", (u_longlong_t)vd->vdev_ashift, 29363ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_asize); 29375cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 2938555d674dSSerapheim Dimitropoulos } 2939555d674dSSerapheim Dimitropoulos 2940555d674dSSerapheim Dimitropoulos error = vdev_metaslab_init(vd, 0); 2941555d674dSSerapheim Dimitropoulos if (error != 0) { 29423ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 29433ee8c80cSPavel Zakharov "[error=%d]", error); 29445cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 29455cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 29465cabbc6bSPrashanth Sreenivasa return (error); 29475cabbc6bSPrashanth Sreenivasa } 294886714001SSerapheim Dimitropoulos 294986714001SSerapheim Dimitropoulos uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 295086714001SSerapheim Dimitropoulos if (checkpoint_sm_obj != 0) { 295186714001SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 295286714001SSerapheim Dimitropoulos ASSERT(vd->vdev_asize != 0); 295386714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 295486714001SSerapheim Dimitropoulos 2955555d674dSSerapheim Dimitropoulos error = space_map_open(&vd->vdev_checkpoint_sm, 295686714001SSerapheim Dimitropoulos mos, checkpoint_sm_obj, 0, vd->vdev_asize, 2957555d674dSSerapheim Dimitropoulos vd->vdev_ashift); 2958555d674dSSerapheim Dimitropoulos if (error != 0) { 295986714001SSerapheim Dimitropoulos vdev_dbgmsg(vd, "vdev_load: space_map_open " 296086714001SSerapheim Dimitropoulos "failed for checkpoint spacemap (obj %llu) " 296186714001SSerapheim Dimitropoulos "[error=%d]", 296286714001SSerapheim Dimitropoulos (u_longlong_t)checkpoint_sm_obj, error); 296386714001SSerapheim Dimitropoulos return (error); 296486714001SSerapheim Dimitropoulos } 296586714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 296686714001SSerapheim Dimitropoulos 296786714001SSerapheim Dimitropoulos /* 296886714001SSerapheim Dimitropoulos * Since the checkpoint_sm contains free entries 2969555d674dSSerapheim Dimitropoulos * exclusively we can use space_map_allocated() to 2970555d674dSSerapheim Dimitropoulos * indicate the cumulative checkpointed space that 2971555d674dSSerapheim Dimitropoulos * has been freed. 297286714001SSerapheim Dimitropoulos */ 297386714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space = 2974555d674dSSerapheim Dimitropoulos -space_map_allocated(vd->vdev_checkpoint_sm); 297586714001SSerapheim Dimitropoulos vd->vdev_spa->spa_checkpoint_info.sci_dspace += 297686714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space; 297786714001SSerapheim Dimitropoulos } 29785cabbc6bSPrashanth Sreenivasa } 2979fa9e4066Sahrens 2980fa9e4066Sahrens /* 2981fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2982fa9e4066Sahrens */ 29835cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2984560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2985560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 29863ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 29873ee8c80cSPavel Zakharov "[error=%d]", error); 29885cabbc6bSPrashanth Sreenivasa return (error); 29895cabbc6bSPrashanth Sreenivasa } 29905cabbc6bSPrashanth Sreenivasa 29915cabbc6bSPrashanth Sreenivasa uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 29925cabbc6bSPrashanth Sreenivasa if (obsolete_sm_object != 0) { 29935cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 29945cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_asize != 0); 299586714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 29965cabbc6bSPrashanth Sreenivasa 29975cabbc6bSPrashanth Sreenivasa if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 29985cabbc6bSPrashanth Sreenivasa obsolete_sm_object, 0, vd->vdev_asize, 0))) { 29995cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 30005cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 30013ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 30023ee8c80cSPavel Zakharov "obsolete spacemap (obj %llu) [error=%d]", 30033ee8c80cSPavel Zakharov (u_longlong_t)obsolete_sm_object, error); 30045cabbc6bSPrashanth Sreenivasa return (error); 30055cabbc6bSPrashanth Sreenivasa } 30065cabbc6bSPrashanth Sreenivasa } 30075cabbc6bSPrashanth Sreenivasa 30085cabbc6bSPrashanth Sreenivasa return (0); 3009fa9e4066Sahrens } 3010fa9e4066Sahrens 301199653d4eSeschrock /* 3012fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 3013fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 3014fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 3015fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 3016fa94a07fSbrendan * repurposed to another pool. 301799653d4eSeschrock */ 301899653d4eSeschrock int 3019fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 302099653d4eSeschrock { 302199653d4eSeschrock nvlist_t *label; 302299653d4eSeschrock uint64_t guid, version; 302399653d4eSeschrock uint64_t state; 302499653d4eSeschrock 3025e14bb325SJeff Bonwick if (!vdev_readable(vd)) 3026c5904d13Seschrock return (0); 3027c5904d13Seschrock 3028dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 302999653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 303099653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 303199653d4eSeschrock return (-1); 303299653d4eSeschrock } 303399653d4eSeschrock 303499653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 3035ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 303699653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 303799653d4eSeschrock guid != vd->vdev_guid || 303899653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 303999653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 304099653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 304199653d4eSeschrock nvlist_free(label); 304299653d4eSeschrock return (-1); 304399653d4eSeschrock } 304499653d4eSeschrock 304599653d4eSeschrock /* 304699653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 304799653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 304899653d4eSeschrock */ 304999653d4eSeschrock nvlist_free(label); 305099653d4eSeschrock return (0); 305199653d4eSeschrock } 305299653d4eSeschrock 3053*814dcd43SSerapheim Dimitropoulos static void 3054*814dcd43SSerapheim Dimitropoulos vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx) 3055*814dcd43SSerapheim Dimitropoulos { 3056*814dcd43SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 3057*814dcd43SSerapheim Dimitropoulos 3058*814dcd43SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) 3059*814dcd43SSerapheim Dimitropoulos return; 3060*814dcd43SSerapheim Dimitropoulos 3061*814dcd43SSerapheim Dimitropoulos uint64_t object = 0; 3062*814dcd43SSerapheim Dimitropoulos int err = zap_lookup(mos, vd->vdev_top_zap, 3063*814dcd43SSerapheim Dimitropoulos VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object); 3064*814dcd43SSerapheim Dimitropoulos if (err == ENOENT) 3065*814dcd43SSerapheim Dimitropoulos return; 3066*814dcd43SSerapheim Dimitropoulos 3067*814dcd43SSerapheim Dimitropoulos VERIFY0(dmu_object_free(mos, object, tx)); 3068*814dcd43SSerapheim Dimitropoulos VERIFY0(zap_remove(mos, vd->vdev_top_zap, 3069*814dcd43SSerapheim Dimitropoulos VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx)); 3070*814dcd43SSerapheim Dimitropoulos } 3071*814dcd43SSerapheim Dimitropoulos 30725cabbc6bSPrashanth Sreenivasa /* 30735cabbc6bSPrashanth Sreenivasa * Free the objects used to store this vdev's spacemaps, and the array 30745cabbc6bSPrashanth Sreenivasa * that points to them. 30755cabbc6bSPrashanth Sreenivasa */ 307688ecc943SGeorge Wilson void 30775cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 30785cabbc6bSPrashanth Sreenivasa { 30795cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0) 30805cabbc6bSPrashanth Sreenivasa return; 30815cabbc6bSPrashanth Sreenivasa 30825cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 30835cabbc6bSPrashanth Sreenivasa uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 30845cabbc6bSPrashanth Sreenivasa size_t array_bytes = array_count * sizeof (uint64_t); 30855cabbc6bSPrashanth Sreenivasa uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 30865cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 30875cabbc6bSPrashanth Sreenivasa array_bytes, smobj_array, 0)); 30885cabbc6bSPrashanth Sreenivasa 30895cabbc6bSPrashanth Sreenivasa for (uint64_t i = 0; i < array_count; i++) { 30905cabbc6bSPrashanth Sreenivasa uint64_t smobj = smobj_array[i]; 30915cabbc6bSPrashanth Sreenivasa if (smobj == 0) 30925cabbc6bSPrashanth Sreenivasa continue; 30935cabbc6bSPrashanth Sreenivasa 30945cabbc6bSPrashanth Sreenivasa space_map_free_obj(mos, smobj, tx); 30955cabbc6bSPrashanth Sreenivasa } 30965cabbc6bSPrashanth Sreenivasa 30975cabbc6bSPrashanth Sreenivasa kmem_free(smobj_array, array_bytes); 30985cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 3099*814dcd43SSerapheim Dimitropoulos vdev_destroy_ms_flush_data(vd, tx); 31005cabbc6bSPrashanth Sreenivasa vd->vdev_ms_array = 0; 31015cabbc6bSPrashanth Sreenivasa } 31025cabbc6bSPrashanth Sreenivasa 31035cabbc6bSPrashanth Sreenivasa static void 31044e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 310588ecc943SGeorge Wilson { 310688ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 310788ecc943SGeorge Wilson 31084e75ba68SSerapheim Dimitropoulos ASSERT(vd->vdev_islog); 3109215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 3110215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 311188ecc943SGeorge Wilson 31124e75ba68SSerapheim Dimitropoulos dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3113215198a6SJoe Stein 31144e75ba68SSerapheim Dimitropoulos vdev_destroy_spacemaps(vd, tx); 31154e75ba68SSerapheim Dimitropoulos if (vd->vdev_top_zap != 0) { 3116215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3117215198a6SJoe Stein vd->vdev_top_zap = 0; 3118215198a6SJoe Stein } 31194e75ba68SSerapheim Dimitropoulos 312088ecc943SGeorge Wilson dmu_tx_commit(tx); 312188ecc943SGeorge Wilson } 312288ecc943SGeorge Wilson 3123fa9e4066Sahrens void 3124fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 3125fa9e4066Sahrens { 3126fa9e4066Sahrens metaslab_t *msp; 312780eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3128fa9e4066Sahrens 31295cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 313088ecc943SGeorge Wilson 3131094e47e9SGeorge Wilson while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3132094e47e9SGeorge Wilson != NULL) 3133fa9e4066Sahrens metaslab_sync_done(msp, txg); 313480eb36f2SGeorge Wilson 313580eb36f2SGeorge Wilson if (reassess) 313680eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 3137fa9e4066Sahrens } 3138fa9e4066Sahrens 3139fa9e4066Sahrens void 3140fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 3141fa9e4066Sahrens { 3142fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3143fa9e4066Sahrens vdev_t *lvd; 3144fa9e4066Sahrens metaslab_t *msp; 3145fa9e4066Sahrens 3146555d674dSSerapheim Dimitropoulos ASSERT3U(txg, ==, spa->spa_syncing_txg); 3147555d674dSSerapheim Dimitropoulos dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 31485cabbc6bSPrashanth Sreenivasa if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 31495cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 31505cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 315188ecc943SGeorge Wilson 31525cabbc6bSPrashanth Sreenivasa vdev_indirect_sync_obsolete(vd, tx); 31535cabbc6bSPrashanth Sreenivasa 31545cabbc6bSPrashanth Sreenivasa /* 31555cabbc6bSPrashanth Sreenivasa * If the vdev is indirect, it can't have dirty 31565cabbc6bSPrashanth Sreenivasa * metaslabs or DTLs. 31575cabbc6bSPrashanth Sreenivasa */ 31585cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 31595cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 31605cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 3161555d674dSSerapheim Dimitropoulos dmu_tx_commit(tx); 31625cabbc6bSPrashanth Sreenivasa return; 31635cabbc6bSPrashanth Sreenivasa } 31645cabbc6bSPrashanth Sreenivasa } 31655cabbc6bSPrashanth Sreenivasa 31665cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 31675cabbc6bSPrashanth Sreenivasa 31685cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 31695cabbc6bSPrashanth Sreenivasa !vd->vdev_removing) { 3170ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 31715cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3172ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3173ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3174ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 3175ecc2d604Sbonwick vdev_config_dirty(vd); 3176ecc2d604Sbonwick } 3177fa9e4066Sahrens 3178ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3179fa9e4066Sahrens metaslab_sync(msp, txg); 3180ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3181ecc2d604Sbonwick } 3182fa9e4066Sahrens 3183fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3184fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 3185fa9e4066Sahrens 31865cabbc6bSPrashanth Sreenivasa /* 31874e75ba68SSerapheim Dimitropoulos * If this is an empty log device being removed, destroy the 31884e75ba68SSerapheim Dimitropoulos * metadata associated with it. 31895cabbc6bSPrashanth Sreenivasa */ 31904e75ba68SSerapheim Dimitropoulos if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 31914e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vd, txg); 31925cabbc6bSPrashanth Sreenivasa 3193fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3194555d674dSSerapheim Dimitropoulos dmu_tx_commit(tx); 3195fa9e4066Sahrens } 3196fa9e4066Sahrens 3197fa9e4066Sahrens uint64_t 3198fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3199fa9e4066Sahrens { 3200fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3201fa9e4066Sahrens } 3202fa9e4066Sahrens 32033d7072f8Seschrock /* 32043d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 32053d7072f8Seschrock * not be opened, and no I/O is attempted. 32063d7072f8Seschrock */ 3207fa9e4066Sahrens int 3208069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3209fa9e4066Sahrens { 32104b964adaSGeorge Wilson vdev_t *vd, *tvd; 3211fa9e4066Sahrens 32128f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 3213fa9e4066Sahrens 3214c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3215e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3216e14bb325SJeff Bonwick 32173d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3218e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3219fa9e4066Sahrens 32204b964adaSGeorge Wilson tvd = vd->vdev_top; 32214b964adaSGeorge Wilson 3222069f55e2SEric Schrock /* 3223069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 3224069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 3225069f55e2SEric Schrock * were faulted. 3226069f55e2SEric Schrock */ 3227069f55e2SEric Schrock vd->vdev_label_aux = aux; 3228069f55e2SEric Schrock 32293d7072f8Seschrock /* 32303d7072f8Seschrock * Faulted state takes precedence over degraded. 32313d7072f8Seschrock */ 323298d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 32333d7072f8Seschrock vd->vdev_faulted = 1ULL; 32343d7072f8Seschrock vd->vdev_degraded = 0ULL; 3235069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 32363d7072f8Seschrock 32373d7072f8Seschrock /* 3238c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 3239c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 32403d7072f8Seschrock */ 32414b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 32423d7072f8Seschrock vd->vdev_degraded = 1ULL; 32433d7072f8Seschrock vd->vdev_faulted = 0ULL; 32443d7072f8Seschrock 32453d7072f8Seschrock /* 32463d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 32473d7072f8Seschrock * mark it degraded. 32483d7072f8Seschrock */ 32494b964adaSGeorge Wilson vdev_reopen(tvd); 32503d7072f8Seschrock 3251069f55e2SEric Schrock if (vdev_readable(vd)) 3252069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 32533d7072f8Seschrock } 32543d7072f8Seschrock 3255e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 32563d7072f8Seschrock } 32573d7072f8Seschrock 32583d7072f8Seschrock /* 32593d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 32603d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 32613d7072f8Seschrock * as I/O is concerned. 32623d7072f8Seschrock */ 32633d7072f8Seschrock int 3264069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 32653d7072f8Seschrock { 3266c5904d13Seschrock vdev_t *vd; 32670a4e9518Sgw 32688f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 32693d7072f8Seschrock 3270c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3271e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3272e14bb325SJeff Bonwick 32730e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3274e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 32750e34b6a7Sbonwick 32763d7072f8Seschrock /* 32773d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 32783d7072f8Seschrock */ 3279e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 3280e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 32813d7072f8Seschrock 32823d7072f8Seschrock vd->vdev_degraded = 1ULL; 32833d7072f8Seschrock if (!vdev_is_dead(vd)) 32843d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3285069f55e2SEric Schrock aux); 32863d7072f8Seschrock 3287e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 32883d7072f8Seschrock } 32893d7072f8Seschrock 32903d7072f8Seschrock /* 3291f7170741SWill Andrews * Online the given vdev. 3292f7170741SWill Andrews * 3293f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3294f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 3295f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 3296f7170741SWill Andrews * events are generated if the device fails to open. 32973d7072f8Seschrock */ 32983d7072f8Seschrock int 3299e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 33003d7072f8Seschrock { 3301573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 33025f368aefSYuri Pankov boolean_t wasoffline; 33035f368aefSYuri Pankov vdev_state_t oldstate; 33043d7072f8Seschrock 33058f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 33063d7072f8Seschrock 3307c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3308e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 33093d7072f8Seschrock 33103d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3311e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3312fa9e4066Sahrens 33135f368aefSYuri Pankov wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 33145f368aefSYuri Pankov oldstate = vd->vdev_state; 331514372834SHans Rosenfeld 3316573ca77eSGeorge Wilson tvd = vd->vdev_top; 3317fa9e4066Sahrens vd->vdev_offline = B_FALSE; 3318441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 3319e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3320e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3321573ca77eSGeorge Wilson 3322573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3323573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3324573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3325573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3326573ca77eSGeorge Wilson } 3327573ca77eSGeorge Wilson 3328573ca77eSGeorge Wilson vdev_reopen(tvd); 33293d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 33303d7072f8Seschrock 3331573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3332573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3333573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 3334573ca77eSGeorge Wilson } 3335573ca77eSGeorge Wilson 33363d7072f8Seschrock if (newstate) 33373d7072f8Seschrock *newstate = vd->vdev_state; 33383d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 33393d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 33403d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 33413d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 33423d7072f8Seschrock vd->vdev_unspare = B_TRUE; 3343fa9e4066Sahrens 3344573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3345573ca77eSGeorge Wilson 3346573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3347573ca77eSGeorge Wilson if (vd->vdev_aux) 3348573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3349573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3350573ca77eSGeorge Wilson } 335114372834SHans Rosenfeld 3352094e47e9SGeorge Wilson /* Restart initializing if necessary */ 3353094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 3354094e47e9SGeorge Wilson if (vdev_writeable(vd) && 3355094e47e9SGeorge Wilson vd->vdev_initialize_thread == NULL && 3356094e47e9SGeorge Wilson vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3357094e47e9SGeorge Wilson (void) vdev_initialize(vd); 3358094e47e9SGeorge Wilson } 3359094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 3360094e47e9SGeorge Wilson 3361084fd14fSBrian Behlendorf /* Restart trimming if necessary */ 3362084fd14fSBrian Behlendorf mutex_enter(&vd->vdev_trim_lock); 3363084fd14fSBrian Behlendorf if (vdev_writeable(vd) && 3364084fd14fSBrian Behlendorf vd->vdev_trim_thread == NULL && 3365084fd14fSBrian Behlendorf vd->vdev_trim_state == VDEV_TRIM_ACTIVE) { 3366084fd14fSBrian Behlendorf (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial, 3367084fd14fSBrian Behlendorf vd->vdev_trim_secure); 3368084fd14fSBrian Behlendorf } 3369084fd14fSBrian Behlendorf mutex_exit(&vd->vdev_trim_lock); 3370084fd14fSBrian Behlendorf 33715f368aefSYuri Pankov if (wasoffline || 33725f368aefSYuri Pankov (oldstate < VDEV_STATE_DEGRADED && 33735f368aefSYuri Pankov vd->vdev_state >= VDEV_STATE_DEGRADED)) 3374ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 337514372834SHans Rosenfeld 33768ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 3377fa9e4066Sahrens } 3378fa9e4066Sahrens 3379a1521560SJeff Bonwick static int 3380a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3381fa9e4066Sahrens { 3382e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 33838f18d1faSGeorge Wilson int error = 0; 33848f18d1faSGeorge Wilson uint64_t generation; 33858f18d1faSGeorge Wilson metaslab_group_t *mg; 33860a4e9518Sgw 33878f18d1faSGeorge Wilson top: 33888f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 3389fa9e4066Sahrens 3390c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3391e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3392fa9e4066Sahrens 33930e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3394e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 33950e34b6a7Sbonwick 3396e6ca193dSGeorge Wilson tvd = vd->vdev_top; 33978f18d1faSGeorge Wilson mg = tvd->vdev_mg; 33988f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 3399e6ca193dSGeorge Wilson 3400fa9e4066Sahrens /* 3401ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 3402fa9e4066Sahrens */ 3403ecc2d604Sbonwick if (!vd->vdev_offline) { 3404ecc2d604Sbonwick /* 34058ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 3406e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 3407e6ca193dSGeorge Wilson * expendable. 3408ecc2d604Sbonwick */ 3409e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3410e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 3411e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3412fa9e4066Sahrens 34138f18d1faSGeorge Wilson /* 3414b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 3415b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 3416b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 3417b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 34188f18d1faSGeorge Wilson */ 34198f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 34208f18d1faSGeorge Wilson /* 34218f18d1faSGeorge Wilson * Prevent any future allocations. 34228f18d1faSGeorge Wilson */ 3423a1521560SJeff Bonwick metaslab_group_passivate(mg); 34248f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 34258f18d1faSGeorge Wilson 34265cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 34278f18d1faSGeorge Wilson 342886714001SSerapheim Dimitropoulos /* 342986714001SSerapheim Dimitropoulos * If the log device was successfully reset but has 343086714001SSerapheim Dimitropoulos * checkpointed data, do not offline it. 343186714001SSerapheim Dimitropoulos */ 343286714001SSerapheim Dimitropoulos if (error == 0 && 343386714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm != NULL) { 343486714001SSerapheim Dimitropoulos error = ZFS_ERR_CHECKPOINT_EXISTS; 343586714001SSerapheim Dimitropoulos } 343686714001SSerapheim Dimitropoulos 34378f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 34388f18d1faSGeorge Wilson 34398f18d1faSGeorge Wilson /* 34408f18d1faSGeorge Wilson * Check to see if the config has changed. 34418f18d1faSGeorge Wilson */ 34428f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 3443a1521560SJeff Bonwick metaslab_group_activate(mg); 34448f18d1faSGeorge Wilson if (error) 34458f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 34468f18d1faSGeorge Wilson vd, error)); 34478f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 34488f18d1faSGeorge Wilson goto top; 34498f18d1faSGeorge Wilson } 3450fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 34518f18d1faSGeorge Wilson } 34528f18d1faSGeorge Wilson 3453ecc2d604Sbonwick /* 3454ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 3455e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 3456e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 3457e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 3458ecc2d604Sbonwick */ 3459ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 3460e6ca193dSGeorge Wilson vdev_reopen(tvd); 3461e6ca193dSGeorge Wilson 3462e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3463e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 3464ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 3465e6ca193dSGeorge Wilson vdev_reopen(tvd); 3466e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3467ecc2d604Sbonwick } 34688f18d1faSGeorge Wilson 34698f18d1faSGeorge Wilson /* 34708f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 34718f18d1faSGeorge Wilson * once we online the device it's open for business. 34728f18d1faSGeorge Wilson */ 34738f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 3474a1521560SJeff Bonwick metaslab_group_activate(mg); 3475fa9e4066Sahrens } 3476fa9e4066Sahrens 3477e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3478ecc2d604Sbonwick 34798f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 3480fa9e4066Sahrens } 3481fa9e4066Sahrens 3482a1521560SJeff Bonwick int 3483a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3484a1521560SJeff Bonwick { 3485a1521560SJeff Bonwick int error; 3486a1521560SJeff Bonwick 3487a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 3488a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 3489a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 3490a1521560SJeff Bonwick 3491a1521560SJeff Bonwick return (error); 3492a1521560SJeff Bonwick } 3493a1521560SJeff Bonwick 3494ea8dc4b6Seschrock /* 3495ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 3496ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 3497ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3498ea8dc4b6Seschrock */ 3499ea8dc4b6Seschrock void 3500e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 3501fa9e4066Sahrens { 3502e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3503e14bb325SJeff Bonwick 3504e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3505fa9e4066Sahrens 3506ea8dc4b6Seschrock if (vd == NULL) 3507e14bb325SJeff Bonwick vd = rvd; 3508fa9e4066Sahrens 3509ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 3510ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 3511ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 3512fa9e4066Sahrens 3513e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3514e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 35153d7072f8Seschrock 35165cabbc6bSPrashanth Sreenivasa /* 35175cabbc6bSPrashanth Sreenivasa * It makes no sense to "clear" an indirect vdev. 35185cabbc6bSPrashanth Sreenivasa */ 35195cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(vd)) 35205cabbc6bSPrashanth Sreenivasa return; 35215cabbc6bSPrashanth Sreenivasa 35223d7072f8Seschrock /* 35238a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 35248a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 35258a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 35268a79c1b5Sek * written out to disk. 35273d7072f8Seschrock */ 3528e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 3529e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 35308a79c1b5Sek 3531096d22d4SEric Schrock /* 3532096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 3533096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 3534096d22d4SEric Schrock * still broken, we want to still post the ereport again. 3535096d22d4SEric Schrock */ 3536096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 3537096d22d4SEric Schrock 35384b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3539e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 3540e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 3541e14bb325SJeff Bonwick 3542f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 35433d7072f8Seschrock 3544096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 3545096d22d4SEric Schrock 3546f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 3547e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3548e14bb325SJeff Bonwick 3549e4c795beSTom Caputi if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) { 3550e4c795beSTom Caputi if (dsl_scan_resilvering(spa->spa_dsl_pool) && 3551e4c795beSTom Caputi spa_feature_is_enabled(spa, 3552e4c795beSTom Caputi SPA_FEATURE_RESILVER_DEFER)) 3553e4c795beSTom Caputi vdev_set_deferred_resilver(spa, vd); 3554e4c795beSTom Caputi else 3555e4c795beSTom Caputi spa_async_request(spa, SPA_ASYNC_RESILVER); 3556e4c795beSTom Caputi } 35573d7072f8Seschrock 3558ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 35593d7072f8Seschrock } 3560096d22d4SEric Schrock 3561096d22d4SEric Schrock /* 3562096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 3563096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 3564096d22d4SEric Schrock * done in response to the FMA fault. 3565096d22d4SEric Schrock */ 3566096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3567096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3568096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 3569096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 3570fa9e4066Sahrens } 3571fa9e4066Sahrens 3572e14bb325SJeff Bonwick boolean_t 3573e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 35740a4e9518Sgw { 357588ecc943SGeorge Wilson /* 357688ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 357788ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 357888ecc943SGeorge Wilson * these types of devices in the various code paths. 357988ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 358088ecc943SGeorge Wilson * before issuing I/O to them. 358188ecc943SGeorge Wilson */ 35825cabbc6bSPrashanth Sreenivasa return (vd->vdev_state < VDEV_STATE_DEGRADED || 35835cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_hole_ops || 358488ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 35850a4e9518Sgw } 35860a4e9518Sgw 3587e14bb325SJeff Bonwick boolean_t 3588e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 35890a4e9518Sgw { 3590e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 35910a4e9518Sgw } 35920a4e9518Sgw 3593e14bb325SJeff Bonwick boolean_t 3594e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 3595fa9e4066Sahrens { 35965cabbc6bSPrashanth Sreenivasa return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 35975cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)); 3598fa9e4066Sahrens } 3599fa9e4066Sahrens 3600a31e6787SGeorge Wilson boolean_t 3601a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 3602a31e6787SGeorge Wilson { 36038ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 36048ad4d6ddSJeff Bonwick 3605a31e6787SGeorge Wilson /* 36068ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 3607a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3608a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 36098ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 36108ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 36118ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 3612a31e6787SGeorge Wilson */ 36138ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 36145cabbc6bSPrashanth Sreenivasa !vd->vdev_cant_write && vdev_is_concrete(vd) && 36150f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 3616a31e6787SGeorge Wilson } 3617a31e6787SGeorge Wilson 3618e14bb325SJeff Bonwick boolean_t 3619e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 3620fa9e4066Sahrens { 3621e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 3622fa9e4066Sahrens 3623e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3624e14bb325SJeff Bonwick return (B_FALSE); 3625fa9e4066Sahrens 3626e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 3627e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 3628fa9e4066Sahrens 3629e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3630e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 3631fa9e4066Sahrens 3632e14bb325SJeff Bonwick return (B_TRUE); 3633fa9e4066Sahrens } 3634fa9e4066Sahrens 363586714001SSerapheim Dimitropoulos boolean_t 363686714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd) 363786714001SSerapheim Dimitropoulos { 3638a0b03b16SSerapheim Dimitropoulos if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 3639a0b03b16SSerapheim Dimitropoulos return (B_TRUE); 3640a0b03b16SSerapheim Dimitropoulos 364186714001SSerapheim Dimitropoulos /* 3642a0b03b16SSerapheim Dimitropoulos * If double-word space map entries are not enabled we assume 3643a0b03b16SSerapheim Dimitropoulos * 47 bits of the space map entry are dedicated to the entry's 3644a0b03b16SSerapheim Dimitropoulos * offset (see SM_OFFSET_BITS in space_map.h). We then use that 3645a0b03b16SSerapheim Dimitropoulos * to calculate the maximum address that can be described by a 3646a0b03b16SSerapheim Dimitropoulos * space map entry for the given device. 364786714001SSerapheim Dimitropoulos */ 3648a0b03b16SSerapheim Dimitropoulos uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 364986714001SSerapheim Dimitropoulos 365086714001SSerapheim Dimitropoulos if (shift >= 63) /* detect potential overflow */ 365186714001SSerapheim Dimitropoulos return (B_TRUE); 365286714001SSerapheim Dimitropoulos 365386714001SSerapheim Dimitropoulos return (vd->vdev_asize < (1ULL << shift)); 365486714001SSerapheim Dimitropoulos } 365586714001SSerapheim Dimitropoulos 3656fa9e4066Sahrens /* 3657fa9e4066Sahrens * Get statistics for the given vdev. 3658fa9e4066Sahrens */ 3659fa9e4066Sahrens void 3660fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3661fa9e4066Sahrens { 36622e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 36632e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 3664c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 36652e4c9986SGeorge Wilson 36662e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3667fa9e4066Sahrens 3668fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3669fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3670fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3671fa9e4066Sahrens vs->vs_state = vd->vdev_state; 3672573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 3673094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3674573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3675094e47e9SGeorge Wilson /* 3676094e47e9SGeorge Wilson * Report intializing progress. Since we don't have the 3677094e47e9SGeorge Wilson * initializing locks held, this is only an estimate (although a 3678094e47e9SGeorge Wilson * fairly accurate one). 3679094e47e9SGeorge Wilson */ 3680094e47e9SGeorge Wilson vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done; 3681094e47e9SGeorge Wilson vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est; 3682094e47e9SGeorge Wilson vs->vs_initialize_state = vd->vdev_initialize_state; 3683094e47e9SGeorge Wilson vs->vs_initialize_action_time = vd->vdev_initialize_action_time; 3684094e47e9SGeorge Wilson } 3685084fd14fSBrian Behlendorf 3686084fd14fSBrian Behlendorf /* 3687084fd14fSBrian Behlendorf * Report manual TRIM progress. Since we don't have 3688084fd14fSBrian Behlendorf * the manual TRIM locks held, this is only an 3689084fd14fSBrian Behlendorf * estimate (although fairly accurate one). 3690084fd14fSBrian Behlendorf */ 3691084fd14fSBrian Behlendorf vs->vs_trim_notsup = !vd->vdev_has_trim; 3692084fd14fSBrian Behlendorf vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done; 3693084fd14fSBrian Behlendorf vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est; 3694084fd14fSBrian Behlendorf vs->vs_trim_state = vd->vdev_trim_state; 3695084fd14fSBrian Behlendorf vs->vs_trim_action_time = vd->vdev_trim_action_time; 3696084fd14fSBrian Behlendorf 3697c39a2aaeSGeorge Wilson /* 3698c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 3699c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 3700c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 3701c39a2aaeSGeorge Wilson */ 3702c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 37037855d95bSToomas Soome vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 37047855d95bSToomas Soome spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3705c39a2aaeSGeorge Wilson } 37065cabbc6bSPrashanth Sreenivasa if (vd->vdev_aux == NULL && vd == vd->vdev_top && 37075cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3708663207adSDon Brady vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 3709663207adSDon Brady vd->vdev_mg->mg_fragmentation : 0; 37102986efa8SAlex Reece } 3711e4c795beSTom Caputi if (vd->vdev_ops->vdev_op_leaf) 3712e4c795beSTom Caputi vs->vs_resilver_deferred = vd->vdev_resilver_deferred; 3713fa9e4066Sahrens 3714fa9e4066Sahrens /* 3715fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 3716fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 3717fa9e4066Sahrens */ 3718fa9e4066Sahrens if (vd == rvd) { 3719e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 3720fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 3721fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 3722fa9e4066Sahrens 3723084fd14fSBrian Behlendorf for (int t = 0; t < VS_ZIO_TYPES; t++) { 3724fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 3725fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 3726fa9e4066Sahrens } 37273f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 3728fa9e4066Sahrens } 3729fa9e4066Sahrens } 37302e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 3731fa9e4066Sahrens } 3732fa9e4066Sahrens 3733fa94a07fSbrendan void 3734fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 3735fa94a07fSbrendan { 3736fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 3737fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 3738fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 3739fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 3740fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 3741fa94a07fSbrendan } 3742fa94a07fSbrendan 37433f9d6ad7SLin Ling void 37443f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 37453f9d6ad7SLin Ling { 37463f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 37473f9d6ad7SLin Ling 37483f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 37493f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 37503f9d6ad7SLin Ling 37513f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 37523f9d6ad7SLin Ling vs->vs_scan_processed = 0; 37533f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 37543f9d6ad7SLin Ling } 37553f9d6ad7SLin Ling 3756fa9e4066Sahrens void 3757e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 3758fa9e4066Sahrens { 37598ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 37608ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3761e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3762fa9e4066Sahrens vdev_t *pvd; 3763fa9e4066Sahrens uint64_t txg = zio->io_txg; 3764fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 3765fa9e4066Sahrens zio_type_t type = zio->io_type; 3766fa9e4066Sahrens int flags = zio->io_flags; 3767fa9e4066Sahrens 3768e14bb325SJeff Bonwick /* 3769e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 3770e14bb325SJeff Bonwick */ 3771e14bb325SJeff Bonwick if (zio->io_gang_tree) 3772e14bb325SJeff Bonwick return; 3773e14bb325SJeff Bonwick 3774fa9e4066Sahrens if (zio->io_error == 0) { 3775e14bb325SJeff Bonwick /* 3776e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 3777e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 3778e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 3779e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 3780e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 3781e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 3782e14bb325SJeff Bonwick * remain zero, which is what we want.) 3783e14bb325SJeff Bonwick * 3784e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 3785e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 3786e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 3787e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 3788e14bb325SJeff Bonwick */ 3789e14bb325SJeff Bonwick if (vd == rvd) 3790e14bb325SJeff Bonwick return; 3791e14bb325SJeff Bonwick 3792e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 37938ad4d6ddSJeff Bonwick 37948ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 37958ad4d6ddSJeff Bonwick return; 37968ad4d6ddSJeff Bonwick 37978ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 37988ad4d6ddSJeff Bonwick 3799e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 380044ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 38013f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 38023f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 38033f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 38043f9d6ad7SLin Ling 38053f9d6ad7SLin Ling /* XXX cleanup? */ 38063f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 38073f9d6ad7SLin Ling atomic_add_64(processed, psize); 38083f9d6ad7SLin Ling vs->vs_scan_processed += psize; 38093f9d6ad7SLin Ling } 38103f9d6ad7SLin Ling 38118ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 3812e14bb325SJeff Bonwick vs->vs_self_healed += psize; 3813fa9e4066Sahrens } 38148ad4d6ddSJeff Bonwick 3815084fd14fSBrian Behlendorf zio_type_t vs_type = type; 3816084fd14fSBrian Behlendorf 3817084fd14fSBrian Behlendorf /* 3818084fd14fSBrian Behlendorf * TRIM ops and bytes are reported to user space as 3819084fd14fSBrian Behlendorf * ZIO_TYPE_IOCTL. This is done to preserve the 3820084fd14fSBrian Behlendorf * vdev_stat_t structure layout for user space. 3821084fd14fSBrian Behlendorf */ 3822084fd14fSBrian Behlendorf if (type == ZIO_TYPE_TRIM) 3823084fd14fSBrian Behlendorf vs_type = ZIO_TYPE_IOCTL; 3824084fd14fSBrian Behlendorf 3825084fd14fSBrian Behlendorf vs->vs_ops[vs_type]++; 3826084fd14fSBrian Behlendorf vs->vs_bytes[vs_type] += psize; 38278ad4d6ddSJeff Bonwick 38288ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3829fa9e4066Sahrens return; 3830fa9e4066Sahrens } 3831fa9e4066Sahrens 3832fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 3833fa9e4066Sahrens return; 3834fa9e4066Sahrens 38358956713aSEric Schrock /* 38368956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 38378956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 38388956713aSEric Schrock * hard errors, when in reality they can happen for any number of 38398956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 38408956713aSEric Schrock */ 38418956713aSEric Schrock if (zio->io_error == EIO && 38428956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 38438956713aSEric Schrock return; 38448956713aSEric Schrock 38458f18d1faSGeorge Wilson /* 38468f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 38478f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 38488f18d1faSGeorge Wilson * errors. 38498f18d1faSGeorge Wilson */ 38508f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 38518f18d1faSGeorge Wilson return; 38528f18d1faSGeorge Wilson 3853e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 3854b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3855e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 3856e14bb325SJeff Bonwick vs->vs_checksum_errors++; 3857e14bb325SJeff Bonwick else 3858e14bb325SJeff Bonwick vs->vs_read_errors++; 3859fa9e4066Sahrens } 3860b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3861e14bb325SJeff Bonwick vs->vs_write_errors++; 3862e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3863fa9e4066Sahrens 38645cabbc6bSPrashanth Sreenivasa if (spa->spa_load_state == SPA_LOAD_NONE && 38655cabbc6bSPrashanth Sreenivasa type == ZIO_TYPE_WRITE && txg != 0 && 38668ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 386744ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 3868b24ab676SJeff Bonwick spa->spa_claiming)) { 38698ad4d6ddSJeff Bonwick /* 3870b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 3871b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 3872b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 3873b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 3874b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 3875b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 3876b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 3877b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 38788ad4d6ddSJeff Bonwick * 38798ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 38808ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 38818ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 38828ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 38838ad4d6ddSJeff Bonwick */ 38848ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 38858ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 388644ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 38878ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 38888ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 38898ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3890b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 3891b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 3892b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3893b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 38948ad4d6ddSJeff Bonwick } 3895b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 38968ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3897fa9e4066Sahrens return; 38988ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 38998ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 39008ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3901fa9e4066Sahrens } 39028ad4d6ddSJeff Bonwick if (vd != rvd) 39038ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3904fa9e4066Sahrens } 3905fa9e4066Sahrens } 3906fa9e4066Sahrens 3907663207adSDon Brady int64_t 3908663207adSDon Brady vdev_deflated_space(vdev_t *vd, int64_t space) 3909663207adSDon Brady { 3910663207adSDon Brady ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 3911663207adSDon Brady ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 3912663207adSDon Brady 3913663207adSDon Brady return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 3914663207adSDon Brady } 3915663207adSDon Brady 3916fa9e4066Sahrens /* 3917084fd14fSBrian Behlendorf * Update the in-core space usage stats for this vdev, its metaslab class, 3918084fd14fSBrian Behlendorf * and the root vdev. 3919fa9e4066Sahrens */ 3920fa9e4066Sahrens void 3921b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3922b24ab676SJeff Bonwick int64_t space_delta) 3923fa9e4066Sahrens { 3924663207adSDon Brady int64_t dspace_delta; 39258654d025Sperrin spa_t *spa = vd->vdev_spa; 39268654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 3927fa9e4066Sahrens 39288654d025Sperrin ASSERT(vd == vd->vdev_top); 392999653d4eSeschrock 39308654d025Sperrin /* 39318654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 39328654d025Sperrin * factor. We must calculate this here and not at the root vdev 39338654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 39348654d025Sperrin * childrens', thus not accurate enough for us. 39358654d025Sperrin */ 3936663207adSDon Brady dspace_delta = vdev_deflated_space(vd, space_delta); 39378654d025Sperrin 39388654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 39399740f25fSSerapheim Dimitropoulos /* ensure we won't underflow */ 39409740f25fSSerapheim Dimitropoulos if (alloc_delta < 0) { 39419740f25fSSerapheim Dimitropoulos ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta); 39429740f25fSSerapheim Dimitropoulos } 39439740f25fSSerapheim Dimitropoulos 39448654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 3945b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 39468654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 39478654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 39488654d025Sperrin 3949663207adSDon Brady /* every class but log contributes to root space stats */ 3950663207adSDon Brady if (vd->vdev_mg != NULL && !vd->vdev_islog) { 39519740f25fSSerapheim Dimitropoulos ASSERT(!vd->vdev_isl2cache); 3952fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 3953fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 3954b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 3955fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 3956fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 3957fa94a07fSbrendan } 3958663207adSDon Brady /* Note: metaslab_class_space_update moved to metaslab_space_update */ 3959fa9e4066Sahrens } 3960fa9e4066Sahrens 3961fa9e4066Sahrens /* 3962fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3963fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3964fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3965fa9e4066Sahrens */ 3966fa9e4066Sahrens void 3967fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3968fa9e4066Sahrens { 3969fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3970fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3971fa9e4066Sahrens int c; 3972fa9e4066Sahrens 3973f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3974f9af39baSGeorge Wilson 3975c5904d13Seschrock /* 39766809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 39776809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3978c5904d13Seschrock */ 3979c5904d13Seschrock if (vd->vdev_aux != NULL) { 3980c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3981c5904d13Seschrock nvlist_t **aux; 3982c5904d13Seschrock uint_t naux; 3983c5904d13Seschrock 3984c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3985c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3986c5904d13Seschrock break; 3987c5904d13Seschrock } 3988c5904d13Seschrock 3989e14bb325SJeff Bonwick if (c == sav->sav_count) { 3990e14bb325SJeff Bonwick /* 3991e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3992e14bb325SJeff Bonwick */ 3993e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3994e14bb325SJeff Bonwick return; 3995e14bb325SJeff Bonwick } 3996e14bb325SJeff Bonwick 3997c5904d13Seschrock sav->sav_sync = B_TRUE; 3998c5904d13Seschrock 39996809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 40006809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 40016809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 40026809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 40036809eb4eSEric Schrock } 4004c5904d13Seschrock 4005c5904d13Seschrock ASSERT(c < naux); 4006c5904d13Seschrock 4007c5904d13Seschrock /* 4008c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 4009c5904d13Seschrock * sketchy, but it will work. 4010c5904d13Seschrock */ 4011c5904d13Seschrock nvlist_free(aux[c]); 40123f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 4013c5904d13Seschrock 4014c5904d13Seschrock return; 4015c5904d13Seschrock } 4016c5904d13Seschrock 40175dabedeeSbonwick /* 4018e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 4019e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 4020e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 40215dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 40225dabedeeSbonwick */ 4023e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4024e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 4025e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 40265dabedeeSbonwick 4027fa9e4066Sahrens if (vd == rvd) { 4028fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 4029fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 4030fa9e4066Sahrens } else { 4031fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 4032fa9e4066Sahrens 403388ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 40345cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 4035e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 40365cabbc6bSPrashanth Sreenivasa } 4037fa9e4066Sahrens } 4038fa9e4066Sahrens } 4039fa9e4066Sahrens 4040fa9e4066Sahrens void 4041fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 4042fa9e4066Sahrens { 40435dabedeeSbonwick spa_t *spa = vd->vdev_spa; 40445dabedeeSbonwick 4045e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4046e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 4047e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 40485dabedeeSbonwick 4049e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 4050e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 4051e14bb325SJeff Bonwick } 4052e14bb325SJeff Bonwick 4053e14bb325SJeff Bonwick /* 4054e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 4055e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 4056e14bb325SJeff Bonwick * the state changes from larger config changes because they require 4057e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 4058e14bb325SJeff Bonwick */ 4059e14bb325SJeff Bonwick void 4060e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 4061e14bb325SJeff Bonwick { 4062e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 4063e14bb325SJeff Bonwick 4064f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4065e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 4066e14bb325SJeff Bonwick 4067e14bb325SJeff Bonwick /* 4068e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 4069e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 4070e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 4071e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 4072e14bb325SJeff Bonwick */ 4073e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4074e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 4075e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 4076e14bb325SJeff Bonwick 40775cabbc6bSPrashanth Sreenivasa if (!list_link_active(&vd->vdev_state_dirty_node) && 40785cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) 4079e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 4080e14bb325SJeff Bonwick } 4081e14bb325SJeff Bonwick 4082e14bb325SJeff Bonwick void 4083e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 4084e14bb325SJeff Bonwick { 4085e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 4086e14bb325SJeff Bonwick 4087e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4088e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 4089e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 4090e14bb325SJeff Bonwick 4091e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 4092e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 4093fa9e4066Sahrens } 4094fa9e4066Sahrens 409532b87932Sek /* 409632b87932Sek * Propagate vdev state up from children to parent. 409732b87932Sek */ 409844cd46caSbillm void 409944cd46caSbillm vdev_propagate_state(vdev_t *vd) 410044cd46caSbillm { 41018ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 41028ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 410344cd46caSbillm int degraded = 0, faulted = 0; 410444cd46caSbillm int corrupted = 0; 410544cd46caSbillm vdev_t *child; 410644cd46caSbillm 41073d7072f8Seschrock if (vd->vdev_children > 0) { 4108573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 41093d7072f8Seschrock child = vd->vdev_child[c]; 411051ece835Seschrock 411188ecc943SGeorge Wilson /* 41125cabbc6bSPrashanth Sreenivasa * Don't factor holes or indirect vdevs into the 41135cabbc6bSPrashanth Sreenivasa * decision. 411488ecc943SGeorge Wilson */ 41155cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(child)) 411688ecc943SGeorge Wilson continue; 411788ecc943SGeorge Wilson 4118e14bb325SJeff Bonwick if (!vdev_readable(child) || 41198ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 412051ece835Seschrock /* 412151ece835Seschrock * Root special: if there is a top-level log 412251ece835Seschrock * device, treat the root vdev as if it were 412351ece835Seschrock * degraded. 412451ece835Seschrock */ 412551ece835Seschrock if (child->vdev_islog && vd == rvd) 412651ece835Seschrock degraded++; 412751ece835Seschrock else 412851ece835Seschrock faulted++; 412951ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 41303d7072f8Seschrock degraded++; 413151ece835Seschrock } 413244cd46caSbillm 41333d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 41343d7072f8Seschrock corrupted++; 41353d7072f8Seschrock } 413644cd46caSbillm 41373d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 41383d7072f8Seschrock 41393d7072f8Seschrock /* 4140e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 41413d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 41423d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 41433d7072f8Seschrock * replicas'. 41443d7072f8Seschrock */ 41453d7072f8Seschrock if (corrupted && vd == rvd && 41463d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 41473d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 41483d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 41493d7072f8Seschrock } 41503d7072f8Seschrock 415151ece835Seschrock if (vd->vdev_parent) 41523d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 415344cd46caSbillm } 415444cd46caSbillm 4155fa9e4066Sahrens /* 4156ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 4157ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 4158ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 4159ea8dc4b6Seschrock * 4160ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 4161ea8dc4b6Seschrock * generated. 4162fa9e4066Sahrens */ 4163fa9e4066Sahrens void 4164ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4165fa9e4066Sahrens { 4166560e6e96Seschrock uint64_t save_state; 4167c5904d13Seschrock spa_t *spa = vd->vdev_spa; 4168ea8dc4b6Seschrock 4169ea8dc4b6Seschrock if (state == vd->vdev_state) { 4170ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 4171fa9e4066Sahrens return; 4172ea8dc4b6Seschrock } 4173ea8dc4b6Seschrock 4174560e6e96Seschrock save_state = vd->vdev_state; 4175fa9e4066Sahrens 4176fa9e4066Sahrens vd->vdev_state = state; 4177fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 4178fa9e4066Sahrens 41793d7072f8Seschrock /* 41803d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 418198d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 418298d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 418398d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 418498d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 418598d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 418698d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 418798d1cbfeSGeorge Wilson * children. 418898d1cbfeSGeorge Wilson */ 418998d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 419098d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 41913d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 41923d7072f8Seschrock 4193069f55e2SEric Schrock /* 4194069f55e2SEric Schrock * If we have brought this vdev back into service, we need 4195069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 4196069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 4197069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 4198069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 4199069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 4200069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 4201069f55e2SEric Schrock */ 4202069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 4203069f55e2SEric Schrock vd->vdev_prevstate != state) 4204069f55e2SEric Schrock zfs_post_state_change(spa, vd); 4205069f55e2SEric Schrock 42063d7072f8Seschrock if (vd->vdev_removed && 42073d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 42083d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 42093d7072f8Seschrock /* 42103d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 42113d7072f8Seschrock * device was previously marked removed and someone attempted to 42123d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 42133d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 42143d7072f8Seschrock * it is one of our special test online cases, which is only 42153d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 42163d7072f8Seschrock * fault. 42173d7072f8Seschrock */ 42183d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 42193d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 42203d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 42213d7072f8Seschrock vd->vdev_removed = B_TRUE; 42223d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 4223ea8dc4b6Seschrock /* 4224cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 4225cb04b873SMark J Musante * mark it as "not available", which signifies that it was 4226cb04b873SMark J Musante * never there to begin with. Failure to open such a device 4227cb04b873SMark J Musante * is not considered an error. 4228ea8dc4b6Seschrock */ 4229cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 4230cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 4231560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 4232560e6e96Seschrock vd->vdev_not_present = 1; 4233560e6e96Seschrock 4234560e6e96Seschrock /* 4235560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 4236560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 4237560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 4238560e6e96Seschrock * want to post the ereport if the device was already in the 4239560e6e96Seschrock * CANT_OPEN state beforehand. 42403d7072f8Seschrock * 42413d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 42423d7072f8Seschrock * online the device in response to an insertion event. If we 42433d7072f8Seschrock * hit this case, then we have detected an insertion event for a 42443d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 42453d7072f8Seschrock * In this scenario, we don't post an ereport because we are 42463d7072f8Seschrock * about to replace the device, or attempt an online with 42473d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 4248560e6e96Seschrock */ 42493d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 42503d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 4251c5904d13Seschrock vd != spa->spa_root_vdev) { 4252ea8dc4b6Seschrock const char *class; 4253ea8dc4b6Seschrock 4254ea8dc4b6Seschrock switch (aux) { 4255ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 4256ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 4257ea8dc4b6Seschrock break; 4258ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 4259ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 4260ea8dc4b6Seschrock break; 4261ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 4262ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 4263ea8dc4b6Seschrock break; 4264ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 4265ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 4266ea8dc4b6Seschrock break; 4267ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 4268ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 4269ea8dc4b6Seschrock break; 4270ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 4271ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 4272ea8dc4b6Seschrock break; 42735711d393Sloli case VDEV_AUX_BAD_ASHIFT: 42745711d393Sloli class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT; 42755711d393Sloli break; 4276ea8dc4b6Seschrock default: 4277ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 4278ea8dc4b6Seschrock } 4279ea8dc4b6Seschrock 4280eb633035STom Caputi zfs_ereport_post(class, spa, vd, NULL, NULL, 4281eb633035STom Caputi save_state, 0); 4282ea8dc4b6Seschrock } 4283ea8dc4b6Seschrock 42843d7072f8Seschrock /* Erase any notion of persistent removed state */ 42853d7072f8Seschrock vd->vdev_removed = B_FALSE; 42863d7072f8Seschrock } else { 42873d7072f8Seschrock vd->vdev_removed = B_FALSE; 42883d7072f8Seschrock } 4289ea8dc4b6Seschrock 42908b33d774STim Haley if (!isopen && vd->vdev_parent) 42918b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 4292fa9e4066Sahrens } 429315e6edf1Sgw 42946f793812SPavel Zakharov boolean_t 42956f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd) 42966f793812SPavel Zakharov { 42976f793812SPavel Zakharov ASSERT(!vd->vdev_ops->vdev_op_leaf); 42986f793812SPavel Zakharov 42996f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) { 43006f793812SPavel Zakharov if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 43016f793812SPavel Zakharov return (B_FALSE); 43026f793812SPavel Zakharov } 43036f793812SPavel Zakharov 43046f793812SPavel Zakharov return (B_TRUE); 43056f793812SPavel Zakharov } 43066f793812SPavel Zakharov 430715e6edf1Sgw /* 430815e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 4309c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 4310c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 431115e6edf1Sgw */ 431215e6edf1Sgw boolean_t 431315e6edf1Sgw vdev_is_bootable(vdev_t *vd) 431415e6edf1Sgw { 431515e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 431615e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 431715e6edf1Sgw 431815e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 431915e6edf1Sgw vd->vdev_children > 1) { 432021c878feSToomas Soome int non_indirect = 0; 432121c878feSToomas Soome 432221c878feSToomas Soome for (int c = 0; c < vd->vdev_children; c++) { 432321c878feSToomas Soome vdev_type = 432421c878feSToomas Soome vd->vdev_child[c]->vdev_ops->vdev_op_type; 432521c878feSToomas Soome if (strcmp(vdev_type, VDEV_TYPE_INDIRECT) != 0) 432621c878feSToomas Soome non_indirect++; 432721c878feSToomas Soome } 432821c878feSToomas Soome /* 432921c878feSToomas Soome * non_indirect > 1 means we have more than one 433021c878feSToomas Soome * top-level vdev, so we stop here. 433121c878feSToomas Soome */ 433221c878feSToomas Soome if (non_indirect > 1) 433321c878feSToomas Soome return (B_FALSE); 433421c878feSToomas Soome } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 433515e6edf1Sgw return (B_FALSE); 433615e6edf1Sgw } 433715e6edf1Sgw } 433815e6edf1Sgw 4339573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 434015e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 434115e6edf1Sgw return (B_FALSE); 434215e6edf1Sgw } 434315e6edf1Sgw return (B_TRUE); 434415e6edf1Sgw } 4345e6ca193dSGeorge Wilson 43465cabbc6bSPrashanth Sreenivasa boolean_t 43475cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd) 43485cabbc6bSPrashanth Sreenivasa { 43495cabbc6bSPrashanth Sreenivasa vdev_ops_t *ops = vd->vdev_ops; 43505cabbc6bSPrashanth Sreenivasa if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 43515cabbc6bSPrashanth Sreenivasa ops == &vdev_missing_ops || ops == &vdev_root_ops) { 43525cabbc6bSPrashanth Sreenivasa return (B_FALSE); 43535cabbc6bSPrashanth Sreenivasa } else { 43545cabbc6bSPrashanth Sreenivasa return (B_TRUE); 43555cabbc6bSPrashanth Sreenivasa } 43565cabbc6bSPrashanth Sreenivasa } 43575cabbc6bSPrashanth Sreenivasa 43584b964adaSGeorge Wilson /* 43594b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 43604b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 43614b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 43624b964adaSGeorge Wilson */ 43634b964adaSGeorge Wilson boolean_t 43644b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 43654b964adaSGeorge Wilson { 43664b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 43674b964adaSGeorge Wilson !vd->vdev_removed) 43684b964adaSGeorge Wilson return (B_TRUE); 43694b964adaSGeorge Wilson 43704b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 43714b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 43724b964adaSGeorge Wilson return (B_TRUE); 43734b964adaSGeorge Wilson 43744b964adaSGeorge Wilson return (B_FALSE); 43754b964adaSGeorge Wilson } 43764b964adaSGeorge Wilson 4377573ca77eSGeorge Wilson /* 4378573ca77eSGeorge Wilson * Expand a vdev if possible. 4379573ca77eSGeorge Wilson */ 4380573ca77eSGeorge Wilson void 4381573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 4382573ca77eSGeorge Wilson { 4383573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 4384573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 438511f6a968SSerapheim Dimitropoulos ASSERT(vdev_is_concrete(vd)); 4386573ca77eSGeorge Wilson 43875cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 43885cabbc6bSPrashanth Sreenivasa 4389663207adSDon Brady if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 4390663207adSDon Brady vdev_is_concrete(vd)) { 4391663207adSDon Brady vdev_metaslab_group_create(vd); 4392573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 4393573ca77eSGeorge Wilson vdev_config_dirty(vd); 4394573ca77eSGeorge Wilson } 4395573ca77eSGeorge Wilson } 43961195e687SMark J Musante 43971195e687SMark J Musante /* 43981195e687SMark J Musante * Split a vdev. 43991195e687SMark J Musante */ 44001195e687SMark J Musante void 44011195e687SMark J Musante vdev_split(vdev_t *vd) 44021195e687SMark J Musante { 44031195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 44041195e687SMark J Musante 44051195e687SMark J Musante vdev_remove_child(pvd, vd); 44061195e687SMark J Musante vdev_compact_children(pvd); 44071195e687SMark J Musante 44081195e687SMark J Musante cvd = pvd->vdev_child[0]; 44091195e687SMark J Musante if (pvd->vdev_children == 1) { 44101195e687SMark J Musante vdev_remove_parent(cvd); 44111195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 44121195e687SMark J Musante } 44131195e687SMark J Musante vdev_propagate_state(cvd); 44141195e687SMark J Musante } 4415283b8460SGeorge.Wilson 4416283b8460SGeorge.Wilson void 4417283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 4418283b8460SGeorge.Wilson { 4419283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 4420283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 4421283b8460SGeorge.Wilson 4422283b8460SGeorge.Wilson vdev_deadman(cvd); 4423283b8460SGeorge.Wilson } 4424283b8460SGeorge.Wilson 4425283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 4426283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 4427283b8460SGeorge.Wilson 4428283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 442969962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 4430283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 4431283b8460SGeorge.Wilson zio_t *fio; 4432283b8460SGeorge.Wilson uint64_t delta; 4433283b8460SGeorge.Wilson 4434283b8460SGeorge.Wilson /* 4435283b8460SGeorge.Wilson * Look at the head of all the pending queues, 4436283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 4437283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 4438283b8460SGeorge.Wilson */ 443969962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 4440c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 4441c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 44423ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 44433ee8c80cSPavel Zakharov "%lluns, delta %lluns, last io %lluns", 44443ee8c80cSPavel Zakharov fio->io_timestamp, (u_longlong_t)delta, 4445283b8460SGeorge.Wilson vq->vq_io_complete_ts); 4446283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 4447283b8460SGeorge.Wilson "hung.", spa_name(spa)); 4448283b8460SGeorge.Wilson } 4449283b8460SGeorge.Wilson } 4450283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 4451283b8460SGeorge.Wilson } 4452283b8460SGeorge.Wilson } 4453e4c795beSTom Caputi 4454e4c795beSTom Caputi void 4455e4c795beSTom Caputi vdev_set_deferred_resilver(spa_t *spa, vdev_t *vd) 4456e4c795beSTom Caputi { 4457e4c795beSTom Caputi for (uint64_t i = 0; i < vd->vdev_children; i++) 4458e4c795beSTom Caputi vdev_set_deferred_resilver(spa, vd->vdev_child[i]); 4459e4c795beSTom Caputi 4460e4c795beSTom Caputi if (!vd->vdev_ops->vdev_op_leaf || !vdev_writeable(vd) || 4461e4c795beSTom Caputi range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 4462e4c795beSTom Caputi return; 4463e4c795beSTom Caputi } 4464e4c795beSTom Caputi 4465e4c795beSTom Caputi vd->vdev_resilver_deferred = B_TRUE; 4466e4c795beSTom Caputi spa->spa_resilver_deferred = B_TRUE; 4467e4c795beSTom Caputi } 4468084fd14fSBrian Behlendorf 4469084fd14fSBrian Behlendorf /* 4470084fd14fSBrian Behlendorf * Translate a logical range to the physical range for the specified vdev_t. 4471084fd14fSBrian Behlendorf * This function is initially called with a leaf vdev and will walk each 4472084fd14fSBrian Behlendorf * parent vdev until it reaches a top-level vdev. Once the top-level is 4473084fd14fSBrian Behlendorf * reached the physical range is initialized and the recursive function 4474084fd14fSBrian Behlendorf * begins to unwind. As it unwinds it calls the parent's vdev specific 4475084fd14fSBrian Behlendorf * translation function to do the real conversion. 4476084fd14fSBrian Behlendorf */ 4477084fd14fSBrian Behlendorf void 4478084fd14fSBrian Behlendorf vdev_xlate(vdev_t *vd, const range_seg_t *logical_rs, range_seg_t *physical_rs) 4479084fd14fSBrian Behlendorf { 4480084fd14fSBrian Behlendorf /* 4481084fd14fSBrian Behlendorf * Walk up the vdev tree 4482084fd14fSBrian Behlendorf */ 4483084fd14fSBrian Behlendorf if (vd != vd->vdev_top) { 4484084fd14fSBrian Behlendorf vdev_xlate(vd->vdev_parent, logical_rs, physical_rs); 4485084fd14fSBrian Behlendorf } else { 4486084fd14fSBrian Behlendorf /* 4487084fd14fSBrian Behlendorf * We've reached the top-level vdev, initialize the 4488084fd14fSBrian Behlendorf * physical range to the logical range and start to 4489084fd14fSBrian Behlendorf * unwind. 4490084fd14fSBrian Behlendorf */ 4491084fd14fSBrian Behlendorf physical_rs->rs_start = logical_rs->rs_start; 4492084fd14fSBrian Behlendorf physical_rs->rs_end = logical_rs->rs_end; 4493084fd14fSBrian Behlendorf return; 4494084fd14fSBrian Behlendorf } 4495084fd14fSBrian Behlendorf 4496084fd14fSBrian Behlendorf vdev_t *pvd = vd->vdev_parent; 4497084fd14fSBrian Behlendorf ASSERT3P(pvd, !=, NULL); 4498084fd14fSBrian Behlendorf ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL); 4499084fd14fSBrian Behlendorf 4500084fd14fSBrian Behlendorf /* 4501084fd14fSBrian Behlendorf * As this recursive function unwinds, translate the logical 4502084fd14fSBrian Behlendorf * range into its physical components by calling the 4503084fd14fSBrian Behlendorf * vdev specific translate function. 4504084fd14fSBrian Behlendorf */ 4505084fd14fSBrian Behlendorf range_seg_t intermediate = { { { 0, 0 } } }; 4506084fd14fSBrian Behlendorf pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate); 4507084fd14fSBrian Behlendorf 4508084fd14fSBrian Behlendorf physical_rs->rs_start = intermediate.rs_start; 4509084fd14fSBrian Behlendorf physical_rs->rs_end = intermediate.rs_end; 4510084fd14fSBrian Behlendorf } 4511