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> 28ce1577b0SDave Eddy * Copyright 2017 Joyent, Inc. 29*663207adSDon 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> 54fa9e4066Sahrens 55fa9e4066Sahrens /* 56fa9e4066Sahrens * Virtual device management. 57fa9e4066Sahrens */ 58fa9e4066Sahrens 59fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = { 60fa9e4066Sahrens &vdev_root_ops, 61fa9e4066Sahrens &vdev_raidz_ops, 62fa9e4066Sahrens &vdev_mirror_ops, 63fa9e4066Sahrens &vdev_replacing_ops, 6499653d4eSeschrock &vdev_spare_ops, 65fa9e4066Sahrens &vdev_disk_ops, 66fa9e4066Sahrens &vdev_file_ops, 67fa9e4066Sahrens &vdev_missing_ops, 6888ecc943SGeorge Wilson &vdev_hole_ops, 695cabbc6bSPrashanth Sreenivasa &vdev_indirect_ops, 70fa9e4066Sahrens NULL 71fa9e4066Sahrens }; 72fa9e4066Sahrens 73088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */ 74088f3894Sahrens int zfs_scrub_limit = 10; 7505b2b3b8Smishra 76a0b03b16SSerapheim Dimitropoulos /* default target for number of metaslabs per top-level vdev */ 77a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_count = 200; 7886714001SSerapheim Dimitropoulos 79b4bf0cf0SDon Brady /* minimum number of metaslabs per top-level vdev */ 80a0b03b16SSerapheim Dimitropoulos int zfs_vdev_min_ms_count = 16; 8186714001SSerapheim Dimitropoulos 82b4bf0cf0SDon Brady /* practical upper limit of total metaslabs per top-level vdev */ 83a0b03b16SSerapheim Dimitropoulos int zfs_vdev_ms_count_limit = 1ULL << 17; 84b4bf0cf0SDon Brady 85b4bf0cf0SDon Brady /* lower limit for metaslab size (512M) */ 86a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_shift = 29; 8786714001SSerapheim Dimitropoulos 88a0b03b16SSerapheim Dimitropoulos /* upper limit for metaslab size (16G) */ 89a0b03b16SSerapheim Dimitropoulos int zfs_vdev_max_ms_shift = 34; 90b4bf0cf0SDon Brady 9186714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE; 9286714001SSerapheim Dimitropoulos 93bf3e216cSMatthew Ahrens /* 9486714001SSerapheim Dimitropoulos * Since the DTL space map of a vdev is not expected to have a lot of 9586714001SSerapheim Dimitropoulos * entries, we default its block size to 4K. 96bf3e216cSMatthew Ahrens */ 9786714001SSerapheim Dimitropoulos int vdev_dtl_sm_blksz = (1 << 12); 98bf3e216cSMatthew Ahrens 9986714001SSerapheim Dimitropoulos /* 10086714001SSerapheim Dimitropoulos * vdev-wide space maps that have lots of entries written to them at 10186714001SSerapheim Dimitropoulos * the end of each transaction can benefit from a higher I/O bandwidth 10286714001SSerapheim Dimitropoulos * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 10386714001SSerapheim Dimitropoulos */ 10486714001SSerapheim Dimitropoulos int vdev_standard_sm_blksz = (1 << 17); 1056f793812SPavel Zakharov 10693a1902eSMatthew Ahrens int zfs_ashift_min; 10793a1902eSMatthew Ahrens 1083ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 1093ee8c80cSPavel Zakharov void 1103ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 1113ee8c80cSPavel Zakharov { 1123ee8c80cSPavel Zakharov va_list adx; 1133ee8c80cSPavel Zakharov char buf[256]; 1143ee8c80cSPavel Zakharov 1153ee8c80cSPavel Zakharov va_start(adx, fmt); 1163ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 1173ee8c80cSPavel Zakharov va_end(adx); 1183ee8c80cSPavel Zakharov 1193ee8c80cSPavel Zakharov if (vd->vdev_path != NULL) { 1203ee8c80cSPavel Zakharov zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 1213ee8c80cSPavel Zakharov vd->vdev_path, buf); 1223ee8c80cSPavel Zakharov } else { 1233ee8c80cSPavel Zakharov zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 1243ee8c80cSPavel Zakharov vd->vdev_ops->vdev_op_type, 1253ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_id, 1263ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_guid, buf); 1273ee8c80cSPavel Zakharov } 1283ee8c80cSPavel Zakharov } 1293ee8c80cSPavel Zakharov 1306f793812SPavel Zakharov void 1316f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 1326f793812SPavel Zakharov { 1336f793812SPavel Zakharov char state[20]; 1346f793812SPavel Zakharov 1356f793812SPavel Zakharov if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 1366f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 1376f793812SPavel Zakharov vd->vdev_ops->vdev_op_type); 1386f793812SPavel Zakharov return; 1396f793812SPavel Zakharov } 1406f793812SPavel Zakharov 1416f793812SPavel Zakharov switch (vd->vdev_state) { 1426f793812SPavel Zakharov case VDEV_STATE_UNKNOWN: 1436f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "unknown"); 1446f793812SPavel Zakharov break; 1456f793812SPavel Zakharov case VDEV_STATE_CLOSED: 1466f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "closed"); 1476f793812SPavel Zakharov break; 1486f793812SPavel Zakharov case VDEV_STATE_OFFLINE: 1496f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "offline"); 1506f793812SPavel Zakharov break; 1516f793812SPavel Zakharov case VDEV_STATE_REMOVED: 1526f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "removed"); 1536f793812SPavel Zakharov break; 1546f793812SPavel Zakharov case VDEV_STATE_CANT_OPEN: 1556f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "can't open"); 1566f793812SPavel Zakharov break; 1576f793812SPavel Zakharov case VDEV_STATE_FAULTED: 1586f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "faulted"); 1596f793812SPavel Zakharov break; 1606f793812SPavel Zakharov case VDEV_STATE_DEGRADED: 1616f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "degraded"); 1626f793812SPavel Zakharov break; 1636f793812SPavel Zakharov case VDEV_STATE_HEALTHY: 1646f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "healthy"); 1656f793812SPavel Zakharov break; 1666f793812SPavel Zakharov default: 1676f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "<state %u>", 1686f793812SPavel Zakharov (uint_t)vd->vdev_state); 1696f793812SPavel Zakharov } 1706f793812SPavel Zakharov 1716f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 172c7a7b2faSAndriy Gapon "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type, 1736f793812SPavel Zakharov vd->vdev_islog ? " (log)" : "", 1746f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 1756f793812SPavel Zakharov vd->vdev_path ? vd->vdev_path : "N/A", state); 1766f793812SPavel Zakharov 1776f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) 1786f793812SPavel Zakharov vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 1796f793812SPavel Zakharov } 1806f793812SPavel Zakharov 181fa9e4066Sahrens /* 182fa9e4066Sahrens * Given a vdev type, return the appropriate ops vector. 183fa9e4066Sahrens */ 184fa9e4066Sahrens static vdev_ops_t * 185fa9e4066Sahrens vdev_getops(const char *type) 186fa9e4066Sahrens { 187fa9e4066Sahrens vdev_ops_t *ops, **opspp; 188fa9e4066Sahrens 189fa9e4066Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 190fa9e4066Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 191fa9e4066Sahrens break; 192fa9e4066Sahrens 193fa9e4066Sahrens return (ops); 194fa9e4066Sahrens } 195fa9e4066Sahrens 196*663207adSDon Brady /* 197*663207adSDon Brady * Derive the enumerated alloction bias from string input. 198*663207adSDon Brady * String origin is either the per-vdev zap or zpool(1M). 199*663207adSDon Brady */ 200*663207adSDon Brady static vdev_alloc_bias_t 201*663207adSDon Brady vdev_derive_alloc_bias(const char *bias) 202*663207adSDon Brady { 203*663207adSDon Brady vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 204*663207adSDon Brady 205*663207adSDon Brady if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0) 206*663207adSDon Brady alloc_bias = VDEV_BIAS_LOG; 207*663207adSDon Brady else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) 208*663207adSDon Brady alloc_bias = VDEV_BIAS_SPECIAL; 209*663207adSDon Brady else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) 210*663207adSDon Brady alloc_bias = VDEV_BIAS_DEDUP; 211*663207adSDon Brady 212*663207adSDon Brady return (alloc_bias); 213*663207adSDon Brady } 214*663207adSDon Brady 215094e47e9SGeorge Wilson /* ARGSUSED */ 216094e47e9SGeorge Wilson void 217094e47e9SGeorge Wilson vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res) 218094e47e9SGeorge Wilson { 219094e47e9SGeorge Wilson res->rs_start = in->rs_start; 220094e47e9SGeorge Wilson res->rs_end = in->rs_end; 221094e47e9SGeorge Wilson } 222094e47e9SGeorge Wilson 223fa9e4066Sahrens /* 224fa9e4066Sahrens * Default asize function: return the MAX of psize with the asize of 225fa9e4066Sahrens * all children. This is what's used by anything other than RAID-Z. 226fa9e4066Sahrens */ 227fa9e4066Sahrens uint64_t 228fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 229fa9e4066Sahrens { 230ecc2d604Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 231fa9e4066Sahrens uint64_t csize; 232fa9e4066Sahrens 233573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 234fa9e4066Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 235fa9e4066Sahrens asize = MAX(asize, csize); 236fa9e4066Sahrens } 237fa9e4066Sahrens 238fa9e4066Sahrens return (asize); 239fa9e4066Sahrens } 240fa9e4066Sahrens 2412a79c5feSlling /* 242573ca77eSGeorge Wilson * Get the minimum allocatable size. We define the allocatable size as 243573ca77eSGeorge Wilson * the vdev's asize rounded to the nearest metaslab. This allows us to 244573ca77eSGeorge Wilson * replace or attach devices which don't have the same physical size but 245573ca77eSGeorge Wilson * can still satisfy the same number of allocations. 2462a79c5feSlling */ 2472a79c5feSlling uint64_t 248573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd) 2492a79c5feSlling { 250573ca77eSGeorge Wilson vdev_t *pvd = vd->vdev_parent; 2512a79c5feSlling 252573ca77eSGeorge Wilson /* 2534263d13fSGeorge Wilson * If our parent is NULL (inactive spare or cache) or is the root, 254573ca77eSGeorge Wilson * just return our own asize. 255573ca77eSGeorge Wilson */ 256573ca77eSGeorge Wilson if (pvd == NULL) 257573ca77eSGeorge Wilson return (vd->vdev_asize); 2582a79c5feSlling 2592a79c5feSlling /* 260573ca77eSGeorge Wilson * The top-level vdev just returns the allocatable size rounded 261573ca77eSGeorge Wilson * to the nearest metaslab. 2622a79c5feSlling */ 263573ca77eSGeorge Wilson if (vd == vd->vdev_top) 264573ca77eSGeorge Wilson return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 2652a79c5feSlling 266573ca77eSGeorge Wilson /* 267573ca77eSGeorge Wilson * The allocatable space for a raidz vdev is N * sizeof(smallest child), 268573ca77eSGeorge Wilson * so each child must provide at least 1/Nth of its asize. 269573ca77eSGeorge Wilson */ 270573ca77eSGeorge Wilson if (pvd->vdev_ops == &vdev_raidz_ops) 271c040c10cSSteven Hartland return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 272c040c10cSSteven Hartland pvd->vdev_children); 2732a79c5feSlling 274573ca77eSGeorge Wilson return (pvd->vdev_min_asize); 275573ca77eSGeorge Wilson } 2762a79c5feSlling 277573ca77eSGeorge Wilson void 278573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd) 279573ca77eSGeorge Wilson { 280573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 281573ca77eSGeorge Wilson 282573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 283573ca77eSGeorge Wilson vdev_set_min_asize(vd->vdev_child[c]); 2842a79c5feSlling } 2852a79c5feSlling 286fa9e4066Sahrens vdev_t * 287fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 288fa9e4066Sahrens { 289fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 290fa9e4066Sahrens 291e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 292e05725b1Sbonwick 293088f3894Sahrens if (vdev < rvd->vdev_children) { 294088f3894Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 295fa9e4066Sahrens return (rvd->vdev_child[vdev]); 296088f3894Sahrens } 297fa9e4066Sahrens 298fa9e4066Sahrens return (NULL); 299fa9e4066Sahrens } 300fa9e4066Sahrens 301fa9e4066Sahrens vdev_t * 302fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 303fa9e4066Sahrens { 304fa9e4066Sahrens vdev_t *mvd; 305fa9e4066Sahrens 3060e34b6a7Sbonwick if (vd->vdev_guid == guid) 307fa9e4066Sahrens return (vd); 308fa9e4066Sahrens 309573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 310fa9e4066Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 311fa9e4066Sahrens NULL) 312fa9e4066Sahrens return (mvd); 313fa9e4066Sahrens 314fa9e4066Sahrens return (NULL); 315fa9e4066Sahrens } 316fa9e4066Sahrens 31712380e1eSArne Jansen static int 31812380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd) 31912380e1eSArne Jansen { 32012380e1eSArne Jansen int n = 0; 32112380e1eSArne Jansen 32212380e1eSArne Jansen if (vd->vdev_ops->vdev_op_leaf) 32312380e1eSArne Jansen return (1); 32412380e1eSArne Jansen 32512380e1eSArne Jansen for (int c = 0; c < vd->vdev_children; c++) 32612380e1eSArne Jansen n += vdev_count_leaves_impl(vd->vdev_child[c]); 32712380e1eSArne Jansen 32812380e1eSArne Jansen return (n); 32912380e1eSArne Jansen } 33012380e1eSArne Jansen 33112380e1eSArne Jansen int 33212380e1eSArne Jansen vdev_count_leaves(spa_t *spa) 33312380e1eSArne Jansen { 33412380e1eSArne Jansen return (vdev_count_leaves_impl(spa->spa_root_vdev)); 33512380e1eSArne Jansen } 33612380e1eSArne Jansen 337fa9e4066Sahrens void 338fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 339fa9e4066Sahrens { 340fa9e4066Sahrens size_t oldsize, newsize; 341fa9e4066Sahrens uint64_t id = cvd->vdev_id; 342fa9e4066Sahrens vdev_t **newchild; 34381cd5c55SMatthew Ahrens spa_t *spa = cvd->vdev_spa; 344fa9e4066Sahrens 34581cd5c55SMatthew Ahrens ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 346fa9e4066Sahrens ASSERT(cvd->vdev_parent == NULL); 347fa9e4066Sahrens 348fa9e4066Sahrens cvd->vdev_parent = pvd; 349fa9e4066Sahrens 350fa9e4066Sahrens if (pvd == NULL) 351fa9e4066Sahrens return; 352fa9e4066Sahrens 353fa9e4066Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 354fa9e4066Sahrens 355fa9e4066Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 356fa9e4066Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 357fa9e4066Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 358fa9e4066Sahrens 359fa9e4066Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 360fa9e4066Sahrens if (pvd->vdev_child != NULL) { 361fa9e4066Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 362fa9e4066Sahrens kmem_free(pvd->vdev_child, oldsize); 363fa9e4066Sahrens } 364fa9e4066Sahrens 365fa9e4066Sahrens pvd->vdev_child = newchild; 366fa9e4066Sahrens pvd->vdev_child[id] = cvd; 367fa9e4066Sahrens 368fa9e4066Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 369fa9e4066Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 370fa9e4066Sahrens 371fa9e4066Sahrens /* 372fa9e4066Sahrens * Walk up all ancestors to update guid sum. 373fa9e4066Sahrens */ 374fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 375fa9e4066Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 376e0f1c0afSOlaf Faaland 377e0f1c0afSOlaf Faaland if (cvd->vdev_ops->vdev_op_leaf) { 378e0f1c0afSOlaf Faaland list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd); 379e0f1c0afSOlaf Faaland cvd->vdev_spa->spa_leaf_list_gen++; 380e0f1c0afSOlaf Faaland } 381fa9e4066Sahrens } 382fa9e4066Sahrens 383fa9e4066Sahrens void 384fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 385fa9e4066Sahrens { 386fa9e4066Sahrens int c; 387fa9e4066Sahrens uint_t id = cvd->vdev_id; 388fa9e4066Sahrens 389fa9e4066Sahrens ASSERT(cvd->vdev_parent == pvd); 390fa9e4066Sahrens 391fa9e4066Sahrens if (pvd == NULL) 392fa9e4066Sahrens return; 393fa9e4066Sahrens 394fa9e4066Sahrens ASSERT(id < pvd->vdev_children); 395fa9e4066Sahrens ASSERT(pvd->vdev_child[id] == cvd); 396fa9e4066Sahrens 397fa9e4066Sahrens pvd->vdev_child[id] = NULL; 398fa9e4066Sahrens cvd->vdev_parent = NULL; 399fa9e4066Sahrens 400fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) 401fa9e4066Sahrens if (pvd->vdev_child[c]) 402fa9e4066Sahrens break; 403fa9e4066Sahrens 404fa9e4066Sahrens if (c == pvd->vdev_children) { 405fa9e4066Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 406fa9e4066Sahrens pvd->vdev_child = NULL; 407fa9e4066Sahrens pvd->vdev_children = 0; 408fa9e4066Sahrens } 409fa9e4066Sahrens 410e0f1c0afSOlaf Faaland if (cvd->vdev_ops->vdev_op_leaf) { 411e0f1c0afSOlaf Faaland spa_t *spa = cvd->vdev_spa; 412e0f1c0afSOlaf Faaland list_remove(&spa->spa_leaf_list, cvd); 413e0f1c0afSOlaf Faaland spa->spa_leaf_list_gen++; 414e0f1c0afSOlaf Faaland } 415e0f1c0afSOlaf Faaland 416fa9e4066Sahrens /* 417fa9e4066Sahrens * Walk up all ancestors to update guid sum. 418fa9e4066Sahrens */ 419fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 420fa9e4066Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 421fa9e4066Sahrens } 422fa9e4066Sahrens 423fa9e4066Sahrens /* 424fa9e4066Sahrens * Remove any holes in the child array. 425fa9e4066Sahrens */ 426fa9e4066Sahrens void 427fa9e4066Sahrens vdev_compact_children(vdev_t *pvd) 428fa9e4066Sahrens { 429fa9e4066Sahrens vdev_t **newchild, *cvd; 430fa9e4066Sahrens int oldc = pvd->vdev_children; 431573ca77eSGeorge Wilson int newc; 432fa9e4066Sahrens 433e14bb325SJeff Bonwick ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 434fa9e4066Sahrens 435573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) 436fa9e4066Sahrens if (pvd->vdev_child[c]) 437fa9e4066Sahrens newc++; 438fa9e4066Sahrens 439fa9e4066Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 440fa9e4066Sahrens 441573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) { 442fa9e4066Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 443fa9e4066Sahrens newchild[newc] = cvd; 444fa9e4066Sahrens cvd->vdev_id = newc++; 445fa9e4066Sahrens } 446fa9e4066Sahrens } 447fa9e4066Sahrens 448fa9e4066Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 449fa9e4066Sahrens pvd->vdev_child = newchild; 450fa9e4066Sahrens pvd->vdev_children = newc; 451fa9e4066Sahrens } 452fa9e4066Sahrens 453fa9e4066Sahrens /* 454fa9e4066Sahrens * Allocate and minimally initialize a vdev_t. 455fa9e4066Sahrens */ 45688ecc943SGeorge Wilson vdev_t * 457fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 458fa9e4066Sahrens { 459fa9e4066Sahrens vdev_t *vd; 4605cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 461fa9e4066Sahrens 462fa9e4066Sahrens vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 4635cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 464fa9e4066Sahrens 4650e34b6a7Sbonwick if (spa->spa_root_vdev == NULL) { 4660e34b6a7Sbonwick ASSERT(ops == &vdev_root_ops); 4670e34b6a7Sbonwick spa->spa_root_vdev = vd; 468e9103aaeSGarrett D'Amore spa->spa_load_guid = spa_generate_guid(NULL); 4690e34b6a7Sbonwick } 4700e34b6a7Sbonwick 47188ecc943SGeorge Wilson if (guid == 0 && ops != &vdev_hole_ops) { 4720e34b6a7Sbonwick if (spa->spa_root_vdev == vd) { 4730e34b6a7Sbonwick /* 4740e34b6a7Sbonwick * The root vdev's guid will also be the pool guid, 4750e34b6a7Sbonwick * which must be unique among all pools. 4760e34b6a7Sbonwick */ 4771195e687SMark J Musante guid = spa_generate_guid(NULL); 4780e34b6a7Sbonwick } else { 4790e34b6a7Sbonwick /* 4800e34b6a7Sbonwick * Any other vdev's guid must be unique within the pool. 4810e34b6a7Sbonwick */ 4821195e687SMark J Musante guid = spa_generate_guid(spa); 4830e34b6a7Sbonwick } 4840e34b6a7Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 4850e34b6a7Sbonwick } 4860e34b6a7Sbonwick 487fa9e4066Sahrens vd->vdev_spa = spa; 488fa9e4066Sahrens vd->vdev_id = id; 489fa9e4066Sahrens vd->vdev_guid = guid; 490fa9e4066Sahrens vd->vdev_guid_sum = guid; 491fa9e4066Sahrens vd->vdev_ops = ops; 492fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 49388ecc943SGeorge Wilson vd->vdev_ishole = (ops == &vdev_hole_ops); 4945cabbc6bSPrashanth Sreenivasa vic->vic_prev_indirect_vdev = UINT64_MAX; 4955cabbc6bSPrashanth Sreenivasa 4965cabbc6bSPrashanth Sreenivasa rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 4975cabbc6bSPrashanth Sreenivasa mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 4985cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); 499fa9e4066Sahrens 500e0f1c0afSOlaf Faaland list_link_init(&vd->vdev_leaf_node); 501fa9e4066Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 5025ad82045Snd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 503e14bb325SJeff Bonwick mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 5040f7643c7SGeorge Wilson mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 505094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); 506094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); 507094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); 508094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); 509094e47e9SGeorge Wilson 5108ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 5115cabbc6bSPrashanth Sreenivasa vd->vdev_dtl[t] = range_tree_create(NULL, NULL); 5128ad4d6ddSJeff Bonwick } 513b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_ms_list, spa, 514fa9e4066Sahrens offsetof(struct metaslab, ms_txg_node)); 515b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_dtl_list, spa, 516fa9e4066Sahrens offsetof(struct vdev, vdev_dtl_node)); 517fa9e4066Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 5183d7072f8Seschrock vdev_queue_init(vd); 5193d7072f8Seschrock vdev_cache_init(vd); 520fa9e4066Sahrens 521fa9e4066Sahrens return (vd); 522fa9e4066Sahrens } 523fa9e4066Sahrens 524fa9e4066Sahrens /* 525fa9e4066Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 526fa9e4066Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 527fa9e4066Sahrens * different for each case. 528fa9e4066Sahrens */ 52999653d4eSeschrock int 53099653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 53199653d4eSeschrock int alloctype) 532fa9e4066Sahrens { 533fa9e4066Sahrens vdev_ops_t *ops; 534fa9e4066Sahrens char *type; 5358654d025Sperrin uint64_t guid = 0, islog, nparity; 536fa9e4066Sahrens vdev_t *vd; 5375cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 538*663207adSDon Brady vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 539*663207adSDon Brady boolean_t top_level = (parent && !parent->vdev_parent); 540fa9e4066Sahrens 541e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 542fa9e4066Sahrens 543fa9e4066Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 544be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 545fa9e4066Sahrens 546fa9e4066Sahrens if ((ops = vdev_getops(type)) == NULL) 547be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 548fa9e4066Sahrens 549fa9e4066Sahrens /* 550fa9e4066Sahrens * If this is a load, get the vdev guid from the nvlist. 551fa9e4066Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 552fa9e4066Sahrens */ 553fa9e4066Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 554fa9e4066Sahrens uint64_t label_id; 555fa9e4066Sahrens 556fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 557fa9e4066Sahrens label_id != id) 558be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 559fa9e4066Sahrens 560fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 561be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 56299653d4eSeschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 56399653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 564be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 565fa94a07fSbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 566fa94a07fSbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 567be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 56821ecdf64SLin Ling } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 56921ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 570be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 571fa9e4066Sahrens } 572fa9e4066Sahrens 57399653d4eSeschrock /* 57499653d4eSeschrock * The first allocated vdev must be of type 'root'. 57599653d4eSeschrock */ 57699653d4eSeschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 577be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 57899653d4eSeschrock 5798654d025Sperrin /* 5808654d025Sperrin * Determine whether we're a log vdev. 5818654d025Sperrin */ 5828654d025Sperrin islog = 0; 5838654d025Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 584990b4856Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 585be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 586fa9e4066Sahrens 58788ecc943SGeorge Wilson if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 588be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 58988ecc943SGeorge Wilson 59099653d4eSeschrock /* 5918654d025Sperrin * Set the nparity property for RAID-Z vdevs. 59299653d4eSeschrock */ 5938654d025Sperrin nparity = -1ULL; 59499653d4eSeschrock if (ops == &vdev_raidz_ops) { 59599653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 5968654d025Sperrin &nparity) == 0) { 597b24ab676SJeff Bonwick if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 598be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 59999653d4eSeschrock /* 600f94275ceSAdam Leventhal * Previous versions could only support 1 or 2 parity 601f94275ceSAdam Leventhal * device. 60299653d4eSeschrock */ 603f94275ceSAdam Leventhal if (nparity > 1 && 604f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ2) 605be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 606f94275ceSAdam Leventhal if (nparity > 2 && 607f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ3) 608be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 60999653d4eSeschrock } else { 61099653d4eSeschrock /* 61199653d4eSeschrock * We require the parity to be specified for SPAs that 61299653d4eSeschrock * support multiple parity levels. 61399653d4eSeschrock */ 614f94275ceSAdam Leventhal if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 615be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 61699653d4eSeschrock /* 61799653d4eSeschrock * Otherwise, we default to 1 parity device for RAID-Z. 61899653d4eSeschrock */ 6198654d025Sperrin nparity = 1; 62099653d4eSeschrock } 62199653d4eSeschrock } else { 6228654d025Sperrin nparity = 0; 62399653d4eSeschrock } 6248654d025Sperrin ASSERT(nparity != -1ULL); 6258654d025Sperrin 626*663207adSDon Brady /* 627*663207adSDon Brady * If creating a top-level vdev, check for allocation classes input 628*663207adSDon Brady */ 629*663207adSDon Brady if (top_level && alloctype == VDEV_ALLOC_ADD) { 630*663207adSDon Brady char *bias; 631*663207adSDon Brady 632*663207adSDon Brady if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 633*663207adSDon Brady &bias) == 0) { 634*663207adSDon Brady alloc_bias = vdev_derive_alloc_bias(bias); 635*663207adSDon Brady 636*663207adSDon Brady /* spa_vdev_add() expects feature to be enabled */ 637*663207adSDon Brady if (spa->spa_load_state != SPA_LOAD_CREATE && 638*663207adSDon Brady !spa_feature_is_enabled(spa, 639*663207adSDon Brady SPA_FEATURE_ALLOCATION_CLASSES)) { 640*663207adSDon Brady return (SET_ERROR(ENOTSUP)); 641*663207adSDon Brady } 642*663207adSDon Brady } 643*663207adSDon Brady } 644*663207adSDon Brady 6458654d025Sperrin vd = vdev_alloc_common(spa, id, guid, ops); 6465cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 6478654d025Sperrin 6488654d025Sperrin vd->vdev_islog = islog; 6498654d025Sperrin vd->vdev_nparity = nparity; 650*663207adSDon Brady if (top_level && alloc_bias != VDEV_BIAS_NONE) 651*663207adSDon Brady vd->vdev_alloc_bias = alloc_bias; 6528654d025Sperrin 6538654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 6548654d025Sperrin vd->vdev_path = spa_strdup(vd->vdev_path); 6558654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 6568654d025Sperrin vd->vdev_devid = spa_strdup(vd->vdev_devid); 6578654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 6588654d025Sperrin &vd->vdev_physpath) == 0) 6598654d025Sperrin vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 6606809eb4eSEric Schrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 6616809eb4eSEric Schrock vd->vdev_fru = spa_strdup(vd->vdev_fru); 66299653d4eSeschrock 663afefbcddSeschrock /* 664afefbcddSeschrock * Set the whole_disk property. If it's not specified, leave the value 665afefbcddSeschrock * as -1. 666afefbcddSeschrock */ 667afefbcddSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 668afefbcddSeschrock &vd->vdev_wholedisk) != 0) 669afefbcddSeschrock vd->vdev_wholedisk = -1ULL; 670afefbcddSeschrock 6715cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_mapping_object); 6725cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 6735cabbc6bSPrashanth Sreenivasa &vic->vic_mapping_object); 6745cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_births_object); 6755cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 6765cabbc6bSPrashanth Sreenivasa &vic->vic_births_object); 6775cabbc6bSPrashanth Sreenivasa ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 6785cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 6795cabbc6bSPrashanth Sreenivasa &vic->vic_prev_indirect_vdev); 6805cabbc6bSPrashanth Sreenivasa 681ea8dc4b6Seschrock /* 682ea8dc4b6Seschrock * Look for the 'not present' flag. This will only be set if the device 683ea8dc4b6Seschrock * was not present at the time of import. 684ea8dc4b6Seschrock */ 6856809eb4eSEric Schrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 6866809eb4eSEric Schrock &vd->vdev_not_present); 687ea8dc4b6Seschrock 688ecc2d604Sbonwick /* 689ecc2d604Sbonwick * Get the alignment requirement. 690ecc2d604Sbonwick */ 691ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 692ecc2d604Sbonwick 69388ecc943SGeorge Wilson /* 69488ecc943SGeorge Wilson * Retrieve the vdev creation time. 69588ecc943SGeorge Wilson */ 69688ecc943SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 69788ecc943SGeorge Wilson &vd->vdev_crtxg); 69888ecc943SGeorge Wilson 699fa9e4066Sahrens /* 700fa9e4066Sahrens * If we're a top-level vdev, try to load the allocation parameters. 701fa9e4066Sahrens */ 702*663207adSDon Brady if (top_level && 7031195e687SMark J Musante (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 704fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 705fa9e4066Sahrens &vd->vdev_ms_array); 706fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 707fa9e4066Sahrens &vd->vdev_ms_shift); 708fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 709fa9e4066Sahrens &vd->vdev_asize); 7103f9d6ad7SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 7113f9d6ad7SLin Ling &vd->vdev_removing); 712215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 713215198a6SJoe Stein &vd->vdev_top_zap); 714215198a6SJoe Stein } else { 715215198a6SJoe Stein ASSERT0(vd->vdev_top_zap); 716fa9e4066Sahrens } 717fa9e4066Sahrens 718*663207adSDon Brady if (top_level && alloctype != VDEV_ALLOC_ATTACH) { 719a1521560SJeff Bonwick ASSERT(alloctype == VDEV_ALLOC_LOAD || 7209f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ADD || 7211195e687SMark J Musante alloctype == VDEV_ALLOC_SPLIT || 7229f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ROOTPOOL); 723*663207adSDon Brady /* Note: metaslab_group_create() is now deferred */ 724a1521560SJeff Bonwick } 725a1521560SJeff Bonwick 726215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && 727215198a6SJoe Stein (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 728215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, 729215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 730215198a6SJoe Stein } else { 731215198a6SJoe Stein ASSERT0(vd->vdev_leaf_zap); 732215198a6SJoe Stein } 733215198a6SJoe Stein 734fa9e4066Sahrens /* 7353d7072f8Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 736fa9e4066Sahrens */ 737215198a6SJoe Stein 738c5904d13Seschrock if (vd->vdev_ops->vdev_op_leaf && 73921ecdf64SLin Ling (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 74021ecdf64SLin Ling alloctype == VDEV_ALLOC_ROOTPOOL)) { 741c5904d13Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 742c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 7430713e232SGeorge Wilson &vd->vdev_dtl_object); 744c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 745c5904d13Seschrock &vd->vdev_unspare); 746c5904d13Seschrock } 74721ecdf64SLin Ling 74821ecdf64SLin Ling if (alloctype == VDEV_ALLOC_ROOTPOOL) { 74921ecdf64SLin Ling uint64_t spare = 0; 75021ecdf64SLin Ling 75121ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 75221ecdf64SLin Ling &spare) == 0 && spare) 75321ecdf64SLin Ling spa_spare_add(vd); 75421ecdf64SLin Ling } 75521ecdf64SLin Ling 756ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 757ecc2d604Sbonwick &vd->vdev_offline); 758c5904d13Seschrock 759b4952e17SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 760b4952e17SGeorge Wilson &vd->vdev_resilver_txg); 761cb04b873SMark J Musante 7623d7072f8Seschrock /* 7633d7072f8Seschrock * When importing a pool, we want to ignore the persistent fault 7643d7072f8Seschrock * state, as the diagnosis made on another system may not be 765069f55e2SEric Schrock * valid in the current context. Local vdevs will 766069f55e2SEric Schrock * remain in the faulted state. 7673d7072f8Seschrock */ 768b16da2e2SGeorge Wilson if (spa_load_state(spa) == SPA_LOAD_OPEN) { 7693d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 7703d7072f8Seschrock &vd->vdev_faulted); 7713d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 7723d7072f8Seschrock &vd->vdev_degraded); 7733d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 7743d7072f8Seschrock &vd->vdev_removed); 775069f55e2SEric Schrock 776069f55e2SEric Schrock if (vd->vdev_faulted || vd->vdev_degraded) { 777069f55e2SEric Schrock char *aux; 778069f55e2SEric Schrock 779069f55e2SEric Schrock vd->vdev_label_aux = 780069f55e2SEric Schrock VDEV_AUX_ERR_EXCEEDED; 781069f55e2SEric Schrock if (nvlist_lookup_string(nv, 782069f55e2SEric Schrock ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 783069f55e2SEric Schrock strcmp(aux, "external") == 0) 784069f55e2SEric Schrock vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 785069f55e2SEric Schrock } 7863d7072f8Seschrock } 787fa9e4066Sahrens } 788fa9e4066Sahrens 789fa9e4066Sahrens /* 790fa9e4066Sahrens * Add ourselves to the parent's list of children. 791fa9e4066Sahrens */ 792fa9e4066Sahrens vdev_add_child(parent, vd); 793fa9e4066Sahrens 79499653d4eSeschrock *vdp = vd; 79599653d4eSeschrock 79699653d4eSeschrock return (0); 797fa9e4066Sahrens } 798fa9e4066Sahrens 799fa9e4066Sahrens void 800fa9e4066Sahrens vdev_free(vdev_t *vd) 801fa9e4066Sahrens { 8023d7072f8Seschrock spa_t *spa = vd->vdev_spa; 803094e47e9SGeorge Wilson ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 804fa9e4066Sahrens 805fa9e4066Sahrens /* 806fa9e4066Sahrens * vdev_free() implies closing the vdev first. This is simpler than 807fa9e4066Sahrens * trying to ensure complicated semantics for all callers. 808fa9e4066Sahrens */ 809fa9e4066Sahrens vdev_close(vd); 810fa9e4066Sahrens 811e14bb325SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 812b24ab676SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 813fa9e4066Sahrens 814fa9e4066Sahrens /* 815fa9e4066Sahrens * Free all children. 816fa9e4066Sahrens */ 817573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 818fa9e4066Sahrens vdev_free(vd->vdev_child[c]); 819fa9e4066Sahrens 820fa9e4066Sahrens ASSERT(vd->vdev_child == NULL); 821fa9e4066Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 822094e47e9SGeorge Wilson ASSERT(vd->vdev_initialize_thread == NULL); 823fa9e4066Sahrens 824fa9e4066Sahrens /* 825fa9e4066Sahrens * Discard allocation state. 826fa9e4066Sahrens */ 827a1521560SJeff Bonwick if (vd->vdev_mg != NULL) { 828fa9e4066Sahrens vdev_metaslab_fini(vd); 829a1521560SJeff Bonwick metaslab_group_destroy(vd->vdev_mg); 830a1521560SJeff Bonwick } 831fa9e4066Sahrens 832fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_space); 833fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_dspace); 834fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 835fa9e4066Sahrens 836fa9e4066Sahrens /* 837fa9e4066Sahrens * Remove this vdev from its parent's child list. 838fa9e4066Sahrens */ 839fa9e4066Sahrens vdev_remove_child(vd->vdev_parent, vd); 840fa9e4066Sahrens 841fa9e4066Sahrens ASSERT(vd->vdev_parent == NULL); 842e0f1c0afSOlaf Faaland ASSERT(!list_link_active(&vd->vdev_leaf_node)); 843fa9e4066Sahrens 8443d7072f8Seschrock /* 8453d7072f8Seschrock * Clean up vdev structure. 8463d7072f8Seschrock */ 8473d7072f8Seschrock vdev_queue_fini(vd); 8483d7072f8Seschrock vdev_cache_fini(vd); 8493d7072f8Seschrock 8503d7072f8Seschrock if (vd->vdev_path) 8513d7072f8Seschrock spa_strfree(vd->vdev_path); 8523d7072f8Seschrock if (vd->vdev_devid) 8533d7072f8Seschrock spa_strfree(vd->vdev_devid); 8543d7072f8Seschrock if (vd->vdev_physpath) 8553d7072f8Seschrock spa_strfree(vd->vdev_physpath); 8566809eb4eSEric Schrock if (vd->vdev_fru) 8576809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 8583d7072f8Seschrock 8593d7072f8Seschrock if (vd->vdev_isspare) 8603d7072f8Seschrock spa_spare_remove(vd); 861fa94a07fSbrendan if (vd->vdev_isl2cache) 862fa94a07fSbrendan spa_l2cache_remove(vd); 8633d7072f8Seschrock 8643d7072f8Seschrock txg_list_destroy(&vd->vdev_ms_list); 8653d7072f8Seschrock txg_list_destroy(&vd->vdev_dtl_list); 8668ad4d6ddSJeff Bonwick 8673d7072f8Seschrock mutex_enter(&vd->vdev_dtl_lock); 8680713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 8698ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 8700713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 8710713e232SGeorge Wilson range_tree_destroy(vd->vdev_dtl[t]); 8728ad4d6ddSJeff Bonwick } 8733d7072f8Seschrock mutex_exit(&vd->vdev_dtl_lock); 8748ad4d6ddSJeff Bonwick 8755cabbc6bSPrashanth Sreenivasa EQUIV(vd->vdev_indirect_births != NULL, 8765cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping != NULL); 8775cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_births != NULL) { 8785cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 8795cabbc6bSPrashanth Sreenivasa vdev_indirect_births_close(vd->vdev_indirect_births); 8805cabbc6bSPrashanth Sreenivasa } 8815cabbc6bSPrashanth Sreenivasa 8825cabbc6bSPrashanth Sreenivasa if (vd->vdev_obsolete_sm != NULL) { 8835cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 8845cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 8855cabbc6bSPrashanth Sreenivasa space_map_close(vd->vdev_obsolete_sm); 8865cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_sm = NULL; 8875cabbc6bSPrashanth Sreenivasa } 8885cabbc6bSPrashanth Sreenivasa range_tree_destroy(vd->vdev_obsolete_segments); 8895cabbc6bSPrashanth Sreenivasa rw_destroy(&vd->vdev_indirect_rwlock); 8905cabbc6bSPrashanth Sreenivasa mutex_destroy(&vd->vdev_obsolete_lock); 8915cabbc6bSPrashanth Sreenivasa 8920f7643c7SGeorge Wilson mutex_destroy(&vd->vdev_queue_lock); 8933d7072f8Seschrock mutex_destroy(&vd->vdev_dtl_lock); 8943d7072f8Seschrock mutex_destroy(&vd->vdev_stat_lock); 895e14bb325SJeff Bonwick mutex_destroy(&vd->vdev_probe_lock); 896094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_lock); 897094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_io_lock); 898094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_io_cv); 899094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_cv); 9003d7072f8Seschrock 9013d7072f8Seschrock if (vd == spa->spa_root_vdev) 9023d7072f8Seschrock spa->spa_root_vdev = NULL; 9033d7072f8Seschrock 9043d7072f8Seschrock kmem_free(vd, sizeof (vdev_t)); 905fa9e4066Sahrens } 906fa9e4066Sahrens 907fa9e4066Sahrens /* 908fa9e4066Sahrens * Transfer top-level vdev state from svd to tvd. 909fa9e4066Sahrens */ 910fa9e4066Sahrens static void 911fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 912fa9e4066Sahrens { 913fa9e4066Sahrens spa_t *spa = svd->vdev_spa; 914fa9e4066Sahrens metaslab_t *msp; 915fa9e4066Sahrens vdev_t *vd; 916fa9e4066Sahrens int t; 917fa9e4066Sahrens 918fa9e4066Sahrens ASSERT(tvd == tvd->vdev_top); 919fa9e4066Sahrens 920fa9e4066Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 921fa9e4066Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 922fa9e4066Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 923215198a6SJoe Stein tvd->vdev_top_zap = svd->vdev_top_zap; 924fa9e4066Sahrens 925fa9e4066Sahrens svd->vdev_ms_array = 0; 926fa9e4066Sahrens svd->vdev_ms_shift = 0; 927fa9e4066Sahrens svd->vdev_ms_count = 0; 928215198a6SJoe Stein svd->vdev_top_zap = 0; 929fa9e4066Sahrens 930cd0837ccSGeorge Wilson if (tvd->vdev_mg) 931cd0837ccSGeorge Wilson ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 932fa9e4066Sahrens tvd->vdev_mg = svd->vdev_mg; 933fa9e4066Sahrens tvd->vdev_ms = svd->vdev_ms; 934fa9e4066Sahrens 935fa9e4066Sahrens svd->vdev_mg = NULL; 936fa9e4066Sahrens svd->vdev_ms = NULL; 937ecc2d604Sbonwick 938ecc2d604Sbonwick if (tvd->vdev_mg != NULL) 939ecc2d604Sbonwick tvd->vdev_mg->mg_vd = tvd; 940fa9e4066Sahrens 94186714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 94286714001SSerapheim Dimitropoulos svd->vdev_checkpoint_sm = NULL; 94386714001SSerapheim Dimitropoulos 944*663207adSDon Brady tvd->vdev_alloc_bias = svd->vdev_alloc_bias; 945*663207adSDon Brady svd->vdev_alloc_bias = VDEV_BIAS_NONE; 946*663207adSDon Brady 947fa9e4066Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 948fa9e4066Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 94999653d4eSeschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 950fa9e4066Sahrens 951fa9e4066Sahrens svd->vdev_stat.vs_alloc = 0; 952fa9e4066Sahrens svd->vdev_stat.vs_space = 0; 95399653d4eSeschrock svd->vdev_stat.vs_dspace = 0; 954fa9e4066Sahrens 9553a4b1be9SMatthew Ahrens /* 9563a4b1be9SMatthew Ahrens * State which may be set on a top-level vdev that's in the 9573a4b1be9SMatthew Ahrens * process of being removed. 9583a4b1be9SMatthew Ahrens */ 9593a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_births_object); 9603a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 9613a4b1be9SMatthew Ahrens ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 9623a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 9633a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 9643a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 9653a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_removing); 9663a4b1be9SMatthew Ahrens tvd->vdev_removing = svd->vdev_removing; 9673a4b1be9SMatthew Ahrens tvd->vdev_indirect_config = svd->vdev_indirect_config; 9683a4b1be9SMatthew Ahrens tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 9693a4b1be9SMatthew Ahrens tvd->vdev_indirect_births = svd->vdev_indirect_births; 9703a4b1be9SMatthew Ahrens range_tree_swap(&svd->vdev_obsolete_segments, 9713a4b1be9SMatthew Ahrens &tvd->vdev_obsolete_segments); 9723a4b1be9SMatthew Ahrens tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 9733a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_mapping_object = 0; 9743a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_births_object = 0; 9753a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 9763a4b1be9SMatthew Ahrens svd->vdev_indirect_mapping = NULL; 9773a4b1be9SMatthew Ahrens svd->vdev_indirect_births = NULL; 9783a4b1be9SMatthew Ahrens svd->vdev_obsolete_sm = NULL; 9793a4b1be9SMatthew Ahrens svd->vdev_removing = 0; 9803a4b1be9SMatthew Ahrens 981fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 982fa9e4066Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 983fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 984fa9e4066Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 985fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 986fa9e4066Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 987fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 988fa9e4066Sahrens } 989fa9e4066Sahrens 990e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_config_dirty_node)) { 991fa9e4066Sahrens vdev_config_clean(svd); 992fa9e4066Sahrens vdev_config_dirty(tvd); 993fa9e4066Sahrens } 994fa9e4066Sahrens 995e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_state_dirty_node)) { 996e14bb325SJeff Bonwick vdev_state_clean(svd); 997e14bb325SJeff Bonwick vdev_state_dirty(tvd); 998e14bb325SJeff Bonwick } 999e14bb325SJeff Bonwick 100099653d4eSeschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 100199653d4eSeschrock svd->vdev_deflate_ratio = 0; 10028654d025Sperrin 10038654d025Sperrin tvd->vdev_islog = svd->vdev_islog; 10048654d025Sperrin svd->vdev_islog = 0; 1005fa9e4066Sahrens } 1006fa9e4066Sahrens 1007fa9e4066Sahrens static void 1008fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 1009fa9e4066Sahrens { 1010fa9e4066Sahrens if (vd == NULL) 1011fa9e4066Sahrens return; 1012fa9e4066Sahrens 1013fa9e4066Sahrens vd->vdev_top = tvd; 1014fa9e4066Sahrens 1015573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1016fa9e4066Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 1017fa9e4066Sahrens } 1018fa9e4066Sahrens 1019fa9e4066Sahrens /* 1020fa9e4066Sahrens * Add a mirror/replacing vdev above an existing vdev. 1021fa9e4066Sahrens */ 1022fa9e4066Sahrens vdev_t * 1023fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 1024fa9e4066Sahrens { 1025fa9e4066Sahrens spa_t *spa = cvd->vdev_spa; 1026fa9e4066Sahrens vdev_t *pvd = cvd->vdev_parent; 1027fa9e4066Sahrens vdev_t *mvd; 1028fa9e4066Sahrens 1029e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1030fa9e4066Sahrens 1031fa9e4066Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 1032ecc2d604Sbonwick 1033ecc2d604Sbonwick mvd->vdev_asize = cvd->vdev_asize; 1034573ca77eSGeorge Wilson mvd->vdev_min_asize = cvd->vdev_min_asize; 10354263d13fSGeorge Wilson mvd->vdev_max_asize = cvd->vdev_max_asize; 10365cabbc6bSPrashanth Sreenivasa mvd->vdev_psize = cvd->vdev_psize; 1037ecc2d604Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 1038ecc2d604Sbonwick mvd->vdev_state = cvd->vdev_state; 103988ecc943SGeorge Wilson mvd->vdev_crtxg = cvd->vdev_crtxg; 1040ecc2d604Sbonwick 1041fa9e4066Sahrens vdev_remove_child(pvd, cvd); 1042fa9e4066Sahrens vdev_add_child(pvd, mvd); 1043fa9e4066Sahrens cvd->vdev_id = mvd->vdev_children; 1044fa9e4066Sahrens vdev_add_child(mvd, cvd); 1045fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1046fa9e4066Sahrens 1047fa9e4066Sahrens if (mvd == mvd->vdev_top) 1048fa9e4066Sahrens vdev_top_transfer(cvd, mvd); 1049fa9e4066Sahrens 1050fa9e4066Sahrens return (mvd); 1051fa9e4066Sahrens } 1052fa9e4066Sahrens 1053fa9e4066Sahrens /* 1054fa9e4066Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 1055fa9e4066Sahrens */ 1056fa9e4066Sahrens void 1057fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd) 1058fa9e4066Sahrens { 1059fa9e4066Sahrens vdev_t *mvd = cvd->vdev_parent; 1060fa9e4066Sahrens vdev_t *pvd = mvd->vdev_parent; 1061fa9e4066Sahrens 1062e14bb325SJeff Bonwick ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1063fa9e4066Sahrens 1064fa9e4066Sahrens ASSERT(mvd->vdev_children == 1); 1065fa9e4066Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 106699653d4eSeschrock mvd->vdev_ops == &vdev_replacing_ops || 106799653d4eSeschrock mvd->vdev_ops == &vdev_spare_ops); 1068ecc2d604Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 1069fa9e4066Sahrens 1070fa9e4066Sahrens vdev_remove_child(mvd, cvd); 1071fa9e4066Sahrens vdev_remove_child(pvd, mvd); 10728ad4d6ddSJeff Bonwick 107399653d4eSeschrock /* 1074e14bb325SJeff Bonwick * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1075e14bb325SJeff Bonwick * Otherwise, we could have detached an offline device, and when we 1076e14bb325SJeff Bonwick * go to import the pool we'll think we have two top-level vdevs, 1077e14bb325SJeff Bonwick * instead of a different version of the same top-level vdev. 107899653d4eSeschrock */ 10798ad4d6ddSJeff Bonwick if (mvd->vdev_top == mvd) { 10808ad4d6ddSJeff Bonwick uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 10811195e687SMark J Musante cvd->vdev_orig_guid = cvd->vdev_guid; 10828ad4d6ddSJeff Bonwick cvd->vdev_guid += guid_delta; 10838ad4d6ddSJeff Bonwick cvd->vdev_guid_sum += guid_delta; 10848ad4d6ddSJeff Bonwick } 1085e14bb325SJeff Bonwick cvd->vdev_id = mvd->vdev_id; 1086e14bb325SJeff Bonwick vdev_add_child(pvd, cvd); 1087fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1088fa9e4066Sahrens 1089fa9e4066Sahrens if (cvd == cvd->vdev_top) 1090fa9e4066Sahrens vdev_top_transfer(mvd, cvd); 1091fa9e4066Sahrens 1092fa9e4066Sahrens ASSERT(mvd->vdev_children == 0); 1093fa9e4066Sahrens vdev_free(mvd); 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096*663207adSDon Brady static void 1097*663207adSDon Brady vdev_metaslab_group_create(vdev_t *vd) 1098*663207adSDon Brady { 1099*663207adSDon Brady spa_t *spa = vd->vdev_spa; 1100*663207adSDon Brady 1101*663207adSDon Brady /* 1102*663207adSDon Brady * metaslab_group_create was delayed until allocation bias was available 1103*663207adSDon Brady */ 1104*663207adSDon Brady if (vd->vdev_mg == NULL) { 1105*663207adSDon Brady metaslab_class_t *mc; 1106*663207adSDon Brady 1107*663207adSDon Brady if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE) 1108*663207adSDon Brady vd->vdev_alloc_bias = VDEV_BIAS_LOG; 1109*663207adSDon Brady 1110*663207adSDon Brady ASSERT3U(vd->vdev_islog, ==, 1111*663207adSDon Brady (vd->vdev_alloc_bias == VDEV_BIAS_LOG)); 1112*663207adSDon Brady 1113*663207adSDon Brady switch (vd->vdev_alloc_bias) { 1114*663207adSDon Brady case VDEV_BIAS_LOG: 1115*663207adSDon Brady mc = spa_log_class(spa); 1116*663207adSDon Brady break; 1117*663207adSDon Brady case VDEV_BIAS_SPECIAL: 1118*663207adSDon Brady mc = spa_special_class(spa); 1119*663207adSDon Brady break; 1120*663207adSDon Brady case VDEV_BIAS_DEDUP: 1121*663207adSDon Brady mc = spa_dedup_class(spa); 1122*663207adSDon Brady break; 1123*663207adSDon Brady default: 1124*663207adSDon Brady mc = spa_normal_class(spa); 1125*663207adSDon Brady } 1126*663207adSDon Brady 1127*663207adSDon Brady vd->vdev_mg = metaslab_group_create(mc, vd, 1128*663207adSDon Brady spa->spa_alloc_count); 1129*663207adSDon Brady 1130*663207adSDon Brady /* 1131*663207adSDon Brady * The spa ashift values currently only reflect the 1132*663207adSDon Brady * general vdev classes. Class destination is late 1133*663207adSDon Brady * binding so ashift checking had to wait until now 1134*663207adSDon Brady */ 1135*663207adSDon Brady if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1136*663207adSDon Brady mc == spa_normal_class(spa) && vd->vdev_aux == NULL) { 1137*663207adSDon Brady if (vd->vdev_ashift > spa->spa_max_ashift) 1138*663207adSDon Brady spa->spa_max_ashift = vd->vdev_ashift; 1139*663207adSDon Brady if (vd->vdev_ashift < spa->spa_min_ashift) 1140*663207adSDon Brady spa->spa_min_ashift = vd->vdev_ashift; 1141*663207adSDon Brady } 1142*663207adSDon Brady } 1143*663207adSDon Brady } 1144*663207adSDon Brady 1145ea8dc4b6Seschrock int 1146fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1147fa9e4066Sahrens { 1148fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 1149ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 1150ecc2d604Sbonwick uint64_t m; 1151fa9e4066Sahrens uint64_t oldc = vd->vdev_ms_count; 1152fa9e4066Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1153ecc2d604Sbonwick metaslab_t **mspp; 1154ecc2d604Sbonwick int error; 1155*663207adSDon Brady boolean_t expanding = (oldc != 0); 1156fa9e4066Sahrens 1157a1521560SJeff Bonwick ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1158a1521560SJeff Bonwick 115988ecc943SGeorge Wilson /* 116088ecc943SGeorge Wilson * This vdev is not being allocated from yet or is a hole. 116188ecc943SGeorge Wilson */ 116288ecc943SGeorge Wilson if (vd->vdev_ms_shift == 0) 11630e34b6a7Sbonwick return (0); 11640e34b6a7Sbonwick 116588ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 116688ecc943SGeorge Wilson 1167fa9e4066Sahrens ASSERT(oldc <= newc); 1168fa9e4066Sahrens 1169ecc2d604Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1170fa9e4066Sahrens 1171*663207adSDon Brady if (expanding) { 1172ecc2d604Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1173ecc2d604Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1174ecc2d604Sbonwick } 1175fa9e4066Sahrens 1176ecc2d604Sbonwick vd->vdev_ms = mspp; 1177ecc2d604Sbonwick vd->vdev_ms_count = newc; 1178ecc2d604Sbonwick for (m = oldc; m < newc; m++) { 11790713e232SGeorge Wilson uint64_t object = 0; 11800713e232SGeorge Wilson 11815cabbc6bSPrashanth Sreenivasa /* 11825cabbc6bSPrashanth Sreenivasa * vdev_ms_array may be 0 if we are creating the "fake" 11835cabbc6bSPrashanth Sreenivasa * metaslabs for an indirect vdev for zdb's leak detection. 11845cabbc6bSPrashanth Sreenivasa * See zdb_leak_init(). 11855cabbc6bSPrashanth Sreenivasa */ 11865cabbc6bSPrashanth Sreenivasa if (txg == 0 && vd->vdev_ms_array != 0) { 1187ecc2d604Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 11887bfdf011SNeil Perrin m * sizeof (uint64_t), sizeof (uint64_t), &object, 11897bfdf011SNeil Perrin DMU_READ_PREFETCH); 11903ee8c80cSPavel Zakharov if (error != 0) { 11913ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "unable to read the metaslab " 11923ee8c80cSPavel Zakharov "array [error=%d]", error); 1193ecc2d604Sbonwick return (error); 11943ee8c80cSPavel Zakharov } 1195fa9e4066Sahrens } 11961e9bd7ecSPrakash Surya 1197*663207adSDon Brady #ifndef _KERNEL 1198*663207adSDon Brady /* 1199*663207adSDon Brady * To accomodate zdb_leak_init() fake indirect 1200*663207adSDon Brady * metaslabs, we allocate a metaslab group for 1201*663207adSDon Brady * indirect vdevs which normally don't have one. 1202*663207adSDon Brady */ 1203*663207adSDon Brady if (vd->vdev_mg == NULL) { 1204*663207adSDon Brady ASSERT0(vdev_is_concrete(vd)); 1205*663207adSDon Brady vdev_metaslab_group_create(vd); 1206*663207adSDon Brady } 1207*663207adSDon Brady #endif 12081e9bd7ecSPrakash Surya error = metaslab_init(vd->vdev_mg, m, object, txg, 12091e9bd7ecSPrakash Surya &(vd->vdev_ms[m])); 12103ee8c80cSPavel Zakharov if (error != 0) { 12113ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 12123ee8c80cSPavel Zakharov error); 12131e9bd7ecSPrakash Surya return (error); 12143ee8c80cSPavel Zakharov } 1215fa9e4066Sahrens } 1216fa9e4066Sahrens 1217a1521560SJeff Bonwick if (txg == 0) 1218a1521560SJeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1219a1521560SJeff Bonwick 12203f9d6ad7SLin Ling /* 12213f9d6ad7SLin Ling * If the vdev is being removed we don't activate 12223f9d6ad7SLin Ling * the metaslabs since we want to ensure that no new 12233f9d6ad7SLin Ling * allocations are performed on this device. 12243f9d6ad7SLin Ling */ 1225*663207adSDon Brady if (!expanding && !vd->vdev_removing) { 1226a1521560SJeff Bonwick metaslab_group_activate(vd->vdev_mg); 1227*663207adSDon Brady } 1228a1521560SJeff Bonwick 1229a1521560SJeff Bonwick if (txg == 0) 1230a1521560SJeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG); 1231a1521560SJeff Bonwick 1232ea8dc4b6Seschrock return (0); 1233fa9e4066Sahrens } 1234fa9e4066Sahrens 1235fa9e4066Sahrens void 1236fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd) 1237fa9e4066Sahrens { 123886714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 123986714001SSerapheim Dimitropoulos ASSERT(spa_feature_is_active(vd->vdev_spa, 124086714001SSerapheim Dimitropoulos SPA_FEATURE_POOL_CHECKPOINT)); 124186714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 124286714001SSerapheim Dimitropoulos /* 124386714001SSerapheim Dimitropoulos * Even though we close the space map, we need to set its 124486714001SSerapheim Dimitropoulos * pointer to NULL. The reason is that vdev_metaslab_fini() 124586714001SSerapheim Dimitropoulos * may be called multiple times for certain operations 124686714001SSerapheim Dimitropoulos * (i.e. when destroying a pool) so we need to ensure that 124786714001SSerapheim Dimitropoulos * this clause never executes twice. This logic is similar 124886714001SSerapheim Dimitropoulos * to the one used for the vdev_ms clause below. 124986714001SSerapheim Dimitropoulos */ 125086714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 125186714001SSerapheim Dimitropoulos } 125286714001SSerapheim Dimitropoulos 1253fa9e4066Sahrens if (vd->vdev_ms != NULL) { 12545cabbc6bSPrashanth Sreenivasa uint64_t count = vd->vdev_ms_count; 12555cabbc6bSPrashanth Sreenivasa 1256a1521560SJeff Bonwick metaslab_group_passivate(vd->vdev_mg); 12575cabbc6bSPrashanth Sreenivasa for (uint64_t m = 0; m < count; m++) { 12580713e232SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 12590713e232SGeorge Wilson 12600713e232SGeorge Wilson if (msp != NULL) 12610713e232SGeorge Wilson metaslab_fini(msp); 12620713e232SGeorge Wilson } 1263fa9e4066Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1264fa9e4066Sahrens vd->vdev_ms = NULL; 12655cabbc6bSPrashanth Sreenivasa 12665cabbc6bSPrashanth Sreenivasa vd->vdev_ms_count = 0; 1267fa9e4066Sahrens } 12685cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_ms_count); 1269fa9e4066Sahrens } 1270fa9e4066Sahrens 1271e14bb325SJeff Bonwick typedef struct vdev_probe_stats { 1272e14bb325SJeff Bonwick boolean_t vps_readable; 1273e14bb325SJeff Bonwick boolean_t vps_writeable; 1274e14bb325SJeff Bonwick int vps_flags; 1275e14bb325SJeff Bonwick } vdev_probe_stats_t; 1276e14bb325SJeff Bonwick 1277e14bb325SJeff Bonwick static void 1278e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio) 12790a4e9518Sgw { 12808ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 1281a3f829aeSBill Moore vdev_t *vd = zio->io_vd; 1282e14bb325SJeff Bonwick vdev_probe_stats_t *vps = zio->io_private; 1283a3f829aeSBill Moore 1284a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio != NULL); 1285e14bb325SJeff Bonwick 1286e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) { 1287e14bb325SJeff Bonwick if (zio->io_error == 0) 1288e14bb325SJeff Bonwick vps->vps_readable = 1; 12898ad4d6ddSJeff Bonwick if (zio->io_error == 0 && spa_writeable(spa)) { 1290a3f829aeSBill Moore zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1291770499e1SDan Kimmel zio->io_offset, zio->io_size, zio->io_abd, 1292e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1293e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1294e14bb325SJeff Bonwick } else { 1295770499e1SDan Kimmel abd_free(zio->io_abd); 1296e14bb325SJeff Bonwick } 1297e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_WRITE) { 1298e14bb325SJeff Bonwick if (zio->io_error == 0) 1299e14bb325SJeff Bonwick vps->vps_writeable = 1; 1300770499e1SDan Kimmel abd_free(zio->io_abd); 1301e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_NULL) { 1302a3f829aeSBill Moore zio_t *pio; 1303e14bb325SJeff Bonwick 1304e14bb325SJeff Bonwick vd->vdev_cant_read |= !vps->vps_readable; 1305e14bb325SJeff Bonwick vd->vdev_cant_write |= !vps->vps_writeable; 1306e14bb325SJeff Bonwick 1307e14bb325SJeff Bonwick if (vdev_readable(vd) && 13088ad4d6ddSJeff Bonwick (vdev_writeable(vd) || !spa_writeable(spa))) { 1309e14bb325SJeff Bonwick zio->io_error = 0; 1310e14bb325SJeff Bonwick } else { 1311e14bb325SJeff Bonwick ASSERT(zio->io_error != 0); 13123ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "failed probe"); 1313e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 13148ad4d6ddSJeff Bonwick spa, vd, NULL, 0, 0); 1315be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 1316e14bb325SJeff Bonwick } 1317a3f829aeSBill Moore 1318a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1319a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio == zio); 1320a3f829aeSBill Moore vd->vdev_probe_zio = NULL; 1321a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1322a3f829aeSBill Moore 13230f7643c7SGeorge Wilson zio_link_t *zl = NULL; 13240f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1325a3f829aeSBill Moore if (!vdev_accessible(vd, pio)) 1326be6fd75aSMatthew Ahrens pio->io_error = SET_ERROR(ENXIO); 1327a3f829aeSBill Moore 1328e14bb325SJeff Bonwick kmem_free(vps, sizeof (*vps)); 1329e14bb325SJeff Bonwick } 1330e14bb325SJeff Bonwick } 13310a4e9518Sgw 1332e14bb325SJeff Bonwick /* 1333f7170741SWill Andrews * Determine whether this device is accessible. 1334f7170741SWill Andrews * 1335f7170741SWill Andrews * Read and write to several known locations: the pad regions of each 1336f7170741SWill Andrews * vdev label but the first, which we leave alone in case it contains 1337f7170741SWill Andrews * a VTOC. 1338e14bb325SJeff Bonwick */ 1339e14bb325SJeff Bonwick zio_t * 1340a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio) 1341e14bb325SJeff Bonwick { 1342e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 1343a3f829aeSBill Moore vdev_probe_stats_t *vps = NULL; 1344a3f829aeSBill Moore zio_t *pio; 1345a3f829aeSBill Moore 1346a3f829aeSBill Moore ASSERT(vd->vdev_ops->vdev_op_leaf); 13470a4e9518Sgw 1348a3f829aeSBill Moore /* 1349a3f829aeSBill Moore * Don't probe the probe. 1350a3f829aeSBill Moore */ 1351a3f829aeSBill Moore if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1352a3f829aeSBill Moore return (NULL); 1353e14bb325SJeff Bonwick 1354a3f829aeSBill Moore /* 1355a3f829aeSBill Moore * To prevent 'probe storms' when a device fails, we create 1356a3f829aeSBill Moore * just one probe i/o at a time. All zios that want to probe 1357a3f829aeSBill Moore * this vdev will become parents of the probe io. 1358a3f829aeSBill Moore */ 1359a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1360e14bb325SJeff Bonwick 1361a3f829aeSBill Moore if ((pio = vd->vdev_probe_zio) == NULL) { 1362a3f829aeSBill Moore vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1363a3f829aeSBill Moore 1364a3f829aeSBill Moore vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1365a3f829aeSBill Moore ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 13668956713aSEric Schrock ZIO_FLAG_TRYHARD; 1367a3f829aeSBill Moore 1368a3f829aeSBill Moore if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1369a3f829aeSBill Moore /* 1370a3f829aeSBill Moore * vdev_cant_read and vdev_cant_write can only 1371a3f829aeSBill Moore * transition from TRUE to FALSE when we have the 1372a3f829aeSBill Moore * SCL_ZIO lock as writer; otherwise they can only 1373a3f829aeSBill Moore * transition from FALSE to TRUE. This ensures that 1374a3f829aeSBill Moore * any zio looking at these values can assume that 1375a3f829aeSBill Moore * failures persist for the life of the I/O. That's 1376a3f829aeSBill Moore * important because when a device has intermittent 1377a3f829aeSBill Moore * connectivity problems, we want to ensure that 1378a3f829aeSBill Moore * they're ascribed to the device (ENXIO) and not 1379a3f829aeSBill Moore * the zio (EIO). 1380a3f829aeSBill Moore * 1381a3f829aeSBill Moore * Since we hold SCL_ZIO as writer here, clear both 1382a3f829aeSBill Moore * values so the probe can reevaluate from first 1383a3f829aeSBill Moore * principles. 1384a3f829aeSBill Moore */ 1385a3f829aeSBill Moore vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1386a3f829aeSBill Moore vd->vdev_cant_read = B_FALSE; 1387a3f829aeSBill Moore vd->vdev_cant_write = B_FALSE; 1388a3f829aeSBill Moore } 1389a3f829aeSBill Moore 1390a3f829aeSBill Moore vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1391a3f829aeSBill Moore vdev_probe_done, vps, 1392a3f829aeSBill Moore vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1393a3f829aeSBill Moore 139498d1cbfeSGeorge Wilson /* 139598d1cbfeSGeorge Wilson * We can't change the vdev state in this context, so we 139698d1cbfeSGeorge Wilson * kick off an async task to do it on our behalf. 139798d1cbfeSGeorge Wilson */ 1398a3f829aeSBill Moore if (zio != NULL) { 1399a3f829aeSBill Moore vd->vdev_probe_wanted = B_TRUE; 1400a3f829aeSBill Moore spa_async_request(spa, SPA_ASYNC_PROBE); 1401a3f829aeSBill Moore } 1402e14bb325SJeff Bonwick } 1403e14bb325SJeff Bonwick 1404a3f829aeSBill Moore if (zio != NULL) 1405a3f829aeSBill Moore zio_add_child(zio, pio); 1406e14bb325SJeff Bonwick 1407a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1408e14bb325SJeff Bonwick 1409a3f829aeSBill Moore if (vps == NULL) { 1410a3f829aeSBill Moore ASSERT(zio != NULL); 1411a3f829aeSBill Moore return (NULL); 1412a3f829aeSBill Moore } 1413e14bb325SJeff Bonwick 1414e14bb325SJeff Bonwick for (int l = 1; l < VDEV_LABELS; l++) { 1415a3f829aeSBill Moore zio_nowait(zio_read_phys(pio, vd, 1416e14bb325SJeff Bonwick vdev_label_offset(vd->vdev_psize, l, 1417770499e1SDan Kimmel offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1418770499e1SDan Kimmel abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1419e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1420e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1421e14bb325SJeff Bonwick } 1422e14bb325SJeff Bonwick 1423a3f829aeSBill Moore if (zio == NULL) 1424a3f829aeSBill Moore return (pio); 1425a3f829aeSBill Moore 1426a3f829aeSBill Moore zio_nowait(pio); 1427a3f829aeSBill Moore return (NULL); 14280a4e9518Sgw } 14290a4e9518Sgw 1430f64c0e34SEric Taylor static void 1431f64c0e34SEric Taylor vdev_open_child(void *arg) 1432f64c0e34SEric Taylor { 1433f64c0e34SEric Taylor vdev_t *vd = arg; 1434f64c0e34SEric Taylor 1435f64c0e34SEric Taylor vd->vdev_open_thread = curthread; 1436f64c0e34SEric Taylor vd->vdev_open_error = vdev_open(vd); 1437f64c0e34SEric Taylor vd->vdev_open_thread = NULL; 1438f64c0e34SEric Taylor } 1439f64c0e34SEric Taylor 1440681d9761SEric Taylor boolean_t 1441681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd) 1442681d9761SEric Taylor { 1443681d9761SEric Taylor if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1444681d9761SEric Taylor strlen(ZVOL_DIR)) == 0) 1445681d9761SEric Taylor return (B_TRUE); 1446681d9761SEric Taylor for (int c = 0; c < vd->vdev_children; c++) 1447681d9761SEric Taylor if (vdev_uses_zvols(vd->vdev_child[c])) 1448681d9761SEric Taylor return (B_TRUE); 1449681d9761SEric Taylor return (B_FALSE); 1450681d9761SEric Taylor } 1451681d9761SEric Taylor 1452f64c0e34SEric Taylor void 1453f64c0e34SEric Taylor vdev_open_children(vdev_t *vd) 1454f64c0e34SEric Taylor { 1455f64c0e34SEric Taylor taskq_t *tq; 1456f64c0e34SEric Taylor int children = vd->vdev_children; 1457f64c0e34SEric Taylor 1458681d9761SEric Taylor /* 1459681d9761SEric Taylor * in order to handle pools on top of zvols, do the opens 1460681d9761SEric Taylor * in a single thread so that the same thread holds the 1461681d9761SEric Taylor * spa_namespace_lock 1462681d9761SEric Taylor */ 1463681d9761SEric Taylor if (vdev_uses_zvols(vd)) { 1464681d9761SEric Taylor for (int c = 0; c < children; c++) 1465681d9761SEric Taylor vd->vdev_child[c]->vdev_open_error = 1466681d9761SEric Taylor vdev_open(vd->vdev_child[c]); 1467681d9761SEric Taylor return; 1468681d9761SEric Taylor } 1469f64c0e34SEric Taylor tq = taskq_create("vdev_open", children, minclsyspri, 1470f64c0e34SEric Taylor children, children, TASKQ_PREPOPULATE); 1471f64c0e34SEric Taylor 1472f64c0e34SEric Taylor for (int c = 0; c < children; c++) 1473f64c0e34SEric Taylor VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1474fc8ae2ecSToomas Soome TQ_SLEEP) != TASKQID_INVALID); 1475f64c0e34SEric Taylor 1476f64c0e34SEric Taylor taskq_destroy(tq); 1477f64c0e34SEric Taylor } 1478f64c0e34SEric Taylor 14795cabbc6bSPrashanth Sreenivasa /* 14805cabbc6bSPrashanth Sreenivasa * Compute the raidz-deflation ratio. Note, we hard-code 14815cabbc6bSPrashanth Sreenivasa * in 128k (1 << 17) because it is the "typical" blocksize. 14825cabbc6bSPrashanth Sreenivasa * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 14835cabbc6bSPrashanth Sreenivasa * otherwise it would inconsistently account for existing bp's. 14845cabbc6bSPrashanth Sreenivasa */ 14855cabbc6bSPrashanth Sreenivasa static void 14865cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd) 14875cabbc6bSPrashanth Sreenivasa { 14885cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 14895cabbc6bSPrashanth Sreenivasa vd->vdev_deflate_ratio = (1 << 17) / 14905cabbc6bSPrashanth Sreenivasa (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 14915cabbc6bSPrashanth Sreenivasa } 14925cabbc6bSPrashanth Sreenivasa } 14935cabbc6bSPrashanth Sreenivasa 1494fa9e4066Sahrens /* 1495fa9e4066Sahrens * Prepare a virtual device for access. 1496fa9e4066Sahrens */ 1497fa9e4066Sahrens int 1498fa9e4066Sahrens vdev_open(vdev_t *vd) 1499fa9e4066Sahrens { 15008ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1501fa9e4066Sahrens int error; 1502fa9e4066Sahrens uint64_t osize = 0; 15034263d13fSGeorge Wilson uint64_t max_osize = 0; 15044263d13fSGeorge Wilson uint64_t asize, max_asize, psize; 1505ecc2d604Sbonwick uint64_t ashift = 0; 1506fa9e4066Sahrens 1507f64c0e34SEric Taylor ASSERT(vd->vdev_open_thread == curthread || 1508f64c0e34SEric Taylor spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1509fa9e4066Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1510fa9e4066Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1511fa9e4066Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1512fa9e4066Sahrens 1513fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1514e6ca193dSGeorge Wilson vd->vdev_cant_read = B_FALSE; 1515e6ca193dSGeorge Wilson vd->vdev_cant_write = B_FALSE; 1516573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 1517fa9e4066Sahrens 1518069f55e2SEric Schrock /* 1519069f55e2SEric Schrock * If this vdev is not removed, check its fault status. If it's 1520069f55e2SEric Schrock * faulted, bail out of the open. 1521069f55e2SEric Schrock */ 15223d7072f8Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 15233d7072f8Seschrock ASSERT(vd->vdev_children == 0); 1524069f55e2SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1525069f55e2SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 15263d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1527069f55e2SEric Schrock vd->vdev_label_aux); 1528be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15293d7072f8Seschrock } else if (vd->vdev_offline) { 1530fa9e4066Sahrens ASSERT(vd->vdev_children == 0); 1531ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1532be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1533fa9e4066Sahrens } 1534fa9e4066Sahrens 15354263d13fSGeorge Wilson error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1536fa9e4066Sahrens 1537095bcd66SGeorge Wilson /* 1538095bcd66SGeorge Wilson * Reset the vdev_reopening flag so that we actually close 1539095bcd66SGeorge Wilson * the vdev on error. 1540095bcd66SGeorge Wilson */ 1541095bcd66SGeorge Wilson vd->vdev_reopening = B_FALSE; 1542ea8dc4b6Seschrock if (zio_injection_enabled && error == 0) 15438956713aSEric Schrock error = zio_handle_device_injection(vd, NULL, ENXIO); 1544ea8dc4b6Seschrock 1545fa9e4066Sahrens if (error) { 15463d7072f8Seschrock if (vd->vdev_removed && 15473d7072f8Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 15483d7072f8Seschrock vd->vdev_removed = B_FALSE; 15493d7072f8Seschrock 15506f793812SPavel Zakharov if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 15516f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 15526f793812SPavel Zakharov vd->vdev_stat.vs_aux); 15536f793812SPavel Zakharov } else { 15546f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 15556f793812SPavel Zakharov vd->vdev_stat.vs_aux); 15566f793812SPavel Zakharov } 1557fa9e4066Sahrens return (error); 1558fa9e4066Sahrens } 1559fa9e4066Sahrens 15603d7072f8Seschrock vd->vdev_removed = B_FALSE; 15613d7072f8Seschrock 1562096d22d4SEric Schrock /* 1563096d22d4SEric Schrock * Recheck the faulted flag now that we have confirmed that 1564096d22d4SEric Schrock * the vdev is accessible. If we're faulted, bail. 1565096d22d4SEric Schrock */ 1566096d22d4SEric Schrock if (vd->vdev_faulted) { 1567096d22d4SEric Schrock ASSERT(vd->vdev_children == 0); 1568096d22d4SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1569096d22d4SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1570096d22d4SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1571096d22d4SEric Schrock vd->vdev_label_aux); 1572be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1573096d22d4SEric Schrock } 1574096d22d4SEric Schrock 15753d7072f8Seschrock if (vd->vdev_degraded) { 15763d7072f8Seschrock ASSERT(vd->vdev_children == 0); 15773d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 15783d7072f8Seschrock VDEV_AUX_ERR_EXCEEDED); 15793d7072f8Seschrock } else { 1580069f55e2SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 15813d7072f8Seschrock } 1582fa9e4066Sahrens 158388ecc943SGeorge Wilson /* 158488ecc943SGeorge Wilson * For hole or missing vdevs we just return success. 158588ecc943SGeorge Wilson */ 158688ecc943SGeorge Wilson if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 158788ecc943SGeorge Wilson return (0); 158888ecc943SGeorge Wilson 1589573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 1590ea8dc4b6Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1591ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1592ea8dc4b6Seschrock VDEV_AUX_NONE); 1593ea8dc4b6Seschrock break; 1594ea8dc4b6Seschrock } 1595573ca77eSGeorge Wilson } 1596fa9e4066Sahrens 1597fa9e4066Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 15984263d13fSGeorge Wilson max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1599fa9e4066Sahrens 1600fa9e4066Sahrens if (vd->vdev_children == 0) { 1601fa9e4066Sahrens if (osize < SPA_MINDEVSIZE) { 1602ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1603ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1604be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1605fa9e4066Sahrens } 1606fa9e4066Sahrens psize = osize; 1607fa9e4066Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 16084263d13fSGeorge Wilson max_asize = max_osize - (VDEV_LABEL_START_SIZE + 16094263d13fSGeorge Wilson VDEV_LABEL_END_SIZE); 1610fa9e4066Sahrens } else { 1611ecc2d604Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1612fa9e4066Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1613ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1614ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1615be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1616fa9e4066Sahrens } 1617fa9e4066Sahrens psize = 0; 1618fa9e4066Sahrens asize = osize; 16194263d13fSGeorge Wilson max_asize = max_osize; 1620fa9e4066Sahrens } 1621fa9e4066Sahrens 1622fa9e4066Sahrens vd->vdev_psize = psize; 1623fa9e4066Sahrens 1624573ca77eSGeorge Wilson /* 1625c040c10cSSteven Hartland * Make sure the allocatable size hasn't shrunk too much. 1626573ca77eSGeorge Wilson */ 1627573ca77eSGeorge Wilson if (asize < vd->vdev_min_asize) { 1628573ca77eSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1629573ca77eSGeorge Wilson VDEV_AUX_BAD_LABEL); 1630be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1631573ca77eSGeorge Wilson } 1632573ca77eSGeorge Wilson 1633fa9e4066Sahrens if (vd->vdev_asize == 0) { 1634fa9e4066Sahrens /* 1635fa9e4066Sahrens * This is the first-ever open, so use the computed values. 1636ecc2d604Sbonwick * For testing purposes, a higher ashift can be requested. 1637fa9e4066Sahrens */ 1638fa9e4066Sahrens vd->vdev_asize = asize; 16394263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1640ecc2d604Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 164193a1902eSMatthew Ahrens vd->vdev_ashift = MAX(zfs_ashift_min, vd->vdev_ashift); 1642fa9e4066Sahrens } else { 1643fa9e4066Sahrens /* 16442384d9f8SGeorge Wilson * Detect if the alignment requirement has increased. 16452384d9f8SGeorge Wilson * We don't want to make the pool unavailable, just 16462384d9f8SGeorge Wilson * issue a warning instead. 1647fa9e4066Sahrens */ 16482384d9f8SGeorge Wilson if (ashift > vd->vdev_top->vdev_ashift && 16492384d9f8SGeorge Wilson vd->vdev_ops->vdev_op_leaf) { 16502384d9f8SGeorge Wilson cmn_err(CE_WARN, 16512384d9f8SGeorge Wilson "Disk, '%s', has a block alignment that is " 16522384d9f8SGeorge Wilson "larger than the pool's alignment\n", 16532384d9f8SGeorge Wilson vd->vdev_path); 1654fa9e4066Sahrens } 16554263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1656573ca77eSGeorge Wilson } 1657fa9e4066Sahrens 1658573ca77eSGeorge Wilson /* 1659c040c10cSSteven Hartland * If all children are healthy we update asize if either: 1660c040c10cSSteven Hartland * The asize has increased, due to a device expansion caused by dynamic 1661c040c10cSSteven Hartland * LUN growth or vdev replacement, and automatic expansion is enabled; 1662c040c10cSSteven Hartland * making the additional space available. 1663c040c10cSSteven Hartland * 1664c040c10cSSteven Hartland * The asize has decreased, due to a device shrink usually caused by a 1665c040c10cSSteven Hartland * vdev replace with a smaller device. This ensures that calculations 1666c040c10cSSteven Hartland * based of max_asize and asize e.g. esize are always valid. It's safe 1667c040c10cSSteven Hartland * to do this as we've already validated that asize is greater than 1668c040c10cSSteven Hartland * vdev_min_asize. 1669573ca77eSGeorge Wilson */ 1670c040c10cSSteven Hartland if (vd->vdev_state == VDEV_STATE_HEALTHY && 1671c040c10cSSteven Hartland ((asize > vd->vdev_asize && 1672c040c10cSSteven Hartland (vd->vdev_expanding || spa->spa_autoexpand)) || 1673c040c10cSSteven Hartland (asize < vd->vdev_asize))) 1674573ca77eSGeorge Wilson vd->vdev_asize = asize; 1675fa9e4066Sahrens 1676573ca77eSGeorge Wilson vdev_set_min_asize(vd); 1677fa9e4066Sahrens 16780a4e9518Sgw /* 16790a4e9518Sgw * Ensure we can issue some IO before declaring the 16800a4e9518Sgw * vdev open for business. 16810a4e9518Sgw */ 1682e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 1683e14bb325SJeff Bonwick (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 168498d1cbfeSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 168598d1cbfeSGeorge Wilson VDEV_AUX_ERR_EXCEEDED); 16860a4e9518Sgw return (error); 16870a4e9518Sgw } 16880a4e9518Sgw 168981cd5c55SMatthew Ahrens /* 169081cd5c55SMatthew Ahrens * Track the min and max ashift values for normal data devices. 1691*663207adSDon Brady * 1692*663207adSDon Brady * DJB - TBD these should perhaps be tracked per allocation class 1693*663207adSDon Brady * (e.g. spa_min_ashift is used to round up post compression buffers) 169481cd5c55SMatthew Ahrens */ 169581cd5c55SMatthew Ahrens if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1696*663207adSDon Brady vd->vdev_alloc_bias == VDEV_BIAS_NONE && 1697*663207adSDon Brady vd->vdev_aux == NULL) { 169881cd5c55SMatthew Ahrens if (vd->vdev_ashift > spa->spa_max_ashift) 169981cd5c55SMatthew Ahrens spa->spa_max_ashift = vd->vdev_ashift; 170081cd5c55SMatthew Ahrens if (vd->vdev_ashift < spa->spa_min_ashift) 170181cd5c55SMatthew Ahrens spa->spa_min_ashift = vd->vdev_ashift; 170281cd5c55SMatthew Ahrens } 170381cd5c55SMatthew Ahrens 1704088f3894Sahrens /* 1705088f3894Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 17068ad4d6ddSJeff Bonwick * resilver. But don't do this if we are doing a reopen for a scrub, 17078ad4d6ddSJeff Bonwick * since this would just restart the scrub we are already doing. 1708088f3894Sahrens */ 17098ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 17108ad4d6ddSJeff Bonwick vdev_resilver_needed(vd, NULL, NULL)) 17118ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1712088f3894Sahrens 1713fa9e4066Sahrens return (0); 1714fa9e4066Sahrens } 1715fa9e4066Sahrens 1716560e6e96Seschrock /* 1717560e6e96Seschrock * Called once the vdevs are all opened, this routine validates the label 17186f793812SPavel Zakharov * contents. This needs to be done before vdev_load() so that we don't 17193d7072f8Seschrock * inadvertently do repair I/Os to the wrong device. 1720560e6e96Seschrock * 1721560e6e96Seschrock * This function will only return failure if one of the vdevs indicates that it 1722560e6e96Seschrock * has since been destroyed or exported. This is only possible if 1723560e6e96Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1724560e6e96Seschrock * will be updated but the function will return 0. 1725560e6e96Seschrock */ 1726560e6e96Seschrock int 17276f793812SPavel Zakharov vdev_validate(vdev_t *vd) 1728560e6e96Seschrock { 1729560e6e96Seschrock spa_t *spa = vd->vdev_spa; 1730560e6e96Seschrock nvlist_t *label; 17316f793812SPavel Zakharov uint64_t guid = 0, aux_guid = 0, top_guid; 1732560e6e96Seschrock uint64_t state; 17336f793812SPavel Zakharov nvlist_t *nvl; 17346f793812SPavel Zakharov uint64_t txg; 1735560e6e96Seschrock 17366f793812SPavel Zakharov if (vdev_validate_skip) 17376f793812SPavel Zakharov return (0); 17386f793812SPavel Zakharov 17396f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 17406f793812SPavel Zakharov if (vdev_validate(vd->vdev_child[c]) != 0) 1741be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1742560e6e96Seschrock 1743b5989ec7Seschrock /* 1744b5989ec7Seschrock * If the device has already failed, or was marked offline, don't do 1745b5989ec7Seschrock * any further validation. Otherwise, label I/O will fail and we will 1746b5989ec7Seschrock * overwrite the previous state. 1747b5989ec7Seschrock */ 17486f793812SPavel Zakharov if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 17496f793812SPavel Zakharov return (0); 1750560e6e96Seschrock 17516f793812SPavel Zakharov /* 17526f793812SPavel Zakharov * If we are performing an extreme rewind, we allow for a label that 17536f793812SPavel Zakharov * was modified at a point after the current txg. 1754d1de72cfSPavel Zakharov * If config lock is not held do not check for the txg. spa_sync could 1755d1de72cfSPavel Zakharov * be updating the vdev's label before updating spa_last_synced_txg. 17566f793812SPavel Zakharov */ 1757d1de72cfSPavel Zakharov if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1758d1de72cfSPavel Zakharov spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 17596f793812SPavel Zakharov txg = UINT64_MAX; 17606f793812SPavel Zakharov else 17616f793812SPavel Zakharov txg = spa_last_synced_txg(spa); 1762560e6e96Seschrock 17636f793812SPavel Zakharov if ((label = vdev_label_read_config(vd, txg)) == NULL) { 17646f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 17656f793812SPavel Zakharov VDEV_AUX_BAD_LABEL); 1766b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1767b6bf6e15SPavel Zakharov "txg %llu", (u_longlong_t)txg); 17686f793812SPavel Zakharov return (0); 17696f793812SPavel Zakharov } 17701195e687SMark J Musante 17716f793812SPavel Zakharov /* 17726f793812SPavel Zakharov * Determine if this vdev has been split off into another 17736f793812SPavel Zakharov * pool. If so, then refuse to open it. 17746f793812SPavel Zakharov */ 17756f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 17766f793812SPavel Zakharov &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 17776f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17786f793812SPavel Zakharov VDEV_AUX_SPLIT_POOL); 17796f793812SPavel Zakharov nvlist_free(label); 17806f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 17816f793812SPavel Zakharov return (0); 17826f793812SPavel Zakharov } 1783560e6e96Seschrock 17846f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 17856f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17866f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 17876f793812SPavel Zakharov nvlist_free(label); 17886f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 17896f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 17906f793812SPavel Zakharov return (0); 17916f793812SPavel Zakharov } 17921195e687SMark J Musante 17936f793812SPavel Zakharov /* 17946f793812SPavel Zakharov * If config is not trusted then ignore the spa guid check. This is 17956f793812SPavel Zakharov * necessary because if the machine crashed during a re-guid the new 17966f793812SPavel Zakharov * guid might have been written to all of the vdev labels, but not the 17976f793812SPavel Zakharov * cached config. The check will be performed again once we have the 17986f793812SPavel Zakharov * trusted config from the MOS. 17996f793812SPavel Zakharov */ 18006f793812SPavel Zakharov if (spa->spa_trust_config && guid != spa_guid(spa)) { 18016f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18026f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18036f793812SPavel Zakharov nvlist_free(label); 18046f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 18056f793812SPavel Zakharov "match config (%llu != %llu)", (u_longlong_t)guid, 18066f793812SPavel Zakharov (u_longlong_t)spa_guid(spa)); 18076f793812SPavel Zakharov return (0); 18086f793812SPavel Zakharov } 18096f793812SPavel Zakharov 18106f793812SPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 18116f793812SPavel Zakharov != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 18126f793812SPavel Zakharov &aux_guid) != 0) 18136f793812SPavel Zakharov aux_guid = 0; 18146f793812SPavel Zakharov 18156f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 18166f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18176f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18186f793812SPavel Zakharov nvlist_free(label); 18196f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18206f793812SPavel Zakharov ZPOOL_CONFIG_GUID); 18216f793812SPavel Zakharov return (0); 18226f793812SPavel Zakharov } 18236f793812SPavel Zakharov 18246f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 18256f793812SPavel Zakharov != 0) { 18266f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18276f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18286f793812SPavel Zakharov nvlist_free(label); 18296f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18306f793812SPavel Zakharov ZPOOL_CONFIG_TOP_GUID); 18316f793812SPavel Zakharov return (0); 18326f793812SPavel Zakharov } 18336f793812SPavel Zakharov 18346f793812SPavel Zakharov /* 18356f793812SPavel Zakharov * If this vdev just became a top-level vdev because its sibling was 18366f793812SPavel Zakharov * detached, it will have adopted the parent's vdev guid -- but the 18376f793812SPavel Zakharov * label may or may not be on disk yet. Fortunately, either version 18386f793812SPavel Zakharov * of the label will have the same top guid, so if we're a top-level 18396f793812SPavel Zakharov * vdev, we can safely compare to that instead. 18406f793812SPavel Zakharov * However, if the config comes from a cachefile that failed to update 18416f793812SPavel Zakharov * after the detach, a top-level vdev will appear as a non top-level 18426f793812SPavel Zakharov * vdev in the config. Also relax the constraints if we perform an 18436f793812SPavel Zakharov * extreme rewind. 18446f793812SPavel Zakharov * 18456f793812SPavel Zakharov * If we split this vdev off instead, then we also check the 18466f793812SPavel Zakharov * original pool's guid. We don't want to consider the vdev 18476f793812SPavel Zakharov * corrupt if it is partway through a split operation. 18486f793812SPavel Zakharov */ 18496f793812SPavel Zakharov if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 18506f793812SPavel Zakharov boolean_t mismatch = B_FALSE; 18516f793812SPavel Zakharov if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 18526f793812SPavel Zakharov if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 18536f793812SPavel Zakharov mismatch = B_TRUE; 18546f793812SPavel Zakharov } else { 18556f793812SPavel Zakharov if (vd->vdev_guid != top_guid && 18566f793812SPavel Zakharov vd->vdev_top->vdev_guid != guid) 18576f793812SPavel Zakharov mismatch = B_TRUE; 1858560e6e96Seschrock } 1859560e6e96Seschrock 18606f793812SPavel Zakharov if (mismatch) { 1861560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1862560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1863560e6e96Seschrock nvlist_free(label); 18646f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: config guid " 18656f793812SPavel Zakharov "doesn't match label guid"); 18666f793812SPavel Zakharov vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 18676f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 18686f793812SPavel Zakharov (u_longlong_t)vd->vdev_top->vdev_guid); 18696f793812SPavel Zakharov vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 18706f793812SPavel Zakharov "aux_guid %llu", (u_longlong_t)guid, 18716f793812SPavel Zakharov (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1872560e6e96Seschrock return (0); 1873560e6e96Seschrock } 18746f793812SPavel Zakharov } 1875560e6e96Seschrock 18766f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 18776f793812SPavel Zakharov &state) != 0) { 18786f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18796f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 1880560e6e96Seschrock nvlist_free(label); 18816f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18826f793812SPavel Zakharov ZPOOL_CONFIG_POOL_STATE); 18836f793812SPavel Zakharov return (0); 18846f793812SPavel Zakharov } 1885560e6e96Seschrock 18866f793812SPavel Zakharov nvlist_free(label); 18876f793812SPavel Zakharov 18886f793812SPavel Zakharov /* 18896f793812SPavel Zakharov * If this is a verbatim import, no need to check the 18906f793812SPavel Zakharov * state of the pool. 18916f793812SPavel Zakharov */ 18926f793812SPavel Zakharov if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 18936f793812SPavel Zakharov spa_load_state(spa) == SPA_LOAD_OPEN && 18946f793812SPavel Zakharov state != POOL_STATE_ACTIVE) { 18956f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 18966f793812SPavel Zakharov "for spa %s", (u_longlong_t)state, spa->spa_name); 18976f793812SPavel Zakharov return (SET_ERROR(EBADF)); 18986f793812SPavel Zakharov } 18996f793812SPavel Zakharov 19006f793812SPavel Zakharov /* 19016f793812SPavel Zakharov * If we were able to open and validate a vdev that was 19026f793812SPavel Zakharov * previously marked permanently unavailable, clear that state 19036f793812SPavel Zakharov * now. 19046f793812SPavel Zakharov */ 19056f793812SPavel Zakharov if (vd->vdev_not_present) 19066f793812SPavel Zakharov vd->vdev_not_present = 0; 19076f793812SPavel Zakharov 19086f793812SPavel Zakharov return (0); 19096f793812SPavel Zakharov } 19106f793812SPavel Zakharov 19116f793812SPavel Zakharov static void 19126f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 19136f793812SPavel Zakharov { 19146f793812SPavel Zakharov if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 19156f793812SPavel Zakharov if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 19166f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 19176f793812SPavel Zakharov "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 19186f793812SPavel Zakharov dvd->vdev_path, svd->vdev_path); 19196f793812SPavel Zakharov spa_strfree(dvd->vdev_path); 19206f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 19213ee8c80cSPavel Zakharov } 19226f793812SPavel Zakharov } else if (svd->vdev_path != NULL) { 19236f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 19246f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 19256f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 19266f793812SPavel Zakharov } 19276f793812SPavel Zakharov } 1928560e6e96Seschrock 19296f793812SPavel Zakharov /* 19306f793812SPavel Zakharov * Recursively copy vdev paths from one vdev to another. Source and destination 19316f793812SPavel Zakharov * vdev trees must have same geometry otherwise return error. Intended to copy 19326f793812SPavel Zakharov * paths from userland config into MOS config. 19336f793812SPavel Zakharov */ 19346f793812SPavel Zakharov int 19356f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 19366f793812SPavel Zakharov { 19376f793812SPavel Zakharov if ((svd->vdev_ops == &vdev_missing_ops) || 19386f793812SPavel Zakharov (svd->vdev_ishole && dvd->vdev_ishole) || 19396f793812SPavel Zakharov (dvd->vdev_ops == &vdev_indirect_ops)) 19406f793812SPavel Zakharov return (0); 19416f793812SPavel Zakharov 19426f793812SPavel Zakharov if (svd->vdev_ops != dvd->vdev_ops) { 19436f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 19446f793812SPavel Zakharov svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 19456f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 19466f793812SPavel Zakharov } 19476f793812SPavel Zakharov 19486f793812SPavel Zakharov if (svd->vdev_guid != dvd->vdev_guid) { 19496f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 19506f793812SPavel Zakharov "%llu)", (u_longlong_t)svd->vdev_guid, 19516f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid); 19526f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 19536f793812SPavel Zakharov } 19546f793812SPavel Zakharov 19556f793812SPavel Zakharov if (svd->vdev_children != dvd->vdev_children) { 19566f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 19576f793812SPavel Zakharov "%llu != %llu", (u_longlong_t)svd->vdev_children, 19586f793812SPavel Zakharov (u_longlong_t)dvd->vdev_children); 19596f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 196051ece835Seschrock } 1961560e6e96Seschrock 19626f793812SPavel Zakharov for (uint64_t i = 0; i < svd->vdev_children; i++) { 19636f793812SPavel Zakharov int error = vdev_copy_path_strict(svd->vdev_child[i], 19646f793812SPavel Zakharov dvd->vdev_child[i]); 19656f793812SPavel Zakharov if (error != 0) 19666f793812SPavel Zakharov return (error); 19676f793812SPavel Zakharov } 19686f793812SPavel Zakharov 19696f793812SPavel Zakharov if (svd->vdev_ops->vdev_op_leaf) 19706f793812SPavel Zakharov vdev_copy_path_impl(svd, dvd); 19716f793812SPavel Zakharov 1972560e6e96Seschrock return (0); 1973560e6e96Seschrock } 1974560e6e96Seschrock 19756f793812SPavel Zakharov static void 19766f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 19776f793812SPavel Zakharov { 19786f793812SPavel Zakharov ASSERT(stvd->vdev_top == stvd); 19796f793812SPavel Zakharov ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 19806f793812SPavel Zakharov 19816f793812SPavel Zakharov for (uint64_t i = 0; i < dvd->vdev_children; i++) { 19826f793812SPavel Zakharov vdev_copy_path_search(stvd, dvd->vdev_child[i]); 19836f793812SPavel Zakharov } 19846f793812SPavel Zakharov 19856f793812SPavel Zakharov if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 19866f793812SPavel Zakharov return; 19876f793812SPavel Zakharov 19886f793812SPavel Zakharov /* 19896f793812SPavel Zakharov * The idea here is that while a vdev can shift positions within 19906f793812SPavel Zakharov * a top vdev (when replacing, attaching mirror, etc.) it cannot 19916f793812SPavel Zakharov * step outside of it. 19926f793812SPavel Zakharov */ 19936f793812SPavel Zakharov vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 19946f793812SPavel Zakharov 19956f793812SPavel Zakharov if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 19966f793812SPavel Zakharov return; 19976f793812SPavel Zakharov 19986f793812SPavel Zakharov ASSERT(vd->vdev_ops->vdev_op_leaf); 19996f793812SPavel Zakharov 20006f793812SPavel Zakharov vdev_copy_path_impl(vd, dvd); 20016f793812SPavel Zakharov } 20026f793812SPavel Zakharov 20036f793812SPavel Zakharov /* 20046f793812SPavel Zakharov * Recursively copy vdev paths from one root vdev to another. Source and 20056f793812SPavel Zakharov * destination vdev trees may differ in geometry. For each destination leaf 20066f793812SPavel Zakharov * vdev, search a vdev with the same guid and top vdev id in the source. 20076f793812SPavel Zakharov * Intended to copy paths from userland config into MOS config. 20086f793812SPavel Zakharov */ 20096f793812SPavel Zakharov void 20106f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 20116f793812SPavel Zakharov { 20126f793812SPavel Zakharov uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 20136f793812SPavel Zakharov ASSERT(srvd->vdev_ops == &vdev_root_ops); 20146f793812SPavel Zakharov ASSERT(drvd->vdev_ops == &vdev_root_ops); 20156f793812SPavel Zakharov 20166f793812SPavel Zakharov for (uint64_t i = 0; i < children; i++) { 20176f793812SPavel Zakharov vdev_copy_path_search(srvd->vdev_child[i], 20186f793812SPavel Zakharov drvd->vdev_child[i]); 20196f793812SPavel Zakharov } 20206f793812SPavel Zakharov } 20216f793812SPavel Zakharov 2022fa9e4066Sahrens /* 2023fa9e4066Sahrens * Close a virtual device. 2024fa9e4066Sahrens */ 2025fa9e4066Sahrens void 2026fa9e4066Sahrens vdev_close(vdev_t *vd) 2027fa9e4066Sahrens { 20288ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 2029095bcd66SGeorge Wilson vdev_t *pvd = vd->vdev_parent; 20308ad4d6ddSJeff Bonwick 20318ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 20328ad4d6ddSJeff Bonwick 20331195e687SMark J Musante /* 20341195e687SMark J Musante * If our parent is reopening, then we are as well, unless we are 20351195e687SMark J Musante * going offline. 20361195e687SMark J Musante */ 2037095bcd66SGeorge Wilson if (pvd != NULL && pvd->vdev_reopening) 20381195e687SMark J Musante vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 2039095bcd66SGeorge Wilson 2040fa9e4066Sahrens vd->vdev_ops->vdev_op_close(vd); 2041fa9e4066Sahrens 20423d7072f8Seschrock vdev_cache_purge(vd); 2043fa9e4066Sahrens 2044560e6e96Seschrock /* 2045573ca77eSGeorge Wilson * We record the previous state before we close it, so that if we are 2046560e6e96Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 2047560e6e96Seschrock * it's still faulted. 2048560e6e96Seschrock */ 2049560e6e96Seschrock vd->vdev_prevstate = vd->vdev_state; 2050560e6e96Seschrock 2051fa9e4066Sahrens if (vd->vdev_offline) 2052fa9e4066Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 2053fa9e4066Sahrens else 2054fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 2055ea8dc4b6Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2056fa9e4066Sahrens } 2057fa9e4066Sahrens 2058dcba9f3fSGeorge Wilson void 2059dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd) 2060dcba9f3fSGeorge Wilson { 2061dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 2062dcba9f3fSGeorge Wilson 2063dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 2064dcba9f3fSGeorge Wilson if (spa->spa_state == POOL_STATE_UNINITIALIZED) 2065dcba9f3fSGeorge Wilson return; 2066dcba9f3fSGeorge Wilson 2067dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 2068dcba9f3fSGeorge Wilson vdev_hold(vd->vdev_child[c]); 2069dcba9f3fSGeorge Wilson 2070dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2071dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_hold(vd); 2072dcba9f3fSGeorge Wilson } 2073dcba9f3fSGeorge Wilson 2074dcba9f3fSGeorge Wilson void 2075dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd) 2076dcba9f3fSGeorge Wilson { 2077dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 2078dcba9f3fSGeorge Wilson 2079dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 2080dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 2081dcba9f3fSGeorge Wilson vdev_rele(vd->vdev_child[c]); 2082dcba9f3fSGeorge Wilson 2083dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2084dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_rele(vd); 2085dcba9f3fSGeorge Wilson } 2086dcba9f3fSGeorge Wilson 2087095bcd66SGeorge Wilson /* 2088095bcd66SGeorge Wilson * Reopen all interior vdevs and any unopened leaves. We don't actually 2089095bcd66SGeorge Wilson * reopen leaf vdevs which had previously been opened as they might deadlock 2090095bcd66SGeorge Wilson * on the spa_config_lock. Instead we only obtain the leaf's physical size. 2091095bcd66SGeorge Wilson * If the leaf has never been opened then open it, as usual. 2092095bcd66SGeorge Wilson */ 2093fa9e4066Sahrens void 2094ea8dc4b6Seschrock vdev_reopen(vdev_t *vd) 2095fa9e4066Sahrens { 2096ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 2097fa9e4066Sahrens 2098e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2099ea8dc4b6Seschrock 21001195e687SMark J Musante /* set the reopening flag unless we're taking the vdev offline */ 21011195e687SMark J Musante vd->vdev_reopening = !vd->vdev_offline; 2102fa9e4066Sahrens vdev_close(vd); 2103fa9e4066Sahrens (void) vdev_open(vd); 2104fa9e4066Sahrens 210539c23413Seschrock /* 210639c23413Seschrock * Call vdev_validate() here to make sure we have the same device. 210739c23413Seschrock * Otherwise, a device with an invalid label could be successfully 210839c23413Seschrock * opened in response to vdev_reopen(). 210939c23413Seschrock */ 2110c5904d13Seschrock if (vd->vdev_aux) { 2111c5904d13Seschrock (void) vdev_validate_aux(vd); 2112e14bb325SJeff Bonwick if (vdev_readable(vd) && vdev_writeable(vd) && 21136809eb4eSEric Schrock vd->vdev_aux == &spa->spa_l2cache && 2114573ca77eSGeorge Wilson !l2arc_vdev_present(vd)) 2115573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 2116c5904d13Seschrock } else { 21176f793812SPavel Zakharov (void) vdev_validate(vd); 2118c5904d13Seschrock } 211939c23413Seschrock 2120fa9e4066Sahrens /* 21213d7072f8Seschrock * Reassess parent vdev's health. 2122fa9e4066Sahrens */ 21233d7072f8Seschrock vdev_propagate_state(vd); 2124fa9e4066Sahrens } 2125fa9e4066Sahrens 2126fa9e4066Sahrens int 212799653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2128fa9e4066Sahrens { 2129fa9e4066Sahrens int error; 2130fa9e4066Sahrens 2131fa9e4066Sahrens /* 2132fa9e4066Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 2133fa9e4066Sahrens * For a create, however, we want to fail the request if 2134fa9e4066Sahrens * there are any components we can't open. 2135fa9e4066Sahrens */ 2136fa9e4066Sahrens error = vdev_open(vd); 2137fa9e4066Sahrens 2138fa9e4066Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2139fa9e4066Sahrens vdev_close(vd); 2140fa9e4066Sahrens return (error ? error : ENXIO); 2141fa9e4066Sahrens } 2142fa9e4066Sahrens 2143fa9e4066Sahrens /* 21440713e232SGeorge Wilson * Recursively load DTLs and initialize all labels. 2145fa9e4066Sahrens */ 21460713e232SGeorge Wilson if ((error = vdev_dtl_load(vd)) != 0 || 21470713e232SGeorge Wilson (error = vdev_label_init(vd, txg, isreplacing ? 214839c23413Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2149fa9e4066Sahrens vdev_close(vd); 2150fa9e4066Sahrens return (error); 2151fa9e4066Sahrens } 2152fa9e4066Sahrens 2153fa9e4066Sahrens return (0); 2154fa9e4066Sahrens } 2155fa9e4066Sahrens 21560e34b6a7Sbonwick void 2157573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd) 2158fa9e4066Sahrens { 215986714001SSerapheim Dimitropoulos uint64_t asize = vd->vdev_asize; 2160a0b03b16SSerapheim Dimitropoulos uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2161b4bf0cf0SDon Brady uint64_t ms_shift; 216286714001SSerapheim Dimitropoulos 2163fa9e4066Sahrens /* 2164b4bf0cf0SDon Brady * There are two dimensions to the metaslab sizing calculation: 2165b4bf0cf0SDon Brady * the size of the metaslab and the count of metaslabs per vdev. 2166b4bf0cf0SDon Brady * 2167a0b03b16SSerapheim Dimitropoulos * The default values used below are a good balance between memory 2168a0b03b16SSerapheim Dimitropoulos * usage (larger metaslab size means more memory needed for loaded 2169a0b03b16SSerapheim Dimitropoulos * metaslabs; more metaslabs means more memory needed for the 2170a0b03b16SSerapheim Dimitropoulos * metaslab_t structs), metaslab load time (larger metaslabs take 2171a0b03b16SSerapheim Dimitropoulos * longer to load), and metaslab sync time (more metaslabs means 2172a0b03b16SSerapheim Dimitropoulos * more time spent syncing all of them). 2173a0b03b16SSerapheim Dimitropoulos * 2174a0b03b16SSerapheim Dimitropoulos * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2175a0b03b16SSerapheim Dimitropoulos * The range of the dimensions are as follows: 2176a0b03b16SSerapheim Dimitropoulos * 2177a0b03b16SSerapheim Dimitropoulos * 2^29 <= ms_size <= 2^34 2178b4bf0cf0SDon Brady * 16 <= ms_count <= 131,072 2179b4bf0cf0SDon Brady * 2180b4bf0cf0SDon Brady * On the lower end of vdev sizes, we aim for metaslabs sizes of 2181b4bf0cf0SDon Brady * at least 512MB (2^29) to minimize fragmentation effects when 2182b4bf0cf0SDon Brady * testing with smaller devices. However, the count constraint 2183b4bf0cf0SDon Brady * of at least 16 metaslabs will override this minimum size goal. 2184b4bf0cf0SDon Brady * 2185b4bf0cf0SDon Brady * On the upper end of vdev sizes, we aim for a maximum metaslab 2186a0b03b16SSerapheim Dimitropoulos * size of 16GB. However, we will cap the total count to 2^17 2187a0b03b16SSerapheim Dimitropoulos * metaslabs to keep our memory footprint in check and let the 2188a0b03b16SSerapheim Dimitropoulos * metaslab size grow from there if that limit is hit. 2189b4bf0cf0SDon Brady * 2190b4bf0cf0SDon Brady * The net effect of applying above constrains is summarized below. 2191b4bf0cf0SDon Brady * 2192*663207adSDon Brady * vdev size metaslab count 2193a0b03b16SSerapheim Dimitropoulos * --------------|----------------- 2194*663207adSDon Brady * < 8GB ~16 2195*663207adSDon Brady * 8GB - 100GB one per 512MB 2196*663207adSDon Brady * 100GB - 3TB ~200 2197*663207adSDon Brady * 3TB - 2PB one per 16GB 2198*663207adSDon Brady * > 2PB ~131,072 2199a0b03b16SSerapheim Dimitropoulos * -------------------------------- 2200a0b03b16SSerapheim Dimitropoulos * 2201a0b03b16SSerapheim Dimitropoulos * Finally, note that all of the above calculate the initial 2202a0b03b16SSerapheim Dimitropoulos * number of metaslabs. Expanding a top-level vdev will result 2203a0b03b16SSerapheim Dimitropoulos * in additional metaslabs being allocated making it possible 2204a0b03b16SSerapheim Dimitropoulos * to exceed the zfs_vdev_ms_count_limit. 2205a0b03b16SSerapheim Dimitropoulos */ 2206a0b03b16SSerapheim Dimitropoulos 2207a0b03b16SSerapheim Dimitropoulos if (ms_count < zfs_vdev_min_ms_count) 2208a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2209a0b03b16SSerapheim Dimitropoulos else if (ms_count > zfs_vdev_default_ms_count) 2210a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2211b4bf0cf0SDon Brady else 2212a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_default_ms_shift; 2213b4bf0cf0SDon Brady 2214b4bf0cf0SDon Brady if (ms_shift < SPA_MAXBLOCKSHIFT) { 2215b4bf0cf0SDon Brady ms_shift = SPA_MAXBLOCKSHIFT; 2216a0b03b16SSerapheim Dimitropoulos } else if (ms_shift > zfs_vdev_max_ms_shift) { 2217a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_max_ms_shift; 2218b4bf0cf0SDon Brady /* cap the total count to constrain memory footprint */ 2219a0b03b16SSerapheim Dimitropoulos if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2220a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 222186714001SSerapheim Dimitropoulos } 222286714001SSerapheim Dimitropoulos 222386714001SSerapheim Dimitropoulos vd->vdev_ms_shift = ms_shift; 222486714001SSerapheim Dimitropoulos ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2225fa9e4066Sahrens } 2226fa9e4066Sahrens 2227fa9e4066Sahrens void 2228ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2229fa9e4066Sahrens { 2230ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 22315cabbc6bSPrashanth Sreenivasa /* indirect vdevs don't have metaslabs or dtls */ 22325cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd) || flags == 0); 2233ecc2d604Sbonwick ASSERT(ISP2(flags)); 2234f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 2235fa9e4066Sahrens 2236ecc2d604Sbonwick if (flags & VDD_METASLAB) 2237ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2238ecc2d604Sbonwick 2239ecc2d604Sbonwick if (flags & VDD_DTL) 2240ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2241ecc2d604Sbonwick 2242ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2243fa9e4066Sahrens } 2244fa9e4066Sahrens 22450713e232SGeorge Wilson void 22460713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 22470713e232SGeorge Wilson { 22480713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 22490713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 22500713e232SGeorge Wilson 22510713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 22520713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 22530713e232SGeorge Wilson } 22540713e232SGeorge Wilson 22558ad4d6ddSJeff Bonwick /* 22568ad4d6ddSJeff Bonwick * DTLs. 22578ad4d6ddSJeff Bonwick * 22588ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 22599fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 22608ad4d6ddSJeff Bonwick * 22618ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 22628ad4d6ddSJeff Bonwick * 22638ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 22648ad4d6ddSJeff Bonwick * 22658ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 22668ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 22678ad4d6ddSJeff Bonwick * txgs that was scrubbed. 22688ad4d6ddSJeff Bonwick * 22698ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 22708ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 22718ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 22728ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 22738ad4d6ddSJeff Bonwick * determine whether a device can be detached. 22748ad4d6ddSJeff Bonwick * 22758ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 22768ad4d6ddSJeff Bonwick * either has the data or it doesn't. 22778ad4d6ddSJeff Bonwick * 22788ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 22798ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 22808ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 22818ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 22828ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 22838ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 22848ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 22858ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 22868ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 22878ad4d6ddSJeff Bonwick * 22888ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 22898ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 22908ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 22918ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 22928ad4d6ddSJeff Bonwick */ 2293fa9e4066Sahrens void 22948ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2295fa9e4066Sahrens { 22960713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 22978ad4d6ddSJeff Bonwick 22988ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 22998ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2300f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 23018ad4d6ddSJeff Bonwick 23025cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 23030713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 23040713e232SGeorge Wilson range_tree_add(rt, txg, size); 23055cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2306fa9e4066Sahrens } 2307fa9e4066Sahrens 23088ad4d6ddSJeff Bonwick boolean_t 23098ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2310fa9e4066Sahrens { 23110713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23128ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 2313fa9e4066Sahrens 23148ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 23158ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2316fa9e4066Sahrens 23175cabbc6bSPrashanth Sreenivasa /* 23185cabbc6bSPrashanth Sreenivasa * While we are loading the pool, the DTLs have not been loaded yet. 23195cabbc6bSPrashanth Sreenivasa * Ignore the DTLs and try all devices. This avoids a recursive 23205cabbc6bSPrashanth Sreenivasa * mutex enter on the vdev_dtl_lock, and also makes us try hard 23215cabbc6bSPrashanth Sreenivasa * when loading the pool (relying on the checksum to ensure that 23225cabbc6bSPrashanth Sreenivasa * we get the right data -- note that we while loading, we are 23235cabbc6bSPrashanth Sreenivasa * only reading the MOS, which is always checksummed). 23245cabbc6bSPrashanth Sreenivasa */ 23255cabbc6bSPrashanth Sreenivasa if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 23265cabbc6bSPrashanth Sreenivasa return (B_FALSE); 23275cabbc6bSPrashanth Sreenivasa 23285cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 232986714001SSerapheim Dimitropoulos if (!range_tree_is_empty(rt)) 23300713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 23315cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2332fa9e4066Sahrens 2333fa9e4066Sahrens return (dirty); 2334fa9e4066Sahrens } 2335fa9e4066Sahrens 23368ad4d6ddSJeff Bonwick boolean_t 23378ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 23388ad4d6ddSJeff Bonwick { 23390713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23408ad4d6ddSJeff Bonwick boolean_t empty; 23418ad4d6ddSJeff Bonwick 23425cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 234386714001SSerapheim Dimitropoulos empty = range_tree_is_empty(rt); 23445cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 23458ad4d6ddSJeff Bonwick 23468ad4d6ddSJeff Bonwick return (empty); 23478ad4d6ddSJeff Bonwick } 23488ad4d6ddSJeff Bonwick 2349b4952e17SGeorge Wilson /* 2350b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 2351b4952e17SGeorge Wilson */ 2352b4952e17SGeorge Wilson static uint64_t 2353b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 2354b4952e17SGeorge Wilson { 23550713e232SGeorge Wilson range_seg_t *rs; 2356b4952e17SGeorge Wilson 2357b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 23580713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2359b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2360b4952e17SGeorge Wilson 23610713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 23620713e232SGeorge Wilson return (rs->rs_start - 1); 2363b4952e17SGeorge Wilson } 2364b4952e17SGeorge Wilson 2365b4952e17SGeorge Wilson /* 2366b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 2367b4952e17SGeorge Wilson */ 2368b4952e17SGeorge Wilson static uint64_t 2369b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 2370b4952e17SGeorge Wilson { 23710713e232SGeorge Wilson range_seg_t *rs; 2372b4952e17SGeorge Wilson 2373b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 23740713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2375b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2376b4952e17SGeorge Wilson 23770713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 23780713e232SGeorge Wilson return (rs->rs_end); 2379b4952e17SGeorge Wilson } 2380b4952e17SGeorge Wilson 2381b4952e17SGeorge Wilson /* 2382b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 2383b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 2384b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 2385b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 2386b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 2387b4952e17SGeorge Wilson * excise the DTLs. 2388b4952e17SGeorge Wilson */ 2389b4952e17SGeorge Wilson static boolean_t 2390b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 2391b4952e17SGeorge Wilson { 2392b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 2393b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2394b4952e17SGeorge Wilson 2395b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 2396b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2397b4952e17SGeorge Wilson 23982d2f193aSMatthew Ahrens if (vd->vdev_state < VDEV_STATE_DEGRADED) 23992d2f193aSMatthew Ahrens return (B_FALSE); 24002d2f193aSMatthew Ahrens 2401b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 240286714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2403b4952e17SGeorge Wilson return (B_TRUE); 2404b4952e17SGeorge Wilson 2405b4952e17SGeorge Wilson /* 2406b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 2407b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 2408b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 2409b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 2410b4952e17SGeorge Wilson * for excision. 2411b4952e17SGeorge Wilson */ 2412b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2413b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2414b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2415b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2416b4952e17SGeorge Wilson return (B_TRUE); 2417b4952e17SGeorge Wilson } 2418b4952e17SGeorge Wilson return (B_FALSE); 2419b4952e17SGeorge Wilson } 2420b4952e17SGeorge Wilson 2421fa9e4066Sahrens /* 2422fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 2423fa9e4066Sahrens */ 2424fa9e4066Sahrens void 2425fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2426fa9e4066Sahrens { 2427ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 24288ad4d6ddSJeff Bonwick avl_tree_t reftree; 24298ad4d6ddSJeff Bonwick int minref; 2430fa9e4066Sahrens 24318ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2432fa9e4066Sahrens 24338ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 24348ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 24358ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 24368ad4d6ddSJeff Bonwick 24375cabbc6bSPrashanth Sreenivasa if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 24388ad4d6ddSJeff Bonwick return; 24398ad4d6ddSJeff Bonwick 24408ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 24413f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 24423f9d6ad7SLin Ling 2443fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 2444b4952e17SGeorge Wilson 2445b4952e17SGeorge Wilson /* 2446b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 2447b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 2448b4952e17SGeorge Wilson * excise regions on vdevs that were available during 2449b4952e17SGeorge Wilson * the entire duration of this scan. 2450b4952e17SGeorge Wilson */ 2451088f3894Sahrens if (scrub_txg != 0 && 24523f9d6ad7SLin Ling (spa->spa_scrub_started || 2453b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2454b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 2455088f3894Sahrens /* 2456088f3894Sahrens * We completed a scrub up to scrub_txg. If we 2457088f3894Sahrens * did it without rebooting, then the scrub dtl 2458088f3894Sahrens * will be valid, so excise the old region and 2459088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 2460088f3894Sahrens * dtl as-is if there was an error. 24618ad4d6ddSJeff Bonwick * 24628ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 24638ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 24648ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 24658ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 24668ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 24678ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 24688ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 24698ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 24708ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 2471088f3894Sahrens */ 24720713e232SGeorge Wilson space_reftree_create(&reftree); 24730713e232SGeorge Wilson space_reftree_add_map(&reftree, 24740713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 24750713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 24760713e232SGeorge Wilson space_reftree_add_map(&reftree, 24770713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 24780713e232SGeorge Wilson space_reftree_generate_map(&reftree, 24790713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 24800713e232SGeorge Wilson space_reftree_destroy(&reftree); 2481fa9e4066Sahrens } 24820713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 24830713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 24840713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2485fa9e4066Sahrens if (scrub_done) 24860713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 24870713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 24888ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 24890713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 24908ad4d6ddSJeff Bonwick else 24910713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 24920713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2493b4952e17SGeorge Wilson 2494b4952e17SGeorge Wilson /* 2495b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 2496b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 2497b4952e17SGeorge Wilson */ 2498b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 249986714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 250086714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2501b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 2502b4952e17SGeorge Wilson 2503fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2504088f3894Sahrens 2505ecc2d604Sbonwick if (txg != 0) 2506ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2507fa9e4066Sahrens return; 2508fa9e4066Sahrens } 2509fa9e4066Sahrens 2510fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 25118ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 251299bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 251399bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 25148ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 25158ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 25168ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 25178ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 25188ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 25198ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 25208ad4d6ddSJeff Bonwick else 25218ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 25220713e232SGeorge Wilson space_reftree_create(&reftree); 25238ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 25248ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 25258ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 25260713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 25278ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 25288ad4d6ddSJeff Bonwick } 25290713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 25300713e232SGeorge Wilson space_reftree_destroy(&reftree); 2531fa9e4066Sahrens } 25328ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 2533fa9e4066Sahrens } 2534fa9e4066Sahrens 25350713e232SGeorge Wilson int 2536fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 2537fa9e4066Sahrens { 2538fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2539ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 25400713e232SGeorge Wilson int error = 0; 2541fa9e4066Sahrens 25420713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 25435cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 2544fa9e4066Sahrens 25450713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 25465cabbc6bSPrashanth Sreenivasa vd->vdev_dtl_object, 0, -1ULL, 0); 25470713e232SGeorge Wilson if (error) 25480713e232SGeorge Wilson return (error); 25490713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2550fa9e4066Sahrens 25510713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 255288ecc943SGeorge Wilson 25530713e232SGeorge Wilson /* 25540713e232SGeorge Wilson * Now that we've opened the space_map we need to update 25550713e232SGeorge Wilson * the in-core DTL. 25560713e232SGeorge Wilson */ 25570713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 2558ecc2d604Sbonwick 25590713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 25600713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 25610713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2562fa9e4066Sahrens 25630713e232SGeorge Wilson return (error); 25640713e232SGeorge Wilson } 25650713e232SGeorge Wilson 25660713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 25670713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 25680713e232SGeorge Wilson if (error != 0) 25690713e232SGeorge Wilson break; 25700713e232SGeorge Wilson } 2571fa9e4066Sahrens 2572fa9e4066Sahrens return (error); 2573fa9e4066Sahrens } 2574fa9e4066Sahrens 2575*663207adSDon Brady static void 2576*663207adSDon Brady vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 2577*663207adSDon Brady { 2578*663207adSDon Brady spa_t *spa = vd->vdev_spa; 2579*663207adSDon Brady objset_t *mos = spa->spa_meta_objset; 2580*663207adSDon Brady vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 2581*663207adSDon Brady const char *string; 2582*663207adSDon Brady 2583*663207adSDon Brady ASSERT(alloc_bias != VDEV_BIAS_NONE); 2584*663207adSDon Brady 2585*663207adSDon Brady string = 2586*663207adSDon Brady (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 2587*663207adSDon Brady (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 2588*663207adSDon Brady (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 2589*663207adSDon Brady 2590*663207adSDon Brady ASSERT(string != NULL); 2591*663207adSDon Brady VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 2592*663207adSDon Brady 1, strlen(string) + 1, string, tx)); 2593*663207adSDon Brady 2594*663207adSDon Brady if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 2595*663207adSDon Brady spa_activate_allocation_classes(spa, tx); 2596*663207adSDon Brady } 2597*663207adSDon Brady } 2598*663207adSDon Brady 2599215198a6SJoe Stein void 2600215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2601215198a6SJoe Stein { 2602215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2603215198a6SJoe Stein 2604215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2605215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2606215198a6SJoe Stein zapobj, tx)); 2607215198a6SJoe Stein } 2608215198a6SJoe Stein 2609215198a6SJoe Stein uint64_t 2610215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2611215198a6SJoe Stein { 2612215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2613215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2614215198a6SJoe Stein DMU_OT_NONE, 0, tx); 2615215198a6SJoe Stein 2616215198a6SJoe Stein ASSERT(zap != 0); 2617215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2618215198a6SJoe Stein zap, tx)); 2619215198a6SJoe Stein 2620215198a6SJoe Stein return (zap); 2621215198a6SJoe Stein } 2622215198a6SJoe Stein 2623215198a6SJoe Stein void 2624215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2625215198a6SJoe Stein { 2626215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 2627215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 2628215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 2629215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 2630215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2631215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2632215198a6SJoe Stein } 2633215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2634215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2635*663207adSDon Brady if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 2636*663207adSDon Brady vdev_zap_allocation_data(vd, tx); 2637215198a6SJoe Stein } 2638215198a6SJoe Stein } 2639*663207adSDon Brady 2640215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2641215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2642215198a6SJoe Stein } 2643215198a6SJoe Stein } 2644215198a6SJoe Stein 2645fa9e4066Sahrens void 2646fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2647fa9e4066Sahrens { 2648fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 26490713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2650ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 26510713e232SGeorge Wilson range_tree_t *rtsync; 2652fa9e4066Sahrens dmu_tx_t *tx; 26530713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2654fa9e4066Sahrens 26555cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 26560713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 265788ecc943SGeorge Wilson 2658fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2659fa9e4066Sahrens 26600713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 26610713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 26620713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 26630713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 26640713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 26650713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2666215198a6SJoe Stein 2667215198a6SJoe Stein /* 2668215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2669215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2670215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2671215198a6SJoe Stein */ 2672215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2673215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2674215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2675215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2676215198a6SJoe Stein } 2677215198a6SJoe Stein 2678fa9e4066Sahrens dmu_tx_commit(tx); 2679fa9e4066Sahrens return; 2680fa9e4066Sahrens } 2681fa9e4066Sahrens 26820713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 26830713e232SGeorge Wilson uint64_t new_object; 26840713e232SGeorge Wilson 268586714001SSerapheim Dimitropoulos new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 26860713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 26870713e232SGeorge Wilson 26880713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 26895cabbc6bSPrashanth Sreenivasa 0, -1ULL, 0)); 26900713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2691fa9e4066Sahrens } 2692fa9e4066Sahrens 26935cabbc6bSPrashanth Sreenivasa rtsync = range_tree_create(NULL, NULL); 2694fa9e4066Sahrens 2695fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 26960713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2697fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2698fa9e4066Sahrens 269986714001SSerapheim Dimitropoulos space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 270017f11284SSerapheim Dimitropoulos space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 27010713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2702fa9e4066Sahrens 27030713e232SGeorge Wilson range_tree_destroy(rtsync); 2704fa9e4066Sahrens 27050713e232SGeorge Wilson /* 27060713e232SGeorge Wilson * If the object for the space map has changed then dirty 27070713e232SGeorge Wilson * the top level so that we update the config. 27080713e232SGeorge Wilson */ 27090713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 27103ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 27113ee8c80cSPavel Zakharov "new object %llu", (u_longlong_t)txg, spa_name(spa), 27123ee8c80cSPavel Zakharov (u_longlong_t)object, 27133ee8c80cSPavel Zakharov (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 27140713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 27150713e232SGeorge Wilson } 2716fa9e4066Sahrens 2717fa9e4066Sahrens dmu_tx_commit(tx); 27180713e232SGeorge Wilson 27190713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 27200713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 27210713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2722fa9e4066Sahrens } 2723fa9e4066Sahrens 27248ad4d6ddSJeff Bonwick /* 27258ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 27268ad4d6ddSJeff Bonwick * without losing data. 27278ad4d6ddSJeff Bonwick */ 27288ad4d6ddSJeff Bonwick boolean_t 27298ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 27308ad4d6ddSJeff Bonwick { 27318ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 27328ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 27338ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 27348ad4d6ddSJeff Bonwick boolean_t required; 27358ad4d6ddSJeff Bonwick 27368ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 27378ad4d6ddSJeff Bonwick 27388ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 27398ad4d6ddSJeff Bonwick return (B_TRUE); 27408ad4d6ddSJeff Bonwick 27418ad4d6ddSJeff Bonwick /* 27428ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 27438ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 27448ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 27458ad4d6ddSJeff Bonwick */ 27468ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 27478ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 27488ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 27498ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 27508ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 27518ad4d6ddSJeff Bonwick 2752cb04b873SMark J Musante if (!required && zio_injection_enabled) 2753cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2754cb04b873SMark J Musante 27558ad4d6ddSJeff Bonwick return (required); 27568ad4d6ddSJeff Bonwick } 27578ad4d6ddSJeff Bonwick 2758088f3894Sahrens /* 2759088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2760088f3894Sahrens */ 2761088f3894Sahrens boolean_t 2762088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2763088f3894Sahrens { 2764088f3894Sahrens boolean_t needed = B_FALSE; 2765088f3894Sahrens uint64_t thismin = UINT64_MAX; 2766088f3894Sahrens uint64_t thismax = 0; 2767088f3894Sahrens 2768088f3894Sahrens if (vd->vdev_children == 0) { 2769088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 277086714001SSerapheim Dimitropoulos if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 27718ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2772088f3894Sahrens 2773b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2774b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2775088f3894Sahrens needed = B_TRUE; 2776088f3894Sahrens } 2777088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2778088f3894Sahrens } else { 27798ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2780088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2781088f3894Sahrens uint64_t cmin, cmax; 2782088f3894Sahrens 2783088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2784088f3894Sahrens thismin = MIN(thismin, cmin); 2785088f3894Sahrens thismax = MAX(thismax, cmax); 2786088f3894Sahrens needed = B_TRUE; 2787088f3894Sahrens } 2788088f3894Sahrens } 2789088f3894Sahrens } 2790088f3894Sahrens 2791088f3894Sahrens if (needed && minp) { 2792088f3894Sahrens *minp = thismin; 2793088f3894Sahrens *maxp = thismax; 2794088f3894Sahrens } 2795088f3894Sahrens return (needed); 2796088f3894Sahrens } 2797088f3894Sahrens 279886714001SSerapheim Dimitropoulos /* 279986714001SSerapheim Dimitropoulos * Gets the checkpoint space map object from the vdev's ZAP. 280086714001SSerapheim Dimitropoulos * Returns the spacemap object, or 0 if it wasn't in the ZAP 280186714001SSerapheim Dimitropoulos * or the ZAP doesn't exist yet. 280286714001SSerapheim Dimitropoulos */ 280386714001SSerapheim Dimitropoulos int 280486714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd) 280586714001SSerapheim Dimitropoulos { 280686714001SSerapheim Dimitropoulos ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 280786714001SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) { 280886714001SSerapheim Dimitropoulos return (0); 280986714001SSerapheim Dimitropoulos } 281086714001SSerapheim Dimitropoulos 281186714001SSerapheim Dimitropoulos uint64_t sm_obj = 0; 281286714001SSerapheim Dimitropoulos int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 281386714001SSerapheim Dimitropoulos VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 281486714001SSerapheim Dimitropoulos 281586714001SSerapheim Dimitropoulos ASSERT(err == 0 || err == ENOENT); 281686714001SSerapheim Dimitropoulos 281786714001SSerapheim Dimitropoulos return (sm_obj); 281886714001SSerapheim Dimitropoulos } 281986714001SSerapheim Dimitropoulos 28205cabbc6bSPrashanth Sreenivasa int 2821ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2822fa9e4066Sahrens { 28235cabbc6bSPrashanth Sreenivasa int error = 0; 2824fa9e4066Sahrens /* 2825fa9e4066Sahrens * Recursively load all children. 2826fa9e4066Sahrens */ 28275cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 28285cabbc6bSPrashanth Sreenivasa error = vdev_load(vd->vdev_child[c]); 28295cabbc6bSPrashanth Sreenivasa if (error != 0) { 28305cabbc6bSPrashanth Sreenivasa return (error); 28315cabbc6bSPrashanth Sreenivasa } 28325cabbc6bSPrashanth Sreenivasa } 28335cabbc6bSPrashanth Sreenivasa 28345cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 2835fa9e4066Sahrens 2836*663207adSDon Brady /* 2837*663207adSDon Brady * On spa_load path, grab the allocation bias from our zap 2838*663207adSDon Brady */ 2839*663207adSDon Brady if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 2840*663207adSDon Brady spa_t *spa = vd->vdev_spa; 2841*663207adSDon Brady char bias_str[64]; 2842*663207adSDon Brady 2843*663207adSDon Brady if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 2844*663207adSDon Brady VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 2845*663207adSDon Brady bias_str) == 0) { 2846*663207adSDon Brady ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 2847*663207adSDon Brady vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 2848*663207adSDon Brady } 2849*663207adSDon Brady } 2850*663207adSDon Brady 2851fa9e4066Sahrens /* 28520e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2853fa9e4066Sahrens */ 28545cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2855*663207adSDon Brady vdev_metaslab_group_create(vd); 2856*663207adSDon Brady 28575cabbc6bSPrashanth Sreenivasa if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 28585cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 28595cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 28603ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 28613ee8c80cSPavel Zakharov "asize=%llu", (u_longlong_t)vd->vdev_ashift, 28623ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_asize); 28635cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 28645cabbc6bSPrashanth Sreenivasa } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 28653ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 28663ee8c80cSPavel Zakharov "[error=%d]", error); 28675cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 28685cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 28695cabbc6bSPrashanth Sreenivasa return (error); 28705cabbc6bSPrashanth Sreenivasa } 287186714001SSerapheim Dimitropoulos 287286714001SSerapheim Dimitropoulos uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 287386714001SSerapheim Dimitropoulos if (checkpoint_sm_obj != 0) { 287486714001SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 287586714001SSerapheim Dimitropoulos ASSERT(vd->vdev_asize != 0); 287686714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 287786714001SSerapheim Dimitropoulos 287886714001SSerapheim Dimitropoulos if ((error = space_map_open(&vd->vdev_checkpoint_sm, 287986714001SSerapheim Dimitropoulos mos, checkpoint_sm_obj, 0, vd->vdev_asize, 288086714001SSerapheim Dimitropoulos vd->vdev_ashift))) { 288186714001SSerapheim Dimitropoulos vdev_dbgmsg(vd, "vdev_load: space_map_open " 288286714001SSerapheim Dimitropoulos "failed for checkpoint spacemap (obj %llu) " 288386714001SSerapheim Dimitropoulos "[error=%d]", 288486714001SSerapheim Dimitropoulos (u_longlong_t)checkpoint_sm_obj, error); 288586714001SSerapheim Dimitropoulos return (error); 288686714001SSerapheim Dimitropoulos } 288786714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 288886714001SSerapheim Dimitropoulos space_map_update(vd->vdev_checkpoint_sm); 288986714001SSerapheim Dimitropoulos 289086714001SSerapheim Dimitropoulos /* 289186714001SSerapheim Dimitropoulos * Since the checkpoint_sm contains free entries 289286714001SSerapheim Dimitropoulos * exclusively we can use sm_alloc to indicate the 289386714001SSerapheim Dimitropoulos * culmulative checkpointed space that has been freed. 289486714001SSerapheim Dimitropoulos */ 289586714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space = 289686714001SSerapheim Dimitropoulos -vd->vdev_checkpoint_sm->sm_alloc; 289786714001SSerapheim Dimitropoulos vd->vdev_spa->spa_checkpoint_info.sci_dspace += 289886714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space; 289986714001SSerapheim Dimitropoulos } 29005cabbc6bSPrashanth Sreenivasa } 2901fa9e4066Sahrens 2902fa9e4066Sahrens /* 2903fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2904fa9e4066Sahrens */ 29055cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2906560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2907560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 29083ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 29093ee8c80cSPavel Zakharov "[error=%d]", error); 29105cabbc6bSPrashanth Sreenivasa return (error); 29115cabbc6bSPrashanth Sreenivasa } 29125cabbc6bSPrashanth Sreenivasa 29135cabbc6bSPrashanth Sreenivasa uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 29145cabbc6bSPrashanth Sreenivasa if (obsolete_sm_object != 0) { 29155cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 29165cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_asize != 0); 291786714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 29185cabbc6bSPrashanth Sreenivasa 29195cabbc6bSPrashanth Sreenivasa if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 29205cabbc6bSPrashanth Sreenivasa obsolete_sm_object, 0, vd->vdev_asize, 0))) { 29215cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 29225cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 29233ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 29243ee8c80cSPavel Zakharov "obsolete spacemap (obj %llu) [error=%d]", 29253ee8c80cSPavel Zakharov (u_longlong_t)obsolete_sm_object, error); 29265cabbc6bSPrashanth Sreenivasa return (error); 29275cabbc6bSPrashanth Sreenivasa } 29285cabbc6bSPrashanth Sreenivasa space_map_update(vd->vdev_obsolete_sm); 29295cabbc6bSPrashanth Sreenivasa } 29305cabbc6bSPrashanth Sreenivasa 29315cabbc6bSPrashanth Sreenivasa return (0); 2932fa9e4066Sahrens } 2933fa9e4066Sahrens 293499653d4eSeschrock /* 2935fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 2936fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 2937fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 2938fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 2939fa94a07fSbrendan * repurposed to another pool. 294099653d4eSeschrock */ 294199653d4eSeschrock int 2942fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 294399653d4eSeschrock { 294499653d4eSeschrock nvlist_t *label; 294599653d4eSeschrock uint64_t guid, version; 294699653d4eSeschrock uint64_t state; 294799653d4eSeschrock 2948e14bb325SJeff Bonwick if (!vdev_readable(vd)) 2949c5904d13Seschrock return (0); 2950c5904d13Seschrock 2951dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 295299653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 295399653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 295499653d4eSeschrock return (-1); 295599653d4eSeschrock } 295699653d4eSeschrock 295799653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2958ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 295999653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 296099653d4eSeschrock guid != vd->vdev_guid || 296199653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 296299653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 296399653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 296499653d4eSeschrock nvlist_free(label); 296599653d4eSeschrock return (-1); 296699653d4eSeschrock } 296799653d4eSeschrock 296899653d4eSeschrock /* 296999653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 297099653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 297199653d4eSeschrock */ 297299653d4eSeschrock nvlist_free(label); 297399653d4eSeschrock return (0); 297499653d4eSeschrock } 297599653d4eSeschrock 29765cabbc6bSPrashanth Sreenivasa /* 29775cabbc6bSPrashanth Sreenivasa * Free the objects used to store this vdev's spacemaps, and the array 29785cabbc6bSPrashanth Sreenivasa * that points to them. 29795cabbc6bSPrashanth Sreenivasa */ 298088ecc943SGeorge Wilson void 29815cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 29825cabbc6bSPrashanth Sreenivasa { 29835cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0) 29845cabbc6bSPrashanth Sreenivasa return; 29855cabbc6bSPrashanth Sreenivasa 29865cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 29875cabbc6bSPrashanth Sreenivasa uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 29885cabbc6bSPrashanth Sreenivasa size_t array_bytes = array_count * sizeof (uint64_t); 29895cabbc6bSPrashanth Sreenivasa uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 29905cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 29915cabbc6bSPrashanth Sreenivasa array_bytes, smobj_array, 0)); 29925cabbc6bSPrashanth Sreenivasa 29935cabbc6bSPrashanth Sreenivasa for (uint64_t i = 0; i < array_count; i++) { 29945cabbc6bSPrashanth Sreenivasa uint64_t smobj = smobj_array[i]; 29955cabbc6bSPrashanth Sreenivasa if (smobj == 0) 29965cabbc6bSPrashanth Sreenivasa continue; 29975cabbc6bSPrashanth Sreenivasa 29985cabbc6bSPrashanth Sreenivasa space_map_free_obj(mos, smobj, tx); 29995cabbc6bSPrashanth Sreenivasa } 30005cabbc6bSPrashanth Sreenivasa 30015cabbc6bSPrashanth Sreenivasa kmem_free(smobj_array, array_bytes); 30025cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 30035cabbc6bSPrashanth Sreenivasa vd->vdev_ms_array = 0; 30045cabbc6bSPrashanth Sreenivasa } 30055cabbc6bSPrashanth Sreenivasa 30065cabbc6bSPrashanth Sreenivasa static void 30074e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 300888ecc943SGeorge Wilson { 300988ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 301088ecc943SGeorge Wilson 30114e75ba68SSerapheim Dimitropoulos ASSERT(vd->vdev_islog); 3012215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 3013215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 301488ecc943SGeorge Wilson 301588ecc943SGeorge Wilson if (vd->vdev_ms != NULL) { 30162e4c9986SGeorge Wilson metaslab_group_t *mg = vd->vdev_mg; 30172e4c9986SGeorge Wilson 30182e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 30192e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 30202e4c9986SGeorge Wilson 302188ecc943SGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) { 302288ecc943SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 302388ecc943SGeorge Wilson 30240713e232SGeorge Wilson if (msp == NULL || msp->ms_sm == NULL) 302588ecc943SGeorge Wilson continue; 302688ecc943SGeorge Wilson 30270713e232SGeorge Wilson mutex_enter(&msp->ms_lock); 30282e4c9986SGeorge Wilson /* 30292e4c9986SGeorge Wilson * If the metaslab was not loaded when the vdev 30302e4c9986SGeorge Wilson * was removed then the histogram accounting may 30312e4c9986SGeorge Wilson * not be accurate. Update the histogram information 30322e4c9986SGeorge Wilson * here so that we ensure that the metaslab group 30332e4c9986SGeorge Wilson * and metaslab class are up-to-date. 30342e4c9986SGeorge Wilson */ 30352e4c9986SGeorge Wilson metaslab_group_histogram_remove(mg, msp); 30362e4c9986SGeorge Wilson 30370713e232SGeorge Wilson VERIFY0(space_map_allocated(msp->ms_sm)); 30380713e232SGeorge Wilson space_map_close(msp->ms_sm); 30390713e232SGeorge Wilson msp->ms_sm = NULL; 30400713e232SGeorge Wilson mutex_exit(&msp->ms_lock); 304188ecc943SGeorge Wilson } 30422e4c9986SGeorge Wilson 304386714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 304486714001SSerapheim Dimitropoulos ASSERT(spa_has_checkpoint(spa)); 304586714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 304686714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 304786714001SSerapheim Dimitropoulos } 304886714001SSerapheim Dimitropoulos 30492e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 30502e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 3051*663207adSDon Brady 30522e4c9986SGeorge Wilson for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 30532e4c9986SGeorge Wilson ASSERT0(mg->mg_histogram[i]); 305488ecc943SGeorge Wilson } 305588ecc943SGeorge Wilson 30564e75ba68SSerapheim Dimitropoulos dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3057215198a6SJoe Stein 30584e75ba68SSerapheim Dimitropoulos vdev_destroy_spacemaps(vd, tx); 30594e75ba68SSerapheim Dimitropoulos if (vd->vdev_top_zap != 0) { 3060215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3061215198a6SJoe Stein vd->vdev_top_zap = 0; 3062215198a6SJoe Stein } 30634e75ba68SSerapheim Dimitropoulos 306488ecc943SGeorge Wilson dmu_tx_commit(tx); 306588ecc943SGeorge Wilson } 306688ecc943SGeorge Wilson 3067fa9e4066Sahrens void 3068fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 3069fa9e4066Sahrens { 3070fa9e4066Sahrens metaslab_t *msp; 307180eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3072fa9e4066Sahrens 30735cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 307488ecc943SGeorge Wilson 3075094e47e9SGeorge Wilson while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3076094e47e9SGeorge Wilson != NULL) 3077fa9e4066Sahrens metaslab_sync_done(msp, txg); 307880eb36f2SGeorge Wilson 307980eb36f2SGeorge Wilson if (reassess) 308080eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 3081fa9e4066Sahrens } 3082fa9e4066Sahrens 3083fa9e4066Sahrens void 3084fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 3085fa9e4066Sahrens { 3086fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3087fa9e4066Sahrens vdev_t *lvd; 3088fa9e4066Sahrens metaslab_t *msp; 3089ecc2d604Sbonwick dmu_tx_t *tx; 3090fa9e4066Sahrens 30915cabbc6bSPrashanth Sreenivasa if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 30925cabbc6bSPrashanth Sreenivasa dmu_tx_t *tx; 30935cabbc6bSPrashanth Sreenivasa 30945cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 30955cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 309688ecc943SGeorge Wilson 30975cabbc6bSPrashanth Sreenivasa tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 30985cabbc6bSPrashanth Sreenivasa vdev_indirect_sync_obsolete(vd, tx); 30995cabbc6bSPrashanth Sreenivasa dmu_tx_commit(tx); 31005cabbc6bSPrashanth Sreenivasa 31015cabbc6bSPrashanth Sreenivasa /* 31025cabbc6bSPrashanth Sreenivasa * If the vdev is indirect, it can't have dirty 31035cabbc6bSPrashanth Sreenivasa * metaslabs or DTLs. 31045cabbc6bSPrashanth Sreenivasa */ 31055cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 31065cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 31075cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 31085cabbc6bSPrashanth Sreenivasa return; 31095cabbc6bSPrashanth Sreenivasa } 31105cabbc6bSPrashanth Sreenivasa } 31115cabbc6bSPrashanth Sreenivasa 31125cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 31135cabbc6bSPrashanth Sreenivasa 31145cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 31155cabbc6bSPrashanth Sreenivasa !vd->vdev_removing) { 3116ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 31175cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3118ecc2d604Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3119ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3120ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3121ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 3122ecc2d604Sbonwick vdev_config_dirty(vd); 3123ecc2d604Sbonwick dmu_tx_commit(tx); 3124ecc2d604Sbonwick } 3125fa9e4066Sahrens 3126ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3127fa9e4066Sahrens metaslab_sync(msp, txg); 3128ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3129ecc2d604Sbonwick } 3130fa9e4066Sahrens 3131fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3132fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 3133fa9e4066Sahrens 31345cabbc6bSPrashanth Sreenivasa /* 31354e75ba68SSerapheim Dimitropoulos * If this is an empty log device being removed, destroy the 31364e75ba68SSerapheim Dimitropoulos * metadata associated with it. 31375cabbc6bSPrashanth Sreenivasa */ 31384e75ba68SSerapheim Dimitropoulos if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 31394e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vd, txg); 31405cabbc6bSPrashanth Sreenivasa 3141fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3142fa9e4066Sahrens } 3143fa9e4066Sahrens 3144fa9e4066Sahrens uint64_t 3145fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3146fa9e4066Sahrens { 3147fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3148fa9e4066Sahrens } 3149fa9e4066Sahrens 31503d7072f8Seschrock /* 31513d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 31523d7072f8Seschrock * not be opened, and no I/O is attempted. 31533d7072f8Seschrock */ 3154fa9e4066Sahrens int 3155069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3156fa9e4066Sahrens { 31574b964adaSGeorge Wilson vdev_t *vd, *tvd; 3158fa9e4066Sahrens 31598f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 3160fa9e4066Sahrens 3161c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3162e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3163e14bb325SJeff Bonwick 31643d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3165e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3166fa9e4066Sahrens 31674b964adaSGeorge Wilson tvd = vd->vdev_top; 31684b964adaSGeorge Wilson 3169069f55e2SEric Schrock /* 3170069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 3171069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 3172069f55e2SEric Schrock * were faulted. 3173069f55e2SEric Schrock */ 3174069f55e2SEric Schrock vd->vdev_label_aux = aux; 3175069f55e2SEric Schrock 31763d7072f8Seschrock /* 31773d7072f8Seschrock * Faulted state takes precedence over degraded. 31783d7072f8Seschrock */ 317998d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 31803d7072f8Seschrock vd->vdev_faulted = 1ULL; 31813d7072f8Seschrock vd->vdev_degraded = 0ULL; 3182069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 31833d7072f8Seschrock 31843d7072f8Seschrock /* 3185c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 3186c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 31873d7072f8Seschrock */ 31884b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 31893d7072f8Seschrock vd->vdev_degraded = 1ULL; 31903d7072f8Seschrock vd->vdev_faulted = 0ULL; 31913d7072f8Seschrock 31923d7072f8Seschrock /* 31933d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 31943d7072f8Seschrock * mark it degraded. 31953d7072f8Seschrock */ 31964b964adaSGeorge Wilson vdev_reopen(tvd); 31973d7072f8Seschrock 3198069f55e2SEric Schrock if (vdev_readable(vd)) 3199069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 32003d7072f8Seschrock } 32013d7072f8Seschrock 3202e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 32033d7072f8Seschrock } 32043d7072f8Seschrock 32053d7072f8Seschrock /* 32063d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 32073d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 32083d7072f8Seschrock * as I/O is concerned. 32093d7072f8Seschrock */ 32103d7072f8Seschrock int 3211069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 32123d7072f8Seschrock { 3213c5904d13Seschrock vdev_t *vd; 32140a4e9518Sgw 32158f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 32163d7072f8Seschrock 3217c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3218e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3219e14bb325SJeff Bonwick 32200e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3221e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 32220e34b6a7Sbonwick 32233d7072f8Seschrock /* 32243d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 32253d7072f8Seschrock */ 3226e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 3227e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 32283d7072f8Seschrock 32293d7072f8Seschrock vd->vdev_degraded = 1ULL; 32303d7072f8Seschrock if (!vdev_is_dead(vd)) 32313d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3232069f55e2SEric Schrock aux); 32333d7072f8Seschrock 3234e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 32353d7072f8Seschrock } 32363d7072f8Seschrock 32373d7072f8Seschrock /* 3238f7170741SWill Andrews * Online the given vdev. 3239f7170741SWill Andrews * 3240f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3241f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 3242f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 3243f7170741SWill Andrews * events are generated if the device fails to open. 32443d7072f8Seschrock */ 32453d7072f8Seschrock int 3246e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 32473d7072f8Seschrock { 3248573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 32495f368aefSYuri Pankov boolean_t wasoffline; 32505f368aefSYuri Pankov vdev_state_t oldstate; 32513d7072f8Seschrock 32528f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 32533d7072f8Seschrock 3254c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3255e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 32563d7072f8Seschrock 32573d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3258e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3259fa9e4066Sahrens 32605f368aefSYuri Pankov wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 32615f368aefSYuri Pankov oldstate = vd->vdev_state; 326214372834SHans Rosenfeld 3263573ca77eSGeorge Wilson tvd = vd->vdev_top; 3264fa9e4066Sahrens vd->vdev_offline = B_FALSE; 3265441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 3266e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3267e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3268573ca77eSGeorge Wilson 3269573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3270573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3271573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3272573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3273573ca77eSGeorge Wilson } 3274573ca77eSGeorge Wilson 3275573ca77eSGeorge Wilson vdev_reopen(tvd); 32763d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 32773d7072f8Seschrock 3278573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3279573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3280573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 3281573ca77eSGeorge Wilson } 3282573ca77eSGeorge Wilson 32833d7072f8Seschrock if (newstate) 32843d7072f8Seschrock *newstate = vd->vdev_state; 32853d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 32863d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 32873d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 32883d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 32893d7072f8Seschrock vd->vdev_unspare = B_TRUE; 3290fa9e4066Sahrens 3291573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3292573ca77eSGeorge Wilson 3293573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3294573ca77eSGeorge Wilson if (vd->vdev_aux) 3295573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3296573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3297573ca77eSGeorge Wilson } 329814372834SHans Rosenfeld 3299094e47e9SGeorge Wilson /* Restart initializing if necessary */ 3300094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 3301094e47e9SGeorge Wilson if (vdev_writeable(vd) && 3302094e47e9SGeorge Wilson vd->vdev_initialize_thread == NULL && 3303094e47e9SGeorge Wilson vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3304094e47e9SGeorge Wilson (void) vdev_initialize(vd); 3305094e47e9SGeorge Wilson } 3306094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 3307094e47e9SGeorge Wilson 33085f368aefSYuri Pankov if (wasoffline || 33095f368aefSYuri Pankov (oldstate < VDEV_STATE_DEGRADED && 33105f368aefSYuri Pankov vd->vdev_state >= VDEV_STATE_DEGRADED)) 3311ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 331214372834SHans Rosenfeld 33138ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 3314fa9e4066Sahrens } 3315fa9e4066Sahrens 3316a1521560SJeff Bonwick static int 3317a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3318fa9e4066Sahrens { 3319e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 33208f18d1faSGeorge Wilson int error = 0; 33218f18d1faSGeorge Wilson uint64_t generation; 33228f18d1faSGeorge Wilson metaslab_group_t *mg; 33230a4e9518Sgw 33248f18d1faSGeorge Wilson top: 33258f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 3326fa9e4066Sahrens 3327c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3328e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3329fa9e4066Sahrens 33300e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3331e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 33320e34b6a7Sbonwick 3333e6ca193dSGeorge Wilson tvd = vd->vdev_top; 33348f18d1faSGeorge Wilson mg = tvd->vdev_mg; 33358f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 3336e6ca193dSGeorge Wilson 3337fa9e4066Sahrens /* 3338ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 3339fa9e4066Sahrens */ 3340ecc2d604Sbonwick if (!vd->vdev_offline) { 3341ecc2d604Sbonwick /* 33428ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 3343e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 3344e6ca193dSGeorge Wilson * expendable. 3345ecc2d604Sbonwick */ 3346e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3347e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 3348e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3349fa9e4066Sahrens 33508f18d1faSGeorge Wilson /* 3351b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 3352b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 3353b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 3354b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 33558f18d1faSGeorge Wilson */ 33568f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 33578f18d1faSGeorge Wilson /* 33588f18d1faSGeorge Wilson * Prevent any future allocations. 33598f18d1faSGeorge Wilson */ 3360a1521560SJeff Bonwick metaslab_group_passivate(mg); 33618f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 33628f18d1faSGeorge Wilson 33635cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 33648f18d1faSGeorge Wilson 336586714001SSerapheim Dimitropoulos /* 336686714001SSerapheim Dimitropoulos * If the log device was successfully reset but has 336786714001SSerapheim Dimitropoulos * checkpointed data, do not offline it. 336886714001SSerapheim Dimitropoulos */ 336986714001SSerapheim Dimitropoulos if (error == 0 && 337086714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm != NULL) { 337186714001SSerapheim Dimitropoulos ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, 337286714001SSerapheim Dimitropoulos !=, 0); 337386714001SSerapheim Dimitropoulos error = ZFS_ERR_CHECKPOINT_EXISTS; 337486714001SSerapheim Dimitropoulos } 337586714001SSerapheim Dimitropoulos 33768f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 33778f18d1faSGeorge Wilson 33788f18d1faSGeorge Wilson /* 33798f18d1faSGeorge Wilson * Check to see if the config has changed. 33808f18d1faSGeorge Wilson */ 33818f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 3382a1521560SJeff Bonwick metaslab_group_activate(mg); 33838f18d1faSGeorge Wilson if (error) 33848f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 33858f18d1faSGeorge Wilson vd, error)); 33868f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 33878f18d1faSGeorge Wilson goto top; 33888f18d1faSGeorge Wilson } 3389fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 33908f18d1faSGeorge Wilson } 33918f18d1faSGeorge Wilson 3392ecc2d604Sbonwick /* 3393ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 3394e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 3395e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 3396e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 3397ecc2d604Sbonwick */ 3398ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 3399e6ca193dSGeorge Wilson vdev_reopen(tvd); 3400e6ca193dSGeorge Wilson 3401e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3402e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 3403ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 3404e6ca193dSGeorge Wilson vdev_reopen(tvd); 3405e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3406ecc2d604Sbonwick } 34078f18d1faSGeorge Wilson 34088f18d1faSGeorge Wilson /* 34098f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 34108f18d1faSGeorge Wilson * once we online the device it's open for business. 34118f18d1faSGeorge Wilson */ 34128f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 3413a1521560SJeff Bonwick metaslab_group_activate(mg); 3414fa9e4066Sahrens } 3415fa9e4066Sahrens 3416e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3417ecc2d604Sbonwick 34188f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 3419fa9e4066Sahrens } 3420fa9e4066Sahrens 3421a1521560SJeff Bonwick int 3422a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3423a1521560SJeff Bonwick { 3424a1521560SJeff Bonwick int error; 3425a1521560SJeff Bonwick 3426a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 3427a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 3428a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 3429a1521560SJeff Bonwick 3430a1521560SJeff Bonwick return (error); 3431a1521560SJeff Bonwick } 3432a1521560SJeff Bonwick 3433ea8dc4b6Seschrock /* 3434ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 3435ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 3436ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3437ea8dc4b6Seschrock */ 3438ea8dc4b6Seschrock void 3439e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 3440fa9e4066Sahrens { 3441e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3442e14bb325SJeff Bonwick 3443e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3444fa9e4066Sahrens 3445ea8dc4b6Seschrock if (vd == NULL) 3446e14bb325SJeff Bonwick vd = rvd; 3447fa9e4066Sahrens 3448ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 3449ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 3450ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 3451fa9e4066Sahrens 3452e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3453e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 34543d7072f8Seschrock 34555cabbc6bSPrashanth Sreenivasa /* 34565cabbc6bSPrashanth Sreenivasa * It makes no sense to "clear" an indirect vdev. 34575cabbc6bSPrashanth Sreenivasa */ 34585cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(vd)) 34595cabbc6bSPrashanth Sreenivasa return; 34605cabbc6bSPrashanth Sreenivasa 34613d7072f8Seschrock /* 34628a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 34638a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 34648a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 34658a79c1b5Sek * written out to disk. 34663d7072f8Seschrock */ 3467e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 3468e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 34698a79c1b5Sek 3470096d22d4SEric Schrock /* 3471096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 3472096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 3473096d22d4SEric Schrock * still broken, we want to still post the ereport again. 3474096d22d4SEric Schrock */ 3475096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 3476096d22d4SEric Schrock 34774b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3478e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 3479e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 3480e14bb325SJeff Bonwick 3481f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 34823d7072f8Seschrock 3483096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 3484096d22d4SEric Schrock 3485f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 3486e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3487e14bb325SJeff Bonwick 3488e14bb325SJeff Bonwick if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3489bb8b5132Sek spa_async_request(spa, SPA_ASYNC_RESILVER); 34903d7072f8Seschrock 3491ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 34923d7072f8Seschrock } 3493096d22d4SEric Schrock 3494096d22d4SEric Schrock /* 3495096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 3496096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 3497096d22d4SEric Schrock * done in response to the FMA fault. 3498096d22d4SEric Schrock */ 3499096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3500096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3501096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 3502096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 3503fa9e4066Sahrens } 3504fa9e4066Sahrens 3505e14bb325SJeff Bonwick boolean_t 3506e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 35070a4e9518Sgw { 350888ecc943SGeorge Wilson /* 350988ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 351088ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 351188ecc943SGeorge Wilson * these types of devices in the various code paths. 351288ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 351388ecc943SGeorge Wilson * before issuing I/O to them. 351488ecc943SGeorge Wilson */ 35155cabbc6bSPrashanth Sreenivasa return (vd->vdev_state < VDEV_STATE_DEGRADED || 35165cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_hole_ops || 351788ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 35180a4e9518Sgw } 35190a4e9518Sgw 3520e14bb325SJeff Bonwick boolean_t 3521e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 35220a4e9518Sgw { 3523e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 35240a4e9518Sgw } 35250a4e9518Sgw 3526e14bb325SJeff Bonwick boolean_t 3527e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 3528fa9e4066Sahrens { 35295cabbc6bSPrashanth Sreenivasa return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 35305cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)); 3531fa9e4066Sahrens } 3532fa9e4066Sahrens 3533a31e6787SGeorge Wilson boolean_t 3534a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 3535a31e6787SGeorge Wilson { 35368ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 35378ad4d6ddSJeff Bonwick 3538a31e6787SGeorge Wilson /* 35398ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 3540a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3541a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 35428ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 35438ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 35448ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 3545a31e6787SGeorge Wilson */ 35468ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 35475cabbc6bSPrashanth Sreenivasa !vd->vdev_cant_write && vdev_is_concrete(vd) && 35480f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 3549a31e6787SGeorge Wilson } 3550a31e6787SGeorge Wilson 3551e14bb325SJeff Bonwick boolean_t 3552e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 3553fa9e4066Sahrens { 3554e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 3555fa9e4066Sahrens 3556e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3557e14bb325SJeff Bonwick return (B_FALSE); 3558fa9e4066Sahrens 3559e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 3560e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 3561fa9e4066Sahrens 3562e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3563e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 3564fa9e4066Sahrens 3565e14bb325SJeff Bonwick return (B_TRUE); 3566fa9e4066Sahrens } 3567fa9e4066Sahrens 356886714001SSerapheim Dimitropoulos boolean_t 356986714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd) 357086714001SSerapheim Dimitropoulos { 3571a0b03b16SSerapheim Dimitropoulos if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 3572a0b03b16SSerapheim Dimitropoulos return (B_TRUE); 3573a0b03b16SSerapheim Dimitropoulos 357486714001SSerapheim Dimitropoulos /* 3575a0b03b16SSerapheim Dimitropoulos * If double-word space map entries are not enabled we assume 3576a0b03b16SSerapheim Dimitropoulos * 47 bits of the space map entry are dedicated to the entry's 3577a0b03b16SSerapheim Dimitropoulos * offset (see SM_OFFSET_BITS in space_map.h). We then use that 3578a0b03b16SSerapheim Dimitropoulos * to calculate the maximum address that can be described by a 3579a0b03b16SSerapheim Dimitropoulos * space map entry for the given device. 358086714001SSerapheim Dimitropoulos */ 3581a0b03b16SSerapheim Dimitropoulos uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 358286714001SSerapheim Dimitropoulos 358386714001SSerapheim Dimitropoulos if (shift >= 63) /* detect potential overflow */ 358486714001SSerapheim Dimitropoulos return (B_TRUE); 358586714001SSerapheim Dimitropoulos 358686714001SSerapheim Dimitropoulos return (vd->vdev_asize < (1ULL << shift)); 358786714001SSerapheim Dimitropoulos } 358886714001SSerapheim Dimitropoulos 3589fa9e4066Sahrens /* 3590fa9e4066Sahrens * Get statistics for the given vdev. 3591fa9e4066Sahrens */ 3592fa9e4066Sahrens void 3593fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3594fa9e4066Sahrens { 35952e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 35962e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 3597c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 35982e4c9986SGeorge Wilson 35992e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3600fa9e4066Sahrens 3601fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3602fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3603fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3604fa9e4066Sahrens vs->vs_state = vd->vdev_state; 3605573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 3606094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3607573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3608094e47e9SGeorge Wilson /* 3609094e47e9SGeorge Wilson * Report intializing progress. Since we don't have the 3610094e47e9SGeorge Wilson * initializing locks held, this is only an estimate (although a 3611094e47e9SGeorge Wilson * fairly accurate one). 3612094e47e9SGeorge Wilson */ 3613094e47e9SGeorge Wilson vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done; 3614094e47e9SGeorge Wilson vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est; 3615094e47e9SGeorge Wilson vs->vs_initialize_state = vd->vdev_initialize_state; 3616094e47e9SGeorge Wilson vs->vs_initialize_action_time = vd->vdev_initialize_action_time; 3617094e47e9SGeorge Wilson } 3618c39a2aaeSGeorge Wilson /* 3619c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 3620c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 3621c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 3622c39a2aaeSGeorge Wilson */ 3623c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 36247855d95bSToomas Soome vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 36257855d95bSToomas Soome spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3626c39a2aaeSGeorge Wilson } 36275cabbc6bSPrashanth Sreenivasa if (vd->vdev_aux == NULL && vd == vd->vdev_top && 36285cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3629*663207adSDon Brady vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 3630*663207adSDon Brady vd->vdev_mg->mg_fragmentation : 0; 36312986efa8SAlex Reece } 3632fa9e4066Sahrens 3633fa9e4066Sahrens /* 3634fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 3635fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 3636fa9e4066Sahrens */ 3637fa9e4066Sahrens if (vd == rvd) { 3638e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 3639fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 3640fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 3641fa9e4066Sahrens 3642e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 3643fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 3644fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 3645fa9e4066Sahrens } 36463f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 3647fa9e4066Sahrens } 3648fa9e4066Sahrens } 36492e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 3650fa9e4066Sahrens } 3651fa9e4066Sahrens 3652fa94a07fSbrendan void 3653fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 3654fa94a07fSbrendan { 3655fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 3656fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 3657fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 3658fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 3659fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 3660fa94a07fSbrendan } 3661fa94a07fSbrendan 36623f9d6ad7SLin Ling void 36633f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 36643f9d6ad7SLin Ling { 36653f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 36663f9d6ad7SLin Ling 36673f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 36683f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 36693f9d6ad7SLin Ling 36703f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 36713f9d6ad7SLin Ling vs->vs_scan_processed = 0; 36723f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 36733f9d6ad7SLin Ling } 36743f9d6ad7SLin Ling 3675fa9e4066Sahrens void 3676e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 3677fa9e4066Sahrens { 36788ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 36798ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3680e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3681fa9e4066Sahrens vdev_t *pvd; 3682fa9e4066Sahrens uint64_t txg = zio->io_txg; 3683fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 3684fa9e4066Sahrens zio_type_t type = zio->io_type; 3685fa9e4066Sahrens int flags = zio->io_flags; 3686fa9e4066Sahrens 3687e14bb325SJeff Bonwick /* 3688e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 3689e14bb325SJeff Bonwick */ 3690e14bb325SJeff Bonwick if (zio->io_gang_tree) 3691e14bb325SJeff Bonwick return; 3692e14bb325SJeff Bonwick 3693fa9e4066Sahrens if (zio->io_error == 0) { 3694e14bb325SJeff Bonwick /* 3695e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 3696e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 3697e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 3698e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 3699e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 3700e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 3701e14bb325SJeff Bonwick * remain zero, which is what we want.) 3702e14bb325SJeff Bonwick * 3703e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 3704e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 3705e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 3706e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 3707e14bb325SJeff Bonwick */ 3708e14bb325SJeff Bonwick if (vd == rvd) 3709e14bb325SJeff Bonwick return; 3710e14bb325SJeff Bonwick 3711e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 37128ad4d6ddSJeff Bonwick 37138ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 37148ad4d6ddSJeff Bonwick return; 37158ad4d6ddSJeff Bonwick 37168ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 37178ad4d6ddSJeff Bonwick 3718e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 371944ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 37203f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 37213f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 37223f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 37233f9d6ad7SLin Ling 37243f9d6ad7SLin Ling /* XXX cleanup? */ 37253f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 37263f9d6ad7SLin Ling atomic_add_64(processed, psize); 37273f9d6ad7SLin Ling vs->vs_scan_processed += psize; 37283f9d6ad7SLin Ling } 37293f9d6ad7SLin Ling 37308ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 3731e14bb325SJeff Bonwick vs->vs_self_healed += psize; 3732fa9e4066Sahrens } 37338ad4d6ddSJeff Bonwick 37348ad4d6ddSJeff Bonwick vs->vs_ops[type]++; 37358ad4d6ddSJeff Bonwick vs->vs_bytes[type] += psize; 37368ad4d6ddSJeff Bonwick 37378ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3738fa9e4066Sahrens return; 3739fa9e4066Sahrens } 3740fa9e4066Sahrens 3741fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 3742fa9e4066Sahrens return; 3743fa9e4066Sahrens 37448956713aSEric Schrock /* 37458956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 37468956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 37478956713aSEric Schrock * hard errors, when in reality they can happen for any number of 37488956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 37498956713aSEric Schrock */ 37508956713aSEric Schrock if (zio->io_error == EIO && 37518956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 37528956713aSEric Schrock return; 37538956713aSEric Schrock 37548f18d1faSGeorge Wilson /* 37558f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 37568f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 37578f18d1faSGeorge Wilson * errors. 37588f18d1faSGeorge Wilson */ 37598f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 37608f18d1faSGeorge Wilson return; 37618f18d1faSGeorge Wilson 3762e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 3763b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3764e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 3765e14bb325SJeff Bonwick vs->vs_checksum_errors++; 3766e14bb325SJeff Bonwick else 3767e14bb325SJeff Bonwick vs->vs_read_errors++; 3768fa9e4066Sahrens } 3769b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3770e14bb325SJeff Bonwick vs->vs_write_errors++; 3771e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3772fa9e4066Sahrens 37735cabbc6bSPrashanth Sreenivasa if (spa->spa_load_state == SPA_LOAD_NONE && 37745cabbc6bSPrashanth Sreenivasa type == ZIO_TYPE_WRITE && txg != 0 && 37758ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 377644ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 3777b24ab676SJeff Bonwick spa->spa_claiming)) { 37788ad4d6ddSJeff Bonwick /* 3779b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 3780b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 3781b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 3782b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 3783b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 3784b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 3785b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 3786b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 37878ad4d6ddSJeff Bonwick * 37888ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 37898ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 37908ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 37918ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 37928ad4d6ddSJeff Bonwick */ 37938ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 37948ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 379544ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 37968ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 37978ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 37988ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3799b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 3800b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 3801b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3802b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 38038ad4d6ddSJeff Bonwick } 3804b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 38058ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3806fa9e4066Sahrens return; 38078ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 38088ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 38098ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3810fa9e4066Sahrens } 38118ad4d6ddSJeff Bonwick if (vd != rvd) 38128ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3813fa9e4066Sahrens } 3814fa9e4066Sahrens } 3815fa9e4066Sahrens 3816*663207adSDon Brady int64_t 3817*663207adSDon Brady vdev_deflated_space(vdev_t *vd, int64_t space) 3818*663207adSDon Brady { 3819*663207adSDon Brady ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 3820*663207adSDon Brady ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 3821*663207adSDon Brady 3822*663207adSDon Brady return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 3823*663207adSDon Brady } 3824*663207adSDon Brady 3825fa9e4066Sahrens /* 3826*663207adSDon Brady * Update the in-core space usage stats for this vdev and the root vdev. 3827fa9e4066Sahrens */ 3828fa9e4066Sahrens void 3829b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3830b24ab676SJeff Bonwick int64_t space_delta) 3831fa9e4066Sahrens { 3832*663207adSDon Brady int64_t dspace_delta; 38338654d025Sperrin spa_t *spa = vd->vdev_spa; 38348654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 3835fa9e4066Sahrens 38368654d025Sperrin ASSERT(vd == vd->vdev_top); 383799653d4eSeschrock 38388654d025Sperrin /* 38398654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 38408654d025Sperrin * factor. We must calculate this here and not at the root vdev 38418654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 38428654d025Sperrin * childrens', thus not accurate enough for us. 38438654d025Sperrin */ 3844*663207adSDon Brady dspace_delta = vdev_deflated_space(vd, space_delta); 38458654d025Sperrin 38468654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 38478654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 3848b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 38498654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 38508654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 38518654d025Sperrin 3852*663207adSDon Brady /* every class but log contributes to root space stats */ 3853*663207adSDon Brady if (vd->vdev_mg != NULL && !vd->vdev_islog) { 3854fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 3855fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 3856b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 3857fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 3858fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 3859fa94a07fSbrendan } 3860*663207adSDon Brady /* Note: metaslab_class_space_update moved to metaslab_space_update */ 3861fa9e4066Sahrens } 3862fa9e4066Sahrens 3863fa9e4066Sahrens /* 3864fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3865fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3866fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3867fa9e4066Sahrens */ 3868fa9e4066Sahrens void 3869fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3870fa9e4066Sahrens { 3871fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3872fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3873fa9e4066Sahrens int c; 3874fa9e4066Sahrens 3875f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3876f9af39baSGeorge Wilson 3877c5904d13Seschrock /* 38786809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 38796809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3880c5904d13Seschrock */ 3881c5904d13Seschrock if (vd->vdev_aux != NULL) { 3882c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3883c5904d13Seschrock nvlist_t **aux; 3884c5904d13Seschrock uint_t naux; 3885c5904d13Seschrock 3886c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3887c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3888c5904d13Seschrock break; 3889c5904d13Seschrock } 3890c5904d13Seschrock 3891e14bb325SJeff Bonwick if (c == sav->sav_count) { 3892e14bb325SJeff Bonwick /* 3893e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3894e14bb325SJeff Bonwick */ 3895e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3896e14bb325SJeff Bonwick return; 3897e14bb325SJeff Bonwick } 3898e14bb325SJeff Bonwick 3899c5904d13Seschrock sav->sav_sync = B_TRUE; 3900c5904d13Seschrock 39016809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 39026809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 39036809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 39046809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 39056809eb4eSEric Schrock } 3906c5904d13Seschrock 3907c5904d13Seschrock ASSERT(c < naux); 3908c5904d13Seschrock 3909c5904d13Seschrock /* 3910c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 3911c5904d13Seschrock * sketchy, but it will work. 3912c5904d13Seschrock */ 3913c5904d13Seschrock nvlist_free(aux[c]); 39143f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3915c5904d13Seschrock 3916c5904d13Seschrock return; 3917c5904d13Seschrock } 3918c5904d13Seschrock 39195dabedeeSbonwick /* 3920e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 3921e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 3922e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 39235dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 39245dabedeeSbonwick */ 3925e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3926e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3927e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 39285dabedeeSbonwick 3929fa9e4066Sahrens if (vd == rvd) { 3930fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 3931fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 3932fa9e4066Sahrens } else { 3933fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 3934fa9e4066Sahrens 393588ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 39365cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3937e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 39385cabbc6bSPrashanth Sreenivasa } 3939fa9e4066Sahrens } 3940fa9e4066Sahrens } 3941fa9e4066Sahrens 3942fa9e4066Sahrens void 3943fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 3944fa9e4066Sahrens { 39455dabedeeSbonwick spa_t *spa = vd->vdev_spa; 39465dabedeeSbonwick 3947e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3948e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3949e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 39505dabedeeSbonwick 3951e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3952e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 3953e14bb325SJeff Bonwick } 3954e14bb325SJeff Bonwick 3955e14bb325SJeff Bonwick /* 3956e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 3957e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3958e14bb325SJeff Bonwick * the state changes from larger config changes because they require 3959e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 3960e14bb325SJeff Bonwick */ 3961e14bb325SJeff Bonwick void 3962e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 3963e14bb325SJeff Bonwick { 3964e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3965e14bb325SJeff Bonwick 3966f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3967e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3968e14bb325SJeff Bonwick 3969e14bb325SJeff Bonwick /* 3970e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 3971e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 3972e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 3973e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 3974e14bb325SJeff Bonwick */ 3975e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3976e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3977e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3978e14bb325SJeff Bonwick 39795cabbc6bSPrashanth Sreenivasa if (!list_link_active(&vd->vdev_state_dirty_node) && 39805cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) 3981e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 3982e14bb325SJeff Bonwick } 3983e14bb325SJeff Bonwick 3984e14bb325SJeff Bonwick void 3985e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 3986e14bb325SJeff Bonwick { 3987e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3988e14bb325SJeff Bonwick 3989e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3990e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3991e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3992e14bb325SJeff Bonwick 3993e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3994e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 3995fa9e4066Sahrens } 3996fa9e4066Sahrens 399732b87932Sek /* 399832b87932Sek * Propagate vdev state up from children to parent. 399932b87932Sek */ 400044cd46caSbillm void 400144cd46caSbillm vdev_propagate_state(vdev_t *vd) 400244cd46caSbillm { 40038ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 40048ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 400544cd46caSbillm int degraded = 0, faulted = 0; 400644cd46caSbillm int corrupted = 0; 400744cd46caSbillm vdev_t *child; 400844cd46caSbillm 40093d7072f8Seschrock if (vd->vdev_children > 0) { 4010573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 40113d7072f8Seschrock child = vd->vdev_child[c]; 401251ece835Seschrock 401388ecc943SGeorge Wilson /* 40145cabbc6bSPrashanth Sreenivasa * Don't factor holes or indirect vdevs into the 40155cabbc6bSPrashanth Sreenivasa * decision. 401688ecc943SGeorge Wilson */ 40175cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(child)) 401888ecc943SGeorge Wilson continue; 401988ecc943SGeorge Wilson 4020e14bb325SJeff Bonwick if (!vdev_readable(child) || 40218ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 402251ece835Seschrock /* 402351ece835Seschrock * Root special: if there is a top-level log 402451ece835Seschrock * device, treat the root vdev as if it were 402551ece835Seschrock * degraded. 402651ece835Seschrock */ 402751ece835Seschrock if (child->vdev_islog && vd == rvd) 402851ece835Seschrock degraded++; 402951ece835Seschrock else 403051ece835Seschrock faulted++; 403151ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 40323d7072f8Seschrock degraded++; 403351ece835Seschrock } 403444cd46caSbillm 40353d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 40363d7072f8Seschrock corrupted++; 40373d7072f8Seschrock } 403844cd46caSbillm 40393d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 40403d7072f8Seschrock 40413d7072f8Seschrock /* 4042e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 40433d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 40443d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 40453d7072f8Seschrock * replicas'. 40463d7072f8Seschrock */ 40473d7072f8Seschrock if (corrupted && vd == rvd && 40483d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 40493d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 40503d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 40513d7072f8Seschrock } 40523d7072f8Seschrock 405351ece835Seschrock if (vd->vdev_parent) 40543d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 405544cd46caSbillm } 405644cd46caSbillm 4057fa9e4066Sahrens /* 4058ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 4059ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 4060ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 4061ea8dc4b6Seschrock * 4062ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 4063ea8dc4b6Seschrock * generated. 4064fa9e4066Sahrens */ 4065fa9e4066Sahrens void 4066ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4067fa9e4066Sahrens { 4068560e6e96Seschrock uint64_t save_state; 4069c5904d13Seschrock spa_t *spa = vd->vdev_spa; 4070ea8dc4b6Seschrock 4071ea8dc4b6Seschrock if (state == vd->vdev_state) { 4072ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 4073fa9e4066Sahrens return; 4074ea8dc4b6Seschrock } 4075ea8dc4b6Seschrock 4076560e6e96Seschrock save_state = vd->vdev_state; 4077fa9e4066Sahrens 4078fa9e4066Sahrens vd->vdev_state = state; 4079fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 4080fa9e4066Sahrens 40813d7072f8Seschrock /* 40823d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 408398d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 408498d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 408598d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 408698d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 408798d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 408898d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 408998d1cbfeSGeorge Wilson * children. 409098d1cbfeSGeorge Wilson */ 409198d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 409298d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 40933d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 40943d7072f8Seschrock 4095069f55e2SEric Schrock /* 4096069f55e2SEric Schrock * If we have brought this vdev back into service, we need 4097069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 4098069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 4099069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 4100069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 4101069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 4102069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 4103069f55e2SEric Schrock */ 4104069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 4105069f55e2SEric Schrock vd->vdev_prevstate != state) 4106069f55e2SEric Schrock zfs_post_state_change(spa, vd); 4107069f55e2SEric Schrock 41083d7072f8Seschrock if (vd->vdev_removed && 41093d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 41103d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 41113d7072f8Seschrock /* 41123d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 41133d7072f8Seschrock * device was previously marked removed and someone attempted to 41143d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 41153d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 41163d7072f8Seschrock * it is one of our special test online cases, which is only 41173d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 41183d7072f8Seschrock * fault. 41193d7072f8Seschrock */ 41203d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 41213d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 41223d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 41233d7072f8Seschrock vd->vdev_removed = B_TRUE; 41243d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 4125ea8dc4b6Seschrock /* 4126cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 4127cb04b873SMark J Musante * mark it as "not available", which signifies that it was 4128cb04b873SMark J Musante * never there to begin with. Failure to open such a device 4129cb04b873SMark J Musante * is not considered an error. 4130ea8dc4b6Seschrock */ 4131cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 4132cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 4133560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 4134560e6e96Seschrock vd->vdev_not_present = 1; 4135560e6e96Seschrock 4136560e6e96Seschrock /* 4137560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 4138560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 4139560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 4140560e6e96Seschrock * want to post the ereport if the device was already in the 4141560e6e96Seschrock * CANT_OPEN state beforehand. 41423d7072f8Seschrock * 41433d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 41443d7072f8Seschrock * online the device in response to an insertion event. If we 41453d7072f8Seschrock * hit this case, then we have detected an insertion event for a 41463d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 41473d7072f8Seschrock * In this scenario, we don't post an ereport because we are 41483d7072f8Seschrock * about to replace the device, or attempt an online with 41493d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 4150560e6e96Seschrock */ 41513d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 41523d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 4153c5904d13Seschrock vd != spa->spa_root_vdev) { 4154ea8dc4b6Seschrock const char *class; 4155ea8dc4b6Seschrock 4156ea8dc4b6Seschrock switch (aux) { 4157ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 4158ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 4159ea8dc4b6Seschrock break; 4160ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 4161ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 4162ea8dc4b6Seschrock break; 4163ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 4164ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 4165ea8dc4b6Seschrock break; 4166ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 4167ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 4168ea8dc4b6Seschrock break; 4169ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 4170ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 4171ea8dc4b6Seschrock break; 4172ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 4173ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 4174ea8dc4b6Seschrock break; 4175ea8dc4b6Seschrock default: 4176ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 4177ea8dc4b6Seschrock } 4178ea8dc4b6Seschrock 4179c5904d13Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 4180ea8dc4b6Seschrock } 4181ea8dc4b6Seschrock 41823d7072f8Seschrock /* Erase any notion of persistent removed state */ 41833d7072f8Seschrock vd->vdev_removed = B_FALSE; 41843d7072f8Seschrock } else { 41853d7072f8Seschrock vd->vdev_removed = B_FALSE; 41863d7072f8Seschrock } 4187ea8dc4b6Seschrock 41888b33d774STim Haley if (!isopen && vd->vdev_parent) 41898b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 4190fa9e4066Sahrens } 419115e6edf1Sgw 41926f793812SPavel Zakharov boolean_t 41936f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd) 41946f793812SPavel Zakharov { 41956f793812SPavel Zakharov ASSERT(!vd->vdev_ops->vdev_op_leaf); 41966f793812SPavel Zakharov 41976f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) { 41986f793812SPavel Zakharov if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 41996f793812SPavel Zakharov return (B_FALSE); 42006f793812SPavel Zakharov } 42016f793812SPavel Zakharov 42026f793812SPavel Zakharov return (B_TRUE); 42036f793812SPavel Zakharov } 42046f793812SPavel Zakharov 420515e6edf1Sgw /* 420615e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 4207c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 4208c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 420915e6edf1Sgw */ 421015e6edf1Sgw boolean_t 421115e6edf1Sgw vdev_is_bootable(vdev_t *vd) 421215e6edf1Sgw { 421315e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 421415e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 421515e6edf1Sgw 421615e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 421715e6edf1Sgw vd->vdev_children > 1) { 421815e6edf1Sgw return (B_FALSE); 42195cabbc6bSPrashanth Sreenivasa } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 42205cabbc6bSPrashanth Sreenivasa strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 422115e6edf1Sgw return (B_FALSE); 422215e6edf1Sgw } 422315e6edf1Sgw } 422415e6edf1Sgw 4225573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 422615e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 422715e6edf1Sgw return (B_FALSE); 422815e6edf1Sgw } 422915e6edf1Sgw return (B_TRUE); 423015e6edf1Sgw } 4231e6ca193dSGeorge Wilson 42325cabbc6bSPrashanth Sreenivasa boolean_t 42335cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd) 42345cabbc6bSPrashanth Sreenivasa { 42355cabbc6bSPrashanth Sreenivasa vdev_ops_t *ops = vd->vdev_ops; 42365cabbc6bSPrashanth Sreenivasa if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 42375cabbc6bSPrashanth Sreenivasa ops == &vdev_missing_ops || ops == &vdev_root_ops) { 42385cabbc6bSPrashanth Sreenivasa return (B_FALSE); 42395cabbc6bSPrashanth Sreenivasa } else { 42405cabbc6bSPrashanth Sreenivasa return (B_TRUE); 42415cabbc6bSPrashanth Sreenivasa } 42425cabbc6bSPrashanth Sreenivasa } 42435cabbc6bSPrashanth Sreenivasa 42444b964adaSGeorge Wilson /* 42454b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 42464b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 42474b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 42484b964adaSGeorge Wilson */ 42494b964adaSGeorge Wilson boolean_t 42504b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 42514b964adaSGeorge Wilson { 42524b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 42534b964adaSGeorge Wilson !vd->vdev_removed) 42544b964adaSGeorge Wilson return (B_TRUE); 42554b964adaSGeorge Wilson 42564b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 42574b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 42584b964adaSGeorge Wilson return (B_TRUE); 42594b964adaSGeorge Wilson 42604b964adaSGeorge Wilson return (B_FALSE); 42614b964adaSGeorge Wilson } 42624b964adaSGeorge Wilson 4263573ca77eSGeorge Wilson /* 4264573ca77eSGeorge Wilson * Expand a vdev if possible. 4265573ca77eSGeorge Wilson */ 4266573ca77eSGeorge Wilson void 4267573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 4268573ca77eSGeorge Wilson { 4269573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 4270573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 427111f6a968SSerapheim Dimitropoulos ASSERT(vdev_is_concrete(vd)); 4272573ca77eSGeorge Wilson 42735cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 42745cabbc6bSPrashanth Sreenivasa 4275*663207adSDon Brady if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 4276*663207adSDon Brady vdev_is_concrete(vd)) { 4277*663207adSDon Brady vdev_metaslab_group_create(vd); 4278573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 4279573ca77eSGeorge Wilson vdev_config_dirty(vd); 4280573ca77eSGeorge Wilson } 4281573ca77eSGeorge Wilson } 42821195e687SMark J Musante 42831195e687SMark J Musante /* 42841195e687SMark J Musante * Split a vdev. 42851195e687SMark J Musante */ 42861195e687SMark J Musante void 42871195e687SMark J Musante vdev_split(vdev_t *vd) 42881195e687SMark J Musante { 42891195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 42901195e687SMark J Musante 42911195e687SMark J Musante vdev_remove_child(pvd, vd); 42921195e687SMark J Musante vdev_compact_children(pvd); 42931195e687SMark J Musante 42941195e687SMark J Musante cvd = pvd->vdev_child[0]; 42951195e687SMark J Musante if (pvd->vdev_children == 1) { 42961195e687SMark J Musante vdev_remove_parent(cvd); 42971195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 42981195e687SMark J Musante } 42991195e687SMark J Musante vdev_propagate_state(cvd); 43001195e687SMark J Musante } 4301283b8460SGeorge.Wilson 4302283b8460SGeorge.Wilson void 4303283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 4304283b8460SGeorge.Wilson { 4305283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 4306283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 4307283b8460SGeorge.Wilson 4308283b8460SGeorge.Wilson vdev_deadman(cvd); 4309283b8460SGeorge.Wilson } 4310283b8460SGeorge.Wilson 4311283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 4312283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 4313283b8460SGeorge.Wilson 4314283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 431569962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 4316283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 4317283b8460SGeorge.Wilson zio_t *fio; 4318283b8460SGeorge.Wilson uint64_t delta; 4319283b8460SGeorge.Wilson 4320283b8460SGeorge.Wilson /* 4321283b8460SGeorge.Wilson * Look at the head of all the pending queues, 4322283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 4323283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 4324283b8460SGeorge.Wilson */ 432569962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 4326c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 4327c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 43283ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 43293ee8c80cSPavel Zakharov "%lluns, delta %lluns, last io %lluns", 43303ee8c80cSPavel Zakharov fio->io_timestamp, (u_longlong_t)delta, 4331283b8460SGeorge.Wilson vq->vq_io_complete_ts); 4332283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 4333283b8460SGeorge.Wilson "hung.", spa_name(spa)); 4334283b8460SGeorge.Wilson } 4335283b8460SGeorge.Wilson } 4336283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 4337283b8460SGeorge.Wilson } 4338283b8460SGeorge.Wilson } 4339