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*a0b03b16SSerapheim Dimitropoulos /* default target for number of metaslabs per top-level vdev */ 76*a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_count = 200; 7786714001SSerapheim Dimitropoulos 78b4bf0cf0SDon Brady /* minimum number of metaslabs per top-level vdev */ 79*a0b03b16SSerapheim Dimitropoulos int zfs_vdev_min_ms_count = 16; 8086714001SSerapheim Dimitropoulos 81b4bf0cf0SDon Brady /* practical upper limit of total metaslabs per top-level vdev */ 82*a0b03b16SSerapheim Dimitropoulos int zfs_vdev_ms_count_limit = 1ULL << 17; 83b4bf0cf0SDon Brady 84b4bf0cf0SDon Brady /* lower limit for metaslab size (512M) */ 85*a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_shift = 29; 8686714001SSerapheim Dimitropoulos 87*a0b03b16SSerapheim Dimitropoulos /* upper limit for metaslab size (16G) */ 88*a0b03b16SSerapheim Dimitropoulos int zfs_vdev_max_ms_shift = 34; 89b4bf0cf0SDon 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, 171c7a7b2faSAndriy Gapon "", (int)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*a0b03b16SSerapheim Dimitropoulos uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2038b4bf0cf0SDon Brady uint64_t ms_shift; 203986714001SSerapheim Dimitropoulos 2040fa9e4066Sahrens /* 2041b4bf0cf0SDon Brady * There are two dimensions to the metaslab sizing calculation: 2042b4bf0cf0SDon Brady * the size of the metaslab and the count of metaslabs per vdev. 2043b4bf0cf0SDon Brady * 2044*a0b03b16SSerapheim Dimitropoulos * The default values used below are a good balance between memory 2045*a0b03b16SSerapheim Dimitropoulos * usage (larger metaslab size means more memory needed for loaded 2046*a0b03b16SSerapheim Dimitropoulos * metaslabs; more metaslabs means more memory needed for the 2047*a0b03b16SSerapheim Dimitropoulos * metaslab_t structs), metaslab load time (larger metaslabs take 2048*a0b03b16SSerapheim Dimitropoulos * longer to load), and metaslab sync time (more metaslabs means 2049*a0b03b16SSerapheim Dimitropoulos * more time spent syncing all of them). 2050*a0b03b16SSerapheim Dimitropoulos * 2051*a0b03b16SSerapheim Dimitropoulos * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2052*a0b03b16SSerapheim Dimitropoulos * The range of the dimensions are as follows: 2053*a0b03b16SSerapheim Dimitropoulos * 2054*a0b03b16SSerapheim Dimitropoulos * 2^29 <= ms_size <= 2^34 2055b4bf0cf0SDon Brady * 16 <= ms_count <= 131,072 2056b4bf0cf0SDon Brady * 2057b4bf0cf0SDon Brady * On the lower end of vdev sizes, we aim for metaslabs sizes of 2058b4bf0cf0SDon Brady * at least 512MB (2^29) to minimize fragmentation effects when 2059b4bf0cf0SDon Brady * testing with smaller devices. However, the count constraint 2060b4bf0cf0SDon Brady * of at least 16 metaslabs will override this minimum size goal. 2061b4bf0cf0SDon Brady * 2062b4bf0cf0SDon Brady * On the upper end of vdev sizes, we aim for a maximum metaslab 2063*a0b03b16SSerapheim Dimitropoulos * size of 16GB. However, we will cap the total count to 2^17 2064*a0b03b16SSerapheim Dimitropoulos * metaslabs to keep our memory footprint in check and let the 2065*a0b03b16SSerapheim Dimitropoulos * metaslab size grow from there if that limit is hit. 2066b4bf0cf0SDon Brady * 2067b4bf0cf0SDon Brady * The net effect of applying above constrains is summarized below. 2068b4bf0cf0SDon Brady * 2069*a0b03b16SSerapheim Dimitropoulos * vdev size metaslab count 2070*a0b03b16SSerapheim Dimitropoulos * --------------|----------------- 2071*a0b03b16SSerapheim Dimitropoulos * < 8GB ~16 2072*a0b03b16SSerapheim Dimitropoulos * 8GB - 100GB one per 512MB 2073*a0b03b16SSerapheim Dimitropoulos * 100GB - 3TB ~200 2074*a0b03b16SSerapheim Dimitropoulos * 3TB - 2PB one per 16GB 2075*a0b03b16SSerapheim Dimitropoulos * > 2PB ~131,072 2076*a0b03b16SSerapheim Dimitropoulos * -------------------------------- 2077*a0b03b16SSerapheim Dimitropoulos * 2078*a0b03b16SSerapheim Dimitropoulos * Finally, note that all of the above calculate the initial 2079*a0b03b16SSerapheim Dimitropoulos * number of metaslabs. Expanding a top-level vdev will result 2080*a0b03b16SSerapheim Dimitropoulos * in additional metaslabs being allocated making it possible 2081*a0b03b16SSerapheim Dimitropoulos * to exceed the zfs_vdev_ms_count_limit. 2082*a0b03b16SSerapheim Dimitropoulos */ 2083*a0b03b16SSerapheim Dimitropoulos 2084*a0b03b16SSerapheim Dimitropoulos if (ms_count < zfs_vdev_min_ms_count) 2085*a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2086*a0b03b16SSerapheim Dimitropoulos else if (ms_count > zfs_vdev_default_ms_count) 2087*a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2088b4bf0cf0SDon Brady else 2089*a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_default_ms_shift; 2090b4bf0cf0SDon Brady 2091b4bf0cf0SDon Brady if (ms_shift < SPA_MAXBLOCKSHIFT) { 2092b4bf0cf0SDon Brady ms_shift = SPA_MAXBLOCKSHIFT; 2093*a0b03b16SSerapheim Dimitropoulos } else if (ms_shift > zfs_vdev_max_ms_shift) { 2094*a0b03b16SSerapheim Dimitropoulos ms_shift = zfs_vdev_max_ms_shift; 2095b4bf0cf0SDon Brady /* cap the total count to constrain memory footprint */ 2096*a0b03b16SSerapheim Dimitropoulos if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2097*a0b03b16SSerapheim Dimitropoulos ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 209886714001SSerapheim Dimitropoulos } 209986714001SSerapheim Dimitropoulos 210086714001SSerapheim Dimitropoulos vd->vdev_ms_shift = ms_shift; 210186714001SSerapheim Dimitropoulos ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2102fa9e4066Sahrens } 2103fa9e4066Sahrens 2104fa9e4066Sahrens void 2105ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2106fa9e4066Sahrens { 2107ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 21085cabbc6bSPrashanth Sreenivasa /* indirect vdevs don't have metaslabs or dtls */ 21095cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd) || flags == 0); 2110ecc2d604Sbonwick ASSERT(ISP2(flags)); 2111f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 2112fa9e4066Sahrens 2113ecc2d604Sbonwick if (flags & VDD_METASLAB) 2114ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2115ecc2d604Sbonwick 2116ecc2d604Sbonwick if (flags & VDD_DTL) 2117ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2118ecc2d604Sbonwick 2119ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2120fa9e4066Sahrens } 2121fa9e4066Sahrens 21220713e232SGeorge Wilson void 21230713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 21240713e232SGeorge Wilson { 21250713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 21260713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 21270713e232SGeorge Wilson 21280713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 21290713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 21300713e232SGeorge Wilson } 21310713e232SGeorge Wilson 21328ad4d6ddSJeff Bonwick /* 21338ad4d6ddSJeff Bonwick * DTLs. 21348ad4d6ddSJeff Bonwick * 21358ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 21369fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 21378ad4d6ddSJeff Bonwick * 21388ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 21398ad4d6ddSJeff Bonwick * 21408ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 21418ad4d6ddSJeff Bonwick * 21428ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 21438ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 21448ad4d6ddSJeff Bonwick * txgs that was scrubbed. 21458ad4d6ddSJeff Bonwick * 21468ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 21478ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 21488ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 21498ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 21508ad4d6ddSJeff Bonwick * determine whether a device can be detached. 21518ad4d6ddSJeff Bonwick * 21528ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 21538ad4d6ddSJeff Bonwick * either has the data or it doesn't. 21548ad4d6ddSJeff Bonwick * 21558ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 21568ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 21578ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 21588ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 21598ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 21608ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 21618ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 21628ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 21638ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 21648ad4d6ddSJeff Bonwick * 21658ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 21668ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 21678ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 21688ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 21698ad4d6ddSJeff Bonwick */ 2170fa9e4066Sahrens void 21718ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2172fa9e4066Sahrens { 21730713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21748ad4d6ddSJeff Bonwick 21758ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 21768ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2177f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 21788ad4d6ddSJeff Bonwick 21795cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 21800713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 21810713e232SGeorge Wilson range_tree_add(rt, txg, size); 21825cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2183fa9e4066Sahrens } 2184fa9e4066Sahrens 21858ad4d6ddSJeff Bonwick boolean_t 21868ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2187fa9e4066Sahrens { 21880713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21898ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 2190fa9e4066Sahrens 21918ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 21928ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2193fa9e4066Sahrens 21945cabbc6bSPrashanth Sreenivasa /* 21955cabbc6bSPrashanth Sreenivasa * While we are loading the pool, the DTLs have not been loaded yet. 21965cabbc6bSPrashanth Sreenivasa * Ignore the DTLs and try all devices. This avoids a recursive 21975cabbc6bSPrashanth Sreenivasa * mutex enter on the vdev_dtl_lock, and also makes us try hard 21985cabbc6bSPrashanth Sreenivasa * when loading the pool (relying on the checksum to ensure that 21995cabbc6bSPrashanth Sreenivasa * we get the right data -- note that we while loading, we are 22005cabbc6bSPrashanth Sreenivasa * only reading the MOS, which is always checksummed). 22015cabbc6bSPrashanth Sreenivasa */ 22025cabbc6bSPrashanth Sreenivasa if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 22035cabbc6bSPrashanth Sreenivasa return (B_FALSE); 22045cabbc6bSPrashanth Sreenivasa 22055cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 220686714001SSerapheim Dimitropoulos if (!range_tree_is_empty(rt)) 22070713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 22085cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2209fa9e4066Sahrens 2210fa9e4066Sahrens return (dirty); 2211fa9e4066Sahrens } 2212fa9e4066Sahrens 22138ad4d6ddSJeff Bonwick boolean_t 22148ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 22158ad4d6ddSJeff Bonwick { 22160713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 22178ad4d6ddSJeff Bonwick boolean_t empty; 22188ad4d6ddSJeff Bonwick 22195cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 222086714001SSerapheim Dimitropoulos empty = range_tree_is_empty(rt); 22215cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 22228ad4d6ddSJeff Bonwick 22238ad4d6ddSJeff Bonwick return (empty); 22248ad4d6ddSJeff Bonwick } 22258ad4d6ddSJeff Bonwick 2226b4952e17SGeorge Wilson /* 2227b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 2228b4952e17SGeorge Wilson */ 2229b4952e17SGeorge Wilson static uint64_t 2230b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 2231b4952e17SGeorge Wilson { 22320713e232SGeorge Wilson range_seg_t *rs; 2233b4952e17SGeorge Wilson 2234b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 22350713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2236b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2237b4952e17SGeorge Wilson 22380713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 22390713e232SGeorge Wilson return (rs->rs_start - 1); 2240b4952e17SGeorge Wilson } 2241b4952e17SGeorge Wilson 2242b4952e17SGeorge Wilson /* 2243b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 2244b4952e17SGeorge Wilson */ 2245b4952e17SGeorge Wilson static uint64_t 2246b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 2247b4952e17SGeorge Wilson { 22480713e232SGeorge Wilson range_seg_t *rs; 2249b4952e17SGeorge Wilson 2250b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 22510713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2252b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2253b4952e17SGeorge Wilson 22540713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 22550713e232SGeorge Wilson return (rs->rs_end); 2256b4952e17SGeorge Wilson } 2257b4952e17SGeorge Wilson 2258b4952e17SGeorge Wilson /* 2259b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 2260b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 2261b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 2262b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 2263b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 2264b4952e17SGeorge Wilson * excise the DTLs. 2265b4952e17SGeorge Wilson */ 2266b4952e17SGeorge Wilson static boolean_t 2267b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 2268b4952e17SGeorge Wilson { 2269b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 2270b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2271b4952e17SGeorge Wilson 2272b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 2273b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2274b4952e17SGeorge Wilson 22752d2f193aSMatthew Ahrens if (vd->vdev_state < VDEV_STATE_DEGRADED) 22762d2f193aSMatthew Ahrens return (B_FALSE); 22772d2f193aSMatthew Ahrens 2278b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 227986714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2280b4952e17SGeorge Wilson return (B_TRUE); 2281b4952e17SGeorge Wilson 2282b4952e17SGeorge Wilson /* 2283b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 2284b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 2285b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 2286b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 2287b4952e17SGeorge Wilson * for excision. 2288b4952e17SGeorge Wilson */ 2289b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2290b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2291b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2292b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2293b4952e17SGeorge Wilson return (B_TRUE); 2294b4952e17SGeorge Wilson } 2295b4952e17SGeorge Wilson return (B_FALSE); 2296b4952e17SGeorge Wilson } 2297b4952e17SGeorge Wilson 2298fa9e4066Sahrens /* 2299fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 2300fa9e4066Sahrens */ 2301fa9e4066Sahrens void 2302fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2303fa9e4066Sahrens { 2304ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 23058ad4d6ddSJeff Bonwick avl_tree_t reftree; 23068ad4d6ddSJeff Bonwick int minref; 2307fa9e4066Sahrens 23088ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2309fa9e4066Sahrens 23108ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 23118ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 23128ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 23138ad4d6ddSJeff Bonwick 23145cabbc6bSPrashanth Sreenivasa if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 23158ad4d6ddSJeff Bonwick return; 23168ad4d6ddSJeff Bonwick 23178ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 23183f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 23193f9d6ad7SLin Ling 2320fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 2321b4952e17SGeorge Wilson 2322b4952e17SGeorge Wilson /* 2323b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 2324b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 2325b4952e17SGeorge Wilson * excise regions on vdevs that were available during 2326b4952e17SGeorge Wilson * the entire duration of this scan. 2327b4952e17SGeorge Wilson */ 2328088f3894Sahrens if (scrub_txg != 0 && 23293f9d6ad7SLin Ling (spa->spa_scrub_started || 2330b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2331b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 2332088f3894Sahrens /* 2333088f3894Sahrens * We completed a scrub up to scrub_txg. If we 2334088f3894Sahrens * did it without rebooting, then the scrub dtl 2335088f3894Sahrens * will be valid, so excise the old region and 2336088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 2337088f3894Sahrens * dtl as-is if there was an error. 23388ad4d6ddSJeff Bonwick * 23398ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 23408ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 23418ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 23428ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 23438ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 23448ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 23458ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 23468ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 23478ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 2348088f3894Sahrens */ 23490713e232SGeorge Wilson space_reftree_create(&reftree); 23500713e232SGeorge Wilson space_reftree_add_map(&reftree, 23510713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 23520713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 23530713e232SGeorge Wilson space_reftree_add_map(&reftree, 23540713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 23550713e232SGeorge Wilson space_reftree_generate_map(&reftree, 23560713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 23570713e232SGeorge Wilson space_reftree_destroy(&reftree); 2358fa9e4066Sahrens } 23590713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 23600713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 23610713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2362fa9e4066Sahrens if (scrub_done) 23630713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 23640713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 23658ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 23660713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 23678ad4d6ddSJeff Bonwick else 23680713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 23690713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2370b4952e17SGeorge Wilson 2371b4952e17SGeorge Wilson /* 2372b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 2373b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 2374b4952e17SGeorge Wilson */ 2375b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 237686714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 237786714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2378b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 2379b4952e17SGeorge Wilson 2380fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2381088f3894Sahrens 2382ecc2d604Sbonwick if (txg != 0) 2383ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2384fa9e4066Sahrens return; 2385fa9e4066Sahrens } 2386fa9e4066Sahrens 2387fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 23888ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 238999bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 239099bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 23918ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 23928ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 23938ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 23948ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 23958ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 23968ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 23978ad4d6ddSJeff Bonwick else 23988ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 23990713e232SGeorge Wilson space_reftree_create(&reftree); 24008ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 24018ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 24028ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 24030713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 24048ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 24058ad4d6ddSJeff Bonwick } 24060713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 24070713e232SGeorge Wilson space_reftree_destroy(&reftree); 2408fa9e4066Sahrens } 24098ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 2410fa9e4066Sahrens } 2411fa9e4066Sahrens 24120713e232SGeorge Wilson int 2413fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 2414fa9e4066Sahrens { 2415fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2416ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 24170713e232SGeorge Wilson int error = 0; 2418fa9e4066Sahrens 24190713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 24205cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 2421fa9e4066Sahrens 24220713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 24235cabbc6bSPrashanth Sreenivasa vd->vdev_dtl_object, 0, -1ULL, 0); 24240713e232SGeorge Wilson if (error) 24250713e232SGeorge Wilson return (error); 24260713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2427fa9e4066Sahrens 24280713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 242988ecc943SGeorge Wilson 24300713e232SGeorge Wilson /* 24310713e232SGeorge Wilson * Now that we've opened the space_map we need to update 24320713e232SGeorge Wilson * the in-core DTL. 24330713e232SGeorge Wilson */ 24340713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 2435ecc2d604Sbonwick 24360713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 24370713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 24380713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2439fa9e4066Sahrens 24400713e232SGeorge Wilson return (error); 24410713e232SGeorge Wilson } 24420713e232SGeorge Wilson 24430713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 24440713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 24450713e232SGeorge Wilson if (error != 0) 24460713e232SGeorge Wilson break; 24470713e232SGeorge Wilson } 2448fa9e4066Sahrens 2449fa9e4066Sahrens return (error); 2450fa9e4066Sahrens } 2451fa9e4066Sahrens 2452215198a6SJoe Stein void 2453215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2454215198a6SJoe Stein { 2455215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2456215198a6SJoe Stein 2457215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2458215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2459215198a6SJoe Stein zapobj, tx)); 2460215198a6SJoe Stein } 2461215198a6SJoe Stein 2462215198a6SJoe Stein uint64_t 2463215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2464215198a6SJoe Stein { 2465215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2466215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2467215198a6SJoe Stein DMU_OT_NONE, 0, tx); 2468215198a6SJoe Stein 2469215198a6SJoe Stein ASSERT(zap != 0); 2470215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2471215198a6SJoe Stein zap, tx)); 2472215198a6SJoe Stein 2473215198a6SJoe Stein return (zap); 2474215198a6SJoe Stein } 2475215198a6SJoe Stein 2476215198a6SJoe Stein void 2477215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2478215198a6SJoe Stein { 2479215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 2480215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 2481215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 2482215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 2483215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2484215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2485215198a6SJoe Stein } 2486215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2487215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2488215198a6SJoe Stein } 2489215198a6SJoe Stein } 2490215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2491215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2492215198a6SJoe Stein } 2493215198a6SJoe Stein } 2494215198a6SJoe Stein 2495fa9e4066Sahrens void 2496fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2497fa9e4066Sahrens { 2498fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 24990713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2500ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 25010713e232SGeorge Wilson range_tree_t *rtsync; 2502fa9e4066Sahrens dmu_tx_t *tx; 25030713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2504fa9e4066Sahrens 25055cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 25060713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 250788ecc943SGeorge Wilson 2508fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2509fa9e4066Sahrens 25100713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 25110713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 25120713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 25130713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 25140713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 25150713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2516215198a6SJoe Stein 2517215198a6SJoe Stein /* 2518215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2519215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2520215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2521215198a6SJoe Stein */ 2522215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2523215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2524215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2525215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2526215198a6SJoe Stein } 2527215198a6SJoe Stein 2528fa9e4066Sahrens dmu_tx_commit(tx); 2529fa9e4066Sahrens return; 2530fa9e4066Sahrens } 2531fa9e4066Sahrens 25320713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 25330713e232SGeorge Wilson uint64_t new_object; 25340713e232SGeorge Wilson 253586714001SSerapheim Dimitropoulos new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 25360713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 25370713e232SGeorge Wilson 25380713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 25395cabbc6bSPrashanth Sreenivasa 0, -1ULL, 0)); 25400713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2541fa9e4066Sahrens } 2542fa9e4066Sahrens 25435cabbc6bSPrashanth Sreenivasa rtsync = range_tree_create(NULL, NULL); 2544fa9e4066Sahrens 2545fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 25460713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2547fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2548fa9e4066Sahrens 254986714001SSerapheim Dimitropoulos space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 255017f11284SSerapheim Dimitropoulos space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 25510713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2552fa9e4066Sahrens 25530713e232SGeorge Wilson range_tree_destroy(rtsync); 2554fa9e4066Sahrens 25550713e232SGeorge Wilson /* 25560713e232SGeorge Wilson * If the object for the space map has changed then dirty 25570713e232SGeorge Wilson * the top level so that we update the config. 25580713e232SGeorge Wilson */ 25590713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 25603ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 25613ee8c80cSPavel Zakharov "new object %llu", (u_longlong_t)txg, spa_name(spa), 25623ee8c80cSPavel Zakharov (u_longlong_t)object, 25633ee8c80cSPavel Zakharov (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 25640713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 25650713e232SGeorge Wilson } 2566fa9e4066Sahrens 2567fa9e4066Sahrens dmu_tx_commit(tx); 25680713e232SGeorge Wilson 25690713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 25700713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 25710713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2572fa9e4066Sahrens } 2573fa9e4066Sahrens 25748ad4d6ddSJeff Bonwick /* 25758ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 25768ad4d6ddSJeff Bonwick * without losing data. 25778ad4d6ddSJeff Bonwick */ 25788ad4d6ddSJeff Bonwick boolean_t 25798ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 25808ad4d6ddSJeff Bonwick { 25818ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 25828ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 25838ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 25848ad4d6ddSJeff Bonwick boolean_t required; 25858ad4d6ddSJeff Bonwick 25868ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 25878ad4d6ddSJeff Bonwick 25888ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 25898ad4d6ddSJeff Bonwick return (B_TRUE); 25908ad4d6ddSJeff Bonwick 25918ad4d6ddSJeff Bonwick /* 25928ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 25938ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 25948ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 25958ad4d6ddSJeff Bonwick */ 25968ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 25978ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 25988ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 25998ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 26008ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 26018ad4d6ddSJeff Bonwick 2602cb04b873SMark J Musante if (!required && zio_injection_enabled) 2603cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2604cb04b873SMark J Musante 26058ad4d6ddSJeff Bonwick return (required); 26068ad4d6ddSJeff Bonwick } 26078ad4d6ddSJeff Bonwick 2608088f3894Sahrens /* 2609088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2610088f3894Sahrens */ 2611088f3894Sahrens boolean_t 2612088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2613088f3894Sahrens { 2614088f3894Sahrens boolean_t needed = B_FALSE; 2615088f3894Sahrens uint64_t thismin = UINT64_MAX; 2616088f3894Sahrens uint64_t thismax = 0; 2617088f3894Sahrens 2618088f3894Sahrens if (vd->vdev_children == 0) { 2619088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 262086714001SSerapheim Dimitropoulos if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 26218ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2622088f3894Sahrens 2623b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2624b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2625088f3894Sahrens needed = B_TRUE; 2626088f3894Sahrens } 2627088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2628088f3894Sahrens } else { 26298ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2630088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2631088f3894Sahrens uint64_t cmin, cmax; 2632088f3894Sahrens 2633088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2634088f3894Sahrens thismin = MIN(thismin, cmin); 2635088f3894Sahrens thismax = MAX(thismax, cmax); 2636088f3894Sahrens needed = B_TRUE; 2637088f3894Sahrens } 2638088f3894Sahrens } 2639088f3894Sahrens } 2640088f3894Sahrens 2641088f3894Sahrens if (needed && minp) { 2642088f3894Sahrens *minp = thismin; 2643088f3894Sahrens *maxp = thismax; 2644088f3894Sahrens } 2645088f3894Sahrens return (needed); 2646088f3894Sahrens } 2647088f3894Sahrens 264886714001SSerapheim Dimitropoulos /* 264986714001SSerapheim Dimitropoulos * Gets the checkpoint space map object from the vdev's ZAP. 265086714001SSerapheim Dimitropoulos * Returns the spacemap object, or 0 if it wasn't in the ZAP 265186714001SSerapheim Dimitropoulos * or the ZAP doesn't exist yet. 265286714001SSerapheim Dimitropoulos */ 265386714001SSerapheim Dimitropoulos int 265486714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd) 265586714001SSerapheim Dimitropoulos { 265686714001SSerapheim Dimitropoulos ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 265786714001SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) { 265886714001SSerapheim Dimitropoulos return (0); 265986714001SSerapheim Dimitropoulos } 266086714001SSerapheim Dimitropoulos 266186714001SSerapheim Dimitropoulos uint64_t sm_obj = 0; 266286714001SSerapheim Dimitropoulos int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 266386714001SSerapheim Dimitropoulos VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 266486714001SSerapheim Dimitropoulos 266586714001SSerapheim Dimitropoulos ASSERT(err == 0 || err == ENOENT); 266686714001SSerapheim Dimitropoulos 266786714001SSerapheim Dimitropoulos return (sm_obj); 266886714001SSerapheim Dimitropoulos } 266986714001SSerapheim Dimitropoulos 26705cabbc6bSPrashanth Sreenivasa int 2671ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2672fa9e4066Sahrens { 26735cabbc6bSPrashanth Sreenivasa int error = 0; 2674fa9e4066Sahrens /* 2675fa9e4066Sahrens * Recursively load all children. 2676fa9e4066Sahrens */ 26775cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 26785cabbc6bSPrashanth Sreenivasa error = vdev_load(vd->vdev_child[c]); 26795cabbc6bSPrashanth Sreenivasa if (error != 0) { 26805cabbc6bSPrashanth Sreenivasa return (error); 26815cabbc6bSPrashanth Sreenivasa } 26825cabbc6bSPrashanth Sreenivasa } 26835cabbc6bSPrashanth Sreenivasa 26845cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 2685fa9e4066Sahrens 2686fa9e4066Sahrens /* 26870e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2688fa9e4066Sahrens */ 26895cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 26905cabbc6bSPrashanth Sreenivasa if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 26915cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 26925cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 26933ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 26943ee8c80cSPavel Zakharov "asize=%llu", (u_longlong_t)vd->vdev_ashift, 26953ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_asize); 26965cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 26975cabbc6bSPrashanth Sreenivasa } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 26983ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 26993ee8c80cSPavel Zakharov "[error=%d]", error); 27005cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 27015cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 27025cabbc6bSPrashanth Sreenivasa return (error); 27035cabbc6bSPrashanth Sreenivasa } 270486714001SSerapheim Dimitropoulos 270586714001SSerapheim Dimitropoulos uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 270686714001SSerapheim Dimitropoulos if (checkpoint_sm_obj != 0) { 270786714001SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 270886714001SSerapheim Dimitropoulos ASSERT(vd->vdev_asize != 0); 270986714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 271086714001SSerapheim Dimitropoulos 271186714001SSerapheim Dimitropoulos if ((error = space_map_open(&vd->vdev_checkpoint_sm, 271286714001SSerapheim Dimitropoulos mos, checkpoint_sm_obj, 0, vd->vdev_asize, 271386714001SSerapheim Dimitropoulos vd->vdev_ashift))) { 271486714001SSerapheim Dimitropoulos vdev_dbgmsg(vd, "vdev_load: space_map_open " 271586714001SSerapheim Dimitropoulos "failed for checkpoint spacemap (obj %llu) " 271686714001SSerapheim Dimitropoulos "[error=%d]", 271786714001SSerapheim Dimitropoulos (u_longlong_t)checkpoint_sm_obj, error); 271886714001SSerapheim Dimitropoulos return (error); 271986714001SSerapheim Dimitropoulos } 272086714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 272186714001SSerapheim Dimitropoulos space_map_update(vd->vdev_checkpoint_sm); 272286714001SSerapheim Dimitropoulos 272386714001SSerapheim Dimitropoulos /* 272486714001SSerapheim Dimitropoulos * Since the checkpoint_sm contains free entries 272586714001SSerapheim Dimitropoulos * exclusively we can use sm_alloc to indicate the 272686714001SSerapheim Dimitropoulos * culmulative checkpointed space that has been freed. 272786714001SSerapheim Dimitropoulos */ 272886714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space = 272986714001SSerapheim Dimitropoulos -vd->vdev_checkpoint_sm->sm_alloc; 273086714001SSerapheim Dimitropoulos vd->vdev_spa->spa_checkpoint_info.sci_dspace += 273186714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space; 273286714001SSerapheim Dimitropoulos } 27335cabbc6bSPrashanth Sreenivasa } 2734fa9e4066Sahrens 2735fa9e4066Sahrens /* 2736fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2737fa9e4066Sahrens */ 27385cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2739560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2740560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 27413ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 27423ee8c80cSPavel Zakharov "[error=%d]", error); 27435cabbc6bSPrashanth Sreenivasa return (error); 27445cabbc6bSPrashanth Sreenivasa } 27455cabbc6bSPrashanth Sreenivasa 27465cabbc6bSPrashanth Sreenivasa uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 27475cabbc6bSPrashanth Sreenivasa if (obsolete_sm_object != 0) { 27485cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 27495cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_asize != 0); 275086714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 27515cabbc6bSPrashanth Sreenivasa 27525cabbc6bSPrashanth Sreenivasa if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 27535cabbc6bSPrashanth Sreenivasa obsolete_sm_object, 0, vd->vdev_asize, 0))) { 27545cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 27555cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 27563ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 27573ee8c80cSPavel Zakharov "obsolete spacemap (obj %llu) [error=%d]", 27583ee8c80cSPavel Zakharov (u_longlong_t)obsolete_sm_object, error); 27595cabbc6bSPrashanth Sreenivasa return (error); 27605cabbc6bSPrashanth Sreenivasa } 27615cabbc6bSPrashanth Sreenivasa space_map_update(vd->vdev_obsolete_sm); 27625cabbc6bSPrashanth Sreenivasa } 27635cabbc6bSPrashanth Sreenivasa 27645cabbc6bSPrashanth Sreenivasa return (0); 2765fa9e4066Sahrens } 2766fa9e4066Sahrens 276799653d4eSeschrock /* 2768fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 2769fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 2770fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 2771fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 2772fa94a07fSbrendan * repurposed to another pool. 277399653d4eSeschrock */ 277499653d4eSeschrock int 2775fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 277699653d4eSeschrock { 277799653d4eSeschrock nvlist_t *label; 277899653d4eSeschrock uint64_t guid, version; 277999653d4eSeschrock uint64_t state; 278099653d4eSeschrock 2781e14bb325SJeff Bonwick if (!vdev_readable(vd)) 2782c5904d13Seschrock return (0); 2783c5904d13Seschrock 2784dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 278599653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 278699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 278799653d4eSeschrock return (-1); 278899653d4eSeschrock } 278999653d4eSeschrock 279099653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2791ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 279299653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 279399653d4eSeschrock guid != vd->vdev_guid || 279499653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 279599653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 279699653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 279799653d4eSeschrock nvlist_free(label); 279899653d4eSeschrock return (-1); 279999653d4eSeschrock } 280099653d4eSeschrock 280199653d4eSeschrock /* 280299653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 280399653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 280499653d4eSeschrock */ 280599653d4eSeschrock nvlist_free(label); 280699653d4eSeschrock return (0); 280799653d4eSeschrock } 280899653d4eSeschrock 28095cabbc6bSPrashanth Sreenivasa /* 28105cabbc6bSPrashanth Sreenivasa * Free the objects used to store this vdev's spacemaps, and the array 28115cabbc6bSPrashanth Sreenivasa * that points to them. 28125cabbc6bSPrashanth Sreenivasa */ 281388ecc943SGeorge Wilson void 28145cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 28155cabbc6bSPrashanth Sreenivasa { 28165cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0) 28175cabbc6bSPrashanth Sreenivasa return; 28185cabbc6bSPrashanth Sreenivasa 28195cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 28205cabbc6bSPrashanth Sreenivasa uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 28215cabbc6bSPrashanth Sreenivasa size_t array_bytes = array_count * sizeof (uint64_t); 28225cabbc6bSPrashanth Sreenivasa uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 28235cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 28245cabbc6bSPrashanth Sreenivasa array_bytes, smobj_array, 0)); 28255cabbc6bSPrashanth Sreenivasa 28265cabbc6bSPrashanth Sreenivasa for (uint64_t i = 0; i < array_count; i++) { 28275cabbc6bSPrashanth Sreenivasa uint64_t smobj = smobj_array[i]; 28285cabbc6bSPrashanth Sreenivasa if (smobj == 0) 28295cabbc6bSPrashanth Sreenivasa continue; 28305cabbc6bSPrashanth Sreenivasa 28315cabbc6bSPrashanth Sreenivasa space_map_free_obj(mos, smobj, tx); 28325cabbc6bSPrashanth Sreenivasa } 28335cabbc6bSPrashanth Sreenivasa 28345cabbc6bSPrashanth Sreenivasa kmem_free(smobj_array, array_bytes); 28355cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 28365cabbc6bSPrashanth Sreenivasa vd->vdev_ms_array = 0; 28375cabbc6bSPrashanth Sreenivasa } 28385cabbc6bSPrashanth Sreenivasa 28395cabbc6bSPrashanth Sreenivasa static void 28404e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 284188ecc943SGeorge Wilson { 284288ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 284388ecc943SGeorge Wilson 28444e75ba68SSerapheim Dimitropoulos ASSERT(vd->vdev_islog); 2845215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 2846215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 284788ecc943SGeorge Wilson 284888ecc943SGeorge Wilson if (vd->vdev_ms != NULL) { 28492e4c9986SGeorge Wilson metaslab_group_t *mg = vd->vdev_mg; 28502e4c9986SGeorge Wilson 28512e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 28522e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 28532e4c9986SGeorge Wilson 285488ecc943SGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) { 285588ecc943SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 285688ecc943SGeorge Wilson 28570713e232SGeorge Wilson if (msp == NULL || msp->ms_sm == NULL) 285888ecc943SGeorge Wilson continue; 285988ecc943SGeorge Wilson 28600713e232SGeorge Wilson mutex_enter(&msp->ms_lock); 28612e4c9986SGeorge Wilson /* 28622e4c9986SGeorge Wilson * If the metaslab was not loaded when the vdev 28632e4c9986SGeorge Wilson * was removed then the histogram accounting may 28642e4c9986SGeorge Wilson * not be accurate. Update the histogram information 28652e4c9986SGeorge Wilson * here so that we ensure that the metaslab group 28662e4c9986SGeorge Wilson * and metaslab class are up-to-date. 28672e4c9986SGeorge Wilson */ 28682e4c9986SGeorge Wilson metaslab_group_histogram_remove(mg, msp); 28692e4c9986SGeorge Wilson 28700713e232SGeorge Wilson VERIFY0(space_map_allocated(msp->ms_sm)); 28710713e232SGeorge Wilson space_map_close(msp->ms_sm); 28720713e232SGeorge Wilson msp->ms_sm = NULL; 28730713e232SGeorge Wilson mutex_exit(&msp->ms_lock); 287488ecc943SGeorge Wilson } 28752e4c9986SGeorge Wilson 287686714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 287786714001SSerapheim Dimitropoulos ASSERT(spa_has_checkpoint(spa)); 287886714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 287986714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 288086714001SSerapheim Dimitropoulos } 288186714001SSerapheim Dimitropoulos 28822e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 28832e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 28842e4c9986SGeorge Wilson for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 28852e4c9986SGeorge Wilson ASSERT0(mg->mg_histogram[i]); 288688ecc943SGeorge Wilson } 288788ecc943SGeorge Wilson 28884e75ba68SSerapheim Dimitropoulos dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2889215198a6SJoe Stein 28904e75ba68SSerapheim Dimitropoulos vdev_destroy_spacemaps(vd, tx); 28914e75ba68SSerapheim Dimitropoulos if (vd->vdev_top_zap != 0) { 2892215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2893215198a6SJoe Stein vd->vdev_top_zap = 0; 2894215198a6SJoe Stein } 28954e75ba68SSerapheim Dimitropoulos 289688ecc943SGeorge Wilson dmu_tx_commit(tx); 289788ecc943SGeorge Wilson } 289888ecc943SGeorge Wilson 2899fa9e4066Sahrens void 2900fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 2901fa9e4066Sahrens { 2902fa9e4066Sahrens metaslab_t *msp; 290380eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2904fa9e4066Sahrens 29055cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 290688ecc943SGeorge Wilson 2907094e47e9SGeorge Wilson while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2908094e47e9SGeorge Wilson != NULL) 2909fa9e4066Sahrens metaslab_sync_done(msp, txg); 291080eb36f2SGeorge Wilson 291180eb36f2SGeorge Wilson if (reassess) 291280eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 2913fa9e4066Sahrens } 2914fa9e4066Sahrens 2915fa9e4066Sahrens void 2916fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 2917fa9e4066Sahrens { 2918fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2919fa9e4066Sahrens vdev_t *lvd; 2920fa9e4066Sahrens metaslab_t *msp; 2921ecc2d604Sbonwick dmu_tx_t *tx; 2922fa9e4066Sahrens 29235cabbc6bSPrashanth Sreenivasa if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 29245cabbc6bSPrashanth Sreenivasa dmu_tx_t *tx; 29255cabbc6bSPrashanth Sreenivasa 29265cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 29275cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 292888ecc943SGeorge Wilson 29295cabbc6bSPrashanth Sreenivasa tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 29305cabbc6bSPrashanth Sreenivasa vdev_indirect_sync_obsolete(vd, tx); 29315cabbc6bSPrashanth Sreenivasa dmu_tx_commit(tx); 29325cabbc6bSPrashanth Sreenivasa 29335cabbc6bSPrashanth Sreenivasa /* 29345cabbc6bSPrashanth Sreenivasa * If the vdev is indirect, it can't have dirty 29355cabbc6bSPrashanth Sreenivasa * metaslabs or DTLs. 29365cabbc6bSPrashanth Sreenivasa */ 29375cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 29385cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 29395cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 29405cabbc6bSPrashanth Sreenivasa return; 29415cabbc6bSPrashanth Sreenivasa } 29425cabbc6bSPrashanth Sreenivasa } 29435cabbc6bSPrashanth Sreenivasa 29445cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 29455cabbc6bSPrashanth Sreenivasa 29465cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 29475cabbc6bSPrashanth Sreenivasa !vd->vdev_removing) { 2948ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 29495cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 2950ecc2d604Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2951ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2952ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2953ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 2954ecc2d604Sbonwick vdev_config_dirty(vd); 2955ecc2d604Sbonwick dmu_tx_commit(tx); 2956ecc2d604Sbonwick } 2957fa9e4066Sahrens 2958ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2959fa9e4066Sahrens metaslab_sync(msp, txg); 2960ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2961ecc2d604Sbonwick } 2962fa9e4066Sahrens 2963fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2964fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 2965fa9e4066Sahrens 29665cabbc6bSPrashanth Sreenivasa /* 29674e75ba68SSerapheim Dimitropoulos * If this is an empty log device being removed, destroy the 29684e75ba68SSerapheim Dimitropoulos * metadata associated with it. 29695cabbc6bSPrashanth Sreenivasa */ 29704e75ba68SSerapheim Dimitropoulos if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 29714e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vd, txg); 29725cabbc6bSPrashanth Sreenivasa 2973fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2974fa9e4066Sahrens } 2975fa9e4066Sahrens 2976fa9e4066Sahrens uint64_t 2977fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2978fa9e4066Sahrens { 2979fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2980fa9e4066Sahrens } 2981fa9e4066Sahrens 29823d7072f8Seschrock /* 29833d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 29843d7072f8Seschrock * not be opened, and no I/O is attempted. 29853d7072f8Seschrock */ 2986fa9e4066Sahrens int 2987069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2988fa9e4066Sahrens { 29894b964adaSGeorge Wilson vdev_t *vd, *tvd; 2990fa9e4066Sahrens 29918f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 2992fa9e4066Sahrens 2993c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2994e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2995e14bb325SJeff Bonwick 29963d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 2997e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2998fa9e4066Sahrens 29994b964adaSGeorge Wilson tvd = vd->vdev_top; 30004b964adaSGeorge Wilson 3001069f55e2SEric Schrock /* 3002069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 3003069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 3004069f55e2SEric Schrock * were faulted. 3005069f55e2SEric Schrock */ 3006069f55e2SEric Schrock vd->vdev_label_aux = aux; 3007069f55e2SEric Schrock 30083d7072f8Seschrock /* 30093d7072f8Seschrock * Faulted state takes precedence over degraded. 30103d7072f8Seschrock */ 301198d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 30123d7072f8Seschrock vd->vdev_faulted = 1ULL; 30133d7072f8Seschrock vd->vdev_degraded = 0ULL; 3014069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 30153d7072f8Seschrock 30163d7072f8Seschrock /* 3017c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 3018c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 30193d7072f8Seschrock */ 30204b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 30213d7072f8Seschrock vd->vdev_degraded = 1ULL; 30223d7072f8Seschrock vd->vdev_faulted = 0ULL; 30233d7072f8Seschrock 30243d7072f8Seschrock /* 30253d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 30263d7072f8Seschrock * mark it degraded. 30273d7072f8Seschrock */ 30284b964adaSGeorge Wilson vdev_reopen(tvd); 30293d7072f8Seschrock 3030069f55e2SEric Schrock if (vdev_readable(vd)) 3031069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 30323d7072f8Seschrock } 30333d7072f8Seschrock 3034e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 30353d7072f8Seschrock } 30363d7072f8Seschrock 30373d7072f8Seschrock /* 30383d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 30393d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 30403d7072f8Seschrock * as I/O is concerned. 30413d7072f8Seschrock */ 30423d7072f8Seschrock int 3043069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 30443d7072f8Seschrock { 3045c5904d13Seschrock vdev_t *vd; 30460a4e9518Sgw 30478f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 30483d7072f8Seschrock 3049c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3050e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3051e14bb325SJeff Bonwick 30520e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3053e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 30540e34b6a7Sbonwick 30553d7072f8Seschrock /* 30563d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 30573d7072f8Seschrock */ 3058e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 3059e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 30603d7072f8Seschrock 30613d7072f8Seschrock vd->vdev_degraded = 1ULL; 30623d7072f8Seschrock if (!vdev_is_dead(vd)) 30633d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3064069f55e2SEric Schrock aux); 30653d7072f8Seschrock 3066e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 30673d7072f8Seschrock } 30683d7072f8Seschrock 30693d7072f8Seschrock /* 3070f7170741SWill Andrews * Online the given vdev. 3071f7170741SWill Andrews * 3072f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3073f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 3074f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 3075f7170741SWill Andrews * events are generated if the device fails to open. 30763d7072f8Seschrock */ 30773d7072f8Seschrock int 3078e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 30793d7072f8Seschrock { 3080573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 30815f368aefSYuri Pankov boolean_t wasoffline; 30825f368aefSYuri Pankov vdev_state_t oldstate; 30833d7072f8Seschrock 30848f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 30853d7072f8Seschrock 3086c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3087e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 30883d7072f8Seschrock 30893d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3090e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3091fa9e4066Sahrens 30925f368aefSYuri Pankov wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 30935f368aefSYuri Pankov oldstate = vd->vdev_state; 309414372834SHans Rosenfeld 3095573ca77eSGeorge Wilson tvd = vd->vdev_top; 3096fa9e4066Sahrens vd->vdev_offline = B_FALSE; 3097441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 3098e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3099e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3100573ca77eSGeorge Wilson 3101573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3102573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3103573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3104573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3105573ca77eSGeorge Wilson } 3106573ca77eSGeorge Wilson 3107573ca77eSGeorge Wilson vdev_reopen(tvd); 31083d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 31093d7072f8Seschrock 3110573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3111573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3112573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 3113573ca77eSGeorge Wilson } 3114573ca77eSGeorge Wilson 31153d7072f8Seschrock if (newstate) 31163d7072f8Seschrock *newstate = vd->vdev_state; 31173d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 31183d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 31193d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 31203d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 31213d7072f8Seschrock vd->vdev_unspare = B_TRUE; 3122fa9e4066Sahrens 3123573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3124573ca77eSGeorge Wilson 3125573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3126573ca77eSGeorge Wilson if (vd->vdev_aux) 3127573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3128573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3129573ca77eSGeorge Wilson } 313014372834SHans Rosenfeld 3131094e47e9SGeorge Wilson /* Restart initializing if necessary */ 3132094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 3133094e47e9SGeorge Wilson if (vdev_writeable(vd) && 3134094e47e9SGeorge Wilson vd->vdev_initialize_thread == NULL && 3135094e47e9SGeorge Wilson vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3136094e47e9SGeorge Wilson (void) vdev_initialize(vd); 3137094e47e9SGeorge Wilson } 3138094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 3139094e47e9SGeorge Wilson 31405f368aefSYuri Pankov if (wasoffline || 31415f368aefSYuri Pankov (oldstate < VDEV_STATE_DEGRADED && 31425f368aefSYuri Pankov vd->vdev_state >= VDEV_STATE_DEGRADED)) 3143ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 314414372834SHans Rosenfeld 31458ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 3146fa9e4066Sahrens } 3147fa9e4066Sahrens 3148a1521560SJeff Bonwick static int 3149a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3150fa9e4066Sahrens { 3151e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 31528f18d1faSGeorge Wilson int error = 0; 31538f18d1faSGeorge Wilson uint64_t generation; 31548f18d1faSGeorge Wilson metaslab_group_t *mg; 31550a4e9518Sgw 31568f18d1faSGeorge Wilson top: 31578f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 3158fa9e4066Sahrens 3159c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3160e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3161fa9e4066Sahrens 31620e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3163e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 31640e34b6a7Sbonwick 3165e6ca193dSGeorge Wilson tvd = vd->vdev_top; 31668f18d1faSGeorge Wilson mg = tvd->vdev_mg; 31678f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 3168e6ca193dSGeorge Wilson 3169fa9e4066Sahrens /* 3170ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 3171fa9e4066Sahrens */ 3172ecc2d604Sbonwick if (!vd->vdev_offline) { 3173ecc2d604Sbonwick /* 31748ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 3175e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 3176e6ca193dSGeorge Wilson * expendable. 3177ecc2d604Sbonwick */ 3178e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3179e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 3180e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3181fa9e4066Sahrens 31828f18d1faSGeorge Wilson /* 3183b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 3184b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 3185b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 3186b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 31878f18d1faSGeorge Wilson */ 31888f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 31898f18d1faSGeorge Wilson /* 31908f18d1faSGeorge Wilson * Prevent any future allocations. 31918f18d1faSGeorge Wilson */ 3192a1521560SJeff Bonwick metaslab_group_passivate(mg); 31938f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 31948f18d1faSGeorge Wilson 31955cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 31968f18d1faSGeorge Wilson 319786714001SSerapheim Dimitropoulos /* 319886714001SSerapheim Dimitropoulos * If the log device was successfully reset but has 319986714001SSerapheim Dimitropoulos * checkpointed data, do not offline it. 320086714001SSerapheim Dimitropoulos */ 320186714001SSerapheim Dimitropoulos if (error == 0 && 320286714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm != NULL) { 320386714001SSerapheim Dimitropoulos ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, 320486714001SSerapheim Dimitropoulos !=, 0); 320586714001SSerapheim Dimitropoulos error = ZFS_ERR_CHECKPOINT_EXISTS; 320686714001SSerapheim Dimitropoulos } 320786714001SSerapheim Dimitropoulos 32088f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 32098f18d1faSGeorge Wilson 32108f18d1faSGeorge Wilson /* 32118f18d1faSGeorge Wilson * Check to see if the config has changed. 32128f18d1faSGeorge Wilson */ 32138f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 3214a1521560SJeff Bonwick metaslab_group_activate(mg); 32158f18d1faSGeorge Wilson if (error) 32168f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 32178f18d1faSGeorge Wilson vd, error)); 32188f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 32198f18d1faSGeorge Wilson goto top; 32208f18d1faSGeorge Wilson } 3221fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 32228f18d1faSGeorge Wilson } 32238f18d1faSGeorge Wilson 3224ecc2d604Sbonwick /* 3225ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 3226e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 3227e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 3228e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 3229ecc2d604Sbonwick */ 3230ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 3231e6ca193dSGeorge Wilson vdev_reopen(tvd); 3232e6ca193dSGeorge Wilson 3233e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3234e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 3235ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 3236e6ca193dSGeorge Wilson vdev_reopen(tvd); 3237e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3238ecc2d604Sbonwick } 32398f18d1faSGeorge Wilson 32408f18d1faSGeorge Wilson /* 32418f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 32428f18d1faSGeorge Wilson * once we online the device it's open for business. 32438f18d1faSGeorge Wilson */ 32448f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 3245a1521560SJeff Bonwick metaslab_group_activate(mg); 3246fa9e4066Sahrens } 3247fa9e4066Sahrens 3248e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3249ecc2d604Sbonwick 32508f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 3251fa9e4066Sahrens } 3252fa9e4066Sahrens 3253a1521560SJeff Bonwick int 3254a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3255a1521560SJeff Bonwick { 3256a1521560SJeff Bonwick int error; 3257a1521560SJeff Bonwick 3258a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 3259a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 3260a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 3261a1521560SJeff Bonwick 3262a1521560SJeff Bonwick return (error); 3263a1521560SJeff Bonwick } 3264a1521560SJeff Bonwick 3265ea8dc4b6Seschrock /* 3266ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 3267ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 3268ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3269ea8dc4b6Seschrock */ 3270ea8dc4b6Seschrock void 3271e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 3272fa9e4066Sahrens { 3273e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3274e14bb325SJeff Bonwick 3275e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3276fa9e4066Sahrens 3277ea8dc4b6Seschrock if (vd == NULL) 3278e14bb325SJeff Bonwick vd = rvd; 3279fa9e4066Sahrens 3280ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 3281ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 3282ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 3283fa9e4066Sahrens 3284e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3285e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 32863d7072f8Seschrock 32875cabbc6bSPrashanth Sreenivasa /* 32885cabbc6bSPrashanth Sreenivasa * It makes no sense to "clear" an indirect vdev. 32895cabbc6bSPrashanth Sreenivasa */ 32905cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(vd)) 32915cabbc6bSPrashanth Sreenivasa return; 32925cabbc6bSPrashanth Sreenivasa 32933d7072f8Seschrock /* 32948a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 32958a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 32968a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 32978a79c1b5Sek * written out to disk. 32983d7072f8Seschrock */ 3299e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 3300e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 33018a79c1b5Sek 3302096d22d4SEric Schrock /* 3303096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 3304096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 3305096d22d4SEric Schrock * still broken, we want to still post the ereport again. 3306096d22d4SEric Schrock */ 3307096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 3308096d22d4SEric Schrock 33094b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3310e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 3311e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 3312e14bb325SJeff Bonwick 3313f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 33143d7072f8Seschrock 3315096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 3316096d22d4SEric Schrock 3317f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 3318e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3319e14bb325SJeff Bonwick 3320e14bb325SJeff Bonwick if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3321bb8b5132Sek spa_async_request(spa, SPA_ASYNC_RESILVER); 33223d7072f8Seschrock 3323ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 33243d7072f8Seschrock } 3325096d22d4SEric Schrock 3326096d22d4SEric Schrock /* 3327096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 3328096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 3329096d22d4SEric Schrock * done in response to the FMA fault. 3330096d22d4SEric Schrock */ 3331096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3332096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3333096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 3334096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 3335fa9e4066Sahrens } 3336fa9e4066Sahrens 3337e14bb325SJeff Bonwick boolean_t 3338e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 33390a4e9518Sgw { 334088ecc943SGeorge Wilson /* 334188ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 334288ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 334388ecc943SGeorge Wilson * these types of devices in the various code paths. 334488ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 334588ecc943SGeorge Wilson * before issuing I/O to them. 334688ecc943SGeorge Wilson */ 33475cabbc6bSPrashanth Sreenivasa return (vd->vdev_state < VDEV_STATE_DEGRADED || 33485cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_hole_ops || 334988ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 33500a4e9518Sgw } 33510a4e9518Sgw 3352e14bb325SJeff Bonwick boolean_t 3353e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 33540a4e9518Sgw { 3355e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 33560a4e9518Sgw } 33570a4e9518Sgw 3358e14bb325SJeff Bonwick boolean_t 3359e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 3360fa9e4066Sahrens { 33615cabbc6bSPrashanth Sreenivasa return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 33625cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)); 3363fa9e4066Sahrens } 3364fa9e4066Sahrens 3365a31e6787SGeorge Wilson boolean_t 3366a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 3367a31e6787SGeorge Wilson { 33688ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 33698ad4d6ddSJeff Bonwick 3370a31e6787SGeorge Wilson /* 33718ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 3372a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3373a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 33748ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 33758ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 33768ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 3377a31e6787SGeorge Wilson */ 33788ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 33795cabbc6bSPrashanth Sreenivasa !vd->vdev_cant_write && vdev_is_concrete(vd) && 33800f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 3381a31e6787SGeorge Wilson } 3382a31e6787SGeorge Wilson 3383e14bb325SJeff Bonwick boolean_t 3384e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 3385fa9e4066Sahrens { 3386e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 3387fa9e4066Sahrens 3388e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3389e14bb325SJeff Bonwick return (B_FALSE); 3390fa9e4066Sahrens 3391e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 3392e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 3393fa9e4066Sahrens 3394e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3395e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 3396fa9e4066Sahrens 3397e14bb325SJeff Bonwick return (B_TRUE); 3398fa9e4066Sahrens } 3399fa9e4066Sahrens 340086714001SSerapheim Dimitropoulos boolean_t 340186714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd) 340286714001SSerapheim Dimitropoulos { 3403*a0b03b16SSerapheim Dimitropoulos if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 3404*a0b03b16SSerapheim Dimitropoulos return (B_TRUE); 3405*a0b03b16SSerapheim Dimitropoulos 340686714001SSerapheim Dimitropoulos /* 3407*a0b03b16SSerapheim Dimitropoulos * If double-word space map entries are not enabled we assume 3408*a0b03b16SSerapheim Dimitropoulos * 47 bits of the space map entry are dedicated to the entry's 3409*a0b03b16SSerapheim Dimitropoulos * offset (see SM_OFFSET_BITS in space_map.h). We then use that 3410*a0b03b16SSerapheim Dimitropoulos * to calculate the maximum address that can be described by a 3411*a0b03b16SSerapheim Dimitropoulos * space map entry for the given device. 341286714001SSerapheim Dimitropoulos */ 3413*a0b03b16SSerapheim Dimitropoulos uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 341486714001SSerapheim Dimitropoulos 341586714001SSerapheim Dimitropoulos if (shift >= 63) /* detect potential overflow */ 341686714001SSerapheim Dimitropoulos return (B_TRUE); 341786714001SSerapheim Dimitropoulos 341886714001SSerapheim Dimitropoulos return (vd->vdev_asize < (1ULL << shift)); 341986714001SSerapheim Dimitropoulos } 342086714001SSerapheim Dimitropoulos 3421fa9e4066Sahrens /* 3422fa9e4066Sahrens * Get statistics for the given vdev. 3423fa9e4066Sahrens */ 3424fa9e4066Sahrens void 3425fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3426fa9e4066Sahrens { 34272e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 34282e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 3429c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 34302e4c9986SGeorge Wilson 34312e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3432fa9e4066Sahrens 3433fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3434fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3435fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3436fa9e4066Sahrens vs->vs_state = vd->vdev_state; 3437573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 3438094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3439573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3440094e47e9SGeorge Wilson /* 3441094e47e9SGeorge Wilson * Report intializing progress. Since we don't have the 3442094e47e9SGeorge Wilson * initializing locks held, this is only an estimate (although a 3443094e47e9SGeorge Wilson * fairly accurate one). 3444094e47e9SGeorge Wilson */ 3445094e47e9SGeorge Wilson vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done; 3446094e47e9SGeorge Wilson vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est; 3447094e47e9SGeorge Wilson vs->vs_initialize_state = vd->vdev_initialize_state; 3448094e47e9SGeorge Wilson vs->vs_initialize_action_time = vd->vdev_initialize_action_time; 3449094e47e9SGeorge Wilson } 3450c39a2aaeSGeorge Wilson /* 3451c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 3452c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 3453c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 3454c39a2aaeSGeorge Wilson */ 3455c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 34567855d95bSToomas Soome vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 34577855d95bSToomas Soome spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3458c39a2aaeSGeorge Wilson } 34595cabbc6bSPrashanth Sreenivasa if (vd->vdev_aux == NULL && vd == vd->vdev_top && 34605cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 34612e4c9986SGeorge Wilson vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 34622986efa8SAlex Reece } 3463fa9e4066Sahrens 3464fa9e4066Sahrens /* 3465fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 3466fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 3467fa9e4066Sahrens */ 3468fa9e4066Sahrens if (vd == rvd) { 3469e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 3470fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 3471fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 3472fa9e4066Sahrens 3473e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 3474fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 3475fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 3476fa9e4066Sahrens } 34773f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 3478fa9e4066Sahrens } 3479fa9e4066Sahrens } 34802e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 3481fa9e4066Sahrens } 3482fa9e4066Sahrens 3483fa94a07fSbrendan void 3484fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 3485fa94a07fSbrendan { 3486fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 3487fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 3488fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 3489fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 3490fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 3491fa94a07fSbrendan } 3492fa94a07fSbrendan 34933f9d6ad7SLin Ling void 34943f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 34953f9d6ad7SLin Ling { 34963f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 34973f9d6ad7SLin Ling 34983f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 34993f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 35003f9d6ad7SLin Ling 35013f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 35023f9d6ad7SLin Ling vs->vs_scan_processed = 0; 35033f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 35043f9d6ad7SLin Ling } 35053f9d6ad7SLin Ling 3506fa9e4066Sahrens void 3507e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 3508fa9e4066Sahrens { 35098ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 35108ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3511e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3512fa9e4066Sahrens vdev_t *pvd; 3513fa9e4066Sahrens uint64_t txg = zio->io_txg; 3514fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 3515fa9e4066Sahrens zio_type_t type = zio->io_type; 3516fa9e4066Sahrens int flags = zio->io_flags; 3517fa9e4066Sahrens 3518e14bb325SJeff Bonwick /* 3519e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 3520e14bb325SJeff Bonwick */ 3521e14bb325SJeff Bonwick if (zio->io_gang_tree) 3522e14bb325SJeff Bonwick return; 3523e14bb325SJeff Bonwick 3524fa9e4066Sahrens if (zio->io_error == 0) { 3525e14bb325SJeff Bonwick /* 3526e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 3527e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 3528e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 3529e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 3530e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 3531e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 3532e14bb325SJeff Bonwick * remain zero, which is what we want.) 3533e14bb325SJeff Bonwick * 3534e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 3535e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 3536e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 3537e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 3538e14bb325SJeff Bonwick */ 3539e14bb325SJeff Bonwick if (vd == rvd) 3540e14bb325SJeff Bonwick return; 3541e14bb325SJeff Bonwick 3542e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 35438ad4d6ddSJeff Bonwick 35448ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 35458ad4d6ddSJeff Bonwick return; 35468ad4d6ddSJeff Bonwick 35478ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 35488ad4d6ddSJeff Bonwick 3549e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 355044ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 35513f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 35523f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 35533f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 35543f9d6ad7SLin Ling 35553f9d6ad7SLin Ling /* XXX cleanup? */ 35563f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 35573f9d6ad7SLin Ling atomic_add_64(processed, psize); 35583f9d6ad7SLin Ling vs->vs_scan_processed += psize; 35593f9d6ad7SLin Ling } 35603f9d6ad7SLin Ling 35618ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 3562e14bb325SJeff Bonwick vs->vs_self_healed += psize; 3563fa9e4066Sahrens } 35648ad4d6ddSJeff Bonwick 35658ad4d6ddSJeff Bonwick vs->vs_ops[type]++; 35668ad4d6ddSJeff Bonwick vs->vs_bytes[type] += psize; 35678ad4d6ddSJeff Bonwick 35688ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3569fa9e4066Sahrens return; 3570fa9e4066Sahrens } 3571fa9e4066Sahrens 3572fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 3573fa9e4066Sahrens return; 3574fa9e4066Sahrens 35758956713aSEric Schrock /* 35768956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 35778956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 35788956713aSEric Schrock * hard errors, when in reality they can happen for any number of 35798956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 35808956713aSEric Schrock */ 35818956713aSEric Schrock if (zio->io_error == EIO && 35828956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 35838956713aSEric Schrock return; 35848956713aSEric Schrock 35858f18d1faSGeorge Wilson /* 35868f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 35878f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 35888f18d1faSGeorge Wilson * errors. 35898f18d1faSGeorge Wilson */ 35908f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 35918f18d1faSGeorge Wilson return; 35928f18d1faSGeorge Wilson 3593e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 3594b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3595e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 3596e14bb325SJeff Bonwick vs->vs_checksum_errors++; 3597e14bb325SJeff Bonwick else 3598e14bb325SJeff Bonwick vs->vs_read_errors++; 3599fa9e4066Sahrens } 3600b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3601e14bb325SJeff Bonwick vs->vs_write_errors++; 3602e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3603fa9e4066Sahrens 36045cabbc6bSPrashanth Sreenivasa if (spa->spa_load_state == SPA_LOAD_NONE && 36055cabbc6bSPrashanth Sreenivasa type == ZIO_TYPE_WRITE && txg != 0 && 36068ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 360744ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 3608b24ab676SJeff Bonwick spa->spa_claiming)) { 36098ad4d6ddSJeff Bonwick /* 3610b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 3611b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 3612b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 3613b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 3614b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 3615b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 3616b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 3617b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 36188ad4d6ddSJeff Bonwick * 36198ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 36208ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 36218ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 36228ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 36238ad4d6ddSJeff Bonwick */ 36248ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 36258ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 362644ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 36278ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 36288ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 36298ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3630b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 3631b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 3632b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3633b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 36348ad4d6ddSJeff Bonwick } 3635b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 36368ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3637fa9e4066Sahrens return; 36388ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 36398ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 36408ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3641fa9e4066Sahrens } 36428ad4d6ddSJeff Bonwick if (vd != rvd) 36438ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3644fa9e4066Sahrens } 3645fa9e4066Sahrens } 3646fa9e4066Sahrens 3647fa9e4066Sahrens /* 3648b24ab676SJeff Bonwick * Update the in-core space usage stats for this vdev, its metaslab class, 3649b24ab676SJeff Bonwick * and the root vdev. 3650fa9e4066Sahrens */ 3651fa9e4066Sahrens void 3652b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3653b24ab676SJeff Bonwick int64_t space_delta) 3654fa9e4066Sahrens { 365599653d4eSeschrock int64_t dspace_delta = space_delta; 36568654d025Sperrin spa_t *spa = vd->vdev_spa; 36578654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 3658b24ab676SJeff Bonwick metaslab_group_t *mg = vd->vdev_mg; 3659b24ab676SJeff Bonwick metaslab_class_t *mc = mg ? mg->mg_class : NULL; 3660fa9e4066Sahrens 36618654d025Sperrin ASSERT(vd == vd->vdev_top); 366299653d4eSeschrock 36638654d025Sperrin /* 36648654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 36658654d025Sperrin * factor. We must calculate this here and not at the root vdev 36668654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 36678654d025Sperrin * childrens', thus not accurate enough for us. 36688654d025Sperrin */ 36698654d025Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 3670e6ca193dSGeorge Wilson ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 36718654d025Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 36728654d025Sperrin vd->vdev_deflate_ratio; 36738654d025Sperrin 36748654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 36758654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 3676b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 36778654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 36788654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 36798654d025Sperrin 3680b24ab676SJeff Bonwick if (mc == spa_normal_class(spa)) { 3681fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 3682fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 3683b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 3684fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 3685fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 3686fa94a07fSbrendan } 3687b24ab676SJeff Bonwick 3688b24ab676SJeff Bonwick if (mc != NULL) { 3689b24ab676SJeff Bonwick ASSERT(rvd == vd->vdev_parent); 3690b24ab676SJeff Bonwick ASSERT(vd->vdev_ms_count != 0); 3691b24ab676SJeff Bonwick 3692b24ab676SJeff Bonwick metaslab_class_space_update(mc, 3693b24ab676SJeff Bonwick alloc_delta, defer_delta, space_delta, dspace_delta); 3694b24ab676SJeff Bonwick } 3695fa9e4066Sahrens } 3696fa9e4066Sahrens 3697fa9e4066Sahrens /* 3698fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3699fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3700fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3701fa9e4066Sahrens */ 3702fa9e4066Sahrens void 3703fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3704fa9e4066Sahrens { 3705fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3706fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3707fa9e4066Sahrens int c; 3708fa9e4066Sahrens 3709f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3710f9af39baSGeorge Wilson 3711c5904d13Seschrock /* 37126809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 37136809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3714c5904d13Seschrock */ 3715c5904d13Seschrock if (vd->vdev_aux != NULL) { 3716c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3717c5904d13Seschrock nvlist_t **aux; 3718c5904d13Seschrock uint_t naux; 3719c5904d13Seschrock 3720c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3721c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3722c5904d13Seschrock break; 3723c5904d13Seschrock } 3724c5904d13Seschrock 3725e14bb325SJeff Bonwick if (c == sav->sav_count) { 3726e14bb325SJeff Bonwick /* 3727e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3728e14bb325SJeff Bonwick */ 3729e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3730e14bb325SJeff Bonwick return; 3731e14bb325SJeff Bonwick } 3732e14bb325SJeff Bonwick 3733c5904d13Seschrock sav->sav_sync = B_TRUE; 3734c5904d13Seschrock 37356809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 37366809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 37376809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 37386809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 37396809eb4eSEric Schrock } 3740c5904d13Seschrock 3741c5904d13Seschrock ASSERT(c < naux); 3742c5904d13Seschrock 3743c5904d13Seschrock /* 3744c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 3745c5904d13Seschrock * sketchy, but it will work. 3746c5904d13Seschrock */ 3747c5904d13Seschrock nvlist_free(aux[c]); 37483f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3749c5904d13Seschrock 3750c5904d13Seschrock return; 3751c5904d13Seschrock } 3752c5904d13Seschrock 37535dabedeeSbonwick /* 3754e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 3755e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 3756e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 37575dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 37585dabedeeSbonwick */ 3759e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3760e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3761e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 37625dabedeeSbonwick 3763fa9e4066Sahrens if (vd == rvd) { 3764fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 3765fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 3766fa9e4066Sahrens } else { 3767fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 3768fa9e4066Sahrens 376988ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 37705cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3771e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 37725cabbc6bSPrashanth Sreenivasa } 3773fa9e4066Sahrens } 3774fa9e4066Sahrens } 3775fa9e4066Sahrens 3776fa9e4066Sahrens void 3777fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 3778fa9e4066Sahrens { 37795dabedeeSbonwick spa_t *spa = vd->vdev_spa; 37805dabedeeSbonwick 3781e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3782e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3783e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 37845dabedeeSbonwick 3785e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3786e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 3787e14bb325SJeff Bonwick } 3788e14bb325SJeff Bonwick 3789e14bb325SJeff Bonwick /* 3790e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 3791e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3792e14bb325SJeff Bonwick * the state changes from larger config changes because they require 3793e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 3794e14bb325SJeff Bonwick */ 3795e14bb325SJeff Bonwick void 3796e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 3797e14bb325SJeff Bonwick { 3798e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3799e14bb325SJeff Bonwick 3800f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3801e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3802e14bb325SJeff Bonwick 3803e14bb325SJeff Bonwick /* 3804e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 3805e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 3806e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 3807e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 3808e14bb325SJeff Bonwick */ 3809e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3810e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3811e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3812e14bb325SJeff Bonwick 38135cabbc6bSPrashanth Sreenivasa if (!list_link_active(&vd->vdev_state_dirty_node) && 38145cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) 3815e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 3816e14bb325SJeff Bonwick } 3817e14bb325SJeff Bonwick 3818e14bb325SJeff Bonwick void 3819e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 3820e14bb325SJeff Bonwick { 3821e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3822e14bb325SJeff Bonwick 3823e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3824e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3825e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3826e14bb325SJeff Bonwick 3827e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3828e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 3829fa9e4066Sahrens } 3830fa9e4066Sahrens 383132b87932Sek /* 383232b87932Sek * Propagate vdev state up from children to parent. 383332b87932Sek */ 383444cd46caSbillm void 383544cd46caSbillm vdev_propagate_state(vdev_t *vd) 383644cd46caSbillm { 38378ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 38388ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 383944cd46caSbillm int degraded = 0, faulted = 0; 384044cd46caSbillm int corrupted = 0; 384144cd46caSbillm vdev_t *child; 384244cd46caSbillm 38433d7072f8Seschrock if (vd->vdev_children > 0) { 3844573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 38453d7072f8Seschrock child = vd->vdev_child[c]; 384651ece835Seschrock 384788ecc943SGeorge Wilson /* 38485cabbc6bSPrashanth Sreenivasa * Don't factor holes or indirect vdevs into the 38495cabbc6bSPrashanth Sreenivasa * decision. 385088ecc943SGeorge Wilson */ 38515cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(child)) 385288ecc943SGeorge Wilson continue; 385388ecc943SGeorge Wilson 3854e14bb325SJeff Bonwick if (!vdev_readable(child) || 38558ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 385651ece835Seschrock /* 385751ece835Seschrock * Root special: if there is a top-level log 385851ece835Seschrock * device, treat the root vdev as if it were 385951ece835Seschrock * degraded. 386051ece835Seschrock */ 386151ece835Seschrock if (child->vdev_islog && vd == rvd) 386251ece835Seschrock degraded++; 386351ece835Seschrock else 386451ece835Seschrock faulted++; 386551ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 38663d7072f8Seschrock degraded++; 386751ece835Seschrock } 386844cd46caSbillm 38693d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 38703d7072f8Seschrock corrupted++; 38713d7072f8Seschrock } 387244cd46caSbillm 38733d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 38743d7072f8Seschrock 38753d7072f8Seschrock /* 3876e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 38773d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 38783d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 38793d7072f8Seschrock * replicas'. 38803d7072f8Seschrock */ 38813d7072f8Seschrock if (corrupted && vd == rvd && 38823d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 38833d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 38843d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 38853d7072f8Seschrock } 38863d7072f8Seschrock 388751ece835Seschrock if (vd->vdev_parent) 38883d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 388944cd46caSbillm } 389044cd46caSbillm 3891fa9e4066Sahrens /* 3892ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 3893ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 3894ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 3895ea8dc4b6Seschrock * 3896ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 3897ea8dc4b6Seschrock * generated. 3898fa9e4066Sahrens */ 3899fa9e4066Sahrens void 3900ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3901fa9e4066Sahrens { 3902560e6e96Seschrock uint64_t save_state; 3903c5904d13Seschrock spa_t *spa = vd->vdev_spa; 3904ea8dc4b6Seschrock 3905ea8dc4b6Seschrock if (state == vd->vdev_state) { 3906ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 3907fa9e4066Sahrens return; 3908ea8dc4b6Seschrock } 3909ea8dc4b6Seschrock 3910560e6e96Seschrock save_state = vd->vdev_state; 3911fa9e4066Sahrens 3912fa9e4066Sahrens vd->vdev_state = state; 3913fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 3914fa9e4066Sahrens 39153d7072f8Seschrock /* 39163d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 391798d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 391898d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 391998d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 392098d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 392198d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 392298d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 392398d1cbfeSGeorge Wilson * children. 392498d1cbfeSGeorge Wilson */ 392598d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 392698d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 39273d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 39283d7072f8Seschrock 3929069f55e2SEric Schrock /* 3930069f55e2SEric Schrock * If we have brought this vdev back into service, we need 3931069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 3932069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 3933069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 3934069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 3935069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 3936069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 3937069f55e2SEric Schrock */ 3938069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3939069f55e2SEric Schrock vd->vdev_prevstate != state) 3940069f55e2SEric Schrock zfs_post_state_change(spa, vd); 3941069f55e2SEric Schrock 39423d7072f8Seschrock if (vd->vdev_removed && 39433d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 39443d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 39453d7072f8Seschrock /* 39463d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 39473d7072f8Seschrock * device was previously marked removed and someone attempted to 39483d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 39493d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 39503d7072f8Seschrock * it is one of our special test online cases, which is only 39513d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 39523d7072f8Seschrock * fault. 39533d7072f8Seschrock */ 39543d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 39553d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 39563d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 39573d7072f8Seschrock vd->vdev_removed = B_TRUE; 39583d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 3959ea8dc4b6Seschrock /* 3960cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 3961cb04b873SMark J Musante * mark it as "not available", which signifies that it was 3962cb04b873SMark J Musante * never there to begin with. Failure to open such a device 3963cb04b873SMark J Musante * is not considered an error. 3964ea8dc4b6Seschrock */ 3965cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3966cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 3967560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 3968560e6e96Seschrock vd->vdev_not_present = 1; 3969560e6e96Seschrock 3970560e6e96Seschrock /* 3971560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 3972560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 3973560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 3974560e6e96Seschrock * want to post the ereport if the device was already in the 3975560e6e96Seschrock * CANT_OPEN state beforehand. 39763d7072f8Seschrock * 39773d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 39783d7072f8Seschrock * online the device in response to an insertion event. If we 39793d7072f8Seschrock * hit this case, then we have detected an insertion event for a 39803d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 39813d7072f8Seschrock * In this scenario, we don't post an ereport because we are 39823d7072f8Seschrock * about to replace the device, or attempt an online with 39833d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 3984560e6e96Seschrock */ 39853d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 39863d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 3987c5904d13Seschrock vd != spa->spa_root_vdev) { 3988ea8dc4b6Seschrock const char *class; 3989ea8dc4b6Seschrock 3990ea8dc4b6Seschrock switch (aux) { 3991ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 3992ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3993ea8dc4b6Seschrock break; 3994ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 3995ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3996ea8dc4b6Seschrock break; 3997ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 3998ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3999ea8dc4b6Seschrock break; 4000ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 4001ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 4002ea8dc4b6Seschrock break; 4003ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 4004ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 4005ea8dc4b6Seschrock break; 4006ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 4007ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 4008ea8dc4b6Seschrock break; 4009ea8dc4b6Seschrock default: 4010ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 4011ea8dc4b6Seschrock } 4012ea8dc4b6Seschrock 4013c5904d13Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 4014ea8dc4b6Seschrock } 4015ea8dc4b6Seschrock 40163d7072f8Seschrock /* Erase any notion of persistent removed state */ 40173d7072f8Seschrock vd->vdev_removed = B_FALSE; 40183d7072f8Seschrock } else { 40193d7072f8Seschrock vd->vdev_removed = B_FALSE; 40203d7072f8Seschrock } 4021ea8dc4b6Seschrock 40228b33d774STim Haley if (!isopen && vd->vdev_parent) 40238b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 4024fa9e4066Sahrens } 402515e6edf1Sgw 40266f793812SPavel Zakharov boolean_t 40276f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd) 40286f793812SPavel Zakharov { 40296f793812SPavel Zakharov ASSERT(!vd->vdev_ops->vdev_op_leaf); 40306f793812SPavel Zakharov 40316f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) { 40326f793812SPavel Zakharov if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 40336f793812SPavel Zakharov return (B_FALSE); 40346f793812SPavel Zakharov } 40356f793812SPavel Zakharov 40366f793812SPavel Zakharov return (B_TRUE); 40376f793812SPavel Zakharov } 40386f793812SPavel Zakharov 403915e6edf1Sgw /* 404015e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 4041c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 4042c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 404315e6edf1Sgw */ 404415e6edf1Sgw boolean_t 404515e6edf1Sgw vdev_is_bootable(vdev_t *vd) 404615e6edf1Sgw { 404715e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 404815e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 404915e6edf1Sgw 405015e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 405115e6edf1Sgw vd->vdev_children > 1) { 405215e6edf1Sgw return (B_FALSE); 40535cabbc6bSPrashanth Sreenivasa } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 40545cabbc6bSPrashanth Sreenivasa strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 405515e6edf1Sgw return (B_FALSE); 405615e6edf1Sgw } 405715e6edf1Sgw } 405815e6edf1Sgw 4059573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 406015e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 406115e6edf1Sgw return (B_FALSE); 406215e6edf1Sgw } 406315e6edf1Sgw return (B_TRUE); 406415e6edf1Sgw } 4065e6ca193dSGeorge Wilson 40665cabbc6bSPrashanth Sreenivasa boolean_t 40675cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd) 40685cabbc6bSPrashanth Sreenivasa { 40695cabbc6bSPrashanth Sreenivasa vdev_ops_t *ops = vd->vdev_ops; 40705cabbc6bSPrashanth Sreenivasa if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 40715cabbc6bSPrashanth Sreenivasa ops == &vdev_missing_ops || ops == &vdev_root_ops) { 40725cabbc6bSPrashanth Sreenivasa return (B_FALSE); 40735cabbc6bSPrashanth Sreenivasa } else { 40745cabbc6bSPrashanth Sreenivasa return (B_TRUE); 40755cabbc6bSPrashanth Sreenivasa } 40765cabbc6bSPrashanth Sreenivasa } 40775cabbc6bSPrashanth Sreenivasa 40784b964adaSGeorge Wilson /* 40794b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 40804b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 40814b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 40824b964adaSGeorge Wilson */ 40834b964adaSGeorge Wilson boolean_t 40844b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 40854b964adaSGeorge Wilson { 40864b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 40874b964adaSGeorge Wilson !vd->vdev_removed) 40884b964adaSGeorge Wilson return (B_TRUE); 40894b964adaSGeorge Wilson 40904b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 40914b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 40924b964adaSGeorge Wilson return (B_TRUE); 40934b964adaSGeorge Wilson 40944b964adaSGeorge Wilson return (B_FALSE); 40954b964adaSGeorge Wilson } 40964b964adaSGeorge Wilson 4097573ca77eSGeorge Wilson /* 4098573ca77eSGeorge Wilson * Expand a vdev if possible. 4099573ca77eSGeorge Wilson */ 4100573ca77eSGeorge Wilson void 4101573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 4102573ca77eSGeorge Wilson { 4103573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 4104573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 410511f6a968SSerapheim Dimitropoulos ASSERT(vdev_is_concrete(vd)); 4106573ca77eSGeorge Wilson 41075cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 41085cabbc6bSPrashanth Sreenivasa 410911f6a968SSerapheim Dimitropoulos if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 4110573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 4111573ca77eSGeorge Wilson vdev_config_dirty(vd); 4112573ca77eSGeorge Wilson } 4113573ca77eSGeorge Wilson } 41141195e687SMark J Musante 41151195e687SMark J Musante /* 41161195e687SMark J Musante * Split a vdev. 41171195e687SMark J Musante */ 41181195e687SMark J Musante void 41191195e687SMark J Musante vdev_split(vdev_t *vd) 41201195e687SMark J Musante { 41211195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 41221195e687SMark J Musante 41231195e687SMark J Musante vdev_remove_child(pvd, vd); 41241195e687SMark J Musante vdev_compact_children(pvd); 41251195e687SMark J Musante 41261195e687SMark J Musante cvd = pvd->vdev_child[0]; 41271195e687SMark J Musante if (pvd->vdev_children == 1) { 41281195e687SMark J Musante vdev_remove_parent(cvd); 41291195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 41301195e687SMark J Musante } 41311195e687SMark J Musante vdev_propagate_state(cvd); 41321195e687SMark J Musante } 4133283b8460SGeorge.Wilson 4134283b8460SGeorge.Wilson void 4135283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 4136283b8460SGeorge.Wilson { 4137283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 4138283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 4139283b8460SGeorge.Wilson 4140283b8460SGeorge.Wilson vdev_deadman(cvd); 4141283b8460SGeorge.Wilson } 4142283b8460SGeorge.Wilson 4143283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 4144283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 4145283b8460SGeorge.Wilson 4146283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 414769962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 4148283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 4149283b8460SGeorge.Wilson zio_t *fio; 4150283b8460SGeorge.Wilson uint64_t delta; 4151283b8460SGeorge.Wilson 4152283b8460SGeorge.Wilson /* 4153283b8460SGeorge.Wilson * Look at the head of all the pending queues, 4154283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 4155283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 4156283b8460SGeorge.Wilson */ 415769962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 4158c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 4159c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 41603ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 41613ee8c80cSPavel Zakharov "%lluns, delta %lluns, last io %lluns", 41623ee8c80cSPavel Zakharov fio->io_timestamp, (u_longlong_t)delta, 4163283b8460SGeorge.Wilson vq->vq_io_complete_ts); 4164283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 4165283b8460SGeorge.Wilson "hung.", spa_name(spa)); 4166283b8460SGeorge.Wilson } 4167283b8460SGeorge.Wilson } 4168283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 4169283b8460SGeorge.Wilson } 4170283b8460SGeorge.Wilson } 4171