1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21236884Smm 22168404Spjd/* 23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24249195Smm * Copyright (c) 2013 by Delphix. All rights reserved. 25168404Spjd */ 26168404Spjd 27168404Spjd/* 28168404Spjd * Virtual Device Labels 29168404Spjd * --------------------- 30168404Spjd * 31168404Spjd * The vdev label serves several distinct purposes: 32168404Spjd * 33168404Spjd * 1. Uniquely identify this device as part of a ZFS pool and confirm its 34168404Spjd * identity within the pool. 35168404Spjd * 36168404Spjd * 2. Verify that all the devices given in a configuration are present 37168404Spjd * within the pool. 38168404Spjd * 39168404Spjd * 3. Determine the uberblock for the pool. 40168404Spjd * 41168404Spjd * 4. In case of an import operation, determine the configuration of the 42168404Spjd * toplevel vdev of which it is a part. 43168404Spjd * 44168404Spjd * 5. If an import operation cannot find all the devices in the pool, 45168404Spjd * provide enough information to the administrator to determine which 46168404Spjd * devices are missing. 47168404Spjd * 48168404Spjd * It is important to note that while the kernel is responsible for writing the 49168404Spjd * label, it only consumes the information in the first three cases. The 50168404Spjd * latter information is only consumed in userland when determining the 51168404Spjd * configuration to import a pool. 52168404Spjd * 53168404Spjd * 54168404Spjd * Label Organization 55168404Spjd * ------------------ 56168404Spjd * 57168404Spjd * Before describing the contents of the label, it's important to understand how 58168404Spjd * the labels are written and updated with respect to the uberblock. 59168404Spjd * 60168404Spjd * When the pool configuration is altered, either because it was newly created 61168404Spjd * or a device was added, we want to update all the labels such that we can deal 62168404Spjd * with fatal failure at any point. To this end, each disk has two labels which 63168404Spjd * are updated before and after the uberblock is synced. Assuming we have 64185029Spjd * labels and an uberblock with the following transaction groups: 65168404Spjd * 66168404Spjd * L1 UB L2 67168404Spjd * +------+ +------+ +------+ 68168404Spjd * | | | | | | 69168404Spjd * | t10 | | t10 | | t10 | 70168404Spjd * | | | | | | 71168404Spjd * +------+ +------+ +------+ 72168404Spjd * 73168404Spjd * In this stable state, the labels and the uberblock were all updated within 74168404Spjd * the same transaction group (10). Each label is mirrored and checksummed, so 75168404Spjd * that we can detect when we fail partway through writing the label. 76168404Spjd * 77168404Spjd * In order to identify which labels are valid, the labels are written in the 78168404Spjd * following manner: 79168404Spjd * 80168404Spjd * 1. For each vdev, update 'L1' to the new label 81168404Spjd * 2. Update the uberblock 82168404Spjd * 3. For each vdev, update 'L2' to the new label 83168404Spjd * 84168404Spjd * Given arbitrary failure, we can determine the correct label to use based on 85168404Spjd * the transaction group. If we fail after updating L1 but before updating the 86168404Spjd * UB, we will notice that L1's transaction group is greater than the uberblock, 87168404Spjd * so L2 must be valid. If we fail after writing the uberblock but before 88168404Spjd * writing L2, we will notice that L2's transaction group is less than L1, and 89168404Spjd * therefore L1 is valid. 90168404Spjd * 91168404Spjd * Another added complexity is that not every label is updated when the config 92168404Spjd * is synced. If we add a single device, we do not want to have to re-write 93168404Spjd * every label for every device in the pool. This means that both L1 and L2 may 94168404Spjd * be older than the pool uberblock, because the necessary information is stored 95168404Spjd * on another vdev. 96168404Spjd * 97168404Spjd * 98168404Spjd * On-disk Format 99168404Spjd * -------------- 100168404Spjd * 101168404Spjd * The vdev label consists of two distinct parts, and is wrapped within the 102168404Spjd * vdev_label_t structure. The label includes 8k of padding to permit legacy 103168404Spjd * VTOC disk labels, but is otherwise ignored. 104168404Spjd * 105168404Spjd * The first half of the label is a packed nvlist which contains pool wide 106168404Spjd * properties, per-vdev properties, and configuration information. It is 107168404Spjd * described in more detail below. 108168404Spjd * 109168404Spjd * The latter half of the label consists of a redundant array of uberblocks. 110168404Spjd * These uberblocks are updated whenever a transaction group is committed, 111168404Spjd * or when the configuration is updated. When a pool is loaded, we scan each 112168404Spjd * vdev for the 'best' uberblock. 113168404Spjd * 114168404Spjd * 115168404Spjd * Configuration Information 116168404Spjd * ------------------------- 117168404Spjd * 118168404Spjd * The nvlist describing the pool and vdev contains the following elements: 119168404Spjd * 120168404Spjd * version ZFS on-disk version 121168404Spjd * name Pool name 122168404Spjd * state Pool state 123168404Spjd * txg Transaction group in which this label was written 124168404Spjd * pool_guid Unique identifier for this pool 125168404Spjd * vdev_tree An nvlist describing vdev tree. 126236884Smm * features_for_read 127236884Smm * An nvlist of the features necessary for reading the MOS. 128168404Spjd * 129168404Spjd * Each leaf device label also contains the following: 130168404Spjd * 131168404Spjd * top_guid Unique ID for top-level vdev in which this is contained 132168404Spjd * guid Unique ID for the leaf vdev 133168404Spjd * 134168404Spjd * The 'vs' configuration follows the format described in 'spa_config.c'. 135168404Spjd */ 136168404Spjd 137168404Spjd#include <sys/zfs_context.h> 138168404Spjd#include <sys/spa.h> 139168404Spjd#include <sys/spa_impl.h> 140168404Spjd#include <sys/dmu.h> 141168404Spjd#include <sys/zap.h> 142168404Spjd#include <sys/vdev.h> 143168404Spjd#include <sys/vdev_impl.h> 144168404Spjd#include <sys/uberblock_impl.h> 145168404Spjd#include <sys/metaslab.h> 146168404Spjd#include <sys/zio.h> 147219089Spjd#include <sys/dsl_scan.h> 148240868Spjd#include <sys/trim_map.h> 149168404Spjd#include <sys/fs/zfs.h> 150168404Spjd 151244188Ssmhstatic boolean_t vdev_trim_on_init = B_TRUE; 152244188SsmhSYSCTL_DECL(_vfs_zfs_vdev); 153244188SsmhSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, trim_on_init, CTLFLAG_RW, 154244188Ssmh &vdev_trim_on_init, 0, "Enable/disable full vdev trim on initialisation"); 155244188Ssmh 156168404Spjd/* 157168404Spjd * Basic routines to read and write from a vdev label. 158168404Spjd * Used throughout the rest of this file. 159168404Spjd */ 160168404Spjduint64_t 161168404Spjdvdev_label_offset(uint64_t psize, int l, uint64_t offset) 162168404Spjd{ 163168404Spjd ASSERT(offset < sizeof (vdev_label_t)); 164185029Spjd ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0); 165168404Spjd 166168404Spjd return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 167168404Spjd 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 168168404Spjd} 169168404Spjd 170185029Spjd/* 171185029Spjd * Returns back the vdev label associated with the passed in offset. 172185029Spjd */ 173185029Spjdint 174185029Spjdvdev_label_number(uint64_t psize, uint64_t offset) 175185029Spjd{ 176185029Spjd int l; 177185029Spjd 178185029Spjd if (offset >= psize - VDEV_LABEL_END_SIZE) { 179185029Spjd offset -= psize - VDEV_LABEL_END_SIZE; 180185029Spjd offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t); 181185029Spjd } 182185029Spjd l = offset / sizeof (vdev_label_t); 183185029Spjd return (l < VDEV_LABELS ? l : -1); 184185029Spjd} 185185029Spjd 186168404Spjdstatic void 187168404Spjdvdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 188185029Spjd uint64_t size, zio_done_func_t *done, void *private, int flags) 189168404Spjd{ 190185029Spjd ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) == 191185029Spjd SCL_STATE_ALL); 192185029Spjd ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 193168404Spjd 194168404Spjd zio_nowait(zio_read_phys(zio, vd, 195168404Spjd vdev_label_offset(vd->vdev_psize, l, offset), 196168404Spjd size, buf, ZIO_CHECKSUM_LABEL, done, private, 197185029Spjd ZIO_PRIORITY_SYNC_READ, flags, B_TRUE)); 198168404Spjd} 199168404Spjd 200168404Spjdstatic void 201168404Spjdvdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 202185029Spjd uint64_t size, zio_done_func_t *done, void *private, int flags) 203168404Spjd{ 204185029Spjd ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL || 205185029Spjd (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) == 206185029Spjd (SCL_CONFIG | SCL_STATE) && 207185029Spjd dsl_pool_sync_context(spa_get_dsl(zio->io_spa)))); 208185029Spjd ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 209168404Spjd 210168404Spjd zio_nowait(zio_write_phys(zio, vd, 211168404Spjd vdev_label_offset(vd->vdev_psize, l, offset), 212168404Spjd size, buf, ZIO_CHECKSUM_LABEL, done, private, 213185029Spjd ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE)); 214168404Spjd} 215168404Spjd 216168404Spjd/* 217168404Spjd * Generate the nvlist representing this vdev's config. 218168404Spjd */ 219168404Spjdnvlist_t * 220168404Spjdvdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 221219089Spjd vdev_config_flag_t flags) 222168404Spjd{ 223168404Spjd nvlist_t *nv = NULL; 224168404Spjd 225254112Sdelphij nv = fnvlist_alloc(); 226168404Spjd 227254112Sdelphij fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type); 228219089Spjd if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE))) 229254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id); 230254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid); 231168404Spjd 232168404Spjd if (vd->vdev_path != NULL) 233254112Sdelphij fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path); 234168404Spjd 235168404Spjd if (vd->vdev_devid != NULL) 236254112Sdelphij fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid); 237168404Spjd 238185029Spjd if (vd->vdev_physpath != NULL) 239254112Sdelphij fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 240254112Sdelphij vd->vdev_physpath); 241185029Spjd 242209962Smm if (vd->vdev_fru != NULL) 243254112Sdelphij fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru); 244209962Smm 245168404Spjd if (vd->vdev_nparity != 0) { 246168404Spjd ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 247168404Spjd VDEV_TYPE_RAIDZ) == 0); 248168404Spjd 249168404Spjd /* 250168404Spjd * Make sure someone hasn't managed to sneak a fancy new vdev 251168404Spjd * into a crufty old storage pool. 252168404Spjd */ 253168404Spjd ASSERT(vd->vdev_nparity == 1 || 254219089Spjd (vd->vdev_nparity <= 2 && 255219089Spjd spa_version(spa) >= SPA_VERSION_RAIDZ2) || 256219089Spjd (vd->vdev_nparity <= 3 && 257219089Spjd spa_version(spa) >= SPA_VERSION_RAIDZ3)); 258168404Spjd 259168404Spjd /* 260168404Spjd * Note that we'll add the nparity tag even on storage pools 261168404Spjd * that only support a single parity device -- older software 262168404Spjd * will just ignore it. 263168404Spjd */ 264254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity); 265168404Spjd } 266168404Spjd 267168404Spjd if (vd->vdev_wholedisk != -1ULL) 268254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 269254112Sdelphij vd->vdev_wholedisk); 270168404Spjd 271168404Spjd if (vd->vdev_not_present) 272254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1); 273168404Spjd 274168404Spjd if (vd->vdev_isspare) 275254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1); 276168404Spjd 277219089Spjd if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) && 278219089Spjd vd == vd->vdev_top) { 279254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 280254112Sdelphij vd->vdev_ms_array); 281254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 282254112Sdelphij vd->vdev_ms_shift); 283254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift); 284254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 285254112Sdelphij vd->vdev_asize); 286254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog); 287219089Spjd if (vd->vdev_removing) 288254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING, 289254112Sdelphij vd->vdev_removing); 290168404Spjd } 291168404Spjd 292262093Savg if (vd->vdev_dtl_sm != NULL) { 293254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 294262093Savg space_map_object(vd->vdev_dtl_sm)); 295262093Savg } 296168404Spjd 297219089Spjd if (vd->vdev_crtxg) 298254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg); 299219089Spjd 300168404Spjd if (getstats) { 301168404Spjd vdev_stat_t vs; 302219089Spjd pool_scan_stat_t ps; 303219089Spjd 304168404Spjd vdev_get_stats(vd, &vs); 305254112Sdelphij fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 306254112Sdelphij (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)); 307219089Spjd 308219089Spjd /* provide either current or previous scan information */ 309219089Spjd if (spa_scan_get_stats(spa, &ps) == 0) { 310254112Sdelphij fnvlist_add_uint64_array(nv, 311219089Spjd ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps, 312254112Sdelphij sizeof (pool_scan_stat_t) / sizeof (uint64_t)); 313219089Spjd } 314168404Spjd } 315168404Spjd 316168404Spjd if (!vd->vdev_ops->vdev_op_leaf) { 317168404Spjd nvlist_t **child; 318219089Spjd int c, idx; 319168404Spjd 320219089Spjd ASSERT(!vd->vdev_ishole); 321219089Spjd 322168404Spjd child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 323168404Spjd KM_SLEEP); 324168404Spjd 325219089Spjd for (c = 0, idx = 0; c < vd->vdev_children; c++) { 326219089Spjd vdev_t *cvd = vd->vdev_child[c]; 327168404Spjd 328219089Spjd /* 329219089Spjd * If we're generating an nvlist of removing 330219089Spjd * vdevs then skip over any device which is 331219089Spjd * not being removed. 332219089Spjd */ 333219089Spjd if ((flags & VDEV_CONFIG_REMOVING) && 334219089Spjd !cvd->vdev_removing) 335219089Spjd continue; 336168404Spjd 337219089Spjd child[idx++] = vdev_config_generate(spa, cvd, 338219089Spjd getstats, flags); 339219089Spjd } 340219089Spjd 341219089Spjd if (idx) { 342254112Sdelphij fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 343254112Sdelphij child, idx); 344219089Spjd } 345219089Spjd 346219089Spjd for (c = 0; c < idx; c++) 347168404Spjd nvlist_free(child[c]); 348168404Spjd 349168404Spjd kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 350168404Spjd 351168404Spjd } else { 352219089Spjd const char *aux = NULL; 353219089Spjd 354168404Spjd if (vd->vdev_offline && !vd->vdev_tmpoffline) 355254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE); 356254112Sdelphij if (vd->vdev_resilver_txg != 0) 357254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 358254112Sdelphij vd->vdev_resilver_txg); 359185029Spjd if (vd->vdev_faulted) 360254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE); 361185029Spjd if (vd->vdev_degraded) 362254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE); 363185029Spjd if (vd->vdev_removed) 364254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE); 365185029Spjd if (vd->vdev_unspare) 366254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE); 367219089Spjd if (vd->vdev_ishole) 368254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE); 369219089Spjd 370219089Spjd switch (vd->vdev_stat.vs_aux) { 371219089Spjd case VDEV_AUX_ERR_EXCEEDED: 372219089Spjd aux = "err_exceeded"; 373219089Spjd break; 374219089Spjd 375219089Spjd case VDEV_AUX_EXTERNAL: 376219089Spjd aux = "external"; 377219089Spjd break; 378219089Spjd } 379219089Spjd 380219089Spjd if (aux != NULL) 381254112Sdelphij fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux); 382219089Spjd 383219089Spjd if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) { 384254112Sdelphij fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID, 385254112Sdelphij vd->vdev_orig_guid); 386219089Spjd } 387168404Spjd } 388168404Spjd 389168404Spjd return (nv); 390168404Spjd} 391168404Spjd 392219089Spjd/* 393219089Spjd * Generate a view of the top-level vdevs. If we currently have holes 394219089Spjd * in the namespace, then generate an array which contains a list of holey 395219089Spjd * vdevs. Additionally, add the number of top-level children that currently 396219089Spjd * exist. 397219089Spjd */ 398219089Spjdvoid 399219089Spjdvdev_top_config_generate(spa_t *spa, nvlist_t *config) 400219089Spjd{ 401219089Spjd vdev_t *rvd = spa->spa_root_vdev; 402219089Spjd uint64_t *array; 403219089Spjd uint_t c, idx; 404219089Spjd 405219089Spjd array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP); 406219089Spjd 407219089Spjd for (c = 0, idx = 0; c < rvd->vdev_children; c++) { 408219089Spjd vdev_t *tvd = rvd->vdev_child[c]; 409219089Spjd 410219089Spjd if (tvd->vdev_ishole) 411219089Spjd array[idx++] = c; 412219089Spjd } 413219089Spjd 414219089Spjd if (idx) { 415219089Spjd VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY, 416219089Spjd array, idx) == 0); 417219089Spjd } 418219089Spjd 419219089Spjd VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 420219089Spjd rvd->vdev_children) == 0); 421219089Spjd 422219089Spjd kmem_free(array, rvd->vdev_children * sizeof (uint64_t)); 423219089Spjd} 424219089Spjd 425236884Smm/* 426239620Smm * Returns the configuration from the label of the given vdev. For vdevs 427239620Smm * which don't have a txg value stored on their label (i.e. spares/cache) 428239620Smm * or have not been completely initialized (txg = 0) just return 429239620Smm * the configuration from the first valid label we find. Otherwise, 430239620Smm * find the most up-to-date label that does not exceed the specified 431239620Smm * 'txg' value. 432236884Smm */ 433168404Spjdnvlist_t * 434239620Smmvdev_label_read_config(vdev_t *vd, uint64_t txg) 435168404Spjd{ 436168404Spjd spa_t *spa = vd->vdev_spa; 437168404Spjd nvlist_t *config = NULL; 438168404Spjd vdev_phys_t *vp; 439168404Spjd zio_t *zio; 440239620Smm uint64_t best_txg = 0; 441239620Smm int error = 0; 442213198Smm int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 443213198Smm ZIO_FLAG_SPECULATIVE; 444168404Spjd 445185029Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 446168404Spjd 447185029Spjd if (!vdev_readable(vd)) 448168404Spjd return (NULL); 449168404Spjd 450168404Spjd vp = zio_buf_alloc(sizeof (vdev_phys_t)); 451168404Spjd 452213198Smmretry: 453185029Spjd for (int l = 0; l < VDEV_LABELS; l++) { 454239620Smm nvlist_t *label = NULL; 455168404Spjd 456185029Spjd zio = zio_root(spa, NULL, NULL, flags); 457168404Spjd 458168404Spjd vdev_label_read(zio, vd, l, vp, 459168404Spjd offsetof(vdev_label_t, vl_vdev_phys), 460185029Spjd sizeof (vdev_phys_t), NULL, NULL, flags); 461168404Spjd 462168404Spjd if (zio_wait(zio) == 0 && 463168404Spjd nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 464239620Smm &label, 0) == 0) { 465239620Smm uint64_t label_txg = 0; 466168404Spjd 467239620Smm /* 468239620Smm * Auxiliary vdevs won't have txg values in their 469239620Smm * labels and newly added vdevs may not have been 470239620Smm * completely initialized so just return the 471239620Smm * configuration from the first valid label we 472239620Smm * encounter. 473239620Smm */ 474239620Smm error = nvlist_lookup_uint64(label, 475239620Smm ZPOOL_CONFIG_POOL_TXG, &label_txg); 476239620Smm if ((error || label_txg == 0) && !config) { 477239620Smm config = label; 478239620Smm break; 479239620Smm } else if (label_txg <= txg && label_txg > best_txg) { 480239620Smm best_txg = label_txg; 481239620Smm nvlist_free(config); 482239620Smm config = fnvlist_dup(label); 483239620Smm } 484168404Spjd } 485239620Smm 486239620Smm if (label != NULL) { 487239620Smm nvlist_free(label); 488239620Smm label = NULL; 489239620Smm } 490168404Spjd } 491168404Spjd 492213198Smm if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) { 493213198Smm flags |= ZIO_FLAG_TRYHARD; 494213198Smm goto retry; 495213198Smm } 496213198Smm 497168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 498168404Spjd 499168404Spjd return (config); 500168404Spjd} 501168404Spjd 502168404Spjd/* 503168404Spjd * Determine if a device is in use. The 'spare_guid' parameter will be filled 504168404Spjd * in with the device guid if this spare is active elsewhere on the system. 505168404Spjd */ 506168404Spjdstatic boolean_t 507168404Spjdvdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 508185029Spjd uint64_t *spare_guid, uint64_t *l2cache_guid) 509168404Spjd{ 510168404Spjd spa_t *spa = vd->vdev_spa; 511168404Spjd uint64_t state, pool_guid, device_guid, txg, spare_pool; 512168404Spjd uint64_t vdtxg = 0; 513168404Spjd nvlist_t *label; 514168404Spjd 515168404Spjd if (spare_guid) 516168404Spjd *spare_guid = 0ULL; 517185029Spjd if (l2cache_guid) 518185029Spjd *l2cache_guid = 0ULL; 519168404Spjd 520168404Spjd /* 521168404Spjd * Read the label, if any, and perform some basic sanity checks. 522168404Spjd */ 523239620Smm if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) 524168404Spjd return (B_FALSE); 525168404Spjd 526168404Spjd (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 527168404Spjd &vdtxg); 528168404Spjd 529168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 530168404Spjd &state) != 0 || 531168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 532168404Spjd &device_guid) != 0) { 533168404Spjd nvlist_free(label); 534168404Spjd return (B_FALSE); 535168404Spjd } 536168404Spjd 537185029Spjd if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 538168404Spjd (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 539168404Spjd &pool_guid) != 0 || 540168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 541168404Spjd &txg) != 0)) { 542168404Spjd nvlist_free(label); 543168404Spjd return (B_FALSE); 544168404Spjd } 545168404Spjd 546168404Spjd nvlist_free(label); 547168404Spjd 548168404Spjd /* 549168404Spjd * Check to see if this device indeed belongs to the pool it claims to 550168404Spjd * be a part of. The only way this is allowed is if the device is a hot 551168404Spjd * spare (which we check for later on). 552168404Spjd */ 553185029Spjd if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 554168404Spjd !spa_guid_exists(pool_guid, device_guid) && 555185029Spjd !spa_spare_exists(device_guid, NULL, NULL) && 556185029Spjd !spa_l2cache_exists(device_guid, NULL)) 557168404Spjd return (B_FALSE); 558168404Spjd 559168404Spjd /* 560168404Spjd * If the transaction group is zero, then this an initialized (but 561168404Spjd * unused) label. This is only an error if the create transaction 562168404Spjd * on-disk is the same as the one we're using now, in which case the 563168404Spjd * user has attempted to add the same vdev multiple times in the same 564168404Spjd * transaction. 565168404Spjd */ 566185029Spjd if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 567185029Spjd txg == 0 && vdtxg == crtxg) 568168404Spjd return (B_TRUE); 569168404Spjd 570168404Spjd /* 571168404Spjd * Check to see if this is a spare device. We do an explicit check for 572168404Spjd * spa_has_spare() here because it may be on our pending list of spares 573185029Spjd * to add. We also check if it is an l2cache device. 574168404Spjd */ 575185029Spjd if (spa_spare_exists(device_guid, &spare_pool, NULL) || 576168404Spjd spa_has_spare(spa, device_guid)) { 577168404Spjd if (spare_guid) 578168404Spjd *spare_guid = device_guid; 579168404Spjd 580168404Spjd switch (reason) { 581168404Spjd case VDEV_LABEL_CREATE: 582185029Spjd case VDEV_LABEL_L2CACHE: 583168404Spjd return (B_TRUE); 584168404Spjd 585168404Spjd case VDEV_LABEL_REPLACE: 586168404Spjd return (!spa_has_spare(spa, device_guid) || 587168404Spjd spare_pool != 0ULL); 588168404Spjd 589168404Spjd case VDEV_LABEL_SPARE: 590168404Spjd return (spa_has_spare(spa, device_guid)); 591168404Spjd } 592168404Spjd } 593168404Spjd 594168404Spjd /* 595185029Spjd * Check to see if this is an l2cache device. 596185029Spjd */ 597185029Spjd if (spa_l2cache_exists(device_guid, NULL)) 598185029Spjd return (B_TRUE); 599185029Spjd 600185029Spjd /* 601219089Spjd * We can't rely on a pool's state if it's been imported 602219089Spjd * read-only. Instead we look to see if the pools is marked 603219089Spjd * read-only in the namespace and set the state to active. 604219089Spjd */ 605219089Spjd if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL && 606219089Spjd spa_mode(spa) == FREAD) 607219089Spjd state = POOL_STATE_ACTIVE; 608219089Spjd 609219089Spjd /* 610168404Spjd * If the device is marked ACTIVE, then this device is in use by another 611168404Spjd * pool on the system. 612168404Spjd */ 613168404Spjd return (state == POOL_STATE_ACTIVE); 614168404Spjd} 615168404Spjd 616168404Spjd/* 617168404Spjd * Initialize a vdev label. We check to make sure each leaf device is not in 618168404Spjd * use, and writable. We put down an initial label which we will later 619168404Spjd * overwrite with a complete label. Note that it's important to do this 620168404Spjd * sequentially, not in parallel, so that we catch cases of multiple use of the 621168404Spjd * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 622168404Spjd * itself. 623168404Spjd */ 624168404Spjdint 625168404Spjdvdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 626168404Spjd{ 627168404Spjd spa_t *spa = vd->vdev_spa; 628168404Spjd nvlist_t *label; 629168404Spjd vdev_phys_t *vp; 630209962Smm char *pad2; 631168404Spjd uberblock_t *ub; 632168404Spjd zio_t *zio; 633168404Spjd char *buf; 634168404Spjd size_t buflen; 635168404Spjd int error; 636185029Spjd uint64_t spare_guid, l2cache_guid; 637185029Spjd int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 638168404Spjd 639185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 640168404Spjd 641185029Spjd for (int c = 0; c < vd->vdev_children; c++) 642168404Spjd if ((error = vdev_label_init(vd->vdev_child[c], 643168404Spjd crtxg, reason)) != 0) 644168404Spjd return (error); 645168404Spjd 646219089Spjd /* Track the creation time for this vdev */ 647219089Spjd vd->vdev_crtxg = crtxg; 648219089Spjd 649263399Sdelphij if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa)) 650168404Spjd return (0); 651168404Spjd 652168404Spjd /* 653168404Spjd * Dead vdevs cannot be initialized. 654168404Spjd */ 655168404Spjd if (vdev_is_dead(vd)) 656249195Smm return (SET_ERROR(EIO)); 657168404Spjd 658168404Spjd /* 659168404Spjd * Determine if the vdev is in use. 660168404Spjd */ 661219089Spjd if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT && 662185029Spjd vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 663249195Smm return (SET_ERROR(EBUSY)); 664168404Spjd 665168404Spjd /* 666185029Spjd * If this is a request to add or replace a spare or l2cache device 667185029Spjd * that is in use elsewhere on the system, then we must update the 668185029Spjd * guid (which was initialized to a random value) to reflect the 669185029Spjd * actual GUID (which is shared between multiple pools). 670168404Spjd */ 671185029Spjd if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 672185029Spjd spare_guid != 0ULL) { 673185029Spjd uint64_t guid_delta = spare_guid - vd->vdev_guid; 674168404Spjd 675185029Spjd vd->vdev_guid += guid_delta; 676168404Spjd 677185029Spjd for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 678185029Spjd pvd->vdev_guid_sum += guid_delta; 679168404Spjd 680168404Spjd /* 681168404Spjd * If this is a replacement, then we want to fallthrough to the 682168404Spjd * rest of the code. If we're adding a spare, then it's already 683185029Spjd * labeled appropriately and we can just return. 684168404Spjd */ 685168404Spjd if (reason == VDEV_LABEL_SPARE) 686168404Spjd return (0); 687219089Spjd ASSERT(reason == VDEV_LABEL_REPLACE || 688219089Spjd reason == VDEV_LABEL_SPLIT); 689168404Spjd } 690168404Spjd 691185029Spjd if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 692185029Spjd l2cache_guid != 0ULL) { 693185029Spjd uint64_t guid_delta = l2cache_guid - vd->vdev_guid; 694185029Spjd 695185029Spjd vd->vdev_guid += guid_delta; 696185029Spjd 697185029Spjd for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 698185029Spjd pvd->vdev_guid_sum += guid_delta; 699185029Spjd 700185029Spjd /* 701185029Spjd * If this is a replacement, then we want to fallthrough to the 702185029Spjd * rest of the code. If we're adding an l2cache, then it's 703185029Spjd * already labeled appropriately and we can just return. 704185029Spjd */ 705185029Spjd if (reason == VDEV_LABEL_L2CACHE) 706185029Spjd return (0); 707185029Spjd ASSERT(reason == VDEV_LABEL_REPLACE); 708185029Spjd } 709185029Spjd 710168404Spjd /* 711240868Spjd * TRIM the whole thing so that we start with a clean slate. 712240868Spjd * It's just an optimization, so we don't care if it fails. 713240868Spjd * Don't TRIM if removing so that we don't interfere with zpool 714240868Spjd * disaster recovery. 715240868Spjd */ 716284193Sdelphij if (zfs_trim_enabled && vdev_trim_on_init && !vd->vdev_notrim && 717284193Sdelphij (reason == VDEV_LABEL_CREATE || reason == VDEV_LABEL_SPARE || 718284193Sdelphij reason == VDEV_LABEL_L2CACHE)) 719240868Spjd zio_wait(zio_trim(NULL, spa, vd, 0, vd->vdev_psize)); 720240868Spjd 721240868Spjd /* 722168404Spjd * Initialize its label. 723168404Spjd */ 724168404Spjd vp = zio_buf_alloc(sizeof (vdev_phys_t)); 725168404Spjd bzero(vp, sizeof (vdev_phys_t)); 726168404Spjd 727168404Spjd /* 728168404Spjd * Generate a label describing the pool and our top-level vdev. 729168404Spjd * We mark it as being from txg 0 to indicate that it's not 730168404Spjd * really part of an active pool just yet. The labels will 731168404Spjd * be written again with a meaningful txg by spa_sync(). 732168404Spjd */ 733168404Spjd if (reason == VDEV_LABEL_SPARE || 734168404Spjd (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 735168404Spjd /* 736168404Spjd * For inactive hot spares, we generate a special label that 737168404Spjd * identifies as a mutually shared hot spare. We write the 738168404Spjd * label if we are adding a hot spare, or if we are removing an 739168404Spjd * active hot spare (in which case we want to revert the 740168404Spjd * labels). 741168404Spjd */ 742168404Spjd VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 743168404Spjd 744168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 745168404Spjd spa_version(spa)) == 0); 746168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 747168404Spjd POOL_STATE_SPARE) == 0); 748168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 749168404Spjd vd->vdev_guid) == 0); 750185029Spjd } else if (reason == VDEV_LABEL_L2CACHE || 751185029Spjd (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 752185029Spjd /* 753185029Spjd * For level 2 ARC devices, add a special label. 754185029Spjd */ 755185029Spjd VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 756185029Spjd 757185029Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 758185029Spjd spa_version(spa)) == 0); 759185029Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 760185029Spjd POOL_STATE_L2CACHE) == 0); 761185029Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 762185029Spjd vd->vdev_guid) == 0); 763168404Spjd } else { 764219089Spjd uint64_t txg = 0ULL; 765168404Spjd 766219089Spjd if (reason == VDEV_LABEL_SPLIT) 767219089Spjd txg = spa->spa_uberblock.ub_txg; 768219089Spjd label = spa_config_generate(spa, vd, txg, B_FALSE); 769219089Spjd 770168404Spjd /* 771168404Spjd * Add our creation time. This allows us to detect multiple 772168404Spjd * vdev uses as described above, and automatically expires if we 773168404Spjd * fail. 774168404Spjd */ 775168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 776168404Spjd crtxg) == 0); 777168404Spjd } 778168404Spjd 779168404Spjd buf = vp->vp_nvlist; 780168404Spjd buflen = sizeof (vp->vp_nvlist); 781168404Spjd 782168404Spjd error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 783168404Spjd if (error != 0) { 784168404Spjd nvlist_free(label); 785168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 786168404Spjd /* EFAULT means nvlist_pack ran out of room */ 787168404Spjd return (error == EFAULT ? ENAMETOOLONG : EINVAL); 788168404Spjd } 789168404Spjd 790168404Spjd /* 791168404Spjd * Initialize uberblock template. 792168404Spjd */ 793219089Spjd ub = zio_buf_alloc(VDEV_UBERBLOCK_RING); 794219089Spjd bzero(ub, VDEV_UBERBLOCK_RING); 795168404Spjd *ub = spa->spa_uberblock; 796168404Spjd ub->ub_txg = 0; 797168404Spjd 798209962Smm /* Initialize the 2nd padding area. */ 799209962Smm pad2 = zio_buf_alloc(VDEV_PAD_SIZE); 800209962Smm bzero(pad2, VDEV_PAD_SIZE); 801209962Smm 802168404Spjd /* 803168404Spjd * Write everything in parallel. 804168404Spjd */ 805213198Smmretry: 806185029Spjd zio = zio_root(spa, NULL, NULL, flags); 807168404Spjd 808185029Spjd for (int l = 0; l < VDEV_LABELS; l++) { 809168404Spjd 810168404Spjd vdev_label_write(zio, vd, l, vp, 811168404Spjd offsetof(vdev_label_t, vl_vdev_phys), 812185029Spjd sizeof (vdev_phys_t), NULL, NULL, flags); 813168404Spjd 814209962Smm /* 815209962Smm * Skip the 1st padding area. 816209962Smm * Zero out the 2nd padding area where it might have 817209962Smm * left over data from previous filesystem format. 818209962Smm */ 819209962Smm vdev_label_write(zio, vd, l, pad2, 820209962Smm offsetof(vdev_label_t, vl_pad2), 821209962Smm VDEV_PAD_SIZE, NULL, NULL, flags); 822168404Spjd 823219089Spjd vdev_label_write(zio, vd, l, ub, 824219089Spjd offsetof(vdev_label_t, vl_uberblock), 825219089Spjd VDEV_UBERBLOCK_RING, NULL, NULL, flags); 826168404Spjd } 827168404Spjd 828168404Spjd error = zio_wait(zio); 829168404Spjd 830213198Smm if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) { 831213198Smm flags |= ZIO_FLAG_TRYHARD; 832213198Smm goto retry; 833213198Smm } 834213198Smm 835168404Spjd nvlist_free(label); 836209962Smm zio_buf_free(pad2, VDEV_PAD_SIZE); 837219089Spjd zio_buf_free(ub, VDEV_UBERBLOCK_RING); 838168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 839168404Spjd 840168404Spjd /* 841168404Spjd * If this vdev hasn't been previously identified as a spare, then we 842185029Spjd * mark it as such only if a) we are labeling it as a spare, or b) it 843185029Spjd * exists as a spare elsewhere in the system. Do the same for 844185029Spjd * level 2 ARC devices. 845168404Spjd */ 846168404Spjd if (error == 0 && !vd->vdev_isspare && 847168404Spjd (reason == VDEV_LABEL_SPARE || 848185029Spjd spa_spare_exists(vd->vdev_guid, NULL, NULL))) 849168404Spjd spa_spare_add(vd); 850168404Spjd 851185029Spjd if (error == 0 && !vd->vdev_isl2cache && 852185029Spjd (reason == VDEV_LABEL_L2CACHE || 853185029Spjd spa_l2cache_exists(vd->vdev_guid, NULL))) 854185029Spjd spa_l2cache_add(vd); 855185029Spjd 856168404Spjd return (error); 857168404Spjd} 858168404Spjd 859168404Spjd/* 860168404Spjd * ========================================================================== 861168404Spjd * uberblock load/sync 862168404Spjd * ========================================================================== 863168404Spjd */ 864168404Spjd 865168404Spjd/* 866168404Spjd * Consider the following situation: txg is safely synced to disk. We've 867168404Spjd * written the first uberblock for txg + 1, and then we lose power. When we 868168404Spjd * come back up, we fail to see the uberblock for txg + 1 because, say, 869168404Spjd * it was on a mirrored device and the replica to which we wrote txg + 1 870168404Spjd * is now offline. If we then make some changes and sync txg + 1, and then 871236884Smm * the missing replica comes back, then for a few seconds we'll have two 872168404Spjd * conflicting uberblocks on disk with the same txg. The solution is simple: 873168404Spjd * among uberblocks with equal txg, choose the one with the latest timestamp. 874168404Spjd */ 875168404Spjdstatic int 876168404Spjdvdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 877168404Spjd{ 878168404Spjd if (ub1->ub_txg < ub2->ub_txg) 879168404Spjd return (-1); 880168404Spjd if (ub1->ub_txg > ub2->ub_txg) 881168404Spjd return (1); 882168404Spjd 883168404Spjd if (ub1->ub_timestamp < ub2->ub_timestamp) 884168404Spjd return (-1); 885168404Spjd if (ub1->ub_timestamp > ub2->ub_timestamp) 886168404Spjd return (1); 887168404Spjd 888168404Spjd return (0); 889168404Spjd} 890168404Spjd 891236884Smmstruct ubl_cbdata { 892236884Smm uberblock_t *ubl_ubbest; /* Best uberblock */ 893236884Smm vdev_t *ubl_vd; /* vdev associated with the above */ 894236884Smm}; 895236884Smm 896168404Spjdstatic void 897168404Spjdvdev_uberblock_load_done(zio_t *zio) 898168404Spjd{ 899236884Smm vdev_t *vd = zio->io_vd; 900219089Spjd spa_t *spa = zio->io_spa; 901185029Spjd zio_t *rio = zio->io_private; 902168404Spjd uberblock_t *ub = zio->io_data; 903236884Smm struct ubl_cbdata *cbp = rio->io_private; 904168404Spjd 905236884Smm ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd)); 906168404Spjd 907168404Spjd if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 908185029Spjd mutex_enter(&rio->io_lock); 909219089Spjd if (ub->ub_txg <= spa->spa_load_max_txg && 910236884Smm vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) { 911236884Smm /* 912239620Smm * Keep track of the vdev in which this uberblock 913239620Smm * was found. We will use this information later 914239620Smm * to obtain the config nvlist associated with 915236884Smm * this uberblock. 916236884Smm */ 917236884Smm *cbp->ubl_ubbest = *ub; 918236884Smm cbp->ubl_vd = vd; 919236884Smm } 920185029Spjd mutex_exit(&rio->io_lock); 921168404Spjd } 922168404Spjd 923168404Spjd zio_buf_free(zio->io_data, zio->io_size); 924168404Spjd} 925168404Spjd 926236884Smmstatic void 927236884Smmvdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags, 928236884Smm struct ubl_cbdata *cbp) 929168404Spjd{ 930185029Spjd for (int c = 0; c < vd->vdev_children; c++) 931236884Smm vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp); 932168404Spjd 933185029Spjd if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 934185029Spjd for (int l = 0; l < VDEV_LABELS; l++) { 935185029Spjd for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 936185029Spjd vdev_label_read(zio, vd, l, 937185029Spjd zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 938185029Spjd VDEV_UBERBLOCK_OFFSET(vd, n), 939185029Spjd VDEV_UBERBLOCK_SIZE(vd), 940185029Spjd vdev_uberblock_load_done, zio, flags); 941185029Spjd } 942168404Spjd } 943168404Spjd } 944236884Smm} 945185029Spjd 946236884Smm/* 947236884Smm * Reads the 'best' uberblock from disk along with its associated 948236884Smm * configuration. First, we read the uberblock array of each label of each 949236884Smm * vdev, keeping track of the uberblock with the highest txg in each array. 950239620Smm * Then, we read the configuration from the same vdev as the best uberblock. 951236884Smm */ 952236884Smmvoid 953236884Smmvdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config) 954236884Smm{ 955236884Smm zio_t *zio; 956236884Smm spa_t *spa = rvd->vdev_spa; 957236884Smm struct ubl_cbdata cb; 958236884Smm int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 959236884Smm ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD; 960236884Smm 961236884Smm ASSERT(ub); 962236884Smm ASSERT(config); 963236884Smm 964236884Smm bzero(ub, sizeof (uberblock_t)); 965236884Smm *config = NULL; 966236884Smm 967236884Smm cb.ubl_ubbest = ub; 968236884Smm cb.ubl_vd = NULL; 969236884Smm 970236884Smm spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 971236884Smm zio = zio_root(spa, NULL, &cb, flags); 972236884Smm vdev_uberblock_load_impl(zio, rvd, flags, &cb); 973236884Smm (void) zio_wait(zio); 974239620Smm 975239620Smm /* 976239620Smm * It's possible that the best uberblock was discovered on a label 977239620Smm * that has a configuration which was written in a future txg. 978239620Smm * Search all labels on this vdev to find the configuration that 979239620Smm * matches the txg for our uberblock. 980239620Smm */ 981239620Smm if (cb.ubl_vd != NULL) 982239620Smm *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg); 983236884Smm spa_config_exit(spa, SCL_ALL, FTAG); 984168404Spjd} 985168404Spjd 986168404Spjd/* 987185029Spjd * On success, increment root zio's count of good writes. 988168404Spjd * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 989168404Spjd */ 990168404Spjdstatic void 991168404Spjdvdev_uberblock_sync_done(zio_t *zio) 992168404Spjd{ 993185029Spjd uint64_t *good_writes = zio->io_private; 994168404Spjd 995168404Spjd if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 996271001Sdelphij atomic_inc_64(good_writes); 997168404Spjd} 998168404Spjd 999185029Spjd/* 1000185029Spjd * Write the uberblock to all labels of all leaves of the specified vdev. 1001185029Spjd */ 1002168404Spjdstatic void 1003185029Spjdvdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags) 1004168404Spjd{ 1005185029Spjd uberblock_t *ubbuf; 1006185029Spjd int n; 1007168404Spjd 1008185029Spjd for (int c = 0; c < vd->vdev_children; c++) 1009185029Spjd vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags); 1010168404Spjd 1011168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 1012168404Spjd return; 1013168404Spjd 1014185029Spjd if (!vdev_writeable(vd)) 1015168404Spjd return; 1016168404Spjd 1017185029Spjd n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 1018168404Spjd 1019185029Spjd ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 1020185029Spjd bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 1021185029Spjd *ubbuf = *ub; 1022168404Spjd 1023185029Spjd for (int l = 0; l < VDEV_LABELS; l++) 1024185029Spjd vdev_label_write(zio, vd, l, ubbuf, 1025185029Spjd VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd), 1026185029Spjd vdev_uberblock_sync_done, zio->io_private, 1027185029Spjd flags | ZIO_FLAG_DONT_PROPAGATE); 1028168404Spjd 1029185029Spjd zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 1030168404Spjd} 1031168404Spjd 1032251629Sdelphij/* Sync the uberblocks to all vdevs in svd[] */ 1033185029Spjdint 1034185029Spjdvdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 1035168404Spjd{ 1036185029Spjd spa_t *spa = svd[0]->vdev_spa; 1037168404Spjd zio_t *zio; 1038185029Spjd uint64_t good_writes = 0; 1039168404Spjd 1040185029Spjd zio = zio_root(spa, NULL, &good_writes, flags); 1041168404Spjd 1042185029Spjd for (int v = 0; v < svdcount; v++) 1043185029Spjd vdev_uberblock_sync(zio, ub, svd[v], flags); 1044168404Spjd 1045185029Spjd (void) zio_wait(zio); 1046168404Spjd 1047168404Spjd /* 1048185029Spjd * Flush the uberblocks to disk. This ensures that the odd labels 1049185029Spjd * are no longer needed (because the new uberblocks and the even 1050185029Spjd * labels are safely on disk), so it is safe to overwrite them. 1051168404Spjd */ 1052185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1053168404Spjd 1054185029Spjd for (int v = 0; v < svdcount; v++) 1055185029Spjd zio_flush(zio, svd[v]); 1056168404Spjd 1057185029Spjd (void) zio_wait(zio); 1058185029Spjd 1059185029Spjd return (good_writes >= 1 ? 0 : EIO); 1060168404Spjd} 1061168404Spjd 1062168404Spjd/* 1063185029Spjd * On success, increment the count of good writes for our top-level vdev. 1064168404Spjd */ 1065168404Spjdstatic void 1066185029Spjdvdev_label_sync_done(zio_t *zio) 1067168404Spjd{ 1068185029Spjd uint64_t *good_writes = zio->io_private; 1069168404Spjd 1070168404Spjd if (zio->io_error == 0) 1071271001Sdelphij atomic_inc_64(good_writes); 1072168404Spjd} 1073168404Spjd 1074185029Spjd/* 1075185029Spjd * If there weren't enough good writes, indicate failure to the parent. 1076185029Spjd */ 1077168404Spjdstatic void 1078185029Spjdvdev_label_sync_top_done(zio_t *zio) 1079168404Spjd{ 1080185029Spjd uint64_t *good_writes = zio->io_private; 1081185029Spjd 1082185029Spjd if (*good_writes == 0) 1083249195Smm zio->io_error = SET_ERROR(EIO); 1084185029Spjd 1085185029Spjd kmem_free(good_writes, sizeof (uint64_t)); 1086185029Spjd} 1087185029Spjd 1088185029Spjd/* 1089185029Spjd * We ignore errors for log and cache devices, simply free the private data. 1090185029Spjd */ 1091185029Spjdstatic void 1092185029Spjdvdev_label_sync_ignore_done(zio_t *zio) 1093185029Spjd{ 1094185029Spjd kmem_free(zio->io_private, sizeof (uint64_t)); 1095185029Spjd} 1096185029Spjd 1097185029Spjd/* 1098185029Spjd * Write all even or odd labels to all leaves of the specified vdev. 1099185029Spjd */ 1100185029Spjdstatic void 1101185029Spjdvdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags) 1102185029Spjd{ 1103168404Spjd nvlist_t *label; 1104168404Spjd vdev_phys_t *vp; 1105168404Spjd char *buf; 1106168404Spjd size_t buflen; 1107168404Spjd 1108185029Spjd for (int c = 0; c < vd->vdev_children; c++) 1109185029Spjd vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags); 1110168404Spjd 1111168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 1112168404Spjd return; 1113168404Spjd 1114185029Spjd if (!vdev_writeable(vd)) 1115168404Spjd return; 1116168404Spjd 1117168404Spjd /* 1118168404Spjd * Generate a label describing the top-level config to which we belong. 1119168404Spjd */ 1120168404Spjd label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 1121168404Spjd 1122168404Spjd vp = zio_buf_alloc(sizeof (vdev_phys_t)); 1123168404Spjd bzero(vp, sizeof (vdev_phys_t)); 1124168404Spjd 1125168404Spjd buf = vp->vp_nvlist; 1126168404Spjd buflen = sizeof (vp->vp_nvlist); 1127168404Spjd 1128185029Spjd if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 1129185029Spjd for (; l < VDEV_LABELS; l += 2) { 1130185029Spjd vdev_label_write(zio, vd, l, vp, 1131185029Spjd offsetof(vdev_label_t, vl_vdev_phys), 1132185029Spjd sizeof (vdev_phys_t), 1133185029Spjd vdev_label_sync_done, zio->io_private, 1134185029Spjd flags | ZIO_FLAG_DONT_PROPAGATE); 1135185029Spjd } 1136185029Spjd } 1137168404Spjd 1138168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 1139168404Spjd nvlist_free(label); 1140168404Spjd} 1141168404Spjd 1142185029Spjdint 1143185029Spjdvdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags) 1144168404Spjd{ 1145185029Spjd list_t *dl = &spa->spa_config_dirty_list; 1146185029Spjd vdev_t *vd; 1147168404Spjd zio_t *zio; 1148168404Spjd int error; 1149168404Spjd 1150185029Spjd /* 1151185029Spjd * Write the new labels to disk. 1152185029Spjd */ 1153185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1154168404Spjd 1155185029Spjd for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 1156185029Spjd uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 1157185029Spjd KM_SLEEP); 1158219089Spjd 1159219089Spjd ASSERT(!vd->vdev_ishole); 1160219089Spjd 1161209962Smm zio_t *vio = zio_null(zio, spa, NULL, 1162185029Spjd (vd->vdev_islog || vd->vdev_aux != NULL) ? 1163185029Spjd vdev_label_sync_ignore_done : vdev_label_sync_top_done, 1164185029Spjd good_writes, flags); 1165185029Spjd vdev_label_sync(vio, vd, l, txg, flags); 1166185029Spjd zio_nowait(vio); 1167185029Spjd } 1168168404Spjd 1169185029Spjd error = zio_wait(zio); 1170168404Spjd 1171168404Spjd /* 1172185029Spjd * Flush the new labels to disk. 1173168404Spjd */ 1174185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1175168404Spjd 1176185029Spjd for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 1177185029Spjd zio_flush(zio, vd); 1178168404Spjd 1179185029Spjd (void) zio_wait(zio); 1180168404Spjd 1181168404Spjd return (error); 1182168404Spjd} 1183168404Spjd 1184168404Spjd/* 1185185029Spjd * Sync the uberblock and any changes to the vdev configuration. 1186168404Spjd * 1187168404Spjd * The order of operations is carefully crafted to ensure that 1188168404Spjd * if the system panics or loses power at any time, the state on disk 1189168404Spjd * is still transactionally consistent. The in-line comments below 1190168404Spjd * describe the failure semantics at each stage. 1191168404Spjd * 1192185029Spjd * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 1193168404Spjd * at any time, you can just call it again, and it will resume its work. 1194168404Spjd */ 1195168404Spjdint 1196213198Smmvdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard) 1197168404Spjd{ 1198185029Spjd spa_t *spa = svd[0]->vdev_spa; 1199168404Spjd uberblock_t *ub = &spa->spa_uberblock; 1200168404Spjd vdev_t *vd; 1201168404Spjd zio_t *zio; 1202185029Spjd int error; 1203185029Spjd int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 1204168404Spjd 1205213198Smm /* 1206213198Smm * Normally, we don't want to try too hard to write every label and 1207213198Smm * uberblock. If there is a flaky disk, we don't want the rest of the 1208213198Smm * sync process to block while we retry. But if we can't write a 1209213198Smm * single label out, we should retry with ZIO_FLAG_TRYHARD before 1210213198Smm * bailing out and declaring the pool faulted. 1211213198Smm */ 1212213198Smm if (tryhard) 1213213198Smm flags |= ZIO_FLAG_TRYHARD; 1214213198Smm 1215168404Spjd ASSERT(ub->ub_txg <= txg); 1216168404Spjd 1217168404Spjd /* 1218185029Spjd * If this isn't a resync due to I/O errors, 1219185029Spjd * and nothing changed in this transaction group, 1220185029Spjd * and the vdev configuration hasn't changed, 1221168404Spjd * then there's nothing to do. 1222168404Spjd */ 1223185029Spjd if (ub->ub_txg < txg && 1224185029Spjd uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 1225185029Spjd list_is_empty(&spa->spa_config_dirty_list)) 1226168404Spjd return (0); 1227168404Spjd 1228168404Spjd if (txg > spa_freeze_txg(spa)) 1229168404Spjd return (0); 1230168404Spjd 1231168404Spjd ASSERT(txg <= spa->spa_final_txg); 1232168404Spjd 1233168404Spjd /* 1234168404Spjd * Flush the write cache of every disk that's been written to 1235168404Spjd * in this transaction group. This ensures that all blocks 1236168404Spjd * written in this txg will be committed to stable storage 1237168404Spjd * before any uberblock that references them. 1238168404Spjd */ 1239185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1240185029Spjd 1241168404Spjd for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 1242185029Spjd vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 1243185029Spjd zio_flush(zio, vd); 1244185029Spjd 1245168404Spjd (void) zio_wait(zio); 1246168404Spjd 1247168404Spjd /* 1248168404Spjd * Sync out the even labels (L0, L2) for every dirty vdev. If the 1249168404Spjd * system dies in the middle of this process, that's OK: all of the 1250168404Spjd * even labels that made it to disk will be newer than any uberblock, 1251168404Spjd * and will therefore be considered invalid. The odd labels (L1, L3), 1252185029Spjd * which have not yet been touched, will still be valid. We flush 1253185029Spjd * the new labels to disk to ensure that all even-label updates 1254185029Spjd * are committed to stable storage before the uberblock update. 1255168404Spjd */ 1256185029Spjd if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) 1257185029Spjd return (error); 1258168404Spjd 1259168404Spjd /* 1260185029Spjd * Sync the uberblocks to all vdevs in svd[]. 1261168404Spjd * If the system dies in the middle of this step, there are two cases 1262168404Spjd * to consider, and the on-disk state is consistent either way: 1263168404Spjd * 1264168404Spjd * (1) If none of the new uberblocks made it to disk, then the 1265168404Spjd * previous uberblock will be the newest, and the odd labels 1266168404Spjd * (which had not yet been touched) will be valid with respect 1267168404Spjd * to that uberblock. 1268168404Spjd * 1269168404Spjd * (2) If one or more new uberblocks made it to disk, then they 1270168404Spjd * will be the newest, and the even labels (which had all 1271168404Spjd * been successfully committed) will be valid with respect 1272168404Spjd * to the new uberblocks. 1273168404Spjd */ 1274185029Spjd if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) 1275168404Spjd return (error); 1276168404Spjd 1277168404Spjd /* 1278168404Spjd * Sync out odd labels for every dirty vdev. If the system dies 1279168404Spjd * in the middle of this process, the even labels and the new 1280168404Spjd * uberblocks will suffice to open the pool. The next time 1281168404Spjd * the pool is opened, the first thing we'll do -- before any 1282168404Spjd * user data is modified -- is mark every vdev dirty so that 1283185029Spjd * all labels will be brought up to date. We flush the new labels 1284185029Spjd * to disk to ensure that all odd-label updates are committed to 1285185029Spjd * stable storage before the next transaction group begins. 1286168404Spjd */ 1287240868Spjd if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) 1288240868Spjd return (error); 1289240868Spjd 1290240868Spjd trim_thread_wakeup(spa); 1291240868Spjd 1292240868Spjd return (0); 1293168404Spjd} 1294