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/fm/fs/zfs.h> 30 #include <sys/spa.h> 31 #include <sys/txg.h> 32 #include <sys/spa_impl.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/zio_impl.h> 35 #include <sys/zio_compress.h> 36 #include <sys/zio_checksum.h> 37 38 /* 39 * ========================================================================== 40 * I/O priority table 41 * ========================================================================== 42 */ 43 uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = { 44 0, /* ZIO_PRIORITY_NOW */ 45 0, /* ZIO_PRIORITY_SYNC_READ */ 46 0, /* ZIO_PRIORITY_SYNC_WRITE */ 47 6, /* ZIO_PRIORITY_ASYNC_READ */ 48 4, /* ZIO_PRIORITY_ASYNC_WRITE */ 49 4, /* ZIO_PRIORITY_FREE */ 50 0, /* ZIO_PRIORITY_CACHE_FILL */ 51 0, /* ZIO_PRIORITY_LOG_WRITE */ 52 10, /* ZIO_PRIORITY_RESILVER */ 53 20, /* ZIO_PRIORITY_SCRUB */ 54 }; 55 56 /* 57 * ========================================================================== 58 * I/O type descriptions 59 * ========================================================================== 60 */ 61 char *zio_type_name[ZIO_TYPES] = { 62 "null", "read", "write", "free", "claim", "ioctl" }; 63 64 /* At or above this size, force gang blocking - for testing */ 65 uint64_t zio_gang_bang = SPA_MAXBLOCKSIZE + 1; 66 67 typedef struct zio_sync_pass { 68 int zp_defer_free; /* defer frees after this pass */ 69 int zp_dontcompress; /* don't compress after this pass */ 70 int zp_rewrite; /* rewrite new bps after this pass */ 71 } zio_sync_pass_t; 72 73 zio_sync_pass_t zio_sync_pass = { 74 1, /* zp_defer_free */ 75 4, /* zp_dontcompress */ 76 1, /* zp_rewrite */ 77 }; 78 79 /* 80 * ========================================================================== 81 * I/O kmem caches 82 * ========================================================================== 83 */ 84 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 85 86 void 87 zio_init(void) 88 { 89 size_t c; 90 91 /* 92 * For small buffers, we want a cache for each multiple of 93 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache 94 * for each quarter-power of 2. For large buffers, we want 95 * a cache for each multiple of PAGESIZE. 96 */ 97 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 98 size_t size = (c + 1) << SPA_MINBLOCKSHIFT; 99 size_t p2 = size; 100 size_t align = 0; 101 102 while (p2 & (p2 - 1)) 103 p2 &= p2 - 1; 104 105 if (size <= 4 * SPA_MINBLOCKSIZE) { 106 align = SPA_MINBLOCKSIZE; 107 } else if (P2PHASE(size, PAGESIZE) == 0) { 108 align = PAGESIZE; 109 } else if (P2PHASE(size, p2 >> 2) == 0) { 110 align = p2 >> 2; 111 } 112 113 if (align != 0) { 114 char name[30]; 115 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size); 116 zio_buf_cache[c] = kmem_cache_create(name, size, 117 align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG); 118 dprintf("creating cache for size %5lx align %5lx\n", 119 size, align); 120 } 121 } 122 123 while (--c != 0) { 124 ASSERT(zio_buf_cache[c] != NULL); 125 if (zio_buf_cache[c - 1] == NULL) 126 zio_buf_cache[c - 1] = zio_buf_cache[c]; 127 } 128 129 zio_inject_init(); 130 } 131 132 void 133 zio_fini(void) 134 { 135 size_t c; 136 kmem_cache_t *last_cache = NULL; 137 138 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 139 if (zio_buf_cache[c] != last_cache) { 140 last_cache = zio_buf_cache[c]; 141 kmem_cache_destroy(zio_buf_cache[c]); 142 } 143 zio_buf_cache[c] = NULL; 144 } 145 146 zio_inject_fini(); 147 } 148 149 /* 150 * ========================================================================== 151 * Allocate and free I/O buffers 152 * ========================================================================== 153 */ 154 void * 155 zio_buf_alloc(size_t size) 156 { 157 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 158 159 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 160 161 return (kmem_cache_alloc(zio_buf_cache[c], KM_SLEEP)); 162 } 163 164 void 165 zio_buf_free(void *buf, size_t size) 166 { 167 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 168 169 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 170 171 kmem_cache_free(zio_buf_cache[c], buf); 172 } 173 174 /* 175 * ========================================================================== 176 * Push and pop I/O transform buffers 177 * ========================================================================== 178 */ 179 static void 180 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize) 181 { 182 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP); 183 184 zt->zt_data = data; 185 zt->zt_size = size; 186 zt->zt_bufsize = bufsize; 187 188 zt->zt_next = zio->io_transform_stack; 189 zio->io_transform_stack = zt; 190 191 zio->io_data = data; 192 zio->io_size = size; 193 } 194 195 static void 196 zio_pop_transform(zio_t *zio, void **data, uint64_t *size, uint64_t *bufsize) 197 { 198 zio_transform_t *zt = zio->io_transform_stack; 199 200 *data = zt->zt_data; 201 *size = zt->zt_size; 202 *bufsize = zt->zt_bufsize; 203 204 zio->io_transform_stack = zt->zt_next; 205 kmem_free(zt, sizeof (zio_transform_t)); 206 207 if ((zt = zio->io_transform_stack) != NULL) { 208 zio->io_data = zt->zt_data; 209 zio->io_size = zt->zt_size; 210 } 211 } 212 213 static void 214 zio_clear_transform_stack(zio_t *zio) 215 { 216 void *data; 217 uint64_t size, bufsize; 218 219 ASSERT(zio->io_transform_stack != NULL); 220 221 zio_pop_transform(zio, &data, &size, &bufsize); 222 while (zio->io_transform_stack != NULL) { 223 zio_buf_free(data, bufsize); 224 zio_pop_transform(zio, &data, &size, &bufsize); 225 } 226 } 227 228 /* 229 * ========================================================================== 230 * Create the various types of I/O (read, write, free) 231 * ========================================================================== 232 */ 233 static zio_t * 234 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 235 void *data, uint64_t size, zio_done_func_t *done, void *private, 236 zio_type_t type, int priority, int flags, uint8_t stage, uint32_t pipeline) 237 { 238 zio_t *zio; 239 240 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 241 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 242 243 zio = kmem_zalloc(sizeof (zio_t), KM_SLEEP); 244 zio->io_parent = pio; 245 zio->io_spa = spa; 246 zio->io_txg = txg; 247 if (bp != NULL) { 248 zio->io_bp = bp; 249 zio->io_bp_copy = *bp; 250 zio->io_bp_orig = *bp; 251 } 252 zio->io_done = done; 253 zio->io_private = private; 254 zio->io_type = type; 255 zio->io_priority = priority; 256 zio->io_stage = stage; 257 zio->io_pipeline = pipeline; 258 zio->io_async_stages = ZIO_ASYNC_PIPELINE_STAGES; 259 zio->io_timestamp = lbolt64; 260 zio->io_flags = flags; 261 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL); 262 zio_push_transform(zio, data, size, size); 263 264 if (pio == NULL) { 265 if (!(flags & ZIO_FLAG_CONFIG_HELD)) 266 spa_config_enter(zio->io_spa, RW_READER, zio); 267 zio->io_root = zio; 268 } else { 269 zio->io_root = pio->io_root; 270 if (!(flags & ZIO_FLAG_NOBOOKMARK)) 271 zio->io_logical = pio->io_logical; 272 mutex_enter(&pio->io_lock); 273 if (stage < ZIO_STAGE_READY) 274 pio->io_children_notready++; 275 pio->io_children_notdone++; 276 zio->io_sibling_next = pio->io_child; 277 zio->io_sibling_prev = NULL; 278 if (pio->io_child != NULL) 279 pio->io_child->io_sibling_prev = zio; 280 pio->io_child = zio; 281 zio->io_ndvas = pio->io_ndvas; 282 mutex_exit(&pio->io_lock); 283 } 284 285 return (zio); 286 } 287 288 zio_t * 289 zio_null(zio_t *pio, spa_t *spa, zio_done_func_t *done, void *private, 290 int flags) 291 { 292 zio_t *zio; 293 294 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 295 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, ZIO_STAGE_OPEN, 296 ZIO_WAIT_FOR_CHILDREN_PIPELINE); 297 298 return (zio); 299 } 300 301 zio_t * 302 zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags) 303 { 304 return (zio_null(NULL, spa, done, private, flags)); 305 } 306 307 zio_t * 308 zio_read(zio_t *pio, spa_t *spa, blkptr_t *bp, void *data, 309 uint64_t size, zio_done_func_t *done, void *private, 310 int priority, int flags, zbookmark_t *zb) 311 { 312 zio_t *zio; 313 314 ASSERT3U(size, ==, BP_GET_LSIZE(bp)); 315 316 zio = zio_create(pio, spa, bp->blk_birth, bp, data, size, done, private, 317 ZIO_TYPE_READ, priority, flags | ZIO_FLAG_USER, 318 ZIO_STAGE_OPEN, ZIO_READ_PIPELINE); 319 zio->io_bookmark = *zb; 320 321 zio->io_logical = zio; 322 323 /* 324 * Work off our copy of the bp so the caller can free it. 325 */ 326 zio->io_bp = &zio->io_bp_copy; 327 328 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) { 329 uint64_t csize = BP_GET_PSIZE(bp); 330 void *cbuf = zio_buf_alloc(csize); 331 332 zio_push_transform(zio, cbuf, csize, csize); 333 zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS; 334 } 335 336 if (BP_IS_GANG(bp)) { 337 uint64_t gsize = SPA_GANGBLOCKSIZE; 338 void *gbuf = zio_buf_alloc(gsize); 339 340 zio_push_transform(zio, gbuf, gsize, gsize); 341 zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS; 342 } 343 344 return (zio); 345 } 346 347 zio_t * 348 zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies, 349 uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 350 zio_done_func_t *done, void *private, int priority, int flags, 351 zbookmark_t *zb) 352 { 353 zio_t *zio; 354 355 ASSERT(checksum >= ZIO_CHECKSUM_OFF && 356 checksum < ZIO_CHECKSUM_FUNCTIONS); 357 358 ASSERT(compress >= ZIO_COMPRESS_OFF && 359 compress < ZIO_COMPRESS_FUNCTIONS); 360 361 zio = zio_create(pio, spa, txg, bp, data, size, done, private, 362 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER, 363 ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE); 364 365 zio->io_bookmark = *zb; 366 367 zio->io_logical = zio; 368 369 zio->io_checksum = checksum; 370 zio->io_compress = compress; 371 zio->io_ndvas = ncopies; 372 373 if (compress != ZIO_COMPRESS_OFF) 374 zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS; 375 376 if (bp->blk_birth != txg) { 377 /* XXX the bp usually (always?) gets re-zeroed later */ 378 BP_ZERO(bp); 379 BP_SET_LSIZE(bp, size); 380 BP_SET_PSIZE(bp, size); 381 } else { 382 /* Make sure someone doesn't change their mind on overwrites */ 383 ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp), 384 spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 385 } 386 387 return (zio); 388 } 389 390 zio_t * 391 zio_rewrite(zio_t *pio, spa_t *spa, int checksum, 392 uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 393 zio_done_func_t *done, void *private, int priority, int flags, 394 zbookmark_t *zb) 395 { 396 zio_t *zio; 397 398 zio = zio_create(pio, spa, txg, bp, data, size, done, private, 399 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER, 400 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 401 402 zio->io_bookmark = *zb; 403 zio->io_checksum = checksum; 404 zio->io_compress = ZIO_COMPRESS_OFF; 405 406 if (pio != NULL) 407 ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 408 409 return (zio); 410 } 411 412 static zio_t * 413 zio_write_allocate(zio_t *pio, spa_t *spa, int checksum, 414 uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 415 zio_done_func_t *done, void *private, int priority, int flags) 416 { 417 zio_t *zio; 418 419 BP_ZERO(bp); 420 BP_SET_LSIZE(bp, size); 421 BP_SET_PSIZE(bp, size); 422 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 423 424 zio = zio_create(pio, spa, txg, bp, data, size, done, private, 425 ZIO_TYPE_WRITE, priority, flags, 426 ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE); 427 428 zio->io_checksum = checksum; 429 zio->io_compress = ZIO_COMPRESS_OFF; 430 431 return (zio); 432 } 433 434 zio_t * 435 zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 436 zio_done_func_t *done, void *private) 437 { 438 zio_t *zio; 439 440 ASSERT(!BP_IS_HOLE(bp)); 441 442 if (txg == spa->spa_syncing_txg && 443 spa->spa_sync_pass > zio_sync_pass.zp_defer_free) { 444 bplist_enqueue_deferred(&spa->spa_sync_bplist, bp); 445 return (zio_null(pio, spa, NULL, NULL, 0)); 446 } 447 448 zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 449 ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, ZIO_FLAG_USER, 450 ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE); 451 452 zio->io_bp = &zio->io_bp_copy; 453 454 return (zio); 455 } 456 457 zio_t * 458 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 459 zio_done_func_t *done, void *private) 460 { 461 zio_t *zio; 462 463 /* 464 * A claim is an allocation of a specific block. Claims are needed 465 * to support immediate writes in the intent log. The issue is that 466 * immediate writes contain committed data, but in a txg that was 467 * *not* committed. Upon opening the pool after an unclean shutdown, 468 * the intent log claims all blocks that contain immediate write data 469 * so that the SPA knows they're in use. 470 * 471 * All claims *must* be resolved in the first txg -- before the SPA 472 * starts allocating blocks -- so that nothing is allocated twice. 473 */ 474 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa)); 475 ASSERT3U(spa_first_txg(spa), <=, txg); 476 477 zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 478 ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0, 479 ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 480 481 zio->io_bp = &zio->io_bp_copy; 482 483 return (zio); 484 } 485 486 zio_t * 487 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 488 zio_done_func_t *done, void *private, int priority, int flags) 489 { 490 zio_t *zio; 491 int c; 492 493 if (vd->vdev_children == 0) { 494 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 495 ZIO_TYPE_IOCTL, priority, flags, 496 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 497 498 zio->io_vd = vd; 499 zio->io_cmd = cmd; 500 } else { 501 zio = zio_null(pio, spa, NULL, NULL, flags); 502 503 for (c = 0; c < vd->vdev_children; c++) 504 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 505 done, private, priority, flags)); 506 } 507 508 return (zio); 509 } 510 511 static void 512 zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size, 513 int checksum) 514 { 515 ASSERT(vd->vdev_children == 0); 516 517 ASSERT(size <= SPA_MAXBLOCKSIZE); 518 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 519 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 520 521 ASSERT(offset + size <= VDEV_LABEL_START_SIZE || 522 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 523 ASSERT3U(offset + size, <=, vd->vdev_psize); 524 525 BP_ZERO(bp); 526 527 BP_SET_LSIZE(bp, size); 528 BP_SET_PSIZE(bp, size); 529 530 BP_SET_CHECKSUM(bp, checksum); 531 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 532 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 533 534 if (checksum != ZIO_CHECKSUM_OFF) 535 ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0); 536 } 537 538 zio_t * 539 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 540 void *data, int checksum, zio_done_func_t *done, void *private, 541 int priority, int flags) 542 { 543 zio_t *zio; 544 blkptr_t blk; 545 546 zio_phys_bp_init(vd, &blk, offset, size, checksum); 547 548 zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 549 ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, 550 ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 551 552 zio->io_vd = vd; 553 zio->io_offset = offset; 554 555 /* 556 * Work off our copy of the bp so the caller can free it. 557 */ 558 zio->io_bp = &zio->io_bp_copy; 559 560 return (zio); 561 } 562 563 zio_t * 564 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 565 void *data, int checksum, zio_done_func_t *done, void *private, 566 int priority, int flags) 567 { 568 zio_block_tail_t *zbt; 569 void *wbuf; 570 zio_t *zio; 571 blkptr_t blk; 572 573 zio_phys_bp_init(vd, &blk, offset, size, checksum); 574 575 zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 576 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, 577 ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 578 579 zio->io_vd = vd; 580 zio->io_offset = offset; 581 582 zio->io_bp = &zio->io_bp_copy; 583 zio->io_checksum = checksum; 584 585 if (zio_checksum_table[checksum].ci_zbt) { 586 /* 587 * zbt checksums are necessarily destructive -- they modify 588 * one word of the write buffer to hold the verifier/checksum. 589 * Therefore, we must make a local copy in case the data is 590 * being written to multiple places. 591 */ 592 wbuf = zio_buf_alloc(size); 593 bcopy(data, wbuf, size); 594 zio_push_transform(zio, wbuf, size, size); 595 596 zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1; 597 zbt->zbt_cksum = blk.blk_cksum; 598 } 599 600 return (zio); 601 } 602 603 /* 604 * Create a child I/O to do some work for us. It has no associated bp. 605 */ 606 zio_t * 607 zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 608 void *data, uint64_t size, int type, int priority, int flags, 609 zio_done_func_t *done, void *private) 610 { 611 uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE; 612 zio_t *cio; 613 614 if (type == ZIO_TYPE_READ && bp != NULL) { 615 /* 616 * If we have the bp, then the child should perform the 617 * checksum and the parent need not. This pushes error 618 * detection as close to the leaves as possible and 619 * eliminates redundant checksums in the interior nodes. 620 */ 621 pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY; 622 zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 623 } 624 625 cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size, 626 done, private, type, priority, 627 (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags, 628 ZIO_STAGE_VDEV_IO_START - 1, pipeline); 629 630 cio->io_vd = vd; 631 cio->io_offset = offset; 632 633 return (cio); 634 } 635 636 /* 637 * ========================================================================== 638 * Initiate I/O, either sync or async 639 * ========================================================================== 640 */ 641 int 642 zio_wait(zio_t *zio) 643 { 644 int error; 645 646 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 647 648 zio->io_waiter = curthread; 649 650 zio_next_stage_async(zio); 651 652 mutex_enter(&zio->io_lock); 653 while (zio->io_stalled != ZIO_STAGE_DONE) 654 cv_wait(&zio->io_cv, &zio->io_lock); 655 mutex_exit(&zio->io_lock); 656 657 error = zio->io_error; 658 mutex_destroy(&zio->io_lock); 659 kmem_free(zio, sizeof (zio_t)); 660 661 return (error); 662 } 663 664 void 665 zio_nowait(zio_t *zio) 666 { 667 zio_next_stage_async(zio); 668 } 669 670 /* 671 * ========================================================================== 672 * I/O pipeline interlocks: parent/child dependency scoreboarding 673 * ========================================================================== 674 */ 675 static void 676 zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp) 677 { 678 mutex_enter(&zio->io_lock); 679 if (*countp == 0) { 680 ASSERT(zio->io_stalled == 0); 681 mutex_exit(&zio->io_lock); 682 zio_next_stage(zio); 683 } else { 684 zio->io_stalled = stage; 685 mutex_exit(&zio->io_lock); 686 } 687 } 688 689 static void 690 zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp) 691 { 692 zio_t *pio = zio->io_parent; 693 694 mutex_enter(&pio->io_lock); 695 if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 696 pio->io_error = zio->io_error; 697 if (--*countp == 0 && pio->io_stalled == stage) { 698 pio->io_stalled = 0; 699 mutex_exit(&pio->io_lock); 700 zio_next_stage_async(pio); 701 } else { 702 mutex_exit(&pio->io_lock); 703 } 704 } 705 706 static void 707 zio_wait_children_ready(zio_t *zio) 708 { 709 zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 710 &zio->io_children_notready); 711 } 712 713 void 714 zio_wait_children_done(zio_t *zio) 715 { 716 zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 717 &zio->io_children_notdone); 718 } 719 720 static void 721 zio_ready(zio_t *zio) 722 { 723 zio_t *pio = zio->io_parent; 724 725 if (pio != NULL) 726 zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 727 &pio->io_children_notready); 728 729 if (zio->io_bp) 730 zio->io_bp_copy = *zio->io_bp; 731 732 zio_next_stage(zio); 733 } 734 735 static void 736 zio_done(zio_t *zio) 737 { 738 zio_t *pio = zio->io_parent; 739 spa_t *spa = zio->io_spa; 740 blkptr_t *bp = zio->io_bp; 741 vdev_t *vd = zio->io_vd; 742 char blkbuf[BP_SPRINTF_LEN]; 743 744 ASSERT(zio->io_children_notready == 0); 745 ASSERT(zio->io_children_notdone == 0); 746 747 if (bp != NULL) { 748 ASSERT(bp->blk_pad[0] == 0); 749 ASSERT(bp->blk_pad[1] == 0); 750 ASSERT(bp->blk_pad[2] == 0); 751 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0); 752 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 753 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 754 ASSERT(!BP_SHOULD_BYTESWAP(bp)); 755 if (zio->io_ndvas != 0) 756 ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 757 ASSERT(BP_COUNT_GANG(bp) == 0 || 758 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 759 } 760 } 761 762 if (vd != NULL) 763 vdev_stat_update(zio); 764 765 if (zio->io_error) { 766 /* 767 * If this I/O is attached to a particular vdev, 768 * generate an error message describing the I/O failure 769 * at the block level. We ignore these errors if the 770 * device is currently unavailable. 771 */ 772 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 773 zfs_ereport_post(FM_EREPORT_ZFS_IO, 774 zio->io_spa, vd, zio, 0, 0); 775 776 if ((zio->io_error == EIO || 777 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && 778 zio->io_logical == zio) { 779 /* 780 * For root I/O requests, tell the SPA to log the error 781 * appropriately. Also, generate a logical data 782 * ereport. 783 */ 784 spa_log_error(zio->io_spa, zio); 785 786 zfs_ereport_post(FM_EREPORT_ZFS_DATA, 787 zio->io_spa, NULL, zio, 0, 0); 788 } 789 790 /* 791 * For I/O requests that cannot fail, panic appropriately. 792 */ 793 if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) { 794 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, 795 bp ? bp : &zio->io_bp_copy); 796 panic("ZFS: %s (%s on %s off %llx: zio %p %s): error " 797 "%d", zio->io_error == ECKSUM ? 798 "bad checksum" : "I/O failure", 799 zio_type_name[zio->io_type], 800 vdev_description(vd), 801 (u_longlong_t)zio->io_offset, 802 zio, blkbuf, zio->io_error); 803 } 804 } 805 806 zio_clear_transform_stack(zio); 807 808 if (zio->io_done) 809 zio->io_done(zio); 810 811 ASSERT(zio->io_delegate_list == NULL); 812 ASSERT(zio->io_delegate_next == NULL); 813 814 if (pio != NULL) { 815 zio_t *next, *prev; 816 817 mutex_enter(&pio->io_lock); 818 next = zio->io_sibling_next; 819 prev = zio->io_sibling_prev; 820 if (next != NULL) 821 next->io_sibling_prev = prev; 822 if (prev != NULL) 823 prev->io_sibling_next = next; 824 if (pio->io_child == zio) 825 pio->io_child = next; 826 mutex_exit(&pio->io_lock); 827 828 zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 829 &pio->io_children_notdone); 830 } 831 832 if (pio == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_HELD)) 833 spa_config_exit(spa, zio); 834 835 if (zio->io_waiter != NULL) { 836 mutex_enter(&zio->io_lock); 837 ASSERT(zio->io_stage == ZIO_STAGE_DONE); 838 zio->io_stalled = zio->io_stage; 839 cv_broadcast(&zio->io_cv); 840 mutex_exit(&zio->io_lock); 841 } else { 842 kmem_free(zio, sizeof (zio_t)); 843 } 844 } 845 846 /* 847 * ========================================================================== 848 * Compression support 849 * ========================================================================== 850 */ 851 static void 852 zio_write_compress(zio_t *zio) 853 { 854 int compress = zio->io_compress; 855 blkptr_t *bp = zio->io_bp; 856 void *cbuf; 857 uint64_t lsize = zio->io_size; 858 uint64_t csize = lsize; 859 uint64_t cbufsize = 0; 860 int pass; 861 862 if (bp->blk_birth == zio->io_txg) { 863 /* 864 * We're rewriting an existing block, which means we're 865 * working on behalf of spa_sync(). For spa_sync() to 866 * converge, it must eventually be the case that we don't 867 * have to allocate new blocks. But compression changes 868 * the blocksize, which forces a reallocate, and makes 869 * convergence take longer. Therefore, after the first 870 * few passes, stop compressing to ensure convergence. 871 */ 872 pass = spa_sync_pass(zio->io_spa); 873 if (pass > zio_sync_pass.zp_dontcompress) 874 compress = ZIO_COMPRESS_OFF; 875 } else { 876 ASSERT(BP_IS_HOLE(bp)); 877 pass = 1; 878 } 879 880 if (compress != ZIO_COMPRESS_OFF) 881 if (!zio_compress_data(compress, zio->io_data, zio->io_size, 882 &cbuf, &csize, &cbufsize)) 883 compress = ZIO_COMPRESS_OFF; 884 885 if (compress != ZIO_COMPRESS_OFF && csize != 0) 886 zio_push_transform(zio, cbuf, csize, cbufsize); 887 888 /* 889 * The final pass of spa_sync() must be all rewrites, but the first 890 * few passes offer a trade-off: allocating blocks defers convergence, 891 * but newly allocated blocks are sequential, so they can be written 892 * to disk faster. Therefore, we allow the first few passes of 893 * spa_sync() to reallocate new blocks, but force rewrites after that. 894 * There should only be a handful of blocks after pass 1 in any case. 895 */ 896 if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize && 897 pass > zio_sync_pass.zp_rewrite) { 898 ASSERT(csize != 0); 899 BP_SET_LSIZE(bp, lsize); 900 BP_SET_COMPRESS(bp, compress); 901 zio->io_pipeline = ZIO_REWRITE_PIPELINE; 902 } else { 903 if (bp->blk_birth == zio->io_txg) { 904 ASSERT3U(BP_GET_LSIZE(bp), ==, lsize); 905 bzero(bp, sizeof (blkptr_t)); 906 } 907 if (csize == 0) { 908 BP_ZERO(bp); 909 zio->io_pipeline = ZIO_WAIT_FOR_CHILDREN_PIPELINE; 910 } else { 911 ASSERT3U(BP_GET_NDVAS(bp), ==, 0); 912 BP_SET_LSIZE(bp, lsize); 913 BP_SET_PSIZE(bp, csize); 914 BP_SET_COMPRESS(bp, compress); 915 zio->io_pipeline = ZIO_WRITE_ALLOCATE_PIPELINE; 916 } 917 } 918 919 zio_next_stage(zio); 920 } 921 922 static void 923 zio_read_decompress(zio_t *zio) 924 { 925 blkptr_t *bp = zio->io_bp; 926 void *data; 927 uint64_t size; 928 uint64_t bufsize; 929 int compress = BP_GET_COMPRESS(bp); 930 931 ASSERT(compress != ZIO_COMPRESS_OFF); 932 933 zio_pop_transform(zio, &data, &size, &bufsize); 934 935 if (zio_decompress_data(compress, data, size, 936 zio->io_data, zio->io_size)) 937 zio->io_error = EIO; 938 939 zio_buf_free(data, bufsize); 940 941 zio_next_stage(zio); 942 } 943 944 /* 945 * ========================================================================== 946 * Gang block support 947 * ========================================================================== 948 */ 949 static void 950 zio_gang_pipeline(zio_t *zio) 951 { 952 /* 953 * By default, the pipeline assumes that we're dealing with a gang 954 * block. If we're not, strip out any gang-specific stages. 955 */ 956 if (!BP_IS_GANG(zio->io_bp)) 957 zio->io_pipeline &= ~ZIO_GANG_STAGES; 958 959 zio_next_stage(zio); 960 } 961 962 static void 963 zio_gang_byteswap(zio_t *zio) 964 { 965 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 966 967 if (BP_SHOULD_BYTESWAP(zio->io_bp)) 968 byteswap_uint64_array(zio->io_data, zio->io_size); 969 } 970 971 static void 972 zio_get_gang_header(zio_t *zio) 973 { 974 blkptr_t *bp = zio->io_bp; 975 uint64_t gsize = SPA_GANGBLOCKSIZE; 976 void *gbuf = zio_buf_alloc(gsize); 977 978 ASSERT(BP_IS_GANG(bp)); 979 980 zio_push_transform(zio, gbuf, gsize, gsize); 981 982 zio_nowait(zio_create(zio, zio->io_spa, bp->blk_birth, bp, gbuf, gsize, 983 NULL, NULL, ZIO_TYPE_READ, zio->io_priority, 984 zio->io_flags & ZIO_FLAG_GANG_INHERIT, 985 ZIO_STAGE_OPEN, ZIO_READ_PIPELINE)); 986 987 zio_wait_children_done(zio); 988 } 989 990 static void 991 zio_read_gang_members(zio_t *zio) 992 { 993 zio_gbh_phys_t *gbh; 994 uint64_t gsize, gbufsize, loff, lsize; 995 int i; 996 997 ASSERT(BP_IS_GANG(zio->io_bp)); 998 999 zio_gang_byteswap(zio); 1000 zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1001 1002 for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) { 1003 blkptr_t *gbp = &gbh->zg_blkptr[i]; 1004 lsize = BP_GET_PSIZE(gbp); 1005 1006 ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF); 1007 ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp)); 1008 ASSERT3U(loff + lsize, <=, zio->io_size); 1009 ASSERT(i < SPA_GBH_NBLKPTRS); 1010 ASSERT(!BP_IS_HOLE(gbp)); 1011 1012 zio_nowait(zio_read(zio, zio->io_spa, gbp, 1013 (char *)zio->io_data + loff, lsize, NULL, NULL, 1014 zio->io_priority, zio->io_flags & ZIO_FLAG_GANG_INHERIT, 1015 &zio->io_bookmark)); 1016 } 1017 1018 zio_buf_free(gbh, gbufsize); 1019 zio_wait_children_done(zio); 1020 } 1021 1022 static void 1023 zio_rewrite_gang_members(zio_t *zio) 1024 { 1025 zio_gbh_phys_t *gbh; 1026 uint64_t gsize, gbufsize, loff, lsize; 1027 int i; 1028 1029 ASSERT(BP_IS_GANG(zio->io_bp)); 1030 ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 1031 1032 zio_gang_byteswap(zio); 1033 zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1034 1035 ASSERT(gsize == gbufsize); 1036 1037 for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) { 1038 blkptr_t *gbp = &gbh->zg_blkptr[i]; 1039 lsize = BP_GET_PSIZE(gbp); 1040 1041 ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF); 1042 ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp)); 1043 ASSERT3U(loff + lsize, <=, zio->io_size); 1044 ASSERT(i < SPA_GBH_NBLKPTRS); 1045 ASSERT(!BP_IS_HOLE(gbp)); 1046 1047 zio_nowait(zio_rewrite(zio, zio->io_spa, zio->io_checksum, 1048 zio->io_txg, gbp, (char *)zio->io_data + loff, lsize, 1049 NULL, NULL, zio->io_priority, zio->io_flags, 1050 &zio->io_bookmark)); 1051 } 1052 1053 zio_push_transform(zio, gbh, gsize, gbufsize); 1054 zio_wait_children_ready(zio); 1055 } 1056 1057 static void 1058 zio_free_gang_members(zio_t *zio) 1059 { 1060 zio_gbh_phys_t *gbh; 1061 uint64_t gsize, gbufsize; 1062 int i; 1063 1064 ASSERT(BP_IS_GANG(zio->io_bp)); 1065 1066 zio_gang_byteswap(zio); 1067 zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1068 1069 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1070 blkptr_t *gbp = &gbh->zg_blkptr[i]; 1071 1072 if (BP_IS_HOLE(gbp)) 1073 continue; 1074 zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg, 1075 gbp, NULL, NULL)); 1076 } 1077 1078 zio_buf_free(gbh, gbufsize); 1079 zio_next_stage(zio); 1080 } 1081 1082 static void 1083 zio_claim_gang_members(zio_t *zio) 1084 { 1085 zio_gbh_phys_t *gbh; 1086 uint64_t gsize, gbufsize; 1087 int i; 1088 1089 ASSERT(BP_IS_GANG(zio->io_bp)); 1090 1091 zio_gang_byteswap(zio); 1092 zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1093 1094 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1095 blkptr_t *gbp = &gbh->zg_blkptr[i]; 1096 if (BP_IS_HOLE(gbp)) 1097 continue; 1098 zio_nowait(zio_claim(zio, zio->io_spa, zio->io_txg, 1099 gbp, NULL, NULL)); 1100 } 1101 1102 zio_buf_free(gbh, gbufsize); 1103 zio_next_stage(zio); 1104 } 1105 1106 static void 1107 zio_write_allocate_gang_member_done(zio_t *zio) 1108 { 1109 zio_t *pio = zio->io_parent; 1110 dva_t *cdva = zio->io_bp->blk_dva; 1111 dva_t *pdva = pio->io_bp->blk_dva; 1112 uint64_t asize; 1113 int d; 1114 1115 ASSERT3U(pio->io_ndvas, ==, zio->io_ndvas); 1116 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 1117 ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(zio->io_bp)); 1118 ASSERT3U(pio->io_ndvas, <=, BP_GET_NDVAS(pio->io_bp)); 1119 1120 mutex_enter(&pio->io_lock); 1121 for (d = 0; d < BP_GET_NDVAS(pio->io_bp); d++) { 1122 ASSERT(DVA_GET_GANG(&pdva[d])); 1123 asize = DVA_GET_ASIZE(&pdva[d]); 1124 asize += DVA_GET_ASIZE(&cdva[d]); 1125 DVA_SET_ASIZE(&pdva[d], asize); 1126 } 1127 mutex_exit(&pio->io_lock); 1128 } 1129 1130 static void 1131 zio_write_allocate_gang_members(zio_t *zio) 1132 { 1133 blkptr_t *bp = zio->io_bp; 1134 dva_t *dva = bp->blk_dva; 1135 spa_t *spa = zio->io_spa; 1136 zio_gbh_phys_t *gbh; 1137 uint64_t txg = zio->io_txg; 1138 uint64_t resid = zio->io_size; 1139 uint64_t maxalloc = P2ROUNDUP(zio->io_size >> 1, SPA_MINBLOCKSIZE); 1140 uint64_t gsize, loff, lsize; 1141 uint32_t gbps_left; 1142 int ndvas = zio->io_ndvas; 1143 int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa)); 1144 int error; 1145 int i, d; 1146 1147 gsize = SPA_GANGBLOCKSIZE; 1148 gbps_left = SPA_GBH_NBLKPTRS; 1149 1150 error = metaslab_alloc(spa, gsize, bp, gbh_ndvas, txg, NULL, B_FALSE); 1151 if (error == ENOSPC) 1152 panic("can't allocate gang block header"); 1153 ASSERT(error == 0); 1154 1155 for (d = 0; d < gbh_ndvas; d++) 1156 DVA_SET_GANG(&dva[d], 1); 1157 1158 bp->blk_birth = txg; 1159 1160 gbh = zio_buf_alloc(gsize); 1161 bzero(gbh, gsize); 1162 1163 /* We need to test multi-level gang blocks */ 1164 if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0) 1165 maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE); 1166 1167 for (loff = 0, i = 0; loff != zio->io_size; 1168 loff += lsize, resid -= lsize, gbps_left--, i++) { 1169 blkptr_t *gbp = &gbh->zg_blkptr[i]; 1170 dva = gbp->blk_dva; 1171 1172 ASSERT(gbps_left != 0); 1173 maxalloc = MIN(maxalloc, resid); 1174 1175 while (resid <= maxalloc * gbps_left) { 1176 error = metaslab_alloc(spa, maxalloc, gbp, ndvas, 1177 txg, bp, B_FALSE); 1178 if (error == 0) 1179 break; 1180 ASSERT3U(error, ==, ENOSPC); 1181 if (maxalloc == SPA_MINBLOCKSIZE) 1182 panic("really out of space"); 1183 maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE); 1184 } 1185 1186 if (resid <= maxalloc * gbps_left) { 1187 lsize = maxalloc; 1188 BP_SET_LSIZE(gbp, lsize); 1189 BP_SET_PSIZE(gbp, lsize); 1190 BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF); 1191 gbp->blk_birth = txg; 1192 zio_nowait(zio_rewrite(zio, spa, 1193 zio->io_checksum, txg, gbp, 1194 (char *)zio->io_data + loff, lsize, 1195 zio_write_allocate_gang_member_done, NULL, 1196 zio->io_priority, zio->io_flags, 1197 &zio->io_bookmark)); 1198 } else { 1199 lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE); 1200 ASSERT(lsize != SPA_MINBLOCKSIZE); 1201 zio_nowait(zio_write_allocate(zio, spa, 1202 zio->io_checksum, txg, gbp, 1203 (char *)zio->io_data + loff, lsize, 1204 zio_write_allocate_gang_member_done, NULL, 1205 zio->io_priority, zio->io_flags)); 1206 } 1207 } 1208 1209 ASSERT(resid == 0 && loff == zio->io_size); 1210 1211 zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE; 1212 1213 zio_push_transform(zio, gbh, gsize, gsize); 1214 /* 1215 * As much as we'd like this to be zio_wait_children_ready(), 1216 * updating our ASIZE doesn't happen until the io_done callback, 1217 * so we have to wait for that to finish in order for our BP 1218 * to be stable. 1219 */ 1220 zio_wait_children_done(zio); 1221 } 1222 1223 /* 1224 * ========================================================================== 1225 * Allocate and free blocks 1226 * ========================================================================== 1227 */ 1228 static void 1229 zio_dva_allocate(zio_t *zio) 1230 { 1231 blkptr_t *bp = zio->io_bp; 1232 int error; 1233 1234 ASSERT(BP_IS_HOLE(bp)); 1235 ASSERT3U(BP_GET_NDVAS(bp), ==, 0); 1236 ASSERT3U(zio->io_ndvas, >, 0); 1237 ASSERT3U(zio->io_ndvas, <=, spa_max_replication(zio->io_spa)); 1238 1239 /* For testing, make some blocks above a certain size be gang blocks */ 1240 if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) { 1241 zio_write_allocate_gang_members(zio); 1242 return; 1243 } 1244 1245 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 1246 1247 error = metaslab_alloc(zio->io_spa, zio->io_size, bp, zio->io_ndvas, 1248 zio->io_txg, NULL, B_FALSE); 1249 1250 if (error == 0) { 1251 bp->blk_birth = zio->io_txg; 1252 } else if (error == ENOSPC) { 1253 if (zio->io_size == SPA_MINBLOCKSIZE) 1254 panic("really, truly out of space"); 1255 zio_write_allocate_gang_members(zio); 1256 return; 1257 } else { 1258 zio->io_error = error; 1259 } 1260 zio_next_stage(zio); 1261 } 1262 1263 static void 1264 zio_dva_free(zio_t *zio) 1265 { 1266 blkptr_t *bp = zio->io_bp; 1267 1268 metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE); 1269 1270 BP_ZERO(bp); 1271 1272 zio_next_stage(zio); 1273 } 1274 1275 static void 1276 zio_dva_claim(zio_t *zio) 1277 { 1278 zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 1279 1280 zio_next_stage(zio); 1281 } 1282 1283 /* 1284 * ========================================================================== 1285 * Read and write to physical devices 1286 * ========================================================================== 1287 */ 1288 1289 static void 1290 zio_vdev_io_start(zio_t *zio) 1291 { 1292 vdev_t *vd = zio->io_vd; 1293 vdev_t *tvd = vd ? vd->vdev_top : NULL; 1294 blkptr_t *bp = zio->io_bp; 1295 uint64_t align; 1296 1297 if (vd == NULL) { 1298 /* The mirror_ops handle multiple DVAs in a single BP */ 1299 vdev_mirror_ops.vdev_op_io_start(zio); 1300 return; 1301 } 1302 1303 align = 1ULL << tvd->vdev_ashift; 1304 1305 if (zio->io_retries == 0 && vd == tvd) 1306 zio->io_flags |= ZIO_FLAG_FAILFAST; 1307 1308 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 1309 vd->vdev_children == 0) { 1310 zio->io_flags |= ZIO_FLAG_PHYSICAL; 1311 zio->io_offset += VDEV_LABEL_START_SIZE; 1312 } 1313 1314 if (P2PHASE(zio->io_size, align) != 0) { 1315 uint64_t asize = P2ROUNDUP(zio->io_size, align); 1316 char *abuf = zio_buf_alloc(asize); 1317 ASSERT(vd == tvd); 1318 if (zio->io_type == ZIO_TYPE_WRITE) { 1319 bcopy(zio->io_data, abuf, zio->io_size); 1320 bzero(abuf + zio->io_size, asize - zio->io_size); 1321 } 1322 zio_push_transform(zio, abuf, asize, asize); 1323 ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK)); 1324 zio->io_flags |= ZIO_FLAG_SUBBLOCK; 1325 } 1326 1327 ASSERT(P2PHASE(zio->io_offset, align) == 0); 1328 ASSERT(P2PHASE(zio->io_size, align) == 0); 1329 ASSERT(bp == NULL || 1330 P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size); 1331 ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE)); 1332 1333 vdev_io_start(zio); 1334 1335 /* zio_next_stage_async() gets called from io completion interrupt */ 1336 } 1337 1338 static void 1339 zio_vdev_io_done(zio_t *zio) 1340 { 1341 if (zio->io_vd == NULL) 1342 /* The mirror_ops handle multiple DVAs in a single BP */ 1343 vdev_mirror_ops.vdev_op_io_done(zio); 1344 else 1345 vdev_io_done(zio); 1346 } 1347 1348 /* XXPOLICY */ 1349 boolean_t 1350 zio_should_retry(zio_t *zio) 1351 { 1352 vdev_t *vd = zio->io_vd; 1353 1354 if (zio->io_error == 0) 1355 return (B_FALSE); 1356 if (zio->io_delegate_list != NULL) 1357 return (B_FALSE); 1358 if (vd && vd != vd->vdev_top) 1359 return (B_FALSE); 1360 if (zio->io_flags & ZIO_FLAG_DONT_RETRY) 1361 return (B_FALSE); 1362 if (zio->io_retries > 0) 1363 return (B_FALSE); 1364 1365 return (B_TRUE); 1366 } 1367 1368 static void 1369 zio_vdev_io_assess(zio_t *zio) 1370 { 1371 vdev_t *vd = zio->io_vd; 1372 vdev_t *tvd = vd ? vd->vdev_top : NULL; 1373 1374 ASSERT(zio->io_vsd == NULL); 1375 1376 if (zio->io_flags & ZIO_FLAG_SUBBLOCK) { 1377 void *abuf; 1378 uint64_t asize; 1379 ASSERT(vd == tvd); 1380 zio_pop_transform(zio, &abuf, &asize, &asize); 1381 if (zio->io_type == ZIO_TYPE_READ) 1382 bcopy(abuf, zio->io_data, zio->io_size); 1383 zio_buf_free(abuf, asize); 1384 zio->io_flags &= ~ZIO_FLAG_SUBBLOCK; 1385 } 1386 1387 if (zio_injection_enabled && !zio->io_error) 1388 zio->io_error = zio_handle_fault_injection(zio, EIO); 1389 1390 /* 1391 * If the I/O failed, determine whether we should attempt to retry it. 1392 */ 1393 /* XXPOLICY */ 1394 if (zio_should_retry(zio)) { 1395 ASSERT(tvd == vd); 1396 1397 zio->io_retries++; 1398 zio->io_error = 0; 1399 zio->io_flags &= ZIO_FLAG_VDEV_INHERIT; 1400 /* XXPOLICY */ 1401 zio->io_flags &= ~ZIO_FLAG_FAILFAST; 1402 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1403 zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1; 1404 1405 dprintf("retry #%d for %s to %s offset %llx\n", 1406 zio->io_retries, zio_type_name[zio->io_type], 1407 vdev_description(vd), zio->io_offset); 1408 1409 zio_next_stage_async(zio); 1410 return; 1411 } 1412 1413 if (zio->io_error != 0 && zio->io_error != ECKSUM && 1414 !(zio->io_flags & ZIO_FLAG_SPECULATIVE) && vd) { 1415 /* 1416 * Poor man's hotplug support. Even if we're done retrying this 1417 * I/O, try to reopen the vdev to see if it's still attached. 1418 * To avoid excessive thrashing, we only try it once a minute. 1419 * This also has the effect of detecting when missing devices 1420 * have come back, by polling the device once a minute. 1421 * 1422 * We need to do this asynchronously because we can't grab 1423 * all the necessary locks way down here. 1424 */ 1425 if (gethrtime() - vd->vdev_last_try > 60ULL * NANOSEC) { 1426 vd->vdev_last_try = gethrtime(); 1427 tvd->vdev_reopen_wanted = 1; 1428 spa_async_request(vd->vdev_spa, SPA_ASYNC_REOPEN); 1429 } 1430 } 1431 1432 zio_next_stage(zio); 1433 } 1434 1435 void 1436 zio_vdev_io_reissue(zio_t *zio) 1437 { 1438 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 1439 ASSERT(zio->io_error == 0); 1440 1441 zio->io_stage--; 1442 } 1443 1444 void 1445 zio_vdev_io_redone(zio_t *zio) 1446 { 1447 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 1448 1449 zio->io_stage--; 1450 } 1451 1452 void 1453 zio_vdev_io_bypass(zio_t *zio) 1454 { 1455 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 1456 ASSERT(zio->io_error == 0); 1457 1458 zio->io_flags |= ZIO_FLAG_IO_BYPASS; 1459 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1; 1460 } 1461 1462 /* 1463 * ========================================================================== 1464 * Generate and verify checksums 1465 * ========================================================================== 1466 */ 1467 static void 1468 zio_checksum_generate(zio_t *zio) 1469 { 1470 int checksum = zio->io_checksum; 1471 blkptr_t *bp = zio->io_bp; 1472 1473 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 1474 1475 BP_SET_CHECKSUM(bp, checksum); 1476 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1477 1478 zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size); 1479 1480 zio_next_stage(zio); 1481 } 1482 1483 static void 1484 zio_gang_checksum_generate(zio_t *zio) 1485 { 1486 zio_cksum_t zc; 1487 zio_gbh_phys_t *gbh = zio->io_data; 1488 1489 ASSERT(BP_IS_GANG(zio->io_bp)); 1490 ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 1491 1492 zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum); 1493 1494 zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size); 1495 1496 zio_next_stage(zio); 1497 } 1498 1499 static void 1500 zio_checksum_verify(zio_t *zio) 1501 { 1502 if (zio->io_bp != NULL) { 1503 zio->io_error = zio_checksum_error(zio); 1504 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) 1505 zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM, 1506 zio->io_spa, zio->io_vd, zio, 0, 0); 1507 } 1508 1509 zio_next_stage(zio); 1510 } 1511 1512 /* 1513 * Called by RAID-Z to ensure we don't compute the checksum twice. 1514 */ 1515 void 1516 zio_checksum_verified(zio_t *zio) 1517 { 1518 zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 1519 } 1520 1521 /* 1522 * Set the external verifier for a gang block based on stuff in the bp 1523 */ 1524 void 1525 zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp) 1526 { 1527 blkptr_t *bp = zio->io_bp; 1528 1529 zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp)); 1530 zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp)); 1531 zcp->zc_word[2] = bp->blk_birth; 1532 zcp->zc_word[3] = 0; 1533 } 1534 1535 /* 1536 * ========================================================================== 1537 * Define the pipeline 1538 * ========================================================================== 1539 */ 1540 typedef void zio_pipe_stage_t(zio_t *zio); 1541 1542 static void 1543 zio_badop(zio_t *zio) 1544 { 1545 panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio); 1546 } 1547 1548 zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = { 1549 zio_badop, 1550 zio_wait_children_ready, 1551 zio_write_compress, 1552 zio_checksum_generate, 1553 zio_gang_pipeline, 1554 zio_get_gang_header, 1555 zio_rewrite_gang_members, 1556 zio_free_gang_members, 1557 zio_claim_gang_members, 1558 zio_dva_allocate, 1559 zio_dva_free, 1560 zio_dva_claim, 1561 zio_gang_checksum_generate, 1562 zio_ready, 1563 zio_vdev_io_start, 1564 zio_vdev_io_done, 1565 zio_vdev_io_assess, 1566 zio_wait_children_done, 1567 zio_checksum_verify, 1568 zio_read_gang_members, 1569 zio_read_decompress, 1570 zio_done, 1571 zio_badop 1572 }; 1573 1574 /* 1575 * Move an I/O to the next stage of the pipeline and execute that stage. 1576 * There's no locking on io_stage because there's no legitimate way for 1577 * multiple threads to be attempting to process the same I/O. 1578 */ 1579 void 1580 zio_next_stage(zio_t *zio) 1581 { 1582 uint32_t pipeline = zio->io_pipeline; 1583 1584 ASSERT(!MUTEX_HELD(&zio->io_lock)); 1585 1586 if (zio->io_error) { 1587 dprintf("zio %p vdev %s offset %llx stage %d error %d\n", 1588 zio, vdev_description(zio->io_vd), 1589 zio->io_offset, zio->io_stage, zio->io_error); 1590 if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0) 1591 pipeline &= ZIO_ERROR_PIPELINE_MASK; 1592 } 1593 1594 while (((1U << ++zio->io_stage) & pipeline) == 0) 1595 continue; 1596 1597 ASSERT(zio->io_stage <= ZIO_STAGE_DONE); 1598 ASSERT(zio->io_stalled == 0); 1599 1600 zio_pipeline[zio->io_stage](zio); 1601 } 1602 1603 void 1604 zio_next_stage_async(zio_t *zio) 1605 { 1606 taskq_t *tq; 1607 uint32_t pipeline = zio->io_pipeline; 1608 1609 ASSERT(!MUTEX_HELD(&zio->io_lock)); 1610 1611 if (zio->io_error) { 1612 dprintf("zio %p vdev %s offset %llx stage %d error %d\n", 1613 zio, vdev_description(zio->io_vd), 1614 zio->io_offset, zio->io_stage, zio->io_error); 1615 if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0) 1616 pipeline &= ZIO_ERROR_PIPELINE_MASK; 1617 } 1618 1619 while (((1U << ++zio->io_stage) & pipeline) == 0) 1620 continue; 1621 1622 ASSERT(zio->io_stage <= ZIO_STAGE_DONE); 1623 ASSERT(zio->io_stalled == 0); 1624 1625 /* 1626 * For performance, we'll probably want two sets of task queues: 1627 * per-CPU issue taskqs and per-CPU completion taskqs. The per-CPU 1628 * part is for read performance: since we have to make a pass over 1629 * the data to checksum it anyway, we want to do this on the same CPU 1630 * that issued the read, because (assuming CPU scheduling affinity) 1631 * that thread is probably still there. Getting this optimization 1632 * right avoids performance-hostile cache-to-cache transfers. 1633 * 1634 * Note that having two sets of task queues is also necessary for 1635 * correctness: if all of the issue threads get bogged down waiting 1636 * for dependent reads (e.g. metaslab freelist) to complete, then 1637 * there won't be any threads available to service I/O completion 1638 * interrupts. 1639 */ 1640 if ((1U << zio->io_stage) & zio->io_async_stages) { 1641 if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE) 1642 tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type]; 1643 else 1644 tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type]; 1645 (void) taskq_dispatch(tq, 1646 (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP); 1647 } else { 1648 zio_pipeline[zio->io_stage](zio); 1649 } 1650 } 1651 1652 /* 1653 * Try to allocate an intent log block. Return 0 on success, errno on failure. 1654 */ 1655 int 1656 zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp, 1657 uint64_t txg) 1658 { 1659 int error; 1660 1661 spa_config_enter(spa, RW_READER, FTAG); 1662 1663 /* 1664 * We were passed the previous log blocks dva_t in bp->blk_dva[0]. 1665 */ 1666 error = metaslab_alloc(spa, size, new_bp, 1, txg, old_bp, B_TRUE); 1667 1668 if (error == 0) { 1669 BP_SET_LSIZE(new_bp, size); 1670 BP_SET_PSIZE(new_bp, size); 1671 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 1672 BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG); 1673 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 1674 BP_SET_LEVEL(new_bp, 0); 1675 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 1676 new_bp->blk_birth = txg; 1677 } 1678 1679 spa_config_exit(spa, FTAG); 1680 1681 return (error); 1682 } 1683 1684 /* 1685 * Free an intent log block. We know it can't be a gang block, so there's 1686 * nothing to do except metaslab_free() it. 1687 */ 1688 void 1689 zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg) 1690 { 1691 ASSERT(!BP_IS_GANG(bp)); 1692 1693 spa_config_enter(spa, RW_READER, FTAG); 1694 1695 metaslab_free(spa, bp, txg, B_FALSE); 1696 1697 spa_config_exit(spa, FTAG); 1698 } 1699