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. 29663207adSDon Brady * Copyright (c) 2017, Intel Corporation. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <sys/zfs_context.h> 33ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 34fa9e4066Sahrens #include <sys/spa.h> 35fa9e4066Sahrens #include <sys/spa_impl.h> 365cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h> 37fa9e4066Sahrens #include <sys/dmu.h> 38fa9e4066Sahrens #include <sys/dmu_tx.h> 395cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h> 40fa9e4066Sahrens #include <sys/vdev_impl.h> 41fa9e4066Sahrens #include <sys/uberblock_impl.h> 42fa9e4066Sahrens #include <sys/metaslab.h> 43fa9e4066Sahrens #include <sys/metaslab_impl.h> 44fa9e4066Sahrens #include <sys/space_map.h> 450713e232SGeorge Wilson #include <sys/space_reftree.h> 46fa9e4066Sahrens #include <sys/zio.h> 47fa9e4066Sahrens #include <sys/zap.h> 48fa9e4066Sahrens #include <sys/fs/zfs.h> 49c5904d13Seschrock #include <sys/arc.h> 50e6ca193dSGeorge Wilson #include <sys/zil.h> 513f9d6ad7SLin Ling #include <sys/dsl_scan.h> 52770499e1SDan Kimmel #include <sys/abd.h> 53094e47e9SGeorge Wilson #include <sys/vdev_initialize.h> 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 196663207adSDon Brady /* 197663207adSDon Brady * Derive the enumerated alloction bias from string input. 198663207adSDon Brady * String origin is either the per-vdev zap or zpool(1M). 199663207adSDon Brady */ 200663207adSDon Brady static vdev_alloc_bias_t 201663207adSDon Brady vdev_derive_alloc_bias(const char *bias) 202663207adSDon Brady { 203663207adSDon Brady vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 204663207adSDon Brady 205663207adSDon Brady if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0) 206663207adSDon Brady alloc_bias = VDEV_BIAS_LOG; 207663207adSDon Brady else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) 208663207adSDon Brady alloc_bias = VDEV_BIAS_SPECIAL; 209663207adSDon Brady else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) 210663207adSDon Brady alloc_bias = VDEV_BIAS_DEDUP; 211663207adSDon Brady 212663207adSDon Brady return (alloc_bias); 213663207adSDon Brady } 214663207adSDon 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); 504*a3874b8bSToomas Soome mutex_init(&vd->vdev_scan_io_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; 538663207adSDon Brady vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 539663207adSDon 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 626663207adSDon Brady /* 627663207adSDon Brady * If creating a top-level vdev, check for allocation classes input 628663207adSDon Brady */ 629663207adSDon Brady if (top_level && alloctype == VDEV_ALLOC_ADD) { 630663207adSDon Brady char *bias; 631663207adSDon Brady 632663207adSDon Brady if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 633663207adSDon Brady &bias) == 0) { 634663207adSDon Brady alloc_bias = vdev_derive_alloc_bias(bias); 635663207adSDon Brady 636663207adSDon Brady /* spa_vdev_add() expects feature to be enabled */ 637663207adSDon Brady if (spa->spa_load_state != SPA_LOAD_CREATE && 638663207adSDon Brady !spa_feature_is_enabled(spa, 639663207adSDon Brady SPA_FEATURE_ALLOCATION_CLASSES)) { 640663207adSDon Brady return (SET_ERROR(ENOTSUP)); 641663207adSDon Brady } 642663207adSDon Brady } 643663207adSDon Brady } 644663207adSDon 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; 650663207adSDon Brady if (top_level && alloc_bias != VDEV_BIAS_NONE) 651663207adSDon 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 */ 702663207adSDon 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 718663207adSDon 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); 723663207adSDon 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 805*a3874b8bSToomas Soome /* 806*a3874b8bSToomas Soome * Scan queues are normally destroyed at the end of a scan. If the 807*a3874b8bSToomas Soome * queue exists here, that implies the vdev is being removed while 808*a3874b8bSToomas Soome * the scan is still running. 809*a3874b8bSToomas Soome */ 810*a3874b8bSToomas Soome if (vd->vdev_scan_io_queue != NULL) { 811*a3874b8bSToomas Soome mutex_enter(&vd->vdev_scan_io_queue_lock); 812*a3874b8bSToomas Soome dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue); 813*a3874b8bSToomas Soome vd->vdev_scan_io_queue = NULL; 814*a3874b8bSToomas Soome mutex_exit(&vd->vdev_scan_io_queue_lock); 815*a3874b8bSToomas Soome } 816*a3874b8bSToomas Soome 817fa9e4066Sahrens /* 818fa9e4066Sahrens * vdev_free() implies closing the vdev first. This is simpler than 819fa9e4066Sahrens * trying to ensure complicated semantics for all callers. 820fa9e4066Sahrens */ 821fa9e4066Sahrens vdev_close(vd); 822fa9e4066Sahrens 823e14bb325SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 824b24ab676SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 825fa9e4066Sahrens 826fa9e4066Sahrens /* 827fa9e4066Sahrens * Free all children. 828fa9e4066Sahrens */ 829573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 830fa9e4066Sahrens vdev_free(vd->vdev_child[c]); 831fa9e4066Sahrens 832fa9e4066Sahrens ASSERT(vd->vdev_child == NULL); 833fa9e4066Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 834094e47e9SGeorge Wilson ASSERT(vd->vdev_initialize_thread == NULL); 835fa9e4066Sahrens 836fa9e4066Sahrens /* 837fa9e4066Sahrens * Discard allocation state. 838fa9e4066Sahrens */ 839a1521560SJeff Bonwick if (vd->vdev_mg != NULL) { 840fa9e4066Sahrens vdev_metaslab_fini(vd); 841a1521560SJeff Bonwick metaslab_group_destroy(vd->vdev_mg); 842a1521560SJeff Bonwick } 843fa9e4066Sahrens 844fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_space); 845fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_dspace); 846fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 847fa9e4066Sahrens 848fa9e4066Sahrens /* 849fa9e4066Sahrens * Remove this vdev from its parent's child list. 850fa9e4066Sahrens */ 851fa9e4066Sahrens vdev_remove_child(vd->vdev_parent, vd); 852fa9e4066Sahrens 853fa9e4066Sahrens ASSERT(vd->vdev_parent == NULL); 854e0f1c0afSOlaf Faaland ASSERT(!list_link_active(&vd->vdev_leaf_node)); 855fa9e4066Sahrens 8563d7072f8Seschrock /* 8573d7072f8Seschrock * Clean up vdev structure. 8583d7072f8Seschrock */ 8593d7072f8Seschrock vdev_queue_fini(vd); 8603d7072f8Seschrock vdev_cache_fini(vd); 8613d7072f8Seschrock 8623d7072f8Seschrock if (vd->vdev_path) 8633d7072f8Seschrock spa_strfree(vd->vdev_path); 8643d7072f8Seschrock if (vd->vdev_devid) 8653d7072f8Seschrock spa_strfree(vd->vdev_devid); 8663d7072f8Seschrock if (vd->vdev_physpath) 8673d7072f8Seschrock spa_strfree(vd->vdev_physpath); 8686809eb4eSEric Schrock if (vd->vdev_fru) 8696809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 8703d7072f8Seschrock 8713d7072f8Seschrock if (vd->vdev_isspare) 8723d7072f8Seschrock spa_spare_remove(vd); 873fa94a07fSbrendan if (vd->vdev_isl2cache) 874fa94a07fSbrendan spa_l2cache_remove(vd); 8753d7072f8Seschrock 8763d7072f8Seschrock txg_list_destroy(&vd->vdev_ms_list); 8773d7072f8Seschrock txg_list_destroy(&vd->vdev_dtl_list); 8788ad4d6ddSJeff Bonwick 8793d7072f8Seschrock mutex_enter(&vd->vdev_dtl_lock); 8800713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 8818ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 8820713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 8830713e232SGeorge Wilson range_tree_destroy(vd->vdev_dtl[t]); 8848ad4d6ddSJeff Bonwick } 8853d7072f8Seschrock mutex_exit(&vd->vdev_dtl_lock); 8868ad4d6ddSJeff Bonwick 8875cabbc6bSPrashanth Sreenivasa EQUIV(vd->vdev_indirect_births != NULL, 8885cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping != NULL); 8895cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_births != NULL) { 8905cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 8915cabbc6bSPrashanth Sreenivasa vdev_indirect_births_close(vd->vdev_indirect_births); 8925cabbc6bSPrashanth Sreenivasa } 8935cabbc6bSPrashanth Sreenivasa 8945cabbc6bSPrashanth Sreenivasa if (vd->vdev_obsolete_sm != NULL) { 8955cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 8965cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 8975cabbc6bSPrashanth Sreenivasa space_map_close(vd->vdev_obsolete_sm); 8985cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_sm = NULL; 8995cabbc6bSPrashanth Sreenivasa } 9005cabbc6bSPrashanth Sreenivasa range_tree_destroy(vd->vdev_obsolete_segments); 9015cabbc6bSPrashanth Sreenivasa rw_destroy(&vd->vdev_indirect_rwlock); 9025cabbc6bSPrashanth Sreenivasa mutex_destroy(&vd->vdev_obsolete_lock); 9035cabbc6bSPrashanth Sreenivasa 9043d7072f8Seschrock mutex_destroy(&vd->vdev_dtl_lock); 9053d7072f8Seschrock mutex_destroy(&vd->vdev_stat_lock); 906e14bb325SJeff Bonwick mutex_destroy(&vd->vdev_probe_lock); 907*a3874b8bSToomas Soome mutex_destroy(&vd->vdev_scan_io_queue_lock); 908094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_lock); 909094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_io_lock); 910094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_io_cv); 911094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_cv); 9123d7072f8Seschrock 9133d7072f8Seschrock if (vd == spa->spa_root_vdev) 9143d7072f8Seschrock spa->spa_root_vdev = NULL; 9153d7072f8Seschrock 9163d7072f8Seschrock kmem_free(vd, sizeof (vdev_t)); 917fa9e4066Sahrens } 918fa9e4066Sahrens 919fa9e4066Sahrens /* 920fa9e4066Sahrens * Transfer top-level vdev state from svd to tvd. 921fa9e4066Sahrens */ 922fa9e4066Sahrens static void 923fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 924fa9e4066Sahrens { 925fa9e4066Sahrens spa_t *spa = svd->vdev_spa; 926fa9e4066Sahrens metaslab_t *msp; 927fa9e4066Sahrens vdev_t *vd; 928fa9e4066Sahrens int t; 929fa9e4066Sahrens 930fa9e4066Sahrens ASSERT(tvd == tvd->vdev_top); 931fa9e4066Sahrens 932fa9e4066Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 933fa9e4066Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 934fa9e4066Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 935215198a6SJoe Stein tvd->vdev_top_zap = svd->vdev_top_zap; 936fa9e4066Sahrens 937fa9e4066Sahrens svd->vdev_ms_array = 0; 938fa9e4066Sahrens svd->vdev_ms_shift = 0; 939fa9e4066Sahrens svd->vdev_ms_count = 0; 940215198a6SJoe Stein svd->vdev_top_zap = 0; 941fa9e4066Sahrens 942cd0837ccSGeorge Wilson if (tvd->vdev_mg) 943cd0837ccSGeorge Wilson ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 944fa9e4066Sahrens tvd->vdev_mg = svd->vdev_mg; 945fa9e4066Sahrens tvd->vdev_ms = svd->vdev_ms; 946fa9e4066Sahrens 947fa9e4066Sahrens svd->vdev_mg = NULL; 948fa9e4066Sahrens svd->vdev_ms = NULL; 949ecc2d604Sbonwick 950ecc2d604Sbonwick if (tvd->vdev_mg != NULL) 951ecc2d604Sbonwick tvd->vdev_mg->mg_vd = tvd; 952fa9e4066Sahrens 95386714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 95486714001SSerapheim Dimitropoulos svd->vdev_checkpoint_sm = NULL; 95586714001SSerapheim Dimitropoulos 956663207adSDon Brady tvd->vdev_alloc_bias = svd->vdev_alloc_bias; 957663207adSDon Brady svd->vdev_alloc_bias = VDEV_BIAS_NONE; 958663207adSDon Brady 959fa9e4066Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 960fa9e4066Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 96199653d4eSeschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 962fa9e4066Sahrens 963fa9e4066Sahrens svd->vdev_stat.vs_alloc = 0; 964fa9e4066Sahrens svd->vdev_stat.vs_space = 0; 96599653d4eSeschrock svd->vdev_stat.vs_dspace = 0; 966fa9e4066Sahrens 9673a4b1be9SMatthew Ahrens /* 9683a4b1be9SMatthew Ahrens * State which may be set on a top-level vdev that's in the 9693a4b1be9SMatthew Ahrens * process of being removed. 9703a4b1be9SMatthew Ahrens */ 9713a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_births_object); 9723a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 9733a4b1be9SMatthew Ahrens ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 9743a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 9753a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 9763a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 9773a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_removing); 9783a4b1be9SMatthew Ahrens tvd->vdev_removing = svd->vdev_removing; 9793a4b1be9SMatthew Ahrens tvd->vdev_indirect_config = svd->vdev_indirect_config; 9803a4b1be9SMatthew Ahrens tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 9813a4b1be9SMatthew Ahrens tvd->vdev_indirect_births = svd->vdev_indirect_births; 9823a4b1be9SMatthew Ahrens range_tree_swap(&svd->vdev_obsolete_segments, 9833a4b1be9SMatthew Ahrens &tvd->vdev_obsolete_segments); 9843a4b1be9SMatthew Ahrens tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 9853a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_mapping_object = 0; 9863a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_births_object = 0; 9873a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 9883a4b1be9SMatthew Ahrens svd->vdev_indirect_mapping = NULL; 9893a4b1be9SMatthew Ahrens svd->vdev_indirect_births = NULL; 9903a4b1be9SMatthew Ahrens svd->vdev_obsolete_sm = NULL; 9913a4b1be9SMatthew Ahrens svd->vdev_removing = 0; 9923a4b1be9SMatthew Ahrens 993fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 994fa9e4066Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 995fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 996fa9e4066Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 997fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 998fa9e4066Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 999fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 1000fa9e4066Sahrens } 1001fa9e4066Sahrens 1002e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_config_dirty_node)) { 1003fa9e4066Sahrens vdev_config_clean(svd); 1004fa9e4066Sahrens vdev_config_dirty(tvd); 1005fa9e4066Sahrens } 1006fa9e4066Sahrens 1007e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_state_dirty_node)) { 1008e14bb325SJeff Bonwick vdev_state_clean(svd); 1009e14bb325SJeff Bonwick vdev_state_dirty(tvd); 1010e14bb325SJeff Bonwick } 1011e14bb325SJeff Bonwick 101299653d4eSeschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 101399653d4eSeschrock svd->vdev_deflate_ratio = 0; 10148654d025Sperrin 10158654d025Sperrin tvd->vdev_islog = svd->vdev_islog; 10168654d025Sperrin svd->vdev_islog = 0; 1017*a3874b8bSToomas Soome 1018*a3874b8bSToomas Soome dsl_scan_io_queue_vdev_xfer(svd, tvd); 1019fa9e4066Sahrens } 1020fa9e4066Sahrens 1021fa9e4066Sahrens static void 1022fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 1023fa9e4066Sahrens { 1024fa9e4066Sahrens if (vd == NULL) 1025fa9e4066Sahrens return; 1026fa9e4066Sahrens 1027fa9e4066Sahrens vd->vdev_top = tvd; 1028fa9e4066Sahrens 1029573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1030fa9e4066Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 1031fa9e4066Sahrens } 1032fa9e4066Sahrens 1033fa9e4066Sahrens /* 1034fa9e4066Sahrens * Add a mirror/replacing vdev above an existing vdev. 1035fa9e4066Sahrens */ 1036fa9e4066Sahrens vdev_t * 1037fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 1038fa9e4066Sahrens { 1039fa9e4066Sahrens spa_t *spa = cvd->vdev_spa; 1040fa9e4066Sahrens vdev_t *pvd = cvd->vdev_parent; 1041fa9e4066Sahrens vdev_t *mvd; 1042fa9e4066Sahrens 1043e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1044fa9e4066Sahrens 1045fa9e4066Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 1046ecc2d604Sbonwick 1047ecc2d604Sbonwick mvd->vdev_asize = cvd->vdev_asize; 1048573ca77eSGeorge Wilson mvd->vdev_min_asize = cvd->vdev_min_asize; 10494263d13fSGeorge Wilson mvd->vdev_max_asize = cvd->vdev_max_asize; 10505cabbc6bSPrashanth Sreenivasa mvd->vdev_psize = cvd->vdev_psize; 1051ecc2d604Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 1052ecc2d604Sbonwick mvd->vdev_state = cvd->vdev_state; 105388ecc943SGeorge Wilson mvd->vdev_crtxg = cvd->vdev_crtxg; 1054ecc2d604Sbonwick 1055fa9e4066Sahrens vdev_remove_child(pvd, cvd); 1056fa9e4066Sahrens vdev_add_child(pvd, mvd); 1057fa9e4066Sahrens cvd->vdev_id = mvd->vdev_children; 1058fa9e4066Sahrens vdev_add_child(mvd, cvd); 1059fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1060fa9e4066Sahrens 1061fa9e4066Sahrens if (mvd == mvd->vdev_top) 1062fa9e4066Sahrens vdev_top_transfer(cvd, mvd); 1063fa9e4066Sahrens 1064fa9e4066Sahrens return (mvd); 1065fa9e4066Sahrens } 1066fa9e4066Sahrens 1067fa9e4066Sahrens /* 1068fa9e4066Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 1069fa9e4066Sahrens */ 1070fa9e4066Sahrens void 1071fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd) 1072fa9e4066Sahrens { 1073fa9e4066Sahrens vdev_t *mvd = cvd->vdev_parent; 1074fa9e4066Sahrens vdev_t *pvd = mvd->vdev_parent; 1075fa9e4066Sahrens 1076e14bb325SJeff Bonwick ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1077fa9e4066Sahrens 1078fa9e4066Sahrens ASSERT(mvd->vdev_children == 1); 1079fa9e4066Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 108099653d4eSeschrock mvd->vdev_ops == &vdev_replacing_ops || 108199653d4eSeschrock mvd->vdev_ops == &vdev_spare_ops); 1082ecc2d604Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 1083fa9e4066Sahrens 1084fa9e4066Sahrens vdev_remove_child(mvd, cvd); 1085fa9e4066Sahrens vdev_remove_child(pvd, mvd); 10868ad4d6ddSJeff Bonwick 108799653d4eSeschrock /* 1088e14bb325SJeff Bonwick * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1089e14bb325SJeff Bonwick * Otherwise, we could have detached an offline device, and when we 1090e14bb325SJeff Bonwick * go to import the pool we'll think we have two top-level vdevs, 1091e14bb325SJeff Bonwick * instead of a different version of the same top-level vdev. 109299653d4eSeschrock */ 10938ad4d6ddSJeff Bonwick if (mvd->vdev_top == mvd) { 10948ad4d6ddSJeff Bonwick uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 10951195e687SMark J Musante cvd->vdev_orig_guid = cvd->vdev_guid; 10968ad4d6ddSJeff Bonwick cvd->vdev_guid += guid_delta; 10978ad4d6ddSJeff Bonwick cvd->vdev_guid_sum += guid_delta; 10988ad4d6ddSJeff Bonwick } 1099e14bb325SJeff Bonwick cvd->vdev_id = mvd->vdev_id; 1100e14bb325SJeff Bonwick vdev_add_child(pvd, cvd); 1101fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1102fa9e4066Sahrens 1103fa9e4066Sahrens if (cvd == cvd->vdev_top) 1104fa9e4066Sahrens vdev_top_transfer(mvd, cvd); 1105fa9e4066Sahrens 1106fa9e4066Sahrens ASSERT(mvd->vdev_children == 0); 1107fa9e4066Sahrens vdev_free(mvd); 1108fa9e4066Sahrens } 1109fa9e4066Sahrens 1110663207adSDon Brady static void 1111663207adSDon Brady vdev_metaslab_group_create(vdev_t *vd) 1112663207adSDon Brady { 1113663207adSDon Brady spa_t *spa = vd->vdev_spa; 1114663207adSDon Brady 1115663207adSDon Brady /* 1116663207adSDon Brady * metaslab_group_create was delayed until allocation bias was available 1117663207adSDon Brady */ 1118663207adSDon Brady if (vd->vdev_mg == NULL) { 1119663207adSDon Brady metaslab_class_t *mc; 1120663207adSDon Brady 1121663207adSDon Brady if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE) 1122663207adSDon Brady vd->vdev_alloc_bias = VDEV_BIAS_LOG; 1123663207adSDon Brady 1124663207adSDon Brady ASSERT3U(vd->vdev_islog, ==, 1125663207adSDon Brady (vd->vdev_alloc_bias == VDEV_BIAS_LOG)); 1126663207adSDon Brady 1127663207adSDon Brady switch (vd->vdev_alloc_bias) { 1128663207adSDon Brady case VDEV_BIAS_LOG: 1129663207adSDon Brady mc = spa_log_class(spa); 1130663207adSDon Brady break; 1131663207adSDon Brady case VDEV_BIAS_SPECIAL: 1132663207adSDon Brady mc = spa_special_class(spa); 1133663207adSDon Brady break; 1134663207adSDon Brady case VDEV_BIAS_DEDUP: 1135663207adSDon Brady mc = spa_dedup_class(spa); 1136663207adSDon Brady break; 1137663207adSDon Brady default: 1138663207adSDon Brady mc = spa_normal_class(spa); 1139663207adSDon Brady } 1140663207adSDon Brady 1141663207adSDon Brady vd->vdev_mg = metaslab_group_create(mc, vd, 1142663207adSDon Brady spa->spa_alloc_count); 1143663207adSDon Brady 1144663207adSDon Brady /* 1145663207adSDon Brady * The spa ashift values currently only reflect the 1146663207adSDon Brady * general vdev classes. Class destination is late 1147663207adSDon Brady * binding so ashift checking had to wait until now 1148663207adSDon Brady */ 1149663207adSDon Brady if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1150663207adSDon Brady mc == spa_normal_class(spa) && vd->vdev_aux == NULL) { 1151663207adSDon Brady if (vd->vdev_ashift > spa->spa_max_ashift) 1152663207adSDon Brady spa->spa_max_ashift = vd->vdev_ashift; 1153663207adSDon Brady if (vd->vdev_ashift < spa->spa_min_ashift) 1154663207adSDon Brady spa->spa_min_ashift = vd->vdev_ashift; 1155663207adSDon Brady } 1156663207adSDon Brady } 1157663207adSDon Brady } 1158663207adSDon Brady 1159ea8dc4b6Seschrock int 1160fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1161fa9e4066Sahrens { 1162fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 1163ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 1164ecc2d604Sbonwick uint64_t m; 1165fa9e4066Sahrens uint64_t oldc = vd->vdev_ms_count; 1166fa9e4066Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1167ecc2d604Sbonwick metaslab_t **mspp; 1168ecc2d604Sbonwick int error; 1169663207adSDon Brady boolean_t expanding = (oldc != 0); 1170fa9e4066Sahrens 1171a1521560SJeff Bonwick ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1172a1521560SJeff Bonwick 117388ecc943SGeorge Wilson /* 117488ecc943SGeorge Wilson * This vdev is not being allocated from yet or is a hole. 117588ecc943SGeorge Wilson */ 117688ecc943SGeorge Wilson if (vd->vdev_ms_shift == 0) 11770e34b6a7Sbonwick return (0); 11780e34b6a7Sbonwick 117988ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 118088ecc943SGeorge Wilson 1181fa9e4066Sahrens ASSERT(oldc <= newc); 1182fa9e4066Sahrens 1183ecc2d604Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1184fa9e4066Sahrens 1185663207adSDon Brady if (expanding) { 1186ecc2d604Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1187ecc2d604Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1188ecc2d604Sbonwick } 1189fa9e4066Sahrens 1190ecc2d604Sbonwick vd->vdev_ms = mspp; 1191ecc2d604Sbonwick vd->vdev_ms_count = newc; 1192ecc2d604Sbonwick for (m = oldc; m < newc; m++) { 11930713e232SGeorge Wilson uint64_t object = 0; 11940713e232SGeorge Wilson 11955cabbc6bSPrashanth Sreenivasa /* 11965cabbc6bSPrashanth Sreenivasa * vdev_ms_array may be 0 if we are creating the "fake" 11975cabbc6bSPrashanth Sreenivasa * metaslabs for an indirect vdev for zdb's leak detection. 11985cabbc6bSPrashanth Sreenivasa * See zdb_leak_init(). 11995cabbc6bSPrashanth Sreenivasa */ 12005cabbc6bSPrashanth Sreenivasa if (txg == 0 && vd->vdev_ms_array != 0) { 1201ecc2d604Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 12027bfdf011SNeil Perrin m * sizeof (uint64_t), sizeof (uint64_t), &object, 12037bfdf011SNeil Perrin DMU_READ_PREFETCH); 12043ee8c80cSPavel Zakharov if (error != 0) { 12053ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "unable to read the metaslab " 12063ee8c80cSPavel Zakharov "array [error=%d]", error); 1207ecc2d604Sbonwick return (error); 12083ee8c80cSPavel Zakharov } 1209fa9e4066Sahrens } 12101e9bd7ecSPrakash Surya 1211663207adSDon Brady #ifndef _KERNEL 1212663207adSDon Brady /* 1213663207adSDon Brady * To accomodate zdb_leak_init() fake indirect 1214663207adSDon Brady * metaslabs, we allocate a metaslab group for 1215663207adSDon Brady * indirect vdevs which normally don't have one. 1216663207adSDon Brady */ 1217663207adSDon Brady if (vd->vdev_mg == NULL) { 1218663207adSDon Brady ASSERT0(vdev_is_concrete(vd)); 1219663207adSDon Brady vdev_metaslab_group_create(vd); 1220663207adSDon Brady } 1221663207adSDon Brady #endif 12221e9bd7ecSPrakash Surya error = metaslab_init(vd->vdev_mg, m, object, txg, 12231e9bd7ecSPrakash Surya &(vd->vdev_ms[m])); 12243ee8c80cSPavel Zakharov if (error != 0) { 12253ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 12263ee8c80cSPavel Zakharov error); 12271e9bd7ecSPrakash Surya return (error); 12283ee8c80cSPavel Zakharov } 1229fa9e4066Sahrens } 1230fa9e4066Sahrens 1231a1521560SJeff Bonwick if (txg == 0) 1232a1521560SJeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1233a1521560SJeff Bonwick 12343f9d6ad7SLin Ling /* 12353f9d6ad7SLin Ling * If the vdev is being removed we don't activate 12363f9d6ad7SLin Ling * the metaslabs since we want to ensure that no new 12373f9d6ad7SLin Ling * allocations are performed on this device. 12383f9d6ad7SLin Ling */ 1239663207adSDon Brady if (!expanding && !vd->vdev_removing) { 1240a1521560SJeff Bonwick metaslab_group_activate(vd->vdev_mg); 1241663207adSDon Brady } 1242a1521560SJeff Bonwick 1243a1521560SJeff Bonwick if (txg == 0) 1244a1521560SJeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG); 1245a1521560SJeff Bonwick 1246ea8dc4b6Seschrock return (0); 1247fa9e4066Sahrens } 1248fa9e4066Sahrens 1249fa9e4066Sahrens void 1250fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd) 1251fa9e4066Sahrens { 125286714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 125386714001SSerapheim Dimitropoulos ASSERT(spa_feature_is_active(vd->vdev_spa, 125486714001SSerapheim Dimitropoulos SPA_FEATURE_POOL_CHECKPOINT)); 125586714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 125686714001SSerapheim Dimitropoulos /* 125786714001SSerapheim Dimitropoulos * Even though we close the space map, we need to set its 125886714001SSerapheim Dimitropoulos * pointer to NULL. The reason is that vdev_metaslab_fini() 125986714001SSerapheim Dimitropoulos * may be called multiple times for certain operations 126086714001SSerapheim Dimitropoulos * (i.e. when destroying a pool) so we need to ensure that 126186714001SSerapheim Dimitropoulos * this clause never executes twice. This logic is similar 126286714001SSerapheim Dimitropoulos * to the one used for the vdev_ms clause below. 126386714001SSerapheim Dimitropoulos */ 126486714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 126586714001SSerapheim Dimitropoulos } 126686714001SSerapheim Dimitropoulos 1267fa9e4066Sahrens if (vd->vdev_ms != NULL) { 1268555d674dSSerapheim Dimitropoulos metaslab_group_t *mg = vd->vdev_mg; 1269555d674dSSerapheim Dimitropoulos metaslab_group_passivate(mg); 12705cabbc6bSPrashanth Sreenivasa 1271555d674dSSerapheim Dimitropoulos uint64_t count = vd->vdev_ms_count; 12725cabbc6bSPrashanth Sreenivasa for (uint64_t m = 0; m < count; m++) { 12730713e232SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 12740713e232SGeorge Wilson if (msp != NULL) 12750713e232SGeorge Wilson metaslab_fini(msp); 12760713e232SGeorge Wilson } 1277fa9e4066Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1278fa9e4066Sahrens vd->vdev_ms = NULL; 12795cabbc6bSPrashanth Sreenivasa 12805cabbc6bSPrashanth Sreenivasa vd->vdev_ms_count = 0; 1281555d674dSSerapheim Dimitropoulos 1282555d674dSSerapheim Dimitropoulos for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 1283555d674dSSerapheim Dimitropoulos ASSERT0(mg->mg_histogram[i]); 1284fa9e4066Sahrens } 12855cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_ms_count); 1286fa9e4066Sahrens } 1287fa9e4066Sahrens 1288e14bb325SJeff Bonwick typedef struct vdev_probe_stats { 1289e14bb325SJeff Bonwick boolean_t vps_readable; 1290e14bb325SJeff Bonwick boolean_t vps_writeable; 1291e14bb325SJeff Bonwick int vps_flags; 1292e14bb325SJeff Bonwick } vdev_probe_stats_t; 1293e14bb325SJeff Bonwick 1294e14bb325SJeff Bonwick static void 1295e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio) 12960a4e9518Sgw { 12978ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 1298a3f829aeSBill Moore vdev_t *vd = zio->io_vd; 1299e14bb325SJeff Bonwick vdev_probe_stats_t *vps = zio->io_private; 1300a3f829aeSBill Moore 1301a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio != NULL); 1302e14bb325SJeff Bonwick 1303e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) { 1304e14bb325SJeff Bonwick if (zio->io_error == 0) 1305e14bb325SJeff Bonwick vps->vps_readable = 1; 13068ad4d6ddSJeff Bonwick if (zio->io_error == 0 && spa_writeable(spa)) { 1307a3f829aeSBill Moore zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1308770499e1SDan Kimmel zio->io_offset, zio->io_size, zio->io_abd, 1309e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1310e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1311e14bb325SJeff Bonwick } else { 1312770499e1SDan Kimmel abd_free(zio->io_abd); 1313e14bb325SJeff Bonwick } 1314e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_WRITE) { 1315e14bb325SJeff Bonwick if (zio->io_error == 0) 1316e14bb325SJeff Bonwick vps->vps_writeable = 1; 1317770499e1SDan Kimmel abd_free(zio->io_abd); 1318e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_NULL) { 1319a3f829aeSBill Moore zio_t *pio; 1320e14bb325SJeff Bonwick 1321e14bb325SJeff Bonwick vd->vdev_cant_read |= !vps->vps_readable; 1322e14bb325SJeff Bonwick vd->vdev_cant_write |= !vps->vps_writeable; 1323e14bb325SJeff Bonwick 1324e14bb325SJeff Bonwick if (vdev_readable(vd) && 13258ad4d6ddSJeff Bonwick (vdev_writeable(vd) || !spa_writeable(spa))) { 1326e14bb325SJeff Bonwick zio->io_error = 0; 1327e14bb325SJeff Bonwick } else { 1328e14bb325SJeff Bonwick ASSERT(zio->io_error != 0); 13293ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "failed probe"); 1330e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 13318ad4d6ddSJeff Bonwick spa, vd, NULL, 0, 0); 1332be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 1333e14bb325SJeff Bonwick } 1334a3f829aeSBill Moore 1335a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1336a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio == zio); 1337a3f829aeSBill Moore vd->vdev_probe_zio = NULL; 1338a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1339a3f829aeSBill Moore 13400f7643c7SGeorge Wilson zio_link_t *zl = NULL; 13410f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1342a3f829aeSBill Moore if (!vdev_accessible(vd, pio)) 1343be6fd75aSMatthew Ahrens pio->io_error = SET_ERROR(ENXIO); 1344a3f829aeSBill Moore 1345e14bb325SJeff Bonwick kmem_free(vps, sizeof (*vps)); 1346e14bb325SJeff Bonwick } 1347e14bb325SJeff Bonwick } 13480a4e9518Sgw 1349e14bb325SJeff Bonwick /* 1350f7170741SWill Andrews * Determine whether this device is accessible. 1351f7170741SWill Andrews * 1352f7170741SWill Andrews * Read and write to several known locations: the pad regions of each 1353f7170741SWill Andrews * vdev label but the first, which we leave alone in case it contains 1354f7170741SWill Andrews * a VTOC. 1355e14bb325SJeff Bonwick */ 1356e14bb325SJeff Bonwick zio_t * 1357a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio) 1358e14bb325SJeff Bonwick { 1359e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 1360a3f829aeSBill Moore vdev_probe_stats_t *vps = NULL; 1361a3f829aeSBill Moore zio_t *pio; 1362a3f829aeSBill Moore 1363a3f829aeSBill Moore ASSERT(vd->vdev_ops->vdev_op_leaf); 13640a4e9518Sgw 1365a3f829aeSBill Moore /* 1366a3f829aeSBill Moore * Don't probe the probe. 1367a3f829aeSBill Moore */ 1368a3f829aeSBill Moore if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1369a3f829aeSBill Moore return (NULL); 1370e14bb325SJeff Bonwick 1371a3f829aeSBill Moore /* 1372a3f829aeSBill Moore * To prevent 'probe storms' when a device fails, we create 1373a3f829aeSBill Moore * just one probe i/o at a time. All zios that want to probe 1374a3f829aeSBill Moore * this vdev will become parents of the probe io. 1375a3f829aeSBill Moore */ 1376a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1377e14bb325SJeff Bonwick 1378a3f829aeSBill Moore if ((pio = vd->vdev_probe_zio) == NULL) { 1379a3f829aeSBill Moore vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1380a3f829aeSBill Moore 1381a3f829aeSBill Moore vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1382a3f829aeSBill Moore ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 13838956713aSEric Schrock ZIO_FLAG_TRYHARD; 1384a3f829aeSBill Moore 1385a3f829aeSBill Moore if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1386a3f829aeSBill Moore /* 1387a3f829aeSBill Moore * vdev_cant_read and vdev_cant_write can only 1388a3f829aeSBill Moore * transition from TRUE to FALSE when we have the 1389a3f829aeSBill Moore * SCL_ZIO lock as writer; otherwise they can only 1390a3f829aeSBill Moore * transition from FALSE to TRUE. This ensures that 1391a3f829aeSBill Moore * any zio looking at these values can assume that 1392a3f829aeSBill Moore * failures persist for the life of the I/O. That's 1393a3f829aeSBill Moore * important because when a device has intermittent 1394a3f829aeSBill Moore * connectivity problems, we want to ensure that 1395a3f829aeSBill Moore * they're ascribed to the device (ENXIO) and not 1396a3f829aeSBill Moore * the zio (EIO). 1397a3f829aeSBill Moore * 1398a3f829aeSBill Moore * Since we hold SCL_ZIO as writer here, clear both 1399a3f829aeSBill Moore * values so the probe can reevaluate from first 1400a3f829aeSBill Moore * principles. 1401a3f829aeSBill Moore */ 1402a3f829aeSBill Moore vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1403a3f829aeSBill Moore vd->vdev_cant_read = B_FALSE; 1404a3f829aeSBill Moore vd->vdev_cant_write = B_FALSE; 1405a3f829aeSBill Moore } 1406a3f829aeSBill Moore 1407a3f829aeSBill Moore vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1408a3f829aeSBill Moore vdev_probe_done, vps, 1409a3f829aeSBill Moore vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1410a3f829aeSBill Moore 141198d1cbfeSGeorge Wilson /* 141298d1cbfeSGeorge Wilson * We can't change the vdev state in this context, so we 141398d1cbfeSGeorge Wilson * kick off an async task to do it on our behalf. 141498d1cbfeSGeorge Wilson */ 1415a3f829aeSBill Moore if (zio != NULL) { 1416a3f829aeSBill Moore vd->vdev_probe_wanted = B_TRUE; 1417a3f829aeSBill Moore spa_async_request(spa, SPA_ASYNC_PROBE); 1418a3f829aeSBill Moore } 1419e14bb325SJeff Bonwick } 1420e14bb325SJeff Bonwick 1421a3f829aeSBill Moore if (zio != NULL) 1422a3f829aeSBill Moore zio_add_child(zio, pio); 1423e14bb325SJeff Bonwick 1424a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1425e14bb325SJeff Bonwick 1426a3f829aeSBill Moore if (vps == NULL) { 1427a3f829aeSBill Moore ASSERT(zio != NULL); 1428a3f829aeSBill Moore return (NULL); 1429a3f829aeSBill Moore } 1430e14bb325SJeff Bonwick 1431e14bb325SJeff Bonwick for (int l = 1; l < VDEV_LABELS; l++) { 1432a3f829aeSBill Moore zio_nowait(zio_read_phys(pio, vd, 1433e14bb325SJeff Bonwick vdev_label_offset(vd->vdev_psize, l, 1434770499e1SDan Kimmel offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1435770499e1SDan Kimmel abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1436e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1437e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1438e14bb325SJeff Bonwick } 1439e14bb325SJeff Bonwick 1440a3f829aeSBill Moore if (zio == NULL) 1441a3f829aeSBill Moore return (pio); 1442a3f829aeSBill Moore 1443a3f829aeSBill Moore zio_nowait(pio); 1444a3f829aeSBill Moore return (NULL); 14450a4e9518Sgw } 14460a4e9518Sgw 1447f64c0e34SEric Taylor static void 1448f64c0e34SEric Taylor vdev_open_child(void *arg) 1449f64c0e34SEric Taylor { 1450f64c0e34SEric Taylor vdev_t *vd = arg; 1451f64c0e34SEric Taylor 1452f64c0e34SEric Taylor vd->vdev_open_thread = curthread; 1453f64c0e34SEric Taylor vd->vdev_open_error = vdev_open(vd); 1454f64c0e34SEric Taylor vd->vdev_open_thread = NULL; 1455f64c0e34SEric Taylor } 1456f64c0e34SEric Taylor 1457681d9761SEric Taylor boolean_t 1458681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd) 1459681d9761SEric Taylor { 1460681d9761SEric Taylor if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1461681d9761SEric Taylor strlen(ZVOL_DIR)) == 0) 1462681d9761SEric Taylor return (B_TRUE); 1463681d9761SEric Taylor for (int c = 0; c < vd->vdev_children; c++) 1464681d9761SEric Taylor if (vdev_uses_zvols(vd->vdev_child[c])) 1465681d9761SEric Taylor return (B_TRUE); 1466681d9761SEric Taylor return (B_FALSE); 1467681d9761SEric Taylor } 1468681d9761SEric Taylor 1469f64c0e34SEric Taylor void 1470f64c0e34SEric Taylor vdev_open_children(vdev_t *vd) 1471f64c0e34SEric Taylor { 1472f64c0e34SEric Taylor taskq_t *tq; 1473f64c0e34SEric Taylor int children = vd->vdev_children; 1474f64c0e34SEric Taylor 1475681d9761SEric Taylor /* 1476681d9761SEric Taylor * in order to handle pools on top of zvols, do the opens 1477681d9761SEric Taylor * in a single thread so that the same thread holds the 1478681d9761SEric Taylor * spa_namespace_lock 1479681d9761SEric Taylor */ 1480681d9761SEric Taylor if (vdev_uses_zvols(vd)) { 1481681d9761SEric Taylor for (int c = 0; c < children; c++) 1482681d9761SEric Taylor vd->vdev_child[c]->vdev_open_error = 1483681d9761SEric Taylor vdev_open(vd->vdev_child[c]); 1484681d9761SEric Taylor return; 1485681d9761SEric Taylor } 1486f64c0e34SEric Taylor tq = taskq_create("vdev_open", children, minclsyspri, 1487f64c0e34SEric Taylor children, children, TASKQ_PREPOPULATE); 1488f64c0e34SEric Taylor 1489f64c0e34SEric Taylor for (int c = 0; c < children; c++) 1490f64c0e34SEric Taylor VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1491fc8ae2ecSToomas Soome TQ_SLEEP) != TASKQID_INVALID); 1492f64c0e34SEric Taylor 1493f64c0e34SEric Taylor taskq_destroy(tq); 1494f64c0e34SEric Taylor } 1495f64c0e34SEric Taylor 14965cabbc6bSPrashanth Sreenivasa /* 14975cabbc6bSPrashanth Sreenivasa * Compute the raidz-deflation ratio. Note, we hard-code 14985cabbc6bSPrashanth Sreenivasa * in 128k (1 << 17) because it is the "typical" blocksize. 14995cabbc6bSPrashanth Sreenivasa * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 15005cabbc6bSPrashanth Sreenivasa * otherwise it would inconsistently account for existing bp's. 15015cabbc6bSPrashanth Sreenivasa */ 15025cabbc6bSPrashanth Sreenivasa static void 15035cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd) 15045cabbc6bSPrashanth Sreenivasa { 15055cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 15065cabbc6bSPrashanth Sreenivasa vd->vdev_deflate_ratio = (1 << 17) / 15075cabbc6bSPrashanth Sreenivasa (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 15085cabbc6bSPrashanth Sreenivasa } 15095cabbc6bSPrashanth Sreenivasa } 15105cabbc6bSPrashanth Sreenivasa 1511fa9e4066Sahrens /* 1512fa9e4066Sahrens * Prepare a virtual device for access. 1513fa9e4066Sahrens */ 1514fa9e4066Sahrens int 1515fa9e4066Sahrens vdev_open(vdev_t *vd) 1516fa9e4066Sahrens { 15178ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1518fa9e4066Sahrens int error; 1519fa9e4066Sahrens uint64_t osize = 0; 15204263d13fSGeorge Wilson uint64_t max_osize = 0; 15214263d13fSGeorge Wilson uint64_t asize, max_asize, psize; 1522ecc2d604Sbonwick uint64_t ashift = 0; 1523fa9e4066Sahrens 1524f64c0e34SEric Taylor ASSERT(vd->vdev_open_thread == curthread || 1525f64c0e34SEric Taylor spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1526fa9e4066Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1527fa9e4066Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1528fa9e4066Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1529fa9e4066Sahrens 1530fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1531e6ca193dSGeorge Wilson vd->vdev_cant_read = B_FALSE; 1532e6ca193dSGeorge Wilson vd->vdev_cant_write = B_FALSE; 1533573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 1534fa9e4066Sahrens 1535069f55e2SEric Schrock /* 1536069f55e2SEric Schrock * If this vdev is not removed, check its fault status. If it's 1537069f55e2SEric Schrock * faulted, bail out of the open. 1538069f55e2SEric Schrock */ 15393d7072f8Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 15403d7072f8Seschrock ASSERT(vd->vdev_children == 0); 1541069f55e2SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1542069f55e2SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 15433d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1544069f55e2SEric Schrock vd->vdev_label_aux); 1545be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15463d7072f8Seschrock } else if (vd->vdev_offline) { 1547fa9e4066Sahrens ASSERT(vd->vdev_children == 0); 1548ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1549be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1550fa9e4066Sahrens } 1551fa9e4066Sahrens 15524263d13fSGeorge Wilson error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1553fa9e4066Sahrens 1554095bcd66SGeorge Wilson /* 1555095bcd66SGeorge Wilson * Reset the vdev_reopening flag so that we actually close 1556095bcd66SGeorge Wilson * the vdev on error. 1557095bcd66SGeorge Wilson */ 1558095bcd66SGeorge Wilson vd->vdev_reopening = B_FALSE; 1559ea8dc4b6Seschrock if (zio_injection_enabled && error == 0) 15608956713aSEric Schrock error = zio_handle_device_injection(vd, NULL, ENXIO); 1561ea8dc4b6Seschrock 1562fa9e4066Sahrens if (error) { 15633d7072f8Seschrock if (vd->vdev_removed && 15643d7072f8Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 15653d7072f8Seschrock vd->vdev_removed = B_FALSE; 15663d7072f8Seschrock 15676f793812SPavel Zakharov if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 15686f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 15696f793812SPavel Zakharov vd->vdev_stat.vs_aux); 15706f793812SPavel Zakharov } else { 15716f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 15726f793812SPavel Zakharov vd->vdev_stat.vs_aux); 15736f793812SPavel Zakharov } 1574fa9e4066Sahrens return (error); 1575fa9e4066Sahrens } 1576fa9e4066Sahrens 15773d7072f8Seschrock vd->vdev_removed = B_FALSE; 15783d7072f8Seschrock 1579096d22d4SEric Schrock /* 1580096d22d4SEric Schrock * Recheck the faulted flag now that we have confirmed that 1581096d22d4SEric Schrock * the vdev is accessible. If we're faulted, bail. 1582096d22d4SEric Schrock */ 1583096d22d4SEric Schrock if (vd->vdev_faulted) { 1584096d22d4SEric Schrock ASSERT(vd->vdev_children == 0); 1585096d22d4SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1586096d22d4SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1587096d22d4SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1588096d22d4SEric Schrock vd->vdev_label_aux); 1589be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1590096d22d4SEric Schrock } 1591096d22d4SEric Schrock 15923d7072f8Seschrock if (vd->vdev_degraded) { 15933d7072f8Seschrock ASSERT(vd->vdev_children == 0); 15943d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 15953d7072f8Seschrock VDEV_AUX_ERR_EXCEEDED); 15963d7072f8Seschrock } else { 1597069f55e2SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 15983d7072f8Seschrock } 1599fa9e4066Sahrens 160088ecc943SGeorge Wilson /* 160188ecc943SGeorge Wilson * For hole or missing vdevs we just return success. 160288ecc943SGeorge Wilson */ 160388ecc943SGeorge Wilson if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 160488ecc943SGeorge Wilson return (0); 160588ecc943SGeorge Wilson 1606573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 1607ea8dc4b6Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1608ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1609ea8dc4b6Seschrock VDEV_AUX_NONE); 1610ea8dc4b6Seschrock break; 1611ea8dc4b6Seschrock } 1612573ca77eSGeorge Wilson } 1613fa9e4066Sahrens 1614fa9e4066Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 16154263d13fSGeorge Wilson max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1616fa9e4066Sahrens 1617fa9e4066Sahrens if (vd->vdev_children == 0) { 1618fa9e4066Sahrens if (osize < SPA_MINDEVSIZE) { 1619ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1620ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1621be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1622fa9e4066Sahrens } 1623fa9e4066Sahrens psize = osize; 1624fa9e4066Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 16254263d13fSGeorge Wilson max_asize = max_osize - (VDEV_LABEL_START_SIZE + 16264263d13fSGeorge Wilson VDEV_LABEL_END_SIZE); 1627fa9e4066Sahrens } else { 1628ecc2d604Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1629fa9e4066Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1630ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1631ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1632be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1633fa9e4066Sahrens } 1634fa9e4066Sahrens psize = 0; 1635fa9e4066Sahrens asize = osize; 16364263d13fSGeorge Wilson max_asize = max_osize; 1637fa9e4066Sahrens } 1638fa9e4066Sahrens 1639fa9e4066Sahrens vd->vdev_psize = psize; 1640fa9e4066Sahrens 1641573ca77eSGeorge Wilson /* 1642c040c10cSSteven Hartland * Make sure the allocatable size hasn't shrunk too much. 1643573ca77eSGeorge Wilson */ 1644573ca77eSGeorge Wilson if (asize < vd->vdev_min_asize) { 1645573ca77eSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1646573ca77eSGeorge Wilson VDEV_AUX_BAD_LABEL); 1647be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1648573ca77eSGeorge Wilson } 1649573ca77eSGeorge Wilson 1650fa9e4066Sahrens if (vd->vdev_asize == 0) { 1651fa9e4066Sahrens /* 1652fa9e4066Sahrens * This is the first-ever open, so use the computed values. 1653ecc2d604Sbonwick * For testing purposes, a higher ashift can be requested. 1654fa9e4066Sahrens */ 1655fa9e4066Sahrens vd->vdev_asize = asize; 16564263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1657ecc2d604Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 165893a1902eSMatthew Ahrens vd->vdev_ashift = MAX(zfs_ashift_min, vd->vdev_ashift); 1659fa9e4066Sahrens } else { 1660fa9e4066Sahrens /* 16612384d9f8SGeorge Wilson * Detect if the alignment requirement has increased. 16622384d9f8SGeorge Wilson * We don't want to make the pool unavailable, just 16632384d9f8SGeorge Wilson * issue a warning instead. 1664fa9e4066Sahrens */ 16652384d9f8SGeorge Wilson if (ashift > vd->vdev_top->vdev_ashift && 16662384d9f8SGeorge Wilson vd->vdev_ops->vdev_op_leaf) { 16672384d9f8SGeorge Wilson cmn_err(CE_WARN, 16682384d9f8SGeorge Wilson "Disk, '%s', has a block alignment that is " 16692384d9f8SGeorge Wilson "larger than the pool's alignment\n", 16702384d9f8SGeorge Wilson vd->vdev_path); 1671fa9e4066Sahrens } 16724263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1673573ca77eSGeorge Wilson } 1674fa9e4066Sahrens 1675573ca77eSGeorge Wilson /* 1676c040c10cSSteven Hartland * If all children are healthy we update asize if either: 1677c040c10cSSteven Hartland * The asize has increased, due to a device expansion caused by dynamic 1678c040c10cSSteven Hartland * LUN growth or vdev replacement, and automatic expansion is enabled; 1679c040c10cSSteven Hartland * making the additional space available. 1680c040c10cSSteven Hartland * 1681c040c10cSSteven Hartland * The asize has decreased, due to a device shrink usually caused by a 1682c040c10cSSteven Hartland * vdev replace with a smaller device. This ensures that calculations 1683c040c10cSSteven Hartland * based of max_asize and asize e.g. esize are always valid. It's safe 1684c040c10cSSteven Hartland * to do this as we've already validated that asize is greater than 1685c040c10cSSteven Hartland * vdev_min_asize. 1686573ca77eSGeorge Wilson */ 1687c040c10cSSteven Hartland if (vd->vdev_state == VDEV_STATE_HEALTHY && 1688c040c10cSSteven Hartland ((asize > vd->vdev_asize && 1689c040c10cSSteven Hartland (vd->vdev_expanding || spa->spa_autoexpand)) || 1690c040c10cSSteven Hartland (asize < vd->vdev_asize))) 1691573ca77eSGeorge Wilson vd->vdev_asize = asize; 1692fa9e4066Sahrens 1693573ca77eSGeorge Wilson vdev_set_min_asize(vd); 1694fa9e4066Sahrens 16950a4e9518Sgw /* 16960a4e9518Sgw * Ensure we can issue some IO before declaring the 16970a4e9518Sgw * vdev open for business. 16980a4e9518Sgw */ 1699e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 1700e14bb325SJeff Bonwick (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 170198d1cbfeSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 170298d1cbfeSGeorge Wilson VDEV_AUX_ERR_EXCEEDED); 17030a4e9518Sgw return (error); 17040a4e9518Sgw } 17050a4e9518Sgw 170681cd5c55SMatthew Ahrens /* 170781cd5c55SMatthew Ahrens * Track the min and max ashift values for normal data devices. 1708663207adSDon Brady * 1709663207adSDon Brady * DJB - TBD these should perhaps be tracked per allocation class 1710663207adSDon Brady * (e.g. spa_min_ashift is used to round up post compression buffers) 171181cd5c55SMatthew Ahrens */ 171281cd5c55SMatthew Ahrens if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1713663207adSDon Brady vd->vdev_alloc_bias == VDEV_BIAS_NONE && 1714663207adSDon Brady vd->vdev_aux == NULL) { 171581cd5c55SMatthew Ahrens if (vd->vdev_ashift > spa->spa_max_ashift) 171681cd5c55SMatthew Ahrens spa->spa_max_ashift = vd->vdev_ashift; 171781cd5c55SMatthew Ahrens if (vd->vdev_ashift < spa->spa_min_ashift) 171881cd5c55SMatthew Ahrens spa->spa_min_ashift = vd->vdev_ashift; 171981cd5c55SMatthew Ahrens } 172081cd5c55SMatthew Ahrens 1721088f3894Sahrens /* 1722088f3894Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 17238ad4d6ddSJeff Bonwick * resilver. But don't do this if we are doing a reopen for a scrub, 17248ad4d6ddSJeff Bonwick * since this would just restart the scrub we are already doing. 1725088f3894Sahrens */ 17268ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 17278ad4d6ddSJeff Bonwick vdev_resilver_needed(vd, NULL, NULL)) 17288ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1729088f3894Sahrens 1730fa9e4066Sahrens return (0); 1731fa9e4066Sahrens } 1732fa9e4066Sahrens 1733560e6e96Seschrock /* 1734560e6e96Seschrock * Called once the vdevs are all opened, this routine validates the label 17356f793812SPavel Zakharov * contents. This needs to be done before vdev_load() so that we don't 17363d7072f8Seschrock * inadvertently do repair I/Os to the wrong device. 1737560e6e96Seschrock * 1738560e6e96Seschrock * This function will only return failure if one of the vdevs indicates that it 1739560e6e96Seschrock * has since been destroyed or exported. This is only possible if 1740560e6e96Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1741560e6e96Seschrock * will be updated but the function will return 0. 1742560e6e96Seschrock */ 1743560e6e96Seschrock int 17446f793812SPavel Zakharov vdev_validate(vdev_t *vd) 1745560e6e96Seschrock { 1746560e6e96Seschrock spa_t *spa = vd->vdev_spa; 1747560e6e96Seschrock nvlist_t *label; 17486f793812SPavel Zakharov uint64_t guid = 0, aux_guid = 0, top_guid; 1749560e6e96Seschrock uint64_t state; 17506f793812SPavel Zakharov nvlist_t *nvl; 17516f793812SPavel Zakharov uint64_t txg; 1752560e6e96Seschrock 17536f793812SPavel Zakharov if (vdev_validate_skip) 17546f793812SPavel Zakharov return (0); 17556f793812SPavel Zakharov 17566f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 17576f793812SPavel Zakharov if (vdev_validate(vd->vdev_child[c]) != 0) 1758be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1759560e6e96Seschrock 1760b5989ec7Seschrock /* 1761b5989ec7Seschrock * If the device has already failed, or was marked offline, don't do 1762b5989ec7Seschrock * any further validation. Otherwise, label I/O will fail and we will 1763b5989ec7Seschrock * overwrite the previous state. 1764b5989ec7Seschrock */ 17656f793812SPavel Zakharov if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 17666f793812SPavel Zakharov return (0); 1767560e6e96Seschrock 17686f793812SPavel Zakharov /* 17696f793812SPavel Zakharov * If we are performing an extreme rewind, we allow for a label that 17706f793812SPavel Zakharov * was modified at a point after the current txg. 1771d1de72cfSPavel Zakharov * If config lock is not held do not check for the txg. spa_sync could 1772d1de72cfSPavel Zakharov * be updating the vdev's label before updating spa_last_synced_txg. 17736f793812SPavel Zakharov */ 1774d1de72cfSPavel Zakharov if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1775d1de72cfSPavel Zakharov spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 17766f793812SPavel Zakharov txg = UINT64_MAX; 17776f793812SPavel Zakharov else 17786f793812SPavel Zakharov txg = spa_last_synced_txg(spa); 1779560e6e96Seschrock 17806f793812SPavel Zakharov if ((label = vdev_label_read_config(vd, txg)) == NULL) { 17816f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 17826f793812SPavel Zakharov VDEV_AUX_BAD_LABEL); 1783b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1784b6bf6e15SPavel Zakharov "txg %llu", (u_longlong_t)txg); 17856f793812SPavel Zakharov return (0); 17866f793812SPavel Zakharov } 17871195e687SMark J Musante 17886f793812SPavel Zakharov /* 17896f793812SPavel Zakharov * Determine if this vdev has been split off into another 17906f793812SPavel Zakharov * pool. If so, then refuse to open it. 17916f793812SPavel Zakharov */ 17926f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 17936f793812SPavel Zakharov &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 17946f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17956f793812SPavel Zakharov VDEV_AUX_SPLIT_POOL); 17966f793812SPavel Zakharov nvlist_free(label); 17976f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 17986f793812SPavel Zakharov return (0); 17996f793812SPavel Zakharov } 1800560e6e96Seschrock 18016f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 18026f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18036f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18046f793812SPavel Zakharov nvlist_free(label); 18056f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18066f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 18076f793812SPavel Zakharov return (0); 18086f793812SPavel Zakharov } 18091195e687SMark J Musante 18106f793812SPavel Zakharov /* 18116f793812SPavel Zakharov * If config is not trusted then ignore the spa guid check. This is 18126f793812SPavel Zakharov * necessary because if the machine crashed during a re-guid the new 18136f793812SPavel Zakharov * guid might have been written to all of the vdev labels, but not the 18146f793812SPavel Zakharov * cached config. The check will be performed again once we have the 18156f793812SPavel Zakharov * trusted config from the MOS. 18166f793812SPavel Zakharov */ 18176f793812SPavel Zakharov if (spa->spa_trust_config && guid != spa_guid(spa)) { 18186f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18196f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18206f793812SPavel Zakharov nvlist_free(label); 18216f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 18226f793812SPavel Zakharov "match config (%llu != %llu)", (u_longlong_t)guid, 18236f793812SPavel Zakharov (u_longlong_t)spa_guid(spa)); 18246f793812SPavel Zakharov return (0); 18256f793812SPavel Zakharov } 18266f793812SPavel Zakharov 18276f793812SPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 18286f793812SPavel Zakharov != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 18296f793812SPavel Zakharov &aux_guid) != 0) 18306f793812SPavel Zakharov aux_guid = 0; 18316f793812SPavel Zakharov 18326f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 18336f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18346f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18356f793812SPavel Zakharov nvlist_free(label); 18366f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18376f793812SPavel Zakharov ZPOOL_CONFIG_GUID); 18386f793812SPavel Zakharov return (0); 18396f793812SPavel Zakharov } 18406f793812SPavel Zakharov 18416f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 18426f793812SPavel Zakharov != 0) { 18436f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18446f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 18456f793812SPavel Zakharov nvlist_free(label); 18466f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18476f793812SPavel Zakharov ZPOOL_CONFIG_TOP_GUID); 18486f793812SPavel Zakharov return (0); 18496f793812SPavel Zakharov } 18506f793812SPavel Zakharov 18516f793812SPavel Zakharov /* 18526f793812SPavel Zakharov * If this vdev just became a top-level vdev because its sibling was 18536f793812SPavel Zakharov * detached, it will have adopted the parent's vdev guid -- but the 18546f793812SPavel Zakharov * label may or may not be on disk yet. Fortunately, either version 18556f793812SPavel Zakharov * of the label will have the same top guid, so if we're a top-level 18566f793812SPavel Zakharov * vdev, we can safely compare to that instead. 18576f793812SPavel Zakharov * However, if the config comes from a cachefile that failed to update 18586f793812SPavel Zakharov * after the detach, a top-level vdev will appear as a non top-level 18596f793812SPavel Zakharov * vdev in the config. Also relax the constraints if we perform an 18606f793812SPavel Zakharov * extreme rewind. 18616f793812SPavel Zakharov * 18626f793812SPavel Zakharov * If we split this vdev off instead, then we also check the 18636f793812SPavel Zakharov * original pool's guid. We don't want to consider the vdev 18646f793812SPavel Zakharov * corrupt if it is partway through a split operation. 18656f793812SPavel Zakharov */ 18666f793812SPavel Zakharov if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 18676f793812SPavel Zakharov boolean_t mismatch = B_FALSE; 18686f793812SPavel Zakharov if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 18696f793812SPavel Zakharov if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 18706f793812SPavel Zakharov mismatch = B_TRUE; 18716f793812SPavel Zakharov } else { 18726f793812SPavel Zakharov if (vd->vdev_guid != top_guid && 18736f793812SPavel Zakharov vd->vdev_top->vdev_guid != guid) 18746f793812SPavel Zakharov mismatch = B_TRUE; 1875560e6e96Seschrock } 1876560e6e96Seschrock 18776f793812SPavel Zakharov if (mismatch) { 1878560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1879560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1880560e6e96Seschrock nvlist_free(label); 18816f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: config guid " 18826f793812SPavel Zakharov "doesn't match label guid"); 18836f793812SPavel Zakharov vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 18846f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 18856f793812SPavel Zakharov (u_longlong_t)vd->vdev_top->vdev_guid); 18866f793812SPavel Zakharov vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 18876f793812SPavel Zakharov "aux_guid %llu", (u_longlong_t)guid, 18886f793812SPavel Zakharov (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1889560e6e96Seschrock return (0); 1890560e6e96Seschrock } 18916f793812SPavel Zakharov } 1892560e6e96Seschrock 18936f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 18946f793812SPavel Zakharov &state) != 0) { 18956f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 18966f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 1897560e6e96Seschrock nvlist_free(label); 18986f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 18996f793812SPavel Zakharov ZPOOL_CONFIG_POOL_STATE); 19006f793812SPavel Zakharov return (0); 19016f793812SPavel Zakharov } 1902560e6e96Seschrock 19036f793812SPavel Zakharov nvlist_free(label); 19046f793812SPavel Zakharov 19056f793812SPavel Zakharov /* 19066f793812SPavel Zakharov * If this is a verbatim import, no need to check the 19076f793812SPavel Zakharov * state of the pool. 19086f793812SPavel Zakharov */ 19096f793812SPavel Zakharov if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 19106f793812SPavel Zakharov spa_load_state(spa) == SPA_LOAD_OPEN && 19116f793812SPavel Zakharov state != POOL_STATE_ACTIVE) { 19126f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 19136f793812SPavel Zakharov "for spa %s", (u_longlong_t)state, spa->spa_name); 19146f793812SPavel Zakharov return (SET_ERROR(EBADF)); 19156f793812SPavel Zakharov } 19166f793812SPavel Zakharov 19176f793812SPavel Zakharov /* 19186f793812SPavel Zakharov * If we were able to open and validate a vdev that was 19196f793812SPavel Zakharov * previously marked permanently unavailable, clear that state 19206f793812SPavel Zakharov * now. 19216f793812SPavel Zakharov */ 19226f793812SPavel Zakharov if (vd->vdev_not_present) 19236f793812SPavel Zakharov vd->vdev_not_present = 0; 19246f793812SPavel Zakharov 19256f793812SPavel Zakharov return (0); 19266f793812SPavel Zakharov } 19276f793812SPavel Zakharov 19286f793812SPavel Zakharov static void 19296f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 19306f793812SPavel Zakharov { 19316f793812SPavel Zakharov if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 19326f793812SPavel Zakharov if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 19336f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 19346f793812SPavel Zakharov "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 19356f793812SPavel Zakharov dvd->vdev_path, svd->vdev_path); 19366f793812SPavel Zakharov spa_strfree(dvd->vdev_path); 19376f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 19383ee8c80cSPavel Zakharov } 19396f793812SPavel Zakharov } else if (svd->vdev_path != NULL) { 19406f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 19416f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 19426f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 19436f793812SPavel Zakharov } 19446f793812SPavel Zakharov } 1945560e6e96Seschrock 19466f793812SPavel Zakharov /* 19476f793812SPavel Zakharov * Recursively copy vdev paths from one vdev to another. Source and destination 19486f793812SPavel Zakharov * vdev trees must have same geometry otherwise return error. Intended to copy 19496f793812SPavel Zakharov * paths from userland config into MOS config. 19506f793812SPavel Zakharov */ 19516f793812SPavel Zakharov int 19526f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 19536f793812SPavel Zakharov { 19546f793812SPavel Zakharov if ((svd->vdev_ops == &vdev_missing_ops) || 19556f793812SPavel Zakharov (svd->vdev_ishole && dvd->vdev_ishole) || 19566f793812SPavel Zakharov (dvd->vdev_ops == &vdev_indirect_ops)) 19576f793812SPavel Zakharov return (0); 19586f793812SPavel Zakharov 19596f793812SPavel Zakharov if (svd->vdev_ops != dvd->vdev_ops) { 19606f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 19616f793812SPavel Zakharov svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 19626f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 19636f793812SPavel Zakharov } 19646f793812SPavel Zakharov 19656f793812SPavel Zakharov if (svd->vdev_guid != dvd->vdev_guid) { 19666f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 19676f793812SPavel Zakharov "%llu)", (u_longlong_t)svd->vdev_guid, 19686f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid); 19696f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 19706f793812SPavel Zakharov } 19716f793812SPavel Zakharov 19726f793812SPavel Zakharov if (svd->vdev_children != dvd->vdev_children) { 19736f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 19746f793812SPavel Zakharov "%llu != %llu", (u_longlong_t)svd->vdev_children, 19756f793812SPavel Zakharov (u_longlong_t)dvd->vdev_children); 19766f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 197751ece835Seschrock } 1978560e6e96Seschrock 19796f793812SPavel Zakharov for (uint64_t i = 0; i < svd->vdev_children; i++) { 19806f793812SPavel Zakharov int error = vdev_copy_path_strict(svd->vdev_child[i], 19816f793812SPavel Zakharov dvd->vdev_child[i]); 19826f793812SPavel Zakharov if (error != 0) 19836f793812SPavel Zakharov return (error); 19846f793812SPavel Zakharov } 19856f793812SPavel Zakharov 19866f793812SPavel Zakharov if (svd->vdev_ops->vdev_op_leaf) 19876f793812SPavel Zakharov vdev_copy_path_impl(svd, dvd); 19886f793812SPavel Zakharov 1989560e6e96Seschrock return (0); 1990560e6e96Seschrock } 1991560e6e96Seschrock 19926f793812SPavel Zakharov static void 19936f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 19946f793812SPavel Zakharov { 19956f793812SPavel Zakharov ASSERT(stvd->vdev_top == stvd); 19966f793812SPavel Zakharov ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 19976f793812SPavel Zakharov 19986f793812SPavel Zakharov for (uint64_t i = 0; i < dvd->vdev_children; i++) { 19996f793812SPavel Zakharov vdev_copy_path_search(stvd, dvd->vdev_child[i]); 20006f793812SPavel Zakharov } 20016f793812SPavel Zakharov 20026f793812SPavel Zakharov if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 20036f793812SPavel Zakharov return; 20046f793812SPavel Zakharov 20056f793812SPavel Zakharov /* 20066f793812SPavel Zakharov * The idea here is that while a vdev can shift positions within 20076f793812SPavel Zakharov * a top vdev (when replacing, attaching mirror, etc.) it cannot 20086f793812SPavel Zakharov * step outside of it. 20096f793812SPavel Zakharov */ 20106f793812SPavel Zakharov vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 20116f793812SPavel Zakharov 20126f793812SPavel Zakharov if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 20136f793812SPavel Zakharov return; 20146f793812SPavel Zakharov 20156f793812SPavel Zakharov ASSERT(vd->vdev_ops->vdev_op_leaf); 20166f793812SPavel Zakharov 20176f793812SPavel Zakharov vdev_copy_path_impl(vd, dvd); 20186f793812SPavel Zakharov } 20196f793812SPavel Zakharov 20206f793812SPavel Zakharov /* 20216f793812SPavel Zakharov * Recursively copy vdev paths from one root vdev to another. Source and 20226f793812SPavel Zakharov * destination vdev trees may differ in geometry. For each destination leaf 20236f793812SPavel Zakharov * vdev, search a vdev with the same guid and top vdev id in the source. 20246f793812SPavel Zakharov * Intended to copy paths from userland config into MOS config. 20256f793812SPavel Zakharov */ 20266f793812SPavel Zakharov void 20276f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 20286f793812SPavel Zakharov { 20296f793812SPavel Zakharov uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 20306f793812SPavel Zakharov ASSERT(srvd->vdev_ops == &vdev_root_ops); 20316f793812SPavel Zakharov ASSERT(drvd->vdev_ops == &vdev_root_ops); 20326f793812SPavel Zakharov 20336f793812SPavel Zakharov for (uint64_t i = 0; i < children; i++) { 20346f793812SPavel Zakharov vdev_copy_path_search(srvd->vdev_child[i], 20356f793812SPavel Zakharov drvd->vdev_child[i]); 20366f793812SPavel Zakharov } 20376f793812SPavel Zakharov } 20386f793812SPavel Zakharov 2039fa9e4066Sahrens /* 2040fa9e4066Sahrens * Close a virtual device. 2041fa9e4066Sahrens */ 2042fa9e4066Sahrens void 2043fa9e4066Sahrens vdev_close(vdev_t *vd) 2044fa9e4066Sahrens { 20458ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 2046095bcd66SGeorge Wilson vdev_t *pvd = vd->vdev_parent; 20478ad4d6ddSJeff Bonwick 20488ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 20498ad4d6ddSJeff Bonwick 20501195e687SMark J Musante /* 20511195e687SMark J Musante * If our parent is reopening, then we are as well, unless we are 20521195e687SMark J Musante * going offline. 20531195e687SMark J Musante */ 2054095bcd66SGeorge Wilson if (pvd != NULL && pvd->vdev_reopening) 20551195e687SMark J Musante vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 2056095bcd66SGeorge Wilson 2057fa9e4066Sahrens vd->vdev_ops->vdev_op_close(vd); 2058fa9e4066Sahrens 20593d7072f8Seschrock vdev_cache_purge(vd); 2060fa9e4066Sahrens 2061560e6e96Seschrock /* 2062573ca77eSGeorge Wilson * We record the previous state before we close it, so that if we are 2063560e6e96Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 2064560e6e96Seschrock * it's still faulted. 2065560e6e96Seschrock */ 2066560e6e96Seschrock vd->vdev_prevstate = vd->vdev_state; 2067560e6e96Seschrock 2068fa9e4066Sahrens if (vd->vdev_offline) 2069fa9e4066Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 2070fa9e4066Sahrens else 2071fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 2072ea8dc4b6Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2073fa9e4066Sahrens } 2074fa9e4066Sahrens 2075dcba9f3fSGeorge Wilson void 2076dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd) 2077dcba9f3fSGeorge Wilson { 2078dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 2079dcba9f3fSGeorge Wilson 2080dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 2081dcba9f3fSGeorge Wilson if (spa->spa_state == POOL_STATE_UNINITIALIZED) 2082dcba9f3fSGeorge Wilson return; 2083dcba9f3fSGeorge Wilson 2084dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 2085dcba9f3fSGeorge Wilson vdev_hold(vd->vdev_child[c]); 2086dcba9f3fSGeorge Wilson 2087dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2088dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_hold(vd); 2089dcba9f3fSGeorge Wilson } 2090dcba9f3fSGeorge Wilson 2091dcba9f3fSGeorge Wilson void 2092dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd) 2093dcba9f3fSGeorge Wilson { 2094dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 2095dcba9f3fSGeorge Wilson 2096dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 2097dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 2098dcba9f3fSGeorge Wilson vdev_rele(vd->vdev_child[c]); 2099dcba9f3fSGeorge Wilson 2100dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 2101dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_rele(vd); 2102dcba9f3fSGeorge Wilson } 2103dcba9f3fSGeorge Wilson 2104095bcd66SGeorge Wilson /* 2105095bcd66SGeorge Wilson * Reopen all interior vdevs and any unopened leaves. We don't actually 2106095bcd66SGeorge Wilson * reopen leaf vdevs which had previously been opened as they might deadlock 2107095bcd66SGeorge Wilson * on the spa_config_lock. Instead we only obtain the leaf's physical size. 2108095bcd66SGeorge Wilson * If the leaf has never been opened then open it, as usual. 2109095bcd66SGeorge Wilson */ 2110fa9e4066Sahrens void 2111ea8dc4b6Seschrock vdev_reopen(vdev_t *vd) 2112fa9e4066Sahrens { 2113ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 2114fa9e4066Sahrens 2115e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2116ea8dc4b6Seschrock 21171195e687SMark J Musante /* set the reopening flag unless we're taking the vdev offline */ 21181195e687SMark J Musante vd->vdev_reopening = !vd->vdev_offline; 2119fa9e4066Sahrens vdev_close(vd); 2120fa9e4066Sahrens (void) vdev_open(vd); 2121fa9e4066Sahrens 212239c23413Seschrock /* 212339c23413Seschrock * Call vdev_validate() here to make sure we have the same device. 212439c23413Seschrock * Otherwise, a device with an invalid label could be successfully 212539c23413Seschrock * opened in response to vdev_reopen(). 212639c23413Seschrock */ 2127c5904d13Seschrock if (vd->vdev_aux) { 2128c5904d13Seschrock (void) vdev_validate_aux(vd); 2129e14bb325SJeff Bonwick if (vdev_readable(vd) && vdev_writeable(vd) && 21306809eb4eSEric Schrock vd->vdev_aux == &spa->spa_l2cache && 2131573ca77eSGeorge Wilson !l2arc_vdev_present(vd)) 2132573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 2133c5904d13Seschrock } else { 21346f793812SPavel Zakharov (void) vdev_validate(vd); 2135c5904d13Seschrock } 213639c23413Seschrock 2137fa9e4066Sahrens /* 21383d7072f8Seschrock * Reassess parent vdev's health. 2139fa9e4066Sahrens */ 21403d7072f8Seschrock vdev_propagate_state(vd); 2141fa9e4066Sahrens } 2142fa9e4066Sahrens 2143fa9e4066Sahrens int 214499653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2145fa9e4066Sahrens { 2146fa9e4066Sahrens int error; 2147fa9e4066Sahrens 2148fa9e4066Sahrens /* 2149fa9e4066Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 2150fa9e4066Sahrens * For a create, however, we want to fail the request if 2151fa9e4066Sahrens * there are any components we can't open. 2152fa9e4066Sahrens */ 2153fa9e4066Sahrens error = vdev_open(vd); 2154fa9e4066Sahrens 2155fa9e4066Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2156fa9e4066Sahrens vdev_close(vd); 2157fa9e4066Sahrens return (error ? error : ENXIO); 2158fa9e4066Sahrens } 2159fa9e4066Sahrens 2160fa9e4066Sahrens /* 21610713e232SGeorge Wilson * Recursively load DTLs and initialize all labels. 2162fa9e4066Sahrens */ 21630713e232SGeorge Wilson if ((error = vdev_dtl_load(vd)) != 0 || 21640713e232SGeorge Wilson (error = vdev_label_init(vd, txg, isreplacing ? 216539c23413Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2166fa9e4066Sahrens vdev_close(vd); 2167fa9e4066Sahrens return (error); 2168fa9e4066Sahrens } 2169fa9e4066Sahrens 2170fa9e4066Sahrens return (0); 2171fa9e4066Sahrens } 2172fa9e4066Sahrens 21730e34b6a7Sbonwick void 2174573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd) 2175fa9e4066Sahrens { 217686714001SSerapheim Dimitropoulos uint64_t asize = vd->vdev_asize; 2177a0b03b16SSerapheim Dimitropoulos uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2178b4bf0cf0SDon Brady uint64_t ms_shift; 217986714001SSerapheim Dimitropoulos 2180*a3874b8bSToomas Soome /* BEGIN CSTYLED */ 2181fa9e4066Sahrens /* 2182b4bf0cf0SDon Brady * There are two dimensions to the metaslab sizing calculation: 2183b4bf0cf0SDon Brady * the size of the metaslab and the count of metaslabs per vdev. 2184b4bf0cf0SDon Brady * 2185a0b03b16SSerapheim Dimitropoulos * The default values used below are a good balance between memory 2186a0b03b16SSerapheim Dimitropoulos * usage (larger metaslab size means more memory needed for loaded 2187a0b03b16SSerapheim Dimitropoulos * metaslabs; more metaslabs means more memory needed for the 2188a0b03b16SSerapheim Dimitropoulos * metaslab_t structs), metaslab load time (larger metaslabs take 2189a0b03b16SSerapheim Dimitropoulos * longer to load), and metaslab sync time (more metaslabs means 2190a0b03b16SSerapheim Dimitropoulos * more time spent syncing all of them). 2191a0b03b16SSerapheim Dimitropoulos * 2192a0b03b16SSerapheim Dimitropoulos * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2193a0b03b16SSerapheim Dimitropoulos * The range of the dimensions are as follows: 2194a0b03b16SSerapheim Dimitropoulos * 2195a0b03b16SSerapheim Dimitropoulos * 2^29 <= ms_size <= 2^34 2196b4bf0cf0SDon Brady * 16 <= ms_count <= 131,072 2197b4bf0cf0SDon Brady * 2198b4bf0cf0SDon Brady * On the lower end of vdev sizes, we aim for metaslabs sizes of 2199b4bf0cf0SDon Brady * at least 512MB (2^29) to minimize fragmentation effects when 2200b4bf0cf0SDon Brady * testing with smaller devices. However, the count constraint 2201b4bf0cf0SDon Brady * of at least 16 metaslabs will override this minimum size goal. 2202b4bf0cf0SDon Brady * 2203b4bf0cf0SDon Brady * On the upper end of vdev sizes, we aim for a maximum metaslab 2204a0b03b16SSerapheim Dimitropoulos * size of 16GB. However, we will cap the total count to 2^17 2205a0b03b16SSerapheim Dimitropoulos * metaslabs to keep our memory footprint in check and let the 2206a0b03b16SSerapheim Dimitropoulos * metaslab size grow from there if that limit is hit. 2207b4bf0cf0SDon Brady * 2208b4bf0cf0SDon Brady * The net effect of applying above constrains is summarized below. 2209b4bf0cf0SDon Brady * 2210663207adSDon Brady * vdev size metaslab count 2211a0b03b16SSerapheim Dimitropoulos * --------------|----------------- 2212663207adSDon Brady * < 8GB ~16 2213663207adSDon Brady * 8GB - 100GB one per 512MB 2214663207adSDon Brady * 100GB - 3TB ~200 2215663207adSDon Brady * 3TB - 2PB one per 16GB 2216663207adSDon Brady * > 2PB ~131,072 2217a0b03b16SSerapheim Dimitropoulos * -------------------------------- 2218a0b03b16SSerapheim Dimitropoulos * 2219a0b03b16SSerapheim Dimitropoulos * Finally, note that all of the above calculate the initial 2220a0b03b16SSerapheim Dimitropoulos * number of metaslabs. Expanding a top-level vdev will result 2221a0b03b16SSerapheim Dimitropoulos * in additional metaslabs being allocated making it possible 2222a0b03b16SSerapheim Dimitropoulos * to exceed the zfs_vdev_ms_count_limit. 2223a0b03b16SSerapheim Dimitropoulos */ 2224*a3874b8bSToomas Soome /* END CSTYLED */ 2225a0b03b16SSerapheim Dimitropoulos 2226a0b03b16SSerapheim Dimitropoulos if (ms_count < zfs_vdev_min_ms_count) 2227a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2228a0b03b16SSerapheim Dimitropoulos else if (ms_count > zfs_vdev_default_ms_count) 2229a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2230b4bf0cf0SDon Brady else 2231a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_default_ms_shift; 2232b4bf0cf0SDon Brady 2233b4bf0cf0SDon Brady if (ms_shift < SPA_MAXBLOCKSHIFT) { 2234b4bf0cf0SDon Brady ms_shift = SPA_MAXBLOCKSHIFT; 2235a0b03b16SSerapheim Dimitropoulos } else if (ms_shift > zfs_vdev_max_ms_shift) { 2236a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_max_ms_shift; 2237b4bf0cf0SDon Brady /* cap the total count to constrain memory footprint */ 2238a0b03b16SSerapheim Dimitropoulos if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2239a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 224086714001SSerapheim Dimitropoulos } 224186714001SSerapheim Dimitropoulos 224286714001SSerapheim Dimitropoulos vd->vdev_ms_shift = ms_shift; 224386714001SSerapheim Dimitropoulos ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2244fa9e4066Sahrens } 2245fa9e4066Sahrens 2246fa9e4066Sahrens void 2247ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2248fa9e4066Sahrens { 2249ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 22505cabbc6bSPrashanth Sreenivasa /* indirect vdevs don't have metaslabs or dtls */ 22515cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd) || flags == 0); 2252ecc2d604Sbonwick ASSERT(ISP2(flags)); 2253f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 2254fa9e4066Sahrens 2255ecc2d604Sbonwick if (flags & VDD_METASLAB) 2256ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2257ecc2d604Sbonwick 2258ecc2d604Sbonwick if (flags & VDD_DTL) 2259ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2260ecc2d604Sbonwick 2261ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2262fa9e4066Sahrens } 2263fa9e4066Sahrens 22640713e232SGeorge Wilson void 22650713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 22660713e232SGeorge Wilson { 22670713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 22680713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 22690713e232SGeorge Wilson 22700713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 22710713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 22720713e232SGeorge Wilson } 22730713e232SGeorge Wilson 22748ad4d6ddSJeff Bonwick /* 22758ad4d6ddSJeff Bonwick * DTLs. 22768ad4d6ddSJeff Bonwick * 22778ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 22789fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 22798ad4d6ddSJeff Bonwick * 22808ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 22818ad4d6ddSJeff Bonwick * 22828ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 22838ad4d6ddSJeff Bonwick * 22848ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 22858ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 22868ad4d6ddSJeff Bonwick * txgs that was scrubbed. 22878ad4d6ddSJeff Bonwick * 22888ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 22898ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 22908ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 22918ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 22928ad4d6ddSJeff Bonwick * determine whether a device can be detached. 22938ad4d6ddSJeff Bonwick * 22948ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 22958ad4d6ddSJeff Bonwick * either has the data or it doesn't. 22968ad4d6ddSJeff Bonwick * 22978ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 22988ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 22998ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 23008ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 23018ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 23028ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 23038ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 23048ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 23058ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 23068ad4d6ddSJeff Bonwick * 23078ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 23088ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 23098ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 23108ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 23118ad4d6ddSJeff Bonwick */ 2312fa9e4066Sahrens void 23138ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2314fa9e4066Sahrens { 23150713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23168ad4d6ddSJeff Bonwick 23178ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 23188ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2319f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 23208ad4d6ddSJeff Bonwick 23215cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 23220713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 23230713e232SGeorge Wilson range_tree_add(rt, txg, size); 23245cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2325fa9e4066Sahrens } 2326fa9e4066Sahrens 23278ad4d6ddSJeff Bonwick boolean_t 23288ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2329fa9e4066Sahrens { 23300713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23318ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 2332fa9e4066Sahrens 23338ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 23348ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2335fa9e4066Sahrens 23365cabbc6bSPrashanth Sreenivasa /* 23375cabbc6bSPrashanth Sreenivasa * While we are loading the pool, the DTLs have not been loaded yet. 23385cabbc6bSPrashanth Sreenivasa * Ignore the DTLs and try all devices. This avoids a recursive 23395cabbc6bSPrashanth Sreenivasa * mutex enter on the vdev_dtl_lock, and also makes us try hard 23405cabbc6bSPrashanth Sreenivasa * when loading the pool (relying on the checksum to ensure that 23415cabbc6bSPrashanth Sreenivasa * we get the right data -- note that we while loading, we are 23425cabbc6bSPrashanth Sreenivasa * only reading the MOS, which is always checksummed). 23435cabbc6bSPrashanth Sreenivasa */ 23445cabbc6bSPrashanth Sreenivasa if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 23455cabbc6bSPrashanth Sreenivasa return (B_FALSE); 23465cabbc6bSPrashanth Sreenivasa 23475cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 234886714001SSerapheim Dimitropoulos if (!range_tree_is_empty(rt)) 23490713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 23505cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2351fa9e4066Sahrens 2352fa9e4066Sahrens return (dirty); 2353fa9e4066Sahrens } 2354fa9e4066Sahrens 23558ad4d6ddSJeff Bonwick boolean_t 23568ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 23578ad4d6ddSJeff Bonwick { 23580713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 23598ad4d6ddSJeff Bonwick boolean_t empty; 23608ad4d6ddSJeff Bonwick 23615cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 236286714001SSerapheim Dimitropoulos empty = range_tree_is_empty(rt); 23635cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 23648ad4d6ddSJeff Bonwick 23658ad4d6ddSJeff Bonwick return (empty); 23668ad4d6ddSJeff Bonwick } 23678ad4d6ddSJeff Bonwick 2368*a3874b8bSToomas Soome /* 2369*a3874b8bSToomas Soome * Returns B_TRUE if vdev determines offset needs to be resilvered. 2370*a3874b8bSToomas Soome */ 2371*a3874b8bSToomas Soome boolean_t 2372*a3874b8bSToomas Soome vdev_dtl_need_resilver(vdev_t *vd, uint64_t offset, size_t psize) 2373*a3874b8bSToomas Soome { 2374*a3874b8bSToomas Soome ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2375*a3874b8bSToomas Soome 2376*a3874b8bSToomas Soome if (vd->vdev_ops->vdev_op_need_resilver == NULL || 2377*a3874b8bSToomas Soome vd->vdev_ops->vdev_op_leaf) 2378*a3874b8bSToomas Soome return (B_TRUE); 2379*a3874b8bSToomas Soome 2380*a3874b8bSToomas Soome return (vd->vdev_ops->vdev_op_need_resilver(vd, offset, psize)); 2381*a3874b8bSToomas Soome } 2382*a3874b8bSToomas Soome 2383b4952e17SGeorge Wilson /* 2384b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 2385b4952e17SGeorge Wilson */ 2386b4952e17SGeorge Wilson static uint64_t 2387b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 2388b4952e17SGeorge Wilson { 23890713e232SGeorge Wilson range_seg_t *rs; 2390b4952e17SGeorge Wilson 2391b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 23920713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2393b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2394b4952e17SGeorge Wilson 23950713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 23960713e232SGeorge Wilson return (rs->rs_start - 1); 2397b4952e17SGeorge Wilson } 2398b4952e17SGeorge Wilson 2399b4952e17SGeorge Wilson /* 2400b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 2401b4952e17SGeorge Wilson */ 2402b4952e17SGeorge Wilson static uint64_t 2403b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 2404b4952e17SGeorge Wilson { 24050713e232SGeorge Wilson range_seg_t *rs; 2406b4952e17SGeorge Wilson 2407b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 24080713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2409b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2410b4952e17SGeorge Wilson 24110713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 24120713e232SGeorge Wilson return (rs->rs_end); 2413b4952e17SGeorge Wilson } 2414b4952e17SGeorge Wilson 2415b4952e17SGeorge Wilson /* 2416b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 2417b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 2418b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 2419b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 2420b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 2421b4952e17SGeorge Wilson * excise the DTLs. 2422b4952e17SGeorge Wilson */ 2423b4952e17SGeorge Wilson static boolean_t 2424b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 2425b4952e17SGeorge Wilson { 2426b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 2427b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2428b4952e17SGeorge Wilson 2429b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 2430b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2431b4952e17SGeorge Wilson 24322d2f193aSMatthew Ahrens if (vd->vdev_state < VDEV_STATE_DEGRADED) 24332d2f193aSMatthew Ahrens return (B_FALSE); 24342d2f193aSMatthew Ahrens 2435b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 243686714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2437b4952e17SGeorge Wilson return (B_TRUE); 2438b4952e17SGeorge Wilson 2439b4952e17SGeorge Wilson /* 2440b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 2441b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 2442b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 2443b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 2444b4952e17SGeorge Wilson * for excision. 2445b4952e17SGeorge Wilson */ 2446b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2447b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2448b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2449b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2450b4952e17SGeorge Wilson return (B_TRUE); 2451b4952e17SGeorge Wilson } 2452b4952e17SGeorge Wilson return (B_FALSE); 2453b4952e17SGeorge Wilson } 2454b4952e17SGeorge Wilson 2455fa9e4066Sahrens /* 2456fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 2457fa9e4066Sahrens */ 2458fa9e4066Sahrens void 2459fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2460fa9e4066Sahrens { 2461ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 24628ad4d6ddSJeff Bonwick avl_tree_t reftree; 24638ad4d6ddSJeff Bonwick int minref; 2464fa9e4066Sahrens 24658ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2466fa9e4066Sahrens 24678ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 24688ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 24698ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 24708ad4d6ddSJeff Bonwick 24715cabbc6bSPrashanth Sreenivasa if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 24728ad4d6ddSJeff Bonwick return; 24738ad4d6ddSJeff Bonwick 24748ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 24753f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 24763f9d6ad7SLin Ling 2477fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 2478b4952e17SGeorge Wilson 2479b4952e17SGeorge Wilson /* 2480b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 2481b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 2482b4952e17SGeorge Wilson * excise regions on vdevs that were available during 2483b4952e17SGeorge Wilson * the entire duration of this scan. 2484b4952e17SGeorge Wilson */ 2485088f3894Sahrens if (scrub_txg != 0 && 24863f9d6ad7SLin Ling (spa->spa_scrub_started || 2487b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2488b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 2489088f3894Sahrens /* 2490088f3894Sahrens * We completed a scrub up to scrub_txg. If we 2491088f3894Sahrens * did it without rebooting, then the scrub dtl 2492088f3894Sahrens * will be valid, so excise the old region and 2493088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 2494088f3894Sahrens * dtl as-is if there was an error. 24958ad4d6ddSJeff Bonwick * 24968ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 24978ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 24988ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 24998ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 25008ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 25018ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 25028ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 25038ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 25048ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 2505088f3894Sahrens */ 25060713e232SGeorge Wilson space_reftree_create(&reftree); 25070713e232SGeorge Wilson space_reftree_add_map(&reftree, 25080713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 25090713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 25100713e232SGeorge Wilson space_reftree_add_map(&reftree, 25110713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 25120713e232SGeorge Wilson space_reftree_generate_map(&reftree, 25130713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 25140713e232SGeorge Wilson space_reftree_destroy(&reftree); 2515fa9e4066Sahrens } 25160713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 25170713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 25180713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2519fa9e4066Sahrens if (scrub_done) 25200713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 25210713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 25228ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 25230713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 25248ad4d6ddSJeff Bonwick else 25250713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 25260713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2527b4952e17SGeorge Wilson 2528b4952e17SGeorge Wilson /* 2529b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 2530b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 2531b4952e17SGeorge Wilson */ 2532b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 253386714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 253486714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2535b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 2536b4952e17SGeorge Wilson 2537fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2538088f3894Sahrens 2539ecc2d604Sbonwick if (txg != 0) 2540ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2541fa9e4066Sahrens return; 2542fa9e4066Sahrens } 2543fa9e4066Sahrens 2544fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 25458ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 254699bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 254799bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 25488ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 25498ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 25508ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 25518ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 25528ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 25538ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 25548ad4d6ddSJeff Bonwick else 25558ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 25560713e232SGeorge Wilson space_reftree_create(&reftree); 25578ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 25588ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 25598ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 25600713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 25618ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 25628ad4d6ddSJeff Bonwick } 25630713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 25640713e232SGeorge Wilson space_reftree_destroy(&reftree); 2565fa9e4066Sahrens } 25668ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 2567fa9e4066Sahrens } 2568fa9e4066Sahrens 25690713e232SGeorge Wilson int 2570fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 2571fa9e4066Sahrens { 2572fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2573ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 25740713e232SGeorge Wilson int error = 0; 2575fa9e4066Sahrens 25760713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 25775cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 2578fa9e4066Sahrens 25790713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 25805cabbc6bSPrashanth Sreenivasa vd->vdev_dtl_object, 0, -1ULL, 0); 25810713e232SGeorge Wilson if (error) 25820713e232SGeorge Wilson return (error); 25830713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2584fa9e4066Sahrens 25850713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 25860713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 25870713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 25880713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2589fa9e4066Sahrens 25900713e232SGeorge Wilson return (error); 25910713e232SGeorge Wilson } 25920713e232SGeorge Wilson 25930713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 25940713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 25950713e232SGeorge Wilson if (error != 0) 25960713e232SGeorge Wilson break; 25970713e232SGeorge Wilson } 2598fa9e4066Sahrens 2599fa9e4066Sahrens return (error); 2600fa9e4066Sahrens } 2601fa9e4066Sahrens 2602663207adSDon Brady static void 2603663207adSDon Brady vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 2604663207adSDon Brady { 2605663207adSDon Brady spa_t *spa = vd->vdev_spa; 2606663207adSDon Brady objset_t *mos = spa->spa_meta_objset; 2607663207adSDon Brady vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 2608663207adSDon Brady const char *string; 2609663207adSDon Brady 2610663207adSDon Brady ASSERT(alloc_bias != VDEV_BIAS_NONE); 2611663207adSDon Brady 2612663207adSDon Brady string = 2613663207adSDon Brady (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 2614663207adSDon Brady (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 2615663207adSDon Brady (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 2616663207adSDon Brady 2617663207adSDon Brady ASSERT(string != NULL); 2618663207adSDon Brady VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 2619663207adSDon Brady 1, strlen(string) + 1, string, tx)); 2620663207adSDon Brady 2621663207adSDon Brady if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 2622663207adSDon Brady spa_activate_allocation_classes(spa, tx); 2623663207adSDon Brady } 2624663207adSDon Brady } 2625663207adSDon Brady 2626215198a6SJoe Stein void 2627215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2628215198a6SJoe Stein { 2629215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2630215198a6SJoe Stein 2631215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2632215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2633215198a6SJoe Stein zapobj, tx)); 2634215198a6SJoe Stein } 2635215198a6SJoe Stein 2636215198a6SJoe Stein uint64_t 2637215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2638215198a6SJoe Stein { 2639215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2640215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2641215198a6SJoe Stein DMU_OT_NONE, 0, tx); 2642215198a6SJoe Stein 2643215198a6SJoe Stein ASSERT(zap != 0); 2644215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2645215198a6SJoe Stein zap, tx)); 2646215198a6SJoe Stein 2647215198a6SJoe Stein return (zap); 2648215198a6SJoe Stein } 2649215198a6SJoe Stein 2650215198a6SJoe Stein void 2651215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2652215198a6SJoe Stein { 2653215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 2654215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 2655215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 2656215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 2657215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2658215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2659215198a6SJoe Stein } 2660215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2661215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2662663207adSDon Brady if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 2663663207adSDon Brady vdev_zap_allocation_data(vd, tx); 2664215198a6SJoe Stein } 2665215198a6SJoe Stein } 2666663207adSDon Brady 2667215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2668215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2669215198a6SJoe Stein } 2670215198a6SJoe Stein } 2671215198a6SJoe Stein 2672fa9e4066Sahrens void 2673fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2674fa9e4066Sahrens { 2675fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 26760713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2677ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 26780713e232SGeorge Wilson range_tree_t *rtsync; 2679fa9e4066Sahrens dmu_tx_t *tx; 26800713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2681fa9e4066Sahrens 26825cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 26830713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 268488ecc943SGeorge Wilson 2685fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2686fa9e4066Sahrens 26870713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 26880713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 26890713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 26900713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 26910713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 26920713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2693215198a6SJoe Stein 2694215198a6SJoe Stein /* 2695215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2696215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2697215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2698215198a6SJoe Stein */ 2699215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2700215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2701215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2702215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2703215198a6SJoe Stein } 2704215198a6SJoe Stein 2705fa9e4066Sahrens dmu_tx_commit(tx); 2706fa9e4066Sahrens return; 2707fa9e4066Sahrens } 2708fa9e4066Sahrens 27090713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 27100713e232SGeorge Wilson uint64_t new_object; 27110713e232SGeorge Wilson 271286714001SSerapheim Dimitropoulos new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 27130713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 27140713e232SGeorge Wilson 27150713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 27165cabbc6bSPrashanth Sreenivasa 0, -1ULL, 0)); 27170713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2718fa9e4066Sahrens } 2719fa9e4066Sahrens 27205cabbc6bSPrashanth Sreenivasa rtsync = range_tree_create(NULL, NULL); 2721fa9e4066Sahrens 2722fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 27230713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2724fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2725fa9e4066Sahrens 272686714001SSerapheim Dimitropoulos space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 272717f11284SSerapheim Dimitropoulos space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 27280713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2729fa9e4066Sahrens 27300713e232SGeorge Wilson range_tree_destroy(rtsync); 2731fa9e4066Sahrens 27320713e232SGeorge Wilson /* 27330713e232SGeorge Wilson * If the object for the space map has changed then dirty 27340713e232SGeorge Wilson * the top level so that we update the config. 27350713e232SGeorge Wilson */ 27360713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 27373ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 27383ee8c80cSPavel Zakharov "new object %llu", (u_longlong_t)txg, spa_name(spa), 27393ee8c80cSPavel Zakharov (u_longlong_t)object, 27403ee8c80cSPavel Zakharov (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 27410713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 27420713e232SGeorge Wilson } 2743fa9e4066Sahrens 2744fa9e4066Sahrens dmu_tx_commit(tx); 2745fa9e4066Sahrens } 2746fa9e4066Sahrens 27478ad4d6ddSJeff Bonwick /* 27488ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 27498ad4d6ddSJeff Bonwick * without losing data. 27508ad4d6ddSJeff Bonwick */ 27518ad4d6ddSJeff Bonwick boolean_t 27528ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 27538ad4d6ddSJeff Bonwick { 27548ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 27558ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 27568ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 27578ad4d6ddSJeff Bonwick boolean_t required; 27588ad4d6ddSJeff Bonwick 27598ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 27608ad4d6ddSJeff Bonwick 27618ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 27628ad4d6ddSJeff Bonwick return (B_TRUE); 27638ad4d6ddSJeff Bonwick 27648ad4d6ddSJeff Bonwick /* 27658ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 27668ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 27678ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 27688ad4d6ddSJeff Bonwick */ 27698ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 27708ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 27718ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 27728ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 27738ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 27748ad4d6ddSJeff Bonwick 2775cb04b873SMark J Musante if (!required && zio_injection_enabled) 2776cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2777cb04b873SMark J Musante 27788ad4d6ddSJeff Bonwick return (required); 27798ad4d6ddSJeff Bonwick } 27808ad4d6ddSJeff Bonwick 2781088f3894Sahrens /* 2782088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2783088f3894Sahrens */ 2784088f3894Sahrens boolean_t 2785088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2786088f3894Sahrens { 2787088f3894Sahrens boolean_t needed = B_FALSE; 2788088f3894Sahrens uint64_t thismin = UINT64_MAX; 2789088f3894Sahrens uint64_t thismax = 0; 2790088f3894Sahrens 2791088f3894Sahrens if (vd->vdev_children == 0) { 2792088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 279386714001SSerapheim Dimitropoulos if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 27948ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2795088f3894Sahrens 2796b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2797b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2798088f3894Sahrens needed = B_TRUE; 2799088f3894Sahrens } 2800088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2801088f3894Sahrens } else { 28028ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2803088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2804088f3894Sahrens uint64_t cmin, cmax; 2805088f3894Sahrens 2806088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2807088f3894Sahrens thismin = MIN(thismin, cmin); 2808088f3894Sahrens thismax = MAX(thismax, cmax); 2809088f3894Sahrens needed = B_TRUE; 2810088f3894Sahrens } 2811088f3894Sahrens } 2812088f3894Sahrens } 2813088f3894Sahrens 2814088f3894Sahrens if (needed && minp) { 2815088f3894Sahrens *minp = thismin; 2816088f3894Sahrens *maxp = thismax; 2817088f3894Sahrens } 2818088f3894Sahrens return (needed); 2819088f3894Sahrens } 2820088f3894Sahrens 282186714001SSerapheim Dimitropoulos /* 282286714001SSerapheim Dimitropoulos * Gets the checkpoint space map object from the vdev's ZAP. 282386714001SSerapheim Dimitropoulos * Returns the spacemap object, or 0 if it wasn't in the ZAP 282486714001SSerapheim Dimitropoulos * or the ZAP doesn't exist yet. 282586714001SSerapheim Dimitropoulos */ 282686714001SSerapheim Dimitropoulos int 282786714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd) 282886714001SSerapheim Dimitropoulos { 282986714001SSerapheim Dimitropoulos ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 283086714001SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) { 283186714001SSerapheim Dimitropoulos return (0); 283286714001SSerapheim Dimitropoulos } 283386714001SSerapheim Dimitropoulos 283486714001SSerapheim Dimitropoulos uint64_t sm_obj = 0; 283586714001SSerapheim Dimitropoulos int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 283686714001SSerapheim Dimitropoulos VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 283786714001SSerapheim Dimitropoulos 283886714001SSerapheim Dimitropoulos ASSERT(err == 0 || err == ENOENT); 283986714001SSerapheim Dimitropoulos 284086714001SSerapheim Dimitropoulos return (sm_obj); 284186714001SSerapheim Dimitropoulos } 284286714001SSerapheim Dimitropoulos 28435cabbc6bSPrashanth Sreenivasa int 2844ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2845fa9e4066Sahrens { 28465cabbc6bSPrashanth Sreenivasa int error = 0; 2847fa9e4066Sahrens /* 2848fa9e4066Sahrens * Recursively load all children. 2849fa9e4066Sahrens */ 28505cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 28515cabbc6bSPrashanth Sreenivasa error = vdev_load(vd->vdev_child[c]); 28525cabbc6bSPrashanth Sreenivasa if (error != 0) { 28535cabbc6bSPrashanth Sreenivasa return (error); 28545cabbc6bSPrashanth Sreenivasa } 28555cabbc6bSPrashanth Sreenivasa } 28565cabbc6bSPrashanth Sreenivasa 28575cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 2858fa9e4066Sahrens 2859663207adSDon Brady /* 2860663207adSDon Brady * On spa_load path, grab the allocation bias from our zap 2861663207adSDon Brady */ 2862663207adSDon Brady if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 2863663207adSDon Brady spa_t *spa = vd->vdev_spa; 2864663207adSDon Brady char bias_str[64]; 2865663207adSDon Brady 2866663207adSDon Brady if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 2867663207adSDon Brady VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 2868663207adSDon Brady bias_str) == 0) { 2869663207adSDon Brady ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 2870663207adSDon Brady vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 2871663207adSDon Brady } 2872663207adSDon Brady } 2873663207adSDon Brady 2874fa9e4066Sahrens /* 28750e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2876fa9e4066Sahrens */ 28775cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2878663207adSDon Brady vdev_metaslab_group_create(vd); 2879663207adSDon Brady 28805cabbc6bSPrashanth Sreenivasa if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 28815cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 28825cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 28833ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 28843ee8c80cSPavel Zakharov "asize=%llu", (u_longlong_t)vd->vdev_ashift, 28853ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_asize); 28865cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 2887555d674dSSerapheim Dimitropoulos } 2888555d674dSSerapheim Dimitropoulos 2889555d674dSSerapheim Dimitropoulos error = vdev_metaslab_init(vd, 0); 2890555d674dSSerapheim Dimitropoulos if (error != 0) { 28913ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 28923ee8c80cSPavel Zakharov "[error=%d]", error); 28935cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 28945cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 28955cabbc6bSPrashanth Sreenivasa return (error); 28965cabbc6bSPrashanth Sreenivasa } 289786714001SSerapheim Dimitropoulos 289886714001SSerapheim Dimitropoulos uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 289986714001SSerapheim Dimitropoulos if (checkpoint_sm_obj != 0) { 290086714001SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 290186714001SSerapheim Dimitropoulos ASSERT(vd->vdev_asize != 0); 290286714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 290386714001SSerapheim Dimitropoulos 2904555d674dSSerapheim Dimitropoulos error = space_map_open(&vd->vdev_checkpoint_sm, 290586714001SSerapheim Dimitropoulos mos, checkpoint_sm_obj, 0, vd->vdev_asize, 2906555d674dSSerapheim Dimitropoulos vd->vdev_ashift); 2907555d674dSSerapheim Dimitropoulos if (error != 0) { 290886714001SSerapheim Dimitropoulos vdev_dbgmsg(vd, "vdev_load: space_map_open " 290986714001SSerapheim Dimitropoulos "failed for checkpoint spacemap (obj %llu) " 291086714001SSerapheim Dimitropoulos "[error=%d]", 291186714001SSerapheim Dimitropoulos (u_longlong_t)checkpoint_sm_obj, error); 291286714001SSerapheim Dimitropoulos return (error); 291386714001SSerapheim Dimitropoulos } 291486714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 291586714001SSerapheim Dimitropoulos 291686714001SSerapheim Dimitropoulos /* 291786714001SSerapheim Dimitropoulos * Since the checkpoint_sm contains free entries 2918555d674dSSerapheim Dimitropoulos * exclusively we can use space_map_allocated() to 2919555d674dSSerapheim Dimitropoulos * indicate the cumulative checkpointed space that 2920555d674dSSerapheim Dimitropoulos * has been freed. 292186714001SSerapheim Dimitropoulos */ 292286714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space = 2923555d674dSSerapheim Dimitropoulos -space_map_allocated(vd->vdev_checkpoint_sm); 292486714001SSerapheim Dimitropoulos vd->vdev_spa->spa_checkpoint_info.sci_dspace += 292586714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space; 292686714001SSerapheim Dimitropoulos } 29275cabbc6bSPrashanth Sreenivasa } 2928fa9e4066Sahrens 2929fa9e4066Sahrens /* 2930fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2931fa9e4066Sahrens */ 29325cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2933560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2934560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 29353ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 29363ee8c80cSPavel Zakharov "[error=%d]", error); 29375cabbc6bSPrashanth Sreenivasa return (error); 29385cabbc6bSPrashanth Sreenivasa } 29395cabbc6bSPrashanth Sreenivasa 29405cabbc6bSPrashanth Sreenivasa uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 29415cabbc6bSPrashanth Sreenivasa if (obsolete_sm_object != 0) { 29425cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 29435cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_asize != 0); 294486714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 29455cabbc6bSPrashanth Sreenivasa 29465cabbc6bSPrashanth Sreenivasa if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 29475cabbc6bSPrashanth Sreenivasa obsolete_sm_object, 0, vd->vdev_asize, 0))) { 29485cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 29495cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 29503ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 29513ee8c80cSPavel Zakharov "obsolete spacemap (obj %llu) [error=%d]", 29523ee8c80cSPavel Zakharov (u_longlong_t)obsolete_sm_object, error); 29535cabbc6bSPrashanth Sreenivasa return (error); 29545cabbc6bSPrashanth Sreenivasa } 29555cabbc6bSPrashanth Sreenivasa } 29565cabbc6bSPrashanth Sreenivasa 29575cabbc6bSPrashanth Sreenivasa return (0); 2958fa9e4066Sahrens } 2959fa9e4066Sahrens 296099653d4eSeschrock /* 2961fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 2962fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 2963fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 2964fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 2965fa94a07fSbrendan * repurposed to another pool. 296699653d4eSeschrock */ 296799653d4eSeschrock int 2968fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 296999653d4eSeschrock { 297099653d4eSeschrock nvlist_t *label; 297199653d4eSeschrock uint64_t guid, version; 297299653d4eSeschrock uint64_t state; 297399653d4eSeschrock 2974e14bb325SJeff Bonwick if (!vdev_readable(vd)) 2975c5904d13Seschrock return (0); 2976c5904d13Seschrock 2977dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 297899653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 297999653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 298099653d4eSeschrock return (-1); 298199653d4eSeschrock } 298299653d4eSeschrock 298399653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2984ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 298599653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 298699653d4eSeschrock guid != vd->vdev_guid || 298799653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 298899653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 298999653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 299099653d4eSeschrock nvlist_free(label); 299199653d4eSeschrock return (-1); 299299653d4eSeschrock } 299399653d4eSeschrock 299499653d4eSeschrock /* 299599653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 299699653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 299799653d4eSeschrock */ 299899653d4eSeschrock nvlist_free(label); 299999653d4eSeschrock return (0); 300099653d4eSeschrock } 300199653d4eSeschrock 30025cabbc6bSPrashanth Sreenivasa /* 30035cabbc6bSPrashanth Sreenivasa * Free the objects used to store this vdev's spacemaps, and the array 30045cabbc6bSPrashanth Sreenivasa * that points to them. 30055cabbc6bSPrashanth Sreenivasa */ 300688ecc943SGeorge Wilson void 30075cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 30085cabbc6bSPrashanth Sreenivasa { 30095cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0) 30105cabbc6bSPrashanth Sreenivasa return; 30115cabbc6bSPrashanth Sreenivasa 30125cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 30135cabbc6bSPrashanth Sreenivasa uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 30145cabbc6bSPrashanth Sreenivasa size_t array_bytes = array_count * sizeof (uint64_t); 30155cabbc6bSPrashanth Sreenivasa uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 30165cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 30175cabbc6bSPrashanth Sreenivasa array_bytes, smobj_array, 0)); 30185cabbc6bSPrashanth Sreenivasa 30195cabbc6bSPrashanth Sreenivasa for (uint64_t i = 0; i < array_count; i++) { 30205cabbc6bSPrashanth Sreenivasa uint64_t smobj = smobj_array[i]; 30215cabbc6bSPrashanth Sreenivasa if (smobj == 0) 30225cabbc6bSPrashanth Sreenivasa continue; 30235cabbc6bSPrashanth Sreenivasa 30245cabbc6bSPrashanth Sreenivasa space_map_free_obj(mos, smobj, tx); 30255cabbc6bSPrashanth Sreenivasa } 30265cabbc6bSPrashanth Sreenivasa 30275cabbc6bSPrashanth Sreenivasa kmem_free(smobj_array, array_bytes); 30285cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 30295cabbc6bSPrashanth Sreenivasa vd->vdev_ms_array = 0; 30305cabbc6bSPrashanth Sreenivasa } 30315cabbc6bSPrashanth Sreenivasa 30325cabbc6bSPrashanth Sreenivasa static void 30334e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 303488ecc943SGeorge Wilson { 303588ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 303688ecc943SGeorge Wilson 30374e75ba68SSerapheim Dimitropoulos ASSERT(vd->vdev_islog); 3038215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 3039215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 304088ecc943SGeorge Wilson 30414e75ba68SSerapheim Dimitropoulos dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3042215198a6SJoe Stein 30434e75ba68SSerapheim Dimitropoulos vdev_destroy_spacemaps(vd, tx); 30444e75ba68SSerapheim Dimitropoulos if (vd->vdev_top_zap != 0) { 3045215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3046215198a6SJoe Stein vd->vdev_top_zap = 0; 3047215198a6SJoe Stein } 30484e75ba68SSerapheim Dimitropoulos 304988ecc943SGeorge Wilson dmu_tx_commit(tx); 305088ecc943SGeorge Wilson } 305188ecc943SGeorge Wilson 3052fa9e4066Sahrens void 3053fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 3054fa9e4066Sahrens { 3055fa9e4066Sahrens metaslab_t *msp; 305680eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3057fa9e4066Sahrens 30585cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 305988ecc943SGeorge Wilson 3060094e47e9SGeorge Wilson while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3061094e47e9SGeorge Wilson != NULL) 3062fa9e4066Sahrens metaslab_sync_done(msp, txg); 306380eb36f2SGeorge Wilson 306480eb36f2SGeorge Wilson if (reassess) 306580eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 3066fa9e4066Sahrens } 3067fa9e4066Sahrens 3068fa9e4066Sahrens void 3069fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 3070fa9e4066Sahrens { 3071fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3072fa9e4066Sahrens vdev_t *lvd; 3073fa9e4066Sahrens metaslab_t *msp; 3074fa9e4066Sahrens 3075555d674dSSerapheim Dimitropoulos ASSERT3U(txg, ==, spa->spa_syncing_txg); 3076555d674dSSerapheim Dimitropoulos dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 30775cabbc6bSPrashanth Sreenivasa if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 30785cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 30795cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 308088ecc943SGeorge Wilson 30815cabbc6bSPrashanth Sreenivasa vdev_indirect_sync_obsolete(vd, tx); 30825cabbc6bSPrashanth Sreenivasa 30835cabbc6bSPrashanth Sreenivasa /* 30845cabbc6bSPrashanth Sreenivasa * If the vdev is indirect, it can't have dirty 30855cabbc6bSPrashanth Sreenivasa * metaslabs or DTLs. 30865cabbc6bSPrashanth Sreenivasa */ 30875cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 30885cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 30895cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 3090555d674dSSerapheim Dimitropoulos dmu_tx_commit(tx); 30915cabbc6bSPrashanth Sreenivasa return; 30925cabbc6bSPrashanth Sreenivasa } 30935cabbc6bSPrashanth Sreenivasa } 30945cabbc6bSPrashanth Sreenivasa 30955cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 30965cabbc6bSPrashanth Sreenivasa 30975cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 30985cabbc6bSPrashanth Sreenivasa !vd->vdev_removing) { 3099ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 31005cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3101ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3102ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3103ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 3104ecc2d604Sbonwick vdev_config_dirty(vd); 3105ecc2d604Sbonwick } 3106fa9e4066Sahrens 3107ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3108fa9e4066Sahrens metaslab_sync(msp, txg); 3109ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3110ecc2d604Sbonwick } 3111fa9e4066Sahrens 3112fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3113fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 3114fa9e4066Sahrens 31155cabbc6bSPrashanth Sreenivasa /* 31164e75ba68SSerapheim Dimitropoulos * If this is an empty log device being removed, destroy the 31174e75ba68SSerapheim Dimitropoulos * metadata associated with it. 31185cabbc6bSPrashanth Sreenivasa */ 31194e75ba68SSerapheim Dimitropoulos if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 31204e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vd, txg); 31215cabbc6bSPrashanth Sreenivasa 3122fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3123555d674dSSerapheim Dimitropoulos dmu_tx_commit(tx); 3124fa9e4066Sahrens } 3125fa9e4066Sahrens 3126fa9e4066Sahrens uint64_t 3127fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3128fa9e4066Sahrens { 3129fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3130fa9e4066Sahrens } 3131fa9e4066Sahrens 31323d7072f8Seschrock /* 31333d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 31343d7072f8Seschrock * not be opened, and no I/O is attempted. 31353d7072f8Seschrock */ 3136fa9e4066Sahrens int 3137069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3138fa9e4066Sahrens { 31394b964adaSGeorge Wilson vdev_t *vd, *tvd; 3140fa9e4066Sahrens 31418f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 3142fa9e4066Sahrens 3143c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3144e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3145e14bb325SJeff Bonwick 31463d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3147e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3148fa9e4066Sahrens 31494b964adaSGeorge Wilson tvd = vd->vdev_top; 31504b964adaSGeorge Wilson 3151069f55e2SEric Schrock /* 3152069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 3153069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 3154069f55e2SEric Schrock * were faulted. 3155069f55e2SEric Schrock */ 3156069f55e2SEric Schrock vd->vdev_label_aux = aux; 3157069f55e2SEric Schrock 31583d7072f8Seschrock /* 31593d7072f8Seschrock * Faulted state takes precedence over degraded. 31603d7072f8Seschrock */ 316198d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 31623d7072f8Seschrock vd->vdev_faulted = 1ULL; 31633d7072f8Seschrock vd->vdev_degraded = 0ULL; 3164069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 31653d7072f8Seschrock 31663d7072f8Seschrock /* 3167c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 3168c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 31693d7072f8Seschrock */ 31704b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 31713d7072f8Seschrock vd->vdev_degraded = 1ULL; 31723d7072f8Seschrock vd->vdev_faulted = 0ULL; 31733d7072f8Seschrock 31743d7072f8Seschrock /* 31753d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 31763d7072f8Seschrock * mark it degraded. 31773d7072f8Seschrock */ 31784b964adaSGeorge Wilson vdev_reopen(tvd); 31793d7072f8Seschrock 3180069f55e2SEric Schrock if (vdev_readable(vd)) 3181069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 31823d7072f8Seschrock } 31833d7072f8Seschrock 3184e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 31853d7072f8Seschrock } 31863d7072f8Seschrock 31873d7072f8Seschrock /* 31883d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 31893d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 31903d7072f8Seschrock * as I/O is concerned. 31913d7072f8Seschrock */ 31923d7072f8Seschrock int 3193069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 31943d7072f8Seschrock { 3195c5904d13Seschrock vdev_t *vd; 31960a4e9518Sgw 31978f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 31983d7072f8Seschrock 3199c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3200e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3201e14bb325SJeff Bonwick 32020e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3203e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 32040e34b6a7Sbonwick 32053d7072f8Seschrock /* 32063d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 32073d7072f8Seschrock */ 3208e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 3209e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 32103d7072f8Seschrock 32113d7072f8Seschrock vd->vdev_degraded = 1ULL; 32123d7072f8Seschrock if (!vdev_is_dead(vd)) 32133d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3214069f55e2SEric Schrock aux); 32153d7072f8Seschrock 3216e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 32173d7072f8Seschrock } 32183d7072f8Seschrock 32193d7072f8Seschrock /* 3220f7170741SWill Andrews * Online the given vdev. 3221f7170741SWill Andrews * 3222f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3223f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 3224f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 3225f7170741SWill Andrews * events are generated if the device fails to open. 32263d7072f8Seschrock */ 32273d7072f8Seschrock int 3228e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 32293d7072f8Seschrock { 3230573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 32315f368aefSYuri Pankov boolean_t wasoffline; 32325f368aefSYuri Pankov vdev_state_t oldstate; 32333d7072f8Seschrock 32348f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 32353d7072f8Seschrock 3236c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3237e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 32383d7072f8Seschrock 32393d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3240e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3241fa9e4066Sahrens 32425f368aefSYuri Pankov wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 32435f368aefSYuri Pankov oldstate = vd->vdev_state; 324414372834SHans Rosenfeld 3245573ca77eSGeorge Wilson tvd = vd->vdev_top; 3246fa9e4066Sahrens vd->vdev_offline = B_FALSE; 3247441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 3248e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3249e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3250573ca77eSGeorge Wilson 3251573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3252573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3253573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3254573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3255573ca77eSGeorge Wilson } 3256573ca77eSGeorge Wilson 3257573ca77eSGeorge Wilson vdev_reopen(tvd); 32583d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 32593d7072f8Seschrock 3260573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3261573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3262573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 3263573ca77eSGeorge Wilson } 3264573ca77eSGeorge Wilson 32653d7072f8Seschrock if (newstate) 32663d7072f8Seschrock *newstate = vd->vdev_state; 32673d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 32683d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 32693d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 32703d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 32713d7072f8Seschrock vd->vdev_unspare = B_TRUE; 3272fa9e4066Sahrens 3273573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3274573ca77eSGeorge Wilson 3275573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3276573ca77eSGeorge Wilson if (vd->vdev_aux) 3277573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3278573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3279573ca77eSGeorge Wilson } 328014372834SHans Rosenfeld 3281094e47e9SGeorge Wilson /* Restart initializing if necessary */ 3282094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 3283094e47e9SGeorge Wilson if (vdev_writeable(vd) && 3284094e47e9SGeorge Wilson vd->vdev_initialize_thread == NULL && 3285094e47e9SGeorge Wilson vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3286094e47e9SGeorge Wilson (void) vdev_initialize(vd); 3287094e47e9SGeorge Wilson } 3288094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 3289094e47e9SGeorge Wilson 32905f368aefSYuri Pankov if (wasoffline || 32915f368aefSYuri Pankov (oldstate < VDEV_STATE_DEGRADED && 32925f368aefSYuri Pankov vd->vdev_state >= VDEV_STATE_DEGRADED)) 3293ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 329414372834SHans Rosenfeld 32958ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 3296fa9e4066Sahrens } 3297fa9e4066Sahrens 3298a1521560SJeff Bonwick static int 3299a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3300fa9e4066Sahrens { 3301e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 33028f18d1faSGeorge Wilson int error = 0; 33038f18d1faSGeorge Wilson uint64_t generation; 33048f18d1faSGeorge Wilson metaslab_group_t *mg; 33050a4e9518Sgw 33068f18d1faSGeorge Wilson top: 33078f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 3308fa9e4066Sahrens 3309c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3310e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3311fa9e4066Sahrens 33120e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3313e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 33140e34b6a7Sbonwick 3315e6ca193dSGeorge Wilson tvd = vd->vdev_top; 33168f18d1faSGeorge Wilson mg = tvd->vdev_mg; 33178f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 3318e6ca193dSGeorge Wilson 3319fa9e4066Sahrens /* 3320ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 3321fa9e4066Sahrens */ 3322ecc2d604Sbonwick if (!vd->vdev_offline) { 3323ecc2d604Sbonwick /* 33248ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 3325e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 3326e6ca193dSGeorge Wilson * expendable. 3327ecc2d604Sbonwick */ 3328e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3329e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 3330e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3331fa9e4066Sahrens 33328f18d1faSGeorge Wilson /* 3333b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 3334b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 3335b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 3336b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 33378f18d1faSGeorge Wilson */ 33388f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 33398f18d1faSGeorge Wilson /* 33408f18d1faSGeorge Wilson * Prevent any future allocations. 33418f18d1faSGeorge Wilson */ 3342a1521560SJeff Bonwick metaslab_group_passivate(mg); 33438f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 33448f18d1faSGeorge Wilson 33455cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 33468f18d1faSGeorge Wilson 334786714001SSerapheim Dimitropoulos /* 334886714001SSerapheim Dimitropoulos * If the log device was successfully reset but has 334986714001SSerapheim Dimitropoulos * checkpointed data, do not offline it. 335086714001SSerapheim Dimitropoulos */ 335186714001SSerapheim Dimitropoulos if (error == 0 && 335286714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm != NULL) { 335386714001SSerapheim Dimitropoulos error = ZFS_ERR_CHECKPOINT_EXISTS; 335486714001SSerapheim Dimitropoulos } 335586714001SSerapheim Dimitropoulos 33568f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 33578f18d1faSGeorge Wilson 33588f18d1faSGeorge Wilson /* 33598f18d1faSGeorge Wilson * Check to see if the config has changed. 33608f18d1faSGeorge Wilson */ 33618f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 3362a1521560SJeff Bonwick metaslab_group_activate(mg); 33638f18d1faSGeorge Wilson if (error) 33648f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 33658f18d1faSGeorge Wilson vd, error)); 33668f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 33678f18d1faSGeorge Wilson goto top; 33688f18d1faSGeorge Wilson } 3369fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 33708f18d1faSGeorge Wilson } 33718f18d1faSGeorge Wilson 3372ecc2d604Sbonwick /* 3373ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 3374e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 3375e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 3376e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 3377ecc2d604Sbonwick */ 3378ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 3379e6ca193dSGeorge Wilson vdev_reopen(tvd); 3380e6ca193dSGeorge Wilson 3381e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3382e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 3383ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 3384e6ca193dSGeorge Wilson vdev_reopen(tvd); 3385e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3386ecc2d604Sbonwick } 33878f18d1faSGeorge Wilson 33888f18d1faSGeorge Wilson /* 33898f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 33908f18d1faSGeorge Wilson * once we online the device it's open for business. 33918f18d1faSGeorge Wilson */ 33928f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 3393a1521560SJeff Bonwick metaslab_group_activate(mg); 3394fa9e4066Sahrens } 3395fa9e4066Sahrens 3396e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3397ecc2d604Sbonwick 33988f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 3399fa9e4066Sahrens } 3400fa9e4066Sahrens 3401a1521560SJeff Bonwick int 3402a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3403a1521560SJeff Bonwick { 3404a1521560SJeff Bonwick int error; 3405a1521560SJeff Bonwick 3406a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 3407a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 3408a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 3409a1521560SJeff Bonwick 3410a1521560SJeff Bonwick return (error); 3411a1521560SJeff Bonwick } 3412a1521560SJeff Bonwick 3413ea8dc4b6Seschrock /* 3414ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 3415ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 3416ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3417ea8dc4b6Seschrock */ 3418ea8dc4b6Seschrock void 3419e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 3420fa9e4066Sahrens { 3421e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3422e14bb325SJeff Bonwick 3423e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3424fa9e4066Sahrens 3425ea8dc4b6Seschrock if (vd == NULL) 3426e14bb325SJeff Bonwick vd = rvd; 3427fa9e4066Sahrens 3428ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 3429ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 3430ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 3431fa9e4066Sahrens 3432e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3433e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 34343d7072f8Seschrock 34355cabbc6bSPrashanth Sreenivasa /* 34365cabbc6bSPrashanth Sreenivasa * It makes no sense to "clear" an indirect vdev. 34375cabbc6bSPrashanth Sreenivasa */ 34385cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(vd)) 34395cabbc6bSPrashanth Sreenivasa return; 34405cabbc6bSPrashanth Sreenivasa 34413d7072f8Seschrock /* 34428a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 34438a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 34448a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 34458a79c1b5Sek * written out to disk. 34463d7072f8Seschrock */ 3447e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 3448e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 34498a79c1b5Sek 3450096d22d4SEric Schrock /* 3451096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 3452096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 3453096d22d4SEric Schrock * still broken, we want to still post the ereport again. 3454096d22d4SEric Schrock */ 3455096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 3456096d22d4SEric Schrock 34574b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3458e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 3459e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 3460e14bb325SJeff Bonwick 3461f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 34623d7072f8Seschrock 3463096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 3464096d22d4SEric Schrock 3465f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 3466e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3467e14bb325SJeff Bonwick 3468e14bb325SJeff Bonwick if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3469bb8b5132Sek spa_async_request(spa, SPA_ASYNC_RESILVER); 34703d7072f8Seschrock 3471ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 34723d7072f8Seschrock } 3473096d22d4SEric Schrock 3474096d22d4SEric Schrock /* 3475096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 3476096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 3477096d22d4SEric Schrock * done in response to the FMA fault. 3478096d22d4SEric Schrock */ 3479096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3480096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3481096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 3482096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 3483fa9e4066Sahrens } 3484fa9e4066Sahrens 3485e14bb325SJeff Bonwick boolean_t 3486e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 34870a4e9518Sgw { 348888ecc943SGeorge Wilson /* 348988ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 349088ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 349188ecc943SGeorge Wilson * these types of devices in the various code paths. 349288ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 349388ecc943SGeorge Wilson * before issuing I/O to them. 349488ecc943SGeorge Wilson */ 34955cabbc6bSPrashanth Sreenivasa return (vd->vdev_state < VDEV_STATE_DEGRADED || 34965cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_hole_ops || 349788ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 34980a4e9518Sgw } 34990a4e9518Sgw 3500e14bb325SJeff Bonwick boolean_t 3501e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 35020a4e9518Sgw { 3503e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 35040a4e9518Sgw } 35050a4e9518Sgw 3506e14bb325SJeff Bonwick boolean_t 3507e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 3508fa9e4066Sahrens { 35095cabbc6bSPrashanth Sreenivasa return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 35105cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)); 3511fa9e4066Sahrens } 3512fa9e4066Sahrens 3513a31e6787SGeorge Wilson boolean_t 3514a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 3515a31e6787SGeorge Wilson { 35168ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 35178ad4d6ddSJeff Bonwick 3518a31e6787SGeorge Wilson /* 35198ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 3520a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3521a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 35228ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 35238ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 35248ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 3525a31e6787SGeorge Wilson */ 35268ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 35275cabbc6bSPrashanth Sreenivasa !vd->vdev_cant_write && vdev_is_concrete(vd) && 35280f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 3529a31e6787SGeorge Wilson } 3530a31e6787SGeorge Wilson 3531e14bb325SJeff Bonwick boolean_t 3532e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 3533fa9e4066Sahrens { 3534e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 3535fa9e4066Sahrens 3536e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3537e14bb325SJeff Bonwick return (B_FALSE); 3538fa9e4066Sahrens 3539e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 3540e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 3541fa9e4066Sahrens 3542e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3543e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 3544fa9e4066Sahrens 3545e14bb325SJeff Bonwick return (B_TRUE); 3546fa9e4066Sahrens } 3547fa9e4066Sahrens 354886714001SSerapheim Dimitropoulos boolean_t 354986714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd) 355086714001SSerapheim Dimitropoulos { 3551a0b03b16SSerapheim Dimitropoulos if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 3552a0b03b16SSerapheim Dimitropoulos return (B_TRUE); 3553a0b03b16SSerapheim Dimitropoulos 355486714001SSerapheim Dimitropoulos /* 3555a0b03b16SSerapheim Dimitropoulos * If double-word space map entries are not enabled we assume 3556a0b03b16SSerapheim Dimitropoulos * 47 bits of the space map entry are dedicated to the entry's 3557a0b03b16SSerapheim Dimitropoulos * offset (see SM_OFFSET_BITS in space_map.h). We then use that 3558a0b03b16SSerapheim Dimitropoulos * to calculate the maximum address that can be described by a 3559a0b03b16SSerapheim Dimitropoulos * space map entry for the given device. 356086714001SSerapheim Dimitropoulos */ 3561a0b03b16SSerapheim Dimitropoulos uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 356286714001SSerapheim Dimitropoulos 356386714001SSerapheim Dimitropoulos if (shift >= 63) /* detect potential overflow */ 356486714001SSerapheim Dimitropoulos return (B_TRUE); 356586714001SSerapheim Dimitropoulos 356686714001SSerapheim Dimitropoulos return (vd->vdev_asize < (1ULL << shift)); 356786714001SSerapheim Dimitropoulos } 356886714001SSerapheim Dimitropoulos 3569fa9e4066Sahrens /* 3570fa9e4066Sahrens * Get statistics for the given vdev. 3571fa9e4066Sahrens */ 3572fa9e4066Sahrens void 3573fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3574fa9e4066Sahrens { 35752e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 35762e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 3577c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 35782e4c9986SGeorge Wilson 35792e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3580fa9e4066Sahrens 3581fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3582fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3583fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3584fa9e4066Sahrens vs->vs_state = vd->vdev_state; 3585573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 3586094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3587573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3588094e47e9SGeorge Wilson /* 3589094e47e9SGeorge Wilson * Report intializing progress. Since we don't have the 3590094e47e9SGeorge Wilson * initializing locks held, this is only an estimate (although a 3591094e47e9SGeorge Wilson * fairly accurate one). 3592094e47e9SGeorge Wilson */ 3593094e47e9SGeorge Wilson vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done; 3594094e47e9SGeorge Wilson vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est; 3595094e47e9SGeorge Wilson vs->vs_initialize_state = vd->vdev_initialize_state; 3596094e47e9SGeorge Wilson vs->vs_initialize_action_time = vd->vdev_initialize_action_time; 3597094e47e9SGeorge Wilson } 3598c39a2aaeSGeorge Wilson /* 3599c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 3600c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 3601c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 3602c39a2aaeSGeorge Wilson */ 3603c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 36047855d95bSToomas Soome vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 36057855d95bSToomas Soome spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3606c39a2aaeSGeorge Wilson } 36075cabbc6bSPrashanth Sreenivasa if (vd->vdev_aux == NULL && vd == vd->vdev_top && 36085cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3609663207adSDon Brady vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 3610663207adSDon Brady vd->vdev_mg->mg_fragmentation : 0; 36112986efa8SAlex Reece } 3612fa9e4066Sahrens 3613fa9e4066Sahrens /* 3614fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 3615fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 3616fa9e4066Sahrens */ 3617fa9e4066Sahrens if (vd == rvd) { 3618e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 3619fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 3620fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 3621fa9e4066Sahrens 3622e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 3623fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 3624fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 3625fa9e4066Sahrens } 36263f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 3627fa9e4066Sahrens } 3628fa9e4066Sahrens } 36292e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 3630fa9e4066Sahrens } 3631fa9e4066Sahrens 3632fa94a07fSbrendan void 3633fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 3634fa94a07fSbrendan { 3635fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 3636fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 3637fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 3638fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 3639fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 3640fa94a07fSbrendan } 3641fa94a07fSbrendan 36423f9d6ad7SLin Ling void 36433f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 36443f9d6ad7SLin Ling { 36453f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 36463f9d6ad7SLin Ling 36473f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 36483f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 36493f9d6ad7SLin Ling 36503f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 36513f9d6ad7SLin Ling vs->vs_scan_processed = 0; 36523f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 36533f9d6ad7SLin Ling } 36543f9d6ad7SLin Ling 3655fa9e4066Sahrens void 3656e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 3657fa9e4066Sahrens { 36588ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 36598ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3660e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3661fa9e4066Sahrens vdev_t *pvd; 3662fa9e4066Sahrens uint64_t txg = zio->io_txg; 3663fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 3664fa9e4066Sahrens zio_type_t type = zio->io_type; 3665fa9e4066Sahrens int flags = zio->io_flags; 3666fa9e4066Sahrens 3667e14bb325SJeff Bonwick /* 3668e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 3669e14bb325SJeff Bonwick */ 3670e14bb325SJeff Bonwick if (zio->io_gang_tree) 3671e14bb325SJeff Bonwick return; 3672e14bb325SJeff Bonwick 3673fa9e4066Sahrens if (zio->io_error == 0) { 3674e14bb325SJeff Bonwick /* 3675e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 3676e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 3677e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 3678e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 3679e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 3680e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 3681e14bb325SJeff Bonwick * remain zero, which is what we want.) 3682e14bb325SJeff Bonwick * 3683e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 3684e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 3685e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 3686e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 3687e14bb325SJeff Bonwick */ 3688e14bb325SJeff Bonwick if (vd == rvd) 3689e14bb325SJeff Bonwick return; 3690e14bb325SJeff Bonwick 3691e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 36928ad4d6ddSJeff Bonwick 36938ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 36948ad4d6ddSJeff Bonwick return; 36958ad4d6ddSJeff Bonwick 36968ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 36978ad4d6ddSJeff Bonwick 3698e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 369944ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 37003f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 37013f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 37023f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 37033f9d6ad7SLin Ling 37043f9d6ad7SLin Ling /* XXX cleanup? */ 37053f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 37063f9d6ad7SLin Ling atomic_add_64(processed, psize); 37073f9d6ad7SLin Ling vs->vs_scan_processed += psize; 37083f9d6ad7SLin Ling } 37093f9d6ad7SLin Ling 37108ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 3711e14bb325SJeff Bonwick vs->vs_self_healed += psize; 3712fa9e4066Sahrens } 37138ad4d6ddSJeff Bonwick 37148ad4d6ddSJeff Bonwick vs->vs_ops[type]++; 37158ad4d6ddSJeff Bonwick vs->vs_bytes[type] += psize; 37168ad4d6ddSJeff Bonwick 37178ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3718fa9e4066Sahrens return; 3719fa9e4066Sahrens } 3720fa9e4066Sahrens 3721fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 3722fa9e4066Sahrens return; 3723fa9e4066Sahrens 37248956713aSEric Schrock /* 37258956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 37268956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 37278956713aSEric Schrock * hard errors, when in reality they can happen for any number of 37288956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 37298956713aSEric Schrock */ 37308956713aSEric Schrock if (zio->io_error == EIO && 37318956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 37328956713aSEric Schrock return; 37338956713aSEric Schrock 37348f18d1faSGeorge Wilson /* 37358f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 37368f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 37378f18d1faSGeorge Wilson * errors. 37388f18d1faSGeorge Wilson */ 37398f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 37408f18d1faSGeorge Wilson return; 37418f18d1faSGeorge Wilson 3742e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 3743b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3744e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 3745e14bb325SJeff Bonwick vs->vs_checksum_errors++; 3746e14bb325SJeff Bonwick else 3747e14bb325SJeff Bonwick vs->vs_read_errors++; 3748fa9e4066Sahrens } 3749b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3750e14bb325SJeff Bonwick vs->vs_write_errors++; 3751e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3752fa9e4066Sahrens 37535cabbc6bSPrashanth Sreenivasa if (spa->spa_load_state == SPA_LOAD_NONE && 37545cabbc6bSPrashanth Sreenivasa type == ZIO_TYPE_WRITE && txg != 0 && 37558ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 375644ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 3757b24ab676SJeff Bonwick spa->spa_claiming)) { 37588ad4d6ddSJeff Bonwick /* 3759b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 3760b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 3761b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 3762b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 3763b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 3764b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 3765b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 3766b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 37678ad4d6ddSJeff Bonwick * 37688ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 37698ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 37708ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 37718ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 37728ad4d6ddSJeff Bonwick */ 37738ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 37748ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 377544ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 37768ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 37778ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 37788ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3779b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 3780b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 3781b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3782b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 37838ad4d6ddSJeff Bonwick } 3784b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 37858ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3786fa9e4066Sahrens return; 37878ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 37888ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 37898ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3790fa9e4066Sahrens } 37918ad4d6ddSJeff Bonwick if (vd != rvd) 37928ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3793fa9e4066Sahrens } 3794fa9e4066Sahrens } 3795fa9e4066Sahrens 3796663207adSDon Brady int64_t 3797663207adSDon Brady vdev_deflated_space(vdev_t *vd, int64_t space) 3798663207adSDon Brady { 3799663207adSDon Brady ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 3800663207adSDon Brady ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 3801663207adSDon Brady 3802663207adSDon Brady return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 3803663207adSDon Brady } 3804663207adSDon Brady 3805fa9e4066Sahrens /* 3806663207adSDon Brady * Update the in-core space usage stats for this vdev and the root vdev. 3807fa9e4066Sahrens */ 3808fa9e4066Sahrens void 3809b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3810b24ab676SJeff Bonwick int64_t space_delta) 3811fa9e4066Sahrens { 3812663207adSDon Brady int64_t dspace_delta; 38138654d025Sperrin spa_t *spa = vd->vdev_spa; 38148654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 3815fa9e4066Sahrens 38168654d025Sperrin ASSERT(vd == vd->vdev_top); 381799653d4eSeschrock 38188654d025Sperrin /* 38198654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 38208654d025Sperrin * factor. We must calculate this here and not at the root vdev 38218654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 38228654d025Sperrin * childrens', thus not accurate enough for us. 38238654d025Sperrin */ 3824663207adSDon Brady dspace_delta = vdev_deflated_space(vd, space_delta); 38258654d025Sperrin 38268654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 38278654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 3828b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 38298654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 38308654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 38318654d025Sperrin 3832663207adSDon Brady /* every class but log contributes to root space stats */ 3833663207adSDon Brady if (vd->vdev_mg != NULL && !vd->vdev_islog) { 3834fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 3835fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 3836b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 3837fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 3838fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 3839fa94a07fSbrendan } 3840663207adSDon Brady /* Note: metaslab_class_space_update moved to metaslab_space_update */ 3841fa9e4066Sahrens } 3842fa9e4066Sahrens 3843fa9e4066Sahrens /* 3844fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3845fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3846fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3847fa9e4066Sahrens */ 3848fa9e4066Sahrens void 3849fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3850fa9e4066Sahrens { 3851fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3852fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3853fa9e4066Sahrens int c; 3854fa9e4066Sahrens 3855f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3856f9af39baSGeorge Wilson 3857c5904d13Seschrock /* 38586809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 38596809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3860c5904d13Seschrock */ 3861c5904d13Seschrock if (vd->vdev_aux != NULL) { 3862c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3863c5904d13Seschrock nvlist_t **aux; 3864c5904d13Seschrock uint_t naux; 3865c5904d13Seschrock 3866c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3867c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3868c5904d13Seschrock break; 3869c5904d13Seschrock } 3870c5904d13Seschrock 3871e14bb325SJeff Bonwick if (c == sav->sav_count) { 3872e14bb325SJeff Bonwick /* 3873e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3874e14bb325SJeff Bonwick */ 3875e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3876e14bb325SJeff Bonwick return; 3877e14bb325SJeff Bonwick } 3878e14bb325SJeff Bonwick 3879c5904d13Seschrock sav->sav_sync = B_TRUE; 3880c5904d13Seschrock 38816809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 38826809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 38836809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 38846809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 38856809eb4eSEric Schrock } 3886c5904d13Seschrock 3887c5904d13Seschrock ASSERT(c < naux); 3888c5904d13Seschrock 3889c5904d13Seschrock /* 3890c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 3891c5904d13Seschrock * sketchy, but it will work. 3892c5904d13Seschrock */ 3893c5904d13Seschrock nvlist_free(aux[c]); 38943f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3895c5904d13Seschrock 3896c5904d13Seschrock return; 3897c5904d13Seschrock } 3898c5904d13Seschrock 38995dabedeeSbonwick /* 3900e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 3901e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 3902e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 39035dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 39045dabedeeSbonwick */ 3905e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3906e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3907e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 39085dabedeeSbonwick 3909fa9e4066Sahrens if (vd == rvd) { 3910fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 3911fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 3912fa9e4066Sahrens } else { 3913fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 3914fa9e4066Sahrens 391588ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 39165cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3917e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 39185cabbc6bSPrashanth Sreenivasa } 3919fa9e4066Sahrens } 3920fa9e4066Sahrens } 3921fa9e4066Sahrens 3922fa9e4066Sahrens void 3923fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 3924fa9e4066Sahrens { 39255dabedeeSbonwick spa_t *spa = vd->vdev_spa; 39265dabedeeSbonwick 3927e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3928e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3929e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 39305dabedeeSbonwick 3931e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3932e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 3933e14bb325SJeff Bonwick } 3934e14bb325SJeff Bonwick 3935e14bb325SJeff Bonwick /* 3936e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 3937e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3938e14bb325SJeff Bonwick * the state changes from larger config changes because they require 3939e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 3940e14bb325SJeff Bonwick */ 3941e14bb325SJeff Bonwick void 3942e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 3943e14bb325SJeff Bonwick { 3944e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3945e14bb325SJeff Bonwick 3946f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3947e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3948e14bb325SJeff Bonwick 3949e14bb325SJeff Bonwick /* 3950e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 3951e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 3952e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 3953e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 3954e14bb325SJeff Bonwick */ 3955e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3956e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3957e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3958e14bb325SJeff Bonwick 39595cabbc6bSPrashanth Sreenivasa if (!list_link_active(&vd->vdev_state_dirty_node) && 39605cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) 3961e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 3962e14bb325SJeff Bonwick } 3963e14bb325SJeff Bonwick 3964e14bb325SJeff Bonwick void 3965e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 3966e14bb325SJeff Bonwick { 3967e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3968e14bb325SJeff Bonwick 3969e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3970e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3971e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3972e14bb325SJeff Bonwick 3973e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3974e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 3975fa9e4066Sahrens } 3976fa9e4066Sahrens 397732b87932Sek /* 397832b87932Sek * Propagate vdev state up from children to parent. 397932b87932Sek */ 398044cd46caSbillm void 398144cd46caSbillm vdev_propagate_state(vdev_t *vd) 398244cd46caSbillm { 39838ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 39848ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 398544cd46caSbillm int degraded = 0, faulted = 0; 398644cd46caSbillm int corrupted = 0; 398744cd46caSbillm vdev_t *child; 398844cd46caSbillm 39893d7072f8Seschrock if (vd->vdev_children > 0) { 3990573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 39913d7072f8Seschrock child = vd->vdev_child[c]; 399251ece835Seschrock 399388ecc943SGeorge Wilson /* 39945cabbc6bSPrashanth Sreenivasa * Don't factor holes or indirect vdevs into the 39955cabbc6bSPrashanth Sreenivasa * decision. 399688ecc943SGeorge Wilson */ 39975cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(child)) 399888ecc943SGeorge Wilson continue; 399988ecc943SGeorge Wilson 4000e14bb325SJeff Bonwick if (!vdev_readable(child) || 40018ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 400251ece835Seschrock /* 400351ece835Seschrock * Root special: if there is a top-level log 400451ece835Seschrock * device, treat the root vdev as if it were 400551ece835Seschrock * degraded. 400651ece835Seschrock */ 400751ece835Seschrock if (child->vdev_islog && vd == rvd) 400851ece835Seschrock degraded++; 400951ece835Seschrock else 401051ece835Seschrock faulted++; 401151ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 40123d7072f8Seschrock degraded++; 401351ece835Seschrock } 401444cd46caSbillm 40153d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 40163d7072f8Seschrock corrupted++; 40173d7072f8Seschrock } 401844cd46caSbillm 40193d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 40203d7072f8Seschrock 40213d7072f8Seschrock /* 4022e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 40233d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 40243d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 40253d7072f8Seschrock * replicas'. 40263d7072f8Seschrock */ 40273d7072f8Seschrock if (corrupted && vd == rvd && 40283d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 40293d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 40303d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 40313d7072f8Seschrock } 40323d7072f8Seschrock 403351ece835Seschrock if (vd->vdev_parent) 40343d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 403544cd46caSbillm } 403644cd46caSbillm 4037fa9e4066Sahrens /* 4038ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 4039ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 4040ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 4041ea8dc4b6Seschrock * 4042ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 4043ea8dc4b6Seschrock * generated. 4044fa9e4066Sahrens */ 4045fa9e4066Sahrens void 4046ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4047fa9e4066Sahrens { 4048560e6e96Seschrock uint64_t save_state; 4049c5904d13Seschrock spa_t *spa = vd->vdev_spa; 4050ea8dc4b6Seschrock 4051ea8dc4b6Seschrock if (state == vd->vdev_state) { 4052ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 4053fa9e4066Sahrens return; 4054ea8dc4b6Seschrock } 4055ea8dc4b6Seschrock 4056560e6e96Seschrock save_state = vd->vdev_state; 4057fa9e4066Sahrens 4058fa9e4066Sahrens vd->vdev_state = state; 4059fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 4060fa9e4066Sahrens 40613d7072f8Seschrock /* 40623d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 406398d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 406498d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 406598d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 406698d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 406798d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 406898d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 406998d1cbfeSGeorge Wilson * children. 407098d1cbfeSGeorge Wilson */ 407198d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 407298d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 40733d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 40743d7072f8Seschrock 4075069f55e2SEric Schrock /* 4076069f55e2SEric Schrock * If we have brought this vdev back into service, we need 4077069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 4078069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 4079069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 4080069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 4081069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 4082069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 4083069f55e2SEric Schrock */ 4084069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 4085069f55e2SEric Schrock vd->vdev_prevstate != state) 4086069f55e2SEric Schrock zfs_post_state_change(spa, vd); 4087069f55e2SEric Schrock 40883d7072f8Seschrock if (vd->vdev_removed && 40893d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 40903d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 40913d7072f8Seschrock /* 40923d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 40933d7072f8Seschrock * device was previously marked removed and someone attempted to 40943d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 40953d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 40963d7072f8Seschrock * it is one of our special test online cases, which is only 40973d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 40983d7072f8Seschrock * fault. 40993d7072f8Seschrock */ 41003d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 41013d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 41023d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 41033d7072f8Seschrock vd->vdev_removed = B_TRUE; 41043d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 4105ea8dc4b6Seschrock /* 4106cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 4107cb04b873SMark J Musante * mark it as "not available", which signifies that it was 4108cb04b873SMark J Musante * never there to begin with. Failure to open such a device 4109cb04b873SMark J Musante * is not considered an error. 4110ea8dc4b6Seschrock */ 4111cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 4112cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 4113560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 4114560e6e96Seschrock vd->vdev_not_present = 1; 4115560e6e96Seschrock 4116560e6e96Seschrock /* 4117560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 4118560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 4119560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 4120560e6e96Seschrock * want to post the ereport if the device was already in the 4121560e6e96Seschrock * CANT_OPEN state beforehand. 41223d7072f8Seschrock * 41233d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 41243d7072f8Seschrock * online the device in response to an insertion event. If we 41253d7072f8Seschrock * hit this case, then we have detected an insertion event for a 41263d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 41273d7072f8Seschrock * In this scenario, we don't post an ereport because we are 41283d7072f8Seschrock * about to replace the device, or attempt an online with 41293d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 4130560e6e96Seschrock */ 41313d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 41323d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 4133c5904d13Seschrock vd != spa->spa_root_vdev) { 4134ea8dc4b6Seschrock const char *class; 4135ea8dc4b6Seschrock 4136ea8dc4b6Seschrock switch (aux) { 4137ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 4138ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 4139ea8dc4b6Seschrock break; 4140ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 4141ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 4142ea8dc4b6Seschrock break; 4143ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 4144ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 4145ea8dc4b6Seschrock break; 4146ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 4147ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 4148ea8dc4b6Seschrock break; 4149ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 4150ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 4151ea8dc4b6Seschrock break; 4152ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 4153ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 4154ea8dc4b6Seschrock break; 4155ea8dc4b6Seschrock default: 4156ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 4157ea8dc4b6Seschrock } 4158ea8dc4b6Seschrock 4159c5904d13Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 4160ea8dc4b6Seschrock } 4161ea8dc4b6Seschrock 41623d7072f8Seschrock /* Erase any notion of persistent removed state */ 41633d7072f8Seschrock vd->vdev_removed = B_FALSE; 41643d7072f8Seschrock } else { 41653d7072f8Seschrock vd->vdev_removed = B_FALSE; 41663d7072f8Seschrock } 4167ea8dc4b6Seschrock 41688b33d774STim Haley if (!isopen && vd->vdev_parent) 41698b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 4170fa9e4066Sahrens } 417115e6edf1Sgw 41726f793812SPavel Zakharov boolean_t 41736f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd) 41746f793812SPavel Zakharov { 41756f793812SPavel Zakharov ASSERT(!vd->vdev_ops->vdev_op_leaf); 41766f793812SPavel Zakharov 41776f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) { 41786f793812SPavel Zakharov if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 41796f793812SPavel Zakharov return (B_FALSE); 41806f793812SPavel Zakharov } 41816f793812SPavel Zakharov 41826f793812SPavel Zakharov return (B_TRUE); 41836f793812SPavel Zakharov } 41846f793812SPavel Zakharov 418515e6edf1Sgw /* 418615e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 4187c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 4188c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 418915e6edf1Sgw */ 419015e6edf1Sgw boolean_t 419115e6edf1Sgw vdev_is_bootable(vdev_t *vd) 419215e6edf1Sgw { 419315e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 419415e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 419515e6edf1Sgw 419615e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 419715e6edf1Sgw vd->vdev_children > 1) { 419815e6edf1Sgw return (B_FALSE); 41995cabbc6bSPrashanth Sreenivasa } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 42005cabbc6bSPrashanth Sreenivasa strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 420115e6edf1Sgw return (B_FALSE); 420215e6edf1Sgw } 420315e6edf1Sgw } 420415e6edf1Sgw 4205573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 420615e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 420715e6edf1Sgw return (B_FALSE); 420815e6edf1Sgw } 420915e6edf1Sgw return (B_TRUE); 421015e6edf1Sgw } 4211e6ca193dSGeorge Wilson 42125cabbc6bSPrashanth Sreenivasa boolean_t 42135cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd) 42145cabbc6bSPrashanth Sreenivasa { 42155cabbc6bSPrashanth Sreenivasa vdev_ops_t *ops = vd->vdev_ops; 42165cabbc6bSPrashanth Sreenivasa if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 42175cabbc6bSPrashanth Sreenivasa ops == &vdev_missing_ops || ops == &vdev_root_ops) { 42185cabbc6bSPrashanth Sreenivasa return (B_FALSE); 42195cabbc6bSPrashanth Sreenivasa } else { 42205cabbc6bSPrashanth Sreenivasa return (B_TRUE); 42215cabbc6bSPrashanth Sreenivasa } 42225cabbc6bSPrashanth Sreenivasa } 42235cabbc6bSPrashanth Sreenivasa 42244b964adaSGeorge Wilson /* 42254b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 42264b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 42274b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 42284b964adaSGeorge Wilson */ 42294b964adaSGeorge Wilson boolean_t 42304b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 42314b964adaSGeorge Wilson { 42324b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 42334b964adaSGeorge Wilson !vd->vdev_removed) 42344b964adaSGeorge Wilson return (B_TRUE); 42354b964adaSGeorge Wilson 42364b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 42374b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 42384b964adaSGeorge Wilson return (B_TRUE); 42394b964adaSGeorge Wilson 42404b964adaSGeorge Wilson return (B_FALSE); 42414b964adaSGeorge Wilson } 42424b964adaSGeorge Wilson 4243573ca77eSGeorge Wilson /* 4244573ca77eSGeorge Wilson * Expand a vdev if possible. 4245573ca77eSGeorge Wilson */ 4246573ca77eSGeorge Wilson void 4247573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 4248573ca77eSGeorge Wilson { 4249573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 4250573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 425111f6a968SSerapheim Dimitropoulos ASSERT(vdev_is_concrete(vd)); 4252573ca77eSGeorge Wilson 42535cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 42545cabbc6bSPrashanth Sreenivasa 4255663207adSDon Brady if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 4256663207adSDon Brady vdev_is_concrete(vd)) { 4257663207adSDon Brady vdev_metaslab_group_create(vd); 4258573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 4259573ca77eSGeorge Wilson vdev_config_dirty(vd); 4260573ca77eSGeorge Wilson } 4261573ca77eSGeorge Wilson } 42621195e687SMark J Musante 42631195e687SMark J Musante /* 42641195e687SMark J Musante * Split a vdev. 42651195e687SMark J Musante */ 42661195e687SMark J Musante void 42671195e687SMark J Musante vdev_split(vdev_t *vd) 42681195e687SMark J Musante { 42691195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 42701195e687SMark J Musante 42711195e687SMark J Musante vdev_remove_child(pvd, vd); 42721195e687SMark J Musante vdev_compact_children(pvd); 42731195e687SMark J Musante 42741195e687SMark J Musante cvd = pvd->vdev_child[0]; 42751195e687SMark J Musante if (pvd->vdev_children == 1) { 42761195e687SMark J Musante vdev_remove_parent(cvd); 42771195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 42781195e687SMark J Musante } 42791195e687SMark J Musante vdev_propagate_state(cvd); 42801195e687SMark J Musante } 4281283b8460SGeorge.Wilson 4282283b8460SGeorge.Wilson void 4283283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 4284283b8460SGeorge.Wilson { 4285283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 4286283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 4287283b8460SGeorge.Wilson 4288283b8460SGeorge.Wilson vdev_deadman(cvd); 4289283b8460SGeorge.Wilson } 4290283b8460SGeorge.Wilson 4291283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 4292283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 4293283b8460SGeorge.Wilson 4294283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 429569962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 4296283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 4297283b8460SGeorge.Wilson zio_t *fio; 4298283b8460SGeorge.Wilson uint64_t delta; 4299283b8460SGeorge.Wilson 4300283b8460SGeorge.Wilson /* 4301283b8460SGeorge.Wilson * Look at the head of all the pending queues, 4302283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 4303283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 4304283b8460SGeorge.Wilson */ 430569962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 4306c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 4307c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 43083ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 43093ee8c80cSPavel Zakharov "%lluns, delta %lluns, last io %lluns", 43103ee8c80cSPavel Zakharov fio->io_timestamp, (u_longlong_t)delta, 4311283b8460SGeorge.Wilson vq->vq_io_complete_ts); 4312283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 4313283b8460SGeorge.Wilson "hung.", spa_name(spa)); 4314283b8460SGeorge.Wilson } 4315283b8460SGeorge.Wilson } 4316283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 4317283b8460SGeorge.Wilson } 4318283b8460SGeorge.Wilson } 4319