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. 2486714001SSerapheim 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 * 36*cfd63e1bSMatthew Ahrens * 2. Verify that all the devices given in a configuration are present 37fa9e4066Sahrens * within the pool. 38fa9e4066Sahrens * 39*cfd63e1bSMatthew Ahrens * 3. Determine the uberblock for the pool. 40fa9e4066Sahrens * 41*cfd63e1bSMatthew Ahrens * 4. In case of an import operation, determine the configuration of the 42fa9e4066Sahrens * toplevel vdev of which it is a part. 43fa9e4066Sahrens * 44*cfd63e1bSMatthew Ahrens * 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 * 80*cfd63e1bSMatthew Ahrens * 1. For each vdev, update 'L1' to the new label 81*cfd63e1bSMatthew Ahrens * 2. Update the uberblock 82*cfd63e1bSMatthew Ahrens * 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 * 120*cfd63e1bSMatthew Ahrens * version ZFS on-disk version 121*cfd63e1bSMatthew Ahrens * name Pool name 122*cfd63e1bSMatthew Ahrens * state Pool state 123*cfd63e1bSMatthew Ahrens * txg Transaction group in which this label was written 124*cfd63e1bSMatthew Ahrens * pool_guid Unique identifier for this pool 125*cfd63e1bSMatthew Ahrens * 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 * 131*cfd63e1bSMatthew Ahrens * top_guid Unique ID for top-level vdev in which this is contained 132*cfd63e1bSMatthew Ahrens * 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 21286714001SSerapheim Dimitropoulos static void 21386714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl) 21486714001SSerapheim Dimitropoulos { 21586714001SSerapheim Dimitropoulos spa_t *spa = vd->vdev_spa; 21686714001SSerapheim Dimitropoulos 21786714001SSerapheim Dimitropoulos if (vd != spa->spa_root_vdev) 21886714001SSerapheim Dimitropoulos return; 21986714001SSerapheim Dimitropoulos 22086714001SSerapheim Dimitropoulos /* provide either current or previous scan information */ 22186714001SSerapheim Dimitropoulos pool_scan_stat_t ps; 22286714001SSerapheim Dimitropoulos if (spa_scan_get_stats(spa, &ps) == 0) { 22386714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl, 22486714001SSerapheim Dimitropoulos ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps, 22586714001SSerapheim Dimitropoulos sizeof (pool_scan_stat_t) / sizeof (uint64_t)); 22686714001SSerapheim Dimitropoulos } 22786714001SSerapheim Dimitropoulos 22886714001SSerapheim Dimitropoulos pool_removal_stat_t prs; 22986714001SSerapheim Dimitropoulos if (spa_removal_get_stats(spa, &prs) == 0) { 23086714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl, 23186714001SSerapheim Dimitropoulos ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs, 23286714001SSerapheim Dimitropoulos sizeof (prs) / sizeof (uint64_t)); 23386714001SSerapheim Dimitropoulos } 23486714001SSerapheim Dimitropoulos 23586714001SSerapheim Dimitropoulos pool_checkpoint_stat_t pcs; 23686714001SSerapheim Dimitropoulos if (spa_checkpoint_get_stats(spa, &pcs) == 0) { 23786714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl, 23886714001SSerapheim Dimitropoulos ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs, 23986714001SSerapheim Dimitropoulos sizeof (pcs) / sizeof (uint64_t)); 24086714001SSerapheim Dimitropoulos } 24186714001SSerapheim Dimitropoulos } 24286714001SSerapheim 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 36586714001SSerapheim 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; 393*cfd63e1bSMatthew Ahrens uint64_t to_alloc = vd->vdev_stat.vs_alloc; 3945cabbc6bSPrashanth Sreenivasa 3955cabbc6bSPrashanth Sreenivasa /* 3965cabbc6bSPrashanth Sreenivasa * There are the same number of allocated segments 3975cabbc6bSPrashanth Sreenivasa * as free segments, so we will have at least one 398*cfd63e1bSMatthew Ahrens * entry per free segment. However, small free 399*cfd63e1bSMatthew Ahrens * segments (smaller than vdev_removal_max_span) 400*cfd63e1bSMatthew Ahrens * will be combined with adjacent allocated segments 401*cfd63e1bSMatthew Ahrens * as a single mapping. 4025cabbc6bSPrashanth Sreenivasa */ 4035cabbc6bSPrashanth Sreenivasa for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) { 404*cfd63e1bSMatthew Ahrens if (1ULL << (i + 1) < vdev_removal_max_span) { 405*cfd63e1bSMatthew Ahrens to_alloc += 406*cfd63e1bSMatthew Ahrens vd->vdev_mg->mg_histogram[i] << 407*cfd63e1bSMatthew Ahrens i + 1; 408*cfd63e1bSMatthew Ahrens } else { 409*cfd63e1bSMatthew Ahrens seg_count += 410*cfd63e1bSMatthew Ahrens vd->vdev_mg->mg_histogram[i]; 411*cfd63e1bSMatthew Ahrens } 4125cabbc6bSPrashanth Sreenivasa } 4135cabbc6bSPrashanth Sreenivasa 4145cabbc6bSPrashanth Sreenivasa /* 415*cfd63e1bSMatthew Ahrens * The maximum length of a mapping is 416*cfd63e1bSMatthew Ahrens * zfs_remove_max_segment, so we need at least one entry 417*cfd63e1bSMatthew Ahrens * per zfs_remove_max_segment of allocated data. 4185cabbc6bSPrashanth Sreenivasa */ 419*cfd63e1bSMatthew Ahrens seg_count += to_alloc / zfs_remove_max_segment; 4205cabbc6bSPrashanth Sreenivasa 4215cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE, 4225cabbc6bSPrashanth Sreenivasa seg_count * 4235cabbc6bSPrashanth Sreenivasa sizeof (vdev_indirect_mapping_entry_phys_t)); 4245cabbc6bSPrashanth Sreenivasa } 425fa9e4066Sahrens } 426fa9e4066Sahrens 427fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 428fa9e4066Sahrens nvlist_t **child; 4293f9d6ad7SLin Ling int c, idx; 430fa9e4066Sahrens 43188ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 43288ecc943SGeorge Wilson 433fa9e4066Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 434fa9e4066Sahrens KM_SLEEP); 435fa9e4066Sahrens 4363f9d6ad7SLin Ling for (c = 0, idx = 0; c < vd->vdev_children; c++) { 4373f9d6ad7SLin Ling vdev_t *cvd = vd->vdev_child[c]; 4383f9d6ad7SLin Ling 4393f9d6ad7SLin Ling /* 4403f9d6ad7SLin Ling * If we're generating an nvlist of removing 4413f9d6ad7SLin Ling * vdevs then skip over any device which is 4423f9d6ad7SLin Ling * not being removed. 4433f9d6ad7SLin Ling */ 4443f9d6ad7SLin Ling if ((flags & VDEV_CONFIG_REMOVING) && 4453f9d6ad7SLin Ling !cvd->vdev_removing) 4463f9d6ad7SLin Ling continue; 447fa9e4066Sahrens 4483f9d6ad7SLin Ling child[idx++] = vdev_config_generate(spa, cvd, 4493f9d6ad7SLin Ling getstats, flags); 4503f9d6ad7SLin Ling } 4513f9d6ad7SLin Ling 4523f9d6ad7SLin Ling if (idx) { 453b4952e17SGeorge Wilson fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 454b4952e17SGeorge Wilson child, idx); 4553f9d6ad7SLin Ling } 456fa9e4066Sahrens 4573f9d6ad7SLin Ling for (c = 0; c < idx; c++) 458fa9e4066Sahrens nvlist_free(child[c]); 459fa9e4066Sahrens 460fa9e4066Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 461441d80aaSlling 462441d80aaSlling } else { 463069f55e2SEric Schrock const char *aux = NULL; 464069f55e2SEric Schrock 465ecc2d604Sbonwick if (vd->vdev_offline && !vd->vdev_tmpoffline) 466b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE); 467b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0) 468b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 469b4952e17SGeorge Wilson vd->vdev_resilver_txg); 4703d7072f8Seschrock if (vd->vdev_faulted) 471b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE); 4723d7072f8Seschrock if (vd->vdev_degraded) 473b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE); 4743d7072f8Seschrock if (vd->vdev_removed) 475b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE); 4763d7072f8Seschrock if (vd->vdev_unspare) 477b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE); 47888ecc943SGeorge Wilson if (vd->vdev_ishole) 479b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE); 480069f55e2SEric Schrock 481069f55e2SEric Schrock switch (vd->vdev_stat.vs_aux) { 482069f55e2SEric Schrock case VDEV_AUX_ERR_EXCEEDED: 483069f55e2SEric Schrock aux = "err_exceeded"; 484069f55e2SEric Schrock break; 485069f55e2SEric Schrock 486069f55e2SEric Schrock case VDEV_AUX_EXTERNAL: 487069f55e2SEric Schrock aux = "external"; 488069f55e2SEric Schrock break; 489069f55e2SEric Schrock } 490069f55e2SEric Schrock 491069f55e2SEric Schrock if (aux != NULL) 492b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux); 4931195e687SMark J Musante 4941195e687SMark J Musante if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) { 495b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID, 496b4952e17SGeorge Wilson vd->vdev_orig_guid); 4971195e687SMark J Musante } 498fa9e4066Sahrens } 499fa9e4066Sahrens 500fa9e4066Sahrens return (nv); 501fa9e4066Sahrens } 502fa9e4066Sahrens 50388ecc943SGeorge Wilson /* 50488ecc943SGeorge Wilson * Generate a view of the top-level vdevs. If we currently have holes 50588ecc943SGeorge Wilson * in the namespace, then generate an array which contains a list of holey 50688ecc943SGeorge Wilson * vdevs. Additionally, add the number of top-level children that currently 50788ecc943SGeorge Wilson * exist. 50888ecc943SGeorge Wilson */ 50988ecc943SGeorge Wilson void 51088ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config) 51188ecc943SGeorge Wilson { 51288ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 51388ecc943SGeorge Wilson uint64_t *array; 5143f9d6ad7SLin Ling uint_t c, idx; 51588ecc943SGeorge Wilson 51688ecc943SGeorge Wilson array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP); 51788ecc943SGeorge Wilson 5183f9d6ad7SLin Ling for (c = 0, idx = 0; c < rvd->vdev_children; c++) { 51988ecc943SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 52088ecc943SGeorge Wilson 5215cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ishole) { 52288ecc943SGeorge Wilson array[idx++] = c; 5235cabbc6bSPrashanth Sreenivasa } 52488ecc943SGeorge Wilson } 52588ecc943SGeorge Wilson 526312c6e15SGeorge Wilson if (idx) { 527312c6e15SGeorge Wilson VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY, 528312c6e15SGeorge Wilson array, idx) == 0); 529312c6e15SGeorge Wilson } 530312c6e15SGeorge Wilson 53188ecc943SGeorge Wilson VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 53288ecc943SGeorge Wilson rvd->vdev_children) == 0); 53388ecc943SGeorge Wilson 53488ecc943SGeorge Wilson kmem_free(array, rvd->vdev_children * sizeof (uint64_t)); 53588ecc943SGeorge Wilson } 53688ecc943SGeorge Wilson 537ad135b5dSChristopher Siden /* 538dfbb9432SGeorge Wilson * Returns the configuration from the label of the given vdev. For vdevs 539dfbb9432SGeorge Wilson * which don't have a txg value stored on their label (i.e. spares/cache) 540dfbb9432SGeorge Wilson * or have not been completely initialized (txg = 0) just return 541dfbb9432SGeorge Wilson * the configuration from the first valid label we find. Otherwise, 542dfbb9432SGeorge Wilson * find the most up-to-date label that does not exceed the specified 543dfbb9432SGeorge Wilson * 'txg' value. 544ad135b5dSChristopher Siden */ 545fa9e4066Sahrens nvlist_t * 546dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg) 547fa9e4066Sahrens { 5480373e76bSbonwick spa_t *spa = vd->vdev_spa; 549fa9e4066Sahrens nvlist_t *config = NULL; 550fa9e4066Sahrens vdev_phys_t *vp; 551770499e1SDan Kimmel abd_t *vp_abd; 552fa9e4066Sahrens zio_t *zio; 553dfbb9432SGeorge Wilson uint64_t best_txg = 0; 554b6bf6e15SPavel Zakharov uint64_t label_txg = 0; 555dfbb9432SGeorge Wilson int error = 0; 5568956713aSEric Schrock int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 5578956713aSEric Schrock ZIO_FLAG_SPECULATIVE; 558fa9e4066Sahrens 559e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 5600373e76bSbonwick 5610a4e9518Sgw if (!vdev_readable(vd)) 562fa9e4066Sahrens return (NULL); 563fa9e4066Sahrens 564770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE); 565770499e1SDan Kimmel vp = abd_to_buf(vp_abd); 566fa9e4066Sahrens 5678956713aSEric Schrock retry: 568e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) { 569dfbb9432SGeorge Wilson nvlist_t *label = NULL; 570fa9e4066Sahrens 571e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, flags); 572fa9e4066Sahrens 573770499e1SDan Kimmel vdev_label_read(zio, vd, l, vp_abd, 574fa9e4066Sahrens offsetof(vdev_label_t, vl_vdev_phys), 575e14bb325SJeff Bonwick sizeof (vdev_phys_t), NULL, NULL, flags); 576fa9e4066Sahrens 577fa9e4066Sahrens if (zio_wait(zio) == 0 && 578fa9e4066Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 579dfbb9432SGeorge Wilson &label, 0) == 0) { 580dfbb9432SGeorge Wilson /* 581dfbb9432SGeorge Wilson * Auxiliary vdevs won't have txg values in their 582dfbb9432SGeorge Wilson * labels and newly added vdevs may not have been 583dfbb9432SGeorge Wilson * completely initialized so just return the 584dfbb9432SGeorge Wilson * configuration from the first valid label we 585dfbb9432SGeorge Wilson * encounter. 586dfbb9432SGeorge Wilson */ 587dfbb9432SGeorge Wilson error = nvlist_lookup_uint64(label, 588dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_TXG, &label_txg); 589dfbb9432SGeorge Wilson if ((error || label_txg == 0) && !config) { 590dfbb9432SGeorge Wilson config = label; 591dfbb9432SGeorge Wilson break; 592dfbb9432SGeorge Wilson } else if (label_txg <= txg && label_txg > best_txg) { 593dfbb9432SGeorge Wilson best_txg = label_txg; 594dfbb9432SGeorge Wilson nvlist_free(config); 595dfbb9432SGeorge Wilson config = fnvlist_dup(label); 596dfbb9432SGeorge Wilson } 597dfbb9432SGeorge Wilson } 598fa9e4066Sahrens 599dfbb9432SGeorge Wilson if (label != NULL) { 600dfbb9432SGeorge Wilson nvlist_free(label); 601dfbb9432SGeorge Wilson label = NULL; 602fa9e4066Sahrens } 603fa9e4066Sahrens } 604fa9e4066Sahrens 6058956713aSEric Schrock if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) { 6068956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD; 6078956713aSEric Schrock goto retry; 6088956713aSEric Schrock } 6098956713aSEric Schrock 610b6bf6e15SPavel Zakharov /* 611b6bf6e15SPavel Zakharov * We found a valid label but it didn't pass txg restrictions. 612b6bf6e15SPavel Zakharov */ 613b6bf6e15SPavel Zakharov if (config == NULL && label_txg != 0) { 614b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "label discarded as txg is too large " 615b6bf6e15SPavel Zakharov "(%llu > %llu)", (u_longlong_t)label_txg, 616b6bf6e15SPavel Zakharov (u_longlong_t)txg); 617b6bf6e15SPavel Zakharov } 618b6bf6e15SPavel Zakharov 619770499e1SDan Kimmel abd_free(vp_abd); 620fa9e4066Sahrens 621fa9e4066Sahrens return (config); 622fa9e4066Sahrens } 623fa9e4066Sahrens 62439c23413Seschrock /* 62539c23413Seschrock * Determine if a device is in use. The 'spare_guid' parameter will be filled 62639c23413Seschrock * in with the device guid if this spare is active elsewhere on the system. 62739c23413Seschrock */ 62839c23413Seschrock static boolean_t 62939c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 630fa94a07fSbrendan uint64_t *spare_guid, uint64_t *l2cache_guid) 63139c23413Seschrock { 63239c23413Seschrock spa_t *spa = vd->vdev_spa; 63339c23413Seschrock uint64_t state, pool_guid, device_guid, txg, spare_pool; 63439c23413Seschrock uint64_t vdtxg = 0; 63539c23413Seschrock nvlist_t *label; 63639c23413Seschrock 63739c23413Seschrock if (spare_guid) 63839c23413Seschrock *spare_guid = 0ULL; 639fa94a07fSbrendan if (l2cache_guid) 640fa94a07fSbrendan *l2cache_guid = 0ULL; 64139c23413Seschrock 64239c23413Seschrock /* 64339c23413Seschrock * Read the label, if any, and perform some basic sanity checks. 64439c23413Seschrock */ 645dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) 64639c23413Seschrock return (B_FALSE); 64739c23413Seschrock 64839c23413Seschrock (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 64939c23413Seschrock &vdtxg); 65039c23413Seschrock 65139c23413Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 65239c23413Seschrock &state) != 0 || 65339c23413Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 65439c23413Seschrock &device_guid) != 0) { 65539c23413Seschrock nvlist_free(label); 65639c23413Seschrock return (B_FALSE); 65739c23413Seschrock } 65839c23413Seschrock 659fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 66039c23413Seschrock (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 66139c23413Seschrock &pool_guid) != 0 || 66239c23413Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 66339c23413Seschrock &txg) != 0)) { 66439c23413Seschrock nvlist_free(label); 66539c23413Seschrock return (B_FALSE); 66639c23413Seschrock } 66739c23413Seschrock 66839c23413Seschrock nvlist_free(label); 66939c23413Seschrock 67039c23413Seschrock /* 67139c23413Seschrock * Check to see if this device indeed belongs to the pool it claims to 67239c23413Seschrock * be a part of. The only way this is allowed is if the device is a hot 67339c23413Seschrock * spare (which we check for later on). 67439c23413Seschrock */ 675fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 67639c23413Seschrock !spa_guid_exists(pool_guid, device_guid) && 67789a89ebfSlling !spa_spare_exists(device_guid, NULL, NULL) && 678fa94a07fSbrendan !spa_l2cache_exists(device_guid, NULL)) 67939c23413Seschrock return (B_FALSE); 68039c23413Seschrock 68139c23413Seschrock /* 68239c23413Seschrock * If the transaction group is zero, then this an initialized (but 68339c23413Seschrock * unused) label. This is only an error if the create transaction 68439c23413Seschrock * on-disk is the same as the one we're using now, in which case the 68539c23413Seschrock * user has attempted to add the same vdev multiple times in the same 68639c23413Seschrock * transaction. 68739c23413Seschrock */ 688fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 689fa94a07fSbrendan txg == 0 && vdtxg == crtxg) 69039c23413Seschrock return (B_TRUE); 69139c23413Seschrock 69239c23413Seschrock /* 69339c23413Seschrock * Check to see if this is a spare device. We do an explicit check for 69439c23413Seschrock * spa_has_spare() here because it may be on our pending list of spares 695fa94a07fSbrendan * to add. We also check if it is an l2cache device. 69639c23413Seschrock */ 69789a89ebfSlling if (spa_spare_exists(device_guid, &spare_pool, NULL) || 69839c23413Seschrock spa_has_spare(spa, device_guid)) { 69939c23413Seschrock if (spare_guid) 70039c23413Seschrock *spare_guid = device_guid; 70139c23413Seschrock 70239c23413Seschrock switch (reason) { 70339c23413Seschrock case VDEV_LABEL_CREATE: 704fa94a07fSbrendan case VDEV_LABEL_L2CACHE: 70539c23413Seschrock return (B_TRUE); 70639c23413Seschrock 70739c23413Seschrock case VDEV_LABEL_REPLACE: 70839c23413Seschrock return (!spa_has_spare(spa, device_guid) || 70939c23413Seschrock spare_pool != 0ULL); 71039c23413Seschrock 71139c23413Seschrock case VDEV_LABEL_SPARE: 71239c23413Seschrock return (spa_has_spare(spa, device_guid)); 71339c23413Seschrock } 71439c23413Seschrock } 71539c23413Seschrock 716fa94a07fSbrendan /* 717fa94a07fSbrendan * Check to see if this is an l2cache device. 718fa94a07fSbrendan */ 719fa94a07fSbrendan if (spa_l2cache_exists(device_guid, NULL)) 720fa94a07fSbrendan return (B_TRUE); 721fa94a07fSbrendan 722f9af39baSGeorge Wilson /* 723f9af39baSGeorge Wilson * We can't rely on a pool's state if it's been imported 724f9af39baSGeorge Wilson * read-only. Instead we look to see if the pools is marked 725f9af39baSGeorge Wilson * read-only in the namespace and set the state to active. 726f9af39baSGeorge Wilson */ 7275bdd995dSRichard Yao if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 7285bdd995dSRichard Yao (spa = spa_by_guid(pool_guid, device_guid)) != NULL && 729f9af39baSGeorge Wilson spa_mode(spa) == FREAD) 730f9af39baSGeorge Wilson state = POOL_STATE_ACTIVE; 731f9af39baSGeorge Wilson 73239c23413Seschrock /* 73339c23413Seschrock * If the device is marked ACTIVE, then this device is in use by another 73439c23413Seschrock * pool on the system. 73539c23413Seschrock */ 73639c23413Seschrock return (state == POOL_STATE_ACTIVE); 73739c23413Seschrock } 73839c23413Seschrock 73939c23413Seschrock /* 74039c23413Seschrock * Initialize a vdev label. We check to make sure each leaf device is not in 74139c23413Seschrock * use, and writable. We put down an initial label which we will later 74239c23413Seschrock * overwrite with a complete label. Note that it's important to do this 74339c23413Seschrock * sequentially, not in parallel, so that we catch cases of multiple use of the 74439c23413Seschrock * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 74539c23413Seschrock * itself. 74639c23413Seschrock */ 74739c23413Seschrock int 74839c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 749fa9e4066Sahrens { 750fa9e4066Sahrens spa_t *spa = vd->vdev_spa; 751fa9e4066Sahrens nvlist_t *label; 752fa9e4066Sahrens vdev_phys_t *vp; 753770499e1SDan Kimmel abd_t *vp_abd; 754770499e1SDan Kimmel abd_t *pad2; 755ecc2d604Sbonwick uberblock_t *ub; 756770499e1SDan Kimmel abd_t *ub_abd; 757fa9e4066Sahrens zio_t *zio; 758fa9e4066Sahrens char *buf; 759fa9e4066Sahrens size_t buflen; 760fa9e4066Sahrens int error; 761fa94a07fSbrendan uint64_t spare_guid, l2cache_guid; 762e14bb325SJeff Bonwick int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 763fa9e4066Sahrens 764e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 7650373e76bSbonwick 766e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 76739c23413Seschrock if ((error = vdev_label_init(vd->vdev_child[c], 76839c23413Seschrock crtxg, reason)) != 0) 769fa9e4066Sahrens return (error); 770fa9e4066Sahrens 77188ecc943SGeorge Wilson /* Track the creation time for this vdev */ 77288ecc943SGeorge Wilson vd->vdev_crtxg = crtxg; 77388ecc943SGeorge Wilson 774973c78e9SGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa)) 775fa9e4066Sahrens return (0); 776fa9e4066Sahrens 777fa9e4066Sahrens /* 77839c23413Seschrock * Dead vdevs cannot be initialized. 779fa9e4066Sahrens */ 780fa9e4066Sahrens if (vdev_is_dead(vd)) 781be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 782fa9e4066Sahrens 783fa9e4066Sahrens /* 78439c23413Seschrock * Determine if the vdev is in use. 785fa9e4066Sahrens */ 7861195e687SMark J Musante if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT && 787fa94a07fSbrendan vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 788be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 78939c23413Seschrock 79039c23413Seschrock /* 791fa94a07fSbrendan * If this is a request to add or replace a spare or l2cache device 792fa94a07fSbrendan * that is in use elsewhere on the system, then we must update the 793fa94a07fSbrendan * guid (which was initialized to a random value) to reflect the 794fa94a07fSbrendan * actual GUID (which is shared between multiple pools). 79539c23413Seschrock */ 796fa94a07fSbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 797fa94a07fSbrendan spare_guid != 0ULL) { 79831157203SJeff Bonwick uint64_t guid_delta = spare_guid - vd->vdev_guid; 79999653d4eSeschrock 80031157203SJeff Bonwick vd->vdev_guid += guid_delta; 80131157203SJeff Bonwick 80231157203SJeff Bonwick for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 80331157203SJeff Bonwick pvd->vdev_guid_sum += guid_delta; 80439c23413Seschrock 80599653d4eSeschrock /* 80639c23413Seschrock * If this is a replacement, then we want to fallthrough to the 80739c23413Seschrock * rest of the code. If we're adding a spare, then it's already 8083d7072f8Seschrock * labeled appropriately and we can just return. 80999653d4eSeschrock */ 81039c23413Seschrock if (reason == VDEV_LABEL_SPARE) 81139c23413Seschrock return (0); 8121195e687SMark J Musante ASSERT(reason == VDEV_LABEL_REPLACE || 8131195e687SMark J Musante reason == VDEV_LABEL_SPLIT); 814fa9e4066Sahrens } 815fa9e4066Sahrens 816fa94a07fSbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 817fa94a07fSbrendan l2cache_guid != 0ULL) { 81831157203SJeff Bonwick uint64_t guid_delta = l2cache_guid - vd->vdev_guid; 81931157203SJeff Bonwick 82031157203SJeff Bonwick vd->vdev_guid += guid_delta; 821fa94a07fSbrendan 82231157203SJeff Bonwick for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 82331157203SJeff Bonwick pvd->vdev_guid_sum += guid_delta; 824fa94a07fSbrendan 825fa94a07fSbrendan /* 826fa94a07fSbrendan * If this is a replacement, then we want to fallthrough to the 827fa94a07fSbrendan * rest of the code. If we're adding an l2cache, then it's 828fa94a07fSbrendan * already labeled appropriately and we can just return. 829fa94a07fSbrendan */ 830fa94a07fSbrendan if (reason == VDEV_LABEL_L2CACHE) 831fa94a07fSbrendan return (0); 832fa94a07fSbrendan ASSERT(reason == VDEV_LABEL_REPLACE); 833fa94a07fSbrendan } 834fa94a07fSbrendan 835fa9e4066Sahrens /* 83639c23413Seschrock * Initialize its label. 837fa9e4066Sahrens */ 838770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE); 839770499e1SDan Kimmel abd_zero(vp_abd, sizeof (vdev_phys_t)); 840770499e1SDan Kimmel vp = abd_to_buf(vp_abd); 841fa9e4066Sahrens 842fa9e4066Sahrens /* 843fa9e4066Sahrens * Generate a label describing the pool and our top-level vdev. 844fa9e4066Sahrens * We mark it as being from txg 0 to indicate that it's not 845fa9e4066Sahrens * really part of an active pool just yet. The labels will 846fa9e4066Sahrens * be written again with a meaningful txg by spa_sync(). 847fa9e4066Sahrens */ 84839c23413Seschrock if (reason == VDEV_LABEL_SPARE || 84939c23413Seschrock (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 85039c23413Seschrock /* 85139c23413Seschrock * For inactive hot spares, we generate a special label that 85239c23413Seschrock * identifies as a mutually shared hot spare. We write the 85339c23413Seschrock * label if we are adding a hot spare, or if we are removing an 85439c23413Seschrock * active hot spare (in which case we want to revert the 85539c23413Seschrock * labels). 85639c23413Seschrock */ 85799653d4eSeschrock VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 85899653d4eSeschrock 85999653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 86099653d4eSeschrock spa_version(spa)) == 0); 86199653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 86299653d4eSeschrock POOL_STATE_SPARE) == 0); 86399653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 86499653d4eSeschrock vd->vdev_guid) == 0); 865fa94a07fSbrendan } else if (reason == VDEV_LABEL_L2CACHE || 866fa94a07fSbrendan (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 867fa94a07fSbrendan /* 868fa94a07fSbrendan * For level 2 ARC devices, add a special label. 869fa94a07fSbrendan */ 870fa94a07fSbrendan VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 871fa94a07fSbrendan 872fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 873fa94a07fSbrendan spa_version(spa)) == 0); 874fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 875fa94a07fSbrendan POOL_STATE_L2CACHE) == 0); 876fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 877fa94a07fSbrendan vd->vdev_guid) == 0); 87899653d4eSeschrock } else { 8791195e687SMark J Musante uint64_t txg = 0ULL; 8801195e687SMark J Musante 8811195e687SMark J Musante if (reason == VDEV_LABEL_SPLIT) 8821195e687SMark J Musante txg = spa->spa_uberblock.ub_txg; 8831195e687SMark J Musante label = spa_config_generate(spa, vd, txg, B_FALSE); 88499653d4eSeschrock 88599653d4eSeschrock /* 88699653d4eSeschrock * Add our creation time. This allows us to detect multiple 88799653d4eSeschrock * vdev uses as described above, and automatically expires if we 88899653d4eSeschrock * fail. 88999653d4eSeschrock */ 89099653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 89199653d4eSeschrock crtxg) == 0); 89299653d4eSeschrock } 893fa9e4066Sahrens 894fa9e4066Sahrens buf = vp->vp_nvlist; 895fa9e4066Sahrens buflen = sizeof (vp->vp_nvlist); 896fa9e4066Sahrens 897a75573b6Smmusante error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 898a75573b6Smmusante if (error != 0) { 899fa9e4066Sahrens nvlist_free(label); 900770499e1SDan Kimmel abd_free(vp_abd); 901a75573b6Smmusante /* EFAULT means nvlist_pack ran out of room */ 902a75573b6Smmusante return (error == EFAULT ? ENAMETOOLONG : EINVAL); 903fa9e4066Sahrens } 904fa9e4066Sahrens 905fa9e4066Sahrens /* 906fa9e4066Sahrens * Initialize uberblock template. 907fa9e4066Sahrens */ 908770499e1SDan Kimmel ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE); 909770499e1SDan Kimmel abd_zero(ub_abd, VDEV_UBERBLOCK_RING); 910770499e1SDan Kimmel abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t)); 911770499e1SDan Kimmel ub = abd_to_buf(ub_abd); 912ecc2d604Sbonwick ub->ub_txg = 0; 913fa9e4066Sahrens 914f83ffe1aSLin Ling /* Initialize the 2nd padding area. */ 915770499e1SDan Kimmel pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE); 916770499e1SDan Kimmel abd_zero(pad2, VDEV_PAD_SIZE); 917f83ffe1aSLin Ling 918fa9e4066Sahrens /* 919fa9e4066Sahrens * Write everything in parallel. 920fa9e4066Sahrens */ 9218956713aSEric Schrock retry: 92217f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags); 923fa9e4066Sahrens 924e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) { 925fa9e4066Sahrens 926770499e1SDan Kimmel vdev_label_write(zio, vd, l, vp_abd, 927fa9e4066Sahrens offsetof(vdev_label_t, vl_vdev_phys), 92817f17c2dSbonwick sizeof (vdev_phys_t), NULL, NULL, flags); 929fa9e4066Sahrens 930f83ffe1aSLin Ling /* 931f83ffe1aSLin Ling * Skip the 1st padding area. 932f83ffe1aSLin Ling * Zero out the 2nd padding area where it might have 933f83ffe1aSLin Ling * left over data from previous filesystem format. 934f83ffe1aSLin Ling */ 935f83ffe1aSLin Ling vdev_label_write(zio, vd, l, pad2, 936f83ffe1aSLin Ling offsetof(vdev_label_t, vl_pad2), 937f83ffe1aSLin Ling VDEV_PAD_SIZE, NULL, NULL, flags); 938f83ffe1aSLin Ling 939770499e1SDan Kimmel vdev_label_write(zio, vd, l, ub_abd, 940f64c0e34SEric Taylor offsetof(vdev_label_t, vl_uberblock), 941f64c0e34SEric Taylor VDEV_UBERBLOCK_RING, NULL, NULL, flags); 942fa9e4066Sahrens } 943fa9e4066Sahrens 944fa9e4066Sahrens error = zio_wait(zio); 945fa9e4066Sahrens 9468956713aSEric Schrock if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) { 9478956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD; 9488956713aSEric Schrock goto retry; 9498956713aSEric Schrock } 9508956713aSEric Schrock 951fa9e4066Sahrens nvlist_free(label); 952770499e1SDan Kimmel abd_free(pad2); 953770499e1SDan Kimmel abd_free(ub_abd); 954770499e1SDan Kimmel abd_free(vp_abd); 955fa9e4066Sahrens 95639c23413Seschrock /* 95739c23413Seschrock * If this vdev hasn't been previously identified as a spare, then we 9583d7072f8Seschrock * mark it as such only if a) we are labeling it as a spare, or b) it 959fa94a07fSbrendan * exists as a spare elsewhere in the system. Do the same for 960fa94a07fSbrendan * level 2 ARC devices. 96139c23413Seschrock */ 96239c23413Seschrock if (error == 0 && !vd->vdev_isspare && 96339c23413Seschrock (reason == VDEV_LABEL_SPARE || 96489a89ebfSlling spa_spare_exists(vd->vdev_guid, NULL, NULL))) 96539c23413Seschrock spa_spare_add(vd); 96699653d4eSeschrock 967fa94a07fSbrendan if (error == 0 && !vd->vdev_isl2cache && 968fa94a07fSbrendan (reason == VDEV_LABEL_L2CACHE || 969fa94a07fSbrendan spa_l2cache_exists(vd->vdev_guid, NULL))) 970fa94a07fSbrendan spa_l2cache_add(vd); 971fa94a07fSbrendan 97239c23413Seschrock return (error); 97399653d4eSeschrock } 97499653d4eSeschrock 975fa9e4066Sahrens /* 976fa9e4066Sahrens * ========================================================================== 977fa9e4066Sahrens * uberblock load/sync 978fa9e4066Sahrens * ========================================================================== 979fa9e4066Sahrens */ 980fa9e4066Sahrens 981fa9e4066Sahrens /* 982fa9e4066Sahrens * Consider the following situation: txg is safely synced to disk. We've 983fa9e4066Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 984fa9e4066Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 985fa9e4066Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 986fa9e4066Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 987ad135b5dSChristopher Siden * the missing replica comes back, then for a few seconds we'll have two 988fa9e4066Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 989fa9e4066Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 990fa9e4066Sahrens */ 991fa9e4066Sahrens static int 992fa9e4066Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 993fa9e4066Sahrens { 994fa9e4066Sahrens if (ub1->ub_txg < ub2->ub_txg) 995fa9e4066Sahrens return (-1); 996fa9e4066Sahrens if (ub1->ub_txg > ub2->ub_txg) 997fa9e4066Sahrens return (1); 998fa9e4066Sahrens 999fa9e4066Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 1000fa9e4066Sahrens return (-1); 1001fa9e4066Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 1002fa9e4066Sahrens return (1); 1003fa9e4066Sahrens 1004fa9e4066Sahrens return (0); 1005fa9e4066Sahrens } 1006fa9e4066Sahrens 1007ad135b5dSChristopher Siden struct ubl_cbdata { 1008ad135b5dSChristopher Siden uberblock_t *ubl_ubbest; /* Best uberblock */ 1009ad135b5dSChristopher Siden vdev_t *ubl_vd; /* vdev associated with the above */ 1010ad135b5dSChristopher Siden }; 1011ad135b5dSChristopher Siden 1012fa9e4066Sahrens static void 1013fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio) 1014fa9e4066Sahrens { 1015ad135b5dSChristopher Siden vdev_t *vd = zio->io_vd; 1016468c413aSTim Haley spa_t *spa = zio->io_spa; 1017e14bb325SJeff Bonwick zio_t *rio = zio->io_private; 1018770499e1SDan Kimmel uberblock_t *ub = abd_to_buf(zio->io_abd); 1019ad135b5dSChristopher Siden struct ubl_cbdata *cbp = rio->io_private; 1020fa9e4066Sahrens 1021ad135b5dSChristopher Siden ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd)); 1022fa9e4066Sahrens 1023ea8dc4b6Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 1024e14bb325SJeff Bonwick mutex_enter(&rio->io_lock); 1025468c413aSTim Haley if (ub->ub_txg <= spa->spa_load_max_txg && 1026ad135b5dSChristopher Siden vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) { 1027ad135b5dSChristopher Siden /* 1028dfbb9432SGeorge Wilson * Keep track of the vdev in which this uberblock 1029dfbb9432SGeorge Wilson * was found. We will use this information later 1030dfbb9432SGeorge Wilson * to obtain the config nvlist associated with 1031ad135b5dSChristopher Siden * this uberblock. 1032ad135b5dSChristopher Siden */ 1033ad135b5dSChristopher Siden *cbp->ubl_ubbest = *ub; 1034ad135b5dSChristopher Siden cbp->ubl_vd = vd; 1035ad135b5dSChristopher Siden } 1036e14bb325SJeff Bonwick mutex_exit(&rio->io_lock); 1037fa9e4066Sahrens } 1038fa9e4066Sahrens 1039770499e1SDan Kimmel abd_free(zio->io_abd); 1040fa9e4066Sahrens } 1041fa9e4066Sahrens 1042ad135b5dSChristopher Siden static void 1043ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags, 1044ad135b5dSChristopher Siden struct ubl_cbdata *cbp) 1045fa9e4066Sahrens { 1046e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 1047ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp); 1048fa9e4066Sahrens 1049e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1050e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) { 1051e14bb325SJeff Bonwick for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 1052e14bb325SJeff Bonwick vdev_label_read(zio, vd, l, 1053770499e1SDan Kimmel abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd), 1054770499e1SDan Kimmel B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n), 1055e14bb325SJeff Bonwick VDEV_UBERBLOCK_SIZE(vd), 1056e14bb325SJeff Bonwick vdev_uberblock_load_done, zio, flags); 1057e14bb325SJeff Bonwick } 1058fa9e4066Sahrens } 1059fa9e4066Sahrens } 1060ad135b5dSChristopher Siden } 1061e14bb325SJeff Bonwick 1062ad135b5dSChristopher Siden /* 1063ad135b5dSChristopher Siden * Reads the 'best' uberblock from disk along with its associated 1064ad135b5dSChristopher Siden * configuration. First, we read the uberblock array of each label of each 1065ad135b5dSChristopher Siden * vdev, keeping track of the uberblock with the highest txg in each array. 1066dfbb9432SGeorge Wilson * Then, we read the configuration from the same vdev as the best uberblock. 1067ad135b5dSChristopher Siden */ 1068ad135b5dSChristopher Siden void 1069ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config) 1070ad135b5dSChristopher Siden { 1071ad135b5dSChristopher Siden zio_t *zio; 1072ad135b5dSChristopher Siden spa_t *spa = rvd->vdev_spa; 1073ad135b5dSChristopher Siden struct ubl_cbdata cb; 1074ad135b5dSChristopher Siden int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 1075ad135b5dSChristopher Siden ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD; 1076ad135b5dSChristopher Siden 1077ad135b5dSChristopher Siden ASSERT(ub); 1078ad135b5dSChristopher Siden ASSERT(config); 1079ad135b5dSChristopher Siden 1080ad135b5dSChristopher Siden bzero(ub, sizeof (uberblock_t)); 1081ad135b5dSChristopher Siden *config = NULL; 1082ad135b5dSChristopher Siden 1083ad135b5dSChristopher Siden cb.ubl_ubbest = ub; 1084ad135b5dSChristopher Siden cb.ubl_vd = NULL; 1085ad135b5dSChristopher Siden 1086ad135b5dSChristopher Siden spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1087ad135b5dSChristopher Siden zio = zio_root(spa, NULL, &cb, flags); 1088ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio, rvd, flags, &cb); 1089ad135b5dSChristopher Siden (void) zio_wait(zio); 1090dfbb9432SGeorge Wilson 1091dfbb9432SGeorge Wilson /* 1092dfbb9432SGeorge Wilson * It's possible that the best uberblock was discovered on a label 1093dfbb9432SGeorge Wilson * that has a configuration which was written in a future txg. 1094dfbb9432SGeorge Wilson * Search all labels on this vdev to find the configuration that 1095dfbb9432SGeorge Wilson * matches the txg for our uberblock. 1096dfbb9432SGeorge Wilson */ 10973ee8c80cSPavel Zakharov if (cb.ubl_vd != NULL) { 10983ee8c80cSPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. " 10993ee8c80cSPavel Zakharov "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg); 11003ee8c80cSPavel Zakharov 1101dfbb9432SGeorge Wilson *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg); 11026f793812SPavel Zakharov if (*config == NULL && spa->spa_extreme_rewind) { 11036f793812SPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "failed to read label config. " 11046f793812SPavel Zakharov "Trying again without txg restrictions."); 11056f793812SPavel Zakharov *config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX); 11066f793812SPavel Zakharov } 11073ee8c80cSPavel Zakharov if (*config == NULL) { 11083ee8c80cSPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "failed to read label config"); 11093ee8c80cSPavel Zakharov } 11103ee8c80cSPavel Zakharov } 1111ad135b5dSChristopher Siden spa_config_exit(spa, SCL_ALL, FTAG); 1112fa9e4066Sahrens } 1113fa9e4066Sahrens 1114fa9e4066Sahrens /* 111517f17c2dSbonwick * On success, increment root zio's count of good writes. 11160373e76bSbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 1117fa9e4066Sahrens */ 1118fa9e4066Sahrens static void 1119fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio) 1120fa9e4066Sahrens { 112117f17c2dSbonwick uint64_t *good_writes = zio->io_private; 1122fa9e4066Sahrens 11230373e76bSbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 11241a5e258fSJosef 'Jeff' Sipek atomic_inc_64(good_writes); 1125fa9e4066Sahrens } 1126fa9e4066Sahrens 112717f17c2dSbonwick /* 112817f17c2dSbonwick * Write the uberblock to all labels of all leaves of the specified vdev. 112917f17c2dSbonwick */ 1130fa9e4066Sahrens static void 1131a3b55830SMatthew Ahrens vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes, 1132a3b55830SMatthew Ahrens uberblock_t *ub, vdev_t *vd, int flags) 1133fa9e4066Sahrens { 1134a3b55830SMatthew Ahrens for (uint64_t c = 0; c < vd->vdev_children; c++) { 1135a3b55830SMatthew Ahrens vdev_uberblock_sync(zio, good_writes, 1136a3b55830SMatthew Ahrens ub, vd->vdev_child[c], flags); 1137a3b55830SMatthew Ahrens } 1138fa9e4066Sahrens 1139fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) 1140fa9e4066Sahrens return; 1141fa9e4066Sahrens 1142e14bb325SJeff Bonwick if (!vdev_writeable(vd)) 1143fa9e4066Sahrens return; 1144fa9e4066Sahrens 1145770499e1SDan Kimmel int n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 1146fa9e4066Sahrens 1147770499e1SDan Kimmel /* Copy the uberblock_t into the ABD */ 1148770499e1SDan Kimmel abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE); 1149770499e1SDan Kimmel abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd)); 1150770499e1SDan Kimmel abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t)); 1151fa9e4066Sahrens 1152e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) 1153770499e1SDan Kimmel vdev_label_write(zio, vd, l, ub_abd, 1154e14bb325SJeff Bonwick VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd), 1155a3b55830SMatthew Ahrens vdev_uberblock_sync_done, good_writes, 1156e14bb325SJeff Bonwick flags | ZIO_FLAG_DONT_PROPAGATE); 1157fa9e4066Sahrens 1158770499e1SDan Kimmel abd_free(ub_abd); 1159fa9e4066Sahrens } 1160fa9e4066Sahrens 11613e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */ 116217f17c2dSbonwick int 116317f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 1164fa9e4066Sahrens { 116517f17c2dSbonwick spa_t *spa = svd[0]->vdev_spa; 1166e14bb325SJeff Bonwick zio_t *zio; 116717f17c2dSbonwick uint64_t good_writes = 0; 1168fa9e4066Sahrens 1169a3b55830SMatthew Ahrens zio = zio_root(spa, NULL, NULL, flags); 1170e14bb325SJeff Bonwick 1171e14bb325SJeff Bonwick for (int v = 0; v < svdcount; v++) 1172a3b55830SMatthew Ahrens vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags); 1173fa9e4066Sahrens 117417f17c2dSbonwick (void) zio_wait(zio); 1175fa9e4066Sahrens 1176fa9e4066Sahrens /* 117717f17c2dSbonwick * Flush the uberblocks to disk. This ensures that the odd labels 117817f17c2dSbonwick * are no longer needed (because the new uberblocks and the even 117917f17c2dSbonwick * labels are safely on disk), so it is safe to overwrite them. 1180fa9e4066Sahrens */ 118117f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags); 1182fa9e4066Sahrens 11835cabbc6bSPrashanth Sreenivasa for (int v = 0; v < svdcount; v++) { 11845cabbc6bSPrashanth Sreenivasa if (vdev_writeable(svd[v])) { 11855cabbc6bSPrashanth Sreenivasa zio_flush(zio, svd[v]); 11865cabbc6bSPrashanth Sreenivasa } 11875cabbc6bSPrashanth Sreenivasa } 1188fa9e4066Sahrens 118917f17c2dSbonwick (void) zio_wait(zio); 119017f17c2dSbonwick 119117f17c2dSbonwick return (good_writes >= 1 ? 0 : EIO); 1192fa9e4066Sahrens } 1193fa9e4066Sahrens 1194fa9e4066Sahrens /* 119517f17c2dSbonwick * On success, increment the count of good writes for our top-level vdev. 1196fa9e4066Sahrens */ 1197fa9e4066Sahrens static void 119817f17c2dSbonwick vdev_label_sync_done(zio_t *zio) 1199fa9e4066Sahrens { 120017f17c2dSbonwick uint64_t *good_writes = zio->io_private; 1201fa9e4066Sahrens 1202fa9e4066Sahrens if (zio->io_error == 0) 12031a5e258fSJosef 'Jeff' Sipek atomic_inc_64(good_writes); 1204fa9e4066Sahrens } 1205fa9e4066Sahrens 120617f17c2dSbonwick /* 120717f17c2dSbonwick * If there weren't enough good writes, indicate failure to the parent. 120817f17c2dSbonwick */ 1209fa9e4066Sahrens static void 121017f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio) 121117f17c2dSbonwick { 121217f17c2dSbonwick uint64_t *good_writes = zio->io_private; 121317f17c2dSbonwick 121417f17c2dSbonwick if (*good_writes == 0) 1215be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO); 121617f17c2dSbonwick 121717f17c2dSbonwick kmem_free(good_writes, sizeof (uint64_t)); 121817f17c2dSbonwick } 121917f17c2dSbonwick 122051ece835Seschrock /* 12210430f8daSeschrock * We ignore errors for log and cache devices, simply free the private data. 122251ece835Seschrock */ 122351ece835Seschrock static void 12240430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio) 122551ece835Seschrock { 122651ece835Seschrock kmem_free(zio->io_private, sizeof (uint64_t)); 122751ece835Seschrock } 122851ece835Seschrock 122917f17c2dSbonwick /* 123017f17c2dSbonwick * Write all even or odd labels to all leaves of the specified vdev. 123117f17c2dSbonwick */ 123217f17c2dSbonwick static void 1233a3b55830SMatthew Ahrens vdev_label_sync(zio_t *zio, uint64_t *good_writes, 1234a3b55830SMatthew Ahrens vdev_t *vd, int l, uint64_t txg, int flags) 1235fa9e4066Sahrens { 1236fa9e4066Sahrens nvlist_t *label; 1237fa9e4066Sahrens vdev_phys_t *vp; 1238770499e1SDan Kimmel abd_t *vp_abd; 1239fa9e4066Sahrens char *buf; 1240fa9e4066Sahrens size_t buflen; 1241fa9e4066Sahrens 1242a3b55830SMatthew Ahrens for (int c = 0; c < vd->vdev_children; c++) { 1243a3b55830SMatthew Ahrens vdev_label_sync(zio, good_writes, 1244a3b55830SMatthew Ahrens vd->vdev_child[c], l, txg, flags); 1245a3b55830SMatthew Ahrens } 1246fa9e4066Sahrens 1247fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) 1248fa9e4066Sahrens return; 1249fa9e4066Sahrens 1250e14bb325SJeff Bonwick if (!vdev_writeable(vd)) 1251fa9e4066Sahrens return; 1252fa9e4066Sahrens 1253fa9e4066Sahrens /* 1254fa9e4066Sahrens * Generate a label describing the top-level config to which we belong. 1255fa9e4066Sahrens */ 12560373e76bSbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 1257fa9e4066Sahrens 1258770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE); 1259770499e1SDan Kimmel abd_zero(vp_abd, sizeof (vdev_phys_t)); 1260770499e1SDan Kimmel vp = abd_to_buf(vp_abd); 1261fa9e4066Sahrens 1262fa9e4066Sahrens buf = vp->vp_nvlist; 1263fa9e4066Sahrens buflen = sizeof (vp->vp_nvlist); 1264fa9e4066Sahrens 126517f17c2dSbonwick if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 126617f17c2dSbonwick for (; l < VDEV_LABELS; l += 2) { 1267770499e1SDan Kimmel vdev_label_write(zio, vd, l, vp_abd, 126817f17c2dSbonwick offsetof(vdev_label_t, vl_vdev_phys), 126917f17c2dSbonwick sizeof (vdev_phys_t), 1270a3b55830SMatthew Ahrens vdev_label_sync_done, good_writes, 1271e14bb325SJeff Bonwick flags | ZIO_FLAG_DONT_PROPAGATE); 127217f17c2dSbonwick } 127317f17c2dSbonwick } 1274fa9e4066Sahrens 1275770499e1SDan Kimmel abd_free(vp_abd); 1276fa9e4066Sahrens nvlist_free(label); 1277fa9e4066Sahrens } 1278fa9e4066Sahrens 127917f17c2dSbonwick int 1280e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags) 1281fa9e4066Sahrens { 1282e14bb325SJeff Bonwick list_t *dl = &spa->spa_config_dirty_list; 128317f17c2dSbonwick vdev_t *vd; 1284e14bb325SJeff Bonwick zio_t *zio; 1285fa9e4066Sahrens int error; 1286fa9e4066Sahrens 1287fa9e4066Sahrens /* 1288e14bb325SJeff Bonwick * Write the new labels to disk. 1289fa9e4066Sahrens */ 1290e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, flags); 1291fa9e4066Sahrens 129217f17c2dSbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 129317f17c2dSbonwick uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 129417f17c2dSbonwick KM_SLEEP); 129588ecc943SGeorge Wilson 129688ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole); 129788ecc943SGeorge Wilson 1298a3f829aeSBill Moore zio_t *vio = zio_null(zio, spa, NULL, 12990430f8daSeschrock (vd->vdev_islog || vd->vdev_aux != NULL) ? 13000430f8daSeschrock vdev_label_sync_ignore_done : vdev_label_sync_top_done, 130117f17c2dSbonwick good_writes, flags); 1302a3b55830SMatthew Ahrens vdev_label_sync(vio, good_writes, vd, l, txg, flags); 130317f17c2dSbonwick zio_nowait(vio); 1304fa9e4066Sahrens } 1305e14bb325SJeff Bonwick 1306e14bb325SJeff Bonwick error = zio_wait(zio); 1307fa9e4066Sahrens 13088654d025Sperrin /* 130917f17c2dSbonwick * Flush the new labels to disk. 13108654d025Sperrin */ 131117f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags); 13128654d025Sperrin 131317f17c2dSbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 131417f17c2dSbonwick zio_flush(zio, vd); 131517f17c2dSbonwick 131617f17c2dSbonwick (void) zio_wait(zio); 1317fa9e4066Sahrens 1318fa9e4066Sahrens return (error); 1319fa9e4066Sahrens } 1320fa9e4066Sahrens 1321fa9e4066Sahrens /* 132217f17c2dSbonwick * Sync the uberblock and any changes to the vdev configuration. 1323fa9e4066Sahrens * 1324fa9e4066Sahrens * The order of operations is carefully crafted to ensure that 1325fa9e4066Sahrens * if the system panics or loses power at any time, the state on disk 1326fa9e4066Sahrens * is still transactionally consistent. The in-line comments below 1327fa9e4066Sahrens * describe the failure semantics at each stage. 1328fa9e4066Sahrens * 132917f17c2dSbonwick * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 1330fa9e4066Sahrens * at any time, you can just call it again, and it will resume its work. 1331fa9e4066Sahrens */ 1332e14bb325SJeff Bonwick int 1333eb5bb584SWill Andrews vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg) 1334fa9e4066Sahrens { 133517f17c2dSbonwick spa_t *spa = svd[0]->vdev_spa; 1336fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 1337eb5bb584SWill Andrews int error = 0; 1338e14bb325SJeff Bonwick int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 1339fa9e4066Sahrens 134086714001SSerapheim Dimitropoulos ASSERT(svdcount != 0); 1341eb5bb584SWill Andrews retry: 13428956713aSEric Schrock /* 13438956713aSEric Schrock * Normally, we don't want to try too hard to write every label and 13448956713aSEric Schrock * uberblock. If there is a flaky disk, we don't want the rest of the 13458956713aSEric Schrock * sync process to block while we retry. But if we can't write a 13468956713aSEric Schrock * single label out, we should retry with ZIO_FLAG_TRYHARD before 13478956713aSEric Schrock * bailing out and declaring the pool faulted. 13488956713aSEric Schrock */ 1349eb5bb584SWill Andrews if (error != 0) { 1350eb5bb584SWill Andrews if ((flags & ZIO_FLAG_TRYHARD) != 0) 1351eb5bb584SWill Andrews return (error); 13528956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD; 1353eb5bb584SWill Andrews } 13548956713aSEric Schrock 1355fa9e4066Sahrens ASSERT(ub->ub_txg <= txg); 1356fa9e4066Sahrens 1357fa9e4066Sahrens /* 135817f17c2dSbonwick * If this isn't a resync due to I/O errors, 135917f17c2dSbonwick * and nothing changed in this transaction group, 136017f17c2dSbonwick * and the vdev configuration hasn't changed, 13610373e76bSbonwick * then there's nothing to do. 1362fa9e4066Sahrens */ 136317f17c2dSbonwick if (ub->ub_txg < txg && 136417f17c2dSbonwick uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 1365e14bb325SJeff Bonwick list_is_empty(&spa->spa_config_dirty_list)) 1366e14bb325SJeff Bonwick return (0); 1367fa9e4066Sahrens 1368fa9e4066Sahrens if (txg > spa_freeze_txg(spa)) 1369e14bb325SJeff Bonwick return (0); 1370fa9e4066Sahrens 13710373e76bSbonwick ASSERT(txg <= spa->spa_final_txg); 13720373e76bSbonwick 1373fa9e4066Sahrens /* 1374fa9e4066Sahrens * Flush the write cache of every disk that's been written to 1375fa9e4066Sahrens * in this transaction group. This ensures that all blocks 1376fa9e4066Sahrens * written in this txg will be committed to stable storage 1377fa9e4066Sahrens * before any uberblock that references them. 1378fa9e4066Sahrens */ 137986714001SSerapheim Dimitropoulos zio_t *zio = zio_root(spa, NULL, NULL, flags); 138017f17c2dSbonwick 138186714001SSerapheim Dimitropoulos for (vdev_t *vd = 138286714001SSerapheim Dimitropoulos txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL; 138317f17c2dSbonwick vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 138417f17c2dSbonwick zio_flush(zio, vd); 138517f17c2dSbonwick 1386fa9e4066Sahrens (void) zio_wait(zio); 1387fa9e4066Sahrens 1388fa9e4066Sahrens /* 1389fa9e4066Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 1390fa9e4066Sahrens * system dies in the middle of this process, that's OK: all of the 1391fa9e4066Sahrens * even labels that made it to disk will be newer than any uberblock, 1392fa9e4066Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 139317f17c2dSbonwick * which have not yet been touched, will still be valid. We flush 139417f17c2dSbonwick * the new labels to disk to ensure that all even-label updates 139517f17c2dSbonwick * are committed to stable storage before the uberblock update. 1396fa9e4066Sahrens */ 139786714001SSerapheim Dimitropoulos if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) { 139886714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) { 139986714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_label_sync_list() returned error %d " 140086714001SSerapheim Dimitropoulos "for pool '%s' when syncing out the even labels " 140186714001SSerapheim Dimitropoulos "of dirty vdevs", error, spa_name(spa)); 140286714001SSerapheim Dimitropoulos } 1403eb5bb584SWill Andrews goto retry; 140486714001SSerapheim Dimitropoulos } 1405fa9e4066Sahrens 1406fa9e4066Sahrens /* 1407e14bb325SJeff Bonwick * Sync the uberblocks to all vdevs in svd[]. 14080373e76bSbonwick * If the system dies in the middle of this step, there are two cases 14090373e76bSbonwick * to consider, and the on-disk state is consistent either way: 1410fa9e4066Sahrens * 1411fa9e4066Sahrens * (1) If none of the new uberblocks made it to disk, then the 1412fa9e4066Sahrens * previous uberblock will be the newest, and the odd labels 1413fa9e4066Sahrens * (which had not yet been touched) will be valid with respect 1414fa9e4066Sahrens * to that uberblock. 1415fa9e4066Sahrens * 1416fa9e4066Sahrens * (2) If one or more new uberblocks made it to disk, then they 1417fa9e4066Sahrens * will be the newest, and the even labels (which had all 1418fa9e4066Sahrens * been successfully committed) will be valid with respect 1419fa9e4066Sahrens * to the new uberblocks. 1420fa9e4066Sahrens */ 142186714001SSerapheim Dimitropoulos if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) { 142286714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) { 142386714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_uberblock_sync_list() returned error " 142486714001SSerapheim Dimitropoulos "%d for pool '%s'", error, spa_name(spa)); 142586714001SSerapheim Dimitropoulos } 1426eb5bb584SWill Andrews goto retry; 142786714001SSerapheim Dimitropoulos } 1428fa9e4066Sahrens 1429fa9e4066Sahrens /* 1430fa9e4066Sahrens * Sync out odd labels for every dirty vdev. If the system dies 1431fa9e4066Sahrens * in the middle of this process, the even labels and the new 1432fa9e4066Sahrens * uberblocks will suffice to open the pool. The next time 1433fa9e4066Sahrens * the pool is opened, the first thing we'll do -- before any 1434fa9e4066Sahrens * user data is modified -- is mark every vdev dirty so that 143517f17c2dSbonwick * all labels will be brought up to date. We flush the new labels 143617f17c2dSbonwick * to disk to ensure that all odd-label updates are committed to 143717f17c2dSbonwick * stable storage before the next transaction group begins. 1438fa9e4066Sahrens */ 143986714001SSerapheim Dimitropoulos if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) { 144086714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) { 144186714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_label_sync_list() returned error %d " 144286714001SSerapheim Dimitropoulos "for pool '%s' when syncing out the odd labels of " 144386714001SSerapheim Dimitropoulos "dirty vdevs", error, spa_name(spa)); 144486714001SSerapheim Dimitropoulos } 1445eb5bb584SWill Andrews goto retry; 144686714001SSerapheim Dimitropoulos } 1447eb5bb584SWill Andrews 1448eb5bb584SWill Andrews return (0); 1449fa9e4066Sahrens } 1450