vdev_mirror.c revision 274304
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 */ 21168404Spjd/* 22219089Spjd * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23168404Spjd * Use is subject to license terms. 24168404Spjd */ 25168404Spjd 26236155Smm/* 27274304Sdelphij * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 28236155Smm */ 29236155Smm 30168404Spjd#include <sys/zfs_context.h> 31168404Spjd#include <sys/spa.h> 32168404Spjd#include <sys/vdev_impl.h> 33168404Spjd#include <sys/zio.h> 34168404Spjd#include <sys/fs/zfs.h> 35168404Spjd 36168404Spjd/* 37168404Spjd * Virtual device vector for mirroring. 38168404Spjd */ 39168404Spjd 40168404Spjdtypedef struct mirror_child { 41168404Spjd vdev_t *mc_vd; 42168404Spjd uint64_t mc_offset; 43168404Spjd int mc_error; 44256956Ssmh int mc_load; 45185029Spjd uint8_t mc_tried; 46185029Spjd uint8_t mc_skipped; 47185029Spjd uint8_t mc_speculative; 48168404Spjd} mirror_child_t; 49168404Spjd 50168404Spjdtypedef struct mirror_map { 51256956Ssmh int *mm_preferred; 52256956Ssmh int mm_preferred_cnt; 53168404Spjd int mm_children; 54256956Ssmh boolean_t mm_replacing; 55256956Ssmh boolean_t mm_root; 56256956Ssmh mirror_child_t mm_child[]; 57168404Spjd} mirror_map_t; 58168404Spjd 59256956Ssmhstatic int vdev_mirror_shift = 21; 60168404Spjd 61256956SsmhSYSCTL_DECL(_vfs_zfs_vdev); 62256956Ssmhstatic SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, mirror, CTLFLAG_RD, 0, 63256956Ssmh "ZFS VDEV Mirror"); 64256956Ssmh 65256956Ssmh/* 66256956Ssmh * The load configuration settings below are tuned by default for 67256956Ssmh * the case where all devices are of the same rotational type. 68256956Ssmh * 69256956Ssmh * If there is a mixture of rotating and non-rotating media, setting 70256956Ssmh * non_rotating_seek_inc to 0 may well provide better results as it 71256956Ssmh * will direct more reads to the non-rotating vdevs which are more 72256956Ssmh * likely to have a higher performance. 73256956Ssmh */ 74256956Ssmh 75256956Ssmh/* Rotating media load calculation configuration. */ 76256956Ssmhstatic int rotating_inc = 0; 77267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RWTUN, 78256956Ssmh &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's"); 79256956Ssmh 80256956Ssmhstatic int rotating_seek_inc = 5; 81267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RWTUN, 82256956Ssmh &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's"); 83256956Ssmh 84256956Ssmhstatic int rotating_seek_offset = 1 * 1024 * 1024; 85267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RWTUN, 86256956Ssmh &rotating_seek_offset, 0, "Offset in bytes from the last I/O which " 87256956Ssmh "triggers a reduced rotating media seek increment"); 88256956Ssmh 89256956Ssmh/* Non-rotating media load calculation configuration. */ 90256956Ssmhstatic int non_rotating_inc = 0; 91267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RWTUN, 92256956Ssmh &non_rotating_inc, 0, 93256956Ssmh "Non-rotating media load increment for non-seeking I/O's"); 94256956Ssmh 95256956Ssmhstatic int non_rotating_seek_inc = 1; 96267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RWTUN, 97256956Ssmh &non_rotating_seek_inc, 0, 98256956Ssmh "Non-rotating media load increment for seeking I/O's"); 99256956Ssmh 100256956Ssmh 101256956Ssmhstatic inline size_t 102256956Ssmhvdev_mirror_map_size(int children) 103256956Ssmh{ 104256956Ssmh return (offsetof(mirror_map_t, mm_child[children]) + 105256956Ssmh sizeof(int) * children); 106256956Ssmh} 107256956Ssmh 108256956Ssmhstatic inline mirror_map_t * 109256956Ssmhvdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root) 110256956Ssmh{ 111256956Ssmh mirror_map_t *mm; 112256956Ssmh 113256956Ssmh mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP); 114256956Ssmh mm->mm_children = children; 115256956Ssmh mm->mm_replacing = replacing; 116256956Ssmh mm->mm_root = root; 117256956Ssmh mm->mm_preferred = (int *)((uintptr_t)mm + 118256956Ssmh offsetof(mirror_map_t, mm_child[children])); 119256956Ssmh 120256956Ssmh return mm; 121256956Ssmh} 122256956Ssmh 123185029Spjdstatic void 124185029Spjdvdev_mirror_map_free(zio_t *zio) 125185029Spjd{ 126185029Spjd mirror_map_t *mm = zio->io_vsd; 127185029Spjd 128256956Ssmh kmem_free(mm, vdev_mirror_map_size(mm->mm_children)); 129185029Spjd} 130185029Spjd 131219089Spjdstatic const zio_vsd_ops_t vdev_mirror_vsd_ops = { 132219089Spjd vdev_mirror_map_free, 133219089Spjd zio_vsd_default_cksum_report 134219089Spjd}; 135219089Spjd 136256956Ssmhstatic int 137256956Ssmhvdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset) 138256956Ssmh{ 139256956Ssmh uint64_t lastoffset; 140256956Ssmh int load; 141256956Ssmh 142256956Ssmh /* All DVAs have equal weight at the root. */ 143256956Ssmh if (mm->mm_root) 144256956Ssmh return (INT_MAX); 145256956Ssmh 146256956Ssmh /* 147256956Ssmh * We don't return INT_MAX if the device is resilvering i.e. 148256956Ssmh * vdev_resilver_txg != 0 as when tested performance was slightly 149256956Ssmh * worse overall when resilvering with compared to without. 150256956Ssmh */ 151256956Ssmh 152256956Ssmh /* Standard load based on pending queue length. */ 153256956Ssmh load = vdev_queue_length(vd); 154256956Ssmh lastoffset = vdev_queue_lastoffset(vd); 155256956Ssmh 156256956Ssmh if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) { 157256956Ssmh /* Non-rotating media. */ 158256956Ssmh if (lastoffset == zio_offset) 159256956Ssmh return (load + non_rotating_inc); 160256956Ssmh 161256956Ssmh /* 162256956Ssmh * Apply a seek penalty even for non-rotating devices as 163256956Ssmh * sequential I/O'a can be aggregated into fewer operations 164256956Ssmh * on the device, thus avoiding unnecessary per-command 165256956Ssmh * overhead and boosting performance. 166256956Ssmh */ 167256956Ssmh return (load + non_rotating_seek_inc); 168256956Ssmh } 169256956Ssmh 170256956Ssmh /* Rotating media I/O's which directly follow the last I/O. */ 171256956Ssmh if (lastoffset == zio_offset) 172256956Ssmh return (load + rotating_inc); 173256956Ssmh 174256956Ssmh /* 175256956Ssmh * Apply half the seek increment to I/O's within seek offset 176256956Ssmh * of the last I/O queued to this vdev as they should incure less 177256956Ssmh * of a seek increment. 178256956Ssmh */ 179256956Ssmh if (ABS(lastoffset - zio_offset) < rotating_seek_offset) 180256956Ssmh return (load + (rotating_seek_inc / 2)); 181256956Ssmh 182256956Ssmh /* Apply the full seek increment to all other I/O's. */ 183256956Ssmh return (load + rotating_seek_inc); 184256956Ssmh} 185256956Ssmh 186256956Ssmh 187168404Spjdstatic mirror_map_t * 188256956Ssmhvdev_mirror_map_init(zio_t *zio) 189168404Spjd{ 190168404Spjd mirror_map_t *mm = NULL; 191168404Spjd mirror_child_t *mc; 192168404Spjd vdev_t *vd = zio->io_vd; 193256956Ssmh int c; 194168404Spjd 195168404Spjd if (vd == NULL) { 196168404Spjd dva_t *dva = zio->io_bp->blk_dva; 197168404Spjd spa_t *spa = zio->io_spa; 198168404Spjd 199256956Ssmh mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE, 200256956Ssmh B_TRUE); 201168404Spjd for (c = 0; c < mm->mm_children; c++) { 202168404Spjd mc = &mm->mm_child[c]; 203168404Spjd mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c])); 204168404Spjd mc->mc_offset = DVA_GET_OFFSET(&dva[c]); 205168404Spjd } 206168404Spjd } else { 207256956Ssmh mm = vdev_mirror_map_alloc(vd->vdev_children, 208256956Ssmh (vd->vdev_ops == &vdev_replacing_ops || 209256956Ssmh vd->vdev_ops == &vdev_spare_ops), B_FALSE); 210168404Spjd for (c = 0; c < mm->mm_children; c++) { 211168404Spjd mc = &mm->mm_child[c]; 212168404Spjd mc->mc_vd = vd->vdev_child[c]; 213168404Spjd mc->mc_offset = zio->io_offset; 214168404Spjd } 215168404Spjd } 216168404Spjd 217168404Spjd zio->io_vsd = mm; 218219089Spjd zio->io_vsd_ops = &vdev_mirror_vsd_ops; 219168404Spjd return (mm); 220168404Spjd} 221168404Spjd 222168404Spjdstatic int 223236155Smmvdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize, 224254591Sgibbs uint64_t *logical_ashift, uint64_t *physical_ashift) 225168404Spjd{ 226168404Spjd int numerrors = 0; 227219089Spjd int lasterror = 0; 228168404Spjd 229168404Spjd if (vd->vdev_children == 0) { 230168404Spjd vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 231249195Smm return (SET_ERROR(EINVAL)); 232168404Spjd } 233168404Spjd 234219089Spjd vdev_open_children(vd); 235168404Spjd 236219089Spjd for (int c = 0; c < vd->vdev_children; c++) { 237219089Spjd vdev_t *cvd = vd->vdev_child[c]; 238219089Spjd 239219089Spjd if (cvd->vdev_open_error) { 240219089Spjd lasterror = cvd->vdev_open_error; 241168404Spjd numerrors++; 242168404Spjd continue; 243168404Spjd } 244168404Spjd 245168404Spjd *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1; 246236155Smm *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1; 247254591Sgibbs *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift); 248254591Sgibbs *physical_ashift = MAX(*physical_ashift, 249254591Sgibbs cvd->vdev_physical_ashift); 250168404Spjd } 251168404Spjd 252168404Spjd if (numerrors == vd->vdev_children) { 253168404Spjd vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; 254168404Spjd return (lasterror); 255168404Spjd } 256168404Spjd 257168404Spjd return (0); 258168404Spjd} 259168404Spjd 260168404Spjdstatic void 261168404Spjdvdev_mirror_close(vdev_t *vd) 262168404Spjd{ 263219089Spjd for (int c = 0; c < vd->vdev_children; c++) 264168404Spjd vdev_close(vd->vdev_child[c]); 265168404Spjd} 266168404Spjd 267168404Spjdstatic void 268168404Spjdvdev_mirror_child_done(zio_t *zio) 269168404Spjd{ 270168404Spjd mirror_child_t *mc = zio->io_private; 271168404Spjd 272168404Spjd mc->mc_error = zio->io_error; 273168404Spjd mc->mc_tried = 1; 274168404Spjd mc->mc_skipped = 0; 275168404Spjd} 276168404Spjd 277168404Spjdstatic void 278168404Spjdvdev_mirror_scrub_done(zio_t *zio) 279168404Spjd{ 280168404Spjd mirror_child_t *mc = zio->io_private; 281168404Spjd 282168404Spjd if (zio->io_error == 0) { 283209962Smm zio_t *pio; 284209962Smm 285209962Smm mutex_enter(&zio->io_lock); 286209962Smm while ((pio = zio_walk_parents(zio)) != NULL) { 287209962Smm mutex_enter(&pio->io_lock); 288209962Smm ASSERT3U(zio->io_size, >=, pio->io_size); 289209962Smm bcopy(zio->io_data, pio->io_data, pio->io_size); 290209962Smm mutex_exit(&pio->io_lock); 291209962Smm } 292209962Smm mutex_exit(&zio->io_lock); 293168404Spjd } 294168404Spjd 295168404Spjd zio_buf_free(zio->io_data, zio->io_size); 296168404Spjd 297168404Spjd mc->mc_error = zio->io_error; 298168404Spjd mc->mc_tried = 1; 299168404Spjd mc->mc_skipped = 0; 300168404Spjd} 301168404Spjd 302168404Spjd/* 303256956Ssmh * Check the other, lower-index DVAs to see if they're on the same 304256956Ssmh * vdev as the child we picked. If they are, use them since they 305256956Ssmh * are likely to have been allocated from the primary metaslab in 306256956Ssmh * use at the time, and hence are more likely to have locality with 307256956Ssmh * single-copy data. 308256956Ssmh */ 309256956Ssmhstatic int 310260713Savgvdev_mirror_dva_select(zio_t *zio, int p) 311256956Ssmh{ 312256956Ssmh dva_t *dva = zio->io_bp->blk_dva; 313256956Ssmh mirror_map_t *mm = zio->io_vsd; 314260713Savg int preferred; 315256956Ssmh int c; 316256956Ssmh 317260713Savg preferred = mm->mm_preferred[p]; 318260713Savg for (p-- ; p >= 0; p--) { 319260713Savg c = mm->mm_preferred[p]; 320256956Ssmh if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred])) 321256956Ssmh preferred = c; 322256956Ssmh } 323256956Ssmh return (preferred); 324256956Ssmh} 325256956Ssmh 326256956Ssmhstatic int 327256956Ssmhvdev_mirror_preferred_child_randomize(zio_t *zio) 328256956Ssmh{ 329256956Ssmh mirror_map_t *mm = zio->io_vsd; 330256956Ssmh int p; 331256956Ssmh 332256956Ssmh if (mm->mm_root) { 333256956Ssmh p = spa_get_random(mm->mm_preferred_cnt); 334260713Savg return (vdev_mirror_dva_select(zio, p)); 335256956Ssmh } 336256956Ssmh 337256956Ssmh /* 338256956Ssmh * To ensure we don't always favour the first matching vdev, 339256956Ssmh * which could lead to wear leveling issues on SSD's, we 340256956Ssmh * use the I/O offset as a pseudo random seed into the vdevs 341256956Ssmh * which have the lowest load. 342256956Ssmh */ 343256956Ssmh p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt; 344256956Ssmh return (mm->mm_preferred[p]); 345256956Ssmh} 346256956Ssmh 347256956Ssmh/* 348256956Ssmh * Try to find a vdev whose DTL doesn't contain the block we want to read 349256956Ssmh * prefering vdevs based on determined load. 350256956Ssmh * 351168404Spjd * If we can't, try the read on any vdev we haven't already tried. 352168404Spjd */ 353168404Spjdstatic int 354168404Spjdvdev_mirror_child_select(zio_t *zio) 355168404Spjd{ 356168404Spjd mirror_map_t *mm = zio->io_vsd; 357168404Spjd uint64_t txg = zio->io_txg; 358256956Ssmh int c, lowest_load; 359168404Spjd 360219089Spjd ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg); 361168404Spjd 362256956Ssmh lowest_load = INT_MAX; 363256956Ssmh mm->mm_preferred_cnt = 0; 364256956Ssmh for (c = 0; c < mm->mm_children; c++) { 365256956Ssmh mirror_child_t *mc; 366256956Ssmh 367168404Spjd mc = &mm->mm_child[c]; 368168404Spjd if (mc->mc_tried || mc->mc_skipped) 369168404Spjd continue; 370256956Ssmh 371185029Spjd if (!vdev_readable(mc->mc_vd)) { 372249195Smm mc->mc_error = SET_ERROR(ENXIO); 373168404Spjd mc->mc_tried = 1; /* don't even try */ 374168404Spjd mc->mc_skipped = 1; 375168404Spjd continue; 376168404Spjd } 377256956Ssmh 378256956Ssmh if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) { 379256956Ssmh mc->mc_error = SET_ERROR(ESTALE); 380256956Ssmh mc->mc_skipped = 1; 381256956Ssmh mc->mc_speculative = 1; 382256956Ssmh continue; 383256956Ssmh } 384256956Ssmh 385256956Ssmh mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset); 386256956Ssmh if (mc->mc_load > lowest_load) 387256956Ssmh continue; 388256956Ssmh 389256956Ssmh if (mc->mc_load < lowest_load) { 390256956Ssmh lowest_load = mc->mc_load; 391256956Ssmh mm->mm_preferred_cnt = 0; 392256956Ssmh } 393256956Ssmh mm->mm_preferred[mm->mm_preferred_cnt] = c; 394256956Ssmh mm->mm_preferred_cnt++; 395168404Spjd } 396168404Spjd 397256956Ssmh if (mm->mm_preferred_cnt == 1) { 398256956Ssmh vdev_queue_register_lastoffset( 399256956Ssmh mm->mm_child[mm->mm_preferred[0]].mc_vd, zio); 400256956Ssmh return (mm->mm_preferred[0]); 401256956Ssmh } 402256956Ssmh 403256956Ssmh if (mm->mm_preferred_cnt > 1) { 404256956Ssmh int c = vdev_mirror_preferred_child_randomize(zio); 405256956Ssmh 406256956Ssmh vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio); 407256956Ssmh return (c); 408256956Ssmh } 409256956Ssmh 410168404Spjd /* 411168404Spjd * Every device is either missing or has this txg in its DTL. 412168404Spjd * Look for any child we haven't already tried before giving up. 413168404Spjd */ 414256956Ssmh for (c = 0; c < mm->mm_children; c++) { 415256956Ssmh if (!mm->mm_child[c].mc_tried) { 416256956Ssmh vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, 417256956Ssmh zio); 418168404Spjd return (c); 419256956Ssmh } 420256956Ssmh } 421168404Spjd 422168404Spjd /* 423168404Spjd * Every child failed. There's no place left to look. 424168404Spjd */ 425168404Spjd return (-1); 426168404Spjd} 427168404Spjd 428274304Sdelphijstatic void 429168404Spjdvdev_mirror_io_start(zio_t *zio) 430168404Spjd{ 431168404Spjd mirror_map_t *mm; 432168404Spjd mirror_child_t *mc; 433168404Spjd int c, children; 434168404Spjd 435256956Ssmh mm = vdev_mirror_map_init(zio); 436168404Spjd 437168404Spjd if (zio->io_type == ZIO_TYPE_READ) { 438168404Spjd if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) { 439168404Spjd /* 440168404Spjd * For scrubbing reads we need to allocate a read 441168404Spjd * buffer for each child and issue reads to all 442168404Spjd * children. If any child succeeds, it will copy its 443168404Spjd * data into zio->io_data in vdev_mirror_scrub_done. 444168404Spjd */ 445168404Spjd for (c = 0; c < mm->mm_children; c++) { 446168404Spjd mc = &mm->mm_child[c]; 447168404Spjd zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 448168404Spjd mc->mc_vd, mc->mc_offset, 449168404Spjd zio_buf_alloc(zio->io_size), zio->io_size, 450185029Spjd zio->io_type, zio->io_priority, 0, 451168404Spjd vdev_mirror_scrub_done, mc)); 452168404Spjd } 453274304Sdelphij zio_execute(zio); 454274304Sdelphij return; 455168404Spjd } 456168404Spjd /* 457168404Spjd * For normal reads just pick one child. 458168404Spjd */ 459168404Spjd c = vdev_mirror_child_select(zio); 460168404Spjd children = (c >= 0); 461168404Spjd } else { 462240868Spjd ASSERT(zio->io_type == ZIO_TYPE_WRITE || 463240868Spjd zio->io_type == ZIO_TYPE_FREE); 464168404Spjd 465168404Spjd /* 466240868Spjd * Writes and frees go to all children. 467168404Spjd */ 468209962Smm c = 0; 469209962Smm children = mm->mm_children; 470168404Spjd } 471168404Spjd 472168404Spjd while (children--) { 473168404Spjd mc = &mm->mm_child[c]; 474168404Spjd zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 475185029Spjd mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size, 476185029Spjd zio->io_type, zio->io_priority, 0, 477185029Spjd vdev_mirror_child_done, mc)); 478168404Spjd c++; 479168404Spjd } 480168404Spjd 481274304Sdelphij zio_execute(zio); 482168404Spjd} 483168404Spjd 484185029Spjdstatic int 485185029Spjdvdev_mirror_worst_error(mirror_map_t *mm) 486185029Spjd{ 487185029Spjd int error[2] = { 0, 0 }; 488185029Spjd 489185029Spjd for (int c = 0; c < mm->mm_children; c++) { 490185029Spjd mirror_child_t *mc = &mm->mm_child[c]; 491185029Spjd int s = mc->mc_speculative; 492185029Spjd error[s] = zio_worst_error(error[s], mc->mc_error); 493185029Spjd } 494185029Spjd 495185029Spjd return (error[0] ? error[0] : error[1]); 496185029Spjd} 497185029Spjd 498168404Spjdstatic void 499168404Spjdvdev_mirror_io_done(zio_t *zio) 500168404Spjd{ 501168404Spjd mirror_map_t *mm = zio->io_vsd; 502168404Spjd mirror_child_t *mc; 503168404Spjd int c; 504168404Spjd int good_copies = 0; 505168404Spjd int unexpected_errors = 0; 506168404Spjd 507168404Spjd for (c = 0; c < mm->mm_children; c++) { 508168404Spjd mc = &mm->mm_child[c]; 509168404Spjd 510168404Spjd if (mc->mc_error) { 511168404Spjd if (!mc->mc_skipped) 512168404Spjd unexpected_errors++; 513185029Spjd } else if (mc->mc_tried) { 514185029Spjd good_copies++; 515168404Spjd } 516168404Spjd } 517168404Spjd 518168404Spjd if (zio->io_type == ZIO_TYPE_WRITE) { 519168404Spjd /* 520168404Spjd * XXX -- for now, treat partial writes as success. 521185029Spjd * 522185029Spjd * Now that we support write reallocation, it would be better 523185029Spjd * to treat partial failure as real failure unless there are 524185029Spjd * no non-degraded top-level vdevs left, and not update DTLs 525185029Spjd * if we intend to reallocate. 526168404Spjd */ 527168404Spjd /* XXPOLICY */ 528185029Spjd if (good_copies != mm->mm_children) { 529185029Spjd /* 530185029Spjd * Always require at least one good copy. 531185029Spjd * 532185029Spjd * For ditto blocks (io_vd == NULL), require 533185029Spjd * all copies to be good. 534185029Spjd * 535185029Spjd * XXX -- for replacing vdevs, there's no great answer. 536185029Spjd * If the old device is really dead, we may not even 537185029Spjd * be able to access it -- so we only want to 538185029Spjd * require good writes to the new device. But if 539185029Spjd * the new device turns out to be flaky, we want 540185029Spjd * to be able to detach it -- which requires all 541185029Spjd * writes to the old device to have succeeded. 542185029Spjd */ 543185029Spjd if (good_copies == 0 || zio->io_vd == NULL) 544185029Spjd zio->io_error = vdev_mirror_worst_error(mm); 545185029Spjd } 546168404Spjd return; 547240868Spjd } else if (zio->io_type == ZIO_TYPE_FREE) { 548240868Spjd return; 549168404Spjd } 550168404Spjd 551168404Spjd ASSERT(zio->io_type == ZIO_TYPE_READ); 552168404Spjd 553168404Spjd /* 554168404Spjd * If we don't have a good copy yet, keep trying other children. 555168404Spjd */ 556168404Spjd /* XXPOLICY */ 557168404Spjd if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) { 558168404Spjd ASSERT(c >= 0 && c < mm->mm_children); 559168404Spjd mc = &mm->mm_child[c]; 560168404Spjd zio_vdev_io_redone(zio); 561168404Spjd zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 562168404Spjd mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size, 563185029Spjd ZIO_TYPE_READ, zio->io_priority, 0, 564168404Spjd vdev_mirror_child_done, mc)); 565168404Spjd return; 566168404Spjd } 567168404Spjd 568168404Spjd /* XXPOLICY */ 569185029Spjd if (good_copies == 0) { 570185029Spjd zio->io_error = vdev_mirror_worst_error(mm); 571168404Spjd ASSERT(zio->io_error != 0); 572185029Spjd } 573168404Spjd 574209962Smm if (good_copies && spa_writeable(zio->io_spa) && 575168404Spjd (unexpected_errors || 576168404Spjd (zio->io_flags & ZIO_FLAG_RESILVER) || 577168404Spjd ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) { 578168404Spjd /* 579168404Spjd * Use the good data we have in hand to repair damaged children. 580168404Spjd */ 581168404Spjd for (c = 0; c < mm->mm_children; c++) { 582168404Spjd /* 583168404Spjd * Don't rewrite known good children. 584168404Spjd * Not only is it unnecessary, it could 585168404Spjd * actually be harmful: if the system lost 586168404Spjd * power while rewriting the only good copy, 587168404Spjd * there would be no good copies left! 588168404Spjd */ 589168404Spjd mc = &mm->mm_child[c]; 590168404Spjd 591168404Spjd if (mc->mc_error == 0) { 592168404Spjd if (mc->mc_tried) 593168404Spjd continue; 594168404Spjd if (!(zio->io_flags & ZIO_FLAG_SCRUB) && 595209962Smm !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL, 596168404Spjd zio->io_txg, 1)) 597168404Spjd continue; 598249195Smm mc->mc_error = SET_ERROR(ESTALE); 599168404Spjd } 600168404Spjd 601185029Spjd zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 602185029Spjd mc->mc_vd, mc->mc_offset, 603185029Spjd zio->io_data, zio->io_size, 604258632Savg ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE, 605209962Smm ZIO_FLAG_IO_REPAIR | (unexpected_errors ? 606209962Smm ZIO_FLAG_SELF_HEAL : 0), NULL, NULL)); 607168404Spjd } 608168404Spjd } 609168404Spjd} 610168404Spjd 611168404Spjdstatic void 612168404Spjdvdev_mirror_state_change(vdev_t *vd, int faulted, int degraded) 613168404Spjd{ 614168404Spjd if (faulted == vd->vdev_children) 615168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 616168404Spjd VDEV_AUX_NO_REPLICAS); 617168404Spjd else if (degraded + faulted != 0) 618168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 619168404Spjd else 620168404Spjd vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 621168404Spjd} 622168404Spjd 623168404Spjdvdev_ops_t vdev_mirror_ops = { 624168404Spjd vdev_mirror_open, 625168404Spjd vdev_mirror_close, 626168404Spjd vdev_default_asize, 627168404Spjd vdev_mirror_io_start, 628168404Spjd vdev_mirror_io_done, 629168404Spjd vdev_mirror_state_change, 630219089Spjd NULL, 631219089Spjd NULL, 632168404Spjd VDEV_TYPE_MIRROR, /* name of this vdev type */ 633168404Spjd B_FALSE /* not a leaf vdev */ 634168404Spjd}; 635168404Spjd 636168404Spjdvdev_ops_t vdev_replacing_ops = { 637168404Spjd vdev_mirror_open, 638168404Spjd vdev_mirror_close, 639168404Spjd vdev_default_asize, 640168404Spjd vdev_mirror_io_start, 641168404Spjd vdev_mirror_io_done, 642168404Spjd vdev_mirror_state_change, 643219089Spjd NULL, 644219089Spjd NULL, 645168404Spjd VDEV_TYPE_REPLACING, /* name of this vdev type */ 646168404Spjd B_FALSE /* not a leaf vdev */ 647168404Spjd}; 648168404Spjd 649168404Spjdvdev_ops_t vdev_spare_ops = { 650168404Spjd vdev_mirror_open, 651168404Spjd vdev_mirror_close, 652168404Spjd vdev_default_asize, 653168404Spjd vdev_mirror_io_start, 654168404Spjd vdev_mirror_io_done, 655168404Spjd vdev_mirror_state_change, 656219089Spjd NULL, 657219089Spjd NULL, 658168404Spjd VDEV_TYPE_SPARE, /* name of this vdev type */ 659168404Spjd B_FALSE /* not a leaf vdev */ 660168404Spjd}; 661