1*789Sahrens /* 2*789Sahrens * CDDL HEADER START 3*789Sahrens * 4*789Sahrens * The contents of this file are subject to the terms of the 5*789Sahrens * Common Development and Distribution License, Version 1.0 only 6*789Sahrens * (the "License"). You may not use this file except in compliance 7*789Sahrens * with the License. 8*789Sahrens * 9*789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*789Sahrens * or http://www.opensolaris.org/os/licensing. 11*789Sahrens * See the License for the specific language governing permissions 12*789Sahrens * and limitations under the License. 13*789Sahrens * 14*789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 15*789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*789Sahrens * If applicable, add the following below this CDDL HEADER, with the 17*789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 18*789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 19*789Sahrens * 20*789Sahrens * CDDL HEADER END 21*789Sahrens */ 22*789Sahrens /* 23*789Sahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*789Sahrens * Use is subject to license terms. 25*789Sahrens */ 26*789Sahrens 27*789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28*789Sahrens 29*789Sahrens /* 30*789Sahrens * Virtual Device Labels 31*789Sahrens * --------------------- 32*789Sahrens * 33*789Sahrens * The vdev label serves several distinct purposes: 34*789Sahrens * 35*789Sahrens * 1. Uniquely identify this device as part of a ZFS pool and confirm its 36*789Sahrens * identity within the pool. 37*789Sahrens * 38*789Sahrens * 2. Verify that all the devices given in a configuration are present 39*789Sahrens * within the pool. 40*789Sahrens * 41*789Sahrens * 3. Determine the uberblock for the pool. 42*789Sahrens * 43*789Sahrens * 4. In case of an import operation, determine the configuration of the 44*789Sahrens * toplevel vdev of which it is a part. 45*789Sahrens * 46*789Sahrens * 5. If an import operation cannot find all the devices in the pool, 47*789Sahrens * provide enough information to the administrator to determine which 48*789Sahrens * devices are missing. 49*789Sahrens * 50*789Sahrens * It is important to note that while the kernel is responsible for writing the 51*789Sahrens * label, it only consumes the information in the first three cases. The 52*789Sahrens * latter information is only consumed in userland when determining the 53*789Sahrens * configuration to import a pool. 54*789Sahrens * 55*789Sahrens * 56*789Sahrens * Label Organization 57*789Sahrens * ------------------ 58*789Sahrens * 59*789Sahrens * Before describing the contents of the label, it's important to understand how 60*789Sahrens * the labels are written and updated with respect to the uberblock. 61*789Sahrens * 62*789Sahrens * When the pool configuration is altered, either because it was newly created 63*789Sahrens * or a device was added, we want to update all the labels such that we can deal 64*789Sahrens * with fatal failure at any point. To this end, each disk has two labels which 65*789Sahrens * are updated before and after the uberblock is synced. Assuming we have 66*789Sahrens * labels and an uberblock with the following transacation groups: 67*789Sahrens * 68*789Sahrens * L1 UB L2 69*789Sahrens * +------+ +------+ +------+ 70*789Sahrens * | | | | | | 71*789Sahrens * | t10 | | t10 | | t10 | 72*789Sahrens * | | | | | | 73*789Sahrens * +------+ +------+ +------+ 74*789Sahrens * 75*789Sahrens * In this stable state, the labels and the uberblock were all updated within 76*789Sahrens * the same transaction group (10). Each label is mirrored and checksummed, so 77*789Sahrens * that we can detect when we fail partway through writing the label. 78*789Sahrens * 79*789Sahrens * In order to identify which labels are valid, the labels are written in the 80*789Sahrens * following manner: 81*789Sahrens * 82*789Sahrens * 1. For each vdev, update 'L1' to the new label 83*789Sahrens * 2. Update the uberblock 84*789Sahrens * 3. For each vdev, update 'L2' to the new label 85*789Sahrens * 86*789Sahrens * Given arbitrary failure, we can determine the correct label to use based on 87*789Sahrens * the transaction group. If we fail after updating L1 but before updating the 88*789Sahrens * UB, we will notice that L1's transaction group is greater than the uberblock, 89*789Sahrens * so L2 must be valid. If we fail after writing the uberblock but before 90*789Sahrens * writing L2, we will notice that L2's transaction group is less than L1, and 91*789Sahrens * therefore L1 is valid. 92*789Sahrens * 93*789Sahrens * Another added complexity is that not every label is updated when the config 94*789Sahrens * is synced. If we add a single device, we do not want to have to re-write 95*789Sahrens * every label for every device in the pool. This means that both L1 and L2 may 96*789Sahrens * be older than the pool uberblock, because the necessary information is stored 97*789Sahrens * on another vdev. 98*789Sahrens * 99*789Sahrens * 100*789Sahrens * On-disk Format 101*789Sahrens * -------------- 102*789Sahrens * 103*789Sahrens * The vdev label consists of two distinct parts, and is wrapped within the 104*789Sahrens * vdev_label_t structure. The label includes 8k of padding to permit legacy 105*789Sahrens * VTOC disk labels, but is otherwise ignored. 106*789Sahrens * 107*789Sahrens * The first half of the label is a packed nvlist which contains pool wide 108*789Sahrens * properties, per-vdev properties, and configuration information. It is 109*789Sahrens * described in more detail below. 110*789Sahrens * 111*789Sahrens * The latter half of the label consists of a redundant array of uberblocks. 112*789Sahrens * These uberblocks are updated whenever a transaction group is committed, 113*789Sahrens * or when the configuration is updated. When a pool is loaded, we scan each 114*789Sahrens * vdev for the 'best' uberblock. 115*789Sahrens * 116*789Sahrens * 117*789Sahrens * Configuration Information 118*789Sahrens * ------------------------- 119*789Sahrens * 120*789Sahrens * The nvlist describing the pool and vdev contains the following elements: 121*789Sahrens * 122*789Sahrens * version ZFS on-disk version 123*789Sahrens * name Pool name 124*789Sahrens * state Pool state 125*789Sahrens * txg Transaction group in which this label was written 126*789Sahrens * pool_guid Unique identifier for this pool 127*789Sahrens * vdev_tree An nvlist describing vdev tree. 128*789Sahrens * 129*789Sahrens * Each leaf device label also contains the following: 130*789Sahrens * 131*789Sahrens * top_guid Unique ID for top-level vdev in which this is contained 132*789Sahrens * guid Unique ID for the leaf vdev 133*789Sahrens * 134*789Sahrens * The 'vs' configuration follows the format described in 'spa_config.c'. 135*789Sahrens */ 136*789Sahrens 137*789Sahrens #include <sys/zfs_context.h> 138*789Sahrens #include <sys/spa.h> 139*789Sahrens #include <sys/spa_impl.h> 140*789Sahrens #include <sys/dmu.h> 141*789Sahrens #include <sys/zap.h> 142*789Sahrens #include <sys/vdev.h> 143*789Sahrens #include <sys/vdev_impl.h> 144*789Sahrens #include <sys/uberblock_impl.h> 145*789Sahrens #include <sys/metaslab.h> 146*789Sahrens #include <sys/zio.h> 147*789Sahrens #include <sys/fs/zfs.h> 148*789Sahrens 149*789Sahrens /* 150*789Sahrens * Basic routines to read and write from a vdev label. 151*789Sahrens * Used throughout the rest of this file. 152*789Sahrens */ 153*789Sahrens uint64_t 154*789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset) 155*789Sahrens { 156*789Sahrens return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 157*789Sahrens 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 158*789Sahrens } 159*789Sahrens 160*789Sahrens static void 161*789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 162*789Sahrens uint64_t size, zio_done_func_t *done, void *private) 163*789Sahrens { 164*789Sahrens ASSERT(vd->vdev_children == 0); 165*789Sahrens 166*789Sahrens zio_nowait(zio_read_phys(zio, vd, 167*789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 168*789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 169*789Sahrens ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_SPECULATIVE | 170*789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_DONT_RETRY)); 171*789Sahrens } 172*789Sahrens 173*789Sahrens static void 174*789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 175*789Sahrens uint64_t size, zio_done_func_t *done, void *private) 176*789Sahrens { 177*789Sahrens ASSERT(vd->vdev_children == 0); 178*789Sahrens 179*789Sahrens zio_nowait(zio_write_phys(zio, vd, 180*789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 181*789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 182*789Sahrens ZIO_PRIORITY_SYNC_WRITE, 183*789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_DONT_RETRY)); 184*789Sahrens } 185*789Sahrens 186*789Sahrens /* 187*789Sahrens * Generate the nvlist representing this vdev's config. 188*789Sahrens */ 189*789Sahrens nvlist_t * 190*789Sahrens vdev_config_generate(vdev_t *vd, int getstats) 191*789Sahrens { 192*789Sahrens nvlist_t *nv = NULL; 193*789Sahrens 194*789Sahrens VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) == 0); 195*789Sahrens 196*789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 197*789Sahrens vd->vdev_ops->vdev_op_type) == 0); 198*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0); 199*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 200*789Sahrens 201*789Sahrens if (vd->vdev_path != NULL) 202*789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 203*789Sahrens vd->vdev_path) == 0); 204*789Sahrens 205*789Sahrens if (vd->vdev_devid != NULL) 206*789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 207*789Sahrens vd->vdev_devid) == 0); 208*789Sahrens 209*789Sahrens if (vd == vd->vdev_top) { 210*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 211*789Sahrens vd->vdev_ms_array) == 0); 212*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 213*789Sahrens vd->vdev_ms_shift) == 0); 214*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 215*789Sahrens vd->vdev_ashift) == 0); 216*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 217*789Sahrens vd->vdev_asize) == 0); 218*789Sahrens } 219*789Sahrens 220*789Sahrens if (vd->vdev_dtl.smo_object != 0) 221*789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 222*789Sahrens vd->vdev_dtl.smo_object) == 0); 223*789Sahrens 224*789Sahrens if (getstats) { 225*789Sahrens vdev_stat_t vs; 226*789Sahrens vdev_get_stats(vd, &vs); 227*789Sahrens VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 228*789Sahrens (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 229*789Sahrens } 230*789Sahrens 231*789Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 232*789Sahrens nvlist_t **child; 233*789Sahrens int c; 234*789Sahrens 235*789Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 236*789Sahrens KM_SLEEP); 237*789Sahrens 238*789Sahrens for (c = 0; c < vd->vdev_children; c++) 239*789Sahrens child[c] = vdev_config_generate(vd->vdev_child[c], 240*789Sahrens getstats); 241*789Sahrens 242*789Sahrens VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 243*789Sahrens child, vd->vdev_children) == 0); 244*789Sahrens 245*789Sahrens for (c = 0; c < vd->vdev_children; c++) 246*789Sahrens nvlist_free(child[c]); 247*789Sahrens 248*789Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 249*789Sahrens } 250*789Sahrens 251*789Sahrens return (nv); 252*789Sahrens } 253*789Sahrens 254*789Sahrens nvlist_t * 255*789Sahrens vdev_label_read_config(vdev_t *vd) 256*789Sahrens { 257*789Sahrens nvlist_t *config = NULL; 258*789Sahrens vdev_phys_t *vp; 259*789Sahrens uint64_t version; 260*789Sahrens zio_t *zio; 261*789Sahrens int l; 262*789Sahrens 263*789Sahrens if (vdev_is_dead(vd)) 264*789Sahrens return (NULL); 265*789Sahrens 266*789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 267*789Sahrens 268*789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 269*789Sahrens 270*789Sahrens zio = zio_root(vd->vdev_spa, NULL, NULL, 271*789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD); 272*789Sahrens 273*789Sahrens vdev_label_read(zio, vd, l, vp, 274*789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 275*789Sahrens sizeof (vdev_phys_t), NULL, NULL); 276*789Sahrens 277*789Sahrens if (zio_wait(zio) == 0 && 278*789Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 279*789Sahrens &config, 0) == 0 && 280*789Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 281*789Sahrens &version) == 0 && 282*789Sahrens version == UBERBLOCK_VERSION) 283*789Sahrens break; 284*789Sahrens 285*789Sahrens if (config != NULL) { 286*789Sahrens nvlist_free(config); 287*789Sahrens config = NULL; 288*789Sahrens } 289*789Sahrens } 290*789Sahrens 291*789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 292*789Sahrens 293*789Sahrens return (config); 294*789Sahrens } 295*789Sahrens 296*789Sahrens int 297*789Sahrens vdev_label_init(vdev_t *vd, uint64_t crtxg) 298*789Sahrens { 299*789Sahrens spa_t *spa = vd->vdev_spa; 300*789Sahrens nvlist_t *label; 301*789Sahrens vdev_phys_t *vp; 302*789Sahrens vdev_boot_header_t *vb; 303*789Sahrens uberblock_phys_t *ubphys; 304*789Sahrens zio_t *zio; 305*789Sahrens int l, c, n; 306*789Sahrens char *buf; 307*789Sahrens size_t buflen; 308*789Sahrens int error; 309*789Sahrens 310*789Sahrens for (c = 0; c < vd->vdev_children; c++) 311*789Sahrens if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0) 312*789Sahrens return (error); 313*789Sahrens 314*789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 315*789Sahrens return (0); 316*789Sahrens 317*789Sahrens /* 318*789Sahrens * Make sure each leaf device is writable, and zero its initial content. 319*789Sahrens * Along the way, also make sure that no leaf is already in use. 320*789Sahrens * Note that it's important to do this sequentially, not in parallel, 321*789Sahrens * so that we catch cases of multiple use of the same leaf vdev in 322*789Sahrens * the vdev we're creating -- e.g. mirroring a disk with itself. 323*789Sahrens */ 324*789Sahrens if (vdev_is_dead(vd)) 325*789Sahrens return (EIO); 326*789Sahrens 327*789Sahrens /* 328*789Sahrens * Check whether this device is already in use. 329*789Sahrens * Ignore the check if crtxg == 0, which we use for device removal. 330*789Sahrens */ 331*789Sahrens if (crtxg != 0 && (label = vdev_label_read_config(vd)) != NULL) { 332*789Sahrens uint64_t version, state, pool_guid, device_guid, txg; 333*789Sahrens uint64_t mycrtxg = 0; 334*789Sahrens 335*789Sahrens (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 336*789Sahrens &mycrtxg); 337*789Sahrens 338*789Sahrens if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, 339*789Sahrens &version) == 0 && version == UBERBLOCK_VERSION && 340*789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 341*789Sahrens &state) == 0 && state == POOL_STATE_ACTIVE && 342*789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 343*789Sahrens &pool_guid) == 0 && 344*789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 345*789Sahrens &device_guid) == 0 && 346*789Sahrens spa_guid_exists(pool_guid, device_guid) && 347*789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 348*789Sahrens &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) { 349*789Sahrens dprintf("vdev %s in use, pool_state %d\n", 350*789Sahrens vdev_description(vd), state); 351*789Sahrens nvlist_free(label); 352*789Sahrens return (EBUSY); 353*789Sahrens } 354*789Sahrens nvlist_free(label); 355*789Sahrens } 356*789Sahrens 357*789Sahrens /* 358*789Sahrens * The device isn't in use, so initialize its label. 359*789Sahrens */ 360*789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 361*789Sahrens bzero(vp, sizeof (vdev_phys_t)); 362*789Sahrens 363*789Sahrens /* 364*789Sahrens * Generate a label describing the pool and our top-level vdev. 365*789Sahrens * We mark it as being from txg 0 to indicate that it's not 366*789Sahrens * really part of an active pool just yet. The labels will 367*789Sahrens * be written again with a meaningful txg by spa_sync(). 368*789Sahrens */ 369*789Sahrens label = spa_config_generate(spa, vd, 0ULL, 0); 370*789Sahrens 371*789Sahrens /* 372*789Sahrens * Add our creation time. This allows us to detect multiple vdev 373*789Sahrens * uses as described above, and automatically expires if we fail. 374*789Sahrens */ 375*789Sahrens VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0); 376*789Sahrens 377*789Sahrens buf = vp->vp_nvlist; 378*789Sahrens buflen = sizeof (vp->vp_nvlist); 379*789Sahrens 380*789Sahrens if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, 0) != 0) { 381*789Sahrens nvlist_free(label); 382*789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 383*789Sahrens return (EINVAL); 384*789Sahrens } 385*789Sahrens 386*789Sahrens /* 387*789Sahrens * Initialize boot block header. 388*789Sahrens */ 389*789Sahrens vb = zio_buf_alloc(sizeof (vdev_boot_header_t)); 390*789Sahrens bzero(vb, sizeof (vdev_boot_header_t)); 391*789Sahrens vb->vb_magic = VDEV_BOOT_MAGIC; 392*789Sahrens vb->vb_version = VDEV_BOOT_VERSION; 393*789Sahrens vb->vb_offset = VDEV_BOOT_OFFSET; 394*789Sahrens vb->vb_size = VDEV_BOOT_SIZE; 395*789Sahrens 396*789Sahrens /* 397*789Sahrens * Initialize uberblock template. 398*789Sahrens */ 399*789Sahrens ubphys = zio_buf_alloc(sizeof (uberblock_phys_t)); 400*789Sahrens bzero(ubphys, sizeof (uberblock_phys_t)); 401*789Sahrens ubphys->ubp_uberblock = spa->spa_uberblock; 402*789Sahrens ubphys->ubp_uberblock.ub_txg = 0; 403*789Sahrens 404*789Sahrens /* 405*789Sahrens * Write everything in parallel. 406*789Sahrens */ 407*789Sahrens zio = zio_root(spa, NULL, NULL, 408*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 409*789Sahrens 410*789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 411*789Sahrens 412*789Sahrens vdev_label_write(zio, vd, l, vp, 413*789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 414*789Sahrens sizeof (vdev_phys_t), NULL, NULL); 415*789Sahrens 416*789Sahrens vdev_label_write(zio, vd, l, vb, 417*789Sahrens offsetof(vdev_label_t, vl_boot_header), 418*789Sahrens sizeof (vdev_boot_header_t), NULL, NULL); 419*789Sahrens 420*789Sahrens for (n = 0; n < VDEV_UBERBLOCKS; n++) { 421*789Sahrens 422*789Sahrens vdev_label_write(zio, vd, l, ubphys, 423*789Sahrens offsetof(vdev_label_t, vl_uberblock[n]), 424*789Sahrens sizeof (uberblock_phys_t), NULL, NULL); 425*789Sahrens 426*789Sahrens } 427*789Sahrens } 428*789Sahrens 429*789Sahrens error = zio_wait(zio); 430*789Sahrens 431*789Sahrens nvlist_free(label); 432*789Sahrens zio_buf_free(ubphys, sizeof (uberblock_phys_t)); 433*789Sahrens zio_buf_free(vb, sizeof (vdev_boot_header_t)); 434*789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 435*789Sahrens 436*789Sahrens return (error); 437*789Sahrens } 438*789Sahrens 439*789Sahrens /* 440*789Sahrens * ========================================================================== 441*789Sahrens * uberblock load/sync 442*789Sahrens * ========================================================================== 443*789Sahrens */ 444*789Sahrens 445*789Sahrens /* 446*789Sahrens * Consider the following situation: txg is safely synced to disk. We've 447*789Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 448*789Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 449*789Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 450*789Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 451*789Sahrens * the missing replica comes back, then for a new seconds we'll have two 452*789Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 453*789Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 454*789Sahrens */ 455*789Sahrens static int 456*789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 457*789Sahrens { 458*789Sahrens if (ub1->ub_txg < ub2->ub_txg) 459*789Sahrens return (-1); 460*789Sahrens if (ub1->ub_txg > ub2->ub_txg) 461*789Sahrens return (1); 462*789Sahrens 463*789Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 464*789Sahrens return (-1); 465*789Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 466*789Sahrens return (1); 467*789Sahrens 468*789Sahrens return (0); 469*789Sahrens } 470*789Sahrens 471*789Sahrens static void 472*789Sahrens vdev_uberblock_load_done(zio_t *zio) 473*789Sahrens { 474*789Sahrens uberblock_phys_t *ubphys = zio->io_data; 475*789Sahrens uberblock_t *ub = &ubphys->ubp_uberblock; 476*789Sahrens uberblock_t *ubbest = zio->io_private; 477*789Sahrens spa_t *spa = zio->io_spa; 478*789Sahrens 479*789Sahrens ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t)); 480*789Sahrens 481*789Sahrens if (uberblock_verify(ub) == 0) { 482*789Sahrens mutex_enter(&spa->spa_uberblock_lock); 483*789Sahrens if (vdev_uberblock_compare(ub, ubbest) > 0) 484*789Sahrens *ubbest = *ub; 485*789Sahrens mutex_exit(&spa->spa_uberblock_lock); 486*789Sahrens } 487*789Sahrens 488*789Sahrens zio_buf_free(zio->io_data, zio->io_size); 489*789Sahrens } 490*789Sahrens 491*789Sahrens void 492*789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 493*789Sahrens { 494*789Sahrens int l, c, n; 495*789Sahrens 496*789Sahrens for (c = 0; c < vd->vdev_children; c++) 497*789Sahrens vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 498*789Sahrens 499*789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 500*789Sahrens return; 501*789Sahrens 502*789Sahrens if (vdev_is_dead(vd)) 503*789Sahrens return; 504*789Sahrens 505*789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 506*789Sahrens for (n = 0; n < VDEV_UBERBLOCKS; n++) { 507*789Sahrens vdev_label_read(zio, vd, l, 508*789Sahrens zio_buf_alloc(sizeof (uberblock_phys_t)), 509*789Sahrens offsetof(vdev_label_t, vl_uberblock[n]), 510*789Sahrens sizeof (uberblock_phys_t), 511*789Sahrens vdev_uberblock_load_done, ubbest); 512*789Sahrens } 513*789Sahrens } 514*789Sahrens } 515*789Sahrens 516*789Sahrens /* 517*789Sahrens * Write the uberblock to both labels of all leaves of the specified vdev. 518*789Sahrens */ 519*789Sahrens static void 520*789Sahrens vdev_uberblock_sync_done(zio_t *zio) 521*789Sahrens { 522*789Sahrens uint64_t *good_writes = zio->io_root->io_private; 523*789Sahrens 524*789Sahrens if (zio->io_error == 0) 525*789Sahrens atomic_add_64(good_writes, 1); 526*789Sahrens } 527*789Sahrens 528*789Sahrens static void 529*789Sahrens vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd, 530*789Sahrens uint64_t txg) 531*789Sahrens { 532*789Sahrens int l, c, n; 533*789Sahrens 534*789Sahrens for (c = 0; c < vd->vdev_children; c++) 535*789Sahrens vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg); 536*789Sahrens 537*789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 538*789Sahrens return; 539*789Sahrens 540*789Sahrens if (vdev_is_dead(vd)) 541*789Sahrens return; 542*789Sahrens 543*789Sahrens n = txg & (VDEV_UBERBLOCKS - 1); 544*789Sahrens 545*789Sahrens ASSERT(ubphys->ubp_uberblock.ub_txg == txg); 546*789Sahrens 547*789Sahrens for (l = 0; l < VDEV_LABELS; l++) 548*789Sahrens vdev_label_write(zio, vd, l, ubphys, 549*789Sahrens offsetof(vdev_label_t, vl_uberblock[n]), 550*789Sahrens sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL); 551*789Sahrens 552*789Sahrens dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg); 553*789Sahrens } 554*789Sahrens 555*789Sahrens static int 556*789Sahrens vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg) 557*789Sahrens { 558*789Sahrens uberblock_phys_t *ubphys; 559*789Sahrens uint64_t *good_writes; 560*789Sahrens zio_t *zio; 561*789Sahrens int error; 562*789Sahrens 563*789Sahrens ubphys = zio_buf_alloc(sizeof (uberblock_phys_t)); 564*789Sahrens bzero(ubphys, sizeof (uberblock_phys_t)); 565*789Sahrens ubphys->ubp_uberblock = *ub; 566*789Sahrens 567*789Sahrens good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 568*789Sahrens 569*789Sahrens zio = zio_root(spa, NULL, good_writes, 570*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 571*789Sahrens 572*789Sahrens vdev_uberblock_sync(zio, ubphys, uvd, txg); 573*789Sahrens 574*789Sahrens error = zio_wait(zio); 575*789Sahrens 576*789Sahrens if (error && *good_writes != 0) { 577*789Sahrens dprintf("partial success: good_writes = %llu\n", *good_writes); 578*789Sahrens error = 0; 579*789Sahrens } 580*789Sahrens 581*789Sahrens /* 582*789Sahrens * It's possible to have no good writes and no error if every vdev is in 583*789Sahrens * the CANT_OPEN state. 584*789Sahrens */ 585*789Sahrens if (*good_writes == 0 && error == 0) 586*789Sahrens error = EIO; 587*789Sahrens 588*789Sahrens kmem_free(good_writes, sizeof (uint64_t)); 589*789Sahrens zio_buf_free(ubphys, sizeof (uberblock_phys_t)); 590*789Sahrens 591*789Sahrens return (error); 592*789Sahrens } 593*789Sahrens 594*789Sahrens /* 595*789Sahrens * Sync out an individual vdev. 596*789Sahrens */ 597*789Sahrens static void 598*789Sahrens vdev_sync_label_done(zio_t *zio) 599*789Sahrens { 600*789Sahrens uint64_t *good_writes = zio->io_root->io_private; 601*789Sahrens 602*789Sahrens if (zio->io_error == 0) 603*789Sahrens atomic_add_64(good_writes, 1); 604*789Sahrens } 605*789Sahrens 606*789Sahrens static void 607*789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg) 608*789Sahrens { 609*789Sahrens nvlist_t *label; 610*789Sahrens vdev_phys_t *vp; 611*789Sahrens char *buf; 612*789Sahrens size_t buflen; 613*789Sahrens int c; 614*789Sahrens 615*789Sahrens for (c = 0; c < vd->vdev_children; c++) 616*789Sahrens vdev_sync_label(zio, vd->vdev_child[c], l, txg); 617*789Sahrens 618*789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 619*789Sahrens return; 620*789Sahrens 621*789Sahrens if (vdev_is_dead(vd)) 622*789Sahrens return; 623*789Sahrens 624*789Sahrens /* 625*789Sahrens * Generate a label describing the top-level config to which we belong. 626*789Sahrens */ 627*789Sahrens label = spa_config_generate(vd->vdev_spa, vd, txg, 0); 628*789Sahrens 629*789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 630*789Sahrens bzero(vp, sizeof (vdev_phys_t)); 631*789Sahrens 632*789Sahrens buf = vp->vp_nvlist; 633*789Sahrens buflen = sizeof (vp->vp_nvlist); 634*789Sahrens 635*789Sahrens if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, 0) == 0) 636*789Sahrens vdev_label_write(zio, vd, l, vp, 637*789Sahrens offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t), 638*789Sahrens vdev_sync_label_done, NULL); 639*789Sahrens 640*789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 641*789Sahrens nvlist_free(label); 642*789Sahrens 643*789Sahrens dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg); 644*789Sahrens } 645*789Sahrens 646*789Sahrens static int 647*789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg) 648*789Sahrens { 649*789Sahrens uint64_t *good_writes; 650*789Sahrens zio_t *zio; 651*789Sahrens int error; 652*789Sahrens 653*789Sahrens ASSERT(vd == vd->vdev_top); 654*789Sahrens 655*789Sahrens good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 656*789Sahrens 657*789Sahrens zio = zio_root(vd->vdev_spa, NULL, good_writes, 658*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 659*789Sahrens 660*789Sahrens /* 661*789Sahrens * Recursively kick off writes to all labels. 662*789Sahrens */ 663*789Sahrens vdev_sync_label(zio, vd, l, txg); 664*789Sahrens 665*789Sahrens error = zio_wait(zio); 666*789Sahrens 667*789Sahrens if (error && *good_writes != 0) { 668*789Sahrens dprintf("partial success: good_writes = %llu\n", *good_writes); 669*789Sahrens error = 0; 670*789Sahrens } 671*789Sahrens 672*789Sahrens if (*good_writes == 0 && error == 0) 673*789Sahrens error = ENODEV; 674*789Sahrens 675*789Sahrens kmem_free(good_writes, sizeof (uint64_t)); 676*789Sahrens 677*789Sahrens return (error); 678*789Sahrens } 679*789Sahrens 680*789Sahrens /* 681*789Sahrens * Sync the entire vdev configuration. 682*789Sahrens * 683*789Sahrens * The order of operations is carefully crafted to ensure that 684*789Sahrens * if the system panics or loses power at any time, the state on disk 685*789Sahrens * is still transactionally consistent. The in-line comments below 686*789Sahrens * describe the failure semantics at each stage. 687*789Sahrens * 688*789Sahrens * Moreover, it is designed to be idempotent: if spa_sync_labels() fails 689*789Sahrens * at any time, you can just call it again, and it will resume its work. 690*789Sahrens */ 691*789Sahrens int 692*789Sahrens spa_sync_labels(spa_t *spa, uint64_t txg) 693*789Sahrens { 694*789Sahrens uberblock_t *ub = &spa->spa_uberblock; 695*789Sahrens vdev_t *rvd = spa->spa_root_vdev; 696*789Sahrens vdev_t *vd, *uvd; 697*789Sahrens zio_t *zio; 698*789Sahrens int c, l, error; 699*789Sahrens 700*789Sahrens ASSERT(ub->ub_txg <= txg); 701*789Sahrens 702*789Sahrens /* 703*789Sahrens * If this isn't a resync due to I/O errors, and nothing changed 704*789Sahrens * in this transaction group, and the vdev configuration hasn't changed, 705*789Sahrens * and this isn't an explicit sync-all, then there's nothing to do. 706*789Sahrens */ 707*789Sahrens if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE && 708*789Sahrens list_is_empty(&spa->spa_dirty_list)) { 709*789Sahrens dprintf("nothing to sync in %s in txg %llu\n", 710*789Sahrens spa_name(spa), txg); 711*789Sahrens return (0); 712*789Sahrens } 713*789Sahrens 714*789Sahrens if (txg > spa_freeze_txg(spa)) 715*789Sahrens return (0); 716*789Sahrens 717*789Sahrens dprintf("syncing %s txg %llu\n", spa_name(spa), txg); 718*789Sahrens 719*789Sahrens /* 720*789Sahrens * Flush the write cache of every disk that's been written to 721*789Sahrens * in this transaction group. This ensures that all blocks 722*789Sahrens * written in this txg will be committed to stable storage 723*789Sahrens * before any uberblock that references them. 724*789Sahrens */ 725*789Sahrens zio = zio_root(spa, NULL, NULL, 726*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 727*789Sahrens for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 728*789Sahrens vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) { 729*789Sahrens zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 730*789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 731*789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 732*789Sahrens } 733*789Sahrens (void) zio_wait(zio); 734*789Sahrens 735*789Sahrens /* 736*789Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 737*789Sahrens * system dies in the middle of this process, that's OK: all of the 738*789Sahrens * even labels that made it to disk will be newer than any uberblock, 739*789Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 740*789Sahrens * which have not yet been touched, will still be valid. 741*789Sahrens */ 742*789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 743*789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 744*789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 745*789Sahrens if (l & 1) 746*789Sahrens continue; 747*789Sahrens if ((error = vdev_sync_labels(vd, l, txg)) != 0) 748*789Sahrens return (error); 749*789Sahrens } 750*789Sahrens } 751*789Sahrens 752*789Sahrens /* 753*789Sahrens * Flush the new labels to disk. This ensures that all even-label 754*789Sahrens * updates are committed to stable storage before the uberblock update. 755*789Sahrens */ 756*789Sahrens zio = zio_root(spa, NULL, NULL, 757*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 758*789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 759*789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 760*789Sahrens zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 761*789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 762*789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 763*789Sahrens } 764*789Sahrens (void) zio_wait(zio); 765*789Sahrens 766*789Sahrens /* 767*789Sahrens * If there are any dirty vdevs, sync the uberblock to all vdevs. 768*789Sahrens * Otherwise, pick one top-level vdev at random. 769*789Sahrens */ 770*789Sahrens if (!list_is_empty(&spa->spa_dirty_list)) 771*789Sahrens uvd = rvd; 772*789Sahrens else 773*789Sahrens uvd = rvd->vdev_child[spa_get_random(rvd->vdev_children)]; 774*789Sahrens 775*789Sahrens /* 776*789Sahrens * Sync the uberblocks. If the system dies in the middle of this 777*789Sahrens * step, there are two cases to consider, and the on-disk state 778*789Sahrens * is consistent either way: 779*789Sahrens * 780*789Sahrens * (1) If none of the new uberblocks made it to disk, then the 781*789Sahrens * previous uberblock will be the newest, and the odd labels 782*789Sahrens * (which had not yet been touched) will be valid with respect 783*789Sahrens * to that uberblock. 784*789Sahrens * 785*789Sahrens * (2) If one or more new uberblocks made it to disk, then they 786*789Sahrens * will be the newest, and the even labels (which had all 787*789Sahrens * been successfully committed) will be valid with respect 788*789Sahrens * to the new uberblocks. 789*789Sahrens */ 790*789Sahrens if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0) 791*789Sahrens return (error); 792*789Sahrens 793*789Sahrens /* 794*789Sahrens * Flush the uberblocks to disk. This ensures that the odd labels 795*789Sahrens * are no longer needed (because the new uberblocks and the even 796*789Sahrens * labels are safely on disk), so it is safe to overwrite them. 797*789Sahrens */ 798*789Sahrens (void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE, 799*789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 800*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 801*789Sahrens 802*789Sahrens /* 803*789Sahrens * Sync out odd labels for every dirty vdev. If the system dies 804*789Sahrens * in the middle of this process, the even labels and the new 805*789Sahrens * uberblocks will suffice to open the pool. The next time 806*789Sahrens * the pool is opened, the first thing we'll do -- before any 807*789Sahrens * user data is modified -- is mark every vdev dirty so that 808*789Sahrens * all labels will be brought up to date. 809*789Sahrens */ 810*789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 811*789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 812*789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 813*789Sahrens if ((l & 1) == 0) 814*789Sahrens continue; 815*789Sahrens if ((error = vdev_sync_labels(vd, l, txg)) != 0) 816*789Sahrens return (error); 817*789Sahrens } 818*789Sahrens } 819*789Sahrens 820*789Sahrens /* 821*789Sahrens * Flush the new labels to disk. This ensures that all odd-label 822*789Sahrens * updates are committed to stable storage before the next 823*789Sahrens * transaction group begins. 824*789Sahrens */ 825*789Sahrens zio = zio_root(spa, NULL, NULL, 826*789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 827*789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 828*789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 829*789Sahrens zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 830*789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 831*789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 832*789Sahrens } 833*789Sahrens (void) zio_wait(zio); 834*789Sahrens 835*789Sahrens /* 836*789Sahrens * Clear the dirty list. 837*789Sahrens */ 838*789Sahrens while (!list_is_empty(&spa->spa_dirty_list)) 839*789Sahrens vdev_config_clean(list_head(&spa->spa_dirty_list)); 840*789Sahrens 841*789Sahrens #ifdef DEBUG 842*789Sahrens for (c = 0; c < rvd->vdev_children; c++) { 843*789Sahrens ASSERT(rvd->vdev_child[c]->vdev_is_dirty == 0); 844*789Sahrens } 845*789Sahrens #endif 846*789Sahrens 847*789Sahrens return (0); 848*789Sahrens } 849