1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/zfs_context.h> 29 #include <sys/spa.h> 30 #include <sys/vdev_impl.h> 31 #include <sys/zio.h> 32 #include <sys/fs/zfs.h> 33 34 /* 35 * Virtual device vector for mirroring. 36 */ 37 38 typedef struct mirror_map { 39 int mm_error; 40 short mm_tried; 41 short mm_skipped; 42 } mirror_map_t; 43 44 static mirror_map_t * 45 vdev_mirror_map_alloc(zio_t *zio) 46 { 47 zio->io_vsd = kmem_zalloc(zio->io_vd->vdev_children * 48 sizeof (mirror_map_t), KM_SLEEP); 49 return (zio->io_vsd); 50 } 51 52 static void 53 vdev_mirror_map_free(zio_t *zio) 54 { 55 kmem_free(zio->io_vsd, 56 zio->io_vd->vdev_children * sizeof (mirror_map_t)); 57 zio->io_vsd = NULL; 58 } 59 60 static int 61 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift) 62 { 63 vdev_t *cvd; 64 uint64_t c; 65 int numerrors = 0; 66 int ret, lasterror = 0; 67 68 if (vd->vdev_children == 0) { 69 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 70 return (EINVAL); 71 } 72 73 for (c = 0; c < vd->vdev_children; c++) { 74 cvd = vd->vdev_child[c]; 75 76 if ((ret = vdev_open(cvd)) != 0) { 77 lasterror = ret; 78 numerrors++; 79 continue; 80 } 81 82 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1; 83 *ashift = cvd->vdev_ashift; 84 } 85 86 if (numerrors == vd->vdev_children) { 87 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; 88 return (lasterror); 89 } 90 91 return (0); 92 } 93 94 static void 95 vdev_mirror_close(vdev_t *vd) 96 { 97 uint64_t c; 98 99 for (c = 0; c < vd->vdev_children; c++) 100 vdev_close(vd->vdev_child[c]); 101 } 102 103 static void 104 vdev_mirror_child_done(zio_t *zio) 105 { 106 mirror_map_t *mm = zio->io_private; 107 108 mm->mm_error = zio->io_error; 109 mm->mm_tried = 1; 110 mm->mm_skipped = 0; 111 } 112 113 static void 114 vdev_mirror_scrub_done(zio_t *zio) 115 { 116 mirror_map_t *mm = zio->io_private; 117 118 if (zio->io_error == 0) { 119 zio_t *pio = zio->io_parent; 120 mutex_enter(&pio->io_lock); 121 bcopy(zio->io_data, pio->io_data, pio->io_size); 122 mutex_exit(&pio->io_lock); 123 } 124 125 zio_buf_free(zio->io_data, zio->io_size); 126 127 mm->mm_error = zio->io_error; 128 mm->mm_tried = 1; 129 mm->mm_skipped = 0; 130 } 131 132 /* 133 * Try to find a child whose DTL doesn't contain the block we want to read. 134 * If we can't, try the read on any vdev we haven't already tried. 135 */ 136 static int 137 vdev_mirror_child_select(zio_t *zio) 138 { 139 mirror_map_t *mm = zio->io_vsd; 140 vdev_t *vd = zio->io_vd; 141 vdev_t *cvd; 142 uint64_t txg = zio->io_txg; 143 int i, c; 144 145 ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg); 146 147 /* 148 * Select the child we'd like to read from absent any errors. 149 * The current policy is to alternate sides at 8M granularity. 150 * XXX -- investigate other policies for read distribution. 151 */ 152 c = (zio->io_offset >> (SPA_MAXBLOCKSHIFT + 6)) % vd->vdev_children; 153 154 /* 155 * If this is a replacing vdev, always try child 0 (the source) first. 156 */ 157 if (vd->vdev_ops == &vdev_replacing_ops) 158 c = 0; 159 160 /* 161 * Try to find a child whose DTL doesn't contain the block to read. 162 * If a child is known to be completely inaccessible (indicated by 163 * vdev_is_dead() returning B_TRUE), don't even try. 164 */ 165 for (i = 0; i < vd->vdev_children; i++, c++) { 166 if (c >= vd->vdev_children) 167 c = 0; 168 if (mm[c].mm_tried || mm[c].mm_skipped) 169 continue; 170 cvd = vd->vdev_child[c]; 171 if (vdev_is_dead(cvd)) { 172 mm[c].mm_error = ENXIO; 173 mm[c].mm_tried = 1; /* don't even try */ 174 mm[c].mm_skipped = 1; 175 continue; 176 } 177 if (!vdev_dtl_contains(&cvd->vdev_dtl_map, txg, 1)) 178 return (c); 179 mm[c].mm_error = ESTALE; 180 mm[c].mm_skipped = 1; 181 } 182 183 /* 184 * Every device is either missing or has this txg in its DTL. 185 * If we don't have any sibling replicas to consult, look for 186 * any child we haven't already tried before giving up. 187 */ 188 if (vd == vd->vdev_top || vd->vdev_parent->vdev_children <= 1) { 189 for (c = 0; c < vd->vdev_children; c++) { 190 if (!mm[c].mm_tried) 191 return (c); 192 } 193 } 194 195 /* 196 * Every child failed. There's no place left to look. 197 */ 198 return (-1); 199 } 200 201 static void 202 vdev_mirror_io_start(zio_t *zio) 203 { 204 vdev_t *vd = zio->io_vd; 205 mirror_map_t *mm; 206 int c, children; 207 208 mm = vdev_mirror_map_alloc(zio); 209 210 if (zio->io_type == ZIO_TYPE_READ) { 211 if ((zio->io_flags & ZIO_FLAG_SCRUB) && 212 vd->vdev_ops != &vdev_replacing_ops) { 213 /* 214 * For scrubbing reads we need to allocate a read 215 * buffer for each child and issue reads to all 216 * children. If any child succeeds, it will copy its 217 * data into zio->io_data in vdev_mirror_scrub_done. 218 */ 219 for (c = 0; c < vd->vdev_children; c++) { 220 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 221 vd->vdev_child[c], zio->io_offset, 222 zio_buf_alloc(zio->io_size), zio->io_size, 223 zio->io_type, zio->io_priority, 224 ZIO_FLAG_CANFAIL, vdev_mirror_scrub_done, 225 &mm[c])); 226 } 227 zio_wait_children_done(zio); 228 return; 229 } 230 /* 231 * For normal reads just pick one child. 232 */ 233 c = vdev_mirror_child_select(zio); 234 children = (c >= 0); 235 } else { 236 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 237 238 /* 239 * If this is a resilvering I/O to a replacing vdev, 240 * only the last child should be written -- unless the 241 * first child happens to have a DTL entry here as well. 242 * All other writes go to all children. 243 */ 244 if ((zio->io_flags & ZIO_FLAG_RESILVER) && 245 vd->vdev_ops == &vdev_replacing_ops && 246 !vdev_dtl_contains(&vd->vdev_child[0]->vdev_dtl_map, 247 zio->io_txg, 1)) { 248 c = vd->vdev_children - 1; 249 children = 1; 250 } else { 251 c = 0; 252 children = vd->vdev_children; 253 } 254 } 255 256 while (children--) { 257 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, 258 vd->vdev_child[c], zio->io_offset, zio->io_data, 259 zio->io_size, zio->io_type, zio->io_priority, 260 ZIO_FLAG_CANFAIL, vdev_mirror_child_done, &mm[c])); 261 c++; 262 } 263 264 zio_wait_children_done(zio); 265 } 266 267 static void 268 vdev_mirror_io_done(zio_t *zio) 269 { 270 vdev_t *vd = zio->io_vd; 271 vdev_t *cvd; 272 mirror_map_t *mm = zio->io_vsd; 273 int c; 274 int good_copies = 0; 275 int unexpected_errors = 0; 276 277 ASSERT(mm != NULL); 278 279 zio->io_error = 0; 280 zio->io_numerrors = 0; 281 282 for (c = 0; c < vd->vdev_children; c++) { 283 if (mm[c].mm_tried && mm[c].mm_error == 0) { 284 good_copies++; 285 continue; 286 } 287 288 /* 289 * We preserve any EIOs because those may be worth retrying; 290 * whereas ECKSUM and ENXIO are more likely to be persistent. 291 */ 292 if (mm[c].mm_error) { 293 if (zio->io_error != EIO) 294 zio->io_error = mm[c].mm_error; 295 if (!mm[c].mm_skipped) 296 unexpected_errors++; 297 zio->io_numerrors++; 298 } 299 } 300 301 if (zio->io_type == ZIO_TYPE_WRITE) { 302 /* 303 * XXX -- for now, treat partial writes as success. 304 */ 305 /* XXPOLICY */ 306 if (good_copies != 0) 307 zio->io_error = 0; 308 ASSERT(mm != NULL); 309 vdev_mirror_map_free(zio); 310 zio_next_stage(zio); 311 return; 312 } 313 314 ASSERT(zio->io_type == ZIO_TYPE_READ); 315 316 /* 317 * If we don't have a good copy yet, keep trying other children. 318 */ 319 /* XXPOLICY */ 320 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) { 321 ASSERT(c >= 0 && c < vd->vdev_children); 322 cvd = vd->vdev_child[c]; 323 dprintf("%s: retrying i/o (err=%d) on child %s\n", 324 vdev_description(zio->io_vd), zio->io_error, 325 vdev_description(cvd)); 326 zio->io_error = 0; 327 zio_vdev_io_redone(zio); 328 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, cvd, 329 zio->io_offset, zio->io_data, zio->io_size, 330 ZIO_TYPE_READ, zio->io_priority, ZIO_FLAG_CANFAIL, 331 vdev_mirror_child_done, &mm[c])); 332 zio_wait_children_done(zio); 333 return; 334 } 335 336 /* XXPOLICY */ 337 if (good_copies) 338 zio->io_error = 0; 339 else 340 ASSERT(zio->io_error != 0); 341 342 if (good_copies && (spa_mode & FWRITE) && 343 (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) { 344 /* 345 * Use the good data we have in hand to repair damaged children. 346 */ 347 for (c = 0; c < vd->vdev_children; c++) { 348 /* 349 * Don't rewrite known good children. 350 * Not only is it unnecessary, it could 351 * actually be harmful: if the system lost 352 * power while rewriting the only good copy, 353 * there would be no good copies left! 354 */ 355 cvd = vd->vdev_child[c]; 356 357 if (mm[c].mm_error == 0) { 358 if (mm[c].mm_tried) 359 continue; 360 if (!vdev_dtl_contains(&cvd->vdev_dtl_map, 361 zio->io_txg, 1)) 362 continue; 363 mm[c].mm_error = ESTALE; 364 } 365 366 dprintf("%s resilvered %s @ 0x%llx error %d\n", 367 vdev_description(vd), 368 vdev_description(cvd), 369 zio->io_offset, mm[c].mm_error); 370 371 zio_nowait(zio_vdev_child_io(zio, zio->io_bp, cvd, 372 zio->io_offset, zio->io_data, zio->io_size, 373 ZIO_TYPE_WRITE, zio->io_priority, 374 ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL | 375 ZIO_FLAG_DONT_PROPAGATE, NULL, NULL)); 376 } 377 } 378 379 vdev_mirror_map_free(zio); 380 zio_next_stage(zio); 381 } 382 383 static void 384 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded) 385 { 386 if (faulted == vd->vdev_children) 387 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 388 VDEV_AUX_NO_REPLICAS); 389 else if (degraded + faulted != 0) 390 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 391 else 392 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 393 } 394 395 vdev_ops_t vdev_mirror_ops = { 396 vdev_mirror_open, 397 vdev_mirror_close, 398 vdev_default_asize, 399 vdev_mirror_io_start, 400 vdev_mirror_io_done, 401 vdev_mirror_state_change, 402 VDEV_TYPE_MIRROR, /* name of this vdev type */ 403 B_FALSE /* not a leaf vdev */ 404 }; 405 406 vdev_ops_t vdev_replacing_ops = { 407 vdev_mirror_open, 408 vdev_mirror_close, 409 vdev_default_asize, 410 vdev_mirror_io_start, 411 vdev_mirror_io_done, 412 vdev_mirror_state_change, 413 VDEV_TYPE_REPLACING, /* name of this vdev type */ 414 B_FALSE /* not a leaf vdev */ 415 }; 416