vdev_label.c revision 236884
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. 24236884Smm * Copyright (c) 2012 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> 148168404Spjd#include <sys/fs/zfs.h> 149168404Spjd 150168404Spjd/* 151168404Spjd * Basic routines to read and write from a vdev label. 152168404Spjd * Used throughout the rest of this file. 153168404Spjd */ 154168404Spjduint64_t 155168404Spjdvdev_label_offset(uint64_t psize, int l, uint64_t offset) 156168404Spjd{ 157168404Spjd ASSERT(offset < sizeof (vdev_label_t)); 158185029Spjd ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0); 159168404Spjd 160168404Spjd return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? 161168404Spjd 0 : psize - VDEV_LABELS * sizeof (vdev_label_t))); 162168404Spjd} 163168404Spjd 164185029Spjd/* 165185029Spjd * Returns back the vdev label associated with the passed in offset. 166185029Spjd */ 167185029Spjdint 168185029Spjdvdev_label_number(uint64_t psize, uint64_t offset) 169185029Spjd{ 170185029Spjd int l; 171185029Spjd 172185029Spjd if (offset >= psize - VDEV_LABEL_END_SIZE) { 173185029Spjd offset -= psize - VDEV_LABEL_END_SIZE; 174185029Spjd offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t); 175185029Spjd } 176185029Spjd l = offset / sizeof (vdev_label_t); 177185029Spjd return (l < VDEV_LABELS ? l : -1); 178185029Spjd} 179185029Spjd 180168404Spjdstatic void 181168404Spjdvdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 182185029Spjd uint64_t size, zio_done_func_t *done, void *private, int flags) 183168404Spjd{ 184185029Spjd ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) == 185185029Spjd SCL_STATE_ALL); 186185029Spjd ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 187168404Spjd 188168404Spjd zio_nowait(zio_read_phys(zio, vd, 189168404Spjd vdev_label_offset(vd->vdev_psize, l, offset), 190168404Spjd size, buf, ZIO_CHECKSUM_LABEL, done, private, 191185029Spjd ZIO_PRIORITY_SYNC_READ, flags, B_TRUE)); 192168404Spjd} 193168404Spjd 194168404Spjdstatic void 195168404Spjdvdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset, 196185029Spjd uint64_t size, zio_done_func_t *done, void *private, int flags) 197168404Spjd{ 198185029Spjd ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL || 199185029Spjd (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) == 200185029Spjd (SCL_CONFIG | SCL_STATE) && 201185029Spjd dsl_pool_sync_context(spa_get_dsl(zio->io_spa)))); 202185029Spjd ASSERT(flags & ZIO_FLAG_CONFIG_WRITER); 203168404Spjd 204168404Spjd zio_nowait(zio_write_phys(zio, vd, 205168404Spjd vdev_label_offset(vd->vdev_psize, l, offset), 206168404Spjd size, buf, ZIO_CHECKSUM_LABEL, done, private, 207185029Spjd ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE)); 208168404Spjd} 209168404Spjd 210168404Spjd/* 211168404Spjd * Generate the nvlist representing this vdev's config. 212168404Spjd */ 213168404Spjdnvlist_t * 214168404Spjdvdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats, 215219089Spjd vdev_config_flag_t flags) 216168404Spjd{ 217168404Spjd nvlist_t *nv = NULL; 218168404Spjd 219168404Spjd VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 220168404Spjd 221168404Spjd VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE, 222168404Spjd vd->vdev_ops->vdev_op_type) == 0); 223219089Spjd if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE))) 224168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id) 225168404Spjd == 0); 226168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0); 227168404Spjd 228168404Spjd if (vd->vdev_path != NULL) 229168404Spjd VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, 230168404Spjd vd->vdev_path) == 0); 231168404Spjd 232168404Spjd if (vd->vdev_devid != NULL) 233168404Spjd VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, 234168404Spjd vd->vdev_devid) == 0); 235168404Spjd 236185029Spjd if (vd->vdev_physpath != NULL) 237185029Spjd VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, 238185029Spjd vd->vdev_physpath) == 0); 239185029Spjd 240209962Smm if (vd->vdev_fru != NULL) 241209962Smm VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU, 242209962Smm vd->vdev_fru) == 0); 243209962Smm 244168404Spjd if (vd->vdev_nparity != 0) { 245168404Spjd ASSERT(strcmp(vd->vdev_ops->vdev_op_type, 246168404Spjd VDEV_TYPE_RAIDZ) == 0); 247168404Spjd 248168404Spjd /* 249168404Spjd * Make sure someone hasn't managed to sneak a fancy new vdev 250168404Spjd * into a crufty old storage pool. 251168404Spjd */ 252168404Spjd ASSERT(vd->vdev_nparity == 1 || 253219089Spjd (vd->vdev_nparity <= 2 && 254219089Spjd spa_version(spa) >= SPA_VERSION_RAIDZ2) || 255219089Spjd (vd->vdev_nparity <= 3 && 256219089Spjd spa_version(spa) >= SPA_VERSION_RAIDZ3)); 257168404Spjd 258168404Spjd /* 259168404Spjd * Note that we'll add the nparity tag even on storage pools 260168404Spjd * that only support a single parity device -- older software 261168404Spjd * will just ignore it. 262168404Spjd */ 263168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, 264168404Spjd vd->vdev_nparity) == 0); 265168404Spjd } 266168404Spjd 267168404Spjd if (vd->vdev_wholedisk != -1ULL) 268168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 269168404Spjd vd->vdev_wholedisk) == 0); 270168404Spjd 271168404Spjd if (vd->vdev_not_present) 272168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0); 273168404Spjd 274168404Spjd if (vd->vdev_isspare) 275168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0); 276168404Spjd 277219089Spjd if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) && 278219089Spjd vd == vd->vdev_top) { 279168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 280168404Spjd vd->vdev_ms_array) == 0); 281168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 282168404Spjd vd->vdev_ms_shift) == 0); 283168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, 284168404Spjd vd->vdev_ashift) == 0); 285168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, 286168404Spjd vd->vdev_asize) == 0); 287185029Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, 288185029Spjd vd->vdev_islog) == 0); 289219089Spjd if (vd->vdev_removing) 290219089Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING, 291219089Spjd vd->vdev_removing) == 0); 292168404Spjd } 293168404Spjd 294209962Smm if (vd->vdev_dtl_smo.smo_object != 0) 295168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL, 296209962Smm vd->vdev_dtl_smo.smo_object) == 0); 297168404Spjd 298219089Spjd if (vd->vdev_crtxg) 299219089Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 300219089Spjd vd->vdev_crtxg) == 0); 301219089Spjd 302168404Spjd if (getstats) { 303168404Spjd vdev_stat_t vs; 304219089Spjd pool_scan_stat_t ps; 305219089Spjd 306168404Spjd vdev_get_stats(vd, &vs); 307219089Spjd VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 308168404Spjd (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0); 309219089Spjd 310219089Spjd /* provide either current or previous scan information */ 311219089Spjd if (spa_scan_get_stats(spa, &ps) == 0) { 312219089Spjd VERIFY(nvlist_add_uint64_array(nv, 313219089Spjd ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps, 314219089Spjd sizeof (pool_scan_stat_t) / sizeof (uint64_t)) 315219089Spjd == 0); 316219089Spjd } 317168404Spjd } 318168404Spjd 319168404Spjd if (!vd->vdev_ops->vdev_op_leaf) { 320168404Spjd nvlist_t **child; 321219089Spjd int c, idx; 322168404Spjd 323219089Spjd ASSERT(!vd->vdev_ishole); 324219089Spjd 325168404Spjd child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *), 326168404Spjd KM_SLEEP); 327168404Spjd 328219089Spjd for (c = 0, idx = 0; c < vd->vdev_children; c++) { 329219089Spjd vdev_t *cvd = vd->vdev_child[c]; 330168404Spjd 331219089Spjd /* 332219089Spjd * If we're generating an nvlist of removing 333219089Spjd * vdevs then skip over any device which is 334219089Spjd * not being removed. 335219089Spjd */ 336219089Spjd if ((flags & VDEV_CONFIG_REMOVING) && 337219089Spjd !cvd->vdev_removing) 338219089Spjd continue; 339168404Spjd 340219089Spjd child[idx++] = vdev_config_generate(spa, cvd, 341219089Spjd getstats, flags); 342219089Spjd } 343219089Spjd 344219089Spjd if (idx) { 345219089Spjd VERIFY(nvlist_add_nvlist_array(nv, 346219089Spjd ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 347219089Spjd } 348219089Spjd 349219089Spjd for (c = 0; c < idx; c++) 350168404Spjd nvlist_free(child[c]); 351168404Spjd 352168404Spjd kmem_free(child, vd->vdev_children * sizeof (nvlist_t *)); 353168404Spjd 354168404Spjd } else { 355219089Spjd const char *aux = NULL; 356219089Spjd 357168404Spjd if (vd->vdev_offline && !vd->vdev_tmpoffline) 358168404Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, 359168404Spjd B_TRUE) == 0); 360219089Spjd if (vd->vdev_resilvering) 361219089Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVERING, 362219089Spjd B_TRUE) == 0); 363185029Spjd if (vd->vdev_faulted) 364185029Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, 365185029Spjd B_TRUE) == 0); 366185029Spjd if (vd->vdev_degraded) 367185029Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, 368185029Spjd B_TRUE) == 0); 369185029Spjd if (vd->vdev_removed) 370185029Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, 371185029Spjd B_TRUE) == 0); 372185029Spjd if (vd->vdev_unspare) 373185029Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, 374185029Spjd B_TRUE) == 0); 375219089Spjd if (vd->vdev_ishole) 376219089Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, 377219089Spjd B_TRUE) == 0); 378219089Spjd 379219089Spjd switch (vd->vdev_stat.vs_aux) { 380219089Spjd case VDEV_AUX_ERR_EXCEEDED: 381219089Spjd aux = "err_exceeded"; 382219089Spjd break; 383219089Spjd 384219089Spjd case VDEV_AUX_EXTERNAL: 385219089Spjd aux = "external"; 386219089Spjd break; 387219089Spjd } 388219089Spjd 389219089Spjd if (aux != NULL) 390219089Spjd VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, 391219089Spjd aux) == 0); 392219089Spjd 393219089Spjd if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) { 394219089Spjd VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID, 395219089Spjd vd->vdev_orig_guid) == 0); 396219089Spjd } 397168404Spjd } 398168404Spjd 399168404Spjd return (nv); 400168404Spjd} 401168404Spjd 402219089Spjd/* 403219089Spjd * Generate a view of the top-level vdevs. If we currently have holes 404219089Spjd * in the namespace, then generate an array which contains a list of holey 405219089Spjd * vdevs. Additionally, add the number of top-level children that currently 406219089Spjd * exist. 407219089Spjd */ 408219089Spjdvoid 409219089Spjdvdev_top_config_generate(spa_t *spa, nvlist_t *config) 410219089Spjd{ 411219089Spjd vdev_t *rvd = spa->spa_root_vdev; 412219089Spjd uint64_t *array; 413219089Spjd uint_t c, idx; 414219089Spjd 415219089Spjd array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP); 416219089Spjd 417219089Spjd for (c = 0, idx = 0; c < rvd->vdev_children; c++) { 418219089Spjd vdev_t *tvd = rvd->vdev_child[c]; 419219089Spjd 420219089Spjd if (tvd->vdev_ishole) 421219089Spjd array[idx++] = c; 422219089Spjd } 423219089Spjd 424219089Spjd if (idx) { 425219089Spjd VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY, 426219089Spjd array, idx) == 0); 427219089Spjd } 428219089Spjd 429219089Spjd VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 430219089Spjd rvd->vdev_children) == 0); 431219089Spjd 432219089Spjd kmem_free(array, rvd->vdev_children * sizeof (uint64_t)); 433219089Spjd} 434219089Spjd 435236884Smm/* 436236884Smm * Returns the configuration from the label of the given vdev. If 'label' is 437236884Smm * VDEV_BEST_LABEL, each label of the vdev will be read until a valid 438236884Smm * configuration is found; otherwise, only the specified label will be read. 439236884Smm */ 440168404Spjdnvlist_t * 441236884Smmvdev_label_read_config(vdev_t *vd, int label) 442168404Spjd{ 443168404Spjd spa_t *spa = vd->vdev_spa; 444168404Spjd nvlist_t *config = NULL; 445168404Spjd vdev_phys_t *vp; 446168404Spjd zio_t *zio; 447213198Smm int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 448213198Smm ZIO_FLAG_SPECULATIVE; 449168404Spjd 450185029Spjd ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 451168404Spjd 452185029Spjd if (!vdev_readable(vd)) 453168404Spjd return (NULL); 454168404Spjd 455168404Spjd vp = zio_buf_alloc(sizeof (vdev_phys_t)); 456168404Spjd 457213198Smmretry: 458185029Spjd for (int l = 0; l < VDEV_LABELS; l++) { 459236884Smm if (label >= 0 && label < VDEV_LABELS && label != l) 460236884Smm continue; 461168404Spjd 462185029Spjd zio = zio_root(spa, NULL, NULL, flags); 463168404Spjd 464168404Spjd vdev_label_read(zio, vd, l, vp, 465168404Spjd offsetof(vdev_label_t, vl_vdev_phys), 466185029Spjd sizeof (vdev_phys_t), NULL, NULL, flags); 467168404Spjd 468168404Spjd if (zio_wait(zio) == 0 && 469168404Spjd nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), 470168404Spjd &config, 0) == 0) 471168404Spjd break; 472168404Spjd 473168404Spjd if (config != NULL) { 474168404Spjd nvlist_free(config); 475168404Spjd config = NULL; 476168404Spjd } 477168404Spjd } 478168404Spjd 479213198Smm if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) { 480213198Smm flags |= ZIO_FLAG_TRYHARD; 481213198Smm goto retry; 482213198Smm } 483213198Smm 484168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 485168404Spjd 486168404Spjd return (config); 487168404Spjd} 488168404Spjd 489168404Spjd/* 490168404Spjd * Determine if a device is in use. The 'spare_guid' parameter will be filled 491168404Spjd * in with the device guid if this spare is active elsewhere on the system. 492168404Spjd */ 493168404Spjdstatic boolean_t 494168404Spjdvdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason, 495185029Spjd uint64_t *spare_guid, uint64_t *l2cache_guid) 496168404Spjd{ 497168404Spjd spa_t *spa = vd->vdev_spa; 498168404Spjd uint64_t state, pool_guid, device_guid, txg, spare_pool; 499168404Spjd uint64_t vdtxg = 0; 500168404Spjd nvlist_t *label; 501168404Spjd 502168404Spjd if (spare_guid) 503168404Spjd *spare_guid = 0ULL; 504185029Spjd if (l2cache_guid) 505185029Spjd *l2cache_guid = 0ULL; 506168404Spjd 507168404Spjd /* 508168404Spjd * Read the label, if any, and perform some basic sanity checks. 509168404Spjd */ 510236884Smm if ((label = vdev_label_read_config(vd, VDEV_BEST_LABEL)) == NULL) 511168404Spjd return (B_FALSE); 512168404Spjd 513168404Spjd (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 514168404Spjd &vdtxg); 515168404Spjd 516168404Spjd if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 517168404Spjd &state) != 0 || 518168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 519168404Spjd &device_guid) != 0) { 520168404Spjd nvlist_free(label); 521168404Spjd return (B_FALSE); 522168404Spjd } 523168404Spjd 524185029Spjd if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 525168404Spjd (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 526168404Spjd &pool_guid) != 0 || 527168404Spjd nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 528168404Spjd &txg) != 0)) { 529168404Spjd nvlist_free(label); 530168404Spjd return (B_FALSE); 531168404Spjd } 532168404Spjd 533168404Spjd nvlist_free(label); 534168404Spjd 535168404Spjd /* 536168404Spjd * Check to see if this device indeed belongs to the pool it claims to 537168404Spjd * be a part of. The only way this is allowed is if the device is a hot 538168404Spjd * spare (which we check for later on). 539168404Spjd */ 540185029Spjd if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 541168404Spjd !spa_guid_exists(pool_guid, device_guid) && 542185029Spjd !spa_spare_exists(device_guid, NULL, NULL) && 543185029Spjd !spa_l2cache_exists(device_guid, NULL)) 544168404Spjd return (B_FALSE); 545168404Spjd 546168404Spjd /* 547168404Spjd * If the transaction group is zero, then this an initialized (but 548168404Spjd * unused) label. This is only an error if the create transaction 549168404Spjd * on-disk is the same as the one we're using now, in which case the 550168404Spjd * user has attempted to add the same vdev multiple times in the same 551168404Spjd * transaction. 552168404Spjd */ 553185029Spjd if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && 554185029Spjd txg == 0 && vdtxg == crtxg) 555168404Spjd return (B_TRUE); 556168404Spjd 557168404Spjd /* 558168404Spjd * Check to see if this is a spare device. We do an explicit check for 559168404Spjd * spa_has_spare() here because it may be on our pending list of spares 560185029Spjd * to add. We also check if it is an l2cache device. 561168404Spjd */ 562185029Spjd if (spa_spare_exists(device_guid, &spare_pool, NULL) || 563168404Spjd spa_has_spare(spa, device_guid)) { 564168404Spjd if (spare_guid) 565168404Spjd *spare_guid = device_guid; 566168404Spjd 567168404Spjd switch (reason) { 568168404Spjd case VDEV_LABEL_CREATE: 569185029Spjd case VDEV_LABEL_L2CACHE: 570168404Spjd return (B_TRUE); 571168404Spjd 572168404Spjd case VDEV_LABEL_REPLACE: 573168404Spjd return (!spa_has_spare(spa, device_guid) || 574168404Spjd spare_pool != 0ULL); 575168404Spjd 576168404Spjd case VDEV_LABEL_SPARE: 577168404Spjd return (spa_has_spare(spa, device_guid)); 578168404Spjd } 579168404Spjd } 580168404Spjd 581168404Spjd /* 582185029Spjd * Check to see if this is an l2cache device. 583185029Spjd */ 584185029Spjd if (spa_l2cache_exists(device_guid, NULL)) 585185029Spjd return (B_TRUE); 586185029Spjd 587185029Spjd /* 588219089Spjd * We can't rely on a pool's state if it's been imported 589219089Spjd * read-only. Instead we look to see if the pools is marked 590219089Spjd * read-only in the namespace and set the state to active. 591219089Spjd */ 592219089Spjd if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL && 593219089Spjd spa_mode(spa) == FREAD) 594219089Spjd state = POOL_STATE_ACTIVE; 595219089Spjd 596219089Spjd /* 597168404Spjd * If the device is marked ACTIVE, then this device is in use by another 598168404Spjd * pool on the system. 599168404Spjd */ 600168404Spjd return (state == POOL_STATE_ACTIVE); 601168404Spjd} 602168404Spjd 603168404Spjd/* 604168404Spjd * Initialize a vdev label. We check to make sure each leaf device is not in 605168404Spjd * use, and writable. We put down an initial label which we will later 606168404Spjd * overwrite with a complete label. Note that it's important to do this 607168404Spjd * sequentially, not in parallel, so that we catch cases of multiple use of the 608168404Spjd * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with 609168404Spjd * itself. 610168404Spjd */ 611168404Spjdint 612168404Spjdvdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason) 613168404Spjd{ 614168404Spjd spa_t *spa = vd->vdev_spa; 615168404Spjd nvlist_t *label; 616168404Spjd vdev_phys_t *vp; 617209962Smm char *pad2; 618168404Spjd uberblock_t *ub; 619168404Spjd zio_t *zio; 620168404Spjd char *buf; 621168404Spjd size_t buflen; 622168404Spjd int error; 623185029Spjd uint64_t spare_guid, l2cache_guid; 624185029Spjd int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 625168404Spjd 626185029Spjd ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 627168404Spjd 628185029Spjd for (int c = 0; c < vd->vdev_children; c++) 629168404Spjd if ((error = vdev_label_init(vd->vdev_child[c], 630168404Spjd crtxg, reason)) != 0) 631168404Spjd return (error); 632168404Spjd 633219089Spjd /* Track the creation time for this vdev */ 634219089Spjd vd->vdev_crtxg = crtxg; 635219089Spjd 636168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 637168404Spjd return (0); 638168404Spjd 639168404Spjd /* 640168404Spjd * Dead vdevs cannot be initialized. 641168404Spjd */ 642168404Spjd if (vdev_is_dead(vd)) 643168404Spjd return (EIO); 644168404Spjd 645168404Spjd /* 646168404Spjd * Determine if the vdev is in use. 647168404Spjd */ 648219089Spjd if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT && 649185029Spjd vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) 650168404Spjd return (EBUSY); 651168404Spjd 652168404Spjd /* 653185029Spjd * If this is a request to add or replace a spare or l2cache device 654185029Spjd * that is in use elsewhere on the system, then we must update the 655185029Spjd * guid (which was initialized to a random value) to reflect the 656185029Spjd * actual GUID (which is shared between multiple pools). 657168404Spjd */ 658185029Spjd if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE && 659185029Spjd spare_guid != 0ULL) { 660185029Spjd uint64_t guid_delta = spare_guid - vd->vdev_guid; 661168404Spjd 662185029Spjd vd->vdev_guid += guid_delta; 663168404Spjd 664185029Spjd for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 665185029Spjd pvd->vdev_guid_sum += guid_delta; 666168404Spjd 667168404Spjd /* 668168404Spjd * If this is a replacement, then we want to fallthrough to the 669168404Spjd * rest of the code. If we're adding a spare, then it's already 670185029Spjd * labeled appropriately and we can just return. 671168404Spjd */ 672168404Spjd if (reason == VDEV_LABEL_SPARE) 673168404Spjd return (0); 674219089Spjd ASSERT(reason == VDEV_LABEL_REPLACE || 675219089Spjd reason == VDEV_LABEL_SPLIT); 676168404Spjd } 677168404Spjd 678185029Spjd if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE && 679185029Spjd l2cache_guid != 0ULL) { 680185029Spjd uint64_t guid_delta = l2cache_guid - vd->vdev_guid; 681185029Spjd 682185029Spjd vd->vdev_guid += guid_delta; 683185029Spjd 684185029Spjd for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 685185029Spjd pvd->vdev_guid_sum += guid_delta; 686185029Spjd 687185029Spjd /* 688185029Spjd * If this is a replacement, then we want to fallthrough to the 689185029Spjd * rest of the code. If we're adding an l2cache, then it's 690185029Spjd * already labeled appropriately and we can just return. 691185029Spjd */ 692185029Spjd if (reason == VDEV_LABEL_L2CACHE) 693185029Spjd return (0); 694185029Spjd ASSERT(reason == VDEV_LABEL_REPLACE); 695185029Spjd } 696185029Spjd 697168404Spjd /* 698168404Spjd * Initialize its label. 699168404Spjd */ 700168404Spjd vp = zio_buf_alloc(sizeof (vdev_phys_t)); 701168404Spjd bzero(vp, sizeof (vdev_phys_t)); 702168404Spjd 703168404Spjd /* 704168404Spjd * Generate a label describing the pool and our top-level vdev. 705168404Spjd * We mark it as being from txg 0 to indicate that it's not 706168404Spjd * really part of an active pool just yet. The labels will 707168404Spjd * be written again with a meaningful txg by spa_sync(). 708168404Spjd */ 709168404Spjd if (reason == VDEV_LABEL_SPARE || 710168404Spjd (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) { 711168404Spjd /* 712168404Spjd * For inactive hot spares, we generate a special label that 713168404Spjd * identifies as a mutually shared hot spare. We write the 714168404Spjd * label if we are adding a hot spare, or if we are removing an 715168404Spjd * active hot spare (in which case we want to revert the 716168404Spjd * labels). 717168404Spjd */ 718168404Spjd VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 719168404Spjd 720168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 721168404Spjd spa_version(spa)) == 0); 722168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 723168404Spjd POOL_STATE_SPARE) == 0); 724168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 725168404Spjd vd->vdev_guid) == 0); 726185029Spjd } else if (reason == VDEV_LABEL_L2CACHE || 727185029Spjd (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) { 728185029Spjd /* 729185029Spjd * For level 2 ARC devices, add a special label. 730185029Spjd */ 731185029Spjd VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0); 732185029Spjd 733185029Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION, 734185029Spjd spa_version(spa)) == 0); 735185029Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, 736185029Spjd POOL_STATE_L2CACHE) == 0); 737185029Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID, 738185029Spjd vd->vdev_guid) == 0); 739168404Spjd } else { 740219089Spjd uint64_t txg = 0ULL; 741168404Spjd 742219089Spjd if (reason == VDEV_LABEL_SPLIT) 743219089Spjd txg = spa->spa_uberblock.ub_txg; 744219089Spjd label = spa_config_generate(spa, vd, txg, B_FALSE); 745219089Spjd 746168404Spjd /* 747168404Spjd * Add our creation time. This allows us to detect multiple 748168404Spjd * vdev uses as described above, and automatically expires if we 749168404Spjd * fail. 750168404Spjd */ 751168404Spjd VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 752168404Spjd crtxg) == 0); 753168404Spjd } 754168404Spjd 755168404Spjd buf = vp->vp_nvlist; 756168404Spjd buflen = sizeof (vp->vp_nvlist); 757168404Spjd 758168404Spjd error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); 759168404Spjd if (error != 0) { 760168404Spjd nvlist_free(label); 761168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 762168404Spjd /* EFAULT means nvlist_pack ran out of room */ 763168404Spjd return (error == EFAULT ? ENAMETOOLONG : EINVAL); 764168404Spjd } 765168404Spjd 766168404Spjd /* 767168404Spjd * Initialize uberblock template. 768168404Spjd */ 769219089Spjd ub = zio_buf_alloc(VDEV_UBERBLOCK_RING); 770219089Spjd bzero(ub, VDEV_UBERBLOCK_RING); 771168404Spjd *ub = spa->spa_uberblock; 772168404Spjd ub->ub_txg = 0; 773168404Spjd 774209962Smm /* Initialize the 2nd padding area. */ 775209962Smm pad2 = zio_buf_alloc(VDEV_PAD_SIZE); 776209962Smm bzero(pad2, VDEV_PAD_SIZE); 777209962Smm 778168404Spjd /* 779168404Spjd * Write everything in parallel. 780168404Spjd */ 781213198Smmretry: 782185029Spjd zio = zio_root(spa, NULL, NULL, flags); 783168404Spjd 784185029Spjd for (int l = 0; l < VDEV_LABELS; l++) { 785168404Spjd 786168404Spjd vdev_label_write(zio, vd, l, vp, 787168404Spjd offsetof(vdev_label_t, vl_vdev_phys), 788185029Spjd sizeof (vdev_phys_t), NULL, NULL, flags); 789168404Spjd 790209962Smm /* 791209962Smm * Skip the 1st padding area. 792209962Smm * Zero out the 2nd padding area where it might have 793209962Smm * left over data from previous filesystem format. 794209962Smm */ 795209962Smm vdev_label_write(zio, vd, l, pad2, 796209962Smm offsetof(vdev_label_t, vl_pad2), 797209962Smm VDEV_PAD_SIZE, NULL, NULL, flags); 798168404Spjd 799219089Spjd vdev_label_write(zio, vd, l, ub, 800219089Spjd offsetof(vdev_label_t, vl_uberblock), 801219089Spjd VDEV_UBERBLOCK_RING, NULL, NULL, flags); 802168404Spjd } 803168404Spjd 804168404Spjd error = zio_wait(zio); 805168404Spjd 806213198Smm if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) { 807213198Smm flags |= ZIO_FLAG_TRYHARD; 808213198Smm goto retry; 809213198Smm } 810213198Smm 811168404Spjd nvlist_free(label); 812209962Smm zio_buf_free(pad2, VDEV_PAD_SIZE); 813219089Spjd zio_buf_free(ub, VDEV_UBERBLOCK_RING); 814168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 815168404Spjd 816168404Spjd /* 817168404Spjd * If this vdev hasn't been previously identified as a spare, then we 818185029Spjd * mark it as such only if a) we are labeling it as a spare, or b) it 819185029Spjd * exists as a spare elsewhere in the system. Do the same for 820185029Spjd * level 2 ARC devices. 821168404Spjd */ 822168404Spjd if (error == 0 && !vd->vdev_isspare && 823168404Spjd (reason == VDEV_LABEL_SPARE || 824185029Spjd spa_spare_exists(vd->vdev_guid, NULL, NULL))) 825168404Spjd spa_spare_add(vd); 826168404Spjd 827185029Spjd if (error == 0 && !vd->vdev_isl2cache && 828185029Spjd (reason == VDEV_LABEL_L2CACHE || 829185029Spjd spa_l2cache_exists(vd->vdev_guid, NULL))) 830185029Spjd spa_l2cache_add(vd); 831185029Spjd 832168404Spjd return (error); 833168404Spjd} 834168404Spjd 835168404Spjd/* 836168404Spjd * ========================================================================== 837168404Spjd * uberblock load/sync 838168404Spjd * ========================================================================== 839168404Spjd */ 840168404Spjd 841168404Spjd/* 842168404Spjd * Consider the following situation: txg is safely synced to disk. We've 843168404Spjd * written the first uberblock for txg + 1, and then we lose power. When we 844168404Spjd * come back up, we fail to see the uberblock for txg + 1 because, say, 845168404Spjd * it was on a mirrored device and the replica to which we wrote txg + 1 846168404Spjd * is now offline. If we then make some changes and sync txg + 1, and then 847236884Smm * the missing replica comes back, then for a few seconds we'll have two 848168404Spjd * conflicting uberblocks on disk with the same txg. The solution is simple: 849168404Spjd * among uberblocks with equal txg, choose the one with the latest timestamp. 850168404Spjd */ 851168404Spjdstatic int 852168404Spjdvdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 853168404Spjd{ 854168404Spjd if (ub1->ub_txg < ub2->ub_txg) 855168404Spjd return (-1); 856168404Spjd if (ub1->ub_txg > ub2->ub_txg) 857168404Spjd return (1); 858168404Spjd 859168404Spjd if (ub1->ub_timestamp < ub2->ub_timestamp) 860168404Spjd return (-1); 861168404Spjd if (ub1->ub_timestamp > ub2->ub_timestamp) 862168404Spjd return (1); 863168404Spjd 864168404Spjd return (0); 865168404Spjd} 866168404Spjd 867236884Smmstruct ubl_cbdata { 868236884Smm uberblock_t *ubl_ubbest; /* Best uberblock */ 869236884Smm vdev_t *ubl_vd; /* vdev associated with the above */ 870236884Smm int ubl_label; /* Label associated with the above */ 871236884Smm}; 872236884Smm 873168404Spjdstatic void 874168404Spjdvdev_uberblock_load_done(zio_t *zio) 875168404Spjd{ 876236884Smm vdev_t *vd = zio->io_vd; 877219089Spjd spa_t *spa = zio->io_spa; 878185029Spjd zio_t *rio = zio->io_private; 879168404Spjd uberblock_t *ub = zio->io_data; 880236884Smm struct ubl_cbdata *cbp = rio->io_private; 881168404Spjd 882236884Smm ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd)); 883168404Spjd 884168404Spjd if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 885185029Spjd mutex_enter(&rio->io_lock); 886219089Spjd if (ub->ub_txg <= spa->spa_load_max_txg && 887236884Smm vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) { 888236884Smm /* 889236884Smm * Keep track of the vdev and label in which this 890236884Smm * uberblock was found. We will use this information 891236884Smm * later to obtain the config nvlist associated with 892236884Smm * this uberblock. 893236884Smm */ 894236884Smm *cbp->ubl_ubbest = *ub; 895236884Smm cbp->ubl_vd = vd; 896236884Smm cbp->ubl_label = vdev_label_number(vd->vdev_psize, 897236884Smm zio->io_offset); 898236884Smm } 899185029Spjd mutex_exit(&rio->io_lock); 900168404Spjd } 901168404Spjd 902168404Spjd zio_buf_free(zio->io_data, zio->io_size); 903168404Spjd} 904168404Spjd 905236884Smmstatic void 906236884Smmvdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags, 907236884Smm struct ubl_cbdata *cbp) 908168404Spjd{ 909185029Spjd for (int c = 0; c < vd->vdev_children; c++) 910236884Smm vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp); 911168404Spjd 912185029Spjd if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 913185029Spjd for (int l = 0; l < VDEV_LABELS; l++) { 914185029Spjd for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 915185029Spjd vdev_label_read(zio, vd, l, 916185029Spjd zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 917185029Spjd VDEV_UBERBLOCK_OFFSET(vd, n), 918185029Spjd VDEV_UBERBLOCK_SIZE(vd), 919185029Spjd vdev_uberblock_load_done, zio, flags); 920185029Spjd } 921168404Spjd } 922168404Spjd } 923236884Smm} 924185029Spjd 925236884Smm/* 926236884Smm * Reads the 'best' uberblock from disk along with its associated 927236884Smm * configuration. First, we read the uberblock array of each label of each 928236884Smm * vdev, keeping track of the uberblock with the highest txg in each array. 929236884Smm * Then, we read the configuration from the same label as the best uberblock. 930236884Smm */ 931236884Smmvoid 932236884Smmvdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config) 933236884Smm{ 934236884Smm int i; 935236884Smm zio_t *zio; 936236884Smm spa_t *spa = rvd->vdev_spa; 937236884Smm struct ubl_cbdata cb; 938236884Smm int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 939236884Smm ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD; 940236884Smm 941236884Smm ASSERT(ub); 942236884Smm ASSERT(config); 943236884Smm 944236884Smm bzero(ub, sizeof (uberblock_t)); 945236884Smm *config = NULL; 946236884Smm 947236884Smm cb.ubl_ubbest = ub; 948236884Smm cb.ubl_vd = NULL; 949236884Smm 950236884Smm spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 951236884Smm zio = zio_root(spa, NULL, &cb, flags); 952236884Smm vdev_uberblock_load_impl(zio, rvd, flags, &cb); 953236884Smm (void) zio_wait(zio); 954236884Smm if (cb.ubl_vd != NULL) { 955236884Smm for (i = cb.ubl_label % 2; i < VDEV_LABELS; i += 2) { 956236884Smm *config = vdev_label_read_config(cb.ubl_vd, i); 957236884Smm if (*config != NULL) 958236884Smm break; 959236884Smm } 960185029Spjd } 961236884Smm spa_config_exit(spa, SCL_ALL, FTAG); 962168404Spjd} 963168404Spjd 964168404Spjd/* 965185029Spjd * On success, increment root zio's count of good writes. 966168404Spjd * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 967168404Spjd */ 968168404Spjdstatic void 969168404Spjdvdev_uberblock_sync_done(zio_t *zio) 970168404Spjd{ 971185029Spjd uint64_t *good_writes = zio->io_private; 972168404Spjd 973168404Spjd if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0) 974168404Spjd atomic_add_64(good_writes, 1); 975168404Spjd} 976168404Spjd 977185029Spjd/* 978185029Spjd * Write the uberblock to all labels of all leaves of the specified vdev. 979185029Spjd */ 980168404Spjdstatic void 981185029Spjdvdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags) 982168404Spjd{ 983185029Spjd uberblock_t *ubbuf; 984185029Spjd int n; 985168404Spjd 986185029Spjd for (int c = 0; c < vd->vdev_children; c++) 987185029Spjd vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags); 988168404Spjd 989168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 990168404Spjd return; 991168404Spjd 992185029Spjd if (!vdev_writeable(vd)) 993168404Spjd return; 994168404Spjd 995185029Spjd n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1); 996168404Spjd 997185029Spjd ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)); 998185029Spjd bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 999185029Spjd *ubbuf = *ub; 1000168404Spjd 1001185029Spjd for (int l = 0; l < VDEV_LABELS; l++) 1002185029Spjd vdev_label_write(zio, vd, l, ubbuf, 1003185029Spjd VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd), 1004185029Spjd vdev_uberblock_sync_done, zio->io_private, 1005185029Spjd flags | ZIO_FLAG_DONT_PROPAGATE); 1006168404Spjd 1007185029Spjd zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd)); 1008168404Spjd} 1009168404Spjd 1010185029Spjdint 1011185029Spjdvdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags) 1012168404Spjd{ 1013185029Spjd spa_t *spa = svd[0]->vdev_spa; 1014168404Spjd zio_t *zio; 1015185029Spjd uint64_t good_writes = 0; 1016168404Spjd 1017185029Spjd zio = zio_root(spa, NULL, &good_writes, flags); 1018168404Spjd 1019185029Spjd for (int v = 0; v < svdcount; v++) 1020185029Spjd vdev_uberblock_sync(zio, ub, svd[v], flags); 1021168404Spjd 1022185029Spjd (void) zio_wait(zio); 1023168404Spjd 1024168404Spjd /* 1025185029Spjd * Flush the uberblocks to disk. This ensures that the odd labels 1026185029Spjd * are no longer needed (because the new uberblocks and the even 1027185029Spjd * labels are safely on disk), so it is safe to overwrite them. 1028168404Spjd */ 1029185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1030168404Spjd 1031185029Spjd for (int v = 0; v < svdcount; v++) 1032185029Spjd zio_flush(zio, svd[v]); 1033168404Spjd 1034185029Spjd (void) zio_wait(zio); 1035185029Spjd 1036185029Spjd return (good_writes >= 1 ? 0 : EIO); 1037168404Spjd} 1038168404Spjd 1039168404Spjd/* 1040185029Spjd * On success, increment the count of good writes for our top-level vdev. 1041168404Spjd */ 1042168404Spjdstatic void 1043185029Spjdvdev_label_sync_done(zio_t *zio) 1044168404Spjd{ 1045185029Spjd uint64_t *good_writes = zio->io_private; 1046168404Spjd 1047168404Spjd if (zio->io_error == 0) 1048168404Spjd atomic_add_64(good_writes, 1); 1049168404Spjd} 1050168404Spjd 1051185029Spjd/* 1052185029Spjd * If there weren't enough good writes, indicate failure to the parent. 1053185029Spjd */ 1054168404Spjdstatic void 1055185029Spjdvdev_label_sync_top_done(zio_t *zio) 1056168404Spjd{ 1057185029Spjd uint64_t *good_writes = zio->io_private; 1058185029Spjd 1059185029Spjd if (*good_writes == 0) 1060185029Spjd zio->io_error = EIO; 1061185029Spjd 1062185029Spjd kmem_free(good_writes, sizeof (uint64_t)); 1063185029Spjd} 1064185029Spjd 1065185029Spjd/* 1066185029Spjd * We ignore errors for log and cache devices, simply free the private data. 1067185029Spjd */ 1068185029Spjdstatic void 1069185029Spjdvdev_label_sync_ignore_done(zio_t *zio) 1070185029Spjd{ 1071185029Spjd kmem_free(zio->io_private, sizeof (uint64_t)); 1072185029Spjd} 1073185029Spjd 1074185029Spjd/* 1075185029Spjd * Write all even or odd labels to all leaves of the specified vdev. 1076185029Spjd */ 1077185029Spjdstatic void 1078185029Spjdvdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags) 1079185029Spjd{ 1080168404Spjd nvlist_t *label; 1081168404Spjd vdev_phys_t *vp; 1082168404Spjd char *buf; 1083168404Spjd size_t buflen; 1084168404Spjd 1085185029Spjd for (int c = 0; c < vd->vdev_children; c++) 1086185029Spjd vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags); 1087168404Spjd 1088168404Spjd if (!vd->vdev_ops->vdev_op_leaf) 1089168404Spjd return; 1090168404Spjd 1091185029Spjd if (!vdev_writeable(vd)) 1092168404Spjd return; 1093168404Spjd 1094168404Spjd /* 1095168404Spjd * Generate a label describing the top-level config to which we belong. 1096168404Spjd */ 1097168404Spjd label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE); 1098168404Spjd 1099168404Spjd vp = zio_buf_alloc(sizeof (vdev_phys_t)); 1100168404Spjd bzero(vp, sizeof (vdev_phys_t)); 1101168404Spjd 1102168404Spjd buf = vp->vp_nvlist; 1103168404Spjd buflen = sizeof (vp->vp_nvlist); 1104168404Spjd 1105185029Spjd if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) { 1106185029Spjd for (; l < VDEV_LABELS; l += 2) { 1107185029Spjd vdev_label_write(zio, vd, l, vp, 1108185029Spjd offsetof(vdev_label_t, vl_vdev_phys), 1109185029Spjd sizeof (vdev_phys_t), 1110185029Spjd vdev_label_sync_done, zio->io_private, 1111185029Spjd flags | ZIO_FLAG_DONT_PROPAGATE); 1112185029Spjd } 1113185029Spjd } 1114168404Spjd 1115168404Spjd zio_buf_free(vp, sizeof (vdev_phys_t)); 1116168404Spjd nvlist_free(label); 1117168404Spjd} 1118168404Spjd 1119185029Spjdint 1120185029Spjdvdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags) 1121168404Spjd{ 1122185029Spjd list_t *dl = &spa->spa_config_dirty_list; 1123185029Spjd vdev_t *vd; 1124168404Spjd zio_t *zio; 1125168404Spjd int error; 1126168404Spjd 1127185029Spjd /* 1128185029Spjd * Write the new labels to disk. 1129185029Spjd */ 1130185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1131168404Spjd 1132185029Spjd for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) { 1133185029Spjd uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t), 1134185029Spjd KM_SLEEP); 1135219089Spjd 1136219089Spjd ASSERT(!vd->vdev_ishole); 1137219089Spjd 1138209962Smm zio_t *vio = zio_null(zio, spa, NULL, 1139185029Spjd (vd->vdev_islog || vd->vdev_aux != NULL) ? 1140185029Spjd vdev_label_sync_ignore_done : vdev_label_sync_top_done, 1141185029Spjd good_writes, flags); 1142185029Spjd vdev_label_sync(vio, vd, l, txg, flags); 1143185029Spjd zio_nowait(vio); 1144185029Spjd } 1145168404Spjd 1146185029Spjd error = zio_wait(zio); 1147168404Spjd 1148168404Spjd /* 1149185029Spjd * Flush the new labels to disk. 1150168404Spjd */ 1151185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1152168404Spjd 1153185029Spjd for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) 1154185029Spjd zio_flush(zio, vd); 1155168404Spjd 1156185029Spjd (void) zio_wait(zio); 1157168404Spjd 1158168404Spjd return (error); 1159168404Spjd} 1160168404Spjd 1161168404Spjd/* 1162185029Spjd * Sync the uberblock and any changes to the vdev configuration. 1163168404Spjd * 1164168404Spjd * The order of operations is carefully crafted to ensure that 1165168404Spjd * if the system panics or loses power at any time, the state on disk 1166168404Spjd * is still transactionally consistent. The in-line comments below 1167168404Spjd * describe the failure semantics at each stage. 1168168404Spjd * 1169185029Spjd * Moreover, vdev_config_sync() is designed to be idempotent: if it fails 1170168404Spjd * at any time, you can just call it again, and it will resume its work. 1171168404Spjd */ 1172168404Spjdint 1173213198Smmvdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard) 1174168404Spjd{ 1175185029Spjd spa_t *spa = svd[0]->vdev_spa; 1176168404Spjd uberblock_t *ub = &spa->spa_uberblock; 1177168404Spjd vdev_t *vd; 1178168404Spjd zio_t *zio; 1179185029Spjd int error; 1180185029Spjd int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 1181168404Spjd 1182213198Smm /* 1183213198Smm * Normally, we don't want to try too hard to write every label and 1184213198Smm * uberblock. If there is a flaky disk, we don't want the rest of the 1185213198Smm * sync process to block while we retry. But if we can't write a 1186213198Smm * single label out, we should retry with ZIO_FLAG_TRYHARD before 1187213198Smm * bailing out and declaring the pool faulted. 1188213198Smm */ 1189213198Smm if (tryhard) 1190213198Smm flags |= ZIO_FLAG_TRYHARD; 1191213198Smm 1192168404Spjd ASSERT(ub->ub_txg <= txg); 1193168404Spjd 1194168404Spjd /* 1195185029Spjd * If this isn't a resync due to I/O errors, 1196185029Spjd * and nothing changed in this transaction group, 1197185029Spjd * and the vdev configuration hasn't changed, 1198168404Spjd * then there's nothing to do. 1199168404Spjd */ 1200185029Spjd if (ub->ub_txg < txg && 1201185029Spjd uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE && 1202185029Spjd list_is_empty(&spa->spa_config_dirty_list)) 1203168404Spjd return (0); 1204168404Spjd 1205168404Spjd if (txg > spa_freeze_txg(spa)) 1206168404Spjd return (0); 1207168404Spjd 1208168404Spjd ASSERT(txg <= spa->spa_final_txg); 1209168404Spjd 1210168404Spjd /* 1211168404Spjd * Flush the write cache of every disk that's been written to 1212168404Spjd * in this transaction group. This ensures that all blocks 1213168404Spjd * written in this txg will be committed to stable storage 1214168404Spjd * before any uberblock that references them. 1215168404Spjd */ 1216185029Spjd zio = zio_root(spa, NULL, NULL, flags); 1217185029Spjd 1218168404Spjd for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd; 1219185029Spjd vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg))) 1220185029Spjd zio_flush(zio, vd); 1221185029Spjd 1222168404Spjd (void) zio_wait(zio); 1223168404Spjd 1224168404Spjd /* 1225168404Spjd * Sync out the even labels (L0, L2) for every dirty vdev. If the 1226168404Spjd * system dies in the middle of this process, that's OK: all of the 1227168404Spjd * even labels that made it to disk will be newer than any uberblock, 1228168404Spjd * and will therefore be considered invalid. The odd labels (L1, L3), 1229185029Spjd * which have not yet been touched, will still be valid. We flush 1230185029Spjd * the new labels to disk to ensure that all even-label updates 1231185029Spjd * are committed to stable storage before the uberblock update. 1232168404Spjd */ 1233185029Spjd if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) 1234185029Spjd return (error); 1235168404Spjd 1236168404Spjd /* 1237185029Spjd * Sync the uberblocks to all vdevs in svd[]. 1238168404Spjd * If the system dies in the middle of this step, there are two cases 1239168404Spjd * to consider, and the on-disk state is consistent either way: 1240168404Spjd * 1241168404Spjd * (1) If none of the new uberblocks made it to disk, then the 1242168404Spjd * previous uberblock will be the newest, and the odd labels 1243168404Spjd * (which had not yet been touched) will be valid with respect 1244168404Spjd * to that uberblock. 1245168404Spjd * 1246168404Spjd * (2) If one or more new uberblocks made it to disk, then they 1247168404Spjd * will be the newest, and the even labels (which had all 1248168404Spjd * been successfully committed) will be valid with respect 1249168404Spjd * to the new uberblocks. 1250168404Spjd */ 1251185029Spjd if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) 1252168404Spjd return (error); 1253168404Spjd 1254168404Spjd /* 1255168404Spjd * Sync out odd labels for every dirty vdev. If the system dies 1256168404Spjd * in the middle of this process, the even labels and the new 1257168404Spjd * uberblocks will suffice to open the pool. The next time 1258168404Spjd * the pool is opened, the first thing we'll do -- before any 1259168404Spjd * user data is modified -- is mark every vdev dirty so that 1260185029Spjd * all labels will be brought up to date. We flush the new labels 1261185029Spjd * to disk to ensure that all odd-label updates are committed to 1262185029Spjd * stable storage before the next transaction group begins. 1263168404Spjd */ 1264185029Spjd return (vdev_label_sync_list(spa, 1, txg, flags)); 1265168404Spjd} 1266