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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/zfs_context.h> 30 #include <sys/spa.h> 31 #include <sys/vdev_impl.h> 32 #include <sys/zio.h> 33 #include <sys/zio_checksum.h> 34 #include <sys/fs/zfs.h> 35 36 /* 37 * Virtual device vector for RAID-Z. 38 */ 39 40 /* 41 * We currently allow up to two-way replication (i.e. single-fault 42 * reconstruction) models in RAID-Z vdevs. The blocks in such vdevs 43 * must all be multiples of two times the leaf vdev blocksize. 44 */ 45 #define VDEV_RAIDZ_ALIGN 2ULL 46 47 typedef struct raidz_col { 48 uint64_t rc_col; 49 uint64_t rc_offset; 50 uint64_t rc_size; 51 void *rc_data; 52 int rc_error; 53 short rc_tried; 54 short rc_skipped; 55 } raidz_col_t; 56 57 typedef struct raidz_map { 58 uint64_t rm_cols; 59 uint64_t rm_bigcols; 60 uint64_t rm_asize; 61 int rm_missing_child; 62 int rm_firstdatacol; 63 raidz_col_t rm_col[1]; 64 } raidz_map_t; 65 66 static raidz_map_t * 67 vdev_raidz_map_alloc(zio_t *zio, uint64_t unit_shift, uint64_t dcols) 68 { 69 raidz_map_t *rm; 70 uint64_t b = zio->io_offset >> unit_shift; 71 uint64_t s = zio->io_size >> unit_shift; 72 uint64_t f = b % dcols; 73 uint64_t o = (b / dcols) << unit_shift; 74 uint64_t q, r, c, bc, col, acols, coff; 75 int firstdatacol; 76 77 q = s / (dcols - 1); 78 r = s - q * (dcols - 1); 79 bc = r + !!r; 80 firstdatacol = 1; 81 82 acols = (q == 0 ? bc : dcols); 83 84 rm = kmem_alloc(offsetof(raidz_map_t, rm_col[acols]), KM_SLEEP); 85 86 rm->rm_cols = acols; 87 rm->rm_bigcols = bc; 88 rm->rm_asize = 0; 89 rm->rm_missing_child = -1; 90 rm->rm_firstdatacol = firstdatacol; 91 92 for (c = 0; c < acols; c++) { 93 col = f + c; 94 coff = o; 95 if (col >= dcols) { 96 col -= dcols; 97 coff += 1ULL << unit_shift; 98 } 99 rm->rm_col[c].rc_col = col; 100 rm->rm_col[c].rc_offset = coff; 101 rm->rm_col[c].rc_size = (q + (c < bc)) << unit_shift; 102 rm->rm_col[c].rc_data = NULL; 103 rm->rm_col[c].rc_error = 0; 104 rm->rm_col[c].rc_tried = 0; 105 rm->rm_col[c].rc_skipped = 0; 106 rm->rm_asize += rm->rm_col[c].rc_size; 107 } 108 109 rm->rm_asize = P2ROUNDUP(rm->rm_asize, VDEV_RAIDZ_ALIGN << unit_shift); 110 111 for (c = 0; c < rm->rm_firstdatacol; c++) 112 rm->rm_col[c].rc_data = zio_buf_alloc(rm->rm_col[c].rc_size); 113 114 rm->rm_col[c].rc_data = zio->io_data; 115 116 for (c = c + 1; c < acols; c++) 117 rm->rm_col[c].rc_data = (char *)rm->rm_col[c - 1].rc_data + 118 rm->rm_col[c - 1].rc_size; 119 120 /* 121 * To prevent hot parity disks, switch the parity and data 122 * columns every 1MB. 123 */ 124 ASSERT(rm->rm_cols >= 2); 125 ASSERT(rm->rm_col[0].rc_size == rm->rm_col[1].rc_size); 126 127 if (zio->io_offset & (1ULL << 20)) { 128 col = rm->rm_col[0].rc_col; 129 o = rm->rm_col[0].rc_offset; 130 rm->rm_col[0].rc_col = rm->rm_col[1].rc_col; 131 rm->rm_col[0].rc_offset = rm->rm_col[1].rc_offset; 132 rm->rm_col[1].rc_col = col; 133 rm->rm_col[1].rc_offset = o; 134 } 135 136 zio->io_vsd = rm; 137 return (rm); 138 } 139 140 static void 141 vdev_raidz_map_free(zio_t *zio) 142 { 143 raidz_map_t *rm = zio->io_vsd; 144 int c; 145 146 for (c = 0; c < rm->rm_firstdatacol; c++) 147 zio_buf_free(rm->rm_col[c].rc_data, rm->rm_col[c].rc_size); 148 149 kmem_free(rm, offsetof(raidz_map_t, rm_col[rm->rm_cols])); 150 zio->io_vsd = NULL; 151 } 152 153 static void 154 vdev_raidz_reconstruct(raidz_map_t *rm, int x) 155 { 156 uint64_t *dst, *src, count, xsize, csize; 157 int i, c; 158 159 for (c = 0; c < rm->rm_cols; c++) { 160 if (c == x) 161 continue; 162 src = rm->rm_col[c].rc_data; 163 dst = rm->rm_col[x].rc_data; 164 csize = rm->rm_col[c].rc_size; 165 xsize = rm->rm_col[x].rc_size; 166 count = MIN(csize, xsize) / sizeof (uint64_t); 167 if (c == !x) { 168 /* 169 * The initial copy happens at either c == 0 or c == 1. 170 * Both of these columns are 'big' columns, so we'll 171 * definitely initialize all of column x. 172 */ 173 ASSERT3U(xsize, <=, csize); 174 for (i = 0; i < count; i++) 175 *dst++ = *src++; 176 } else { 177 for (i = 0; i < count; i++) 178 *dst++ ^= *src++; 179 } 180 } 181 } 182 183 static int 184 vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift) 185 { 186 vdev_t *cvd; 187 int c, error; 188 int lasterror = 0; 189 int numerrors = 0; 190 191 /* 192 * XXX -- minimum children should be raid-type-specific 193 */ 194 if (vd->vdev_children < 2) { 195 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 196 return (EINVAL); 197 } 198 199 for (c = 0; c < vd->vdev_children; c++) { 200 cvd = vd->vdev_child[c]; 201 202 if ((error = vdev_open(cvd)) != 0) { 203 lasterror = error; 204 numerrors++; 205 continue; 206 } 207 208 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1; 209 *ashift = cvd->vdev_ashift; 210 } 211 212 *asize *= vd->vdev_children; 213 214 if (numerrors > 1) { 215 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; 216 return (lasterror); 217 } 218 219 return (0); 220 } 221 222 static void 223 vdev_raidz_close(vdev_t *vd) 224 { 225 int c; 226 227 for (c = 0; c < vd->vdev_children; c++) 228 vdev_close(vd->vdev_child[c]); 229 } 230 231 static uint64_t 232 vdev_raidz_asize(vdev_t *vd, uint64_t psize) 233 { 234 uint64_t asize; 235 uint64_t cols = vd->vdev_children; 236 237 asize = psize >> vd->vdev_ashift; 238 asize += (asize + cols - 2) / (cols - 1); 239 asize = P2ROUNDUP(asize, VDEV_RAIDZ_ALIGN) << vd->vdev_ashift; 240 241 return (asize); 242 } 243 244 static void 245 vdev_raidz_child_done(zio_t *zio) 246 { 247 raidz_col_t *rc = zio->io_private; 248 249 rc->rc_error = zio->io_error; 250 rc->rc_tried = 1; 251 rc->rc_skipped = 0; 252 } 253 254 static void 255 vdev_raidz_repair_done(zio_t *zio) 256 { 257 zio_buf_free(zio->io_data, zio->io_size); 258 } 259 260 static void 261 vdev_raidz_io_start(zio_t *zio) 262 { 263 vdev_t *vd = zio->io_vd; 264 vdev_t *cvd; 265 blkptr_t *bp = zio->io_bp; 266 raidz_map_t *rm; 267 raidz_col_t *rc; 268 int c; 269 270 rm = vdev_raidz_map_alloc(zio, vd->vdev_ashift, vd->vdev_children); 271 272 if (DVA_GET_GANG(ZIO_GET_DVA(zio))) { 273 ASSERT3U(rm->rm_asize, ==, 274 vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE)); 275 ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 276 } else { 277 ASSERT3U(rm->rm_asize, ==, DVA_GET_ASIZE(ZIO_GET_DVA(zio))); 278 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 279 } 280 281 if (zio->io_type == ZIO_TYPE_WRITE) { 282 283 /* 284 * Generate RAID parity in virtual column 0. 285 */ 286 vdev_raidz_reconstruct(rm, 0); 287 288 for (c = 0; c < rm->rm_cols; c++) { 289 rc = &rm->rm_col[c]; 290 cvd = vd->vdev_child[rc->rc_col]; 291 zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 292 rc->rc_offset, rc->rc_data, rc->rc_size, 293 zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL, 294 vdev_raidz_child_done, rc)); 295 } 296 zio_wait_children_done(zio); 297 return; 298 } 299 300 ASSERT(zio->io_type == ZIO_TYPE_READ); 301 302 for (c = rm->rm_cols - 1; c >= 0; c--) { 303 rc = &rm->rm_col[c]; 304 cvd = vd->vdev_child[rc->rc_col]; 305 if (vdev_is_dead(cvd)) { 306 rm->rm_missing_child = c; 307 rc->rc_error = ENXIO; 308 rc->rc_tried = 1; /* don't even try */ 309 rc->rc_skipped = 1; 310 continue; 311 } 312 if (vdev_dtl_contains(&cvd->vdev_dtl_map, bp->blk_birth, 1)) { 313 rm->rm_missing_child = c; 314 rc->rc_error = ESTALE; 315 rc->rc_skipped = 1; 316 continue; 317 } 318 if (c >= rm->rm_firstdatacol || rm->rm_missing_child != -1 || 319 (zio->io_flags & ZIO_FLAG_SCRUB)) { 320 zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 321 rc->rc_offset, rc->rc_data, rc->rc_size, 322 zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL, 323 vdev_raidz_child_done, rc)); 324 } 325 } 326 327 zio_wait_children_done(zio); 328 } 329 330 static void 331 vdev_raidz_io_done(zio_t *zio) 332 { 333 vdev_t *vd = zio->io_vd; 334 vdev_t *cvd; 335 raidz_map_t *rm = zio->io_vsd; 336 raidz_col_t *rc; 337 blkptr_t *bp = zio->io_bp; 338 int unexpected_errors = 0; 339 int c; 340 341 ASSERT(bp != NULL); /* XXX need to add code to enforce this */ 342 343 zio->io_error = 0; 344 zio->io_numerrors = 0; 345 346 for (c = 0; c < rm->rm_cols; c++) { 347 rc = &rm->rm_col[c]; 348 349 /* 350 * We preserve any EIOs because those may be worth retrying; 351 * whereas ECKSUM and ENXIO are more likely to be persistent. 352 */ 353 if (rc->rc_error) { 354 if (zio->io_error != EIO) 355 zio->io_error = rc->rc_error; 356 if (!rc->rc_skipped) 357 unexpected_errors++; 358 zio->io_numerrors++; 359 } 360 } 361 362 if (zio->io_type == ZIO_TYPE_WRITE) { 363 /* 364 * If this is not a failfast write, and we were able to 365 * write enough columns to reconstruct the data, good enough. 366 */ 367 /* XXPOLICY */ 368 if (zio->io_numerrors <= rm->rm_firstdatacol && 369 !(zio->io_flags & ZIO_FLAG_FAILFAST)) 370 zio->io_error = 0; 371 372 vdev_raidz_map_free(zio); 373 zio_next_stage(zio); 374 return; 375 } 376 377 ASSERT(zio->io_type == ZIO_TYPE_READ); 378 379 /* 380 * If there were no I/O errors, and the data checksums correctly, 381 * the read is complete. 382 */ 383 /* XXPOLICY */ 384 if (zio->io_numerrors == 0 && zio_checksum_error(zio) == 0) { 385 ASSERT(unexpected_errors == 0); 386 ASSERT(zio->io_error == 0); 387 388 /* 389 * We know the data's good. If we read the parity, 390 * verify that it's good as well. If not, fix it. 391 */ 392 for (c = 0; c < rm->rm_firstdatacol; c++) { 393 void *orig; 394 rc = &rm->rm_col[c]; 395 if (!rc->rc_tried) 396 continue; 397 orig = zio_buf_alloc(rc->rc_size); 398 bcopy(rc->rc_data, orig, rc->rc_size); 399 vdev_raidz_reconstruct(rm, c); 400 if (bcmp(orig, rc->rc_data, rc->rc_size) != 0) { 401 vdev_checksum_error(zio, 402 vd->vdev_child[rc->rc_col]); 403 rc->rc_error = ECKSUM; 404 unexpected_errors++; 405 } 406 zio_buf_free(orig, rc->rc_size); 407 } 408 goto done; 409 } 410 411 /* 412 * If there was exactly one I/O error, it's the one we expected, 413 * and the reconstructed data checksums, the read is complete. 414 * This happens when one child is offline and vdev_fault_assess() 415 * knows it, or when one child has stale data and the DTL knows it. 416 */ 417 if (zio->io_numerrors == 1 && (c = rm->rm_missing_child) != -1) { 418 rc = &rm->rm_col[c]; 419 ASSERT(unexpected_errors == 0); 420 ASSERT(rc->rc_error == ENXIO || rc->rc_error == ESTALE); 421 vdev_raidz_reconstruct(rm, c); 422 if (zio_checksum_error(zio) == 0) { 423 zio->io_error = 0; 424 goto done; 425 } 426 } 427 428 /* 429 * This isn't a typical error -- either we got a read error or 430 * more than one child claimed a problem. Read every block we 431 * haven't already so we can try combinatorial reconstruction. 432 */ 433 unexpected_errors = 1; 434 rm->rm_missing_child = -1; 435 436 for (c = 0; c < rm->rm_cols; c++) 437 if (!rm->rm_col[c].rc_tried) 438 break; 439 440 if (c != rm->rm_cols) { 441 zio->io_error = 0; 442 zio_vdev_io_redone(zio); 443 for (c = 0; c < rm->rm_cols; c++) { 444 rc = &rm->rm_col[c]; 445 if (rc->rc_tried) 446 continue; 447 zio_nowait(zio_vdev_child_io(zio, NULL, 448 vd->vdev_child[rc->rc_col], 449 rc->rc_offset, rc->rc_data, rc->rc_size, 450 zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL, 451 vdev_raidz_child_done, rc)); 452 } 453 zio_wait_children_done(zio); 454 return; 455 } 456 457 /* 458 * If there were more errors than parity disks, give up. 459 */ 460 if (zio->io_numerrors > rm->rm_firstdatacol) { 461 ASSERT(zio->io_error != 0); 462 goto done; 463 } 464 465 /* 466 * The number of I/O errors is correctable. Correct them here. 467 */ 468 ASSERT(zio->io_numerrors <= rm->rm_firstdatacol); 469 for (c = 0; c < rm->rm_cols; c++) { 470 rc = &rm->rm_col[c]; 471 ASSERT(rc->rc_tried); 472 if (rc->rc_error) { 473 vdev_raidz_reconstruct(rm, c); 474 if (zio_checksum_error(zio) == 0) 475 zio->io_error = 0; 476 else 477 zio->io_error = rc->rc_error; 478 goto done; 479 } 480 } 481 482 /* 483 * There were no I/O errors, but the data doesn't checksum. 484 * Try all permutations to see if we can find one that does. 485 */ 486 ASSERT(zio->io_numerrors == 0); 487 for (c = 0; c < rm->rm_cols; c++) { 488 void *orig; 489 rc = &rm->rm_col[c]; 490 491 orig = zio_buf_alloc(rc->rc_size); 492 bcopy(rc->rc_data, orig, rc->rc_size); 493 vdev_raidz_reconstruct(rm, c); 494 495 if (zio_checksum_error(zio) == 0) { 496 zio_buf_free(orig, rc->rc_size); 497 zio->io_error = 0; 498 /* 499 * If this child didn't know that it returned bad data, 500 * inform it. 501 */ 502 if (rc->rc_tried && rc->rc_error == 0) 503 vdev_checksum_error(zio, 504 vd->vdev_child[rc->rc_col]); 505 rc->rc_error = ECKSUM; 506 goto done; 507 } 508 509 bcopy(orig, rc->rc_data, rc->rc_size); 510 zio_buf_free(orig, rc->rc_size); 511 } 512 513 /* 514 * All combinations failed to checksum. 515 */ 516 zio->io_error = ECKSUM; 517 518 done: 519 zio_checksum_verified(zio); 520 521 if (zio->io_error == 0 && (spa_mode & FWRITE) && 522 (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) { 523 /* 524 * Use the good data we have in hand to repair damaged children. 525 */ 526 for (c = 0; c < rm->rm_cols; c++) { 527 rc = &rm->rm_col[c]; 528 cvd = vd->vdev_child[rc->rc_col]; 529 530 if (rc->rc_error) { 531 /* 532 * Make a copy of the data because we're 533 * going to free the RAID-Z map below. 534 */ 535 void *data = zio_buf_alloc(rc->rc_size); 536 bcopy(rc->rc_data, data, rc->rc_size); 537 538 dprintf("%s resilvered %s @ 0x%llx error %d\n", 539 vdev_description(vd), 540 vdev_description(cvd), 541 zio->io_offset, rc->rc_error); 542 543 zio_nowait(zio_vdev_child_io(zio, NULL, cvd, 544 rc->rc_offset, data, rc->rc_size, 545 ZIO_TYPE_WRITE, zio->io_priority, 546 ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL | 547 ZIO_FLAG_DONT_PROPAGATE, 548 vdev_raidz_repair_done, NULL)); 549 } 550 } 551 } 552 553 vdev_raidz_map_free(zio); 554 zio_next_stage(zio); 555 } 556 557 static void 558 vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded) 559 { 560 if (faulted > 1) 561 vdev_set_state(vd, VDEV_STATE_CANT_OPEN, VDEV_AUX_NO_REPLICAS); 562 else if (degraded + faulted != 0) 563 vdev_set_state(vd, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 564 else 565 vdev_set_state(vd, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 566 } 567 568 vdev_ops_t vdev_raidz_ops = { 569 vdev_raidz_open, 570 vdev_raidz_close, 571 vdev_raidz_asize, 572 vdev_raidz_io_start, 573 vdev_raidz_io_done, 574 vdev_raidz_state_change, 575 VDEV_TYPE_RAIDZ, /* name of this vdev type */ 576 B_FALSE /* not a leaf vdev */ 577 }; 578