vdev.c revision 271776
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd 22168404Spjd/* 23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24228103Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25265740Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 26247348Smm * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 27168404Spjd */ 28168404Spjd 29168404Spjd#include <sys/zfs_context.h> 30168404Spjd#include <sys/fm/fs/zfs.h> 31168404Spjd#include <sys/spa.h> 32168404Spjd#include <sys/spa_impl.h> 33168404Spjd#include <sys/dmu.h> 34168404Spjd#include <sys/dmu_tx.h> 35168404Spjd#include <sys/vdev_impl.h> 36168404Spjd#include <sys/uberblock_impl.h> 37168404Spjd#include <sys/metaslab.h> 38168404Spjd#include <sys/metaslab_impl.h> 39168404Spjd#include <sys/space_map.h> 40262093Savg#include <sys/space_reftree.h> 41168404Spjd#include <sys/zio.h> 42168404Spjd#include <sys/zap.h> 43168404Spjd#include <sys/fs/zfs.h> 44185029Spjd#include <sys/arc.h> 45213197Smm#include <sys/zil.h> 46219089Spjd#include <sys/dsl_scan.h> 47240868Spjd#include <sys/trim_map.h> 48168404Spjd 49168404SpjdSYSCTL_DECL(_vfs_zfs); 50168404SpjdSYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV"); 51168404Spjd 52168404Spjd/* 53168404Spjd * Virtual device management. 54168404Spjd */ 55168404Spjd 56266122Ssmh/* 57254591Sgibbs * The limit for ZFS to automatically increase a top-level vdev's ashift 58254591Sgibbs * from logical ashift to physical ashift. 59254591Sgibbs * 60254591Sgibbs * Example: one or more 512B emulation child vdevs 61254591Sgibbs * child->vdev_ashift = 9 (512 bytes) 62254591Sgibbs * child->vdev_physical_ashift = 12 (4096 bytes) 63254591Sgibbs * zfs_max_auto_ashift = 11 (2048 bytes) 64266122Ssmh * zfs_min_auto_ashift = 9 (512 bytes) 65254591Sgibbs * 66266122Ssmh * On pool creation or the addition of a new top-level vdev, ZFS will 67266122Ssmh * increase the ashift of the top-level vdev to 2048 as limited by 68266122Ssmh * zfs_max_auto_ashift. 69254591Sgibbs * 70254591Sgibbs * Example: one or more 512B emulation child vdevs 71254591Sgibbs * child->vdev_ashift = 9 (512 bytes) 72254591Sgibbs * child->vdev_physical_ashift = 12 (4096 bytes) 73254591Sgibbs * zfs_max_auto_ashift = 13 (8192 bytes) 74266122Ssmh * zfs_min_auto_ashift = 9 (512 bytes) 75254591Sgibbs * 76266122Ssmh * On pool creation or the addition of a new top-level vdev, ZFS will 77266122Ssmh * increase the ashift of the top-level vdev to 4096 to match the 78266122Ssmh * max vdev_physical_ashift. 79266122Ssmh * 80266122Ssmh * Example: one or more 512B emulation child vdevs 81266122Ssmh * child->vdev_ashift = 9 (512 bytes) 82266122Ssmh * child->vdev_physical_ashift = 9 (512 bytes) 83266122Ssmh * zfs_max_auto_ashift = 13 (8192 bytes) 84266122Ssmh * zfs_min_auto_ashift = 12 (4096 bytes) 85266122Ssmh * 86266122Ssmh * On pool creation or the addition of a new top-level vdev, ZFS will 87266122Ssmh * increase the ashift of the top-level vdev to 4096 to match the 88266122Ssmh * zfs_min_auto_ashift. 89254591Sgibbs */ 90254591Sgibbsstatic uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT; 91266122Ssmhstatic uint64_t zfs_min_auto_ashift = SPA_MINASHIFT; 92254591Sgibbs 93254591Sgibbsstatic int 94254591Sgibbssysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS) 95254591Sgibbs{ 96254591Sgibbs uint64_t val; 97254591Sgibbs int err; 98254591Sgibbs 99254591Sgibbs val = zfs_max_auto_ashift; 100254591Sgibbs err = sysctl_handle_64(oidp, &val, 0, req); 101254591Sgibbs if (err != 0 || req->newptr == NULL) 102254591Sgibbs return (err); 103254591Sgibbs 104266122Ssmh if (val > SPA_MAXASHIFT || val < zfs_min_auto_ashift) 105266122Ssmh return (EINVAL); 106254591Sgibbs 107254591Sgibbs zfs_max_auto_ashift = val; 108254591Sgibbs 109254591Sgibbs return (0); 110254591Sgibbs} 111254591SgibbsSYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift, 112254591Sgibbs CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 113254591Sgibbs sysctl_vfs_zfs_max_auto_ashift, "QU", 114266122Ssmh "Max ashift used when optimising for logical -> physical sectors size on " 115266122Ssmh "new top-level vdevs."); 116254591Sgibbs 117266122Ssmhstatic int 118266122Ssmhsysctl_vfs_zfs_min_auto_ashift(SYSCTL_HANDLER_ARGS) 119266122Ssmh{ 120266122Ssmh uint64_t val; 121266122Ssmh int err; 122266122Ssmh 123266122Ssmh val = zfs_min_auto_ashift; 124266122Ssmh err = sysctl_handle_64(oidp, &val, 0, req); 125266122Ssmh if (err != 0 || req->newptr == NULL) 126266122Ssmh return (err); 127266122Ssmh 128266122Ssmh if (val < SPA_MINASHIFT || val > zfs_max_auto_ashift) 129266122Ssmh return (EINVAL); 130266122Ssmh 131266122Ssmh zfs_min_auto_ashift = val; 132266122Ssmh 133266122Ssmh return (0); 134266122Ssmh} 135266122SsmhSYSCTL_PROC(_vfs_zfs, OID_AUTO, min_auto_ashift, 136266122Ssmh CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t), 137266122Ssmh sysctl_vfs_zfs_min_auto_ashift, "QU", 138266122Ssmh "Min ashift used when creating new top-level vdevs."); 139266122Ssmh 140168404Spjdstatic vdev_ops_t *vdev_ops_table[] = { 141168404Spjd &vdev_root_ops, 142168404Spjd &vdev_raidz_ops, 143168404Spjd &vdev_mirror_ops, 144168404Spjd &vdev_replacing_ops, 145168404Spjd &vdev_spare_ops, 146168404Spjd#ifdef _KERNEL 147168404Spjd &vdev_geom_ops, 148168404Spjd#else 149168404Spjd &vdev_disk_ops, 150185029Spjd#endif 151168404Spjd &vdev_file_ops, 152168404Spjd &vdev_missing_ops, 153219089Spjd &vdev_hole_ops, 154168404Spjd NULL 155168404Spjd}; 156168404Spjd 157168404Spjd 158168404Spjd/* 159168404Spjd * Given a vdev type, return the appropriate ops vector. 160168404Spjd */ 161168404Spjdstatic vdev_ops_t * 162168404Spjdvdev_getops(const char *type) 163168404Spjd{ 164168404Spjd vdev_ops_t *ops, **opspp; 165168404Spjd 166168404Spjd for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 167168404Spjd if (strcmp(ops->vdev_op_type, type) == 0) 168168404Spjd break; 169168404Spjd 170168404Spjd return (ops); 171168404Spjd} 172168404Spjd 173168404Spjd/* 174168404Spjd * Default asize function: return the MAX of psize with the asize of 175168404Spjd * all children. This is what's used by anything other than RAID-Z. 176168404Spjd */ 177168404Spjduint64_t 178168404Spjdvdev_default_asize(vdev_t *vd, uint64_t psize) 179168404Spjd{ 180168404Spjd uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 181168404Spjd uint64_t csize; 182168404Spjd 183219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 184168404Spjd csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 185168404Spjd asize = MAX(asize, csize); 186168404Spjd } 187168404Spjd 188168404Spjd return (asize); 189168404Spjd} 190168404Spjd 191168404Spjd/* 192219089Spjd * Get the minimum allocatable size. We define the allocatable size as 193219089Spjd * the vdev's asize rounded to the nearest metaslab. This allows us to 194219089Spjd * replace or attach devices which don't have the same physical size but 195219089Spjd * can still satisfy the same number of allocations. 196168404Spjd */ 197168404Spjduint64_t 198219089Spjdvdev_get_min_asize(vdev_t *vd) 199168404Spjd{ 200219089Spjd vdev_t *pvd = vd->vdev_parent; 201168404Spjd 202219089Spjd /* 203236155Smm * If our parent is NULL (inactive spare or cache) or is the root, 204219089Spjd * just return our own asize. 205219089Spjd */ 206219089Spjd if (pvd == NULL) 207219089Spjd return (vd->vdev_asize); 208168404Spjd 209168404Spjd /* 210219089Spjd * The top-level vdev just returns the allocatable size rounded 211219089Spjd * to the nearest metaslab. 212168404Spjd */ 213219089Spjd if (vd == vd->vdev_top) 214219089Spjd return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 215168404Spjd 216219089Spjd /* 217219089Spjd * The allocatable space for a raidz vdev is N * sizeof(smallest child), 218219089Spjd * so each child must provide at least 1/Nth of its asize. 219219089Spjd */ 220219089Spjd if (pvd->vdev_ops == &vdev_raidz_ops) 221219089Spjd return (pvd->vdev_min_asize / pvd->vdev_children); 222168404Spjd 223219089Spjd return (pvd->vdev_min_asize); 224219089Spjd} 225168404Spjd 226219089Spjdvoid 227219089Spjdvdev_set_min_asize(vdev_t *vd) 228219089Spjd{ 229219089Spjd vd->vdev_min_asize = vdev_get_min_asize(vd); 230219089Spjd 231219089Spjd for (int c = 0; c < vd->vdev_children; c++) 232219089Spjd vdev_set_min_asize(vd->vdev_child[c]); 233168404Spjd} 234168404Spjd 235168404Spjdvdev_t * 236168404Spjdvdev_lookup_top(spa_t *spa, uint64_t vdev) 237168404Spjd{ 238168404Spjd vdev_t *rvd = spa->spa_root_vdev; 239168404Spjd 240185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 241185029Spjd 242185029Spjd if (vdev < rvd->vdev_children) { 243185029Spjd ASSERT(rvd->vdev_child[vdev] != NULL); 244168404Spjd return (rvd->vdev_child[vdev]); 245185029Spjd } 246168404Spjd 247168404Spjd return (NULL); 248168404Spjd} 249168404Spjd 250168404Spjdvdev_t * 251168404Spjdvdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 252168404Spjd{ 253168404Spjd vdev_t *mvd; 254168404Spjd 255168404Spjd if (vd->vdev_guid == guid) 256168404Spjd return (vd); 257168404Spjd 258219089Spjd for (int c = 0; c < vd->vdev_children; c++) 259168404Spjd if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 260168404Spjd NULL) 261168404Spjd return (mvd); 262168404Spjd 263168404Spjd return (NULL); 264168404Spjd} 265168404Spjd 266168404Spjdvoid 267168404Spjdvdev_add_child(vdev_t *pvd, vdev_t *cvd) 268168404Spjd{ 269168404Spjd size_t oldsize, newsize; 270168404Spjd uint64_t id = cvd->vdev_id; 271168404Spjd vdev_t **newchild; 272168404Spjd 273185029Spjd ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 274168404Spjd ASSERT(cvd->vdev_parent == NULL); 275168404Spjd 276168404Spjd cvd->vdev_parent = pvd; 277168404Spjd 278168404Spjd if (pvd == NULL) 279168404Spjd return; 280168404Spjd 281168404Spjd ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 282168404Spjd 283168404Spjd oldsize = pvd->vdev_children * sizeof (vdev_t *); 284168404Spjd pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 285168404Spjd newsize = pvd->vdev_children * sizeof (vdev_t *); 286168404Spjd 287168404Spjd newchild = kmem_zalloc(newsize, KM_SLEEP); 288168404Spjd if (pvd->vdev_child != NULL) { 289168404Spjd bcopy(pvd->vdev_child, newchild, oldsize); 290168404Spjd kmem_free(pvd->vdev_child, oldsize); 291168404Spjd } 292168404Spjd 293168404Spjd pvd->vdev_child = newchild; 294168404Spjd pvd->vdev_child[id] = cvd; 295168404Spjd 296168404Spjd cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 297168404Spjd ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 298168404Spjd 299168404Spjd /* 300168404Spjd * Walk up all ancestors to update guid sum. 301168404Spjd */ 302168404Spjd for (; pvd != NULL; pvd = pvd->vdev_parent) 303168404Spjd pvd->vdev_guid_sum += cvd->vdev_guid_sum; 304168404Spjd} 305168404Spjd 306168404Spjdvoid 307168404Spjdvdev_remove_child(vdev_t *pvd, vdev_t *cvd) 308168404Spjd{ 309168404Spjd int c; 310168404Spjd uint_t id = cvd->vdev_id; 311168404Spjd 312168404Spjd ASSERT(cvd->vdev_parent == pvd); 313168404Spjd 314168404Spjd if (pvd == NULL) 315168404Spjd return; 316168404Spjd 317168404Spjd ASSERT(id < pvd->vdev_children); 318168404Spjd ASSERT(pvd->vdev_child[id] == cvd); 319168404Spjd 320168404Spjd pvd->vdev_child[id] = NULL; 321168404Spjd cvd->vdev_parent = NULL; 322168404Spjd 323168404Spjd for (c = 0; c < pvd->vdev_children; c++) 324168404Spjd if (pvd->vdev_child[c]) 325168404Spjd break; 326168404Spjd 327168404Spjd if (c == pvd->vdev_children) { 328168404Spjd kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 329168404Spjd pvd->vdev_child = NULL; 330168404Spjd pvd->vdev_children = 0; 331168404Spjd } 332168404Spjd 333168404Spjd /* 334168404Spjd * Walk up all ancestors to update guid sum. 335168404Spjd */ 336168404Spjd for (; pvd != NULL; pvd = pvd->vdev_parent) 337168404Spjd pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 338168404Spjd} 339168404Spjd 340168404Spjd/* 341168404Spjd * Remove any holes in the child array. 342168404Spjd */ 343168404Spjdvoid 344168404Spjdvdev_compact_children(vdev_t *pvd) 345168404Spjd{ 346168404Spjd vdev_t **newchild, *cvd; 347168404Spjd int oldc = pvd->vdev_children; 348219089Spjd int newc; 349168404Spjd 350185029Spjd ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 351168404Spjd 352219089Spjd for (int c = newc = 0; c < oldc; c++) 353168404Spjd if (pvd->vdev_child[c]) 354168404Spjd newc++; 355168404Spjd 356168404Spjd newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 357168404Spjd 358219089Spjd for (int c = newc = 0; c < oldc; c++) { 359168404Spjd if ((cvd = pvd->vdev_child[c]) != NULL) { 360168404Spjd newchild[newc] = cvd; 361168404Spjd cvd->vdev_id = newc++; 362168404Spjd } 363168404Spjd } 364168404Spjd 365168404Spjd kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 366168404Spjd pvd->vdev_child = newchild; 367168404Spjd pvd->vdev_children = newc; 368168404Spjd} 369168404Spjd 370168404Spjd/* 371168404Spjd * Allocate and minimally initialize a vdev_t. 372168404Spjd */ 373219089Spjdvdev_t * 374168404Spjdvdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 375168404Spjd{ 376168404Spjd vdev_t *vd; 377168404Spjd 378168404Spjd vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 379168404Spjd 380168404Spjd if (spa->spa_root_vdev == NULL) { 381168404Spjd ASSERT(ops == &vdev_root_ops); 382168404Spjd spa->spa_root_vdev = vd; 383228103Smm spa->spa_load_guid = spa_generate_guid(NULL); 384168404Spjd } 385168404Spjd 386219089Spjd if (guid == 0 && ops != &vdev_hole_ops) { 387168404Spjd if (spa->spa_root_vdev == vd) { 388168404Spjd /* 389168404Spjd * The root vdev's guid will also be the pool guid, 390168404Spjd * which must be unique among all pools. 391168404Spjd */ 392219089Spjd guid = spa_generate_guid(NULL); 393168404Spjd } else { 394168404Spjd /* 395168404Spjd * Any other vdev's guid must be unique within the pool. 396168404Spjd */ 397219089Spjd guid = spa_generate_guid(spa); 398168404Spjd } 399168404Spjd ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 400168404Spjd } 401168404Spjd 402168404Spjd vd->vdev_spa = spa; 403168404Spjd vd->vdev_id = id; 404168404Spjd vd->vdev_guid = guid; 405168404Spjd vd->vdev_guid_sum = guid; 406168404Spjd vd->vdev_ops = ops; 407168404Spjd vd->vdev_state = VDEV_STATE_CLOSED; 408219089Spjd vd->vdev_ishole = (ops == &vdev_hole_ops); 409168404Spjd 410168404Spjd mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 411168404Spjd mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 412185029Spjd mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 413209962Smm for (int t = 0; t < DTL_TYPES; t++) { 414262093Savg vd->vdev_dtl[t] = range_tree_create(NULL, NULL, 415209962Smm &vd->vdev_dtl_lock); 416209962Smm } 417168404Spjd txg_list_create(&vd->vdev_ms_list, 418168404Spjd offsetof(struct metaslab, ms_txg_node)); 419168404Spjd txg_list_create(&vd->vdev_dtl_list, 420168404Spjd offsetof(struct vdev, vdev_dtl_node)); 421168404Spjd vd->vdev_stat.vs_timestamp = gethrtime(); 422185029Spjd vdev_queue_init(vd); 423185029Spjd vdev_cache_init(vd); 424168404Spjd 425168404Spjd return (vd); 426168404Spjd} 427168404Spjd 428168404Spjd/* 429168404Spjd * Allocate a new vdev. The 'alloctype' is used to control whether we are 430168404Spjd * creating a new vdev or loading an existing one - the behavior is slightly 431168404Spjd * different for each case. 432168404Spjd */ 433168404Spjdint 434168404Spjdvdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 435168404Spjd int alloctype) 436168404Spjd{ 437168404Spjd vdev_ops_t *ops; 438168404Spjd char *type; 439185029Spjd uint64_t guid = 0, islog, nparity; 440168404Spjd vdev_t *vd; 441168404Spjd 442185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 443168404Spjd 444168404Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 445249195Smm return (SET_ERROR(EINVAL)); 446168404Spjd 447168404Spjd if ((ops = vdev_getops(type)) == NULL) 448249195Smm return (SET_ERROR(EINVAL)); 449168404Spjd 450168404Spjd /* 451168404Spjd * If this is a load, get the vdev guid from the nvlist. 452168404Spjd * Otherwise, vdev_alloc_common() will generate one for us. 453168404Spjd */ 454168404Spjd if (alloctype == VDEV_ALLOC_LOAD) { 455168404Spjd uint64_t label_id; 456168404Spjd 457168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 458168404Spjd label_id != id) 459249195Smm return (SET_ERROR(EINVAL)); 460168404Spjd 461168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 462249195Smm return (SET_ERROR(EINVAL)); 463168404Spjd } else if (alloctype == VDEV_ALLOC_SPARE) { 464168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 465249195Smm return (SET_ERROR(EINVAL)); 466185029Spjd } else if (alloctype == VDEV_ALLOC_L2CACHE) { 467185029Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 468249195Smm return (SET_ERROR(EINVAL)); 469219089Spjd } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 470219089Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 471249195Smm return (SET_ERROR(EINVAL)); 472168404Spjd } 473168404Spjd 474168404Spjd /* 475168404Spjd * The first allocated vdev must be of type 'root'. 476168404Spjd */ 477168404Spjd if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 478249195Smm return (SET_ERROR(EINVAL)); 479168404Spjd 480185029Spjd /* 481185029Spjd * Determine whether we're a log vdev. 482185029Spjd */ 483185029Spjd islog = 0; 484185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 485185029Spjd if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 486249195Smm return (SET_ERROR(ENOTSUP)); 487168404Spjd 488219089Spjd if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 489249195Smm return (SET_ERROR(ENOTSUP)); 490219089Spjd 491168404Spjd /* 492185029Spjd * Set the nparity property for RAID-Z vdevs. 493168404Spjd */ 494185029Spjd nparity = -1ULL; 495168404Spjd if (ops == &vdev_raidz_ops) { 496168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 497185029Spjd &nparity) == 0) { 498219089Spjd if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 499249195Smm return (SET_ERROR(EINVAL)); 500168404Spjd /* 501219089Spjd * Previous versions could only support 1 or 2 parity 502219089Spjd * device. 503168404Spjd */ 504219089Spjd if (nparity > 1 && 505219089Spjd spa_version(spa) < SPA_VERSION_RAIDZ2) 506249195Smm return (SET_ERROR(ENOTSUP)); 507219089Spjd if (nparity > 2 && 508219089Spjd spa_version(spa) < SPA_VERSION_RAIDZ3) 509249195Smm return (SET_ERROR(ENOTSUP)); 510168404Spjd } else { 511168404Spjd /* 512168404Spjd * We require the parity to be specified for SPAs that 513168404Spjd * support multiple parity levels. 514168404Spjd */ 515219089Spjd if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 516249195Smm return (SET_ERROR(EINVAL)); 517168404Spjd /* 518168404Spjd * Otherwise, we default to 1 parity device for RAID-Z. 519168404Spjd */ 520185029Spjd nparity = 1; 521168404Spjd } 522168404Spjd } else { 523185029Spjd nparity = 0; 524168404Spjd } 525185029Spjd ASSERT(nparity != -1ULL); 526168404Spjd 527185029Spjd vd = vdev_alloc_common(spa, id, guid, ops); 528185029Spjd 529185029Spjd vd->vdev_islog = islog; 530185029Spjd vd->vdev_nparity = nparity; 531185029Spjd 532185029Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 533185029Spjd vd->vdev_path = spa_strdup(vd->vdev_path); 534185029Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 535185029Spjd vd->vdev_devid = spa_strdup(vd->vdev_devid); 536185029Spjd if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 537185029Spjd &vd->vdev_physpath) == 0) 538185029Spjd vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 539209962Smm if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 540209962Smm vd->vdev_fru = spa_strdup(vd->vdev_fru); 541185029Spjd 542168404Spjd /* 543168404Spjd * Set the whole_disk property. If it's not specified, leave the value 544168404Spjd * as -1. 545168404Spjd */ 546168404Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 547168404Spjd &vd->vdev_wholedisk) != 0) 548168404Spjd vd->vdev_wholedisk = -1ULL; 549168404Spjd 550168404Spjd /* 551168404Spjd * Look for the 'not present' flag. This will only be set if the device 552168404Spjd * was not present at the time of import. 553168404Spjd */ 554209962Smm (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 555209962Smm &vd->vdev_not_present); 556168404Spjd 557168404Spjd /* 558168404Spjd * Get the alignment requirement. 559168404Spjd */ 560168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 561168404Spjd 562168404Spjd /* 563219089Spjd * Retrieve the vdev creation time. 564219089Spjd */ 565219089Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 566219089Spjd &vd->vdev_crtxg); 567219089Spjd 568219089Spjd /* 569168404Spjd * If we're a top-level vdev, try to load the allocation parameters. 570168404Spjd */ 571219089Spjd if (parent && !parent->vdev_parent && 572219089Spjd (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 573168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 574168404Spjd &vd->vdev_ms_array); 575168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 576168404Spjd &vd->vdev_ms_shift); 577168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 578168404Spjd &vd->vdev_asize); 579219089Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 580219089Spjd &vd->vdev_removing); 581168404Spjd } 582168404Spjd 583230514Smm if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 584219089Spjd ASSERT(alloctype == VDEV_ALLOC_LOAD || 585219089Spjd alloctype == VDEV_ALLOC_ADD || 586219089Spjd alloctype == VDEV_ALLOC_SPLIT || 587219089Spjd alloctype == VDEV_ALLOC_ROOTPOOL); 588219089Spjd vd->vdev_mg = metaslab_group_create(islog ? 589219089Spjd spa_log_class(spa) : spa_normal_class(spa), vd); 590219089Spjd } 591219089Spjd 592168404Spjd /* 593185029Spjd * If we're a leaf vdev, try to load the DTL object and other state. 594168404Spjd */ 595185029Spjd if (vd->vdev_ops->vdev_op_leaf && 596219089Spjd (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 597219089Spjd alloctype == VDEV_ALLOC_ROOTPOOL)) { 598185029Spjd if (alloctype == VDEV_ALLOC_LOAD) { 599185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 600262093Savg &vd->vdev_dtl_object); 601185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 602185029Spjd &vd->vdev_unspare); 603185029Spjd } 604219089Spjd 605219089Spjd if (alloctype == VDEV_ALLOC_ROOTPOOL) { 606219089Spjd uint64_t spare = 0; 607219089Spjd 608219089Spjd if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 609219089Spjd &spare) == 0 && spare) 610219089Spjd spa_spare_add(vd); 611219089Spjd } 612219089Spjd 613168404Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 614168404Spjd &vd->vdev_offline); 615185029Spjd 616254112Sdelphij (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 617254112Sdelphij &vd->vdev_resilver_txg); 618219089Spjd 619185029Spjd /* 620185029Spjd * When importing a pool, we want to ignore the persistent fault 621185029Spjd * state, as the diagnosis made on another system may not be 622219089Spjd * valid in the current context. Local vdevs will 623219089Spjd * remain in the faulted state. 624185029Spjd */ 625219089Spjd if (spa_load_state(spa) == SPA_LOAD_OPEN) { 626185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 627185029Spjd &vd->vdev_faulted); 628185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 629185029Spjd &vd->vdev_degraded); 630185029Spjd (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 631185029Spjd &vd->vdev_removed); 632219089Spjd 633219089Spjd if (vd->vdev_faulted || vd->vdev_degraded) { 634219089Spjd char *aux; 635219089Spjd 636219089Spjd vd->vdev_label_aux = 637219089Spjd VDEV_AUX_ERR_EXCEEDED; 638219089Spjd if (nvlist_lookup_string(nv, 639219089Spjd ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 640219089Spjd strcmp(aux, "external") == 0) 641219089Spjd vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 642219089Spjd } 643185029Spjd } 644168404Spjd } 645168404Spjd 646168404Spjd /* 647168404Spjd * Add ourselves to the parent's list of children. 648168404Spjd */ 649168404Spjd vdev_add_child(parent, vd); 650168404Spjd 651168404Spjd *vdp = vd; 652168404Spjd 653168404Spjd return (0); 654168404Spjd} 655168404Spjd 656168404Spjdvoid 657168404Spjdvdev_free(vdev_t *vd) 658168404Spjd{ 659185029Spjd spa_t *spa = vd->vdev_spa; 660168404Spjd 661168404Spjd /* 662168404Spjd * vdev_free() implies closing the vdev first. This is simpler than 663168404Spjd * trying to ensure complicated semantics for all callers. 664168404Spjd */ 665168404Spjd vdev_close(vd); 666168404Spjd 667185029Spjd ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 668219089Spjd ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 669168404Spjd 670168404Spjd /* 671168404Spjd * Free all children. 672168404Spjd */ 673219089Spjd for (int c = 0; c < vd->vdev_children; c++) 674168404Spjd vdev_free(vd->vdev_child[c]); 675168404Spjd 676168404Spjd ASSERT(vd->vdev_child == NULL); 677168404Spjd ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 678168404Spjd 679168404Spjd /* 680168404Spjd * Discard allocation state. 681168404Spjd */ 682219089Spjd if (vd->vdev_mg != NULL) { 683168404Spjd vdev_metaslab_fini(vd); 684219089Spjd metaslab_group_destroy(vd->vdev_mg); 685219089Spjd } 686168404Spjd 687240415Smm ASSERT0(vd->vdev_stat.vs_space); 688240415Smm ASSERT0(vd->vdev_stat.vs_dspace); 689240415Smm ASSERT0(vd->vdev_stat.vs_alloc); 690168404Spjd 691168404Spjd /* 692168404Spjd * Remove this vdev from its parent's child list. 693168404Spjd */ 694168404Spjd vdev_remove_child(vd->vdev_parent, vd); 695168404Spjd 696168404Spjd ASSERT(vd->vdev_parent == NULL); 697168404Spjd 698185029Spjd /* 699185029Spjd * Clean up vdev structure. 700185029Spjd */ 701185029Spjd vdev_queue_fini(vd); 702185029Spjd vdev_cache_fini(vd); 703185029Spjd 704185029Spjd if (vd->vdev_path) 705185029Spjd spa_strfree(vd->vdev_path); 706185029Spjd if (vd->vdev_devid) 707185029Spjd spa_strfree(vd->vdev_devid); 708185029Spjd if (vd->vdev_physpath) 709185029Spjd spa_strfree(vd->vdev_physpath); 710209962Smm if (vd->vdev_fru) 711209962Smm spa_strfree(vd->vdev_fru); 712185029Spjd 713185029Spjd if (vd->vdev_isspare) 714185029Spjd spa_spare_remove(vd); 715185029Spjd if (vd->vdev_isl2cache) 716185029Spjd spa_l2cache_remove(vd); 717185029Spjd 718185029Spjd txg_list_destroy(&vd->vdev_ms_list); 719185029Spjd txg_list_destroy(&vd->vdev_dtl_list); 720209962Smm 721185029Spjd mutex_enter(&vd->vdev_dtl_lock); 722262093Savg space_map_close(vd->vdev_dtl_sm); 723209962Smm for (int t = 0; t < DTL_TYPES; t++) { 724262093Savg range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 725262093Savg range_tree_destroy(vd->vdev_dtl[t]); 726209962Smm } 727185029Spjd mutex_exit(&vd->vdev_dtl_lock); 728209962Smm 729185029Spjd mutex_destroy(&vd->vdev_dtl_lock); 730185029Spjd mutex_destroy(&vd->vdev_stat_lock); 731185029Spjd mutex_destroy(&vd->vdev_probe_lock); 732185029Spjd 733185029Spjd if (vd == spa->spa_root_vdev) 734185029Spjd spa->spa_root_vdev = NULL; 735185029Spjd 736185029Spjd kmem_free(vd, sizeof (vdev_t)); 737168404Spjd} 738168404Spjd 739168404Spjd/* 740168404Spjd * Transfer top-level vdev state from svd to tvd. 741168404Spjd */ 742168404Spjdstatic void 743168404Spjdvdev_top_transfer(vdev_t *svd, vdev_t *tvd) 744168404Spjd{ 745168404Spjd spa_t *spa = svd->vdev_spa; 746168404Spjd metaslab_t *msp; 747168404Spjd vdev_t *vd; 748168404Spjd int t; 749168404Spjd 750168404Spjd ASSERT(tvd == tvd->vdev_top); 751168404Spjd 752168404Spjd tvd->vdev_ms_array = svd->vdev_ms_array; 753168404Spjd tvd->vdev_ms_shift = svd->vdev_ms_shift; 754168404Spjd tvd->vdev_ms_count = svd->vdev_ms_count; 755168404Spjd 756168404Spjd svd->vdev_ms_array = 0; 757168404Spjd svd->vdev_ms_shift = 0; 758168404Spjd svd->vdev_ms_count = 0; 759168404Spjd 760230514Smm if (tvd->vdev_mg) 761230514Smm ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 762168404Spjd tvd->vdev_mg = svd->vdev_mg; 763168404Spjd tvd->vdev_ms = svd->vdev_ms; 764168404Spjd 765168404Spjd svd->vdev_mg = NULL; 766168404Spjd svd->vdev_ms = NULL; 767168404Spjd 768168404Spjd if (tvd->vdev_mg != NULL) 769168404Spjd tvd->vdev_mg->mg_vd = tvd; 770168404Spjd 771168404Spjd tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 772168404Spjd tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 773168404Spjd tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 774168404Spjd 775168404Spjd svd->vdev_stat.vs_alloc = 0; 776168404Spjd svd->vdev_stat.vs_space = 0; 777168404Spjd svd->vdev_stat.vs_dspace = 0; 778168404Spjd 779168404Spjd for (t = 0; t < TXG_SIZE; t++) { 780168404Spjd while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 781168404Spjd (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 782168404Spjd while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 783168404Spjd (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 784168404Spjd if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 785168404Spjd (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 786168404Spjd } 787168404Spjd 788185029Spjd if (list_link_active(&svd->vdev_config_dirty_node)) { 789168404Spjd vdev_config_clean(svd); 790168404Spjd vdev_config_dirty(tvd); 791168404Spjd } 792168404Spjd 793185029Spjd if (list_link_active(&svd->vdev_state_dirty_node)) { 794185029Spjd vdev_state_clean(svd); 795185029Spjd vdev_state_dirty(tvd); 796185029Spjd } 797168404Spjd 798168404Spjd tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 799168404Spjd svd->vdev_deflate_ratio = 0; 800185029Spjd 801185029Spjd tvd->vdev_islog = svd->vdev_islog; 802185029Spjd svd->vdev_islog = 0; 803168404Spjd} 804168404Spjd 805168404Spjdstatic void 806168404Spjdvdev_top_update(vdev_t *tvd, vdev_t *vd) 807168404Spjd{ 808168404Spjd if (vd == NULL) 809168404Spjd return; 810168404Spjd 811168404Spjd vd->vdev_top = tvd; 812168404Spjd 813219089Spjd for (int c = 0; c < vd->vdev_children; c++) 814168404Spjd vdev_top_update(tvd, vd->vdev_child[c]); 815168404Spjd} 816168404Spjd 817168404Spjd/* 818168404Spjd * Add a mirror/replacing vdev above an existing vdev. 819168404Spjd */ 820168404Spjdvdev_t * 821168404Spjdvdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 822168404Spjd{ 823168404Spjd spa_t *spa = cvd->vdev_spa; 824168404Spjd vdev_t *pvd = cvd->vdev_parent; 825168404Spjd vdev_t *mvd; 826168404Spjd 827185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 828168404Spjd 829168404Spjd mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 830168404Spjd 831168404Spjd mvd->vdev_asize = cvd->vdev_asize; 832219089Spjd mvd->vdev_min_asize = cvd->vdev_min_asize; 833236155Smm mvd->vdev_max_asize = cvd->vdev_max_asize; 834168404Spjd mvd->vdev_ashift = cvd->vdev_ashift; 835254591Sgibbs mvd->vdev_logical_ashift = cvd->vdev_logical_ashift; 836254591Sgibbs mvd->vdev_physical_ashift = cvd->vdev_physical_ashift; 837168404Spjd mvd->vdev_state = cvd->vdev_state; 838219089Spjd mvd->vdev_crtxg = cvd->vdev_crtxg; 839168404Spjd 840168404Spjd vdev_remove_child(pvd, cvd); 841168404Spjd vdev_add_child(pvd, mvd); 842168404Spjd cvd->vdev_id = mvd->vdev_children; 843168404Spjd vdev_add_child(mvd, cvd); 844168404Spjd vdev_top_update(cvd->vdev_top, cvd->vdev_top); 845168404Spjd 846168404Spjd if (mvd == mvd->vdev_top) 847168404Spjd vdev_top_transfer(cvd, mvd); 848168404Spjd 849168404Spjd return (mvd); 850168404Spjd} 851168404Spjd 852168404Spjd/* 853168404Spjd * Remove a 1-way mirror/replacing vdev from the tree. 854168404Spjd */ 855168404Spjdvoid 856168404Spjdvdev_remove_parent(vdev_t *cvd) 857168404Spjd{ 858168404Spjd vdev_t *mvd = cvd->vdev_parent; 859168404Spjd vdev_t *pvd = mvd->vdev_parent; 860168404Spjd 861185029Spjd ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 862168404Spjd 863168404Spjd ASSERT(mvd->vdev_children == 1); 864168404Spjd ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 865168404Spjd mvd->vdev_ops == &vdev_replacing_ops || 866168404Spjd mvd->vdev_ops == &vdev_spare_ops); 867168404Spjd cvd->vdev_ashift = mvd->vdev_ashift; 868254591Sgibbs cvd->vdev_logical_ashift = mvd->vdev_logical_ashift; 869254591Sgibbs cvd->vdev_physical_ashift = mvd->vdev_physical_ashift; 870168404Spjd 871168404Spjd vdev_remove_child(mvd, cvd); 872168404Spjd vdev_remove_child(pvd, mvd); 873209962Smm 874185029Spjd /* 875185029Spjd * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 876185029Spjd * Otherwise, we could have detached an offline device, and when we 877185029Spjd * go to import the pool we'll think we have two top-level vdevs, 878185029Spjd * instead of a different version of the same top-level vdev. 879185029Spjd */ 880209962Smm if (mvd->vdev_top == mvd) { 881209962Smm uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 882219089Spjd cvd->vdev_orig_guid = cvd->vdev_guid; 883209962Smm cvd->vdev_guid += guid_delta; 884209962Smm cvd->vdev_guid_sum += guid_delta; 885209962Smm } 886168404Spjd cvd->vdev_id = mvd->vdev_id; 887168404Spjd vdev_add_child(pvd, cvd); 888168404Spjd vdev_top_update(cvd->vdev_top, cvd->vdev_top); 889168404Spjd 890168404Spjd if (cvd == cvd->vdev_top) 891168404Spjd vdev_top_transfer(mvd, cvd); 892168404Spjd 893168404Spjd ASSERT(mvd->vdev_children == 0); 894168404Spjd vdev_free(mvd); 895168404Spjd} 896168404Spjd 897168404Spjdint 898168404Spjdvdev_metaslab_init(vdev_t *vd, uint64_t txg) 899168404Spjd{ 900168404Spjd spa_t *spa = vd->vdev_spa; 901168404Spjd objset_t *mos = spa->spa_meta_objset; 902168404Spjd uint64_t m; 903168404Spjd uint64_t oldc = vd->vdev_ms_count; 904168404Spjd uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 905168404Spjd metaslab_t **mspp; 906168404Spjd int error; 907168404Spjd 908219089Spjd ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 909219089Spjd 910219089Spjd /* 911219089Spjd * This vdev is not being allocated from yet or is a hole. 912219089Spjd */ 913219089Spjd if (vd->vdev_ms_shift == 0) 914168404Spjd return (0); 915168404Spjd 916219089Spjd ASSERT(!vd->vdev_ishole); 917219089Spjd 918213197Smm /* 919213197Smm * Compute the raidz-deflation ratio. Note, we hard-code 920213197Smm * in 128k (1 << 17) because it is the current "typical" blocksize. 921213197Smm * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change, 922213197Smm * or we will inconsistently account for existing bp's. 923213197Smm */ 924213197Smm vd->vdev_deflate_ratio = (1 << 17) / 925213197Smm (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 926213197Smm 927168404Spjd ASSERT(oldc <= newc); 928168404Spjd 929168404Spjd mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 930168404Spjd 931168404Spjd if (oldc != 0) { 932168404Spjd bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 933168404Spjd kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 934168404Spjd } 935168404Spjd 936168404Spjd vd->vdev_ms = mspp; 937168404Spjd vd->vdev_ms_count = newc; 938168404Spjd 939168404Spjd for (m = oldc; m < newc; m++) { 940262093Savg uint64_t object = 0; 941262093Savg 942168404Spjd if (txg == 0) { 943168404Spjd error = dmu_read(mos, vd->vdev_ms_array, 944209962Smm m * sizeof (uint64_t), sizeof (uint64_t), &object, 945209962Smm DMU_READ_PREFETCH); 946168404Spjd if (error) 947168404Spjd return (error); 948168404Spjd } 949262093Savg vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, m, object, txg); 950168404Spjd } 951168404Spjd 952219089Spjd if (txg == 0) 953219089Spjd spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 954219089Spjd 955219089Spjd /* 956219089Spjd * If the vdev is being removed we don't activate 957219089Spjd * the metaslabs since we want to ensure that no new 958219089Spjd * allocations are performed on this device. 959219089Spjd */ 960219089Spjd if (oldc == 0 && !vd->vdev_removing) 961219089Spjd metaslab_group_activate(vd->vdev_mg); 962219089Spjd 963219089Spjd if (txg == 0) 964219089Spjd spa_config_exit(spa, SCL_ALLOC, FTAG); 965219089Spjd 966168404Spjd return (0); 967168404Spjd} 968168404Spjd 969168404Spjdvoid 970168404Spjdvdev_metaslab_fini(vdev_t *vd) 971168404Spjd{ 972168404Spjd uint64_t m; 973168404Spjd uint64_t count = vd->vdev_ms_count; 974168404Spjd 975168404Spjd if (vd->vdev_ms != NULL) { 976219089Spjd metaslab_group_passivate(vd->vdev_mg); 977262093Savg for (m = 0; m < count; m++) { 978262093Savg metaslab_t *msp = vd->vdev_ms[m]; 979262093Savg 980262093Savg if (msp != NULL) 981262093Savg metaslab_fini(msp); 982262093Savg } 983168404Spjd kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 984168404Spjd vd->vdev_ms = NULL; 985168404Spjd } 986168404Spjd} 987168404Spjd 988185029Spjdtypedef struct vdev_probe_stats { 989185029Spjd boolean_t vps_readable; 990185029Spjd boolean_t vps_writeable; 991185029Spjd int vps_flags; 992185029Spjd} vdev_probe_stats_t; 993185029Spjd 994185029Spjdstatic void 995185029Spjdvdev_probe_done(zio_t *zio) 996185029Spjd{ 997209962Smm spa_t *spa = zio->io_spa; 998209962Smm vdev_t *vd = zio->io_vd; 999185029Spjd vdev_probe_stats_t *vps = zio->io_private; 1000185029Spjd 1001209962Smm ASSERT(vd->vdev_probe_zio != NULL); 1002209962Smm 1003185029Spjd if (zio->io_type == ZIO_TYPE_READ) { 1004185029Spjd if (zio->io_error == 0) 1005185029Spjd vps->vps_readable = 1; 1006209962Smm if (zio->io_error == 0 && spa_writeable(spa)) { 1007209962Smm zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1008185029Spjd zio->io_offset, zio->io_size, zio->io_data, 1009185029Spjd ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1010185029Spjd ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1011185029Spjd } else { 1012185029Spjd zio_buf_free(zio->io_data, zio->io_size); 1013185029Spjd } 1014185029Spjd } else if (zio->io_type == ZIO_TYPE_WRITE) { 1015185029Spjd if (zio->io_error == 0) 1016185029Spjd vps->vps_writeable = 1; 1017185029Spjd zio_buf_free(zio->io_data, zio->io_size); 1018185029Spjd } else if (zio->io_type == ZIO_TYPE_NULL) { 1019209962Smm zio_t *pio; 1020185029Spjd 1021185029Spjd vd->vdev_cant_read |= !vps->vps_readable; 1022185029Spjd vd->vdev_cant_write |= !vps->vps_writeable; 1023185029Spjd 1024185029Spjd if (vdev_readable(vd) && 1025209962Smm (vdev_writeable(vd) || !spa_writeable(spa))) { 1026185029Spjd zio->io_error = 0; 1027185029Spjd } else { 1028185029Spjd ASSERT(zio->io_error != 0); 1029185029Spjd zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1030209962Smm spa, vd, NULL, 0, 0); 1031249195Smm zio->io_error = SET_ERROR(ENXIO); 1032185029Spjd } 1033209962Smm 1034209962Smm mutex_enter(&vd->vdev_probe_lock); 1035209962Smm ASSERT(vd->vdev_probe_zio == zio); 1036209962Smm vd->vdev_probe_zio = NULL; 1037209962Smm mutex_exit(&vd->vdev_probe_lock); 1038209962Smm 1039209962Smm while ((pio = zio_walk_parents(zio)) != NULL) 1040209962Smm if (!vdev_accessible(vd, pio)) 1041249195Smm pio->io_error = SET_ERROR(ENXIO); 1042209962Smm 1043185029Spjd kmem_free(vps, sizeof (*vps)); 1044185029Spjd } 1045185029Spjd} 1046185029Spjd 1047168404Spjd/* 1048251631Sdelphij * Determine whether this device is accessible. 1049251631Sdelphij * 1050251631Sdelphij * Read and write to several known locations: the pad regions of each 1051251631Sdelphij * vdev label but the first, which we leave alone in case it contains 1052251631Sdelphij * a VTOC. 1053185029Spjd */ 1054185029Spjdzio_t * 1055209962Smmvdev_probe(vdev_t *vd, zio_t *zio) 1056185029Spjd{ 1057185029Spjd spa_t *spa = vd->vdev_spa; 1058209962Smm vdev_probe_stats_t *vps = NULL; 1059209962Smm zio_t *pio; 1060185029Spjd 1061209962Smm ASSERT(vd->vdev_ops->vdev_op_leaf); 1062185029Spjd 1063209962Smm /* 1064209962Smm * Don't probe the probe. 1065209962Smm */ 1066209962Smm if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1067209962Smm return (NULL); 1068185029Spjd 1069209962Smm /* 1070209962Smm * To prevent 'probe storms' when a device fails, we create 1071209962Smm * just one probe i/o at a time. All zios that want to probe 1072209962Smm * this vdev will become parents of the probe io. 1073209962Smm */ 1074209962Smm mutex_enter(&vd->vdev_probe_lock); 1075209962Smm 1076209962Smm if ((pio = vd->vdev_probe_zio) == NULL) { 1077209962Smm vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1078209962Smm 1079209962Smm vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1080209962Smm ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1081213198Smm ZIO_FLAG_TRYHARD; 1082209962Smm 1083209962Smm if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1084209962Smm /* 1085209962Smm * vdev_cant_read and vdev_cant_write can only 1086209962Smm * transition from TRUE to FALSE when we have the 1087209962Smm * SCL_ZIO lock as writer; otherwise they can only 1088209962Smm * transition from FALSE to TRUE. This ensures that 1089209962Smm * any zio looking at these values can assume that 1090209962Smm * failures persist for the life of the I/O. That's 1091209962Smm * important because when a device has intermittent 1092209962Smm * connectivity problems, we want to ensure that 1093209962Smm * they're ascribed to the device (ENXIO) and not 1094209962Smm * the zio (EIO). 1095209962Smm * 1096209962Smm * Since we hold SCL_ZIO as writer here, clear both 1097209962Smm * values so the probe can reevaluate from first 1098209962Smm * principles. 1099209962Smm */ 1100209962Smm vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1101209962Smm vd->vdev_cant_read = B_FALSE; 1102209962Smm vd->vdev_cant_write = B_FALSE; 1103209962Smm } 1104209962Smm 1105209962Smm vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1106209962Smm vdev_probe_done, vps, 1107209962Smm vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1108209962Smm 1109219089Spjd /* 1110219089Spjd * We can't change the vdev state in this context, so we 1111219089Spjd * kick off an async task to do it on our behalf. 1112219089Spjd */ 1113209962Smm if (zio != NULL) { 1114209962Smm vd->vdev_probe_wanted = B_TRUE; 1115209962Smm spa_async_request(spa, SPA_ASYNC_PROBE); 1116209962Smm } 1117185029Spjd } 1118185029Spjd 1119209962Smm if (zio != NULL) 1120209962Smm zio_add_child(zio, pio); 1121185029Spjd 1122209962Smm mutex_exit(&vd->vdev_probe_lock); 1123185029Spjd 1124209962Smm if (vps == NULL) { 1125209962Smm ASSERT(zio != NULL); 1126209962Smm return (NULL); 1127209962Smm } 1128185029Spjd 1129185029Spjd for (int l = 1; l < VDEV_LABELS; l++) { 1130209962Smm zio_nowait(zio_read_phys(pio, vd, 1131185029Spjd vdev_label_offset(vd->vdev_psize, l, 1132209962Smm offsetof(vdev_label_t, vl_pad2)), 1133209962Smm VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 1134185029Spjd ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1135185029Spjd ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1136185029Spjd } 1137185029Spjd 1138209962Smm if (zio == NULL) 1139209962Smm return (pio); 1140209962Smm 1141209962Smm zio_nowait(pio); 1142209962Smm return (NULL); 1143185029Spjd} 1144185029Spjd 1145219089Spjdstatic void 1146219089Spjdvdev_open_child(void *arg) 1147219089Spjd{ 1148219089Spjd vdev_t *vd = arg; 1149219089Spjd 1150219089Spjd vd->vdev_open_thread = curthread; 1151219089Spjd vd->vdev_open_error = vdev_open(vd); 1152219089Spjd vd->vdev_open_thread = NULL; 1153219089Spjd} 1154219089Spjd 1155219089Spjdboolean_t 1156219089Spjdvdev_uses_zvols(vdev_t *vd) 1157219089Spjd{ 1158219089Spjd if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1159219089Spjd strlen(ZVOL_DIR)) == 0) 1160219089Spjd return (B_TRUE); 1161219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1162219089Spjd if (vdev_uses_zvols(vd->vdev_child[c])) 1163219089Spjd return (B_TRUE); 1164219089Spjd return (B_FALSE); 1165219089Spjd} 1166219089Spjd 1167219089Spjdvoid 1168219089Spjdvdev_open_children(vdev_t *vd) 1169219089Spjd{ 1170219089Spjd taskq_t *tq; 1171219089Spjd int children = vd->vdev_children; 1172219089Spjd 1173219089Spjd /* 1174219089Spjd * in order to handle pools on top of zvols, do the opens 1175219089Spjd * in a single thread so that the same thread holds the 1176219089Spjd * spa_namespace_lock 1177219089Spjd */ 1178219089Spjd if (B_TRUE || vdev_uses_zvols(vd)) { 1179219089Spjd for (int c = 0; c < children; c++) 1180219089Spjd vd->vdev_child[c]->vdev_open_error = 1181219089Spjd vdev_open(vd->vdev_child[c]); 1182219089Spjd return; 1183219089Spjd } 1184219089Spjd tq = taskq_create("vdev_open", children, minclsyspri, 1185219089Spjd children, children, TASKQ_PREPOPULATE); 1186219089Spjd 1187219089Spjd for (int c = 0; c < children; c++) 1188219089Spjd VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1189219089Spjd TQ_SLEEP) != 0); 1190219089Spjd 1191219089Spjd taskq_destroy(tq); 1192219089Spjd} 1193219089Spjd 1194185029Spjd/* 1195168404Spjd * Prepare a virtual device for access. 1196168404Spjd */ 1197168404Spjdint 1198168404Spjdvdev_open(vdev_t *vd) 1199168404Spjd{ 1200209962Smm spa_t *spa = vd->vdev_spa; 1201168404Spjd int error; 1202168404Spjd uint64_t osize = 0; 1203236155Smm uint64_t max_osize = 0; 1204236155Smm uint64_t asize, max_asize, psize; 1205254591Sgibbs uint64_t logical_ashift = 0; 1206254591Sgibbs uint64_t physical_ashift = 0; 1207168404Spjd 1208219089Spjd ASSERT(vd->vdev_open_thread == curthread || 1209219089Spjd spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1210168404Spjd ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1211168404Spjd vd->vdev_state == VDEV_STATE_CANT_OPEN || 1212168404Spjd vd->vdev_state == VDEV_STATE_OFFLINE); 1213168404Spjd 1214168404Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1215213197Smm vd->vdev_cant_read = B_FALSE; 1216213197Smm vd->vdev_cant_write = B_FALSE; 1217219089Spjd vd->vdev_min_asize = vdev_get_min_asize(vd); 1218168404Spjd 1219219089Spjd /* 1220219089Spjd * If this vdev is not removed, check its fault status. If it's 1221219089Spjd * faulted, bail out of the open. 1222219089Spjd */ 1223185029Spjd if (!vd->vdev_removed && vd->vdev_faulted) { 1224168404Spjd ASSERT(vd->vdev_children == 0); 1225219089Spjd ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1226219089Spjd vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1227185029Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1228219089Spjd vd->vdev_label_aux); 1229249195Smm return (SET_ERROR(ENXIO)); 1230185029Spjd } else if (vd->vdev_offline) { 1231185029Spjd ASSERT(vd->vdev_children == 0); 1232168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1233249195Smm return (SET_ERROR(ENXIO)); 1234168404Spjd } 1235168404Spjd 1236254591Sgibbs error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, 1237254591Sgibbs &logical_ashift, &physical_ashift); 1238168404Spjd 1239219089Spjd /* 1240219089Spjd * Reset the vdev_reopening flag so that we actually close 1241219089Spjd * the vdev on error. 1242219089Spjd */ 1243219089Spjd vd->vdev_reopening = B_FALSE; 1244168404Spjd if (zio_injection_enabled && error == 0) 1245213198Smm error = zio_handle_device_injection(vd, NULL, ENXIO); 1246168404Spjd 1247185029Spjd if (error) { 1248185029Spjd if (vd->vdev_removed && 1249185029Spjd vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1250185029Spjd vd->vdev_removed = B_FALSE; 1251168404Spjd 1252168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1253168404Spjd vd->vdev_stat.vs_aux); 1254168404Spjd return (error); 1255168404Spjd } 1256168404Spjd 1257185029Spjd vd->vdev_removed = B_FALSE; 1258168404Spjd 1259219089Spjd /* 1260219089Spjd * Recheck the faulted flag now that we have confirmed that 1261219089Spjd * the vdev is accessible. If we're faulted, bail. 1262219089Spjd */ 1263219089Spjd if (vd->vdev_faulted) { 1264219089Spjd ASSERT(vd->vdev_children == 0); 1265219089Spjd ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1266219089Spjd vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1267219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1268219089Spjd vd->vdev_label_aux); 1269249195Smm return (SET_ERROR(ENXIO)); 1270219089Spjd } 1271219089Spjd 1272185029Spjd if (vd->vdev_degraded) { 1273185029Spjd ASSERT(vd->vdev_children == 0); 1274185029Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1275185029Spjd VDEV_AUX_ERR_EXCEEDED); 1276185029Spjd } else { 1277219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1278185029Spjd } 1279185029Spjd 1280219089Spjd /* 1281219089Spjd * For hole or missing vdevs we just return success. 1282219089Spjd */ 1283219089Spjd if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1284219089Spjd return (0); 1285219089Spjd 1286240868Spjd if (vd->vdev_ops->vdev_op_leaf) { 1287240868Spjd vd->vdev_notrim = B_FALSE; 1288240868Spjd trim_map_create(vd); 1289240868Spjd } 1290240868Spjd 1291219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 1292168404Spjd if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1293168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1294168404Spjd VDEV_AUX_NONE); 1295168404Spjd break; 1296168404Spjd } 1297219089Spjd } 1298168404Spjd 1299168404Spjd osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1300236155Smm max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1301168404Spjd 1302168404Spjd if (vd->vdev_children == 0) { 1303168404Spjd if (osize < SPA_MINDEVSIZE) { 1304168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1305168404Spjd VDEV_AUX_TOO_SMALL); 1306249195Smm return (SET_ERROR(EOVERFLOW)); 1307168404Spjd } 1308168404Spjd psize = osize; 1309168404Spjd asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1310236155Smm max_asize = max_osize - (VDEV_LABEL_START_SIZE + 1311236155Smm VDEV_LABEL_END_SIZE); 1312168404Spjd } else { 1313168404Spjd if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1314168404Spjd (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1315168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1316168404Spjd VDEV_AUX_TOO_SMALL); 1317249195Smm return (SET_ERROR(EOVERFLOW)); 1318168404Spjd } 1319168404Spjd psize = 0; 1320168404Spjd asize = osize; 1321236155Smm max_asize = max_osize; 1322168404Spjd } 1323168404Spjd 1324168404Spjd vd->vdev_psize = psize; 1325168404Spjd 1326219089Spjd /* 1327219089Spjd * Make sure the allocatable size hasn't shrunk. 1328219089Spjd */ 1329219089Spjd if (asize < vd->vdev_min_asize) { 1330219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1331219089Spjd VDEV_AUX_BAD_LABEL); 1332249195Smm return (SET_ERROR(EINVAL)); 1333219089Spjd } 1334219089Spjd 1335254591Sgibbs vd->vdev_physical_ashift = 1336254591Sgibbs MAX(physical_ashift, vd->vdev_physical_ashift); 1337254591Sgibbs vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift); 1338254591Sgibbs vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift); 1339254591Sgibbs 1340254591Sgibbs if (vd->vdev_logical_ashift > SPA_MAXASHIFT) { 1341254591Sgibbs vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1342254591Sgibbs VDEV_AUX_ASHIFT_TOO_BIG); 1343254591Sgibbs return (EINVAL); 1344254591Sgibbs } 1345254591Sgibbs 1346168404Spjd if (vd->vdev_asize == 0) { 1347168404Spjd /* 1348168404Spjd * This is the first-ever open, so use the computed values. 1349168404Spjd * For testing purposes, a higher ashift can be requested. 1350168404Spjd */ 1351168404Spjd vd->vdev_asize = asize; 1352236155Smm vd->vdev_max_asize = max_asize; 1353168404Spjd } else { 1354168404Spjd /* 1355254591Sgibbs * Make sure the alignment requirement hasn't increased. 1356168404Spjd */ 1357254591Sgibbs if (vd->vdev_ashift > vd->vdev_top->vdev_ashift && 1358253441Sdelphij vd->vdev_ops->vdev_op_leaf) { 1359254591Sgibbs vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1360254591Sgibbs VDEV_AUX_BAD_LABEL); 1361254591Sgibbs return (EINVAL); 1362168404Spjd } 1363236155Smm vd->vdev_max_asize = max_asize; 1364219089Spjd } 1365168404Spjd 1366219089Spjd /* 1367219089Spjd * If all children are healthy and the asize has increased, 1368219089Spjd * then we've experienced dynamic LUN growth. If automatic 1369219089Spjd * expansion is enabled then use the additional space. 1370219089Spjd */ 1371219089Spjd if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize && 1372219089Spjd (vd->vdev_expanding || spa->spa_autoexpand)) 1373219089Spjd vd->vdev_asize = asize; 1374168404Spjd 1375219089Spjd vdev_set_min_asize(vd); 1376168404Spjd 1377168404Spjd /* 1378185029Spjd * Ensure we can issue some IO before declaring the 1379185029Spjd * vdev open for business. 1380185029Spjd */ 1381185029Spjd if (vd->vdev_ops->vdev_op_leaf && 1382185029Spjd (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1383219089Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1384219089Spjd VDEV_AUX_ERR_EXCEEDED); 1385185029Spjd return (error); 1386185029Spjd } 1387185029Spjd 1388185029Spjd /* 1389185029Spjd * If a leaf vdev has a DTL, and seems healthy, then kick off a 1390209962Smm * resilver. But don't do this if we are doing a reopen for a scrub, 1391209962Smm * since this would just restart the scrub we are already doing. 1392168404Spjd */ 1393209962Smm if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1394209962Smm vdev_resilver_needed(vd, NULL, NULL)) 1395209962Smm spa_async_request(spa, SPA_ASYNC_RESILVER); 1396168404Spjd 1397168404Spjd return (0); 1398168404Spjd} 1399168404Spjd 1400168404Spjd/* 1401168404Spjd * Called once the vdevs are all opened, this routine validates the label 1402168404Spjd * contents. This needs to be done before vdev_load() so that we don't 1403185029Spjd * inadvertently do repair I/Os to the wrong device. 1404168404Spjd * 1405230514Smm * If 'strict' is false ignore the spa guid check. This is necessary because 1406230514Smm * if the machine crashed during a re-guid the new guid might have been written 1407230514Smm * to all of the vdev labels, but not the cached config. The strict check 1408230514Smm * will be performed when the pool is opened again using the mos config. 1409230514Smm * 1410168404Spjd * This function will only return failure if one of the vdevs indicates that it 1411168404Spjd * has since been destroyed or exported. This is only possible if 1412168404Spjd * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1413168404Spjd * will be updated but the function will return 0. 1414168404Spjd */ 1415168404Spjdint 1416230514Smmvdev_validate(vdev_t *vd, boolean_t strict) 1417168404Spjd{ 1418168404Spjd spa_t *spa = vd->vdev_spa; 1419168404Spjd nvlist_t *label; 1420219089Spjd uint64_t guid = 0, top_guid; 1421168404Spjd uint64_t state; 1422168404Spjd 1423219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1424230514Smm if (vdev_validate(vd->vdev_child[c], strict) != 0) 1425249195Smm return (SET_ERROR(EBADF)); 1426168404Spjd 1427168404Spjd /* 1428168404Spjd * If the device has already failed, or was marked offline, don't do 1429168404Spjd * any further validation. Otherwise, label I/O will fail and we will 1430168404Spjd * overwrite the previous state. 1431168404Spjd */ 1432185029Spjd if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1433219089Spjd uint64_t aux_guid = 0; 1434219089Spjd nvlist_t *nvl; 1435246631Smm uint64_t txg = spa_last_synced_txg(spa) != 0 ? 1436246631Smm spa_last_synced_txg(spa) : -1ULL; 1437168404Spjd 1438239620Smm if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1439168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1440168404Spjd VDEV_AUX_BAD_LABEL); 1441168404Spjd return (0); 1442168404Spjd } 1443168404Spjd 1444219089Spjd /* 1445219089Spjd * Determine if this vdev has been split off into another 1446219089Spjd * pool. If so, then refuse to open it. 1447219089Spjd */ 1448219089Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1449219089Spjd &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1450219089Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1451219089Spjd VDEV_AUX_SPLIT_POOL); 1452219089Spjd nvlist_free(label); 1453219089Spjd return (0); 1454219089Spjd } 1455219089Spjd 1456230514Smm if (strict && (nvlist_lookup_uint64(label, 1457230514Smm ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1458230514Smm guid != spa_guid(spa))) { 1459168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1460168404Spjd VDEV_AUX_CORRUPT_DATA); 1461168404Spjd nvlist_free(label); 1462168404Spjd return (0); 1463168404Spjd } 1464168404Spjd 1465219089Spjd if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1466219089Spjd != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1467219089Spjd &aux_guid) != 0) 1468219089Spjd aux_guid = 0; 1469219089Spjd 1470185029Spjd /* 1471185029Spjd * If this vdev just became a top-level vdev because its 1472185029Spjd * sibling was detached, it will have adopted the parent's 1473185029Spjd * vdev guid -- but the label may or may not be on disk yet. 1474185029Spjd * Fortunately, either version of the label will have the 1475185029Spjd * same top guid, so if we're a top-level vdev, we can 1476185029Spjd * safely compare to that instead. 1477219089Spjd * 1478219089Spjd * If we split this vdev off instead, then we also check the 1479219089Spjd * original pool's guid. We don't want to consider the vdev 1480219089Spjd * corrupt if it is partway through a split operation. 1481185029Spjd */ 1482168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1483185029Spjd &guid) != 0 || 1484185029Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 1485185029Spjd &top_guid) != 0 || 1486219089Spjd ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) && 1487185029Spjd (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 1488168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1489168404Spjd VDEV_AUX_CORRUPT_DATA); 1490168404Spjd nvlist_free(label); 1491168404Spjd return (0); 1492168404Spjd } 1493168404Spjd 1494168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1495168404Spjd &state) != 0) { 1496168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1497168404Spjd VDEV_AUX_CORRUPT_DATA); 1498168404Spjd nvlist_free(label); 1499168404Spjd return (0); 1500168404Spjd } 1501168404Spjd 1502168404Spjd nvlist_free(label); 1503168404Spjd 1504209962Smm /* 1505219089Spjd * If this is a verbatim import, no need to check the 1506209962Smm * state of the pool. 1507209962Smm */ 1508219089Spjd if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1509219089Spjd spa_load_state(spa) == SPA_LOAD_OPEN && 1510168404Spjd state != POOL_STATE_ACTIVE) 1511249195Smm return (SET_ERROR(EBADF)); 1512185029Spjd 1513185029Spjd /* 1514185029Spjd * If we were able to open and validate a vdev that was 1515185029Spjd * previously marked permanently unavailable, clear that state 1516185029Spjd * now. 1517185029Spjd */ 1518185029Spjd if (vd->vdev_not_present) 1519185029Spjd vd->vdev_not_present = 0; 1520168404Spjd } 1521168404Spjd 1522168404Spjd return (0); 1523168404Spjd} 1524168404Spjd 1525168404Spjd/* 1526168404Spjd * Close a virtual device. 1527168404Spjd */ 1528168404Spjdvoid 1529168404Spjdvdev_close(vdev_t *vd) 1530168404Spjd{ 1531209962Smm spa_t *spa = vd->vdev_spa; 1532219089Spjd vdev_t *pvd = vd->vdev_parent; 1533209962Smm 1534209962Smm ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1535209962Smm 1536219089Spjd /* 1537219089Spjd * If our parent is reopening, then we are as well, unless we are 1538219089Spjd * going offline. 1539219089Spjd */ 1540219089Spjd if (pvd != NULL && pvd->vdev_reopening) 1541219089Spjd vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1542219089Spjd 1543168404Spjd vd->vdev_ops->vdev_op_close(vd); 1544168404Spjd 1545185029Spjd vdev_cache_purge(vd); 1546168404Spjd 1547240868Spjd if (vd->vdev_ops->vdev_op_leaf) 1548240868Spjd trim_map_destroy(vd); 1549240868Spjd 1550168404Spjd /* 1551219089Spjd * We record the previous state before we close it, so that if we are 1552168404Spjd * doing a reopen(), we don't generate FMA ereports if we notice that 1553168404Spjd * it's still faulted. 1554168404Spjd */ 1555168404Spjd vd->vdev_prevstate = vd->vdev_state; 1556168404Spjd 1557168404Spjd if (vd->vdev_offline) 1558168404Spjd vd->vdev_state = VDEV_STATE_OFFLINE; 1559168404Spjd else 1560168404Spjd vd->vdev_state = VDEV_STATE_CLOSED; 1561168404Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1562168404Spjd} 1563168404Spjd 1564168404Spjdvoid 1565219089Spjdvdev_hold(vdev_t *vd) 1566219089Spjd{ 1567219089Spjd spa_t *spa = vd->vdev_spa; 1568219089Spjd 1569219089Spjd ASSERT(spa_is_root(spa)); 1570219089Spjd if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1571219089Spjd return; 1572219089Spjd 1573219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1574219089Spjd vdev_hold(vd->vdev_child[c]); 1575219089Spjd 1576219089Spjd if (vd->vdev_ops->vdev_op_leaf) 1577219089Spjd vd->vdev_ops->vdev_op_hold(vd); 1578219089Spjd} 1579219089Spjd 1580219089Spjdvoid 1581219089Spjdvdev_rele(vdev_t *vd) 1582219089Spjd{ 1583219089Spjd spa_t *spa = vd->vdev_spa; 1584219089Spjd 1585219089Spjd ASSERT(spa_is_root(spa)); 1586219089Spjd for (int c = 0; c < vd->vdev_children; c++) 1587219089Spjd vdev_rele(vd->vdev_child[c]); 1588219089Spjd 1589219089Spjd if (vd->vdev_ops->vdev_op_leaf) 1590219089Spjd vd->vdev_ops->vdev_op_rele(vd); 1591219089Spjd} 1592219089Spjd 1593219089Spjd/* 1594219089Spjd * Reopen all interior vdevs and any unopened leaves. We don't actually 1595219089Spjd * reopen leaf vdevs which had previously been opened as they might deadlock 1596219089Spjd * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1597219089Spjd * If the leaf has never been opened then open it, as usual. 1598219089Spjd */ 1599219089Spjdvoid 1600168404Spjdvdev_reopen(vdev_t *vd) 1601168404Spjd{ 1602168404Spjd spa_t *spa = vd->vdev_spa; 1603168404Spjd 1604185029Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1605168404Spjd 1606219089Spjd /* set the reopening flag unless we're taking the vdev offline */ 1607219089Spjd vd->vdev_reopening = !vd->vdev_offline; 1608168404Spjd vdev_close(vd); 1609168404Spjd (void) vdev_open(vd); 1610168404Spjd 1611168404Spjd /* 1612168404Spjd * Call vdev_validate() here to make sure we have the same device. 1613168404Spjd * Otherwise, a device with an invalid label could be successfully 1614168404Spjd * opened in response to vdev_reopen(). 1615168404Spjd */ 1616185029Spjd if (vd->vdev_aux) { 1617185029Spjd (void) vdev_validate_aux(vd); 1618185029Spjd if (vdev_readable(vd) && vdev_writeable(vd) && 1619209962Smm vd->vdev_aux == &spa->spa_l2cache && 1620219089Spjd !l2arc_vdev_present(vd)) 1621219089Spjd l2arc_add_vdev(spa, vd); 1622185029Spjd } else { 1623246631Smm (void) vdev_validate(vd, B_TRUE); 1624185029Spjd } 1625168404Spjd 1626168404Spjd /* 1627185029Spjd * Reassess parent vdev's health. 1628168404Spjd */ 1629185029Spjd vdev_propagate_state(vd); 1630168404Spjd} 1631168404Spjd 1632168404Spjdint 1633168404Spjdvdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1634168404Spjd{ 1635168404Spjd int error; 1636168404Spjd 1637168404Spjd /* 1638168404Spjd * Normally, partial opens (e.g. of a mirror) are allowed. 1639168404Spjd * For a create, however, we want to fail the request if 1640168404Spjd * there are any components we can't open. 1641168404Spjd */ 1642168404Spjd error = vdev_open(vd); 1643168404Spjd 1644168404Spjd if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1645168404Spjd vdev_close(vd); 1646168404Spjd return (error ? error : ENXIO); 1647168404Spjd } 1648168404Spjd 1649168404Spjd /* 1650262093Savg * Recursively load DTLs and initialize all labels. 1651168404Spjd */ 1652262093Savg if ((error = vdev_dtl_load(vd)) != 0 || 1653262093Savg (error = vdev_label_init(vd, txg, isreplacing ? 1654168404Spjd VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1655168404Spjd vdev_close(vd); 1656168404Spjd return (error); 1657168404Spjd } 1658168404Spjd 1659168404Spjd return (0); 1660168404Spjd} 1661168404Spjd 1662168404Spjdvoid 1663219089Spjdvdev_metaslab_set_size(vdev_t *vd) 1664168404Spjd{ 1665168404Spjd /* 1666168404Spjd * Aim for roughly 200 metaslabs per vdev. 1667168404Spjd */ 1668265740Sdelphij vd->vdev_ms_shift = highbit64(vd->vdev_asize / 200); 1669168404Spjd vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1670168404Spjd} 1671168404Spjd 1672254591Sgibbs/* 1673266122Ssmh * Maximize performance by inflating the configured ashift for top level 1674266122Ssmh * vdevs to be as close to the physical ashift as possible while maintaining 1675266122Ssmh * administrator defined limits and ensuring it doesn't go below the 1676266122Ssmh * logical ashift. 1677254591Sgibbs */ 1678168404Spjdvoid 1679254591Sgibbsvdev_ashift_optimize(vdev_t *vd) 1680254591Sgibbs{ 1681266122Ssmh if (vd == vd->vdev_top) { 1682266122Ssmh if (vd->vdev_ashift < vd->vdev_physical_ashift) { 1683266122Ssmh vd->vdev_ashift = MIN( 1684266122Ssmh MAX(zfs_max_auto_ashift, vd->vdev_ashift), 1685266122Ssmh MAX(zfs_min_auto_ashift, vd->vdev_physical_ashift)); 1686266122Ssmh } else { 1687266122Ssmh /* 1688266122Ssmh * Unusual case where logical ashift > physical ashift 1689266122Ssmh * so we can't cap the calculated ashift based on max 1690266122Ssmh * ashift as that would cause failures. 1691266122Ssmh * We still check if we need to increase it to match 1692266122Ssmh * the min ashift. 1693266122Ssmh */ 1694266122Ssmh vd->vdev_ashift = MAX(zfs_min_auto_ashift, 1695266122Ssmh vd->vdev_ashift); 1696266122Ssmh } 1697254591Sgibbs } 1698254591Sgibbs} 1699254591Sgibbs 1700254591Sgibbsvoid 1701168404Spjdvdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1702168404Spjd{ 1703168404Spjd ASSERT(vd == vd->vdev_top); 1704219089Spjd ASSERT(!vd->vdev_ishole); 1705168404Spjd ASSERT(ISP2(flags)); 1706219089Spjd ASSERT(spa_writeable(vd->vdev_spa)); 1707168404Spjd 1708168404Spjd if (flags & VDD_METASLAB) 1709168404Spjd (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1710168404Spjd 1711168404Spjd if (flags & VDD_DTL) 1712168404Spjd (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1713168404Spjd 1714168404Spjd (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1715168404Spjd} 1716168404Spjd 1717262093Savgvoid 1718262093Savgvdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 1719262093Savg{ 1720262093Savg for (int c = 0; c < vd->vdev_children; c++) 1721262093Savg vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 1722262093Savg 1723262093Savg if (vd->vdev_ops->vdev_op_leaf) 1724262093Savg vdev_dirty(vd->vdev_top, flags, vd, txg); 1725262093Savg} 1726262093Savg 1727209962Smm/* 1728209962Smm * DTLs. 1729209962Smm * 1730209962Smm * A vdev's DTL (dirty time log) is the set of transaction groups for which 1731219089Spjd * the vdev has less than perfect replication. There are four kinds of DTL: 1732209962Smm * 1733209962Smm * DTL_MISSING: txgs for which the vdev has no valid copies of the data 1734209962Smm * 1735209962Smm * DTL_PARTIAL: txgs for which data is available, but not fully replicated 1736209962Smm * 1737209962Smm * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 1738209962Smm * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 1739209962Smm * txgs that was scrubbed. 1740209962Smm * 1741209962Smm * DTL_OUTAGE: txgs which cannot currently be read, whether due to 1742209962Smm * persistent errors or just some device being offline. 1743209962Smm * Unlike the other three, the DTL_OUTAGE map is not generally 1744209962Smm * maintained; it's only computed when needed, typically to 1745209962Smm * determine whether a device can be detached. 1746209962Smm * 1747209962Smm * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 1748209962Smm * either has the data or it doesn't. 1749209962Smm * 1750209962Smm * For interior vdevs such as mirror and RAID-Z the picture is more complex. 1751209962Smm * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 1752209962Smm * if any child is less than fully replicated, then so is its parent. 1753209962Smm * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 1754209962Smm * comprising only those txgs which appear in 'maxfaults' or more children; 1755209962Smm * those are the txgs we don't have enough replication to read. For example, 1756209962Smm * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 1757209962Smm * thus, its DTL_MISSING consists of the set of txgs that appear in more than 1758209962Smm * two child DTL_MISSING maps. 1759209962Smm * 1760209962Smm * It should be clear from the above that to compute the DTLs and outage maps 1761209962Smm * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 1762209962Smm * Therefore, that is all we keep on disk. When loading the pool, or after 1763209962Smm * a configuration change, we generate all other DTLs from first principles. 1764209962Smm */ 1765168404Spjdvoid 1766209962Smmvdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1767168404Spjd{ 1768262093Savg range_tree_t *rt = vd->vdev_dtl[t]; 1769209962Smm 1770209962Smm ASSERT(t < DTL_TYPES); 1771209962Smm ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1772219089Spjd ASSERT(spa_writeable(vd->vdev_spa)); 1773209962Smm 1774262093Savg mutex_enter(rt->rt_lock); 1775262093Savg if (!range_tree_contains(rt, txg, size)) 1776262093Savg range_tree_add(rt, txg, size); 1777262093Savg mutex_exit(rt->rt_lock); 1778168404Spjd} 1779168404Spjd 1780209962Smmboolean_t 1781209962Smmvdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1782168404Spjd{ 1783262093Savg range_tree_t *rt = vd->vdev_dtl[t]; 1784209962Smm boolean_t dirty = B_FALSE; 1785168404Spjd 1786209962Smm ASSERT(t < DTL_TYPES); 1787209962Smm ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1788168404Spjd 1789262093Savg mutex_enter(rt->rt_lock); 1790262093Savg if (range_tree_space(rt) != 0) 1791262093Savg dirty = range_tree_contains(rt, txg, size); 1792262093Savg mutex_exit(rt->rt_lock); 1793168404Spjd 1794168404Spjd return (dirty); 1795168404Spjd} 1796168404Spjd 1797209962Smmboolean_t 1798209962Smmvdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 1799209962Smm{ 1800262093Savg range_tree_t *rt = vd->vdev_dtl[t]; 1801209962Smm boolean_t empty; 1802209962Smm 1803262093Savg mutex_enter(rt->rt_lock); 1804262093Savg empty = (range_tree_space(rt) == 0); 1805262093Savg mutex_exit(rt->rt_lock); 1806209962Smm 1807209962Smm return (empty); 1808209962Smm} 1809209962Smm 1810168404Spjd/* 1811254112Sdelphij * Returns the lowest txg in the DTL range. 1812254112Sdelphij */ 1813254112Sdelphijstatic uint64_t 1814254112Sdelphijvdev_dtl_min(vdev_t *vd) 1815254112Sdelphij{ 1816262093Savg range_seg_t *rs; 1817254112Sdelphij 1818254112Sdelphij ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 1819262093Savg ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1820254112Sdelphij ASSERT0(vd->vdev_children); 1821254112Sdelphij 1822262093Savg rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 1823262093Savg return (rs->rs_start - 1); 1824254112Sdelphij} 1825254112Sdelphij 1826254112Sdelphij/* 1827254112Sdelphij * Returns the highest txg in the DTL. 1828254112Sdelphij */ 1829254112Sdelphijstatic uint64_t 1830254112Sdelphijvdev_dtl_max(vdev_t *vd) 1831254112Sdelphij{ 1832262093Savg range_seg_t *rs; 1833254112Sdelphij 1834254112Sdelphij ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 1835262093Savg ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1836254112Sdelphij ASSERT0(vd->vdev_children); 1837254112Sdelphij 1838262093Savg rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 1839262093Savg return (rs->rs_end); 1840254112Sdelphij} 1841254112Sdelphij 1842254112Sdelphij/* 1843254112Sdelphij * Determine if a resilvering vdev should remove any DTL entries from 1844254112Sdelphij * its range. If the vdev was resilvering for the entire duration of the 1845254112Sdelphij * scan then it should excise that range from its DTLs. Otherwise, this 1846254112Sdelphij * vdev is considered partially resilvered and should leave its DTL 1847254112Sdelphij * entries intact. The comment in vdev_dtl_reassess() describes how we 1848254112Sdelphij * excise the DTLs. 1849254112Sdelphij */ 1850254112Sdelphijstatic boolean_t 1851254112Sdelphijvdev_dtl_should_excise(vdev_t *vd) 1852254112Sdelphij{ 1853254112Sdelphij spa_t *spa = vd->vdev_spa; 1854254112Sdelphij dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1855254112Sdelphij 1856254112Sdelphij ASSERT0(scn->scn_phys.scn_errors); 1857254112Sdelphij ASSERT0(vd->vdev_children); 1858254112Sdelphij 1859254112Sdelphij if (vd->vdev_resilver_txg == 0 || 1860262093Savg range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0) 1861254112Sdelphij return (B_TRUE); 1862254112Sdelphij 1863254112Sdelphij /* 1864254112Sdelphij * When a resilver is initiated the scan will assign the scn_max_txg 1865254112Sdelphij * value to the highest txg value that exists in all DTLs. If this 1866254112Sdelphij * device's max DTL is not part of this scan (i.e. it is not in 1867254112Sdelphij * the range (scn_min_txg, scn_max_txg] then it is not eligible 1868254112Sdelphij * for excision. 1869254112Sdelphij */ 1870254112Sdelphij if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 1871254112Sdelphij ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 1872254112Sdelphij ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 1873254112Sdelphij ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 1874254112Sdelphij return (B_TRUE); 1875254112Sdelphij } 1876254112Sdelphij return (B_FALSE); 1877254112Sdelphij} 1878254112Sdelphij 1879254112Sdelphij/* 1880168404Spjd * Reassess DTLs after a config change or scrub completion. 1881168404Spjd */ 1882168404Spjdvoid 1883168404Spjdvdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1884168404Spjd{ 1885168404Spjd spa_t *spa = vd->vdev_spa; 1886209962Smm avl_tree_t reftree; 1887209962Smm int minref; 1888168404Spjd 1889209962Smm ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1890168404Spjd 1891209962Smm for (int c = 0; c < vd->vdev_children; c++) 1892209962Smm vdev_dtl_reassess(vd->vdev_child[c], txg, 1893209962Smm scrub_txg, scrub_done); 1894209962Smm 1895219089Spjd if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux) 1896209962Smm return; 1897209962Smm 1898209962Smm if (vd->vdev_ops->vdev_op_leaf) { 1899219089Spjd dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1900219089Spjd 1901168404Spjd mutex_enter(&vd->vdev_dtl_lock); 1902254112Sdelphij 1903254112Sdelphij /* 1904254112Sdelphij * If we've completed a scan cleanly then determine 1905254112Sdelphij * if this vdev should remove any DTLs. We only want to 1906254112Sdelphij * excise regions on vdevs that were available during 1907254112Sdelphij * the entire duration of this scan. 1908254112Sdelphij */ 1909185029Spjd if (scrub_txg != 0 && 1910219089Spjd (spa->spa_scrub_started || 1911254112Sdelphij (scn != NULL && scn->scn_phys.scn_errors == 0)) && 1912254112Sdelphij vdev_dtl_should_excise(vd)) { 1913185029Spjd /* 1914185029Spjd * We completed a scrub up to scrub_txg. If we 1915185029Spjd * did it without rebooting, then the scrub dtl 1916185029Spjd * will be valid, so excise the old region and 1917185029Spjd * fold in the scrub dtl. Otherwise, leave the 1918185029Spjd * dtl as-is if there was an error. 1919209962Smm * 1920209962Smm * There's little trick here: to excise the beginning 1921209962Smm * of the DTL_MISSING map, we put it into a reference 1922209962Smm * tree and then add a segment with refcnt -1 that 1923209962Smm * covers the range [0, scrub_txg). This means 1924209962Smm * that each txg in that range has refcnt -1 or 0. 1925209962Smm * We then add DTL_SCRUB with a refcnt of 2, so that 1926209962Smm * entries in the range [0, scrub_txg) will have a 1927209962Smm * positive refcnt -- either 1 or 2. We then convert 1928209962Smm * the reference tree into the new DTL_MISSING map. 1929185029Spjd */ 1930262093Savg space_reftree_create(&reftree); 1931262093Savg space_reftree_add_map(&reftree, 1932262093Savg vd->vdev_dtl[DTL_MISSING], 1); 1933262093Savg space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 1934262093Savg space_reftree_add_map(&reftree, 1935262093Savg vd->vdev_dtl[DTL_SCRUB], 2); 1936262093Savg space_reftree_generate_map(&reftree, 1937262093Savg vd->vdev_dtl[DTL_MISSING], 1); 1938262093Savg space_reftree_destroy(&reftree); 1939168404Spjd } 1940262093Savg range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 1941262093Savg range_tree_walk(vd->vdev_dtl[DTL_MISSING], 1942262093Savg range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 1943168404Spjd if (scrub_done) 1944262093Savg range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 1945262093Savg range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 1946209962Smm if (!vdev_readable(vd)) 1947262093Savg range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 1948209962Smm else 1949262093Savg range_tree_walk(vd->vdev_dtl[DTL_MISSING], 1950262093Savg range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 1951254112Sdelphij 1952254112Sdelphij /* 1953254112Sdelphij * If the vdev was resilvering and no longer has any 1954271776Ssmh * DTLs then reset its resilvering flag and dirty 1955271776Ssmh * the top level so that we persist the change. 1956254112Sdelphij */ 1957254112Sdelphij if (vd->vdev_resilver_txg != 0 && 1958262093Savg range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 && 1959271776Ssmh range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) { 1960254112Sdelphij vd->vdev_resilver_txg = 0; 1961271776Ssmh vdev_config_dirty(vd->vdev_top); 1962271776Ssmh } 1963254112Sdelphij 1964168404Spjd mutex_exit(&vd->vdev_dtl_lock); 1965185029Spjd 1966168404Spjd if (txg != 0) 1967168404Spjd vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1968168404Spjd return; 1969168404Spjd } 1970168404Spjd 1971168404Spjd mutex_enter(&vd->vdev_dtl_lock); 1972209962Smm for (int t = 0; t < DTL_TYPES; t++) { 1973209962Smm /* account for child's outage in parent's missing map */ 1974209962Smm int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 1975209962Smm if (t == DTL_SCRUB) 1976209962Smm continue; /* leaf vdevs only */ 1977209962Smm if (t == DTL_PARTIAL) 1978209962Smm minref = 1; /* i.e. non-zero */ 1979209962Smm else if (vd->vdev_nparity != 0) 1980209962Smm minref = vd->vdev_nparity + 1; /* RAID-Z */ 1981209962Smm else 1982209962Smm minref = vd->vdev_children; /* any kind of mirror */ 1983262093Savg space_reftree_create(&reftree); 1984209962Smm for (int c = 0; c < vd->vdev_children; c++) { 1985209962Smm vdev_t *cvd = vd->vdev_child[c]; 1986209962Smm mutex_enter(&cvd->vdev_dtl_lock); 1987262093Savg space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 1988209962Smm mutex_exit(&cvd->vdev_dtl_lock); 1989209962Smm } 1990262093Savg space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 1991262093Savg space_reftree_destroy(&reftree); 1992209962Smm } 1993168404Spjd mutex_exit(&vd->vdev_dtl_lock); 1994168404Spjd} 1995168404Spjd 1996262093Savgint 1997168404Spjdvdev_dtl_load(vdev_t *vd) 1998168404Spjd{ 1999168404Spjd spa_t *spa = vd->vdev_spa; 2000168404Spjd objset_t *mos = spa->spa_meta_objset; 2001262093Savg int error = 0; 2002168404Spjd 2003262093Savg if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 2004262093Savg ASSERT(!vd->vdev_ishole); 2005168404Spjd 2006262093Savg error = space_map_open(&vd->vdev_dtl_sm, mos, 2007262093Savg vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock); 2008262093Savg if (error) 2009262093Savg return (error); 2010262093Savg ASSERT(vd->vdev_dtl_sm != NULL); 2011168404Spjd 2012262093Savg mutex_enter(&vd->vdev_dtl_lock); 2013219089Spjd 2014262093Savg /* 2015262093Savg * Now that we've opened the space_map we need to update 2016262093Savg * the in-core DTL. 2017262093Savg */ 2018262093Savg space_map_update(vd->vdev_dtl_sm); 2019262093Savg 2020262093Savg error = space_map_load(vd->vdev_dtl_sm, 2021262093Savg vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 2022262093Savg mutex_exit(&vd->vdev_dtl_lock); 2023262093Savg 2024168404Spjd return (error); 2025262093Savg } 2026168404Spjd 2027262093Savg for (int c = 0; c < vd->vdev_children; c++) { 2028262093Savg error = vdev_dtl_load(vd->vdev_child[c]); 2029262093Savg if (error != 0) 2030262093Savg break; 2031262093Savg } 2032168404Spjd 2033168404Spjd return (error); 2034168404Spjd} 2035168404Spjd 2036168404Spjdvoid 2037168404Spjdvdev_dtl_sync(vdev_t *vd, uint64_t txg) 2038168404Spjd{ 2039168404Spjd spa_t *spa = vd->vdev_spa; 2040262093Savg range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2041168404Spjd objset_t *mos = spa->spa_meta_objset; 2042262093Savg range_tree_t *rtsync; 2043262093Savg kmutex_t rtlock; 2044168404Spjd dmu_tx_t *tx; 2045262093Savg uint64_t object = space_map_object(vd->vdev_dtl_sm); 2046168404Spjd 2047219089Spjd ASSERT(!vd->vdev_ishole); 2048262093Savg ASSERT(vd->vdev_ops->vdev_op_leaf); 2049219089Spjd 2050168404Spjd tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2051168404Spjd 2052262093Savg if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2053262093Savg mutex_enter(&vd->vdev_dtl_lock); 2054262093Savg space_map_free(vd->vdev_dtl_sm, tx); 2055262093Savg space_map_close(vd->vdev_dtl_sm); 2056262093Savg vd->vdev_dtl_sm = NULL; 2057262093Savg mutex_exit(&vd->vdev_dtl_lock); 2058168404Spjd dmu_tx_commit(tx); 2059168404Spjd return; 2060168404Spjd } 2061168404Spjd 2062262093Savg if (vd->vdev_dtl_sm == NULL) { 2063262093Savg uint64_t new_object; 2064262093Savg 2065262093Savg new_object = space_map_alloc(mos, tx); 2066262093Savg VERIFY3U(new_object, !=, 0); 2067262093Savg 2068262093Savg VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2069262093Savg 0, -1ULL, 0, &vd->vdev_dtl_lock)); 2070262093Savg ASSERT(vd->vdev_dtl_sm != NULL); 2071168404Spjd } 2072168404Spjd 2073262093Savg bzero(&rtlock, sizeof(rtlock)); 2074262093Savg mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL); 2075168404Spjd 2076262093Savg rtsync = range_tree_create(NULL, NULL, &rtlock); 2077168404Spjd 2078262093Savg mutex_enter(&rtlock); 2079168404Spjd 2080168404Spjd mutex_enter(&vd->vdev_dtl_lock); 2081262093Savg range_tree_walk(rt, range_tree_add, rtsync); 2082168404Spjd mutex_exit(&vd->vdev_dtl_lock); 2083168404Spjd 2084262093Savg space_map_truncate(vd->vdev_dtl_sm, tx); 2085262093Savg space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx); 2086262093Savg range_tree_vacate(rtsync, NULL, NULL); 2087168404Spjd 2088262093Savg range_tree_destroy(rtsync); 2089168404Spjd 2090262093Savg mutex_exit(&rtlock); 2091262093Savg mutex_destroy(&rtlock); 2092168404Spjd 2093262093Savg /* 2094262093Savg * If the object for the space map has changed then dirty 2095262093Savg * the top level so that we update the config. 2096262093Savg */ 2097262093Savg if (object != space_map_object(vd->vdev_dtl_sm)) { 2098262093Savg zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, " 2099262093Savg "new object %llu", txg, spa_name(spa), object, 2100262093Savg space_map_object(vd->vdev_dtl_sm)); 2101262093Savg vdev_config_dirty(vd->vdev_top); 2102262093Savg } 2103168404Spjd 2104168404Spjd dmu_tx_commit(tx); 2105262093Savg 2106262093Savg mutex_enter(&vd->vdev_dtl_lock); 2107262093Savg space_map_update(vd->vdev_dtl_sm); 2108262093Savg mutex_exit(&vd->vdev_dtl_lock); 2109168404Spjd} 2110168404Spjd 2111185029Spjd/* 2112209962Smm * Determine whether the specified vdev can be offlined/detached/removed 2113209962Smm * without losing data. 2114209962Smm */ 2115209962Smmboolean_t 2116209962Smmvdev_dtl_required(vdev_t *vd) 2117209962Smm{ 2118209962Smm spa_t *spa = vd->vdev_spa; 2119209962Smm vdev_t *tvd = vd->vdev_top; 2120209962Smm uint8_t cant_read = vd->vdev_cant_read; 2121209962Smm boolean_t required; 2122209962Smm 2123209962Smm ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2124209962Smm 2125209962Smm if (vd == spa->spa_root_vdev || vd == tvd) 2126209962Smm return (B_TRUE); 2127209962Smm 2128209962Smm /* 2129209962Smm * Temporarily mark the device as unreadable, and then determine 2130209962Smm * whether this results in any DTL outages in the top-level vdev. 2131209962Smm * If not, we can safely offline/detach/remove the device. 2132209962Smm */ 2133209962Smm vd->vdev_cant_read = B_TRUE; 2134209962Smm vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2135209962Smm required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2136209962Smm vd->vdev_cant_read = cant_read; 2137209962Smm vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2138209962Smm 2139219089Spjd if (!required && zio_injection_enabled) 2140219089Spjd required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2141219089Spjd 2142209962Smm return (required); 2143209962Smm} 2144209962Smm 2145209962Smm/* 2146185029Spjd * Determine if resilver is needed, and if so the txg range. 2147185029Spjd */ 2148185029Spjdboolean_t 2149185029Spjdvdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2150185029Spjd{ 2151185029Spjd boolean_t needed = B_FALSE; 2152185029Spjd uint64_t thismin = UINT64_MAX; 2153185029Spjd uint64_t thismax = 0; 2154185029Spjd 2155185029Spjd if (vd->vdev_children == 0) { 2156185029Spjd mutex_enter(&vd->vdev_dtl_lock); 2157262093Savg if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 && 2158209962Smm vdev_writeable(vd)) { 2159185029Spjd 2160254112Sdelphij thismin = vdev_dtl_min(vd); 2161254112Sdelphij thismax = vdev_dtl_max(vd); 2162185029Spjd needed = B_TRUE; 2163185029Spjd } 2164185029Spjd mutex_exit(&vd->vdev_dtl_lock); 2165185029Spjd } else { 2166209962Smm for (int c = 0; c < vd->vdev_children; c++) { 2167185029Spjd vdev_t *cvd = vd->vdev_child[c]; 2168185029Spjd uint64_t cmin, cmax; 2169185029Spjd 2170185029Spjd if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2171185029Spjd thismin = MIN(thismin, cmin); 2172185029Spjd thismax = MAX(thismax, cmax); 2173185029Spjd needed = B_TRUE; 2174185029Spjd } 2175185029Spjd } 2176185029Spjd } 2177185029Spjd 2178185029Spjd if (needed && minp) { 2179185029Spjd *minp = thismin; 2180185029Spjd *maxp = thismax; 2181185029Spjd } 2182185029Spjd return (needed); 2183185029Spjd} 2184185029Spjd 2185168404Spjdvoid 2186168404Spjdvdev_load(vdev_t *vd) 2187168404Spjd{ 2188168404Spjd /* 2189168404Spjd * Recursively load all children. 2190168404Spjd */ 2191209962Smm for (int c = 0; c < vd->vdev_children; c++) 2192168404Spjd vdev_load(vd->vdev_child[c]); 2193168404Spjd 2194168404Spjd /* 2195168404Spjd * If this is a top-level vdev, initialize its metaslabs. 2196168404Spjd */ 2197219089Spjd if (vd == vd->vdev_top && !vd->vdev_ishole && 2198168404Spjd (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 2199168404Spjd vdev_metaslab_init(vd, 0) != 0)) 2200168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2201168404Spjd VDEV_AUX_CORRUPT_DATA); 2202168404Spjd 2203168404Spjd /* 2204168404Spjd * If this is a leaf vdev, load its DTL. 2205168404Spjd */ 2206168404Spjd if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 2207168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2208168404Spjd VDEV_AUX_CORRUPT_DATA); 2209168404Spjd} 2210168404Spjd 2211168404Spjd/* 2212185029Spjd * The special vdev case is used for hot spares and l2cache devices. Its 2213185029Spjd * sole purpose it to set the vdev state for the associated vdev. To do this, 2214185029Spjd * we make sure that we can open the underlying device, then try to read the 2215185029Spjd * label, and make sure that the label is sane and that it hasn't been 2216185029Spjd * repurposed to another pool. 2217168404Spjd */ 2218168404Spjdint 2219185029Spjdvdev_validate_aux(vdev_t *vd) 2220168404Spjd{ 2221168404Spjd nvlist_t *label; 2222168404Spjd uint64_t guid, version; 2223168404Spjd uint64_t state; 2224168404Spjd 2225185029Spjd if (!vdev_readable(vd)) 2226185029Spjd return (0); 2227185029Spjd 2228239620Smm if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 2229168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2230168404Spjd VDEV_AUX_CORRUPT_DATA); 2231168404Spjd return (-1); 2232168404Spjd } 2233168404Spjd 2234168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2235236884Smm !SPA_VERSION_IS_SUPPORTED(version) || 2236168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 2237168404Spjd guid != vd->vdev_guid || 2238168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 2239168404Spjd vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2240168404Spjd VDEV_AUX_CORRUPT_DATA); 2241168404Spjd nvlist_free(label); 2242168404Spjd return (-1); 2243168404Spjd } 2244168404Spjd 2245168404Spjd /* 2246168404Spjd * We don't actually check the pool state here. If it's in fact in 2247168404Spjd * use by another pool, we update this fact on the fly when requested. 2248168404Spjd */ 2249168404Spjd nvlist_free(label); 2250168404Spjd return (0); 2251168404Spjd} 2252168404Spjd 2253168404Spjdvoid 2254219089Spjdvdev_remove(vdev_t *vd, uint64_t txg) 2255219089Spjd{ 2256219089Spjd spa_t *spa = vd->vdev_spa; 2257219089Spjd objset_t *mos = spa->spa_meta_objset; 2258219089Spjd dmu_tx_t *tx; 2259219089Spjd 2260219089Spjd tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2261219089Spjd 2262219089Spjd if (vd->vdev_ms != NULL) { 2263269773Sdelphij metaslab_group_t *mg = vd->vdev_mg; 2264269773Sdelphij 2265269773Sdelphij metaslab_group_histogram_verify(mg); 2266269773Sdelphij metaslab_class_histogram_verify(mg->mg_class); 2267269773Sdelphij 2268219089Spjd for (int m = 0; m < vd->vdev_ms_count; m++) { 2269219089Spjd metaslab_t *msp = vd->vdev_ms[m]; 2270219089Spjd 2271262093Savg if (msp == NULL || msp->ms_sm == NULL) 2272219089Spjd continue; 2273219089Spjd 2274262093Savg mutex_enter(&msp->ms_lock); 2275269773Sdelphij /* 2276269773Sdelphij * If the metaslab was not loaded when the vdev 2277269773Sdelphij * was removed then the histogram accounting may 2278269773Sdelphij * not be accurate. Update the histogram information 2279269773Sdelphij * here so that we ensure that the metaslab group 2280269773Sdelphij * and metaslab class are up-to-date. 2281269773Sdelphij */ 2282269773Sdelphij metaslab_group_histogram_remove(mg, msp); 2283269773Sdelphij 2284262093Savg VERIFY0(space_map_allocated(msp->ms_sm)); 2285262093Savg space_map_free(msp->ms_sm, tx); 2286262093Savg space_map_close(msp->ms_sm); 2287262093Savg msp->ms_sm = NULL; 2288262093Savg mutex_exit(&msp->ms_lock); 2289219089Spjd } 2290269773Sdelphij 2291269773Sdelphij metaslab_group_histogram_verify(mg); 2292269773Sdelphij metaslab_class_histogram_verify(mg->mg_class); 2293269773Sdelphij for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 2294269773Sdelphij ASSERT0(mg->mg_histogram[i]); 2295269773Sdelphij 2296219089Spjd } 2297219089Spjd 2298219089Spjd if (vd->vdev_ms_array) { 2299219089Spjd (void) dmu_object_free(mos, vd->vdev_ms_array, tx); 2300219089Spjd vd->vdev_ms_array = 0; 2301219089Spjd } 2302219089Spjd dmu_tx_commit(tx); 2303219089Spjd} 2304219089Spjd 2305219089Spjdvoid 2306168404Spjdvdev_sync_done(vdev_t *vd, uint64_t txg) 2307168404Spjd{ 2308168404Spjd metaslab_t *msp; 2309211931Smm boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2310168404Spjd 2311219089Spjd ASSERT(!vd->vdev_ishole); 2312219089Spjd 2313168404Spjd while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2314168404Spjd metaslab_sync_done(msp, txg); 2315211931Smm 2316211931Smm if (reassess) 2317211931Smm metaslab_sync_reassess(vd->vdev_mg); 2318168404Spjd} 2319168404Spjd 2320168404Spjdvoid 2321168404Spjdvdev_sync(vdev_t *vd, uint64_t txg) 2322168404Spjd{ 2323168404Spjd spa_t *spa = vd->vdev_spa; 2324168404Spjd vdev_t *lvd; 2325168404Spjd metaslab_t *msp; 2326168404Spjd dmu_tx_t *tx; 2327168404Spjd 2328219089Spjd ASSERT(!vd->vdev_ishole); 2329219089Spjd 2330168404Spjd if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 2331168404Spjd ASSERT(vd == vd->vdev_top); 2332168404Spjd tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2333168404Spjd vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2334168404Spjd DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2335168404Spjd ASSERT(vd->vdev_ms_array != 0); 2336168404Spjd vdev_config_dirty(vd); 2337168404Spjd dmu_tx_commit(tx); 2338168404Spjd } 2339168404Spjd 2340219089Spjd /* 2341219089Spjd * Remove the metadata associated with this vdev once it's empty. 2342219089Spjd */ 2343219089Spjd if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 2344219089Spjd vdev_remove(vd, txg); 2345219089Spjd 2346168404Spjd while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2347168404Spjd metaslab_sync(msp, txg); 2348168404Spjd (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2349168404Spjd } 2350168404Spjd 2351168404Spjd while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2352168404Spjd vdev_dtl_sync(lvd, txg); 2353168404Spjd 2354168404Spjd (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2355168404Spjd} 2356168404Spjd 2357168404Spjduint64_t 2358168404Spjdvdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2359168404Spjd{ 2360168404Spjd return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2361168404Spjd} 2362168404Spjd 2363185029Spjd/* 2364185029Spjd * Mark the given vdev faulted. A faulted vdev behaves as if the device could 2365185029Spjd * not be opened, and no I/O is attempted. 2366185029Spjd */ 2367185029Spjdint 2368219089Spjdvdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2369168404Spjd{ 2370219089Spjd vdev_t *vd, *tvd; 2371168404Spjd 2372219089Spjd spa_vdev_state_enter(spa, SCL_NONE); 2373185029Spjd 2374185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2375185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2376185029Spjd 2377185029Spjd if (!vd->vdev_ops->vdev_op_leaf) 2378185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2379185029Spjd 2380219089Spjd tvd = vd->vdev_top; 2381219089Spjd 2382185029Spjd /* 2383219089Spjd * We don't directly use the aux state here, but if we do a 2384219089Spjd * vdev_reopen(), we need this value to be present to remember why we 2385219089Spjd * were faulted. 2386219089Spjd */ 2387219089Spjd vd->vdev_label_aux = aux; 2388219089Spjd 2389219089Spjd /* 2390185029Spjd * Faulted state takes precedence over degraded. 2391185029Spjd */ 2392219089Spjd vd->vdev_delayed_close = B_FALSE; 2393185029Spjd vd->vdev_faulted = 1ULL; 2394185029Spjd vd->vdev_degraded = 0ULL; 2395219089Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 2396185029Spjd 2397185029Spjd /* 2398219089Spjd * If this device has the only valid copy of the data, then 2399219089Spjd * back off and simply mark the vdev as degraded instead. 2400185029Spjd */ 2401219089Spjd if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 2402185029Spjd vd->vdev_degraded = 1ULL; 2403185029Spjd vd->vdev_faulted = 0ULL; 2404185029Spjd 2405185029Spjd /* 2406185029Spjd * If we reopen the device and it's not dead, only then do we 2407185029Spjd * mark it degraded. 2408185029Spjd */ 2409219089Spjd vdev_reopen(tvd); 2410185029Spjd 2411219089Spjd if (vdev_readable(vd)) 2412219089Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 2413185029Spjd } 2414185029Spjd 2415185029Spjd return (spa_vdev_state_exit(spa, vd, 0)); 2416168404Spjd} 2417168404Spjd 2418185029Spjd/* 2419185029Spjd * Mark the given vdev degraded. A degraded vdev is purely an indication to the 2420185029Spjd * user that something is wrong. The vdev continues to operate as normal as far 2421185029Spjd * as I/O is concerned. 2422185029Spjd */ 2423185029Spjdint 2424219089Spjdvdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2425168404Spjd{ 2426185029Spjd vdev_t *vd; 2427168404Spjd 2428219089Spjd spa_vdev_state_enter(spa, SCL_NONE); 2429168404Spjd 2430185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2431185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2432168404Spjd 2433185029Spjd if (!vd->vdev_ops->vdev_op_leaf) 2434185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2435185029Spjd 2436185029Spjd /* 2437185029Spjd * If the vdev is already faulted, then don't do anything. 2438185029Spjd */ 2439185029Spjd if (vd->vdev_faulted || vd->vdev_degraded) 2440185029Spjd return (spa_vdev_state_exit(spa, NULL, 0)); 2441185029Spjd 2442185029Spjd vd->vdev_degraded = 1ULL; 2443185029Spjd if (!vdev_is_dead(vd)) 2444185029Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2445219089Spjd aux); 2446185029Spjd 2447185029Spjd return (spa_vdev_state_exit(spa, vd, 0)); 2448168404Spjd} 2449168404Spjd 2450185029Spjd/* 2451251631Sdelphij * Online the given vdev. 2452251631Sdelphij * 2453251631Sdelphij * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2454251631Sdelphij * spare device should be detached when the device finishes resilvering. 2455251631Sdelphij * Second, the online should be treated like a 'test' online case, so no FMA 2456251631Sdelphij * events are generated if the device fails to open. 2457185029Spjd */ 2458168404Spjdint 2459185029Spjdvdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2460168404Spjd{ 2461219089Spjd vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2462168404Spjd 2463219089Spjd spa_vdev_state_enter(spa, SCL_NONE); 2464168404Spjd 2465185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2466185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2467168404Spjd 2468168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 2469185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2470168404Spjd 2471219089Spjd tvd = vd->vdev_top; 2472168404Spjd vd->vdev_offline = B_FALSE; 2473168404Spjd vd->vdev_tmpoffline = B_FALSE; 2474185029Spjd vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 2475185029Spjd vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 2476219089Spjd 2477219089Spjd /* XXX - L2ARC 1.0 does not support expansion */ 2478219089Spjd if (!vd->vdev_aux) { 2479219089Spjd for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2480219089Spjd pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 2481219089Spjd } 2482219089Spjd 2483219089Spjd vdev_reopen(tvd); 2484185029Spjd vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 2485168404Spjd 2486219089Spjd if (!vd->vdev_aux) { 2487219089Spjd for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2488219089Spjd pvd->vdev_expanding = B_FALSE; 2489219089Spjd } 2490219089Spjd 2491185029Spjd if (newstate) 2492185029Spjd *newstate = vd->vdev_state; 2493185029Spjd if ((flags & ZFS_ONLINE_UNSPARE) && 2494185029Spjd !vdev_is_dead(vd) && vd->vdev_parent && 2495185029Spjd vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2496185029Spjd vd->vdev_parent->vdev_child[0] == vd) 2497185029Spjd vd->vdev_unspare = B_TRUE; 2498168404Spjd 2499219089Spjd if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 2500219089Spjd 2501219089Spjd /* XXX - L2ARC 1.0 does not support expansion */ 2502219089Spjd if (vd->vdev_aux) 2503219089Spjd return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 2504219089Spjd spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 2505219089Spjd } 2506209962Smm return (spa_vdev_state_exit(spa, vd, 0)); 2507168404Spjd} 2508168404Spjd 2509219089Spjdstatic int 2510219089Spjdvdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 2511168404Spjd{ 2512213197Smm vdev_t *vd, *tvd; 2513219089Spjd int error = 0; 2514219089Spjd uint64_t generation; 2515219089Spjd metaslab_group_t *mg; 2516168404Spjd 2517219089Spjdtop: 2518219089Spjd spa_vdev_state_enter(spa, SCL_ALLOC); 2519168404Spjd 2520185029Spjd if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2521185029Spjd return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2522168404Spjd 2523168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 2524185029Spjd return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2525168404Spjd 2526213197Smm tvd = vd->vdev_top; 2527219089Spjd mg = tvd->vdev_mg; 2528219089Spjd generation = spa->spa_config_generation + 1; 2529213197Smm 2530168404Spjd /* 2531168404Spjd * If the device isn't already offline, try to offline it. 2532168404Spjd */ 2533168404Spjd if (!vd->vdev_offline) { 2534168404Spjd /* 2535209962Smm * If this device has the only valid copy of some data, 2536213197Smm * don't allow it to be offlined. Log devices are always 2537213197Smm * expendable. 2538168404Spjd */ 2539213197Smm if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2540213197Smm vdev_dtl_required(vd)) 2541185029Spjd return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2542168404Spjd 2543168404Spjd /* 2544219089Spjd * If the top-level is a slog and it has had allocations 2545219089Spjd * then proceed. We check that the vdev's metaslab group 2546219089Spjd * is not NULL since it's possible that we may have just 2547219089Spjd * added this vdev but not yet initialized its metaslabs. 2548219089Spjd */ 2549219089Spjd if (tvd->vdev_islog && mg != NULL) { 2550219089Spjd /* 2551219089Spjd * Prevent any future allocations. 2552219089Spjd */ 2553219089Spjd metaslab_group_passivate(mg); 2554219089Spjd (void) spa_vdev_state_exit(spa, vd, 0); 2555219089Spjd 2556219089Spjd error = spa_offline_log(spa); 2557219089Spjd 2558219089Spjd spa_vdev_state_enter(spa, SCL_ALLOC); 2559219089Spjd 2560219089Spjd /* 2561219089Spjd * Check to see if the config has changed. 2562219089Spjd */ 2563219089Spjd if (error || generation != spa->spa_config_generation) { 2564219089Spjd metaslab_group_activate(mg); 2565219089Spjd if (error) 2566219089Spjd return (spa_vdev_state_exit(spa, 2567219089Spjd vd, error)); 2568219089Spjd (void) spa_vdev_state_exit(spa, vd, 0); 2569219089Spjd goto top; 2570219089Spjd } 2571240415Smm ASSERT0(tvd->vdev_stat.vs_alloc); 2572219089Spjd } 2573219089Spjd 2574219089Spjd /* 2575168404Spjd * Offline this device and reopen its top-level vdev. 2576213197Smm * If the top-level vdev is a log device then just offline 2577213197Smm * it. Otherwise, if this action results in the top-level 2578213197Smm * vdev becoming unusable, undo it and fail the request. 2579168404Spjd */ 2580168404Spjd vd->vdev_offline = B_TRUE; 2581213197Smm vdev_reopen(tvd); 2582213197Smm 2583213197Smm if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2584213197Smm vdev_is_dead(tvd)) { 2585168404Spjd vd->vdev_offline = B_FALSE; 2586213197Smm vdev_reopen(tvd); 2587185029Spjd return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2588168404Spjd } 2589219089Spjd 2590219089Spjd /* 2591219089Spjd * Add the device back into the metaslab rotor so that 2592219089Spjd * once we online the device it's open for business. 2593219089Spjd */ 2594219089Spjd if (tvd->vdev_islog && mg != NULL) 2595219089Spjd metaslab_group_activate(mg); 2596168404Spjd } 2597168404Spjd 2598185029Spjd vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 2599168404Spjd 2600219089Spjd return (spa_vdev_state_exit(spa, vd, 0)); 2601219089Spjd} 2602213197Smm 2603219089Spjdint 2604219089Spjdvdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 2605219089Spjd{ 2606219089Spjd int error; 2607213197Smm 2608219089Spjd mutex_enter(&spa->spa_vdev_top_lock); 2609219089Spjd error = vdev_offline_locked(spa, guid, flags); 2610219089Spjd mutex_exit(&spa->spa_vdev_top_lock); 2611219089Spjd 2612219089Spjd return (error); 2613168404Spjd} 2614168404Spjd 2615168404Spjd/* 2616168404Spjd * Clear the error counts associated with this vdev. Unlike vdev_online() and 2617168404Spjd * vdev_offline(), we assume the spa config is locked. We also clear all 2618168404Spjd * children. If 'vd' is NULL, then the user wants to clear all vdevs. 2619168404Spjd */ 2620168404Spjdvoid 2621168404Spjdvdev_clear(spa_t *spa, vdev_t *vd) 2622168404Spjd{ 2623185029Spjd vdev_t *rvd = spa->spa_root_vdev; 2624168404Spjd 2625185029Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2626185029Spjd 2627168404Spjd if (vd == NULL) 2628185029Spjd vd = rvd; 2629168404Spjd 2630168404Spjd vd->vdev_stat.vs_read_errors = 0; 2631168404Spjd vd->vdev_stat.vs_write_errors = 0; 2632168404Spjd vd->vdev_stat.vs_checksum_errors = 0; 2633168404Spjd 2634185029Spjd for (int c = 0; c < vd->vdev_children; c++) 2635168404Spjd vdev_clear(spa, vd->vdev_child[c]); 2636185029Spjd 2637253991Smav if (vd == rvd) { 2638253991Smav for (int c = 0; c < spa->spa_l2cache.sav_count; c++) 2639253991Smav vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]); 2640253991Smav 2641253991Smav for (int c = 0; c < spa->spa_spares.sav_count; c++) 2642253991Smav vdev_clear(spa, spa->spa_spares.sav_vdevs[c]); 2643253991Smav } 2644253991Smav 2645185029Spjd /* 2646185029Spjd * If we're in the FAULTED state or have experienced failed I/O, then 2647185029Spjd * clear the persistent state and attempt to reopen the device. We 2648185029Spjd * also mark the vdev config dirty, so that the new faulted state is 2649185029Spjd * written out to disk. 2650185029Spjd */ 2651185029Spjd if (vd->vdev_faulted || vd->vdev_degraded || 2652185029Spjd !vdev_readable(vd) || !vdev_writeable(vd)) { 2653185029Spjd 2654219089Spjd /* 2655219089Spjd * When reopening in reponse to a clear event, it may be due to 2656219089Spjd * a fmadm repair request. In this case, if the device is 2657219089Spjd * still broken, we want to still post the ereport again. 2658219089Spjd */ 2659219089Spjd vd->vdev_forcefault = B_TRUE; 2660219089Spjd 2661219089Spjd vd->vdev_faulted = vd->vdev_degraded = 0ULL; 2662185029Spjd vd->vdev_cant_read = B_FALSE; 2663185029Spjd vd->vdev_cant_write = B_FALSE; 2664185029Spjd 2665219089Spjd vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 2666185029Spjd 2667219089Spjd vd->vdev_forcefault = B_FALSE; 2668219089Spjd 2669219089Spjd if (vd != rvd && vdev_writeable(vd->vdev_top)) 2670185029Spjd vdev_state_dirty(vd->vdev_top); 2671185029Spjd 2672185029Spjd if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 2673185029Spjd spa_async_request(spa, SPA_ASYNC_RESILVER); 2674185029Spjd 2675185029Spjd spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 2676185029Spjd } 2677219089Spjd 2678219089Spjd /* 2679219089Spjd * When clearing a FMA-diagnosed fault, we always want to 2680219089Spjd * unspare the device, as we assume that the original spare was 2681219089Spjd * done in response to the FMA fault. 2682219089Spjd */ 2683219089Spjd if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 2684219089Spjd vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2685219089Spjd vd->vdev_parent->vdev_child[0] == vd) 2686219089Spjd vd->vdev_unspare = B_TRUE; 2687168404Spjd} 2688168404Spjd 2689185029Spjdboolean_t 2690168404Spjdvdev_is_dead(vdev_t *vd) 2691168404Spjd{ 2692219089Spjd /* 2693219089Spjd * Holes and missing devices are always considered "dead". 2694219089Spjd * This simplifies the code since we don't have to check for 2695219089Spjd * these types of devices in the various code paths. 2696219089Spjd * Instead we rely on the fact that we skip over dead devices 2697219089Spjd * before issuing I/O to them. 2698219089Spjd */ 2699219089Spjd return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole || 2700219089Spjd vd->vdev_ops == &vdev_missing_ops); 2701168404Spjd} 2702168404Spjd 2703185029Spjdboolean_t 2704185029Spjdvdev_readable(vdev_t *vd) 2705168404Spjd{ 2706185029Spjd return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2707185029Spjd} 2708168404Spjd 2709185029Spjdboolean_t 2710185029Spjdvdev_writeable(vdev_t *vd) 2711185029Spjd{ 2712185029Spjd return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 2713185029Spjd} 2714168404Spjd 2715185029Spjdboolean_t 2716208370Smmvdev_allocatable(vdev_t *vd) 2717208370Smm{ 2718209962Smm uint64_t state = vd->vdev_state; 2719209962Smm 2720208370Smm /* 2721209962Smm * We currently allow allocations from vdevs which may be in the 2722208370Smm * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 2723208370Smm * fails to reopen then we'll catch it later when we're holding 2724209962Smm * the proper locks. Note that we have to get the vdev state 2725209962Smm * in a local variable because although it changes atomically, 2726209962Smm * we're asking two separate questions about it. 2727208370Smm */ 2728209962Smm return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 2729219089Spjd !vd->vdev_cant_write && !vd->vdev_ishole); 2730208370Smm} 2731208370Smm 2732208370Smmboolean_t 2733185029Spjdvdev_accessible(vdev_t *vd, zio_t *zio) 2734185029Spjd{ 2735185029Spjd ASSERT(zio->io_vd == vd); 2736168404Spjd 2737185029Spjd if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 2738185029Spjd return (B_FALSE); 2739168404Spjd 2740185029Spjd if (zio->io_type == ZIO_TYPE_READ) 2741185029Spjd return (!vd->vdev_cant_read); 2742168404Spjd 2743185029Spjd if (zio->io_type == ZIO_TYPE_WRITE) 2744185029Spjd return (!vd->vdev_cant_write); 2745168404Spjd 2746185029Spjd return (B_TRUE); 2747168404Spjd} 2748168404Spjd 2749168404Spjd/* 2750168404Spjd * Get statistics for the given vdev. 2751168404Spjd */ 2752168404Spjdvoid 2753168404Spjdvdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2754168404Spjd{ 2755269773Sdelphij spa_t *spa = vd->vdev_spa; 2756269773Sdelphij vdev_t *rvd = spa->spa_root_vdev; 2757168404Spjd 2758269773Sdelphij ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2759269773Sdelphij 2760168404Spjd mutex_enter(&vd->vdev_stat_lock); 2761168404Spjd bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 2762168404Spjd vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2763168404Spjd vs->vs_state = vd->vdev_state; 2764219089Spjd vs->vs_rsize = vdev_get_min_asize(vd); 2765219089Spjd if (vd->vdev_ops->vdev_op_leaf) 2766219089Spjd vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 2767236155Smm vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize; 2768254591Sgibbs vs->vs_configured_ashift = vd->vdev_top != NULL 2769254591Sgibbs ? vd->vdev_top->vdev_ashift : vd->vdev_ashift; 2770254591Sgibbs vs->vs_logical_ashift = vd->vdev_logical_ashift; 2771254591Sgibbs vs->vs_physical_ashift = vd->vdev_physical_ashift; 2772270128Sdelphij if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) { 2773269773Sdelphij vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 2774270128Sdelphij } 2775168404Spjd 2776168404Spjd /* 2777168404Spjd * If we're getting stats on the root vdev, aggregate the I/O counts 2778168404Spjd * over all top-level vdevs (i.e. the direct children of the root). 2779168404Spjd */ 2780168404Spjd if (vd == rvd) { 2781185029Spjd for (int c = 0; c < rvd->vdev_children; c++) { 2782168404Spjd vdev_t *cvd = rvd->vdev_child[c]; 2783168404Spjd vdev_stat_t *cvs = &cvd->vdev_stat; 2784168404Spjd 2785185029Spjd for (int t = 0; t < ZIO_TYPES; t++) { 2786168404Spjd vs->vs_ops[t] += cvs->vs_ops[t]; 2787168404Spjd vs->vs_bytes[t] += cvs->vs_bytes[t]; 2788168404Spjd } 2789219089Spjd cvs->vs_scan_removing = cvd->vdev_removing; 2790168404Spjd } 2791168404Spjd } 2792269773Sdelphij mutex_exit(&vd->vdev_stat_lock); 2793168404Spjd} 2794168404Spjd 2795168404Spjdvoid 2796185029Spjdvdev_clear_stats(vdev_t *vd) 2797168404Spjd{ 2798185029Spjd mutex_enter(&vd->vdev_stat_lock); 2799185029Spjd vd->vdev_stat.vs_space = 0; 2800185029Spjd vd->vdev_stat.vs_dspace = 0; 2801185029Spjd vd->vdev_stat.vs_alloc = 0; 2802185029Spjd mutex_exit(&vd->vdev_stat_lock); 2803185029Spjd} 2804185029Spjd 2805185029Spjdvoid 2806219089Spjdvdev_scan_stat_init(vdev_t *vd) 2807219089Spjd{ 2808219089Spjd vdev_stat_t *vs = &vd->vdev_stat; 2809219089Spjd 2810219089Spjd for (int c = 0; c < vd->vdev_children; c++) 2811219089Spjd vdev_scan_stat_init(vd->vdev_child[c]); 2812219089Spjd 2813219089Spjd mutex_enter(&vd->vdev_stat_lock); 2814219089Spjd vs->vs_scan_processed = 0; 2815219089Spjd mutex_exit(&vd->vdev_stat_lock); 2816219089Spjd} 2817219089Spjd 2818219089Spjdvoid 2819185029Spjdvdev_stat_update(zio_t *zio, uint64_t psize) 2820185029Spjd{ 2821209962Smm spa_t *spa = zio->io_spa; 2822209962Smm vdev_t *rvd = spa->spa_root_vdev; 2823185029Spjd vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2824168404Spjd vdev_t *pvd; 2825168404Spjd uint64_t txg = zio->io_txg; 2826168404Spjd vdev_stat_t *vs = &vd->vdev_stat; 2827168404Spjd zio_type_t type = zio->io_type; 2828168404Spjd int flags = zio->io_flags; 2829168404Spjd 2830185029Spjd /* 2831185029Spjd * If this i/o is a gang leader, it didn't do any actual work. 2832185029Spjd */ 2833185029Spjd if (zio->io_gang_tree) 2834185029Spjd return; 2835185029Spjd 2836168404Spjd if (zio->io_error == 0) { 2837185029Spjd /* 2838185029Spjd * If this is a root i/o, don't count it -- we've already 2839185029Spjd * counted the top-level vdevs, and vdev_get_stats() will 2840185029Spjd * aggregate them when asked. This reduces contention on 2841185029Spjd * the root vdev_stat_lock and implicitly handles blocks 2842185029Spjd * that compress away to holes, for which there is no i/o. 2843185029Spjd * (Holes never create vdev children, so all the counters 2844185029Spjd * remain zero, which is what we want.) 2845185029Spjd * 2846185029Spjd * Note: this only applies to successful i/o (io_error == 0) 2847185029Spjd * because unlike i/o counts, errors are not additive. 2848185029Spjd * When reading a ditto block, for example, failure of 2849185029Spjd * one top-level vdev does not imply a root-level error. 2850185029Spjd */ 2851185029Spjd if (vd == rvd) 2852185029Spjd return; 2853185029Spjd 2854185029Spjd ASSERT(vd == zio->io_vd); 2855209962Smm 2856209962Smm if (flags & ZIO_FLAG_IO_BYPASS) 2857209962Smm return; 2858209962Smm 2859209962Smm mutex_enter(&vd->vdev_stat_lock); 2860209962Smm 2861185029Spjd if (flags & ZIO_FLAG_IO_REPAIR) { 2862219089Spjd if (flags & ZIO_FLAG_SCAN_THREAD) { 2863219089Spjd dsl_scan_phys_t *scn_phys = 2864219089Spjd &spa->spa_dsl_pool->dp_scan->scn_phys; 2865219089Spjd uint64_t *processed = &scn_phys->scn_processed; 2866219089Spjd 2867219089Spjd /* XXX cleanup? */ 2868219089Spjd if (vd->vdev_ops->vdev_op_leaf) 2869219089Spjd atomic_add_64(processed, psize); 2870219089Spjd vs->vs_scan_processed += psize; 2871219089Spjd } 2872219089Spjd 2873209962Smm if (flags & ZIO_FLAG_SELF_HEAL) 2874185029Spjd vs->vs_self_healed += psize; 2875168404Spjd } 2876209962Smm 2877209962Smm vs->vs_ops[type]++; 2878209962Smm vs->vs_bytes[type] += psize; 2879209962Smm 2880209962Smm mutex_exit(&vd->vdev_stat_lock); 2881168404Spjd return; 2882168404Spjd } 2883168404Spjd 2884168404Spjd if (flags & ZIO_FLAG_SPECULATIVE) 2885168404Spjd return; 2886168404Spjd 2887213198Smm /* 2888213198Smm * If this is an I/O error that is going to be retried, then ignore the 2889213198Smm * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 2890213198Smm * hard errors, when in reality they can happen for any number of 2891213198Smm * innocuous reasons (bus resets, MPxIO link failure, etc). 2892213198Smm */ 2893213198Smm if (zio->io_error == EIO && 2894213198Smm !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 2895213198Smm return; 2896213198Smm 2897219089Spjd /* 2898219089Spjd * Intent logs writes won't propagate their error to the root 2899219089Spjd * I/O so don't mark these types of failures as pool-level 2900219089Spjd * errors. 2901219089Spjd */ 2902219089Spjd if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 2903219089Spjd return; 2904219089Spjd 2905185029Spjd mutex_enter(&vd->vdev_stat_lock); 2906209962Smm if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 2907185029Spjd if (zio->io_error == ECKSUM) 2908185029Spjd vs->vs_checksum_errors++; 2909185029Spjd else 2910185029Spjd vs->vs_read_errors++; 2911168404Spjd } 2912209962Smm if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 2913185029Spjd vs->vs_write_errors++; 2914185029Spjd mutex_exit(&vd->vdev_stat_lock); 2915168404Spjd 2916209962Smm if (type == ZIO_TYPE_WRITE && txg != 0 && 2917209962Smm (!(flags & ZIO_FLAG_IO_REPAIR) || 2918219089Spjd (flags & ZIO_FLAG_SCAN_THREAD) || 2919219089Spjd spa->spa_claiming)) { 2920209962Smm /* 2921219089Spjd * This is either a normal write (not a repair), or it's 2922219089Spjd * a repair induced by the scrub thread, or it's a repair 2923219089Spjd * made by zil_claim() during spa_load() in the first txg. 2924219089Spjd * In the normal case, we commit the DTL change in the same 2925219089Spjd * txg as the block was born. In the scrub-induced repair 2926219089Spjd * case, we know that scrubs run in first-pass syncing context, 2927219089Spjd * so we commit the DTL change in spa_syncing_txg(spa). 2928219089Spjd * In the zil_claim() case, we commit in spa_first_txg(spa). 2929209962Smm * 2930209962Smm * We currently do not make DTL entries for failed spontaneous 2931209962Smm * self-healing writes triggered by normal (non-scrubbing) 2932209962Smm * reads, because we have no transactional context in which to 2933209962Smm * do so -- and it's not clear that it'd be desirable anyway. 2934209962Smm */ 2935209962Smm if (vd->vdev_ops->vdev_op_leaf) { 2936209962Smm uint64_t commit_txg = txg; 2937219089Spjd if (flags & ZIO_FLAG_SCAN_THREAD) { 2938209962Smm ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2939209962Smm ASSERT(spa_sync_pass(spa) == 1); 2940209962Smm vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 2941219089Spjd commit_txg = spa_syncing_txg(spa); 2942219089Spjd } else if (spa->spa_claiming) { 2943219089Spjd ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2944219089Spjd commit_txg = spa_first_txg(spa); 2945209962Smm } 2946219089Spjd ASSERT(commit_txg >= spa_syncing_txg(spa)); 2947209962Smm if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 2948168404Spjd return; 2949209962Smm for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2950209962Smm vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 2951209962Smm vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2952168404Spjd } 2953209962Smm if (vd != rvd) 2954209962Smm vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2955168404Spjd } 2956168404Spjd} 2957168404Spjd 2958168404Spjd/* 2959219089Spjd * Update the in-core space usage stats for this vdev, its metaslab class, 2960219089Spjd * and the root vdev. 2961168404Spjd */ 2962168404Spjdvoid 2963219089Spjdvdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 2964219089Spjd int64_t space_delta) 2965168404Spjd{ 2966168404Spjd int64_t dspace_delta = space_delta; 2967185029Spjd spa_t *spa = vd->vdev_spa; 2968185029Spjd vdev_t *rvd = spa->spa_root_vdev; 2969219089Spjd metaslab_group_t *mg = vd->vdev_mg; 2970219089Spjd metaslab_class_t *mc = mg ? mg->mg_class : NULL; 2971168404Spjd 2972185029Spjd ASSERT(vd == vd->vdev_top); 2973168404Spjd 2974185029Spjd /* 2975185029Spjd * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 2976185029Spjd * factor. We must calculate this here and not at the root vdev 2977185029Spjd * because the root vdev's psize-to-asize is simply the max of its 2978185029Spjd * childrens', thus not accurate enough for us. 2979185029Spjd */ 2980185029Spjd ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 2981213197Smm ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 2982185029Spjd dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 2983185029Spjd vd->vdev_deflate_ratio; 2984185029Spjd 2985185029Spjd mutex_enter(&vd->vdev_stat_lock); 2986219089Spjd vd->vdev_stat.vs_alloc += alloc_delta; 2987185029Spjd vd->vdev_stat.vs_space += space_delta; 2988185029Spjd vd->vdev_stat.vs_dspace += dspace_delta; 2989185029Spjd mutex_exit(&vd->vdev_stat_lock); 2990185029Spjd 2991219089Spjd if (mc == spa_normal_class(spa)) { 2992185029Spjd mutex_enter(&rvd->vdev_stat_lock); 2993219089Spjd rvd->vdev_stat.vs_alloc += alloc_delta; 2994185029Spjd rvd->vdev_stat.vs_space += space_delta; 2995185029Spjd rvd->vdev_stat.vs_dspace += dspace_delta; 2996185029Spjd mutex_exit(&rvd->vdev_stat_lock); 2997185029Spjd } 2998219089Spjd 2999219089Spjd if (mc != NULL) { 3000219089Spjd ASSERT(rvd == vd->vdev_parent); 3001219089Spjd ASSERT(vd->vdev_ms_count != 0); 3002219089Spjd 3003219089Spjd metaslab_class_space_update(mc, 3004219089Spjd alloc_delta, defer_delta, space_delta, dspace_delta); 3005219089Spjd } 3006168404Spjd} 3007168404Spjd 3008168404Spjd/* 3009168404Spjd * Mark a top-level vdev's config as dirty, placing it on the dirty list 3010168404Spjd * so that it will be written out next time the vdev configuration is synced. 3011168404Spjd * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3012168404Spjd */ 3013168404Spjdvoid 3014168404Spjdvdev_config_dirty(vdev_t *vd) 3015168404Spjd{ 3016168404Spjd spa_t *spa = vd->vdev_spa; 3017168404Spjd vdev_t *rvd = spa->spa_root_vdev; 3018168404Spjd int c; 3019168404Spjd 3020219089Spjd ASSERT(spa_writeable(spa)); 3021219089Spjd 3022168404Spjd /* 3023209962Smm * If this is an aux vdev (as with l2cache and spare devices), then we 3024209962Smm * update the vdev config manually and set the sync flag. 3025185029Spjd */ 3026185029Spjd if (vd->vdev_aux != NULL) { 3027185029Spjd spa_aux_vdev_t *sav = vd->vdev_aux; 3028185029Spjd nvlist_t **aux; 3029185029Spjd uint_t naux; 3030185029Spjd 3031185029Spjd for (c = 0; c < sav->sav_count; c++) { 3032185029Spjd if (sav->sav_vdevs[c] == vd) 3033185029Spjd break; 3034185029Spjd } 3035185029Spjd 3036185029Spjd if (c == sav->sav_count) { 3037185029Spjd /* 3038185029Spjd * We're being removed. There's nothing more to do. 3039185029Spjd */ 3040185029Spjd ASSERT(sav->sav_sync == B_TRUE); 3041185029Spjd return; 3042185029Spjd } 3043185029Spjd 3044185029Spjd sav->sav_sync = B_TRUE; 3045185029Spjd 3046209962Smm if (nvlist_lookup_nvlist_array(sav->sav_config, 3047209962Smm ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 3048209962Smm VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 3049209962Smm ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 3050209962Smm } 3051185029Spjd 3052185029Spjd ASSERT(c < naux); 3053185029Spjd 3054185029Spjd /* 3055185029Spjd * Setting the nvlist in the middle if the array is a little 3056185029Spjd * sketchy, but it will work. 3057185029Spjd */ 3058185029Spjd nvlist_free(aux[c]); 3059219089Spjd aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3060185029Spjd 3061185029Spjd return; 3062185029Spjd } 3063185029Spjd 3064185029Spjd /* 3065185029Spjd * The dirty list is protected by the SCL_CONFIG lock. The caller 3066185029Spjd * must either hold SCL_CONFIG as writer, or must be the sync thread 3067185029Spjd * (which holds SCL_CONFIG as reader). There's only one sync thread, 3068168404Spjd * so this is sufficient to ensure mutual exclusion. 3069168404Spjd */ 3070185029Spjd ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3071185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3072185029Spjd spa_config_held(spa, SCL_CONFIG, RW_READER))); 3073168404Spjd 3074168404Spjd if (vd == rvd) { 3075168404Spjd for (c = 0; c < rvd->vdev_children; c++) 3076168404Spjd vdev_config_dirty(rvd->vdev_child[c]); 3077168404Spjd } else { 3078168404Spjd ASSERT(vd == vd->vdev_top); 3079168404Spjd 3080219089Spjd if (!list_link_active(&vd->vdev_config_dirty_node) && 3081219089Spjd !vd->vdev_ishole) 3082185029Spjd list_insert_head(&spa->spa_config_dirty_list, vd); 3083168404Spjd } 3084168404Spjd} 3085168404Spjd 3086168404Spjdvoid 3087168404Spjdvdev_config_clean(vdev_t *vd) 3088168404Spjd{ 3089168404Spjd spa_t *spa = vd->vdev_spa; 3090168404Spjd 3091185029Spjd ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3092185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3093185029Spjd spa_config_held(spa, SCL_CONFIG, RW_READER))); 3094168404Spjd 3095185029Spjd ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3096185029Spjd list_remove(&spa->spa_config_dirty_list, vd); 3097168404Spjd} 3098168404Spjd 3099185029Spjd/* 3100185029Spjd * Mark a top-level vdev's state as dirty, so that the next pass of 3101185029Spjd * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3102185029Spjd * the state changes from larger config changes because they require 3103185029Spjd * much less locking, and are often needed for administrative actions. 3104185029Spjd */ 3105168404Spjdvoid 3106185029Spjdvdev_state_dirty(vdev_t *vd) 3107185029Spjd{ 3108185029Spjd spa_t *spa = vd->vdev_spa; 3109185029Spjd 3110219089Spjd ASSERT(spa_writeable(spa)); 3111185029Spjd ASSERT(vd == vd->vdev_top); 3112185029Spjd 3113185029Spjd /* 3114185029Spjd * The state list is protected by the SCL_STATE lock. The caller 3115185029Spjd * must either hold SCL_STATE as writer, or must be the sync thread 3116185029Spjd * (which holds SCL_STATE as reader). There's only one sync thread, 3117185029Spjd * so this is sufficient to ensure mutual exclusion. 3118185029Spjd */ 3119185029Spjd ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3120185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3121185029Spjd spa_config_held(spa, SCL_STATE, RW_READER))); 3122185029Spjd 3123219089Spjd if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole) 3124185029Spjd list_insert_head(&spa->spa_state_dirty_list, vd); 3125185029Spjd} 3126185029Spjd 3127185029Spjdvoid 3128185029Spjdvdev_state_clean(vdev_t *vd) 3129185029Spjd{ 3130185029Spjd spa_t *spa = vd->vdev_spa; 3131185029Spjd 3132185029Spjd ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3133185029Spjd (dsl_pool_sync_context(spa_get_dsl(spa)) && 3134185029Spjd spa_config_held(spa, SCL_STATE, RW_READER))); 3135185029Spjd 3136185029Spjd ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3137185029Spjd list_remove(&spa->spa_state_dirty_list, vd); 3138185029Spjd} 3139185029Spjd 3140185029Spjd/* 3141185029Spjd * Propagate vdev state up from children to parent. 3142185029Spjd */ 3143185029Spjdvoid 3144168404Spjdvdev_propagate_state(vdev_t *vd) 3145168404Spjd{ 3146209962Smm spa_t *spa = vd->vdev_spa; 3147209962Smm vdev_t *rvd = spa->spa_root_vdev; 3148168404Spjd int degraded = 0, faulted = 0; 3149168404Spjd int corrupted = 0; 3150168404Spjd vdev_t *child; 3151168404Spjd 3152185029Spjd if (vd->vdev_children > 0) { 3153219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 3154185029Spjd child = vd->vdev_child[c]; 3155168404Spjd 3156219089Spjd /* 3157219089Spjd * Don't factor holes into the decision. 3158219089Spjd */ 3159219089Spjd if (child->vdev_ishole) 3160219089Spjd continue; 3161219089Spjd 3162185029Spjd if (!vdev_readable(child) || 3163209962Smm (!vdev_writeable(child) && spa_writeable(spa))) { 3164185029Spjd /* 3165185029Spjd * Root special: if there is a top-level log 3166185029Spjd * device, treat the root vdev as if it were 3167185029Spjd * degraded. 3168185029Spjd */ 3169185029Spjd if (child->vdev_islog && vd == rvd) 3170185029Spjd degraded++; 3171185029Spjd else 3172185029Spjd faulted++; 3173185029Spjd } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 3174185029Spjd degraded++; 3175185029Spjd } 3176185029Spjd 3177185029Spjd if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 3178185029Spjd corrupted++; 3179185029Spjd } 3180185029Spjd 3181185029Spjd vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 3182185029Spjd 3183185029Spjd /* 3184185029Spjd * Root special: if there is a top-level vdev that cannot be 3185185029Spjd * opened due to corrupted metadata, then propagate the root 3186185029Spjd * vdev's aux state as 'corrupt' rather than 'insufficient 3187185029Spjd * replicas'. 3188185029Spjd */ 3189185029Spjd if (corrupted && vd == rvd && 3190185029Spjd rvd->vdev_state == VDEV_STATE_CANT_OPEN) 3191185029Spjd vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 3192185029Spjd VDEV_AUX_CORRUPT_DATA); 3193168404Spjd } 3194168404Spjd 3195185029Spjd if (vd->vdev_parent) 3196185029Spjd vdev_propagate_state(vd->vdev_parent); 3197168404Spjd} 3198168404Spjd 3199168404Spjd/* 3200168404Spjd * Set a vdev's state. If this is during an open, we don't update the parent 3201168404Spjd * state, because we're in the process of opening children depth-first. 3202168404Spjd * Otherwise, we propagate the change to the parent. 3203168404Spjd * 3204168404Spjd * If this routine places a device in a faulted state, an appropriate ereport is 3205168404Spjd * generated. 3206168404Spjd */ 3207168404Spjdvoid 3208168404Spjdvdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3209168404Spjd{ 3210168404Spjd uint64_t save_state; 3211185029Spjd spa_t *spa = vd->vdev_spa; 3212168404Spjd 3213168404Spjd if (state == vd->vdev_state) { 3214168404Spjd vd->vdev_stat.vs_aux = aux; 3215168404Spjd return; 3216168404Spjd } 3217168404Spjd 3218168404Spjd save_state = vd->vdev_state; 3219168404Spjd 3220168404Spjd vd->vdev_state = state; 3221168404Spjd vd->vdev_stat.vs_aux = aux; 3222168404Spjd 3223173373Spjd /* 3224173373Spjd * If we are setting the vdev state to anything but an open state, then 3225219089Spjd * always close the underlying device unless the device has requested 3226219089Spjd * a delayed close (i.e. we're about to remove or fault the device). 3227219089Spjd * Otherwise, we keep accessible but invalid devices open forever. 3228219089Spjd * We don't call vdev_close() itself, because that implies some extra 3229219089Spjd * checks (offline, etc) that we don't want here. This is limited to 3230219089Spjd * leaf devices, because otherwise closing the device will affect other 3231219089Spjd * children. 3232173373Spjd */ 3233219089Spjd if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 3234219089Spjd vd->vdev_ops->vdev_op_leaf) 3235173373Spjd vd->vdev_ops->vdev_op_close(vd); 3236173373Spjd 3237219089Spjd /* 3238219089Spjd * If we have brought this vdev back into service, we need 3239219089Spjd * to notify fmd so that it can gracefully repair any outstanding 3240219089Spjd * cases due to a missing device. We do this in all cases, even those 3241219089Spjd * that probably don't correlate to a repaired fault. This is sure to 3242219089Spjd * catch all cases, and we let the zfs-retire agent sort it out. If 3243219089Spjd * this is a transient state it's OK, as the retire agent will 3244219089Spjd * double-check the state of the vdev before repairing it. 3245219089Spjd */ 3246219089Spjd if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3247219089Spjd vd->vdev_prevstate != state) 3248219089Spjd zfs_post_state_change(spa, vd); 3249219089Spjd 3250185029Spjd if (vd->vdev_removed && 3251185029Spjd state == VDEV_STATE_CANT_OPEN && 3252185029Spjd (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 3253168404Spjd /* 3254185029Spjd * If the previous state is set to VDEV_STATE_REMOVED, then this 3255185029Spjd * device was previously marked removed and someone attempted to 3256185029Spjd * reopen it. If this failed due to a nonexistent device, then 3257185029Spjd * keep the device in the REMOVED state. We also let this be if 3258185029Spjd * it is one of our special test online cases, which is only 3259185029Spjd * attempting to online the device and shouldn't generate an FMA 3260185029Spjd * fault. 3261185029Spjd */ 3262185029Spjd vd->vdev_state = VDEV_STATE_REMOVED; 3263185029Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 3264185029Spjd } else if (state == VDEV_STATE_REMOVED) { 3265185029Spjd vd->vdev_removed = B_TRUE; 3266185029Spjd } else if (state == VDEV_STATE_CANT_OPEN) { 3267185029Spjd /* 3268219089Spjd * If we fail to open a vdev during an import or recovery, we 3269219089Spjd * mark it as "not available", which signifies that it was 3270219089Spjd * never there to begin with. Failure to open such a device 3271219089Spjd * is not considered an error. 3272168404Spjd */ 3273219089Spjd if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3274219089Spjd spa_load_state(spa) == SPA_LOAD_RECOVER) && 3275168404Spjd vd->vdev_ops->vdev_op_leaf) 3276168404Spjd vd->vdev_not_present = 1; 3277168404Spjd 3278168404Spjd /* 3279168404Spjd * Post the appropriate ereport. If the 'prevstate' field is 3280168404Spjd * set to something other than VDEV_STATE_UNKNOWN, it indicates 3281168404Spjd * that this is part of a vdev_reopen(). In this case, we don't 3282168404Spjd * want to post the ereport if the device was already in the 3283168404Spjd * CANT_OPEN state beforehand. 3284185029Spjd * 3285185029Spjd * If the 'checkremove' flag is set, then this is an attempt to 3286185029Spjd * online the device in response to an insertion event. If we 3287185029Spjd * hit this case, then we have detected an insertion event for a 3288185029Spjd * faulted or offline device that wasn't in the removed state. 3289185029Spjd * In this scenario, we don't post an ereport because we are 3290185029Spjd * about to replace the device, or attempt an online with 3291185029Spjd * vdev_forcefault, which will generate the fault for us. 3292168404Spjd */ 3293185029Spjd if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 3294185029Spjd !vd->vdev_not_present && !vd->vdev_checkremove && 3295185029Spjd vd != spa->spa_root_vdev) { 3296168404Spjd const char *class; 3297168404Spjd 3298168404Spjd switch (aux) { 3299168404Spjd case VDEV_AUX_OPEN_FAILED: 3300168404Spjd class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3301168404Spjd break; 3302168404Spjd case VDEV_AUX_CORRUPT_DATA: 3303168404Spjd class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3304168404Spjd break; 3305168404Spjd case VDEV_AUX_NO_REPLICAS: 3306168404Spjd class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3307168404Spjd break; 3308168404Spjd case VDEV_AUX_BAD_GUID_SUM: 3309168404Spjd class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3310168404Spjd break; 3311168404Spjd case VDEV_AUX_TOO_SMALL: 3312168404Spjd class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3313168404Spjd break; 3314168404Spjd case VDEV_AUX_BAD_LABEL: 3315168404Spjd class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3316168404Spjd break; 3317168404Spjd default: 3318168404Spjd class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3319168404Spjd } 3320168404Spjd 3321185029Spjd zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3322168404Spjd } 3323185029Spjd 3324185029Spjd /* Erase any notion of persistent removed state */ 3325185029Spjd vd->vdev_removed = B_FALSE; 3326185029Spjd } else { 3327185029Spjd vd->vdev_removed = B_FALSE; 3328168404Spjd } 3329168404Spjd 3330209962Smm if (!isopen && vd->vdev_parent) 3331209962Smm vdev_propagate_state(vd->vdev_parent); 3332185029Spjd} 3333168404Spjd 3334185029Spjd/* 3335185029Spjd * Check the vdev configuration to ensure that it's capable of supporting 3336193163Sdfr * a root pool. 3337193163Sdfr * 3338193163Sdfr * On Solaris, we do not support RAID-Z or partial configuration. In 3339193163Sdfr * addition, only a single top-level vdev is allowed and none of the 3340193163Sdfr * leaves can be wholedisks. 3341193163Sdfr * 3342193163Sdfr * For FreeBSD, we can boot from any configuration. There is a 3343193163Sdfr * limitation that the boot filesystem must be either uncompressed or 3344193163Sdfr * compresses with lzjb compression but I'm not sure how to enforce 3345193163Sdfr * that here. 3346185029Spjd */ 3347185029Spjdboolean_t 3348185029Spjdvdev_is_bootable(vdev_t *vd) 3349185029Spjd{ 3350213197Smm#ifdef sun 3351185029Spjd if (!vd->vdev_ops->vdev_op_leaf) { 3352185029Spjd char *vdev_type = vd->vdev_ops->vdev_op_type; 3353185029Spjd 3354185029Spjd if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 3355185029Spjd vd->vdev_children > 1) { 3356185029Spjd return (B_FALSE); 3357185029Spjd } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 3358185029Spjd strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 3359185029Spjd return (B_FALSE); 3360185029Spjd } 3361185029Spjd } else if (vd->vdev_wholedisk == 1) { 3362185029Spjd return (B_FALSE); 3363185029Spjd } 3364185029Spjd 3365219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 3366185029Spjd if (!vdev_is_bootable(vd->vdev_child[c])) 3367185029Spjd return (B_FALSE); 3368185029Spjd } 3369213197Smm#endif /* sun */ 3370185029Spjd return (B_TRUE); 3371168404Spjd} 3372213197Smm 3373219089Spjd/* 3374219089Spjd * Load the state from the original vdev tree (ovd) which 3375219089Spjd * we've retrieved from the MOS config object. If the original 3376219089Spjd * vdev was offline or faulted then we transfer that state to the 3377219089Spjd * device in the current vdev tree (nvd). 3378219089Spjd */ 3379213197Smmvoid 3380219089Spjdvdev_load_log_state(vdev_t *nvd, vdev_t *ovd) 3381213197Smm{ 3382219089Spjd spa_t *spa = nvd->vdev_spa; 3383213197Smm 3384219089Spjd ASSERT(nvd->vdev_top->vdev_islog); 3385219089Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3386219089Spjd ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid); 3387213197Smm 3388219089Spjd for (int c = 0; c < nvd->vdev_children; c++) 3389219089Spjd vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]); 3390213197Smm 3391219089Spjd if (nvd->vdev_ops->vdev_op_leaf) { 3392213197Smm /* 3393219089Spjd * Restore the persistent vdev state 3394213197Smm */ 3395219089Spjd nvd->vdev_offline = ovd->vdev_offline; 3396219089Spjd nvd->vdev_faulted = ovd->vdev_faulted; 3397219089Spjd nvd->vdev_degraded = ovd->vdev_degraded; 3398219089Spjd nvd->vdev_removed = ovd->vdev_removed; 3399213197Smm } 3400213197Smm} 3401219089Spjd 3402219089Spjd/* 3403219089Spjd * Determine if a log device has valid content. If the vdev was 3404219089Spjd * removed or faulted in the MOS config then we know that 3405219089Spjd * the content on the log device has already been written to the pool. 3406219089Spjd */ 3407219089Spjdboolean_t 3408219089Spjdvdev_log_state_valid(vdev_t *vd) 3409219089Spjd{ 3410219089Spjd if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 3411219089Spjd !vd->vdev_removed) 3412219089Spjd return (B_TRUE); 3413219089Spjd 3414219089Spjd for (int c = 0; c < vd->vdev_children; c++) 3415219089Spjd if (vdev_log_state_valid(vd->vdev_child[c])) 3416219089Spjd return (B_TRUE); 3417219089Spjd 3418219089Spjd return (B_FALSE); 3419219089Spjd} 3420219089Spjd 3421219089Spjd/* 3422219089Spjd * Expand a vdev if possible. 3423219089Spjd */ 3424219089Spjdvoid 3425219089Spjdvdev_expand(vdev_t *vd, uint64_t txg) 3426219089Spjd{ 3427219089Spjd ASSERT(vd->vdev_top == vd); 3428219089Spjd ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3429219089Spjd 3430219089Spjd if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 3431219089Spjd VERIFY(vdev_metaslab_init(vd, txg) == 0); 3432219089Spjd vdev_config_dirty(vd); 3433219089Spjd } 3434219089Spjd} 3435219089Spjd 3436219089Spjd/* 3437219089Spjd * Split a vdev. 3438219089Spjd */ 3439219089Spjdvoid 3440219089Spjdvdev_split(vdev_t *vd) 3441219089Spjd{ 3442219089Spjd vdev_t *cvd, *pvd = vd->vdev_parent; 3443219089Spjd 3444219089Spjd vdev_remove_child(pvd, vd); 3445219089Spjd vdev_compact_children(pvd); 3446219089Spjd 3447219089Spjd cvd = pvd->vdev_child[0]; 3448219089Spjd if (pvd->vdev_children == 1) { 3449219089Spjd vdev_remove_parent(cvd); 3450219089Spjd cvd->vdev_splitting = B_TRUE; 3451219089Spjd } 3452219089Spjd vdev_propagate_state(cvd); 3453219089Spjd} 3454247265Smm 3455247265Smmvoid 3456247265Smmvdev_deadman(vdev_t *vd) 3457247265Smm{ 3458247265Smm for (int c = 0; c < vd->vdev_children; c++) { 3459247265Smm vdev_t *cvd = vd->vdev_child[c]; 3460247265Smm 3461247265Smm vdev_deadman(cvd); 3462247265Smm } 3463247265Smm 3464247265Smm if (vd->vdev_ops->vdev_op_leaf) { 3465247265Smm vdev_queue_t *vq = &vd->vdev_queue; 3466247265Smm 3467247265Smm mutex_enter(&vq->vq_lock); 3468260763Savg if (avl_numnodes(&vq->vq_active_tree) > 0) { 3469247265Smm spa_t *spa = vd->vdev_spa; 3470247265Smm zio_t *fio; 3471247265Smm uint64_t delta; 3472247265Smm 3473247265Smm /* 3474247265Smm * Look at the head of all the pending queues, 3475247265Smm * if any I/O has been outstanding for longer than 3476247265Smm * the spa_deadman_synctime we panic the system. 3477247265Smm */ 3478260763Savg fio = avl_first(&vq->vq_active_tree); 3479249206Smm delta = gethrtime() - fio->io_timestamp; 3480249206Smm if (delta > spa_deadman_synctime(spa)) { 3481249206Smm zfs_dbgmsg("SLOW IO: zio timestamp %lluns, " 3482249206Smm "delta %lluns, last io %lluns", 3483247265Smm fio->io_timestamp, delta, 3484247265Smm vq->vq_io_complete_ts); 3485247265Smm fm_panic("I/O to pool '%s' appears to be " 3486247348Smm "hung on vdev guid %llu at '%s'.", 3487247348Smm spa_name(spa), 3488247348Smm (long long unsigned int) vd->vdev_guid, 3489247348Smm vd->vdev_path); 3490247265Smm } 3491247265Smm } 3492247265Smm mutex_exit(&vq->vq_lock); 3493247265Smm } 3494247265Smm} 3495