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 /* 221485Slling * Copyright 2006 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 65789Sahrens * labels and an uberblock with the following transacation 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 { 155789Sahrens return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 156789Sahrens 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 157789Sahrens } 158789Sahrens 159789Sahrens static void 160789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 161789Sahrens uint64_t size, zio_done_func_t *done, void *private) 162789Sahrens { 163789Sahrens ASSERT(vd->vdev_children == 0); 164789Sahrens 165789Sahrens zio_nowait(zio_read_phys(zio, vd, 166789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 167789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 1681544Seschrock ZIO_PRIORITY_SYNC_READ, 1691544Seschrock ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE)); 170789Sahrens } 171789Sahrens 172789Sahrens static void 173789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 174789Sahrens uint64_t size, zio_done_func_t *done, void *private) 175789Sahrens { 176789Sahrens ASSERT(vd->vdev_children == 0); 177789Sahrens 178789Sahrens zio_nowait(zio_write_phys(zio, vd, 179789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 180789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 1811544Seschrock ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL)); 182789Sahrens } 183789Sahrens 184789Sahrens /* 185789Sahrens * Generate the nvlist representing this vdev's config. 186789Sahrens */ 187789Sahrens nvlist_t * 188789Sahrens vdev_config_generate(vdev_t *vd, int getstats) 189789Sahrens { 190789Sahrens nvlist_t *nv = NULL; 191789Sahrens 1921544Seschrock VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 193789Sahrens 194789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 195789Sahrens vd->vdev_ops->vdev_op_type) == 0); 196789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) == 0); 197789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 198789Sahrens 199789Sahrens if (vd->vdev_path != NULL) 200789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 201789Sahrens vd->vdev_path) == 0); 202789Sahrens 203789Sahrens if (vd->vdev_devid != NULL) 204789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 205789Sahrens vd->vdev_devid) == 0); 206789Sahrens 2071171Seschrock if (vd->vdev_wholedisk != -1ULL) 2081171Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2091171Seschrock vd->vdev_wholedisk) == 0); 2101171Seschrock 2111544Seschrock if (vd->vdev_not_present) 2121544Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 2131544Seschrock 214789Sahrens if (vd == vd->vdev_top) { 215789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 216789Sahrens vd->vdev_ms_array) == 0); 217789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 218789Sahrens vd->vdev_ms_shift) == 0); 219789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 220789Sahrens vd->vdev_ashift) == 0); 221789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 222789Sahrens vd->vdev_asize) == 0); 223789Sahrens } 224789Sahrens 225789Sahrens if (vd->vdev_dtl.smo_object != 0) 226789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 227789Sahrens vd->vdev_dtl.smo_object) == 0); 228789Sahrens 229789Sahrens if (getstats) { 230789Sahrens vdev_stat_t vs; 231789Sahrens vdev_get_stats(vd, &vs); 232789Sahrens VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 233789Sahrens (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 234789Sahrens } 235789Sahrens 236789Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 237789Sahrens nvlist_t **child; 238789Sahrens int c; 239789Sahrens 240789Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 241789Sahrens KM_SLEEP); 242789Sahrens 243789Sahrens for (c = 0; c < vd->vdev_children; c++) 244789Sahrens child[c] = vdev_config_generate(vd->vdev_child[c], 245789Sahrens getstats); 246789Sahrens 247789Sahrens VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 248789Sahrens child, vd->vdev_children) == 0); 249789Sahrens 250789Sahrens for (c = 0; c < vd->vdev_children; c++) 251789Sahrens nvlist_free(child[c]); 252789Sahrens 253789Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 2541485Slling 2551485Slling } else { 2561485Slling if (!vd->vdev_tmpoffline) { 2571485Slling if (vd->vdev_offline) 2581485Slling VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 2591485Slling B_TRUE) == 0); 2601485Slling else 2611485Slling (void) nvlist_remove(nv, ZPOOL_CONFIG_OFFLINE, 2621485Slling DATA_TYPE_UINT64); 2631485Slling } 264789Sahrens } 265789Sahrens 266789Sahrens return (nv); 267789Sahrens } 268789Sahrens 269789Sahrens nvlist_t * 270789Sahrens vdev_label_read_config(vdev_t *vd) 271789Sahrens { 2721635Sbonwick spa_t *spa = vd->vdev_spa; 273789Sahrens nvlist_t *config = NULL; 274789Sahrens vdev_phys_t *vp; 275789Sahrens zio_t *zio; 276789Sahrens int l; 277789Sahrens 2781635Sbonwick ASSERT(spa_config_held(spa, RW_READER)); 2791635Sbonwick 280789Sahrens if (vdev_is_dead(vd)) 281789Sahrens return (NULL); 282789Sahrens 283789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 284789Sahrens 285789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 286789Sahrens 2871635Sbonwick zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL | 2881544Seschrock ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CONFIG_HELD); 289789Sahrens 290789Sahrens vdev_label_read(zio, vd, l, vp, 291789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 292789Sahrens sizeof (vdev_phys_t), NULL, NULL); 293789Sahrens 294789Sahrens if (zio_wait(zio) == 0 && 295789Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 2961544Seschrock &config, 0) == 0) 297789Sahrens break; 298789Sahrens 299789Sahrens if (config != NULL) { 300789Sahrens nvlist_free(config); 301789Sahrens config = NULL; 302789Sahrens } 303789Sahrens } 304789Sahrens 305789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 306789Sahrens 307789Sahrens return (config); 308789Sahrens } 309789Sahrens 310789Sahrens int 311789Sahrens vdev_label_init(vdev_t *vd, uint64_t crtxg) 312789Sahrens { 313789Sahrens spa_t *spa = vd->vdev_spa; 314789Sahrens nvlist_t *label; 315789Sahrens vdev_phys_t *vp; 316789Sahrens vdev_boot_header_t *vb; 317789Sahrens uberblock_phys_t *ubphys; 318789Sahrens zio_t *zio; 319789Sahrens int l, c, n; 320789Sahrens char *buf; 321789Sahrens size_t buflen; 322789Sahrens int error; 323789Sahrens 3241635Sbonwick ASSERT(spa_config_held(spa, RW_WRITER)); 3251635Sbonwick 326789Sahrens for (c = 0; c < vd->vdev_children; c++) 327789Sahrens if ((error = vdev_label_init(vd->vdev_child[c], crtxg)) != 0) 328789Sahrens return (error); 329789Sahrens 330789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 331789Sahrens return (0); 332789Sahrens 333789Sahrens /* 334789Sahrens * Make sure each leaf device is writable, and zero its initial content. 335789Sahrens * Along the way, also make sure that no leaf is already in use. 336789Sahrens * Note that it's important to do this sequentially, not in parallel, 337789Sahrens * so that we catch cases of multiple use of the same leaf vdev in 338789Sahrens * the vdev we're creating -- e.g. mirroring a disk with itself. 339789Sahrens */ 340789Sahrens if (vdev_is_dead(vd)) 341789Sahrens return (EIO); 342789Sahrens 343789Sahrens /* 344789Sahrens * Check whether this device is already in use. 345789Sahrens * Ignore the check if crtxg == 0, which we use for device removal. 346789Sahrens */ 3471544Seschrock if (crtxg != 0 && 3481544Seschrock (label = vdev_label_read_config(vd)) != NULL) { 3491544Seschrock uint64_t state, pool_guid, device_guid, txg; 350789Sahrens uint64_t mycrtxg = 0; 351789Sahrens 352789Sahrens (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 353789Sahrens &mycrtxg); 354789Sahrens 3551544Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 356789Sahrens &state) == 0 && state == POOL_STATE_ACTIVE && 357789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 358789Sahrens &pool_guid) == 0 && 359789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 360789Sahrens &device_guid) == 0 && 361789Sahrens spa_guid_exists(pool_guid, device_guid) && 362789Sahrens nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 363789Sahrens &txg) == 0 && (txg != 0 || mycrtxg == crtxg)) { 364789Sahrens dprintf("vdev %s in use, pool_state %d\n", 365789Sahrens vdev_description(vd), state); 366789Sahrens nvlist_free(label); 367789Sahrens return (EBUSY); 368789Sahrens } 369789Sahrens nvlist_free(label); 370789Sahrens } 371789Sahrens 372789Sahrens /* 373789Sahrens * The device isn't in use, so initialize its label. 374789Sahrens */ 375789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 376789Sahrens bzero(vp, sizeof (vdev_phys_t)); 377789Sahrens 378789Sahrens /* 379789Sahrens * Generate a label describing the pool and our top-level vdev. 380789Sahrens * We mark it as being from txg 0 to indicate that it's not 381789Sahrens * really part of an active pool just yet. The labels will 382789Sahrens * be written again with a meaningful txg by spa_sync(). 383789Sahrens */ 3841635Sbonwick label = spa_config_generate(spa, vd, 0ULL, B_FALSE); 385789Sahrens 386789Sahrens /* 387789Sahrens * Add our creation time. This allows us to detect multiple vdev 388789Sahrens * uses as described above, and automatically expires if we fail. 389789Sahrens */ 390789Sahrens VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, crtxg) == 0); 391789Sahrens 392789Sahrens buf = vp->vp_nvlist; 393789Sahrens buflen = sizeof (vp->vp_nvlist); 394789Sahrens 3951544Seschrock if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) != 0) { 396789Sahrens nvlist_free(label); 397789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 398789Sahrens return (EINVAL); 399789Sahrens } 400789Sahrens 401789Sahrens /* 402789Sahrens * Initialize boot block header. 403789Sahrens */ 404789Sahrens vb = zio_buf_alloc(sizeof (vdev_boot_header_t)); 405789Sahrens bzero(vb, sizeof (vdev_boot_header_t)); 406789Sahrens vb->vb_magic = VDEV_BOOT_MAGIC; 407789Sahrens vb->vb_version = VDEV_BOOT_VERSION; 408789Sahrens vb->vb_offset = VDEV_BOOT_OFFSET; 409789Sahrens vb->vb_size = VDEV_BOOT_SIZE; 410789Sahrens 411789Sahrens /* 412789Sahrens * Initialize uberblock template. 413789Sahrens */ 414789Sahrens ubphys = zio_buf_alloc(sizeof (uberblock_phys_t)); 415789Sahrens bzero(ubphys, sizeof (uberblock_phys_t)); 416789Sahrens ubphys->ubp_uberblock = spa->spa_uberblock; 417789Sahrens ubphys->ubp_uberblock.ub_txg = 0; 418789Sahrens 419789Sahrens /* 420789Sahrens * Write everything in parallel. 421789Sahrens */ 422789Sahrens zio = zio_root(spa, NULL, NULL, 423789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 424789Sahrens 425789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 426789Sahrens 427789Sahrens vdev_label_write(zio, vd, l, vp, 428789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 429789Sahrens sizeof (vdev_phys_t), NULL, NULL); 430789Sahrens 431789Sahrens vdev_label_write(zio, vd, l, vb, 432789Sahrens offsetof(vdev_label_t, vl_boot_header), 433789Sahrens sizeof (vdev_boot_header_t), NULL, NULL); 434789Sahrens 435789Sahrens for (n = 0; n < VDEV_UBERBLOCKS; n++) { 436789Sahrens 437789Sahrens vdev_label_write(zio, vd, l, ubphys, 438789Sahrens offsetof(vdev_label_t, vl_uberblock[n]), 439789Sahrens sizeof (uberblock_phys_t), NULL, NULL); 440789Sahrens 441789Sahrens } 442789Sahrens } 443789Sahrens 444789Sahrens error = zio_wait(zio); 445789Sahrens 446789Sahrens nvlist_free(label); 447789Sahrens zio_buf_free(ubphys, sizeof (uberblock_phys_t)); 448789Sahrens zio_buf_free(vb, sizeof (vdev_boot_header_t)); 449789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 450789Sahrens 451789Sahrens return (error); 452789Sahrens } 453789Sahrens 454789Sahrens /* 455789Sahrens * ========================================================================== 456789Sahrens * uberblock load/sync 457789Sahrens * ========================================================================== 458789Sahrens */ 459789Sahrens 460789Sahrens /* 461789Sahrens * Consider the following situation: txg is safely synced to disk. We've 462789Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 463789Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 464789Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 465789Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 466789Sahrens * the missing replica comes back, then for a new seconds we'll have two 467789Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 468789Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 469789Sahrens */ 470789Sahrens static int 471789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 472789Sahrens { 473789Sahrens if (ub1->ub_txg < ub2->ub_txg) 474789Sahrens return (-1); 475789Sahrens if (ub1->ub_txg > ub2->ub_txg) 476789Sahrens return (1); 477789Sahrens 478789Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 479789Sahrens return (-1); 480789Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 481789Sahrens return (1); 482789Sahrens 483789Sahrens return (0); 484789Sahrens } 485789Sahrens 486789Sahrens static void 487789Sahrens vdev_uberblock_load_done(zio_t *zio) 488789Sahrens { 489789Sahrens uberblock_phys_t *ubphys = zio->io_data; 490789Sahrens uberblock_t *ub = &ubphys->ubp_uberblock; 491789Sahrens uberblock_t *ubbest = zio->io_private; 492789Sahrens spa_t *spa = zio->io_spa; 493789Sahrens 494789Sahrens ASSERT3U(zio->io_size, ==, sizeof (uberblock_phys_t)); 495789Sahrens 4961544Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 497789Sahrens mutex_enter(&spa->spa_uberblock_lock); 498789Sahrens if (vdev_uberblock_compare(ub, ubbest) > 0) 499789Sahrens *ubbest = *ub; 500789Sahrens mutex_exit(&spa->spa_uberblock_lock); 501789Sahrens } 502789Sahrens 503789Sahrens zio_buf_free(zio->io_data, zio->io_size); 504789Sahrens } 505789Sahrens 506789Sahrens void 507789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 508789Sahrens { 509789Sahrens int l, c, n; 510789Sahrens 511789Sahrens for (c = 0; c < vd->vdev_children; c++) 512789Sahrens vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 513789Sahrens 514789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 515789Sahrens return; 516789Sahrens 517789Sahrens if (vdev_is_dead(vd)) 518789Sahrens return; 519789Sahrens 520789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 521789Sahrens for (n = 0; n < VDEV_UBERBLOCKS; n++) { 522789Sahrens vdev_label_read(zio, vd, l, 523789Sahrens zio_buf_alloc(sizeof (uberblock_phys_t)), 524789Sahrens offsetof(vdev_label_t, vl_uberblock[n]), 525789Sahrens sizeof (uberblock_phys_t), 526789Sahrens vdev_uberblock_load_done, ubbest); 527789Sahrens } 528789Sahrens } 529789Sahrens } 530789Sahrens 531789Sahrens /* 532789Sahrens * Write the uberblock to both labels of all leaves of the specified vdev. 5331635Sbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 534789Sahrens */ 535789Sahrens static void 536789Sahrens vdev_uberblock_sync_done(zio_t *zio) 537789Sahrens { 538789Sahrens uint64_t *good_writes = zio->io_root->io_private; 539789Sahrens 5401635Sbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 541789Sahrens atomic_add_64(good_writes, 1); 542789Sahrens } 543789Sahrens 544789Sahrens static void 545789Sahrens vdev_uberblock_sync(zio_t *zio, uberblock_phys_t *ubphys, vdev_t *vd, 546789Sahrens uint64_t txg) 547789Sahrens { 548789Sahrens int l, c, n; 549789Sahrens 550789Sahrens for (c = 0; c < vd->vdev_children; c++) 551789Sahrens vdev_uberblock_sync(zio, ubphys, vd->vdev_child[c], txg); 552789Sahrens 553789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 554789Sahrens return; 555789Sahrens 556789Sahrens if (vdev_is_dead(vd)) 557789Sahrens return; 558789Sahrens 559789Sahrens n = txg & (VDEV_UBERBLOCKS - 1); 560789Sahrens 561789Sahrens ASSERT(ubphys->ubp_uberblock.ub_txg == txg); 562789Sahrens 563789Sahrens for (l = 0; l < VDEV_LABELS; l++) 564789Sahrens vdev_label_write(zio, vd, l, ubphys, 565789Sahrens offsetof(vdev_label_t, vl_uberblock[n]), 566789Sahrens sizeof (uberblock_phys_t), vdev_uberblock_sync_done, NULL); 567789Sahrens 568789Sahrens dprintf("vdev %s in txg %llu\n", vdev_description(vd), txg); 569789Sahrens } 570789Sahrens 571789Sahrens static int 572789Sahrens vdev_uberblock_sync_tree(spa_t *spa, uberblock_t *ub, vdev_t *uvd, uint64_t txg) 573789Sahrens { 574789Sahrens uberblock_phys_t *ubphys; 575789Sahrens uint64_t *good_writes; 576789Sahrens zio_t *zio; 577789Sahrens int error; 578789Sahrens 579789Sahrens ubphys = zio_buf_alloc(sizeof (uberblock_phys_t)); 580789Sahrens bzero(ubphys, sizeof (uberblock_phys_t)); 581789Sahrens ubphys->ubp_uberblock = *ub; 582789Sahrens 583789Sahrens good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 584789Sahrens 585789Sahrens zio = zio_root(spa, NULL, good_writes, 586789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 587789Sahrens 588789Sahrens vdev_uberblock_sync(zio, ubphys, uvd, txg); 589789Sahrens 590789Sahrens error = zio_wait(zio); 591789Sahrens 592789Sahrens if (error && *good_writes != 0) { 593789Sahrens dprintf("partial success: good_writes = %llu\n", *good_writes); 594789Sahrens error = 0; 595789Sahrens } 596789Sahrens 597789Sahrens /* 598789Sahrens * It's possible to have no good writes and no error if every vdev is in 599789Sahrens * the CANT_OPEN state. 600789Sahrens */ 601789Sahrens if (*good_writes == 0 && error == 0) 602789Sahrens error = EIO; 603789Sahrens 604789Sahrens kmem_free(good_writes, sizeof (uint64_t)); 605789Sahrens zio_buf_free(ubphys, sizeof (uberblock_phys_t)); 606789Sahrens 607789Sahrens return (error); 608789Sahrens } 609789Sahrens 610789Sahrens /* 611789Sahrens * Sync out an individual vdev. 612789Sahrens */ 613789Sahrens static void 614789Sahrens vdev_sync_label_done(zio_t *zio) 615789Sahrens { 616789Sahrens uint64_t *good_writes = zio->io_root->io_private; 617789Sahrens 618789Sahrens if (zio->io_error == 0) 619789Sahrens atomic_add_64(good_writes, 1); 620789Sahrens } 621789Sahrens 622789Sahrens static void 623789Sahrens vdev_sync_label(zio_t *zio, vdev_t *vd, int l, uint64_t txg) 624789Sahrens { 625789Sahrens nvlist_t *label; 626789Sahrens vdev_phys_t *vp; 627789Sahrens char *buf; 628789Sahrens size_t buflen; 629789Sahrens int c; 630789Sahrens 631789Sahrens for (c = 0; c < vd->vdev_children; c++) 632789Sahrens vdev_sync_label(zio, vd->vdev_child[c], l, txg); 633789Sahrens 634789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 635789Sahrens return; 636789Sahrens 637789Sahrens if (vdev_is_dead(vd)) 638789Sahrens return; 639789Sahrens 640789Sahrens /* 641789Sahrens * Generate a label describing the top-level config to which we belong. 642789Sahrens */ 6431635Sbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 644789Sahrens 645789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 646789Sahrens bzero(vp, sizeof (vdev_phys_t)); 647789Sahrens 648789Sahrens buf = vp->vp_nvlist; 649789Sahrens buflen = sizeof (vp->vp_nvlist); 650789Sahrens 6511544Seschrock if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) 652789Sahrens vdev_label_write(zio, vd, l, vp, 653789Sahrens offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t), 654789Sahrens vdev_sync_label_done, NULL); 655789Sahrens 656789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 657789Sahrens nvlist_free(label); 658789Sahrens 659789Sahrens dprintf("%s label %d txg %llu\n", vdev_description(vd), l, txg); 660789Sahrens } 661789Sahrens 662789Sahrens static int 663789Sahrens vdev_sync_labels(vdev_t *vd, int l, uint64_t txg) 664789Sahrens { 665789Sahrens uint64_t *good_writes; 666789Sahrens zio_t *zio; 667789Sahrens int error; 668789Sahrens 669789Sahrens ASSERT(vd == vd->vdev_top); 670789Sahrens 671789Sahrens good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP); 672789Sahrens 673789Sahrens zio = zio_root(vd->vdev_spa, NULL, good_writes, 674789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 675789Sahrens 676789Sahrens /* 677789Sahrens * Recursively kick off writes to all labels. 678789Sahrens */ 679789Sahrens vdev_sync_label(zio, vd, l, txg); 680789Sahrens 681789Sahrens error = zio_wait(zio); 682789Sahrens 683789Sahrens if (error && *good_writes != 0) { 684789Sahrens dprintf("partial success: good_writes = %llu\n", *good_writes); 685789Sahrens error = 0; 686789Sahrens } 687789Sahrens 688789Sahrens if (*good_writes == 0 && error == 0) 689789Sahrens error = ENODEV; 690789Sahrens 691789Sahrens kmem_free(good_writes, sizeof (uint64_t)); 692789Sahrens 693789Sahrens return (error); 694789Sahrens } 695789Sahrens 696789Sahrens /* 697789Sahrens * Sync the entire vdev configuration. 698789Sahrens * 699789Sahrens * The order of operations is carefully crafted to ensure that 700789Sahrens * if the system panics or loses power at any time, the state on disk 701789Sahrens * is still transactionally consistent. The in-line comments below 702789Sahrens * describe the failure semantics at each stage. 703789Sahrens * 704789Sahrens * Moreover, it is designed to be idempotent: if spa_sync_labels() fails 705789Sahrens * at any time, you can just call it again, and it will resume its work. 706789Sahrens */ 707789Sahrens int 7081635Sbonwick vdev_config_sync(vdev_t *uvd, uint64_t txg) 709789Sahrens { 7101635Sbonwick spa_t *spa = uvd->vdev_spa; 711789Sahrens uberblock_t *ub = &spa->spa_uberblock; 712789Sahrens vdev_t *rvd = spa->spa_root_vdev; 7131635Sbonwick vdev_t *vd; 714789Sahrens zio_t *zio; 715*1637Sbonwick int l, error; 716789Sahrens 717789Sahrens ASSERT(ub->ub_txg <= txg); 718789Sahrens 719789Sahrens /* 720789Sahrens * If this isn't a resync due to I/O errors, and nothing changed 721789Sahrens * in this transaction group, and the vdev configuration hasn't changed, 7221635Sbonwick * then there's nothing to do. 723789Sahrens */ 724789Sahrens if (ub->ub_txg < txg && uberblock_update(ub, rvd, txg) == B_FALSE && 725789Sahrens list_is_empty(&spa->spa_dirty_list)) { 726789Sahrens dprintf("nothing to sync in %s in txg %llu\n", 727789Sahrens spa_name(spa), txg); 728789Sahrens return (0); 729789Sahrens } 730789Sahrens 731789Sahrens if (txg > spa_freeze_txg(spa)) 732789Sahrens return (0); 733789Sahrens 7341635Sbonwick ASSERT(txg <= spa->spa_final_txg); 7351635Sbonwick 736789Sahrens dprintf("syncing %s txg %llu\n", spa_name(spa), txg); 737789Sahrens 738789Sahrens /* 739789Sahrens * Flush the write cache of every disk that's been written to 740789Sahrens * in this transaction group. This ensures that all blocks 741789Sahrens * written in this txg will be committed to stable storage 742789Sahrens * before any uberblock that references them. 743789Sahrens */ 744789Sahrens zio = zio_root(spa, NULL, NULL, 745789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 746789Sahrens for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 747789Sahrens vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) { 748789Sahrens zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 749789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 750789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 751789Sahrens } 752789Sahrens (void) zio_wait(zio); 753789Sahrens 754789Sahrens /* 755789Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 756789Sahrens * system dies in the middle of this process, that's OK: all of the 757789Sahrens * even labels that made it to disk will be newer than any uberblock, 758789Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 759789Sahrens * which have not yet been touched, will still be valid. 760789Sahrens */ 761789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 762789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 763789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 764789Sahrens if (l & 1) 765789Sahrens continue; 766789Sahrens if ((error = vdev_sync_labels(vd, l, txg)) != 0) 767789Sahrens return (error); 768789Sahrens } 769789Sahrens } 770789Sahrens 771789Sahrens /* 772789Sahrens * Flush the new labels to disk. This ensures that all even-label 773789Sahrens * updates are committed to stable storage before the uberblock update. 774789Sahrens */ 775789Sahrens zio = zio_root(spa, NULL, NULL, 776789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 777789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 778789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 779789Sahrens zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 780789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 781789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 782789Sahrens } 783789Sahrens (void) zio_wait(zio); 784789Sahrens 785789Sahrens /* 7861635Sbonwick * Sync the uberblocks to all vdevs in the tree specified by uvd. 7871635Sbonwick * If the system dies in the middle of this step, there are two cases 7881635Sbonwick * to consider, and the on-disk state is consistent either way: 789789Sahrens * 790789Sahrens * (1) If none of the new uberblocks made it to disk, then the 791789Sahrens * previous uberblock will be the newest, and the odd labels 792789Sahrens * (which had not yet been touched) will be valid with respect 793789Sahrens * to that uberblock. 794789Sahrens * 795789Sahrens * (2) If one or more new uberblocks made it to disk, then they 796789Sahrens * will be the newest, and the even labels (which had all 797789Sahrens * been successfully committed) will be valid with respect 798789Sahrens * to the new uberblocks. 799789Sahrens */ 800789Sahrens if ((error = vdev_uberblock_sync_tree(spa, ub, uvd, txg)) != 0) 801789Sahrens return (error); 802789Sahrens 803789Sahrens /* 804789Sahrens * Flush the uberblocks to disk. This ensures that the odd labels 805789Sahrens * are no longer needed (because the new uberblocks and the even 806789Sahrens * labels are safely on disk), so it is safe to overwrite them. 807789Sahrens */ 808789Sahrens (void) zio_wait(zio_ioctl(NULL, spa, uvd, DKIOCFLUSHWRITECACHE, 809789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 810789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 811789Sahrens 812789Sahrens /* 813789Sahrens * Sync out odd labels for every dirty vdev. If the system dies 814789Sahrens * in the middle of this process, the even labels and the new 815789Sahrens * uberblocks will suffice to open the pool. The next time 816789Sahrens * the pool is opened, the first thing we'll do -- before any 817789Sahrens * user data is modified -- is mark every vdev dirty so that 818789Sahrens * all labels will be brought up to date. 819789Sahrens */ 820789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 821789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 822789Sahrens for (l = 0; l < VDEV_LABELS; l++) { 823789Sahrens if ((l & 1) == 0) 824789Sahrens continue; 825789Sahrens if ((error = vdev_sync_labels(vd, l, txg)) != 0) 826789Sahrens return (error); 827789Sahrens } 828789Sahrens } 829789Sahrens 830789Sahrens /* 831789Sahrens * Flush the new labels to disk. This ensures that all odd-label 832789Sahrens * updates are committed to stable storage before the next 833789Sahrens * transaction group begins. 834789Sahrens */ 835789Sahrens zio = zio_root(spa, NULL, NULL, 836789Sahrens ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 837789Sahrens for (vd = list_head(&spa->spa_dirty_list); vd != NULL; 838789Sahrens vd = list_next(&spa->spa_dirty_list, vd)) { 839789Sahrens zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 840789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 841789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 842789Sahrens } 843789Sahrens (void) zio_wait(zio); 844789Sahrens 845789Sahrens return (0); 846789Sahrens } 847