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 /* 226615Sgw25295 * Copyright 2008 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 1626615Sgw25295 /* 1636615Sgw25295 * Returns back the vdev label associated with the passed in offset. 1646615Sgw25295 */ 1656615Sgw25295 int 1666615Sgw25295 vdev_label_number(uint64_t psize, uint64_t offset) 1676615Sgw25295 { 1686615Sgw25295 int l; 1696615Sgw25295 1706615Sgw25295 if (offset >= psize - VDEV_LABEL_END_SIZE) { 1716615Sgw25295 offset -= psize - VDEV_LABEL_END_SIZE; 1726615Sgw25295 offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t); 1736615Sgw25295 } 1746615Sgw25295 l = offset / sizeof (vdev_label_t); 1756615Sgw25295 return (l < VDEV_LABELS ? l : -1); 1766615Sgw25295 } 1776615Sgw25295 178789Sahrens static void 179789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 180789Sahrens uint64_t size, zio_done_func_t *done, void *private) 181789Sahrens { 182789Sahrens ASSERT(vd->vdev_children == 0); 183789Sahrens 184789Sahrens zio_nowait(zio_read_phys(zio, vd, 185789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 186789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 1871544Seschrock ZIO_PRIORITY_SYNC_READ, 1885450Sbrendan ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 1895450Sbrendan B_TRUE)); 190789Sahrens } 191789Sahrens 192789Sahrens static void 193789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 1945688Sbonwick uint64_t size, zio_done_func_t *done, void *private, int flags) 195789Sahrens { 196789Sahrens ASSERT(vd->vdev_children == 0); 197789Sahrens 198789Sahrens zio_nowait(zio_write_phys(zio, vd, 199789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 200789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 2015688Sbonwick ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE)); 202789Sahrens } 203789Sahrens 204789Sahrens /* 205789Sahrens * Generate the nvlist representing this vdev's config. 206789Sahrens */ 207789Sahrens nvlist_t * 2082082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 2095450Sbrendan boolean_t isspare, boolean_t isl2cache) 210789Sahrens { 211789Sahrens nvlist_t *nv = NULL; 212789Sahrens 2131544Seschrock VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 214789Sahrens 215789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 216789Sahrens vd->vdev_ops->vdev_op_type) == 0); 2175450Sbrendan if (!isspare && !isl2cache) 2182082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) 2192082Seschrock == 0); 220789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 221789Sahrens 222789Sahrens if (vd->vdev_path != NULL) 223789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 224789Sahrens vd->vdev_path) == 0); 225789Sahrens 226789Sahrens if (vd->vdev_devid != NULL) 227789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 228789Sahrens vd->vdev_devid) == 0); 229789Sahrens 2304451Seschrock if (vd->vdev_physpath != NULL) 2314451Seschrock VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 2324451Seschrock vd->vdev_physpath) == 0); 2334451Seschrock 2342082Seschrock if (vd->vdev_nparity != 0) { 2352082Seschrock ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 2362082Seschrock VDEV_TYPE_RAIDZ) == 0); 2372082Seschrock 2382082Seschrock /* 2392082Seschrock * Make sure someone hasn't managed to sneak a fancy new vdev 2402082Seschrock * into a crufty old storage pool. 2412082Seschrock */ 2422082Seschrock ASSERT(vd->vdev_nparity == 1 || 2432082Seschrock (vd->vdev_nparity == 2 && 2444577Sahrens spa_version(spa) >= SPA_VERSION_RAID6)); 2452082Seschrock 2462082Seschrock /* 2472082Seschrock * Note that we'll add the nparity tag even on storage pools 2482082Seschrock * that only support a single parity device -- older software 2492082Seschrock * will just ignore it. 2502082Seschrock */ 2512082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, 2522082Seschrock vd->vdev_nparity) == 0); 2532082Seschrock } 2542082Seschrock 2551171Seschrock if (vd->vdev_wholedisk != -1ULL) 2561171Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2571171Seschrock vd->vdev_wholedisk) == 0); 2581171Seschrock 2591544Seschrock if (vd->vdev_not_present) 2601544Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 2611544Seschrock 2622082Seschrock if (vd->vdev_isspare) 2632082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0); 2642082Seschrock 2655450Sbrendan if (!isspare && !isl2cache && vd == vd->vdev_top) { 266789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 267789Sahrens vd->vdev_ms_array) == 0); 268789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 269789Sahrens vd->vdev_ms_shift) == 0); 270789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 271789Sahrens vd->vdev_ashift) == 0); 272789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 273789Sahrens vd->vdev_asize) == 0); 2744527Sperrin VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, 2754527Sperrin vd->vdev_islog) == 0); 276789Sahrens } 277789Sahrens 278789Sahrens if (vd->vdev_dtl.smo_object != 0) 279789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 280789Sahrens vd->vdev_dtl.smo_object) == 0); 281789Sahrens 282789Sahrens if (getstats) { 283789Sahrens vdev_stat_t vs; 284789Sahrens vdev_get_stats(vd, &vs); 285789Sahrens VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 286789Sahrens (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 287789Sahrens } 288789Sahrens 289789Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 290789Sahrens nvlist_t **child; 291789Sahrens int c; 292789Sahrens 293789Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 294789Sahrens KM_SLEEP); 295789Sahrens 296789Sahrens for (c = 0; c < vd->vdev_children; c++) 2972082Seschrock child[c] = vdev_config_generate(spa, vd->vdev_child[c], 2985450Sbrendan getstats, isspare, isl2cache); 299789Sahrens 300789Sahrens VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 301789Sahrens child, vd->vdev_children) == 0); 302789Sahrens 303789Sahrens for (c = 0; c < vd->vdev_children; c++) 304789Sahrens nvlist_free(child[c]); 305789Sahrens 306789Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 3071485Slling 3081485Slling } else { 3091732Sbonwick if (vd->vdev_offline && !vd->vdev_tmpoffline) 3101485Slling VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 3111732Sbonwick B_TRUE) == 0); 3124451Seschrock if (vd->vdev_faulted) 3134451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, 3144451Seschrock B_TRUE) == 0); 3154451Seschrock if (vd->vdev_degraded) 3164451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, 3174451Seschrock B_TRUE) == 0); 3184451Seschrock if (vd->vdev_removed) 3194451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, 3204451Seschrock B_TRUE) == 0); 3214451Seschrock if (vd->vdev_unspare) 3224451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, 3234451Seschrock B_TRUE) == 0); 324789Sahrens } 325789Sahrens 326789Sahrens return (nv); 327789Sahrens } 328789Sahrens 329789Sahrens nvlist_t * 330789Sahrens vdev_label_read_config(vdev_t *vd) 331789Sahrens { 3321635Sbonwick spa_t *spa = vd->vdev_spa; 333789Sahrens nvlist_t *config = NULL; 334789Sahrens vdev_phys_t *vp; 335789Sahrens zio_t *zio; 336789Sahrens int l; 337789Sahrens 3384787Sahrens ASSERT(spa_config_held(spa, RW_READER) || 3394787Sahrens spa_config_held(spa, RW_WRITER)); 3401635Sbonwick 3415329Sgw25295 if (!vdev_readable(vd)) 342789Sahrens return (NULL); 343789Sahrens 344789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 345789Sahrens 346789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 347789Sahrens 3481635Sbonwick zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL | 3491544Seschrock ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD); 350789Sahrens 351789Sahrens vdev_label_read(zio, vd, l, vp, 352789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 353789Sahrens sizeof (vdev_phys_t), NULL, NULL); 354789Sahrens 355789Sahrens if (zio_wait(zio) == 0 && 356789Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 3571544Seschrock &config, 0) == 0) 358789Sahrens break; 359789Sahrens 360789Sahrens if (config != NULL) { 361789Sahrens nvlist_free(config); 362789Sahrens config = NULL; 363789Sahrens } 364789Sahrens } 365789Sahrens 366789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 367789Sahrens 368789Sahrens return (config); 369789Sahrens } 370789Sahrens 3713377Seschrock /* 3723377Seschrock * Determine if a device is in use. The 'spare_guid' parameter will be filled 3733377Seschrock * in with the device guid if this spare is active elsewhere on the system. 3743377Seschrock */ 3753377Seschrock static boolean_t 3763377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 3775450Sbrendan uint64_t *spare_guid, uint64_t *l2cache_guid) 3783377Seschrock { 3793377Seschrock spa_t *spa = vd->vdev_spa; 3803377Seschrock uint64_t state, pool_guid, device_guid, txg, spare_pool; 3813377Seschrock uint64_t vdtxg = 0; 3823377Seschrock nvlist_t *label; 3833377Seschrock 3843377Seschrock if (spare_guid) 3853377Seschrock *spare_guid = 0ULL; 3865450Sbrendan if (l2cache_guid) 3875450Sbrendan *l2cache_guid = 0ULL; 3883377Seschrock 3893377Seschrock /* 3903377Seschrock * Read the label, if any, and perform some basic sanity checks. 3913377Seschrock */ 3923377Seschrock if ((label = vdev_label_read_config(vd)) == NULL) 3933377Seschrock return (B_FALSE); 3943377Seschrock 3953377Seschrock (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 3963377Seschrock &vdtxg); 3973377Seschrock 3983377Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 3993377Seschrock &state) != 0 || 4003377Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 4013377Seschrock &device_guid) != 0) { 4023377Seschrock nvlist_free(label); 4033377Seschrock return (B_FALSE); 4043377Seschrock } 4053377Seschrock 4065450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4073377Seschrock (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 4083377Seschrock &pool_guid) != 0 || 4093377Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 4103377Seschrock &txg) != 0)) { 4113377Seschrock nvlist_free(label); 4123377Seschrock return (B_FALSE); 4133377Seschrock } 4143377Seschrock 4153377Seschrock nvlist_free(label); 4163377Seschrock 4173377Seschrock /* 4183377Seschrock * Check to see if this device indeed belongs to the pool it claims to 4193377Seschrock * be a part of. The only way this is allowed is if the device is a hot 4203377Seschrock * spare (which we check for later on). 4213377Seschrock */ 4225450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4233377Seschrock !spa_guid_exists(pool_guid, device_guid) && 4245450Sbrendan !spa_spare_exists(device_guid, NULL) && 4255450Sbrendan !spa_l2cache_exists(device_guid, NULL)) 4263377Seschrock return (B_FALSE); 4273377Seschrock 4283377Seschrock /* 4293377Seschrock * If the transaction group is zero, then this an initialized (but 4303377Seschrock * unused) label. This is only an error if the create transaction 4313377Seschrock * on-disk is the same as the one we're using now, in which case the 4323377Seschrock * user has attempted to add the same vdev multiple times in the same 4333377Seschrock * transaction. 4343377Seschrock */ 4355450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4365450Sbrendan txg == 0 && vdtxg == crtxg) 4373377Seschrock return (B_TRUE); 4383377Seschrock 4393377Seschrock /* 4403377Seschrock * Check to see if this is a spare device. We do an explicit check for 4413377Seschrock * spa_has_spare() here because it may be on our pending list of spares 4425450Sbrendan * to add. We also check if it is an l2cache device. 4433377Seschrock */ 4443377Seschrock if (spa_spare_exists(device_guid, &spare_pool) || 4453377Seschrock spa_has_spare(spa, device_guid)) { 4463377Seschrock if (spare_guid) 4473377Seschrock *spare_guid = device_guid; 4483377Seschrock 4493377Seschrock switch (reason) { 4503377Seschrock case VDEV_LABEL_CREATE: 4515450Sbrendan case VDEV_LABEL_L2CACHE: 4523377Seschrock return (B_TRUE); 4533377Seschrock 4543377Seschrock case VDEV_LABEL_REPLACE: 4553377Seschrock return (!spa_has_spare(spa, device_guid) || 4563377Seschrock spare_pool != 0ULL); 4573377Seschrock 4583377Seschrock case VDEV_LABEL_SPARE: 4593377Seschrock return (spa_has_spare(spa, device_guid)); 4603377Seschrock } 4613377Seschrock } 4623377Seschrock 4633377Seschrock /* 4645450Sbrendan * Check to see if this is an l2cache device. 4655450Sbrendan */ 4665450Sbrendan if (spa_l2cache_exists(device_guid, NULL)) 4675450Sbrendan return (B_TRUE); 4685450Sbrendan 4695450Sbrendan /* 4703377Seschrock * If the device is marked ACTIVE, then this device is in use by another 4713377Seschrock * pool on the system. 4723377Seschrock */ 4733377Seschrock return (state == POOL_STATE_ACTIVE); 4743377Seschrock } 4753377Seschrock 4763377Seschrock /* 4773377Seschrock * Initialize a vdev label. We check to make sure each leaf device is not in 4783377Seschrock * use, and writable. We put down an initial label which we will later 4793377Seschrock * overwrite with a complete label. Note that it's important to do this 4803377Seschrock * sequentially, not in parallel, so that we catch cases of multiple use of the 4813377Seschrock * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 4823377Seschrock * itself. 4833377Seschrock */ 4843377Seschrock int 4853377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 486789Sahrens { 487789Sahrens spa_t *spa = vd->vdev_spa; 488789Sahrens nvlist_t *label; 489789Sahrens vdev_phys_t *vp; 490789Sahrens vdev_boot_header_t *vb; 4911732Sbonwick uberblock_t *ub; 492789Sahrens zio_t *zio; 493789Sahrens int l, c, n; 494789Sahrens char *buf; 495789Sahrens size_t buflen; 496789Sahrens int error; 4975450Sbrendan uint64_t spare_guid, l2cache_guid; 4985688Sbonwick int flags = ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL; 499789Sahrens 5001635Sbonwick ASSERT(spa_config_held(spa, RW_WRITER)); 5011635Sbonwick 502789Sahrens for (c = 0; c < vd->vdev_children; c++) 5033377Seschrock if ((error = vdev_label_init(vd->vdev_child[c], 5043377Seschrock crtxg, reason)) != 0) 505789Sahrens return (error); 506789Sahrens 507789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 508789Sahrens return (0); 509789Sahrens 510789Sahrens /* 5113377Seschrock * Dead vdevs cannot be initialized. 512789Sahrens */ 513789Sahrens if (vdev_is_dead(vd)) 514789Sahrens return (EIO); 515789Sahrens 516789Sahrens /* 5173377Seschrock * Determine if the vdev is in use. 518789Sahrens */ 5193377Seschrock if (reason != VDEV_LABEL_REMOVE && 5205450Sbrendan vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 5213377Seschrock return (EBUSY); 522789Sahrens 5233377Seschrock ASSERT(reason != VDEV_LABEL_REMOVE || 5245450Sbrendan vdev_inuse(vd, crtxg, reason, NULL, NULL)); 525789Sahrens 5263377Seschrock /* 5275450Sbrendan * If this is a request to add or replace a spare or l2cache device 5285450Sbrendan * that is in use elsewhere on the system, then we must update the 5295450Sbrendan * guid (which was initialized to a random value) to reflect the 5305450Sbrendan * actual GUID (which is shared between multiple pools). 5313377Seschrock */ 5325450Sbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 5335450Sbrendan spare_guid != 0ULL) { 5343377Seschrock vdev_t *pvd = vd->vdev_parent; 5352082Seschrock 5363377Seschrock for (; pvd != NULL; pvd = pvd->vdev_parent) { 5373377Seschrock pvd->vdev_guid_sum -= vd->vdev_guid; 5383377Seschrock pvd->vdev_guid_sum += spare_guid; 539789Sahrens } 5402082Seschrock 5413377Seschrock vd->vdev_guid = vd->vdev_guid_sum = spare_guid; 5422082Seschrock 5433377Seschrock /* 5443377Seschrock * If this is a replacement, then we want to fallthrough to the 5453377Seschrock * rest of the code. If we're adding a spare, then it's already 5464451Seschrock * labeled appropriately and we can just return. 5473377Seschrock */ 5483377Seschrock if (reason == VDEV_LABEL_SPARE) 5493377Seschrock return (0); 5503377Seschrock ASSERT(reason == VDEV_LABEL_REPLACE); 551789Sahrens } 552789Sahrens 5535450Sbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 5545450Sbrendan l2cache_guid != 0ULL) { 5555450Sbrendan vdev_t *pvd = vd->vdev_parent; 5565450Sbrendan 5575450Sbrendan for (; pvd != NULL; pvd = pvd->vdev_parent) { 5585450Sbrendan pvd->vdev_guid_sum -= vd->vdev_guid; 5595450Sbrendan pvd->vdev_guid_sum += l2cache_guid; 5605450Sbrendan } 5615450Sbrendan 5625450Sbrendan vd->vdev_guid = vd->vdev_guid_sum = l2cache_guid; 5635450Sbrendan 5645450Sbrendan /* 5655450Sbrendan * If this is a replacement, then we want to fallthrough to the 5665450Sbrendan * rest of the code. If we're adding an l2cache, then it's 5675450Sbrendan * already labeled appropriately and we can just return. 5685450Sbrendan */ 5695450Sbrendan if (reason == VDEV_LABEL_L2CACHE) 5705450Sbrendan return (0); 5715450Sbrendan ASSERT(reason == VDEV_LABEL_REPLACE); 5725450Sbrendan } 5735450Sbrendan 574789Sahrens /* 5753377Seschrock * Initialize its label. 576789Sahrens */ 577789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 578789Sahrens bzero(vp, sizeof (vdev_phys_t)); 579789Sahrens 580789Sahrens /* 581789Sahrens * Generate a label describing the pool and our top-level vdev. 582789Sahrens * We mark it as being from txg 0 to indicate that it's not 583789Sahrens * really part of an active pool just yet. The labels will 584789Sahrens * be written again with a meaningful txg by spa_sync(). 585789Sahrens */ 5863377Seschrock if (reason == VDEV_LABEL_SPARE || 5873377Seschrock (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 5883377Seschrock /* 5893377Seschrock * For inactive hot spares, we generate a special label that 5903377Seschrock * identifies as a mutually shared hot spare. We write the 5913377Seschrock * label if we are adding a hot spare, or if we are removing an 5923377Seschrock * active hot spare (in which case we want to revert the 5933377Seschrock * labels). 5943377Seschrock */ 5952082Seschrock VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 596789Sahrens 5972082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 5982082Seschrock spa_version(spa)) == 0); 5992082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 6002082Seschrock POOL_STATE_SPARE) == 0); 6012082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 6022082Seschrock vd->vdev_guid) == 0); 6035450Sbrendan } else if (reason == VDEV_LABEL_L2CACHE || 6045450Sbrendan (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 6055450Sbrendan /* 6065450Sbrendan * For level 2 ARC devices, add a special label. 6075450Sbrendan */ 6085450Sbrendan VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6095450Sbrendan 6105450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 6115450Sbrendan spa_version(spa)) == 0); 6125450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 6135450Sbrendan POOL_STATE_L2CACHE) == 0); 6145450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 6155450Sbrendan vd->vdev_guid) == 0); 6162082Seschrock } else { 6172082Seschrock label = spa_config_generate(spa, vd, 0ULL, B_FALSE); 6182082Seschrock 6192082Seschrock /* 6202082Seschrock * Add our creation time. This allows us to detect multiple 6212082Seschrock * vdev uses as described above, and automatically expires if we 6222082Seschrock * fail. 6232082Seschrock */ 6242082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 6252082Seschrock crtxg) == 0); 6262082Seschrock } 627789Sahrens 628789Sahrens buf = vp->vp_nvlist; 629789Sahrens buflen = sizeof (vp->vp_nvlist); 630789Sahrens 6313460Smmusante error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 6323460Smmusante if (error != 0) { 633789Sahrens nvlist_free(label); 634789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 6353460Smmusante /* EFAULT means nvlist_pack ran out of room */ 6363460Smmusante return (error == EFAULT ? ENAMETOOLONG : EINVAL); 637789Sahrens } 638789Sahrens 639789Sahrens /* 640789Sahrens * Initialize boot block header. 641789Sahrens */ 642789Sahrens vb = zio_buf_alloc(sizeof (vdev_boot_header_t)); 643789Sahrens bzero(vb, sizeof (vdev_boot_header_t)); 644789Sahrens vb->vb_magic = VDEV_BOOT_MAGIC; 645789Sahrens vb->vb_version = VDEV_BOOT_VERSION; 646789Sahrens vb->vb_offset = VDEV_BOOT_OFFSET; 647789Sahrens vb->vb_size = VDEV_BOOT_SIZE; 648789Sahrens 649789Sahrens /* 650789Sahrens * Initialize uberblock template. 651789Sahrens */ 6521732Sbonwick ub = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 6531732Sbonwick bzero(ub, VDEV_UBERBLOCK_SIZE(vd)); 6541732Sbonwick *ub = spa->spa_uberblock; 6551732Sbonwick ub->ub_txg = 0; 656789Sahrens 657789Sahrens /* 658789Sahrens * Write everything in parallel. 659789Sahrens */ 6605688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 661789Sahrens 662789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 663789Sahrens 664789Sahrens vdev_label_write(zio, vd, l, vp, 665789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 6665688Sbonwick sizeof (vdev_phys_t), NULL, NULL, flags); 667789Sahrens 668789Sahrens vdev_label_write(zio, vd, l, vb, 669789Sahrens offsetof(vdev_label_t, vl_boot_header), 6705688Sbonwick sizeof (vdev_boot_header_t), NULL, NULL, flags); 671789Sahrens 6721732Sbonwick for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 6731732Sbonwick vdev_label_write(zio, vd, l, ub, 6741732Sbonwick VDEV_UBERBLOCK_OFFSET(vd, n), 6755688Sbonwick VDEV_UBERBLOCK_SIZE(vd), NULL, NULL, flags); 676789Sahrens } 677789Sahrens } 678789Sahrens 679789Sahrens error = zio_wait(zio); 680789Sahrens 681789Sahrens nvlist_free(label); 6821732Sbonwick zio_buf_free(ub, VDEV_UBERBLOCK_SIZE(vd)); 683789Sahrens zio_buf_free(vb, sizeof (vdev_boot_header_t)); 684789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 685789Sahrens 6863377Seschrock /* 6873377Seschrock * If this vdev hasn't been previously identified as a spare, then we 6884451Seschrock * mark it as such only if a) we are labeling it as a spare, or b) it 6895450Sbrendan * exists as a spare elsewhere in the system. Do the same for 6905450Sbrendan * level 2 ARC devices. 6913377Seschrock */ 6923377Seschrock if (error == 0 && !vd->vdev_isspare && 6933377Seschrock (reason == VDEV_LABEL_SPARE || 6943377Seschrock spa_spare_exists(vd->vdev_guid, NULL))) 6953377Seschrock spa_spare_add(vd); 6962082Seschrock 6975450Sbrendan if (error == 0 && !vd->vdev_isl2cache && 6985450Sbrendan (reason == VDEV_LABEL_L2CACHE || 6995450Sbrendan spa_l2cache_exists(vd->vdev_guid, NULL))) 7005450Sbrendan spa_l2cache_add(vd); 7015450Sbrendan 7023377Seschrock return (error); 7032082Seschrock } 7042082Seschrock 705789Sahrens /* 706789Sahrens * ========================================================================== 707789Sahrens * uberblock load/sync 708789Sahrens * ========================================================================== 709789Sahrens */ 710789Sahrens 711789Sahrens /* 712789Sahrens * Consider the following situation: txg is safely synced to disk. We've 713789Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 714789Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 715789Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 716789Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 717789Sahrens * the missing replica comes back, then for a new seconds we'll have two 718789Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 719789Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 720789Sahrens */ 721789Sahrens static int 722789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 723789Sahrens { 724789Sahrens if (ub1->ub_txg < ub2->ub_txg) 725789Sahrens return (-1); 726789Sahrens if (ub1->ub_txg > ub2->ub_txg) 727789Sahrens return (1); 728789Sahrens 729789Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 730789Sahrens return (-1); 731789Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 732789Sahrens return (1); 733789Sahrens 734789Sahrens return (0); 735789Sahrens } 736789Sahrens 737789Sahrens static void 738789Sahrens vdev_uberblock_load_done(zio_t *zio) 739789Sahrens { 7401732Sbonwick uberblock_t *ub = zio->io_data; 741789Sahrens uberblock_t *ubbest = zio->io_private; 742789Sahrens spa_t *spa = zio->io_spa; 743789Sahrens 7441732Sbonwick ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd)); 745789Sahrens 7461544Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 747789Sahrens mutex_enter(&spa->spa_uberblock_lock); 748789Sahrens if (vdev_uberblock_compare(ub, ubbest) > 0) 749789Sahrens *ubbest = *ub; 750789Sahrens mutex_exit(&spa->spa_uberblock_lock); 751789Sahrens } 752789Sahrens 753789Sahrens zio_buf_free(zio->io_data, zio->io_size); 754789Sahrens } 755789Sahrens 756789Sahrens void 757789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 758789Sahrens { 759789Sahrens int l, c, n; 760789Sahrens 761789Sahrens for (c = 0; c < vd->vdev_children; c++) 762789Sahrens vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 763789Sahrens 764789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 765789Sahrens return; 766789Sahrens 767789Sahrens if (vdev_is_dead(vd)) 768789Sahrens return; 769789Sahrens 770789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 7711732Sbonwick for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 772789Sahrens vdev_label_read(zio, vd, l, 7731732Sbonwick zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 7741732Sbonwick VDEV_UBERBLOCK_OFFSET(vd, n), 7751732Sbonwick VDEV_UBERBLOCK_SIZE(vd), 776789Sahrens vdev_uberblock_load_done, ubbest); 777789Sahrens } 778789Sahrens } 779789Sahrens } 780789Sahrens 781789Sahrens /* 7825688Sbonwick * On success, increment root zio's count of good writes. 7831635Sbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 784789Sahrens */ 785789Sahrens static void 786789Sahrens vdev_uberblock_sync_done(zio_t *zio) 787789Sahrens { 7885688Sbonwick uint64_t *good_writes = zio->io_private; 789789Sahrens 7901635Sbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 791789Sahrens atomic_add_64(good_writes, 1); 792789Sahrens } 793789Sahrens 7945688Sbonwick /* 7955688Sbonwick * Write the uberblock to all labels of all leaves of the specified vdev. 7965688Sbonwick */ 797789Sahrens static void 7985688Sbonwick vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd) 799789Sahrens { 800789Sahrens int l, c, n; 8015688Sbonwick uberblock_t *ubbuf; 802789Sahrens 803789Sahrens for (c = 0; c < vd->vdev_children; c++) 8045688Sbonwick vdev_uberblock_sync(zio, ub, vd->vdev_child[c]); 805789Sahrens 806789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 807789Sahrens return; 808789Sahrens 809789Sahrens if (vdev_is_dead(vd)) 810789Sahrens return; 811789Sahrens 8125688Sbonwick n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 813789Sahrens 8145688Sbonwick ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 8155688Sbonwick bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 8161732Sbonwick *ubbuf = *ub; 817789Sahrens 8185688Sbonwick for (l = 0; l < VDEV_LABELS; l++) 8195688Sbonwick vdev_label_write(zio, vd, l, ubbuf, 8205688Sbonwick VDEV_UBERBLOCK_OFFSET(vd, n), 8215688Sbonwick VDEV_UBERBLOCK_SIZE(vd), 8225688Sbonwick vdev_uberblock_sync_done, zio->io_private, 8235688Sbonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE); 824789Sahrens 8255688Sbonwick zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 8265688Sbonwick } 827789Sahrens 8286615Sgw25295 static void 8296615Sgw25295 vdev_uberblock_sync_list_done(zio_t *zio) 8306615Sgw25295 { 8316615Sgw25295 uint64_t *good_writes = zio->io_private; 8326615Sgw25295 8336615Sgw25295 if (*good_writes == 0) 8346615Sgw25295 zio->io_error = EIO; 8356615Sgw25295 } 8366615Sgw25295 8375688Sbonwick int 8385688Sbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 8395688Sbonwick { 8405688Sbonwick spa_t *spa = svd[0]->vdev_spa; 8415688Sbonwick int v; 8426615Sgw25295 zio_t *zio, *nio; 8435688Sbonwick uint64_t good_writes = 0; 8446615Sgw25295 int io_flags = flags; 8456615Sgw25295 8466615Sgw25295 /* 8476615Sgw25295 * If we've been asked to update all the vdevs then we change 8486615Sgw25295 * our flags to ZIO_FLAG_MUSTSUCCEED so that the pipeline can 8496615Sgw25295 * handle error should all update fail. 8506615Sgw25295 */ 8516615Sgw25295 if (svdcount == spa->spa_root_vdev->vdev_children) 8526615Sgw25295 io_flags &= ~ZIO_FLAG_CANFAIL; 853789Sahrens 8546615Sgw25295 /* 8556615Sgw25295 * We rely on the value of good_writes and the root I/O to determine 8566615Sgw25295 * how a complete failure is handled. In the event that the root is a 8576615Sgw25295 * ZIO_FLAG_MUSTSUCCED, then the pipeline will block this I/O if we 8586615Sgw25295 * were unable to update any uberblock. Once the I/O is blocked the 8596615Sgw25295 * pipeline will retry it when the error is cleared. Unfortunately, 8606615Sgw25295 * the pipeline does not have the complete I/O tree so it will be 8616615Sgw25295 * unable to retry the actual uberblock update. Instead we rely on 8626615Sgw25295 * the value of good_writes to return the failed status to the caller 8636615Sgw25295 * which will retry on error and thus resubmit the complete I/O 8646615Sgw25295 * tree. 8656615Sgw25295 */ 8666615Sgw25295 zio = zio_root(spa, NULL, NULL, io_flags); 8676615Sgw25295 nio = zio_null(zio, spa, vdev_uberblock_sync_list_done, &good_writes, 8686615Sgw25295 flags); 8695688Sbonwick for (v = 0; v < svdcount; v++) 8706615Sgw25295 vdev_uberblock_sync(nio, ub, svd[v]); 8716615Sgw25295 zio_nowait(nio); 8725688Sbonwick (void) zio_wait(zio); 873789Sahrens 874789Sahrens /* 8755688Sbonwick * Flush the uberblocks to disk. This ensures that the odd labels 8765688Sbonwick * are no longer needed (because the new uberblocks and the even 8775688Sbonwick * labels are safely on disk), so it is safe to overwrite them. 878789Sahrens */ 8795688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 880789Sahrens 8815688Sbonwick for (v = 0; v < svdcount; v++) 8825688Sbonwick zio_flush(zio, svd[v]); 883789Sahrens 8845688Sbonwick (void) zio_wait(zio); 8855688Sbonwick 8865688Sbonwick return (good_writes >= 1 ? 0 : EIO); 887789Sahrens } 888789Sahrens 889789Sahrens /* 8905688Sbonwick * On success, increment the count of good writes for our top-level vdev. 891789Sahrens */ 892789Sahrens static void 8935688Sbonwick vdev_label_sync_done(zio_t *zio) 894789Sahrens { 8955688Sbonwick uint64_t *good_writes = zio->io_private; 896789Sahrens 897789Sahrens if (zio->io_error == 0) 898789Sahrens atomic_add_64(good_writes, 1); 899789Sahrens } 900789Sahrens 9015688Sbonwick /* 9025688Sbonwick * If there weren't enough good writes, indicate failure to the parent. 9035688Sbonwick */ 904789Sahrens static void 9055688Sbonwick vdev_label_sync_top_done(zio_t *zio) 9065688Sbonwick { 9075688Sbonwick uint64_t *good_writes = zio->io_private; 9085688Sbonwick 9095688Sbonwick if (*good_writes == 0) 9105688Sbonwick zio->io_error = EIO; 9115688Sbonwick 9125688Sbonwick kmem_free(good_writes, sizeof (uint64_t)); 9135688Sbonwick } 9145688Sbonwick 9155688Sbonwick /* 916*7041Seschrock * We ignore errors for log and cache devices, simply free the private data. 9176976Seschrock */ 9186976Seschrock static void 919*7041Seschrock vdev_label_sync_ignore_done(zio_t *zio) 9206976Seschrock { 9216976Seschrock kmem_free(zio->io_private, sizeof (uint64_t)); 9226976Seschrock } 9236976Seschrock 9246976Seschrock /* 9255688Sbonwick * Write all even or odd labels to all leaves of the specified vdev. 9265688Sbonwick */ 9275688Sbonwick static void 9285688Sbonwick vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg) 929789Sahrens { 930789Sahrens nvlist_t *label; 931789Sahrens vdev_phys_t *vp; 932789Sahrens char *buf; 933789Sahrens size_t buflen; 934789Sahrens int c; 935789Sahrens 936789Sahrens for (c = 0; c < vd->vdev_children; c++) 9375688Sbonwick vdev_label_sync(zio, vd->vdev_child[c], l, txg); 938789Sahrens 939789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 940789Sahrens return; 941789Sahrens 942789Sahrens if (vdev_is_dead(vd)) 943789Sahrens return; 944789Sahrens 945789Sahrens /* 946789Sahrens * Generate a label describing the top-level config to which we belong. 947789Sahrens */ 9481635Sbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 949789Sahrens 950789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 951789Sahrens bzero(vp, sizeof (vdev_phys_t)); 952789Sahrens 953789Sahrens buf = vp->vp_nvlist; 954789Sahrens buflen = sizeof (vp->vp_nvlist); 955789Sahrens 9565688Sbonwick if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 9575688Sbonwick for (; l < VDEV_LABELS; l += 2) { 9585688Sbonwick vdev_label_write(zio, vd, l, vp, 9595688Sbonwick offsetof(vdev_label_t, vl_vdev_phys), 9605688Sbonwick sizeof (vdev_phys_t), 9615688Sbonwick vdev_label_sync_done, zio->io_private, 9625688Sbonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE); 9635688Sbonwick } 9645688Sbonwick } 965789Sahrens 966789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 967789Sahrens nvlist_free(label); 968789Sahrens } 969789Sahrens 9705688Sbonwick int 9715688Sbonwick vdev_label_sync_list(spa_t *spa, int l, int flags, uint64_t txg) 972789Sahrens { 9735688Sbonwick list_t *dl = &spa->spa_dirty_list; 9745688Sbonwick vdev_t *vd; 9756615Sgw25295 zio_t *zio, *nio; 976789Sahrens int error; 9776615Sgw25295 int io_flags = flags & ~ZIO_FLAG_CANFAIL; 978789Sahrens 9795688Sbonwick /* 9806615Sgw25295 * The root I/O for all label updates must succeed and we track 9816615Sgw25295 * the error returned back from the null I/O to determine if we 9826615Sgw25295 * need to reissue the I/O tree from scratch. If we are unable 9836615Sgw25295 * to update any leaf vdev associated with a dirty top-level vdev, 9846615Sgw25295 * then the pipeline will either suspend or panic when the root I/O 9856615Sgw25295 * is issued. If the error is cleared, then the pipleine will retry 9866615Sgw25295 * the root I/O. Unfortunately we've lost the entire I/O tree so we 9876615Sgw25295 * return back the original error to the caller and allow the caller 9886615Sgw25295 * to call use again so that we can build the I/O tree from scratch. 9895688Sbonwick */ 9906615Sgw25295 zio = zio_root(spa, NULL, NULL, io_flags); 9916615Sgw25295 nio = zio_null(zio, spa, NULL, NULL, flags); 992789Sahrens 9935688Sbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 9945688Sbonwick uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 9955688Sbonwick KM_SLEEP); 996*7041Seschrock zio_t *vio = zio_null(nio, spa, 997*7041Seschrock (vd->vdev_islog || vd->vdev_aux != NULL) ? 998*7041Seschrock vdev_label_sync_ignore_done : vdev_label_sync_top_done, 9995688Sbonwick good_writes, flags); 10005688Sbonwick vdev_label_sync(vio, vd, l, txg); 10015688Sbonwick zio_nowait(vio); 10025688Sbonwick } 10036615Sgw25295 error = zio_wait(nio); 10046615Sgw25295 (void) zio_wait(zio); 1005789Sahrens 10065688Sbonwick /* 10075688Sbonwick * Flush the new labels to disk. 10085688Sbonwick */ 10095688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 1010789Sahrens 10115688Sbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 10125688Sbonwick zio_flush(zio, vd); 10134527Sperrin 10145688Sbonwick (void) zio_wait(zio); 1015789Sahrens 1016789Sahrens return (error); 1017789Sahrens } 1018789Sahrens 1019789Sahrens /* 10205688Sbonwick * Sync the uberblock and any changes to the vdev configuration. 1021789Sahrens * 1022789Sahrens * The order of operations is carefully crafted to ensure that 1023789Sahrens * if the system panics or loses power at any time, the state on disk 1024789Sahrens * is still transactionally consistent. The in-line comments below 1025789Sahrens * describe the failure semantics at each stage. 1026789Sahrens * 10275688Sbonwick * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 1028789Sahrens * at any time, you can just call it again, and it will resume its work. 1029789Sahrens */ 10306615Sgw25295 void 10315688Sbonwick vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg) 1032789Sahrens { 10335688Sbonwick spa_t *spa = svd[0]->vdev_spa; 1034789Sahrens uberblock_t *ub = &spa->spa_uberblock; 10351635Sbonwick vdev_t *vd; 1036789Sahrens zio_t *zio; 10375688Sbonwick int flags = ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL; 1038789Sahrens 1039789Sahrens ASSERT(ub->ub_txg <= txg); 1040789Sahrens 1041789Sahrens /* 10425688Sbonwick * If this isn't a resync due to I/O errors, 10435688Sbonwick * and nothing changed in this transaction group, 10445688Sbonwick * and the vdev configuration hasn't changed, 10451635Sbonwick * then there's nothing to do. 1046789Sahrens */ 10475688Sbonwick if (ub->ub_txg < txg && 10485688Sbonwick uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 10495688Sbonwick list_is_empty(&spa->spa_dirty_list)) 10506615Sgw25295 return; 1051789Sahrens 1052789Sahrens if (txg > spa_freeze_txg(spa)) 10536615Sgw25295 return; 1054789Sahrens 10551635Sbonwick ASSERT(txg <= spa->spa_final_txg); 10561635Sbonwick 1057789Sahrens /* 1058789Sahrens * Flush the write cache of every disk that's been written to 1059789Sahrens * in this transaction group. This ensures that all blocks 1060789Sahrens * written in this txg will be committed to stable storage 1061789Sahrens * before any uberblock that references them. 1062789Sahrens */ 10635688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 10645688Sbonwick 1065789Sahrens for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 10665688Sbonwick vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 10675688Sbonwick zio_flush(zio, vd); 10685688Sbonwick 1069789Sahrens (void) zio_wait(zio); 1070789Sahrens 1071789Sahrens /* 1072789Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 1073789Sahrens * system dies in the middle of this process, that's OK: all of the 1074789Sahrens * even labels that made it to disk will be newer than any uberblock, 1075789Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 10765688Sbonwick * which have not yet been touched, will still be valid. We flush 10775688Sbonwick * the new labels to disk to ensure that all even-label updates 10785688Sbonwick * are committed to stable storage before the uberblock update. 10796615Sgw25295 * Failure to update any of the labels will invoke the 'failmode' 10806615Sgw25295 * code path. Thus we must retry the entire I/O tree once the error 10816615Sgw25295 * is cleared and we ar resumed. 1082789Sahrens */ 10836615Sgw25295 while (vdev_label_sync_list(spa, 0, flags, txg) != 0) 10846615Sgw25295 ; 1085789Sahrens 1086789Sahrens /* 10876615Sgw25295 * Sync the uberblocks to all vdevs in svd[]. If we are unable 10886615Sgw25295 * to do so, then we attempt to sync out to all top-level vdevs. 10891635Sbonwick * If the system dies in the middle of this step, there are two cases 10901635Sbonwick * to consider, and the on-disk state is consistent either way: 1091789Sahrens * 1092789Sahrens * (1) If none of the new uberblocks made it to disk, then the 1093789Sahrens * previous uberblock will be the newest, and the odd labels 1094789Sahrens * (which had not yet been touched) will be valid with respect 1095789Sahrens * to that uberblock. 1096789Sahrens * 1097789Sahrens * (2) If one or more new uberblocks made it to disk, then they 1098789Sahrens * will be the newest, and the even labels (which had all 1099789Sahrens * been successfully committed) will be valid with respect 1100789Sahrens * to the new uberblocks. 11016615Sgw25295 * 11026615Sgw25295 * In addition, if we have failed to update all the uberblocks then 11036615Sgw25295 * we will follow the 'failmode' code path. We must retry the entire 11046615Sgw25295 * I/O tree if we are resumed. 1105789Sahrens */ 11066615Sgw25295 if (vdev_uberblock_sync_list(svd, svdcount, ub, flags) != 0) { 11076615Sgw25295 vdev_t *rvd = spa->spa_root_vdev; 11086615Sgw25295 11096615Sgw25295 while (vdev_uberblock_sync_list(rvd->vdev_child, 11106615Sgw25295 rvd->vdev_children, ub, flags)) 11116615Sgw25295 ; 11126615Sgw25295 } 1113789Sahrens 1114789Sahrens /* 1115789Sahrens * Sync out odd labels for every dirty vdev. If the system dies 1116789Sahrens * in the middle of this process, the even labels and the new 1117789Sahrens * uberblocks will suffice to open the pool. The next time 1118789Sahrens * the pool is opened, the first thing we'll do -- before any 1119789Sahrens * user data is modified -- is mark every vdev dirty so that 11205688Sbonwick * all labels will be brought up to date. We flush the new labels 11215688Sbonwick * to disk to ensure that all odd-label updates are committed to 11225688Sbonwick * stable storage before the next transaction group begins. 11236615Sgw25295 * Failure to update any of the labels will invoke the 'failmode' 11246615Sgw25295 * code path. Thus we must retry the entire I/O tree once the error 11256615Sgw25295 * is cleared and we are resumed. 1126789Sahrens */ 11276615Sgw25295 while (vdev_label_sync_list(spa, 1, flags, txg) != 0) 11286615Sgw25295 ; 1129789Sahrens } 1130