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. 29fa9e4066Sahrens */ 30fa9e4066Sahrens 31fa9e4066Sahrens #include <sys/zfs_context.h> 32ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 33fa9e4066Sahrens #include <sys/spa.h> 34fa9e4066Sahrens #include <sys/spa_impl.h> 355cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h> 36fa9e4066Sahrens #include <sys/dmu.h> 37fa9e4066Sahrens #include <sys/dmu_tx.h> 385cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h> 39fa9e4066Sahrens #include <sys/vdev_impl.h> 40fa9e4066Sahrens #include <sys/uberblock_impl.h> 41fa9e4066Sahrens #include <sys/metaslab.h> 42fa9e4066Sahrens #include <sys/metaslab_impl.h> 43fa9e4066Sahrens #include <sys/space_map.h> 440713e232SGeorge Wilson #include <sys/space_reftree.h> 45fa9e4066Sahrens #include <sys/zio.h> 46fa9e4066Sahrens #include <sys/zap.h> 47fa9e4066Sahrens #include <sys/fs/zfs.h> 48c5904d13Seschrock #include <sys/arc.h> 49e6ca193dSGeorge Wilson #include <sys/zil.h> 503f9d6ad7SLin Ling #include <sys/dsl_scan.h> 51770499e1SDan Kimmel #include <sys/abd.h> 52094e47e9SGeorge Wilson #include <sys/vdev_initialize.h> 53fa9e4066Sahrens 54fa9e4066Sahrens /* 55fa9e4066Sahrens * Virtual device management. 56fa9e4066Sahrens */ 57fa9e4066Sahrens 58fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = { 59fa9e4066Sahrens &vdev_root_ops, 60fa9e4066Sahrens &vdev_raidz_ops, 61fa9e4066Sahrens &vdev_mirror_ops, 62fa9e4066Sahrens &vdev_replacing_ops, 6399653d4eSeschrock &vdev_spare_ops, 64fa9e4066Sahrens &vdev_disk_ops, 65fa9e4066Sahrens &vdev_file_ops, 66fa9e4066Sahrens &vdev_missing_ops, 6788ecc943SGeorge Wilson &vdev_hole_ops, 685cabbc6bSPrashanth Sreenivasa &vdev_indirect_ops, 69fa9e4066Sahrens NULL 70fa9e4066Sahrens }; 71fa9e4066Sahrens 72088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */ 73088f3894Sahrens int zfs_scrub_limit = 10; 7405b2b3b8Smishra 75*b4bf0cf0SDon Brady /* target number of metaslabs per top-level vdev */ 7686714001SSerapheim Dimitropoulos int vdev_max_ms_count = 200; 7786714001SSerapheim Dimitropoulos 78*b4bf0cf0SDon Brady /* minimum number of metaslabs per top-level vdev */ 7986714001SSerapheim Dimitropoulos int vdev_min_ms_count = 16; 8086714001SSerapheim Dimitropoulos 81*b4bf0cf0SDon Brady /* practical upper limit of total metaslabs per top-level vdev */ 82*b4bf0cf0SDon Brady int vdev_ms_count_limit = 1ULL << 17; 83*b4bf0cf0SDon Brady 84*b4bf0cf0SDon Brady /* lower limit for metaslab size (512M) */ 8586714001SSerapheim Dimitropoulos int vdev_default_ms_shift = 29; 8686714001SSerapheim Dimitropoulos 87*b4bf0cf0SDon Brady /* upper limit for metaslab size (256G) */ 88*b4bf0cf0SDon Brady int vdev_max_ms_shift = 38; 89*b4bf0cf0SDon Brady 9086714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE; 9186714001SSerapheim Dimitropoulos 92bf3e216cSMatthew Ahrens /* 9386714001SSerapheim Dimitropoulos * Since the DTL space map of a vdev is not expected to have a lot of 9486714001SSerapheim Dimitropoulos * entries, we default its block size to 4K. 95bf3e216cSMatthew Ahrens */ 9686714001SSerapheim Dimitropoulos int vdev_dtl_sm_blksz = (1 << 12); 97bf3e216cSMatthew Ahrens 9886714001SSerapheim Dimitropoulos /* 9986714001SSerapheim Dimitropoulos * vdev-wide space maps that have lots of entries written to them at 10086714001SSerapheim Dimitropoulos * the end of each transaction can benefit from a higher I/O bandwidth 10186714001SSerapheim Dimitropoulos * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 10286714001SSerapheim Dimitropoulos */ 10386714001SSerapheim Dimitropoulos int vdev_standard_sm_blksz = (1 << 17); 1046f793812SPavel Zakharov 10593a1902eSMatthew Ahrens int zfs_ashift_min; 10693a1902eSMatthew Ahrens 1073ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 1083ee8c80cSPavel Zakharov void 1093ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 1103ee8c80cSPavel Zakharov { 1113ee8c80cSPavel Zakharov va_list adx; 1123ee8c80cSPavel Zakharov char buf[256]; 1133ee8c80cSPavel Zakharov 1143ee8c80cSPavel Zakharov va_start(adx, fmt); 1153ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 1163ee8c80cSPavel Zakharov va_end(adx); 1173ee8c80cSPavel Zakharov 1183ee8c80cSPavel Zakharov if (vd->vdev_path != NULL) { 1193ee8c80cSPavel Zakharov zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 1203ee8c80cSPavel Zakharov vd->vdev_path, buf); 1213ee8c80cSPavel Zakharov } else { 1223ee8c80cSPavel Zakharov zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 1233ee8c80cSPavel Zakharov vd->vdev_ops->vdev_op_type, 1243ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_id, 1253ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_guid, buf); 1263ee8c80cSPavel Zakharov } 1273ee8c80cSPavel Zakharov } 1283ee8c80cSPavel Zakharov 1296f793812SPavel Zakharov void 1306f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 1316f793812SPavel Zakharov { 1326f793812SPavel Zakharov char state[20]; 1336f793812SPavel Zakharov 1346f793812SPavel Zakharov if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 1356f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 1366f793812SPavel Zakharov vd->vdev_ops->vdev_op_type); 1376f793812SPavel Zakharov return; 1386f793812SPavel Zakharov } 1396f793812SPavel Zakharov 1406f793812SPavel Zakharov switch (vd->vdev_state) { 1416f793812SPavel Zakharov case VDEV_STATE_UNKNOWN: 1426f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "unknown"); 1436f793812SPavel Zakharov break; 1446f793812SPavel Zakharov case VDEV_STATE_CLOSED: 1456f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "closed"); 1466f793812SPavel Zakharov break; 1476f793812SPavel Zakharov case VDEV_STATE_OFFLINE: 1486f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "offline"); 1496f793812SPavel Zakharov break; 1506f793812SPavel Zakharov case VDEV_STATE_REMOVED: 1516f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "removed"); 1526f793812SPavel Zakharov break; 1536f793812SPavel Zakharov case VDEV_STATE_CANT_OPEN: 1546f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "can't open"); 1556f793812SPavel Zakharov break; 1566f793812SPavel Zakharov case VDEV_STATE_FAULTED: 1576f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "faulted"); 1586f793812SPavel Zakharov break; 1596f793812SPavel Zakharov case VDEV_STATE_DEGRADED: 1606f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "degraded"); 1616f793812SPavel Zakharov break; 1626f793812SPavel Zakharov case VDEV_STATE_HEALTHY: 1636f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "healthy"); 1646f793812SPavel Zakharov break; 1656f793812SPavel Zakharov default: 1666f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "<state %u>", 1676f793812SPavel Zakharov (uint_t)vd->vdev_state); 1686f793812SPavel Zakharov } 1696f793812SPavel Zakharov 1706f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 1716f793812SPavel Zakharov "", vd->vdev_id, vd->vdev_ops->vdev_op_type, 1726f793812SPavel Zakharov vd->vdev_islog ? " (log)" : "", 1736f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 1746f793812SPavel Zakharov vd->vdev_path ? vd->vdev_path : "N/A", state); 1756f793812SPavel Zakharov 1766f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) 1776f793812SPavel Zakharov vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 1786f793812SPavel Zakharov } 1796f793812SPavel Zakharov 180fa9e4066Sahrens /* 181fa9e4066Sahrens * Given a vdev type, return the appropriate ops vector. 182fa9e4066Sahrens */ 183fa9e4066Sahrens static vdev_ops_t * 184fa9e4066Sahrens vdev_getops(const char *type) 185fa9e4066Sahrens { 186fa9e4066Sahrens vdev_ops_t *ops, **opspp; 187fa9e4066Sahrens 188fa9e4066Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 189fa9e4066Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 190fa9e4066Sahrens break; 191fa9e4066Sahrens 192fa9e4066Sahrens return (ops); 193fa9e4066Sahrens } 194fa9e4066Sahrens 195094e47e9SGeorge Wilson /* ARGSUSED */ 196094e47e9SGeorge Wilson void 197094e47e9SGeorge Wilson vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res) 198094e47e9SGeorge Wilson { 199094e47e9SGeorge Wilson res->rs_start = in->rs_start; 200094e47e9SGeorge Wilson res->rs_end = in->rs_end; 201094e47e9SGeorge Wilson } 202094e47e9SGeorge Wilson 203fa9e4066Sahrens /* 204fa9e4066Sahrens * Default asize function: return the MAX of psize with the asize of 205fa9e4066Sahrens * all children. This is what's used by anything other than RAID-Z. 206fa9e4066Sahrens */ 207fa9e4066Sahrens uint64_t 208fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 209fa9e4066Sahrens { 210ecc2d604Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 211fa9e4066Sahrens uint64_t csize; 212fa9e4066Sahrens 213573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 214fa9e4066Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 215fa9e4066Sahrens asize = MAX(asize, csize); 216fa9e4066Sahrens } 217fa9e4066Sahrens 218fa9e4066Sahrens return (asize); 219fa9e4066Sahrens } 220fa9e4066Sahrens 2212a79c5feSlling /* 222573ca77eSGeorge Wilson * Get the minimum allocatable size. We define the allocatable size as 223573ca77eSGeorge Wilson * the vdev's asize rounded to the nearest metaslab. This allows us to 224573ca77eSGeorge Wilson * replace or attach devices which don't have the same physical size but 225573ca77eSGeorge Wilson * can still satisfy the same number of allocations. 2262a79c5feSlling */ 2272a79c5feSlling uint64_t 228573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd) 2292a79c5feSlling { 230573ca77eSGeorge Wilson vdev_t *pvd = vd->vdev_parent; 2312a79c5feSlling 232573ca77eSGeorge Wilson /* 2334263d13fSGeorge Wilson * If our parent is NULL (inactive spare or cache) or is the root, 234573ca77eSGeorge Wilson * just return our own asize. 235573ca77eSGeorge Wilson */ 236573ca77eSGeorge Wilson if (pvd == NULL) 237573ca77eSGeorge Wilson return (vd->vdev_asize); 2382a79c5feSlling 2392a79c5feSlling /* 240573ca77eSGeorge Wilson * The top-level vdev just returns the allocatable size rounded 241573ca77eSGeorge Wilson * to the nearest metaslab. 2422a79c5feSlling */ 243573ca77eSGeorge Wilson if (vd == vd->vdev_top) 244573ca77eSGeorge Wilson return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 2452a79c5feSlling 246573ca77eSGeorge Wilson /* 247573ca77eSGeorge Wilson * The allocatable space for a raidz vdev is N * sizeof(smallest child), 248573ca77eSGeorge Wilson * so each child must provide at least 1/Nth of its asize. 249573ca77eSGeorge Wilson */ 250573ca77eSGeorge Wilson if (pvd->vdev_ops == &vdev_raidz_ops) 251c040c10cSSteven Hartland return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 252c040c10cSSteven Hartland pvd->vdev_children); 2532a79c5feSlling 254573ca77eSGeorge Wilson return (pvd->vdev_min_asize); 255573ca77eSGeorge Wilson } 2562a79c5feSlling 257573ca77eSGeorge Wilson void 258573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd) 259573ca77eSGeorge Wilson { 260573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 261573ca77eSGeorge Wilson 262573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 263573ca77eSGeorge Wilson vdev_set_min_asize(vd->vdev_child[c]); 2642a79c5feSlling } 2652a79c5feSlling 266fa9e4066Sahrens vdev_t * 267fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 268fa9e4066Sahrens { 269fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 270fa9e4066Sahrens 271e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 272e05725b1Sbonwick 273088f3894Sahrens if (vdev < rvd->vdev_children) { 274088f3894Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 275fa9e4066Sahrens return (rvd->vdev_child[vdev]); 276088f3894Sahrens } 277fa9e4066Sahrens 278fa9e4066Sahrens return (NULL); 279fa9e4066Sahrens } 280fa9e4066Sahrens 281fa9e4066Sahrens vdev_t * 282fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 283fa9e4066Sahrens { 284fa9e4066Sahrens vdev_t *mvd; 285fa9e4066Sahrens 2860e34b6a7Sbonwick if (vd->vdev_guid == guid) 287fa9e4066Sahrens return (vd); 288fa9e4066Sahrens 289573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 290fa9e4066Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 291fa9e4066Sahrens NULL) 292fa9e4066Sahrens return (mvd); 293fa9e4066Sahrens 294fa9e4066Sahrens return (NULL); 295fa9e4066Sahrens } 296fa9e4066Sahrens 29712380e1eSArne Jansen static int 29812380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd) 29912380e1eSArne Jansen { 30012380e1eSArne Jansen int n = 0; 30112380e1eSArne Jansen 30212380e1eSArne Jansen if (vd->vdev_ops->vdev_op_leaf) 30312380e1eSArne Jansen return (1); 30412380e1eSArne Jansen 30512380e1eSArne Jansen for (int c = 0; c < vd->vdev_children; c++) 30612380e1eSArne Jansen n += vdev_count_leaves_impl(vd->vdev_child[c]); 30712380e1eSArne Jansen 30812380e1eSArne Jansen return (n); 30912380e1eSArne Jansen } 31012380e1eSArne Jansen 31112380e1eSArne Jansen int 31212380e1eSArne Jansen vdev_count_leaves(spa_t *spa) 31312380e1eSArne Jansen { 31412380e1eSArne Jansen return (vdev_count_leaves_impl(spa->spa_root_vdev)); 31512380e1eSArne Jansen } 31612380e1eSArne Jansen 317fa9e4066Sahrens void 318fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 319fa9e4066Sahrens { 320fa9e4066Sahrens size_t oldsize, newsize; 321fa9e4066Sahrens uint64_t id = cvd->vdev_id; 322fa9e4066Sahrens vdev_t **newchild; 32381cd5c55SMatthew Ahrens spa_t *spa = cvd->vdev_spa; 324fa9e4066Sahrens 32581cd5c55SMatthew Ahrens ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 326fa9e4066Sahrens ASSERT(cvd->vdev_parent == NULL); 327fa9e4066Sahrens 328fa9e4066Sahrens cvd->vdev_parent = pvd; 329fa9e4066Sahrens 330fa9e4066Sahrens if (pvd == NULL) 331fa9e4066Sahrens return; 332fa9e4066Sahrens 333fa9e4066Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 334fa9e4066Sahrens 335fa9e4066Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 336fa9e4066Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 337fa9e4066Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 338fa9e4066Sahrens 339fa9e4066Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 340fa9e4066Sahrens if (pvd->vdev_child != NULL) { 341fa9e4066Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 342fa9e4066Sahrens kmem_free(pvd->vdev_child, oldsize); 343fa9e4066Sahrens } 344fa9e4066Sahrens 345fa9e4066Sahrens pvd->vdev_child = newchild; 346fa9e4066Sahrens pvd->vdev_child[id] = cvd; 347fa9e4066Sahrens 348fa9e4066Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 349fa9e4066Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 350fa9e4066Sahrens 351fa9e4066Sahrens /* 352fa9e4066Sahrens * Walk up all ancestors to update guid sum. 353fa9e4066Sahrens */ 354fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 355fa9e4066Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 356fa9e4066Sahrens } 357fa9e4066Sahrens 358fa9e4066Sahrens void 359fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 360fa9e4066Sahrens { 361fa9e4066Sahrens int c; 362fa9e4066Sahrens uint_t id = cvd->vdev_id; 363fa9e4066Sahrens 364fa9e4066Sahrens ASSERT(cvd->vdev_parent == pvd); 365fa9e4066Sahrens 366fa9e4066Sahrens if (pvd == NULL) 367fa9e4066Sahrens return; 368fa9e4066Sahrens 369fa9e4066Sahrens ASSERT(id < pvd->vdev_children); 370fa9e4066Sahrens ASSERT(pvd->vdev_child[id] == cvd); 371fa9e4066Sahrens 372fa9e4066Sahrens pvd->vdev_child[id] = NULL; 373fa9e4066Sahrens cvd->vdev_parent = NULL; 374fa9e4066Sahrens 375fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) 376fa9e4066Sahrens if (pvd->vdev_child[c]) 377fa9e4066Sahrens break; 378fa9e4066Sahrens 379fa9e4066Sahrens if (c == pvd->vdev_children) { 380fa9e4066Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 381fa9e4066Sahrens pvd->vdev_child = NULL; 382fa9e4066Sahrens pvd->vdev_children = 0; 383fa9e4066Sahrens } 384fa9e4066Sahrens 385fa9e4066Sahrens /* 386fa9e4066Sahrens * Walk up all ancestors to update guid sum. 387fa9e4066Sahrens */ 388fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 389fa9e4066Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 390fa9e4066Sahrens } 391fa9e4066Sahrens 392fa9e4066Sahrens /* 393fa9e4066Sahrens * Remove any holes in the child array. 394fa9e4066Sahrens */ 395fa9e4066Sahrens void 396fa9e4066Sahrens vdev_compact_children(vdev_t *pvd) 397fa9e4066Sahrens { 398fa9e4066Sahrens vdev_t **newchild, *cvd; 399fa9e4066Sahrens int oldc = pvd->vdev_children; 400573ca77eSGeorge Wilson int newc; 401fa9e4066Sahrens 402e14bb325SJeff Bonwick ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 403fa9e4066Sahrens 404573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) 405fa9e4066Sahrens if (pvd->vdev_child[c]) 406fa9e4066Sahrens newc++; 407fa9e4066Sahrens 408fa9e4066Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 409fa9e4066Sahrens 410573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) { 411fa9e4066Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 412fa9e4066Sahrens newchild[newc] = cvd; 413fa9e4066Sahrens cvd->vdev_id = newc++; 414fa9e4066Sahrens } 415fa9e4066Sahrens } 416fa9e4066Sahrens 417fa9e4066Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 418fa9e4066Sahrens pvd->vdev_child = newchild; 419fa9e4066Sahrens pvd->vdev_children = newc; 420fa9e4066Sahrens } 421fa9e4066Sahrens 422fa9e4066Sahrens /* 423fa9e4066Sahrens * Allocate and minimally initialize a vdev_t. 424fa9e4066Sahrens */ 42588ecc943SGeorge Wilson vdev_t * 426fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 427fa9e4066Sahrens { 428fa9e4066Sahrens vdev_t *vd; 4295cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 430fa9e4066Sahrens 431fa9e4066Sahrens vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 4325cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 433fa9e4066Sahrens 4340e34b6a7Sbonwick if (spa->spa_root_vdev == NULL) { 4350e34b6a7Sbonwick ASSERT(ops == &vdev_root_ops); 4360e34b6a7Sbonwick spa->spa_root_vdev = vd; 437e9103aaeSGarrett D'Amore spa->spa_load_guid = spa_generate_guid(NULL); 4380e34b6a7Sbonwick } 4390e34b6a7Sbonwick 44088ecc943SGeorge Wilson if (guid == 0 && ops != &vdev_hole_ops) { 4410e34b6a7Sbonwick if (spa->spa_root_vdev == vd) { 4420e34b6a7Sbonwick /* 4430e34b6a7Sbonwick * The root vdev's guid will also be the pool guid, 4440e34b6a7Sbonwick * which must be unique among all pools. 4450e34b6a7Sbonwick */ 4461195e687SMark J Musante guid = spa_generate_guid(NULL); 4470e34b6a7Sbonwick } else { 4480e34b6a7Sbonwick /* 4490e34b6a7Sbonwick * Any other vdev's guid must be unique within the pool. 4500e34b6a7Sbonwick */ 4511195e687SMark J Musante guid = spa_generate_guid(spa); 4520e34b6a7Sbonwick } 4530e34b6a7Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 4540e34b6a7Sbonwick } 4550e34b6a7Sbonwick 456fa9e4066Sahrens vd->vdev_spa = spa; 457fa9e4066Sahrens vd->vdev_id = id; 458fa9e4066Sahrens vd->vdev_guid = guid; 459fa9e4066Sahrens vd->vdev_guid_sum = guid; 460fa9e4066Sahrens vd->vdev_ops = ops; 461fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 46288ecc943SGeorge Wilson vd->vdev_ishole = (ops == &vdev_hole_ops); 4635cabbc6bSPrashanth Sreenivasa vic->vic_prev_indirect_vdev = UINT64_MAX; 4645cabbc6bSPrashanth Sreenivasa 4655cabbc6bSPrashanth Sreenivasa rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 4665cabbc6bSPrashanth Sreenivasa mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 4675cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); 468fa9e4066Sahrens 469fa9e4066Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 4705ad82045Snd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 471e14bb325SJeff Bonwick mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 4720f7643c7SGeorge Wilson mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 473094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); 474094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); 475094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); 476094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); 477094e47e9SGeorge Wilson 4788ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 4795cabbc6bSPrashanth Sreenivasa vd->vdev_dtl[t] = range_tree_create(NULL, NULL); 4808ad4d6ddSJeff Bonwick } 481b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_ms_list, spa, 482fa9e4066Sahrens offsetof(struct metaslab, ms_txg_node)); 483b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_dtl_list, spa, 484fa9e4066Sahrens offsetof(struct vdev, vdev_dtl_node)); 485fa9e4066Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 4863d7072f8Seschrock vdev_queue_init(vd); 4873d7072f8Seschrock vdev_cache_init(vd); 488fa9e4066Sahrens 489fa9e4066Sahrens return (vd); 490fa9e4066Sahrens } 491fa9e4066Sahrens 492fa9e4066Sahrens /* 493fa9e4066Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 494fa9e4066Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 495fa9e4066Sahrens * different for each case. 496fa9e4066Sahrens */ 49799653d4eSeschrock int 49899653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 49999653d4eSeschrock int alloctype) 500fa9e4066Sahrens { 501fa9e4066Sahrens vdev_ops_t *ops; 502fa9e4066Sahrens char *type; 5038654d025Sperrin uint64_t guid = 0, islog, nparity; 504fa9e4066Sahrens vdev_t *vd; 5055cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 506fa9e4066Sahrens 507e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 508fa9e4066Sahrens 509fa9e4066Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 510be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 511fa9e4066Sahrens 512fa9e4066Sahrens if ((ops = vdev_getops(type)) == NULL) 513be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 514fa9e4066Sahrens 515fa9e4066Sahrens /* 516fa9e4066Sahrens * If this is a load, get the vdev guid from the nvlist. 517fa9e4066Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 518fa9e4066Sahrens */ 519fa9e4066Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 520fa9e4066Sahrens uint64_t label_id; 521fa9e4066Sahrens 522fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 523fa9e4066Sahrens label_id != id) 524be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 525fa9e4066Sahrens 526fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 527be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 52899653d4eSeschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 52999653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 530be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 531fa94a07fSbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 532fa94a07fSbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 533be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 53421ecdf64SLin Ling } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 53521ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 536be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 537fa9e4066Sahrens } 538fa9e4066Sahrens 53999653d4eSeschrock /* 54099653d4eSeschrock * The first allocated vdev must be of type 'root'. 54199653d4eSeschrock */ 54299653d4eSeschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 543be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 54499653d4eSeschrock 5458654d025Sperrin /* 5468654d025Sperrin * Determine whether we're a log vdev. 5478654d025Sperrin */ 5488654d025Sperrin islog = 0; 5498654d025Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 550990b4856Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 551be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 552fa9e4066Sahrens 55388ecc943SGeorge Wilson if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 554be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 55588ecc943SGeorge Wilson 55699653d4eSeschrock /* 5578654d025Sperrin * Set the nparity property for RAID-Z vdevs. 55899653d4eSeschrock */ 5598654d025Sperrin nparity = -1ULL; 56099653d4eSeschrock if (ops == &vdev_raidz_ops) { 56199653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 5628654d025Sperrin &nparity) == 0) { 563b24ab676SJeff Bonwick if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 564be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 56599653d4eSeschrock /* 566f94275ceSAdam Leventhal * Previous versions could only support 1 or 2 parity 567f94275ceSAdam Leventhal * device. 56899653d4eSeschrock */ 569f94275ceSAdam Leventhal if (nparity > 1 && 570f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ2) 571be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 572f94275ceSAdam Leventhal if (nparity > 2 && 573f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ3) 574be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 57599653d4eSeschrock } else { 57699653d4eSeschrock /* 57799653d4eSeschrock * We require the parity to be specified for SPAs that 57899653d4eSeschrock * support multiple parity levels. 57999653d4eSeschrock */ 580f94275ceSAdam Leventhal if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 581be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 58299653d4eSeschrock /* 58399653d4eSeschrock * Otherwise, we default to 1 parity device for RAID-Z. 58499653d4eSeschrock */ 5858654d025Sperrin nparity = 1; 58699653d4eSeschrock } 58799653d4eSeschrock } else { 5888654d025Sperrin nparity = 0; 58999653d4eSeschrock } 5908654d025Sperrin ASSERT(nparity != -1ULL); 5918654d025Sperrin 5928654d025Sperrin vd = vdev_alloc_common(spa, id, guid, ops); 5935cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 5948654d025Sperrin 5958654d025Sperrin vd->vdev_islog = islog; 5968654d025Sperrin vd->vdev_nparity = nparity; 5978654d025Sperrin 5988654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 5998654d025Sperrin vd->vdev_path = spa_strdup(vd->vdev_path); 6008654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 6018654d025Sperrin vd->vdev_devid = spa_strdup(vd->vdev_devid); 6028654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 6038654d025Sperrin &vd->vdev_physpath) == 0) 6048654d025Sperrin vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 6056809eb4eSEric Schrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 6066809eb4eSEric Schrock vd->vdev_fru = spa_strdup(vd->vdev_fru); 60799653d4eSeschrock 608afefbcddSeschrock /* 609afefbcddSeschrock * Set the whole_disk property. If it's not specified, leave the value 610afefbcddSeschrock * as -1. 611afefbcddSeschrock */ 612afefbcddSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 613afefbcddSeschrock &vd->vdev_wholedisk) != 0) 614afefbcddSeschrock vd->vdev_wholedisk = -1ULL; 615afefbcddSeschrock 6165cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_mapping_object); 6175cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 6185cabbc6bSPrashanth Sreenivasa &vic->vic_mapping_object); 6195cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_births_object); 6205cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 6215cabbc6bSPrashanth Sreenivasa &vic->vic_births_object); 6225cabbc6bSPrashanth Sreenivasa ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 6235cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 6245cabbc6bSPrashanth Sreenivasa &vic->vic_prev_indirect_vdev); 6255cabbc6bSPrashanth Sreenivasa 626ea8dc4b6Seschrock /* 627ea8dc4b6Seschrock * Look for the 'not present' flag. This will only be set if the device 628ea8dc4b6Seschrock * was not present at the time of import. 629ea8dc4b6Seschrock */ 6306809eb4eSEric Schrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 6316809eb4eSEric Schrock &vd->vdev_not_present); 632ea8dc4b6Seschrock 633ecc2d604Sbonwick /* 634ecc2d604Sbonwick * Get the alignment requirement. 635ecc2d604Sbonwick */ 636ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 637ecc2d604Sbonwick 63888ecc943SGeorge Wilson /* 63988ecc943SGeorge Wilson * Retrieve the vdev creation time. 64088ecc943SGeorge Wilson */ 64188ecc943SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 64288ecc943SGeorge Wilson &vd->vdev_crtxg); 64388ecc943SGeorge Wilson 644fa9e4066Sahrens /* 645fa9e4066Sahrens * If we're a top-level vdev, try to load the allocation parameters. 646fa9e4066Sahrens */ 6471195e687SMark J Musante if (parent && !parent->vdev_parent && 6481195e687SMark J Musante (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 649fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 650fa9e4066Sahrens &vd->vdev_ms_array); 651fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 652fa9e4066Sahrens &vd->vdev_ms_shift); 653fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 654fa9e4066Sahrens &vd->vdev_asize); 6553f9d6ad7SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 6563f9d6ad7SLin Ling &vd->vdev_removing); 657215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 658215198a6SJoe Stein &vd->vdev_top_zap); 659215198a6SJoe Stein } else { 660215198a6SJoe Stein ASSERT0(vd->vdev_top_zap); 661fa9e4066Sahrens } 662fa9e4066Sahrens 663cd0837ccSGeorge Wilson if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 664a1521560SJeff Bonwick ASSERT(alloctype == VDEV_ALLOC_LOAD || 6659f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ADD || 6661195e687SMark J Musante alloctype == VDEV_ALLOC_SPLIT || 6679f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ROOTPOOL); 668a1521560SJeff Bonwick vd->vdev_mg = metaslab_group_create(islog ? 669f78cdc34SPaul Dagnelie spa_log_class(spa) : spa_normal_class(spa), vd, 670f78cdc34SPaul Dagnelie spa->spa_alloc_count); 671a1521560SJeff Bonwick } 672a1521560SJeff Bonwick 673215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && 674215198a6SJoe Stein (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 675215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, 676215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 677215198a6SJoe Stein } else { 678215198a6SJoe Stein ASSERT0(vd->vdev_leaf_zap); 679215198a6SJoe Stein } 680215198a6SJoe Stein 681fa9e4066Sahrens /* 6823d7072f8Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 683fa9e4066Sahrens */ 684215198a6SJoe Stein 685c5904d13Seschrock if (vd->vdev_ops->vdev_op_leaf && 68621ecdf64SLin Ling (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 68721ecdf64SLin Ling alloctype == VDEV_ALLOC_ROOTPOOL)) { 688c5904d13Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 689c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 6900713e232SGeorge Wilson &vd->vdev_dtl_object); 691c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 692c5904d13Seschrock &vd->vdev_unspare); 693c5904d13Seschrock } 69421ecdf64SLin Ling 69521ecdf64SLin Ling if (alloctype == VDEV_ALLOC_ROOTPOOL) { 69621ecdf64SLin Ling uint64_t spare = 0; 69721ecdf64SLin Ling 69821ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 69921ecdf64SLin Ling &spare) == 0 && spare) 70021ecdf64SLin Ling spa_spare_add(vd); 70121ecdf64SLin Ling } 70221ecdf64SLin Ling 703ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 704ecc2d604Sbonwick &vd->vdev_offline); 705c5904d13Seschrock 706b4952e17SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 707b4952e17SGeorge Wilson &vd->vdev_resilver_txg); 708cb04b873SMark J Musante 7093d7072f8Seschrock /* 7103d7072f8Seschrock * When importing a pool, we want to ignore the persistent fault 7113d7072f8Seschrock * state, as the diagnosis made on another system may not be 712069f55e2SEric Schrock * valid in the current context. Local vdevs will 713069f55e2SEric Schrock * remain in the faulted state. 7143d7072f8Seschrock */ 715b16da2e2SGeorge Wilson if (spa_load_state(spa) == SPA_LOAD_OPEN) { 7163d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 7173d7072f8Seschrock &vd->vdev_faulted); 7183d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 7193d7072f8Seschrock &vd->vdev_degraded); 7203d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 7213d7072f8Seschrock &vd->vdev_removed); 722069f55e2SEric Schrock 723069f55e2SEric Schrock if (vd->vdev_faulted || vd->vdev_degraded) { 724069f55e2SEric Schrock char *aux; 725069f55e2SEric Schrock 726069f55e2SEric Schrock vd->vdev_label_aux = 727069f55e2SEric Schrock VDEV_AUX_ERR_EXCEEDED; 728069f55e2SEric Schrock if (nvlist_lookup_string(nv, 729069f55e2SEric Schrock ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 730069f55e2SEric Schrock strcmp(aux, "external") == 0) 731069f55e2SEric Schrock vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 732069f55e2SEric Schrock } 7333d7072f8Seschrock } 734fa9e4066Sahrens } 735fa9e4066Sahrens 736fa9e4066Sahrens /* 737fa9e4066Sahrens * Add ourselves to the parent's list of children. 738fa9e4066Sahrens */ 739fa9e4066Sahrens vdev_add_child(parent, vd); 740fa9e4066Sahrens 74199653d4eSeschrock *vdp = vd; 74299653d4eSeschrock 74399653d4eSeschrock return (0); 744fa9e4066Sahrens } 745fa9e4066Sahrens 746fa9e4066Sahrens void 747fa9e4066Sahrens vdev_free(vdev_t *vd) 748fa9e4066Sahrens { 7493d7072f8Seschrock spa_t *spa = vd->vdev_spa; 750094e47e9SGeorge Wilson ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 751fa9e4066Sahrens 752fa9e4066Sahrens /* 753fa9e4066Sahrens * vdev_free() implies closing the vdev first. This is simpler than 754fa9e4066Sahrens * trying to ensure complicated semantics for all callers. 755fa9e4066Sahrens */ 756fa9e4066Sahrens vdev_close(vd); 757fa9e4066Sahrens 758e14bb325SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 759b24ab676SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 760fa9e4066Sahrens 761fa9e4066Sahrens /* 762fa9e4066Sahrens * Free all children. 763fa9e4066Sahrens */ 764573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 765fa9e4066Sahrens vdev_free(vd->vdev_child[c]); 766fa9e4066Sahrens 767fa9e4066Sahrens ASSERT(vd->vdev_child == NULL); 768fa9e4066Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 769094e47e9SGeorge Wilson ASSERT(vd->vdev_initialize_thread == NULL); 770fa9e4066Sahrens 771fa9e4066Sahrens /* 772fa9e4066Sahrens * Discard allocation state. 773fa9e4066Sahrens */ 774a1521560SJeff Bonwick if (vd->vdev_mg != NULL) { 775fa9e4066Sahrens vdev_metaslab_fini(vd); 776a1521560SJeff Bonwick metaslab_group_destroy(vd->vdev_mg); 777a1521560SJeff Bonwick } 778fa9e4066Sahrens 779fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_space); 780fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_dspace); 781fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 782fa9e4066Sahrens 783fa9e4066Sahrens /* 784fa9e4066Sahrens * Remove this vdev from its parent's child list. 785fa9e4066Sahrens */ 786fa9e4066Sahrens vdev_remove_child(vd->vdev_parent, vd); 787fa9e4066Sahrens 788fa9e4066Sahrens ASSERT(vd->vdev_parent == NULL); 789fa9e4066Sahrens 7903d7072f8Seschrock /* 7913d7072f8Seschrock * Clean up vdev structure. 7923d7072f8Seschrock */ 7933d7072f8Seschrock vdev_queue_fini(vd); 7943d7072f8Seschrock vdev_cache_fini(vd); 7953d7072f8Seschrock 7963d7072f8Seschrock if (vd->vdev_path) 7973d7072f8Seschrock spa_strfree(vd->vdev_path); 7983d7072f8Seschrock if (vd->vdev_devid) 7993d7072f8Seschrock spa_strfree(vd->vdev_devid); 8003d7072f8Seschrock if (vd->vdev_physpath) 8013d7072f8Seschrock spa_strfree(vd->vdev_physpath); 8026809eb4eSEric Schrock if (vd->vdev_fru) 8036809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 8043d7072f8Seschrock 8053d7072f8Seschrock if (vd->vdev_isspare) 8063d7072f8Seschrock spa_spare_remove(vd); 807fa94a07fSbrendan if (vd->vdev_isl2cache) 808fa94a07fSbrendan spa_l2cache_remove(vd); 8093d7072f8Seschrock 8103d7072f8Seschrock txg_list_destroy(&vd->vdev_ms_list); 8113d7072f8Seschrock txg_list_destroy(&vd->vdev_dtl_list); 8128ad4d6ddSJeff Bonwick 8133d7072f8Seschrock mutex_enter(&vd->vdev_dtl_lock); 8140713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 8158ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 8160713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 8170713e232SGeorge Wilson range_tree_destroy(vd->vdev_dtl[t]); 8188ad4d6ddSJeff Bonwick } 8193d7072f8Seschrock mutex_exit(&vd->vdev_dtl_lock); 8208ad4d6ddSJeff Bonwick 8215cabbc6bSPrashanth Sreenivasa EQUIV(vd->vdev_indirect_births != NULL, 8225cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping != NULL); 8235cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_births != NULL) { 8245cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 8255cabbc6bSPrashanth Sreenivasa vdev_indirect_births_close(vd->vdev_indirect_births); 8265cabbc6bSPrashanth Sreenivasa } 8275cabbc6bSPrashanth Sreenivasa 8285cabbc6bSPrashanth Sreenivasa if (vd->vdev_obsolete_sm != NULL) { 8295cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 8305cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 8315cabbc6bSPrashanth Sreenivasa space_map_close(vd->vdev_obsolete_sm); 8325cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_sm = NULL; 8335cabbc6bSPrashanth Sreenivasa } 8345cabbc6bSPrashanth Sreenivasa range_tree_destroy(vd->vdev_obsolete_segments); 8355cabbc6bSPrashanth Sreenivasa rw_destroy(&vd->vdev_indirect_rwlock); 8365cabbc6bSPrashanth Sreenivasa mutex_destroy(&vd->vdev_obsolete_lock); 8375cabbc6bSPrashanth Sreenivasa 8380f7643c7SGeorge Wilson mutex_destroy(&vd->vdev_queue_lock); 8393d7072f8Seschrock mutex_destroy(&vd->vdev_dtl_lock); 8403d7072f8Seschrock mutex_destroy(&vd->vdev_stat_lock); 841e14bb325SJeff Bonwick mutex_destroy(&vd->vdev_probe_lock); 842094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_lock); 843094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_io_lock); 844094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_io_cv); 845094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_cv); 8463d7072f8Seschrock 8473d7072f8Seschrock if (vd == spa->spa_root_vdev) 8483d7072f8Seschrock spa->spa_root_vdev = NULL; 8493d7072f8Seschrock 8503d7072f8Seschrock kmem_free(vd, sizeof (vdev_t)); 851fa9e4066Sahrens } 852fa9e4066Sahrens 853fa9e4066Sahrens /* 854fa9e4066Sahrens * Transfer top-level vdev state from svd to tvd. 855fa9e4066Sahrens */ 856fa9e4066Sahrens static void 857fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 858fa9e4066Sahrens { 859fa9e4066Sahrens spa_t *spa = svd->vdev_spa; 860fa9e4066Sahrens metaslab_t *msp; 861fa9e4066Sahrens vdev_t *vd; 862fa9e4066Sahrens int t; 863fa9e4066Sahrens 864fa9e4066Sahrens ASSERT(tvd == tvd->vdev_top); 865fa9e4066Sahrens 866fa9e4066Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 867fa9e4066Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 868fa9e4066Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 869215198a6SJoe Stein tvd->vdev_top_zap = svd->vdev_top_zap; 870fa9e4066Sahrens 871fa9e4066Sahrens svd->vdev_ms_array = 0; 872fa9e4066Sahrens svd->vdev_ms_shift = 0; 873fa9e4066Sahrens svd->vdev_ms_count = 0; 874215198a6SJoe Stein svd->vdev_top_zap = 0; 875fa9e4066Sahrens 876cd0837ccSGeorge Wilson if (tvd->vdev_mg) 877cd0837ccSGeorge Wilson ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 878fa9e4066Sahrens tvd->vdev_mg = svd->vdev_mg; 879fa9e4066Sahrens tvd->vdev_ms = svd->vdev_ms; 880fa9e4066Sahrens 881fa9e4066Sahrens svd->vdev_mg = NULL; 882fa9e4066Sahrens svd->vdev_ms = NULL; 883ecc2d604Sbonwick 884ecc2d604Sbonwick if (tvd->vdev_mg != NULL) 885ecc2d604Sbonwick tvd->vdev_mg->mg_vd = tvd; 886fa9e4066Sahrens 88786714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 88886714001SSerapheim Dimitropoulos svd->vdev_checkpoint_sm = NULL; 88986714001SSerapheim Dimitropoulos 890fa9e4066Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 891fa9e4066Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 89299653d4eSeschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 893fa9e4066Sahrens 894fa9e4066Sahrens svd->vdev_stat.vs_alloc = 0; 895fa9e4066Sahrens svd->vdev_stat.vs_space = 0; 89699653d4eSeschrock svd->vdev_stat.vs_dspace = 0; 897fa9e4066Sahrens 8983a4b1be9SMatthew Ahrens /* 8993a4b1be9SMatthew Ahrens * State which may be set on a top-level vdev that's in the 9003a4b1be9SMatthew Ahrens * process of being removed. 9013a4b1be9SMatthew Ahrens */ 9023a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_births_object); 9033a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 9043a4b1be9SMatthew Ahrens ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 9053a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 9063a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 9073a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 9083a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_removing); 9093a4b1be9SMatthew Ahrens tvd->vdev_removing = svd->vdev_removing; 9103a4b1be9SMatthew Ahrens tvd->vdev_indirect_config = svd->vdev_indirect_config; 9113a4b1be9SMatthew Ahrens tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 9123a4b1be9SMatthew Ahrens tvd->vdev_indirect_births = svd->vdev_indirect_births; 9133a4b1be9SMatthew Ahrens range_tree_swap(&svd->vdev_obsolete_segments, 9143a4b1be9SMatthew Ahrens &tvd->vdev_obsolete_segments); 9153a4b1be9SMatthew Ahrens tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 9163a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_mapping_object = 0; 9173a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_births_object = 0; 9183a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 9193a4b1be9SMatthew Ahrens svd->vdev_indirect_mapping = NULL; 9203a4b1be9SMatthew Ahrens svd->vdev_indirect_births = NULL; 9213a4b1be9SMatthew Ahrens svd->vdev_obsolete_sm = NULL; 9223a4b1be9SMatthew Ahrens svd->vdev_removing = 0; 9233a4b1be9SMatthew Ahrens 924fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 925fa9e4066Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 926fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 927fa9e4066Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 928fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 929fa9e4066Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 930fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 931fa9e4066Sahrens } 932fa9e4066Sahrens 933e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_config_dirty_node)) { 934fa9e4066Sahrens vdev_config_clean(svd); 935fa9e4066Sahrens vdev_config_dirty(tvd); 936fa9e4066Sahrens } 937fa9e4066Sahrens 938e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_state_dirty_node)) { 939e14bb325SJeff Bonwick vdev_state_clean(svd); 940e14bb325SJeff Bonwick vdev_state_dirty(tvd); 941e14bb325SJeff Bonwick } 942e14bb325SJeff Bonwick 94399653d4eSeschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 94499653d4eSeschrock svd->vdev_deflate_ratio = 0; 9458654d025Sperrin 9468654d025Sperrin tvd->vdev_islog = svd->vdev_islog; 9478654d025Sperrin svd->vdev_islog = 0; 948fa9e4066Sahrens } 949fa9e4066Sahrens 950fa9e4066Sahrens static void 951fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 952fa9e4066Sahrens { 953fa9e4066Sahrens if (vd == NULL) 954fa9e4066Sahrens return; 955fa9e4066Sahrens 956fa9e4066Sahrens vd->vdev_top = tvd; 957fa9e4066Sahrens 958573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 959fa9e4066Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 960fa9e4066Sahrens } 961fa9e4066Sahrens 962fa9e4066Sahrens /* 963fa9e4066Sahrens * Add a mirror/replacing vdev above an existing vdev. 964fa9e4066Sahrens */ 965fa9e4066Sahrens vdev_t * 966fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 967fa9e4066Sahrens { 968fa9e4066Sahrens spa_t *spa = cvd->vdev_spa; 969fa9e4066Sahrens vdev_t *pvd = cvd->vdev_parent; 970fa9e4066Sahrens vdev_t *mvd; 971fa9e4066Sahrens 972e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 973fa9e4066Sahrens 974fa9e4066Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 975ecc2d604Sbonwick 976ecc2d604Sbonwick mvd->vdev_asize = cvd->vdev_asize; 977573ca77eSGeorge Wilson mvd->vdev_min_asize = cvd->vdev_min_asize; 9784263d13fSGeorge Wilson mvd->vdev_max_asize = cvd->vdev_max_asize; 9795cabbc6bSPrashanth Sreenivasa mvd->vdev_psize = cvd->vdev_psize; 980ecc2d604Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 981ecc2d604Sbonwick mvd->vdev_state = cvd->vdev_state; 98288ecc943SGeorge Wilson mvd->vdev_crtxg = cvd->vdev_crtxg; 983ecc2d604Sbonwick 984fa9e4066Sahrens vdev_remove_child(pvd, cvd); 985fa9e4066Sahrens vdev_add_child(pvd, mvd); 986fa9e4066Sahrens cvd->vdev_id = mvd->vdev_children; 987fa9e4066Sahrens vdev_add_child(mvd, cvd); 988fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 989fa9e4066Sahrens 990fa9e4066Sahrens if (mvd == mvd->vdev_top) 991fa9e4066Sahrens vdev_top_transfer(cvd, mvd); 992fa9e4066Sahrens 993fa9e4066Sahrens return (mvd); 994fa9e4066Sahrens } 995fa9e4066Sahrens 996fa9e4066Sahrens /* 997fa9e4066Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 998fa9e4066Sahrens */ 999fa9e4066Sahrens void 1000fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd) 1001fa9e4066Sahrens { 1002fa9e4066Sahrens vdev_t *mvd = cvd->vdev_parent; 1003fa9e4066Sahrens vdev_t *pvd = mvd->vdev_parent; 1004fa9e4066Sahrens 1005e14bb325SJeff Bonwick ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1006fa9e4066Sahrens 1007fa9e4066Sahrens ASSERT(mvd->vdev_children == 1); 1008fa9e4066Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 100999653d4eSeschrock mvd->vdev_ops == &vdev_replacing_ops || 101099653d4eSeschrock mvd->vdev_ops == &vdev_spare_ops); 1011ecc2d604Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 1012fa9e4066Sahrens 1013fa9e4066Sahrens vdev_remove_child(mvd, cvd); 1014fa9e4066Sahrens vdev_remove_child(pvd, mvd); 10158ad4d6ddSJeff Bonwick 101699653d4eSeschrock /* 1017e14bb325SJeff Bonwick * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1018e14bb325SJeff Bonwick * Otherwise, we could have detached an offline device, and when we 1019e14bb325SJeff Bonwick * go to import the pool we'll think we have two top-level vdevs, 1020e14bb325SJeff Bonwick * instead of a different version of the same top-level vdev. 102199653d4eSeschrock */ 10228ad4d6ddSJeff Bonwick if (mvd->vdev_top == mvd) { 10238ad4d6ddSJeff Bonwick uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 10241195e687SMark J Musante cvd->vdev_orig_guid = cvd->vdev_guid; 10258ad4d6ddSJeff Bonwick cvd->vdev_guid += guid_delta; 10268ad4d6ddSJeff Bonwick cvd->vdev_guid_sum += guid_delta; 10278ad4d6ddSJeff Bonwick } 1028e14bb325SJeff Bonwick cvd->vdev_id = mvd->vdev_id; 1029e14bb325SJeff Bonwick vdev_add_child(pvd, cvd); 1030fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1031fa9e4066Sahrens 1032fa9e4066Sahrens if (cvd == cvd->vdev_top) 1033fa9e4066Sahrens vdev_top_transfer(mvd, cvd); 1034fa9e4066Sahrens 1035fa9e4066Sahrens ASSERT(mvd->vdev_children == 0); 1036fa9e4066Sahrens vdev_free(mvd); 1037fa9e4066Sahrens } 1038fa9e4066Sahrens 1039ea8dc4b6Seschrock int 1040fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1041fa9e4066Sahrens { 1042fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 1043ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 1044ecc2d604Sbonwick uint64_t m; 1045fa9e4066Sahrens uint64_t oldc = vd->vdev_ms_count; 1046fa9e4066Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1047ecc2d604Sbonwick metaslab_t **mspp; 1048ecc2d604Sbonwick int error; 1049fa9e4066Sahrens 1050a1521560SJeff Bonwick ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1051a1521560SJeff Bonwick 105288ecc943SGeorge Wilson /* 105388ecc943SGeorge Wilson * This vdev is not being allocated from yet or is a hole. 105488ecc943SGeorge Wilson */ 105588ecc943SGeorge Wilson if (vd->vdev_ms_shift == 0) 10560e34b6a7Sbonwick return (0); 10570e34b6a7Sbonwick 105888ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 105988ecc943SGeorge Wilson 1060fa9e4066Sahrens ASSERT(oldc <= newc); 1061fa9e4066Sahrens 1062ecc2d604Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1063fa9e4066Sahrens 1064ecc2d604Sbonwick if (oldc != 0) { 1065ecc2d604Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1066ecc2d604Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1067ecc2d604Sbonwick } 1068fa9e4066Sahrens 1069ecc2d604Sbonwick vd->vdev_ms = mspp; 1070ecc2d604Sbonwick vd->vdev_ms_count = newc; 1071ecc2d604Sbonwick for (m = oldc; m < newc; m++) { 10720713e232SGeorge Wilson uint64_t object = 0; 10730713e232SGeorge Wilson 10745cabbc6bSPrashanth Sreenivasa /* 10755cabbc6bSPrashanth Sreenivasa * vdev_ms_array may be 0 if we are creating the "fake" 10765cabbc6bSPrashanth Sreenivasa * metaslabs for an indirect vdev for zdb's leak detection. 10775cabbc6bSPrashanth Sreenivasa * See zdb_leak_init(). 10785cabbc6bSPrashanth Sreenivasa */ 10795cabbc6bSPrashanth Sreenivasa if (txg == 0 && vd->vdev_ms_array != 0) { 1080ecc2d604Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 10817bfdf011SNeil Perrin m * sizeof (uint64_t), sizeof (uint64_t), &object, 10827bfdf011SNeil Perrin DMU_READ_PREFETCH); 10833ee8c80cSPavel Zakharov if (error != 0) { 10843ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "unable to read the metaslab " 10853ee8c80cSPavel Zakharov "array [error=%d]", error); 1086ecc2d604Sbonwick return (error); 10873ee8c80cSPavel Zakharov } 1088fa9e4066Sahrens } 10891e9bd7ecSPrakash Surya 10901e9bd7ecSPrakash Surya error = metaslab_init(vd->vdev_mg, m, object, txg, 10911e9bd7ecSPrakash Surya &(vd->vdev_ms[m])); 10923ee8c80cSPavel Zakharov if (error != 0) { 10933ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 10943ee8c80cSPavel Zakharov error); 10951e9bd7ecSPrakash Surya return (error); 10963ee8c80cSPavel Zakharov } 1097fa9e4066Sahrens } 1098fa9e4066Sahrens 1099a1521560SJeff Bonwick if (txg == 0) 1100a1521560SJeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1101a1521560SJeff Bonwick 11023f9d6ad7SLin Ling /* 11033f9d6ad7SLin Ling * If the vdev is being removed we don't activate 11043f9d6ad7SLin Ling * the metaslabs since we want to ensure that no new 11053f9d6ad7SLin Ling * allocations are performed on this device. 11063f9d6ad7SLin Ling */ 11073f9d6ad7SLin Ling if (oldc == 0 && !vd->vdev_removing) 1108a1521560SJeff Bonwick metaslab_group_activate(vd->vdev_mg); 1109a1521560SJeff Bonwick 1110a1521560SJeff Bonwick if (txg == 0) 1111a1521560SJeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG); 1112a1521560SJeff Bonwick 1113ea8dc4b6Seschrock return (0); 1114fa9e4066Sahrens } 1115fa9e4066Sahrens 1116fa9e4066Sahrens void 1117fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd) 1118fa9e4066Sahrens { 111986714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 112086714001SSerapheim Dimitropoulos ASSERT(spa_feature_is_active(vd->vdev_spa, 112186714001SSerapheim Dimitropoulos SPA_FEATURE_POOL_CHECKPOINT)); 112286714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 112386714001SSerapheim Dimitropoulos /* 112486714001SSerapheim Dimitropoulos * Even though we close the space map, we need to set its 112586714001SSerapheim Dimitropoulos * pointer to NULL. The reason is that vdev_metaslab_fini() 112686714001SSerapheim Dimitropoulos * may be called multiple times for certain operations 112786714001SSerapheim Dimitropoulos * (i.e. when destroying a pool) so we need to ensure that 112886714001SSerapheim Dimitropoulos * this clause never executes twice. This logic is similar 112986714001SSerapheim Dimitropoulos * to the one used for the vdev_ms clause below. 113086714001SSerapheim Dimitropoulos */ 113186714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 113286714001SSerapheim Dimitropoulos } 113386714001SSerapheim Dimitropoulos 1134fa9e4066Sahrens if (vd->vdev_ms != NULL) { 11355cabbc6bSPrashanth Sreenivasa uint64_t count = vd->vdev_ms_count; 11365cabbc6bSPrashanth Sreenivasa 1137a1521560SJeff Bonwick metaslab_group_passivate(vd->vdev_mg); 11385cabbc6bSPrashanth Sreenivasa for (uint64_t m = 0; m < count; m++) { 11390713e232SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 11400713e232SGeorge Wilson 11410713e232SGeorge Wilson if (msp != NULL) 11420713e232SGeorge Wilson metaslab_fini(msp); 11430713e232SGeorge Wilson } 1144fa9e4066Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1145fa9e4066Sahrens vd->vdev_ms = NULL; 11465cabbc6bSPrashanth Sreenivasa 11475cabbc6bSPrashanth Sreenivasa vd->vdev_ms_count = 0; 1148fa9e4066Sahrens } 11495cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_ms_count); 1150fa9e4066Sahrens } 1151fa9e4066Sahrens 1152e14bb325SJeff Bonwick typedef struct vdev_probe_stats { 1153e14bb325SJeff Bonwick boolean_t vps_readable; 1154e14bb325SJeff Bonwick boolean_t vps_writeable; 1155e14bb325SJeff Bonwick int vps_flags; 1156e14bb325SJeff Bonwick } vdev_probe_stats_t; 1157e14bb325SJeff Bonwick 1158e14bb325SJeff Bonwick static void 1159e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio) 11600a4e9518Sgw { 11618ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 1162a3f829aeSBill Moore vdev_t *vd = zio->io_vd; 1163e14bb325SJeff Bonwick vdev_probe_stats_t *vps = zio->io_private; 1164a3f829aeSBill Moore 1165a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio != NULL); 1166e14bb325SJeff Bonwick 1167e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) { 1168e14bb325SJeff Bonwick if (zio->io_error == 0) 1169e14bb325SJeff Bonwick vps->vps_readable = 1; 11708ad4d6ddSJeff Bonwick if (zio->io_error == 0 && spa_writeable(spa)) { 1171a3f829aeSBill Moore zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1172770499e1SDan Kimmel zio->io_offset, zio->io_size, zio->io_abd, 1173e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1174e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1175e14bb325SJeff Bonwick } else { 1176770499e1SDan Kimmel abd_free(zio->io_abd); 1177e14bb325SJeff Bonwick } 1178e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_WRITE) { 1179e14bb325SJeff Bonwick if (zio->io_error == 0) 1180e14bb325SJeff Bonwick vps->vps_writeable = 1; 1181770499e1SDan Kimmel abd_free(zio->io_abd); 1182e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_NULL) { 1183a3f829aeSBill Moore zio_t *pio; 1184e14bb325SJeff Bonwick 1185e14bb325SJeff Bonwick vd->vdev_cant_read |= !vps->vps_readable; 1186e14bb325SJeff Bonwick vd->vdev_cant_write |= !vps->vps_writeable; 1187e14bb325SJeff Bonwick 1188e14bb325SJeff Bonwick if (vdev_readable(vd) && 11898ad4d6ddSJeff Bonwick (vdev_writeable(vd) || !spa_writeable(spa))) { 1190e14bb325SJeff Bonwick zio->io_error = 0; 1191e14bb325SJeff Bonwick } else { 1192e14bb325SJeff Bonwick ASSERT(zio->io_error != 0); 11933ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "failed probe"); 1194e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 11958ad4d6ddSJeff Bonwick spa, vd, NULL, 0, 0); 1196be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 1197e14bb325SJeff Bonwick } 1198a3f829aeSBill Moore 1199a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1200a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio == zio); 1201a3f829aeSBill Moore vd->vdev_probe_zio = NULL; 1202a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1203a3f829aeSBill Moore 12040f7643c7SGeorge Wilson zio_link_t *zl = NULL; 12050f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1206a3f829aeSBill Moore if (!vdev_accessible(vd, pio)) 1207be6fd75aSMatthew Ahrens pio->io_error = SET_ERROR(ENXIO); 1208a3f829aeSBill Moore 1209e14bb325SJeff Bonwick kmem_free(vps, sizeof (*vps)); 1210e14bb325SJeff Bonwick } 1211e14bb325SJeff Bonwick } 12120a4e9518Sgw 1213e14bb325SJeff Bonwick /* 1214f7170741SWill Andrews * Determine whether this device is accessible. 1215f7170741SWill Andrews * 1216f7170741SWill Andrews * Read and write to several known locations: the pad regions of each 1217f7170741SWill Andrews * vdev label but the first, which we leave alone in case it contains 1218f7170741SWill Andrews * a VTOC. 1219e14bb325SJeff Bonwick */ 1220e14bb325SJeff Bonwick zio_t * 1221a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio) 1222e14bb325SJeff Bonwick { 1223e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 1224a3f829aeSBill Moore vdev_probe_stats_t *vps = NULL; 1225a3f829aeSBill Moore zio_t *pio; 1226a3f829aeSBill Moore 1227a3f829aeSBill Moore ASSERT(vd->vdev_ops->vdev_op_leaf); 12280a4e9518Sgw 1229a3f829aeSBill Moore /* 1230a3f829aeSBill Moore * Don't probe the probe. 1231a3f829aeSBill Moore */ 1232a3f829aeSBill Moore if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1233a3f829aeSBill Moore return (NULL); 1234e14bb325SJeff Bonwick 1235a3f829aeSBill Moore /* 1236a3f829aeSBill Moore * To prevent 'probe storms' when a device fails, we create 1237a3f829aeSBill Moore * just one probe i/o at a time. All zios that want to probe 1238a3f829aeSBill Moore * this vdev will become parents of the probe io. 1239a3f829aeSBill Moore */ 1240a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1241e14bb325SJeff Bonwick 1242a3f829aeSBill Moore if ((pio = vd->vdev_probe_zio) == NULL) { 1243a3f829aeSBill Moore vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1244a3f829aeSBill Moore 1245a3f829aeSBill Moore vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1246a3f829aeSBill Moore ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 12478956713aSEric Schrock ZIO_FLAG_TRYHARD; 1248a3f829aeSBill Moore 1249a3f829aeSBill Moore if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1250a3f829aeSBill Moore /* 1251a3f829aeSBill Moore * vdev_cant_read and vdev_cant_write can only 1252a3f829aeSBill Moore * transition from TRUE to FALSE when we have the 1253a3f829aeSBill Moore * SCL_ZIO lock as writer; otherwise they can only 1254a3f829aeSBill Moore * transition from FALSE to TRUE. This ensures that 1255a3f829aeSBill Moore * any zio looking at these values can assume that 1256a3f829aeSBill Moore * failures persist for the life of the I/O. That's 1257a3f829aeSBill Moore * important because when a device has intermittent 1258a3f829aeSBill Moore * connectivity problems, we want to ensure that 1259a3f829aeSBill Moore * they're ascribed to the device (ENXIO) and not 1260a3f829aeSBill Moore * the zio (EIO). 1261a3f829aeSBill Moore * 1262a3f829aeSBill Moore * Since we hold SCL_ZIO as writer here, clear both 1263a3f829aeSBill Moore * values so the probe can reevaluate from first 1264a3f829aeSBill Moore * principles. 1265a3f829aeSBill Moore */ 1266a3f829aeSBill Moore vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1267a3f829aeSBill Moore vd->vdev_cant_read = B_FALSE; 1268a3f829aeSBill Moore vd->vdev_cant_write = B_FALSE; 1269a3f829aeSBill Moore } 1270a3f829aeSBill Moore 1271a3f829aeSBill Moore vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1272a3f829aeSBill Moore vdev_probe_done, vps, 1273a3f829aeSBill Moore vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1274a3f829aeSBill Moore 127598d1cbfeSGeorge Wilson /* 127698d1cbfeSGeorge Wilson * We can't change the vdev state in this context, so we 127798d1cbfeSGeorge Wilson * kick off an async task to do it on our behalf. 127898d1cbfeSGeorge Wilson */ 1279a3f829aeSBill Moore if (zio != NULL) { 1280a3f829aeSBill Moore vd->vdev_probe_wanted = B_TRUE; 1281a3f829aeSBill Moore spa_async_request(spa, SPA_ASYNC_PROBE); 1282a3f829aeSBill Moore } 1283e14bb325SJeff Bonwick } 1284e14bb325SJeff Bonwick 1285a3f829aeSBill Moore if (zio != NULL) 1286a3f829aeSBill Moore zio_add_child(zio, pio); 1287e14bb325SJeff Bonwick 1288a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1289e14bb325SJeff Bonwick 1290a3f829aeSBill Moore if (vps == NULL) { 1291a3f829aeSBill Moore ASSERT(zio != NULL); 1292a3f829aeSBill Moore return (NULL); 1293a3f829aeSBill Moore } 1294e14bb325SJeff Bonwick 1295e14bb325SJeff Bonwick for (int l = 1; l < VDEV_LABELS; l++) { 1296a3f829aeSBill Moore zio_nowait(zio_read_phys(pio, vd, 1297e14bb325SJeff Bonwick vdev_label_offset(vd->vdev_psize, l, 1298770499e1SDan Kimmel offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1299770499e1SDan Kimmel abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1300e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1301e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1302e14bb325SJeff Bonwick } 1303e14bb325SJeff Bonwick 1304a3f829aeSBill Moore if (zio == NULL) 1305a3f829aeSBill Moore return (pio); 1306a3f829aeSBill Moore 1307a3f829aeSBill Moore zio_nowait(pio); 1308a3f829aeSBill Moore return (NULL); 13090a4e9518Sgw } 13100a4e9518Sgw 1311f64c0e34SEric Taylor static void 1312f64c0e34SEric Taylor vdev_open_child(void *arg) 1313f64c0e34SEric Taylor { 1314f64c0e34SEric Taylor vdev_t *vd = arg; 1315f64c0e34SEric Taylor 1316f64c0e34SEric Taylor vd->vdev_open_thread = curthread; 1317f64c0e34SEric Taylor vd->vdev_open_error = vdev_open(vd); 1318f64c0e34SEric Taylor vd->vdev_open_thread = NULL; 1319f64c0e34SEric Taylor } 1320f64c0e34SEric Taylor 1321681d9761SEric Taylor boolean_t 1322681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd) 1323681d9761SEric Taylor { 1324681d9761SEric Taylor if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1325681d9761SEric Taylor strlen(ZVOL_DIR)) == 0) 1326681d9761SEric Taylor return (B_TRUE); 1327681d9761SEric Taylor for (int c = 0; c < vd->vdev_children; c++) 1328681d9761SEric Taylor if (vdev_uses_zvols(vd->vdev_child[c])) 1329681d9761SEric Taylor return (B_TRUE); 1330681d9761SEric Taylor return (B_FALSE); 1331681d9761SEric Taylor } 1332681d9761SEric Taylor 1333f64c0e34SEric Taylor void 1334f64c0e34SEric Taylor vdev_open_children(vdev_t *vd) 1335f64c0e34SEric Taylor { 1336f64c0e34SEric Taylor taskq_t *tq; 1337f64c0e34SEric Taylor int children = vd->vdev_children; 1338f64c0e34SEric Taylor 1339681d9761SEric Taylor /* 1340681d9761SEric Taylor * in order to handle pools on top of zvols, do the opens 1341681d9761SEric Taylor * in a single thread so that the same thread holds the 1342681d9761SEric Taylor * spa_namespace_lock 1343681d9761SEric Taylor */ 1344681d9761SEric Taylor if (vdev_uses_zvols(vd)) { 1345681d9761SEric Taylor for (int c = 0; c < children; c++) 1346681d9761SEric Taylor vd->vdev_child[c]->vdev_open_error = 1347681d9761SEric Taylor vdev_open(vd->vdev_child[c]); 1348681d9761SEric Taylor return; 1349681d9761SEric Taylor } 1350f64c0e34SEric Taylor tq = taskq_create("vdev_open", children, minclsyspri, 1351f64c0e34SEric Taylor children, children, TASKQ_PREPOPULATE); 1352f64c0e34SEric Taylor 1353f64c0e34SEric Taylor for (int c = 0; c < children; c++) 1354f64c0e34SEric Taylor VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1355f64c0e34SEric Taylor TQ_SLEEP) != NULL); 1356f64c0e34SEric Taylor 1357f64c0e34SEric Taylor taskq_destroy(tq); 1358f64c0e34SEric Taylor } 1359f64c0e34SEric Taylor 13605cabbc6bSPrashanth Sreenivasa /* 13615cabbc6bSPrashanth Sreenivasa * Compute the raidz-deflation ratio. Note, we hard-code 13625cabbc6bSPrashanth Sreenivasa * in 128k (1 << 17) because it is the "typical" blocksize. 13635cabbc6bSPrashanth Sreenivasa * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 13645cabbc6bSPrashanth Sreenivasa * otherwise it would inconsistently account for existing bp's. 13655cabbc6bSPrashanth Sreenivasa */ 13665cabbc6bSPrashanth Sreenivasa static void 13675cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd) 13685cabbc6bSPrashanth Sreenivasa { 13695cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 13705cabbc6bSPrashanth Sreenivasa vd->vdev_deflate_ratio = (1 << 17) / 13715cabbc6bSPrashanth Sreenivasa (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 13725cabbc6bSPrashanth Sreenivasa } 13735cabbc6bSPrashanth Sreenivasa } 13745cabbc6bSPrashanth Sreenivasa 1375fa9e4066Sahrens /* 1376fa9e4066Sahrens * Prepare a virtual device for access. 1377fa9e4066Sahrens */ 1378fa9e4066Sahrens int 1379fa9e4066Sahrens vdev_open(vdev_t *vd) 1380fa9e4066Sahrens { 13818ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1382fa9e4066Sahrens int error; 1383fa9e4066Sahrens uint64_t osize = 0; 13844263d13fSGeorge Wilson uint64_t max_osize = 0; 13854263d13fSGeorge Wilson uint64_t asize, max_asize, psize; 1386ecc2d604Sbonwick uint64_t ashift = 0; 1387fa9e4066Sahrens 1388f64c0e34SEric Taylor ASSERT(vd->vdev_open_thread == curthread || 1389f64c0e34SEric Taylor spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1390fa9e4066Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1391fa9e4066Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1392fa9e4066Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1393fa9e4066Sahrens 1394fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1395e6ca193dSGeorge Wilson vd->vdev_cant_read = B_FALSE; 1396e6ca193dSGeorge Wilson vd->vdev_cant_write = B_FALSE; 1397573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 1398fa9e4066Sahrens 1399069f55e2SEric Schrock /* 1400069f55e2SEric Schrock * If this vdev is not removed, check its fault status. If it's 1401069f55e2SEric Schrock * faulted, bail out of the open. 1402069f55e2SEric Schrock */ 14033d7072f8Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 14043d7072f8Seschrock ASSERT(vd->vdev_children == 0); 1405069f55e2SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1406069f55e2SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 14073d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1408069f55e2SEric Schrock vd->vdev_label_aux); 1409be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 14103d7072f8Seschrock } else if (vd->vdev_offline) { 1411fa9e4066Sahrens ASSERT(vd->vdev_children == 0); 1412ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1413be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1414fa9e4066Sahrens } 1415fa9e4066Sahrens 14164263d13fSGeorge Wilson error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1417fa9e4066Sahrens 1418095bcd66SGeorge Wilson /* 1419095bcd66SGeorge Wilson * Reset the vdev_reopening flag so that we actually close 1420095bcd66SGeorge Wilson * the vdev on error. 1421095bcd66SGeorge Wilson */ 1422095bcd66SGeorge Wilson vd->vdev_reopening = B_FALSE; 1423ea8dc4b6Seschrock if (zio_injection_enabled && error == 0) 14248956713aSEric Schrock error = zio_handle_device_injection(vd, NULL, ENXIO); 1425ea8dc4b6Seschrock 1426fa9e4066Sahrens if (error) { 14273d7072f8Seschrock if (vd->vdev_removed && 14283d7072f8Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 14293d7072f8Seschrock vd->vdev_removed = B_FALSE; 14303d7072f8Seschrock 14316f793812SPavel Zakharov if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 14326f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 14336f793812SPavel Zakharov vd->vdev_stat.vs_aux); 14346f793812SPavel Zakharov } else { 14356f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 14366f793812SPavel Zakharov vd->vdev_stat.vs_aux); 14376f793812SPavel Zakharov } 1438fa9e4066Sahrens return (error); 1439fa9e4066Sahrens } 1440fa9e4066Sahrens 14413d7072f8Seschrock vd->vdev_removed = B_FALSE; 14423d7072f8Seschrock 1443096d22d4SEric Schrock /* 1444096d22d4SEric Schrock * Recheck the faulted flag now that we have confirmed that 1445096d22d4SEric Schrock * the vdev is accessible. If we're faulted, bail. 1446096d22d4SEric Schrock */ 1447096d22d4SEric Schrock if (vd->vdev_faulted) { 1448096d22d4SEric Schrock ASSERT(vd->vdev_children == 0); 1449096d22d4SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1450096d22d4SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1451096d22d4SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1452096d22d4SEric Schrock vd->vdev_label_aux); 1453be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1454096d22d4SEric Schrock } 1455096d22d4SEric Schrock 14563d7072f8Seschrock if (vd->vdev_degraded) { 14573d7072f8Seschrock ASSERT(vd->vdev_children == 0); 14583d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 14593d7072f8Seschrock VDEV_AUX_ERR_EXCEEDED); 14603d7072f8Seschrock } else { 1461069f55e2SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 14623d7072f8Seschrock } 1463fa9e4066Sahrens 146488ecc943SGeorge Wilson /* 146588ecc943SGeorge Wilson * For hole or missing vdevs we just return success. 146688ecc943SGeorge Wilson */ 146788ecc943SGeorge Wilson if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 146888ecc943SGeorge Wilson return (0); 146988ecc943SGeorge Wilson 1470573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 1471ea8dc4b6Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1472ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1473ea8dc4b6Seschrock VDEV_AUX_NONE); 1474ea8dc4b6Seschrock break; 1475ea8dc4b6Seschrock } 1476573ca77eSGeorge Wilson } 1477fa9e4066Sahrens 1478fa9e4066Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 14794263d13fSGeorge Wilson max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1480fa9e4066Sahrens 1481fa9e4066Sahrens if (vd->vdev_children == 0) { 1482fa9e4066Sahrens if (osize < SPA_MINDEVSIZE) { 1483ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1484ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1485be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1486fa9e4066Sahrens } 1487fa9e4066Sahrens psize = osize; 1488fa9e4066Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 14894263d13fSGeorge Wilson max_asize = max_osize - (VDEV_LABEL_START_SIZE + 14904263d13fSGeorge Wilson VDEV_LABEL_END_SIZE); 1491fa9e4066Sahrens } else { 1492ecc2d604Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1493fa9e4066Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1494ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1495ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1496be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1497fa9e4066Sahrens } 1498fa9e4066Sahrens psize = 0; 1499fa9e4066Sahrens asize = osize; 15004263d13fSGeorge Wilson max_asize = max_osize; 1501fa9e4066Sahrens } 1502fa9e4066Sahrens 1503fa9e4066Sahrens vd->vdev_psize = psize; 1504fa9e4066Sahrens 1505573ca77eSGeorge Wilson /* 1506c040c10cSSteven Hartland * Make sure the allocatable size hasn't shrunk too much. 1507573ca77eSGeorge Wilson */ 1508573ca77eSGeorge Wilson if (asize < vd->vdev_min_asize) { 1509573ca77eSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1510573ca77eSGeorge Wilson VDEV_AUX_BAD_LABEL); 1511be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1512573ca77eSGeorge Wilson } 1513573ca77eSGeorge Wilson 1514fa9e4066Sahrens if (vd->vdev_asize == 0) { 1515fa9e4066Sahrens /* 1516fa9e4066Sahrens * This is the first-ever open, so use the computed values. 1517ecc2d604Sbonwick * For testing purposes, a higher ashift can be requested. 1518fa9e4066Sahrens */ 1519fa9e4066Sahrens vd->vdev_asize = asize; 15204263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1521ecc2d604Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 152293a1902eSMatthew Ahrens vd->vdev_ashift = MAX(zfs_ashift_min, vd->vdev_ashift); 1523fa9e4066Sahrens } else { 1524fa9e4066Sahrens /* 15252384d9f8SGeorge Wilson * Detect if the alignment requirement has increased. 15262384d9f8SGeorge Wilson * We don't want to make the pool unavailable, just 15272384d9f8SGeorge Wilson * issue a warning instead. 1528fa9e4066Sahrens */ 15292384d9f8SGeorge Wilson if (ashift > vd->vdev_top->vdev_ashift && 15302384d9f8SGeorge Wilson vd->vdev_ops->vdev_op_leaf) { 15312384d9f8SGeorge Wilson cmn_err(CE_WARN, 15322384d9f8SGeorge Wilson "Disk, '%s', has a block alignment that is " 15332384d9f8SGeorge Wilson "larger than the pool's alignment\n", 15342384d9f8SGeorge Wilson vd->vdev_path); 1535fa9e4066Sahrens } 15364263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1537573ca77eSGeorge Wilson } 1538fa9e4066Sahrens 1539573ca77eSGeorge Wilson /* 1540c040c10cSSteven Hartland * If all children are healthy we update asize if either: 1541c040c10cSSteven Hartland * The asize has increased, due to a device expansion caused by dynamic 1542c040c10cSSteven Hartland * LUN growth or vdev replacement, and automatic expansion is enabled; 1543c040c10cSSteven Hartland * making the additional space available. 1544c040c10cSSteven Hartland * 1545c040c10cSSteven Hartland * The asize has decreased, due to a device shrink usually caused by a 1546c040c10cSSteven Hartland * vdev replace with a smaller device. This ensures that calculations 1547c040c10cSSteven Hartland * based of max_asize and asize e.g. esize are always valid. It's safe 1548c040c10cSSteven Hartland * to do this as we've already validated that asize is greater than 1549c040c10cSSteven Hartland * vdev_min_asize. 1550573ca77eSGeorge Wilson */ 1551c040c10cSSteven Hartland if (vd->vdev_state == VDEV_STATE_HEALTHY && 1552c040c10cSSteven Hartland ((asize > vd->vdev_asize && 1553c040c10cSSteven Hartland (vd->vdev_expanding || spa->spa_autoexpand)) || 1554c040c10cSSteven Hartland (asize < vd->vdev_asize))) 1555573ca77eSGeorge Wilson vd->vdev_asize = asize; 1556fa9e4066Sahrens 1557573ca77eSGeorge Wilson vdev_set_min_asize(vd); 1558fa9e4066Sahrens 15590a4e9518Sgw /* 15600a4e9518Sgw * Ensure we can issue some IO before declaring the 15610a4e9518Sgw * vdev open for business. 15620a4e9518Sgw */ 1563e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 1564e14bb325SJeff Bonwick (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 156598d1cbfeSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 156698d1cbfeSGeorge Wilson VDEV_AUX_ERR_EXCEEDED); 15670a4e9518Sgw return (error); 15680a4e9518Sgw } 15690a4e9518Sgw 157081cd5c55SMatthew Ahrens /* 157181cd5c55SMatthew Ahrens * Track the min and max ashift values for normal data devices. 157281cd5c55SMatthew Ahrens */ 157381cd5c55SMatthew Ahrens if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 157481cd5c55SMatthew Ahrens !vd->vdev_islog && vd->vdev_aux == NULL) { 157581cd5c55SMatthew Ahrens if (vd->vdev_ashift > spa->spa_max_ashift) 157681cd5c55SMatthew Ahrens spa->spa_max_ashift = vd->vdev_ashift; 157781cd5c55SMatthew Ahrens if (vd->vdev_ashift < spa->spa_min_ashift) 157881cd5c55SMatthew Ahrens spa->spa_min_ashift = vd->vdev_ashift; 157981cd5c55SMatthew Ahrens } 158081cd5c55SMatthew Ahrens 1581088f3894Sahrens /* 1582088f3894Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 15838ad4d6ddSJeff Bonwick * resilver. But don't do this if we are doing a reopen for a scrub, 15848ad4d6ddSJeff Bonwick * since this would just restart the scrub we are already doing. 1585088f3894Sahrens */ 15868ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 15878ad4d6ddSJeff Bonwick vdev_resilver_needed(vd, NULL, NULL)) 15888ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1589088f3894Sahrens 1590fa9e4066Sahrens return (0); 1591fa9e4066Sahrens } 1592fa9e4066Sahrens 1593560e6e96Seschrock /* 1594560e6e96Seschrock * Called once the vdevs are all opened, this routine validates the label 15956f793812SPavel Zakharov * contents. This needs to be done before vdev_load() so that we don't 15963d7072f8Seschrock * inadvertently do repair I/Os to the wrong device. 1597560e6e96Seschrock * 1598560e6e96Seschrock * This function will only return failure if one of the vdevs indicates that it 1599560e6e96Seschrock * has since been destroyed or exported. This is only possible if 1600560e6e96Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1601560e6e96Seschrock * will be updated but the function will return 0. 1602560e6e96Seschrock */ 1603560e6e96Seschrock int 16046f793812SPavel Zakharov vdev_validate(vdev_t *vd) 1605560e6e96Seschrock { 1606560e6e96Seschrock spa_t *spa = vd->vdev_spa; 1607560e6e96Seschrock nvlist_t *label; 16086f793812SPavel Zakharov uint64_t guid = 0, aux_guid = 0, top_guid; 1609560e6e96Seschrock uint64_t state; 16106f793812SPavel Zakharov nvlist_t *nvl; 16116f793812SPavel Zakharov uint64_t txg; 1612560e6e96Seschrock 16136f793812SPavel Zakharov if (vdev_validate_skip) 16146f793812SPavel Zakharov return (0); 16156f793812SPavel Zakharov 16166f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 16176f793812SPavel Zakharov if (vdev_validate(vd->vdev_child[c]) != 0) 1618be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1619560e6e96Seschrock 1620b5989ec7Seschrock /* 1621b5989ec7Seschrock * If the device has already failed, or was marked offline, don't do 1622b5989ec7Seschrock * any further validation. Otherwise, label I/O will fail and we will 1623b5989ec7Seschrock * overwrite the previous state. 1624b5989ec7Seschrock */ 16256f793812SPavel Zakharov if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 16266f793812SPavel Zakharov return (0); 1627560e6e96Seschrock 16286f793812SPavel Zakharov /* 16296f793812SPavel Zakharov * If we are performing an extreme rewind, we allow for a label that 16306f793812SPavel Zakharov * was modified at a point after the current txg. 1631d1de72cfSPavel Zakharov * If config lock is not held do not check for the txg. spa_sync could 1632d1de72cfSPavel Zakharov * be updating the vdev's label before updating spa_last_synced_txg. 16336f793812SPavel Zakharov */ 1634d1de72cfSPavel Zakharov if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1635d1de72cfSPavel Zakharov spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 16366f793812SPavel Zakharov txg = UINT64_MAX; 16376f793812SPavel Zakharov else 16386f793812SPavel Zakharov txg = spa_last_synced_txg(spa); 1639560e6e96Seschrock 16406f793812SPavel Zakharov if ((label = vdev_label_read_config(vd, txg)) == NULL) { 16416f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 16426f793812SPavel Zakharov VDEV_AUX_BAD_LABEL); 1643b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1644b6bf6e15SPavel Zakharov "txg %llu", (u_longlong_t)txg); 16456f793812SPavel Zakharov return (0); 16466f793812SPavel Zakharov } 16471195e687SMark J Musante 16486f793812SPavel Zakharov /* 16496f793812SPavel Zakharov * Determine if this vdev has been split off into another 16506f793812SPavel Zakharov * pool. If so, then refuse to open it. 16516f793812SPavel Zakharov */ 16526f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 16536f793812SPavel Zakharov &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 16546f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16556f793812SPavel Zakharov VDEV_AUX_SPLIT_POOL); 16566f793812SPavel Zakharov nvlist_free(label); 16576f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 16586f793812SPavel Zakharov return (0); 16596f793812SPavel Zakharov } 1660560e6e96Seschrock 16616f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 16626f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16636f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16646f793812SPavel Zakharov nvlist_free(label); 16656f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 16666f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 16676f793812SPavel Zakharov return (0); 16686f793812SPavel Zakharov } 16691195e687SMark J Musante 16706f793812SPavel Zakharov /* 16716f793812SPavel Zakharov * If config is not trusted then ignore the spa guid check. This is 16726f793812SPavel Zakharov * necessary because if the machine crashed during a re-guid the new 16736f793812SPavel Zakharov * guid might have been written to all of the vdev labels, but not the 16746f793812SPavel Zakharov * cached config. The check will be performed again once we have the 16756f793812SPavel Zakharov * trusted config from the MOS. 16766f793812SPavel Zakharov */ 16776f793812SPavel Zakharov if (spa->spa_trust_config && guid != spa_guid(spa)) { 16786f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16796f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16806f793812SPavel Zakharov nvlist_free(label); 16816f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 16826f793812SPavel Zakharov "match config (%llu != %llu)", (u_longlong_t)guid, 16836f793812SPavel Zakharov (u_longlong_t)spa_guid(spa)); 16846f793812SPavel Zakharov return (0); 16856f793812SPavel Zakharov } 16866f793812SPavel Zakharov 16876f793812SPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 16886f793812SPavel Zakharov != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 16896f793812SPavel Zakharov &aux_guid) != 0) 16906f793812SPavel Zakharov aux_guid = 0; 16916f793812SPavel Zakharov 16926f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 16936f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16946f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16956f793812SPavel Zakharov nvlist_free(label); 16966f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 16976f793812SPavel Zakharov ZPOOL_CONFIG_GUID); 16986f793812SPavel Zakharov return (0); 16996f793812SPavel Zakharov } 17006f793812SPavel Zakharov 17016f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 17026f793812SPavel Zakharov != 0) { 17036f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17046f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 17056f793812SPavel Zakharov nvlist_free(label); 17066f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 17076f793812SPavel Zakharov ZPOOL_CONFIG_TOP_GUID); 17086f793812SPavel Zakharov return (0); 17096f793812SPavel Zakharov } 17106f793812SPavel Zakharov 17116f793812SPavel Zakharov /* 17126f793812SPavel Zakharov * If this vdev just became a top-level vdev because its sibling was 17136f793812SPavel Zakharov * detached, it will have adopted the parent's vdev guid -- but the 17146f793812SPavel Zakharov * label may or may not be on disk yet. Fortunately, either version 17156f793812SPavel Zakharov * of the label will have the same top guid, so if we're a top-level 17166f793812SPavel Zakharov * vdev, we can safely compare to that instead. 17176f793812SPavel Zakharov * However, if the config comes from a cachefile that failed to update 17186f793812SPavel Zakharov * after the detach, a top-level vdev will appear as a non top-level 17196f793812SPavel Zakharov * vdev in the config. Also relax the constraints if we perform an 17206f793812SPavel Zakharov * extreme rewind. 17216f793812SPavel Zakharov * 17226f793812SPavel Zakharov * If we split this vdev off instead, then we also check the 17236f793812SPavel Zakharov * original pool's guid. We don't want to consider the vdev 17246f793812SPavel Zakharov * corrupt if it is partway through a split operation. 17256f793812SPavel Zakharov */ 17266f793812SPavel Zakharov if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 17276f793812SPavel Zakharov boolean_t mismatch = B_FALSE; 17286f793812SPavel Zakharov if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 17296f793812SPavel Zakharov if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 17306f793812SPavel Zakharov mismatch = B_TRUE; 17316f793812SPavel Zakharov } else { 17326f793812SPavel Zakharov if (vd->vdev_guid != top_guid && 17336f793812SPavel Zakharov vd->vdev_top->vdev_guid != guid) 17346f793812SPavel Zakharov mismatch = B_TRUE; 1735560e6e96Seschrock } 1736560e6e96Seschrock 17376f793812SPavel Zakharov if (mismatch) { 1738560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1739560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1740560e6e96Seschrock nvlist_free(label); 17416f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: config guid " 17426f793812SPavel Zakharov "doesn't match label guid"); 17436f793812SPavel Zakharov vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 17446f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 17456f793812SPavel Zakharov (u_longlong_t)vd->vdev_top->vdev_guid); 17466f793812SPavel Zakharov vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 17476f793812SPavel Zakharov "aux_guid %llu", (u_longlong_t)guid, 17486f793812SPavel Zakharov (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1749560e6e96Seschrock return (0); 1750560e6e96Seschrock } 17516f793812SPavel Zakharov } 1752560e6e96Seschrock 17536f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 17546f793812SPavel Zakharov &state) != 0) { 17556f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17566f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 1757560e6e96Seschrock nvlist_free(label); 17586f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 17596f793812SPavel Zakharov ZPOOL_CONFIG_POOL_STATE); 17606f793812SPavel Zakharov return (0); 17616f793812SPavel Zakharov } 1762560e6e96Seschrock 17636f793812SPavel Zakharov nvlist_free(label); 17646f793812SPavel Zakharov 17656f793812SPavel Zakharov /* 17666f793812SPavel Zakharov * If this is a verbatim import, no need to check the 17676f793812SPavel Zakharov * state of the pool. 17686f793812SPavel Zakharov */ 17696f793812SPavel Zakharov if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 17706f793812SPavel Zakharov spa_load_state(spa) == SPA_LOAD_OPEN && 17716f793812SPavel Zakharov state != POOL_STATE_ACTIVE) { 17726f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 17736f793812SPavel Zakharov "for spa %s", (u_longlong_t)state, spa->spa_name); 17746f793812SPavel Zakharov return (SET_ERROR(EBADF)); 17756f793812SPavel Zakharov } 17766f793812SPavel Zakharov 17776f793812SPavel Zakharov /* 17786f793812SPavel Zakharov * If we were able to open and validate a vdev that was 17796f793812SPavel Zakharov * previously marked permanently unavailable, clear that state 17806f793812SPavel Zakharov * now. 17816f793812SPavel Zakharov */ 17826f793812SPavel Zakharov if (vd->vdev_not_present) 17836f793812SPavel Zakharov vd->vdev_not_present = 0; 17846f793812SPavel Zakharov 17856f793812SPavel Zakharov return (0); 17866f793812SPavel Zakharov } 17876f793812SPavel Zakharov 17886f793812SPavel Zakharov static void 17896f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 17906f793812SPavel Zakharov { 17916f793812SPavel Zakharov if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 17926f793812SPavel Zakharov if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 17936f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 17946f793812SPavel Zakharov "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 17956f793812SPavel Zakharov dvd->vdev_path, svd->vdev_path); 17966f793812SPavel Zakharov spa_strfree(dvd->vdev_path); 17976f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 17983ee8c80cSPavel Zakharov } 17996f793812SPavel Zakharov } else if (svd->vdev_path != NULL) { 18006f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 18016f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 18026f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 18036f793812SPavel Zakharov } 18046f793812SPavel Zakharov } 1805560e6e96Seschrock 18066f793812SPavel Zakharov /* 18076f793812SPavel Zakharov * Recursively copy vdev paths from one vdev to another. Source and destination 18086f793812SPavel Zakharov * vdev trees must have same geometry otherwise return error. Intended to copy 18096f793812SPavel Zakharov * paths from userland config into MOS config. 18106f793812SPavel Zakharov */ 18116f793812SPavel Zakharov int 18126f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 18136f793812SPavel Zakharov { 18146f793812SPavel Zakharov if ((svd->vdev_ops == &vdev_missing_ops) || 18156f793812SPavel Zakharov (svd->vdev_ishole && dvd->vdev_ishole) || 18166f793812SPavel Zakharov (dvd->vdev_ops == &vdev_indirect_ops)) 18176f793812SPavel Zakharov return (0); 18186f793812SPavel Zakharov 18196f793812SPavel Zakharov if (svd->vdev_ops != dvd->vdev_ops) { 18206f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 18216f793812SPavel Zakharov svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 18226f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 18236f793812SPavel Zakharov } 18246f793812SPavel Zakharov 18256f793812SPavel Zakharov if (svd->vdev_guid != dvd->vdev_guid) { 18266f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 18276f793812SPavel Zakharov "%llu)", (u_longlong_t)svd->vdev_guid, 18286f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid); 18296f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 18306f793812SPavel Zakharov } 18316f793812SPavel Zakharov 18326f793812SPavel Zakharov if (svd->vdev_children != dvd->vdev_children) { 18336f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 18346f793812SPavel Zakharov "%llu != %llu", (u_longlong_t)svd->vdev_children, 18356f793812SPavel Zakharov (u_longlong_t)dvd->vdev_children); 18366f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 183751ece835Seschrock } 1838560e6e96Seschrock 18396f793812SPavel Zakharov for (uint64_t i = 0; i < svd->vdev_children; i++) { 18406f793812SPavel Zakharov int error = vdev_copy_path_strict(svd->vdev_child[i], 18416f793812SPavel Zakharov dvd->vdev_child[i]); 18426f793812SPavel Zakharov if (error != 0) 18436f793812SPavel Zakharov return (error); 18446f793812SPavel Zakharov } 18456f793812SPavel Zakharov 18466f793812SPavel Zakharov if (svd->vdev_ops->vdev_op_leaf) 18476f793812SPavel Zakharov vdev_copy_path_impl(svd, dvd); 18486f793812SPavel Zakharov 1849560e6e96Seschrock return (0); 1850560e6e96Seschrock } 1851560e6e96Seschrock 18526f793812SPavel Zakharov static void 18536f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 18546f793812SPavel Zakharov { 18556f793812SPavel Zakharov ASSERT(stvd->vdev_top == stvd); 18566f793812SPavel Zakharov ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 18576f793812SPavel Zakharov 18586f793812SPavel Zakharov for (uint64_t i = 0; i < dvd->vdev_children; i++) { 18596f793812SPavel Zakharov vdev_copy_path_search(stvd, dvd->vdev_child[i]); 18606f793812SPavel Zakharov } 18616f793812SPavel Zakharov 18626f793812SPavel Zakharov if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 18636f793812SPavel Zakharov return; 18646f793812SPavel Zakharov 18656f793812SPavel Zakharov /* 18666f793812SPavel Zakharov * The idea here is that while a vdev can shift positions within 18676f793812SPavel Zakharov * a top vdev (when replacing, attaching mirror, etc.) it cannot 18686f793812SPavel Zakharov * step outside of it. 18696f793812SPavel Zakharov */ 18706f793812SPavel Zakharov vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 18716f793812SPavel Zakharov 18726f793812SPavel Zakharov if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 18736f793812SPavel Zakharov return; 18746f793812SPavel Zakharov 18756f793812SPavel Zakharov ASSERT(vd->vdev_ops->vdev_op_leaf); 18766f793812SPavel Zakharov 18776f793812SPavel Zakharov vdev_copy_path_impl(vd, dvd); 18786f793812SPavel Zakharov } 18796f793812SPavel Zakharov 18806f793812SPavel Zakharov /* 18816f793812SPavel Zakharov * Recursively copy vdev paths from one root vdev to another. Source and 18826f793812SPavel Zakharov * destination vdev trees may differ in geometry. For each destination leaf 18836f793812SPavel Zakharov * vdev, search a vdev with the same guid and top vdev id in the source. 18846f793812SPavel Zakharov * Intended to copy paths from userland config into MOS config. 18856f793812SPavel Zakharov */ 18866f793812SPavel Zakharov void 18876f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 18886f793812SPavel Zakharov { 18896f793812SPavel Zakharov uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 18906f793812SPavel Zakharov ASSERT(srvd->vdev_ops == &vdev_root_ops); 18916f793812SPavel Zakharov ASSERT(drvd->vdev_ops == &vdev_root_ops); 18926f793812SPavel Zakharov 18936f793812SPavel Zakharov for (uint64_t i = 0; i < children; i++) { 18946f793812SPavel Zakharov vdev_copy_path_search(srvd->vdev_child[i], 18956f793812SPavel Zakharov drvd->vdev_child[i]); 18966f793812SPavel Zakharov } 18976f793812SPavel Zakharov } 18986f793812SPavel Zakharov 1899fa9e4066Sahrens /* 1900fa9e4066Sahrens * Close a virtual device. 1901fa9e4066Sahrens */ 1902fa9e4066Sahrens void 1903fa9e4066Sahrens vdev_close(vdev_t *vd) 1904fa9e4066Sahrens { 19058ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1906095bcd66SGeorge Wilson vdev_t *pvd = vd->vdev_parent; 19078ad4d6ddSJeff Bonwick 19088ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 19098ad4d6ddSJeff Bonwick 19101195e687SMark J Musante /* 19111195e687SMark J Musante * If our parent is reopening, then we are as well, unless we are 19121195e687SMark J Musante * going offline. 19131195e687SMark J Musante */ 1914095bcd66SGeorge Wilson if (pvd != NULL && pvd->vdev_reopening) 19151195e687SMark J Musante vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1916095bcd66SGeorge Wilson 1917fa9e4066Sahrens vd->vdev_ops->vdev_op_close(vd); 1918fa9e4066Sahrens 19193d7072f8Seschrock vdev_cache_purge(vd); 1920fa9e4066Sahrens 1921560e6e96Seschrock /* 1922573ca77eSGeorge Wilson * We record the previous state before we close it, so that if we are 1923560e6e96Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 1924560e6e96Seschrock * it's still faulted. 1925560e6e96Seschrock */ 1926560e6e96Seschrock vd->vdev_prevstate = vd->vdev_state; 1927560e6e96Seschrock 1928fa9e4066Sahrens if (vd->vdev_offline) 1929fa9e4066Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 1930fa9e4066Sahrens else 1931fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 1932ea8dc4b6Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1933fa9e4066Sahrens } 1934fa9e4066Sahrens 1935dcba9f3fSGeorge Wilson void 1936dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd) 1937dcba9f3fSGeorge Wilson { 1938dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 1939dcba9f3fSGeorge Wilson 1940dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 1941dcba9f3fSGeorge Wilson if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1942dcba9f3fSGeorge Wilson return; 1943dcba9f3fSGeorge Wilson 1944dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1945dcba9f3fSGeorge Wilson vdev_hold(vd->vdev_child[c]); 1946dcba9f3fSGeorge Wilson 1947dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 1948dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_hold(vd); 1949dcba9f3fSGeorge Wilson } 1950dcba9f3fSGeorge Wilson 1951dcba9f3fSGeorge Wilson void 1952dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd) 1953dcba9f3fSGeorge Wilson { 1954dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 1955dcba9f3fSGeorge Wilson 1956dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 1957dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1958dcba9f3fSGeorge Wilson vdev_rele(vd->vdev_child[c]); 1959dcba9f3fSGeorge Wilson 1960dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 1961dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_rele(vd); 1962dcba9f3fSGeorge Wilson } 1963dcba9f3fSGeorge Wilson 1964095bcd66SGeorge Wilson /* 1965095bcd66SGeorge Wilson * Reopen all interior vdevs and any unopened leaves. We don't actually 1966095bcd66SGeorge Wilson * reopen leaf vdevs which had previously been opened as they might deadlock 1967095bcd66SGeorge Wilson * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1968095bcd66SGeorge Wilson * If the leaf has never been opened then open it, as usual. 1969095bcd66SGeorge Wilson */ 1970fa9e4066Sahrens void 1971ea8dc4b6Seschrock vdev_reopen(vdev_t *vd) 1972fa9e4066Sahrens { 1973ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 1974fa9e4066Sahrens 1975e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1976ea8dc4b6Seschrock 19771195e687SMark J Musante /* set the reopening flag unless we're taking the vdev offline */ 19781195e687SMark J Musante vd->vdev_reopening = !vd->vdev_offline; 1979fa9e4066Sahrens vdev_close(vd); 1980fa9e4066Sahrens (void) vdev_open(vd); 1981fa9e4066Sahrens 198239c23413Seschrock /* 198339c23413Seschrock * Call vdev_validate() here to make sure we have the same device. 198439c23413Seschrock * Otherwise, a device with an invalid label could be successfully 198539c23413Seschrock * opened in response to vdev_reopen(). 198639c23413Seschrock */ 1987c5904d13Seschrock if (vd->vdev_aux) { 1988c5904d13Seschrock (void) vdev_validate_aux(vd); 1989e14bb325SJeff Bonwick if (vdev_readable(vd) && vdev_writeable(vd) && 19906809eb4eSEric Schrock vd->vdev_aux == &spa->spa_l2cache && 1991573ca77eSGeorge Wilson !l2arc_vdev_present(vd)) 1992573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1993c5904d13Seschrock } else { 19946f793812SPavel Zakharov (void) vdev_validate(vd); 1995c5904d13Seschrock } 199639c23413Seschrock 1997fa9e4066Sahrens /* 19983d7072f8Seschrock * Reassess parent vdev's health. 1999fa9e4066Sahrens */ 20003d7072f8Seschrock vdev_propagate_state(vd); 2001fa9e4066Sahrens } 2002fa9e4066Sahrens 2003fa9e4066Sahrens int 200499653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2005fa9e4066Sahrens { 2006fa9e4066Sahrens int error; 2007fa9e4066Sahrens 2008fa9e4066Sahrens /* 2009fa9e4066Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 2010fa9e4066Sahrens * For a create, however, we want to fail the request if 2011fa9e4066Sahrens * there are any components we can't open. 2012fa9e4066Sahrens */ 2013fa9e4066Sahrens error = vdev_open(vd); 2014fa9e4066Sahrens 2015fa9e4066Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2016fa9e4066Sahrens vdev_close(vd); 2017fa9e4066Sahrens return (error ? error : ENXIO); 2018fa9e4066Sahrens } 2019fa9e4066Sahrens 2020fa9e4066Sahrens /* 20210713e232SGeorge Wilson * Recursively load DTLs and initialize all labels. 2022fa9e4066Sahrens */ 20230713e232SGeorge Wilson if ((error = vdev_dtl_load(vd)) != 0 || 20240713e232SGeorge Wilson (error = vdev_label_init(vd, txg, isreplacing ? 202539c23413Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2026fa9e4066Sahrens vdev_close(vd); 2027fa9e4066Sahrens return (error); 2028fa9e4066Sahrens } 2029fa9e4066Sahrens 2030fa9e4066Sahrens return (0); 2031fa9e4066Sahrens } 2032fa9e4066Sahrens 20330e34b6a7Sbonwick void 2034573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd) 2035fa9e4066Sahrens { 203686714001SSerapheim Dimitropoulos uint64_t asize = vd->vdev_asize; 2037*b4bf0cf0SDon Brady uint64_t ms_count = asize >> vdev_default_ms_shift; 2038*b4bf0cf0SDon Brady uint64_t ms_shift; 203986714001SSerapheim Dimitropoulos 2040fa9e4066Sahrens /* 2041*b4bf0cf0SDon Brady * There are two dimensions to the metaslab sizing calculation: 2042*b4bf0cf0SDon Brady * the size of the metaslab and the count of metaslabs per vdev. 2043*b4bf0cf0SDon Brady * In general, we aim for vdev_max_ms_count (200) metaslabs. The 2044*b4bf0cf0SDon Brady * range of the dimensions are as follows: 2045*b4bf0cf0SDon Brady * 2046*b4bf0cf0SDon Brady * 2^29 <= ms_size <= 2^38 2047*b4bf0cf0SDon Brady * 16 <= ms_count <= 131,072 2048*b4bf0cf0SDon Brady * 2049*b4bf0cf0SDon Brady * On the lower end of vdev sizes, we aim for metaslabs sizes of 2050*b4bf0cf0SDon Brady * at least 512MB (2^29) to minimize fragmentation effects when 2051*b4bf0cf0SDon Brady * testing with smaller devices. However, the count constraint 2052*b4bf0cf0SDon Brady * of at least 16 metaslabs will override this minimum size goal. 2053*b4bf0cf0SDon Brady * 2054*b4bf0cf0SDon Brady * On the upper end of vdev sizes, we aim for a maximum metaslab 2055*b4bf0cf0SDon Brady * size of 256GB. However, we will cap the total count to 2^17 2056*b4bf0cf0SDon Brady * metaslabs to keep our memory footprint in check. 2057*b4bf0cf0SDon Brady * 2058*b4bf0cf0SDon Brady * The net effect of applying above constrains is summarized below. 2059*b4bf0cf0SDon Brady * 2060*b4bf0cf0SDon Brady * vdev size metaslab count 2061*b4bf0cf0SDon Brady * -------------|----------------- 2062*b4bf0cf0SDon Brady * < 8GB ~16 2063*b4bf0cf0SDon Brady * 8GB - 100GB one per 512MB 2064*b4bf0cf0SDon Brady * 100GB - 50TB ~200 2065*b4bf0cf0SDon Brady * 50TB - 32PB one per 256GB 2066*b4bf0cf0SDon Brady * > 32PB ~131,072 2067*b4bf0cf0SDon Brady * ------------------------------- 2068fa9e4066Sahrens */ 206986714001SSerapheim Dimitropoulos 2070*b4bf0cf0SDon Brady if (ms_count < vdev_min_ms_count) 207186714001SSerapheim Dimitropoulos ms_shift = highbit64(asize / vdev_min_ms_count); 2072*b4bf0cf0SDon Brady else if (ms_count > vdev_max_ms_count) 207386714001SSerapheim Dimitropoulos ms_shift = highbit64(asize / vdev_max_ms_count); 2074*b4bf0cf0SDon Brady else 2075*b4bf0cf0SDon Brady ms_shift = vdev_default_ms_shift; 2076*b4bf0cf0SDon Brady 2077*b4bf0cf0SDon Brady if (ms_shift < SPA_MAXBLOCKSHIFT) { 2078*b4bf0cf0SDon Brady ms_shift = SPA_MAXBLOCKSHIFT; 2079*b4bf0cf0SDon Brady } else if (ms_shift > vdev_max_ms_shift) { 2080*b4bf0cf0SDon Brady ms_shift = vdev_max_ms_shift; 2081*b4bf0cf0SDon Brady /* cap the total count to constrain memory footprint */ 2082*b4bf0cf0SDon Brady if ((asize >> ms_shift) > vdev_ms_count_limit) 2083*b4bf0cf0SDon Brady ms_shift = highbit64(asize / vdev_ms_count_limit); 208486714001SSerapheim Dimitropoulos } 208586714001SSerapheim Dimitropoulos 208686714001SSerapheim Dimitropoulos vd->vdev_ms_shift = ms_shift; 208786714001SSerapheim Dimitropoulos ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2088fa9e4066Sahrens } 2089fa9e4066Sahrens 2090fa9e4066Sahrens void 2091ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2092fa9e4066Sahrens { 2093ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 20945cabbc6bSPrashanth Sreenivasa /* indirect vdevs don't have metaslabs or dtls */ 20955cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd) || flags == 0); 2096ecc2d604Sbonwick ASSERT(ISP2(flags)); 2097f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 2098fa9e4066Sahrens 2099ecc2d604Sbonwick if (flags & VDD_METASLAB) 2100ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2101ecc2d604Sbonwick 2102ecc2d604Sbonwick if (flags & VDD_DTL) 2103ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2104ecc2d604Sbonwick 2105ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2106fa9e4066Sahrens } 2107fa9e4066Sahrens 21080713e232SGeorge Wilson void 21090713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 21100713e232SGeorge Wilson { 21110713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 21120713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 21130713e232SGeorge Wilson 21140713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 21150713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 21160713e232SGeorge Wilson } 21170713e232SGeorge Wilson 21188ad4d6ddSJeff Bonwick /* 21198ad4d6ddSJeff Bonwick * DTLs. 21208ad4d6ddSJeff Bonwick * 21218ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 21229fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 21238ad4d6ddSJeff Bonwick * 21248ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 21258ad4d6ddSJeff Bonwick * 21268ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 21278ad4d6ddSJeff Bonwick * 21288ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 21298ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 21308ad4d6ddSJeff Bonwick * txgs that was scrubbed. 21318ad4d6ddSJeff Bonwick * 21328ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 21338ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 21348ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 21358ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 21368ad4d6ddSJeff Bonwick * determine whether a device can be detached. 21378ad4d6ddSJeff Bonwick * 21388ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 21398ad4d6ddSJeff Bonwick * either has the data or it doesn't. 21408ad4d6ddSJeff Bonwick * 21418ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 21428ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 21438ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 21448ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 21458ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 21468ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 21478ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 21488ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 21498ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 21508ad4d6ddSJeff Bonwick * 21518ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 21528ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 21538ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 21548ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 21558ad4d6ddSJeff Bonwick */ 2156fa9e4066Sahrens void 21578ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2158fa9e4066Sahrens { 21590713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21608ad4d6ddSJeff Bonwick 21618ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 21628ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2163f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 21648ad4d6ddSJeff Bonwick 21655cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 21660713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 21670713e232SGeorge Wilson range_tree_add(rt, txg, size); 21685cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2169fa9e4066Sahrens } 2170fa9e4066Sahrens 21718ad4d6ddSJeff Bonwick boolean_t 21728ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2173fa9e4066Sahrens { 21740713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21758ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 2176fa9e4066Sahrens 21778ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 21788ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2179fa9e4066Sahrens 21805cabbc6bSPrashanth Sreenivasa /* 21815cabbc6bSPrashanth Sreenivasa * While we are loading the pool, the DTLs have not been loaded yet. 21825cabbc6bSPrashanth Sreenivasa * Ignore the DTLs and try all devices. This avoids a recursive 21835cabbc6bSPrashanth Sreenivasa * mutex enter on the vdev_dtl_lock, and also makes us try hard 21845cabbc6bSPrashanth Sreenivasa * when loading the pool (relying on the checksum to ensure that 21855cabbc6bSPrashanth Sreenivasa * we get the right data -- note that we while loading, we are 21865cabbc6bSPrashanth Sreenivasa * only reading the MOS, which is always checksummed). 21875cabbc6bSPrashanth Sreenivasa */ 21885cabbc6bSPrashanth Sreenivasa if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 21895cabbc6bSPrashanth Sreenivasa return (B_FALSE); 21905cabbc6bSPrashanth Sreenivasa 21915cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 219286714001SSerapheim Dimitropoulos if (!range_tree_is_empty(rt)) 21930713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 21945cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2195fa9e4066Sahrens 2196fa9e4066Sahrens return (dirty); 2197fa9e4066Sahrens } 2198fa9e4066Sahrens 21998ad4d6ddSJeff Bonwick boolean_t 22008ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 22018ad4d6ddSJeff Bonwick { 22020713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 22038ad4d6ddSJeff Bonwick boolean_t empty; 22048ad4d6ddSJeff Bonwick 22055cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 220686714001SSerapheim Dimitropoulos empty = range_tree_is_empty(rt); 22075cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 22088ad4d6ddSJeff Bonwick 22098ad4d6ddSJeff Bonwick return (empty); 22108ad4d6ddSJeff Bonwick } 22118ad4d6ddSJeff Bonwick 2212b4952e17SGeorge Wilson /* 2213b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 2214b4952e17SGeorge Wilson */ 2215b4952e17SGeorge Wilson static uint64_t 2216b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 2217b4952e17SGeorge Wilson { 22180713e232SGeorge Wilson range_seg_t *rs; 2219b4952e17SGeorge Wilson 2220b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 22210713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2222b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2223b4952e17SGeorge Wilson 22240713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 22250713e232SGeorge Wilson return (rs->rs_start - 1); 2226b4952e17SGeorge Wilson } 2227b4952e17SGeorge Wilson 2228b4952e17SGeorge Wilson /* 2229b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 2230b4952e17SGeorge Wilson */ 2231b4952e17SGeorge Wilson static uint64_t 2232b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 2233b4952e17SGeorge Wilson { 22340713e232SGeorge Wilson range_seg_t *rs; 2235b4952e17SGeorge Wilson 2236b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 22370713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2238b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2239b4952e17SGeorge Wilson 22400713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 22410713e232SGeorge Wilson return (rs->rs_end); 2242b4952e17SGeorge Wilson } 2243b4952e17SGeorge Wilson 2244b4952e17SGeorge Wilson /* 2245b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 2246b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 2247b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 2248b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 2249b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 2250b4952e17SGeorge Wilson * excise the DTLs. 2251b4952e17SGeorge Wilson */ 2252b4952e17SGeorge Wilson static boolean_t 2253b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 2254b4952e17SGeorge Wilson { 2255b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 2256b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2257b4952e17SGeorge Wilson 2258b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 2259b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2260b4952e17SGeorge Wilson 22612d2f193aSMatthew Ahrens if (vd->vdev_state < VDEV_STATE_DEGRADED) 22622d2f193aSMatthew Ahrens return (B_FALSE); 22632d2f193aSMatthew Ahrens 2264b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 226586714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2266b4952e17SGeorge Wilson return (B_TRUE); 2267b4952e17SGeorge Wilson 2268b4952e17SGeorge Wilson /* 2269b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 2270b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 2271b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 2272b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 2273b4952e17SGeorge Wilson * for excision. 2274b4952e17SGeorge Wilson */ 2275b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2276b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2277b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2278b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2279b4952e17SGeorge Wilson return (B_TRUE); 2280b4952e17SGeorge Wilson } 2281b4952e17SGeorge Wilson return (B_FALSE); 2282b4952e17SGeorge Wilson } 2283b4952e17SGeorge Wilson 2284fa9e4066Sahrens /* 2285fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 2286fa9e4066Sahrens */ 2287fa9e4066Sahrens void 2288fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2289fa9e4066Sahrens { 2290ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 22918ad4d6ddSJeff Bonwick avl_tree_t reftree; 22928ad4d6ddSJeff Bonwick int minref; 2293fa9e4066Sahrens 22948ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2295fa9e4066Sahrens 22968ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 22978ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 22988ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 22998ad4d6ddSJeff Bonwick 23005cabbc6bSPrashanth Sreenivasa if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 23018ad4d6ddSJeff Bonwick return; 23028ad4d6ddSJeff Bonwick 23038ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 23043f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 23053f9d6ad7SLin Ling 2306fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 2307b4952e17SGeorge Wilson 2308b4952e17SGeorge Wilson /* 2309b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 2310b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 2311b4952e17SGeorge Wilson * excise regions on vdevs that were available during 2312b4952e17SGeorge Wilson * the entire duration of this scan. 2313b4952e17SGeorge Wilson */ 2314088f3894Sahrens if (scrub_txg != 0 && 23153f9d6ad7SLin Ling (spa->spa_scrub_started || 2316b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2317b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 2318088f3894Sahrens /* 2319088f3894Sahrens * We completed a scrub up to scrub_txg. If we 2320088f3894Sahrens * did it without rebooting, then the scrub dtl 2321088f3894Sahrens * will be valid, so excise the old region and 2322088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 2323088f3894Sahrens * dtl as-is if there was an error. 23248ad4d6ddSJeff Bonwick * 23258ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 23268ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 23278ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 23288ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 23298ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 23308ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 23318ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 23328ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 23338ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 2334088f3894Sahrens */ 23350713e232SGeorge Wilson space_reftree_create(&reftree); 23360713e232SGeorge Wilson space_reftree_add_map(&reftree, 23370713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 23380713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 23390713e232SGeorge Wilson space_reftree_add_map(&reftree, 23400713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 23410713e232SGeorge Wilson space_reftree_generate_map(&reftree, 23420713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 23430713e232SGeorge Wilson space_reftree_destroy(&reftree); 2344fa9e4066Sahrens } 23450713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 23460713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 23470713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2348fa9e4066Sahrens if (scrub_done) 23490713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 23500713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 23518ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 23520713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 23538ad4d6ddSJeff Bonwick else 23540713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 23550713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2356b4952e17SGeorge Wilson 2357b4952e17SGeorge Wilson /* 2358b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 2359b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 2360b4952e17SGeorge Wilson */ 2361b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 236286714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 236386714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2364b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 2365b4952e17SGeorge Wilson 2366fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2367088f3894Sahrens 2368ecc2d604Sbonwick if (txg != 0) 2369ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2370fa9e4066Sahrens return; 2371fa9e4066Sahrens } 2372fa9e4066Sahrens 2373fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 23748ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 237599bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 237699bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 23778ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 23788ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 23798ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 23808ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 23818ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 23828ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 23838ad4d6ddSJeff Bonwick else 23848ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 23850713e232SGeorge Wilson space_reftree_create(&reftree); 23868ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 23878ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 23888ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 23890713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 23908ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 23918ad4d6ddSJeff Bonwick } 23920713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 23930713e232SGeorge Wilson space_reftree_destroy(&reftree); 2394fa9e4066Sahrens } 23958ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 2396fa9e4066Sahrens } 2397fa9e4066Sahrens 23980713e232SGeorge Wilson int 2399fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 2400fa9e4066Sahrens { 2401fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2402ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 24030713e232SGeorge Wilson int error = 0; 2404fa9e4066Sahrens 24050713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 24065cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 2407fa9e4066Sahrens 24080713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 24095cabbc6bSPrashanth Sreenivasa vd->vdev_dtl_object, 0, -1ULL, 0); 24100713e232SGeorge Wilson if (error) 24110713e232SGeorge Wilson return (error); 24120713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2413fa9e4066Sahrens 24140713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 241588ecc943SGeorge Wilson 24160713e232SGeorge Wilson /* 24170713e232SGeorge Wilson * Now that we've opened the space_map we need to update 24180713e232SGeorge Wilson * the in-core DTL. 24190713e232SGeorge Wilson */ 24200713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 2421ecc2d604Sbonwick 24220713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 24230713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 24240713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2425fa9e4066Sahrens 24260713e232SGeorge Wilson return (error); 24270713e232SGeorge Wilson } 24280713e232SGeorge Wilson 24290713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 24300713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 24310713e232SGeorge Wilson if (error != 0) 24320713e232SGeorge Wilson break; 24330713e232SGeorge Wilson } 2434fa9e4066Sahrens 2435fa9e4066Sahrens return (error); 2436fa9e4066Sahrens } 2437fa9e4066Sahrens 2438215198a6SJoe Stein void 2439215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2440215198a6SJoe Stein { 2441215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2442215198a6SJoe Stein 2443215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2444215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2445215198a6SJoe Stein zapobj, tx)); 2446215198a6SJoe Stein } 2447215198a6SJoe Stein 2448215198a6SJoe Stein uint64_t 2449215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2450215198a6SJoe Stein { 2451215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2452215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2453215198a6SJoe Stein DMU_OT_NONE, 0, tx); 2454215198a6SJoe Stein 2455215198a6SJoe Stein ASSERT(zap != 0); 2456215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2457215198a6SJoe Stein zap, tx)); 2458215198a6SJoe Stein 2459215198a6SJoe Stein return (zap); 2460215198a6SJoe Stein } 2461215198a6SJoe Stein 2462215198a6SJoe Stein void 2463215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2464215198a6SJoe Stein { 2465215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 2466215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 2467215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 2468215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 2469215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2470215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2471215198a6SJoe Stein } 2472215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2473215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2474215198a6SJoe Stein } 2475215198a6SJoe Stein } 2476215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2477215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2478215198a6SJoe Stein } 2479215198a6SJoe Stein } 2480215198a6SJoe Stein 2481fa9e4066Sahrens void 2482fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2483fa9e4066Sahrens { 2484fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 24850713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2486ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 24870713e232SGeorge Wilson range_tree_t *rtsync; 2488fa9e4066Sahrens dmu_tx_t *tx; 24890713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2490fa9e4066Sahrens 24915cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 24920713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 249388ecc943SGeorge Wilson 2494fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2495fa9e4066Sahrens 24960713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 24970713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 24980713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 24990713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 25000713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 25010713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2502215198a6SJoe Stein 2503215198a6SJoe Stein /* 2504215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2505215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2506215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2507215198a6SJoe Stein */ 2508215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2509215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2510215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2511215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2512215198a6SJoe Stein } 2513215198a6SJoe Stein 2514fa9e4066Sahrens dmu_tx_commit(tx); 2515fa9e4066Sahrens return; 2516fa9e4066Sahrens } 2517fa9e4066Sahrens 25180713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 25190713e232SGeorge Wilson uint64_t new_object; 25200713e232SGeorge Wilson 252186714001SSerapheim Dimitropoulos new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 25220713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 25230713e232SGeorge Wilson 25240713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 25255cabbc6bSPrashanth Sreenivasa 0, -1ULL, 0)); 25260713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2527fa9e4066Sahrens } 2528fa9e4066Sahrens 25295cabbc6bSPrashanth Sreenivasa rtsync = range_tree_create(NULL, NULL); 2530fa9e4066Sahrens 2531fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 25320713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2533fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2534fa9e4066Sahrens 253586714001SSerapheim Dimitropoulos space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 253617f11284SSerapheim Dimitropoulos space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 25370713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2538fa9e4066Sahrens 25390713e232SGeorge Wilson range_tree_destroy(rtsync); 2540fa9e4066Sahrens 25410713e232SGeorge Wilson /* 25420713e232SGeorge Wilson * If the object for the space map has changed then dirty 25430713e232SGeorge Wilson * the top level so that we update the config. 25440713e232SGeorge Wilson */ 25450713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 25463ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 25473ee8c80cSPavel Zakharov "new object %llu", (u_longlong_t)txg, spa_name(spa), 25483ee8c80cSPavel Zakharov (u_longlong_t)object, 25493ee8c80cSPavel Zakharov (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 25500713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 25510713e232SGeorge Wilson } 2552fa9e4066Sahrens 2553fa9e4066Sahrens dmu_tx_commit(tx); 25540713e232SGeorge Wilson 25550713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 25560713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 25570713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2558fa9e4066Sahrens } 2559fa9e4066Sahrens 25608ad4d6ddSJeff Bonwick /* 25618ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 25628ad4d6ddSJeff Bonwick * without losing data. 25638ad4d6ddSJeff Bonwick */ 25648ad4d6ddSJeff Bonwick boolean_t 25658ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 25668ad4d6ddSJeff Bonwick { 25678ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 25688ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 25698ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 25708ad4d6ddSJeff Bonwick boolean_t required; 25718ad4d6ddSJeff Bonwick 25728ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 25738ad4d6ddSJeff Bonwick 25748ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 25758ad4d6ddSJeff Bonwick return (B_TRUE); 25768ad4d6ddSJeff Bonwick 25778ad4d6ddSJeff Bonwick /* 25788ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 25798ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 25808ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 25818ad4d6ddSJeff Bonwick */ 25828ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 25838ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 25848ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 25858ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 25868ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 25878ad4d6ddSJeff Bonwick 2588cb04b873SMark J Musante if (!required && zio_injection_enabled) 2589cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2590cb04b873SMark J Musante 25918ad4d6ddSJeff Bonwick return (required); 25928ad4d6ddSJeff Bonwick } 25938ad4d6ddSJeff Bonwick 2594088f3894Sahrens /* 2595088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2596088f3894Sahrens */ 2597088f3894Sahrens boolean_t 2598088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2599088f3894Sahrens { 2600088f3894Sahrens boolean_t needed = B_FALSE; 2601088f3894Sahrens uint64_t thismin = UINT64_MAX; 2602088f3894Sahrens uint64_t thismax = 0; 2603088f3894Sahrens 2604088f3894Sahrens if (vd->vdev_children == 0) { 2605088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 260686714001SSerapheim Dimitropoulos if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 26078ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2608088f3894Sahrens 2609b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2610b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2611088f3894Sahrens needed = B_TRUE; 2612088f3894Sahrens } 2613088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2614088f3894Sahrens } else { 26158ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2616088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2617088f3894Sahrens uint64_t cmin, cmax; 2618088f3894Sahrens 2619088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2620088f3894Sahrens thismin = MIN(thismin, cmin); 2621088f3894Sahrens thismax = MAX(thismax, cmax); 2622088f3894Sahrens needed = B_TRUE; 2623088f3894Sahrens } 2624088f3894Sahrens } 2625088f3894Sahrens } 2626088f3894Sahrens 2627088f3894Sahrens if (needed && minp) { 2628088f3894Sahrens *minp = thismin; 2629088f3894Sahrens *maxp = thismax; 2630088f3894Sahrens } 2631088f3894Sahrens return (needed); 2632088f3894Sahrens } 2633088f3894Sahrens 263486714001SSerapheim Dimitropoulos /* 263586714001SSerapheim Dimitropoulos * Gets the checkpoint space map object from the vdev's ZAP. 263686714001SSerapheim Dimitropoulos * Returns the spacemap object, or 0 if it wasn't in the ZAP 263786714001SSerapheim Dimitropoulos * or the ZAP doesn't exist yet. 263886714001SSerapheim Dimitropoulos */ 263986714001SSerapheim Dimitropoulos int 264086714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd) 264186714001SSerapheim Dimitropoulos { 264286714001SSerapheim Dimitropoulos ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 264386714001SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) { 264486714001SSerapheim Dimitropoulos return (0); 264586714001SSerapheim Dimitropoulos } 264686714001SSerapheim Dimitropoulos 264786714001SSerapheim Dimitropoulos uint64_t sm_obj = 0; 264886714001SSerapheim Dimitropoulos int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 264986714001SSerapheim Dimitropoulos VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 265086714001SSerapheim Dimitropoulos 265186714001SSerapheim Dimitropoulos ASSERT(err == 0 || err == ENOENT); 265286714001SSerapheim Dimitropoulos 265386714001SSerapheim Dimitropoulos return (sm_obj); 265486714001SSerapheim Dimitropoulos } 265586714001SSerapheim Dimitropoulos 26565cabbc6bSPrashanth Sreenivasa int 2657ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2658fa9e4066Sahrens { 26595cabbc6bSPrashanth Sreenivasa int error = 0; 2660fa9e4066Sahrens /* 2661fa9e4066Sahrens * Recursively load all children. 2662fa9e4066Sahrens */ 26635cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 26645cabbc6bSPrashanth Sreenivasa error = vdev_load(vd->vdev_child[c]); 26655cabbc6bSPrashanth Sreenivasa if (error != 0) { 26665cabbc6bSPrashanth Sreenivasa return (error); 26675cabbc6bSPrashanth Sreenivasa } 26685cabbc6bSPrashanth Sreenivasa } 26695cabbc6bSPrashanth Sreenivasa 26705cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 2671fa9e4066Sahrens 2672fa9e4066Sahrens /* 26730e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2674fa9e4066Sahrens */ 26755cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 26765cabbc6bSPrashanth Sreenivasa if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 26775cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 26785cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 26793ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 26803ee8c80cSPavel Zakharov "asize=%llu", (u_longlong_t)vd->vdev_ashift, 26813ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_asize); 26825cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 26835cabbc6bSPrashanth Sreenivasa } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 26843ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 26853ee8c80cSPavel Zakharov "[error=%d]", error); 26865cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 26875cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 26885cabbc6bSPrashanth Sreenivasa return (error); 26895cabbc6bSPrashanth Sreenivasa } 269086714001SSerapheim Dimitropoulos 269186714001SSerapheim Dimitropoulos uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 269286714001SSerapheim Dimitropoulos if (checkpoint_sm_obj != 0) { 269386714001SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 269486714001SSerapheim Dimitropoulos ASSERT(vd->vdev_asize != 0); 269586714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 269686714001SSerapheim Dimitropoulos 269786714001SSerapheim Dimitropoulos if ((error = space_map_open(&vd->vdev_checkpoint_sm, 269886714001SSerapheim Dimitropoulos mos, checkpoint_sm_obj, 0, vd->vdev_asize, 269986714001SSerapheim Dimitropoulos vd->vdev_ashift))) { 270086714001SSerapheim Dimitropoulos vdev_dbgmsg(vd, "vdev_load: space_map_open " 270186714001SSerapheim Dimitropoulos "failed for checkpoint spacemap (obj %llu) " 270286714001SSerapheim Dimitropoulos "[error=%d]", 270386714001SSerapheim Dimitropoulos (u_longlong_t)checkpoint_sm_obj, error); 270486714001SSerapheim Dimitropoulos return (error); 270586714001SSerapheim Dimitropoulos } 270686714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 270786714001SSerapheim Dimitropoulos space_map_update(vd->vdev_checkpoint_sm); 270886714001SSerapheim Dimitropoulos 270986714001SSerapheim Dimitropoulos /* 271086714001SSerapheim Dimitropoulos * Since the checkpoint_sm contains free entries 271186714001SSerapheim Dimitropoulos * exclusively we can use sm_alloc to indicate the 271286714001SSerapheim Dimitropoulos * culmulative checkpointed space that has been freed. 271386714001SSerapheim Dimitropoulos */ 271486714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space = 271586714001SSerapheim Dimitropoulos -vd->vdev_checkpoint_sm->sm_alloc; 271686714001SSerapheim Dimitropoulos vd->vdev_spa->spa_checkpoint_info.sci_dspace += 271786714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space; 271886714001SSerapheim Dimitropoulos } 27195cabbc6bSPrashanth Sreenivasa } 2720fa9e4066Sahrens 2721fa9e4066Sahrens /* 2722fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2723fa9e4066Sahrens */ 27245cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2725560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2726560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 27273ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 27283ee8c80cSPavel Zakharov "[error=%d]", error); 27295cabbc6bSPrashanth Sreenivasa return (error); 27305cabbc6bSPrashanth Sreenivasa } 27315cabbc6bSPrashanth Sreenivasa 27325cabbc6bSPrashanth Sreenivasa uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 27335cabbc6bSPrashanth Sreenivasa if (obsolete_sm_object != 0) { 27345cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 27355cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_asize != 0); 273686714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 27375cabbc6bSPrashanth Sreenivasa 27385cabbc6bSPrashanth Sreenivasa if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 27395cabbc6bSPrashanth Sreenivasa obsolete_sm_object, 0, vd->vdev_asize, 0))) { 27405cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 27415cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 27423ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 27433ee8c80cSPavel Zakharov "obsolete spacemap (obj %llu) [error=%d]", 27443ee8c80cSPavel Zakharov (u_longlong_t)obsolete_sm_object, error); 27455cabbc6bSPrashanth Sreenivasa return (error); 27465cabbc6bSPrashanth Sreenivasa } 27475cabbc6bSPrashanth Sreenivasa space_map_update(vd->vdev_obsolete_sm); 27485cabbc6bSPrashanth Sreenivasa } 27495cabbc6bSPrashanth Sreenivasa 27505cabbc6bSPrashanth Sreenivasa return (0); 2751fa9e4066Sahrens } 2752fa9e4066Sahrens 275399653d4eSeschrock /* 2754fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 2755fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 2756fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 2757fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 2758fa94a07fSbrendan * repurposed to another pool. 275999653d4eSeschrock */ 276099653d4eSeschrock int 2761fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 276299653d4eSeschrock { 276399653d4eSeschrock nvlist_t *label; 276499653d4eSeschrock uint64_t guid, version; 276599653d4eSeschrock uint64_t state; 276699653d4eSeschrock 2767e14bb325SJeff Bonwick if (!vdev_readable(vd)) 2768c5904d13Seschrock return (0); 2769c5904d13Seschrock 2770dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 277199653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 277299653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 277399653d4eSeschrock return (-1); 277499653d4eSeschrock } 277599653d4eSeschrock 277699653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2777ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 277899653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 277999653d4eSeschrock guid != vd->vdev_guid || 278099653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 278199653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 278299653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 278399653d4eSeschrock nvlist_free(label); 278499653d4eSeschrock return (-1); 278599653d4eSeschrock } 278699653d4eSeschrock 278799653d4eSeschrock /* 278899653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 278999653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 279099653d4eSeschrock */ 279199653d4eSeschrock nvlist_free(label); 279299653d4eSeschrock return (0); 279399653d4eSeschrock } 279499653d4eSeschrock 27955cabbc6bSPrashanth Sreenivasa /* 27965cabbc6bSPrashanth Sreenivasa * Free the objects used to store this vdev's spacemaps, and the array 27975cabbc6bSPrashanth Sreenivasa * that points to them. 27985cabbc6bSPrashanth Sreenivasa */ 279988ecc943SGeorge Wilson void 28005cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 28015cabbc6bSPrashanth Sreenivasa { 28025cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0) 28035cabbc6bSPrashanth Sreenivasa return; 28045cabbc6bSPrashanth Sreenivasa 28055cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 28065cabbc6bSPrashanth Sreenivasa uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 28075cabbc6bSPrashanth Sreenivasa size_t array_bytes = array_count * sizeof (uint64_t); 28085cabbc6bSPrashanth Sreenivasa uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 28095cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 28105cabbc6bSPrashanth Sreenivasa array_bytes, smobj_array, 0)); 28115cabbc6bSPrashanth Sreenivasa 28125cabbc6bSPrashanth Sreenivasa for (uint64_t i = 0; i < array_count; i++) { 28135cabbc6bSPrashanth Sreenivasa uint64_t smobj = smobj_array[i]; 28145cabbc6bSPrashanth Sreenivasa if (smobj == 0) 28155cabbc6bSPrashanth Sreenivasa continue; 28165cabbc6bSPrashanth Sreenivasa 28175cabbc6bSPrashanth Sreenivasa space_map_free_obj(mos, smobj, tx); 28185cabbc6bSPrashanth Sreenivasa } 28195cabbc6bSPrashanth Sreenivasa 28205cabbc6bSPrashanth Sreenivasa kmem_free(smobj_array, array_bytes); 28215cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 28225cabbc6bSPrashanth Sreenivasa vd->vdev_ms_array = 0; 28235cabbc6bSPrashanth Sreenivasa } 28245cabbc6bSPrashanth Sreenivasa 28255cabbc6bSPrashanth Sreenivasa static void 28265cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vdev_t *vd, uint64_t txg) 282788ecc943SGeorge Wilson { 282888ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 282988ecc943SGeorge Wilson dmu_tx_t *tx; 283088ecc943SGeorge Wilson 2831215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 2832215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 283388ecc943SGeorge Wilson 283488ecc943SGeorge Wilson if (vd->vdev_ms != NULL) { 28352e4c9986SGeorge Wilson metaslab_group_t *mg = vd->vdev_mg; 28362e4c9986SGeorge Wilson 28372e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 28382e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 28392e4c9986SGeorge Wilson 284088ecc943SGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) { 284188ecc943SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 284288ecc943SGeorge Wilson 28430713e232SGeorge Wilson if (msp == NULL || msp->ms_sm == NULL) 284488ecc943SGeorge Wilson continue; 284588ecc943SGeorge Wilson 28460713e232SGeorge Wilson mutex_enter(&msp->ms_lock); 28472e4c9986SGeorge Wilson /* 28482e4c9986SGeorge Wilson * If the metaslab was not loaded when the vdev 28492e4c9986SGeorge Wilson * was removed then the histogram accounting may 28502e4c9986SGeorge Wilson * not be accurate. Update the histogram information 28512e4c9986SGeorge Wilson * here so that we ensure that the metaslab group 28522e4c9986SGeorge Wilson * and metaslab class are up-to-date. 28532e4c9986SGeorge Wilson */ 28542e4c9986SGeorge Wilson metaslab_group_histogram_remove(mg, msp); 28552e4c9986SGeorge Wilson 28560713e232SGeorge Wilson VERIFY0(space_map_allocated(msp->ms_sm)); 28570713e232SGeorge Wilson space_map_close(msp->ms_sm); 28580713e232SGeorge Wilson msp->ms_sm = NULL; 28590713e232SGeorge Wilson mutex_exit(&msp->ms_lock); 286088ecc943SGeorge Wilson } 28612e4c9986SGeorge Wilson 286286714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 286386714001SSerapheim Dimitropoulos ASSERT(spa_has_checkpoint(spa)); 286486714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 286586714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 286686714001SSerapheim Dimitropoulos } 286786714001SSerapheim Dimitropoulos 28682e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 28692e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 28702e4c9986SGeorge Wilson for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 28712e4c9986SGeorge Wilson ASSERT0(mg->mg_histogram[i]); 287288ecc943SGeorge Wilson } 287388ecc943SGeorge Wilson 28745cabbc6bSPrashanth Sreenivasa tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 28755cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vd, tx); 2876215198a6SJoe Stein 2877215198a6SJoe Stein if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2878215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2879215198a6SJoe Stein vd->vdev_top_zap = 0; 2880215198a6SJoe Stein } 288188ecc943SGeorge Wilson dmu_tx_commit(tx); 288288ecc943SGeorge Wilson } 288388ecc943SGeorge Wilson 2884fa9e4066Sahrens void 2885fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 2886fa9e4066Sahrens { 2887fa9e4066Sahrens metaslab_t *msp; 288880eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2889fa9e4066Sahrens 28905cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 289188ecc943SGeorge Wilson 2892094e47e9SGeorge Wilson while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2893094e47e9SGeorge Wilson != NULL) 2894fa9e4066Sahrens metaslab_sync_done(msp, txg); 289580eb36f2SGeorge Wilson 289680eb36f2SGeorge Wilson if (reassess) 289780eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 2898fa9e4066Sahrens } 2899fa9e4066Sahrens 2900fa9e4066Sahrens void 2901fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 2902fa9e4066Sahrens { 2903fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2904fa9e4066Sahrens vdev_t *lvd; 2905fa9e4066Sahrens metaslab_t *msp; 2906ecc2d604Sbonwick dmu_tx_t *tx; 2907fa9e4066Sahrens 29085cabbc6bSPrashanth Sreenivasa if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 29095cabbc6bSPrashanth Sreenivasa dmu_tx_t *tx; 29105cabbc6bSPrashanth Sreenivasa 29115cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 29125cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 291388ecc943SGeorge Wilson 29145cabbc6bSPrashanth Sreenivasa tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 29155cabbc6bSPrashanth Sreenivasa vdev_indirect_sync_obsolete(vd, tx); 29165cabbc6bSPrashanth Sreenivasa dmu_tx_commit(tx); 29175cabbc6bSPrashanth Sreenivasa 29185cabbc6bSPrashanth Sreenivasa /* 29195cabbc6bSPrashanth Sreenivasa * If the vdev is indirect, it can't have dirty 29205cabbc6bSPrashanth Sreenivasa * metaslabs or DTLs. 29215cabbc6bSPrashanth Sreenivasa */ 29225cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 29235cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 29245cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 29255cabbc6bSPrashanth Sreenivasa return; 29265cabbc6bSPrashanth Sreenivasa } 29275cabbc6bSPrashanth Sreenivasa } 29285cabbc6bSPrashanth Sreenivasa 29295cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 29305cabbc6bSPrashanth Sreenivasa 29315cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 29325cabbc6bSPrashanth Sreenivasa !vd->vdev_removing) { 2933ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 29345cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 2935ecc2d604Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2936ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2937ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2938ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 2939ecc2d604Sbonwick vdev_config_dirty(vd); 2940ecc2d604Sbonwick dmu_tx_commit(tx); 2941ecc2d604Sbonwick } 2942fa9e4066Sahrens 2943ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2944fa9e4066Sahrens metaslab_sync(msp, txg); 2945ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2946ecc2d604Sbonwick } 2947fa9e4066Sahrens 2948fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2949fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 2950fa9e4066Sahrens 29515cabbc6bSPrashanth Sreenivasa /* 29525cabbc6bSPrashanth Sreenivasa * Remove the metadata associated with this vdev once it's empty. 29535cabbc6bSPrashanth Sreenivasa * Note that this is typically used for log/cache device removal; 29545cabbc6bSPrashanth Sreenivasa * we don't empty toplevel vdevs when removing them. But if 29555cabbc6bSPrashanth Sreenivasa * a toplevel happens to be emptied, this is not harmful. 29565cabbc6bSPrashanth Sreenivasa */ 29575cabbc6bSPrashanth Sreenivasa if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) { 29585cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vd, txg); 29595cabbc6bSPrashanth Sreenivasa } 29605cabbc6bSPrashanth Sreenivasa 2961fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2962fa9e4066Sahrens } 2963fa9e4066Sahrens 2964fa9e4066Sahrens uint64_t 2965fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2966fa9e4066Sahrens { 2967fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2968fa9e4066Sahrens } 2969fa9e4066Sahrens 29703d7072f8Seschrock /* 29713d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 29723d7072f8Seschrock * not be opened, and no I/O is attempted. 29733d7072f8Seschrock */ 2974fa9e4066Sahrens int 2975069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2976fa9e4066Sahrens { 29774b964adaSGeorge Wilson vdev_t *vd, *tvd; 2978fa9e4066Sahrens 29798f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 2980fa9e4066Sahrens 2981c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2982e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2983e14bb325SJeff Bonwick 29843d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 2985e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2986fa9e4066Sahrens 29874b964adaSGeorge Wilson tvd = vd->vdev_top; 29884b964adaSGeorge Wilson 2989069f55e2SEric Schrock /* 2990069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 2991069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 2992069f55e2SEric Schrock * were faulted. 2993069f55e2SEric Schrock */ 2994069f55e2SEric Schrock vd->vdev_label_aux = aux; 2995069f55e2SEric Schrock 29963d7072f8Seschrock /* 29973d7072f8Seschrock * Faulted state takes precedence over degraded. 29983d7072f8Seschrock */ 299998d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 30003d7072f8Seschrock vd->vdev_faulted = 1ULL; 30013d7072f8Seschrock vd->vdev_degraded = 0ULL; 3002069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 30033d7072f8Seschrock 30043d7072f8Seschrock /* 3005c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 3006c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 30073d7072f8Seschrock */ 30084b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 30093d7072f8Seschrock vd->vdev_degraded = 1ULL; 30103d7072f8Seschrock vd->vdev_faulted = 0ULL; 30113d7072f8Seschrock 30123d7072f8Seschrock /* 30133d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 30143d7072f8Seschrock * mark it degraded. 30153d7072f8Seschrock */ 30164b964adaSGeorge Wilson vdev_reopen(tvd); 30173d7072f8Seschrock 3018069f55e2SEric Schrock if (vdev_readable(vd)) 3019069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 30203d7072f8Seschrock } 30213d7072f8Seschrock 3022e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 30233d7072f8Seschrock } 30243d7072f8Seschrock 30253d7072f8Seschrock /* 30263d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 30273d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 30283d7072f8Seschrock * as I/O is concerned. 30293d7072f8Seschrock */ 30303d7072f8Seschrock int 3031069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 30323d7072f8Seschrock { 3033c5904d13Seschrock vdev_t *vd; 30340a4e9518Sgw 30358f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 30363d7072f8Seschrock 3037c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3038e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3039e14bb325SJeff Bonwick 30400e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3041e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 30420e34b6a7Sbonwick 30433d7072f8Seschrock /* 30443d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 30453d7072f8Seschrock */ 3046e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 3047e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 30483d7072f8Seschrock 30493d7072f8Seschrock vd->vdev_degraded = 1ULL; 30503d7072f8Seschrock if (!vdev_is_dead(vd)) 30513d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3052069f55e2SEric Schrock aux); 30533d7072f8Seschrock 3054e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 30553d7072f8Seschrock } 30563d7072f8Seschrock 30573d7072f8Seschrock /* 3058f7170741SWill Andrews * Online the given vdev. 3059f7170741SWill Andrews * 3060f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3061f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 3062f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 3063f7170741SWill Andrews * events are generated if the device fails to open. 30643d7072f8Seschrock */ 30653d7072f8Seschrock int 3066e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 30673d7072f8Seschrock { 3068573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 30695f368aefSYuri Pankov boolean_t wasoffline; 30705f368aefSYuri Pankov vdev_state_t oldstate; 30713d7072f8Seschrock 30728f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 30733d7072f8Seschrock 3074c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3075e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 30763d7072f8Seschrock 30773d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3078e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3079fa9e4066Sahrens 30805f368aefSYuri Pankov wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 30815f368aefSYuri Pankov oldstate = vd->vdev_state; 308214372834SHans Rosenfeld 3083573ca77eSGeorge Wilson tvd = vd->vdev_top; 3084fa9e4066Sahrens vd->vdev_offline = B_FALSE; 3085441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 3086e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3087e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3088573ca77eSGeorge Wilson 3089573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3090573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3091573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3092573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3093573ca77eSGeorge Wilson } 3094573ca77eSGeorge Wilson 3095573ca77eSGeorge Wilson vdev_reopen(tvd); 30963d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 30973d7072f8Seschrock 3098573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3099573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3100573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 3101573ca77eSGeorge Wilson } 3102573ca77eSGeorge Wilson 31033d7072f8Seschrock if (newstate) 31043d7072f8Seschrock *newstate = vd->vdev_state; 31053d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 31063d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 31073d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 31083d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 31093d7072f8Seschrock vd->vdev_unspare = B_TRUE; 3110fa9e4066Sahrens 3111573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3112573ca77eSGeorge Wilson 3113573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3114573ca77eSGeorge Wilson if (vd->vdev_aux) 3115573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3116573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3117573ca77eSGeorge Wilson } 311814372834SHans Rosenfeld 3119094e47e9SGeorge Wilson /* Restart initializing if necessary */ 3120094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 3121094e47e9SGeorge Wilson if (vdev_writeable(vd) && 3122094e47e9SGeorge Wilson vd->vdev_initialize_thread == NULL && 3123094e47e9SGeorge Wilson vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3124094e47e9SGeorge Wilson (void) vdev_initialize(vd); 3125094e47e9SGeorge Wilson } 3126094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 3127094e47e9SGeorge Wilson 31285f368aefSYuri Pankov if (wasoffline || 31295f368aefSYuri Pankov (oldstate < VDEV_STATE_DEGRADED && 31305f368aefSYuri Pankov vd->vdev_state >= VDEV_STATE_DEGRADED)) 3131ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 313214372834SHans Rosenfeld 31338ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 3134fa9e4066Sahrens } 3135fa9e4066Sahrens 3136a1521560SJeff Bonwick static int 3137a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3138fa9e4066Sahrens { 3139e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 31408f18d1faSGeorge Wilson int error = 0; 31418f18d1faSGeorge Wilson uint64_t generation; 31428f18d1faSGeorge Wilson metaslab_group_t *mg; 31430a4e9518Sgw 31448f18d1faSGeorge Wilson top: 31458f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 3146fa9e4066Sahrens 3147c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3148e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3149fa9e4066Sahrens 31500e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3151e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 31520e34b6a7Sbonwick 3153e6ca193dSGeorge Wilson tvd = vd->vdev_top; 31548f18d1faSGeorge Wilson mg = tvd->vdev_mg; 31558f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 3156e6ca193dSGeorge Wilson 3157fa9e4066Sahrens /* 3158ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 3159fa9e4066Sahrens */ 3160ecc2d604Sbonwick if (!vd->vdev_offline) { 3161ecc2d604Sbonwick /* 31628ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 3163e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 3164e6ca193dSGeorge Wilson * expendable. 3165ecc2d604Sbonwick */ 3166e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3167e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 3168e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3169fa9e4066Sahrens 31708f18d1faSGeorge Wilson /* 3171b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 3172b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 3173b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 3174b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 31758f18d1faSGeorge Wilson */ 31768f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 31778f18d1faSGeorge Wilson /* 31788f18d1faSGeorge Wilson * Prevent any future allocations. 31798f18d1faSGeorge Wilson */ 3180a1521560SJeff Bonwick metaslab_group_passivate(mg); 31818f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 31828f18d1faSGeorge Wilson 31835cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 31848f18d1faSGeorge Wilson 318586714001SSerapheim Dimitropoulos /* 318686714001SSerapheim Dimitropoulos * If the log device was successfully reset but has 318786714001SSerapheim Dimitropoulos * checkpointed data, do not offline it. 318886714001SSerapheim Dimitropoulos */ 318986714001SSerapheim Dimitropoulos if (error == 0 && 319086714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm != NULL) { 319186714001SSerapheim Dimitropoulos ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, 319286714001SSerapheim Dimitropoulos !=, 0); 319386714001SSerapheim Dimitropoulos error = ZFS_ERR_CHECKPOINT_EXISTS; 319486714001SSerapheim Dimitropoulos } 319586714001SSerapheim Dimitropoulos 31968f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 31978f18d1faSGeorge Wilson 31988f18d1faSGeorge Wilson /* 31998f18d1faSGeorge Wilson * Check to see if the config has changed. 32008f18d1faSGeorge Wilson */ 32018f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 3202a1521560SJeff Bonwick metaslab_group_activate(mg); 32038f18d1faSGeorge Wilson if (error) 32048f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 32058f18d1faSGeorge Wilson vd, error)); 32068f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 32078f18d1faSGeorge Wilson goto top; 32088f18d1faSGeorge Wilson } 3209fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 32108f18d1faSGeorge Wilson } 32118f18d1faSGeorge Wilson 3212ecc2d604Sbonwick /* 3213ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 3214e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 3215e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 3216e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 3217ecc2d604Sbonwick */ 3218ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 3219e6ca193dSGeorge Wilson vdev_reopen(tvd); 3220e6ca193dSGeorge Wilson 3221e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3222e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 3223ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 3224e6ca193dSGeorge Wilson vdev_reopen(tvd); 3225e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3226ecc2d604Sbonwick } 32278f18d1faSGeorge Wilson 32288f18d1faSGeorge Wilson /* 32298f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 32308f18d1faSGeorge Wilson * once we online the device it's open for business. 32318f18d1faSGeorge Wilson */ 32328f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 3233a1521560SJeff Bonwick metaslab_group_activate(mg); 3234fa9e4066Sahrens } 3235fa9e4066Sahrens 3236e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3237ecc2d604Sbonwick 32388f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 3239fa9e4066Sahrens } 3240fa9e4066Sahrens 3241a1521560SJeff Bonwick int 3242a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3243a1521560SJeff Bonwick { 3244a1521560SJeff Bonwick int error; 3245a1521560SJeff Bonwick 3246a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 3247a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 3248a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 3249a1521560SJeff Bonwick 3250a1521560SJeff Bonwick return (error); 3251a1521560SJeff Bonwick } 3252a1521560SJeff Bonwick 3253ea8dc4b6Seschrock /* 3254ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 3255ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 3256ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3257ea8dc4b6Seschrock */ 3258ea8dc4b6Seschrock void 3259e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 3260fa9e4066Sahrens { 3261e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3262e14bb325SJeff Bonwick 3263e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3264fa9e4066Sahrens 3265ea8dc4b6Seschrock if (vd == NULL) 3266e14bb325SJeff Bonwick vd = rvd; 3267fa9e4066Sahrens 3268ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 3269ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 3270ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 3271fa9e4066Sahrens 3272e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3273e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 32743d7072f8Seschrock 32755cabbc6bSPrashanth Sreenivasa /* 32765cabbc6bSPrashanth Sreenivasa * It makes no sense to "clear" an indirect vdev. 32775cabbc6bSPrashanth Sreenivasa */ 32785cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(vd)) 32795cabbc6bSPrashanth Sreenivasa return; 32805cabbc6bSPrashanth Sreenivasa 32813d7072f8Seschrock /* 32828a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 32838a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 32848a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 32858a79c1b5Sek * written out to disk. 32863d7072f8Seschrock */ 3287e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 3288e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 32898a79c1b5Sek 3290096d22d4SEric Schrock /* 3291096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 3292096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 3293096d22d4SEric Schrock * still broken, we want to still post the ereport again. 3294096d22d4SEric Schrock */ 3295096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 3296096d22d4SEric Schrock 32974b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3298e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 3299e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 3300e14bb325SJeff Bonwick 3301f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 33023d7072f8Seschrock 3303096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 3304096d22d4SEric Schrock 3305f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 3306e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3307e14bb325SJeff Bonwick 3308e14bb325SJeff Bonwick if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3309bb8b5132Sek spa_async_request(spa, SPA_ASYNC_RESILVER); 33103d7072f8Seschrock 3311ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 33123d7072f8Seschrock } 3313096d22d4SEric Schrock 3314096d22d4SEric Schrock /* 3315096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 3316096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 3317096d22d4SEric Schrock * done in response to the FMA fault. 3318096d22d4SEric Schrock */ 3319096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3320096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3321096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 3322096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 3323fa9e4066Sahrens } 3324fa9e4066Sahrens 3325e14bb325SJeff Bonwick boolean_t 3326e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 33270a4e9518Sgw { 332888ecc943SGeorge Wilson /* 332988ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 333088ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 333188ecc943SGeorge Wilson * these types of devices in the various code paths. 333288ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 333388ecc943SGeorge Wilson * before issuing I/O to them. 333488ecc943SGeorge Wilson */ 33355cabbc6bSPrashanth Sreenivasa return (vd->vdev_state < VDEV_STATE_DEGRADED || 33365cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_hole_ops || 333788ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 33380a4e9518Sgw } 33390a4e9518Sgw 3340e14bb325SJeff Bonwick boolean_t 3341e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 33420a4e9518Sgw { 3343e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 33440a4e9518Sgw } 33450a4e9518Sgw 3346e14bb325SJeff Bonwick boolean_t 3347e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 3348fa9e4066Sahrens { 33495cabbc6bSPrashanth Sreenivasa return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 33505cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)); 3351fa9e4066Sahrens } 3352fa9e4066Sahrens 3353a31e6787SGeorge Wilson boolean_t 3354a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 3355a31e6787SGeorge Wilson { 33568ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 33578ad4d6ddSJeff Bonwick 3358a31e6787SGeorge Wilson /* 33598ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 3360a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3361a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 33628ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 33638ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 33648ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 3365a31e6787SGeorge Wilson */ 33668ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 33675cabbc6bSPrashanth Sreenivasa !vd->vdev_cant_write && vdev_is_concrete(vd) && 33680f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 3369a31e6787SGeorge Wilson } 3370a31e6787SGeorge Wilson 3371e14bb325SJeff Bonwick boolean_t 3372e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 3373fa9e4066Sahrens { 3374e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 3375fa9e4066Sahrens 3376e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3377e14bb325SJeff Bonwick return (B_FALSE); 3378fa9e4066Sahrens 3379e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 3380e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 3381fa9e4066Sahrens 3382e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3383e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 3384fa9e4066Sahrens 3385e14bb325SJeff Bonwick return (B_TRUE); 3386fa9e4066Sahrens } 3387fa9e4066Sahrens 338886714001SSerapheim Dimitropoulos boolean_t 338986714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd) 339086714001SSerapheim Dimitropoulos { 339186714001SSerapheim Dimitropoulos /* 339286714001SSerapheim Dimitropoulos * Assuming 47 bits of the space map entry dedicated for the entry's 339386714001SSerapheim Dimitropoulos * offset (see description in space_map.h), we calculate the maximum 339486714001SSerapheim Dimitropoulos * address that can be described by a space map entry for the given 339586714001SSerapheim Dimitropoulos * device. 339686714001SSerapheim Dimitropoulos */ 339786714001SSerapheim Dimitropoulos uint64_t shift = vd->vdev_ashift + 47; 339886714001SSerapheim Dimitropoulos 339986714001SSerapheim Dimitropoulos if (shift >= 63) /* detect potential overflow */ 340086714001SSerapheim Dimitropoulos return (B_TRUE); 340186714001SSerapheim Dimitropoulos 340286714001SSerapheim Dimitropoulos return (vd->vdev_asize < (1ULL << shift)); 340386714001SSerapheim Dimitropoulos } 340486714001SSerapheim Dimitropoulos 3405fa9e4066Sahrens /* 3406fa9e4066Sahrens * Get statistics for the given vdev. 3407fa9e4066Sahrens */ 3408fa9e4066Sahrens void 3409fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3410fa9e4066Sahrens { 34112e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 34122e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 3413c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 34142e4c9986SGeorge Wilson 34152e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3416fa9e4066Sahrens 3417fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3418fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3419fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3420fa9e4066Sahrens vs->vs_state = vd->vdev_state; 3421573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 3422094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3423573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3424094e47e9SGeorge Wilson /* 3425094e47e9SGeorge Wilson * Report intializing progress. Since we don't have the 3426094e47e9SGeorge Wilson * initializing locks held, this is only an estimate (although a 3427094e47e9SGeorge Wilson * fairly accurate one). 3428094e47e9SGeorge Wilson */ 3429094e47e9SGeorge Wilson vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done; 3430094e47e9SGeorge Wilson vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est; 3431094e47e9SGeorge Wilson vs->vs_initialize_state = vd->vdev_initialize_state; 3432094e47e9SGeorge Wilson vs->vs_initialize_action_time = vd->vdev_initialize_action_time; 3433094e47e9SGeorge Wilson } 3434c39a2aaeSGeorge Wilson /* 3435c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 3436c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 3437c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 3438c39a2aaeSGeorge Wilson */ 3439c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 34407855d95bSToomas Soome vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 34417855d95bSToomas Soome spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3442c39a2aaeSGeorge Wilson } 34435cabbc6bSPrashanth Sreenivasa if (vd->vdev_aux == NULL && vd == vd->vdev_top && 34445cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 34452e4c9986SGeorge Wilson vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 34462986efa8SAlex Reece } 3447fa9e4066Sahrens 3448fa9e4066Sahrens /* 3449fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 3450fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 3451fa9e4066Sahrens */ 3452fa9e4066Sahrens if (vd == rvd) { 3453e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 3454fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 3455fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 3456fa9e4066Sahrens 3457e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 3458fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 3459fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 3460fa9e4066Sahrens } 34613f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 3462fa9e4066Sahrens } 3463fa9e4066Sahrens } 34642e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 3465fa9e4066Sahrens } 3466fa9e4066Sahrens 3467fa94a07fSbrendan void 3468fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 3469fa94a07fSbrendan { 3470fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 3471fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 3472fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 3473fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 3474fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 3475fa94a07fSbrendan } 3476fa94a07fSbrendan 34773f9d6ad7SLin Ling void 34783f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 34793f9d6ad7SLin Ling { 34803f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 34813f9d6ad7SLin Ling 34823f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 34833f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 34843f9d6ad7SLin Ling 34853f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 34863f9d6ad7SLin Ling vs->vs_scan_processed = 0; 34873f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 34883f9d6ad7SLin Ling } 34893f9d6ad7SLin Ling 3490fa9e4066Sahrens void 3491e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 3492fa9e4066Sahrens { 34938ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 34948ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3495e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3496fa9e4066Sahrens vdev_t *pvd; 3497fa9e4066Sahrens uint64_t txg = zio->io_txg; 3498fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 3499fa9e4066Sahrens zio_type_t type = zio->io_type; 3500fa9e4066Sahrens int flags = zio->io_flags; 3501fa9e4066Sahrens 3502e14bb325SJeff Bonwick /* 3503e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 3504e14bb325SJeff Bonwick */ 3505e14bb325SJeff Bonwick if (zio->io_gang_tree) 3506e14bb325SJeff Bonwick return; 3507e14bb325SJeff Bonwick 3508fa9e4066Sahrens if (zio->io_error == 0) { 3509e14bb325SJeff Bonwick /* 3510e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 3511e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 3512e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 3513e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 3514e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 3515e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 3516e14bb325SJeff Bonwick * remain zero, which is what we want.) 3517e14bb325SJeff Bonwick * 3518e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 3519e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 3520e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 3521e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 3522e14bb325SJeff Bonwick */ 3523e14bb325SJeff Bonwick if (vd == rvd) 3524e14bb325SJeff Bonwick return; 3525e14bb325SJeff Bonwick 3526e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 35278ad4d6ddSJeff Bonwick 35288ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 35298ad4d6ddSJeff Bonwick return; 35308ad4d6ddSJeff Bonwick 35318ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 35328ad4d6ddSJeff Bonwick 3533e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 353444ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 35353f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 35363f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 35373f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 35383f9d6ad7SLin Ling 35393f9d6ad7SLin Ling /* XXX cleanup? */ 35403f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 35413f9d6ad7SLin Ling atomic_add_64(processed, psize); 35423f9d6ad7SLin Ling vs->vs_scan_processed += psize; 35433f9d6ad7SLin Ling } 35443f9d6ad7SLin Ling 35458ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 3546e14bb325SJeff Bonwick vs->vs_self_healed += psize; 3547fa9e4066Sahrens } 35488ad4d6ddSJeff Bonwick 35498ad4d6ddSJeff Bonwick vs->vs_ops[type]++; 35508ad4d6ddSJeff Bonwick vs->vs_bytes[type] += psize; 35518ad4d6ddSJeff Bonwick 35528ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3553fa9e4066Sahrens return; 3554fa9e4066Sahrens } 3555fa9e4066Sahrens 3556fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 3557fa9e4066Sahrens return; 3558fa9e4066Sahrens 35598956713aSEric Schrock /* 35608956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 35618956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 35628956713aSEric Schrock * hard errors, when in reality they can happen for any number of 35638956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 35648956713aSEric Schrock */ 35658956713aSEric Schrock if (zio->io_error == EIO && 35668956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 35678956713aSEric Schrock return; 35688956713aSEric Schrock 35698f18d1faSGeorge Wilson /* 35708f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 35718f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 35728f18d1faSGeorge Wilson * errors. 35738f18d1faSGeorge Wilson */ 35748f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 35758f18d1faSGeorge Wilson return; 35768f18d1faSGeorge Wilson 3577e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 3578b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3579e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 3580e14bb325SJeff Bonwick vs->vs_checksum_errors++; 3581e14bb325SJeff Bonwick else 3582e14bb325SJeff Bonwick vs->vs_read_errors++; 3583fa9e4066Sahrens } 3584b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3585e14bb325SJeff Bonwick vs->vs_write_errors++; 3586e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3587fa9e4066Sahrens 35885cabbc6bSPrashanth Sreenivasa if (spa->spa_load_state == SPA_LOAD_NONE && 35895cabbc6bSPrashanth Sreenivasa type == ZIO_TYPE_WRITE && txg != 0 && 35908ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 359144ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 3592b24ab676SJeff Bonwick spa->spa_claiming)) { 35938ad4d6ddSJeff Bonwick /* 3594b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 3595b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 3596b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 3597b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 3598b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 3599b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 3600b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 3601b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 36028ad4d6ddSJeff Bonwick * 36038ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 36048ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 36058ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 36068ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 36078ad4d6ddSJeff Bonwick */ 36088ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 36098ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 361044ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 36118ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 36128ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 36138ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3614b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 3615b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 3616b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3617b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 36188ad4d6ddSJeff Bonwick } 3619b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 36208ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3621fa9e4066Sahrens return; 36228ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 36238ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 36248ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3625fa9e4066Sahrens } 36268ad4d6ddSJeff Bonwick if (vd != rvd) 36278ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3628fa9e4066Sahrens } 3629fa9e4066Sahrens } 3630fa9e4066Sahrens 3631fa9e4066Sahrens /* 3632b24ab676SJeff Bonwick * Update the in-core space usage stats for this vdev, its metaslab class, 3633b24ab676SJeff Bonwick * and the root vdev. 3634fa9e4066Sahrens */ 3635fa9e4066Sahrens void 3636b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3637b24ab676SJeff Bonwick int64_t space_delta) 3638fa9e4066Sahrens { 363999653d4eSeschrock int64_t dspace_delta = space_delta; 36408654d025Sperrin spa_t *spa = vd->vdev_spa; 36418654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 3642b24ab676SJeff Bonwick metaslab_group_t *mg = vd->vdev_mg; 3643b24ab676SJeff Bonwick metaslab_class_t *mc = mg ? mg->mg_class : NULL; 3644fa9e4066Sahrens 36458654d025Sperrin ASSERT(vd == vd->vdev_top); 364699653d4eSeschrock 36478654d025Sperrin /* 36488654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 36498654d025Sperrin * factor. We must calculate this here and not at the root vdev 36508654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 36518654d025Sperrin * childrens', thus not accurate enough for us. 36528654d025Sperrin */ 36538654d025Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 3654e6ca193dSGeorge Wilson ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 36558654d025Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 36568654d025Sperrin vd->vdev_deflate_ratio; 36578654d025Sperrin 36588654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 36598654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 3660b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 36618654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 36628654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 36638654d025Sperrin 3664b24ab676SJeff Bonwick if (mc == spa_normal_class(spa)) { 3665fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 3666fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 3667b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 3668fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 3669fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 3670fa94a07fSbrendan } 3671b24ab676SJeff Bonwick 3672b24ab676SJeff Bonwick if (mc != NULL) { 3673b24ab676SJeff Bonwick ASSERT(rvd == vd->vdev_parent); 3674b24ab676SJeff Bonwick ASSERT(vd->vdev_ms_count != 0); 3675b24ab676SJeff Bonwick 3676b24ab676SJeff Bonwick metaslab_class_space_update(mc, 3677b24ab676SJeff Bonwick alloc_delta, defer_delta, space_delta, dspace_delta); 3678b24ab676SJeff Bonwick } 3679fa9e4066Sahrens } 3680fa9e4066Sahrens 3681fa9e4066Sahrens /* 3682fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3683fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3684fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3685fa9e4066Sahrens */ 3686fa9e4066Sahrens void 3687fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3688fa9e4066Sahrens { 3689fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3690fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3691fa9e4066Sahrens int c; 3692fa9e4066Sahrens 3693f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3694f9af39baSGeorge Wilson 3695c5904d13Seschrock /* 36966809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 36976809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3698c5904d13Seschrock */ 3699c5904d13Seschrock if (vd->vdev_aux != NULL) { 3700c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3701c5904d13Seschrock nvlist_t **aux; 3702c5904d13Seschrock uint_t naux; 3703c5904d13Seschrock 3704c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3705c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3706c5904d13Seschrock break; 3707c5904d13Seschrock } 3708c5904d13Seschrock 3709e14bb325SJeff Bonwick if (c == sav->sav_count) { 3710e14bb325SJeff Bonwick /* 3711e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3712e14bb325SJeff Bonwick */ 3713e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3714e14bb325SJeff Bonwick return; 3715e14bb325SJeff Bonwick } 3716e14bb325SJeff Bonwick 3717c5904d13Seschrock sav->sav_sync = B_TRUE; 3718c5904d13Seschrock 37196809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 37206809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 37216809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 37226809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 37236809eb4eSEric Schrock } 3724c5904d13Seschrock 3725c5904d13Seschrock ASSERT(c < naux); 3726c5904d13Seschrock 3727c5904d13Seschrock /* 3728c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 3729c5904d13Seschrock * sketchy, but it will work. 3730c5904d13Seschrock */ 3731c5904d13Seschrock nvlist_free(aux[c]); 37323f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3733c5904d13Seschrock 3734c5904d13Seschrock return; 3735c5904d13Seschrock } 3736c5904d13Seschrock 37375dabedeeSbonwick /* 3738e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 3739e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 3740e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 37415dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 37425dabedeeSbonwick */ 3743e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3744e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3745e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 37465dabedeeSbonwick 3747fa9e4066Sahrens if (vd == rvd) { 3748fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 3749fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 3750fa9e4066Sahrens } else { 3751fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 3752fa9e4066Sahrens 375388ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 37545cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3755e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 37565cabbc6bSPrashanth Sreenivasa } 3757fa9e4066Sahrens } 3758fa9e4066Sahrens } 3759fa9e4066Sahrens 3760fa9e4066Sahrens void 3761fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 3762fa9e4066Sahrens { 37635dabedeeSbonwick spa_t *spa = vd->vdev_spa; 37645dabedeeSbonwick 3765e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3766e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3767e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 37685dabedeeSbonwick 3769e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3770e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 3771e14bb325SJeff Bonwick } 3772e14bb325SJeff Bonwick 3773e14bb325SJeff Bonwick /* 3774e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 3775e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3776e14bb325SJeff Bonwick * the state changes from larger config changes because they require 3777e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 3778e14bb325SJeff Bonwick */ 3779e14bb325SJeff Bonwick void 3780e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 3781e14bb325SJeff Bonwick { 3782e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3783e14bb325SJeff Bonwick 3784f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3785e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3786e14bb325SJeff Bonwick 3787e14bb325SJeff Bonwick /* 3788e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 3789e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 3790e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 3791e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 3792e14bb325SJeff Bonwick */ 3793e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3794e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3795e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3796e14bb325SJeff Bonwick 37975cabbc6bSPrashanth Sreenivasa if (!list_link_active(&vd->vdev_state_dirty_node) && 37985cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) 3799e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 3800e14bb325SJeff Bonwick } 3801e14bb325SJeff Bonwick 3802e14bb325SJeff Bonwick void 3803e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 3804e14bb325SJeff Bonwick { 3805e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3806e14bb325SJeff Bonwick 3807e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3808e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3809e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3810e14bb325SJeff Bonwick 3811e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3812e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 3813fa9e4066Sahrens } 3814fa9e4066Sahrens 381532b87932Sek /* 381632b87932Sek * Propagate vdev state up from children to parent. 381732b87932Sek */ 381844cd46caSbillm void 381944cd46caSbillm vdev_propagate_state(vdev_t *vd) 382044cd46caSbillm { 38218ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 38228ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 382344cd46caSbillm int degraded = 0, faulted = 0; 382444cd46caSbillm int corrupted = 0; 382544cd46caSbillm vdev_t *child; 382644cd46caSbillm 38273d7072f8Seschrock if (vd->vdev_children > 0) { 3828573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 38293d7072f8Seschrock child = vd->vdev_child[c]; 383051ece835Seschrock 383188ecc943SGeorge Wilson /* 38325cabbc6bSPrashanth Sreenivasa * Don't factor holes or indirect vdevs into the 38335cabbc6bSPrashanth Sreenivasa * decision. 383488ecc943SGeorge Wilson */ 38355cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(child)) 383688ecc943SGeorge Wilson continue; 383788ecc943SGeorge Wilson 3838e14bb325SJeff Bonwick if (!vdev_readable(child) || 38398ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 384051ece835Seschrock /* 384151ece835Seschrock * Root special: if there is a top-level log 384251ece835Seschrock * device, treat the root vdev as if it were 384351ece835Seschrock * degraded. 384451ece835Seschrock */ 384551ece835Seschrock if (child->vdev_islog && vd == rvd) 384651ece835Seschrock degraded++; 384751ece835Seschrock else 384851ece835Seschrock faulted++; 384951ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 38503d7072f8Seschrock degraded++; 385151ece835Seschrock } 385244cd46caSbillm 38533d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 38543d7072f8Seschrock corrupted++; 38553d7072f8Seschrock } 385644cd46caSbillm 38573d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 38583d7072f8Seschrock 38593d7072f8Seschrock /* 3860e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 38613d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 38623d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 38633d7072f8Seschrock * replicas'. 38643d7072f8Seschrock */ 38653d7072f8Seschrock if (corrupted && vd == rvd && 38663d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 38673d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 38683d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 38693d7072f8Seschrock } 38703d7072f8Seschrock 387151ece835Seschrock if (vd->vdev_parent) 38723d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 387344cd46caSbillm } 387444cd46caSbillm 3875fa9e4066Sahrens /* 3876ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 3877ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 3878ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 3879ea8dc4b6Seschrock * 3880ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 3881ea8dc4b6Seschrock * generated. 3882fa9e4066Sahrens */ 3883fa9e4066Sahrens void 3884ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3885fa9e4066Sahrens { 3886560e6e96Seschrock uint64_t save_state; 3887c5904d13Seschrock spa_t *spa = vd->vdev_spa; 3888ea8dc4b6Seschrock 3889ea8dc4b6Seschrock if (state == vd->vdev_state) { 3890ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 3891fa9e4066Sahrens return; 3892ea8dc4b6Seschrock } 3893ea8dc4b6Seschrock 3894560e6e96Seschrock save_state = vd->vdev_state; 3895fa9e4066Sahrens 3896fa9e4066Sahrens vd->vdev_state = state; 3897fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 3898fa9e4066Sahrens 38993d7072f8Seschrock /* 39003d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 390198d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 390298d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 390398d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 390498d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 390598d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 390698d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 390798d1cbfeSGeorge Wilson * children. 390898d1cbfeSGeorge Wilson */ 390998d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 391098d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 39113d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 39123d7072f8Seschrock 3913069f55e2SEric Schrock /* 3914069f55e2SEric Schrock * If we have brought this vdev back into service, we need 3915069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 3916069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 3917069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 3918069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 3919069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 3920069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 3921069f55e2SEric Schrock */ 3922069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3923069f55e2SEric Schrock vd->vdev_prevstate != state) 3924069f55e2SEric Schrock zfs_post_state_change(spa, vd); 3925069f55e2SEric Schrock 39263d7072f8Seschrock if (vd->vdev_removed && 39273d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 39283d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 39293d7072f8Seschrock /* 39303d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 39313d7072f8Seschrock * device was previously marked removed and someone attempted to 39323d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 39333d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 39343d7072f8Seschrock * it is one of our special test online cases, which is only 39353d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 39363d7072f8Seschrock * fault. 39373d7072f8Seschrock */ 39383d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 39393d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 39403d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 39413d7072f8Seschrock vd->vdev_removed = B_TRUE; 39423d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 3943ea8dc4b6Seschrock /* 3944cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 3945cb04b873SMark J Musante * mark it as "not available", which signifies that it was 3946cb04b873SMark J Musante * never there to begin with. Failure to open such a device 3947cb04b873SMark J Musante * is not considered an error. 3948ea8dc4b6Seschrock */ 3949cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3950cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 3951560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 3952560e6e96Seschrock vd->vdev_not_present = 1; 3953560e6e96Seschrock 3954560e6e96Seschrock /* 3955560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 3956560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 3957560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 3958560e6e96Seschrock * want to post the ereport if the device was already in the 3959560e6e96Seschrock * CANT_OPEN state beforehand. 39603d7072f8Seschrock * 39613d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 39623d7072f8Seschrock * online the device in response to an insertion event. If we 39633d7072f8Seschrock * hit this case, then we have detected an insertion event for a 39643d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 39653d7072f8Seschrock * In this scenario, we don't post an ereport because we are 39663d7072f8Seschrock * about to replace the device, or attempt an online with 39673d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 3968560e6e96Seschrock */ 39693d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 39703d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 3971c5904d13Seschrock vd != spa->spa_root_vdev) { 3972ea8dc4b6Seschrock const char *class; 3973ea8dc4b6Seschrock 3974ea8dc4b6Seschrock switch (aux) { 3975ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 3976ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3977ea8dc4b6Seschrock break; 3978ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 3979ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3980ea8dc4b6Seschrock break; 3981ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 3982ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3983ea8dc4b6Seschrock break; 3984ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 3985ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3986ea8dc4b6Seschrock break; 3987ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 3988ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3989ea8dc4b6Seschrock break; 3990ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 3991ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3992ea8dc4b6Seschrock break; 3993ea8dc4b6Seschrock default: 3994ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3995ea8dc4b6Seschrock } 3996ea8dc4b6Seschrock 3997c5904d13Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3998ea8dc4b6Seschrock } 3999ea8dc4b6Seschrock 40003d7072f8Seschrock /* Erase any notion of persistent removed state */ 40013d7072f8Seschrock vd->vdev_removed = B_FALSE; 40023d7072f8Seschrock } else { 40033d7072f8Seschrock vd->vdev_removed = B_FALSE; 40043d7072f8Seschrock } 4005ea8dc4b6Seschrock 40068b33d774STim Haley if (!isopen && vd->vdev_parent) 40078b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 4008fa9e4066Sahrens } 400915e6edf1Sgw 40106f793812SPavel Zakharov boolean_t 40116f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd) 40126f793812SPavel Zakharov { 40136f793812SPavel Zakharov ASSERT(!vd->vdev_ops->vdev_op_leaf); 40146f793812SPavel Zakharov 40156f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) { 40166f793812SPavel Zakharov if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 40176f793812SPavel Zakharov return (B_FALSE); 40186f793812SPavel Zakharov } 40196f793812SPavel Zakharov 40206f793812SPavel Zakharov return (B_TRUE); 40216f793812SPavel Zakharov } 40226f793812SPavel Zakharov 402315e6edf1Sgw /* 402415e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 4025c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 4026c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 402715e6edf1Sgw */ 402815e6edf1Sgw boolean_t 402915e6edf1Sgw vdev_is_bootable(vdev_t *vd) 403015e6edf1Sgw { 403115e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 403215e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 403315e6edf1Sgw 403415e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 403515e6edf1Sgw vd->vdev_children > 1) { 403615e6edf1Sgw return (B_FALSE); 40375cabbc6bSPrashanth Sreenivasa } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 40385cabbc6bSPrashanth Sreenivasa strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 403915e6edf1Sgw return (B_FALSE); 404015e6edf1Sgw } 404115e6edf1Sgw } 404215e6edf1Sgw 4043573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 404415e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 404515e6edf1Sgw return (B_FALSE); 404615e6edf1Sgw } 404715e6edf1Sgw return (B_TRUE); 404815e6edf1Sgw } 4049e6ca193dSGeorge Wilson 40505cabbc6bSPrashanth Sreenivasa boolean_t 40515cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd) 40525cabbc6bSPrashanth Sreenivasa { 40535cabbc6bSPrashanth Sreenivasa vdev_ops_t *ops = vd->vdev_ops; 40545cabbc6bSPrashanth Sreenivasa if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 40555cabbc6bSPrashanth Sreenivasa ops == &vdev_missing_ops || ops == &vdev_root_ops) { 40565cabbc6bSPrashanth Sreenivasa return (B_FALSE); 40575cabbc6bSPrashanth Sreenivasa } else { 40585cabbc6bSPrashanth Sreenivasa return (B_TRUE); 40595cabbc6bSPrashanth Sreenivasa } 40605cabbc6bSPrashanth Sreenivasa } 40615cabbc6bSPrashanth Sreenivasa 40624b964adaSGeorge Wilson /* 40634b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 40644b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 40654b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 40664b964adaSGeorge Wilson */ 40674b964adaSGeorge Wilson boolean_t 40684b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 40694b964adaSGeorge Wilson { 40704b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 40714b964adaSGeorge Wilson !vd->vdev_removed) 40724b964adaSGeorge Wilson return (B_TRUE); 40734b964adaSGeorge Wilson 40744b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 40754b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 40764b964adaSGeorge Wilson return (B_TRUE); 40774b964adaSGeorge Wilson 40784b964adaSGeorge Wilson return (B_FALSE); 40794b964adaSGeorge Wilson } 40804b964adaSGeorge Wilson 4081573ca77eSGeorge Wilson /* 4082573ca77eSGeorge Wilson * Expand a vdev if possible. 4083573ca77eSGeorge Wilson */ 4084573ca77eSGeorge Wilson void 4085573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 4086573ca77eSGeorge Wilson { 4087573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 4088573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4089573ca77eSGeorge Wilson 40905cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 40915cabbc6bSPrashanth Sreenivasa 40925cabbc6bSPrashanth Sreenivasa if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 40935cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 4094573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 4095573ca77eSGeorge Wilson vdev_config_dirty(vd); 4096573ca77eSGeorge Wilson } 4097573ca77eSGeorge Wilson } 40981195e687SMark J Musante 40991195e687SMark J Musante /* 41001195e687SMark J Musante * Split a vdev. 41011195e687SMark J Musante */ 41021195e687SMark J Musante void 41031195e687SMark J Musante vdev_split(vdev_t *vd) 41041195e687SMark J Musante { 41051195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 41061195e687SMark J Musante 41071195e687SMark J Musante vdev_remove_child(pvd, vd); 41081195e687SMark J Musante vdev_compact_children(pvd); 41091195e687SMark J Musante 41101195e687SMark J Musante cvd = pvd->vdev_child[0]; 41111195e687SMark J Musante if (pvd->vdev_children == 1) { 41121195e687SMark J Musante vdev_remove_parent(cvd); 41131195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 41141195e687SMark J Musante } 41151195e687SMark J Musante vdev_propagate_state(cvd); 41161195e687SMark J Musante } 4117283b8460SGeorge.Wilson 4118283b8460SGeorge.Wilson void 4119283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 4120283b8460SGeorge.Wilson { 4121283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 4122283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 4123283b8460SGeorge.Wilson 4124283b8460SGeorge.Wilson vdev_deadman(cvd); 4125283b8460SGeorge.Wilson } 4126283b8460SGeorge.Wilson 4127283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 4128283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 4129283b8460SGeorge.Wilson 4130283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 413169962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 4132283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 4133283b8460SGeorge.Wilson zio_t *fio; 4134283b8460SGeorge.Wilson uint64_t delta; 4135283b8460SGeorge.Wilson 4136283b8460SGeorge.Wilson /* 4137283b8460SGeorge.Wilson * Look at the head of all the pending queues, 4138283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 4139283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 4140283b8460SGeorge.Wilson */ 414169962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 4142c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 4143c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 41443ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 41453ee8c80cSPavel Zakharov "%lluns, delta %lluns, last io %lluns", 41463ee8c80cSPavel Zakharov fio->io_timestamp, (u_longlong_t)delta, 4147283b8460SGeorge.Wilson vq->vq_io_complete_ts); 4148283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 4149283b8460SGeorge.Wilson "hung.", spa_name(spa)); 4150283b8460SGeorge.Wilson } 4151283b8460SGeorge.Wilson } 4152283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 4153283b8460SGeorge.Wilson } 4154283b8460SGeorge.Wilson } 4155