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 7586714001SSerapheim Dimitropoulos /* maximum number of metaslabs per top-level vdev */ 7686714001SSerapheim Dimitropoulos int vdev_max_ms_count = 200; 7786714001SSerapheim Dimitropoulos 7886714001SSerapheim Dimitropoulos /* minimum amount of metaslabs per top-level vdev */ 7986714001SSerapheim Dimitropoulos int vdev_min_ms_count = 16; 8086714001SSerapheim Dimitropoulos 8186714001SSerapheim Dimitropoulos /* see comment in vdev_metaslab_set_size() */ 8286714001SSerapheim Dimitropoulos int vdev_default_ms_shift = 29; 8386714001SSerapheim Dimitropoulos 8486714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE; 8586714001SSerapheim Dimitropoulos 86bf3e216cSMatthew Ahrens /* 8786714001SSerapheim Dimitropoulos * Since the DTL space map of a vdev is not expected to have a lot of 8886714001SSerapheim Dimitropoulos * entries, we default its block size to 4K. 89bf3e216cSMatthew Ahrens */ 9086714001SSerapheim Dimitropoulos int vdev_dtl_sm_blksz = (1 << 12); 91bf3e216cSMatthew Ahrens 9286714001SSerapheim Dimitropoulos /* 9386714001SSerapheim Dimitropoulos * vdev-wide space maps that have lots of entries written to them at 9486714001SSerapheim Dimitropoulos * the end of each transaction can benefit from a higher I/O bandwidth 9586714001SSerapheim Dimitropoulos * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 9686714001SSerapheim Dimitropoulos */ 9786714001SSerapheim Dimitropoulos int vdev_standard_sm_blksz = (1 << 17); 986f793812SPavel Zakharov 99*93a1902eSMatthew Ahrens int zfs_ashift_min; 100*93a1902eSMatthew Ahrens 1013ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 1023ee8c80cSPavel Zakharov void 1033ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 1043ee8c80cSPavel Zakharov { 1053ee8c80cSPavel Zakharov va_list adx; 1063ee8c80cSPavel Zakharov char buf[256]; 1073ee8c80cSPavel Zakharov 1083ee8c80cSPavel Zakharov va_start(adx, fmt); 1093ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 1103ee8c80cSPavel Zakharov va_end(adx); 1113ee8c80cSPavel Zakharov 1123ee8c80cSPavel Zakharov if (vd->vdev_path != NULL) { 1133ee8c80cSPavel Zakharov zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 1143ee8c80cSPavel Zakharov vd->vdev_path, buf); 1153ee8c80cSPavel Zakharov } else { 1163ee8c80cSPavel Zakharov zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 1173ee8c80cSPavel Zakharov vd->vdev_ops->vdev_op_type, 1183ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_id, 1193ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_guid, buf); 1203ee8c80cSPavel Zakharov } 1213ee8c80cSPavel Zakharov } 1223ee8c80cSPavel Zakharov 1236f793812SPavel Zakharov void 1246f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 1256f793812SPavel Zakharov { 1266f793812SPavel Zakharov char state[20]; 1276f793812SPavel Zakharov 1286f793812SPavel Zakharov if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 1296f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 1306f793812SPavel Zakharov vd->vdev_ops->vdev_op_type); 1316f793812SPavel Zakharov return; 1326f793812SPavel Zakharov } 1336f793812SPavel Zakharov 1346f793812SPavel Zakharov switch (vd->vdev_state) { 1356f793812SPavel Zakharov case VDEV_STATE_UNKNOWN: 1366f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "unknown"); 1376f793812SPavel Zakharov break; 1386f793812SPavel Zakharov case VDEV_STATE_CLOSED: 1396f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "closed"); 1406f793812SPavel Zakharov break; 1416f793812SPavel Zakharov case VDEV_STATE_OFFLINE: 1426f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "offline"); 1436f793812SPavel Zakharov break; 1446f793812SPavel Zakharov case VDEV_STATE_REMOVED: 1456f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "removed"); 1466f793812SPavel Zakharov break; 1476f793812SPavel Zakharov case VDEV_STATE_CANT_OPEN: 1486f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "can't open"); 1496f793812SPavel Zakharov break; 1506f793812SPavel Zakharov case VDEV_STATE_FAULTED: 1516f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "faulted"); 1526f793812SPavel Zakharov break; 1536f793812SPavel Zakharov case VDEV_STATE_DEGRADED: 1546f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "degraded"); 1556f793812SPavel Zakharov break; 1566f793812SPavel Zakharov case VDEV_STATE_HEALTHY: 1576f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "healthy"); 1586f793812SPavel Zakharov break; 1596f793812SPavel Zakharov default: 1606f793812SPavel Zakharov (void) snprintf(state, sizeof (state), "<state %u>", 1616f793812SPavel Zakharov (uint_t)vd->vdev_state); 1626f793812SPavel Zakharov } 1636f793812SPavel Zakharov 1646f793812SPavel Zakharov zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 1656f793812SPavel Zakharov "", vd->vdev_id, vd->vdev_ops->vdev_op_type, 1666f793812SPavel Zakharov vd->vdev_islog ? " (log)" : "", 1676f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 1686f793812SPavel Zakharov vd->vdev_path ? vd->vdev_path : "N/A", state); 1696f793812SPavel Zakharov 1706f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) 1716f793812SPavel Zakharov vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 1726f793812SPavel Zakharov } 1736f793812SPavel Zakharov 174fa9e4066Sahrens /* 175fa9e4066Sahrens * Given a vdev type, return the appropriate ops vector. 176fa9e4066Sahrens */ 177fa9e4066Sahrens static vdev_ops_t * 178fa9e4066Sahrens vdev_getops(const char *type) 179fa9e4066Sahrens { 180fa9e4066Sahrens vdev_ops_t *ops, **opspp; 181fa9e4066Sahrens 182fa9e4066Sahrens for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 183fa9e4066Sahrens if (strcmp(ops->vdev_op_type, type) == 0) 184fa9e4066Sahrens break; 185fa9e4066Sahrens 186fa9e4066Sahrens return (ops); 187fa9e4066Sahrens } 188fa9e4066Sahrens 189094e47e9SGeorge Wilson /* ARGSUSED */ 190094e47e9SGeorge Wilson void 191094e47e9SGeorge Wilson vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res) 192094e47e9SGeorge Wilson { 193094e47e9SGeorge Wilson res->rs_start = in->rs_start; 194094e47e9SGeorge Wilson res->rs_end = in->rs_end; 195094e47e9SGeorge Wilson } 196094e47e9SGeorge Wilson 197fa9e4066Sahrens /* 198fa9e4066Sahrens * Default asize function: return the MAX of psize with the asize of 199fa9e4066Sahrens * all children. This is what's used by anything other than RAID-Z. 200fa9e4066Sahrens */ 201fa9e4066Sahrens uint64_t 202fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize) 203fa9e4066Sahrens { 204ecc2d604Sbonwick uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 205fa9e4066Sahrens uint64_t csize; 206fa9e4066Sahrens 207573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 208fa9e4066Sahrens csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 209fa9e4066Sahrens asize = MAX(asize, csize); 210fa9e4066Sahrens } 211fa9e4066Sahrens 212fa9e4066Sahrens return (asize); 213fa9e4066Sahrens } 214fa9e4066Sahrens 2152a79c5feSlling /* 216573ca77eSGeorge Wilson * Get the minimum allocatable size. We define the allocatable size as 217573ca77eSGeorge Wilson * the vdev's asize rounded to the nearest metaslab. This allows us to 218573ca77eSGeorge Wilson * replace or attach devices which don't have the same physical size but 219573ca77eSGeorge Wilson * can still satisfy the same number of allocations. 2202a79c5feSlling */ 2212a79c5feSlling uint64_t 222573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd) 2232a79c5feSlling { 224573ca77eSGeorge Wilson vdev_t *pvd = vd->vdev_parent; 2252a79c5feSlling 226573ca77eSGeorge Wilson /* 2274263d13fSGeorge Wilson * If our parent is NULL (inactive spare or cache) or is the root, 228573ca77eSGeorge Wilson * just return our own asize. 229573ca77eSGeorge Wilson */ 230573ca77eSGeorge Wilson if (pvd == NULL) 231573ca77eSGeorge Wilson return (vd->vdev_asize); 2322a79c5feSlling 2332a79c5feSlling /* 234573ca77eSGeorge Wilson * The top-level vdev just returns the allocatable size rounded 235573ca77eSGeorge Wilson * to the nearest metaslab. 2362a79c5feSlling */ 237573ca77eSGeorge Wilson if (vd == vd->vdev_top) 238573ca77eSGeorge Wilson return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 2392a79c5feSlling 240573ca77eSGeorge Wilson /* 241573ca77eSGeorge Wilson * The allocatable space for a raidz vdev is N * sizeof(smallest child), 242573ca77eSGeorge Wilson * so each child must provide at least 1/Nth of its asize. 243573ca77eSGeorge Wilson */ 244573ca77eSGeorge Wilson if (pvd->vdev_ops == &vdev_raidz_ops) 245c040c10cSSteven Hartland return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 246c040c10cSSteven Hartland pvd->vdev_children); 2472a79c5feSlling 248573ca77eSGeorge Wilson return (pvd->vdev_min_asize); 249573ca77eSGeorge Wilson } 2502a79c5feSlling 251573ca77eSGeorge Wilson void 252573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd) 253573ca77eSGeorge Wilson { 254573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 255573ca77eSGeorge Wilson 256573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 257573ca77eSGeorge Wilson vdev_set_min_asize(vd->vdev_child[c]); 2582a79c5feSlling } 2592a79c5feSlling 260fa9e4066Sahrens vdev_t * 261fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev) 262fa9e4066Sahrens { 263fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 264fa9e4066Sahrens 265e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 266e05725b1Sbonwick 267088f3894Sahrens if (vdev < rvd->vdev_children) { 268088f3894Sahrens ASSERT(rvd->vdev_child[vdev] != NULL); 269fa9e4066Sahrens return (rvd->vdev_child[vdev]); 270088f3894Sahrens } 271fa9e4066Sahrens 272fa9e4066Sahrens return (NULL); 273fa9e4066Sahrens } 274fa9e4066Sahrens 275fa9e4066Sahrens vdev_t * 276fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 277fa9e4066Sahrens { 278fa9e4066Sahrens vdev_t *mvd; 279fa9e4066Sahrens 2800e34b6a7Sbonwick if (vd->vdev_guid == guid) 281fa9e4066Sahrens return (vd); 282fa9e4066Sahrens 283573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 284fa9e4066Sahrens if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 285fa9e4066Sahrens NULL) 286fa9e4066Sahrens return (mvd); 287fa9e4066Sahrens 288fa9e4066Sahrens return (NULL); 289fa9e4066Sahrens } 290fa9e4066Sahrens 29112380e1eSArne Jansen static int 29212380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd) 29312380e1eSArne Jansen { 29412380e1eSArne Jansen int n = 0; 29512380e1eSArne Jansen 29612380e1eSArne Jansen if (vd->vdev_ops->vdev_op_leaf) 29712380e1eSArne Jansen return (1); 29812380e1eSArne Jansen 29912380e1eSArne Jansen for (int c = 0; c < vd->vdev_children; c++) 30012380e1eSArne Jansen n += vdev_count_leaves_impl(vd->vdev_child[c]); 30112380e1eSArne Jansen 30212380e1eSArne Jansen return (n); 30312380e1eSArne Jansen } 30412380e1eSArne Jansen 30512380e1eSArne Jansen int 30612380e1eSArne Jansen vdev_count_leaves(spa_t *spa) 30712380e1eSArne Jansen { 30812380e1eSArne Jansen return (vdev_count_leaves_impl(spa->spa_root_vdev)); 30912380e1eSArne Jansen } 31012380e1eSArne Jansen 311fa9e4066Sahrens void 312fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd) 313fa9e4066Sahrens { 314fa9e4066Sahrens size_t oldsize, newsize; 315fa9e4066Sahrens uint64_t id = cvd->vdev_id; 316fa9e4066Sahrens vdev_t **newchild; 31781cd5c55SMatthew Ahrens spa_t *spa = cvd->vdev_spa; 318fa9e4066Sahrens 31981cd5c55SMatthew Ahrens ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 320fa9e4066Sahrens ASSERT(cvd->vdev_parent == NULL); 321fa9e4066Sahrens 322fa9e4066Sahrens cvd->vdev_parent = pvd; 323fa9e4066Sahrens 324fa9e4066Sahrens if (pvd == NULL) 325fa9e4066Sahrens return; 326fa9e4066Sahrens 327fa9e4066Sahrens ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 328fa9e4066Sahrens 329fa9e4066Sahrens oldsize = pvd->vdev_children * sizeof (vdev_t *); 330fa9e4066Sahrens pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 331fa9e4066Sahrens newsize = pvd->vdev_children * sizeof (vdev_t *); 332fa9e4066Sahrens 333fa9e4066Sahrens newchild = kmem_zalloc(newsize, KM_SLEEP); 334fa9e4066Sahrens if (pvd->vdev_child != NULL) { 335fa9e4066Sahrens bcopy(pvd->vdev_child, newchild, oldsize); 336fa9e4066Sahrens kmem_free(pvd->vdev_child, oldsize); 337fa9e4066Sahrens } 338fa9e4066Sahrens 339fa9e4066Sahrens pvd->vdev_child = newchild; 340fa9e4066Sahrens pvd->vdev_child[id] = cvd; 341fa9e4066Sahrens 342fa9e4066Sahrens cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 343fa9e4066Sahrens ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 344fa9e4066Sahrens 345fa9e4066Sahrens /* 346fa9e4066Sahrens * Walk up all ancestors to update guid sum. 347fa9e4066Sahrens */ 348fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 349fa9e4066Sahrens pvd->vdev_guid_sum += cvd->vdev_guid_sum; 350fa9e4066Sahrens } 351fa9e4066Sahrens 352fa9e4066Sahrens void 353fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 354fa9e4066Sahrens { 355fa9e4066Sahrens int c; 356fa9e4066Sahrens uint_t id = cvd->vdev_id; 357fa9e4066Sahrens 358fa9e4066Sahrens ASSERT(cvd->vdev_parent == pvd); 359fa9e4066Sahrens 360fa9e4066Sahrens if (pvd == NULL) 361fa9e4066Sahrens return; 362fa9e4066Sahrens 363fa9e4066Sahrens ASSERT(id < pvd->vdev_children); 364fa9e4066Sahrens ASSERT(pvd->vdev_child[id] == cvd); 365fa9e4066Sahrens 366fa9e4066Sahrens pvd->vdev_child[id] = NULL; 367fa9e4066Sahrens cvd->vdev_parent = NULL; 368fa9e4066Sahrens 369fa9e4066Sahrens for (c = 0; c < pvd->vdev_children; c++) 370fa9e4066Sahrens if (pvd->vdev_child[c]) 371fa9e4066Sahrens break; 372fa9e4066Sahrens 373fa9e4066Sahrens if (c == pvd->vdev_children) { 374fa9e4066Sahrens kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 375fa9e4066Sahrens pvd->vdev_child = NULL; 376fa9e4066Sahrens pvd->vdev_children = 0; 377fa9e4066Sahrens } 378fa9e4066Sahrens 379fa9e4066Sahrens /* 380fa9e4066Sahrens * Walk up all ancestors to update guid sum. 381fa9e4066Sahrens */ 382fa9e4066Sahrens for (; pvd != NULL; pvd = pvd->vdev_parent) 383fa9e4066Sahrens pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 384fa9e4066Sahrens } 385fa9e4066Sahrens 386fa9e4066Sahrens /* 387fa9e4066Sahrens * Remove any holes in the child array. 388fa9e4066Sahrens */ 389fa9e4066Sahrens void 390fa9e4066Sahrens vdev_compact_children(vdev_t *pvd) 391fa9e4066Sahrens { 392fa9e4066Sahrens vdev_t **newchild, *cvd; 393fa9e4066Sahrens int oldc = pvd->vdev_children; 394573ca77eSGeorge Wilson int newc; 395fa9e4066Sahrens 396e14bb325SJeff Bonwick ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 397fa9e4066Sahrens 398573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) 399fa9e4066Sahrens if (pvd->vdev_child[c]) 400fa9e4066Sahrens newc++; 401fa9e4066Sahrens 402fa9e4066Sahrens newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 403fa9e4066Sahrens 404573ca77eSGeorge Wilson for (int c = newc = 0; c < oldc; c++) { 405fa9e4066Sahrens if ((cvd = pvd->vdev_child[c]) != NULL) { 406fa9e4066Sahrens newchild[newc] = cvd; 407fa9e4066Sahrens cvd->vdev_id = newc++; 408fa9e4066Sahrens } 409fa9e4066Sahrens } 410fa9e4066Sahrens 411fa9e4066Sahrens kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 412fa9e4066Sahrens pvd->vdev_child = newchild; 413fa9e4066Sahrens pvd->vdev_children = newc; 414fa9e4066Sahrens } 415fa9e4066Sahrens 416fa9e4066Sahrens /* 417fa9e4066Sahrens * Allocate and minimally initialize a vdev_t. 418fa9e4066Sahrens */ 41988ecc943SGeorge Wilson vdev_t * 420fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 421fa9e4066Sahrens { 422fa9e4066Sahrens vdev_t *vd; 4235cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 424fa9e4066Sahrens 425fa9e4066Sahrens vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 4265cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 427fa9e4066Sahrens 4280e34b6a7Sbonwick if (spa->spa_root_vdev == NULL) { 4290e34b6a7Sbonwick ASSERT(ops == &vdev_root_ops); 4300e34b6a7Sbonwick spa->spa_root_vdev = vd; 431e9103aaeSGarrett D'Amore spa->spa_load_guid = spa_generate_guid(NULL); 4320e34b6a7Sbonwick } 4330e34b6a7Sbonwick 43488ecc943SGeorge Wilson if (guid == 0 && ops != &vdev_hole_ops) { 4350e34b6a7Sbonwick if (spa->spa_root_vdev == vd) { 4360e34b6a7Sbonwick /* 4370e34b6a7Sbonwick * The root vdev's guid will also be the pool guid, 4380e34b6a7Sbonwick * which must be unique among all pools. 4390e34b6a7Sbonwick */ 4401195e687SMark J Musante guid = spa_generate_guid(NULL); 4410e34b6a7Sbonwick } else { 4420e34b6a7Sbonwick /* 4430e34b6a7Sbonwick * Any other vdev's guid must be unique within the pool. 4440e34b6a7Sbonwick */ 4451195e687SMark J Musante guid = spa_generate_guid(spa); 4460e34b6a7Sbonwick } 4470e34b6a7Sbonwick ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 4480e34b6a7Sbonwick } 4490e34b6a7Sbonwick 450fa9e4066Sahrens vd->vdev_spa = spa; 451fa9e4066Sahrens vd->vdev_id = id; 452fa9e4066Sahrens vd->vdev_guid = guid; 453fa9e4066Sahrens vd->vdev_guid_sum = guid; 454fa9e4066Sahrens vd->vdev_ops = ops; 455fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 45688ecc943SGeorge Wilson vd->vdev_ishole = (ops == &vdev_hole_ops); 4575cabbc6bSPrashanth Sreenivasa vic->vic_prev_indirect_vdev = UINT64_MAX; 4585cabbc6bSPrashanth Sreenivasa 4595cabbc6bSPrashanth Sreenivasa rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 4605cabbc6bSPrashanth Sreenivasa mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 4615cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); 462fa9e4066Sahrens 463fa9e4066Sahrens mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 4645ad82045Snd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 465e14bb325SJeff Bonwick mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 4660f7643c7SGeorge Wilson mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 467094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); 468094e47e9SGeorge Wilson mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); 469094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); 470094e47e9SGeorge Wilson cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); 471094e47e9SGeorge Wilson 4728ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 4735cabbc6bSPrashanth Sreenivasa vd->vdev_dtl[t] = range_tree_create(NULL, NULL); 4748ad4d6ddSJeff Bonwick } 475b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_ms_list, spa, 476fa9e4066Sahrens offsetof(struct metaslab, ms_txg_node)); 477b7b2590dSMatthew Ahrens txg_list_create(&vd->vdev_dtl_list, spa, 478fa9e4066Sahrens offsetof(struct vdev, vdev_dtl_node)); 479fa9e4066Sahrens vd->vdev_stat.vs_timestamp = gethrtime(); 4803d7072f8Seschrock vdev_queue_init(vd); 4813d7072f8Seschrock vdev_cache_init(vd); 482fa9e4066Sahrens 483fa9e4066Sahrens return (vd); 484fa9e4066Sahrens } 485fa9e4066Sahrens 486fa9e4066Sahrens /* 487fa9e4066Sahrens * Allocate a new vdev. The 'alloctype' is used to control whether we are 488fa9e4066Sahrens * creating a new vdev or loading an existing one - the behavior is slightly 489fa9e4066Sahrens * different for each case. 490fa9e4066Sahrens */ 49199653d4eSeschrock int 49299653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 49399653d4eSeschrock int alloctype) 494fa9e4066Sahrens { 495fa9e4066Sahrens vdev_ops_t *ops; 496fa9e4066Sahrens char *type; 4978654d025Sperrin uint64_t guid = 0, islog, nparity; 498fa9e4066Sahrens vdev_t *vd; 4995cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic; 500fa9e4066Sahrens 501e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 502fa9e4066Sahrens 503fa9e4066Sahrens if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 504be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 505fa9e4066Sahrens 506fa9e4066Sahrens if ((ops = vdev_getops(type)) == NULL) 507be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 508fa9e4066Sahrens 509fa9e4066Sahrens /* 510fa9e4066Sahrens * If this is a load, get the vdev guid from the nvlist. 511fa9e4066Sahrens * Otherwise, vdev_alloc_common() will generate one for us. 512fa9e4066Sahrens */ 513fa9e4066Sahrens if (alloctype == VDEV_ALLOC_LOAD) { 514fa9e4066Sahrens uint64_t label_id; 515fa9e4066Sahrens 516fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 517fa9e4066Sahrens label_id != id) 518be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 519fa9e4066Sahrens 520fa9e4066Sahrens if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 521be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 52299653d4eSeschrock } else if (alloctype == VDEV_ALLOC_SPARE) { 52399653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 524be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 525fa94a07fSbrendan } else if (alloctype == VDEV_ALLOC_L2CACHE) { 526fa94a07fSbrendan if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 527be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 52821ecdf64SLin Ling } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 52921ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 530be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 531fa9e4066Sahrens } 532fa9e4066Sahrens 53399653d4eSeschrock /* 53499653d4eSeschrock * The first allocated vdev must be of type 'root'. 53599653d4eSeschrock */ 53699653d4eSeschrock if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 537be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 53899653d4eSeschrock 5398654d025Sperrin /* 5408654d025Sperrin * Determine whether we're a log vdev. 5418654d025Sperrin */ 5428654d025Sperrin islog = 0; 5438654d025Sperrin (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 544990b4856Slling if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 545be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 546fa9e4066Sahrens 54788ecc943SGeorge Wilson if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 548be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 54988ecc943SGeorge Wilson 55099653d4eSeschrock /* 5518654d025Sperrin * Set the nparity property for RAID-Z vdevs. 55299653d4eSeschrock */ 5538654d025Sperrin nparity = -1ULL; 55499653d4eSeschrock if (ops == &vdev_raidz_ops) { 55599653d4eSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 5568654d025Sperrin &nparity) == 0) { 557b24ab676SJeff Bonwick if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 558be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 55999653d4eSeschrock /* 560f94275ceSAdam Leventhal * Previous versions could only support 1 or 2 parity 561f94275ceSAdam Leventhal * device. 56299653d4eSeschrock */ 563f94275ceSAdam Leventhal if (nparity > 1 && 564f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ2) 565be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 566f94275ceSAdam Leventhal if (nparity > 2 && 567f94275ceSAdam Leventhal spa_version(spa) < SPA_VERSION_RAIDZ3) 568be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 56999653d4eSeschrock } else { 57099653d4eSeschrock /* 57199653d4eSeschrock * We require the parity to be specified for SPAs that 57299653d4eSeschrock * support multiple parity levels. 57399653d4eSeschrock */ 574f94275ceSAdam Leventhal if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 575be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 57699653d4eSeschrock /* 57799653d4eSeschrock * Otherwise, we default to 1 parity device for RAID-Z. 57899653d4eSeschrock */ 5798654d025Sperrin nparity = 1; 58099653d4eSeschrock } 58199653d4eSeschrock } else { 5828654d025Sperrin nparity = 0; 58399653d4eSeschrock } 5848654d025Sperrin ASSERT(nparity != -1ULL); 5858654d025Sperrin 5868654d025Sperrin vd = vdev_alloc_common(spa, id, guid, ops); 5875cabbc6bSPrashanth Sreenivasa vic = &vd->vdev_indirect_config; 5888654d025Sperrin 5898654d025Sperrin vd->vdev_islog = islog; 5908654d025Sperrin vd->vdev_nparity = nparity; 5918654d025Sperrin 5928654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 5938654d025Sperrin vd->vdev_path = spa_strdup(vd->vdev_path); 5948654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 5958654d025Sperrin vd->vdev_devid = spa_strdup(vd->vdev_devid); 5968654d025Sperrin if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 5978654d025Sperrin &vd->vdev_physpath) == 0) 5988654d025Sperrin vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 5996809eb4eSEric Schrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 6006809eb4eSEric Schrock vd->vdev_fru = spa_strdup(vd->vdev_fru); 60199653d4eSeschrock 602afefbcddSeschrock /* 603afefbcddSeschrock * Set the whole_disk property. If it's not specified, leave the value 604afefbcddSeschrock * as -1. 605afefbcddSeschrock */ 606afefbcddSeschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 607afefbcddSeschrock &vd->vdev_wholedisk) != 0) 608afefbcddSeschrock vd->vdev_wholedisk = -1ULL; 609afefbcddSeschrock 6105cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_mapping_object); 6115cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 6125cabbc6bSPrashanth Sreenivasa &vic->vic_mapping_object); 6135cabbc6bSPrashanth Sreenivasa ASSERT0(vic->vic_births_object); 6145cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 6155cabbc6bSPrashanth Sreenivasa &vic->vic_births_object); 6165cabbc6bSPrashanth Sreenivasa ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 6175cabbc6bSPrashanth Sreenivasa (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 6185cabbc6bSPrashanth Sreenivasa &vic->vic_prev_indirect_vdev); 6195cabbc6bSPrashanth Sreenivasa 620ea8dc4b6Seschrock /* 621ea8dc4b6Seschrock * Look for the 'not present' flag. This will only be set if the device 622ea8dc4b6Seschrock * was not present at the time of import. 623ea8dc4b6Seschrock */ 6246809eb4eSEric Schrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 6256809eb4eSEric Schrock &vd->vdev_not_present); 626ea8dc4b6Seschrock 627ecc2d604Sbonwick /* 628ecc2d604Sbonwick * Get the alignment requirement. 629ecc2d604Sbonwick */ 630ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 631ecc2d604Sbonwick 63288ecc943SGeorge Wilson /* 63388ecc943SGeorge Wilson * Retrieve the vdev creation time. 63488ecc943SGeorge Wilson */ 63588ecc943SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 63688ecc943SGeorge Wilson &vd->vdev_crtxg); 63788ecc943SGeorge Wilson 638fa9e4066Sahrens /* 639fa9e4066Sahrens * If we're a top-level vdev, try to load the allocation parameters. 640fa9e4066Sahrens */ 6411195e687SMark J Musante if (parent && !parent->vdev_parent && 6421195e687SMark J Musante (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 643fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 644fa9e4066Sahrens &vd->vdev_ms_array); 645fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 646fa9e4066Sahrens &vd->vdev_ms_shift); 647fa9e4066Sahrens (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 648fa9e4066Sahrens &vd->vdev_asize); 6493f9d6ad7SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 6503f9d6ad7SLin Ling &vd->vdev_removing); 651215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 652215198a6SJoe Stein &vd->vdev_top_zap); 653215198a6SJoe Stein } else { 654215198a6SJoe Stein ASSERT0(vd->vdev_top_zap); 655fa9e4066Sahrens } 656fa9e4066Sahrens 657cd0837ccSGeorge Wilson if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 658a1521560SJeff Bonwick ASSERT(alloctype == VDEV_ALLOC_LOAD || 6599f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ADD || 6601195e687SMark J Musante alloctype == VDEV_ALLOC_SPLIT || 6619f4ab4d8SGeorge Wilson alloctype == VDEV_ALLOC_ROOTPOOL); 662a1521560SJeff Bonwick vd->vdev_mg = metaslab_group_create(islog ? 663f78cdc34SPaul Dagnelie spa_log_class(spa) : spa_normal_class(spa), vd, 664f78cdc34SPaul Dagnelie spa->spa_alloc_count); 665a1521560SJeff Bonwick } 666a1521560SJeff Bonwick 667215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && 668215198a6SJoe Stein (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 669215198a6SJoe Stein (void) nvlist_lookup_uint64(nv, 670215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 671215198a6SJoe Stein } else { 672215198a6SJoe Stein ASSERT0(vd->vdev_leaf_zap); 673215198a6SJoe Stein } 674215198a6SJoe Stein 675fa9e4066Sahrens /* 6763d7072f8Seschrock * If we're a leaf vdev, try to load the DTL object and other state. 677fa9e4066Sahrens */ 678215198a6SJoe Stein 679c5904d13Seschrock if (vd->vdev_ops->vdev_op_leaf && 68021ecdf64SLin Ling (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 68121ecdf64SLin Ling alloctype == VDEV_ALLOC_ROOTPOOL)) { 682c5904d13Seschrock if (alloctype == VDEV_ALLOC_LOAD) { 683c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 6840713e232SGeorge Wilson &vd->vdev_dtl_object); 685c5904d13Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 686c5904d13Seschrock &vd->vdev_unspare); 687c5904d13Seschrock } 68821ecdf64SLin Ling 68921ecdf64SLin Ling if (alloctype == VDEV_ALLOC_ROOTPOOL) { 69021ecdf64SLin Ling uint64_t spare = 0; 69121ecdf64SLin Ling 69221ecdf64SLin Ling if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 69321ecdf64SLin Ling &spare) == 0 && spare) 69421ecdf64SLin Ling spa_spare_add(vd); 69521ecdf64SLin Ling } 69621ecdf64SLin Ling 697ecc2d604Sbonwick (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 698ecc2d604Sbonwick &vd->vdev_offline); 699c5904d13Seschrock 700b4952e17SGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 701b4952e17SGeorge Wilson &vd->vdev_resilver_txg); 702cb04b873SMark J Musante 7033d7072f8Seschrock /* 7043d7072f8Seschrock * When importing a pool, we want to ignore the persistent fault 7053d7072f8Seschrock * state, as the diagnosis made on another system may not be 706069f55e2SEric Schrock * valid in the current context. Local vdevs will 707069f55e2SEric Schrock * remain in the faulted state. 7083d7072f8Seschrock */ 709b16da2e2SGeorge Wilson if (spa_load_state(spa) == SPA_LOAD_OPEN) { 7103d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 7113d7072f8Seschrock &vd->vdev_faulted); 7123d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 7133d7072f8Seschrock &vd->vdev_degraded); 7143d7072f8Seschrock (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 7153d7072f8Seschrock &vd->vdev_removed); 716069f55e2SEric Schrock 717069f55e2SEric Schrock if (vd->vdev_faulted || vd->vdev_degraded) { 718069f55e2SEric Schrock char *aux; 719069f55e2SEric Schrock 720069f55e2SEric Schrock vd->vdev_label_aux = 721069f55e2SEric Schrock VDEV_AUX_ERR_EXCEEDED; 722069f55e2SEric Schrock if (nvlist_lookup_string(nv, 723069f55e2SEric Schrock ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 724069f55e2SEric Schrock strcmp(aux, "external") == 0) 725069f55e2SEric Schrock vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 726069f55e2SEric Schrock } 7273d7072f8Seschrock } 728fa9e4066Sahrens } 729fa9e4066Sahrens 730fa9e4066Sahrens /* 731fa9e4066Sahrens * Add ourselves to the parent's list of children. 732fa9e4066Sahrens */ 733fa9e4066Sahrens vdev_add_child(parent, vd); 734fa9e4066Sahrens 73599653d4eSeschrock *vdp = vd; 73699653d4eSeschrock 73799653d4eSeschrock return (0); 738fa9e4066Sahrens } 739fa9e4066Sahrens 740fa9e4066Sahrens void 741fa9e4066Sahrens vdev_free(vdev_t *vd) 742fa9e4066Sahrens { 7433d7072f8Seschrock spa_t *spa = vd->vdev_spa; 744094e47e9SGeorge Wilson ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 745fa9e4066Sahrens 746fa9e4066Sahrens /* 747fa9e4066Sahrens * vdev_free() implies closing the vdev first. This is simpler than 748fa9e4066Sahrens * trying to ensure complicated semantics for all callers. 749fa9e4066Sahrens */ 750fa9e4066Sahrens vdev_close(vd); 751fa9e4066Sahrens 752e14bb325SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 753b24ab676SJeff Bonwick ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 754fa9e4066Sahrens 755fa9e4066Sahrens /* 756fa9e4066Sahrens * Free all children. 757fa9e4066Sahrens */ 758573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 759fa9e4066Sahrens vdev_free(vd->vdev_child[c]); 760fa9e4066Sahrens 761fa9e4066Sahrens ASSERT(vd->vdev_child == NULL); 762fa9e4066Sahrens ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 763094e47e9SGeorge Wilson ASSERT(vd->vdev_initialize_thread == NULL); 764fa9e4066Sahrens 765fa9e4066Sahrens /* 766fa9e4066Sahrens * Discard allocation state. 767fa9e4066Sahrens */ 768a1521560SJeff Bonwick if (vd->vdev_mg != NULL) { 769fa9e4066Sahrens vdev_metaslab_fini(vd); 770a1521560SJeff Bonwick metaslab_group_destroy(vd->vdev_mg); 771a1521560SJeff Bonwick } 772fa9e4066Sahrens 773fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_space); 774fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_dspace); 775fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 776fa9e4066Sahrens 777fa9e4066Sahrens /* 778fa9e4066Sahrens * Remove this vdev from its parent's child list. 779fa9e4066Sahrens */ 780fa9e4066Sahrens vdev_remove_child(vd->vdev_parent, vd); 781fa9e4066Sahrens 782fa9e4066Sahrens ASSERT(vd->vdev_parent == NULL); 783fa9e4066Sahrens 7843d7072f8Seschrock /* 7853d7072f8Seschrock * Clean up vdev structure. 7863d7072f8Seschrock */ 7873d7072f8Seschrock vdev_queue_fini(vd); 7883d7072f8Seschrock vdev_cache_fini(vd); 7893d7072f8Seschrock 7903d7072f8Seschrock if (vd->vdev_path) 7913d7072f8Seschrock spa_strfree(vd->vdev_path); 7923d7072f8Seschrock if (vd->vdev_devid) 7933d7072f8Seschrock spa_strfree(vd->vdev_devid); 7943d7072f8Seschrock if (vd->vdev_physpath) 7953d7072f8Seschrock spa_strfree(vd->vdev_physpath); 7966809eb4eSEric Schrock if (vd->vdev_fru) 7976809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 7983d7072f8Seschrock 7993d7072f8Seschrock if (vd->vdev_isspare) 8003d7072f8Seschrock spa_spare_remove(vd); 801fa94a07fSbrendan if (vd->vdev_isl2cache) 802fa94a07fSbrendan spa_l2cache_remove(vd); 8033d7072f8Seschrock 8043d7072f8Seschrock txg_list_destroy(&vd->vdev_ms_list); 8053d7072f8Seschrock txg_list_destroy(&vd->vdev_dtl_list); 8068ad4d6ddSJeff Bonwick 8073d7072f8Seschrock mutex_enter(&vd->vdev_dtl_lock); 8080713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 8098ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 8100713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 8110713e232SGeorge Wilson range_tree_destroy(vd->vdev_dtl[t]); 8128ad4d6ddSJeff Bonwick } 8133d7072f8Seschrock mutex_exit(&vd->vdev_dtl_lock); 8148ad4d6ddSJeff Bonwick 8155cabbc6bSPrashanth Sreenivasa EQUIV(vd->vdev_indirect_births != NULL, 8165cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping != NULL); 8175cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_births != NULL) { 8185cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 8195cabbc6bSPrashanth Sreenivasa vdev_indirect_births_close(vd->vdev_indirect_births); 8205cabbc6bSPrashanth Sreenivasa } 8215cabbc6bSPrashanth Sreenivasa 8225cabbc6bSPrashanth Sreenivasa if (vd->vdev_obsolete_sm != NULL) { 8235cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 8245cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 8255cabbc6bSPrashanth Sreenivasa space_map_close(vd->vdev_obsolete_sm); 8265cabbc6bSPrashanth Sreenivasa vd->vdev_obsolete_sm = NULL; 8275cabbc6bSPrashanth Sreenivasa } 8285cabbc6bSPrashanth Sreenivasa range_tree_destroy(vd->vdev_obsolete_segments); 8295cabbc6bSPrashanth Sreenivasa rw_destroy(&vd->vdev_indirect_rwlock); 8305cabbc6bSPrashanth Sreenivasa mutex_destroy(&vd->vdev_obsolete_lock); 8315cabbc6bSPrashanth Sreenivasa 8320f7643c7SGeorge Wilson mutex_destroy(&vd->vdev_queue_lock); 8333d7072f8Seschrock mutex_destroy(&vd->vdev_dtl_lock); 8343d7072f8Seschrock mutex_destroy(&vd->vdev_stat_lock); 835e14bb325SJeff Bonwick mutex_destroy(&vd->vdev_probe_lock); 836094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_lock); 837094e47e9SGeorge Wilson mutex_destroy(&vd->vdev_initialize_io_lock); 838094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_io_cv); 839094e47e9SGeorge Wilson cv_destroy(&vd->vdev_initialize_cv); 8403d7072f8Seschrock 8413d7072f8Seschrock if (vd == spa->spa_root_vdev) 8423d7072f8Seschrock spa->spa_root_vdev = NULL; 8433d7072f8Seschrock 8443d7072f8Seschrock kmem_free(vd, sizeof (vdev_t)); 845fa9e4066Sahrens } 846fa9e4066Sahrens 847fa9e4066Sahrens /* 848fa9e4066Sahrens * Transfer top-level vdev state from svd to tvd. 849fa9e4066Sahrens */ 850fa9e4066Sahrens static void 851fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 852fa9e4066Sahrens { 853fa9e4066Sahrens spa_t *spa = svd->vdev_spa; 854fa9e4066Sahrens metaslab_t *msp; 855fa9e4066Sahrens vdev_t *vd; 856fa9e4066Sahrens int t; 857fa9e4066Sahrens 858fa9e4066Sahrens ASSERT(tvd == tvd->vdev_top); 859fa9e4066Sahrens 860fa9e4066Sahrens tvd->vdev_ms_array = svd->vdev_ms_array; 861fa9e4066Sahrens tvd->vdev_ms_shift = svd->vdev_ms_shift; 862fa9e4066Sahrens tvd->vdev_ms_count = svd->vdev_ms_count; 863215198a6SJoe Stein tvd->vdev_top_zap = svd->vdev_top_zap; 864fa9e4066Sahrens 865fa9e4066Sahrens svd->vdev_ms_array = 0; 866fa9e4066Sahrens svd->vdev_ms_shift = 0; 867fa9e4066Sahrens svd->vdev_ms_count = 0; 868215198a6SJoe Stein svd->vdev_top_zap = 0; 869fa9e4066Sahrens 870cd0837ccSGeorge Wilson if (tvd->vdev_mg) 871cd0837ccSGeorge Wilson ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 872fa9e4066Sahrens tvd->vdev_mg = svd->vdev_mg; 873fa9e4066Sahrens tvd->vdev_ms = svd->vdev_ms; 874fa9e4066Sahrens 875fa9e4066Sahrens svd->vdev_mg = NULL; 876fa9e4066Sahrens svd->vdev_ms = NULL; 877ecc2d604Sbonwick 878ecc2d604Sbonwick if (tvd->vdev_mg != NULL) 879ecc2d604Sbonwick tvd->vdev_mg->mg_vd = tvd; 880fa9e4066Sahrens 88186714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 88286714001SSerapheim Dimitropoulos svd->vdev_checkpoint_sm = NULL; 88386714001SSerapheim Dimitropoulos 884fa9e4066Sahrens tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 885fa9e4066Sahrens tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 88699653d4eSeschrock tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 887fa9e4066Sahrens 888fa9e4066Sahrens svd->vdev_stat.vs_alloc = 0; 889fa9e4066Sahrens svd->vdev_stat.vs_space = 0; 89099653d4eSeschrock svd->vdev_stat.vs_dspace = 0; 891fa9e4066Sahrens 8923a4b1be9SMatthew Ahrens /* 8933a4b1be9SMatthew Ahrens * State which may be set on a top-level vdev that's in the 8943a4b1be9SMatthew Ahrens * process of being removed. 8953a4b1be9SMatthew Ahrens */ 8963a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_births_object); 8973a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 8983a4b1be9SMatthew Ahrens ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 8993a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 9003a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 9013a4b1be9SMatthew Ahrens ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 9023a4b1be9SMatthew Ahrens ASSERT0(tvd->vdev_removing); 9033a4b1be9SMatthew Ahrens tvd->vdev_removing = svd->vdev_removing; 9043a4b1be9SMatthew Ahrens tvd->vdev_indirect_config = svd->vdev_indirect_config; 9053a4b1be9SMatthew Ahrens tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 9063a4b1be9SMatthew Ahrens tvd->vdev_indirect_births = svd->vdev_indirect_births; 9073a4b1be9SMatthew Ahrens range_tree_swap(&svd->vdev_obsolete_segments, 9083a4b1be9SMatthew Ahrens &tvd->vdev_obsolete_segments); 9093a4b1be9SMatthew Ahrens tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 9103a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_mapping_object = 0; 9113a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_births_object = 0; 9123a4b1be9SMatthew Ahrens svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 9133a4b1be9SMatthew Ahrens svd->vdev_indirect_mapping = NULL; 9143a4b1be9SMatthew Ahrens svd->vdev_indirect_births = NULL; 9153a4b1be9SMatthew Ahrens svd->vdev_obsolete_sm = NULL; 9163a4b1be9SMatthew Ahrens svd->vdev_removing = 0; 9173a4b1be9SMatthew Ahrens 918fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 919fa9e4066Sahrens while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 920fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 921fa9e4066Sahrens while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 922fa9e4066Sahrens (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 923fa9e4066Sahrens if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 924fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 925fa9e4066Sahrens } 926fa9e4066Sahrens 927e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_config_dirty_node)) { 928fa9e4066Sahrens vdev_config_clean(svd); 929fa9e4066Sahrens vdev_config_dirty(tvd); 930fa9e4066Sahrens } 931fa9e4066Sahrens 932e14bb325SJeff Bonwick if (list_link_active(&svd->vdev_state_dirty_node)) { 933e14bb325SJeff Bonwick vdev_state_clean(svd); 934e14bb325SJeff Bonwick vdev_state_dirty(tvd); 935e14bb325SJeff Bonwick } 936e14bb325SJeff Bonwick 93799653d4eSeschrock tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 93899653d4eSeschrock svd->vdev_deflate_ratio = 0; 9398654d025Sperrin 9408654d025Sperrin tvd->vdev_islog = svd->vdev_islog; 9418654d025Sperrin svd->vdev_islog = 0; 942fa9e4066Sahrens } 943fa9e4066Sahrens 944fa9e4066Sahrens static void 945fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd) 946fa9e4066Sahrens { 947fa9e4066Sahrens if (vd == NULL) 948fa9e4066Sahrens return; 949fa9e4066Sahrens 950fa9e4066Sahrens vd->vdev_top = tvd; 951fa9e4066Sahrens 952573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 953fa9e4066Sahrens vdev_top_update(tvd, vd->vdev_child[c]); 954fa9e4066Sahrens } 955fa9e4066Sahrens 956fa9e4066Sahrens /* 957fa9e4066Sahrens * Add a mirror/replacing vdev above an existing vdev. 958fa9e4066Sahrens */ 959fa9e4066Sahrens vdev_t * 960fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 961fa9e4066Sahrens { 962fa9e4066Sahrens spa_t *spa = cvd->vdev_spa; 963fa9e4066Sahrens vdev_t *pvd = cvd->vdev_parent; 964fa9e4066Sahrens vdev_t *mvd; 965fa9e4066Sahrens 966e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 967fa9e4066Sahrens 968fa9e4066Sahrens mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 969ecc2d604Sbonwick 970ecc2d604Sbonwick mvd->vdev_asize = cvd->vdev_asize; 971573ca77eSGeorge Wilson mvd->vdev_min_asize = cvd->vdev_min_asize; 9724263d13fSGeorge Wilson mvd->vdev_max_asize = cvd->vdev_max_asize; 9735cabbc6bSPrashanth Sreenivasa mvd->vdev_psize = cvd->vdev_psize; 974ecc2d604Sbonwick mvd->vdev_ashift = cvd->vdev_ashift; 975ecc2d604Sbonwick mvd->vdev_state = cvd->vdev_state; 97688ecc943SGeorge Wilson mvd->vdev_crtxg = cvd->vdev_crtxg; 977ecc2d604Sbonwick 978fa9e4066Sahrens vdev_remove_child(pvd, cvd); 979fa9e4066Sahrens vdev_add_child(pvd, mvd); 980fa9e4066Sahrens cvd->vdev_id = mvd->vdev_children; 981fa9e4066Sahrens vdev_add_child(mvd, cvd); 982fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 983fa9e4066Sahrens 984fa9e4066Sahrens if (mvd == mvd->vdev_top) 985fa9e4066Sahrens vdev_top_transfer(cvd, mvd); 986fa9e4066Sahrens 987fa9e4066Sahrens return (mvd); 988fa9e4066Sahrens } 989fa9e4066Sahrens 990fa9e4066Sahrens /* 991fa9e4066Sahrens * Remove a 1-way mirror/replacing vdev from the tree. 992fa9e4066Sahrens */ 993fa9e4066Sahrens void 994fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd) 995fa9e4066Sahrens { 996fa9e4066Sahrens vdev_t *mvd = cvd->vdev_parent; 997fa9e4066Sahrens vdev_t *pvd = mvd->vdev_parent; 998fa9e4066Sahrens 999e14bb325SJeff Bonwick ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1000fa9e4066Sahrens 1001fa9e4066Sahrens ASSERT(mvd->vdev_children == 1); 1002fa9e4066Sahrens ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 100399653d4eSeschrock mvd->vdev_ops == &vdev_replacing_ops || 100499653d4eSeschrock mvd->vdev_ops == &vdev_spare_ops); 1005ecc2d604Sbonwick cvd->vdev_ashift = mvd->vdev_ashift; 1006fa9e4066Sahrens 1007fa9e4066Sahrens vdev_remove_child(mvd, cvd); 1008fa9e4066Sahrens vdev_remove_child(pvd, mvd); 10098ad4d6ddSJeff Bonwick 101099653d4eSeschrock /* 1011e14bb325SJeff Bonwick * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1012e14bb325SJeff Bonwick * Otherwise, we could have detached an offline device, and when we 1013e14bb325SJeff Bonwick * go to import the pool we'll think we have two top-level vdevs, 1014e14bb325SJeff Bonwick * instead of a different version of the same top-level vdev. 101599653d4eSeschrock */ 10168ad4d6ddSJeff Bonwick if (mvd->vdev_top == mvd) { 10178ad4d6ddSJeff Bonwick uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 10181195e687SMark J Musante cvd->vdev_orig_guid = cvd->vdev_guid; 10198ad4d6ddSJeff Bonwick cvd->vdev_guid += guid_delta; 10208ad4d6ddSJeff Bonwick cvd->vdev_guid_sum += guid_delta; 10218ad4d6ddSJeff Bonwick } 1022e14bb325SJeff Bonwick cvd->vdev_id = mvd->vdev_id; 1023e14bb325SJeff Bonwick vdev_add_child(pvd, cvd); 1024fa9e4066Sahrens vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1025fa9e4066Sahrens 1026fa9e4066Sahrens if (cvd == cvd->vdev_top) 1027fa9e4066Sahrens vdev_top_transfer(mvd, cvd); 1028fa9e4066Sahrens 1029fa9e4066Sahrens ASSERT(mvd->vdev_children == 0); 1030fa9e4066Sahrens vdev_free(mvd); 1031fa9e4066Sahrens } 1032fa9e4066Sahrens 1033ea8dc4b6Seschrock int 1034fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1035fa9e4066Sahrens { 1036fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 1037ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 1038ecc2d604Sbonwick uint64_t m; 1039fa9e4066Sahrens uint64_t oldc = vd->vdev_ms_count; 1040fa9e4066Sahrens uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1041ecc2d604Sbonwick metaslab_t **mspp; 1042ecc2d604Sbonwick int error; 1043fa9e4066Sahrens 1044a1521560SJeff Bonwick ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1045a1521560SJeff Bonwick 104688ecc943SGeorge Wilson /* 104788ecc943SGeorge Wilson * This vdev is not being allocated from yet or is a hole. 104888ecc943SGeorge Wilson */ 104988ecc943SGeorge Wilson if (vd->vdev_ms_shift == 0) 10500e34b6a7Sbonwick return (0); 10510e34b6a7Sbonwick 105288ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 105388ecc943SGeorge Wilson 1054fa9e4066Sahrens ASSERT(oldc <= newc); 1055fa9e4066Sahrens 1056ecc2d604Sbonwick mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1057fa9e4066Sahrens 1058ecc2d604Sbonwick if (oldc != 0) { 1059ecc2d604Sbonwick bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1060ecc2d604Sbonwick kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1061ecc2d604Sbonwick } 1062fa9e4066Sahrens 1063ecc2d604Sbonwick vd->vdev_ms = mspp; 1064ecc2d604Sbonwick vd->vdev_ms_count = newc; 1065ecc2d604Sbonwick for (m = oldc; m < newc; m++) { 10660713e232SGeorge Wilson uint64_t object = 0; 10670713e232SGeorge Wilson 10685cabbc6bSPrashanth Sreenivasa /* 10695cabbc6bSPrashanth Sreenivasa * vdev_ms_array may be 0 if we are creating the "fake" 10705cabbc6bSPrashanth Sreenivasa * metaslabs for an indirect vdev for zdb's leak detection. 10715cabbc6bSPrashanth Sreenivasa * See zdb_leak_init(). 10725cabbc6bSPrashanth Sreenivasa */ 10735cabbc6bSPrashanth Sreenivasa if (txg == 0 && vd->vdev_ms_array != 0) { 1074ecc2d604Sbonwick error = dmu_read(mos, vd->vdev_ms_array, 10757bfdf011SNeil Perrin m * sizeof (uint64_t), sizeof (uint64_t), &object, 10767bfdf011SNeil Perrin DMU_READ_PREFETCH); 10773ee8c80cSPavel Zakharov if (error != 0) { 10783ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "unable to read the metaslab " 10793ee8c80cSPavel Zakharov "array [error=%d]", error); 1080ecc2d604Sbonwick return (error); 10813ee8c80cSPavel Zakharov } 1082fa9e4066Sahrens } 10831e9bd7ecSPrakash Surya 10841e9bd7ecSPrakash Surya error = metaslab_init(vd->vdev_mg, m, object, txg, 10851e9bd7ecSPrakash Surya &(vd->vdev_ms[m])); 10863ee8c80cSPavel Zakharov if (error != 0) { 10873ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 10883ee8c80cSPavel Zakharov error); 10891e9bd7ecSPrakash Surya return (error); 10903ee8c80cSPavel Zakharov } 1091fa9e4066Sahrens } 1092fa9e4066Sahrens 1093a1521560SJeff Bonwick if (txg == 0) 1094a1521560SJeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1095a1521560SJeff Bonwick 10963f9d6ad7SLin Ling /* 10973f9d6ad7SLin Ling * If the vdev is being removed we don't activate 10983f9d6ad7SLin Ling * the metaslabs since we want to ensure that no new 10993f9d6ad7SLin Ling * allocations are performed on this device. 11003f9d6ad7SLin Ling */ 11013f9d6ad7SLin Ling if (oldc == 0 && !vd->vdev_removing) 1102a1521560SJeff Bonwick metaslab_group_activate(vd->vdev_mg); 1103a1521560SJeff Bonwick 1104a1521560SJeff Bonwick if (txg == 0) 1105a1521560SJeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG); 1106a1521560SJeff Bonwick 1107ea8dc4b6Seschrock return (0); 1108fa9e4066Sahrens } 1109fa9e4066Sahrens 1110fa9e4066Sahrens void 1111fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd) 1112fa9e4066Sahrens { 111386714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 111486714001SSerapheim Dimitropoulos ASSERT(spa_feature_is_active(vd->vdev_spa, 111586714001SSerapheim Dimitropoulos SPA_FEATURE_POOL_CHECKPOINT)); 111686714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 111786714001SSerapheim Dimitropoulos /* 111886714001SSerapheim Dimitropoulos * Even though we close the space map, we need to set its 111986714001SSerapheim Dimitropoulos * pointer to NULL. The reason is that vdev_metaslab_fini() 112086714001SSerapheim Dimitropoulos * may be called multiple times for certain operations 112186714001SSerapheim Dimitropoulos * (i.e. when destroying a pool) so we need to ensure that 112286714001SSerapheim Dimitropoulos * this clause never executes twice. This logic is similar 112386714001SSerapheim Dimitropoulos * to the one used for the vdev_ms clause below. 112486714001SSerapheim Dimitropoulos */ 112586714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 112686714001SSerapheim Dimitropoulos } 112786714001SSerapheim Dimitropoulos 1128fa9e4066Sahrens if (vd->vdev_ms != NULL) { 11295cabbc6bSPrashanth Sreenivasa uint64_t count = vd->vdev_ms_count; 11305cabbc6bSPrashanth Sreenivasa 1131a1521560SJeff Bonwick metaslab_group_passivate(vd->vdev_mg); 11325cabbc6bSPrashanth Sreenivasa for (uint64_t m = 0; m < count; m++) { 11330713e232SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 11340713e232SGeorge Wilson 11350713e232SGeorge Wilson if (msp != NULL) 11360713e232SGeorge Wilson metaslab_fini(msp); 11370713e232SGeorge Wilson } 1138fa9e4066Sahrens kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1139fa9e4066Sahrens vd->vdev_ms = NULL; 11405cabbc6bSPrashanth Sreenivasa 11415cabbc6bSPrashanth Sreenivasa vd->vdev_ms_count = 0; 1142fa9e4066Sahrens } 11435cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_ms_count); 1144fa9e4066Sahrens } 1145fa9e4066Sahrens 1146e14bb325SJeff Bonwick typedef struct vdev_probe_stats { 1147e14bb325SJeff Bonwick boolean_t vps_readable; 1148e14bb325SJeff Bonwick boolean_t vps_writeable; 1149e14bb325SJeff Bonwick int vps_flags; 1150e14bb325SJeff Bonwick } vdev_probe_stats_t; 1151e14bb325SJeff Bonwick 1152e14bb325SJeff Bonwick static void 1153e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio) 11540a4e9518Sgw { 11558ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 1156a3f829aeSBill Moore vdev_t *vd = zio->io_vd; 1157e14bb325SJeff Bonwick vdev_probe_stats_t *vps = zio->io_private; 1158a3f829aeSBill Moore 1159a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio != NULL); 1160e14bb325SJeff Bonwick 1161e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) { 1162e14bb325SJeff Bonwick if (zio->io_error == 0) 1163e14bb325SJeff Bonwick vps->vps_readable = 1; 11648ad4d6ddSJeff Bonwick if (zio->io_error == 0 && spa_writeable(spa)) { 1165a3f829aeSBill Moore zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1166770499e1SDan Kimmel zio->io_offset, zio->io_size, zio->io_abd, 1167e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1168e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1169e14bb325SJeff Bonwick } else { 1170770499e1SDan Kimmel abd_free(zio->io_abd); 1171e14bb325SJeff Bonwick } 1172e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_WRITE) { 1173e14bb325SJeff Bonwick if (zio->io_error == 0) 1174e14bb325SJeff Bonwick vps->vps_writeable = 1; 1175770499e1SDan Kimmel abd_free(zio->io_abd); 1176e14bb325SJeff Bonwick } else if (zio->io_type == ZIO_TYPE_NULL) { 1177a3f829aeSBill Moore zio_t *pio; 1178e14bb325SJeff Bonwick 1179e14bb325SJeff Bonwick vd->vdev_cant_read |= !vps->vps_readable; 1180e14bb325SJeff Bonwick vd->vdev_cant_write |= !vps->vps_writeable; 1181e14bb325SJeff Bonwick 1182e14bb325SJeff Bonwick if (vdev_readable(vd) && 11838ad4d6ddSJeff Bonwick (vdev_writeable(vd) || !spa_writeable(spa))) { 1184e14bb325SJeff Bonwick zio->io_error = 0; 1185e14bb325SJeff Bonwick } else { 1186e14bb325SJeff Bonwick ASSERT(zio->io_error != 0); 11873ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "failed probe"); 1188e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 11898ad4d6ddSJeff Bonwick spa, vd, NULL, 0, 0); 1190be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 1191e14bb325SJeff Bonwick } 1192a3f829aeSBill Moore 1193a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1194a3f829aeSBill Moore ASSERT(vd->vdev_probe_zio == zio); 1195a3f829aeSBill Moore vd->vdev_probe_zio = NULL; 1196a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1197a3f829aeSBill Moore 11980f7643c7SGeorge Wilson zio_link_t *zl = NULL; 11990f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1200a3f829aeSBill Moore if (!vdev_accessible(vd, pio)) 1201be6fd75aSMatthew Ahrens pio->io_error = SET_ERROR(ENXIO); 1202a3f829aeSBill Moore 1203e14bb325SJeff Bonwick kmem_free(vps, sizeof (*vps)); 1204e14bb325SJeff Bonwick } 1205e14bb325SJeff Bonwick } 12060a4e9518Sgw 1207e14bb325SJeff Bonwick /* 1208f7170741SWill Andrews * Determine whether this device is accessible. 1209f7170741SWill Andrews * 1210f7170741SWill Andrews * Read and write to several known locations: the pad regions of each 1211f7170741SWill Andrews * vdev label but the first, which we leave alone in case it contains 1212f7170741SWill Andrews * a VTOC. 1213e14bb325SJeff Bonwick */ 1214e14bb325SJeff Bonwick zio_t * 1215a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio) 1216e14bb325SJeff Bonwick { 1217e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 1218a3f829aeSBill Moore vdev_probe_stats_t *vps = NULL; 1219a3f829aeSBill Moore zio_t *pio; 1220a3f829aeSBill Moore 1221a3f829aeSBill Moore ASSERT(vd->vdev_ops->vdev_op_leaf); 12220a4e9518Sgw 1223a3f829aeSBill Moore /* 1224a3f829aeSBill Moore * Don't probe the probe. 1225a3f829aeSBill Moore */ 1226a3f829aeSBill Moore if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1227a3f829aeSBill Moore return (NULL); 1228e14bb325SJeff Bonwick 1229a3f829aeSBill Moore /* 1230a3f829aeSBill Moore * To prevent 'probe storms' when a device fails, we create 1231a3f829aeSBill Moore * just one probe i/o at a time. All zios that want to probe 1232a3f829aeSBill Moore * this vdev will become parents of the probe io. 1233a3f829aeSBill Moore */ 1234a3f829aeSBill Moore mutex_enter(&vd->vdev_probe_lock); 1235e14bb325SJeff Bonwick 1236a3f829aeSBill Moore if ((pio = vd->vdev_probe_zio) == NULL) { 1237a3f829aeSBill Moore vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1238a3f829aeSBill Moore 1239a3f829aeSBill Moore vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1240a3f829aeSBill Moore ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 12418956713aSEric Schrock ZIO_FLAG_TRYHARD; 1242a3f829aeSBill Moore 1243a3f829aeSBill Moore if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1244a3f829aeSBill Moore /* 1245a3f829aeSBill Moore * vdev_cant_read and vdev_cant_write can only 1246a3f829aeSBill Moore * transition from TRUE to FALSE when we have the 1247a3f829aeSBill Moore * SCL_ZIO lock as writer; otherwise they can only 1248a3f829aeSBill Moore * transition from FALSE to TRUE. This ensures that 1249a3f829aeSBill Moore * any zio looking at these values can assume that 1250a3f829aeSBill Moore * failures persist for the life of the I/O. That's 1251a3f829aeSBill Moore * important because when a device has intermittent 1252a3f829aeSBill Moore * connectivity problems, we want to ensure that 1253a3f829aeSBill Moore * they're ascribed to the device (ENXIO) and not 1254a3f829aeSBill Moore * the zio (EIO). 1255a3f829aeSBill Moore * 1256a3f829aeSBill Moore * Since we hold SCL_ZIO as writer here, clear both 1257a3f829aeSBill Moore * values so the probe can reevaluate from first 1258a3f829aeSBill Moore * principles. 1259a3f829aeSBill Moore */ 1260a3f829aeSBill Moore vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1261a3f829aeSBill Moore vd->vdev_cant_read = B_FALSE; 1262a3f829aeSBill Moore vd->vdev_cant_write = B_FALSE; 1263a3f829aeSBill Moore } 1264a3f829aeSBill Moore 1265a3f829aeSBill Moore vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1266a3f829aeSBill Moore vdev_probe_done, vps, 1267a3f829aeSBill Moore vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1268a3f829aeSBill Moore 126998d1cbfeSGeorge Wilson /* 127098d1cbfeSGeorge Wilson * We can't change the vdev state in this context, so we 127198d1cbfeSGeorge Wilson * kick off an async task to do it on our behalf. 127298d1cbfeSGeorge Wilson */ 1273a3f829aeSBill Moore if (zio != NULL) { 1274a3f829aeSBill Moore vd->vdev_probe_wanted = B_TRUE; 1275a3f829aeSBill Moore spa_async_request(spa, SPA_ASYNC_PROBE); 1276a3f829aeSBill Moore } 1277e14bb325SJeff Bonwick } 1278e14bb325SJeff Bonwick 1279a3f829aeSBill Moore if (zio != NULL) 1280a3f829aeSBill Moore zio_add_child(zio, pio); 1281e14bb325SJeff Bonwick 1282a3f829aeSBill Moore mutex_exit(&vd->vdev_probe_lock); 1283e14bb325SJeff Bonwick 1284a3f829aeSBill Moore if (vps == NULL) { 1285a3f829aeSBill Moore ASSERT(zio != NULL); 1286a3f829aeSBill Moore return (NULL); 1287a3f829aeSBill Moore } 1288e14bb325SJeff Bonwick 1289e14bb325SJeff Bonwick for (int l = 1; l < VDEV_LABELS; l++) { 1290a3f829aeSBill Moore zio_nowait(zio_read_phys(pio, vd, 1291e14bb325SJeff Bonwick vdev_label_offset(vd->vdev_psize, l, 1292770499e1SDan Kimmel offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1293770499e1SDan Kimmel abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1294e14bb325SJeff Bonwick ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1295e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1296e14bb325SJeff Bonwick } 1297e14bb325SJeff Bonwick 1298a3f829aeSBill Moore if (zio == NULL) 1299a3f829aeSBill Moore return (pio); 1300a3f829aeSBill Moore 1301a3f829aeSBill Moore zio_nowait(pio); 1302a3f829aeSBill Moore return (NULL); 13030a4e9518Sgw } 13040a4e9518Sgw 1305f64c0e34SEric Taylor static void 1306f64c0e34SEric Taylor vdev_open_child(void *arg) 1307f64c0e34SEric Taylor { 1308f64c0e34SEric Taylor vdev_t *vd = arg; 1309f64c0e34SEric Taylor 1310f64c0e34SEric Taylor vd->vdev_open_thread = curthread; 1311f64c0e34SEric Taylor vd->vdev_open_error = vdev_open(vd); 1312f64c0e34SEric Taylor vd->vdev_open_thread = NULL; 1313f64c0e34SEric Taylor } 1314f64c0e34SEric Taylor 1315681d9761SEric Taylor boolean_t 1316681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd) 1317681d9761SEric Taylor { 1318681d9761SEric Taylor if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1319681d9761SEric Taylor strlen(ZVOL_DIR)) == 0) 1320681d9761SEric Taylor return (B_TRUE); 1321681d9761SEric Taylor for (int c = 0; c < vd->vdev_children; c++) 1322681d9761SEric Taylor if (vdev_uses_zvols(vd->vdev_child[c])) 1323681d9761SEric Taylor return (B_TRUE); 1324681d9761SEric Taylor return (B_FALSE); 1325681d9761SEric Taylor } 1326681d9761SEric Taylor 1327f64c0e34SEric Taylor void 1328f64c0e34SEric Taylor vdev_open_children(vdev_t *vd) 1329f64c0e34SEric Taylor { 1330f64c0e34SEric Taylor taskq_t *tq; 1331f64c0e34SEric Taylor int children = vd->vdev_children; 1332f64c0e34SEric Taylor 1333681d9761SEric Taylor /* 1334681d9761SEric Taylor * in order to handle pools on top of zvols, do the opens 1335681d9761SEric Taylor * in a single thread so that the same thread holds the 1336681d9761SEric Taylor * spa_namespace_lock 1337681d9761SEric Taylor */ 1338681d9761SEric Taylor if (vdev_uses_zvols(vd)) { 1339681d9761SEric Taylor for (int c = 0; c < children; c++) 1340681d9761SEric Taylor vd->vdev_child[c]->vdev_open_error = 1341681d9761SEric Taylor vdev_open(vd->vdev_child[c]); 1342681d9761SEric Taylor return; 1343681d9761SEric Taylor } 1344f64c0e34SEric Taylor tq = taskq_create("vdev_open", children, minclsyspri, 1345f64c0e34SEric Taylor children, children, TASKQ_PREPOPULATE); 1346f64c0e34SEric Taylor 1347f64c0e34SEric Taylor for (int c = 0; c < children; c++) 1348f64c0e34SEric Taylor VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1349f64c0e34SEric Taylor TQ_SLEEP) != NULL); 1350f64c0e34SEric Taylor 1351f64c0e34SEric Taylor taskq_destroy(tq); 1352f64c0e34SEric Taylor } 1353f64c0e34SEric Taylor 13545cabbc6bSPrashanth Sreenivasa /* 13555cabbc6bSPrashanth Sreenivasa * Compute the raidz-deflation ratio. Note, we hard-code 13565cabbc6bSPrashanth Sreenivasa * in 128k (1 << 17) because it is the "typical" blocksize. 13575cabbc6bSPrashanth Sreenivasa * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 13585cabbc6bSPrashanth Sreenivasa * otherwise it would inconsistently account for existing bp's. 13595cabbc6bSPrashanth Sreenivasa */ 13605cabbc6bSPrashanth Sreenivasa static void 13615cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd) 13625cabbc6bSPrashanth Sreenivasa { 13635cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 13645cabbc6bSPrashanth Sreenivasa vd->vdev_deflate_ratio = (1 << 17) / 13655cabbc6bSPrashanth Sreenivasa (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 13665cabbc6bSPrashanth Sreenivasa } 13675cabbc6bSPrashanth Sreenivasa } 13685cabbc6bSPrashanth Sreenivasa 1369fa9e4066Sahrens /* 1370fa9e4066Sahrens * Prepare a virtual device for access. 1371fa9e4066Sahrens */ 1372fa9e4066Sahrens int 1373fa9e4066Sahrens vdev_open(vdev_t *vd) 1374fa9e4066Sahrens { 13758ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1376fa9e4066Sahrens int error; 1377fa9e4066Sahrens uint64_t osize = 0; 13784263d13fSGeorge Wilson uint64_t max_osize = 0; 13794263d13fSGeorge Wilson uint64_t asize, max_asize, psize; 1380ecc2d604Sbonwick uint64_t ashift = 0; 1381fa9e4066Sahrens 1382f64c0e34SEric Taylor ASSERT(vd->vdev_open_thread == curthread || 1383f64c0e34SEric Taylor spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1384fa9e4066Sahrens ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1385fa9e4066Sahrens vd->vdev_state == VDEV_STATE_CANT_OPEN || 1386fa9e4066Sahrens vd->vdev_state == VDEV_STATE_OFFLINE); 1387fa9e4066Sahrens 1388fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1389e6ca193dSGeorge Wilson vd->vdev_cant_read = B_FALSE; 1390e6ca193dSGeorge Wilson vd->vdev_cant_write = B_FALSE; 1391573ca77eSGeorge Wilson vd->vdev_min_asize = vdev_get_min_asize(vd); 1392fa9e4066Sahrens 1393069f55e2SEric Schrock /* 1394069f55e2SEric Schrock * If this vdev is not removed, check its fault status. If it's 1395069f55e2SEric Schrock * faulted, bail out of the open. 1396069f55e2SEric Schrock */ 13973d7072f8Seschrock if (!vd->vdev_removed && vd->vdev_faulted) { 13983d7072f8Seschrock ASSERT(vd->vdev_children == 0); 1399069f55e2SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1400069f55e2SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 14013d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1402069f55e2SEric Schrock vd->vdev_label_aux); 1403be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 14043d7072f8Seschrock } else if (vd->vdev_offline) { 1405fa9e4066Sahrens ASSERT(vd->vdev_children == 0); 1406ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1407be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1408fa9e4066Sahrens } 1409fa9e4066Sahrens 14104263d13fSGeorge Wilson error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1411fa9e4066Sahrens 1412095bcd66SGeorge Wilson /* 1413095bcd66SGeorge Wilson * Reset the vdev_reopening flag so that we actually close 1414095bcd66SGeorge Wilson * the vdev on error. 1415095bcd66SGeorge Wilson */ 1416095bcd66SGeorge Wilson vd->vdev_reopening = B_FALSE; 1417ea8dc4b6Seschrock if (zio_injection_enabled && error == 0) 14188956713aSEric Schrock error = zio_handle_device_injection(vd, NULL, ENXIO); 1419ea8dc4b6Seschrock 1420fa9e4066Sahrens if (error) { 14213d7072f8Seschrock if (vd->vdev_removed && 14223d7072f8Seschrock vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 14233d7072f8Seschrock vd->vdev_removed = B_FALSE; 14243d7072f8Seschrock 14256f793812SPavel Zakharov if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 14266f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 14276f793812SPavel Zakharov vd->vdev_stat.vs_aux); 14286f793812SPavel Zakharov } else { 14296f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 14306f793812SPavel Zakharov vd->vdev_stat.vs_aux); 14316f793812SPavel Zakharov } 1432fa9e4066Sahrens return (error); 1433fa9e4066Sahrens } 1434fa9e4066Sahrens 14353d7072f8Seschrock vd->vdev_removed = B_FALSE; 14363d7072f8Seschrock 1437096d22d4SEric Schrock /* 1438096d22d4SEric Schrock * Recheck the faulted flag now that we have confirmed that 1439096d22d4SEric Schrock * the vdev is accessible. If we're faulted, bail. 1440096d22d4SEric Schrock */ 1441096d22d4SEric Schrock if (vd->vdev_faulted) { 1442096d22d4SEric Schrock ASSERT(vd->vdev_children == 0); 1443096d22d4SEric Schrock ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1444096d22d4SEric Schrock vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1445096d22d4SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1446096d22d4SEric Schrock vd->vdev_label_aux); 1447be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1448096d22d4SEric Schrock } 1449096d22d4SEric Schrock 14503d7072f8Seschrock if (vd->vdev_degraded) { 14513d7072f8Seschrock ASSERT(vd->vdev_children == 0); 14523d7072f8Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 14533d7072f8Seschrock VDEV_AUX_ERR_EXCEEDED); 14543d7072f8Seschrock } else { 1455069f55e2SEric Schrock vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 14563d7072f8Seschrock } 1457fa9e4066Sahrens 145888ecc943SGeorge Wilson /* 145988ecc943SGeorge Wilson * For hole or missing vdevs we just return success. 146088ecc943SGeorge Wilson */ 146188ecc943SGeorge Wilson if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 146288ecc943SGeorge Wilson return (0); 146388ecc943SGeorge Wilson 1464573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 1465ea8dc4b6Seschrock if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1466ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1467ea8dc4b6Seschrock VDEV_AUX_NONE); 1468ea8dc4b6Seschrock break; 1469ea8dc4b6Seschrock } 1470573ca77eSGeorge Wilson } 1471fa9e4066Sahrens 1472fa9e4066Sahrens osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 14734263d13fSGeorge Wilson max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1474fa9e4066Sahrens 1475fa9e4066Sahrens if (vd->vdev_children == 0) { 1476fa9e4066Sahrens if (osize < SPA_MINDEVSIZE) { 1477ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1478ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1479be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1480fa9e4066Sahrens } 1481fa9e4066Sahrens psize = osize; 1482fa9e4066Sahrens asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 14834263d13fSGeorge Wilson max_asize = max_osize - (VDEV_LABEL_START_SIZE + 14844263d13fSGeorge Wilson VDEV_LABEL_END_SIZE); 1485fa9e4066Sahrens } else { 1486ecc2d604Sbonwick if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1487fa9e4066Sahrens (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1488ea8dc4b6Seschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1489ea8dc4b6Seschrock VDEV_AUX_TOO_SMALL); 1490be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 1491fa9e4066Sahrens } 1492fa9e4066Sahrens psize = 0; 1493fa9e4066Sahrens asize = osize; 14944263d13fSGeorge Wilson max_asize = max_osize; 1495fa9e4066Sahrens } 1496fa9e4066Sahrens 1497fa9e4066Sahrens vd->vdev_psize = psize; 1498fa9e4066Sahrens 1499573ca77eSGeorge Wilson /* 1500c040c10cSSteven Hartland * Make sure the allocatable size hasn't shrunk too much. 1501573ca77eSGeorge Wilson */ 1502573ca77eSGeorge Wilson if (asize < vd->vdev_min_asize) { 1503573ca77eSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1504573ca77eSGeorge Wilson VDEV_AUX_BAD_LABEL); 1505be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1506573ca77eSGeorge Wilson } 1507573ca77eSGeorge Wilson 1508fa9e4066Sahrens if (vd->vdev_asize == 0) { 1509fa9e4066Sahrens /* 1510fa9e4066Sahrens * This is the first-ever open, so use the computed values. 1511ecc2d604Sbonwick * For testing purposes, a higher ashift can be requested. 1512fa9e4066Sahrens */ 1513fa9e4066Sahrens vd->vdev_asize = asize; 15144263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1515ecc2d604Sbonwick vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1516*93a1902eSMatthew Ahrens vd->vdev_ashift = MAX(zfs_ashift_min, vd->vdev_ashift); 1517fa9e4066Sahrens } else { 1518fa9e4066Sahrens /* 15192384d9f8SGeorge Wilson * Detect if the alignment requirement has increased. 15202384d9f8SGeorge Wilson * We don't want to make the pool unavailable, just 15212384d9f8SGeorge Wilson * issue a warning instead. 1522fa9e4066Sahrens */ 15232384d9f8SGeorge Wilson if (ashift > vd->vdev_top->vdev_ashift && 15242384d9f8SGeorge Wilson vd->vdev_ops->vdev_op_leaf) { 15252384d9f8SGeorge Wilson cmn_err(CE_WARN, 15262384d9f8SGeorge Wilson "Disk, '%s', has a block alignment that is " 15272384d9f8SGeorge Wilson "larger than the pool's alignment\n", 15282384d9f8SGeorge Wilson vd->vdev_path); 1529fa9e4066Sahrens } 15304263d13fSGeorge Wilson vd->vdev_max_asize = max_asize; 1531573ca77eSGeorge Wilson } 1532fa9e4066Sahrens 1533573ca77eSGeorge Wilson /* 1534c040c10cSSteven Hartland * If all children are healthy we update asize if either: 1535c040c10cSSteven Hartland * The asize has increased, due to a device expansion caused by dynamic 1536c040c10cSSteven Hartland * LUN growth or vdev replacement, and automatic expansion is enabled; 1537c040c10cSSteven Hartland * making the additional space available. 1538c040c10cSSteven Hartland * 1539c040c10cSSteven Hartland * The asize has decreased, due to a device shrink usually caused by a 1540c040c10cSSteven Hartland * vdev replace with a smaller device. This ensures that calculations 1541c040c10cSSteven Hartland * based of max_asize and asize e.g. esize are always valid. It's safe 1542c040c10cSSteven Hartland * to do this as we've already validated that asize is greater than 1543c040c10cSSteven Hartland * vdev_min_asize. 1544573ca77eSGeorge Wilson */ 1545c040c10cSSteven Hartland if (vd->vdev_state == VDEV_STATE_HEALTHY && 1546c040c10cSSteven Hartland ((asize > vd->vdev_asize && 1547c040c10cSSteven Hartland (vd->vdev_expanding || spa->spa_autoexpand)) || 1548c040c10cSSteven Hartland (asize < vd->vdev_asize))) 1549573ca77eSGeorge Wilson vd->vdev_asize = asize; 1550fa9e4066Sahrens 1551573ca77eSGeorge Wilson vdev_set_min_asize(vd); 1552fa9e4066Sahrens 15530a4e9518Sgw /* 15540a4e9518Sgw * Ensure we can issue some IO before declaring the 15550a4e9518Sgw * vdev open for business. 15560a4e9518Sgw */ 1557e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 1558e14bb325SJeff Bonwick (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 155998d1cbfeSGeorge Wilson vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 156098d1cbfeSGeorge Wilson VDEV_AUX_ERR_EXCEEDED); 15610a4e9518Sgw return (error); 15620a4e9518Sgw } 15630a4e9518Sgw 156481cd5c55SMatthew Ahrens /* 156581cd5c55SMatthew Ahrens * Track the min and max ashift values for normal data devices. 156681cd5c55SMatthew Ahrens */ 156781cd5c55SMatthew Ahrens if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 156881cd5c55SMatthew Ahrens !vd->vdev_islog && vd->vdev_aux == NULL) { 156981cd5c55SMatthew Ahrens if (vd->vdev_ashift > spa->spa_max_ashift) 157081cd5c55SMatthew Ahrens spa->spa_max_ashift = vd->vdev_ashift; 157181cd5c55SMatthew Ahrens if (vd->vdev_ashift < spa->spa_min_ashift) 157281cd5c55SMatthew Ahrens spa->spa_min_ashift = vd->vdev_ashift; 157381cd5c55SMatthew Ahrens } 157481cd5c55SMatthew Ahrens 1575088f3894Sahrens /* 1576088f3894Sahrens * If a leaf vdev has a DTL, and seems healthy, then kick off a 15778ad4d6ddSJeff Bonwick * resilver. But don't do this if we are doing a reopen for a scrub, 15788ad4d6ddSJeff Bonwick * since this would just restart the scrub we are already doing. 1579088f3894Sahrens */ 15808ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 15818ad4d6ddSJeff Bonwick vdev_resilver_needed(vd, NULL, NULL)) 15828ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 1583088f3894Sahrens 1584fa9e4066Sahrens return (0); 1585fa9e4066Sahrens } 1586fa9e4066Sahrens 1587560e6e96Seschrock /* 1588560e6e96Seschrock * Called once the vdevs are all opened, this routine validates the label 15896f793812SPavel Zakharov * contents. This needs to be done before vdev_load() so that we don't 15903d7072f8Seschrock * inadvertently do repair I/Os to the wrong device. 1591560e6e96Seschrock * 1592560e6e96Seschrock * This function will only return failure if one of the vdevs indicates that it 1593560e6e96Seschrock * has since been destroyed or exported. This is only possible if 1594560e6e96Seschrock * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1595560e6e96Seschrock * will be updated but the function will return 0. 1596560e6e96Seschrock */ 1597560e6e96Seschrock int 15986f793812SPavel Zakharov vdev_validate(vdev_t *vd) 1599560e6e96Seschrock { 1600560e6e96Seschrock spa_t *spa = vd->vdev_spa; 1601560e6e96Seschrock nvlist_t *label; 16026f793812SPavel Zakharov uint64_t guid = 0, aux_guid = 0, top_guid; 1603560e6e96Seschrock uint64_t state; 16046f793812SPavel Zakharov nvlist_t *nvl; 16056f793812SPavel Zakharov uint64_t txg; 1606560e6e96Seschrock 16076f793812SPavel Zakharov if (vdev_validate_skip) 16086f793812SPavel Zakharov return (0); 16096f793812SPavel Zakharov 16106f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 16116f793812SPavel Zakharov if (vdev_validate(vd->vdev_child[c]) != 0) 1612be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 1613560e6e96Seschrock 1614b5989ec7Seschrock /* 1615b5989ec7Seschrock * If the device has already failed, or was marked offline, don't do 1616b5989ec7Seschrock * any further validation. Otherwise, label I/O will fail and we will 1617b5989ec7Seschrock * overwrite the previous state. 1618b5989ec7Seschrock */ 16196f793812SPavel Zakharov if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 16206f793812SPavel Zakharov return (0); 1621560e6e96Seschrock 16226f793812SPavel Zakharov /* 16236f793812SPavel Zakharov * If we are performing an extreme rewind, we allow for a label that 16246f793812SPavel Zakharov * was modified at a point after the current txg. 1625d1de72cfSPavel Zakharov * If config lock is not held do not check for the txg. spa_sync could 1626d1de72cfSPavel Zakharov * be updating the vdev's label before updating spa_last_synced_txg. 16276f793812SPavel Zakharov */ 1628d1de72cfSPavel Zakharov if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1629d1de72cfSPavel Zakharov spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 16306f793812SPavel Zakharov txg = UINT64_MAX; 16316f793812SPavel Zakharov else 16326f793812SPavel Zakharov txg = spa_last_synced_txg(spa); 1633560e6e96Seschrock 16346f793812SPavel Zakharov if ((label = vdev_label_read_config(vd, txg)) == NULL) { 16356f793812SPavel Zakharov vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 16366f793812SPavel Zakharov VDEV_AUX_BAD_LABEL); 1637b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1638b6bf6e15SPavel Zakharov "txg %llu", (u_longlong_t)txg); 16396f793812SPavel Zakharov return (0); 16406f793812SPavel Zakharov } 16411195e687SMark J Musante 16426f793812SPavel Zakharov /* 16436f793812SPavel Zakharov * Determine if this vdev has been split off into another 16446f793812SPavel Zakharov * pool. If so, then refuse to open it. 16456f793812SPavel Zakharov */ 16466f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 16476f793812SPavel Zakharov &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 16486f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16496f793812SPavel Zakharov VDEV_AUX_SPLIT_POOL); 16506f793812SPavel Zakharov nvlist_free(label); 16516f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 16526f793812SPavel Zakharov return (0); 16536f793812SPavel Zakharov } 1654560e6e96Seschrock 16556f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 16566f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16576f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16586f793812SPavel Zakharov nvlist_free(label); 16596f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 16606f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 16616f793812SPavel Zakharov return (0); 16626f793812SPavel Zakharov } 16631195e687SMark J Musante 16646f793812SPavel Zakharov /* 16656f793812SPavel Zakharov * If config is not trusted then ignore the spa guid check. This is 16666f793812SPavel Zakharov * necessary because if the machine crashed during a re-guid the new 16676f793812SPavel Zakharov * guid might have been written to all of the vdev labels, but not the 16686f793812SPavel Zakharov * cached config. The check will be performed again once we have the 16696f793812SPavel Zakharov * trusted config from the MOS. 16706f793812SPavel Zakharov */ 16716f793812SPavel Zakharov if (spa->spa_trust_config && guid != spa_guid(spa)) { 16726f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16736f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16746f793812SPavel Zakharov nvlist_free(label); 16756f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 16766f793812SPavel Zakharov "match config (%llu != %llu)", (u_longlong_t)guid, 16776f793812SPavel Zakharov (u_longlong_t)spa_guid(spa)); 16786f793812SPavel Zakharov return (0); 16796f793812SPavel Zakharov } 16806f793812SPavel Zakharov 16816f793812SPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 16826f793812SPavel Zakharov != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 16836f793812SPavel Zakharov &aux_guid) != 0) 16846f793812SPavel Zakharov aux_guid = 0; 16856f793812SPavel Zakharov 16866f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 16876f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16886f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16896f793812SPavel Zakharov nvlist_free(label); 16906f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 16916f793812SPavel Zakharov ZPOOL_CONFIG_GUID); 16926f793812SPavel Zakharov return (0); 16936f793812SPavel Zakharov } 16946f793812SPavel Zakharov 16956f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 16966f793812SPavel Zakharov != 0) { 16976f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 16986f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 16996f793812SPavel Zakharov nvlist_free(label); 17006f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 17016f793812SPavel Zakharov ZPOOL_CONFIG_TOP_GUID); 17026f793812SPavel Zakharov return (0); 17036f793812SPavel Zakharov } 17046f793812SPavel Zakharov 17056f793812SPavel Zakharov /* 17066f793812SPavel Zakharov * If this vdev just became a top-level vdev because its sibling was 17076f793812SPavel Zakharov * detached, it will have adopted the parent's vdev guid -- but the 17086f793812SPavel Zakharov * label may or may not be on disk yet. Fortunately, either version 17096f793812SPavel Zakharov * of the label will have the same top guid, so if we're a top-level 17106f793812SPavel Zakharov * vdev, we can safely compare to that instead. 17116f793812SPavel Zakharov * However, if the config comes from a cachefile that failed to update 17126f793812SPavel Zakharov * after the detach, a top-level vdev will appear as a non top-level 17136f793812SPavel Zakharov * vdev in the config. Also relax the constraints if we perform an 17146f793812SPavel Zakharov * extreme rewind. 17156f793812SPavel Zakharov * 17166f793812SPavel Zakharov * If we split this vdev off instead, then we also check the 17176f793812SPavel Zakharov * original pool's guid. We don't want to consider the vdev 17186f793812SPavel Zakharov * corrupt if it is partway through a split operation. 17196f793812SPavel Zakharov */ 17206f793812SPavel Zakharov if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 17216f793812SPavel Zakharov boolean_t mismatch = B_FALSE; 17226f793812SPavel Zakharov if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 17236f793812SPavel Zakharov if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 17246f793812SPavel Zakharov mismatch = B_TRUE; 17256f793812SPavel Zakharov } else { 17266f793812SPavel Zakharov if (vd->vdev_guid != top_guid && 17276f793812SPavel Zakharov vd->vdev_top->vdev_guid != guid) 17286f793812SPavel Zakharov mismatch = B_TRUE; 1729560e6e96Seschrock } 1730560e6e96Seschrock 17316f793812SPavel Zakharov if (mismatch) { 1732560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1733560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 1734560e6e96Seschrock nvlist_free(label); 17356f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: config guid " 17366f793812SPavel Zakharov "doesn't match label guid"); 17376f793812SPavel Zakharov vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 17386f793812SPavel Zakharov (u_longlong_t)vd->vdev_guid, 17396f793812SPavel Zakharov (u_longlong_t)vd->vdev_top->vdev_guid); 17406f793812SPavel Zakharov vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 17416f793812SPavel Zakharov "aux_guid %llu", (u_longlong_t)guid, 17426f793812SPavel Zakharov (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1743560e6e96Seschrock return (0); 1744560e6e96Seschrock } 17456f793812SPavel Zakharov } 1746560e6e96Seschrock 17476f793812SPavel Zakharov if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 17486f793812SPavel Zakharov &state) != 0) { 17496f793812SPavel Zakharov vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 17506f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA); 1751560e6e96Seschrock nvlist_free(label); 17526f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 17536f793812SPavel Zakharov ZPOOL_CONFIG_POOL_STATE); 17546f793812SPavel Zakharov return (0); 17556f793812SPavel Zakharov } 1756560e6e96Seschrock 17576f793812SPavel Zakharov nvlist_free(label); 17586f793812SPavel Zakharov 17596f793812SPavel Zakharov /* 17606f793812SPavel Zakharov * If this is a verbatim import, no need to check the 17616f793812SPavel Zakharov * state of the pool. 17626f793812SPavel Zakharov */ 17636f793812SPavel Zakharov if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 17646f793812SPavel Zakharov spa_load_state(spa) == SPA_LOAD_OPEN && 17656f793812SPavel Zakharov state != POOL_STATE_ACTIVE) { 17666f793812SPavel Zakharov vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 17676f793812SPavel Zakharov "for spa %s", (u_longlong_t)state, spa->spa_name); 17686f793812SPavel Zakharov return (SET_ERROR(EBADF)); 17696f793812SPavel Zakharov } 17706f793812SPavel Zakharov 17716f793812SPavel Zakharov /* 17726f793812SPavel Zakharov * If we were able to open and validate a vdev that was 17736f793812SPavel Zakharov * previously marked permanently unavailable, clear that state 17746f793812SPavel Zakharov * now. 17756f793812SPavel Zakharov */ 17766f793812SPavel Zakharov if (vd->vdev_not_present) 17776f793812SPavel Zakharov vd->vdev_not_present = 0; 17786f793812SPavel Zakharov 17796f793812SPavel Zakharov return (0); 17806f793812SPavel Zakharov } 17816f793812SPavel Zakharov 17826f793812SPavel Zakharov static void 17836f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 17846f793812SPavel Zakharov { 17856f793812SPavel Zakharov if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 17866f793812SPavel Zakharov if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 17876f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 17886f793812SPavel Zakharov "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 17896f793812SPavel Zakharov dvd->vdev_path, svd->vdev_path); 17906f793812SPavel Zakharov spa_strfree(dvd->vdev_path); 17916f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 17923ee8c80cSPavel Zakharov } 17936f793812SPavel Zakharov } else if (svd->vdev_path != NULL) { 17946f793812SPavel Zakharov dvd->vdev_path = spa_strdup(svd->vdev_path); 17956f793812SPavel Zakharov zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 17966f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 17976f793812SPavel Zakharov } 17986f793812SPavel Zakharov } 1799560e6e96Seschrock 18006f793812SPavel Zakharov /* 18016f793812SPavel Zakharov * Recursively copy vdev paths from one vdev to another. Source and destination 18026f793812SPavel Zakharov * vdev trees must have same geometry otherwise return error. Intended to copy 18036f793812SPavel Zakharov * paths from userland config into MOS config. 18046f793812SPavel Zakharov */ 18056f793812SPavel Zakharov int 18066f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 18076f793812SPavel Zakharov { 18086f793812SPavel Zakharov if ((svd->vdev_ops == &vdev_missing_ops) || 18096f793812SPavel Zakharov (svd->vdev_ishole && dvd->vdev_ishole) || 18106f793812SPavel Zakharov (dvd->vdev_ops == &vdev_indirect_ops)) 18116f793812SPavel Zakharov return (0); 18126f793812SPavel Zakharov 18136f793812SPavel Zakharov if (svd->vdev_ops != dvd->vdev_ops) { 18146f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 18156f793812SPavel Zakharov svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 18166f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 18176f793812SPavel Zakharov } 18186f793812SPavel Zakharov 18196f793812SPavel Zakharov if (svd->vdev_guid != dvd->vdev_guid) { 18206f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 18216f793812SPavel Zakharov "%llu)", (u_longlong_t)svd->vdev_guid, 18226f793812SPavel Zakharov (u_longlong_t)dvd->vdev_guid); 18236f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 18246f793812SPavel Zakharov } 18256f793812SPavel Zakharov 18266f793812SPavel Zakharov if (svd->vdev_children != dvd->vdev_children) { 18276f793812SPavel Zakharov vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 18286f793812SPavel Zakharov "%llu != %llu", (u_longlong_t)svd->vdev_children, 18296f793812SPavel Zakharov (u_longlong_t)dvd->vdev_children); 18306f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 183151ece835Seschrock } 1832560e6e96Seschrock 18336f793812SPavel Zakharov for (uint64_t i = 0; i < svd->vdev_children; i++) { 18346f793812SPavel Zakharov int error = vdev_copy_path_strict(svd->vdev_child[i], 18356f793812SPavel Zakharov dvd->vdev_child[i]); 18366f793812SPavel Zakharov if (error != 0) 18376f793812SPavel Zakharov return (error); 18386f793812SPavel Zakharov } 18396f793812SPavel Zakharov 18406f793812SPavel Zakharov if (svd->vdev_ops->vdev_op_leaf) 18416f793812SPavel Zakharov vdev_copy_path_impl(svd, dvd); 18426f793812SPavel Zakharov 1843560e6e96Seschrock return (0); 1844560e6e96Seschrock } 1845560e6e96Seschrock 18466f793812SPavel Zakharov static void 18476f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 18486f793812SPavel Zakharov { 18496f793812SPavel Zakharov ASSERT(stvd->vdev_top == stvd); 18506f793812SPavel Zakharov ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 18516f793812SPavel Zakharov 18526f793812SPavel Zakharov for (uint64_t i = 0; i < dvd->vdev_children; i++) { 18536f793812SPavel Zakharov vdev_copy_path_search(stvd, dvd->vdev_child[i]); 18546f793812SPavel Zakharov } 18556f793812SPavel Zakharov 18566f793812SPavel Zakharov if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 18576f793812SPavel Zakharov return; 18586f793812SPavel Zakharov 18596f793812SPavel Zakharov /* 18606f793812SPavel Zakharov * The idea here is that while a vdev can shift positions within 18616f793812SPavel Zakharov * a top vdev (when replacing, attaching mirror, etc.) it cannot 18626f793812SPavel Zakharov * step outside of it. 18636f793812SPavel Zakharov */ 18646f793812SPavel Zakharov vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 18656f793812SPavel Zakharov 18666f793812SPavel Zakharov if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 18676f793812SPavel Zakharov return; 18686f793812SPavel Zakharov 18696f793812SPavel Zakharov ASSERT(vd->vdev_ops->vdev_op_leaf); 18706f793812SPavel Zakharov 18716f793812SPavel Zakharov vdev_copy_path_impl(vd, dvd); 18726f793812SPavel Zakharov } 18736f793812SPavel Zakharov 18746f793812SPavel Zakharov /* 18756f793812SPavel Zakharov * Recursively copy vdev paths from one root vdev to another. Source and 18766f793812SPavel Zakharov * destination vdev trees may differ in geometry. For each destination leaf 18776f793812SPavel Zakharov * vdev, search a vdev with the same guid and top vdev id in the source. 18786f793812SPavel Zakharov * Intended to copy paths from userland config into MOS config. 18796f793812SPavel Zakharov */ 18806f793812SPavel Zakharov void 18816f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 18826f793812SPavel Zakharov { 18836f793812SPavel Zakharov uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 18846f793812SPavel Zakharov ASSERT(srvd->vdev_ops == &vdev_root_ops); 18856f793812SPavel Zakharov ASSERT(drvd->vdev_ops == &vdev_root_ops); 18866f793812SPavel Zakharov 18876f793812SPavel Zakharov for (uint64_t i = 0; i < children; i++) { 18886f793812SPavel Zakharov vdev_copy_path_search(srvd->vdev_child[i], 18896f793812SPavel Zakharov drvd->vdev_child[i]); 18906f793812SPavel Zakharov } 18916f793812SPavel Zakharov } 18926f793812SPavel Zakharov 1893fa9e4066Sahrens /* 1894fa9e4066Sahrens * Close a virtual device. 1895fa9e4066Sahrens */ 1896fa9e4066Sahrens void 1897fa9e4066Sahrens vdev_close(vdev_t *vd) 1898fa9e4066Sahrens { 18998ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 1900095bcd66SGeorge Wilson vdev_t *pvd = vd->vdev_parent; 19018ad4d6ddSJeff Bonwick 19028ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 19038ad4d6ddSJeff Bonwick 19041195e687SMark J Musante /* 19051195e687SMark J Musante * If our parent is reopening, then we are as well, unless we are 19061195e687SMark J Musante * going offline. 19071195e687SMark J Musante */ 1908095bcd66SGeorge Wilson if (pvd != NULL && pvd->vdev_reopening) 19091195e687SMark J Musante vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1910095bcd66SGeorge Wilson 1911fa9e4066Sahrens vd->vdev_ops->vdev_op_close(vd); 1912fa9e4066Sahrens 19133d7072f8Seschrock vdev_cache_purge(vd); 1914fa9e4066Sahrens 1915560e6e96Seschrock /* 1916573ca77eSGeorge Wilson * We record the previous state before we close it, so that if we are 1917560e6e96Seschrock * doing a reopen(), we don't generate FMA ereports if we notice that 1918560e6e96Seschrock * it's still faulted. 1919560e6e96Seschrock */ 1920560e6e96Seschrock vd->vdev_prevstate = vd->vdev_state; 1921560e6e96Seschrock 1922fa9e4066Sahrens if (vd->vdev_offline) 1923fa9e4066Sahrens vd->vdev_state = VDEV_STATE_OFFLINE; 1924fa9e4066Sahrens else 1925fa9e4066Sahrens vd->vdev_state = VDEV_STATE_CLOSED; 1926ea8dc4b6Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1927fa9e4066Sahrens } 1928fa9e4066Sahrens 1929dcba9f3fSGeorge Wilson void 1930dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd) 1931dcba9f3fSGeorge Wilson { 1932dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 1933dcba9f3fSGeorge Wilson 1934dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 1935dcba9f3fSGeorge Wilson if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1936dcba9f3fSGeorge Wilson return; 1937dcba9f3fSGeorge Wilson 1938dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1939dcba9f3fSGeorge Wilson vdev_hold(vd->vdev_child[c]); 1940dcba9f3fSGeorge Wilson 1941dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 1942dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_hold(vd); 1943dcba9f3fSGeorge Wilson } 1944dcba9f3fSGeorge Wilson 1945dcba9f3fSGeorge Wilson void 1946dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd) 1947dcba9f3fSGeorge Wilson { 1948dcba9f3fSGeorge Wilson spa_t *spa = vd->vdev_spa; 1949dcba9f3fSGeorge Wilson 1950dcba9f3fSGeorge Wilson ASSERT(spa_is_root(spa)); 1951dcba9f3fSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 1952dcba9f3fSGeorge Wilson vdev_rele(vd->vdev_child[c]); 1953dcba9f3fSGeorge Wilson 1954dcba9f3fSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 1955dcba9f3fSGeorge Wilson vd->vdev_ops->vdev_op_rele(vd); 1956dcba9f3fSGeorge Wilson } 1957dcba9f3fSGeorge Wilson 1958095bcd66SGeorge Wilson /* 1959095bcd66SGeorge Wilson * Reopen all interior vdevs and any unopened leaves. We don't actually 1960095bcd66SGeorge Wilson * reopen leaf vdevs which had previously been opened as they might deadlock 1961095bcd66SGeorge Wilson * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1962095bcd66SGeorge Wilson * If the leaf has never been opened then open it, as usual. 1963095bcd66SGeorge Wilson */ 1964fa9e4066Sahrens void 1965ea8dc4b6Seschrock vdev_reopen(vdev_t *vd) 1966fa9e4066Sahrens { 1967ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 1968fa9e4066Sahrens 1969e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1970ea8dc4b6Seschrock 19711195e687SMark J Musante /* set the reopening flag unless we're taking the vdev offline */ 19721195e687SMark J Musante vd->vdev_reopening = !vd->vdev_offline; 1973fa9e4066Sahrens vdev_close(vd); 1974fa9e4066Sahrens (void) vdev_open(vd); 1975fa9e4066Sahrens 197639c23413Seschrock /* 197739c23413Seschrock * Call vdev_validate() here to make sure we have the same device. 197839c23413Seschrock * Otherwise, a device with an invalid label could be successfully 197939c23413Seschrock * opened in response to vdev_reopen(). 198039c23413Seschrock */ 1981c5904d13Seschrock if (vd->vdev_aux) { 1982c5904d13Seschrock (void) vdev_validate_aux(vd); 1983e14bb325SJeff Bonwick if (vdev_readable(vd) && vdev_writeable(vd) && 19846809eb4eSEric Schrock vd->vdev_aux == &spa->spa_l2cache && 1985573ca77eSGeorge Wilson !l2arc_vdev_present(vd)) 1986573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1987c5904d13Seschrock } else { 19886f793812SPavel Zakharov (void) vdev_validate(vd); 1989c5904d13Seschrock } 199039c23413Seschrock 1991fa9e4066Sahrens /* 19923d7072f8Seschrock * Reassess parent vdev's health. 1993fa9e4066Sahrens */ 19943d7072f8Seschrock vdev_propagate_state(vd); 1995fa9e4066Sahrens } 1996fa9e4066Sahrens 1997fa9e4066Sahrens int 199899653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1999fa9e4066Sahrens { 2000fa9e4066Sahrens int error; 2001fa9e4066Sahrens 2002fa9e4066Sahrens /* 2003fa9e4066Sahrens * Normally, partial opens (e.g. of a mirror) are allowed. 2004fa9e4066Sahrens * For a create, however, we want to fail the request if 2005fa9e4066Sahrens * there are any components we can't open. 2006fa9e4066Sahrens */ 2007fa9e4066Sahrens error = vdev_open(vd); 2008fa9e4066Sahrens 2009fa9e4066Sahrens if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2010fa9e4066Sahrens vdev_close(vd); 2011fa9e4066Sahrens return (error ? error : ENXIO); 2012fa9e4066Sahrens } 2013fa9e4066Sahrens 2014fa9e4066Sahrens /* 20150713e232SGeorge Wilson * Recursively load DTLs and initialize all labels. 2016fa9e4066Sahrens */ 20170713e232SGeorge Wilson if ((error = vdev_dtl_load(vd)) != 0 || 20180713e232SGeorge Wilson (error = vdev_label_init(vd, txg, isreplacing ? 201939c23413Seschrock VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2020fa9e4066Sahrens vdev_close(vd); 2021fa9e4066Sahrens return (error); 2022fa9e4066Sahrens } 2023fa9e4066Sahrens 2024fa9e4066Sahrens return (0); 2025fa9e4066Sahrens } 2026fa9e4066Sahrens 20270e34b6a7Sbonwick void 2028573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd) 2029fa9e4066Sahrens { 203086714001SSerapheim Dimitropoulos uint64_t asize = vd->vdev_asize; 203186714001SSerapheim Dimitropoulos uint64_t ms_shift = 0; 203286714001SSerapheim Dimitropoulos 2033fa9e4066Sahrens /* 203486714001SSerapheim Dimitropoulos * For vdevs that are bigger than 8G the metaslab size varies in 203586714001SSerapheim Dimitropoulos * a way that the number of metaslabs increases in powers of two, 203686714001SSerapheim Dimitropoulos * linearly in terms of vdev_asize, starting from 16 metaslabs. 203786714001SSerapheim Dimitropoulos * So for vdev_asize of 8G we get 16 metaslabs, for 16G, we get 32, 203886714001SSerapheim Dimitropoulos * and so on, until we hit the maximum metaslab count limit 203986714001SSerapheim Dimitropoulos * [vdev_max_ms_count] from which point the metaslab count stays 204086714001SSerapheim Dimitropoulos * the same. 2041fa9e4066Sahrens */ 204286714001SSerapheim Dimitropoulos ms_shift = vdev_default_ms_shift; 204386714001SSerapheim Dimitropoulos 204486714001SSerapheim Dimitropoulos if ((asize >> ms_shift) < vdev_min_ms_count) { 204586714001SSerapheim Dimitropoulos /* 204686714001SSerapheim Dimitropoulos * For devices that are less than 8G we want to have 204786714001SSerapheim Dimitropoulos * exactly 16 metaslabs. We don't want less as integer 204886714001SSerapheim Dimitropoulos * division rounds down, so less metaslabs mean more 204986714001SSerapheim Dimitropoulos * wasted space. We don't want more as these vdevs are 205086714001SSerapheim Dimitropoulos * small and in the likely event that we are running 205186714001SSerapheim Dimitropoulos * out of space, the SPA will have a hard time finding 205286714001SSerapheim Dimitropoulos * space due to fragmentation. 205386714001SSerapheim Dimitropoulos */ 205486714001SSerapheim Dimitropoulos ms_shift = highbit64(asize / vdev_min_ms_count); 205586714001SSerapheim Dimitropoulos ms_shift = MAX(ms_shift, SPA_MAXBLOCKSHIFT); 205686714001SSerapheim Dimitropoulos 205786714001SSerapheim Dimitropoulos } else if ((asize >> ms_shift) > vdev_max_ms_count) { 205886714001SSerapheim Dimitropoulos ms_shift = highbit64(asize / vdev_max_ms_count); 205986714001SSerapheim Dimitropoulos } 206086714001SSerapheim Dimitropoulos 206186714001SSerapheim Dimitropoulos vd->vdev_ms_shift = ms_shift; 206286714001SSerapheim Dimitropoulos ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2063fa9e4066Sahrens } 2064fa9e4066Sahrens 2065fa9e4066Sahrens void 2066ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2067fa9e4066Sahrens { 2068ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 20695cabbc6bSPrashanth Sreenivasa /* indirect vdevs don't have metaslabs or dtls */ 20705cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd) || flags == 0); 2071ecc2d604Sbonwick ASSERT(ISP2(flags)); 2072f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 2073fa9e4066Sahrens 2074ecc2d604Sbonwick if (flags & VDD_METASLAB) 2075ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2076ecc2d604Sbonwick 2077ecc2d604Sbonwick if (flags & VDD_DTL) 2078ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2079ecc2d604Sbonwick 2080ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2081fa9e4066Sahrens } 2082fa9e4066Sahrens 20830713e232SGeorge Wilson void 20840713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 20850713e232SGeorge Wilson { 20860713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 20870713e232SGeorge Wilson vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 20880713e232SGeorge Wilson 20890713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) 20900713e232SGeorge Wilson vdev_dirty(vd->vdev_top, flags, vd, txg); 20910713e232SGeorge Wilson } 20920713e232SGeorge Wilson 20938ad4d6ddSJeff Bonwick /* 20948ad4d6ddSJeff Bonwick * DTLs. 20958ad4d6ddSJeff Bonwick * 20968ad4d6ddSJeff Bonwick * A vdev's DTL (dirty time log) is the set of transaction groups for which 20979fb35debSEric Taylor * the vdev has less than perfect replication. There are four kinds of DTL: 20988ad4d6ddSJeff Bonwick * 20998ad4d6ddSJeff Bonwick * DTL_MISSING: txgs for which the vdev has no valid copies of the data 21008ad4d6ddSJeff Bonwick * 21018ad4d6ddSJeff Bonwick * DTL_PARTIAL: txgs for which data is available, but not fully replicated 21028ad4d6ddSJeff Bonwick * 21038ad4d6ddSJeff Bonwick * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 21048ad4d6ddSJeff Bonwick * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 21058ad4d6ddSJeff Bonwick * txgs that was scrubbed. 21068ad4d6ddSJeff Bonwick * 21078ad4d6ddSJeff Bonwick * DTL_OUTAGE: txgs which cannot currently be read, whether due to 21088ad4d6ddSJeff Bonwick * persistent errors or just some device being offline. 21098ad4d6ddSJeff Bonwick * Unlike the other three, the DTL_OUTAGE map is not generally 21108ad4d6ddSJeff Bonwick * maintained; it's only computed when needed, typically to 21118ad4d6ddSJeff Bonwick * determine whether a device can be detached. 21128ad4d6ddSJeff Bonwick * 21138ad4d6ddSJeff Bonwick * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 21148ad4d6ddSJeff Bonwick * either has the data or it doesn't. 21158ad4d6ddSJeff Bonwick * 21168ad4d6ddSJeff Bonwick * For interior vdevs such as mirror and RAID-Z the picture is more complex. 21178ad4d6ddSJeff Bonwick * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 21188ad4d6ddSJeff Bonwick * if any child is less than fully replicated, then so is its parent. 21198ad4d6ddSJeff Bonwick * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 21208ad4d6ddSJeff Bonwick * comprising only those txgs which appear in 'maxfaults' or more children; 21218ad4d6ddSJeff Bonwick * those are the txgs we don't have enough replication to read. For example, 21228ad4d6ddSJeff Bonwick * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 21238ad4d6ddSJeff Bonwick * thus, its DTL_MISSING consists of the set of txgs that appear in more than 21248ad4d6ddSJeff Bonwick * two child DTL_MISSING maps. 21258ad4d6ddSJeff Bonwick * 21268ad4d6ddSJeff Bonwick * It should be clear from the above that to compute the DTLs and outage maps 21278ad4d6ddSJeff Bonwick * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 21288ad4d6ddSJeff Bonwick * Therefore, that is all we keep on disk. When loading the pool, or after 21298ad4d6ddSJeff Bonwick * a configuration change, we generate all other DTLs from first principles. 21308ad4d6ddSJeff Bonwick */ 2131fa9e4066Sahrens void 21328ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2133fa9e4066Sahrens { 21340713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21358ad4d6ddSJeff Bonwick 21368ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 21378ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2138f9af39baSGeorge Wilson ASSERT(spa_writeable(vd->vdev_spa)); 21398ad4d6ddSJeff Bonwick 21405cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 21410713e232SGeorge Wilson if (!range_tree_contains(rt, txg, size)) 21420713e232SGeorge Wilson range_tree_add(rt, txg, size); 21435cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2144fa9e4066Sahrens } 2145fa9e4066Sahrens 21468ad4d6ddSJeff Bonwick boolean_t 21478ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2148fa9e4066Sahrens { 21490713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21508ad4d6ddSJeff Bonwick boolean_t dirty = B_FALSE; 2151fa9e4066Sahrens 21528ad4d6ddSJeff Bonwick ASSERT(t < DTL_TYPES); 21538ad4d6ddSJeff Bonwick ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2154fa9e4066Sahrens 21555cabbc6bSPrashanth Sreenivasa /* 21565cabbc6bSPrashanth Sreenivasa * While we are loading the pool, the DTLs have not been loaded yet. 21575cabbc6bSPrashanth Sreenivasa * Ignore the DTLs and try all devices. This avoids a recursive 21585cabbc6bSPrashanth Sreenivasa * mutex enter on the vdev_dtl_lock, and also makes us try hard 21595cabbc6bSPrashanth Sreenivasa * when loading the pool (relying on the checksum to ensure that 21605cabbc6bSPrashanth Sreenivasa * we get the right data -- note that we while loading, we are 21615cabbc6bSPrashanth Sreenivasa * only reading the MOS, which is always checksummed). 21625cabbc6bSPrashanth Sreenivasa */ 21635cabbc6bSPrashanth Sreenivasa if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 21645cabbc6bSPrashanth Sreenivasa return (B_FALSE); 21655cabbc6bSPrashanth Sreenivasa 21665cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 216786714001SSerapheim Dimitropoulos if (!range_tree_is_empty(rt)) 21680713e232SGeorge Wilson dirty = range_tree_contains(rt, txg, size); 21695cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 2170fa9e4066Sahrens 2171fa9e4066Sahrens return (dirty); 2172fa9e4066Sahrens } 2173fa9e4066Sahrens 21748ad4d6ddSJeff Bonwick boolean_t 21758ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 21768ad4d6ddSJeff Bonwick { 21770713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t]; 21788ad4d6ddSJeff Bonwick boolean_t empty; 21798ad4d6ddSJeff Bonwick 21805cabbc6bSPrashanth Sreenivasa mutex_enter(&vd->vdev_dtl_lock); 218186714001SSerapheim Dimitropoulos empty = range_tree_is_empty(rt); 21825cabbc6bSPrashanth Sreenivasa mutex_exit(&vd->vdev_dtl_lock); 21838ad4d6ddSJeff Bonwick 21848ad4d6ddSJeff Bonwick return (empty); 21858ad4d6ddSJeff Bonwick } 21868ad4d6ddSJeff Bonwick 2187b4952e17SGeorge Wilson /* 2188b4952e17SGeorge Wilson * Returns the lowest txg in the DTL range. 2189b4952e17SGeorge Wilson */ 2190b4952e17SGeorge Wilson static uint64_t 2191b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd) 2192b4952e17SGeorge Wilson { 21930713e232SGeorge Wilson range_seg_t *rs; 2194b4952e17SGeorge Wilson 2195b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 21960713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2197b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2198b4952e17SGeorge Wilson 21990713e232SGeorge Wilson rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 22000713e232SGeorge Wilson return (rs->rs_start - 1); 2201b4952e17SGeorge Wilson } 2202b4952e17SGeorge Wilson 2203b4952e17SGeorge Wilson /* 2204b4952e17SGeorge Wilson * Returns the highest txg in the DTL. 2205b4952e17SGeorge Wilson */ 2206b4952e17SGeorge Wilson static uint64_t 2207b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd) 2208b4952e17SGeorge Wilson { 22090713e232SGeorge Wilson range_seg_t *rs; 2210b4952e17SGeorge Wilson 2211b4952e17SGeorge Wilson ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 22120713e232SGeorge Wilson ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2213b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2214b4952e17SGeorge Wilson 22150713e232SGeorge Wilson rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 22160713e232SGeorge Wilson return (rs->rs_end); 2217b4952e17SGeorge Wilson } 2218b4952e17SGeorge Wilson 2219b4952e17SGeorge Wilson /* 2220b4952e17SGeorge Wilson * Determine if a resilvering vdev should remove any DTL entries from 2221b4952e17SGeorge Wilson * its range. If the vdev was resilvering for the entire duration of the 2222b4952e17SGeorge Wilson * scan then it should excise that range from its DTLs. Otherwise, this 2223b4952e17SGeorge Wilson * vdev is considered partially resilvered and should leave its DTL 2224b4952e17SGeorge Wilson * entries intact. The comment in vdev_dtl_reassess() describes how we 2225b4952e17SGeorge Wilson * excise the DTLs. 2226b4952e17SGeorge Wilson */ 2227b4952e17SGeorge Wilson static boolean_t 2228b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd) 2229b4952e17SGeorge Wilson { 2230b4952e17SGeorge Wilson spa_t *spa = vd->vdev_spa; 2231b4952e17SGeorge Wilson dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2232b4952e17SGeorge Wilson 2233b4952e17SGeorge Wilson ASSERT0(scn->scn_phys.scn_errors); 2234b4952e17SGeorge Wilson ASSERT0(vd->vdev_children); 2235b4952e17SGeorge Wilson 22362d2f193aSMatthew Ahrens if (vd->vdev_state < VDEV_STATE_DEGRADED) 22372d2f193aSMatthew Ahrens return (B_FALSE); 22382d2f193aSMatthew Ahrens 2239b4952e17SGeorge Wilson if (vd->vdev_resilver_txg == 0 || 224086714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2241b4952e17SGeorge Wilson return (B_TRUE); 2242b4952e17SGeorge Wilson 2243b4952e17SGeorge Wilson /* 2244b4952e17SGeorge Wilson * When a resilver is initiated the scan will assign the scn_max_txg 2245b4952e17SGeorge Wilson * value to the highest txg value that exists in all DTLs. If this 2246b4952e17SGeorge Wilson * device's max DTL is not part of this scan (i.e. it is not in 2247b4952e17SGeorge Wilson * the range (scn_min_txg, scn_max_txg] then it is not eligible 2248b4952e17SGeorge Wilson * for excision. 2249b4952e17SGeorge Wilson */ 2250b4952e17SGeorge Wilson if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2251b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2252b4952e17SGeorge Wilson ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2253b4952e17SGeorge Wilson ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2254b4952e17SGeorge Wilson return (B_TRUE); 2255b4952e17SGeorge Wilson } 2256b4952e17SGeorge Wilson return (B_FALSE); 2257b4952e17SGeorge Wilson } 2258b4952e17SGeorge Wilson 2259fa9e4066Sahrens /* 2260fa9e4066Sahrens * Reassess DTLs after a config change or scrub completion. 2261fa9e4066Sahrens */ 2262fa9e4066Sahrens void 2263fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2264fa9e4066Sahrens { 2265ea8dc4b6Seschrock spa_t *spa = vd->vdev_spa; 22668ad4d6ddSJeff Bonwick avl_tree_t reftree; 22678ad4d6ddSJeff Bonwick int minref; 2268fa9e4066Sahrens 22698ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2270fa9e4066Sahrens 22718ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 22728ad4d6ddSJeff Bonwick vdev_dtl_reassess(vd->vdev_child[c], txg, 22738ad4d6ddSJeff Bonwick scrub_txg, scrub_done); 22748ad4d6ddSJeff Bonwick 22755cabbc6bSPrashanth Sreenivasa if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 22768ad4d6ddSJeff Bonwick return; 22778ad4d6ddSJeff Bonwick 22788ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 22793f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 22803f9d6ad7SLin Ling 2281fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 2282b4952e17SGeorge Wilson 2283b4952e17SGeorge Wilson /* 2284b4952e17SGeorge Wilson * If we've completed a scan cleanly then determine 2285b4952e17SGeorge Wilson * if this vdev should remove any DTLs. We only want to 2286b4952e17SGeorge Wilson * excise regions on vdevs that were available during 2287b4952e17SGeorge Wilson * the entire duration of this scan. 2288b4952e17SGeorge Wilson */ 2289088f3894Sahrens if (scrub_txg != 0 && 22903f9d6ad7SLin Ling (spa->spa_scrub_started || 2291b4952e17SGeorge Wilson (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2292b4952e17SGeorge Wilson vdev_dtl_should_excise(vd)) { 2293088f3894Sahrens /* 2294088f3894Sahrens * We completed a scrub up to scrub_txg. If we 2295088f3894Sahrens * did it without rebooting, then the scrub dtl 2296088f3894Sahrens * will be valid, so excise the old region and 2297088f3894Sahrens * fold in the scrub dtl. Otherwise, leave the 2298088f3894Sahrens * dtl as-is if there was an error. 22998ad4d6ddSJeff Bonwick * 23008ad4d6ddSJeff Bonwick * There's little trick here: to excise the beginning 23018ad4d6ddSJeff Bonwick * of the DTL_MISSING map, we put it into a reference 23028ad4d6ddSJeff Bonwick * tree and then add a segment with refcnt -1 that 23038ad4d6ddSJeff Bonwick * covers the range [0, scrub_txg). This means 23048ad4d6ddSJeff Bonwick * that each txg in that range has refcnt -1 or 0. 23058ad4d6ddSJeff Bonwick * We then add DTL_SCRUB with a refcnt of 2, so that 23068ad4d6ddSJeff Bonwick * entries in the range [0, scrub_txg) will have a 23078ad4d6ddSJeff Bonwick * positive refcnt -- either 1 or 2. We then convert 23088ad4d6ddSJeff Bonwick * the reference tree into the new DTL_MISSING map. 2309088f3894Sahrens */ 23100713e232SGeorge Wilson space_reftree_create(&reftree); 23110713e232SGeorge Wilson space_reftree_add_map(&reftree, 23120713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 23130713e232SGeorge Wilson space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 23140713e232SGeorge Wilson space_reftree_add_map(&reftree, 23150713e232SGeorge Wilson vd->vdev_dtl[DTL_SCRUB], 2); 23160713e232SGeorge Wilson space_reftree_generate_map(&reftree, 23170713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], 1); 23180713e232SGeorge Wilson space_reftree_destroy(&reftree); 2319fa9e4066Sahrens } 23200713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 23210713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 23220713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2323fa9e4066Sahrens if (scrub_done) 23240713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 23250713e232SGeorge Wilson range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 23268ad4d6ddSJeff Bonwick if (!vdev_readable(vd)) 23270713e232SGeorge Wilson range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 23288ad4d6ddSJeff Bonwick else 23290713e232SGeorge Wilson range_tree_walk(vd->vdev_dtl[DTL_MISSING], 23300713e232SGeorge Wilson range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2331b4952e17SGeorge Wilson 2332b4952e17SGeorge Wilson /* 2333b4952e17SGeorge Wilson * If the vdev was resilvering and no longer has any 2334b4952e17SGeorge Wilson * DTLs then reset its resilvering flag. 2335b4952e17SGeorge Wilson */ 2336b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0 && 233786714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 233886714001SSerapheim Dimitropoulos range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2339b4952e17SGeorge Wilson vd->vdev_resilver_txg = 0; 2340b4952e17SGeorge Wilson 2341fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2342088f3894Sahrens 2343ecc2d604Sbonwick if (txg != 0) 2344ecc2d604Sbonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2345fa9e4066Sahrens return; 2346fa9e4066Sahrens } 2347fa9e4066Sahrens 2348fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 23498ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) { 235099bb17e2SEric Taylor /* account for child's outage in parent's missing map */ 235199bb17e2SEric Taylor int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 23528ad4d6ddSJeff Bonwick if (t == DTL_SCRUB) 23538ad4d6ddSJeff Bonwick continue; /* leaf vdevs only */ 23548ad4d6ddSJeff Bonwick if (t == DTL_PARTIAL) 23558ad4d6ddSJeff Bonwick minref = 1; /* i.e. non-zero */ 23568ad4d6ddSJeff Bonwick else if (vd->vdev_nparity != 0) 23578ad4d6ddSJeff Bonwick minref = vd->vdev_nparity + 1; /* RAID-Z */ 23588ad4d6ddSJeff Bonwick else 23598ad4d6ddSJeff Bonwick minref = vd->vdev_children; /* any kind of mirror */ 23600713e232SGeorge Wilson space_reftree_create(&reftree); 23618ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 23628ad4d6ddSJeff Bonwick vdev_t *cvd = vd->vdev_child[c]; 23638ad4d6ddSJeff Bonwick mutex_enter(&cvd->vdev_dtl_lock); 23640713e232SGeorge Wilson space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 23658ad4d6ddSJeff Bonwick mutex_exit(&cvd->vdev_dtl_lock); 23668ad4d6ddSJeff Bonwick } 23670713e232SGeorge Wilson space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 23680713e232SGeorge Wilson space_reftree_destroy(&reftree); 2369fa9e4066Sahrens } 23708ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_dtl_lock); 2371fa9e4066Sahrens } 2372fa9e4066Sahrens 23730713e232SGeorge Wilson int 2374fa9e4066Sahrens vdev_dtl_load(vdev_t *vd) 2375fa9e4066Sahrens { 2376fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2377ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 23780713e232SGeorge Wilson int error = 0; 2379fa9e4066Sahrens 23800713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 23815cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 2382fa9e4066Sahrens 23830713e232SGeorge Wilson error = space_map_open(&vd->vdev_dtl_sm, mos, 23845cabbc6bSPrashanth Sreenivasa vd->vdev_dtl_object, 0, -1ULL, 0); 23850713e232SGeorge Wilson if (error) 23860713e232SGeorge Wilson return (error); 23870713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2388fa9e4066Sahrens 23890713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 239088ecc943SGeorge Wilson 23910713e232SGeorge Wilson /* 23920713e232SGeorge Wilson * Now that we've opened the space_map we need to update 23930713e232SGeorge Wilson * the in-core DTL. 23940713e232SGeorge Wilson */ 23950713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 2396ecc2d604Sbonwick 23970713e232SGeorge Wilson error = space_map_load(vd->vdev_dtl_sm, 23980713e232SGeorge Wilson vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 23990713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2400fa9e4066Sahrens 24010713e232SGeorge Wilson return (error); 24020713e232SGeorge Wilson } 24030713e232SGeorge Wilson 24040713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 24050713e232SGeorge Wilson error = vdev_dtl_load(vd->vdev_child[c]); 24060713e232SGeorge Wilson if (error != 0) 24070713e232SGeorge Wilson break; 24080713e232SGeorge Wilson } 2409fa9e4066Sahrens 2410fa9e4066Sahrens return (error); 2411fa9e4066Sahrens } 2412fa9e4066Sahrens 2413215198a6SJoe Stein void 2414215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2415215198a6SJoe Stein { 2416215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2417215198a6SJoe Stein 2418215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2419215198a6SJoe Stein VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2420215198a6SJoe Stein zapobj, tx)); 2421215198a6SJoe Stein } 2422215198a6SJoe Stein 2423215198a6SJoe Stein uint64_t 2424215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2425215198a6SJoe Stein { 2426215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2427215198a6SJoe Stein uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2428215198a6SJoe Stein DMU_OT_NONE, 0, tx); 2429215198a6SJoe Stein 2430215198a6SJoe Stein ASSERT(zap != 0); 2431215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2432215198a6SJoe Stein zap, tx)); 2433215198a6SJoe Stein 2434215198a6SJoe Stein return (zap); 2435215198a6SJoe Stein } 2436215198a6SJoe Stein 2437215198a6SJoe Stein void 2438215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2439215198a6SJoe Stein { 2440215198a6SJoe Stein if (vd->vdev_ops != &vdev_hole_ops && 2441215198a6SJoe Stein vd->vdev_ops != &vdev_missing_ops && 2442215198a6SJoe Stein vd->vdev_ops != &vdev_root_ops && 2443215198a6SJoe Stein !vd->vdev_top->vdev_removing) { 2444215198a6SJoe Stein if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2445215198a6SJoe Stein vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2446215198a6SJoe Stein } 2447215198a6SJoe Stein if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2448215198a6SJoe Stein vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2449215198a6SJoe Stein } 2450215198a6SJoe Stein } 2451215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2452215198a6SJoe Stein vdev_construct_zaps(vd->vdev_child[i], tx); 2453215198a6SJoe Stein } 2454215198a6SJoe Stein } 2455215198a6SJoe Stein 2456fa9e4066Sahrens void 2457fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2458fa9e4066Sahrens { 2459fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 24600713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2461ecc2d604Sbonwick objset_t *mos = spa->spa_meta_objset; 24620713e232SGeorge Wilson range_tree_t *rtsync; 2463fa9e4066Sahrens dmu_tx_t *tx; 24640713e232SGeorge Wilson uint64_t object = space_map_object(vd->vdev_dtl_sm); 2465fa9e4066Sahrens 24665cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 24670713e232SGeorge Wilson ASSERT(vd->vdev_ops->vdev_op_leaf); 246888ecc943SGeorge Wilson 2469fa9e4066Sahrens tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2470fa9e4066Sahrens 24710713e232SGeorge Wilson if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 24720713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 24730713e232SGeorge Wilson space_map_free(vd->vdev_dtl_sm, tx); 24740713e232SGeorge Wilson space_map_close(vd->vdev_dtl_sm); 24750713e232SGeorge Wilson vd->vdev_dtl_sm = NULL; 24760713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2477215198a6SJoe Stein 2478215198a6SJoe Stein /* 2479215198a6SJoe Stein * We only destroy the leaf ZAP for detached leaves or for 2480215198a6SJoe Stein * removed log devices. Removed data devices handle leaf ZAP 2481215198a6SJoe Stein * cleanup later, once cancellation is no longer possible. 2482215198a6SJoe Stein */ 2483215198a6SJoe Stein if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2484215198a6SJoe Stein vd->vdev_top->vdev_islog)) { 2485215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2486215198a6SJoe Stein vd->vdev_leaf_zap = 0; 2487215198a6SJoe Stein } 2488215198a6SJoe Stein 2489fa9e4066Sahrens dmu_tx_commit(tx); 2490fa9e4066Sahrens return; 2491fa9e4066Sahrens } 2492fa9e4066Sahrens 24930713e232SGeorge Wilson if (vd->vdev_dtl_sm == NULL) { 24940713e232SGeorge Wilson uint64_t new_object; 24950713e232SGeorge Wilson 249686714001SSerapheim Dimitropoulos new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx); 24970713e232SGeorge Wilson VERIFY3U(new_object, !=, 0); 24980713e232SGeorge Wilson 24990713e232SGeorge Wilson VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 25005cabbc6bSPrashanth Sreenivasa 0, -1ULL, 0)); 25010713e232SGeorge Wilson ASSERT(vd->vdev_dtl_sm != NULL); 2502fa9e4066Sahrens } 2503fa9e4066Sahrens 25045cabbc6bSPrashanth Sreenivasa rtsync = range_tree_create(NULL, NULL); 2505fa9e4066Sahrens 2506fa9e4066Sahrens mutex_enter(&vd->vdev_dtl_lock); 25070713e232SGeorge Wilson range_tree_walk(rt, range_tree_add, rtsync); 2508fa9e4066Sahrens mutex_exit(&vd->vdev_dtl_lock); 2509fa9e4066Sahrens 251086714001SSerapheim Dimitropoulos space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx); 251117f11284SSerapheim Dimitropoulos space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 25120713e232SGeorge Wilson range_tree_vacate(rtsync, NULL, NULL); 2513fa9e4066Sahrens 25140713e232SGeorge Wilson range_tree_destroy(rtsync); 2515fa9e4066Sahrens 25160713e232SGeorge Wilson /* 25170713e232SGeorge Wilson * If the object for the space map has changed then dirty 25180713e232SGeorge Wilson * the top level so that we update the config. 25190713e232SGeorge Wilson */ 25200713e232SGeorge Wilson if (object != space_map_object(vd->vdev_dtl_sm)) { 25213ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 25223ee8c80cSPavel Zakharov "new object %llu", (u_longlong_t)txg, spa_name(spa), 25233ee8c80cSPavel Zakharov (u_longlong_t)object, 25243ee8c80cSPavel Zakharov (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 25250713e232SGeorge Wilson vdev_config_dirty(vd->vdev_top); 25260713e232SGeorge Wilson } 2527fa9e4066Sahrens 2528fa9e4066Sahrens dmu_tx_commit(tx); 25290713e232SGeorge Wilson 25300713e232SGeorge Wilson mutex_enter(&vd->vdev_dtl_lock); 25310713e232SGeorge Wilson space_map_update(vd->vdev_dtl_sm); 25320713e232SGeorge Wilson mutex_exit(&vd->vdev_dtl_lock); 2533fa9e4066Sahrens } 2534fa9e4066Sahrens 25358ad4d6ddSJeff Bonwick /* 25368ad4d6ddSJeff Bonwick * Determine whether the specified vdev can be offlined/detached/removed 25378ad4d6ddSJeff Bonwick * without losing data. 25388ad4d6ddSJeff Bonwick */ 25398ad4d6ddSJeff Bonwick boolean_t 25408ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd) 25418ad4d6ddSJeff Bonwick { 25428ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 25438ad4d6ddSJeff Bonwick vdev_t *tvd = vd->vdev_top; 25448ad4d6ddSJeff Bonwick uint8_t cant_read = vd->vdev_cant_read; 25458ad4d6ddSJeff Bonwick boolean_t required; 25468ad4d6ddSJeff Bonwick 25478ad4d6ddSJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 25488ad4d6ddSJeff Bonwick 25498ad4d6ddSJeff Bonwick if (vd == spa->spa_root_vdev || vd == tvd) 25508ad4d6ddSJeff Bonwick return (B_TRUE); 25518ad4d6ddSJeff Bonwick 25528ad4d6ddSJeff Bonwick /* 25538ad4d6ddSJeff Bonwick * Temporarily mark the device as unreadable, and then determine 25548ad4d6ddSJeff Bonwick * whether this results in any DTL outages in the top-level vdev. 25558ad4d6ddSJeff Bonwick * If not, we can safely offline/detach/remove the device. 25568ad4d6ddSJeff Bonwick */ 25578ad4d6ddSJeff Bonwick vd->vdev_cant_read = B_TRUE; 25588ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 25598ad4d6ddSJeff Bonwick required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 25608ad4d6ddSJeff Bonwick vd->vdev_cant_read = cant_read; 25618ad4d6ddSJeff Bonwick vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 25628ad4d6ddSJeff Bonwick 2563cb04b873SMark J Musante if (!required && zio_injection_enabled) 2564cb04b873SMark J Musante required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2565cb04b873SMark J Musante 25668ad4d6ddSJeff Bonwick return (required); 25678ad4d6ddSJeff Bonwick } 25688ad4d6ddSJeff Bonwick 2569088f3894Sahrens /* 2570088f3894Sahrens * Determine if resilver is needed, and if so the txg range. 2571088f3894Sahrens */ 2572088f3894Sahrens boolean_t 2573088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2574088f3894Sahrens { 2575088f3894Sahrens boolean_t needed = B_FALSE; 2576088f3894Sahrens uint64_t thismin = UINT64_MAX; 2577088f3894Sahrens uint64_t thismax = 0; 2578088f3894Sahrens 2579088f3894Sahrens if (vd->vdev_children == 0) { 2580088f3894Sahrens mutex_enter(&vd->vdev_dtl_lock); 258186714001SSerapheim Dimitropoulos if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 25828ad4d6ddSJeff Bonwick vdev_writeable(vd)) { 2583088f3894Sahrens 2584b4952e17SGeorge Wilson thismin = vdev_dtl_min(vd); 2585b4952e17SGeorge Wilson thismax = vdev_dtl_max(vd); 2586088f3894Sahrens needed = B_TRUE; 2587088f3894Sahrens } 2588088f3894Sahrens mutex_exit(&vd->vdev_dtl_lock); 2589088f3894Sahrens } else { 25908ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 2591088f3894Sahrens vdev_t *cvd = vd->vdev_child[c]; 2592088f3894Sahrens uint64_t cmin, cmax; 2593088f3894Sahrens 2594088f3894Sahrens if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2595088f3894Sahrens thismin = MIN(thismin, cmin); 2596088f3894Sahrens thismax = MAX(thismax, cmax); 2597088f3894Sahrens needed = B_TRUE; 2598088f3894Sahrens } 2599088f3894Sahrens } 2600088f3894Sahrens } 2601088f3894Sahrens 2602088f3894Sahrens if (needed && minp) { 2603088f3894Sahrens *minp = thismin; 2604088f3894Sahrens *maxp = thismax; 2605088f3894Sahrens } 2606088f3894Sahrens return (needed); 2607088f3894Sahrens } 2608088f3894Sahrens 260986714001SSerapheim Dimitropoulos /* 261086714001SSerapheim Dimitropoulos * Gets the checkpoint space map object from the vdev's ZAP. 261186714001SSerapheim Dimitropoulos * Returns the spacemap object, or 0 if it wasn't in the ZAP 261286714001SSerapheim Dimitropoulos * or the ZAP doesn't exist yet. 261386714001SSerapheim Dimitropoulos */ 261486714001SSerapheim Dimitropoulos int 261586714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd) 261686714001SSerapheim Dimitropoulos { 261786714001SSerapheim Dimitropoulos ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 261886714001SSerapheim Dimitropoulos if (vd->vdev_top_zap == 0) { 261986714001SSerapheim Dimitropoulos return (0); 262086714001SSerapheim Dimitropoulos } 262186714001SSerapheim Dimitropoulos 262286714001SSerapheim Dimitropoulos uint64_t sm_obj = 0; 262386714001SSerapheim Dimitropoulos int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 262486714001SSerapheim Dimitropoulos VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 262586714001SSerapheim Dimitropoulos 262686714001SSerapheim Dimitropoulos ASSERT(err == 0 || err == ENOENT); 262786714001SSerapheim Dimitropoulos 262886714001SSerapheim Dimitropoulos return (sm_obj); 262986714001SSerapheim Dimitropoulos } 263086714001SSerapheim Dimitropoulos 26315cabbc6bSPrashanth Sreenivasa int 2632ea8dc4b6Seschrock vdev_load(vdev_t *vd) 2633fa9e4066Sahrens { 26345cabbc6bSPrashanth Sreenivasa int error = 0; 2635fa9e4066Sahrens /* 2636fa9e4066Sahrens * Recursively load all children. 2637fa9e4066Sahrens */ 26385cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 26395cabbc6bSPrashanth Sreenivasa error = vdev_load(vd->vdev_child[c]); 26405cabbc6bSPrashanth Sreenivasa if (error != 0) { 26415cabbc6bSPrashanth Sreenivasa return (error); 26425cabbc6bSPrashanth Sreenivasa } 26435cabbc6bSPrashanth Sreenivasa } 26445cabbc6bSPrashanth Sreenivasa 26455cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 2646fa9e4066Sahrens 2647fa9e4066Sahrens /* 26480e34b6a7Sbonwick * If this is a top-level vdev, initialize its metaslabs. 2649fa9e4066Sahrens */ 26505cabbc6bSPrashanth Sreenivasa if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 26515cabbc6bSPrashanth Sreenivasa if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 26525cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 26535cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 26543ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 26553ee8c80cSPavel Zakharov "asize=%llu", (u_longlong_t)vd->vdev_ashift, 26563ee8c80cSPavel Zakharov (u_longlong_t)vd->vdev_asize); 26575cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 26585cabbc6bSPrashanth Sreenivasa } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 26593ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 26603ee8c80cSPavel Zakharov "[error=%d]", error); 26615cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 26625cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 26635cabbc6bSPrashanth Sreenivasa return (error); 26645cabbc6bSPrashanth Sreenivasa } 266586714001SSerapheim Dimitropoulos 266686714001SSerapheim Dimitropoulos uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 266786714001SSerapheim Dimitropoulos if (checkpoint_sm_obj != 0) { 266886714001SSerapheim Dimitropoulos objset_t *mos = spa_meta_objset(vd->vdev_spa); 266986714001SSerapheim Dimitropoulos ASSERT(vd->vdev_asize != 0); 267086714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 267186714001SSerapheim Dimitropoulos 267286714001SSerapheim Dimitropoulos if ((error = space_map_open(&vd->vdev_checkpoint_sm, 267386714001SSerapheim Dimitropoulos mos, checkpoint_sm_obj, 0, vd->vdev_asize, 267486714001SSerapheim Dimitropoulos vd->vdev_ashift))) { 267586714001SSerapheim Dimitropoulos vdev_dbgmsg(vd, "vdev_load: space_map_open " 267686714001SSerapheim Dimitropoulos "failed for checkpoint spacemap (obj %llu) " 267786714001SSerapheim Dimitropoulos "[error=%d]", 267886714001SSerapheim Dimitropoulos (u_longlong_t)checkpoint_sm_obj, error); 267986714001SSerapheim Dimitropoulos return (error); 268086714001SSerapheim Dimitropoulos } 268186714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 268286714001SSerapheim Dimitropoulos space_map_update(vd->vdev_checkpoint_sm); 268386714001SSerapheim Dimitropoulos 268486714001SSerapheim Dimitropoulos /* 268586714001SSerapheim Dimitropoulos * Since the checkpoint_sm contains free entries 268686714001SSerapheim Dimitropoulos * exclusively we can use sm_alloc to indicate the 268786714001SSerapheim Dimitropoulos * culmulative checkpointed space that has been freed. 268886714001SSerapheim Dimitropoulos */ 268986714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space = 269086714001SSerapheim Dimitropoulos -vd->vdev_checkpoint_sm->sm_alloc; 269186714001SSerapheim Dimitropoulos vd->vdev_spa->spa_checkpoint_info.sci_dspace += 269286714001SSerapheim Dimitropoulos vd->vdev_stat.vs_checkpoint_space; 269386714001SSerapheim Dimitropoulos } 26945cabbc6bSPrashanth Sreenivasa } 2695fa9e4066Sahrens 2696fa9e4066Sahrens /* 2697fa9e4066Sahrens * If this is a leaf vdev, load its DTL. 2698fa9e4066Sahrens */ 26995cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2700560e6e96Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2701560e6e96Seschrock VDEV_AUX_CORRUPT_DATA); 27023ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 27033ee8c80cSPavel Zakharov "[error=%d]", error); 27045cabbc6bSPrashanth Sreenivasa return (error); 27055cabbc6bSPrashanth Sreenivasa } 27065cabbc6bSPrashanth Sreenivasa 27075cabbc6bSPrashanth Sreenivasa uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 27085cabbc6bSPrashanth Sreenivasa if (obsolete_sm_object != 0) { 27095cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 27105cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_asize != 0); 271186714001SSerapheim Dimitropoulos ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 27125cabbc6bSPrashanth Sreenivasa 27135cabbc6bSPrashanth Sreenivasa if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 27145cabbc6bSPrashanth Sreenivasa obsolete_sm_object, 0, vd->vdev_asize, 0))) { 27155cabbc6bSPrashanth Sreenivasa vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 27165cabbc6bSPrashanth Sreenivasa VDEV_AUX_CORRUPT_DATA); 27173ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 27183ee8c80cSPavel Zakharov "obsolete spacemap (obj %llu) [error=%d]", 27193ee8c80cSPavel Zakharov (u_longlong_t)obsolete_sm_object, error); 27205cabbc6bSPrashanth Sreenivasa return (error); 27215cabbc6bSPrashanth Sreenivasa } 27225cabbc6bSPrashanth Sreenivasa space_map_update(vd->vdev_obsolete_sm); 27235cabbc6bSPrashanth Sreenivasa } 27245cabbc6bSPrashanth Sreenivasa 27255cabbc6bSPrashanth Sreenivasa return (0); 2726fa9e4066Sahrens } 2727fa9e4066Sahrens 272899653d4eSeschrock /* 2729fa94a07fSbrendan * The special vdev case is used for hot spares and l2cache devices. Its 2730fa94a07fSbrendan * sole purpose it to set the vdev state for the associated vdev. To do this, 2731fa94a07fSbrendan * we make sure that we can open the underlying device, then try to read the 2732fa94a07fSbrendan * label, and make sure that the label is sane and that it hasn't been 2733fa94a07fSbrendan * repurposed to another pool. 273499653d4eSeschrock */ 273599653d4eSeschrock int 2736fa94a07fSbrendan vdev_validate_aux(vdev_t *vd) 273799653d4eSeschrock { 273899653d4eSeschrock nvlist_t *label; 273999653d4eSeschrock uint64_t guid, version; 274099653d4eSeschrock uint64_t state; 274199653d4eSeschrock 2742e14bb325SJeff Bonwick if (!vdev_readable(vd)) 2743c5904d13Seschrock return (0); 2744c5904d13Seschrock 2745dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 274699653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 274799653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 274899653d4eSeschrock return (-1); 274999653d4eSeschrock } 275099653d4eSeschrock 275199653d4eSeschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2752ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(version) || 275399653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 275499653d4eSeschrock guid != vd->vdev_guid || 275599653d4eSeschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 275699653d4eSeschrock vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 275799653d4eSeschrock VDEV_AUX_CORRUPT_DATA); 275899653d4eSeschrock nvlist_free(label); 275999653d4eSeschrock return (-1); 276099653d4eSeschrock } 276199653d4eSeschrock 276299653d4eSeschrock /* 276399653d4eSeschrock * We don't actually check the pool state here. If it's in fact in 276499653d4eSeschrock * use by another pool, we update this fact on the fly when requested. 276599653d4eSeschrock */ 276699653d4eSeschrock nvlist_free(label); 276799653d4eSeschrock return (0); 276899653d4eSeschrock } 276999653d4eSeschrock 27705cabbc6bSPrashanth Sreenivasa /* 27715cabbc6bSPrashanth Sreenivasa * Free the objects used to store this vdev's spacemaps, and the array 27725cabbc6bSPrashanth Sreenivasa * that points to them. 27735cabbc6bSPrashanth Sreenivasa */ 277488ecc943SGeorge Wilson void 27755cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 27765cabbc6bSPrashanth Sreenivasa { 27775cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0) 27785cabbc6bSPrashanth Sreenivasa return; 27795cabbc6bSPrashanth Sreenivasa 27805cabbc6bSPrashanth Sreenivasa objset_t *mos = vd->vdev_spa->spa_meta_objset; 27815cabbc6bSPrashanth Sreenivasa uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 27825cabbc6bSPrashanth Sreenivasa size_t array_bytes = array_count * sizeof (uint64_t); 27835cabbc6bSPrashanth Sreenivasa uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 27845cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 27855cabbc6bSPrashanth Sreenivasa array_bytes, smobj_array, 0)); 27865cabbc6bSPrashanth Sreenivasa 27875cabbc6bSPrashanth Sreenivasa for (uint64_t i = 0; i < array_count; i++) { 27885cabbc6bSPrashanth Sreenivasa uint64_t smobj = smobj_array[i]; 27895cabbc6bSPrashanth Sreenivasa if (smobj == 0) 27905cabbc6bSPrashanth Sreenivasa continue; 27915cabbc6bSPrashanth Sreenivasa 27925cabbc6bSPrashanth Sreenivasa space_map_free_obj(mos, smobj, tx); 27935cabbc6bSPrashanth Sreenivasa } 27945cabbc6bSPrashanth Sreenivasa 27955cabbc6bSPrashanth Sreenivasa kmem_free(smobj_array, array_bytes); 27965cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 27975cabbc6bSPrashanth Sreenivasa vd->vdev_ms_array = 0; 27985cabbc6bSPrashanth Sreenivasa } 27995cabbc6bSPrashanth Sreenivasa 28005cabbc6bSPrashanth Sreenivasa static void 28015cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vdev_t *vd, uint64_t txg) 280288ecc943SGeorge Wilson { 280388ecc943SGeorge Wilson spa_t *spa = vd->vdev_spa; 280488ecc943SGeorge Wilson dmu_tx_t *tx; 280588ecc943SGeorge Wilson 2806215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 2807215198a6SJoe Stein ASSERT3U(txg, ==, spa_syncing_txg(spa)); 280888ecc943SGeorge Wilson 280988ecc943SGeorge Wilson if (vd->vdev_ms != NULL) { 28102e4c9986SGeorge Wilson metaslab_group_t *mg = vd->vdev_mg; 28112e4c9986SGeorge Wilson 28122e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 28132e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 28142e4c9986SGeorge Wilson 281588ecc943SGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) { 281688ecc943SGeorge Wilson metaslab_t *msp = vd->vdev_ms[m]; 281788ecc943SGeorge Wilson 28180713e232SGeorge Wilson if (msp == NULL || msp->ms_sm == NULL) 281988ecc943SGeorge Wilson continue; 282088ecc943SGeorge Wilson 28210713e232SGeorge Wilson mutex_enter(&msp->ms_lock); 28222e4c9986SGeorge Wilson /* 28232e4c9986SGeorge Wilson * If the metaslab was not loaded when the vdev 28242e4c9986SGeorge Wilson * was removed then the histogram accounting may 28252e4c9986SGeorge Wilson * not be accurate. Update the histogram information 28262e4c9986SGeorge Wilson * here so that we ensure that the metaslab group 28272e4c9986SGeorge Wilson * and metaslab class are up-to-date. 28282e4c9986SGeorge Wilson */ 28292e4c9986SGeorge Wilson metaslab_group_histogram_remove(mg, msp); 28302e4c9986SGeorge Wilson 28310713e232SGeorge Wilson VERIFY0(space_map_allocated(msp->ms_sm)); 28320713e232SGeorge Wilson space_map_close(msp->ms_sm); 28330713e232SGeorge Wilson msp->ms_sm = NULL; 28340713e232SGeorge Wilson mutex_exit(&msp->ms_lock); 283588ecc943SGeorge Wilson } 28362e4c9986SGeorge Wilson 283786714001SSerapheim Dimitropoulos if (vd->vdev_checkpoint_sm != NULL) { 283886714001SSerapheim Dimitropoulos ASSERT(spa_has_checkpoint(spa)); 283986714001SSerapheim Dimitropoulos space_map_close(vd->vdev_checkpoint_sm); 284086714001SSerapheim Dimitropoulos vd->vdev_checkpoint_sm = NULL; 284186714001SSerapheim Dimitropoulos } 284286714001SSerapheim Dimitropoulos 28432e4c9986SGeorge Wilson metaslab_group_histogram_verify(mg); 28442e4c9986SGeorge Wilson metaslab_class_histogram_verify(mg->mg_class); 28452e4c9986SGeorge Wilson for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 28462e4c9986SGeorge Wilson ASSERT0(mg->mg_histogram[i]); 284788ecc943SGeorge Wilson } 284888ecc943SGeorge Wilson 28495cabbc6bSPrashanth Sreenivasa tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 28505cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vd, tx); 2851215198a6SJoe Stein 2852215198a6SJoe Stein if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2853215198a6SJoe Stein vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2854215198a6SJoe Stein vd->vdev_top_zap = 0; 2855215198a6SJoe Stein } 285688ecc943SGeorge Wilson dmu_tx_commit(tx); 285788ecc943SGeorge Wilson } 285888ecc943SGeorge Wilson 2859fa9e4066Sahrens void 2860fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg) 2861fa9e4066Sahrens { 2862fa9e4066Sahrens metaslab_t *msp; 286380eb36f2SGeorge Wilson boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2864fa9e4066Sahrens 28655cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 286688ecc943SGeorge Wilson 2867094e47e9SGeorge Wilson while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2868094e47e9SGeorge Wilson != NULL) 2869fa9e4066Sahrens metaslab_sync_done(msp, txg); 287080eb36f2SGeorge Wilson 287180eb36f2SGeorge Wilson if (reassess) 287280eb36f2SGeorge Wilson metaslab_sync_reassess(vd->vdev_mg); 2873fa9e4066Sahrens } 2874fa9e4066Sahrens 2875fa9e4066Sahrens void 2876fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg) 2877fa9e4066Sahrens { 2878fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 2879fa9e4066Sahrens vdev_t *lvd; 2880fa9e4066Sahrens metaslab_t *msp; 2881ecc2d604Sbonwick dmu_tx_t *tx; 2882fa9e4066Sahrens 28835cabbc6bSPrashanth Sreenivasa if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 28845cabbc6bSPrashanth Sreenivasa dmu_tx_t *tx; 28855cabbc6bSPrashanth Sreenivasa 28865cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 28875cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 288888ecc943SGeorge Wilson 28895cabbc6bSPrashanth Sreenivasa tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 28905cabbc6bSPrashanth Sreenivasa vdev_indirect_sync_obsolete(vd, tx); 28915cabbc6bSPrashanth Sreenivasa dmu_tx_commit(tx); 28925cabbc6bSPrashanth Sreenivasa 28935cabbc6bSPrashanth Sreenivasa /* 28945cabbc6bSPrashanth Sreenivasa * If the vdev is indirect, it can't have dirty 28955cabbc6bSPrashanth Sreenivasa * metaslabs or DTLs. 28965cabbc6bSPrashanth Sreenivasa */ 28975cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 28985cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 28995cabbc6bSPrashanth Sreenivasa ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 29005cabbc6bSPrashanth Sreenivasa return; 29015cabbc6bSPrashanth Sreenivasa } 29025cabbc6bSPrashanth Sreenivasa } 29035cabbc6bSPrashanth Sreenivasa 29045cabbc6bSPrashanth Sreenivasa ASSERT(vdev_is_concrete(vd)); 29055cabbc6bSPrashanth Sreenivasa 29065cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 29075cabbc6bSPrashanth Sreenivasa !vd->vdev_removing) { 2908ecc2d604Sbonwick ASSERT(vd == vd->vdev_top); 29095cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 2910ecc2d604Sbonwick tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2911ecc2d604Sbonwick vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2912ecc2d604Sbonwick DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2913ecc2d604Sbonwick ASSERT(vd->vdev_ms_array != 0); 2914ecc2d604Sbonwick vdev_config_dirty(vd); 2915ecc2d604Sbonwick dmu_tx_commit(tx); 2916ecc2d604Sbonwick } 2917fa9e4066Sahrens 2918ecc2d604Sbonwick while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2919fa9e4066Sahrens metaslab_sync(msp, txg); 2920ecc2d604Sbonwick (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2921ecc2d604Sbonwick } 2922fa9e4066Sahrens 2923fa9e4066Sahrens while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2924fa9e4066Sahrens vdev_dtl_sync(lvd, txg); 2925fa9e4066Sahrens 29265cabbc6bSPrashanth Sreenivasa /* 29275cabbc6bSPrashanth Sreenivasa * Remove the metadata associated with this vdev once it's empty. 29285cabbc6bSPrashanth Sreenivasa * Note that this is typically used for log/cache device removal; 29295cabbc6bSPrashanth Sreenivasa * we don't empty toplevel vdevs when removing them. But if 29305cabbc6bSPrashanth Sreenivasa * a toplevel happens to be emptied, this is not harmful. 29315cabbc6bSPrashanth Sreenivasa */ 29325cabbc6bSPrashanth Sreenivasa if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) { 29335cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vd, txg); 29345cabbc6bSPrashanth Sreenivasa } 29355cabbc6bSPrashanth Sreenivasa 2936fa9e4066Sahrens (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2937fa9e4066Sahrens } 2938fa9e4066Sahrens 2939fa9e4066Sahrens uint64_t 2940fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2941fa9e4066Sahrens { 2942fa9e4066Sahrens return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2943fa9e4066Sahrens } 2944fa9e4066Sahrens 29453d7072f8Seschrock /* 29463d7072f8Seschrock * Mark the given vdev faulted. A faulted vdev behaves as if the device could 29473d7072f8Seschrock * not be opened, and no I/O is attempted. 29483d7072f8Seschrock */ 2949fa9e4066Sahrens int 2950069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2951fa9e4066Sahrens { 29524b964adaSGeorge Wilson vdev_t *vd, *tvd; 2953fa9e4066Sahrens 29548f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 2955fa9e4066Sahrens 2956c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2957e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2958e14bb325SJeff Bonwick 29593d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 2960e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2961fa9e4066Sahrens 29624b964adaSGeorge Wilson tvd = vd->vdev_top; 29634b964adaSGeorge Wilson 2964069f55e2SEric Schrock /* 2965069f55e2SEric Schrock * We don't directly use the aux state here, but if we do a 2966069f55e2SEric Schrock * vdev_reopen(), we need this value to be present to remember why we 2967069f55e2SEric Schrock * were faulted. 2968069f55e2SEric Schrock */ 2969069f55e2SEric Schrock vd->vdev_label_aux = aux; 2970069f55e2SEric Schrock 29713d7072f8Seschrock /* 29723d7072f8Seschrock * Faulted state takes precedence over degraded. 29733d7072f8Seschrock */ 297498d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 29753d7072f8Seschrock vd->vdev_faulted = 1ULL; 29763d7072f8Seschrock vd->vdev_degraded = 0ULL; 2977069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 29783d7072f8Seschrock 29793d7072f8Seschrock /* 2980c79790bcSGeorge Wilson * If this device has the only valid copy of the data, then 2981c79790bcSGeorge Wilson * back off and simply mark the vdev as degraded instead. 29823d7072f8Seschrock */ 29834b964adaSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 29843d7072f8Seschrock vd->vdev_degraded = 1ULL; 29853d7072f8Seschrock vd->vdev_faulted = 0ULL; 29863d7072f8Seschrock 29873d7072f8Seschrock /* 29883d7072f8Seschrock * If we reopen the device and it's not dead, only then do we 29893d7072f8Seschrock * mark it degraded. 29903d7072f8Seschrock */ 29914b964adaSGeorge Wilson vdev_reopen(tvd); 29923d7072f8Seschrock 2993069f55e2SEric Schrock if (vdev_readable(vd)) 2994069f55e2SEric Schrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 29953d7072f8Seschrock } 29963d7072f8Seschrock 2997e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 29983d7072f8Seschrock } 29993d7072f8Seschrock 30003d7072f8Seschrock /* 30013d7072f8Seschrock * Mark the given vdev degraded. A degraded vdev is purely an indication to the 30023d7072f8Seschrock * user that something is wrong. The vdev continues to operate as normal as far 30033d7072f8Seschrock * as I/O is concerned. 30043d7072f8Seschrock */ 30053d7072f8Seschrock int 3006069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 30073d7072f8Seschrock { 3008c5904d13Seschrock vdev_t *vd; 30090a4e9518Sgw 30108f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 30113d7072f8Seschrock 3012c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3013e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3014e14bb325SJeff Bonwick 30150e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3016e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 30170e34b6a7Sbonwick 30183d7072f8Seschrock /* 30193d7072f8Seschrock * If the vdev is already faulted, then don't do anything. 30203d7072f8Seschrock */ 3021e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded) 3022e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, 0)); 30233d7072f8Seschrock 30243d7072f8Seschrock vd->vdev_degraded = 1ULL; 30253d7072f8Seschrock if (!vdev_is_dead(vd)) 30263d7072f8Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3027069f55e2SEric Schrock aux); 30283d7072f8Seschrock 3029e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 30303d7072f8Seschrock } 30313d7072f8Seschrock 30323d7072f8Seschrock /* 3033f7170741SWill Andrews * Online the given vdev. 3034f7170741SWill Andrews * 3035f7170741SWill Andrews * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3036f7170741SWill Andrews * spare device should be detached when the device finishes resilvering. 3037f7170741SWill Andrews * Second, the online should be treated like a 'test' online case, so no FMA 3038f7170741SWill Andrews * events are generated if the device fails to open. 30393d7072f8Seschrock */ 30403d7072f8Seschrock int 3041e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 30423d7072f8Seschrock { 3043573ca77eSGeorge Wilson vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 30445f368aefSYuri Pankov boolean_t wasoffline; 30455f368aefSYuri Pankov vdev_state_t oldstate; 30463d7072f8Seschrock 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)); 30513d7072f8Seschrock 30523d7072f8Seschrock if (!vd->vdev_ops->vdev_op_leaf) 3053e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3054fa9e4066Sahrens 30555f368aefSYuri Pankov wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 30565f368aefSYuri Pankov oldstate = vd->vdev_state; 305714372834SHans Rosenfeld 3058573ca77eSGeorge Wilson tvd = vd->vdev_top; 3059fa9e4066Sahrens vd->vdev_offline = B_FALSE; 3060441d80aaSlling vd->vdev_tmpoffline = B_FALSE; 3061e14bb325SJeff Bonwick vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3062e14bb325SJeff Bonwick vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3063573ca77eSGeorge Wilson 3064573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3065573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3066573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3067573ca77eSGeorge Wilson pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3068573ca77eSGeorge Wilson } 3069573ca77eSGeorge Wilson 3070573ca77eSGeorge Wilson vdev_reopen(tvd); 30713d7072f8Seschrock vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 30723d7072f8Seschrock 3073573ca77eSGeorge Wilson if (!vd->vdev_aux) { 3074573ca77eSGeorge Wilson for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3075573ca77eSGeorge Wilson pvd->vdev_expanding = B_FALSE; 3076573ca77eSGeorge Wilson } 3077573ca77eSGeorge Wilson 30783d7072f8Seschrock if (newstate) 30793d7072f8Seschrock *newstate = vd->vdev_state; 30803d7072f8Seschrock if ((flags & ZFS_ONLINE_UNSPARE) && 30813d7072f8Seschrock !vdev_is_dead(vd) && vd->vdev_parent && 30823d7072f8Seschrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 30833d7072f8Seschrock vd->vdev_parent->vdev_child[0] == vd) 30843d7072f8Seschrock vd->vdev_unspare = B_TRUE; 3085fa9e4066Sahrens 3086573ca77eSGeorge Wilson if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3087573ca77eSGeorge Wilson 3088573ca77eSGeorge Wilson /* XXX - L2ARC 1.0 does not support expansion */ 3089573ca77eSGeorge Wilson if (vd->vdev_aux) 3090573ca77eSGeorge Wilson return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3091573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3092573ca77eSGeorge Wilson } 309314372834SHans Rosenfeld 3094094e47e9SGeorge Wilson /* Restart initializing if necessary */ 3095094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 3096094e47e9SGeorge Wilson if (vdev_writeable(vd) && 3097094e47e9SGeorge Wilson vd->vdev_initialize_thread == NULL && 3098094e47e9SGeorge Wilson vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3099094e47e9SGeorge Wilson (void) vdev_initialize(vd); 3100094e47e9SGeorge Wilson } 3101094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 3102094e47e9SGeorge Wilson 31035f368aefSYuri Pankov if (wasoffline || 31045f368aefSYuri Pankov (oldstate < VDEV_STATE_DEGRADED && 31055f368aefSYuri Pankov vd->vdev_state >= VDEV_STATE_DEGRADED)) 3106ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 310714372834SHans Rosenfeld 31088ad4d6ddSJeff Bonwick return (spa_vdev_state_exit(spa, vd, 0)); 3109fa9e4066Sahrens } 3110fa9e4066Sahrens 3111a1521560SJeff Bonwick static int 3112a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3113fa9e4066Sahrens { 3114e6ca193dSGeorge Wilson vdev_t *vd, *tvd; 31158f18d1faSGeorge Wilson int error = 0; 31168f18d1faSGeorge Wilson uint64_t generation; 31178f18d1faSGeorge Wilson metaslab_group_t *mg; 31180a4e9518Sgw 31198f18d1faSGeorge Wilson top: 31208f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 3121fa9e4066Sahrens 3122c5904d13Seschrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3123e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3124fa9e4066Sahrens 31250e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 3126e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 31270e34b6a7Sbonwick 3128e6ca193dSGeorge Wilson tvd = vd->vdev_top; 31298f18d1faSGeorge Wilson mg = tvd->vdev_mg; 31308f18d1faSGeorge Wilson generation = spa->spa_config_generation + 1; 3131e6ca193dSGeorge Wilson 3132fa9e4066Sahrens /* 3133ecc2d604Sbonwick * If the device isn't already offline, try to offline it. 3134fa9e4066Sahrens */ 3135ecc2d604Sbonwick if (!vd->vdev_offline) { 3136ecc2d604Sbonwick /* 31378ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 3138e6ca193dSGeorge Wilson * don't allow it to be offlined. Log devices are always 3139e6ca193dSGeorge Wilson * expendable. 3140ecc2d604Sbonwick */ 3141e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3142e6ca193dSGeorge Wilson vdev_dtl_required(vd)) 3143e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3144fa9e4066Sahrens 31458f18d1faSGeorge Wilson /* 3146b24ab676SJeff Bonwick * If the top-level is a slog and it has had allocations 3147b24ab676SJeff Bonwick * then proceed. We check that the vdev's metaslab group 3148b24ab676SJeff Bonwick * is not NULL since it's possible that we may have just 3149b24ab676SJeff Bonwick * added this vdev but not yet initialized its metaslabs. 31508f18d1faSGeorge Wilson */ 31518f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) { 31528f18d1faSGeorge Wilson /* 31538f18d1faSGeorge Wilson * Prevent any future allocations. 31548f18d1faSGeorge Wilson */ 3155a1521560SJeff Bonwick metaslab_group_passivate(mg); 31568f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 31578f18d1faSGeorge Wilson 31585cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 31598f18d1faSGeorge Wilson 316086714001SSerapheim Dimitropoulos /* 316186714001SSerapheim Dimitropoulos * If the log device was successfully reset but has 316286714001SSerapheim Dimitropoulos * checkpointed data, do not offline it. 316386714001SSerapheim Dimitropoulos */ 316486714001SSerapheim Dimitropoulos if (error == 0 && 316586714001SSerapheim Dimitropoulos tvd->vdev_checkpoint_sm != NULL) { 316686714001SSerapheim Dimitropoulos ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc, 316786714001SSerapheim Dimitropoulos !=, 0); 316886714001SSerapheim Dimitropoulos error = ZFS_ERR_CHECKPOINT_EXISTS; 316986714001SSerapheim Dimitropoulos } 317086714001SSerapheim Dimitropoulos 31718f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_ALLOC); 31728f18d1faSGeorge Wilson 31738f18d1faSGeorge Wilson /* 31748f18d1faSGeorge Wilson * Check to see if the config has changed. 31758f18d1faSGeorge Wilson */ 31768f18d1faSGeorge Wilson if (error || generation != spa->spa_config_generation) { 3177a1521560SJeff Bonwick metaslab_group_activate(mg); 31788f18d1faSGeorge Wilson if (error) 31798f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, 31808f18d1faSGeorge Wilson vd, error)); 31818f18d1faSGeorge Wilson (void) spa_vdev_state_exit(spa, vd, 0); 31828f18d1faSGeorge Wilson goto top; 31838f18d1faSGeorge Wilson } 3184fb09f5aaSMadhav Suresh ASSERT0(tvd->vdev_stat.vs_alloc); 31858f18d1faSGeorge Wilson } 31868f18d1faSGeorge Wilson 3187ecc2d604Sbonwick /* 3188ecc2d604Sbonwick * Offline this device and reopen its top-level vdev. 3189e6ca193dSGeorge Wilson * If the top-level vdev is a log device then just offline 3190e6ca193dSGeorge Wilson * it. Otherwise, if this action results in the top-level 3191e6ca193dSGeorge Wilson * vdev becoming unusable, undo it and fail the request. 3192ecc2d604Sbonwick */ 3193ecc2d604Sbonwick vd->vdev_offline = B_TRUE; 3194e6ca193dSGeorge Wilson vdev_reopen(tvd); 3195e6ca193dSGeorge Wilson 3196e6ca193dSGeorge Wilson if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3197e6ca193dSGeorge Wilson vdev_is_dead(tvd)) { 3198ecc2d604Sbonwick vd->vdev_offline = B_FALSE; 3199e6ca193dSGeorge Wilson vdev_reopen(tvd); 3200e14bb325SJeff Bonwick return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3201ecc2d604Sbonwick } 32028f18d1faSGeorge Wilson 32038f18d1faSGeorge Wilson /* 32048f18d1faSGeorge Wilson * Add the device back into the metaslab rotor so that 32058f18d1faSGeorge Wilson * once we online the device it's open for business. 32068f18d1faSGeorge Wilson */ 32078f18d1faSGeorge Wilson if (tvd->vdev_islog && mg != NULL) 3208a1521560SJeff Bonwick metaslab_group_activate(mg); 3209fa9e4066Sahrens } 3210fa9e4066Sahrens 3211e14bb325SJeff Bonwick vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3212ecc2d604Sbonwick 32138f18d1faSGeorge Wilson return (spa_vdev_state_exit(spa, vd, 0)); 3214fa9e4066Sahrens } 3215fa9e4066Sahrens 3216a1521560SJeff Bonwick int 3217a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3218a1521560SJeff Bonwick { 3219a1521560SJeff Bonwick int error; 3220a1521560SJeff Bonwick 3221a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 3222a1521560SJeff Bonwick error = vdev_offline_locked(spa, guid, flags); 3223a1521560SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 3224a1521560SJeff Bonwick 3225a1521560SJeff Bonwick return (error); 3226a1521560SJeff Bonwick } 3227a1521560SJeff Bonwick 3228ea8dc4b6Seschrock /* 3229ea8dc4b6Seschrock * Clear the error counts associated with this vdev. Unlike vdev_online() and 3230ea8dc4b6Seschrock * vdev_offline(), we assume the spa config is locked. We also clear all 3231ea8dc4b6Seschrock * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3232ea8dc4b6Seschrock */ 3233ea8dc4b6Seschrock void 3234e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd) 3235fa9e4066Sahrens { 3236e14bb325SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3237e14bb325SJeff Bonwick 3238e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3239fa9e4066Sahrens 3240ea8dc4b6Seschrock if (vd == NULL) 3241e14bb325SJeff Bonwick vd = rvd; 3242fa9e4066Sahrens 3243ea8dc4b6Seschrock vd->vdev_stat.vs_read_errors = 0; 3244ea8dc4b6Seschrock vd->vdev_stat.vs_write_errors = 0; 3245ea8dc4b6Seschrock vd->vdev_stat.vs_checksum_errors = 0; 3246fa9e4066Sahrens 3247e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 3248e14bb325SJeff Bonwick vdev_clear(spa, vd->vdev_child[c]); 32493d7072f8Seschrock 32505cabbc6bSPrashanth Sreenivasa /* 32515cabbc6bSPrashanth Sreenivasa * It makes no sense to "clear" an indirect vdev. 32525cabbc6bSPrashanth Sreenivasa */ 32535cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(vd)) 32545cabbc6bSPrashanth Sreenivasa return; 32555cabbc6bSPrashanth Sreenivasa 32563d7072f8Seschrock /* 32578a79c1b5Sek * If we're in the FAULTED state or have experienced failed I/O, then 32588a79c1b5Sek * clear the persistent state and attempt to reopen the device. We 32598a79c1b5Sek * also mark the vdev config dirty, so that the new faulted state is 32608a79c1b5Sek * written out to disk. 32613d7072f8Seschrock */ 3262e14bb325SJeff Bonwick if (vd->vdev_faulted || vd->vdev_degraded || 3263e14bb325SJeff Bonwick !vdev_readable(vd) || !vdev_writeable(vd)) { 32648a79c1b5Sek 3265096d22d4SEric Schrock /* 3266096d22d4SEric Schrock * When reopening in reponse to a clear event, it may be due to 3267096d22d4SEric Schrock * a fmadm repair request. In this case, if the device is 3268096d22d4SEric Schrock * still broken, we want to still post the ereport again. 3269096d22d4SEric Schrock */ 3270096d22d4SEric Schrock vd->vdev_forcefault = B_TRUE; 3271096d22d4SEric Schrock 32724b964adaSGeorge Wilson vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3273e14bb325SJeff Bonwick vd->vdev_cant_read = B_FALSE; 3274e14bb325SJeff Bonwick vd->vdev_cant_write = B_FALSE; 3275e14bb325SJeff Bonwick 3276f9af39baSGeorge Wilson vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 32773d7072f8Seschrock 3278096d22d4SEric Schrock vd->vdev_forcefault = B_FALSE; 3279096d22d4SEric Schrock 3280f9af39baSGeorge Wilson if (vd != rvd && vdev_writeable(vd->vdev_top)) 3281e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 3282e14bb325SJeff Bonwick 3283e14bb325SJeff Bonwick if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3284bb8b5132Sek spa_async_request(spa, SPA_ASYNC_RESILVER); 32853d7072f8Seschrock 3286ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 32873d7072f8Seschrock } 3288096d22d4SEric Schrock 3289096d22d4SEric Schrock /* 3290096d22d4SEric Schrock * When clearing a FMA-diagnosed fault, we always want to 3291096d22d4SEric Schrock * unspare the device, as we assume that the original spare was 3292096d22d4SEric Schrock * done in response to the FMA fault. 3293096d22d4SEric Schrock */ 3294096d22d4SEric Schrock if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3295096d22d4SEric Schrock vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3296096d22d4SEric Schrock vd->vdev_parent->vdev_child[0] == vd) 3297096d22d4SEric Schrock vd->vdev_unspare = B_TRUE; 3298fa9e4066Sahrens } 3299fa9e4066Sahrens 3300e14bb325SJeff Bonwick boolean_t 3301e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd) 33020a4e9518Sgw { 330388ecc943SGeorge Wilson /* 330488ecc943SGeorge Wilson * Holes and missing devices are always considered "dead". 330588ecc943SGeorge Wilson * This simplifies the code since we don't have to check for 330688ecc943SGeorge Wilson * these types of devices in the various code paths. 330788ecc943SGeorge Wilson * Instead we rely on the fact that we skip over dead devices 330888ecc943SGeorge Wilson * before issuing I/O to them. 330988ecc943SGeorge Wilson */ 33105cabbc6bSPrashanth Sreenivasa return (vd->vdev_state < VDEV_STATE_DEGRADED || 33115cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_hole_ops || 331288ecc943SGeorge Wilson vd->vdev_ops == &vdev_missing_ops); 33130a4e9518Sgw } 33140a4e9518Sgw 3315e14bb325SJeff Bonwick boolean_t 3316e14bb325SJeff Bonwick vdev_readable(vdev_t *vd) 33170a4e9518Sgw { 3318e14bb325SJeff Bonwick return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 33190a4e9518Sgw } 33200a4e9518Sgw 3321e14bb325SJeff Bonwick boolean_t 3322e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd) 3323fa9e4066Sahrens { 33245cabbc6bSPrashanth Sreenivasa return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 33255cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)); 3326fa9e4066Sahrens } 3327fa9e4066Sahrens 3328a31e6787SGeorge Wilson boolean_t 3329a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd) 3330a31e6787SGeorge Wilson { 33318ad4d6ddSJeff Bonwick uint64_t state = vd->vdev_state; 33328ad4d6ddSJeff Bonwick 3333a31e6787SGeorge Wilson /* 33348ad4d6ddSJeff Bonwick * We currently allow allocations from vdevs which may be in the 3335a31e6787SGeorge Wilson * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3336a31e6787SGeorge Wilson * fails to reopen then we'll catch it later when we're holding 33378ad4d6ddSJeff Bonwick * the proper locks. Note that we have to get the vdev state 33388ad4d6ddSJeff Bonwick * in a local variable because although it changes atomically, 33398ad4d6ddSJeff Bonwick * we're asking two separate questions about it. 3340a31e6787SGeorge Wilson */ 33418ad4d6ddSJeff Bonwick return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 33425cabbc6bSPrashanth Sreenivasa !vd->vdev_cant_write && vdev_is_concrete(vd) && 33430f7643c7SGeorge Wilson vd->vdev_mg->mg_initialized); 3344a31e6787SGeorge Wilson } 3345a31e6787SGeorge Wilson 3346e14bb325SJeff Bonwick boolean_t 3347e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio) 3348fa9e4066Sahrens { 3349e14bb325SJeff Bonwick ASSERT(zio->io_vd == vd); 3350fa9e4066Sahrens 3351e14bb325SJeff Bonwick if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3352e14bb325SJeff Bonwick return (B_FALSE); 3353fa9e4066Sahrens 3354e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 3355e14bb325SJeff Bonwick return (!vd->vdev_cant_read); 3356fa9e4066Sahrens 3357e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3358e14bb325SJeff Bonwick return (!vd->vdev_cant_write); 3359fa9e4066Sahrens 3360e14bb325SJeff Bonwick return (B_TRUE); 3361fa9e4066Sahrens } 3362fa9e4066Sahrens 336386714001SSerapheim Dimitropoulos boolean_t 336486714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd) 336586714001SSerapheim Dimitropoulos { 336686714001SSerapheim Dimitropoulos /* 336786714001SSerapheim Dimitropoulos * Assuming 47 bits of the space map entry dedicated for the entry's 336886714001SSerapheim Dimitropoulos * offset (see description in space_map.h), we calculate the maximum 336986714001SSerapheim Dimitropoulos * address that can be described by a space map entry for the given 337086714001SSerapheim Dimitropoulos * device. 337186714001SSerapheim Dimitropoulos */ 337286714001SSerapheim Dimitropoulos uint64_t shift = vd->vdev_ashift + 47; 337386714001SSerapheim Dimitropoulos 337486714001SSerapheim Dimitropoulos if (shift >= 63) /* detect potential overflow */ 337586714001SSerapheim Dimitropoulos return (B_TRUE); 337686714001SSerapheim Dimitropoulos 337786714001SSerapheim Dimitropoulos return (vd->vdev_asize < (1ULL << shift)); 337886714001SSerapheim Dimitropoulos } 337986714001SSerapheim Dimitropoulos 3380fa9e4066Sahrens /* 3381fa9e4066Sahrens * Get statistics for the given vdev. 3382fa9e4066Sahrens */ 3383fa9e4066Sahrens void 3384fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3385fa9e4066Sahrens { 33862e4c9986SGeorge Wilson spa_t *spa = vd->vdev_spa; 33872e4c9986SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 3388c39a2aaeSGeorge Wilson vdev_t *tvd = vd->vdev_top; 33892e4c9986SGeorge Wilson 33902e4c9986SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3391fa9e4066Sahrens 3392fa9e4066Sahrens mutex_enter(&vd->vdev_stat_lock); 3393fa9e4066Sahrens bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3394fa9e4066Sahrens vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3395fa9e4066Sahrens vs->vs_state = vd->vdev_state; 3396573ca77eSGeorge Wilson vs->vs_rsize = vdev_get_min_asize(vd); 3397094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 3398573ca77eSGeorge Wilson vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3399094e47e9SGeorge Wilson /* 3400094e47e9SGeorge Wilson * Report intializing progress. Since we don't have the 3401094e47e9SGeorge Wilson * initializing locks held, this is only an estimate (although a 3402094e47e9SGeorge Wilson * fairly accurate one). 3403094e47e9SGeorge Wilson */ 3404094e47e9SGeorge Wilson vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done; 3405094e47e9SGeorge Wilson vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est; 3406094e47e9SGeorge Wilson vs->vs_initialize_state = vd->vdev_initialize_state; 3407094e47e9SGeorge Wilson vs->vs_initialize_action_time = vd->vdev_initialize_action_time; 3408094e47e9SGeorge Wilson } 3409c39a2aaeSGeorge Wilson /* 3410c39a2aaeSGeorge Wilson * Report expandable space on top-level, non-auxillary devices only. 3411c39a2aaeSGeorge Wilson * The expandable space is reported in terms of metaslab sized units 3412c39a2aaeSGeorge Wilson * since that determines how much space the pool can expand. 3413c39a2aaeSGeorge Wilson */ 3414c39a2aaeSGeorge Wilson if (vd->vdev_aux == NULL && tvd != NULL) { 34157855d95bSToomas Soome vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 34167855d95bSToomas Soome spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3417c39a2aaeSGeorge Wilson } 34185cabbc6bSPrashanth Sreenivasa if (vd->vdev_aux == NULL && vd == vd->vdev_top && 34195cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 34202e4c9986SGeorge Wilson vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 34212986efa8SAlex Reece } 3422fa9e4066Sahrens 3423fa9e4066Sahrens /* 3424fa9e4066Sahrens * If we're getting stats on the root vdev, aggregate the I/O counts 3425fa9e4066Sahrens * over all top-level vdevs (i.e. the direct children of the root). 3426fa9e4066Sahrens */ 3427fa9e4066Sahrens if (vd == rvd) { 3428e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) { 3429fa9e4066Sahrens vdev_t *cvd = rvd->vdev_child[c]; 3430fa9e4066Sahrens vdev_stat_t *cvs = &cvd->vdev_stat; 3431fa9e4066Sahrens 3432e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 3433fa9e4066Sahrens vs->vs_ops[t] += cvs->vs_ops[t]; 3434fa9e4066Sahrens vs->vs_bytes[t] += cvs->vs_bytes[t]; 3435fa9e4066Sahrens } 34363f9d6ad7SLin Ling cvs->vs_scan_removing = cvd->vdev_removing; 3437fa9e4066Sahrens } 3438fa9e4066Sahrens } 34392e4c9986SGeorge Wilson mutex_exit(&vd->vdev_stat_lock); 3440fa9e4066Sahrens } 3441fa9e4066Sahrens 3442fa94a07fSbrendan void 3443fa94a07fSbrendan vdev_clear_stats(vdev_t *vd) 3444fa94a07fSbrendan { 3445fa94a07fSbrendan mutex_enter(&vd->vdev_stat_lock); 3446fa94a07fSbrendan vd->vdev_stat.vs_space = 0; 3447fa94a07fSbrendan vd->vdev_stat.vs_dspace = 0; 3448fa94a07fSbrendan vd->vdev_stat.vs_alloc = 0; 3449fa94a07fSbrendan mutex_exit(&vd->vdev_stat_lock); 3450fa94a07fSbrendan } 3451fa94a07fSbrendan 34523f9d6ad7SLin Ling void 34533f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd) 34543f9d6ad7SLin Ling { 34553f9d6ad7SLin Ling vdev_stat_t *vs = &vd->vdev_stat; 34563f9d6ad7SLin Ling 34573f9d6ad7SLin Ling for (int c = 0; c < vd->vdev_children; c++) 34583f9d6ad7SLin Ling vdev_scan_stat_init(vd->vdev_child[c]); 34593f9d6ad7SLin Ling 34603f9d6ad7SLin Ling mutex_enter(&vd->vdev_stat_lock); 34613f9d6ad7SLin Ling vs->vs_scan_processed = 0; 34623f9d6ad7SLin Ling mutex_exit(&vd->vdev_stat_lock); 34633f9d6ad7SLin Ling } 34643f9d6ad7SLin Ling 3465fa9e4066Sahrens void 3466e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize) 3467fa9e4066Sahrens { 34688ad4d6ddSJeff Bonwick spa_t *spa = zio->io_spa; 34698ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 3470e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3471fa9e4066Sahrens vdev_t *pvd; 3472fa9e4066Sahrens uint64_t txg = zio->io_txg; 3473fa9e4066Sahrens vdev_stat_t *vs = &vd->vdev_stat; 3474fa9e4066Sahrens zio_type_t type = zio->io_type; 3475fa9e4066Sahrens int flags = zio->io_flags; 3476fa9e4066Sahrens 3477e14bb325SJeff Bonwick /* 3478e14bb325SJeff Bonwick * If this i/o is a gang leader, it didn't do any actual work. 3479e14bb325SJeff Bonwick */ 3480e14bb325SJeff Bonwick if (zio->io_gang_tree) 3481e14bb325SJeff Bonwick return; 3482e14bb325SJeff Bonwick 3483fa9e4066Sahrens if (zio->io_error == 0) { 3484e14bb325SJeff Bonwick /* 3485e14bb325SJeff Bonwick * If this is a root i/o, don't count it -- we've already 3486e14bb325SJeff Bonwick * counted the top-level vdevs, and vdev_get_stats() will 3487e14bb325SJeff Bonwick * aggregate them when asked. This reduces contention on 3488e14bb325SJeff Bonwick * the root vdev_stat_lock and implicitly handles blocks 3489e14bb325SJeff Bonwick * that compress away to holes, for which there is no i/o. 3490e14bb325SJeff Bonwick * (Holes never create vdev children, so all the counters 3491e14bb325SJeff Bonwick * remain zero, which is what we want.) 3492e14bb325SJeff Bonwick * 3493e14bb325SJeff Bonwick * Note: this only applies to successful i/o (io_error == 0) 3494e14bb325SJeff Bonwick * because unlike i/o counts, errors are not additive. 3495e14bb325SJeff Bonwick * When reading a ditto block, for example, failure of 3496e14bb325SJeff Bonwick * one top-level vdev does not imply a root-level error. 3497e14bb325SJeff Bonwick */ 3498e14bb325SJeff Bonwick if (vd == rvd) 3499e14bb325SJeff Bonwick return; 3500e14bb325SJeff Bonwick 3501e14bb325SJeff Bonwick ASSERT(vd == zio->io_vd); 35028ad4d6ddSJeff Bonwick 35038ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_IO_BYPASS) 35048ad4d6ddSJeff Bonwick return; 35058ad4d6ddSJeff Bonwick 35068ad4d6ddSJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 35078ad4d6ddSJeff Bonwick 3508e14bb325SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) { 350944ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 35103f9d6ad7SLin Ling dsl_scan_phys_t *scn_phys = 35113f9d6ad7SLin Ling &spa->spa_dsl_pool->dp_scan->scn_phys; 35123f9d6ad7SLin Ling uint64_t *processed = &scn_phys->scn_processed; 35133f9d6ad7SLin Ling 35143f9d6ad7SLin Ling /* XXX cleanup? */ 35153f9d6ad7SLin Ling if (vd->vdev_ops->vdev_op_leaf) 35163f9d6ad7SLin Ling atomic_add_64(processed, psize); 35173f9d6ad7SLin Ling vs->vs_scan_processed += psize; 35183f9d6ad7SLin Ling } 35193f9d6ad7SLin Ling 35208ad4d6ddSJeff Bonwick if (flags & ZIO_FLAG_SELF_HEAL) 3521e14bb325SJeff Bonwick vs->vs_self_healed += psize; 3522fa9e4066Sahrens } 35238ad4d6ddSJeff Bonwick 35248ad4d6ddSJeff Bonwick vs->vs_ops[type]++; 35258ad4d6ddSJeff Bonwick vs->vs_bytes[type] += psize; 35268ad4d6ddSJeff Bonwick 35278ad4d6ddSJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3528fa9e4066Sahrens return; 3529fa9e4066Sahrens } 3530fa9e4066Sahrens 3531fa9e4066Sahrens if (flags & ZIO_FLAG_SPECULATIVE) 3532fa9e4066Sahrens return; 3533fa9e4066Sahrens 35348956713aSEric Schrock /* 35358956713aSEric Schrock * If this is an I/O error that is going to be retried, then ignore the 35368956713aSEric Schrock * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 35378956713aSEric Schrock * hard errors, when in reality they can happen for any number of 35388956713aSEric Schrock * innocuous reasons (bus resets, MPxIO link failure, etc). 35398956713aSEric Schrock */ 35408956713aSEric Schrock if (zio->io_error == EIO && 35418956713aSEric Schrock !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 35428956713aSEric Schrock return; 35438956713aSEric Schrock 35448f18d1faSGeorge Wilson /* 35458f18d1faSGeorge Wilson * Intent logs writes won't propagate their error to the root 35468f18d1faSGeorge Wilson * I/O so don't mark these types of failures as pool-level 35478f18d1faSGeorge Wilson * errors. 35488f18d1faSGeorge Wilson */ 35498f18d1faSGeorge Wilson if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 35508f18d1faSGeorge Wilson return; 35518f18d1faSGeorge Wilson 3552e14bb325SJeff Bonwick mutex_enter(&vd->vdev_stat_lock); 3553b47119fdSGeorge Wilson if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3554e14bb325SJeff Bonwick if (zio->io_error == ECKSUM) 3555e14bb325SJeff Bonwick vs->vs_checksum_errors++; 3556e14bb325SJeff Bonwick else 3557e14bb325SJeff Bonwick vs->vs_read_errors++; 3558fa9e4066Sahrens } 3559b47119fdSGeorge Wilson if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3560e14bb325SJeff Bonwick vs->vs_write_errors++; 3561e14bb325SJeff Bonwick mutex_exit(&vd->vdev_stat_lock); 3562fa9e4066Sahrens 35635cabbc6bSPrashanth Sreenivasa if (spa->spa_load_state == SPA_LOAD_NONE && 35645cabbc6bSPrashanth Sreenivasa type == ZIO_TYPE_WRITE && txg != 0 && 35658ad4d6ddSJeff Bonwick (!(flags & ZIO_FLAG_IO_REPAIR) || 356644ecc532SGeorge Wilson (flags & ZIO_FLAG_SCAN_THREAD) || 3567b24ab676SJeff Bonwick spa->spa_claiming)) { 35688ad4d6ddSJeff Bonwick /* 3569b24ab676SJeff Bonwick * This is either a normal write (not a repair), or it's 3570b24ab676SJeff Bonwick * a repair induced by the scrub thread, or it's a repair 3571b24ab676SJeff Bonwick * made by zil_claim() during spa_load() in the first txg. 3572b24ab676SJeff Bonwick * In the normal case, we commit the DTL change in the same 3573b24ab676SJeff Bonwick * txg as the block was born. In the scrub-induced repair 3574b24ab676SJeff Bonwick * case, we know that scrubs run in first-pass syncing context, 3575b24ab676SJeff Bonwick * so we commit the DTL change in spa_syncing_txg(spa). 3576b24ab676SJeff Bonwick * In the zil_claim() case, we commit in spa_first_txg(spa). 35778ad4d6ddSJeff Bonwick * 35788ad4d6ddSJeff Bonwick * We currently do not make DTL entries for failed spontaneous 35798ad4d6ddSJeff Bonwick * self-healing writes triggered by normal (non-scrubbing) 35808ad4d6ddSJeff Bonwick * reads, because we have no transactional context in which to 35818ad4d6ddSJeff Bonwick * do so -- and it's not clear that it'd be desirable anyway. 35828ad4d6ddSJeff Bonwick */ 35838ad4d6ddSJeff Bonwick if (vd->vdev_ops->vdev_op_leaf) { 35848ad4d6ddSJeff Bonwick uint64_t commit_txg = txg; 358544ecc532SGeorge Wilson if (flags & ZIO_FLAG_SCAN_THREAD) { 35868ad4d6ddSJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 35878ad4d6ddSJeff Bonwick ASSERT(spa_sync_pass(spa) == 1); 35888ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3589b24ab676SJeff Bonwick commit_txg = spa_syncing_txg(spa); 3590b24ab676SJeff Bonwick } else if (spa->spa_claiming) { 3591b24ab676SJeff Bonwick ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3592b24ab676SJeff Bonwick commit_txg = spa_first_txg(spa); 35938ad4d6ddSJeff Bonwick } 3594b24ab676SJeff Bonwick ASSERT(commit_txg >= spa_syncing_txg(spa)); 35958ad4d6ddSJeff Bonwick if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3596fa9e4066Sahrens return; 35978ad4d6ddSJeff Bonwick for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 35988ad4d6ddSJeff Bonwick vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 35998ad4d6ddSJeff Bonwick vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3600fa9e4066Sahrens } 36018ad4d6ddSJeff Bonwick if (vd != rvd) 36028ad4d6ddSJeff Bonwick vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3603fa9e4066Sahrens } 3604fa9e4066Sahrens } 3605fa9e4066Sahrens 3606fa9e4066Sahrens /* 3607b24ab676SJeff Bonwick * Update the in-core space usage stats for this vdev, its metaslab class, 3608b24ab676SJeff Bonwick * and the root vdev. 3609fa9e4066Sahrens */ 3610fa9e4066Sahrens void 3611b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3612b24ab676SJeff Bonwick int64_t space_delta) 3613fa9e4066Sahrens { 361499653d4eSeschrock int64_t dspace_delta = space_delta; 36158654d025Sperrin spa_t *spa = vd->vdev_spa; 36168654d025Sperrin vdev_t *rvd = spa->spa_root_vdev; 3617b24ab676SJeff Bonwick metaslab_group_t *mg = vd->vdev_mg; 3618b24ab676SJeff Bonwick metaslab_class_t *mc = mg ? mg->mg_class : NULL; 3619fa9e4066Sahrens 36208654d025Sperrin ASSERT(vd == vd->vdev_top); 362199653d4eSeschrock 36228654d025Sperrin /* 36238654d025Sperrin * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 36248654d025Sperrin * factor. We must calculate this here and not at the root vdev 36258654d025Sperrin * because the root vdev's psize-to-asize is simply the max of its 36268654d025Sperrin * childrens', thus not accurate enough for us. 36278654d025Sperrin */ 36288654d025Sperrin ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 3629e6ca193dSGeorge Wilson ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 36308654d025Sperrin dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 36318654d025Sperrin vd->vdev_deflate_ratio; 36328654d025Sperrin 36338654d025Sperrin mutex_enter(&vd->vdev_stat_lock); 36348654d025Sperrin vd->vdev_stat.vs_alloc += alloc_delta; 3635b24ab676SJeff Bonwick vd->vdev_stat.vs_space += space_delta; 36368654d025Sperrin vd->vdev_stat.vs_dspace += dspace_delta; 36378654d025Sperrin mutex_exit(&vd->vdev_stat_lock); 36388654d025Sperrin 3639b24ab676SJeff Bonwick if (mc == spa_normal_class(spa)) { 3640fa94a07fSbrendan mutex_enter(&rvd->vdev_stat_lock); 3641fa94a07fSbrendan rvd->vdev_stat.vs_alloc += alloc_delta; 3642b24ab676SJeff Bonwick rvd->vdev_stat.vs_space += space_delta; 3643fa94a07fSbrendan rvd->vdev_stat.vs_dspace += dspace_delta; 3644fa94a07fSbrendan mutex_exit(&rvd->vdev_stat_lock); 3645fa94a07fSbrendan } 3646b24ab676SJeff Bonwick 3647b24ab676SJeff Bonwick if (mc != NULL) { 3648b24ab676SJeff Bonwick ASSERT(rvd == vd->vdev_parent); 3649b24ab676SJeff Bonwick ASSERT(vd->vdev_ms_count != 0); 3650b24ab676SJeff Bonwick 3651b24ab676SJeff Bonwick metaslab_class_space_update(mc, 3652b24ab676SJeff Bonwick alloc_delta, defer_delta, space_delta, dspace_delta); 3653b24ab676SJeff Bonwick } 3654fa9e4066Sahrens } 3655fa9e4066Sahrens 3656fa9e4066Sahrens /* 3657fa9e4066Sahrens * Mark a top-level vdev's config as dirty, placing it on the dirty list 3658fa9e4066Sahrens * so that it will be written out next time the vdev configuration is synced. 3659fa9e4066Sahrens * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3660fa9e4066Sahrens */ 3661fa9e4066Sahrens void 3662fa9e4066Sahrens vdev_config_dirty(vdev_t *vd) 3663fa9e4066Sahrens { 3664fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 3665fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 3666fa9e4066Sahrens int c; 3667fa9e4066Sahrens 3668f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3669f9af39baSGeorge Wilson 3670c5904d13Seschrock /* 36716809eb4eSEric Schrock * If this is an aux vdev (as with l2cache and spare devices), then we 36726809eb4eSEric Schrock * update the vdev config manually and set the sync flag. 3673c5904d13Seschrock */ 3674c5904d13Seschrock if (vd->vdev_aux != NULL) { 3675c5904d13Seschrock spa_aux_vdev_t *sav = vd->vdev_aux; 3676c5904d13Seschrock nvlist_t **aux; 3677c5904d13Seschrock uint_t naux; 3678c5904d13Seschrock 3679c5904d13Seschrock for (c = 0; c < sav->sav_count; c++) { 3680c5904d13Seschrock if (sav->sav_vdevs[c] == vd) 3681c5904d13Seschrock break; 3682c5904d13Seschrock } 3683c5904d13Seschrock 3684e14bb325SJeff Bonwick if (c == sav->sav_count) { 3685e14bb325SJeff Bonwick /* 3686e14bb325SJeff Bonwick * We're being removed. There's nothing more to do. 3687e14bb325SJeff Bonwick */ 3688e14bb325SJeff Bonwick ASSERT(sav->sav_sync == B_TRUE); 3689e14bb325SJeff Bonwick return; 3690e14bb325SJeff Bonwick } 3691e14bb325SJeff Bonwick 3692c5904d13Seschrock sav->sav_sync = B_TRUE; 3693c5904d13Seschrock 36946809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(sav->sav_config, 36956809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 36966809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 36976809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 36986809eb4eSEric Schrock } 3699c5904d13Seschrock 3700c5904d13Seschrock ASSERT(c < naux); 3701c5904d13Seschrock 3702c5904d13Seschrock /* 3703c5904d13Seschrock * Setting the nvlist in the middle if the array is a little 3704c5904d13Seschrock * sketchy, but it will work. 3705c5904d13Seschrock */ 3706c5904d13Seschrock nvlist_free(aux[c]); 37073f9d6ad7SLin Ling aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3708c5904d13Seschrock 3709c5904d13Seschrock return; 3710c5904d13Seschrock } 3711c5904d13Seschrock 37125dabedeeSbonwick /* 3713e14bb325SJeff Bonwick * The dirty list is protected by the SCL_CONFIG lock. The caller 3714e14bb325SJeff Bonwick * must either hold SCL_CONFIG as writer, or must be the sync thread 3715e14bb325SJeff Bonwick * (which holds SCL_CONFIG as reader). There's only one sync thread, 37165dabedeeSbonwick * so this is sufficient to ensure mutual exclusion. 37175dabedeeSbonwick */ 3718e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3719e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3720e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 37215dabedeeSbonwick 3722fa9e4066Sahrens if (vd == rvd) { 3723fa9e4066Sahrens for (c = 0; c < rvd->vdev_children; c++) 3724fa9e4066Sahrens vdev_config_dirty(rvd->vdev_child[c]); 3725fa9e4066Sahrens } else { 3726fa9e4066Sahrens ASSERT(vd == vd->vdev_top); 3727fa9e4066Sahrens 372888ecc943SGeorge Wilson if (!list_link_active(&vd->vdev_config_dirty_node) && 37295cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 3730e14bb325SJeff Bonwick list_insert_head(&spa->spa_config_dirty_list, vd); 37315cabbc6bSPrashanth Sreenivasa } 3732fa9e4066Sahrens } 3733fa9e4066Sahrens } 3734fa9e4066Sahrens 3735fa9e4066Sahrens void 3736fa9e4066Sahrens vdev_config_clean(vdev_t *vd) 3737fa9e4066Sahrens { 37385dabedeeSbonwick spa_t *spa = vd->vdev_spa; 37395dabedeeSbonwick 3740e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3741e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3742e14bb325SJeff Bonwick spa_config_held(spa, SCL_CONFIG, RW_READER))); 37435dabedeeSbonwick 3744e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3745e14bb325SJeff Bonwick list_remove(&spa->spa_config_dirty_list, vd); 3746e14bb325SJeff Bonwick } 3747e14bb325SJeff Bonwick 3748e14bb325SJeff Bonwick /* 3749e14bb325SJeff Bonwick * Mark a top-level vdev's state as dirty, so that the next pass of 3750e14bb325SJeff Bonwick * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3751e14bb325SJeff Bonwick * the state changes from larger config changes because they require 3752e14bb325SJeff Bonwick * much less locking, and are often needed for administrative actions. 3753e14bb325SJeff Bonwick */ 3754e14bb325SJeff Bonwick void 3755e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd) 3756e14bb325SJeff Bonwick { 3757e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3758e14bb325SJeff Bonwick 3759f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 3760e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3761e14bb325SJeff Bonwick 3762e14bb325SJeff Bonwick /* 3763e14bb325SJeff Bonwick * The state list is protected by the SCL_STATE lock. The caller 3764e14bb325SJeff Bonwick * must either hold SCL_STATE as writer, or must be the sync thread 3765e14bb325SJeff Bonwick * (which holds SCL_STATE as reader). There's only one sync thread, 3766e14bb325SJeff Bonwick * so this is sufficient to ensure mutual exclusion. 3767e14bb325SJeff Bonwick */ 3768e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3769e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3770e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3771e14bb325SJeff Bonwick 37725cabbc6bSPrashanth Sreenivasa if (!list_link_active(&vd->vdev_state_dirty_node) && 37735cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) 3774e14bb325SJeff Bonwick list_insert_head(&spa->spa_state_dirty_list, vd); 3775e14bb325SJeff Bonwick } 3776e14bb325SJeff Bonwick 3777e14bb325SJeff Bonwick void 3778e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd) 3779e14bb325SJeff Bonwick { 3780e14bb325SJeff Bonwick spa_t *spa = vd->vdev_spa; 3781e14bb325SJeff Bonwick 3782e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3783e14bb325SJeff Bonwick (dsl_pool_sync_context(spa_get_dsl(spa)) && 3784e14bb325SJeff Bonwick spa_config_held(spa, SCL_STATE, RW_READER))); 3785e14bb325SJeff Bonwick 3786e14bb325SJeff Bonwick ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3787e14bb325SJeff Bonwick list_remove(&spa->spa_state_dirty_list, vd); 3788fa9e4066Sahrens } 3789fa9e4066Sahrens 379032b87932Sek /* 379132b87932Sek * Propagate vdev state up from children to parent. 379232b87932Sek */ 379344cd46caSbillm void 379444cd46caSbillm vdev_propagate_state(vdev_t *vd) 379544cd46caSbillm { 37968ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa; 37978ad4d6ddSJeff Bonwick vdev_t *rvd = spa->spa_root_vdev; 379844cd46caSbillm int degraded = 0, faulted = 0; 379944cd46caSbillm int corrupted = 0; 380044cd46caSbillm vdev_t *child; 380144cd46caSbillm 38023d7072f8Seschrock if (vd->vdev_children > 0) { 3803573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 38043d7072f8Seschrock child = vd->vdev_child[c]; 380551ece835Seschrock 380688ecc943SGeorge Wilson /* 38075cabbc6bSPrashanth Sreenivasa * Don't factor holes or indirect vdevs into the 38085cabbc6bSPrashanth Sreenivasa * decision. 380988ecc943SGeorge Wilson */ 38105cabbc6bSPrashanth Sreenivasa if (!vdev_is_concrete(child)) 381188ecc943SGeorge Wilson continue; 381288ecc943SGeorge Wilson 3813e14bb325SJeff Bonwick if (!vdev_readable(child) || 38148ad4d6ddSJeff Bonwick (!vdev_writeable(child) && spa_writeable(spa))) { 381551ece835Seschrock /* 381651ece835Seschrock * Root special: if there is a top-level log 381751ece835Seschrock * device, treat the root vdev as if it were 381851ece835Seschrock * degraded. 381951ece835Seschrock */ 382051ece835Seschrock if (child->vdev_islog && vd == rvd) 382151ece835Seschrock degraded++; 382251ece835Seschrock else 382351ece835Seschrock faulted++; 382451ece835Seschrock } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 38253d7072f8Seschrock degraded++; 382651ece835Seschrock } 382744cd46caSbillm 38283d7072f8Seschrock if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 38293d7072f8Seschrock corrupted++; 38303d7072f8Seschrock } 383144cd46caSbillm 38323d7072f8Seschrock vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 38333d7072f8Seschrock 38343d7072f8Seschrock /* 3835e14bb325SJeff Bonwick * Root special: if there is a top-level vdev that cannot be 38363d7072f8Seschrock * opened due to corrupted metadata, then propagate the root 38373d7072f8Seschrock * vdev's aux state as 'corrupt' rather than 'insufficient 38383d7072f8Seschrock * replicas'. 38393d7072f8Seschrock */ 38403d7072f8Seschrock if (corrupted && vd == rvd && 38413d7072f8Seschrock rvd->vdev_state == VDEV_STATE_CANT_OPEN) 38423d7072f8Seschrock vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 38433d7072f8Seschrock VDEV_AUX_CORRUPT_DATA); 38443d7072f8Seschrock } 38453d7072f8Seschrock 384651ece835Seschrock if (vd->vdev_parent) 38473d7072f8Seschrock vdev_propagate_state(vd->vdev_parent); 384844cd46caSbillm } 384944cd46caSbillm 3850fa9e4066Sahrens /* 3851ea8dc4b6Seschrock * Set a vdev's state. If this is during an open, we don't update the parent 3852ea8dc4b6Seschrock * state, because we're in the process of opening children depth-first. 3853ea8dc4b6Seschrock * Otherwise, we propagate the change to the parent. 3854ea8dc4b6Seschrock * 3855ea8dc4b6Seschrock * If this routine places a device in a faulted state, an appropriate ereport is 3856ea8dc4b6Seschrock * generated. 3857fa9e4066Sahrens */ 3858fa9e4066Sahrens void 3859ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3860fa9e4066Sahrens { 3861560e6e96Seschrock uint64_t save_state; 3862c5904d13Seschrock spa_t *spa = vd->vdev_spa; 3863ea8dc4b6Seschrock 3864ea8dc4b6Seschrock if (state == vd->vdev_state) { 3865ea8dc4b6Seschrock vd->vdev_stat.vs_aux = aux; 3866fa9e4066Sahrens return; 3867ea8dc4b6Seschrock } 3868ea8dc4b6Seschrock 3869560e6e96Seschrock save_state = vd->vdev_state; 3870fa9e4066Sahrens 3871fa9e4066Sahrens vd->vdev_state = state; 3872fa9e4066Sahrens vd->vdev_stat.vs_aux = aux; 3873fa9e4066Sahrens 38743d7072f8Seschrock /* 38753d7072f8Seschrock * If we are setting the vdev state to anything but an open state, then 387698d1cbfeSGeorge Wilson * always close the underlying device unless the device has requested 387798d1cbfeSGeorge Wilson * a delayed close (i.e. we're about to remove or fault the device). 387898d1cbfeSGeorge Wilson * Otherwise, we keep accessible but invalid devices open forever. 387998d1cbfeSGeorge Wilson * We don't call vdev_close() itself, because that implies some extra 388098d1cbfeSGeorge Wilson * checks (offline, etc) that we don't want here. This is limited to 388198d1cbfeSGeorge Wilson * leaf devices, because otherwise closing the device will affect other 388298d1cbfeSGeorge Wilson * children. 388398d1cbfeSGeorge Wilson */ 388498d1cbfeSGeorge Wilson if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 388598d1cbfeSGeorge Wilson vd->vdev_ops->vdev_op_leaf) 38863d7072f8Seschrock vd->vdev_ops->vdev_op_close(vd); 38873d7072f8Seschrock 3888069f55e2SEric Schrock /* 3889069f55e2SEric Schrock * If we have brought this vdev back into service, we need 3890069f55e2SEric Schrock * to notify fmd so that it can gracefully repair any outstanding 3891069f55e2SEric Schrock * cases due to a missing device. We do this in all cases, even those 3892069f55e2SEric Schrock * that probably don't correlate to a repaired fault. This is sure to 3893069f55e2SEric Schrock * catch all cases, and we let the zfs-retire agent sort it out. If 3894069f55e2SEric Schrock * this is a transient state it's OK, as the retire agent will 3895069f55e2SEric Schrock * double-check the state of the vdev before repairing it. 3896069f55e2SEric Schrock */ 3897069f55e2SEric Schrock if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3898069f55e2SEric Schrock vd->vdev_prevstate != state) 3899069f55e2SEric Schrock zfs_post_state_change(spa, vd); 3900069f55e2SEric Schrock 39013d7072f8Seschrock if (vd->vdev_removed && 39023d7072f8Seschrock state == VDEV_STATE_CANT_OPEN && 39033d7072f8Seschrock (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 39043d7072f8Seschrock /* 39053d7072f8Seschrock * If the previous state is set to VDEV_STATE_REMOVED, then this 39063d7072f8Seschrock * device was previously marked removed and someone attempted to 39073d7072f8Seschrock * reopen it. If this failed due to a nonexistent device, then 39083d7072f8Seschrock * keep the device in the REMOVED state. We also let this be if 39093d7072f8Seschrock * it is one of our special test online cases, which is only 39103d7072f8Seschrock * attempting to online the device and shouldn't generate an FMA 39113d7072f8Seschrock * fault. 39123d7072f8Seschrock */ 39133d7072f8Seschrock vd->vdev_state = VDEV_STATE_REMOVED; 39143d7072f8Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 39153d7072f8Seschrock } else if (state == VDEV_STATE_REMOVED) { 39163d7072f8Seschrock vd->vdev_removed = B_TRUE; 39173d7072f8Seschrock } else if (state == VDEV_STATE_CANT_OPEN) { 3918ea8dc4b6Seschrock /* 3919cb04b873SMark J Musante * If we fail to open a vdev during an import or recovery, we 3920cb04b873SMark J Musante * mark it as "not available", which signifies that it was 3921cb04b873SMark J Musante * never there to begin with. Failure to open such a device 3922cb04b873SMark J Musante * is not considered an error. 3923ea8dc4b6Seschrock */ 3924cb04b873SMark J Musante if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3925cb04b873SMark J Musante spa_load_state(spa) == SPA_LOAD_RECOVER) && 3926560e6e96Seschrock vd->vdev_ops->vdev_op_leaf) 3927560e6e96Seschrock vd->vdev_not_present = 1; 3928560e6e96Seschrock 3929560e6e96Seschrock /* 3930560e6e96Seschrock * Post the appropriate ereport. If the 'prevstate' field is 3931560e6e96Seschrock * set to something other than VDEV_STATE_UNKNOWN, it indicates 3932560e6e96Seschrock * that this is part of a vdev_reopen(). In this case, we don't 3933560e6e96Seschrock * want to post the ereport if the device was already in the 3934560e6e96Seschrock * CANT_OPEN state beforehand. 39353d7072f8Seschrock * 39363d7072f8Seschrock * If the 'checkremove' flag is set, then this is an attempt to 39373d7072f8Seschrock * online the device in response to an insertion event. If we 39383d7072f8Seschrock * hit this case, then we have detected an insertion event for a 39393d7072f8Seschrock * faulted or offline device that wasn't in the removed state. 39403d7072f8Seschrock * In this scenario, we don't post an ereport because we are 39413d7072f8Seschrock * about to replace the device, or attempt an online with 39423d7072f8Seschrock * vdev_forcefault, which will generate the fault for us. 3943560e6e96Seschrock */ 39443d7072f8Seschrock if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 39453d7072f8Seschrock !vd->vdev_not_present && !vd->vdev_checkremove && 3946c5904d13Seschrock vd != spa->spa_root_vdev) { 3947ea8dc4b6Seschrock const char *class; 3948ea8dc4b6Seschrock 3949ea8dc4b6Seschrock switch (aux) { 3950ea8dc4b6Seschrock case VDEV_AUX_OPEN_FAILED: 3951ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3952ea8dc4b6Seschrock break; 3953ea8dc4b6Seschrock case VDEV_AUX_CORRUPT_DATA: 3954ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3955ea8dc4b6Seschrock break; 3956ea8dc4b6Seschrock case VDEV_AUX_NO_REPLICAS: 3957ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3958ea8dc4b6Seschrock break; 3959ea8dc4b6Seschrock case VDEV_AUX_BAD_GUID_SUM: 3960ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3961ea8dc4b6Seschrock break; 3962ea8dc4b6Seschrock case VDEV_AUX_TOO_SMALL: 3963ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3964ea8dc4b6Seschrock break; 3965ea8dc4b6Seschrock case VDEV_AUX_BAD_LABEL: 3966ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3967ea8dc4b6Seschrock break; 3968ea8dc4b6Seschrock default: 3969ea8dc4b6Seschrock class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3970ea8dc4b6Seschrock } 3971ea8dc4b6Seschrock 3972c5904d13Seschrock zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3973ea8dc4b6Seschrock } 3974ea8dc4b6Seschrock 39753d7072f8Seschrock /* Erase any notion of persistent removed state */ 39763d7072f8Seschrock vd->vdev_removed = B_FALSE; 39773d7072f8Seschrock } else { 39783d7072f8Seschrock vd->vdev_removed = B_FALSE; 39793d7072f8Seschrock } 3980ea8dc4b6Seschrock 39818b33d774STim Haley if (!isopen && vd->vdev_parent) 39828b33d774STim Haley vdev_propagate_state(vd->vdev_parent); 3983fa9e4066Sahrens } 398415e6edf1Sgw 39856f793812SPavel Zakharov boolean_t 39866f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd) 39876f793812SPavel Zakharov { 39886f793812SPavel Zakharov ASSERT(!vd->vdev_ops->vdev_op_leaf); 39896f793812SPavel Zakharov 39906f793812SPavel Zakharov for (uint64_t i = 0; i < vd->vdev_children; i++) { 39916f793812SPavel Zakharov if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 39926f793812SPavel Zakharov return (B_FALSE); 39936f793812SPavel Zakharov } 39946f793812SPavel Zakharov 39956f793812SPavel Zakharov return (B_TRUE); 39966f793812SPavel Zakharov } 39976f793812SPavel Zakharov 399815e6edf1Sgw /* 399915e6edf1Sgw * Check the vdev configuration to ensure that it's capable of supporting 4000c8811bd3SToomas Soome * a root pool. We do not support partial configuration. 4001c8811bd3SToomas Soome * In addition, only a single top-level vdev is allowed. 400215e6edf1Sgw */ 400315e6edf1Sgw boolean_t 400415e6edf1Sgw vdev_is_bootable(vdev_t *vd) 400515e6edf1Sgw { 400615e6edf1Sgw if (!vd->vdev_ops->vdev_op_leaf) { 400715e6edf1Sgw char *vdev_type = vd->vdev_ops->vdev_op_type; 400815e6edf1Sgw 400915e6edf1Sgw if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 401015e6edf1Sgw vd->vdev_children > 1) { 401115e6edf1Sgw return (B_FALSE); 40125cabbc6bSPrashanth Sreenivasa } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 40135cabbc6bSPrashanth Sreenivasa strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 401415e6edf1Sgw return (B_FALSE); 401515e6edf1Sgw } 401615e6edf1Sgw } 401715e6edf1Sgw 4018573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 401915e6edf1Sgw if (!vdev_is_bootable(vd->vdev_child[c])) 402015e6edf1Sgw return (B_FALSE); 402115e6edf1Sgw } 402215e6edf1Sgw return (B_TRUE); 402315e6edf1Sgw } 4024e6ca193dSGeorge Wilson 40255cabbc6bSPrashanth Sreenivasa boolean_t 40265cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd) 40275cabbc6bSPrashanth Sreenivasa { 40285cabbc6bSPrashanth Sreenivasa vdev_ops_t *ops = vd->vdev_ops; 40295cabbc6bSPrashanth Sreenivasa if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 40305cabbc6bSPrashanth Sreenivasa ops == &vdev_missing_ops || ops == &vdev_root_ops) { 40315cabbc6bSPrashanth Sreenivasa return (B_FALSE); 40325cabbc6bSPrashanth Sreenivasa } else { 40335cabbc6bSPrashanth Sreenivasa return (B_TRUE); 40345cabbc6bSPrashanth Sreenivasa } 40355cabbc6bSPrashanth Sreenivasa } 40365cabbc6bSPrashanth Sreenivasa 40374b964adaSGeorge Wilson /* 40384b964adaSGeorge Wilson * Determine if a log device has valid content. If the vdev was 40394b964adaSGeorge Wilson * removed or faulted in the MOS config then we know that 40404b964adaSGeorge Wilson * the content on the log device has already been written to the pool. 40414b964adaSGeorge Wilson */ 40424b964adaSGeorge Wilson boolean_t 40434b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd) 40444b964adaSGeorge Wilson { 40454b964adaSGeorge Wilson if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 40464b964adaSGeorge Wilson !vd->vdev_removed) 40474b964adaSGeorge Wilson return (B_TRUE); 40484b964adaSGeorge Wilson 40494b964adaSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 40504b964adaSGeorge Wilson if (vdev_log_state_valid(vd->vdev_child[c])) 40514b964adaSGeorge Wilson return (B_TRUE); 40524b964adaSGeorge Wilson 40534b964adaSGeorge Wilson return (B_FALSE); 40544b964adaSGeorge Wilson } 40554b964adaSGeorge Wilson 4056573ca77eSGeorge Wilson /* 4057573ca77eSGeorge Wilson * Expand a vdev if possible. 4058573ca77eSGeorge Wilson */ 4059573ca77eSGeorge Wilson void 4060573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg) 4061573ca77eSGeorge Wilson { 4062573ca77eSGeorge Wilson ASSERT(vd->vdev_top == vd); 4063573ca77eSGeorge Wilson ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4064573ca77eSGeorge Wilson 40655cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vd); 40665cabbc6bSPrashanth Sreenivasa 40675cabbc6bSPrashanth Sreenivasa if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 40685cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 4069573ca77eSGeorge Wilson VERIFY(vdev_metaslab_init(vd, txg) == 0); 4070573ca77eSGeorge Wilson vdev_config_dirty(vd); 4071573ca77eSGeorge Wilson } 4072573ca77eSGeorge Wilson } 40731195e687SMark J Musante 40741195e687SMark J Musante /* 40751195e687SMark J Musante * Split a vdev. 40761195e687SMark J Musante */ 40771195e687SMark J Musante void 40781195e687SMark J Musante vdev_split(vdev_t *vd) 40791195e687SMark J Musante { 40801195e687SMark J Musante vdev_t *cvd, *pvd = vd->vdev_parent; 40811195e687SMark J Musante 40821195e687SMark J Musante vdev_remove_child(pvd, vd); 40831195e687SMark J Musante vdev_compact_children(pvd); 40841195e687SMark J Musante 40851195e687SMark J Musante cvd = pvd->vdev_child[0]; 40861195e687SMark J Musante if (pvd->vdev_children == 1) { 40871195e687SMark J Musante vdev_remove_parent(cvd); 40881195e687SMark J Musante cvd->vdev_splitting = B_TRUE; 40891195e687SMark J Musante } 40901195e687SMark J Musante vdev_propagate_state(cvd); 40911195e687SMark J Musante } 4092283b8460SGeorge.Wilson 4093283b8460SGeorge.Wilson void 4094283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd) 4095283b8460SGeorge.Wilson { 4096283b8460SGeorge.Wilson for (int c = 0; c < vd->vdev_children; c++) { 4097283b8460SGeorge.Wilson vdev_t *cvd = vd->vdev_child[c]; 4098283b8460SGeorge.Wilson 4099283b8460SGeorge.Wilson vdev_deadman(cvd); 4100283b8460SGeorge.Wilson } 4101283b8460SGeorge.Wilson 4102283b8460SGeorge.Wilson if (vd->vdev_ops->vdev_op_leaf) { 4103283b8460SGeorge.Wilson vdev_queue_t *vq = &vd->vdev_queue; 4104283b8460SGeorge.Wilson 4105283b8460SGeorge.Wilson mutex_enter(&vq->vq_lock); 410669962b56SMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) > 0) { 4107283b8460SGeorge.Wilson spa_t *spa = vd->vdev_spa; 4108283b8460SGeorge.Wilson zio_t *fio; 4109283b8460SGeorge.Wilson uint64_t delta; 4110283b8460SGeorge.Wilson 4111283b8460SGeorge.Wilson /* 4112283b8460SGeorge.Wilson * Look at the head of all the pending queues, 4113283b8460SGeorge.Wilson * if any I/O has been outstanding for longer than 4114283b8460SGeorge.Wilson * the spa_deadman_synctime we panic the system. 4115283b8460SGeorge.Wilson */ 411669962b56SMatthew Ahrens fio = avl_first(&vq->vq_active_tree); 4117c55e05cbSMatthew Ahrens delta = gethrtime() - fio->io_timestamp; 4118c55e05cbSMatthew Ahrens if (delta > spa_deadman_synctime(spa)) { 41193ee8c80cSPavel Zakharov vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 41203ee8c80cSPavel Zakharov "%lluns, delta %lluns, last io %lluns", 41213ee8c80cSPavel Zakharov fio->io_timestamp, (u_longlong_t)delta, 4122283b8460SGeorge.Wilson vq->vq_io_complete_ts); 4123283b8460SGeorge.Wilson fm_panic("I/O to pool '%s' appears to be " 4124283b8460SGeorge.Wilson "hung.", spa_name(spa)); 4125283b8460SGeorge.Wilson } 4126283b8460SGeorge.Wilson } 4127283b8460SGeorge.Wilson mutex_exit(&vq->vq_lock); 4128283b8460SGeorge.Wilson } 4129283b8460SGeorge.Wilson } 4130