1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21ad135b5dSChristopher Siden 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24*86714001SSerapheim Dimitropoulos * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens /* 28fa9e4066Sahrens * Virtual Device Labels 29fa9e4066Sahrens * --------------------- 30fa9e4066Sahrens * 31fa9e4066Sahrens * The vdev label serves several distinct purposes: 32fa9e4066Sahrens * 33fa9e4066Sahrens * 1. Uniquely identify this device as part of a ZFS pool and confirm its 34fa9e4066Sahrens * identity within the pool. 35fa9e4066Sahrens * 36fa9e4066Sahrens * 2. Verify that all the devices given in a configuration are present 37fa9e4066Sahrens * within the pool. 38fa9e4066Sahrens * 39fa9e4066Sahrens * 3. Determine the uberblock for the pool. 40fa9e4066Sahrens * 41fa9e4066Sahrens * 4. In case of an import operation, determine the configuration of the 42fa9e4066Sahrens * toplevel vdev of which it is a part. 43fa9e4066Sahrens * 44fa9e4066Sahrens * 5. If an import operation cannot find all the devices in the pool, 45fa9e4066Sahrens * provide enough information to the administrator to determine which 46fa9e4066Sahrens * devices are missing. 47fa9e4066Sahrens * 48fa9e4066Sahrens * It is important to note that while the kernel is responsible for writing the 49fa9e4066Sahrens * label, it only consumes the information in the first three cases. The 50fa9e4066Sahrens * latter information is only consumed in userland when determining the 51fa9e4066Sahrens * configuration to import a pool. 52fa9e4066Sahrens * 53fa9e4066Sahrens * 54fa9e4066Sahrens * Label Organization 55fa9e4066Sahrens * ------------------ 56fa9e4066Sahrens * 57fa9e4066Sahrens * Before describing the contents of the label, it's important to understand how 58fa9e4066Sahrens * the labels are written and updated with respect to the uberblock. 59fa9e4066Sahrens * 60fa9e4066Sahrens * When the pool configuration is altered, either because it was newly created 61fa9e4066Sahrens * or a device was added, we want to update all the labels such that we can deal 62fa9e4066Sahrens * with fatal failure at any point. To this end, each disk has two labels which 63fa9e4066Sahrens * are updated before and after the uberblock is synced. Assuming we have 643d7072f8Seschrock * labels and an uberblock with the following transaction groups: 65fa9e4066Sahrens * 66fa9e4066Sahrens * L1 UB L2 67fa9e4066Sahrens * +------+ +------+ +------+ 68fa9e4066Sahrens * | | | | | | 69fa9e4066Sahrens * | t10 | | t10 | | t10 | 70fa9e4066Sahrens * | | | | | | 71fa9e4066Sahrens * +------+ +------+ +------+ 72fa9e4066Sahrens * 73fa9e4066Sahrens * In this stable state, the labels and the uberblock were all updated within 74fa9e4066Sahrens * the same transaction group (10). Each label is mirrored and checksummed, so 75fa9e4066Sahrens * that we can detect when we fail partway through writing the label. 76fa9e4066Sahrens * 77fa9e4066Sahrens * In order to identify which labels are valid, the labels are written in the 78fa9e4066Sahrens * following manner: 79fa9e4066Sahrens * 80fa9e4066Sahrens * 1. For each vdev, update 'L1' to the new label 81fa9e4066Sahrens * 2. Update the uberblock 82fa9e4066Sahrens * 3. For each vdev, update 'L2' to the new label 83fa9e4066Sahrens * 84fa9e4066Sahrens * Given arbitrary failure, we can determine the correct label to use based on 85fa9e4066Sahrens * the transaction group. If we fail after updating L1 but before updating the 86fa9e4066Sahrens * UB, we will notice that L1's transaction group is greater than the uberblock, 87fa9e4066Sahrens * so L2 must be valid. If we fail after writing the uberblock but before 88fa9e4066Sahrens * writing L2, we will notice that L2's transaction group is less than L1, and 89fa9e4066Sahrens * therefore L1 is valid. 90fa9e4066Sahrens * 91fa9e4066Sahrens * Another added complexity is that not every label is updated when the config 92fa9e4066Sahrens * is synced. If we add a single device, we do not want to have to re-write 93fa9e4066Sahrens * every label for every device in the pool. This means that both L1 and L2 may 94fa9e4066Sahrens * be older than the pool uberblock, because the necessary information is stored 95fa9e4066Sahrens * on another vdev. 96fa9e4066Sahrens * 97fa9e4066Sahrens * 98fa9e4066Sahrens * On-disk Format 99fa9e4066Sahrens * -------------- 100fa9e4066Sahrens * 101fa9e4066Sahrens * The vdev label consists of two distinct parts, and is wrapped within the 102fa9e4066Sahrens * vdev_label_t structure. The label includes 8k of padding to permit legacy 103fa9e4066Sahrens * VTOC disk labels, but is otherwise ignored. 104fa9e4066Sahrens * 105fa9e4066Sahrens * The first half of the label is a packed nvlist which contains pool wide 106fa9e4066Sahrens * properties, per-vdev properties, and configuration information. It is 107fa9e4066Sahrens * described in more detail below. 108fa9e4066Sahrens * 109fa9e4066Sahrens * The latter half of the label consists of a redundant array of uberblocks. 110fa9e4066Sahrens * These uberblocks are updated whenever a transaction group is committed, 111fa9e4066Sahrens * or when the configuration is updated. When a pool is loaded, we scan each 112fa9e4066Sahrens * vdev for the 'best' uberblock. 113fa9e4066Sahrens * 114fa9e4066Sahrens * 115fa9e4066Sahrens * Configuration Information 116fa9e4066Sahrens * ------------------------- 117fa9e4066Sahrens * 118fa9e4066Sahrens * The nvlist describing the pool and vdev contains the following elements: 119fa9e4066Sahrens * 120fa9e4066Sahrens * version ZFS on-disk version 121fa9e4066Sahrens * name Pool name 122fa9e4066Sahrens * state Pool state 123fa9e4066Sahrens * txg Transaction group in which this label was written 124fa9e4066Sahrens * pool_guid Unique identifier for this pool 125fa9e4066Sahrens * vdev_tree An nvlist describing vdev tree. 126ad135b5dSChristopher Siden * features_for_read 127ad135b5dSChristopher Siden * An nvlist of the features necessary for reading the MOS. 128fa9e4066Sahrens * 129fa9e4066Sahrens * Each leaf device label also contains the following: 130fa9e4066Sahrens * 131fa9e4066Sahrens * top_guid Unique ID for top-level vdev in which this is contained 132fa9e4066Sahrens * guid Unique ID for the leaf vdev 133fa9e4066Sahrens * 134fa9e4066Sahrens * The 'vs' configuration follows the format described in 'spa_config.c'. 135fa9e4066Sahrens */ 136fa9e4066Sahrens 137fa9e4066Sahrens #include <sys/zfs_context.h> 138fa9e4066Sahrens #include <sys/spa.h> 139fa9e4066Sahrens #include <sys/spa_impl.h> 140fa9e4066Sahrens #include <sys/dmu.h> 141fa9e4066Sahrens #include <sys/zap.h> 142fa9e4066Sahrens #include <sys/vdev.h> 143fa9e4066Sahrens #include <sys/vdev_impl.h> 144fa9e4066Sahrens #include <sys/uberblock_impl.h> 145fa9e4066Sahrens #include <sys/metaslab.h> 1465cabbc6bSPrashanth Sreenivasa #include <sys/metaslab_impl.h> 147fa9e4066Sahrens #include <sys/zio.h> 1483f9d6ad7SLin Ling #include <sys/dsl_scan.h> 149770499e1SDan Kimmel #include <sys/abd.h> 150fa9e4066Sahrens #include <sys/fs/zfs.h> 151fa9e4066Sahrens 152fa9e4066Sahrens /* 153fa9e4066Sahrens * Basic routines to read and write from a vdev label. 154fa9e4066Sahrens * Used throughout the rest of this file. 155fa9e4066Sahrens */ 156fa9e4066Sahrens uint64_t 157fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset) 158fa9e4066Sahrens { 159ecc2d604Sbonwick ASSERT(offset < sizeof (vdev_label_t)); 160e7437265Sahrens ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0); 161ecc2d604Sbonwick 162fa9e4066Sahrens return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 163fa9e4066Sahrens 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 164fa9e4066Sahrens } 165fa9e4066Sahrens 16621bf64a7Sgw /* 16721bf64a7Sgw * Returns back the vdev label associated with the passed in offset. 16821bf64a7Sgw */ 16921bf64a7Sgw int 17021bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset) 17121bf64a7Sgw { 17221bf64a7Sgw int l; 17321bf64a7Sgw 17421bf64a7Sgw if (offset >= psize - VDEV_LABEL_END_SIZE) { 17521bf64a7Sgw offset -= psize - VDEV_LABEL_END_SIZE; 17621bf64a7Sgw offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t); 17721bf64a7Sgw } 17821bf64a7Sgw l = offset / sizeof (vdev_label_t); 17921bf64a7Sgw return (l < VDEV_LABELS ? l : -1); 18021bf64a7Sgw } 18121bf64a7Sgw 182fa9e4066Sahrens static void 183770499e1SDan Kimmel vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset, 1849a686fbcSPaul Dagnelie uint64_t size, zio_done_func_t *done, void *private, int flags) 185fa9e4066Sahrens { 186e14bb325SJeff Bonwick ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) == 187e14bb325SJeff Bonwick SCL_STATE_ALL); 188e14bb325SJeff Bonwick ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 189fa9e4066Sahrens 190fa9e4066Sahrens zio_nowait(zio_read_phys(zio, vd, 191fa9e4066Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 192fa9e4066Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 193e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, flags, B_TRUE)); 194fa9e4066Sahrens } 195fa9e4066Sahrens 196fa9e4066Sahrens static void 197770499e1SDan Kimmel vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset, 1989a686fbcSPaul Dagnelie uint64_t size, zio_done_func_t *done, void *private, int flags) 199fa9e4066Sahrens { 200e14bb325SJeff Bonwick ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL || 201e14bb325SJeff Bonwick (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) == 202e14bb325SJeff Bonwick (SCL_CONFIG | SCL_STATE) && 203e14bb325SJeff Bonwick dsl_pool_sync_context(spa_get_dsl(zio->io_spa)))); 204e14bb325SJeff Bonwick ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 205fa9e4066Sahrens 206fa9e4066Sahrens zio_nowait(zio_write_phys(zio, vd, 207fa9e4066Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 208fa9e4066Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 20917f17c2dSbonwick ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE)); 210fa9e4066Sahrens } 211fa9e4066Sahrens 212*86714001SSerapheim Dimitropoulos static void 213*86714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl) 214*86714001SSerapheim Dimitropoulos { 215*86714001SSerapheim Dimitropoulos spa_t *spa = vd->vdev_spa; 216*86714001SSerapheim Dimitropoulos 217*86714001SSerapheim Dimitropoulos if (vd != spa->spa_root_vdev) 218*86714001SSerapheim Dimitropoulos return; 219*86714001SSerapheim Dimitropoulos 220*86714001SSerapheim Dimitropoulos /* provide either current or previous scan information */ 221*86714001SSerapheim Dimitropoulos pool_scan_stat_t ps; 222*86714001SSerapheim Dimitropoulos if (spa_scan_get_stats(spa, &ps) == 0) { 223*86714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl, 224*86714001SSerapheim Dimitropoulos ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps, 225*86714001SSerapheim Dimitropoulos sizeof (pool_scan_stat_t) / sizeof (uint64_t)); 226*86714001SSerapheim Dimitropoulos } 227*86714001SSerapheim Dimitropoulos 228*86714001SSerapheim Dimitropoulos pool_removal_stat_t prs; 229*86714001SSerapheim Dimitropoulos if (spa_removal_get_stats(spa, &prs) == 0) { 230*86714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl, 231*86714001SSerapheim Dimitropoulos ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs, 232*86714001SSerapheim Dimitropoulos sizeof (prs) / sizeof (uint64_t)); 233*86714001SSerapheim Dimitropoulos } 234*86714001SSerapheim Dimitropoulos 235*86714001SSerapheim Dimitropoulos pool_checkpoint_stat_t pcs; 236*86714001SSerapheim Dimitropoulos if (spa_checkpoint_get_stats(spa, &pcs) == 0) { 237*86714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl, 238*86714001SSerapheim Dimitropoulos ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs, 239*86714001SSerapheim Dimitropoulos sizeof (pcs) / sizeof (uint64_t)); 240*86714001SSerapheim Dimitropoulos } 241*86714001SSerapheim Dimitropoulos } 242*86714001SSerapheim Dimitropoulos 243fa9e4066Sahrens /* 244fa9e4066Sahrens * Generate the nvlist representing this vdev's config. 245fa9e4066Sahrens */ 246fa9e4066Sahrens nvlist_t * 24799653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 2483f9d6ad7SLin Ling vdev_config_flag_t flags) 249fa9e4066Sahrens { 250fa9e4066Sahrens nvlist_t *nv = NULL; 2515cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 252fa9e4066Sahrens 253b4952e17SGeorge Wilson nv = fnvlist_alloc(); 254fa9e4066Sahrens 255b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type); 2563f9d6ad7SLin Ling if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE))) 257b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id); 258b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid); 259fa9e4066Sahrens 260fa9e4066Sahrens if (vd->vdev_path != NULL) 261b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path); 262fa9e4066Sahrens 263fa9e4066Sahrens if (vd->vdev_devid != NULL) 264b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid); 265fa9e4066Sahrens 2663d7072f8Seschrock if (vd->vdev_physpath != NULL) 267b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 268b4952e17SGeorge Wilson vd->vdev_physpath); 2693d7072f8Seschrock 2706809eb4eSEric Schrock if (vd->vdev_fru != NULL) 271b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru); 2726809eb4eSEric Schrock 27399653d4eSeschrock if (vd->vdev_nparity != 0) { 27499653d4eSeschrock ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 27599653d4eSeschrock VDEV_TYPE_RAIDZ) == 0); 27699653d4eSeschrock 27799653d4eSeschrock /* 27899653d4eSeschrock * Make sure someone hasn't managed to sneak a fancy new vdev 27999653d4eSeschrock * into a crufty old storage pool. 28099653d4eSeschrock */ 28199653d4eSeschrock ASSERT(vd->vdev_nparity == 1 || 282f94275ceSAdam Leventhal (vd->vdev_nparity <= 2 && 283f94275ceSAdam Leventhal spa_version(spa) >= SPA_VERSION_RAIDZ2) || 284f94275ceSAdam Leventhal (vd->vdev_nparity <= 3 && 285f94275ceSAdam Leventhal spa_version(spa) >= SPA_VERSION_RAIDZ3)); 28699653d4eSeschrock 28799653d4eSeschrock /* 28899653d4eSeschrock * Note that we'll add the nparity tag even on storage pools 28999653d4eSeschrock * that only support a single parity device -- older software 29099653d4eSeschrock * will just ignore it. 29199653d4eSeschrock */ 292b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity); 29399653d4eSeschrock } 29499653d4eSeschrock 295afefbcddSeschrock if (vd->vdev_wholedisk != -1ULL) 296b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 297b4952e17SGeorge Wilson vd->vdev_wholedisk); 298afefbcddSeschrock 2996f793812SPavel Zakharov if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING)) 300b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1); 301ea8dc4b6Seschrock 30299653d4eSeschrock if (vd->vdev_isspare) 303b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1); 30499653d4eSeschrock 3053f9d6ad7SLin Ling if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) && 3063f9d6ad7SLin Ling vd == vd->vdev_top) { 307b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 308b4952e17SGeorge Wilson vd->vdev_ms_array); 309b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 310b4952e17SGeorge Wilson vd->vdev_ms_shift); 311b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift); 312b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 313b4952e17SGeorge Wilson vd->vdev_asize); 314b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog); 3155cabbc6bSPrashanth Sreenivasa if (vd->vdev_removing) { 316b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING, 317b4952e17SGeorge Wilson vd->vdev_removing); 3185cabbc6bSPrashanth Sreenivasa } 319fa9e4066Sahrens } 320fa9e4066Sahrens 3210713e232SGeorge Wilson if (vd->vdev_dtl_sm != NULL) { 322b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 3230713e232SGeorge Wilson space_map_object(vd->vdev_dtl_sm)); 3240713e232SGeorge Wilson } 325fa9e4066Sahrens 3265cabbc6bSPrashanth Sreenivasa if (vic->vic_mapping_object != 0) { 3275cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 3285cabbc6bSPrashanth Sreenivasa vic->vic_mapping_object); 3295cabbc6bSPrashanth Sreenivasa } 3305cabbc6bSPrashanth Sreenivasa 3315cabbc6bSPrashanth Sreenivasa if (vic->vic_births_object != 0) { 3325cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 3335cabbc6bSPrashanth Sreenivasa vic->vic_births_object); 3345cabbc6bSPrashanth Sreenivasa } 3355cabbc6bSPrashanth Sreenivasa 3365cabbc6bSPrashanth Sreenivasa if (vic->vic_prev_indirect_vdev != UINT64_MAX) { 3375cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 3385cabbc6bSPrashanth Sreenivasa vic->vic_prev_indirect_vdev); 3395cabbc6bSPrashanth Sreenivasa } 3405cabbc6bSPrashanth Sreenivasa 34188ecc943SGeorge Wilson if (vd->vdev_crtxg) 342b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg); 34388ecc943SGeorge Wilson 344215198a6SJoe Stein if (flags & VDEV_CONFIG_MOS) { 345215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 346215198a6SJoe Stein ASSERT(vd->vdev_ops->vdev_op_leaf); 347215198a6SJoe Stein fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP, 348215198a6SJoe Stein vd->vdev_leaf_zap); 349215198a6SJoe Stein } 350215198a6SJoe Stein 351215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 352215198a6SJoe Stein ASSERT(vd == vd->vdev_top); 353215198a6SJoe Stein fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 354215198a6SJoe Stein vd->vdev_top_zap); 355215198a6SJoe Stein } 356215198a6SJoe Stein } 357215198a6SJoe Stein 358fa9e4066Sahrens if (getstats) { 359fa9e4066Sahrens vdev_stat_t vs; 3603f9d6ad7SLin Ling 361fa9e4066Sahrens vdev_get_stats(vd, &vs); 362b4952e17SGeorge Wilson fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 363b4952e17SGeorge Wilson (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)); 3643f9d6ad7SLin Ling 365*86714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vd, nv); 3665cabbc6bSPrashanth Sreenivasa 3675cabbc6bSPrashanth Sreenivasa /* 3685cabbc6bSPrashanth Sreenivasa * Note: this can be called from open context 3695cabbc6bSPrashanth Sreenivasa * (spa_get_stats()), so we need the rwlock to prevent 3705cabbc6bSPrashanth Sreenivasa * the mapping from being changed by condensing. 3715cabbc6bSPrashanth Sreenivasa */ 3725cabbc6bSPrashanth Sreenivasa rw_enter(&vd->vdev_indirect_rwlock, RW_READER); 3735cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_mapping != NULL) { 3745cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_indirect_births != NULL); 3755cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_t *vim = 3765cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping; 3775cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE, 3785cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_size(vim)); 3795cabbc6bSPrashanth Sreenivasa } 3805cabbc6bSPrashanth Sreenivasa rw_exit(&vd->vdev_indirect_rwlock); 3815cabbc6bSPrashanth Sreenivasa if (vd->vdev_mg != NULL && 3825cabbc6bSPrashanth Sreenivasa vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) { 3835cabbc6bSPrashanth Sreenivasa /* 3845cabbc6bSPrashanth Sreenivasa * Compute approximately how much memory would be used 3855cabbc6bSPrashanth Sreenivasa * for the indirect mapping if this device were to 3865cabbc6bSPrashanth Sreenivasa * be removed. 3875cabbc6bSPrashanth Sreenivasa * 3885cabbc6bSPrashanth Sreenivasa * Note: If the frag metric is invalid, then not 3895cabbc6bSPrashanth Sreenivasa * enough metaslabs have been converted to have 3905cabbc6bSPrashanth Sreenivasa * histograms. 3915cabbc6bSPrashanth Sreenivasa */ 3925cabbc6bSPrashanth Sreenivasa uint64_t seg_count = 0; 3935cabbc6bSPrashanth Sreenivasa 3945cabbc6bSPrashanth Sreenivasa /* 3955cabbc6bSPrashanth Sreenivasa * There are the same number of allocated segments 3965cabbc6bSPrashanth Sreenivasa * as free segments, so we will have at least one 3975cabbc6bSPrashanth Sreenivasa * entry per free segment. 3985cabbc6bSPrashanth Sreenivasa */ 3995cabbc6bSPrashanth Sreenivasa for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) { 4005cabbc6bSPrashanth Sreenivasa seg_count += vd->vdev_mg->mg_histogram[i]; 4015cabbc6bSPrashanth Sreenivasa } 4025cabbc6bSPrashanth Sreenivasa 4035cabbc6bSPrashanth Sreenivasa /* 4045cabbc6bSPrashanth Sreenivasa * The maximum length of a mapping is SPA_MAXBLOCKSIZE, 4055cabbc6bSPrashanth Sreenivasa * so we need at least one entry per SPA_MAXBLOCKSIZE 4065cabbc6bSPrashanth Sreenivasa * of allocated data. 4075cabbc6bSPrashanth Sreenivasa */ 4085cabbc6bSPrashanth Sreenivasa seg_count += vd->vdev_stat.vs_alloc / SPA_MAXBLOCKSIZE; 4095cabbc6bSPrashanth Sreenivasa 4105cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE, 4115cabbc6bSPrashanth Sreenivasa seg_count * 4125cabbc6bSPrashanth Sreenivasa sizeof (vdev_indirect_mapping_entry_phys_t)); 4135cabbc6bSPrashanth Sreenivasa } 414fa9e4066Sahrens } 415fa9e4066Sahrens 416fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 417fa9e4066Sahrens nvlist_t **child; 4183f9d6ad7SLin Ling int c, idx; 419fa9e4066Sahrens 42088ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 42188ecc943SGeorge Wilson 422fa9e4066Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 423fa9e4066Sahrens KM_SLEEP); 424fa9e4066Sahrens 4253f9d6ad7SLin Ling for (c = 0, idx = 0; c < vd->vdev_children; c++) { 4263f9d6ad7SLin Ling vdev_t *cvd = vd->vdev_child[c]; 4273f9d6ad7SLin Ling 4283f9d6ad7SLin Ling /* 4293f9d6ad7SLin Ling * If we're generating an nvlist of removing 4303f9d6ad7SLin Ling * vdevs then skip over any device which is 4313f9d6ad7SLin Ling * not being removed. 4323f9d6ad7SLin Ling */ 4333f9d6ad7SLin Ling if ((flags & VDEV_CONFIG_REMOVING) && 4343f9d6ad7SLin Ling !cvd->vdev_removing) 4353f9d6ad7SLin Ling continue; 436fa9e4066Sahrens 4373f9d6ad7SLin Ling child[idx++] = vdev_config_generate(spa, cvd, 4383f9d6ad7SLin Ling getstats, flags); 4393f9d6ad7SLin Ling } 4403f9d6ad7SLin Ling 4413f9d6ad7SLin Ling if (idx) { 442b4952e17SGeorge Wilson fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 443b4952e17SGeorge Wilson child, idx); 4443f9d6ad7SLin Ling } 445fa9e4066Sahrens 4463f9d6ad7SLin Ling for (c = 0; c < idx; c++) 447fa9e4066Sahrens nvlist_free(child[c]); 448fa9e4066Sahrens 449fa9e4066Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 450441d80aaSlling 451441d80aaSlling } else { 452069f55e2SEric Schrock const char *aux = NULL; 453069f55e2SEric Schrock 454ecc2d604Sbonwick if (vd->vdev_offline && !vd->vdev_tmpoffline) 455b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE); 456b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0) 457b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 458b4952e17SGeorge Wilson vd->vdev_resilver_txg); 4593d7072f8Seschrock if (vd->vdev_faulted) 460b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE); 4613d7072f8Seschrock if (vd->vdev_degraded) 462b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE); 4633d7072f8Seschrock if (vd->vdev_removed) 464b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE); 4653d7072f8Seschrock if (vd->vdev_unspare) 466b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE); 46788ecc943SGeorge Wilson if (vd->vdev_ishole) 468b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE); 469069f55e2SEric Schrock 470069f55e2SEric Schrock switch (vd->vdev_stat.vs_aux) { 471069f55e2SEric Schrock case VDEV_AUX_ERR_EXCEEDED: 472069f55e2SEric Schrock aux = "err_exceeded"; 473069f55e2SEric Schrock break; 474069f55e2SEric Schrock 475069f55e2SEric Schrock case VDEV_AUX_EXTERNAL: 476069f55e2SEric Schrock aux = "external"; 477069f55e2SEric Schrock break; 478069f55e2SEric Schrock } 479069f55e2SEric Schrock 480069f55e2SEric Schrock if (aux != NULL) 481b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux); 4821195e687SMark J Musante 4831195e687SMark J Musante if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) { 484b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID, 485b4952e17SGeorge Wilson vd->vdev_orig_guid); 4861195e687SMark J Musante } 487fa9e4066Sahrens } 488fa9e4066Sahrens 489fa9e4066Sahrens return (nv); 490fa9e4066Sahrens } 491fa9e4066Sahrens 49288ecc943SGeorge Wilson /* 49388ecc943SGeorge Wilson * Generate a view of the top-level vdevs. If we currently have holes 49488ecc943SGeorge Wilson * in the namespace, then generate an array which contains a list of holey 49588ecc943SGeorge Wilson * vdevs. Additionally, add the number of top-level children that currently 49688ecc943SGeorge Wilson * exist. 49788ecc943SGeorge Wilson */ 49888ecc943SGeorge Wilson void 49988ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config) 50088ecc943SGeorge Wilson { 50188ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 50288ecc943SGeorge Wilson uint64_t *array; 5033f9d6ad7SLin Ling uint_t c, idx; 50488ecc943SGeorge Wilson 50588ecc943SGeorge Wilson array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP); 50688ecc943SGeorge Wilson 5073f9d6ad7SLin Ling for (c = 0, idx = 0; c < rvd->vdev_children; c++) { 50888ecc943SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 50988ecc943SGeorge Wilson 5105cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ishole) { 51188ecc943SGeorge Wilson array[idx++] = c; 5125cabbc6bSPrashanth Sreenivasa } 51388ecc943SGeorge Wilson } 51488ecc943SGeorge Wilson 515312c6e15SGeorge Wilson if (idx) { 516312c6e15SGeorge Wilson VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY, 517312c6e15SGeorge Wilson array, idx) == 0); 518312c6e15SGeorge Wilson } 519312c6e15SGeorge Wilson 52088ecc943SGeorge Wilson VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 52188ecc943SGeorge Wilson rvd->vdev_children) == 0); 52288ecc943SGeorge Wilson 52388ecc943SGeorge Wilson kmem_free(array, rvd->vdev_children * sizeof (uint64_t)); 52488ecc943SGeorge Wilson } 52588ecc943SGeorge Wilson 526ad135b5dSChristopher Siden /* 527dfbb9432SGeorge Wilson * Returns the configuration from the label of the given vdev. For vdevs 528dfbb9432SGeorge Wilson * which don't have a txg value stored on their label (i.e. spares/cache) 529dfbb9432SGeorge Wilson * or have not been completely initialized (txg = 0) just return 530dfbb9432SGeorge Wilson * the configuration from the first valid label we find. Otherwise, 531dfbb9432SGeorge Wilson * find the most up-to-date label that does not exceed the specified 532dfbb9432SGeorge Wilson * 'txg' value. 533ad135b5dSChristopher Siden */ 534fa9e4066Sahrens nvlist_t * 535dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg) 536fa9e4066Sahrens { 5370373e76bSbonwick spa_t *spa = vd->vdev_spa; 538fa9e4066Sahrens nvlist_t *config = NULL; 539fa9e4066Sahrens vdev_phys_t *vp; 540770499e1SDan Kimmel abd_t *vp_abd; 541fa9e4066Sahrens zio_t *zio; 542dfbb9432SGeorge Wilson uint64_t best_txg = 0; 543dfbb9432SGeorge Wilson int error = 0; 5448956713aSEric Schrock int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 5458956713aSEric Schrock ZIO_FLAG_SPECULATIVE; 546fa9e4066Sahrens 547e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 5480373e76bSbonwick 5490a4e9518Sgw if (!vdev_readable(vd)) 550fa9e4066Sahrens return (NULL); 551fa9e4066Sahrens 552770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE); 553770499e1SDan Kimmel vp = abd_to_buf(vp_abd); 554fa9e4066Sahrens 5558956713aSEric Schrock retry: 556e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) { 557dfbb9432SGeorge Wilson nvlist_t *label = NULL; 558fa9e4066Sahrens 559e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, flags); 560fa9e4066Sahrens 561770499e1SDan Kimmel vdev_label_read(zio, vd, l, vp_abd, 562fa9e4066Sahrens offsetof(vdev_label_t, vl_vdev_phys), 563e14bb325SJeff Bonwick sizeof (vdev_phys_t), NULL, NULL, flags); 564fa9e4066Sahrens 565fa9e4066Sahrens if (zio_wait(zio) == 0 && 566fa9e4066Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 567dfbb9432SGeorge Wilson &label, 0) == 0) { 568dfbb9432SGeorge Wilson uint64_t label_txg = 0; 569dfbb9432SGeorge Wilson 570dfbb9432SGeorge Wilson /* 571dfbb9432SGeorge Wilson * Auxiliary vdevs won't have txg values in their 572dfbb9432SGeorge Wilson * labels and newly added vdevs may not have been 573dfbb9432SGeorge Wilson * completely initialized so just return the 574dfbb9432SGeorge Wilson * configuration from the first valid label we 575dfbb9432SGeorge Wilson * encounter. 576dfbb9432SGeorge Wilson */ 577dfbb9432SGeorge Wilson error = nvlist_lookup_uint64(label, 578dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_TXG, &label_txg); 579dfbb9432SGeorge Wilson if ((error || label_txg == 0) && !config) { 580dfbb9432SGeorge Wilson config = label; 581dfbb9432SGeorge Wilson break; 582dfbb9432SGeorge Wilson } else if (label_txg <= txg && label_txg > best_txg) { 583dfbb9432SGeorge Wilson best_txg = label_txg; 584dfbb9432SGeorge Wilson nvlist_free(config); 585dfbb9432SGeorge Wilson config = fnvlist_dup(label); 586dfbb9432SGeorge Wilson } 587dfbb9432SGeorge Wilson } 588fa9e4066Sahrens 589dfbb9432SGeorge Wilson if (label != NULL) { 590dfbb9432SGeorge Wilson nvlist_free(label); 591dfbb9432SGeorge Wilson label = NULL; 592fa9e4066Sahrens } 593fa9e4066Sahrens } 594fa9e4066Sahrens 5958956713aSEric Schrock if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) { 5968956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD; 5978956713aSEric Schrock goto retry; 5988956713aSEric Schrock } 5998956713aSEric Schrock 600770499e1SDan Kimmel abd_free(vp_abd); 601fa9e4066Sahrens 602fa9e4066Sahrens return (config); 603fa9e4066Sahrens } 604fa9e4066Sahrens 60539c23413Seschrock /* 60639c23413Seschrock * Determine if a device is in use. The 'spare_guid' parameter will be filled 60739c23413Seschrock * in with the device guid if this spare is active elsewhere on the system. 60839c23413Seschrock */ 60939c23413Seschrock static boolean_t 61039c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 611fa94a07fSbrendan uint64_t *spare_guid, uint64_t *l2cache_guid) 61239c23413Seschrock { 61339c23413Seschrock spa_t *spa = vd->vdev_spa; 61439c23413Seschrock uint64_t state, pool_guid, device_guid, txg, spare_pool; 61539c23413Seschrock uint64_t vdtxg = 0; 61639c23413Seschrock nvlist_t *label; 61739c23413Seschrock 61839c23413Seschrock if (spare_guid) 61939c23413Seschrock *spare_guid = 0ULL; 620fa94a07fSbrendan if (l2cache_guid) 621fa94a07fSbrendan *l2cache_guid = 0ULL; 62239c23413Seschrock 62339c23413Seschrock /* 62439c23413Seschrock * Read the label, if any, and perform some basic sanity checks. 62539c23413Seschrock */ 626dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) 62739c23413Seschrock return (B_FALSE); 62839c23413Seschrock 62939c23413Seschrock (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 63039c23413Seschrock &vdtxg); 63139c23413Seschrock 63239c23413Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 63339c23413Seschrock &state) != 0 || 63439c23413Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 63539c23413Seschrock &device_guid) != 0) { 63639c23413Seschrock nvlist_free(label); 63739c23413Seschrock return (B_FALSE); 63839c23413Seschrock } 63939c23413Seschrock 640fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 64139c23413Seschrock (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 64239c23413Seschrock &pool_guid) != 0 || 64339c23413Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 64439c23413Seschrock &txg) != 0)) { 64539c23413Seschrock nvlist_free(label); 64639c23413Seschrock return (B_FALSE); 64739c23413Seschrock } 64839c23413Seschrock 64939c23413Seschrock nvlist_free(label); 65039c23413Seschrock 65139c23413Seschrock /* 65239c23413Seschrock * Check to see if this device indeed belongs to the pool it claims to 65339c23413Seschrock * be a part of. The only way this is allowed is if the device is a hot 65439c23413Seschrock * spare (which we check for later on). 65539c23413Seschrock */ 656fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 65739c23413Seschrock !spa_guid_exists(pool_guid, device_guid) && 65889a89ebfSlling !spa_spare_exists(device_guid, NULL, NULL) && 659fa94a07fSbrendan !spa_l2cache_exists(device_guid, NULL)) 66039c23413Seschrock return (B_FALSE); 66139c23413Seschrock 66239c23413Seschrock /* 66339c23413Seschrock * If the transaction group is zero, then this an initialized (but 66439c23413Seschrock * unused) label. This is only an error if the create transaction 66539c23413Seschrock * on-disk is the same as the one we're using now, in which case the 66639c23413Seschrock * user has attempted to add the same vdev multiple times in the same 66739c23413Seschrock * transaction. 66839c23413Seschrock */ 669fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 670fa94a07fSbrendan txg == 0 && vdtxg == crtxg) 67139c23413Seschrock return (B_TRUE); 67239c23413Seschrock 67339c23413Seschrock /* 67439c23413Seschrock * Check to see if this is a spare device. We do an explicit check for 67539c23413Seschrock * spa_has_spare() here because it may be on our pending list of spares 676fa94a07fSbrendan * to add. We also check if it is an l2cache device. 67739c23413Seschrock */ 67889a89ebfSlling if (spa_spare_exists(device_guid, &spare_pool, NULL) || 67939c23413Seschrock spa_has_spare(spa, device_guid)) { 68039c23413Seschrock if (spare_guid) 68139c23413Seschrock *spare_guid = device_guid; 68239c23413Seschrock 68339c23413Seschrock switch (reason) { 68439c23413Seschrock case VDEV_LABEL_CREATE: 685fa94a07fSbrendan case VDEV_LABEL_L2CACHE: 68639c23413Seschrock return (B_TRUE); 68739c23413Seschrock 68839c23413Seschrock case VDEV_LABEL_REPLACE: 68939c23413Seschrock return (!spa_has_spare(spa, device_guid) || 69039c23413Seschrock spare_pool != 0ULL); 69139c23413Seschrock 69239c23413Seschrock case VDEV_LABEL_SPARE: 69339c23413Seschrock return (spa_has_spare(spa, device_guid)); 69439c23413Seschrock } 69539c23413Seschrock } 69639c23413Seschrock 697fa94a07fSbrendan /* 698fa94a07fSbrendan * Check to see if this is an l2cache device. 699fa94a07fSbrendan */ 700fa94a07fSbrendan if (spa_l2cache_exists(device_guid, NULL)) 701fa94a07fSbrendan return (B_TRUE); 702fa94a07fSbrendan 703f9af39baSGeorge Wilson /* 704f9af39baSGeorge Wilson * We can't rely on a pool's state if it's been imported 705f9af39baSGeorge Wilson * read-only. Instead we look to see if the pools is marked 706f9af39baSGeorge Wilson * read-only in the namespace and set the state to active. 707f9af39baSGeorge Wilson */ 7085bdd995dSRichard Yao if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 7095bdd995dSRichard Yao (spa = spa_by_guid(pool_guid, device_guid)) != NULL && 710f9af39baSGeorge Wilson spa_mode(spa) == FREAD) 711f9af39baSGeorge Wilson state = POOL_STATE_ACTIVE; 712f9af39baSGeorge Wilson 71339c23413Seschrock /* 71439c23413Seschrock * If the device is marked ACTIVE, then this device is in use by another 71539c23413Seschrock * pool on the system. 71639c23413Seschrock */ 71739c23413Seschrock return (state == POOL_STATE_ACTIVE); 71839c23413Seschrock } 71939c23413Seschrock 72039c23413Seschrock /* 72139c23413Seschrock * Initialize a vdev label. We check to make sure each leaf device is not in 72239c23413Seschrock * use, and writable. We put down an initial label which we will later 72339c23413Seschrock * overwrite with a complete label. Note that it's important to do this 72439c23413Seschrock * sequentially, not in parallel, so that we catch cases of multiple use of the 72539c23413Seschrock * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 72639c23413Seschrock * itself. 72739c23413Seschrock */ 72839c23413Seschrock int 72939c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 730fa9e4066Sahrens { 731fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 732fa9e4066Sahrens nvlist_t *label; 733fa9e4066Sahrens vdev_phys_t *vp; 734770499e1SDan Kimmel abd_t *vp_abd; 735770499e1SDan Kimmel abd_t *pad2; 736ecc2d604Sbonwick uberblock_t *ub; 737770499e1SDan Kimmel abd_t *ub_abd; 738fa9e4066Sahrens zio_t *zio; 739fa9e4066Sahrens char *buf; 740fa9e4066Sahrens size_t buflen; 741fa9e4066Sahrens int error; 742fa94a07fSbrendan uint64_t spare_guid, l2cache_guid; 743e14bb325SJeff Bonwick int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 744fa9e4066Sahrens 745e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 7460373e76bSbonwick 747e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 74839c23413Seschrock if ((error = vdev_label_init(vd->vdev_child[c], 74939c23413Seschrock crtxg, reason)) != 0) 750fa9e4066Sahrens return (error); 751fa9e4066Sahrens 75288ecc943SGeorge Wilson /* Track the creation time for this vdev */ 75388ecc943SGeorge Wilson vd->vdev_crtxg = crtxg; 75488ecc943SGeorge Wilson 755973c78e9SGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa)) 756fa9e4066Sahrens return (0); 757fa9e4066Sahrens 758fa9e4066Sahrens /* 75939c23413Seschrock * Dead vdevs cannot be initialized. 760fa9e4066Sahrens */ 761fa9e4066Sahrens if (vdev_is_dead(vd)) 762be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 763fa9e4066Sahrens 764fa9e4066Sahrens /* 76539c23413Seschrock * Determine if the vdev is in use. 766fa9e4066Sahrens */ 7671195e687SMark J Musante if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT && 768fa94a07fSbrendan vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 769be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 77039c23413Seschrock 77139c23413Seschrock /* 772fa94a07fSbrendan * If this is a request to add or replace a spare or l2cache device 773fa94a07fSbrendan * that is in use elsewhere on the system, then we must update the 774fa94a07fSbrendan * guid (which was initialized to a random value) to reflect the 775fa94a07fSbrendan * actual GUID (which is shared between multiple pools). 77639c23413Seschrock */ 777fa94a07fSbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 778fa94a07fSbrendan spare_guid != 0ULL) { 77931157203SJeff Bonwick uint64_t guid_delta = spare_guid - vd->vdev_guid; 78099653d4eSeschrock 78131157203SJeff Bonwick vd->vdev_guid += guid_delta; 78231157203SJeff Bonwick 78331157203SJeff Bonwick for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 78431157203SJeff Bonwick pvd->vdev_guid_sum += guid_delta; 78539c23413Seschrock 78699653d4eSeschrock /* 78739c23413Seschrock * If this is a replacement, then we want to fallthrough to the 78839c23413Seschrock * rest of the code. If we're adding a spare, then it's already 7893d7072f8Seschrock * labeled appropriately and we can just return. 79099653d4eSeschrock */ 79139c23413Seschrock if (reason == VDEV_LABEL_SPARE) 79239c23413Seschrock return (0); 7931195e687SMark J Musante ASSERT(reason == VDEV_LABEL_REPLACE || 7941195e687SMark J Musante reason == VDEV_LABEL_SPLIT); 795fa9e4066Sahrens } 796fa9e4066Sahrens 797fa94a07fSbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 798fa94a07fSbrendan l2cache_guid != 0ULL) { 79931157203SJeff Bonwick uint64_t guid_delta = l2cache_guid - vd->vdev_guid; 80031157203SJeff Bonwick 80131157203SJeff Bonwick vd->vdev_guid += guid_delta; 802fa94a07fSbrendan 80331157203SJeff Bonwick for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 80431157203SJeff Bonwick pvd->vdev_guid_sum += guid_delta; 805fa94a07fSbrendan 806fa94a07fSbrendan /* 807fa94a07fSbrendan * If this is a replacement, then we want to fallthrough to the 808fa94a07fSbrendan * rest of the code. If we're adding an l2cache, then it's 809fa94a07fSbrendan * already labeled appropriately and we can just return. 810fa94a07fSbrendan */ 811fa94a07fSbrendan if (reason == VDEV_LABEL_L2CACHE) 812fa94a07fSbrendan return (0); 813fa94a07fSbrendan ASSERT(reason == VDEV_LABEL_REPLACE); 814fa94a07fSbrendan } 815fa94a07fSbrendan 816fa9e4066Sahrens /* 81739c23413Seschrock * Initialize its label. 818fa9e4066Sahrens */ 819770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE); 820770499e1SDan Kimmel abd_zero(vp_abd, sizeof (vdev_phys_t)); 821770499e1SDan Kimmel vp = abd_to_buf(vp_abd); 822fa9e4066Sahrens 823fa9e4066Sahrens /* 824fa9e4066Sahrens * Generate a label describing the pool and our top-level vdev. 825fa9e4066Sahrens * We mark it as being from txg 0 to indicate that it's not 826fa9e4066Sahrens * really part of an active pool just yet. The labels will 827fa9e4066Sahrens * be written again with a meaningful txg by spa_sync(). 828fa9e4066Sahrens */ 82939c23413Seschrock if (reason == VDEV_LABEL_SPARE || 83039c23413Seschrock (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 83139c23413Seschrock /* 83239c23413Seschrock * For inactive hot spares, we generate a special label that 83339c23413Seschrock * identifies as a mutually shared hot spare. We write the 83439c23413Seschrock * label if we are adding a hot spare, or if we are removing an 83539c23413Seschrock * active hot spare (in which case we want to revert the 83639c23413Seschrock * labels). 83739c23413Seschrock */ 83899653d4eSeschrock VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 83999653d4eSeschrock 84099653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 84199653d4eSeschrock spa_version(spa)) == 0); 84299653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 84399653d4eSeschrock POOL_STATE_SPARE) == 0); 84499653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 84599653d4eSeschrock vd->vdev_guid) == 0); 846fa94a07fSbrendan } else if (reason == VDEV_LABEL_L2CACHE || 847fa94a07fSbrendan (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 848fa94a07fSbrendan /* 849fa94a07fSbrendan * For level 2 ARC devices, add a special label. 850fa94a07fSbrendan */ 851fa94a07fSbrendan VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 852fa94a07fSbrendan 853fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 854fa94a07fSbrendan spa_version(spa)) == 0); 855fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 856fa94a07fSbrendan POOL_STATE_L2CACHE) == 0); 857fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 858fa94a07fSbrendan vd->vdev_guid) == 0); 85999653d4eSeschrock } else { 8601195e687SMark J Musante uint64_t txg = 0ULL; 8611195e687SMark J Musante 8621195e687SMark J Musante if (reason == VDEV_LABEL_SPLIT) 8631195e687SMark J Musante txg = spa->spa_uberblock.ub_txg; 8641195e687SMark J Musante label = spa_config_generate(spa, vd, txg, B_FALSE); 86599653d4eSeschrock 86699653d4eSeschrock /* 86799653d4eSeschrock * Add our creation time. This allows us to detect multiple 86899653d4eSeschrock * vdev uses as described above, and automatically expires if we 86999653d4eSeschrock * fail. 87099653d4eSeschrock */ 87199653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 87299653d4eSeschrock crtxg) == 0); 87399653d4eSeschrock } 874fa9e4066Sahrens 875fa9e4066Sahrens buf = vp->vp_nvlist; 876fa9e4066Sahrens buflen = sizeof (vp->vp_nvlist); 877fa9e4066Sahrens 878a75573b6Smmusante error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 879a75573b6Smmusante if (error != 0) { 880fa9e4066Sahrens nvlist_free(label); 881770499e1SDan Kimmel abd_free(vp_abd); 882a75573b6Smmusante /* EFAULT means nvlist_pack ran out of room */ 883a75573b6Smmusante return (error == EFAULT ? ENAMETOOLONG : EINVAL); 884fa9e4066Sahrens } 885fa9e4066Sahrens 886fa9e4066Sahrens /* 887fa9e4066Sahrens * Initialize uberblock template. 888fa9e4066Sahrens */ 889770499e1SDan Kimmel ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE); 890770499e1SDan Kimmel abd_zero(ub_abd, VDEV_UBERBLOCK_RING); 891770499e1SDan Kimmel abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t)); 892770499e1SDan Kimmel ub = abd_to_buf(ub_abd); 893ecc2d604Sbonwick ub->ub_txg = 0; 894fa9e4066Sahrens 895f83ffe1aSLin Ling /* Initialize the 2nd padding area. */ 896770499e1SDan Kimmel pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE); 897770499e1SDan Kimmel abd_zero(pad2, VDEV_PAD_SIZE); 898f83ffe1aSLin Ling 899fa9e4066Sahrens /* 900fa9e4066Sahrens * Write everything in parallel. 901fa9e4066Sahrens */ 9028956713aSEric Schrock retry: 90317f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags); 904fa9e4066Sahrens 905e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) { 906fa9e4066Sahrens 907770499e1SDan Kimmel vdev_label_write(zio, vd, l, vp_abd, 908fa9e4066Sahrens offsetof(vdev_label_t, vl_vdev_phys), 90917f17c2dSbonwick sizeof (vdev_phys_t), NULL, NULL, flags); 910fa9e4066Sahrens 911f83ffe1aSLin Ling /* 912f83ffe1aSLin Ling * Skip the 1st padding area. 913f83ffe1aSLin Ling * Zero out the 2nd padding area where it might have 914f83ffe1aSLin Ling * left over data from previous filesystem format. 915f83ffe1aSLin Ling */ 916f83ffe1aSLin Ling vdev_label_write(zio, vd, l, pad2, 917f83ffe1aSLin Ling offsetof(vdev_label_t, vl_pad2), 918f83ffe1aSLin Ling VDEV_PAD_SIZE, NULL, NULL, flags); 919f83ffe1aSLin Ling 920770499e1SDan Kimmel vdev_label_write(zio, vd, l, ub_abd, 921f64c0e34SEric Taylor offsetof(vdev_label_t, vl_uberblock), 922f64c0e34SEric Taylor VDEV_UBERBLOCK_RING, NULL, NULL, flags); 923fa9e4066Sahrens } 924fa9e4066Sahrens 925fa9e4066Sahrens error = zio_wait(zio); 926fa9e4066Sahrens 9278956713aSEric Schrock if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) { 9288956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD; 9298956713aSEric Schrock goto retry; 9308956713aSEric Schrock } 9318956713aSEric Schrock 932fa9e4066Sahrens nvlist_free(label); 933770499e1SDan Kimmel abd_free(pad2); 934770499e1SDan Kimmel abd_free(ub_abd); 935770499e1SDan Kimmel abd_free(vp_abd); 936fa9e4066Sahrens 93739c23413Seschrock /* 93839c23413Seschrock * If this vdev hasn't been previously identified as a spare, then we 9393d7072f8Seschrock * mark it as such only if a) we are labeling it as a spare, or b) it 940fa94a07fSbrendan * exists as a spare elsewhere in the system. Do the same for 941fa94a07fSbrendan * level 2 ARC devices. 94239c23413Seschrock */ 94339c23413Seschrock if (error == 0 && !vd->vdev_isspare && 94439c23413Seschrock (reason == VDEV_LABEL_SPARE || 94589a89ebfSlling spa_spare_exists(vd->vdev_guid, NULL, NULL))) 94639c23413Seschrock spa_spare_add(vd); 94799653d4eSeschrock 948fa94a07fSbrendan if (error == 0 && !vd->vdev_isl2cache && 949fa94a07fSbrendan (reason == VDEV_LABEL_L2CACHE || 950fa94a07fSbrendan spa_l2cache_exists(vd->vdev_guid, NULL))) 951fa94a07fSbrendan spa_l2cache_add(vd); 952fa94a07fSbrendan 95339c23413Seschrock return (error); 95499653d4eSeschrock } 95599653d4eSeschrock 956fa9e4066Sahrens /* 957fa9e4066Sahrens * ========================================================================== 958fa9e4066Sahrens * uberblock load/sync 959fa9e4066Sahrens * ========================================================================== 960fa9e4066Sahrens */ 961fa9e4066Sahrens 962fa9e4066Sahrens /* 963fa9e4066Sahrens * Consider the following situation: txg is safely synced to disk. We've 964fa9e4066Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 965fa9e4066Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 966fa9e4066Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 967fa9e4066Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 968ad135b5dSChristopher Siden * the missing replica comes back, then for a few seconds we'll have two 969fa9e4066Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 970fa9e4066Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 971fa9e4066Sahrens */ 972fa9e4066Sahrens static int 973fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 974fa9e4066Sahrens { 975fa9e4066Sahrens if (ub1->ub_txg < ub2->ub_txg) 976fa9e4066Sahrens return (-1); 977fa9e4066Sahrens if (ub1->ub_txg > ub2->ub_txg) 978fa9e4066Sahrens return (1); 979fa9e4066Sahrens 980fa9e4066Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 981fa9e4066Sahrens return (-1); 982fa9e4066Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 983fa9e4066Sahrens return (1); 984fa9e4066Sahrens 985fa9e4066Sahrens return (0); 986fa9e4066Sahrens } 987fa9e4066Sahrens 988ad135b5dSChristopher Siden struct ubl_cbdata { 989ad135b5dSChristopher Siden uberblock_t *ubl_ubbest; /* Best uberblock */ 990ad135b5dSChristopher Siden vdev_t *ubl_vd; /* vdev associated with the above */ 991ad135b5dSChristopher Siden }; 992ad135b5dSChristopher Siden 993fa9e4066Sahrens static void 994fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio) 995fa9e4066Sahrens { 996ad135b5dSChristopher Siden vdev_t *vd = zio->io_vd; 997468c413aSTim Haley spa_t *spa = zio->io_spa; 998e14bb325SJeff Bonwick zio_t *rio = zio->io_private; 999770499e1SDan Kimmel uberblock_t *ub = abd_to_buf(zio->io_abd); 1000ad135b5dSChristopher Siden struct ubl_cbdata *cbp = rio->io_private; 1001fa9e4066Sahrens 1002ad135b5dSChristopher Siden ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd)); 1003fa9e4066Sahrens 1004ea8dc4b6Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 1005e14bb325SJeff Bonwick mutex_enter(&rio->io_lock); 1006468c413aSTim Haley if (ub->ub_txg <= spa->spa_load_max_txg && 1007ad135b5dSChristopher Siden vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) { 1008ad135b5dSChristopher Siden /* 1009dfbb9432SGeorge Wilson * Keep track of the vdev in which this uberblock 1010dfbb9432SGeorge Wilson * was found. We will use this information later 1011dfbb9432SGeorge Wilson * to obtain the config nvlist associated with 1012ad135b5dSChristopher Siden * this uberblock. 1013ad135b5dSChristopher Siden */ 1014ad135b5dSChristopher Siden *cbp->ubl_ubbest = *ub; 1015ad135b5dSChristopher Siden cbp->ubl_vd = vd; 1016ad135b5dSChristopher Siden } 1017e14bb325SJeff Bonwick mutex_exit(&rio->io_lock); 1018fa9e4066Sahrens } 1019fa9e4066Sahrens 1020770499e1SDan Kimmel abd_free(zio->io_abd); 1021fa9e4066Sahrens } 1022fa9e4066Sahrens 1023ad135b5dSChristopher Siden static void 1024ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags, 1025ad135b5dSChristopher Siden struct ubl_cbdata *cbp) 1026fa9e4066Sahrens { 1027e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 1028ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp); 1029fa9e4066Sahrens 1030e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1031e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) { 1032e14bb325SJeff Bonwick for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 1033e14bb325SJeff Bonwick vdev_label_read(zio, vd, l, 1034770499e1SDan Kimmel abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd), 1035770499e1SDan Kimmel B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n), 1036e14bb325SJeff Bonwick VDEV_UBERBLOCK_SIZE(vd), 1037e14bb325SJeff Bonwick vdev_uberblock_load_done, zio, flags); 1038e14bb325SJeff Bonwick } 1039fa9e4066Sahrens } 1040fa9e4066Sahrens } 1041ad135b5dSChristopher Siden } 1042e14bb325SJeff Bonwick 1043ad135b5dSChristopher Siden /* 1044ad135b5dSChristopher Siden * Reads the 'best' uberblock from disk along with its associated 1045ad135b5dSChristopher Siden * configuration. First, we read the uberblock array of each label of each 1046ad135b5dSChristopher Siden * vdev, keeping track of the uberblock with the highest txg in each array. 1047dfbb9432SGeorge Wilson * Then, we read the configuration from the same vdev as the best uberblock. 1048ad135b5dSChristopher Siden */ 1049ad135b5dSChristopher Siden void 1050ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config) 1051ad135b5dSChristopher Siden { 1052ad135b5dSChristopher Siden zio_t *zio; 1053ad135b5dSChristopher Siden spa_t *spa = rvd->vdev_spa; 1054ad135b5dSChristopher Siden struct ubl_cbdata cb; 1055ad135b5dSChristopher Siden int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 1056ad135b5dSChristopher Siden ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD; 1057ad135b5dSChristopher Siden 1058ad135b5dSChristopher Siden ASSERT(ub); 1059ad135b5dSChristopher Siden ASSERT(config); 1060ad135b5dSChristopher Siden 1061ad135b5dSChristopher Siden bzero(ub, sizeof (uberblock_t)); 1062ad135b5dSChristopher Siden *config = NULL; 1063ad135b5dSChristopher Siden 1064ad135b5dSChristopher Siden cb.ubl_ubbest = ub; 1065ad135b5dSChristopher Siden cb.ubl_vd = NULL; 1066ad135b5dSChristopher Siden 1067ad135b5dSChristopher Siden spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1068ad135b5dSChristopher Siden zio = zio_root(spa, NULL, &cb, flags); 1069ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio, rvd, flags, &cb); 1070ad135b5dSChristopher Siden (void) zio_wait(zio); 1071dfbb9432SGeorge Wilson 1072dfbb9432SGeorge Wilson /* 1073dfbb9432SGeorge Wilson * It's possible that the best uberblock was discovered on a label 1074dfbb9432SGeorge Wilson * that has a configuration which was written in a future txg. 1075dfbb9432SGeorge Wilson * Search all labels on this vdev to find the configuration that 1076dfbb9432SGeorge Wilson * matches the txg for our uberblock. 1077dfbb9432SGeorge Wilson */ 10783ee8c80cSPavel Zakharov if (cb.ubl_vd != NULL) { 10793ee8c80cSPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. " 10803ee8c80cSPavel Zakharov "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg); 10813ee8c80cSPavel Zakharov 1082dfbb9432SGeorge Wilson *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg); 10836f793812SPavel Zakharov if (*config == NULL && spa->spa_extreme_rewind) { 10846f793812SPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "failed to read label config. " 10856f793812SPavel Zakharov "Trying again without txg restrictions."); 10866f793812SPavel Zakharov *config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX); 10876f793812SPavel Zakharov } 10883ee8c80cSPavel Zakharov if (*config == NULL) { 10893ee8c80cSPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "failed to read label config"); 10903ee8c80cSPavel Zakharov } 10913ee8c80cSPavel Zakharov } 1092ad135b5dSChristopher Siden spa_config_exit(spa, SCL_ALL, FTAG); 1093fa9e4066Sahrens } 1094fa9e4066Sahrens 1095fa9e4066Sahrens /* 109617f17c2dSbonwick * On success, increment root zio's count of good writes. 10970373e76bSbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 1098fa9e4066Sahrens */ 1099fa9e4066Sahrens static void 1100fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio) 1101fa9e4066Sahrens { 110217f17c2dSbonwick uint64_t *good_writes = zio->io_private; 1103fa9e4066Sahrens 11040373e76bSbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 11051a5e258fSJosef 'Jeff' Sipek atomic_inc_64(good_writes); 1106fa9e4066Sahrens } 1107fa9e4066Sahrens 110817f17c2dSbonwick /* 110917f17c2dSbonwick * Write the uberblock to all labels of all leaves of the specified vdev. 111017f17c2dSbonwick */ 1111fa9e4066Sahrens static void 1112e14bb325SJeff Bonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags) 1113fa9e4066Sahrens { 11146f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 1115e14bb325SJeff Bonwick vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags); 1116fa9e4066Sahrens 1117fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) 1118fa9e4066Sahrens return; 1119fa9e4066Sahrens 1120e14bb325SJeff Bonwick if (!vdev_writeable(vd)) 1121fa9e4066Sahrens return; 1122fa9e4066Sahrens 1123770499e1SDan Kimmel int n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 1124fa9e4066Sahrens 1125770499e1SDan Kimmel /* Copy the uberblock_t into the ABD */ 1126770499e1SDan Kimmel abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE); 1127770499e1SDan Kimmel abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd)); 1128770499e1SDan Kimmel abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t)); 1129fa9e4066Sahrens 1130e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) 1131770499e1SDan Kimmel vdev_label_write(zio, vd, l, ub_abd, 1132e14bb325SJeff Bonwick VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd), 113317f17c2dSbonwick vdev_uberblock_sync_done, zio->io_private, 1134e14bb325SJeff Bonwick flags | ZIO_FLAG_DONT_PROPAGATE); 1135fa9e4066Sahrens 1136770499e1SDan Kimmel abd_free(ub_abd); 1137fa9e4066Sahrens } 1138fa9e4066Sahrens 11393e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */ 114017f17c2dSbonwick int 114117f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 1142fa9e4066Sahrens { 114317f17c2dSbonwick spa_t *spa = svd[0]->vdev_spa; 1144e14bb325SJeff Bonwick zio_t *zio; 114517f17c2dSbonwick uint64_t good_writes = 0; 1146fa9e4066Sahrens 1147e14bb325SJeff Bonwick zio = zio_root(spa, NULL, &good_writes, flags); 1148e14bb325SJeff Bonwick 1149e14bb325SJeff Bonwick for (int v = 0; v < svdcount; v++) 1150e14bb325SJeff Bonwick vdev_uberblock_sync(zio, ub, svd[v], flags); 1151fa9e4066Sahrens 115217f17c2dSbonwick (void) zio_wait(zio); 1153fa9e4066Sahrens 1154fa9e4066Sahrens /* 115517f17c2dSbonwick * Flush the uberblocks to disk. This ensures that the odd labels 115617f17c2dSbonwick * are no longer needed (because the new uberblocks and the even 115717f17c2dSbonwick * labels are safely on disk), so it is safe to overwrite them. 1158fa9e4066Sahrens */ 115917f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags); 1160fa9e4066Sahrens 11615cabbc6bSPrashanth Sreenivasa for (int v = 0; v < svdcount; v++) { 11625cabbc6bSPrashanth Sreenivasa if (vdev_writeable(svd[v])) { 11635cabbc6bSPrashanth Sreenivasa zio_flush(zio, svd[v]); 11645cabbc6bSPrashanth Sreenivasa } 11655cabbc6bSPrashanth Sreenivasa } 1166fa9e4066Sahrens 116717f17c2dSbonwick (void) zio_wait(zio); 116817f17c2dSbonwick 116917f17c2dSbonwick return (good_writes >= 1 ? 0 : EIO); 1170fa9e4066Sahrens } 1171fa9e4066Sahrens 1172fa9e4066Sahrens /* 117317f17c2dSbonwick * On success, increment the count of good writes for our top-level vdev. 1174fa9e4066Sahrens */ 1175fa9e4066Sahrens static void 117617f17c2dSbonwick vdev_label_sync_done(zio_t *zio) 1177fa9e4066Sahrens { 117817f17c2dSbonwick uint64_t *good_writes = zio->io_private; 1179fa9e4066Sahrens 1180fa9e4066Sahrens if (zio->io_error == 0) 11811a5e258fSJosef 'Jeff' Sipek atomic_inc_64(good_writes); 1182fa9e4066Sahrens } 1183fa9e4066Sahrens 118417f17c2dSbonwick /* 118517f17c2dSbonwick * If there weren't enough good writes, indicate failure to the parent. 118617f17c2dSbonwick */ 1187fa9e4066Sahrens static void 118817f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio) 118917f17c2dSbonwick { 119017f17c2dSbonwick uint64_t *good_writes = zio->io_private; 119117f17c2dSbonwick 119217f17c2dSbonwick if (*good_writes == 0) 1193be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO); 119417f17c2dSbonwick 119517f17c2dSbonwick kmem_free(good_writes, sizeof (uint64_t)); 119617f17c2dSbonwick } 119717f17c2dSbonwick 119851ece835Seschrock /* 11990430f8daSeschrock * We ignore errors for log and cache devices, simply free the private data. 120051ece835Seschrock */ 120151ece835Seschrock static void 12020430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio) 120351ece835Seschrock { 120451ece835Seschrock kmem_free(zio->io_private, sizeof (uint64_t)); 120551ece835Seschrock } 120651ece835Seschrock 120717f17c2dSbonwick /* 120817f17c2dSbonwick * Write all even or odd labels to all leaves of the specified vdev. 120917f17c2dSbonwick */ 121017f17c2dSbonwick static void 1211e14bb325SJeff Bonwick vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags) 1212fa9e4066Sahrens { 1213fa9e4066Sahrens nvlist_t *label; 1214fa9e4066Sahrens vdev_phys_t *vp; 1215770499e1SDan Kimmel abd_t *vp_abd; 1216fa9e4066Sahrens char *buf; 1217fa9e4066Sahrens size_t buflen; 1218fa9e4066Sahrens 1219e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 1220e14bb325SJeff Bonwick vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags); 1221fa9e4066Sahrens 1222fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) 1223fa9e4066Sahrens return; 1224fa9e4066Sahrens 1225e14bb325SJeff Bonwick if (!vdev_writeable(vd)) 1226fa9e4066Sahrens return; 1227fa9e4066Sahrens 1228fa9e4066Sahrens /* 1229fa9e4066Sahrens * Generate a label describing the top-level config to which we belong. 1230fa9e4066Sahrens */ 12310373e76bSbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 1232fa9e4066Sahrens 1233770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE); 1234770499e1SDan Kimmel abd_zero(vp_abd, sizeof (vdev_phys_t)); 1235770499e1SDan Kimmel vp = abd_to_buf(vp_abd); 1236fa9e4066Sahrens 1237fa9e4066Sahrens buf = vp->vp_nvlist; 1238fa9e4066Sahrens buflen = sizeof (vp->vp_nvlist); 1239fa9e4066Sahrens 124017f17c2dSbonwick if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 124117f17c2dSbonwick for (; l < VDEV_LABELS; l += 2) { 1242770499e1SDan Kimmel vdev_label_write(zio, vd, l, vp_abd, 124317f17c2dSbonwick offsetof(vdev_label_t, vl_vdev_phys), 124417f17c2dSbonwick sizeof (vdev_phys_t), 124517f17c2dSbonwick vdev_label_sync_done, zio->io_private, 1246e14bb325SJeff Bonwick flags | ZIO_FLAG_DONT_PROPAGATE); 124717f17c2dSbonwick } 124817f17c2dSbonwick } 1249fa9e4066Sahrens 1250770499e1SDan Kimmel abd_free(vp_abd); 1251fa9e4066Sahrens nvlist_free(label); 1252fa9e4066Sahrens } 1253fa9e4066Sahrens 125417f17c2dSbonwick int 1255e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags) 1256fa9e4066Sahrens { 1257e14bb325SJeff Bonwick list_t *dl = &spa->spa_config_dirty_list; 125817f17c2dSbonwick vdev_t *vd; 1259e14bb325SJeff Bonwick zio_t *zio; 1260fa9e4066Sahrens int error; 1261fa9e4066Sahrens 1262fa9e4066Sahrens /* 1263e14bb325SJeff Bonwick * Write the new labels to disk. 1264fa9e4066Sahrens */ 1265e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, flags); 1266fa9e4066Sahrens 126717f17c2dSbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 126817f17c2dSbonwick uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 126917f17c2dSbonwick KM_SLEEP); 127088ecc943SGeorge Wilson 127188ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 127288ecc943SGeorge Wilson 1273a3f829aeSBill Moore zio_t *vio = zio_null(zio, spa, NULL, 12740430f8daSeschrock (vd->vdev_islog || vd->vdev_aux != NULL) ? 12750430f8daSeschrock vdev_label_sync_ignore_done : vdev_label_sync_top_done, 127617f17c2dSbonwick good_writes, flags); 1277e14bb325SJeff Bonwick vdev_label_sync(vio, vd, l, txg, flags); 127817f17c2dSbonwick zio_nowait(vio); 1279fa9e4066Sahrens } 1280e14bb325SJeff Bonwick 1281e14bb325SJeff Bonwick error = zio_wait(zio); 1282fa9e4066Sahrens 12838654d025Sperrin /* 128417f17c2dSbonwick * Flush the new labels to disk. 12858654d025Sperrin */ 128617f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags); 12878654d025Sperrin 128817f17c2dSbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 128917f17c2dSbonwick zio_flush(zio, vd); 129017f17c2dSbonwick 129117f17c2dSbonwick (void) zio_wait(zio); 1292fa9e4066Sahrens 1293fa9e4066Sahrens return (error); 1294fa9e4066Sahrens } 1295fa9e4066Sahrens 1296fa9e4066Sahrens /* 129717f17c2dSbonwick * Sync the uberblock and any changes to the vdev configuration. 1298fa9e4066Sahrens * 1299fa9e4066Sahrens * The order of operations is carefully crafted to ensure that 1300fa9e4066Sahrens * if the system panics or loses power at any time, the state on disk 1301fa9e4066Sahrens * is still transactionally consistent. The in-line comments below 1302fa9e4066Sahrens * describe the failure semantics at each stage. 1303fa9e4066Sahrens * 130417f17c2dSbonwick * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 1305fa9e4066Sahrens * at any time, you can just call it again, and it will resume its work. 1306fa9e4066Sahrens */ 1307e14bb325SJeff Bonwick int 1308eb5bb584SWill Andrews vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg) 1309fa9e4066Sahrens { 131017f17c2dSbonwick spa_t *spa = svd[0]->vdev_spa; 1311fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 1312eb5bb584SWill Andrews int error = 0; 1313e14bb325SJeff Bonwick int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 1314fa9e4066Sahrens 1315*86714001SSerapheim Dimitropoulos ASSERT(svdcount != 0); 1316eb5bb584SWill Andrews retry: 13178956713aSEric Schrock /* 13188956713aSEric Schrock * Normally, we don't want to try too hard to write every label and 13198956713aSEric Schrock * uberblock. If there is a flaky disk, we don't want the rest of the 13208956713aSEric Schrock * sync process to block while we retry. But if we can't write a 13218956713aSEric Schrock * single label out, we should retry with ZIO_FLAG_TRYHARD before 13228956713aSEric Schrock * bailing out and declaring the pool faulted. 13238956713aSEric Schrock */ 1324eb5bb584SWill Andrews if (error != 0) { 1325eb5bb584SWill Andrews if ((flags & ZIO_FLAG_TRYHARD) != 0) 1326eb5bb584SWill Andrews return (error); 13278956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD; 1328eb5bb584SWill Andrews } 13298956713aSEric Schrock 1330fa9e4066Sahrens ASSERT(ub->ub_txg <= txg); 1331fa9e4066Sahrens 1332fa9e4066Sahrens /* 133317f17c2dSbonwick * If this isn't a resync due to I/O errors, 133417f17c2dSbonwick * and nothing changed in this transaction group, 133517f17c2dSbonwick * and the vdev configuration hasn't changed, 13360373e76bSbonwick * then there's nothing to do. 1337fa9e4066Sahrens */ 133817f17c2dSbonwick if (ub->ub_txg < txg && 133917f17c2dSbonwick uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 1340e14bb325SJeff Bonwick list_is_empty(&spa->spa_config_dirty_list)) 1341e14bb325SJeff Bonwick return (0); 1342fa9e4066Sahrens 1343fa9e4066Sahrens if (txg > spa_freeze_txg(spa)) 1344e14bb325SJeff Bonwick return (0); 1345fa9e4066Sahrens 13460373e76bSbonwick ASSERT(txg <= spa->spa_final_txg); 13470373e76bSbonwick 1348fa9e4066Sahrens /* 1349fa9e4066Sahrens * Flush the write cache of every disk that's been written to 1350fa9e4066Sahrens * in this transaction group. This ensures that all blocks 1351fa9e4066Sahrens * written in this txg will be committed to stable storage 1352fa9e4066Sahrens * before any uberblock that references them. 1353fa9e4066Sahrens */ 1354*86714001SSerapheim Dimitropoulos zio_t *zio = zio_root(spa, NULL, NULL, flags); 135517f17c2dSbonwick 1356*86714001SSerapheim Dimitropoulos for (vdev_t *vd = 1357*86714001SSerapheim Dimitropoulos txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL; 135817f17c2dSbonwick vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 135917f17c2dSbonwick zio_flush(zio, vd); 136017f17c2dSbonwick 1361fa9e4066Sahrens (void) zio_wait(zio); 1362fa9e4066Sahrens 1363fa9e4066Sahrens /* 1364fa9e4066Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 1365fa9e4066Sahrens * system dies in the middle of this process, that's OK: all of the 1366fa9e4066Sahrens * even labels that made it to disk will be newer than any uberblock, 1367fa9e4066Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 136817f17c2dSbonwick * which have not yet been touched, will still be valid. We flush 136917f17c2dSbonwick * the new labels to disk to ensure that all even-label updates 137017f17c2dSbonwick * are committed to stable storage before the uberblock update. 1371fa9e4066Sahrens */ 1372*86714001SSerapheim Dimitropoulos if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) { 1373*86714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) { 1374*86714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_label_sync_list() returned error %d " 1375*86714001SSerapheim Dimitropoulos "for pool '%s' when syncing out the even labels " 1376*86714001SSerapheim Dimitropoulos "of dirty vdevs", error, spa_name(spa)); 1377*86714001SSerapheim Dimitropoulos } 1378eb5bb584SWill Andrews goto retry; 1379*86714001SSerapheim Dimitropoulos } 1380fa9e4066Sahrens 1381fa9e4066Sahrens /* 1382e14bb325SJeff Bonwick * Sync the uberblocks to all vdevs in svd[]. 13830373e76bSbonwick * If the system dies in the middle of this step, there are two cases 13840373e76bSbonwick * to consider, and the on-disk state is consistent either way: 1385fa9e4066Sahrens * 1386fa9e4066Sahrens * (1) If none of the new uberblocks made it to disk, then the 1387fa9e4066Sahrens * previous uberblock will be the newest, and the odd labels 1388fa9e4066Sahrens * (which had not yet been touched) will be valid with respect 1389fa9e4066Sahrens * to that uberblock. 1390fa9e4066Sahrens * 1391fa9e4066Sahrens * (2) If one or more new uberblocks made it to disk, then they 1392fa9e4066Sahrens * will be the newest, and the even labels (which had all 1393fa9e4066Sahrens * been successfully committed) will be valid with respect 1394fa9e4066Sahrens * to the new uberblocks. 1395fa9e4066Sahrens */ 1396*86714001SSerapheim Dimitropoulos if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) { 1397*86714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) { 1398*86714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_uberblock_sync_list() returned error " 1399*86714001SSerapheim Dimitropoulos "%d for pool '%s'", error, spa_name(spa)); 1400*86714001SSerapheim Dimitropoulos } 1401eb5bb584SWill Andrews goto retry; 1402*86714001SSerapheim Dimitropoulos } 1403fa9e4066Sahrens 1404fa9e4066Sahrens /* 1405fa9e4066Sahrens * Sync out odd labels for every dirty vdev. If the system dies 1406fa9e4066Sahrens * in the middle of this process, the even labels and the new 1407fa9e4066Sahrens * uberblocks will suffice to open the pool. The next time 1408fa9e4066Sahrens * the pool is opened, the first thing we'll do -- before any 1409fa9e4066Sahrens * user data is modified -- is mark every vdev dirty so that 141017f17c2dSbonwick * all labels will be brought up to date. We flush the new labels 141117f17c2dSbonwick * to disk to ensure that all odd-label updates are committed to 141217f17c2dSbonwick * stable storage before the next transaction group begins. 1413fa9e4066Sahrens */ 1414*86714001SSerapheim Dimitropoulos if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) { 1415*86714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) { 1416*86714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_label_sync_list() returned error %d " 1417*86714001SSerapheim Dimitropoulos "for pool '%s' when syncing out the odd labels of " 1418*86714001SSerapheim Dimitropoulos "dirty vdevs", error, spa_name(spa)); 1419*86714001SSerapheim Dimitropoulos } 1420eb5bb584SWill Andrews goto retry; 1421*86714001SSerapheim Dimitropoulos } 1422eb5bb584SWill Andrews 1423eb5bb584SWill Andrews return (0); 1424fa9e4066Sahrens } 1425