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 /* 228632SBill.Moore@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 27789Sahrens * Virtual Device Labels 28789Sahrens * --------------------- 29789Sahrens * 30789Sahrens * The vdev label serves several distinct purposes: 31789Sahrens * 32789Sahrens * 1. Uniquely identify this device as part of a ZFS pool and confirm its 33789Sahrens * identity within the pool. 34789Sahrens * 35789Sahrens * 2. Verify that all the devices given in a configuration are present 36789Sahrens * within the pool. 37789Sahrens * 38789Sahrens * 3. Determine the uberblock for the pool. 39789Sahrens * 40789Sahrens * 4. In case of an import operation, determine the configuration of the 41789Sahrens * toplevel vdev of which it is a part. 42789Sahrens * 43789Sahrens * 5. If an import operation cannot find all the devices in the pool, 44789Sahrens * provide enough information to the administrator to determine which 45789Sahrens * devices are missing. 46789Sahrens * 47789Sahrens * It is important to note that while the kernel is responsible for writing the 48789Sahrens * label, it only consumes the information in the first three cases. The 49789Sahrens * latter information is only consumed in userland when determining the 50789Sahrens * configuration to import a pool. 51789Sahrens * 52789Sahrens * 53789Sahrens * Label Organization 54789Sahrens * ------------------ 55789Sahrens * 56789Sahrens * Before describing the contents of the label, it's important to understand how 57789Sahrens * the labels are written and updated with respect to the uberblock. 58789Sahrens * 59789Sahrens * When the pool configuration is altered, either because it was newly created 60789Sahrens * or a device was added, we want to update all the labels such that we can deal 61789Sahrens * with fatal failure at any point. To this end, each disk has two labels which 62789Sahrens * are updated before and after the uberblock is synced. Assuming we have 634451Seschrock * labels and an uberblock with the following transaction groups: 64789Sahrens * 65789Sahrens * L1 UB L2 66789Sahrens * +------+ +------+ +------+ 67789Sahrens * | | | | | | 68789Sahrens * | t10 | | t10 | | t10 | 69789Sahrens * | | | | | | 70789Sahrens * +------+ +------+ +------+ 71789Sahrens * 72789Sahrens * In this stable state, the labels and the uberblock were all updated within 73789Sahrens * the same transaction group (10). Each label is mirrored and checksummed, so 74789Sahrens * that we can detect when we fail partway through writing the label. 75789Sahrens * 76789Sahrens * In order to identify which labels are valid, the labels are written in the 77789Sahrens * following manner: 78789Sahrens * 79789Sahrens * 1. For each vdev, update 'L1' to the new label 80789Sahrens * 2. Update the uberblock 81789Sahrens * 3. For each vdev, update 'L2' to the new label 82789Sahrens * 83789Sahrens * Given arbitrary failure, we can determine the correct label to use based on 84789Sahrens * the transaction group. If we fail after updating L1 but before updating the 85789Sahrens * UB, we will notice that L1's transaction group is greater than the uberblock, 86789Sahrens * so L2 must be valid. If we fail after writing the uberblock but before 87789Sahrens * writing L2, we will notice that L2's transaction group is less than L1, and 88789Sahrens * therefore L1 is valid. 89789Sahrens * 90789Sahrens * Another added complexity is that not every label is updated when the config 91789Sahrens * is synced. If we add a single device, we do not want to have to re-write 92789Sahrens * every label for every device in the pool. This means that both L1 and L2 may 93789Sahrens * be older than the pool uberblock, because the necessary information is stored 94789Sahrens * on another vdev. 95789Sahrens * 96789Sahrens * 97789Sahrens * On-disk Format 98789Sahrens * -------------- 99789Sahrens * 100789Sahrens * The vdev label consists of two distinct parts, and is wrapped within the 101789Sahrens * vdev_label_t structure. The label includes 8k of padding to permit legacy 102789Sahrens * VTOC disk labels, but is otherwise ignored. 103789Sahrens * 104789Sahrens * The first half of the label is a packed nvlist which contains pool wide 105789Sahrens * properties, per-vdev properties, and configuration information. It is 106789Sahrens * described in more detail below. 107789Sahrens * 108789Sahrens * The latter half of the label consists of a redundant array of uberblocks. 109789Sahrens * These uberblocks are updated whenever a transaction group is committed, 110789Sahrens * or when the configuration is updated. When a pool is loaded, we scan each 111789Sahrens * vdev for the 'best' uberblock. 112789Sahrens * 113789Sahrens * 114789Sahrens * Configuration Information 115789Sahrens * ------------------------- 116789Sahrens * 117789Sahrens * The nvlist describing the pool and vdev contains the following elements: 118789Sahrens * 119789Sahrens * version ZFS on-disk version 120789Sahrens * name Pool name 121789Sahrens * state Pool state 122789Sahrens * txg Transaction group in which this label was written 123789Sahrens * pool_guid Unique identifier for this pool 124789Sahrens * vdev_tree An nvlist describing vdev tree. 125789Sahrens * 126789Sahrens * Each leaf device label also contains the following: 127789Sahrens * 128789Sahrens * top_guid Unique ID for top-level vdev in which this is contained 129789Sahrens * guid Unique ID for the leaf vdev 130789Sahrens * 131789Sahrens * The 'vs' configuration follows the format described in 'spa_config.c'. 132789Sahrens */ 133789Sahrens 134789Sahrens #include <sys/zfs_context.h> 135789Sahrens #include <sys/spa.h> 136789Sahrens #include <sys/spa_impl.h> 137789Sahrens #include <sys/dmu.h> 138789Sahrens #include <sys/zap.h> 139789Sahrens #include <sys/vdev.h> 140789Sahrens #include <sys/vdev_impl.h> 141789Sahrens #include <sys/uberblock_impl.h> 142789Sahrens #include <sys/metaslab.h> 143789Sahrens #include <sys/zio.h> 144789Sahrens #include <sys/fs/zfs.h> 145789Sahrens 146789Sahrens /* 147789Sahrens * Basic routines to read and write from a vdev label. 148789Sahrens * Used throughout the rest of this file. 149789Sahrens */ 150789Sahrens uint64_t 151789Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset) 152789Sahrens { 1531732Sbonwick ASSERT(offset < sizeof (vdev_label_t)); 1544577Sahrens ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0); 1551732Sbonwick 156789Sahrens return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 157789Sahrens 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 158789Sahrens } 159789Sahrens 1606615Sgw25295 /* 1616615Sgw25295 * Returns back the vdev label associated with the passed in offset. 1626615Sgw25295 */ 1636615Sgw25295 int 1646615Sgw25295 vdev_label_number(uint64_t psize, uint64_t offset) 1656615Sgw25295 { 1666615Sgw25295 int l; 1676615Sgw25295 1686615Sgw25295 if (offset >= psize - VDEV_LABEL_END_SIZE) { 1696615Sgw25295 offset -= psize - VDEV_LABEL_END_SIZE; 1706615Sgw25295 offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t); 1716615Sgw25295 } 1726615Sgw25295 l = offset / sizeof (vdev_label_t); 1736615Sgw25295 return (l < VDEV_LABELS ? l : -1); 1746615Sgw25295 } 1756615Sgw25295 176789Sahrens static void 177789Sahrens vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 1787754SJeff.Bonwick@Sun.COM uint64_t size, zio_done_func_t *done, void *private, int flags) 179789Sahrens { 1807754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) == 1817754SJeff.Bonwick@Sun.COM SCL_STATE_ALL); 1827754SJeff.Bonwick@Sun.COM ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 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, 1877754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, flags, B_TRUE)); 188789Sahrens } 189789Sahrens 190789Sahrens static void 191789Sahrens vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 1925688Sbonwick uint64_t size, zio_done_func_t *done, void *private, int flags) 193789Sahrens { 1947754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL || 1957754SJeff.Bonwick@Sun.COM (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) == 1967754SJeff.Bonwick@Sun.COM (SCL_CONFIG | SCL_STATE) && 1977754SJeff.Bonwick@Sun.COM dsl_pool_sync_context(spa_get_dsl(zio->io_spa)))); 1987754SJeff.Bonwick@Sun.COM ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 199789Sahrens 200789Sahrens zio_nowait(zio_write_phys(zio, vd, 201789Sahrens vdev_label_offset(vd->vdev_psize, l, offset), 202789Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private, 2035688Sbonwick ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE)); 204789Sahrens } 205789Sahrens 206789Sahrens /* 207789Sahrens * Generate the nvlist representing this vdev's config. 208789Sahrens */ 209789Sahrens nvlist_t * 2102082Seschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 2115450Sbrendan boolean_t isspare, boolean_t isl2cache) 212789Sahrens { 213789Sahrens nvlist_t *nv = NULL; 214789Sahrens 2151544Seschrock VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 216789Sahrens 217789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 218789Sahrens vd->vdev_ops->vdev_op_type) == 0); 2195450Sbrendan if (!isspare && !isl2cache) 2202082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) 2212082Seschrock == 0); 222789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 223789Sahrens 224789Sahrens if (vd->vdev_path != NULL) 225789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 226789Sahrens vd->vdev_path) == 0); 227789Sahrens 228789Sahrens if (vd->vdev_devid != NULL) 229789Sahrens VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 230789Sahrens vd->vdev_devid) == 0); 231789Sahrens 2324451Seschrock if (vd->vdev_physpath != NULL) 2334451Seschrock VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 2344451Seschrock vd->vdev_physpath) == 0); 2354451Seschrock 2369425SEric.Schrock@Sun.COM if (vd->vdev_fru != NULL) 2379425SEric.Schrock@Sun.COM VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU, 2389425SEric.Schrock@Sun.COM vd->vdev_fru) == 0); 2399425SEric.Schrock@Sun.COM 2402082Seschrock if (vd->vdev_nparity != 0) { 2412082Seschrock ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 2422082Seschrock VDEV_TYPE_RAIDZ) == 0); 2432082Seschrock 2442082Seschrock /* 2452082Seschrock * Make sure someone hasn't managed to sneak a fancy new vdev 2462082Seschrock * into a crufty old storage pool. 2472082Seschrock */ 2482082Seschrock ASSERT(vd->vdev_nparity == 1 || 24910105Sadam.leventhal@sun.com (vd->vdev_nparity <= 2 && 25010105Sadam.leventhal@sun.com spa_version(spa) >= SPA_VERSION_RAIDZ2) || 25110105Sadam.leventhal@sun.com (vd->vdev_nparity <= 3 && 25210105Sadam.leventhal@sun.com spa_version(spa) >= SPA_VERSION_RAIDZ3)); 2532082Seschrock 2542082Seschrock /* 2552082Seschrock * Note that we'll add the nparity tag even on storage pools 2562082Seschrock * that only support a single parity device -- older software 2572082Seschrock * will just ignore it. 2582082Seschrock */ 2592082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, 2602082Seschrock vd->vdev_nparity) == 0); 2612082Seschrock } 2622082Seschrock 2631171Seschrock if (vd->vdev_wholedisk != -1ULL) 2641171Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2651171Seschrock vd->vdev_wholedisk) == 0); 2661171Seschrock 2671544Seschrock if (vd->vdev_not_present) 2681544Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 2691544Seschrock 2702082Seschrock if (vd->vdev_isspare) 2712082Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0); 2722082Seschrock 2735450Sbrendan if (!isspare && !isl2cache && vd == vd->vdev_top) { 274789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 275789Sahrens vd->vdev_ms_array) == 0); 276789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 277789Sahrens vd->vdev_ms_shift) == 0); 278789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 279789Sahrens vd->vdev_ashift) == 0); 280789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 281789Sahrens vd->vdev_asize) == 0); 2824527Sperrin VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, 2834527Sperrin vd->vdev_islog) == 0); 284789Sahrens } 285789Sahrens 2868241SJeff.Bonwick@Sun.COM if (vd->vdev_dtl_smo.smo_object != 0) 287789Sahrens VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 2888241SJeff.Bonwick@Sun.COM vd->vdev_dtl_smo.smo_object) == 0); 289789Sahrens 29010594SGeorge.Wilson@Sun.COM if (vd->vdev_crtxg) 29110594SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 29210594SGeorge.Wilson@Sun.COM vd->vdev_crtxg) == 0); 29310594SGeorge.Wilson@Sun.COM 294789Sahrens if (getstats) { 295789Sahrens vdev_stat_t vs; 296789Sahrens vdev_get_stats(vd, &vs); 297789Sahrens VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_STATS, 298789Sahrens (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 299789Sahrens } 300789Sahrens 301789Sahrens if (!vd->vdev_ops->vdev_op_leaf) { 302789Sahrens nvlist_t **child; 303789Sahrens int c; 304789Sahrens 30510594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 30610594SGeorge.Wilson@Sun.COM 307789Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 308789Sahrens KM_SLEEP); 309789Sahrens 310789Sahrens for (c = 0; c < vd->vdev_children; c++) 3112082Seschrock child[c] = vdev_config_generate(spa, vd->vdev_child[c], 3125450Sbrendan getstats, isspare, isl2cache); 313789Sahrens 314789Sahrens VERIFY(nvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 315789Sahrens child, vd->vdev_children) == 0); 316789Sahrens 317789Sahrens for (c = 0; c < vd->vdev_children; c++) 318789Sahrens nvlist_free(child[c]); 319789Sahrens 320789Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 3211485Slling 3221485Slling } else { 3231732Sbonwick if (vd->vdev_offline && !vd->vdev_tmpoffline) 3241485Slling VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 3251732Sbonwick B_TRUE) == 0); 3264451Seschrock if (vd->vdev_faulted) 3274451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, 3284451Seschrock B_TRUE) == 0); 3294451Seschrock if (vd->vdev_degraded) 3304451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, 3314451Seschrock B_TRUE) == 0); 3324451Seschrock if (vd->vdev_removed) 3334451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, 3344451Seschrock B_TRUE) == 0); 3354451Seschrock if (vd->vdev_unspare) 3364451Seschrock VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, 3374451Seschrock B_TRUE) == 0); 33810594SGeorge.Wilson@Sun.COM if (vd->vdev_ishole) 33910594SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, 34010594SGeorge.Wilson@Sun.COM B_TRUE) == 0); 341789Sahrens } 342789Sahrens 343789Sahrens return (nv); 344789Sahrens } 345789Sahrens 34610594SGeorge.Wilson@Sun.COM /* 34710594SGeorge.Wilson@Sun.COM * Generate a view of the top-level vdevs. If we currently have holes 34810594SGeorge.Wilson@Sun.COM * in the namespace, then generate an array which contains a list of holey 34910594SGeorge.Wilson@Sun.COM * vdevs. Additionally, add the number of top-level children that currently 35010594SGeorge.Wilson@Sun.COM * exist. 35110594SGeorge.Wilson@Sun.COM */ 35210594SGeorge.Wilson@Sun.COM void 35310594SGeorge.Wilson@Sun.COM vdev_top_config_generate(spa_t *spa, nvlist_t *config) 35410594SGeorge.Wilson@Sun.COM { 35510594SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 35610594SGeorge.Wilson@Sun.COM uint64_t *array; 35710594SGeorge.Wilson@Sun.COM uint_t idx; 35810594SGeorge.Wilson@Sun.COM 35910594SGeorge.Wilson@Sun.COM array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP); 36010594SGeorge.Wilson@Sun.COM 36110594SGeorge.Wilson@Sun.COM idx = 0; 36210594SGeorge.Wilson@Sun.COM for (int c = 0; c < rvd->vdev_children; c++) { 36310594SGeorge.Wilson@Sun.COM vdev_t *tvd = rvd->vdev_child[c]; 36410594SGeorge.Wilson@Sun.COM 36510594SGeorge.Wilson@Sun.COM if (tvd->vdev_ishole) 36610594SGeorge.Wilson@Sun.COM array[idx++] = c; 36710594SGeorge.Wilson@Sun.COM } 36810594SGeorge.Wilson@Sun.COM 369*10666SGeorge.Wilson@Sun.COM if (idx) { 370*10666SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY, 371*10666SGeorge.Wilson@Sun.COM array, idx) == 0); 372*10666SGeorge.Wilson@Sun.COM } 373*10666SGeorge.Wilson@Sun.COM 37410594SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 37510594SGeorge.Wilson@Sun.COM rvd->vdev_children) == 0); 37610594SGeorge.Wilson@Sun.COM 37710594SGeorge.Wilson@Sun.COM kmem_free(array, rvd->vdev_children * sizeof (uint64_t)); 37810594SGeorge.Wilson@Sun.COM } 37910594SGeorge.Wilson@Sun.COM 380789Sahrens nvlist_t * 381789Sahrens vdev_label_read_config(vdev_t *vd) 382789Sahrens { 3831635Sbonwick spa_t *spa = vd->vdev_spa; 384789Sahrens nvlist_t *config = NULL; 385789Sahrens vdev_phys_t *vp; 386789Sahrens zio_t *zio; 3879725SEric.Schrock@Sun.COM int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 3889725SEric.Schrock@Sun.COM ZIO_FLAG_SPECULATIVE; 389789Sahrens 3907754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3911635Sbonwick 3925329Sgw25295 if (!vdev_readable(vd)) 393789Sahrens return (NULL); 394789Sahrens 395789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 396789Sahrens 3979725SEric.Schrock@Sun.COM retry: 3987754SJeff.Bonwick@Sun.COM for (int l = 0; l < VDEV_LABELS; l++) { 399789Sahrens 4007754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, flags); 401789Sahrens 402789Sahrens vdev_label_read(zio, vd, l, vp, 403789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 4047754SJeff.Bonwick@Sun.COM sizeof (vdev_phys_t), NULL, NULL, flags); 405789Sahrens 406789Sahrens if (zio_wait(zio) == 0 && 407789Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 4081544Seschrock &config, 0) == 0) 409789Sahrens break; 410789Sahrens 411789Sahrens if (config != NULL) { 412789Sahrens nvlist_free(config); 413789Sahrens config = NULL; 414789Sahrens } 415789Sahrens } 416789Sahrens 4179725SEric.Schrock@Sun.COM if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) { 4189725SEric.Schrock@Sun.COM flags |= ZIO_FLAG_TRYHARD; 4199725SEric.Schrock@Sun.COM goto retry; 4209725SEric.Schrock@Sun.COM } 4219725SEric.Schrock@Sun.COM 422789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 423789Sahrens 424789Sahrens return (config); 425789Sahrens } 426789Sahrens 4273377Seschrock /* 4283377Seschrock * Determine if a device is in use. The 'spare_guid' parameter will be filled 4293377Seschrock * in with the device guid if this spare is active elsewhere on the system. 4303377Seschrock */ 4313377Seschrock static boolean_t 4323377Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 4335450Sbrendan uint64_t *spare_guid, uint64_t *l2cache_guid) 4343377Seschrock { 4353377Seschrock spa_t *spa = vd->vdev_spa; 4363377Seschrock uint64_t state, pool_guid, device_guid, txg, spare_pool; 4373377Seschrock uint64_t vdtxg = 0; 4383377Seschrock nvlist_t *label; 4393377Seschrock 4403377Seschrock if (spare_guid) 4413377Seschrock *spare_guid = 0ULL; 4425450Sbrendan if (l2cache_guid) 4435450Sbrendan *l2cache_guid = 0ULL; 4443377Seschrock 4453377Seschrock /* 4463377Seschrock * Read the label, if any, and perform some basic sanity checks. 4473377Seschrock */ 4483377Seschrock if ((label = vdev_label_read_config(vd)) == NULL) 4493377Seschrock return (B_FALSE); 4503377Seschrock 4513377Seschrock (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 4523377Seschrock &vdtxg); 4533377Seschrock 4543377Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 4553377Seschrock &state) != 0 || 4563377Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 4573377Seschrock &device_guid) != 0) { 4583377Seschrock nvlist_free(label); 4593377Seschrock return (B_FALSE); 4603377Seschrock } 4613377Seschrock 4625450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4633377Seschrock (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 4643377Seschrock &pool_guid) != 0 || 4653377Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 4663377Seschrock &txg) != 0)) { 4673377Seschrock nvlist_free(label); 4683377Seschrock return (B_FALSE); 4693377Seschrock } 4703377Seschrock 4713377Seschrock nvlist_free(label); 4723377Seschrock 4733377Seschrock /* 4743377Seschrock * Check to see if this device indeed belongs to the pool it claims to 4753377Seschrock * be a part of. The only way this is allowed is if the device is a hot 4763377Seschrock * spare (which we check for later on). 4773377Seschrock */ 4785450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4793377Seschrock !spa_guid_exists(pool_guid, device_guid) && 4807214Slling !spa_spare_exists(device_guid, NULL, NULL) && 4815450Sbrendan !spa_l2cache_exists(device_guid, NULL)) 4823377Seschrock return (B_FALSE); 4833377Seschrock 4843377Seschrock /* 4853377Seschrock * If the transaction group is zero, then this an initialized (but 4863377Seschrock * unused) label. This is only an error if the create transaction 4873377Seschrock * on-disk is the same as the one we're using now, in which case the 4883377Seschrock * user has attempted to add the same vdev multiple times in the same 4893377Seschrock * transaction. 4903377Seschrock */ 4915450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 4925450Sbrendan txg == 0 && vdtxg == crtxg) 4933377Seschrock return (B_TRUE); 4943377Seschrock 4953377Seschrock /* 4963377Seschrock * Check to see if this is a spare device. We do an explicit check for 4973377Seschrock * spa_has_spare() here because it may be on our pending list of spares 4985450Sbrendan * to add. We also check if it is an l2cache device. 4993377Seschrock */ 5007214Slling if (spa_spare_exists(device_guid, &spare_pool, NULL) || 5013377Seschrock spa_has_spare(spa, device_guid)) { 5023377Seschrock if (spare_guid) 5033377Seschrock *spare_guid = device_guid; 5043377Seschrock 5053377Seschrock switch (reason) { 5063377Seschrock case VDEV_LABEL_CREATE: 5075450Sbrendan case VDEV_LABEL_L2CACHE: 5083377Seschrock return (B_TRUE); 5093377Seschrock 5103377Seschrock case VDEV_LABEL_REPLACE: 5113377Seschrock return (!spa_has_spare(spa, device_guid) || 5123377Seschrock spare_pool != 0ULL); 5133377Seschrock 5143377Seschrock case VDEV_LABEL_SPARE: 5153377Seschrock return (spa_has_spare(spa, device_guid)); 5163377Seschrock } 5173377Seschrock } 5183377Seschrock 5193377Seschrock /* 5205450Sbrendan * Check to see if this is an l2cache device. 5215450Sbrendan */ 5225450Sbrendan if (spa_l2cache_exists(device_guid, NULL)) 5235450Sbrendan return (B_TRUE); 5245450Sbrendan 5255450Sbrendan /* 5263377Seschrock * If the device is marked ACTIVE, then this device is in use by another 5273377Seschrock * pool on the system. 5283377Seschrock */ 5293377Seschrock return (state == POOL_STATE_ACTIVE); 5303377Seschrock } 5313377Seschrock 5323377Seschrock /* 5333377Seschrock * Initialize a vdev label. We check to make sure each leaf device is not in 5343377Seschrock * use, and writable. We put down an initial label which we will later 5353377Seschrock * overwrite with a complete label. Note that it's important to do this 5363377Seschrock * sequentially, not in parallel, so that we catch cases of multiple use of the 5373377Seschrock * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 5383377Seschrock * itself. 5393377Seschrock */ 5403377Seschrock int 5413377Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 542789Sahrens { 543789Sahrens spa_t *spa = vd->vdev_spa; 544789Sahrens nvlist_t *label; 545789Sahrens vdev_phys_t *vp; 5469056SLin.Ling@Sun.COM char *pad2; 5471732Sbonwick uberblock_t *ub; 548789Sahrens zio_t *zio; 549789Sahrens char *buf; 550789Sahrens size_t buflen; 551789Sahrens int error; 5525450Sbrendan uint64_t spare_guid, l2cache_guid; 5537754SJeff.Bonwick@Sun.COM int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 554789Sahrens 5557754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5561635Sbonwick 5577754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 5583377Seschrock if ((error = vdev_label_init(vd->vdev_child[c], 5593377Seschrock crtxg, reason)) != 0) 560789Sahrens return (error); 561789Sahrens 56210594SGeorge.Wilson@Sun.COM /* Track the creation time for this vdev */ 56310594SGeorge.Wilson@Sun.COM vd->vdev_crtxg = crtxg; 56410594SGeorge.Wilson@Sun.COM 565789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 566789Sahrens return (0); 567789Sahrens 568789Sahrens /* 5693377Seschrock * Dead vdevs cannot be initialized. 570789Sahrens */ 571789Sahrens if (vdev_is_dead(vd)) 572789Sahrens return (EIO); 573789Sahrens 574789Sahrens /* 5753377Seschrock * Determine if the vdev is in use. 576789Sahrens */ 5773377Seschrock if (reason != VDEV_LABEL_REMOVE && 5785450Sbrendan vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 5793377Seschrock return (EBUSY); 580789Sahrens 5813377Seschrock /* 5825450Sbrendan * If this is a request to add or replace a spare or l2cache device 5835450Sbrendan * that is in use elsewhere on the system, then we must update the 5845450Sbrendan * guid (which was initialized to a random value) to reflect the 5855450Sbrendan * actual GUID (which is shared between multiple pools). 5863377Seschrock */ 5875450Sbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 5885450Sbrendan spare_guid != 0ULL) { 5897768SJeff.Bonwick@Sun.COM uint64_t guid_delta = spare_guid - vd->vdev_guid; 5902082Seschrock 5917768SJeff.Bonwick@Sun.COM vd->vdev_guid += guid_delta; 5927768SJeff.Bonwick@Sun.COM 5937768SJeff.Bonwick@Sun.COM for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 5947768SJeff.Bonwick@Sun.COM pvd->vdev_guid_sum += guid_delta; 5952082Seschrock 5963377Seschrock /* 5973377Seschrock * If this is a replacement, then we want to fallthrough to the 5983377Seschrock * rest of the code. If we're adding a spare, then it's already 5994451Seschrock * labeled appropriately and we can just return. 6003377Seschrock */ 6013377Seschrock if (reason == VDEV_LABEL_SPARE) 6023377Seschrock return (0); 6033377Seschrock ASSERT(reason == VDEV_LABEL_REPLACE); 604789Sahrens } 605789Sahrens 6065450Sbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 6075450Sbrendan l2cache_guid != 0ULL) { 6087768SJeff.Bonwick@Sun.COM uint64_t guid_delta = l2cache_guid - vd->vdev_guid; 6095450Sbrendan 6107768SJeff.Bonwick@Sun.COM vd->vdev_guid += guid_delta; 6117768SJeff.Bonwick@Sun.COM 6127768SJeff.Bonwick@Sun.COM for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 6137768SJeff.Bonwick@Sun.COM pvd->vdev_guid_sum += guid_delta; 6145450Sbrendan 6155450Sbrendan /* 6165450Sbrendan * If this is a replacement, then we want to fallthrough to the 6175450Sbrendan * rest of the code. If we're adding an l2cache, then it's 6185450Sbrendan * already labeled appropriately and we can just return. 6195450Sbrendan */ 6205450Sbrendan if (reason == VDEV_LABEL_L2CACHE) 6215450Sbrendan return (0); 6225450Sbrendan ASSERT(reason == VDEV_LABEL_REPLACE); 6235450Sbrendan } 6245450Sbrendan 625789Sahrens /* 6263377Seschrock * Initialize its label. 627789Sahrens */ 628789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 629789Sahrens bzero(vp, sizeof (vdev_phys_t)); 630789Sahrens 631789Sahrens /* 632789Sahrens * Generate a label describing the pool and our top-level vdev. 633789Sahrens * We mark it as being from txg 0 to indicate that it's not 634789Sahrens * really part of an active pool just yet. The labels will 635789Sahrens * be written again with a meaningful txg by spa_sync(). 636789Sahrens */ 6373377Seschrock if (reason == VDEV_LABEL_SPARE || 6383377Seschrock (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 6393377Seschrock /* 6403377Seschrock * For inactive hot spares, we generate a special label that 6413377Seschrock * identifies as a mutually shared hot spare. We write the 6423377Seschrock * label if we are adding a hot spare, or if we are removing an 6433377Seschrock * active hot spare (in which case we want to revert the 6443377Seschrock * labels). 6453377Seschrock */ 6462082Seschrock VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 647789Sahrens 6482082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 6492082Seschrock spa_version(spa)) == 0); 6502082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 6512082Seschrock POOL_STATE_SPARE) == 0); 6522082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 6532082Seschrock vd->vdev_guid) == 0); 6545450Sbrendan } else if (reason == VDEV_LABEL_L2CACHE || 6555450Sbrendan (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 6565450Sbrendan /* 6575450Sbrendan * For level 2 ARC devices, add a special label. 6585450Sbrendan */ 6595450Sbrendan VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6605450Sbrendan 6615450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 6625450Sbrendan spa_version(spa)) == 0); 6635450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 6645450Sbrendan POOL_STATE_L2CACHE) == 0); 6655450Sbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 6665450Sbrendan vd->vdev_guid) == 0); 6672082Seschrock } else { 6682082Seschrock label = spa_config_generate(spa, vd, 0ULL, B_FALSE); 6692082Seschrock 6702082Seschrock /* 6712082Seschrock * Add our creation time. This allows us to detect multiple 6722082Seschrock * vdev uses as described above, and automatically expires if we 6732082Seschrock * fail. 6742082Seschrock */ 6752082Seschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 6762082Seschrock crtxg) == 0); 6772082Seschrock } 678789Sahrens 679789Sahrens buf = vp->vp_nvlist; 680789Sahrens buflen = sizeof (vp->vp_nvlist); 681789Sahrens 6823460Smmusante error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 6833460Smmusante if (error != 0) { 684789Sahrens nvlist_free(label); 685789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 6863460Smmusante /* EFAULT means nvlist_pack ran out of room */ 6873460Smmusante return (error == EFAULT ? ENAMETOOLONG : EINVAL); 688789Sahrens } 689789Sahrens 690789Sahrens /* 691789Sahrens * Initialize uberblock template. 692789Sahrens */ 6939846SEric.Taylor@Sun.COM ub = zio_buf_alloc(VDEV_UBERBLOCK_RING); 6949846SEric.Taylor@Sun.COM bzero(ub, VDEV_UBERBLOCK_RING); 6951732Sbonwick *ub = spa->spa_uberblock; 6961732Sbonwick ub->ub_txg = 0; 697789Sahrens 6989056SLin.Ling@Sun.COM /* Initialize the 2nd padding area. */ 6999056SLin.Ling@Sun.COM pad2 = zio_buf_alloc(VDEV_PAD_SIZE); 7009056SLin.Ling@Sun.COM bzero(pad2, VDEV_PAD_SIZE); 7019056SLin.Ling@Sun.COM 702789Sahrens /* 703789Sahrens * Write everything in parallel. 704789Sahrens */ 7059725SEric.Schrock@Sun.COM retry: 7065688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 707789Sahrens 7087754SJeff.Bonwick@Sun.COM for (int l = 0; l < VDEV_LABELS; l++) { 709789Sahrens 710789Sahrens vdev_label_write(zio, vd, l, vp, 711789Sahrens offsetof(vdev_label_t, vl_vdev_phys), 7125688Sbonwick sizeof (vdev_phys_t), NULL, NULL, flags); 713789Sahrens 7149056SLin.Ling@Sun.COM /* 7159056SLin.Ling@Sun.COM * Skip the 1st padding area. 7169056SLin.Ling@Sun.COM * Zero out the 2nd padding area where it might have 7179056SLin.Ling@Sun.COM * left over data from previous filesystem format. 7189056SLin.Ling@Sun.COM */ 7199056SLin.Ling@Sun.COM vdev_label_write(zio, vd, l, pad2, 7209056SLin.Ling@Sun.COM offsetof(vdev_label_t, vl_pad2), 7219056SLin.Ling@Sun.COM VDEV_PAD_SIZE, NULL, NULL, flags); 7229056SLin.Ling@Sun.COM 7239846SEric.Taylor@Sun.COM vdev_label_write(zio, vd, l, ub, 7249846SEric.Taylor@Sun.COM offsetof(vdev_label_t, vl_uberblock), 7259846SEric.Taylor@Sun.COM VDEV_UBERBLOCK_RING, NULL, NULL, flags); 726789Sahrens } 727789Sahrens 728789Sahrens error = zio_wait(zio); 729789Sahrens 7309725SEric.Schrock@Sun.COM if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) { 7319725SEric.Schrock@Sun.COM flags |= ZIO_FLAG_TRYHARD; 7329725SEric.Schrock@Sun.COM goto retry; 7339725SEric.Schrock@Sun.COM } 7349725SEric.Schrock@Sun.COM 735789Sahrens nvlist_free(label); 7369056SLin.Ling@Sun.COM zio_buf_free(pad2, VDEV_PAD_SIZE); 7379846SEric.Taylor@Sun.COM zio_buf_free(ub, VDEV_UBERBLOCK_RING); 738789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 739789Sahrens 7403377Seschrock /* 7413377Seschrock * If this vdev hasn't been previously identified as a spare, then we 7424451Seschrock * mark it as such only if a) we are labeling it as a spare, or b) it 7435450Sbrendan * exists as a spare elsewhere in the system. Do the same for 7445450Sbrendan * level 2 ARC devices. 7453377Seschrock */ 7463377Seschrock if (error == 0 && !vd->vdev_isspare && 7473377Seschrock (reason == VDEV_LABEL_SPARE || 7487214Slling spa_spare_exists(vd->vdev_guid, NULL, NULL))) 7493377Seschrock spa_spare_add(vd); 7502082Seschrock 7515450Sbrendan if (error == 0 && !vd->vdev_isl2cache && 7525450Sbrendan (reason == VDEV_LABEL_L2CACHE || 7535450Sbrendan spa_l2cache_exists(vd->vdev_guid, NULL))) 7545450Sbrendan spa_l2cache_add(vd); 7555450Sbrendan 7563377Seschrock return (error); 7572082Seschrock } 7582082Seschrock 759789Sahrens /* 760789Sahrens * ========================================================================== 761789Sahrens * uberblock load/sync 762789Sahrens * ========================================================================== 763789Sahrens */ 764789Sahrens 765789Sahrens /* 7668188SVictor.Latushkin@Sun.COM * For use by zdb and debugging purposes only 7678188SVictor.Latushkin@Sun.COM */ 7688188SVictor.Latushkin@Sun.COM uint64_t ub_max_txg = UINT64_MAX; 7698188SVictor.Latushkin@Sun.COM 7708188SVictor.Latushkin@Sun.COM /* 771789Sahrens * Consider the following situation: txg is safely synced to disk. We've 772789Sahrens * written the first uberblock for txg + 1, and then we lose power. When we 773789Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say, 774789Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1 775789Sahrens * is now offline. If we then make some changes and sync txg + 1, and then 776789Sahrens * the missing replica comes back, then for a new seconds we'll have two 777789Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple: 778789Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp. 779789Sahrens */ 780789Sahrens static int 781789Sahrens vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 782789Sahrens { 783789Sahrens if (ub1->ub_txg < ub2->ub_txg) 784789Sahrens return (-1); 785789Sahrens if (ub1->ub_txg > ub2->ub_txg) 786789Sahrens return (1); 787789Sahrens 788789Sahrens if (ub1->ub_timestamp < ub2->ub_timestamp) 789789Sahrens return (-1); 790789Sahrens if (ub1->ub_timestamp > ub2->ub_timestamp) 791789Sahrens return (1); 792789Sahrens 793789Sahrens return (0); 794789Sahrens } 795789Sahrens 796789Sahrens static void 797789Sahrens vdev_uberblock_load_done(zio_t *zio) 798789Sahrens { 7997754SJeff.Bonwick@Sun.COM zio_t *rio = zio->io_private; 8001732Sbonwick uberblock_t *ub = zio->io_data; 8017754SJeff.Bonwick@Sun.COM uberblock_t *ubbest = rio->io_private; 802789Sahrens 8031732Sbonwick ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd)); 804789Sahrens 8051544Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 8067754SJeff.Bonwick@Sun.COM mutex_enter(&rio->io_lock); 8078188SVictor.Latushkin@Sun.COM if (ub->ub_txg <= ub_max_txg && 8088188SVictor.Latushkin@Sun.COM vdev_uberblock_compare(ub, ubbest) > 0) 809789Sahrens *ubbest = *ub; 8107754SJeff.Bonwick@Sun.COM mutex_exit(&rio->io_lock); 811789Sahrens } 812789Sahrens 813789Sahrens zio_buf_free(zio->io_data, zio->io_size); 814789Sahrens } 815789Sahrens 816789Sahrens void 817789Sahrens vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest) 818789Sahrens { 8197754SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 8207754SJeff.Bonwick@Sun.COM vdev_t *rvd = spa->spa_root_vdev; 8219725SEric.Schrock@Sun.COM int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 8229725SEric.Schrock@Sun.COM ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD; 823789Sahrens 8247754SJeff.Bonwick@Sun.COM if (vd == rvd) { 8257754SJeff.Bonwick@Sun.COM ASSERT(zio == NULL); 8267754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 8277754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, ubbest, flags); 8287754SJeff.Bonwick@Sun.COM bzero(ubbest, sizeof (uberblock_t)); 8297754SJeff.Bonwick@Sun.COM } 8307754SJeff.Bonwick@Sun.COM 8317754SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 8327754SJeff.Bonwick@Sun.COM 8337754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 834789Sahrens vdev_uberblock_load(zio, vd->vdev_child[c], ubbest); 835789Sahrens 8367754SJeff.Bonwick@Sun.COM if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 8377754SJeff.Bonwick@Sun.COM for (int l = 0; l < VDEV_LABELS; l++) { 8387754SJeff.Bonwick@Sun.COM for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 8397754SJeff.Bonwick@Sun.COM vdev_label_read(zio, vd, l, 8407754SJeff.Bonwick@Sun.COM zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 8417754SJeff.Bonwick@Sun.COM VDEV_UBERBLOCK_OFFSET(vd, n), 8427754SJeff.Bonwick@Sun.COM VDEV_UBERBLOCK_SIZE(vd), 8437754SJeff.Bonwick@Sun.COM vdev_uberblock_load_done, zio, flags); 8447754SJeff.Bonwick@Sun.COM } 8457754SJeff.Bonwick@Sun.COM } 8467754SJeff.Bonwick@Sun.COM } 847789Sahrens 8487754SJeff.Bonwick@Sun.COM if (vd == rvd) { 8497754SJeff.Bonwick@Sun.COM (void) zio_wait(zio); 8507754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 851789Sahrens } 852789Sahrens } 853789Sahrens 854789Sahrens /* 8555688Sbonwick * On success, increment root zio's count of good writes. 8561635Sbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 857789Sahrens */ 858789Sahrens static void 859789Sahrens vdev_uberblock_sync_done(zio_t *zio) 860789Sahrens { 8615688Sbonwick uint64_t *good_writes = zio->io_private; 862789Sahrens 8631635Sbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 864789Sahrens atomic_add_64(good_writes, 1); 865789Sahrens } 866789Sahrens 8675688Sbonwick /* 8685688Sbonwick * Write the uberblock to all labels of all leaves of the specified vdev. 8695688Sbonwick */ 870789Sahrens static void 8717754SJeff.Bonwick@Sun.COM vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags) 872789Sahrens { 8735688Sbonwick uberblock_t *ubbuf; 8747754SJeff.Bonwick@Sun.COM int n; 875789Sahrens 8767754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 8777754SJeff.Bonwick@Sun.COM vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags); 878789Sahrens 879789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 880789Sahrens return; 881789Sahrens 8827754SJeff.Bonwick@Sun.COM if (!vdev_writeable(vd)) 883789Sahrens return; 884789Sahrens 8855688Sbonwick n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 886789Sahrens 8875688Sbonwick ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 8885688Sbonwick bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 8891732Sbonwick *ubbuf = *ub; 890789Sahrens 8917754SJeff.Bonwick@Sun.COM for (int l = 0; l < VDEV_LABELS; l++) 8925688Sbonwick vdev_label_write(zio, vd, l, ubbuf, 8937754SJeff.Bonwick@Sun.COM VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd), 8945688Sbonwick vdev_uberblock_sync_done, zio->io_private, 8957754SJeff.Bonwick@Sun.COM flags | ZIO_FLAG_DONT_PROPAGATE); 896789Sahrens 8975688Sbonwick zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 8985688Sbonwick } 899789Sahrens 9005688Sbonwick int 9015688Sbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 9025688Sbonwick { 9035688Sbonwick spa_t *spa = svd[0]->vdev_spa; 9047754SJeff.Bonwick@Sun.COM zio_t *zio; 9055688Sbonwick uint64_t good_writes = 0; 906789Sahrens 9077754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, &good_writes, flags); 9087754SJeff.Bonwick@Sun.COM 9097754SJeff.Bonwick@Sun.COM for (int v = 0; v < svdcount; v++) 9107754SJeff.Bonwick@Sun.COM vdev_uberblock_sync(zio, ub, svd[v], flags); 9117754SJeff.Bonwick@Sun.COM 9125688Sbonwick (void) zio_wait(zio); 913789Sahrens 914789Sahrens /* 9155688Sbonwick * Flush the uberblocks to disk. This ensures that the odd labels 9165688Sbonwick * are no longer needed (because the new uberblocks and the even 9175688Sbonwick * labels are safely on disk), so it is safe to overwrite them. 918789Sahrens */ 9195688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 920789Sahrens 9217754SJeff.Bonwick@Sun.COM for (int v = 0; v < svdcount; v++) 9225688Sbonwick zio_flush(zio, svd[v]); 923789Sahrens 9245688Sbonwick (void) zio_wait(zio); 9255688Sbonwick 9265688Sbonwick return (good_writes >= 1 ? 0 : EIO); 927789Sahrens } 928789Sahrens 929789Sahrens /* 9305688Sbonwick * On success, increment the count of good writes for our top-level vdev. 931789Sahrens */ 932789Sahrens static void 9335688Sbonwick vdev_label_sync_done(zio_t *zio) 934789Sahrens { 9355688Sbonwick uint64_t *good_writes = zio->io_private; 936789Sahrens 937789Sahrens if (zio->io_error == 0) 938789Sahrens atomic_add_64(good_writes, 1); 939789Sahrens } 940789Sahrens 9415688Sbonwick /* 9425688Sbonwick * If there weren't enough good writes, indicate failure to the parent. 9435688Sbonwick */ 944789Sahrens static void 9455688Sbonwick vdev_label_sync_top_done(zio_t *zio) 9465688Sbonwick { 9475688Sbonwick uint64_t *good_writes = zio->io_private; 9485688Sbonwick 9495688Sbonwick if (*good_writes == 0) 9505688Sbonwick zio->io_error = EIO; 9515688Sbonwick 9525688Sbonwick kmem_free(good_writes, sizeof (uint64_t)); 9535688Sbonwick } 9545688Sbonwick 9555688Sbonwick /* 9567041Seschrock * We ignore errors for log and cache devices, simply free the private data. 9576976Seschrock */ 9586976Seschrock static void 9597041Seschrock vdev_label_sync_ignore_done(zio_t *zio) 9606976Seschrock { 9616976Seschrock kmem_free(zio->io_private, sizeof (uint64_t)); 9626976Seschrock } 9636976Seschrock 9646976Seschrock /* 9655688Sbonwick * Write all even or odd labels to all leaves of the specified vdev. 9665688Sbonwick */ 9675688Sbonwick static void 9687754SJeff.Bonwick@Sun.COM vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags) 969789Sahrens { 970789Sahrens nvlist_t *label; 971789Sahrens vdev_phys_t *vp; 972789Sahrens char *buf; 973789Sahrens size_t buflen; 974789Sahrens 9757754SJeff.Bonwick@Sun.COM for (int c = 0; c < vd->vdev_children; c++) 9767754SJeff.Bonwick@Sun.COM vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags); 977789Sahrens 978789Sahrens if (!vd->vdev_ops->vdev_op_leaf) 979789Sahrens return; 980789Sahrens 9817754SJeff.Bonwick@Sun.COM if (!vdev_writeable(vd)) 982789Sahrens return; 983789Sahrens 984789Sahrens /* 985789Sahrens * Generate a label describing the top-level config to which we belong. 986789Sahrens */ 9871635Sbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 988789Sahrens 989789Sahrens vp = zio_buf_alloc(sizeof (vdev_phys_t)); 990789Sahrens bzero(vp, sizeof (vdev_phys_t)); 991789Sahrens 992789Sahrens buf = vp->vp_nvlist; 993789Sahrens buflen = sizeof (vp->vp_nvlist); 994789Sahrens 9955688Sbonwick if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 9965688Sbonwick for (; l < VDEV_LABELS; l += 2) { 9975688Sbonwick vdev_label_write(zio, vd, l, vp, 9985688Sbonwick offsetof(vdev_label_t, vl_vdev_phys), 9995688Sbonwick sizeof (vdev_phys_t), 10005688Sbonwick vdev_label_sync_done, zio->io_private, 10017754SJeff.Bonwick@Sun.COM flags | ZIO_FLAG_DONT_PROPAGATE); 10025688Sbonwick } 10035688Sbonwick } 1004789Sahrens 1005789Sahrens zio_buf_free(vp, sizeof (vdev_phys_t)); 1006789Sahrens nvlist_free(label); 1007789Sahrens } 1008789Sahrens 10095688Sbonwick int 10107754SJeff.Bonwick@Sun.COM vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags) 1011789Sahrens { 10127754SJeff.Bonwick@Sun.COM list_t *dl = &spa->spa_config_dirty_list; 10135688Sbonwick vdev_t *vd; 10147754SJeff.Bonwick@Sun.COM zio_t *zio; 1015789Sahrens int error; 1016789Sahrens 10175688Sbonwick /* 10187754SJeff.Bonwick@Sun.COM * Write the new labels to disk. 10195688Sbonwick */ 10207754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, flags); 1021789Sahrens 10225688Sbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 10235688Sbonwick uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 10245688Sbonwick KM_SLEEP); 102510594SGeorge.Wilson@Sun.COM 102610594SGeorge.Wilson@Sun.COM ASSERT(!vd->vdev_ishole); 102710594SGeorge.Wilson@Sun.COM 10288632SBill.Moore@Sun.COM zio_t *vio = zio_null(zio, spa, NULL, 10297041Seschrock (vd->vdev_islog || vd->vdev_aux != NULL) ? 10307041Seschrock vdev_label_sync_ignore_done : vdev_label_sync_top_done, 10315688Sbonwick good_writes, flags); 10327754SJeff.Bonwick@Sun.COM vdev_label_sync(vio, vd, l, txg, flags); 10335688Sbonwick zio_nowait(vio); 10345688Sbonwick } 10357754SJeff.Bonwick@Sun.COM 10367754SJeff.Bonwick@Sun.COM error = zio_wait(zio); 1037789Sahrens 10385688Sbonwick /* 10395688Sbonwick * Flush the new labels to disk. 10405688Sbonwick */ 10415688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 1042789Sahrens 10435688Sbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 10445688Sbonwick zio_flush(zio, vd); 10454527Sperrin 10465688Sbonwick (void) zio_wait(zio); 1047789Sahrens 1048789Sahrens return (error); 1049789Sahrens } 1050789Sahrens 1051789Sahrens /* 10525688Sbonwick * Sync the uberblock and any changes to the vdev configuration. 1053789Sahrens * 1054789Sahrens * The order of operations is carefully crafted to ensure that 1055789Sahrens * if the system panics or loses power at any time, the state on disk 1056789Sahrens * is still transactionally consistent. The in-line comments below 1057789Sahrens * describe the failure semantics at each stage. 1058789Sahrens * 10595688Sbonwick * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 1060789Sahrens * at any time, you can just call it again, and it will resume its work. 1061789Sahrens */ 10627754SJeff.Bonwick@Sun.COM int 10639725SEric.Schrock@Sun.COM vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard) 1064789Sahrens { 10655688Sbonwick spa_t *spa = svd[0]->vdev_spa; 1066789Sahrens uberblock_t *ub = &spa->spa_uberblock; 10671635Sbonwick vdev_t *vd; 1068789Sahrens zio_t *zio; 10697754SJeff.Bonwick@Sun.COM int error; 10707754SJeff.Bonwick@Sun.COM int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 1071789Sahrens 10729725SEric.Schrock@Sun.COM /* 10739725SEric.Schrock@Sun.COM * Normally, we don't want to try too hard to write every label and 10749725SEric.Schrock@Sun.COM * uberblock. If there is a flaky disk, we don't want the rest of the 10759725SEric.Schrock@Sun.COM * sync process to block while we retry. But if we can't write a 10769725SEric.Schrock@Sun.COM * single label out, we should retry with ZIO_FLAG_TRYHARD before 10779725SEric.Schrock@Sun.COM * bailing out and declaring the pool faulted. 10789725SEric.Schrock@Sun.COM */ 10799725SEric.Schrock@Sun.COM if (tryhard) 10809725SEric.Schrock@Sun.COM flags |= ZIO_FLAG_TRYHARD; 10819725SEric.Schrock@Sun.COM 1082789Sahrens ASSERT(ub->ub_txg <= txg); 1083789Sahrens 1084789Sahrens /* 10855688Sbonwick * If this isn't a resync due to I/O errors, 10865688Sbonwick * and nothing changed in this transaction group, 10875688Sbonwick * and the vdev configuration hasn't changed, 10881635Sbonwick * then there's nothing to do. 1089789Sahrens */ 10905688Sbonwick if (ub->ub_txg < txg && 10915688Sbonwick uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 10927754SJeff.Bonwick@Sun.COM list_is_empty(&spa->spa_config_dirty_list)) 10937754SJeff.Bonwick@Sun.COM return (0); 1094789Sahrens 1095789Sahrens if (txg > spa_freeze_txg(spa)) 10967754SJeff.Bonwick@Sun.COM return (0); 1097789Sahrens 10981635Sbonwick ASSERT(txg <= spa->spa_final_txg); 10991635Sbonwick 1100789Sahrens /* 1101789Sahrens * Flush the write cache of every disk that's been written to 1102789Sahrens * in this transaction group. This ensures that all blocks 1103789Sahrens * written in this txg will be committed to stable storage 1104789Sahrens * before any uberblock that references them. 1105789Sahrens */ 11065688Sbonwick zio = zio_root(spa, NULL, NULL, flags); 11075688Sbonwick 1108789Sahrens for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 11095688Sbonwick vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 11105688Sbonwick zio_flush(zio, vd); 11115688Sbonwick 1112789Sahrens (void) zio_wait(zio); 1113789Sahrens 1114789Sahrens /* 1115789Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the 1116789Sahrens * system dies in the middle of this process, that's OK: all of the 1117789Sahrens * even labels that made it to disk will be newer than any uberblock, 1118789Sahrens * and will therefore be considered invalid. The odd labels (L1, L3), 11195688Sbonwick * which have not yet been touched, will still be valid. We flush 11205688Sbonwick * the new labels to disk to ensure that all even-label updates 11215688Sbonwick * are committed to stable storage before the uberblock update. 1122789Sahrens */ 11237754SJeff.Bonwick@Sun.COM if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) 11247754SJeff.Bonwick@Sun.COM return (error); 1125789Sahrens 1126789Sahrens /* 11277754SJeff.Bonwick@Sun.COM * Sync the uberblocks to all vdevs in svd[]. 11281635Sbonwick * If the system dies in the middle of this step, there are two cases 11291635Sbonwick * to consider, and the on-disk state is consistent either way: 1130789Sahrens * 1131789Sahrens * (1) If none of the new uberblocks made it to disk, then the 1132789Sahrens * previous uberblock will be the newest, and the odd labels 1133789Sahrens * (which had not yet been touched) will be valid with respect 1134789Sahrens * to that uberblock. 1135789Sahrens * 1136789Sahrens * (2) If one or more new uberblocks made it to disk, then they 1137789Sahrens * will be the newest, and the even labels (which had all 1138789Sahrens * been successfully committed) will be valid with respect 1139789Sahrens * to the new uberblocks. 1140789Sahrens */ 11417754SJeff.Bonwick@Sun.COM if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) 11427754SJeff.Bonwick@Sun.COM return (error); 1143789Sahrens 1144789Sahrens /* 1145789Sahrens * Sync out odd labels for every dirty vdev. If the system dies 1146789Sahrens * in the middle of this process, the even labels and the new 1147789Sahrens * uberblocks will suffice to open the pool. The next time 1148789Sahrens * the pool is opened, the first thing we'll do -- before any 1149789Sahrens * user data is modified -- is mark every vdev dirty so that 11505688Sbonwick * all labels will be brought up to date. We flush the new labels 11515688Sbonwick * to disk to ensure that all odd-label updates are committed to 11525688Sbonwick * stable storage before the next transaction group begins. 1153789Sahrens */ 11547754SJeff.Bonwick@Sun.COM return (vdev_label_sync_list(spa, 1, txg, flags)); 1155789Sahrens } 1156