1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 223377Seschrock * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens /* 29789Sahrens * Virtual Device Labels 30789Sahrens * --------------------- 31789Sahrens * 32789Sahrens * The vdev label serves several distinct purposes: 33789Sahrens * 34789Sahrens * 1. Uniquely identify this device as part of a ZFS pool and confirm its 35789Sahrens * identity within the pool. 36789Sahrens * 37789Sahrens * 2. Verify that all the devices given in a configuration are present 38789Sahrens * within the pool. 39789Sahrens * 40789Sahrens * 3. Determine the uberblock for the pool. 41789Sahrens * 42789Sahrens * 4. In case of an import operation, determine the configuration of the 43789Sahrens * toplevel vdev of which it is a part. 44789Sahrens * 45789Sahrens * 5. If an import operation cannot find all the devices in the pool, 46789Sahrens * provide enough information to the administrator to determine which 47789Sahrens * devices are missing. 48789Sahrens * 49789Sahrens * It is important to note that while the kernel is responsible for writing the 50789Sahrens * label, it only consumes the information in the first three cases. The 51789Sahrens * latter information is only consumed in userland when determining the 52789Sahrens * configuration to import a pool. 53789Sahrens * 54789Sahrens * 55789Sahrens * Label Organization 56789Sahrens * ------------------ 57789Sahrens * 58789Sahrens * Before describing the contents of the label, it's important to understand how 59789Sahrens * the labels are written and updated with respect to the uberblock. 60789Sahrens * 61789Sahrens * When the pool configuration is altered, either because it was newly created 62789Sahrens * or a device was added, we want to update all the labels such that we can deal 63789Sahrens * with fatal failure at any point. To this end, each disk has two labels which 64789Sahrens * are updated before and after the uberblock is synced. Assuming we have 654451Seschrock * labels and an uberblock with the following transaction groups: 66789Sahrens * 67789Sahrens * L1 UB L2 68789Sahrens * +------+ +------+ +------+ 69789Sahrens * | | | | | | 70789Sahrens * | t10 | | t10 | | t10 | 71789Sahrens * | | | | | | 72789Sahrens * +------+ +------+ +------+ 73789Sahrens * 74789Sahrens * In this stable state, the labels and the uberblock were all updated within 75789Sahrens * the same transaction group (10). Each label is mirrored and checksummed, so 76789Sahrens * that we can detect when we fail partway through writing the label. 77789Sahrens * 78789Sahrens * In order to identify which labels are valid, the labels are written in the 79789Sahrens * following manner: 80789Sahrens * 81789Sahrens * 1. For each vdev, update 'L1' to the new label 82789Sahrens * 2. Update the uberblock 83789Sahrens * 3. For each vdev, update 'L2' to the new label 84789Sahrens * 85789Sahrens * Given arbitrary failure, we can determine the correct label to use based on 86789Sahrens * the transaction group. If we fail after updating L1 but before updating the 87789Sahrens * UB, we will notice that L1's transaction group is greater than the uberblock, 88789Sahrens * so L2 must be valid. If we fail after writing the uberblock but before 89789Sahrens * writing L2, we will notice that L2's transaction group is less than L1, and 90789Sahrens * therefore L1 is valid. 91789Sahrens * 92789Sahrens * Another added complexity is that not every label is updated when the config 93789Sahrens * is synced. If we add a single device, we do not want to have to re-write 94789Sahrens * every label for every device in the pool. This means that both L1 and L2 may 95789Sahrens * be older than the pool uberblock, because the necessary information is stored 96789Sahrens * on another vdev. 97789Sahrens * 98789Sahrens * 99789Sahrens * On-disk Format 100789Sahrens * -------------- 101789Sahrens * 102789Sahrens * The vdev label consists of two distinct parts, and is wrapped within the 103789Sahrens * vdev_label_t structure. The label includes 8k of padding to permit legacy 104789Sahrens * VTOC disk labels, but is otherwise ignored. 105789Sahrens * 106789Sahrens * The first half of the label is a packed nvlist which contains pool wide 107789Sahrens * properties, per-vdev properties, and configuration information. It is 108789Sahrens * described in more detail below. 109789Sahrens * 110789Sahrens * The latter half of the label consists of a redundant array of uberblocks. 111789Sahrens * These uberblocks are updated whenever a transaction group is committed, 112789Sahrens * or when the configuration is updated. When a pool is loaded, we scan each 113789Sahrens * vdev for the 'best' uberblock. 114789Sahrens * 115789Sahrens * 116789Sahrens * Configuration Information 117789Sahrens * ------------------------- 118789Sahrens * 119789Sahrens * The nvlist describing the pool and vdev contains the following elements: 120789Sahrens * 121789Sahrens * version ZFS on-disk version 122789Sahrens * name Pool name 123789Sahrens * state Pool state 124789Sahrens * txg Transaction group in which this label was written 125789Sahrens * pool_guid Unique identifier for this pool 126789Sahrens * vdev_tree An nvlist describing vdev tree. 127789Sahrens * 128789Sahrens * Each leaf device label also contains the following: 129789Sahrens * 130789Sahrens * top_guid Unique ID for top-level vdev in which this is contained 131789Sahrens * guid Unique ID for the leaf vdev 132789Sahrens * 133789Sahrens * The 'vs' configuration follows the format described in 'spa_config.c'. 134789Sahrens */ 135789Sahrens 136789Sahrens #include <sys/zfs_context.h> 137789Sahrens #include <sys/spa.h> 138789Sahrens #include <sys/spa_impl.h> 139789Sahrens #include <sys/dmu.h> 140789Sahrens #include <sys/zap.h> 141789Sahrens #include <sys/vdev.h> 142789Sahrens #include <sys/vdev_impl.h> 143789Sahrens #include <sys/uberblock_impl.h> 144789Sahrens #include <sys/metaslab.h> 145789Sahrens #include <sys/zio.h> 146789Sahrens #include <sys/fs/zfs.h> 147789Sahrens 148789Sahrens /* 149789Sahrens * Basic routines to read and write from a vdev label. 150789Sahrens * Used throughout the rest of this file. 151789Sahrens */ 152789Sahrens uint64_t 153789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset) 154789Sahrens { 1551732Sbonwick ASSERT(offset < sizeof (vdev_label_t)); 1564577Sahrens ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0); 1571732Sbonwick 158789Sahrens return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 159789Sahrens 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 160789Sahrens } 161789Sahrens 162789Sahrens static void 163789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 164789Sahrens uint64_t size, zio_done_func_t *done, void *private) 165789Sahrens { 166789Sahrens ASSERT(vd->vdev_children == 0); 167789Sahrens 168789Sahrens zio_nowait(zio_read_phys(zio, vd, 169789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 170789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 1711544Seschrock ZIO_PRIORITY_SYNC_READ, 1725450Sbrendan ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 1735450Sbrendan B_TRUE)); 174789Sahrens } 175789Sahrens 176789Sahrens static void 177789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 178*5688Sbonwick uint64_t size, zio_done_func_t *done, void *private, int flags) 179789Sahrens { 180789Sahrens ASSERT(vd->vdev_children == 0); 181789Sahrens 182789Sahrens zio_nowait(zio_write_phys(zio, vd, 183789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 184789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 185*5688Sbonwick ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE)); 186789Sahrens } 187789Sahrens 188789Sahrens /* 189789Sahrens * Generate the nvlist representing this vdev's config. 190789Sahrens */ 191789Sahrens nvlist_t * 1922082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 1935450Sbrendan boolean_t isspare, boolean_t isl2cache) 194789Sahrens { 195789Sahrens nvlist_t *nv = NULL; 196789Sahrens 1971544Seschrock VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 198789Sahrens 199789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 200789Sahrens vd->vdev_ops->vdev_op_type) == 0); 2015450Sbrendan if (!isspare && !isl2cache) 2022082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) 2032082Seschrock == 0); 204789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 205789Sahrens 206789Sahrens if (vd->vdev_path != NULL) 207789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 208789Sahrens vd->vdev_path) == 0); 209789Sahrens 210789Sahrens if (vd->vdev_devid != NULL) 211789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 212789Sahrens vd->vdev_devid) == 0); 213789Sahrens 2144451Seschrock if (vd->vdev_physpath != NULL) 2154451Seschrock VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 2164451Seschrock vd->vdev_physpath) == 0); 2174451Seschrock 2182082Seschrock if (vd->vdev_nparity != 0) { 2192082Seschrock ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 2202082Seschrock VDEV_TYPE_RAIDZ) == 0); 2212082Seschrock 2222082Seschrock /* 2232082Seschrock * Make sure someone hasn't managed to sneak a fancy new vdev 2242082Seschrock * into a crufty old storage pool. 2252082Seschrock */ 2262082Seschrock ASSERT(vd->vdev_nparity == 1 || 2272082Seschrock (vd->vdev_nparity == 2 && 2284577Sahrens spa_version(spa) >= SPA_VERSION_RAID6)); 2292082Seschrock 2302082Seschrock /* 2312082Seschrock * Note that we'll add the nparity tag even on storage pools 2322082Seschrock * that only support a single parity device -- older software 2332082Seschrock * will just ignore it. 2342082Seschrock */ 2352082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, 2362082Seschrock vd->vdev_nparity) == 0); 2372082Seschrock } 2382082Seschrock 2391171Seschrock if (vd->vdev_wholedisk != -1ULL) 2401171Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2411171Seschrock vd->vdev_wholedisk) == 0); 2421171Seschrock 2431544Seschrock if (vd->vdev_not_present) 2441544Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 2451544Seschrock 2462082Seschrock if (vd->vdev_isspare) 2472082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0); 2482082Seschrock 2495450Sbrendan if (!isspare && !isl2cache && vd == vd->vdev_top) { 250789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 251789Sahrens vd->vdev_ms_array) == 0); 252789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 253789Sahrens vd->vdev_ms_shift) == 0); 254789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 255789Sahrens vd->vdev_ashift) == 0); 256789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 257789Sahrens vd->vdev_asize) == 0); 2584527Sperrin VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, 2594527Sperrin vd->vdev_islog) == 0); 260789Sahrens } 261789Sahrens 262789Sahrens if (vd->vdev_dtl.smo_object != 0) 263789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 264789Sahrens vd->vdev_dtl.smo_object) == 0); 265789Sahrens 266789Sahrens if (getstats) { 267789Sahrens vdev_stat_t vs; 268789Sahrens vdev_get_stats(vd, &vs); 269789Sahrens VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 270789Sahrens (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 271789Sahrens } 272789Sahrens 273789Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 274789Sahrens nvlist_t **child; 275789Sahrens int c; 276789Sahrens 277789Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 278789Sahrens KM_SLEEP); 279789Sahrens 280789Sahrens for (c = 0; c < vd->vdev_children; c++) 2812082Seschrock child[c] = vdev_config_generate(spa, vd->vdev_child[c], 2825450Sbrendan getstats, isspare, isl2cache); 283789Sahrens 284789Sahrens VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 285789Sahrens child, vd->vdev_children) == 0); 286789Sahrens 287789Sahrens for (c = 0; c < vd->vdev_children; c++) 288789Sahrens nvlist_free(child[c]); 289789Sahrens 290789Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 2911485Slling 2921485Slling } else { 2931732Sbonwick if (vd->vdev_offline && !vd->vdev_tmpoffline) 2941485Slling VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 2951732Sbonwick B_TRUE) == 0); 2964451Seschrock if (vd->vdev_faulted) 2974451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, 2984451Seschrock B_TRUE) == 0); 2994451Seschrock if (vd->vdev_degraded) 3004451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, 3014451Seschrock B_TRUE) == 0); 3024451Seschrock if (vd->vdev_removed) 3034451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, 3044451Seschrock B_TRUE) == 0); 3054451Seschrock if (vd->vdev_unspare) 3064451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, 3074451Seschrock B_TRUE) == 0); 308789Sahrens } 309789Sahrens 310789Sahrens return (nv); 311789Sahrens } 312789Sahrens 313789Sahrens nvlist_t * 314789Sahrens vdev_label_read_config(vdev_t *vd) 315789Sahrens { 3161635Sbonwick spa_t *spa = vd->vdev_spa; 317789Sahrens nvlist_t *config = NULL; 318789Sahrens vdev_phys_t *vp; 319789Sahrens zio_t *zio; 320789Sahrens int l; 321789Sahrens 3224787Sahrens ASSERT(spa_config_held(spa, RW_READER) || 3234787Sahrens spa_config_held(spa, RW_WRITER)); 3241635Sbonwick 3255329Sgw25295 if (!vdev_readable(vd)) 326789Sahrens return (NULL); 327789Sahrens 328789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 329789Sahrens 330789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 331789Sahrens 3321635Sbonwick zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL | 3331544Seschrock ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD); 334789Sahrens 335789Sahrens vdev_label_read(zio, vd, l, vp, 336789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 337789Sahrens sizeof (vdev_phys_t), NULL, NULL); 338789Sahrens 339789Sahrens if (zio_wait(zio) == 0 && 340789Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 3411544Seschrock &config, 0) == 0) 342789Sahrens break; 343789Sahrens 344789Sahrens if (config != NULL) { 345789Sahrens nvlist_free(config); 346789Sahrens config = NULL; 347789Sahrens } 348789Sahrens } 349789Sahrens 350789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 351789Sahrens 352789Sahrens return (config); 353789Sahrens } 354789Sahrens 3553377Seschrock /* 3563377Seschrock * Determine if a device is in use. The 'spare_guid' parameter will be filled 3573377Seschrock * in with the device guid if this spare is active elsewhere on the system. 3583377Seschrock */ 3593377Seschrock static boolean_t 3603377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 3615450Sbrendan uint64_t *spare_guid, uint64_t *l2cache_guid) 3623377Seschrock { 3633377Seschrock spa_t *spa = vd->vdev_spa; 3643377Seschrock uint64_t state, pool_guid, device_guid, txg, spare_pool; 3653377Seschrock uint64_t vdtxg = 0; 3663377Seschrock nvlist_t *label; 3673377Seschrock 3683377Seschrock if (spare_guid) 3693377Seschrock *spare_guid = 0ULL; 3705450Sbrendan if (l2cache_guid) 3715450Sbrendan *l2cache_guid = 0ULL; 3723377Seschrock 3733377Seschrock /* 3743377Seschrock * Read the label, if any, and perform some basic sanity checks. 3753377Seschrock */ 3763377Seschrock if ((label = vdev_label_read_config(vd)) == NULL) 3773377Seschrock return (B_FALSE); 3783377Seschrock 3793377Seschrock (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 3803377Seschrock &vdtxg); 3813377Seschrock 3823377Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 3833377Seschrock &state) != 0 || 3843377Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 3853377Seschrock &device_guid) != 0) { 3863377Seschrock nvlist_free(label); 3873377Seschrock return (B_FALSE); 3883377Seschrock } 3893377Seschrock 3905450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 3913377Seschrock (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 3923377Seschrock &pool_guid) != 0 || 3933377Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 3943377Seschrock &txg) != 0)) { 3953377Seschrock nvlist_free(label); 3963377Seschrock return (B_FALSE); 3973377Seschrock } 3983377Seschrock 3993377Seschrock nvlist_free(label); 4003377Seschrock 4013377Seschrock /* 4023377Seschrock * Check to see if this device indeed belongs to the pool it claims to 4033377Seschrock * be a part of. The only way this is allowed is if the device is a hot 4043377Seschrock * spare (which we check for later on). 4053377Seschrock */ 4065450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4073377Seschrock !spa_guid_exists(pool_guid, device_guid) && 4085450Sbrendan !spa_spare_exists(device_guid, NULL) && 4095450Sbrendan !spa_l2cache_exists(device_guid, NULL)) 4103377Seschrock return (B_FALSE); 4113377Seschrock 4123377Seschrock /* 4133377Seschrock * If the transaction group is zero, then this an initialized (but 4143377Seschrock * unused) label. This is only an error if the create transaction 4153377Seschrock * on-disk is the same as the one we're using now, in which case the 4163377Seschrock * user has attempted to add the same vdev multiple times in the same 4173377Seschrock * transaction. 4183377Seschrock */ 4195450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4205450Sbrendan txg == 0 && vdtxg == crtxg) 4213377Seschrock return (B_TRUE); 4223377Seschrock 4233377Seschrock /* 4243377Seschrock * Check to see if this is a spare device. We do an explicit check for 4253377Seschrock * spa_has_spare() here because it may be on our pending list of spares 4265450Sbrendan * to add. We also check if it is an l2cache device. 4273377Seschrock */ 4283377Seschrock if (spa_spare_exists(device_guid, &spare_pool) || 4293377Seschrock spa_has_spare(spa, device_guid)) { 4303377Seschrock if (spare_guid) 4313377Seschrock *spare_guid = device_guid; 4323377Seschrock 4333377Seschrock switch (reason) { 4343377Seschrock case VDEV_LABEL_CREATE: 4355450Sbrendan case VDEV_LABEL_L2CACHE: 4363377Seschrock return (B_TRUE); 4373377Seschrock 4383377Seschrock case VDEV_LABEL_REPLACE: 4393377Seschrock return (!spa_has_spare(spa, device_guid) || 4403377Seschrock spare_pool != 0ULL); 4413377Seschrock 4423377Seschrock case VDEV_LABEL_SPARE: 4433377Seschrock return (spa_has_spare(spa, device_guid)); 4443377Seschrock } 4453377Seschrock } 4463377Seschrock 4473377Seschrock /* 4485450Sbrendan * Check to see if this is an l2cache device. 4495450Sbrendan */ 4505450Sbrendan if (spa_l2cache_exists(device_guid, NULL)) 4515450Sbrendan return (B_TRUE); 4525450Sbrendan 4535450Sbrendan /* 4543377Seschrock * If the device is marked ACTIVE, then this device is in use by another 4553377Seschrock * pool on the system. 4563377Seschrock */ 4573377Seschrock return (state == POOL_STATE_ACTIVE); 4583377Seschrock } 4593377Seschrock 4603377Seschrock /* 4613377Seschrock * Initialize a vdev label. We check to make sure each leaf device is not in 4623377Seschrock * use, and writable. We put down an initial label which we will later 4633377Seschrock * overwrite with a complete label. Note that it's important to do this 4643377Seschrock * sequentially, not in parallel, so that we catch cases of multiple use of the 4653377Seschrock * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 4663377Seschrock * itself. 4673377Seschrock */ 4683377Seschrock int 4693377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 470789Sahrens { 471789Sahrens spa_t *spa = vd->vdev_spa; 472789Sahrens nvlist_t *label; 473789Sahrens vdev_phys_t *vp; 474789Sahrens vdev_boot_header_t *vb; 4751732Sbonwick uberblock_t *ub; 476789Sahrens zio_t *zio; 477789Sahrens int l, c, n; 478789Sahrens char *buf; 479789Sahrens size_t buflen; 480789Sahrens int error; 4815450Sbrendan uint64_t spare_guid, l2cache_guid; 482*5688Sbonwick int flags = ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL; 483789Sahrens 4841635Sbonwick ASSERT(spa_config_held(spa, RW_WRITER)); 4851635Sbonwick 486789Sahrens for (c = 0; c < vd->vdev_children; c++) 4873377Seschrock if ((error = vdev_label_init(vd->vdev_child[c], 4883377Seschrock crtxg, reason)) != 0) 489789Sahrens return (error); 490789Sahrens 491789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 492789Sahrens return (0); 493789Sahrens 494789Sahrens /* 4953377Seschrock * Dead vdevs cannot be initialized. 496789Sahrens */ 497789Sahrens if (vdev_is_dead(vd)) 498789Sahrens return (EIO); 499789Sahrens 500789Sahrens /* 5013377Seschrock * Determine if the vdev is in use. 502789Sahrens */ 5033377Seschrock if (reason != VDEV_LABEL_REMOVE && 5045450Sbrendan vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 5053377Seschrock return (EBUSY); 506789Sahrens 5073377Seschrock ASSERT(reason != VDEV_LABEL_REMOVE || 5085450Sbrendan vdev_inuse(vd, crtxg, reason, NULL, NULL)); 509789Sahrens 5103377Seschrock /* 5115450Sbrendan * If this is a request to add or replace a spare or l2cache device 5125450Sbrendan * that is in use elsewhere on the system, then we must update the 5135450Sbrendan * guid (which was initialized to a random value) to reflect the 5145450Sbrendan * actual GUID (which is shared between multiple pools). 5153377Seschrock */ 5165450Sbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 5175450Sbrendan spare_guid != 0ULL) { 5183377Seschrock vdev_t *pvd = vd->vdev_parent; 5192082Seschrock 5203377Seschrock for (; pvd != NULL; pvd = pvd->vdev_parent) { 5213377Seschrock pvd->vdev_guid_sum -= vd->vdev_guid; 5223377Seschrock pvd->vdev_guid_sum += spare_guid; 523789Sahrens } 5242082Seschrock 5253377Seschrock vd->vdev_guid = vd->vdev_guid_sum = spare_guid; 5262082Seschrock 5273377Seschrock /* 5283377Seschrock * If this is a replacement, then we want to fallthrough to the 5293377Seschrock * rest of the code. If we're adding a spare, then it's already 5304451Seschrock * labeled appropriately and we can just return. 5313377Seschrock */ 5323377Seschrock if (reason == VDEV_LABEL_SPARE) 5333377Seschrock return (0); 5343377Seschrock ASSERT(reason == VDEV_LABEL_REPLACE); 535789Sahrens } 536789Sahrens 5375450Sbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 5385450Sbrendan l2cache_guid != 0ULL) { 5395450Sbrendan vdev_t *pvd = vd->vdev_parent; 5405450Sbrendan 5415450Sbrendan for (; pvd != NULL; pvd = pvd->vdev_parent) { 5425450Sbrendan pvd->vdev_guid_sum -= vd->vdev_guid; 5435450Sbrendan pvd->vdev_guid_sum += l2cache_guid; 5445450Sbrendan } 5455450Sbrendan 5465450Sbrendan vd->vdev_guid = vd->vdev_guid_sum = l2cache_guid; 5475450Sbrendan 5485450Sbrendan /* 5495450Sbrendan * If this is a replacement, then we want to fallthrough to the 5505450Sbrendan * rest of the code. If we're adding an l2cache, then it's 5515450Sbrendan * already labeled appropriately and we can just return. 5525450Sbrendan */ 5535450Sbrendan if (reason == VDEV_LABEL_L2CACHE) 5545450Sbrendan return (0); 5555450Sbrendan ASSERT(reason == VDEV_LABEL_REPLACE); 5565450Sbrendan } 5575450Sbrendan 558789Sahrens /* 5593377Seschrock * Initialize its label. 560789Sahrens */ 561789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 562789Sahrens bzero(vp, sizeof (vdev_phys_t)); 563789Sahrens 564789Sahrens /* 565789Sahrens * Generate a label describing the pool and our top-level vdev. 566789Sahrens * We mark it as being from txg 0 to indicate that it's not 567789Sahrens * really part of an active pool just yet. The labels will 568789Sahrens * be written again with a meaningful txg by spa_sync(). 569789Sahrens */ 5703377Seschrock if (reason == VDEV_LABEL_SPARE || 5713377Seschrock (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 5723377Seschrock /* 5733377Seschrock * For inactive hot spares, we generate a special label that 5743377Seschrock * identifies as a mutually shared hot spare. We write the 5753377Seschrock * label if we are adding a hot spare, or if we are removing an 5763377Seschrock * active hot spare (in which case we want to revert the 5773377Seschrock * labels). 5783377Seschrock */ 5792082Seschrock VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 580789Sahrens 5812082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 5822082Seschrock spa_version(spa)) == 0); 5832082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 5842082Seschrock POOL_STATE_SPARE) == 0); 5852082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 5862082Seschrock vd->vdev_guid) == 0); 5875450Sbrendan } else if (reason == VDEV_LABEL_L2CACHE || 5885450Sbrendan (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 5895450Sbrendan /* 5905450Sbrendan * For level 2 ARC devices, add a special label. 5915450Sbrendan */ 5925450Sbrendan VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5935450Sbrendan 5945450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 5955450Sbrendan spa_version(spa)) == 0); 5965450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 5975450Sbrendan POOL_STATE_L2CACHE) == 0); 5985450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 5995450Sbrendan vd->vdev_guid) == 0); 6002082Seschrock } else { 6012082Seschrock label = spa_config_generate(spa, vd, 0ULL, B_FALSE); 6022082Seschrock 6032082Seschrock /* 6042082Seschrock * Add our creation time. This allows us to detect multiple 6052082Seschrock * vdev uses as described above, and automatically expires if we 6062082Seschrock * fail. 6072082Seschrock */ 6082082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 6092082Seschrock crtxg) == 0); 6102082Seschrock } 611789Sahrens 612789Sahrens buf = vp->vp_nvlist; 613789Sahrens buflen = sizeof (vp->vp_nvlist); 614789Sahrens 6153460Smmusante error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 6163460Smmusante if (error != 0) { 617789Sahrens nvlist_free(label); 618789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 6193460Smmusante /* EFAULT means nvlist_pack ran out of room */ 6203460Smmusante return (error == EFAULT ? ENAMETOOLONG : EINVAL); 621789Sahrens } 622789Sahrens 623789Sahrens /* 624789Sahrens * Initialize boot block header. 625789Sahrens */ 626789Sahrens vb = zio_buf_alloc(sizeof (vdev_boot_header_t)); 627789Sahrens bzero(vb, sizeof (vdev_boot_header_t)); 628789Sahrens vb->vb_magic = VDEV_BOOT_MAGIC; 629789Sahrens vb->vb_version = VDEV_BOOT_VERSION; 630789Sahrens vb->vb_offset = VDEV_BOOT_OFFSET; 631789Sahrens vb->vb_size = VDEV_BOOT_SIZE; 632789Sahrens 633789Sahrens /* 634789Sahrens * Initialize uberblock template. 635789Sahrens */ 6361732Sbonwick ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 6371732Sbonwick bzero(ub, VDEV_UBERBLOCK_SIZE(vd)); 6381732Sbonwick *ub = spa->spa_uberblock; 6391732Sbonwick ub->ub_txg = 0; 640789Sahrens 641789Sahrens /* 642789Sahrens * Write everything in parallel. 643789Sahrens */ 644*5688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 645789Sahrens 646789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 647789Sahrens 648789Sahrens vdev_label_write(zio, vd, l, vp, 649789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 650*5688Sbonwick sizeof (vdev_phys_t), NULL, NULL, flags); 651789Sahrens 652789Sahrens vdev_label_write(zio, vd, l, vb, 653789Sahrens offsetof(vdev_label_t, vl_boot_header), 654*5688Sbonwick sizeof (vdev_boot_header_t), NULL, NULL, flags); 655789Sahrens 6561732Sbonwick for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 6571732Sbonwick vdev_label_write(zio, vd, l, ub, 6581732Sbonwick VDEV_UBERBLOCK_OFFSET(vd, n), 659*5688Sbonwick VDEV_UBERBLOCK_SIZE(vd), NULL, NULL, flags); 660789Sahrens } 661789Sahrens } 662789Sahrens 663789Sahrens error = zio_wait(zio); 664789Sahrens 665789Sahrens nvlist_free(label); 6661732Sbonwick zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd)); 667789Sahrens zio_buf_free(vb, sizeof (vdev_boot_header_t)); 668789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 669789Sahrens 6703377Seschrock /* 6713377Seschrock * If this vdev hasn't been previously identified as a spare, then we 6724451Seschrock * mark it as such only if a) we are labeling it as a spare, or b) it 6735450Sbrendan * exists as a spare elsewhere in the system. Do the same for 6745450Sbrendan * level 2 ARC devices. 6753377Seschrock */ 6763377Seschrock if (error == 0 && !vd->vdev_isspare && 6773377Seschrock (reason == VDEV_LABEL_SPARE || 6783377Seschrock spa_spare_exists(vd->vdev_guid, NULL))) 6793377Seschrock spa_spare_add(vd); 6802082Seschrock 6815450Sbrendan if (error == 0 && !vd->vdev_isl2cache && 6825450Sbrendan (reason == VDEV_LABEL_L2CACHE || 6835450Sbrendan spa_l2cache_exists(vd->vdev_guid, NULL))) 6845450Sbrendan spa_l2cache_add(vd); 6855450Sbrendan 6863377Seschrock return (error); 6872082Seschrock } 6882082Seschrock 689789Sahrens /* 690789Sahrens * ========================================================================== 691789Sahrens * uberblock load/sync 692789Sahrens * ========================================================================== 693789Sahrens */ 694789Sahrens 695789Sahrens /* 696789Sahrens * Consider the following situation: txg is safely synced to disk. We've 697789Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 698789Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 699789Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 700789Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 701789Sahrens * the missing replica comes back, then for a new seconds we'll have two 702789Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 703789Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 704789Sahrens */ 705789Sahrens static int 706789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 707789Sahrens { 708789Sahrens if (ub1->ub_txg < ub2->ub_txg) 709789Sahrens return (-1); 710789Sahrens if (ub1->ub_txg > ub2->ub_txg) 711789Sahrens return (1); 712789Sahrens 713789Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 714789Sahrens return (-1); 715789Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 716789Sahrens return (1); 717789Sahrens 718789Sahrens return (0); 719789Sahrens } 720789Sahrens 721789Sahrens static void 722789Sahrens vdev_uberblock_load_done(zio_t *zio) 723789Sahrens { 7241732Sbonwick uberblock_t *ub = zio->io_data; 725789Sahrens uberblock_t *ubbest = zio->io_private; 726789Sahrens spa_t *spa = zio->io_spa; 727789Sahrens 7281732Sbonwick ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd)); 729789Sahrens 7301544Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 731789Sahrens mutex_enter(&spa->spa_uberblock_lock); 732789Sahrens if (vdev_uberblock_compare(ub, ubbest) > 0) 733789Sahrens *ubbest = *ub; 734789Sahrens mutex_exit(&spa->spa_uberblock_lock); 735789Sahrens } 736789Sahrens 737789Sahrens zio_buf_free(zio->io_data, zio->io_size); 738789Sahrens } 739789Sahrens 740789Sahrens void 741789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 742789Sahrens { 743789Sahrens int l, c, n; 744789Sahrens 745789Sahrens for (c = 0; c < vd->vdev_children; c++) 746789Sahrens vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 747789Sahrens 748789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 749789Sahrens return; 750789Sahrens 751789Sahrens if (vdev_is_dead(vd)) 752789Sahrens return; 753789Sahrens 754789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 7551732Sbonwick for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 756789Sahrens vdev_label_read(zio, vd, l, 7571732Sbonwick zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 7581732Sbonwick VDEV_UBERBLOCK_OFFSET(vd, n), 7591732Sbonwick VDEV_UBERBLOCK_SIZE(vd), 760789Sahrens vdev_uberblock_load_done, ubbest); 761789Sahrens } 762789Sahrens } 763789Sahrens } 764789Sahrens 765789Sahrens /* 766*5688Sbonwick * On success, increment root zio's count of good writes. 7671635Sbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 768789Sahrens */ 769789Sahrens static void 770789Sahrens vdev_uberblock_sync_done(zio_t *zio) 771789Sahrens { 772*5688Sbonwick uint64_t *good_writes = zio->io_private; 773789Sahrens 7741635Sbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 775789Sahrens atomic_add_64(good_writes, 1); 776789Sahrens } 777789Sahrens 778*5688Sbonwick /* 779*5688Sbonwick * Write the uberblock to all labels of all leaves of the specified vdev. 780*5688Sbonwick */ 781789Sahrens static void 782*5688Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd) 783789Sahrens { 784789Sahrens int l, c, n; 785*5688Sbonwick uberblock_t *ubbuf; 786789Sahrens 787789Sahrens for (c = 0; c < vd->vdev_children; c++) 788*5688Sbonwick vdev_uberblock_sync(zio, ub, vd->vdev_child[c]); 789789Sahrens 790789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 791789Sahrens return; 792789Sahrens 793789Sahrens if (vdev_is_dead(vd)) 794789Sahrens return; 795789Sahrens 796*5688Sbonwick n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 797789Sahrens 798*5688Sbonwick ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 799*5688Sbonwick bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 8001732Sbonwick *ubbuf = *ub; 801789Sahrens 802*5688Sbonwick for (l = 0; l < VDEV_LABELS; l++) 803*5688Sbonwick vdev_label_write(zio, vd, l, ubbuf, 804*5688Sbonwick VDEV_UBERBLOCK_OFFSET(vd, n), 805*5688Sbonwick VDEV_UBERBLOCK_SIZE(vd), 806*5688Sbonwick vdev_uberblock_sync_done, zio->io_private, 807*5688Sbonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE); 808789Sahrens 809*5688Sbonwick zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 810*5688Sbonwick } 811789Sahrens 812*5688Sbonwick int 813*5688Sbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 814*5688Sbonwick { 815*5688Sbonwick spa_t *spa = svd[0]->vdev_spa; 816*5688Sbonwick int v; 817*5688Sbonwick zio_t *zio; 818*5688Sbonwick uint64_t good_writes = 0; 819789Sahrens 820*5688Sbonwick zio = zio_root(spa, NULL, &good_writes, flags); 821*5688Sbonwick 822*5688Sbonwick for (v = 0; v < svdcount; v++) 823*5688Sbonwick vdev_uberblock_sync(zio, ub, svd[v]); 824*5688Sbonwick 825*5688Sbonwick (void) zio_wait(zio); 826789Sahrens 827789Sahrens /* 828*5688Sbonwick * Flush the uberblocks to disk. This ensures that the odd labels 829*5688Sbonwick * are no longer needed (because the new uberblocks and the even 830*5688Sbonwick * labels are safely on disk), so it is safe to overwrite them. 831789Sahrens */ 832*5688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 833789Sahrens 834*5688Sbonwick for (v = 0; v < svdcount; v++) 835*5688Sbonwick zio_flush(zio, svd[v]); 836789Sahrens 837*5688Sbonwick (void) zio_wait(zio); 838*5688Sbonwick 839*5688Sbonwick return (good_writes >= 1 ? 0 : EIO); 840789Sahrens } 841789Sahrens 842789Sahrens /* 843*5688Sbonwick * On success, increment the count of good writes for our top-level vdev. 844789Sahrens */ 845789Sahrens static void 846*5688Sbonwick vdev_label_sync_done(zio_t *zio) 847789Sahrens { 848*5688Sbonwick uint64_t *good_writes = zio->io_private; 849789Sahrens 850789Sahrens if (zio->io_error == 0) 851789Sahrens atomic_add_64(good_writes, 1); 852789Sahrens } 853789Sahrens 854*5688Sbonwick /* 855*5688Sbonwick * If there weren't enough good writes, indicate failure to the parent. 856*5688Sbonwick */ 857789Sahrens static void 858*5688Sbonwick vdev_label_sync_top_done(zio_t *zio) 859*5688Sbonwick { 860*5688Sbonwick uint64_t *good_writes = zio->io_private; 861*5688Sbonwick 862*5688Sbonwick if (*good_writes == 0) 863*5688Sbonwick zio->io_error = EIO; 864*5688Sbonwick 865*5688Sbonwick kmem_free(good_writes, sizeof (uint64_t)); 866*5688Sbonwick } 867*5688Sbonwick 868*5688Sbonwick /* 869*5688Sbonwick * Write all even or odd labels to all leaves of the specified vdev. 870*5688Sbonwick */ 871*5688Sbonwick static void 872*5688Sbonwick vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg) 873789Sahrens { 874789Sahrens nvlist_t *label; 875789Sahrens vdev_phys_t *vp; 876789Sahrens char *buf; 877789Sahrens size_t buflen; 878789Sahrens int c; 879789Sahrens 880789Sahrens for (c = 0; c < vd->vdev_children; c++) 881*5688Sbonwick vdev_label_sync(zio, vd->vdev_child[c], l, txg); 882789Sahrens 883789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 884789Sahrens return; 885789Sahrens 886789Sahrens if (vdev_is_dead(vd)) 887789Sahrens return; 888789Sahrens 889789Sahrens /* 890789Sahrens * Generate a label describing the top-level config to which we belong. 891789Sahrens */ 8921635Sbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 893789Sahrens 894789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 895789Sahrens bzero(vp, sizeof (vdev_phys_t)); 896789Sahrens 897789Sahrens buf = vp->vp_nvlist; 898789Sahrens buflen = sizeof (vp->vp_nvlist); 899789Sahrens 900*5688Sbonwick if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 901*5688Sbonwick for (; l < VDEV_LABELS; l += 2) { 902*5688Sbonwick vdev_label_write(zio, vd, l, vp, 903*5688Sbonwick offsetof(vdev_label_t, vl_vdev_phys), 904*5688Sbonwick sizeof (vdev_phys_t), 905*5688Sbonwick vdev_label_sync_done, zio->io_private, 906*5688Sbonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE); 907*5688Sbonwick } 908*5688Sbonwick } 909789Sahrens 910789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 911789Sahrens nvlist_free(label); 912789Sahrens } 913789Sahrens 914*5688Sbonwick int 915*5688Sbonwick vdev_label_sync_list(spa_t *spa, int l, int flags, uint64_t txg) 916789Sahrens { 917*5688Sbonwick list_t *dl = &spa->spa_dirty_list; 918*5688Sbonwick vdev_t *vd; 919789Sahrens zio_t *zio; 920789Sahrens int error; 921789Sahrens 922*5688Sbonwick /* 923*5688Sbonwick * Write the new labels to disk. 924*5688Sbonwick */ 925*5688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 926789Sahrens 927*5688Sbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 928*5688Sbonwick uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 929*5688Sbonwick KM_SLEEP); 930*5688Sbonwick zio_t *vio = zio_null(zio, spa, vdev_label_sync_top_done, 931*5688Sbonwick good_writes, flags); 932*5688Sbonwick vdev_label_sync(vio, vd, l, txg); 933*5688Sbonwick zio_nowait(vio); 934*5688Sbonwick } 935789Sahrens 936789Sahrens error = zio_wait(zio); 937789Sahrens 938*5688Sbonwick /* 939*5688Sbonwick * Flush the new labels to disk. 940*5688Sbonwick */ 941*5688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 942789Sahrens 943*5688Sbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 944*5688Sbonwick zio_flush(zio, vd); 9454527Sperrin 946*5688Sbonwick (void) zio_wait(zio); 947789Sahrens 948789Sahrens return (error); 949789Sahrens } 950789Sahrens 951789Sahrens /* 952*5688Sbonwick * Sync the uberblock and any changes to the vdev configuration. 953789Sahrens * 954789Sahrens * The order of operations is carefully crafted to ensure that 955789Sahrens * if the system panics or loses power at any time, the state on disk 956789Sahrens * is still transactionally consistent. The in-line comments below 957789Sahrens * describe the failure semantics at each stage. 958789Sahrens * 959*5688Sbonwick * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 960789Sahrens * at any time, you can just call it again, and it will resume its work. 961789Sahrens */ 962789Sahrens int 963*5688Sbonwick vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg) 964789Sahrens { 965*5688Sbonwick spa_t *spa = svd[0]->vdev_spa; 966789Sahrens uberblock_t *ub = &spa->spa_uberblock; 9671635Sbonwick vdev_t *vd; 968789Sahrens zio_t *zio; 969*5688Sbonwick int error; 970*5688Sbonwick int flags = ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL; 971789Sahrens 972789Sahrens ASSERT(ub->ub_txg <= txg); 973789Sahrens 974789Sahrens /* 975*5688Sbonwick * If this isn't a resync due to I/O errors, 976*5688Sbonwick * and nothing changed in this transaction group, 977*5688Sbonwick * and the vdev configuration hasn't changed, 9781635Sbonwick * then there's nothing to do. 979789Sahrens */ 980*5688Sbonwick if (ub->ub_txg < txg && 981*5688Sbonwick uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 982*5688Sbonwick list_is_empty(&spa->spa_dirty_list)) 983789Sahrens return (0); 984789Sahrens 985789Sahrens if (txg > spa_freeze_txg(spa)) 986789Sahrens return (0); 987789Sahrens 9881635Sbonwick ASSERT(txg <= spa->spa_final_txg); 9891635Sbonwick 990789Sahrens /* 991789Sahrens * Flush the write cache of every disk that's been written to 992789Sahrens * in this transaction group. This ensures that all blocks 993789Sahrens * written in this txg will be committed to stable storage 994789Sahrens * before any uberblock that references them. 995789Sahrens */ 996*5688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 997*5688Sbonwick 998789Sahrens for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 999*5688Sbonwick vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 1000*5688Sbonwick zio_flush(zio, vd); 1001*5688Sbonwick 1002789Sahrens (void) zio_wait(zio); 1003789Sahrens 1004789Sahrens /* 1005789Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 1006789Sahrens * system dies in the middle of this process, that's OK: all of the 1007789Sahrens * even labels that made it to disk will be newer than any uberblock, 1008789Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 1009*5688Sbonwick * which have not yet been touched, will still be valid. We flush 1010*5688Sbonwick * the new labels to disk to ensure that all even-label updates 1011*5688Sbonwick * are committed to stable storage before the uberblock update. 1012789Sahrens */ 1013*5688Sbonwick if ((error = vdev_label_sync_list(spa, 0, flags, txg)) != 0) 1014*5688Sbonwick return (error); 1015789Sahrens 1016789Sahrens /* 1017*5688Sbonwick * Sync the uberblocks to all vdevs in svd[]. 10181635Sbonwick * If the system dies in the middle of this step, there are two cases 10191635Sbonwick * to consider, and the on-disk state is consistent either way: 1020789Sahrens * 1021789Sahrens * (1) If none of the new uberblocks made it to disk, then the 1022789Sahrens * previous uberblock will be the newest, and the odd labels 1023789Sahrens * (which had not yet been touched) will be valid with respect 1024789Sahrens * to that uberblock. 1025789Sahrens * 1026789Sahrens * (2) If one or more new uberblocks made it to disk, then they 1027789Sahrens * will be the newest, and the even labels (which had all 1028789Sahrens * been successfully committed) will be valid with respect 1029789Sahrens * to the new uberblocks. 1030789Sahrens */ 1031*5688Sbonwick if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) 1032789Sahrens return (error); 1033789Sahrens 1034789Sahrens /* 1035789Sahrens * Sync out odd labels for every dirty vdev. If the system dies 1036789Sahrens * in the middle of this process, the even labels and the new 1037789Sahrens * uberblocks will suffice to open the pool. The next time 1038789Sahrens * the pool is opened, the first thing we'll do -- before any 1039789Sahrens * user data is modified -- is mark every vdev dirty so that 1040*5688Sbonwick * all labels will be brought up to date. We flush the new labels 1041*5688Sbonwick * to disk to ensure that all odd-label updates are committed to 1042*5688Sbonwick * stable storage before the next transaction group begins. 1043789Sahrens */ 1044*5688Sbonwick return (vdev_label_sync_list(spa, 1, flags, txg)); 1045789Sahrens } 1046